oc-flight-deck 0.6.2 → 0.6.3

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,82 @@
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
+ /** `45s`, `14m07s`, `2h14m37s`. Seconds always show, so the row ticks every second. */
27
+ export function formatDuration(ms: number): string {
28
+ const total = Math.floor(ms / 1_000);
29
+ const seconds = total % 60;
30
+ const minutes = Math.floor(total / 60) % 60;
31
+ const hours = Math.floor(total / 3_600);
32
+ const ss = String(seconds).padStart(2, "0");
33
+ if (hours > 0) return `${hours}h${String(minutes).padStart(2, "0")}m${ss}s`;
34
+ return minutes > 0 ? `${minutes}m${ss}s` : `${seconds}s`;
35
+ }
36
+
37
+ /** Fuel-gauge width in cells when the caller does not override it. */
38
+ export const DEFAULT_BAR_WIDTH = 10;
39
+
40
+ /** A ten-cell gauge, so a percentage is readable at a glance rather than parsed. */
41
+ export function fuelBar(ratio: number, width: number = DEFAULT_BAR_WIDTH): string {
42
+ const cells = Math.max(1, Math.floor(width));
43
+ const filled = Math.max(0, Math.min(cells, Math.round(ratio * cells)));
44
+ return `${"█".repeat(filled)}${"░".repeat(cells - filled)}`;
45
+ }
46
+
47
+ const SPARK_LEVELS = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"] as const;
48
+
49
+ /**
50
+ * Shorten a value for a narrow rail.
51
+ *
52
+ * The sidebar is roughly thirty-odd columns wide and the label already costs
53
+ * ten, so a resource path has to be cut. The ellipsis is deliberate: a silently
54
+ * truncated path reads as a complete one.
55
+ */
56
+ export function clip(text: string, max: number): string {
57
+ if (text.length <= max) return text;
58
+ const cut = text.slice(0, Math.max(1, max - 1));
59
+ // Never cut a surrogate pair in half: the orphaned half renders as a
60
+ // replacement character, which reads as corruption rather than as a shortened
61
+ // path. Losing one more column is the cheaper mistake.
62
+ const safe = /[\uD800-\uDBFF]$/.test(cut) ? cut.slice(0, -1) : cut;
63
+ return `${safe}…`;
64
+ }
65
+
66
+ /**
67
+ * Recent turn sizes as a shape.
68
+ *
69
+ * Scaled against the largest value in the window rather than an absolute scale,
70
+ * so the sparkline stays readable whether turns are tiny or enormous.
71
+ */
72
+ export function sparkline(values: readonly number[]): string {
73
+ if (values.length === 0) return "";
74
+ const max = Math.max(...values);
75
+ if (!Number.isFinite(max) || max <= 0) return SPARK_LEVELS[0].repeat(values.length);
76
+ return values
77
+ .map((value) => {
78
+ const level = Math.floor((Math.max(0, value) / max) * (SPARK_LEVELS.length - 1));
79
+ return SPARK_LEVELS[Math.min(SPARK_LEVELS.length - 1, level)];
80
+ })
81
+ .join("");
82
+ }
@@ -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
  *