oc-flight-deck 0.12.0 → 0.19.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.
@@ -8,9 +8,9 @@
8
8
  // Coercion of untrusted values happens through ./coerce.js.
9
9
 
10
10
  import type { Plugin } from "@opencode/plugin/tui";
11
- import { createActiveElapsed } from "./active-elapsed.js";
11
+ import { createActiveTime } from "./session-elapsed.js";
12
12
  import { asCount, asRecord, asText, type SessionLike } from "./coerce.js";
13
- import { turnKey, turnSpan, unionSpanMs, type ThroughputSpan } from "./stats.js";
13
+ import { turnKey, type ThroughputSpan } from "./stats.js";
14
14
 
15
15
  /**
16
16
  * The location the rail reads shell records and VCS from: the host's current
@@ -25,7 +25,7 @@ export function locationOf(context: Plugin.Context) {
25
25
  }
26
26
  }
27
27
 
28
- export function createSessionReads(context: Plugin.Context, maxBankedMs?: number) {
28
+ export function createSessionReads(context: Plugin.Context) {
29
29
  /** How many trailing messages are inspected for tool parts. */
30
30
  const RECENT_MESSAGES = 4;
31
31
 
@@ -55,7 +55,14 @@ export function createSessionReads(context: Plugin.Context, maxBankedMs?: number
55
55
  };
56
56
 
57
57
  // Subagent sessions are separate sessions, and the parent's `cost` does not
58
- // include them, so a bare `cost` row understates a swarm. Sum the family.
58
+ // include them, so a bare `cost` row understates a swarm. Sum the family —
59
+ // and the same scope for `tokens`: the money row already shows the family,
60
+ // so the token rows beside it must share it, or one row's swarm is another
61
+ // row's session. The parent's own tokens come from its session record (its
62
+ // own cost arrives via `ownCost`, already in the caller's hand), every
63
+ // child's from theirs: `input`, `output`, `reasoning`, and both `cache`
64
+ // rungs, so a caller that uses this INSTEAD of the session's own record
65
+ // gets the full family figure.
59
66
  const treeTotals = (sessionID: string, ownCost: unknown) => {
60
67
  if (!isFamilyRoot(sessionID)) return undefined;
61
68
 
@@ -65,7 +72,24 @@ export function createSessionReads(context: Plugin.Context, maxBankedMs?: number
65
72
  } catch {
66
73
  return undefined;
67
74
  }
68
- if (ids.length <= 1) return undefined;
75
+
76
+ let own: SessionLike | undefined;
77
+ try {
78
+ own = context.data.session.get(sessionID) as SessionLike | undefined;
79
+ } catch {
80
+ own = undefined;
81
+ }
82
+ const tokens = { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } };
83
+ const addTokens = (source: SessionLike | undefined): void => {
84
+ const record = asRecord(source?.tokens);
85
+ tokens.input += asCount(record?.["input"]) ?? 0;
86
+ tokens.output += asCount(record?.["output"]) ?? 0;
87
+ tokens.reasoning += asCount(record?.["reasoning"]) ?? 0;
88
+ const cache = asRecord(record?.["cache"]);
89
+ tokens.cache.read += asCount(cache?.["read"]) ?? 0;
90
+ tokens.cache.write += asCount(cache?.["write"]) ?? 0;
91
+ };
92
+ addTokens(own);
69
93
 
70
94
  let cost = asCount(ownCost) ?? 0;
71
95
  let count = 0;
@@ -81,8 +105,15 @@ export function createSessionReads(context: Plugin.Context, maxBankedMs?: number
81
105
  if (child === undefined) continue;
82
106
  count += 1;
83
107
  cost += asCount(child.cost) ?? 0;
108
+ addTokens(child);
84
109
  }
85
- return count === 0 ? undefined : { cost, count };
110
+ // The family figure, subagents included. A session that spawned none still
111
+ // records one: its own cost against a count of zero, so the `total` row has
112
+ // a real number to draw instead of blanking at `$0.000`. `undefined` is now
113
+ // reserved for a figure that genuinely cannot be computed — a non-root
114
+ // session (its family is its parent's), or a host read that threw.
115
+ if (ownCost === undefined && count === 0) return undefined;
116
+ return { cost, count, tokens };
86
117
  };
87
118
 
88
119
  // A message's own totals are per-request, so the last assistant message's
