oc-flight-deck 0.6.2 → 0.6.6

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.
@@ -0,0 +1,100 @@
1
+ // Narrow-rail rendering: turn values into short strings and glyph strips.
2
+ //
3
+ // The sidebar is roughly thirty-odd columns wide, so every figure is shortened
4
+ // to something an agent can read in one glance: counts (`518k`), costs
5
+ // (`$0.023`), durations (`2h14m37s`), paths (clipped with an ellipsis), a
6
+ // ten-cell fuel gauge, and a sparkline shaped from recent turn sizes. All pure
7
+ // and dependency-free, so the formatting is trivial to test.
8
+
9
+ /** `518k`, `29.2M`, `32M`, `940` — short enough for a narrow rail, precise enough to read. */
10
+ export function formatCount(value: number): string {
11
+ if (value < 1_000) return String(Math.round(value));
12
+ // Round *before* choosing the unit. Rounding only inside the `k` branch made
13
+ // anything from 999,500 to 999,999 print as `1000k` — a number with two
14
+ // magnitudes in it, and one an agent reads as a different size entirely.
15
+ const thousands = Math.round(value / 1_000);
16
+ if (thousands < 1_000) return `${thousands}k`;
17
+ const millions = (value / 1_000_000).toFixed(1);
18
+ return `${millions.endsWith(".0") ? millions.slice(0, -2) : millions}M`;
19
+ }
20
+
21
+ /** Sub-dollar sessions keep a third decimal so a cheap run never reads `$0.00`. */
22
+ export function formatCost(value: number): string {
23
+ return `$${value.toFixed(value < 1 ? 3 : 2)}`;
24
+ }
25
+
26
+ /**
27
+ * How `formatDuration` joins its segments: `"compact"` hugs the units
28
+ * (`2h14m37s`), `"spaced"` puts one space between them (`2h 14m 37s`).
29
+ */
30
+ export type DurationStyle = "compact" | "spaced";
31
+
32
+ /**
33
+ * `45s`, `14m07s`, `2h14m37s`; with `spaced`, one space between the segments
34
+ * (`14m 07s`, `2h 14m 37s`). Seconds always show, so the row ticks every
35
+ * second. Zero-padding is identical in both styles: minutes pad when hours are
36
+ * present, seconds pad when minutes are present.
37
+ *
38
+ * The parameter default stays `"compact"`: this formatter's own contract is
39
+ * unchanged for direct callers, while the rail's shipped default comes from
40
+ * `format.duration` (spaced).
41
+ */
42
+ export function formatDuration(ms: number, style: DurationStyle = "compact"): string {
43
+ const total = Math.floor(ms / 1_000);
44
+ const seconds = total % 60;
45
+ const minutes = Math.floor(total / 60) % 60;
46
+ const hours = Math.floor(total / 3_600);
47
+ const ss = String(seconds).padStart(2, "0");
48
+ // Only the join between segments differs; with `compact` the join is empty,
49
+ // so that style stays byte-identical to what it has always rendered.
50
+ const join = style === "spaced" ? " " : "";
51
+ if (hours > 0) return `${hours}h${join}${String(minutes).padStart(2, "0")}m${join}${ss}s`;
52
+ return minutes > 0 ? `${minutes}m${join}${ss}s` : `${seconds}s`;
53
+ }
54
+
55
+ /** Fuel-gauge width in cells when the caller does not override it. */
56
+ export const DEFAULT_BAR_WIDTH = 10;
57
+
58
+ /** A ten-cell gauge, so a percentage is readable at a glance rather than parsed. */
59
+ export function fuelBar(ratio: number, width: number = DEFAULT_BAR_WIDTH): string {
60
+ const cells = Math.max(1, Math.floor(width));
61
+ const filled = Math.max(0, Math.min(cells, Math.round(ratio * cells)));
62
+ return `${"█".repeat(filled)}${"░".repeat(cells - filled)}`;
63
+ }
64
+
65
+ const SPARK_LEVELS = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"] as const;
66
+
67
+ /**
68
+ * Shorten a value for a narrow rail.
69
+ *
70
+ * The sidebar is roughly thirty-odd columns wide and the label already costs
71
+ * ten, so a resource path has to be cut. The ellipsis is deliberate: a silently
72
+ * truncated path reads as a complete one.
73
+ */
74
+ export function clip(text: string, max: number): string {
75
+ if (text.length <= max) return text;
76
+ const cut = text.slice(0, Math.max(1, max - 1));
77
+ // Never cut a surrogate pair in half: the orphaned half renders as a
78
+ // replacement character, which reads as corruption rather than as a shortened
79
+ // path. Losing one more column is the cheaper mistake.
80
+ const safe = /[\uD800-\uDBFF]$/.test(cut) ? cut.slice(0, -1) : cut;
81
+ return `${safe}…`;
82
+ }
83
+
84
+ /**
85
+ * Recent turn sizes as a shape.
86
+ *
87
+ * Scaled against the largest value in the window rather than an absolute scale,
88
+ * so the sparkline stays readable whether turns are tiny or enormous.
89
+ */
90
+ export function sparkline(values: readonly number[]): string {
91
+ if (values.length === 0) return "";
92
+ const max = Math.max(...values);
93
+ if (!Number.isFinite(max) || max <= 0) return SPARK_LEVELS[0].repeat(values.length);
94
+ return values
95
+ .map((value) => {
96
+ const level = Math.floor((Math.max(0, value) / max) * (SPARK_LEVELS.length - 1));
97
+ return SPARK_LEVELS[Math.min(SPARK_LEVELS.length - 1, level)];
98
+ })
99
+ .join("");
100
+ }
@@ -0,0 +1,72 @@
1
+ // Guard-token rendering for the `guard` row.
2
+ //
3
+ // The harness-guard RPC payload is untrusted: these helpers turn it into one
4
+ // short ASCII token, or `undefined` when there is nothing usable to show. They
5
+ // live here rather than in ./rows.js so the row renderer stays rendering.
6
+
7
+ import { asRecord } from "./coerce.js";
8
+ import { formatCount } from "./format.js";
9
+
10
+ /**
11
+ * A guard count: a non-negative whole number, or an array counted by length.
12
+ *
13
+ * The RPC aggregates are numbers, but an array (findings, breaches) reads the
14
+ * same way — how many — so both count. Anything else is dropped to `undefined`
15
+ * so the caller can fall back to zero rather than print garbage.
16
+ */
17
+ function asGuardCount(value: unknown): number | undefined {
18
+ if (typeof value === "number" && Number.isFinite(value) && value >= 0) return Math.floor(value);
19
+ if (Array.isArray(value)) return value.length;
20
+ return undefined;
21
+ }
22
+
23
+ function guardCountOrZero(...candidates: readonly unknown[]): number {
24
+ for (const candidate of candidates) {
25
+ const count = asGuardCount(candidate);
26
+ if (count !== undefined) return count;
27
+ }
28
+ return 0;
29
+ }
30
+
31
+ /**
32
+ * Short harness token for the `guard` row, or `undefined` when there is no
33
+ * usable data (the persist layer then renders the placeholder).
34
+ *
35
+ * A section counts as usable only with an explicit boolean `available`: a
36
+ * present section with a missing or garbage flag is dropped, not read as
37
+ * either `ok` or `unknown`. Any usable section reporting `available: false`
38
+ * reads as `unknown` — a failed or disabled source never renders a false `ok`.
39
+ * Otherwise the worst signal wins: breaches, then orphans, then findings.
40
+ * ASCII, short, no padding.
41
+ */
42
+ export function guardToken(value: unknown): string | undefined {
43
+ try {
44
+ const top = asRecord(value);
45
+ if (top === undefined) return undefined;
46
+
47
+ const air = asRecord(top.airworthiness);
48
+ const war = asRecord(top.warden);
49
+ const plan = asRecord(top.flightPlan);
50
+
51
+ const airUsable = air !== undefined && (air.available === true || air.available === false);
52
+ const warUsable = war !== undefined && (war.available === true || war.available === false);
53
+ const planUsable = plan !== undefined && (plan.available === true || plan.available === false);
54
+ if (!airUsable && !warUsable && !planUsable) return undefined;
55
+
56
+ if ((airUsable && air.available === false) || (warUsable && war.available === false) || (planUsable && plan.available === false)) {
57
+ return "unknown";
58
+ }
59
+
60
+ const breaches = warUsable ? guardCountOrZero(war.breaches) : 0;
61
+ const orphans = warUsable ? guardCountOrZero(war.orphans) : 0;
62
+ const findings = airUsable ? guardCountOrZero(air.findings, air.counts) : 0;
63
+
64
+ // Abbreviated so a huge count cannot exceed the rail width; stays ASCII.
65
+ if (breaches > 0) return `${formatCount(breaches)} breach`;
66
+ if (orphans > 0) return `${formatCount(orphans)} orphan`;
67
+ if (findings > 0) return `${formatCount(findings)} finding`;
68
+ return "ok";
69
+ } catch {
70
+ return undefined;
71
+ }
72
+ }
package/src/tui/guard.ts CHANGED
@@ -30,22 +30,22 @@
30
30
  // the persist layer renders the placeholder.
31
31
 
32
32
  /** Aggregates from guard's `status` output. Only what the row needs. */
33
- export interface GuardAirworthiness {
33
+ interface GuardAirworthiness {
34
34
  readonly available: boolean;
35
35
  readonly findings: number;
36
36
  }
37
37
 
38
- export interface GuardWarden {
38
+ interface GuardWarden {
39
39
  readonly available: boolean;
40
40
  readonly breaches: number;
41
41
  readonly orphans: number;
42
42
  }
43
43
 
44
- export interface GuardFlightPlan {
44
+ interface GuardFlightPlan {
45
45
  readonly available: boolean;
46
46
  }
47
47
 
48
- export interface GuardStatus {
48
+ interface GuardStatus {
49
49
  readonly airworthiness?: GuardAirworthiness;
50
50
  readonly warden?: GuardWarden;
51
51
  readonly flightPlan?: GuardFlightPlan;
@@ -83,7 +83,7 @@ export interface GuardDeps {
83
83
  readonly status: (input: { readonly sessionID: string }) => Promise<unknown>;
84
84
  }
85
85
 
86
- export interface GuardBridge {
86
+ interface GuardBridge {
87
87
  /**
88
88
  * Current status, read from the host's store.
89
89
  *