@ftrdotdev/pi-delegates 0.0.0-stage → 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.
Files changed (138) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/LICENSE +21 -0
  3. package/OPERATIONS.md +130 -0
  4. package/README.md +456 -2
  5. package/ROADMAP.md +179 -0
  6. package/TESTING.md +896 -0
  7. package/agents.ts +8 -0
  8. package/examples/README.md +114 -0
  9. package/examples/gated.ts +28 -0
  10. package/index.ts +9 -0
  11. package/package.json +94 -4
  12. package/skills/pi-delegates/SKILL.md +176 -0
  13. package/skills/pi-delegates/references/control.md +159 -0
  14. package/skills/pi-delegates/references/definitions.md +163 -0
  15. package/skills/pi-delegates/references/gates.md +175 -0
  16. package/skills/pi-delegates/references/launch.md +184 -0
  17. package/src/docs-contract.test.ts +106 -0
  18. package/src/domain/definitions.test.ts +41 -0
  19. package/src/domain/definitions.ts +163 -0
  20. package/src/domain/failure.ts +33 -0
  21. package/src/domain/ids.ts +13 -0
  22. package/src/domain/model-source.ts +30 -0
  23. package/src/domain/names.ts +35 -0
  24. package/src/domain/orchestration/admission.ts +78 -0
  25. package/src/domain/orchestration/control.test.ts +1332 -0
  26. package/src/domain/orchestration/control.ts +457 -0
  27. package/src/domain/orchestration/fanout.test.ts +524 -0
  28. package/src/domain/orchestration/fanout.ts +372 -0
  29. package/src/domain/orchestration/gates.test.ts +685 -0
  30. package/src/domain/orchestration/gates.ts +360 -0
  31. package/src/domain/orchestration/launch.test.ts +372 -0
  32. package/src/domain/orchestration/launch.ts +691 -0
  33. package/src/domain/orchestration/requests.test.ts +695 -0
  34. package/src/domain/orchestration/requests.ts +782 -0
  35. package/src/domain/orchestration/review.test.ts +292 -0
  36. package/src/domain/orchestration/review.ts +168 -0
  37. package/src/domain/orchestration/run-wait.test.ts +265 -0
  38. package/src/domain/orchestration/run-wait.ts +256 -0
  39. package/src/domain/orchestration/workflow-section.test.ts +62 -0
  40. package/src/domain/orchestration/workflow-section.ts +99 -0
  41. package/src/domain/orchestration/workflow.test.ts +1566 -0
  42. package/src/domain/orchestration/workflow.ts +778 -0
  43. package/src/domain/run-records.test.ts +27 -0
  44. package/src/domain/run-records.ts +689 -0
  45. package/src/domain/settlement.test.ts +186 -0
  46. package/src/domain/settlement.ts +137 -0
  47. package/src/domain/shapes.test.ts +185 -0
  48. package/src/domain/skills.test.ts +477 -0
  49. package/src/domain/skills.ts +114 -0
  50. package/src/domain/tool-schema.test.ts +46 -0
  51. package/src/domain/tool-schema.ts +20 -0
  52. package/src/domain/toolkit.test.ts +215 -0
  53. package/src/domain/toolkit.ts +374 -0
  54. package/src/domain/turn-budget.test.ts +37 -0
  55. package/src/domain/turn-budget.ts +23 -0
  56. package/src/domain/view.test.ts +803 -0
  57. package/src/domain/view.ts +491 -0
  58. package/src/example.test.ts +15 -0
  59. package/src/extension/commands.test.ts +485 -0
  60. package/src/extension/commands.ts +230 -0
  61. package/src/extension/entry.test.ts +1276 -0
  62. package/src/extension/entry.ts +380 -0
  63. package/src/extension/gate-alerts.test.ts +237 -0
  64. package/src/extension/gate-alerts.ts +143 -0
  65. package/src/extension/guard.test.ts +48 -0
  66. package/src/extension/guard.ts +31 -0
  67. package/src/extension/host-signals.test.ts +20 -0
  68. package/src/extension/host-signals.ts +13 -0
  69. package/src/extension/inspector-command.test.ts +1343 -0
  70. package/src/extension/inspector-command.ts +685 -0
  71. package/src/extension/prompt.test.ts +108 -0
  72. package/src/extension/prompt.ts +50 -0
  73. package/src/extension/request-alerts.test.ts +183 -0
  74. package/src/extension/request-alerts.ts +181 -0
  75. package/src/extension/retry-observer.test.ts +292 -0
  76. package/src/extension/retry-observer.ts +150 -0
  77. package/src/extension/revocation.test.ts +81 -0
  78. package/src/extension/revocation.ts +84 -0
  79. package/src/extension/settle-alerts.test.ts +579 -0
  80. package/src/extension/settle-alerts.ts +175 -0
  81. package/src/extension/shortcut.test.ts +62 -0
  82. package/src/extension/shortcut.ts +105 -0
  83. package/src/extension/tools.test.ts +1294 -0
  84. package/src/extension/tools.ts +553 -0
  85. package/src/extension/version.test.ts +115 -0
  86. package/src/extension/version.ts +97 -0
  87. package/src/goldens.test.ts +294 -0
  88. package/src/harness-policy.test.ts +76 -0
  89. package/src/layers.test.ts +309 -0
  90. package/src/layers.ts +405 -0
  91. package/src/package-manifest.test.ts +170 -0
  92. package/src/presentation/actions.test.ts +221 -0
  93. package/src/presentation/actions.ts +85 -0
  94. package/src/presentation/activity.test.ts +386 -0
  95. package/src/presentation/activity.ts +66 -0
  96. package/src/presentation/close.test.ts +359 -0
  97. package/src/presentation/inspector.test.ts +682 -0
  98. package/src/presentation/inspector.ts +442 -0
  99. package/src/presentation/overlay.test.ts +553 -0
  100. package/src/presentation/overlay.ts +229 -0
  101. package/src/presentation/repaint.test.ts +365 -0
  102. package/src/presentation/shortcut.ts +2 -0
  103. package/src/presentation/snapshot.test.ts +535 -0
  104. package/src/presentation/snapshot.ts +306 -0
  105. package/src/presentation/status.test.ts +43 -0
  106. package/src/presentation/status.ts +30 -0
  107. package/src/presentation/strip.test.ts +203 -0
  108. package/src/presentation/strip.ts +67 -0
  109. package/src/presentation/wording.test.ts +388 -0
  110. package/src/presentation/wording.ts +294 -0
  111. package/src/runtime/bridge.test.ts +131 -0
  112. package/src/runtime/bridge.ts +128 -0
  113. package/src/runtime/clock.test.ts +31 -0
  114. package/src/runtime/clock.ts +31 -0
  115. package/src/runtime/config.test.ts +63 -0
  116. package/src/runtime/config.ts +92 -0
  117. package/src/runtime/context.test.ts +19 -0
  118. package/src/runtime/context.ts +9 -0
  119. package/src/runtime/execution-env.test.ts +33 -0
  120. package/src/runtime/execution-env.ts +15 -0
  121. package/src/runtime/extensions.test.ts +86 -0
  122. package/src/runtime/extensions.ts +152 -0
  123. package/src/runtime/handle.ts +54 -0
  124. package/src/runtime/log.test.ts +140 -0
  125. package/src/runtime/log.ts +91 -0
  126. package/src/runtime/mcp.test.ts +1450 -0
  127. package/src/runtime/mcp.ts +1005 -0
  128. package/src/runtime/models.test.ts +145 -0
  129. package/src/runtime/models.ts +259 -0
  130. package/src/runtime/observe.test.ts +50 -0
  131. package/src/runtime/observe.ts +150 -0
  132. package/src/runtime/runtime-state.test.ts +25 -0
  133. package/src/runtime/runtime-state.ts +36 -0
  134. package/src/runtime/session-tag.ts +11 -0
  135. package/src/runtime/session.test.ts +352 -0
  136. package/src/runtime/session.ts +263 -0
  137. package/src/runtime/storage.ts +29 -0
  138. package/src/test-hygiene.test.ts +97 -0