@@ -226,53 +257,39 @@ export function createSessionReads(context: Plugin.Context, maxBankedMs?: number
226
257
  return status === undefined ? undefined : false;
227
258
  };
228
259
 
229
- // Accumulated active wall time: the clock runs only while `busy()` says the
230
- // session, its family, or its shells is working, and freezes otherwise. The
231
- // `now` parameter keeps the transition observation deterministic for tests.
232
- //
233
- // The seed gives that same clock the session's already-recorded work so a
234
- // restart does not reset the row to `—`. It scans exactly the scope `busy()`
235
- // consults — the displayed session plus every id `family()` returns — and
236
- // takes the merged union of those assistant turns' spans, tokens ignored: a
237
- // zero-output turn still took wall time. The scan is memoised one result per
238
- // session id, and every read degrades to zero rather than throwing, so an
239
- // The scan is memoised one result per session id, and every read degrades to zero rather than throwing, so an
240
- // absent family or an unstamped host leaves today's behaviour untouched. The
241
- // memo holds at most `MAX_SEEDS` entries: without eviction a TUI lifetime of
242
- // session-hopping grows it forever, and an evicted revisit simply re-scans
243
- // the host's own timestamps, so nothing is lost.
244
- const MAX_SEEDS = 50;
245
- const seeds = new Map<string, number>();
246
- const rememberSeed = (sessionID: string, seed: number): void => {
247
- if (seeds.has(sessionID)) seeds.delete(sessionID);
248
- else if (seeds.size >= MAX_SEEDS) {
249
- for (const oldest of seeds.keys()) {
250
- seeds.delete(oldest);
251
- break;
252
- }
253
- }
254
- seeds.set(sessionID, seed);
255
- };
256
- const seedElapsed = (sessionID: string): number => {
257
- const cached = seeds.get(sessionID);
258
- if (cached !== undefined) {
259
- seeds.delete(sessionID);
260
- seeds.set(sessionID, cached);
261
- return cached;
262
- }
263
-
264
- // One `now` for the whole scan, so every in-flight turn ends at the same
265
- // instant and the union cannot be skewed by the reads drifting apart.
266
- const now = Date.now();
267
- const ids = new Set<string>([sessionID]);
268
- try {
269
- for (const id of context.data.session.family(sessionID) ?? []) ids.add(id);
270
- } catch {
271
- // No family on this host: the session's own turns still stand.
272
- }
273
-
274
- let seed = 0;
260
+ // Active time over the session's whole lifetime, derived from the host's own
261
+ // timestamps rather than accumulated in this process: the figure is the
262
+ // merged union of the assistant turns' spans across exactly the scope
263
+ // `busy()` consults — the displayed session plus every id `family()`
264
+ // returns — with an uncompleted turn ending at `now` while the scope is
265
+ // genuinely working, and collapsing to its last recorded stamp when nothing
266
+ // is working. So a restart recomputes the same figure instead of resetting,
267
+ // a climbing turn keeps climbing while it is in flight, a settled scope
268
+ // freezes, and a stalled or abandoned turn bills no idle time. Tokens are
269
+ // ignored: a zero-output turn still took wall time. One `now` for the whole
270
+ // scan, so every in-flight turn ends at the same instant and the union
271
+ // cannot be skewed by the reads drifting apart. Every read degrades to
272
+ // empty rather than throwing, so an absent family or an unstamped host hides
273
+ // the row instead of breaking the rail. Monotonicity, caching and the
274
+ // per-session bound live in ./session-elapsed.js; this is only the host read.
275
+ const spansOf = (sessionID: string, now: number): readonly ThroughputSpan[] => {
275
276
  try {
277
+ // Whether the scope is genuinely working, read once for the whole scan:
278
+ // every uncompleted turn in it shares the same end rule, and the scope
279
+ // is exactly what `busy()` already consults. A read that throws counts
280
+ // as not busy.
281
+ let live = false;
282
+ try {
283
+ live = busy(sessionID) === true;
284
+ } catch {
285
+ live = false;
286
+ }
287
+ const ids = new Set<string>([sessionID]);
288
+ try {
289
+ for (const id of context.data.session.family(sessionID) ?? []) ids.add(id);
290
+ } catch {
291
+ // No family on this host: the session's own turns still stand.
292
+ }
276
293
  const spans: ThroughputSpan[] = [];
277
294
  for (const id of ids) {
278
295
  for (const entry of messagesOf(id)) {
@@ -280,33 +297,48 @@ export function createSessionReads(context: Plugin.Context, maxBankedMs?: number
280
297
  if (message?.["type"] !== "assistant") continue;
281
298
  const stamp = asRecord(message["time"]);
282
299
  const created = asCount(stamp?.["created"]);
300
+ if (created === undefined) continue;
283
301
  const completed = asCount(stamp?.["completed"]);
284
302
  const streamed = asCount(stamp?.["streamed"]);
285
- // `elapsed` ends a settled turn at `completed`: the clock stays busy
286
- // through the turn's tool settlement, which `streamed` would drop.
287
- const span = turnSpan(created, completed, streamed, now, "completed");
288
- if (span === undefined) continue;
303
+ // A completed turn ends at `completed`, covering its tool
304
+ // settlement. An uncompleted one ends at `now` while the scope is
305
+ // genuinely working, and collapses to its last recorded stamp
306
+ // (`streamed`, else its own start) when nothing is working — so a
307
+ // host that never writes `completed` cannot bill idle either.
308
+ const end = completed ?? (live ? now : (streamed ?? created));
309
+ if (!Number.isFinite(end)) continue;
289
310
  const output = asRecord(message["tokens"])?.["output"];
290
- spans.push({ key: turnKey(message["id"], created, completed, output, streamed), ...span });
311
+ spans.push({
312
+ key: turnKey(message["id"], created, completed, output, streamed),
313
+ start: created,
314
+ end: Math.max(created, end),
315
+ });
291
316
  }
292
317
  }
293
- seed = unionSpanMs(spans);
318
+ return spans;
294
319
  } catch {
295
- // A throwing read never reaches the rail: it degrades to today's zero.
296
- seed = 0;
320
+ // A throwing read never reaches the rail: it degrades to no spans.
321
+ return [];
297
322
  }
323
+ };
298
324
 
299
- rememberSeed(sessionID, seed);
300
- return seed;
325
+ // Whether the host still knows a session. The elapsed clock's sweep consults
326
+ // this before dropping an entry, so only a forgotten session loses its
327
+ // high-water mark; a throwing or absent record counts as forgotten.
328
+ const knows = (sessionID: string): boolean => {
329
+ try {
330
+ return context.data.session.get(sessionID) !== undefined;
331
+ } catch {
332
+ return false;
333
+ }
301
334
  };
302
335
 
303
- const activeElapsed = createActiveElapsed(busy, { maxBankedMs, seedOf: seedElapsed });
336
+ const activeTime = createActiveTime({ spansOf, knows });
304
337
  const sessionElapsed = (sessionID: string, now: number = Date.now()): number | undefined =>
305
- activeElapsed(sessionID, now);
338
+ activeTime(sessionID, now);
306
339
 
307
340
  return {
308
341
  messagesOf,
309
- isFamilyRoot,
310
342
  treeTotals,
311
343
  contextUsage,
312
344
  sessionElapsed,
@@ -2,38 +2,48 @@
2
2
  // from a clock this process keeps - as a near-instantaneous output rate, not a
3
3
  // lifetime average.
4
4
  //
5
- // Each poll feeds the scope's cumulative output tokens and cumulative
6
- // generating milliseconds into a per-session EWMA tracker (`./throughput.js`):
7
- // the rate is the DELTA between successive polls, smoothed (alpha 0.3) and
8
- // repainted only on a move of >= 1 tok/s or >= 10%, so the row tracks live
9
- // decoding instead of sagging behind everything the session ever did. The
10
- // numerator is generation tokens - output plus reasoning, which keeps its own
11
- // row too - and the denominator is generating time only: the union of the
12
- // turns' own streaming
13
- // spans on a stamped host, the busy-gated active clock on one without stamps.
14
- // Idle is never divided by; when nothing is busy the tracker freezes and the
15
- // row holds its last figure. The row hides until ~2 s of generating time and
16
- // ~10 tokens are on record, rather than flashing a one-sample rate.
5
+ // Each poll feeds cumulative generation tokens and cumulative generating
6
+ // milliseconds into the EWMA tracker (`./throughput.js`): the rate is the
7
+ // DELTA between successive polls, smoothed (alpha 0.3) and repainted only on
8
+ // a move of >= 1 tok/s or >= 10%, so the row tracks live decoding instead of
9
+ // sagging behind everything ever produced. The numerator is generation
10
+ // tokens - output plus reasoning, which keeps its own row too - and the
11
+ // denominator is generating time only: the union of the streaming spans in
12
+ // scope. The scope is `sidebar.tps.scope` (`./config.js`): `"family"` (the
13
+ // default) paces the displayed session plus its subagent sessions, summing
14
+ // generation tokens across the whole family over the UNION of all their
15
+ // spans, so concurrent decoders share wall clock instead of adding it; a
16
+ // root that has gone idle while its child is still decoding still reads
17
+ // live. `"session"` restores the per-session figure: the displayed session's
18
+ // own tokens over its own spans, reading zero whenever its own generating
19
+ // time does not advance, even while a child still runs. The idle test is the
20
+ // denominator's delta either way: when the generating time in scope does not
21
+ // advance between polls, nothing in scope is generating right now, so the
22
+ // reader reports zero — idle means no tokens per second are being produced.
23
+ // The first poll for a session only sets the tracker's baselines and reads
24
+ // as zero — no rate is known yet, and the cumulative lifetime average never
25
+ // seeds the figure, so a session deep into its life must not flash its
26
+ // history as if it were live.
17
27
  //
18
28
  // The capability is learned once and remembered for the process. `undefined`
19
29
  // means "not yet known": a read that failed, or carried no assistant turns,
20
30
  // tells us nothing and must not choose a metric. Once known it is sticky in
21
31
  // BOTH directions, so a transient `message.list` failure can never flip a
22
32
  // stamped host back to the clock path (nor an unstamped host off it) -
23
- // a failure just hides the row. One value for the whole host, deliberately
33
+ // a failure just reads as zero. One value for the whole host, deliberately
24
34
  // not keyed by session.
25
35
  //
26
- // The rail re-runs on the fast tick, and walking every family message for
27
- // every session would repeat the same work fifty times a second, so the
28
- // result is held briefly. `undefined` is cached too: an idle session must not
29
- // be re-derived on each tick either.
36
+ // The rail re-runs on the fast tick, and walking the session's messages on
37
+ // every tick would repeat the same work fifty times a second, so the result
38
+ // is held briefly. Zero is cached too: an idle session must not be re-derived
39
+ // on each tick either.
30
40
  //
31
- // One factory closes over the plugin `context`; `isFamilyRoot` comes from
32
- // ./session-reads.js so a child session keeps its own figure while a root
33
- // sums its tree. The math itself lives in ./throughput.js (pure).
41
+ // One factory closes over the plugin `context`. The math itself lives in
42
+ // ./throughput.js (pure).
34
43
 
35
44
  import type { Plugin } from "@opencode/plugin/tui";
36
45
  import { asCount, asRecord, type SessionLike } from "./coerce.js";
46
+ import { DEFAULT_TPS_SCOPE, type TpsScope } from "./config.js";
37
47
  import {
38
48
  createInstantTps,
39
49
  turnKey,
@@ -56,27 +66,22 @@ interface TpsScan {
56
66
  }
57
67
 
58
68
  /**
59
- * The live signals the TPS reader borrows rather than re-deriving: the busy
60
- * gate (is anything working?) freezes the row on idle, and the active clock
61
- * (how much generating time is on record?) is the denominator for a host
62
- * with no message timestamps. Both come from ./session-reads.js; absent, the
63
- * stamped path still works and the unstamped path hides for lack of time.
69
+ * The live signal the TPS reader borrows rather than re-deriving: the active
70
+ * clock (how much generating time is on record?) is the denominator for a host
71
+ * with no message timestamps — that clock already covers the scope, so it is
72
+ * borrowed as-is. It comes from ./session-reads.js; absent, the stamped path
73
+ * still works and the unstamped path reads as zero for lack of time. There is
74
+ * deliberately no busy gate here: idleness is read off the session's own
75
+ * generating-time delta, never off a family-wide flag.
64
76
  */
65
77
  export interface TpsLiveReads {
66
- readonly busyOf?: (sessionID: string) => boolean | undefined;
67
78
  readonly elapsedMsOf?: (sessionID: string, now: number) => number | undefined;
68
79
  }
69
80
 
70
- export function createTpsReader(
71
- context: Plugin.Context,
72
- isFamilyRoot: (sessionID: string) => boolean,
73
- live?: TpsLiveReads,
74
- ) {
81
+ export function createTpsReader(context: Plugin.Context, live?: TpsLiveReads, scope: TpsScope = DEFAULT_TPS_SCOPE) {
75
82
  const TPS_CACHE_MS = 1_000;
76
83
  let hostStamps: boolean | undefined;
77
- let tpsCache:
78
- | { readonly sessionID: string; readonly at: number; readonly value: number | undefined }
79
- | undefined;
84
+ let tpsCache: { readonly sessionID: string; readonly at: number; readonly value: number } | undefined;
80
85
  // One EWMA tracker per displayed session, bounded: without eviction a TUI
81
86
  // lifetime of session-hopping grows this map forever. Eviction only drops
82
87
  // smoothing - a revisit restarts its baselines, exactly like a first sight.
@@ -128,7 +133,7 @@ export function createTpsReader(
128
133
  stamped += 1;
129
134
  // Without `created` there is no start; the turn is skipped, not guessed.
130
135
  // `tps` ends a turn at `streamed`: decoding stopped is the numerator clock.
131
- const span = turnSpan(created, completed, streamed, now, "streamed");
136
+ const span = turnSpan(created, completed, streamed, now);
132
137
  if (span === undefined) continue;
133
138
  const tokens = asRecord(message["tokens"]);
134
139
  // Generation: output plus reasoning, which keeps its own row too. A
@@ -148,118 +153,153 @@ export function createTpsReader(
148
153
  return { ok: true, assistant, stamped, spans };
149
154
  };
150
155
 
151
- // Generation tokens on record for the scope, for a host with no message
152
- // timestamps: the session records' own `output` and `reasoning` rungs,
153
- // summed over the family from a root. A child asking `family()` gets its
154
- // ancestors and siblings too, so it keeps its own figure.
155
- const recordGeneration = (sessionID: string, session: SessionLike | undefined): number => {
156
- const generationOf = (source: SessionLike | undefined): number => {
157
- const tokens = asRecord(source?.tokens);
158
- return (asCount(tokens?.["output"]) ?? 0) + (asCount(tokens?.["reasoning"]) ?? 0);
159
- };
160
- if (!isFamilyRoot(sessionID)) return generationOf(session);
156
+ // The displayed session plus every id `family()` returns for it — the same
157
+ // traversal `busy()` in ./session-reads.js consults, so the scope is the
158
+ // one the `elapsed` row already covers. A host without `family()` degrades
159
+ // to the session alone rather than failing the row.
160
+ const familyIds = (sessionID: string): readonly string[] => {
161
+ const ids = [sessionID];
161
162
  try {
162
- const ids = context.data.session.family(sessionID) ?? [];
163
- if (ids.length <= 1) return generationOf(session);
164
- let total = 0;
165
- for (const id of ids) {
166
- if (id === sessionID) continue;
167
- try {
168
- total += generationOf(context.data.session.get(id) as SessionLike | undefined);
169
- } catch {
170
- // One unreadable member must not hide the rest.
171
- }
163
+ for (const id of context.data.session.family(sessionID) ?? []) {
164
+ if (id === sessionID || ids.includes(id)) continue;
165
+ ids.push(id);
172
166
  }
173
- return generationOf(session) + total;
174
167
  } catch {
175
- // No family on this host: the session's own tokens still stand.
176
- return generationOf(session);
168
+ // No family on this host: the session's own turns still stand.
177
169
  }
170
+ return ids;
178
171
  };
179
172
 
180
- const busyOf = (sessionID: string): boolean | undefined => {
181
- try {
182
- return live?.busyOf?.(sessionID);
183
- } catch {
184
- return undefined;
173
+ // Generation spans for the whole family scope: every member's own spans,
174
+ // keyed per member so two sessions' id-less turns sharing a start stay
175
+ // distinct. The union downstream merges their overlaps — concurrent
176
+ // decoders share wall clock, so it is never summed.
177
+ const scanScope = (ids: readonly string[], now: number): TpsScan => {
178
+ let ok = true;
179
+ let assistant = 0;
180
+ let stamped = 0;
181
+ const spans: ThroughputSpan[] = [];
182
+ for (const id of ids) {
183
+ const scan = scanMessages(id, now);
184
+ // A refused read is not "this member has no messages": one failure
185
+ // keeps the capability unknown rather than proving anything.
186
+ ok = ok && scan.ok;
187
+ assistant += scan.assistant;
188
+ stamped += scan.stamped;
189
+ for (const span of scan.spans) spans.push({ ...span, key: `${id} ${String(span.key)}` });
185
190
  }
191
+ return { ok, assistant, stamped, spans };
186
192
  };
187
193
 
188
- const computeTps = (sessionID: string, session: SessionLike | undefined): number | undefined => {
189
- const time = asRecord(session?.time);
190
- const created = asCount(time?.["created"]);
191
-
192
- // One `now` for the whole scope, so every in-flight turn ends at the same
193
- // instant and the union cannot be skewed by the scans drifting apart.
194
- const now = Date.now();
195
- const busy = busyOf(sessionID);
194
+ // Generation tokens on record for the session: its own record's `output`
195
+ // and `reasoning` rungs. The `"session"` scope stops here — a root reports
196
+ // only its own rate, and a child keeps its own figure.
197
+ const recordGeneration = (session: SessionLike | undefined): number => {
198
+ const tokens = asRecord(session?.tokens);
199
+ return (asCount(tokens?.["output"]) ?? 0) + (asCount(tokens?.["reasoning"]) ?? 0);
200
+ };
196
201
 
197
- // Timestamped assistant generation this session has produced, plus - only
198
- // from a family root - every subagent session's. A child asking `family()`
199
- // gets its ancestors and siblings back, so it keeps its own spans.
200
- const scans: TpsScan[] = hostStamps === false ? [] : [scanMessages(sessionID, now)];
201
- if (hostStamps !== false && isFamilyRoot(sessionID)) {
202
+ // Generation tokens on record for the whole family scope: the displayed
203
+ // session's own rungs plus every member's. One unreadable member must not
204
+ // blank the rest of the tree — but the total is then incomplete, so the
205
+ // walk reports it and the caller holds the tracker's baselines instead of
206
+ // feeding the partial figure in.
207
+ const familyGeneration = (
208
+ sessionID: string,
209
+ session: SessionLike | undefined,
210
+ ): { readonly ok: boolean; readonly generation: number } => {
211
+ let generation = recordGeneration(session);
212
+ let ok = true;
213
+ for (const id of familyIds(sessionID)) {
214
+ if (id === sessionID) continue;
202
215
  try {
203
- for (const id of context.data.session.family(sessionID) ?? []) {
204
- if (id !== sessionID) scans.push(scanMessages(id, now));
205
- }
216
+ generation += recordGeneration(context.data.session.get(id) as SessionLike | undefined);
206
217
  } catch {
207
- // No family on this host: the session's own turns still stand.
218
+ // One unreadable member must not blank the rest of the tree.
219
+ ok = false;
208
220
  }
209
221
  }
222
+ return { ok, generation };
223
+ };
210
224
 
211
- const spans: ThroughputSpan[] = [];
212
- let assistant = 0;
213
- let stamped = 0;
214
- let readOk = false;
215
- for (const scan of scans) {
216
- spans.push(...scan.spans);
217
- assistant += scan.assistant;
218
- stamped += scan.stamped;
219
- readOk = readOk || scan.ok;
220
- }
225
+ const computeTps = (sessionID: string, session: SessionLike | undefined): number | undefined => {
226
+ const time = asRecord(session?.time);
227
+ const created = asCount(time?.["created"]);
228
+
229
+ const now = Date.now();
230
+
231
+ // Timestamped assistant generation in scope — the whole family under
232
+ // `"family"`, this session's own spans only under `"session"`.
233
+ const scan: TpsScan =
234
+ hostStamps === false
235
+ ? { ok: true, assistant: 0, stamped: 0, spans: [] }
236
+ : scope === "family"
237
+ ? scanScope(familyIds(sessionID), now)
238
+ : scanMessages(sessionID, now);
221
239
 
222
240
  if (hostStamps === undefined) {
223
241
  // A stamped assistant turn proves the host stamps. Assistant turns with
224
242
  // no timestamp prove it does not. Neither, and the capability stays
225
- // unknown so the row hides rather than guessing a metric.
226
- if (stamped > 0) hostStamps = true;
227
- else if (readOk && assistant > 0) hostStamps = false;
243
+ // unknown so the row reads as zero rather than guessing a metric.
244
+ if (scan.stamped > 0) hostStamps = true;
245
+ else if (scan.ok && scan.assistant > 0) hostStamps = false;
228
246
  else return undefined;
229
247
  }
230
248
 
231
249
  const tracker = trackerOf(sessionID);
232
250
  if (hostStamps === true) {
233
- // Generating time is the union of the turns' own streaming spans: idle
251
+ // A refused member read leaves an incomplete cumulative total: feeding
252
+ // it in would move the baselines, and the member's history would return
253
+ // as a spike on recovery. The poll reads as zero with the baselines
254
+ // held, so the next healthy poll divides only what is genuinely new.
255
+ // `"session"` never takes this branch: its single read keeps the
256
+ // pre-change behaviour byte-identically.
257
+ if (scope === "family" && scan.ok === false) return 0;
258
+ // Generating time is the union of the turns' spans in scope: idle
234
259
  // between turns was never recorded, so it can never enter the rate. An
235
260
  // in-flight turn ends at now, so the denominator keeps pace with the
236
261
  // decoder while work happens and freezes the instant it settles. The
237
262
  // numerator over it is generation: output plus reasoning.
238
- const { tokens } = unionGenerationTotals(spans);
239
- const genMs = unionSpanMs(spans);
240
- return tracker(tokens, genMs, busy);
263
+ const { tokens } = unionGenerationTotals(scan.spans);
264
+ const genMs = unionSpanMs(scan.spans);
265
+ return tracker(tokens, genMs);
241
266
  }
242
267
 
243
- // No timestamps on this host: the denominator is the busy-gated active
244
- // clock, which runs only while the scope is actually working. Without that
245
- // clock there is no honest denominator - the row hides rather than falling
246
- // back to a lifetime average that bills idle as work.
247
- if (created === undefined) return tracker(recordGeneration(sessionID, session), 0, busy);
268
+ // No timestamps on this host: the denominator is the active clock, which
269
+ // already covers the family scope (it IS the `elapsed` figure), so it is
270
+ // borrowed as-is. Without that clock there is no honest denominator
271
+ // - the row reads as zero rather than falling back to a lifetime average
272
+ // that bills idle as work.
273
+ const family = scope === "family" ? familyGeneration(sessionID, session) : undefined;
274
+ // As on the stamped path: one unreadable member leaves the total
275
+ // incomplete, so the poll reads as zero with the baselines held.
276
+ if (family !== undefined && family.ok === false) return 0;
277
+ const generation = family?.generation ?? recordGeneration(session);
278
+ // The creation-time gate is the `"session"` scope's alone: without a
279
+ // stamp there is no clock to borrow. Under `"family"` the displayed
280
+ // session may carry no stamp while a child still paces off the
281
+ // family-wide active clock, so the gate must not silence it.
282
+ if (created === undefined && scope === "session") return tracker(recordGeneration(session), 0);
248
283
  let genMs = 0;
249
284
  try {
250
285
  genMs = live?.elapsedMsOf?.(sessionID, now) ?? 0;
251
286
  } catch {
252
287
  genMs = 0;
253
288
  }
254
- return tracker(recordGeneration(sessionID, session), genMs, busy);
289
+ return tracker(generation, genMs);
255
290
  };
256
291
 
257
- const sessionTps = (sessionID: string, session: SessionLike | undefined): number | undefined => {
292
+ // The reader never answers `undefined` for a session it was asked about: a
293
+ // session that is not generating reads as zero immediately — its generating
294
+ // time is not advancing, so the tracker reports zero — and a session with no
295
+ // figure yet reads as zero too. (The row itself hides a zero; that decision
296
+ // lives in ./rows.js.)
297
+ const sessionTps = (sessionID: string, session: SessionLike | undefined): number => {
258
298
  const cached = tpsCache;
259
299
  if (cached !== undefined && cached.sessionID === sessionID && Date.now() - cached.at < TPS_CACHE_MS) {
260
300
  return cached.value;
261
301
  }
262
- const value = computeTps(sessionID, session);
302
+ const value = computeTps(sessionID, session) ?? 0;
263
303
  tpsCache = { sessionID, at: Date.now(), value };
264
304
  return value;
265
305
  };