oc-flight-deck 0.6.2 → 0.6.3

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.
@@ -0,0 +1,85 @@
1
+ // Session throughput: output tokens per second, as a lifetime average or as a
2
+ // trailing-window rate.
3
+ //
4
+ // Pure: no clock, no I/O, no rendering. Untrusted sample fields are coerced
5
+ // through ./coerce.js so a garbage value is skipped rather than guessed at.
6
+
7
+ import { asCount } from "./coerce.js";
8
+
9
+ /**
10
+ * Overall session throughput: output tokens over the session's lifetime.
11
+ *
12
+ * Unlike a per-turn rate, this is the whole conversation's average, so it
13
+ * includes every subagent and every pause. A just-started session is floored
14
+ * at one second so it cannot divide by a sliver of time and flash an absurd
15
+ * rate. Returns `undefined` when there is nothing to divide.
16
+ */
17
+ export function sessionThroughput(outputTokens: unknown, elapsedMs: unknown): number | undefined {
18
+ const output = asCount(outputTokens);
19
+ const elapsed = asCount(elapsedMs);
20
+ if (output === undefined || output <= 0) return undefined;
21
+ if (elapsed === undefined || elapsed <= 0) return undefined;
22
+ return output / (Math.max(elapsed, 1_000) / 1_000);
23
+ }
24
+
25
+ /** Length of the trailing window, in milliseconds, that `windowedThroughput` measures. */
26
+ export const TPS_WINDOW_MS = 60_000;
27
+
28
+ /**
29
+ * One assistant turn's contribution to a throughput window.
30
+ *
31
+ * Both fields are untrusted: `tokens` is the turn's output count and `at` is the
32
+ * timestamp it finished at. Anything unusable is skipped rather than guessed at.
33
+ */
34
+ export interface ThroughputSample {
35
+ readonly tokens?: unknown;
36
+ readonly at?: unknown;
37
+ }
38
+
39
+ /**
40
+ * A trailing-window throughput rate: output tokens per second over the last
41
+ * `windowMs`, measured from `nowMs`.
42
+ *
43
+ * The denominator is how much of the window the session has actually lived, so
44
+ * a just-started session ramps up against its real age instead of being divided
45
+ * by a full minute it has not run — floored at one second, the same floor the
46
+ * lifetime average uses, so a sliver of time cannot flash an absurd rate. Once
47
+ * the session is older than the window, it settles into a clean rolling minute.
48
+ *
49
+ * Returns `undefined` — the caller hides the row — unless at least one sample
50
+ * carries a usable timestamp inside the window and a positive output count. An
51
+ * idle session therefore reports no rate rather than a decaying one. The window
52
+ * is inclusive at both ends (`start <= at <= now`) so a boundary sample counts.
53
+ *
54
+ * Pure: no clock, no I/O, no rendering.
55
+ */
56
+ export function windowedThroughput(
57
+ samples: readonly ThroughputSample[],
58
+ nowMs: number,
59
+ windowMs: number,
60
+ sessionCreatedMs: number,
61
+ ): number | undefined {
62
+ if (!Number.isFinite(nowMs)) return undefined;
63
+ if (!Number.isFinite(windowMs) || windowMs <= 0) return undefined;
64
+
65
+ // An unusable creation time cannot tell us how long the session has lived, so
66
+ // the whole window is assumed available rather than dropping the rate.
67
+ const created = Number.isFinite(sessionCreatedMs) ? sessionCreatedMs : undefined;
68
+ const alive = created === undefined ? windowMs : nowMs - created;
69
+ const denominatorMs = Math.max(1_000, Math.min(windowMs, alive));
70
+
71
+ const start = nowMs - windowMs;
72
+ let output = 0;
73
+ let sawOne = false;
74
+ for (const sample of samples) {
75
+ const at = asCount(sample.at);
76
+ if (at === undefined || at < start || at > nowMs) continue;
77
+ const tokens = asCount(sample.tokens);
78
+ if (tokens === undefined || tokens <= 0) continue;
79
+ output += tokens;
80
+ sawOne = true;
81
+ }
82
+
83
+ if (!sawOne) return undefined;
84
+ return output / (denominatorMs / 1_000);
85
+ }
package/src/tui/ticker.ts CHANGED
@@ -54,7 +54,7 @@ export interface TickerHost {
54
54
  };
55
55
  }
56
56
 
57
- export interface Ticker {
57
+ interface Ticker {
58
58
  /**
59
59
  * Current frame, read from the host's store.
60
60
  *
@@ -78,7 +78,7 @@ export const TICKER_KEY = "flight-deck.frame";
78
78
  * instead of ten times. The timer itself keeps firing (cheap); it is the store
79
79
  * write, which wakes the host's reactive graph, that is skipped.
80
80
  */
81
- export const IDLE_REFRESH_MS = 1_000;
81
+ const IDLE_REFRESH_MS = 1_000;
82
82
 
83
83
  // Untrusted store values are normalised here rather than at the render, so a
84
84
  // corrupt frame can never reach the spinner's array index.