oc-flight-deck 0.23.1 → 0.25.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/CHANGELOG.md +19 -3
- package/README.md +42 -24
- package/dist/tui/index.js +232 -104
- package/docs/GUIDE.md +11 -8
- package/flight-deck.example.jsonc +49 -37
- package/flight-deck.schema.json +29 -12
- package/package.json +7 -7
- package/src/tui/config.ts +118 -16
- package/src/tui/go-panel.ts +5 -3
- package/src/tui/index.tsx +38 -3
- package/src/tui/lifetime-totals.ts +92 -0
- package/src/tui/presentation.ts +11 -5
- package/src/tui/rows.ts +75 -70
- package/src/tui/stat-fields.ts +57 -26
package/src/tui/index.tsx
CHANGED
|
@@ -15,13 +15,14 @@ import { loadConfigFile } from "./file-config.js";
|
|
|
15
15
|
import { createGoLookReader } from "./go-look.js";
|
|
16
16
|
import { footerLine, railLineSpans, railLines, railLineStyle, type RailLine } from "./presentation.js";
|
|
17
17
|
import { goPanelLines } from "./go-panel.js";
|
|
18
|
-
import { ANIMATED_FIELDS, type StatSource } from "./stats.js";
|
|
18
|
+
import { ANIMATED_FIELDS, canonicalField, type StatSource } from "./stats.js";
|
|
19
19
|
import { attributeMask, themeColor } from "./style.js";
|
|
20
20
|
import { startGuardBridge } from "./guard.js";
|
|
21
21
|
import { startGoBridge } from "./go.js";
|
|
22
22
|
import { startCodexQuota } from "./codex-quota.js";
|
|
23
23
|
import { startTicker } from "./ticker.js";
|
|
24
24
|
import { asCount, asRecord, type SessionLike } from "./coerce.js";
|
|
25
|
+
import { createLifetimeTotals } from "./lifetime-totals.js";
|
|
25
26
|
import { createProjectTotals } from "./project-totals.js";
|
|
26
27
|
import { createRealtimeTps } from "./realtime-tps.js";
|
|
27
28
|
import { createSessionReads, locationOf } from "./session-reads.js";
|
|
@@ -54,7 +55,12 @@ export default Plugin.define({
|
|
|
54
55
|
// Only the rows actually on the rail are worth deriving: the rail re-runs on
|
|
55
56
|
// every tick, and a figure nobody asked to see should not cost a walk of the
|
|
56
57
|
// session database or the model catalog.
|
|
57
|
-
|
|
58
|
+
// Canonical keys for the "is this row on screen" checks: `cost` and
|
|
59
|
+
// `total` are accepted aliases of `spend` and `lifetime`, and every one of
|
|
60
|
+
// them must ask for the same underlying read.
|
|
61
|
+
const rows = config.sidebar.enabled
|
|
62
|
+
? new Set(config.sidebar.rows.map(canonicalField))
|
|
63
|
+
: new Set<string>();
|
|
58
64
|
const wants = (name: string): boolean => rows.has(name);
|
|
59
65
|
|
|
60
66
|
if (file.issue !== undefined && file.source !== undefined) {
|
|
@@ -175,6 +181,10 @@ export default Plugin.define({
|
|
|
175
181
|
|
|
176
182
|
const { projectTotals } = createProjectTotals(context);
|
|
177
183
|
|
|
184
|
+
// The lifetime figure is a high-water mark, not just the host's current
|
|
185
|
+
// family roll-up. This ledger is the only durable state the rail keeps.
|
|
186
|
+
const lifetimeTotals = createLifetimeTotals({ path: config.lifetimeTotalsPath });
|
|
187
|
+
|
|
178
188
|
const cautionThreshold = cautionThresholds(config.caution);
|
|
179
189
|
|
|
180
190
|
const cautionsOf = (sessionID: string, now: number): Caution[] =>
|
|
@@ -311,10 +321,34 @@ export default Plugin.define({
|
|
|
311
321
|
// roll-up. Neither reads the other's source, so no hand-off can make the
|
|
312
322
|
// lifetime figure inherit a per-model one.
|
|
313
323
|
const wantsUsage =
|
|
314
|
-
wants("
|
|
324
|
+
wants("spend") || wants("lifetime") || wants("tokens") || wants("cache") || wants("reasoning");
|
|
315
325
|
const ownCost = wantsUsage ? sessionCost(sessionID, session as SessionLike | undefined) : undefined;
|
|
316
326
|
const tree = wantsUsage ? treeTotals(sessionID) : undefined;
|
|
317
327
|
|
|
328
|
+
// The lifetime figure is a high-water mark. Resolve the family root the
|
|
329
|
+
// same way treeTotals does, read what earlier ticks recorded, and raise
|
|
330
|
+
// the stored figure when the host currently reports more. Only `lifetime`
|
|
331
|
+
// carries the raised number: `tree` stays the live roll-up so the cost
|
|
332
|
+
// row keeps naming the current model's spend alone.
|
|
333
|
+
let lifetime: { cost: number; count: number; kind: string } | undefined;
|
|
334
|
+
if (wantsUsage) {
|
|
335
|
+
let root = sessionID;
|
|
336
|
+
try {
|
|
337
|
+
root = context.data.session.root(sessionID) ?? sessionID;
|
|
338
|
+
} catch {
|
|
339
|
+
root = sessionID;
|
|
340
|
+
}
|
|
341
|
+
const stored = lifetimeTotals.read(root);
|
|
342
|
+
const live = tree?.cost;
|
|
343
|
+
if (live !== undefined && (stored === undefined || live > stored)) {
|
|
344
|
+
lifetimeTotals.record(root, live);
|
|
345
|
+
}
|
|
346
|
+
const high = Math.max(live ?? 0, stored ?? 0);
|
|
347
|
+
if (live !== undefined || stored !== undefined) {
|
|
348
|
+
lifetime = { cost: high, count: tree?.count ?? 0, kind: tree?.kind ?? "bounded" };
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
318
352
|
return {
|
|
319
353
|
...session,
|
|
320
354
|
cost: ownCost?.cost,
|
|
@@ -323,6 +357,7 @@ export default Plugin.define({
|
|
|
323
357
|
caution: wants("caution") ? announce(sessionID) : undefined,
|
|
324
358
|
branch: wants("branch") ? branchOf(location) : undefined,
|
|
325
359
|
tree,
|
|
360
|
+
lifetime,
|
|
326
361
|
context: wants("context") ? contextUsage(sessionID, session?.model) : undefined,
|
|
327
362
|
project: wants("project") ? projectTotals(sessionID) : undefined,
|
|
328
363
|
status: wants("status") ? statusOf(sessionID) : undefined,
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// A durable high-water ledger for the lifetime family cost.
|
|
2
|
+
//
|
|
3
|
+
// The host's family roll-up is exact while the host holds it, but pruning old
|
|
4
|
+
// messages or restarting can make it shrink. The lifetime row is a high-water
|
|
5
|
+
// mark: it never goes backwards. A single small JSON file under the user's data
|
|
6
|
+
// directory remembers the highest figure seen for each family root. Every fs
|
|
7
|
+
// failure is swallowed — a broken or unwritable ledger must never break the
|
|
8
|
+
// rail — and no entry is ever deleted or expired.
|
|
9
|
+
|
|
10
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { homedir } from "node:os";
|
|
12
|
+
import { dirname, join } from "node:path";
|
|
13
|
+
|
|
14
|
+
export interface LifetimeTotals {
|
|
15
|
+
/** Highest figure known for `id`, or `undefined` when none has been seen. */
|
|
16
|
+
readonly read: (id: string) => number | undefined;
|
|
17
|
+
/** Raise `id` to `value` when it is higher than everything already known. */
|
|
18
|
+
readonly record: (id: string, value: number) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface LifetimeTotalsOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Ledger path. Defaults to
|
|
24
|
+
* `~/.local/share/opencode/flight-deck/totals.json`; tests pass a temp dir.
|
|
25
|
+
*/
|
|
26
|
+
readonly path?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The shipped ledger path, under the user's data directory. */
|
|
30
|
+
export function defaultLifetimeTotalsPath(): string {
|
|
31
|
+
return join(homedir(), ".local", "share", "opencode", "flight-deck", "totals.json");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Open the ledger. `path` is the only option; an absent or unreadable file is
|
|
36
|
+
* an empty ledger, never an error. The in-process map is a second high-water:
|
|
37
|
+
* a host that drops messages mid-session cannot shrink `read()` either.
|
|
38
|
+
*/
|
|
39
|
+
export function createLifetimeTotals(options: LifetimeTotalsOptions = {}): LifetimeTotals {
|
|
40
|
+
const path = options.path ?? defaultLifetimeTotalsPath();
|
|
41
|
+
const held = new Map<string, number>();
|
|
42
|
+
|
|
43
|
+
// Re-read on every call: another OpenCode process may have raised the file
|
|
44
|
+
// since our last tick, and we must not lower our answer to its figure.
|
|
45
|
+
const storedValues = (): Record<string, number> => {
|
|
46
|
+
try {
|
|
47
|
+
if (!existsSync(path)) return {};
|
|
48
|
+
const parsed: unknown = JSON.parse(readFileSync(path, "utf8"));
|
|
49
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return {};
|
|
50
|
+
const values: Record<string, number> = {};
|
|
51
|
+
for (const [id, value] of Object.entries(parsed)) {
|
|
52
|
+
if (typeof value === "number" && Number.isFinite(value) && value > 0) values[id] = value;
|
|
53
|
+
}
|
|
54
|
+
return values;
|
|
55
|
+
} catch {
|
|
56
|
+
// A corrupt or unreadable file is treated as empty, never thrown on.
|
|
57
|
+
return {};
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const writeAll = (values: Record<string, number>): void => {
|
|
62
|
+
try {
|
|
63
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
64
|
+
const temporary = `${path}.${process.pid}.tmp`;
|
|
65
|
+
writeFileSync(temporary, JSON.stringify(values));
|
|
66
|
+
renameSync(temporary, path);
|
|
67
|
+
} catch {
|
|
68
|
+
// Persistence is best-effort: the in-process high-water still holds.
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
read(id) {
|
|
74
|
+
const stored = storedValues()[id];
|
|
75
|
+
const memory = held.get(id);
|
|
76
|
+
if (stored === undefined && memory === undefined) return undefined;
|
|
77
|
+
return Math.max(stored ?? 0, memory ?? 0);
|
|
78
|
+
},
|
|
79
|
+
record(id, value) {
|
|
80
|
+
// A non-finite or non-positive figure is not a spend and cannot raise a
|
|
81
|
+
// high-water mark.
|
|
82
|
+
if (!Number.isFinite(value) || value <= 0) return;
|
|
83
|
+
const previous = Math.max(held.get(id) ?? 0, storedValues()[id] ?? 0);
|
|
84
|
+
if (value <= previous) return;
|
|
85
|
+
held.set(id, value);
|
|
86
|
+
// Merge into what is on disk: never delete or prune another entry.
|
|
87
|
+
const values = storedValues();
|
|
88
|
+
values[id] = value;
|
|
89
|
+
writeAll(values);
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
package/src/tui/presentation.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// session state, and nothing leaves the process.
|
|
7
7
|
|
|
8
8
|
import type { FlightDeckConfig } from "./config.js";
|
|
9
|
+
import { canonicalField } from "./stat-fields.js";
|
|
9
10
|
import { statLine, statRows, statSegments, type StatSource, type StatSegment } from "./stats.js";
|
|
10
11
|
import { rowStyle, type LineStyle, type StyleColor, type StyleConfig } from "./style.js";
|
|
11
12
|
|
|
@@ -44,7 +45,10 @@ export function railLines(config: FlightDeckConfig, source: StatSource = {}): re
|
|
|
44
45
|
// The display side of the config, which the row renderer reads alongside its
|
|
45
46
|
// geometry. `format` is a top-level section, so it is mapped here.
|
|
46
47
|
// `tpsDisplay` is the `tps` row's view of `sidebar.tps.display`: which of
|
|
47
|
-
// the pair's figures draws (the
|
|
48
|
+
// the pair's figures draws (the realtime estimate by default). `tpsLabel`
|
|
49
|
+
// is its view of `sidebar.tps.label`: the suffix on the single-figure rows.
|
|
50
|
+
// `tpsTtft` is its view of `sidebar.tps.ttft`: whether the observed time
|
|
51
|
+
// to first token may draw beside or instead of the figures.
|
|
48
52
|
const layout = {
|
|
49
53
|
...config.layout,
|
|
50
54
|
persist: config.sidebar.persist,
|
|
@@ -52,10 +56,12 @@ export function railLines(config: FlightDeckConfig, source: StatSource = {}): re
|
|
|
52
56
|
durationStyle: config.format.duration,
|
|
53
57
|
timestamp: config.format.timestamp,
|
|
54
58
|
tpsDisplay: config.sidebar.tps.display,
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
//
|
|
58
|
-
|
|
59
|
+
tpsLabel: config.sidebar.tps.label,
|
|
60
|
+
tpsTtft: config.sidebar.tps.ttft,
|
|
61
|
+
// Decided from the whole row list, the way `statRows` decides it: `spend`
|
|
62
|
+
// folds the subagent total into itself only when no `lifetime` row (or its
|
|
63
|
+
// `total` alias) was asked for, even if the cap later drops that row.
|
|
64
|
+
hasLifetimeRow: config.sidebar.rows.some((field) => canonicalField(field) === "lifetime"),
|
|
59
65
|
};
|
|
60
66
|
|
|
61
67
|
const lines: RailLine[] = config.sidebar.lines.map((text) => ({ text }));
|
package/src/tui/rows.ts
CHANGED
|
@@ -340,7 +340,7 @@ export function statLine(
|
|
|
340
340
|
const branch = asText(source.branch);
|
|
341
341
|
return branch === undefined ? undefined : row("branch", branch);
|
|
342
342
|
}
|
|
343
|
-
case "
|
|
343
|
+
case "spend": {
|
|
344
344
|
const cost = asCount(source.cost);
|
|
345
345
|
const tree = asRecord(source.tree);
|
|
346
346
|
const treeCost = asCount(tree?.cost);
|
|
@@ -348,53 +348,53 @@ export function statLine(
|
|
|
348
348
|
const family = count !== undefined && count > 0 ? count : 0;
|
|
349
349
|
|
|
350
350
|
// Merge only when subagents actually ran, and only when the rail is not
|
|
351
|
-
// already showing a `
|
|
352
|
-
// superset was the thing worth fixing; a
|
|
353
|
-
// would just be that duplication
|
|
351
|
+
// already showing a `lifetime` row (or its `total` alias). Two rows
|
|
352
|
+
// holding a number and its own superset was the thing worth fixing; a
|
|
353
|
+
// setting to choose between them would just be that duplication.
|
|
354
354
|
const merge =
|
|
355
|
-
layout.
|
|
355
|
+
layout.hasLifetimeRow !== true &&
|
|
356
356
|
cost !== undefined &&
|
|
357
357
|
treeCost !== undefined &&
|
|
358
358
|
family > 0 &&
|
|
359
359
|
treeCost > cost;
|
|
360
360
|
|
|
361
361
|
if (!merge) {
|
|
362
|
-
// `
|
|
363
|
-
//
|
|
364
|
-
//
|
|
365
|
-
|
|
366
|
-
|
|
362
|
+
// `spend` is the current model's spend and nothing else. With no spend
|
|
363
|
+
// on that model the row draws a sane zero, never a dash or a hole, and
|
|
364
|
+
// it still never borrows the family figure under a one-model label.
|
|
365
|
+
// `field` keeps the caller's spelling, so the `cost` alias still draws
|
|
366
|
+
// its old label.
|
|
367
|
+
return row(field, `${feeMark(source.costKind)}${formatCost(cost ?? 0)}`);
|
|
367
368
|
}
|
|
368
369
|
|
|
369
370
|
// The delta (`+$0.020`) used to be here and pushed the row to 40 columns,
|
|
370
371
|
// wide enough to wrap in a normal sidebar. It was real information that
|
|
371
372
|
// did not earn its width: the count already explains why the figure is
|
|
372
|
-
// higher, and this form is the same width as the
|
|
373
|
+
// higher, and this form is the same width as the lifetime row.
|
|
373
374
|
const plural = family === 1 ? "subagent" : "subagents";
|
|
374
|
-
return row(
|
|
375
|
-
"cost",
|
|
376
|
-
`${feeMark(tree?.kind)}${formatCost(treeCost as number)} · ${family} ${plural}`,
|
|
377
|
-
);
|
|
375
|
+
return row(field, `${feeMark(tree?.kind)}${formatCost(treeCost as number)} · ${family} ${plural}`);
|
|
378
376
|
}
|
|
379
|
-
case "
|
|
377
|
+
case "lifetime": {
|
|
380
378
|
// The family figure: this session plus its subagent sessions. Since 0.20.0
|
|
381
379
|
// it is drawn even when no subagent has run — the true family total is then
|
|
382
380
|
// the session's own cost, and the row reads `$0.000 · 0 subagents` rather
|
|
383
381
|
// than blanking, because the row the owner renames to `lifetime` must never
|
|
384
|
-
// have a hole in it.
|
|
385
|
-
//
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
const
|
|
389
|
-
|
|
390
|
-
|
|
382
|
+
// have a hole in it. With no family record at all it still draws the
|
|
383
|
+
// honest zero: this is a numeric row, so it never blanks or falls back
|
|
384
|
+
// to the placeholder.
|
|
385
|
+
const live = asRecord(source.tree);
|
|
386
|
+
const durable = asRecord(source.lifetime);
|
|
387
|
+
// The durable high-water wins over the host's current roll-up so the
|
|
388
|
+
// lifetime never shrinks. The cost row keeps reading `tree` alone, so
|
|
389
|
+
// raising this figure cannot change the current model's spend.
|
|
390
|
+
const cost = asCount(durable?.cost) ?? asCount(live?.cost) ?? 0;
|
|
391
|
+
const count = asCount(live?.count) ?? 0;
|
|
391
392
|
const plural = count === 1 ? "subagent" : "subagents";
|
|
392
|
-
return row(
|
|
393
|
+
return row(field, `${feeMark(durable?.kind ?? live?.kind)}${formatCost(cost)} · ${count} ${plural}`);
|
|
393
394
|
}
|
|
394
395
|
case "project": {
|
|
395
396
|
const project = asRecord(source.project);
|
|
396
|
-
const cost = asCount(project?.cost);
|
|
397
|
-
if (cost === undefined) return undefined;
|
|
397
|
+
const cost = asCount(project?.cost) ?? 0;
|
|
398
398
|
const count = asCount(project?.count);
|
|
399
399
|
return row("project", `${formatCost(cost)}${count !== undefined && count > 1 ? ` · ${count} sessions` : ""}`);
|
|
400
400
|
}
|
|
@@ -402,15 +402,16 @@ export function statLine(
|
|
|
402
402
|
const tokens = asRecord(source.tokens);
|
|
403
403
|
const input = asCount(tokens?.input);
|
|
404
404
|
const output = asCount(tokens?.output);
|
|
405
|
-
|
|
405
|
+
// No token record is a measured zero, not missing data: the row always
|
|
406
|
+
// draws its honest `0 in · 0 out`.
|
|
406
407
|
return row("tokens", `${formatCount(input ?? 0)} in · ${formatCount(output ?? 0)} out`);
|
|
407
408
|
}
|
|
408
409
|
case "cache": {
|
|
409
410
|
const tokens = asRecord(source.tokens);
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
411
|
+
const cache = asRecord(tokens?.cache);
|
|
412
|
+
// An absent token record draws `0 read` rather than blanking: a numeric
|
|
413
|
+
// row always has an honest zero to show.
|
|
414
|
+
const read = asCount(cache?.read) ?? 0;
|
|
414
415
|
const input = asCount(tokens?.input);
|
|
415
416
|
// Hit ratio is the whole point: it explains a cheap bill on a huge token
|
|
416
417
|
// count, and it is the first thing to break when caching stops working.
|
|
@@ -437,8 +438,10 @@ export function statLine(
|
|
|
437
438
|
case "context": {
|
|
438
439
|
const context = asRecord(source.context);
|
|
439
440
|
const used = asCount(context?.used);
|
|
440
|
-
if (used === undefined || used === 0) return undefined;
|
|
441
441
|
const limit = asCount(context?.limit);
|
|
442
|
+
// No occupancy yet is a measured zero, drawn as `~0 used` with the same
|
|
443
|
+
// estimate mark as the populated row. A numeric row never blanks.
|
|
444
|
+
if (used === undefined || used === 0) return row("context", "~0 used");
|
|
442
445
|
// `used` is an occupancy PROXY (last request's input + cache read +
|
|
443
446
|
// cache write), not a host-reported figure, so both shapes below carry
|
|
444
447
|
// a `~`: the row reads as the estimate it is, never as an exact count.
|
|
@@ -467,8 +470,9 @@ export function statLine(
|
|
|
467
470
|
return row("perms", resource === undefined ? action : `${action} · ${clip(resource, 18)}`);
|
|
468
471
|
}
|
|
469
472
|
case "elapsed": {
|
|
470
|
-
|
|
471
|
-
|
|
473
|
+
// A session with no recorded turn time has spent zero active seconds.
|
|
474
|
+
// Draw that zero instead of hiding the numeric row.
|
|
475
|
+
const ms = asCount(source.elapsedMs) ?? 0;
|
|
472
476
|
return row("elapsed", formatDuration(ms, layout.durationStyle));
|
|
473
477
|
}
|
|
474
478
|
case "updated": {
|
|
@@ -490,42 +494,38 @@ export function statLine(
|
|
|
490
494
|
// `ttft` the mean time to first token in seconds — all unknown at this
|
|
491
495
|
// boundary, so all re-coerced like every other field. Which one draws
|
|
492
496
|
// is `layout.tpsDisplay` (the row's view of `sidebar.tps.display`,
|
|
493
|
-
// default `"
|
|
497
|
+
// default `"rt"`); presentation.ts feeds it from the resolved config.
|
|
498
|
+
// `layout.tpsTtft` is `sidebar.tps.ttft`: off, `ttft` never draws in any
|
|
499
|
+
// mode. `layout.tpsLabel` is `sidebar.tps.label`: the suffix on the two
|
|
500
|
+
// single-figure modes ("tok/s" shipped); "both" names its own halves.
|
|
494
501
|
const pair = asRecord(source.tps);
|
|
495
502
|
const rt = asCount(pair?.["rt"]);
|
|
496
503
|
const avg = asCount(pair?.["avg"]);
|
|
497
504
|
const ttft = asCount(pair?.["ttft"]);
|
|
498
|
-
const
|
|
505
|
+
const shownTtft = (layout.tpsTtft ?? true) ? ttft : undefined;
|
|
506
|
+
const display = layout.tpsDisplay ?? "rt";
|
|
507
|
+
const label = layout.tpsLabel ?? "tok/s";
|
|
508
|
+
// A numeric row never blanks. An absent or sub-half rate is a measured
|
|
509
|
+
// zero, so it draws `0 <label>` — never a placeholder and never a
|
|
510
|
+
// ttft-only fallback. Figures are whole tokens/second: the value is an
|
|
511
|
+
// EWMA over estimated delta samples, so a decimal would fake precision.
|
|
512
|
+
const rounded = (value: number | undefined): number => {
|
|
513
|
+
const value_ = value === undefined ? 0 : Math.round(value);
|
|
514
|
+
return value_ > 0 ? value_ : 0;
|
|
515
|
+
};
|
|
516
|
+
const current = rounded(rt);
|
|
517
|
+
const settled = rounded(avg);
|
|
518
|
+
const suffix = shownTtft === undefined ? "" : ` · ${shownTtft.toFixed(1)}s ttft`;
|
|
499
519
|
if (display === "rt") {
|
|
500
|
-
|
|
501
|
-
// EWMA over estimated delta samples, so a decimal would fake a
|
|
502
|
-
// precision the samples never had. Anything below half a
|
|
503
|
-
// token/second hides rather than printing "0 tok/s".
|
|
504
|
-
const rounded = rt === undefined ? undefined : Math.round(rt);
|
|
505
|
-
if (rounded === undefined || rounded <= 0) return undefined;
|
|
506
|
-
return row("tps", `${rounded} tok/s`);
|
|
520
|
+
return row("tps", current > 0 ? `${current} ${label}${suffix}` : `0 ${label}`);
|
|
507
521
|
}
|
|
508
522
|
if (display === "both") {
|
|
509
|
-
// Both
|
|
510
|
-
//
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
// The realtime-alone degrade is only for a missing average; a present
|
|
514
|
-
// average that rounds to zero hides the whole row, like the avg
|
|
515
|
-
// mode's own hide rule. Without either figure there is no row.
|
|
516
|
-
if (avg !== undefined) {
|
|
517
|
-
if (Math.round(avg) <= 0) return undefined;
|
|
518
|
-
const head = `${rt === undefined ? 0 : Math.round(rt)} rt · ${Math.round(avg)} avg`;
|
|
519
|
-
return ttft === undefined ? row("tps", head) : row("tps", `${head} · ${ttft.toFixed(1)}s ttft`);
|
|
520
|
-
}
|
|
521
|
-
if (rt !== undefined && Math.round(rt) > 0) return row("tps", `${Math.round(rt)} rt`);
|
|
522
|
-
return undefined;
|
|
523
|
+
// Both halves keep their structural names, whatever `label` says.
|
|
524
|
+
// TTFT rides only when a rate stands; a zero rate never borrows it.
|
|
525
|
+
const head = `${current} rt · ${settled} avg`;
|
|
526
|
+
return row("tps", settled > 0 ? head + suffix : head);
|
|
523
527
|
}
|
|
524
|
-
|
|
525
|
-
// rounded. The hide rule is the realtime row's own: at or below half a
|
|
526
|
-
// token/second it hides rather than printing "0 tok/s".
|
|
527
|
-
if (avg === undefined || Math.round(avg) <= 0) return undefined;
|
|
528
|
-
return row("tps", `${Math.round(avg)} tok/s`);
|
|
528
|
+
return row("tps", `${settled} ${label}`);
|
|
529
529
|
}
|
|
530
530
|
case "spark": {
|
|
531
531
|
const values = Array.isArray(source.spark) ? source.spark.map((value) => asCount(value) ?? 0) : [];
|
|
@@ -536,8 +536,9 @@ export function statLine(
|
|
|
536
536
|
return row("spark", sparkline(windowed));
|
|
537
537
|
}
|
|
538
538
|
case "reasoning": {
|
|
539
|
-
|
|
540
|
-
|
|
539
|
+
// A model that emits no reasoning tokens still reads `0` on the row;
|
|
540
|
+
// the count keeps the same unit as a populated figure.
|
|
541
|
+
const reasoning = asCount(asRecord(source.tokens)?.reasoning) ?? 0;
|
|
541
542
|
return row("reasoning", formatCount(reasoning));
|
|
542
543
|
}
|
|
543
544
|
case "turns": {
|
|
@@ -638,18 +639,22 @@ export function statRows(
|
|
|
638
639
|
source: StatSource,
|
|
639
640
|
layout: LayoutHint = {},
|
|
640
641
|
): readonly string[] {
|
|
641
|
-
// The rows list decides whether `
|
|
642
|
-
//
|
|
643
|
-
//
|
|
644
|
-
const hint: LayoutHint = {
|
|
642
|
+
// The rows list decides whether `spend` merges the subagent total into
|
|
643
|
+
// itself. Both `lifetime` and its `total` alias count, so an old config keeps
|
|
644
|
+
// its one-money-row-per-figure shape.
|
|
645
|
+
const hint: LayoutHint = {
|
|
646
|
+
...layout,
|
|
647
|
+
hasLifetimeRow: fields.some((field) => canonicalField(field) === "lifetime"),
|
|
648
|
+
};
|
|
645
649
|
const persist = hint.persist === true;
|
|
646
650
|
const placeholder = hint.placeholder ?? DEFAULT_PLACEHOLDER;
|
|
647
651
|
const labelWidth = hint.labelWidth ?? DEFAULT_LABEL_WIDTH;
|
|
648
652
|
const rows: string[] = [];
|
|
649
653
|
for (const field of fields) {
|
|
650
|
-
//
|
|
654
|
+
// The renderer canonicalizes internally; the original spelling is kept so
|
|
655
|
+
// an alias still draws its old label byte-for-byte.
|
|
651
656
|
const name = canonicalField(field);
|
|
652
|
-
const line = statLine(
|
|
657
|
+
const line = statLine(field, source, hint);
|
|
653
658
|
if (line !== undefined) {
|
|
654
659
|
rows.push(line);
|
|
655
660
|
continue;
|
package/src/tui/stat-fields.ts
CHANGED
|
@@ -40,6 +40,11 @@ export interface StatSource {
|
|
|
40
40
|
*/
|
|
41
41
|
readonly sessionId?: unknown;
|
|
42
42
|
readonly tree?: unknown;
|
|
43
|
+
/**
|
|
44
|
+
* The durable high-water family figure for the lifetime row, when the host's
|
|
45
|
+
* current `tree` has shrunk. Untrusted like every other field.
|
|
46
|
+
*/
|
|
47
|
+
readonly lifetime?: unknown;
|
|
43
48
|
readonly context?: unknown;
|
|
44
49
|
readonly project?: unknown;
|
|
45
50
|
readonly status?: unknown;
|
|
@@ -90,8 +95,8 @@ export const STAT_FIELDS = [
|
|
|
90
95
|
"agent",
|
|
91
96
|
"model",
|
|
92
97
|
"branch",
|
|
93
|
-
"
|
|
94
|
-
"
|
|
98
|
+
"spend",
|
|
99
|
+
"lifetime",
|
|
95
100
|
"project",
|
|
96
101
|
"tokens",
|
|
97
102
|
"cache",
|
|
@@ -128,23 +133,32 @@ type StatField = (typeof STAT_FIELDS)[number];
|
|
|
128
133
|
export const ANIMATED_FIELDS = ["caution", "status", "elapsed", "tps"] as const;
|
|
129
134
|
|
|
130
135
|
export function isStatField(value: string): value is StatField {
|
|
131
|
-
// `session`
|
|
136
|
+
// `session`, `cost` and `total` are accepted aliases, folded back by
|
|
132
137
|
// `canonicalField`, but kept out of `STAT_FIELDS` on purpose — that array is
|
|
133
|
-
// the config vocabulary echoed in config error messages
|
|
134
|
-
//
|
|
135
|
-
return
|
|
138
|
+
// the canonical config vocabulary echoed in config error messages. The
|
|
139
|
+
// aliases stay accepted forever so no existing config breaks.
|
|
140
|
+
return (
|
|
141
|
+
value === "session" ||
|
|
142
|
+
value === "cost" ||
|
|
143
|
+
value === "total" ||
|
|
144
|
+
STAT_FIELDS.some((field) => field === value)
|
|
145
|
+
);
|
|
136
146
|
}
|
|
137
147
|
|
|
138
148
|
/**
|
|
139
149
|
* The canonical config key for a field name.
|
|
140
150
|
*
|
|
141
|
-
* `session`
|
|
142
|
-
* other name is returned unchanged. The renderers canonicalize their
|
|
143
|
-
* argument at entry, so
|
|
144
|
-
*
|
|
151
|
+
* `session` folds to `ses`, `cost` to `spend`, and `total` to `lifetime`.
|
|
152
|
+
* Every other name is returned unchanged. The renderers canonicalize their
|
|
153
|
+
* field argument at entry, so an alias and its key take the same behavioural
|
|
154
|
+
* path; the caller's original spelling is kept for the label, so an old config
|
|
155
|
+
* still draws the same bytes.
|
|
145
156
|
*/
|
|
146
157
|
export function canonicalField(name: string): string {
|
|
147
|
-
|
|
158
|
+
if (name === "session") return "ses";
|
|
159
|
+
if (name === "cost") return "spend";
|
|
160
|
+
if (name === "total") return "lifetime";
|
|
161
|
+
return name;
|
|
148
162
|
}
|
|
149
163
|
|
|
150
164
|
/**
|
|
@@ -189,13 +203,13 @@ export interface LayoutHint {
|
|
|
189
203
|
*/
|
|
190
204
|
readonly timestamp?: TimestampFormat;
|
|
191
205
|
/**
|
|
192
|
-
* True when the rail already draws a `total`
|
|
206
|
+
* True when the rail already draws a `lifetime` row (or its `total` alias).
|
|
193
207
|
*
|
|
194
|
-
* The `
|
|
195
|
-
* on the rail already shows it — which makes "
|
|
196
|
-
*
|
|
208
|
+
* The `spend` row merges the subagent total into itself only when nothing
|
|
209
|
+
* else on the rail already shows it — which makes "one money row or two" a
|
|
210
|
+
* property of `sidebar.rows`, not a separate setting to find.
|
|
197
211
|
*/
|
|
198
|
-
readonly
|
|
212
|
+
readonly hasLifetimeRow?: boolean;
|
|
199
213
|
/**
|
|
200
214
|
* Per-row label overrides, keyed by the canonical row name (`ses`, not
|
|
201
215
|
* `session`). Carried here rather than read from config directly so both
|
|
@@ -204,8 +218,8 @@ export interface LayoutHint {
|
|
|
204
218
|
*/
|
|
205
219
|
readonly labels?: Readonly<Record<string, string>>;
|
|
206
220
|
/**
|
|
207
|
-
* Which figure the `tps` row draws: the settled-turn average (`"avg"
|
|
208
|
-
* default
|
|
221
|
+
* Which figure the `tps` row draws: the settled-turn average (`"avg"`),
|
|
222
|
+
* the default realtime estimate from the host's streaming delta events
|
|
209
223
|
* (`"rt"`), or both on one line (`"both"`). Carried here rather than read
|
|
210
224
|
* from config directly so the row stays a pure function of its inputs,
|
|
211
225
|
* exactly like `labels` above.
|
|
@@ -215,6 +229,21 @@ export interface LayoutHint {
|
|
|
215
229
|
* imports this one.
|
|
216
230
|
*/
|
|
217
231
|
readonly tpsDisplay?: "avg" | "rt" | "both";
|
|
232
|
+
/**
|
|
233
|
+
* Suffix printed after the number on the single-figure `tps` rows (`rt`
|
|
234
|
+
* and `avg`), from `sidebar.tps.label`. Default `"tok/s"`. `both` names
|
|
235
|
+
* its halves itself and ignores this. Structurally typed, never the
|
|
236
|
+
* config's own type: this module must not import config.
|
|
237
|
+
*/
|
|
238
|
+
readonly tpsLabel?: string;
|
|
239
|
+
/**
|
|
240
|
+
* Whether the `tps` row may draw the observed mean time to first token
|
|
241
|
+
* (`1.2s ttft`). Off suppresses that segment in every display mode,
|
|
242
|
+
* including the realtime fallback row shown when no rate can be drawn.
|
|
243
|
+
* Default true. Carried here as the config's boolean, never its own type:
|
|
244
|
+
* like the other hints, this module must not import config.
|
|
245
|
+
*/
|
|
246
|
+
readonly tpsTtft?: boolean;
|
|
218
247
|
/**
|
|
219
248
|
* When true, `statRows` renders one row per named field even when the host
|
|
220
249
|
* has no data for it, using `skeletonValue` (a live-shaped zero) and falling
|
|
@@ -236,7 +265,7 @@ export interface LayoutHint {
|
|
|
236
265
|
}
|
|
237
266
|
|
|
238
267
|
/** Fallback placeholder for a persistent row with no data and no skeleton. */
|
|
239
|
-
export const DEFAULT_PLACEHOLDER = "
|
|
268
|
+
export const DEFAULT_PLACEHOLDER = "0";
|
|
240
269
|
|
|
241
270
|
export const DEFAULT_LABEL_WIDTH = 10;
|
|
242
271
|
export const DEFAULT_SPARK_WIDTH = 12;
|
|
@@ -256,8 +285,8 @@ export const DEFAULT_SPARK_WIDTH = 12;
|
|
|
256
285
|
*/
|
|
257
286
|
export function skeletonValue(field: string, layout: LayoutHint): string | undefined {
|
|
258
287
|
switch (canonicalField(field)) {
|
|
259
|
-
case "
|
|
260
|
-
case "
|
|
288
|
+
case "spend":
|
|
289
|
+
case "lifetime":
|
|
261
290
|
case "project":
|
|
262
291
|
return formatCost(0);
|
|
263
292
|
case "tokens":
|
|
@@ -270,11 +299,13 @@ export function skeletonValue(field: string, layout: LayoutHint): string | undef
|
|
|
270
299
|
return "0 waiting";
|
|
271
300
|
case "elapsed":
|
|
272
301
|
return formatDuration(0, layout.durationStyle);
|
|
273
|
-
case "tps":
|
|
274
|
-
//
|
|
275
|
-
//
|
|
276
|
-
//
|
|
277
|
-
|
|
302
|
+
case "tps": {
|
|
303
|
+
// Same zero the row itself draws: an absent rate is a measured zero,
|
|
304
|
+
// never a placeholder. Kept here for callers that ask for a skeleton
|
|
305
|
+
// directly, even though the row now always renders one.
|
|
306
|
+
const label = layout.tpsLabel ?? "tok/s";
|
|
307
|
+
return layout.tpsDisplay === "both" ? `0 rt · 0 avg` : `0 ${label}`;
|
|
308
|
+
}
|
|
278
309
|
case "spark":
|
|
279
310
|
return "▁".repeat(Math.max(0, Math.floor(layout.sparkWidth ?? DEFAULT_SPARK_WIDTH)));
|
|
280
311
|
case "reasoning":
|