@@ -0,0 +1,306 @@
1
+ import { Schema } from "effect";
2
+ import type { GateRecord } from "../domain/orchestration/gates.ts";
3
+ import { requestState, type RequestRecord } from "../domain/orchestration/requests.ts";
4
+ import {
5
+ isTerminalStatus,
6
+ laneViews,
7
+ LaneView,
8
+ RunStatus,
9
+ type SessionView,
10
+ } from "../domain/view.ts";
11
+ import type { RunActivity } from "./activity.ts";
12
+
13
+ /** The status vocabulary of one live tool slot, as `LiveState.tools` carries it. */
14
+ const SlotStatus = Schema.Literals(["pending", "running", "sleeping", "waiting", "done"]);
15
+
16
+ export const InspectorRow = Schema.Struct({
17
+ /** `child:<runId>` | `run:<runId>`: the selection identity across replacements. */
18
+ id: Schema.String,
19
+ /** The id a control write addresses (the row's own run id). */
20
+ runId: Schema.String,
21
+ /** `run:<parentRunId>` for a child row; null for a top-level or single run. */
22
+ parentId: Schema.NullOr(Schema.String),
23
+ /** Child runs under this row; 0 for an execution row. */
24
+ childCount: Schema.Finite,
25
+ label: Schema.String,
26
+ lane: Schema.String,
27
+ status: RunStatus,
28
+ conversationId: Schema.NullOr(Schema.Finite),
29
+ /** Observation address only; never rendered. */
30
+ taskId: Schema.NullOr(Schema.Finite),
31
+ gates: Schema.Array(Schema.String),
32
+ /** Request ids the run waits on; empty unless the run waits on a request. */
33
+ requests: Schema.Array(Schema.String),
34
+ tool: Schema.NullOr(Schema.String),
35
+ slot: Schema.NullOr(SlotStatus),
36
+ idleMs: Schema.NullOr(Schema.Finite),
37
+ /** `endedAt - startedAt` once terminal; null while live or without timestamps. */
38
+ durationMs: Schema.NullOr(Schema.Finite),
39
+ diagnostic: Schema.NullOr(Schema.String),
40
+ });
41
+ export type InspectorRow = typeof InspectorRow.Type;
42
+
43
+ export const GateRow = Schema.Struct({
44
+ /** `gate:<runId>:<gate>`: the gate's selection identity. */
45
+ id: Schema.String,
46
+ runId: Schema.String,
47
+ gate: Schema.String,
48
+ /** Joined from the gate's run row; the projection already has it. */
49
+ lane: Schema.String,
50
+ tool: Schema.NullOr(Schema.String),
51
+ /** Every child run whose conversation reached this gate. */
52
+ childRunIds: Schema.Array(Schema.String),
53
+ /** `now - requestedAt`: the gate age, and the expansion block's only clock reading. */
54
+ idleMs: Schema.Finite,
55
+ });
56
+ export type GateRow = typeof GateRow.Type;
57
+
58
+ export const RequestRow = Schema.Struct({
59
+ /** `request:<runId>:<requestId>`: the request's selection identity. */
60
+ id: Schema.String,
61
+ runId: Schema.String,
62
+ requestId: Schema.String,
63
+ prompt: Schema.String,
64
+ /** The option labels, for the expansion block. */
65
+ options: Schema.Array(Schema.String),
66
+ /** The absolute deadline, in milliseconds. */
67
+ deadline: Schema.Finite,
68
+ });
69
+ export type RequestRow = typeof RequestRow.Type;
70
+
71
+ /**
72
+ * The snapshot's content, without the revision. The equivalence is derived
73
+ * over this schema, so a revision alone is never a change: `revision` counts
74
+ * publications, and the owner has already decided the content moved.
75
+ */
76
+ const InspectorContent = Schema.Struct({
77
+ rows: Schema.Array(InspectorRow),
78
+ gates: Schema.Array(GateRow),
79
+ requests: Schema.Array(RequestRow),
80
+ lanes: Schema.Array(LaneView),
81
+ counts: Schema.Struct({
82
+ running: Schema.Finite,
83
+ queued: Schema.Finite,
84
+ waiting: Schema.Finite,
85
+ gates: Schema.Finite,
86
+ requests: Schema.Finite,
87
+ }),
88
+ omitted: Schema.Finite,
89
+ });
90
+
91
+ export const InspectorSnapshot = Schema.Struct({
92
+ revision: Schema.Finite,
93
+ ...InspectorContent.fields,
94
+ });
95
+ export type InspectorSnapshot = typeof InspectorSnapshot.Type;
96
+
97
+ /** Derived, not hand-written: the owner's only change detector. */
98
+ export const InspectorSnapshotEquivalence = Schema.toEquivalence(InspectorContent);
99
+
100
+ /** The state before the first committed read: nothing live, revision 0. */
101
+ export const emptySnapshot = (): InspectorSnapshot => ({
102
+ revision: 0,
103
+ rows: [],
104
+ gates: [],
105
+ requests: [],
106
+ lanes: [],
107
+ counts: { running: 0, queued: 0, waiting: 0, gates: 0, requests: 0 },
108
+ omitted: 0,
109
+ });
110
+
111
+ const idleOf = (
112
+ activity: RunActivity | undefined,
113
+ now: number,
114
+ status: InspectorRow["status"],
115
+ ): {
116
+ readonly tool: string | null;
117
+ readonly slot: RunActivity["slot"];
118
+ readonly idleMs: number | null;
119
+ } =>
120
+ activity === undefined || isTerminalStatus(status)
121
+ ? { tool: null, slot: null, idleMs: null }
122
+ : { tool: activity.tool, slot: activity.slot, idleMs: now - activity.at };
123
+
124
+ const rowOf = (input: {
125
+ readonly id: string;
126
+ readonly runId: string;
127
+ readonly parentId: string | null;
128
+ readonly childCount: number;
129
+ readonly label: string;
130
+ readonly lane: string;
131
+ readonly status: InspectorRow["status"];
132
+ readonly conversationId: number | null;
133
+ readonly taskId: number | null;
134
+ readonly gates: readonly string[];
135
+ readonly requests: readonly string[];
136
+ readonly diagnostic: string | null;
137
+ readonly durationMs: number | null;
138
+ readonly activity: RunActivity | undefined;
139
+ readonly now: number;
140
+ }): InspectorRow => ({
141
+ id: input.id,
142
+ runId: input.runId,
143
+ parentId: input.parentId,
144
+ childCount: input.childCount,
145
+ label: input.label,
146
+ lane: input.lane,
147
+ status: input.status,
148
+ conversationId: input.conversationId,
149
+ taskId: input.taskId,
150
+ gates: [...input.gates],
151
+ requests: [...input.requests],
152
+ diagnostic: input.diagnostic,
153
+ durationMs: input.durationMs,
154
+ ...idleOf(input.activity, input.now, input.status),
155
+ });
156
+
157
+ /** A group run is the only kind whose `children` are real child runs, not the single-run alias. */
158
+ const isGroupRun = (run: SessionView["runs"][number]): boolean =>
159
+ run.children.some((child) => child.runId !== run.runId);
160
+
161
+ /** Gate names whose record names this child, directly or through `childRunIds`. */
162
+ const gatesForChild = (gates: readonly GateRecord[], childRunId: string): string[] =>
163
+ gates
164
+ .filter((gate) => gate.runId === childRunId || gate.childRunIds.includes(childRunId))
165
+ .map((gate) => gate.gate);
166
+
167
+ /** Pending request ids whose record names this child run, directly or as the owner. */
168
+ const requestsForChild = (
169
+ requests: readonly RequestRecord[],
170
+ childRunId: string,
171
+ now: number,
172
+ ): string[] =>
173
+ requests
174
+ .filter(
175
+ (record) =>
176
+ (record.runId === childRunId || record.childRunId === childRunId) &&
177
+ requestState(record, now) === "pending",
178
+ )
179
+ .map((record) => record.requestId);
180
+
181
+ /** One row per run, then its child rows newest first; a single run is its own row. */
182
+ const rowsOf = (
183
+ view: SessionView,
184
+ activity: ReadonlyMap<string, RunActivity>,
185
+ now: number,
186
+ ): InspectorRow[] => {
187
+ const rows: InspectorRow[] = [];
188
+ for (const run of [...view.runs].reverse()) {
189
+ const group = isGroupRun(run);
190
+ rows.push(
191
+ rowOf({
192
+ id: `run:${run.runId}`,
193
+ runId: run.runId,
194
+ parentId: null,
195
+ childCount: group ? run.children.length : 0,
196
+ label: run.definition,
197
+ lane: run.lane,
198
+ status: run.status,
199
+ conversationId: run.conversationId,
200
+ taskId: run.taskId,
201
+ gates: run.gates,
202
+ requests: run.requests,
203
+ diagnostic: run.diagnostic,
204
+ durationMs: run.durationMs,
205
+ activity: activity.get(run.runId),
206
+ now,
207
+ }),
208
+ );
209
+ if (!group) continue;
210
+ for (const child of [...run.children].reverse()) {
211
+ rows.push(
212
+ rowOf({
213
+ id: `child:${child.runId}`,
214
+ runId: child.runId,
215
+ parentId: `run:${run.runId}`,
216
+ childCount: 0,
217
+ label: child.agent,
218
+ lane: run.lane,
219
+ status: child.status,
220
+ conversationId: child.conversationId,
221
+ taskId: child.taskId,
222
+ gates: gatesForChild(view.gates, child.runId),
223
+ requests: requestsForChild(view.requests, child.runId, now),
224
+ diagnostic: run.diagnostic,
225
+ durationMs: child.durationMs,
226
+ activity: activity.get(child.runId),
227
+ now,
228
+ }),
229
+ );
230
+ }
231
+ }
232
+ return rows;
233
+ };
234
+
235
+ const laneOf = (gate: GateRecord, rows: readonly InspectorRow[]): string => {
236
+ const direct = rows.find((row) => row.runId === gate.runId);
237
+ if (direct !== undefined) return direct.lane;
238
+ for (const childRunId of gate.childRunIds) {
239
+ const row = rows.find((candidate) => candidate.runId === childRunId);
240
+ if (row !== undefined) return row.lane;
241
+ }
242
+ return "unknown";
243
+ };
244
+
245
+ /** Pending gates, oldest first: the oldest gate blocks the most. */
246
+ const gatesOf = (view: SessionView, rows: readonly InspectorRow[], now: number): GateRow[] =>
247
+ [...view.gates]
248
+ .sort((left, right) => left.requestedAt - right.requestedAt)
249
+ .map((gate) => ({
250
+ id: `gate:${gate.runId}:${gate.gate}`,
251
+ runId: gate.runId,
252
+ gate: gate.gate,
253
+ lane: laneOf(gate, rows),
254
+ tool: gate.tool,
255
+ childRunIds: [...gate.childRunIds],
256
+ idleMs: now - gate.requestedAt,
257
+ }));
258
+
259
+ const countsOf = (
260
+ rows: readonly InspectorRow[],
261
+ gateCount: number,
262
+ requestCount: number,
263
+ ): InspectorSnapshot["counts"] => {
264
+ const counts = { running: 0, queued: 0, waiting: 0, gates: gateCount, requests: requestCount };
265
+ for (const row of rows) {
266
+ // A parent row is a container; its children carry the execution status.
267
+ if (row.childCount > 0) continue;
268
+ if (row.status === "running") counts.running += 1;
269
+ else if (row.status === "queued") counts.queued += 1;
270
+ else if (row.status === "waiting") counts.waiting += 1;
271
+ }
272
+ return counts;
273
+ };
274
+
275
+ /** Pending requests, oldest first: the oldest blocks the most. */
276
+ const requestsOf = (view: SessionView, now: number): RequestRow[] =>
277
+ [...view.requests]
278
+ .filter((record) => requestState(record, now) === "pending")
279
+ .sort((left, right) => left.requestedAt - right.requestedAt)
280
+ .map((record) => ({
281
+ id: `request:${record.runId}:${record.requestId}`,
282
+ runId: record.runId,
283
+ requestId: record.requestId,
284
+ prompt: record.prompt,
285
+ options: record.options.map((option) => option.label),
286
+ deadline: record.deadline,
287
+ }));
288
+
289
+ export const projectSnapshot = (input: {
290
+ readonly view: SessionView;
291
+ readonly activity: ReadonlyMap<string, RunActivity>;
292
+ readonly now: number;
293
+ readonly revision: number;
294
+ }): InspectorSnapshot => {
295
+ const rows = rowsOf(input.view, input.activity, input.now);
296
+ const requests = requestsOf(input.view, input.now);
297
+ return {
298
+ revision: input.revision,
299
+ rows,
300
+ gates: gatesOf(input.view, rows, input.now),
301
+ requests,
302
+ lanes: laneViews(input.view.runs),
303
+ counts: countsOf(rows, input.view.gates.length, requests.length),
304
+ omitted: input.view.omitted ?? 0,
305
+ };
306
+ };
@@ -0,0 +1,43 @@
1
+ import { assert, it } from "@effect/vitest";
2
+ import { activeStatus, offStatus, STATUS_KEY } from "./status.ts";
3
+
4
+ const counts = (
5
+ input: Partial<Record<"running" | "queued" | "waiting" | "gates" | "requests", number>>,
6
+ ) => ({
7
+ running: 0,
8
+ queued: 0,
9
+ waiting: 0,
10
+ gates: 0,
11
+ requests: 0,
12
+ ...input,
13
+ });
14
+
15
+ it("the footer key is one entry", () => {
16
+ assert.strictEqual(STATUS_KEY, "delegates");
17
+ });
18
+
19
+ it("the off line names the command, not the inert shortcut", () => {
20
+ assert.strictEqual(offStatus(), "delegates off · /delegates");
21
+ });
22
+
23
+ it("the idle line names the shortcut and no counter", () => {
24
+ assert.strictEqual(activeStatus(counts({})), "delegates idle · ctrl+space");
25
+ });
26
+
27
+ it("the live line carries every non-zero counter in a fixed order", () => {
28
+ assert.strictEqual(
29
+ activeStatus(counts({ running: 2, waiting: 1, gates: 1, queued: 3, requests: 1 })),
30
+ "delegates running 2 · queued 3 · waiting 1 · gates 1 · requests 1 · ctrl+space",
31
+ );
32
+ });
33
+
34
+ it("zero counters never print", () => {
35
+ assert.strictEqual(
36
+ activeStatus(counts({ running: 1, queued: 0, waiting: 0, gates: 0, requests: 0 })),
37
+ "delegates running 1 · ctrl+space",
38
+ );
39
+ });
40
+
41
+ it("a custom shortcut replaces the default in the line", () => {
42
+ assert.strictEqual(activeStatus(counts({}), "f2"), "delegates idle · f2");
43
+ });
@@ -0,0 +1,30 @@
1
+ import { DEFAULT_INSPECT_SHORTCUT } from "./shortcut.ts";
2
+ import type { InspectorSnapshot } from "./snapshot.ts";
3
+ import { counterParts } from "./wording.ts";
4
+
5
+ /**
6
+ * The footer status key. Pi shows every extension status on one shared footer
7
+ * line, sorted by key, so this package owns one entry on it, not a row.
8
+ */
9
+ export const STATUS_KEY = "delegates";
10
+
11
+ /** The panel's footer name, so the line reads the same as the strip's box label. */
12
+ const LABEL = "delegates";
13
+
14
+ /**
15
+ * The footer line while the inspector is off. The command is the only working
16
+ * door, so the line names it; the shortcut would be inert.
17
+ */
18
+ export const offStatus = (): string => `${LABEL} off · /delegates`;
19
+
20
+ /**
21
+ * The footer line while the inspector is on. Idle says so instead of printing
22
+ * zero for every counter; live work names the counters the strip may hide.
23
+ */
24
+ export const activeStatus = (
25
+ counts: InspectorSnapshot["counts"],
26
+ shortcut = DEFAULT_INSPECT_SHORTCUT,
27
+ ): string => {
28
+ const parts = counterParts(counts);
29
+ return `${LABEL} ${parts.length === 0 ? "idle" : parts.join(" · ")} · ${shortcut}`;
30
+ };
@@ -0,0 +1,203 @@
1
+ import { assert, it } from "@effect/vitest";
2
+ import type { Theme } from "@earendil-works/pi-coding-agent";
3
+ import { SELECTION_MARKER } from "@ftrdotdev/pi-tui";
4
+ import type { GateRecord } from "../domain/orchestration/gates.ts";
5
+ import {
6
+ laneViews,
7
+ type ChildView,
8
+ type RunStatus,
9
+ type RunView,
10
+ type SessionView,
11
+ } from "../domain/view.ts";
12
+ import { projectSnapshot } from "./snapshot.ts";
13
+ import { makeDelegatesStrip, stripLines } from "./strip.ts";
14
+ import { formatElapsed } from "./wording.ts";
15
+
16
+ const plainTheme = {
17
+ fg: (_color: string, text: string): string => text,
18
+ bg: (_color: string, text: string): string => text,
19
+ bold: (text: string): string => text,
20
+ style: (text: string): string => text,
21
+ } as unknown as Theme;
22
+
23
+ const runView = (
24
+ input: Partial<RunView> & { readonly runId: string; readonly status: RunStatus },
25
+ ): RunView => ({
26
+ definition: "scout",
27
+ mode: "single",
28
+ lane: "default",
29
+ conversationId: null,
30
+ taskId: null,
31
+ answer: null,
32
+ durationMs: null,
33
+ children: [],
34
+ missing: [],
35
+ gates: [],
36
+ requests: [],
37
+ diagnostic: null,
38
+ execution: "wait",
39
+ maxTurns: null,
40
+ failure: null,
41
+ ...input,
42
+ });
43
+
44
+ const childView = (
45
+ input: Partial<ChildView> & { readonly child: number; readonly runId: string },
46
+ ): ChildView => ({
47
+ conversationId: null,
48
+ taskId: null,
49
+ agent: "scout",
50
+ status: "running",
51
+ answer: null,
52
+ durationMs: null,
53
+ maxTurns: null,
54
+ failure: null,
55
+ ...input,
56
+ });
57
+
58
+ const gateRecord = (input: Partial<GateRecord> & { readonly gate: string }): GateRecord => ({
59
+ runId: "run:one",
60
+ status: "pending",
61
+ tool: "bash",
62
+ taskId: null,
63
+ taskIds: [],
64
+ childRunIds: [],
65
+ requestedAt: 1_000,
66
+ deadline: 0,
67
+ decision: null,
68
+ decidedBy: null,
69
+ reason: null,
70
+ ...input,
71
+ });
72
+
73
+ const snapshotOf = (
74
+ runs: readonly RunView[],
75
+ gates: readonly GateRecord[] = [],
76
+ activity: ReadonlyMap<
77
+ string,
78
+ { tool: string | null; slot: "running" | null; turns: number; at: number }
79
+ > = new Map(),
80
+ ) => {
81
+ const view: SessionView = { runs, lanes: laneViews(runs), gates, requests: [], reviews: [] };
82
+ return projectSnapshot({ view, activity, now: 100_000, revision: 1 });
83
+ };
84
+
85
+ const fanoutWith = (children: readonly ChildView[], status: RunStatus = "running"): RunView =>
86
+ runView({ runId: "run:fanout", definition: "fanout", status, children });
87
+
88
+ it("the strip renders the house frame around its content lines", () => {
89
+ const children = [
90
+ childView({ child: 0, runId: "run:deployer", agent: "deployer", conversationId: 1 }),
91
+ childView({ child: 1, runId: "run:scout", agent: "scout", conversationId: 2 }),
92
+ ];
93
+ const snapshot = snapshotOf(
94
+ [fanoutWith(children)],
95
+ [gateRecord({ runId: "run:fanout", gate: "deploy" })],
96
+ new Map([
97
+ ["run:deployer", { tool: "bash", slot: "running" as const, turns: 1, at: 28_000 }],
98
+ ["run:scout", { tool: null, slot: null, turns: 0, at: 28_000 }],
99
+ ]),
100
+ );
101
+ const strip = makeDelegatesStrip({ getSnapshot: () => snapshot, theme: plainTheme });
102
+ const lines = strip.render(80);
103
+ assert.strictEqual(lines.length, 5);
104
+ assert.ok(lines[0]?.includes("Delegates"));
105
+ assert.ok(lines[1]?.includes("running 2 · gates 1 — ctrl+space"));
106
+ assert.ok(lines[2]?.includes("deployer · bash"));
107
+ assert.ok(lines[3]?.includes("scout · 1m 12s ago"));
108
+ });
109
+
110
+ it("the counter line names a configured shortcut", () => {
111
+ const snapshot = snapshotOf([fanoutWith([childView({ child: 0, runId: "run:a" })])]);
112
+ assert.ok(stripLines(snapshot, "ctrl+alt+i")[0]?.includes("ctrl+alt+i"));
113
+ });
114
+
115
+ it("a gates-only strip renders one content line, a live-work strip two, a full strip three", () => {
116
+ const gate = gateRecord({ runId: "run:fanout", gate: "deploy" });
117
+ const gatesOnly = snapshotOf([], [gate]);
118
+ assert.strictEqual(stripLines(gatesOnly).length, 1);
119
+
120
+ const liveWork = snapshotOf([fanoutWith([childView({ child: 0, runId: "run:a" })])], [gate]);
121
+ assert.strictEqual(stripLines(liveWork).length, 2);
122
+
123
+ const full = snapshotOf(
124
+ [
125
+ fanoutWith([
126
+ childView({ child: 0, runId: "run:a", agent: "a" }),
127
+ childView({ child: 1, runId: "run:b", agent: "b" }),
128
+ childView({ child: 2, runId: "run:c", agent: "c", status: "queued" }),
129
+ childView({ child: 3, runId: "run:d", agent: "d", status: "waiting" }),
130
+ ]),
131
+ ],
132
+ [gate],
133
+ );
134
+ const lines = stripLines(full);
135
+ assert.strictEqual(lines.length, 3);
136
+ for (const line of lines) assert.notStrictEqual(line, "");
137
+ });
138
+
139
+ it("the two child lines name the oldest live rows, running before waiting", () => {
140
+ const children = [
141
+ childView({ child: 0, runId: "run:wait-old", agent: "wait-old", status: "waiting" }),
142
+ childView({ child: 1, runId: "run:run-old", agent: "run-old", status: "running" }),
143
+ childView({ child: 2, runId: "run:run-new", agent: "run-new", status: "running" }),
144
+ childView({ child: 3, runId: "run:wait-new", agent: "wait-new", status: "waiting" }),
145
+ ];
146
+ const lines = stripLines(snapshotOf([fanoutWith(children)]));
147
+ assert.ok(lines[1]?.startsWith("run-old · "), lines[1]);
148
+ assert.ok(lines[2]?.startsWith("run-new · "), lines[2]);
149
+ assert.ok(!lines.some((line) => line.includes("wait-old")), lines.join("\n"));
150
+ });
151
+
152
+ it("the counter line names queued work only when non-zero", () => {
153
+ const running = snapshotOf([
154
+ fanoutWith([childView({ child: 0, runId: "run:a", status: "running" })]),
155
+ ]);
156
+ assert.ok(!(stripLines(running)[0]?.includes("queued") ?? false), stripLines(running)[0]);
157
+
158
+ const queued = snapshotOf([
159
+ fanoutWith([
160
+ childView({ child: 0, runId: "run:a", status: "running" }),
161
+ childView({ child: 1, runId: "run:b", status: "queued" }),
162
+ ]),
163
+ ]);
164
+ const line = stripLines(queued)[0];
165
+ assert.ok(line?.includes("queued 1"), line);
166
+ });
167
+
168
+ it("elapsed ages are pinned at seconds, minutes and hours", () => {
169
+ assert.strictEqual(formatElapsed(12_000), "12s");
170
+ assert.strictEqual(formatElapsed(72_000), "1m 12s");
171
+ assert.strictEqual(formatElapsed(3_900_000), "1h 05m");
172
+ const snapshot = snapshotOf([fanoutWith([childView({ child: 0, runId: "run:a" })])]);
173
+ for (const line of stripLines(snapshot)) {
174
+ assert.ok(!line.includes("null") && !line.includes("undefined"), line);
175
+ }
176
+ });
177
+
178
+ it("more running children than the budget renders the pinned overflow suffix", () => {
179
+ const children = Array.from({ length: 5 }, (_, index) =>
180
+ childView({ child: index, runId: `run:${index}`, agent: `child ${index}` }),
181
+ );
182
+ const lines = stripLines(snapshotOf([fanoutWith(children)]));
183
+ assert.strictEqual(lines.length, 3);
184
+ assert.ok(lines.at(-1)?.endsWith("+3 more"), lines.at(-1));
185
+ });
186
+
187
+ it("a snapshot with no live work and no gates renders the idle line", () => {
188
+ const snapshot = snapshotOf([runView({ runId: "run:answered", status: "answered" })]);
189
+ assert.deepStrictEqual(stripLines(snapshot), ["idle — ctrl+space"]);
190
+ const strip = makeDelegatesStrip({ getSnapshot: () => snapshot, theme: plainTheme });
191
+ const lines = strip.render(80);
192
+ assert.strictEqual(lines.length, 3);
193
+ assert.ok(lines[0]?.includes("Delegates"));
194
+ assert.ok(lines[1]?.includes("idle — ctrl+space"));
195
+ });
196
+
197
+ it("the strip never renders the selection marker", () => {
198
+ const snapshot = snapshotOf([fanoutWith([childView({ child: 0, runId: "run:a", agent: "a" })])]);
199
+ const strip = makeDelegatesStrip({ getSnapshot: () => snapshot, theme: plainTheme });
200
+ for (const line of strip.render(80)) {
201
+ assert.ok(!line.startsWith(SELECTION_MARKER), line);
202
+ }
203
+ });
@@ -0,0 +1,67 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import type { Component } from "@earendil-works/pi-tui";
3
+ import { defineComponent, makeBorderedBox } from "@ftrdotdev/pi-tui";
4
+ import { isLiveStatus } from "./inspector.ts";
5
+ import { DEFAULT_INSPECT_SHORTCUT } from "./shortcut.ts";
6
+ import type { InspectorRow, InspectorSnapshot } from "./snapshot.ts";
7
+ import { childLine, countersLine } from "./wording.ts";
8
+
9
+ export const STRIP_KEY = "delegates-inspector";
10
+ /** Content lines the render must not exceed: counters plus at most two child lines. */
11
+ export const STRIP_MAX_CONTENT_LINES = 3;
12
+ export const STRIP_CHILD_LINES = 2;
13
+ /** The strip sits above the editor, where the tracker widget sits. */
14
+ export const STRIP_PLACEMENT = "aboveEditor" as const;
15
+
16
+ /** Oldest first, running before waiting; queued children last. */
17
+ const childOrder = (row: InspectorRow): number =>
18
+ row.status === "running" ? 0 : row.status === "waiting" ? 1 : 2;
19
+
20
+ /**
21
+ * One to three content lines, never blank: the counters line, then at most two
22
+ * live child lines. `countersLine` renders `idle — ctrl+space` rather than an
23
+ * empty string, so the box stays on screen from activation to teardown. The
24
+ * candidates are the live rows in reverse projection order (the projection is
25
+ * newest first), so a newly launched child never evicts the oldest.
26
+ */
27
+ export const stripLines = (
28
+ snapshot: InspectorSnapshot,
29
+ shortcut = DEFAULT_INSPECT_SHORTCUT,
30
+ ): string[] => {
31
+ const live = snapshot.rows
32
+ .filter((row) => row.childCount === 0 && isLiveStatus(row.status))
33
+ .reverse()
34
+ .map((row, index) => ({ row, index }))
35
+ .sort((left, right) => childOrder(left.row) - childOrder(right.row) || left.index - right.index)
36
+ .map((entry) => entry.row);
37
+ const named = live.slice(0, STRIP_CHILD_LINES);
38
+ const overflow = Math.max(0, live.length - STRIP_CHILD_LINES);
39
+ const lines = [
40
+ countersLine(snapshot.counts, shortcut),
41
+ ...named.map((row, index) => childLine(row, index === named.length - 1 ? overflow : 0)),
42
+ ];
43
+ return lines.slice(0, STRIP_MAX_CONTENT_LINES);
44
+ };
45
+
46
+ /**
47
+ * The widget component: house box labelled `Delegates`, `cache: false`
48
+ * because the lines change with every publication.
49
+ */
50
+ export const makeDelegatesStrip = (options: {
51
+ readonly getSnapshot: () => InspectorSnapshot;
52
+ readonly theme: Theme;
53
+ readonly shortcut?: string;
54
+ }): Component => {
55
+ const box = makeBorderedBox(
56
+ {
57
+ render: () => stripLines(options.getSnapshot(), options.shortcut),
58
+ invalidate: () => {},
59
+ },
60
+ options.theme,
61
+ { label: "Delegates", color: "border", cache: false },
62
+ );
63
+ return defineComponent({
64
+ render: (width: number) => box.render(width),
65
+ invalidate: () => box.invalidate(),
66
+ });
67
+ };