oc-flight-deck 0.19.1 → 0.20.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.
@@ -8,10 +8,12 @@
8
8
  // Two pieces live here. The union helpers sum generation tokens (output plus
9
9
  // reasoning, the same pair the official TUI divides) and merge the assistant
10
10
  // turns' own streaming spans, so idle time is never in the denominator and the
11
- // figure freezes once everything settles. `createInstantTps` is the live row's
12
- // tracker: generation-token per-poll deltas over generating-time deltas, smoothed
13
- // with an EWMA and repainted through hysteresis, so the rail shows what the
14
- // decoder is doing now instead of everything the session ever did.
11
+ // figure freezes once everything settles. The realtime figure beside them
12
+ // lives in ./realtime-tps.js: it estimates tokens from the host's streaming
13
+ // delta events, because a poll of the message list can only see a turn's
14
+ // tokens after it settles. The EWMA smoother and the repaint hysteresis below
15
+ // are shared by that tracker, so the rail bends toward new samples instead of
16
+ // flickering on them.
15
17
 
16
18
  import { asCount, asText } from "./coerce.js";
17
19
 
@@ -207,21 +209,6 @@ export const TPS_EWMA_ALPHA = 0.3;
207
209
  export const TPS_REPAINT_ABS = 1;
208
210
  export const TPS_REPAINT_RATIO = 0.1;
209
211
 
210
- /**
211
- * One per-poll instantaneous rate: generation-token DELTA over generating-time
212
- * DELTA, never over idle time. The caller passes only what happened since the
213
- * previous poll; both deltas exclude idle by construction (the union of
214
- * streaming spans, or the busy-gated active clock), so a pause can never sag
215
- * the figure. Returns `undefined` when there is no new generating time to
216
- * divide by.
217
- */
218
- export function instantTpsRate(deltaTokens: unknown, deltaMs: unknown): number | undefined {
219
- const tokens = asCount(deltaTokens);
220
- const ms = asCount(deltaMs);
221
- if (tokens === undefined || ms === undefined || ms <= 0) return undefined;
222
- return tokens / (ms / 1_000);
223
- }
224
-
225
212
  /**
226
213
  * Fold one instantaneous sample into the running rate. The first sample seeds
227
214
  * the average directly; every later one moves it `alpha` of the way toward
@@ -248,97 +235,20 @@ export function tpsNeedsRepaint(displayed: number | undefined, smoothed: number)
248
235
  }
249
236
 
250
237
  /**
251
- * Generation tokens and generating time for the TPS numerator: output plus
252
- * reasoning, the same pair the official TUI divides.
238
+ * The settled-turn average generation rate over already-accepted spans.
253
239
  *
254
- * The same identity rule, clamp, pre-sort and merge as `unionSpanTotals`.
255
- * Only the live TPS path uses this.
240
+ * The caller passes only turns that recorded `streamed` — settled work, so
241
+ * in-flight turns never enter either side — and this divides their generation
242
+ * tokens by the union of their spans, in seconds. Weighted by provider-active
243
+ * duration the way the official TUI weights it: each turn's tokens count once
244
+ * and overlapping spans merge, so concurrent decoders share wall clock. This
245
+ * is the `avg` the `tps` row draws by default: `undefined` when no settled
246
+ * turn exists (or their spans cover no time), so the row hides rather than
247
+ * dividing by zero. A zero-token settled history divides cleanly to zero,
248
+ * which the row hides through its existing rounded-zero rule.
256
249
  */
