oc-flight-deck 0.1.2 → 0.2.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/src/tui/stats.ts CHANGED
@@ -30,6 +30,14 @@ export interface StatProject {
30
30
 
31
31
  /** The subset of a session snapshot the rail reads. All fields are untrusted. */
32
32
  export interface StatSource {
33
+ /**
34
+ * The annunciator line, precomputed by the caller.
35
+ *
36
+ * A plain string on purpose: the rules that decide whether something is wrong
37
+ * live in ./caution.ts and need the clock and the session snapshot, not a
38
+ * formatting function. Severity is already encoded in the glyph.
39
+ */
40
+ readonly caution?: unknown;
33
41
  readonly agent?: unknown;
34
42
  readonly model?: unknown;
35
43
  readonly cost?: unknown;
@@ -53,6 +61,7 @@ export interface StatSource {
53
61
 
54
62
  /** Fields a user may name in `sidebar.rows`, in the order they are documented. */
55
63
  export const STAT_FIELDS = [
64
+ "caution",
56
65
  "status",
57
66
  "agent",
58
67
  "model",
@@ -78,20 +87,42 @@ export type StatField = (typeof STAT_FIELDS)[number];
78
87
  *
79
88
  * The ticker exists to animate these. When none of them is on screen there is
80
89
  * nothing to animate, so the plugin does not start a timer at all.
90
+ *
91
+ * `caution` belongs here for a stronger reason than the others: its whole job is
92
+ * noticing that time has passed without anything happening, so without a tick
93
+ * its thresholds could never be crossed on screen.
81
94
  */
82
- export const ANIMATED_FIELDS = ["status", "elapsed"] as const;
95
+ export const ANIMATED_FIELDS = ["caution", "status", "elapsed"] as const;
83
96
 
84
97
  export function isStatField(value: string): value is StatField {
85
98
  return (STAT_FIELDS as readonly string[]).includes(value);
86
99
  }
87
100
 
88
- const LABEL_WIDTH = 10;
89
- const BAR_WIDTH = 10;
90
-
91
- function row(label: string, value: string): string {
92
- return `${label.padEnd(LABEL_WIDTH)}${value}`;
101
+ /**
102
+ * Row geometry, overridable per call.
103
+ *
104
+ * A structural type rather than an import of `FlightDeckConfig`: ./config.ts
105
+ * imports this module, so reaching back into it would create a cycle. The
106
+ * shapes are compatible, so the config passes straight through.
107
+ */
108
+ export interface LayoutHint {
109
+ readonly labelWidth?: number;
110
+ readonly barWidth?: number;
111
+ readonly sparkWidth?: number;
112
+ /**
113
+ * True when the rail already draws a `total` row.
114
+ *
115
+ * The `cost` row merges the subagent total into itself only when nothing else
116
+ * on the rail already shows it — which makes "do I want one money row or two"
117
+ * a property of `sidebar.rows`, not a separate setting to find.
118
+ */
119
+ readonly hasTotalRow?: boolean;
93
120
  }
94
121
 
122
+ const DEFAULT_LABEL_WIDTH = 10;
123
+ const DEFAULT_BAR_WIDTH = 10;
124
+ const DEFAULT_SPARK_WIDTH = 12;
125
+
95
126
  function asRecord(value: unknown): Record<string, unknown> | undefined {
96
127
  return typeof value === "object" && value !== null && !Array.isArray(value)
97
128
  ? (value as Record<string, unknown>)
@@ -133,13 +164,25 @@ export function formatDuration(ms: number): string {
133
164
  }
134
165
 
135
166
  /** A ten-cell gauge, so a percentage is readable at a glance rather than parsed. */
136
- export function fuelBar(ratio: number): string {
137
- const filled = Math.max(0, Math.min(BAR_WIDTH, Math.round(ratio * BAR_WIDTH)));
138
- return `${"█".repeat(filled)}${"░".repeat(BAR_WIDTH - filled)}`;
167
+ export function fuelBar(ratio: number, width: number = DEFAULT_BAR_WIDTH): string {
168
+ const cells = Math.max(1, Math.floor(width));
169
+ const filled = Math.max(0, Math.min(cells, Math.round(ratio * cells)));
170
+ return `${"█".repeat(filled)}${"░".repeat(cells - filled)}`;
139
171
  }
140
172
 
141
173
  const SPARK_LEVELS = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"] as const;
142
174
 
175
+ /**
176
+ * Shorten a value for a narrow rail.
177
+ *
178
+ * The sidebar is roughly thirty-odd columns wide and the label already costs
179
+ * ten, so a resource path has to be cut. The ellipsis is deliberate: a silently
180
+ * truncated path reads as a complete one.
181
+ */
182
+ export function clip(text: string, max: number): string {
183
+ return text.length <= max ? text : `${text.slice(0, Math.max(1, max - 1))}…`;
184
+ }
185
+
143
186
  /** Braille frames, advanced by the ticker, shown only while the session runs. */
144
187
  const SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] as const;
145
188
 
@@ -168,8 +211,27 @@ export function sparkline(values: readonly number[]): string {
168
211
  * shows `branch unknown` before the repo loads looks broken, while a rail
169
212
  * that simply grows a row when the data arrives looks alive.
170
213
  */
171
- export function statLine(field: string, source: StatSource): string | undefined {
214
+ export function statLine(
215
+ field: string,
216
+ source: StatSource,
217
+ layout: LayoutHint = {},
218
+ ): string | undefined {
219
+ // Bound here rather than at module scope so a config file can change the
220
+ // column width without every call site having to know about it.
221
+ const labelWidth = layout.labelWidth ?? DEFAULT_LABEL_WIDTH;
222
+ const barWidth = layout.barWidth ?? DEFAULT_BAR_WIDTH;
223
+ const sparkWidth = layout.sparkWidth ?? DEFAULT_SPARK_WIDTH;
224
+ const row = (label: string, value: string): string => `${label.padEnd(labelWidth)}${value}`;
225
+
172
226
  switch (field) {
227
+ case "caution": {
228
+ // Rendered only when the caller found something to say, which is what
229
+ // keeps a healthy session visually identical to one without the
230
+ // annunciator at all. The string already carries its glyph; nothing here
231
+ // knows or cares about severity.
232
+ const text = asText(source.caution);
233
+ return text === undefined ? undefined : row("caution", text);
234
+ }
173
235
  case "status": {
174
236
  const status = asText(source.status);
175
237
  if (status === undefined && source.busy === undefined) return undefined;
@@ -203,7 +265,33 @@ export function statLine(field: string, source: StatSource): string | undefined
203
265
  }
204
266
  case "cost": {
205
267
  const cost = asCount(source.cost);
206
- return cost === undefined ? undefined : row("cost", formatCost(cost));
268
+ const tree = asRecord(source.tree);
269
+ const treeCost = asCount(tree?.cost);
270
+ const count = asCount(tree?.count);
271
+ const family = count !== undefined && count > 0 ? count : 0;
272
+
273
+ // Merge only when subagents actually ran, and only when the rail is not
274
+ // already showing a `total` row. Two rows holding a number and its own
275
+ // superset was the thing worth fixing; a setting to choose between them
276
+ // would just be that duplication with extra steps.
277
+ const merge =
278
+ layout.hasTotalRow !== true &&
279
+ cost !== undefined &&
280
+ treeCost !== undefined &&
281
+ family > 0 &&
282
+ treeCost > cost;
283
+
284
+ if (!merge) {
285
+ const value = layout.hasTotalRow === true ? (cost ?? treeCost) : (treeCost ?? cost);
286
+ return value === undefined ? undefined : row("cost", formatCost(value));
287
+ }
288
+
289
+ // The delta (`+$0.020`) used to be here and pushed the row to 40 columns,
290
+ // wide enough to wrap in a normal sidebar. It was real information that
291
+ // did not earn its width: the count already explains why the figure is
292
+ // higher, and this form is the same width as the old `total` row.
293
+ const plural = family === 1 ? "subagent" : "subagents";
294
+ return row("cost", `${formatCost(treeCost as number)} · ${family} ${plural}`);
207
295
  }
208
296
  case "total": {
209
297
  // Only worth a row once a subagent has actually run: subagent sessions are
@@ -253,12 +341,25 @@ export function statLine(field: string, source: StatSource): string | undefined
253
341
  const ratio = used / limit;
254
342
  // Show the percentage whenever the host reports it, but never a bar that
255
343
  // reads as more than full.
256
- return row("context", `${fuelBar(ratio)} ${Math.round(Math.min(1, ratio) * 100)}%`);
344
+ return row("context", `${fuelBar(ratio, barWidth)} ${Math.round(Math.min(1, ratio) * 100)}%`);
257
345
  }
258
346
  case "perms": {
259
- const perms = asCount(source.perms);
260
- if (perms === undefined || perms === 0) return undefined;
261
- return row("perms", perms === 1 ? "1 waiting" : `${perms} waiting`);
347
+ const perms = asRecord(source.perms);
348
+ const count = asCount(perms?.["count"]);
349
+ if (count === undefined || count === 0) return undefined;
350
+ const action = asText(perms?.["action"]);
351
+ const resource = asText(perms?.["resource"]);
352
+
353
+ // One request: name it, because that is the decision you are being asked
354
+ // to make. Several: the count leads, because the first request in the
355
+ // queue is not necessarily the one you are about to be shown.
356
+ if (count > 1) {
357
+ return row("perms", action === undefined ? `${count} waiting` : `${count} waiting · ${action}`);
358
+ }
359
+ if (action === undefined) return row("perms", "1 waiting");
360
+ // Clipped hard: a row that wraps costs the reader more than a row that
361
+ // shortens a path, and there is no way to ask the host how wide it is.
362
+ return row("perms", resource === undefined ? action : `${action} · ${clip(resource, 18)}`);
262
363
  }
263
364
  case "elapsed": {
264
365
  const ms = asCount(source.elapsedMs);
@@ -272,8 +373,11 @@ export function statLine(field: string, source: StatSource): string | undefined
272
373
  }
273
374
  case "spark": {
274
375
  const values = Array.isArray(source.spark) ? source.spark.map((value) => asCount(value) ?? 0) : [];
275
- if (values.length < 2) return undefined;
276
- return row("spark", sparkline(values));
376
+ // The newest samples are the interesting ones, so a narrowed window keeps
377
+ // the recent shape rather than the oldest.
378
+ const windowed = values.slice(-Math.max(2, Math.floor(sparkWidth)));
379
+ if (windowed.length < 2) return undefined;
380
+ return row("spark", sparkline(windowed));
277
381
  }
278
382
  case "reasoning": {
279
383
  const reasoning = asCount(asRecord(source.tokens)?.reasoning);
@@ -291,10 +395,18 @@ export function statLine(field: string, source: StatSource): string | undefined
291
395
  }
292
396
 
293
397
  /** Render every named field that currently has data, in the given order. */
294
- export function statRows(fields: readonly string[], source: StatSource): readonly string[] {
398
+ export function statRows(
399
+ fields: readonly string[],
400
+ source: StatSource,
401
+ layout: LayoutHint = {},
402
+ ): readonly string[] {
403
+ // The rows list decides whether `cost` merges the subagent total into itself.
404
+ // Passing that down means the choice lives in `sidebar.rows`, where someone
405
+ // is already deciding what the rail shows.
406
+ const hint: LayoutHint = { ...layout, hasTotalRow: fields.includes("total") };
295
407
  const rows: string[] = [];
296
408
  for (const field of fields) {
297
- const line = statLine(field, source);
409
+ const line = statLine(field, source, hint);
298
410
  if (line !== undefined) rows.push(line);
299
411
  }
300
412
  return rows;