oc-flight-deck 0.1.1 → 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/config.ts CHANGED
@@ -9,6 +9,7 @@
9
9
  // defaults below, so the rail always renders and a typo can never break the TUI.
10
10
 
11
11
  import { isStatField, STAT_FIELDS } from "./stats.js";
12
+ import type { CautionThresholds } from "./caution.js";
12
13
 
13
14
  export interface SidebarConfig {
14
15
  /** Show the sidebar rail. Default `true`. */
@@ -32,19 +33,126 @@ export interface FooterConfig {
32
33
  readonly text: string;
33
34
  }
34
35
 
36
+ export interface CautionConfig {
37
+ /** Show the caution annunciator. Default `true`. */
38
+ readonly enabled: boolean;
39
+ /** A tool running longer than this is worth noting. Default `180`. */
40
+ readonly toolWatchSeconds: number;
41
+ /** ... and longer than this is a real caution. Default `420`. */
42
+ readonly toolCautionSeconds: number;
43
+ /** A running session quiet for this long is worth noting. Default `600`. */
44
+ readonly turnWatchSeconds: number;
45
+ /** ... and quieter than this is a real caution. Default `1200`. */
46
+ readonly turnCautionSeconds: number;
47
+ /** Identical consecutive calls before it counts as a loop. Default `3`. */
48
+ readonly repeatThreshold: number;
49
+ /**
50
+ * Tools that are slow by nature, so "slow" is not an anomaly for them.
51
+ *
52
+ * Calibrated against 53,672 real settled tool calls: at a 180-second watch
53
+ * threshold, 307 calls exceed it — and 218 of those are these tools.
54
+ * `subagent` alone accounts for 152, with a p99 of 14.7 minutes. A delegated
55
+ * agent running a quarter of an hour is working as designed, and an
56
+ * annunciator that lights on every delegation is one you learn to ignore.
57
+ * With these exempt, 0.16% of real calls cross the threshold.
58
+ *
59
+ * Matching is by exact tool name. Add your own long-running tools.
60
+ */
61
+ readonly exemptTools: readonly string[];
62
+ /** Raise a toast the first time a caution appears, once per distinct problem. */
63
+ readonly toast: boolean;
64
+ }
65
+
35
66
  export interface FlightDeckConfig {
36
67
  readonly sidebar: SidebarConfig;
37
68
  readonly footer: FooterConfig;
69
+ /**
70
+ * The annunciator: the one thing on the rail that is not session state.
71
+ *
72
+ * It exists because a hang emits no events, so nothing else here can see it.
73
+ * Silent when healthy, which is why it costs nothing visually.
74
+ */
75
+ readonly caution: CautionConfig;
76
+ /** Geometry of a row. Every value has a sane default. */
77
+ readonly layout: LayoutConfig;
78
+ /** Glyphs for the annunciator, for terminals that render the defaults badly. */
79
+ readonly glyphs: GlyphConfig;
38
80
  /**
39
81
  * Update cadence in milliseconds for time-derived rows.
40
82
  *
41
83
  * Cost and tokens arrive with events, so they stay current without a timer.
42
- * Anything derived from the clock — `elapsed` especially — has nothing to
43
- * react to, so it needs a tick. `0` turns the ticker off entirely.
84
+ * Anything derived from the clock — `elapsed`, the status spinner's frames,
85
+ * and the annunciator's escalating thresholds — has nothing to react to, so
86
+ * it needs a tick. `0` turns the ticker off, leaving every row event-driven.
44
87
  */
45
88
  readonly refresh: number;
46
89
  }
47
90
 
91
+ /** The millisecond view the rules in ./caution.ts operate on. */
92
+ export function cautionThresholds(config: CautionConfig): CautionThresholds {
93
+ return {
94
+ toolWatchMs: config.toolWatchSeconds * 1_000,
95
+ toolCautionMs: config.toolCautionSeconds * 1_000,
96
+ turnWatchMs: config.turnWatchSeconds * 1_000,
97
+ turnCautionMs: config.turnCautionSeconds * 1_000,
98
+ repeatThreshold: config.repeatThreshold,
99
+ exemptTools: config.exemptTools,
100
+ };
101
+ }
102
+
103
+ /** Pure cosmetics: the geometry of a row. */
104
+ export interface LayoutConfig {
105
+ /** Width of the row label column. Default `10`. */
106
+ readonly labelWidth: number;
107
+ /** Cells in the context gauge. Default `10`. */
108
+ readonly barWidth: number;
109
+ /** Samples drawn in the sparkline. Default `12`. */
110
+ readonly sparkWidth: number;
111
+ }
112
+
113
+ /**
114
+ * The annunciator's glyphs.
115
+ *
116
+ * Configurable because this is the one row that has to read at a glance, and
117
+ * some terminals render `⚠` as a box or the wrong width. Swapping it for `!` is
118
+ * a worse-looking but working panel, which beats an unreadable one.
119
+ */
120
+ export interface GlyphConfig {
121
+ readonly watch: string;
122
+ readonly caution: string;
123
+ readonly clear: string;
124
+ }
125
+
126
+ export const DEFAULT_LAYOUT: LayoutConfig = {
127
+ labelWidth: 10,
128
+ barWidth: 10,
129
+ sparkWidth: 12,
130
+ };
131
+
132
+ export const DEFAULT_GLYPHS: GlyphConfig = {
133
+ watch: "▲",
134
+ caution: "⚠",
135
+ clear: "○",
136
+ };
137
+
138
+ export const DEFAULT_CAUTION: CautionConfig = {
139
+ enabled: true,
140
+ toolWatchSeconds: 180,
141
+ toolCautionSeconds: 420,
142
+ turnWatchSeconds: 600,
143
+ turnCautionSeconds: 1200,
144
+ repeatThreshold: 3,
145
+ exemptTools: ["question", "task", "subagent", "agent", "delegate", "delegate_many", "delegate_task"],
146
+ /**
147
+ * Off by default. The rail is the signal; a toast is an interruption.
148
+ *
149
+ * The annunciator's job is to sit there and be ignorable until it isn't, and a
150
+ * notification that takes over the screen is the opposite of that. It is worth
151
+ * turning on if you walk away from long runs, which is why it exists at all.
152
+ */
153
+ toast: false,
154
+ };
155
+
48
156
  /**
49
157
  * Fixed lines shown above the live rows — branding only.
50
158
  *
@@ -63,12 +171,12 @@ export const DEFAULT_SIDEBAR_LINES: readonly string[] = [
63
171
  * install shows real numbers with no configuration file at all.
64
172
  */
65
173
  export const DEFAULT_SIDEBAR_ROWS: readonly string[] = [
174
+ "caution",
66
175
  "status",
67
176
  "agent",
68
177
  "model",
69
178
  "branch",
70
179
  "cost",
71
- "total",
72
180
  "project",
73
181
  "tokens",
74
182
  "cache",
@@ -76,20 +184,35 @@ export const DEFAULT_SIDEBAR_ROWS: readonly string[] = [
76
184
  "perms",
77
185
  "elapsed",
78
186
  "tps",
79
- "spark",
80
187
  ];
81
188
 
82
- /** One tick a second: enough for the clock-derived rows, cheap enough to ignore. */
83
- export const DEFAULT_REFRESH_MS = 1_000;
189
+ /**
190
+ * Ten ticks a second: the status spinner has ten frames, so one rotation takes a
191
+ * second and reads as motion instead of as a stuck glyph. A tick costs one
192
+ * host-store write plus a recompute measured in microseconds, and when nothing
193
+ * on the rail changed the host's renderer diffs the frame away to nothing.
194
+ * Raise it to spend less on idle sessions; `0` turns the ticker off entirely.
195
+ */
196
+ export const DEFAULT_REFRESH_MS = 100;
84
197
 
85
198
  /** A tick slower than once a minute is indistinguishable from no ticker. */
86
199
  const MAX_REFRESH_MS = 60_000;
87
200
 
201
+ /**
202
+ * A tick wakes the host's renderer, so a sub-frame interval from a config file
203
+ * is a way to wedge the TUI rather than a way to make it smoother. 16ms is one
204
+ * frame at 60Hz; below that there is nothing left to animate.
205
+ */
206
+ export const MIN_REFRESH_MS = 16;
207
+
88
208
  export const DEFAULT_FOOTER_TEXT = "Flight Deck";
89
209
 
90
210
  export const DEFAULT_CONFIG: FlightDeckConfig = {
91
211
  sidebar: { enabled: true, lines: DEFAULT_SIDEBAR_LINES, rows: DEFAULT_SIDEBAR_ROWS },
92
212
  footer: { enabled: false, text: DEFAULT_FOOTER_TEXT },
213
+ caution: DEFAULT_CAUTION,
214
+ layout: DEFAULT_LAYOUT,
215
+ glyphs: DEFAULT_GLYPHS,
93
216
  refresh: DEFAULT_REFRESH_MS,
94
217
  };
95
218
 
@@ -216,7 +339,10 @@ function readRows(value: unknown, issues: string[]): readonly string[] {
216
339
  return rows.slice(0, MAX_LINES);
217
340
  }
218
341
 
219
- function sectionOf(options: Record<string, unknown>, key: "sidebar" | "footer"): Record<string, unknown> {
342
+ /** Every config section that can be set from the file and overridden by the host. */
343
+ type SectionKey = "sidebar" | "footer" | "caution" | "layout" | "glyphs";
344
+
345
+ function sectionOf(options: Record<string, unknown>, key: SectionKey): Record<string, unknown> {
220
346
  const value = options[key];
221
347
  return isRecord(value) ? value : {};
222
348
  }
@@ -237,10 +363,19 @@ function readRefresh(value: unknown, issues: string[]): number {
237
363
  issues.push(`refresh was longer than ${MAX_REFRESH_MS}ms; using ${MAX_REFRESH_MS}`);
238
364
  return MAX_REFRESH_MS;
239
365
  }
366
+ // `0` is "off", not "as fast as possible", so it is deliberately not clamped.
367
+ if (value > 0 && value < MIN_REFRESH_MS) {
368
+ issues.push(`refresh was faster than ${MIN_REFRESH_MS}ms; using ${MIN_REFRESH_MS}`);
369
+ return MIN_REFRESH_MS;
370
+ }
240
371
  return value;
241
372
  }
242
373
 
243
- function mergeSection(file: Record<string, unknown>, host: Record<string, unknown>, key: "sidebar" | "footer"): unknown {
374
+ function mergeSection(
375
+ file: Record<string, unknown>,
376
+ host: Record<string, unknown>,
377
+ key: SectionKey,
378
+ ): unknown {
244
379
  const fileValue = file[key];
245
380
  const hostValue = host[key];
246
381
  // A malformed section from either source replaces the other and is reported
@@ -265,6 +400,157 @@ export function mergeOptions(fileOptions: unknown, hostOptions: unknown): Record
265
400
  ...host,
266
401
  sidebar: mergeSection(file, host, "sidebar"),
267
402
  footer: mergeSection(file, host, "footer"),
403
+ caution: mergeSection(file, host, "caution"),
404
+ layout: mergeSection(file, host, "layout"),
405
+ glyphs: mergeSection(file, host, "glyphs"),
406
+ };
407
+ }
408
+
409
+ /**
410
+ * A non-negative whole number, or the default with a reported issue.
411
+ *
412
+ * Used for both seconds and counts: the validation is identical, and the path
413
+ * in the message tells the reader which they are looking at.
414
+ */
415
+ function readNumber(
416
+ value: unknown,
417
+ fallback: number,
418
+ path: string,
419
+ issues: string[],
420
+ min: number,
421
+ max: number = Number.POSITIVE_INFINITY,
422
+ ): number {
423
+ if (value === undefined) return fallback;
424
+ if (typeof value !== "number" || !Number.isFinite(value)) {
425
+ issues.push(`${path} must be a number; using the default`);
426
+ return fallback;
427
+ }
428
+ if (value < min || value > max) {
429
+ const limit = max === Number.POSITIVE_INFINITY ? `at least ${min}` : `between ${min} and ${max}`;
430
+ issues.push(`${path} must be ${limit}; using the default`);
431
+ return fallback;
432
+ }
433
+ return Math.floor(value);
434
+ }
435
+
436
+ function readLayout(section: Record<string, unknown>, issues: string[]): LayoutConfig {
437
+ return {
438
+ // The label column has to fit "caution" plus a space, and must not eat the
439
+ // rail: six is the narrowest still legible, twenty-four is all of it.
440
+ labelWidth: readNumber(section["labelWidth"], DEFAULT_LAYOUT.labelWidth, "layout.labelWidth", issues, 6, 24),
441
+ barWidth: readNumber(section["barWidth"], DEFAULT_LAYOUT.barWidth, "layout.barWidth", issues, 1, 40),
442
+ sparkWidth: readNumber(section["sparkWidth"], DEFAULT_LAYOUT.sparkWidth, "layout.sparkWidth", issues, 2, 64),
443
+ };
444
+ }
445
+
446
+ /**
447
+ * One glyph, validated for width.
448
+ *
449
+ * A glyph wider than two cells pushes every row out of alignment, and a control
450
+ * character can break the renderer outright — so both fall back rather than
451
+ * being passed through.
452
+ */
453
+ function readGlyph(value: unknown, fallback: string, path: string, issues: string[]): string {
454
+ if (value === undefined) return fallback;
455
+ if (typeof value !== "string") {
456
+ issues.push(`${path} must be a string; using the default`);
457
+ return fallback;
458
+ }
459
+ const text = value.trim();
460
+ if (text.length === 0 || [...text].length > 2) {
461
+ issues.push(`${path} must be one or two characters; using the default`);
462
+ return fallback;
463
+ }
464
+ if (CONTROL_CHAR.test(text)) {
465
+ issues.push(`${path} contained control characters; using the default`);
466
+ return fallback;
467
+ }
468
+ return text;
469
+ }
470
+
471
+ function readGlyphs(section: Record<string, unknown>, issues: string[]): GlyphConfig {
472
+ return {
473
+ watch: readGlyph(section["watch"], DEFAULT_GLYPHS.watch, "glyphs.watch", issues),
474
+ caution: readGlyph(section["caution"], DEFAULT_GLYPHS.caution, "glyphs.caution", issues),
475
+ clear: readGlyph(section["clear"], DEFAULT_GLYPHS.clear, "glyphs.clear", issues),
476
+ };
477
+ }
478
+
479
+ /** Tool names, matched exactly. A malformed list falls back whole, not partly. */
480
+ function readToolNames(value: unknown, issues: string[]): readonly string[] {
481
+ if (value === undefined) return DEFAULT_CAUTION.exemptTools;
482
+ if (!Array.isArray(value)) {
483
+ issues.push("caution.exemptTools must be an array of tool names; using the default");
484
+ return DEFAULT_CAUTION.exemptTools;
485
+ }
486
+ const names: string[] = [];
487
+ for (const entry of value) {
488
+ if (typeof entry !== "string" || entry.trim().length === 0) {
489
+ issues.push("caution.exemptTools must contain only non-empty tool names; using the default");
490
+ return DEFAULT_CAUTION.exemptTools;
491
+ }
492
+ names.push(entry.trim());
493
+ }
494
+ return names;
495
+ }
496
+
497
+ function readCaution(section: Record<string, unknown>, issues: string[]): CautionConfig {
498
+ const toolWatchSeconds = readNumber(
499
+ section["toolWatchSeconds"],
500
+ DEFAULT_CAUTION.toolWatchSeconds,
501
+ "caution.toolWatchSeconds",
502
+ issues,
503
+ 0,
504
+ );
505
+ const turnWatchSeconds = readNumber(
506
+ section["turnWatchSeconds"],
507
+ DEFAULT_CAUTION.turnWatchSeconds,
508
+ "caution.turnWatchSeconds",
509
+ issues,
510
+ 0,
511
+ );
512
+
513
+ // A caution threshold at or below its watch threshold makes the escalating
514
+ // state unreachable. Raise it and say so, rather than accept a config that
515
+ // cannot do what it claims.
516
+ let toolCautionSeconds = readNumber(
517
+ section["toolCautionSeconds"],
518
+ DEFAULT_CAUTION.toolCautionSeconds,
519
+ "caution.toolCautionSeconds",
520
+ issues,
521
+ 0,
522
+ );
523
+ let turnCautionSeconds = readNumber(
524
+ section["turnCautionSeconds"],
525
+ DEFAULT_CAUTION.turnCautionSeconds,
526
+ "caution.turnCautionSeconds",
527
+ issues,
528
+ 0,
529
+ );
530
+ if (toolCautionSeconds < toolWatchSeconds) {
531
+ issues.push("caution.toolCautionSeconds is below caution.toolWatchSeconds; raised to match");
532
+ toolCautionSeconds = toolWatchSeconds;
533
+ }
534
+ if (turnCautionSeconds < turnWatchSeconds) {
535
+ issues.push("caution.turnCautionSeconds is below caution.turnWatchSeconds; raised to match");
536
+ turnCautionSeconds = turnWatchSeconds;
537
+ }
538
+
539
+ return {
540
+ enabled: readBoolean(section["enabled"], DEFAULT_CAUTION.enabled, "caution.enabled", issues),
541
+ toolWatchSeconds,
542
+ toolCautionSeconds,
543
+ turnWatchSeconds,
544
+ turnCautionSeconds,
545
+ repeatThreshold: readNumber(
546
+ section["repeatThreshold"],
547
+ DEFAULT_CAUTION.repeatThreshold,
548
+ "caution.repeatThreshold",
549
+ issues,
550
+ 2,
551
+ ),
552
+ exemptTools: readToolNames(section["exemptTools"], issues),
553
+ toast: readBoolean(section["toast"], DEFAULT_CAUTION.toast, "caution.toast", issues),
268
554
  };
269
555
  }
270
556
 
@@ -288,15 +574,22 @@ export function resolveConfig(options: unknown): ConfigResolution {
288
574
 
289
575
  const rawSidebar = options.sidebar;
290
576
  const rawFooter = options.footer;
577
+ const rawCaution = options.caution;
291
578
  if (rawSidebar !== undefined && !isRecord(rawSidebar)) {
292
579
  issues.push("sidebar must be an object; using defaults");
293
580
  }
294
581
  if (rawFooter !== undefined && !isRecord(rawFooter)) {
295
582
  issues.push("footer must be an object; using defaults");
296
583
  }
584
+ if (rawCaution !== undefined && !isRecord(rawCaution)) {
585
+ issues.push("caution must be an object; using defaults");
586
+ }
297
587
 
298
588
  const sidebar = isRecord(rawSidebar) ? rawSidebar : {};
299
589
  const footer = isRecord(rawFooter) ? rawFooter : {};
590
+ const caution = isRecord(rawCaution) ? rawCaution : {};
591
+ const layout = isRecord(options.layout) ? options.layout : {};
592
+ const glyphs = isRecord(options.glyphs) ? options.glyphs : {};
300
593
 
301
594
  // Writing any footer setting counts as asking for the footer, so the rail
302
595
  // turns on as soon as you configure it. It is off only when you said nothing
@@ -314,6 +607,9 @@ export function resolveConfig(options: unknown): ConfigResolution {
314
607
  enabled: readBoolean(footer.enabled, footerConfigured, "footer.enabled", issues),
315
608
  text: readText(footer.text, DEFAULT_CONFIG.footer.text, "footer.text", issues),
316
609
  },
610
+ caution: readCaution(caution, issues),
611
+ layout: readLayout(layout, issues),
612
+ glyphs: readGlyphs(glyphs, issues),
317
613
  refresh: readRefresh(options.refresh, issues),
318
614
  },
319
615
  issues,