257
- export function unionGenerationTotals(spans: readonly ThroughputSpan[]): SpanTotals {
258
- const seen = new Set<string>();
259
- const accepted: Array<{ start: number; end: number }> = [];
260
- let tokens = 0;
261
-
262
- for (const span of spans) {
263
- const key = asText(span.key);
264
- if (key === undefined || seen.has(key)) continue;
265
-
266
- const start = asCount(span.start);
267
- const end = asCount(span.end);
268
- if (start === undefined || end === undefined) continue;
269
- const output = asCount(span.tokens);
270
- const reasoning = asCount(span.reasoning);
271
-
272
- seen.add(key);
273
- accepted.push({ start, end: Math.max(start, end) });
274
- tokens += (output ?? 0) + (reasoning ?? 0);
275
- }
276
-
277
- return { tokens, unionMs: mergedSpanMs(accepted) };
278
- }
279
-
280
- /**
281
- * Near-instantaneous generation rate for one displayed session scope.
282
- *
283
- * The caller feeds each poll's cumulative generation tokens and cumulative
284
- * generating milliseconds (the streaming-span union, which excludes idle by
285
- * construction) and gets back the figure to draw. The rule is per-session and
286
- * honest about idle: the first sample only sets the baselines and reads as
287
- * zero — no rate is known yet, and a session deep into its life must not flash
288
- * its lifetime average as if it were live. A poll whose generating time did
289
- * not advance means the session is not generating right now: it reads as zero
290
- * with the smoother untouched, so the next poll that does add generating time
291
- * resumes from the held smoothed figure rather than from zero. A backwards
292
- * counter rebaselines (baselines and smoother alike, since the pre-reset
293
- * average belongs to another era) and reads as zero. Otherwise the
294
- * instantaneous rate is the delta of generation tokens over the delta of
295
- * generating milliseconds, smoothed by the EWMA and held through the
296
- * hysteresis. Deltas between successive polls are the rate, so a pause can
297
- * never sag the figure — it reads as zero while it lasts.
298
- */
299
- export function createInstantTps(alpha: number = TPS_EWMA_ALPHA) {
300
- let prevTokens: number | undefined;
301
- let prevGenMs: number | undefined;
302
- let smoothed: number | undefined;
303
- let displayed: number | undefined;
304
-
305
- return function sample(totalTokens: unknown, totalGenMs: unknown): number | undefined {
306
- const tokens = asCount(totalTokens);
307
- const genMs = asCount(totalGenMs);
308
- if (tokens === undefined || genMs === undefined) return displayed;
309
-
310
- // First sight sets the baselines. No rate is known yet — not even the
311
- // cumulative lifetime average, which would flash a whole session's history
312
- // as if it were live — so the row reads as zero until the next poll
313
- // brings a delta to divide.
314
- if (prevTokens === undefined || prevGenMs === undefined) {
315
- prevTokens = tokens;
316
- prevGenMs = genMs;
317
- return 0;
318
- }
319
-
320
- const deltaTokens = tokens - prevTokens;
321
- const deltaMs = genMs - prevGenMs;
322
- // Counters must not step backwards; a reset rebaselines - baselines and
323
- // smoother alike, since the pre-reset average belongs to another era -
324
- // instead of dividing a negative delta into a nonsense rate.
325
- if (deltaTokens < 0 || deltaMs < 0) {
326
- prevTokens = tokens;
327
- prevGenMs = genMs;
328
- smoothed = undefined;
329
- return 0;
330
- }
331
- prevTokens = tokens;
332
- prevGenMs = genMs;
333
-
334
- // No new generating time: the session is not generating right now, so the
335
- // rate is zero — and the smoother is left untouched, so the next poll that
336
- // does add generating time resumes from the held smoothed figure rather
337
- // than from zero.
338
- if (deltaMs <= 0) return 0;
339
- const instant = deltaTokens / (deltaMs / 1_000);
340
- smoothed = smoothTpsRate(smoothed, instant, alpha);
341
- if (tpsNeedsRepaint(displayed, smoothed)) displayed = smoothed;
342
- return displayed;
343
- };
250
+ export function settledAverageRate(spans: readonly ThroughputSpan[]): number | undefined {
251
+ const { tokens, unionMs } = unionSpanTotals(spans);
252
+ if (unionMs <= 0) return undefined;
253
+ return tokens / (unionMs / 1_000);
344
254
  }