oc-flight-deck 0.6.0 → 0.6.1

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
@@ -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` | 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. |
57
+ | `tps` | Current speed: output tokens in the last 60 seconds, divided by that window, subagents included. Idle longer than the window and it hides — the `status` row already says `idle`. On a host that exposes no per-message timestamps it falls back to the lifetime average, which reads lower than a peak per-turn rate. Fixed at 60 s, not configurable. |
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. |
@@ -82,9 +82,10 @@
82
82
  // context a gauge of how full the context window is
83
83
  // perms what is waiting for approval, not just how many
84
84
  // elapsed wall-clock time since the session started
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)
85
+ // tps current speed: output tokens in the last 60 seconds,
86
+ // subagents included. Hides while idle longer than the
87
+ // window; falls back to the session's lifetime average on
88
+ // hosts with no per-message timestamps
88
89
  // spark recent turn sizes as a sparkline
89
90
  // reasoning reasoning tokens, when the model emits them
90
91
  // 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.6.0",
3
+ "version": "0.6.1",
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",
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, sessionThroughput, type StatSource } from "./stats.js";
7
+ import { ANIMATED_FIELDS, sessionThroughput, TPS_WINDOW_MS, windowedThroughput, type StatSource, type ThroughputSample } from "./stats.js";
8
8
  import { startGuardBridge } from "./guard.js";
9
9
  import { startTicker } from "./ticker.js";
10
10
 
@@ -31,6 +31,16 @@ const RECENT_MESSAGES = 4;
31
31
 
32
32
  type SessionLike = { cost?: unknown; model?: unknown; time?: unknown; tokens?: unknown };
33
33
 
34
+ /** What one session's message list contributes to a throughput window. */
35
+ interface TpsScan {
36
+ /** False only when the host refused to list the session's messages. */
37
+ readonly ok: boolean;
38
+ /** Assistant messages seen, whether or not they carried a timestamp. */
39
+ readonly assistant: number;
40
+ /** Samples that carried a usable timestamp. */
41
+ readonly samples: readonly ThroughputSample[];
42
+ }
43
+
34
44
  function asCount(value: unknown): number | undefined {
35
45
  return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined;
36
46
  }
@@ -325,16 +335,65 @@ export default Plugin.define({
325
335
  return status === undefined ? undefined : false;
326
336
  };
327
337
 
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
+ // Overall session throughput, from two sources chosen by host capability and
339
+ // never by the moment. A host that stamps its messages gets a trailing-window
340
+ // rate — output tokens over the last minute, subagents summed in — so the
341
+ // figure reflects current speed rather than a whole-conversation average. A
342
+ // host proven to expose no message timestamps uses the lifetime average.
343
+ //
344
+ // The capability is learned once and remembered for the process. `undefined`
345
+ // means "not yet known": a read that failed, or carried no assistant turns,
346
+ // tells us nothing and must not choose a metric. Once known it is sticky in
347
+ // BOTH directions, so a transient `message.list` failure can never flip a
348
+ // stamped host back to the lifetime average (nor an unstamped host off it) —
349
+ // a failure just hides the row. One value for the whole host, deliberately
350
+ // not keyed by session.
351
+ //
352
+ // The rail re-runs on the fast tick, and walking every family message for
353
+ // every session would repeat the same work fifty times a second, so the
354
+ // result is held briefly. `undefined` is cached too: an idle window must not
355
+ // be re-derived on each tick either.
356
+ const TPS_CACHE_MS = 1_000;
357
+ let hostStamps: boolean | undefined;
358
+ let tpsCache:
359
+ | { readonly sessionID: string; readonly at: number; readonly value: number | undefined }
360
+ | undefined;
361
+
362
+ const scanMessages = (id: string): TpsScan => {
363
+ let messages: readonly unknown[];
364
+ try {
365
+ messages = context.data.session.message.list(id) ?? [];
366
+ } catch {
367
+ // A refused read is not "this host has no messages".
368
+ return { ok: false, assistant: 0, samples: [] };
369
+ }
370
+ const samples: ThroughputSample[] = [];
371
+ let assistant = 0;
372
+ for (const entry of messages) {
373
+ const message = asRecord(entry);
374
+ if (message?.["type"] !== "assistant") continue;
375
+ assistant += 1;
376
+ const stamp = asRecord(message["time"]);
377
+ const at =
378
+ asCount(stamp?.["completed"]) ?? asCount(stamp?.["streamed"]) ?? asCount(stamp?.["created"]);
379
+ // "Stamped" depends only on the timestamp, never on the output count: a
380
+ // batch of zero-output assistant turns still proves the host stamps.
381
+ if (at === undefined) continue;
382
+ samples.push({ tokens: asRecord(message["tokens"])?.["output"], at });
383
+ }
384
+ return { ok: true, assistant, samples };
385
+ };
386
+
387
+ // The lifetime average, the metric of last resort for a host that exposes no
388
+ // per-message timestamps. Measured from the session's own clock
389
+ // (`time.updated`, falling back to now) so a finished session settles on a
390
+ // stable figure instead of decaying as it sits on screen.
391
+ const lifetimeTps = (
392
+ sessionID: string,
393
+ session: SessionLike | undefined,
394
+ created: number,
395
+ time: Record<string, unknown> | undefined,
396
+ ): number | undefined => {
338
397
  const updated = asCount(time?.["updated"]);
339
398
  const end = updated === undefined ? Date.now() : Math.min(Date.now(), updated);
340
399
  const elapsed = end - created;
@@ -366,6 +425,63 @@ export default Plugin.define({
366
425
  return sessionThroughput(output, elapsed);
367
426
  };
368
427
 
428
+ const computeTps = (sessionID: string, session: SessionLike | undefined): number | undefined => {
429
+ const time = asRecord(session?.time);
430
+ const created = asCount(time?.["created"]);
431
+ if (created === undefined) return undefined;
432
+
433
+ // A host proven unstamped needs no message read at all: its metric is the
434
+ // lifetime average whether or not the read would have succeeded.
435
+ if (hostStamps === false) return lifetimeTps(sessionID, session, created, time);
436
+
437
+ // Timestamped assistant output this session has produced, plus — only from
438
+ // a family root — every subagent session's. A child asking `family()` gets
439
+ // its ancestors and siblings back, so it keeps its own samples.
440
+ const scans: TpsScan[] = [scanMessages(sessionID)];
441
+ if (isFamilyRoot(sessionID)) {
442
+ try {
443
+ for (const id of context.data.session.family(sessionID) ?? []) {
444
+ if (id !== sessionID) scans.push(scanMessages(id));
445
+ }
446
+ } catch {
447
+ // No family on this host: the session's own samples still stand.
448
+ }
449
+ }
450
+
451
+ const samples: ThroughputSample[] = [];
452
+ let assistant = 0;
453
+ let readOk = false;
454
+ for (const scan of scans) {
455
+ samples.push(...scan.samples);
456
+ assistant += scan.assistant;
457
+ readOk = readOk || scan.ok;
458
+ }
459
+
460
+ if (hostStamps === undefined) {
461
+ // A stamped assistant turn proves the host stamps. Assistant turns with
462
+ // no timestamp prove it does not. Neither, and the capability stays
463
+ // unknown so the row hides rather than guessing a metric.
464
+ if (samples.length > 0) hostStamps = true;
465
+ else if (readOk && assistant > 0) hostStamps = false;
466
+ else return undefined;
467
+ }
468
+
469
+ if (hostStamps === true) {
470
+ return windowedThroughput(samples, Date.now(), TPS_WINDOW_MS, created);
471
+ }
472
+ return lifetimeTps(sessionID, session, created, time);
473
+ };
474
+
475
+ const sessionTps = (sessionID: string, session: SessionLike | undefined): number | undefined => {
476
+ const cached = tpsCache;
477
+ if (cached !== undefined && cached.sessionID === sessionID && Date.now() - cached.at < TPS_CACHE_MS) {
478
+ return cached.value;
479
+ }
480
+ const value = computeTps(sessionID, session);
481
+ tpsCache = { sessionID, at: Date.now(), value };
482
+ return value;
483
+ };
484
+
369
485
  // Recent turn sizes, oldest first. Drawn from messages the host already
370
486
  // holds, so the sparkline needs no history of our own to accumulate.
371
487
  const sparkValues = (sessionID: string): readonly number[] => {
package/src/tui/stats.ts CHANGED
@@ -54,9 +54,10 @@ export interface StatSource {
54
54
  readonly busy?: unknown;
55
55
  readonly perms?: unknown;
56
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.
57
+ * Current session throughput. On a host that stamps its messages this is a
58
+ * trailing-window rate over the last minute, subagents summed in; on a host
59
+ * without message timestamps it falls back to the lifetime average. Either
60
+ * way it is derived from state the host already holds.
60
61
  */
61
62
  readonly tps?: unknown;
62
63
  readonly elapsedMs?: unknown;
@@ -259,6 +260,68 @@ export function sessionThroughput(outputTokens: unknown, elapsedMs: unknown): nu
259
260
  return output / (Math.max(elapsed, 1_000) / 1_000);
260
261
  }
261
262
 
263
+ /** Length of the trailing window, in milliseconds, that `windowedThroughput` measures. */
264
+ export const TPS_WINDOW_MS = 60_000;
265
+
266
+ /**
267
+ * One assistant turn's contribution to a throughput window.
268
+ *
269
+ * Both fields are untrusted: `tokens` is the turn's output count and `at` is the
270
+ * timestamp it finished at. Anything unusable is skipped rather than guessed at.
271
+ */
272
+ export interface ThroughputSample {
273
+ readonly tokens?: unknown;
274
+ readonly at?: unknown;
275
+ }
276
+
277
+ /**
278
+ * A trailing-window throughput rate: output tokens per second over the last
279
+ * `windowMs`, measured from `nowMs`.
280
+ *
281
+ * The denominator is how much of the window the session has actually lived, so
282
+ * a just-started session ramps up against its real age instead of being divided
283
+ * by a full minute it has not run — floored at one second, the same floor the
284
+ * lifetime average uses, so a sliver of time cannot flash an absurd rate. Once
285
+ * the session is older than the window, it settles into a clean rolling minute.
286
+ *
287
+ * Returns `undefined` — the caller hides the row — unless at least one sample
288
+ * carries a usable timestamp inside the window and a positive output count. An
289
+ * idle session therefore reports no rate rather than a decaying one. The window
290
+ * is inclusive at both ends (`start <= at <= now`) so a boundary sample counts.
291
+ *
292
+ * Pure and dependency-free: no clock, no I/O, no rendering.
293
+ */
294
+ export function windowedThroughput(
295
+ samples: readonly ThroughputSample[],
296
+ nowMs: number,
297
+ windowMs: number,
298
+ sessionCreatedMs: number,
299
+ ): number | undefined {
300
+ if (!Number.isFinite(nowMs)) return undefined;
301
+ if (!Number.isFinite(windowMs) || windowMs <= 0) return undefined;
302
+
303
+ // An unusable creation time cannot tell us how long the session has lived, so
304
+ // the whole window is assumed available rather than dropping the rate.
305
+ const created = Number.isFinite(sessionCreatedMs) ? sessionCreatedMs : undefined;
306
+ const alive = created === undefined ? windowMs : nowMs - created;
307
+ const denominatorMs = Math.max(1_000, Math.min(windowMs, alive));
308
+
309
+ const start = nowMs - windowMs;
310
+ let output = 0;
311
+ let sawOne = false;
312
+ for (const sample of samples) {
313
+ const at = asCount(sample.at);
314
+ if (at === undefined || at < start || at > nowMs) continue;
315
+ const tokens = asCount(sample.tokens);
316
+ if (tokens === undefined || tokens <= 0) continue;
317
+ output += tokens;
318
+ sawOne = true;
319
+ }
320
+
321
+ if (!sawOne) return undefined;
322
+ return output / (denominatorMs / 1_000);
323
+ }
324
+
262
325
  /** `45s`, `14m`, `2h 14m`. */
263
326
  export function formatDuration(ms: number): string {
264
327
  const seconds = Math.floor(ms / 1_000);