oc-flight-deck 0.1.2 → 0.2.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/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,34 @@ 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;
93
112
  }
94
113
 
114
+ const DEFAULT_LABEL_WIDTH = 10;
115
+ const DEFAULT_BAR_WIDTH = 10;
116
+ const DEFAULT_SPARK_WIDTH = 12;
117
+
95
118
  function asRecord(value: unknown): Record<string, unknown> | undefined {
96
119
  return typeof value === "object" && value !== null && !Array.isArray(value)
97
120
  ? (value as Record<string, unknown>)
@@ -133,13 +156,25 @@ export function formatDuration(ms: number): string {
133
156
  }
134
157
 
135
158
  /** 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)}`;
159
+ export function fuelBar(ratio: number, width: number = DEFAULT_BAR_WIDTH): string {
160
+ const cells = Math.max(1, Math.floor(width));
161
+ const filled = Math.max(0, Math.min(cells, Math.round(ratio * cells)));
162
+ return `${"█".repeat(filled)}${"░".repeat(cells - filled)}`;
139
163
  }
140
164
 
141
165
  const SPARK_LEVELS = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"] as const;
142
166
 
167
+ /**
168
+ * Shorten a value for a narrow rail.
169
+ *
170
+ * The sidebar is roughly thirty-odd columns wide and the label already costs
171
+ * ten, so a resource path has to be cut. The ellipsis is deliberate: a silently
172
+ * truncated path reads as a complete one.
173
+ */
174
+ export function clip(text: string, max: number): string {
175
+ return text.length <= max ? text : `${text.slice(0, Math.max(1, max - 1))}…`;
176
+ }
177
+
143
178
  /** Braille frames, advanced by the ticker, shown only while the session runs. */
144
179
  const SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] as const;
145
180
 
@@ -168,8 +203,27 @@ export function sparkline(values: readonly number[]): string {
168
203
  * shows `branch unknown` before the repo loads looks broken, while a rail
169
204
  * that simply grows a row when the data arrives looks alive.
170
205
  */
171
- export function statLine(field: string, source: StatSource): string | undefined {
206
+ export function statLine(
207
+ field: string,
208
+ source: StatSource,
209
+ layout: LayoutHint = {},
210
+ ): string | undefined {
211
+ // Bound here rather than at module scope so a config file can change the
212
+ // column width without every call site having to know about it.
213
+ const labelWidth = layout.labelWidth ?? DEFAULT_LABEL_WIDTH;
214
+ const barWidth = layout.barWidth ?? DEFAULT_BAR_WIDTH;
215
+ const sparkWidth = layout.sparkWidth ?? DEFAULT_SPARK_WIDTH;
216
+ const row = (label: string, value: string): string => `${label.padEnd(labelWidth)}${value}`;
217
+
172
218
  switch (field) {
219
+ case "caution": {
220
+ // Rendered only when the caller found something to say, which is what
221
+ // keeps a healthy session visually identical to one without the
222
+ // annunciator at all. The string already carries its glyph; nothing here
223
+ // knows or cares about severity.
224
+ const text = asText(source.caution);
225
+ return text === undefined ? undefined : row("caution", text);
226
+ }
173
227
  case "status": {
174
228
  const status = asText(source.status);
175
229
  if (status === undefined && source.busy === undefined) return undefined;
@@ -203,7 +257,26 @@ export function statLine(field: string, source: StatSource): string | undefined
203
257
  }
204
258
  case "cost": {
205
259
  const cost = asCount(source.cost);
206
- return cost === undefined ? undefined : row("cost", formatCost(cost));
260
+ const tree = asRecord(source.tree);
261
+ const treeCost = asCount(tree?.cost);
262
+ const count = asCount(tree?.count);
263
+
264
+ // When subagents ran, this session's own cost understates the bill. Show
265
+ // the family total and the delta that produced it, rather than spending a
266
+ // second row on a number that contains the first. `total` still exists for
267
+ // anyone who wants the two figures on separate rows.
268
+ if (
269
+ cost !== undefined &&
270
+ treeCost !== undefined &&
271
+ count !== undefined &&
272
+ count > 0 &&
273
+ treeCost > cost
274
+ ) {
275
+ const plural = count === 1 ? "subagent" : "subagents";
276
+ return row("cost", `${formatCost(treeCost)} · +${formatCost(treeCost - cost)} · ${count} ${plural}`);
277
+ }
278
+ const value = cost ?? treeCost;
279
+ return value === undefined ? undefined : row("cost", formatCost(value));
207
280
  }
208
281
  case "total": {
209
282
  // Only worth a row once a subagent has actually run: subagent sessions are
@@ -253,12 +326,23 @@ export function statLine(field: string, source: StatSource): string | undefined
253
326
  const ratio = used / limit;
254
327
  // Show the percentage whenever the host reports it, but never a bar that
255
328
  // reads as more than full.
256
- return row("context", `${fuelBar(ratio)} ${Math.round(Math.min(1, ratio) * 100)}%`);
329
+ return row("context", `${fuelBar(ratio, barWidth)} ${Math.round(Math.min(1, ratio) * 100)}%`);
257
330
  }
258
331
  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`);
332
+ const perms = asRecord(source.perms);
333
+ const count = asCount(perms?.["count"]);
334
+ if (count === undefined || count === 0) return undefined;
335
+ const action = asText(perms?.["action"]);
336
+ const resource = asText(perms?.["resource"]);
337
+
338
+ // One request: name it, because that is the decision you are being asked
339
+ // to make. Several: the count leads, because the first request in the
340
+ // queue is not necessarily the one you are about to be shown.
341
+ if (count > 1) {
342
+ return row("perms", action === undefined ? `${count} waiting` : `${count} waiting · ${action}`);
343
+ }
344
+ if (action === undefined) return row("perms", "1 waiting");
345
+ return row("perms", resource === undefined ? action : `${action} · ${clip(resource, 22)}`);
262
346
  }
263
347
  case "elapsed": {
264
348
  const ms = asCount(source.elapsedMs);
@@ -272,8 +356,11 @@ export function statLine(field: string, source: StatSource): string | undefined
272
356
  }
273
357
  case "spark": {
274
358
  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));
359
+ // The newest samples are the interesting ones, so a narrowed window keeps
360
+ // the recent shape rather than the oldest.
361
+ const windowed = values.slice(-Math.max(2, Math.floor(sparkWidth)));
362
+ if (windowed.length < 2) return undefined;
363
+ return row("spark", sparkline(windowed));
277
364
  }
278
365
  case "reasoning": {
279
366
  const reasoning = asCount(asRecord(source.tokens)?.reasoning);
@@ -291,10 +378,14 @@ export function statLine(field: string, source: StatSource): string | undefined
291
378
  }
292
379
 
293
380
  /** Render every named field that currently has data, in the given order. */
294
- export function statRows(fields: readonly string[], source: StatSource): readonly string[] {
381
+ export function statRows(
382
+ fields: readonly string[],
383
+ source: StatSource,
384
+ layout: LayoutHint = {},
385
+ ): readonly string[] {
295
386
  const rows: string[] = [];
296
387
  for (const field of fields) {
297
- const line = statLine(field, source);
388
+ const line = statLine(field, source, layout);
298
389
  if (line !== undefined) rows.push(line);
299
390
  }
300
391
  return rows;