oc-flight-deck 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -22,7 +22,7 @@ tokens 533k in · 91k out
22
22
  cache 98% hit · 32M read
23
23
  context ██░░░░░░░░ 18%
24
24
  elapsed 2h 14m
25
- tps 106 tok/s
25
+ tps 18 tok/s
26
26
  spark ▁▃▂▅█▄▃▂
27
27
  ```
28
28
 
@@ -54,7 +54,7 @@ learn. The panel appears beside an open session and starts reading.
54
54
  | `context` | A gauge of how full the window is. |
55
55
  | `perms` | What is waiting for approval, not just how many. |
56
56
  | `elapsed` | How long you've been at it. |
57
- | `tps` | Output tokens per second, measured from the last completed turn. |
57
+ | `tps` | Overall session throughput: every output token over the session's life, subagents included. A whole-conversation average, so it reads lower than a peak rate. |
58
58
  | `spark` | Recent turn sizes as a shape. Available, off by default. |
59
59
  | `reasoning` | Reasoning tokens, when the model emits them. Available, off by default. |
60
60
  | `turns` | How many prompts you have sent this session. Available, off by default. |
@@ -183,7 +183,9 @@ all you get exactly the defaults written out below.
183
183
  ```jsonc
184
184
  {
185
185
  // Tick rate in ms. The spinner has ten frames, so 100 turns it once a
186
- // second. 0 turns the timer off entirely.
186
+ // second. 0 turns the timer off entirely. While nothing is running the
187
+ // tick backs off to about once a second — the spinner is hidden then, and
188
+ // the clock only reads in whole seconds.
187
189
  "refresh": 100,
188
190
 
189
191
  "sidebar": {
@@ -282,7 +284,7 @@ Built on the official
282
284
 
283
285
  ## Compatibility
284
286
 
285
- Built against `@opencode/plugin` `2.0.2`; pin a host version you've tested.
287
+ Built against `@opencode/plugin` `2.0.10`; pin a host version you've tested.
286
288
 
287
289
  Requires OpenCode V2 (`opencode2`). Building from source needs Bun 1.4+ or
288
290
  Node 22+.
@@ -39,6 +39,10 @@
39
39
  // `elapsed` plus the spinner are the reason this exists. Without a tick they
40
40
  // would sit frozen between turns.
41
41
  //
42
+ // While nothing is running the tick backs off to about once a second: the
43
+ // spinner is hidden then, and `elapsed` only reads in whole seconds, so the
44
+ // host's renderer is woken a tenth as often for the same picture.
45
+ //
42
46
  // 100 default — the spinner turns once a second, the clock stays live
43
47
  // 50 twice as lively, still negligible work per tick
44
48
  // 1000 a calm wall-clock tick, but the spinner visibly stutters
@@ -78,7 +82,9 @@
78
82
  // context a gauge of how full the context window is
79
83
  // perms what is waiting for approval, not just how many
80
84
  // elapsed wall-clock time since the session started
81
- // tps output tokens per second on the last completed turn
85
+ // tps overall session throughput: output tokens over the
86
+ // session's life, subagents included (an average, so it
87
+ // reads lower than a peak rate)
82
88
  // spark recent turn sizes as a sparkline
83
89
  // reasoning reasoning tokens, when the model emits them
84
90
  // turns how many prompts you have sent this session
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oc-flight-deck",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Live OpenCode V2 session telemetry — agent, model, branch, cost, tokens, cache hit rate and context usage in your sidebar.",
5
5
  "keywords": [
6
6
  "opencode",
@@ -53,7 +53,7 @@
53
53
  "access": "public"
54
54
  },
55
55
  "dependencies": {
56
- "@opencode/plugin": "2.0.2"
56
+ "@opencode/plugin": "2.0.10"
57
57
  },
58
58
  "devDependencies": {
59
59
  "@opentui/core": "0.5.10",
package/src/tui/index.tsx CHANGED
@@ -4,7 +4,7 @@ import { cautionDetail, cautionText, detectCautions, worstCaution, type Caution
4
4
  import { cautionThresholds, mergeOptions, resolveConfig } from "./config.js";
5
5
  import { loadConfigFile } from "./file-config.js";
6
6
  import { footerLine, liveRowOffset, sidebarLines } from "./presentation.js";
7
- import { ANIMATED_FIELDS, type StatSource } from "./stats.js";
7
+ import { ANIMATED_FIELDS, sessionThroughput, type StatSource } from "./stats.js";
8
8
  import { startGuardBridge } from "./guard.js";
9
9
  import { startTicker } from "./ticker.js";
10
10
 
@@ -29,7 +29,7 @@ import { startTicker } from "./ticker.js";
29
29
  /** How many trailing messages are inspected for tool parts. */
30
30
  const RECENT_MESSAGES = 4;
31
31
 
32
- type SessionLike = { cost?: unknown; model?: unknown; time?: unknown };
32
+ type SessionLike = { cost?: unknown; model?: unknown; time?: unknown; tokens?: unknown };
33
33
 
34
34
  function asCount(value: unknown): number | undefined {
35
35
  return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined;
@@ -53,6 +53,12 @@ export default Plugin.define({
53
53
  const file = loadConfigFile();
54
54
  const { config, issues } = resolveConfig(mergeOptions(file.options, context.options));
55
55
 
56
+ // Only the rows actually on the rail are worth deriving: the rail re-runs on
57
+ // every tick, and a figure nobody asked to see should not cost a walk of the
58
+ // session database or the model catalog.
59
+ const rows = config.sidebar.enabled ? new Set(config.sidebar.rows) : new Set<string>();
60
+ const wants = (name: string): boolean => rows.has(name);
61
+
56
62
  if (file.issue !== undefined && file.source !== undefined) {
57
63
  console.warn(`[flight-deck] ${file.issue}`);
58
64
  }
@@ -177,10 +183,23 @@ export default Plugin.define({
177
183
  //
178
184
  // If the host does not report a project id, the unfiltered list is used, so
179
185
  // this degrades to the old behaviour instead of showing nothing.
186
+ // `project` walks every session the host knows about, and that collection grows
187
+ // with your history rather than staying a handful. It moves slowly, so the walk
188
+ // is held briefly instead of repeated on every tick.
189
+ const PROJECT_CACHE_MS = 5_000;
190
+ let projectCache:
191
+ | { readonly projectID: string | undefined; readonly at: number; readonly value: { cost: number; count: number } }
192
+ | undefined;
193
+
180
194
  const projectTotals = (sessionID: string) => {
181
195
  try {
182
- const sessions = context.data.session.list() ?? [];
183
196
  const projectID = asText(asRecord(context.data.session.get(sessionID))?.["projectID"]);
197
+ const cached = projectCache;
198
+ if (cached !== undefined && cached.projectID === projectID && Date.now() - cached.at < PROJECT_CACHE_MS) {
199
+ return cached.value;
200
+ }
201
+
202
+ const sessions = context.data.session.list() ?? [];
184
203
  const scoped =
185
204
  projectID === undefined
186
205
  ? sessions
@@ -188,7 +207,9 @@ export default Plugin.define({
188
207
 
189
208
  let cost = 0;
190
209
  for (const entry of scoped) cost += asCount(asRecord(entry)?.["cost"]) ?? 0;
191
- return { cost, count: scoped.length };
210
+ const value = { cost, count: scoped.length };
211
+ projectCache = { projectID, at: Date.now(), value };
212
+ return value;
192
213
  } catch {
193
214
  return undefined;
194
215
  }
@@ -216,12 +237,16 @@ export default Plugin.define({
216
237
  // The host's own shell records, which outlive the tool call: a backgrounded
217
238
  // command returns its result immediately, so its part settles while the
218
239
  // process keeps running. This is the only signal that survives that.
240
+ //
241
+ // Shells are listed per LOCATION, not per session, so the session is matched
242
+ // on the record's own `metadata.sessionID` — the documented shape, rather
243
+ // than a session-scoped accessor the published API does not carry.
219
244
  const shellsOf = (sessionID: string): readonly unknown[] => {
220
245
  try {
221
- const shellApi = context.data.shell as unknown as
222
- | { listBySession?: (id: string) => readonly unknown[] }
223
- | undefined;
224
- return shellApi?.listBySession?.(sessionID) ?? [];
246
+ const shells = context.data.shell.list(locationOf()) ?? [];
247
+ return shells.filter(
248
+ (entry) => asText(asRecord(asRecord(entry)?.["metadata"])?.["sessionID"]) === sessionID,
249
+ );
225
250
  } catch {
226
251
  return [];
227
252
  }
@@ -292,38 +317,53 @@ export default Plugin.define({
292
317
  }
293
318
 
294
319
  try {
295
- // `listBySession` is on the reactive client the host hands the plugin,
296
- // but not on every published type surface, so it is reached defensively:
297
- // a host without it still falls back to the session's own status.
298
- const shellApi = context.data.shell as unknown as
299
- | { listBySession?: (id: string) => readonly unknown[] }
300
- | undefined;
301
- const shells = shellApi?.listBySession?.(sessionID);
302
- if (shells !== undefined && shells.some((entry) => asRecord(entry)?.status === "running")) return true;
320
+ if (shellsOf(sessionID).some((entry) => asRecord(entry)?.["status"] === "running")) return true;
303
321
  } catch {
304
- // Same again: unreadable shells just mean no shell signal.
322
+ // Unreadable shells just mean no shell signal.
305
323
  }
306
324
 
307
325
  return status === undefined ? undefined : false;
308
326
  };
309
327
 
310
- // Throughput of the last completed turn. The host records when streaming
311
- // finished, so this is measured rather than estimated from wall-clock.
312
- const lastTps = (sessionID: string): number | undefined => {
313
- const messages = messagesOf(sessionID);
314
- for (let index = messages.length - 1; index >= 0; index -= 1) {
315
- const message = asRecord(messages[index]);
316
- const tokens = asRecord(message?.tokens);
317
- const output = asCount(tokens?.output);
318
- const time = asRecord(message?.time);
319
- const created = asCount(time?.created);
320
- const streamed = asCount(time?.streamed);
321
- if (output === undefined || created === undefined || streamed === undefined) continue;
322
- const duration = streamed - created;
323
- if (duration <= 0) continue;
324
- return output / (duration / 1_000);
328
+ // Overall session throughput: every output token the conversation has
329
+ // produced, divided by how long the session has been alive. Subagents run as
330
+ // separate sessions, so their output is summed in — this is the whole
331
+ // session's rate, not the main chat's last turn. Measured from the session's
332
+ // own clock (`time.updated`, falling back to now) so a finished session
333
+ // settles on a stable figure instead of decaying as it sits on screen.
334
+ const sessionTps = (sessionID: string, session: SessionLike | undefined): number | undefined => {
335
+ const time = asRecord(session?.time);
336
+ const created = asCount(time?.["created"]);
337
+ if (created === undefined) return undefined;
338
+ const updated = asCount(time?.["updated"]);
339
+ const end = updated === undefined ? Date.now() : Math.min(Date.now(), updated);
340
+ const elapsed = end - created;
341
+ if (elapsed <= 0) return undefined;
342
+
343
+ const own = asCount(asRecord(session?.tokens)?.["output"]);
344
+ let output = own;
345
+ // Only a family root owns the tree beneath it; a child asking `family()`
346
+ // gets its ancestors and siblings too, so it keeps its own figure.
347
+ if (isFamilyRoot(sessionID)) {
348
+ try {
349
+ const ids = context.data.session.family(sessionID) ?? [];
350
+ if (ids.length > 1) {
351
+ let total = 0;
352
+ let sawOne = false;
353
+ for (const id of ids) {
354
+ const member = context.data.session.get(id) as SessionLike | undefined;
355
+ const out = asCount(asRecord(member?.tokens)?.["output"]);
356
+ if (out === undefined) continue;
357
+ total += out;
358
+ sawOne = true;
359
+ }
360
+ if (sawOne) output = total;
361
+ }
362
+ } catch {
363
+ // No family on this host: the session's own tokens still stand.
364
+ }
325
365
  }
326
- return undefined;
366
+ return sessionThroughput(output, elapsed);
327
367
  };
328
368
 
329
369
  // Recent turn sizes, oldest first. Drawn from messages the host already
@@ -349,7 +389,8 @@ export default Plugin.define({
349
389
  const animated =
350
390
  config.sidebar.enabled &&
351
391
  config.sidebar.rows.some((name) => (ANIMATED_FIELDS as readonly string[]).includes(name));
352
- const ticker = startTicker(context, animated ? config.refresh : 0);
392
+ let spinnerNeeded = false;
393
+ const ticker = startTicker(context, animated ? config.refresh : 0, () => spinnerNeeded);
353
394
 
354
395
  // The guard row is opt-in and polls its own RPC: starting the bridge only
355
396
  // when the row is on the rail means no timer and no request for a row
@@ -397,24 +438,31 @@ export default Plugin.define({
397
438
  const snapshot = (sessionID: string): StatSource => {
398
439
  const location = locationOf();
399
440
  const session = context.data.session.get(sessionID) as SessionLike | undefined;
441
+ const isBusy = busy(sessionID);
442
+ // The fast tick exists for the spinner, and the spinner is only drawn while
443
+ // something is moving. Everything else on the rail reads in seconds.
444
+ spinnerNeeded = wants("status") && isBusy === true;
445
+
400
446
  // A "turn" is one prompt and the work it caused. The host's message list
401
447
  // also carries system, shell and switch records, so counting records
402
448
  // reports several times the turns actually taken.
403
- const turns = messagesOf(sessionID).filter((entry) => asRecord(entry)?.["type"] === "user").length;
449
+ const turns = wants("turns")
450
+ ? messagesOf(sessionID).filter((entry) => asRecord(entry)?.["type"] === "user").length
451
+ : undefined;
404
452
 
405
453
  return {
406
454
  ...session,
407
- caution: announce(sessionID),
408
- branch: branchOf(location),
409
- tree: treeTotals(sessionID, session?.cost),
410
- context: contextUsage(sessionID, session?.model),
411
- project: projectTotals(sessionID),
412
- status: statusOf(sessionID),
413
- busy: busy(sessionID),
414
- perms: permsOf(sessionID),
415
- tps: lastTps(sessionID),
416
- spark: sparkValues(sessionID),
417
- elapsedMs: sessionElapsed(session),
455
+ caution: wants("caution") ? announce(sessionID) : undefined,
456
+ branch: wants("branch") ? branchOf(location) : undefined,
457
+ tree: wants("cost") || wants("total") ? treeTotals(sessionID, session?.cost) : undefined,
458
+ context: wants("context") ? contextUsage(sessionID, session?.model) : undefined,
459
+ project: wants("project") ? projectTotals(sessionID) : undefined,
460
+ status: wants("status") ? statusOf(sessionID) : undefined,
461
+ busy: isBusy,
462
+ perms: wants("perms") ? permsOf(sessionID) : undefined,
463
+ tps: wants("tps") ? sessionTps(sessionID, session) : undefined,
464
+ spark: wants("spark") ? sparkValues(sessionID) : undefined,
465
+ elapsedMs: wants("elapsed") ? sessionElapsed(session) : undefined,
418
466
  turns,
419
467
  guard: bridge?.status,
420
468
  // Read the tick inside the render so the host registers a dependency on
package/src/tui/stats.ts CHANGED
@@ -53,6 +53,11 @@ export interface StatSource {
53
53
  /** True when anything is working: this session, a subagent, or a shell. */
54
54
  readonly busy?: unknown;
55
55
  readonly perms?: unknown;
56
+ /**
57
+ * Overall session throughput: output tokens over the session's lifetime,
58
+ * with subagents summed in. A whole-conversation average, so it reads lower
59
+ * than a peak per-turn rate.
60
+ */
56
61
  readonly tps?: unknown;
57
62
  readonly elapsedMs?: unknown;
58
63
  readonly turns?: unknown;
@@ -238,6 +243,22 @@ export function formatCost(value: number): string {
238
243
  return `$${value.toFixed(value < 1 ? 3 : 2)}`;
239
244
  }
240
245
 
246
+ /**
247
+ * Overall session throughput: output tokens over the session's lifetime.
248
+ *
249
+ * Unlike a per-turn rate, this is the whole conversation's average, so it
250
+ * includes every subagent and every pause. A just-started session is floored
251
+ * at one second so it cannot divide by a sliver of time and flash an absurd
252
+ * rate. Returns `undefined` when there is nothing to divide.
253
+ */
254
+ export function sessionThroughput(outputTokens: unknown, elapsedMs: unknown): number | undefined {
255
+ const output = asCount(outputTokens);
256
+ const elapsed = asCount(elapsedMs);
257
+ if (output === undefined || output <= 0) return undefined;
258
+ if (elapsed === undefined || elapsed <= 0) return undefined;
259
+ return output / (Math.max(elapsed, 1_000) / 1_000);
260
+ }
261
+
241
262
  /** `45s`, `14m`, `2h 14m`. */
242
263
  export function formatDuration(ms: number): string {
243
264
  const seconds = Math.floor(ms / 1_000);
package/src/tui/ticker.ts CHANGED
@@ -20,6 +20,11 @@
20
20
  // this plugin, gone when the TUI exits. Not the durable store, not a session,
21
21
  // not a file, not the network.
22
22
  //
23
+ // The tick serves the spinner first, but the spinner is only drawn while work
24
+ // is happening. When nothing is moving the store is written about once a second
25
+ // instead of on every interval — see `IDLE_REFRESH_MS` — so the host's reactive
26
+ // graph is woken a tenth as often for a picture that has not changed.
27
+ //
23
28
  // The host is a beta and its store is reached across a version boundary, so
24
29
  // everything about it is validated at runtime and every failure path degrades to
25
30
  // "no ticker" rather than raising into the TUI.
@@ -64,6 +69,17 @@ export interface Ticker {
64
69
  /** Namespaced so it cannot collide with another plugin's memory keys. */
65
70
  export const TICKER_KEY = "flight-deck.frame";
66
71
 
72
+ /**
73
+ * How often the frame still advances while nothing is moving.
74
+ *
75
+ * The spinner needs ~10 frames a second, but it is only drawn while work is
76
+ * happening. When idle, only `elapsed` and the annunciator still need a tick,
77
+ * and both read in whole seconds — so the host renderer is woken once a second
78
+ * instead of ten times. The timer itself keeps firing (cheap); it is the store
79
+ * write, which wakes the host's reactive graph, that is skipped.
80
+ */
81
+ export const IDLE_REFRESH_MS = 1_000;
82
+
67
83
  // Untrusted store values are normalised here rather than at the render, so a
68
84
  // corrupt frame can never reach the spinner's array index.
69
85
  function readFrame(store: FrameStore | undefined): number {
@@ -96,7 +112,11 @@ function openStore(storage: NonNullable<TickerHost["storage"]>): { store: FrameS
96
112
  * on host events, which is exactly how it behaved before the ticker was fixed.
97
113
  * Degrading quietly keeps a cosmetic panel from ever breaking the TUI.
98
114
  */
99
- export function startTicker(host: TickerHost | undefined, intervalMs: number): Ticker | undefined {
115
+ export function startTicker(
116
+ host: TickerHost | undefined,
117
+ intervalMs: number,
118
+ isBusy: () => boolean = () => true,
119
+ ): Ticker | undefined {
100
120
  if (!Number.isFinite(intervalMs) || intervalMs <= 0) return undefined;
101
121
 
102
122
  const storage = host?.storage;
@@ -105,7 +125,23 @@ export function startTicker(host: TickerHost | undefined, intervalMs: number): T
105
125
  const opened = openStore(storage);
106
126
  if (opened === undefined) return undefined;
107
127
 
128
+ const idleEvery = Math.max(1, Math.round(IDLE_REFRESH_MS / intervalMs));
129
+ let ticks = 0;
130
+
108
131
  let timer: ReturnType<typeof setInterval> | undefined = setInterval(() => {
132
+ ticks += 1;
133
+
134
+ // A broken busy predicate must never freeze the spinner, so it degrades to
135
+ // "busy" rather than propagating.
136
+ let busy = true;
137
+ try {
138
+ busy = isBusy();
139
+ } catch {
140
+ busy = true;
141
+ }
142
+ // While idle, skip the store write on all but every `idleEvery`th tick.
143
+ if (!busy && ticks % idleEvery !== 0) return;
144
+
109
145
  try {
110
146
  opened.update((draft) => {
111
147
  draft.frame = readFrame(draft) + 1;