@mrclrchtr/supi-review 7.3.1 → 7.4.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 (46) hide show
  1. package/README.md +4 -0
  2. package/node_modules/@mrclrchtr/supi-agent-runtime/README.md +10 -0
  3. package/node_modules/@mrclrchtr/supi-agent-runtime/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  4. package/node_modules/@mrclrchtr/supi-agent-runtime/package.json +7 -3
  5. package/node_modules/@mrclrchtr/supi-agent-runtime/src/api.ts +37 -1
  6. package/node_modules/@mrclrchtr/supi-agent-runtime/src/run.ts +151 -21
  7. package/node_modules/@mrclrchtr/supi-agent-runtime/src/session/agent-run-registry-types.ts +95 -0
  8. package/node_modules/@mrclrchtr/supi-agent-runtime/src/session/agent-run-registry.ts +372 -0
  9. package/node_modules/@mrclrchtr/supi-agent-runtime/src/session/transcript-format.ts +184 -0
  10. package/node_modules/@mrclrchtr/supi-agent-runtime/src/session/transcript-store.ts +284 -0
  11. package/node_modules/@mrclrchtr/supi-agent-runtime/src/types.ts +22 -0
  12. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-command.ts +206 -0
  13. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-overlay-data.ts +78 -0
  14. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-overlay-render.ts +167 -0
  15. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-overlay.ts +400 -0
  16. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-run-list.ts +95 -0
  17. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-run-switcher.ts +29 -0
  18. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-run-viewer-input.ts +69 -0
  19. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-run-viewer-render.ts +246 -0
  20. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-run-viewer.ts +400 -0
  21. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-run-viewport.ts +112 -0
  22. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-steering-input.ts +50 -0
  23. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-transcript-context.ts +56 -0
  24. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-transcript-pane.ts +386 -0
  25. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-transcript-payload.ts +66 -0
  26. package/node_modules/@mrclrchtr/supi-agent-runtime/src/ui/agents-transcript.ts +380 -0
  27. package/node_modules/@mrclrchtr/supi-code-intelligence/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  28. package/node_modules/@mrclrchtr/supi-code-intelligence/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  29. package/node_modules/@mrclrchtr/supi-code-intelligence/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  30. package/node_modules/@mrclrchtr/supi-code-intelligence/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  31. package/node_modules/@mrclrchtr/supi-code-intelligence/node_modules/@mrclrchtr/supi-lsp/package.json +3 -3
  32. package/node_modules/@mrclrchtr/supi-code-intelligence/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  33. package/node_modules/@mrclrchtr/supi-code-intelligence/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  34. package/node_modules/@mrclrchtr/supi-code-intelligence/node_modules/@mrclrchtr/supi-tree-sitter/package.json +3 -3
  35. package/node_modules/@mrclrchtr/supi-code-intelligence/package.json +5 -5
  36. package/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  37. package/package.json +4 -4
  38. package/src/review.ts +5 -2
  39. package/src/tool/review_run/child-session.ts +29 -7
  40. package/src/tool/review_run/execute.ts +6 -1
  41. package/src/tool/review_run/execution.ts +6 -0
  42. package/src/tool/review_run/register.ts +3 -1
  43. package/src/tool/review_run/runner.ts +40 -0
  44. package/src/tool/review_run/workflow.ts +7 -1
  45. package/src/tui/review-command.ts +74 -3
  46. package/src/types.ts +5 -0
@@ -0,0 +1,167 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import {
3
+ type Container,
4
+ Spacer,
5
+ Text,
6
+ truncateToWidth,
7
+ visibleWidth,
8
+ } from "@earendil-works/pi-tui";
9
+ import type { AgentsOverlayData, AgentsProfilePageEntry } from "./agents-overlay-data.ts";
10
+
11
+ const LIST_WINDOW_SIZE = 8;
12
+
13
+ /** Center a legend row using visible terminal columns, with clipping on narrow screens. */
14
+ export function centerLegend(text: string, width: number): string {
15
+ const clipped = truncateToWidth(text, Math.max(0, width - 2));
16
+ const padding = Math.max(0, Math.floor((width - visibleWidth(clipped)) / 2));
17
+ return `${" ".repeat(padding)}${clipped}`;
18
+ }
19
+
20
+ /** Render effective Agent Profiles with human-only source provenance. */
21
+ export function renderProfilesSection(
22
+ container: Container,
23
+ data: AgentsOverlayData,
24
+ selectedIndex: number,
25
+ theme: Theme,
26
+ ): void {
27
+ const profiles = data.profilePages?.profiles ?? [];
28
+ container.addChild(new Text(theme.fg("accent", theme.bold("Effective Agent Profiles")), 1, 0));
29
+ if (profiles.length === 0) {
30
+ container.addChild(new Text(theme.fg("dim", "No effective profiles."), 1, 0));
31
+ return;
32
+ }
33
+ for (const [index, profile] of visibleWindow(profiles, selectedIndex).entries()) {
34
+ const actualIndex = windowStart(profiles.length, selectedIndex) + index;
35
+ const selected = actualIndex === selectedIndex;
36
+ const label = `${selected ? "▶" : " "} ${profile.id} — ${profile.source ?? "unavailable"}`;
37
+ container.addChild(
38
+ new Text(selected ? theme.fg("accent", label) : theme.fg("dim", label), 1, 0),
39
+ );
40
+ }
41
+ const profile = profiles[selectedIndex];
42
+ if (profile) renderProfileDetails(container, profile, theme);
43
+ const omitted = data.profilePages?.omittedProfileCount ?? 0;
44
+ if (omitted > 0) {
45
+ const noun = omitted === 1 ? "profile" : "profiles";
46
+ container.addChild(
47
+ new Text(
48
+ theme.fg("warning", `${omitted} additional ${noun} omitted by the catalogue limit.`),
49
+ 1,
50
+ 0,
51
+ ),
52
+ );
53
+ }
54
+ }
55
+
56
+ /** Render bounded Profile Diagnostics and their omission disclosure. */
57
+ export function renderDiagnosticsSection(
58
+ container: Container,
59
+ data: AgentsOverlayData,
60
+ selectedIndex: number,
61
+ theme: Theme,
62
+ ): void {
63
+ const diagnostics = data.profilePages?.diagnostics ?? [];
64
+ container.addChild(new Text(theme.fg("accent", theme.bold("Profile Diagnostics")), 1, 0));
65
+ if (diagnostics.length === 0) {
66
+ container.addChild(new Text(theme.fg("success", "No Profile Diagnostics."), 1, 0));
67
+ addDiagnosticOmission(container, data.profilePages?.omittedDiagnosticCount ?? 0, theme);
68
+ return;
69
+ }
70
+ for (const [index, diagnostic] of visibleWindow(diagnostics, selectedIndex).entries()) {
71
+ const actualIndex = windowStart(diagnostics.length, selectedIndex) + index;
72
+ const selected = actualIndex === selectedIndex;
73
+ const label = `${selected ? "▶" : " "} ${diagnostic.profileId} · ${diagnostic.code}`;
74
+ container.addChild(
75
+ new Text(selected ? theme.fg("accent", label) : theme.fg("dim", label), 1, 0),
76
+ );
77
+ }
78
+ const diagnostic = diagnostics[selectedIndex];
79
+ if (diagnostic) {
80
+ container.addChild(new Spacer(1));
81
+ container.addChild(new Text(`${diagnostic.source} · ${diagnostic.code}`, 1, 0));
82
+ container.addChild(new Text(diagnostic.message, 1, 0));
83
+ if (diagnostic.directory)
84
+ container.addChild(new Text(theme.fg("dim", diagnostic.directory), 1, 0));
85
+ }
86
+ addDiagnosticOmission(container, data.profilePages?.omittedDiagnosticCount ?? 0, theme);
87
+ }
88
+
89
+ function renderProfileDetails(
90
+ container: Container,
91
+ profile: AgentsProfilePageEntry,
92
+ theme: Theme,
93
+ ): void {
94
+ container.addChild(new Spacer(1));
95
+ container.addChild(
96
+ new Text(
97
+ `Description (${profile.fieldSources?.description ?? "unavailable"}): ${profile.description}`,
98
+ 1,
99
+ 0,
100
+ ),
101
+ );
102
+ if (profile.unavailable) {
103
+ container.addChild(new Text(theme.fg("error", `Unavailable: ${profile.unavailable}`), 1, 0));
104
+ return;
105
+ }
106
+ container.addChild(new Text(`Strongest source: ${profile.source} — ${profile.directory}`, 1, 0));
107
+ container.addChild(
108
+ new Text(
109
+ theme.fg(
110
+ "dim",
111
+ `Model (${profile.fieldSources?.model ?? "session"}): ${profile.model} · Thinking (${profile.fieldSources?.thinking ?? "session"}): ${profile.thinking}`,
112
+ ),
113
+ 1,
114
+ 0,
115
+ ),
116
+ );
117
+ container.addChild(
118
+ new Text(
119
+ theme.fg(
120
+ "dim",
121
+ `Timeout (${profile.fieldSources?.timeoutMinutes ?? "default"}): ${profile.timeoutMinutes ?? "none"}`,
122
+ ),
123
+ 1,
124
+ 0,
125
+ ),
126
+ );
127
+ container.addChild(
128
+ new Text(
129
+ theme.fg(
130
+ "dim",
131
+ `Tools (${profile.fieldSources?.tools ?? "unavailable"}): ${profile.tools?.join(", ") || "none"}`,
132
+ ),
133
+ 1,
134
+ 0,
135
+ ),
136
+ );
137
+ container.addChild(
138
+ new Text(
139
+ theme.fg(
140
+ "dim",
141
+ `Prompt (${profile.fieldSources?.systemPrompt ?? "unavailable"}): ${profile.systemPrompt} · Instructions (${profile.fieldSources?.instructionScopes ?? "unavailable"}): ${profile.instructionScopes?.join(", ") || "none"}`,
142
+ ),
143
+ 1,
144
+ 0,
145
+ ),
146
+ );
147
+ }
148
+
149
+ function addDiagnosticOmission(container: Container, count: number, theme: Theme): void {
150
+ if (count > 0)
151
+ container.addChild(
152
+ new Text(
153
+ theme.fg("warning", `${count} additional diagnostics omitted by the overlay limit.`),
154
+ 1,
155
+ 0,
156
+ ),
157
+ );
158
+ }
159
+
160
+ function visibleWindow<T>(items: readonly T[], selected: number): readonly T[] {
161
+ const start = windowStart(items.length, selected);
162
+ return items.slice(start, start + LIST_WINDOW_SIZE);
163
+ }
164
+
165
+ function windowStart(length: number, selected: number, size = LIST_WINDOW_SIZE): number {
166
+ return Math.min(Math.max(0, length - size), Math.max(0, selected - Math.floor(size / 2)));
167
+ }
@@ -0,0 +1,400 @@
1
+ import { DynamicBorder } from "@earendil-works/pi-coding-agent";
2
+ import {
3
+ Container,
4
+ type Focusable,
5
+ Key,
6
+ type KeyId,
7
+ matchesKey,
8
+ type TuiMouseEvent,
9
+ type TuiMouseEventResult,
10
+ truncateToWidth,
11
+ } from "@earendil-works/pi-tui";
12
+ import type {
13
+ AgentOverlayControlResult,
14
+ AgentsDialogDependencies,
15
+ AgentsOverlayData,
16
+ AgentsOverlayRun,
17
+ } from "./agents-overlay-data.ts";
18
+ import { AGENTS_OVERLAY_MAX_HEIGHT_PERCENT } from "./agents-overlay-data.ts";
19
+ import {
20
+ centerLegend,
21
+ renderDiagnosticsSection,
22
+ renderProfilesSection,
23
+ } from "./agents-overlay-render.ts";
24
+ import { AgentsRunViewer, type AgentsRunViewerAction } from "./agents-run-viewer.ts";
25
+ import { AgentsSteeringInput } from "./agents-steering-input.ts";
26
+
27
+ type AgentsTab = "agents" | "reviews" | "profiles" | "diagnostics";
28
+
29
+ const AGENTS_TITLE = "Agents";
30
+ const CATALOGUE_HINT = "tab/←→ sections · ↑↓ select · esc close";
31
+
32
+ /** TUI-only Agent Run inspector and selected-run controller. */
33
+ export class AgentsDialog implements Focusable {
34
+ #cachedLines: string[] | undefined;
35
+ #cachedWidth: number | undefined;
36
+ #cachedHeight: number | undefined;
37
+ #profileIndex = 0;
38
+ #diagnosticIndex = 0;
39
+ #notice: string | undefined;
40
+ #stopConfirmKey: string | undefined;
41
+ #tabIndex = 0;
42
+ #busy = false;
43
+ #steeringInput: AgentsSteeringInput | undefined;
44
+ #focused = false;
45
+ #unsubscribe: (() => void) | undefined;
46
+ #elapsedInterval: ReturnType<typeof setInterval> | undefined;
47
+ readonly #runViewer: AgentsRunViewer;
48
+
49
+ constructor(
50
+ private data: AgentsOverlayData,
51
+ private readonly dependencies: AgentsDialogDependencies,
52
+ ) {
53
+ this.#runViewer = new AgentsRunViewer(
54
+ data,
55
+ dependencies.theme,
56
+ () => this.#changed(),
57
+ dependencies.keybindings,
58
+ );
59
+ this.#runViewer.setSection("agents");
60
+ this.#unsubscribe = dependencies.subscribe?.((next) => this.updateData(next));
61
+ this.#updateElapsedTimer();
62
+ }
63
+
64
+ /** Implement Pi's Focusable contract for the embedded steering input. */
65
+ get focused(): boolean {
66
+ return this.#focused;
67
+ }
68
+
69
+ set focused(value: boolean) {
70
+ if (this.#focused === value) return;
71
+ this.#focused = value;
72
+ if (this.#steeringInput) this.#steeringInput.focused = value;
73
+ this.#runViewer.focused = value;
74
+ this.#changed();
75
+ }
76
+
77
+ render(width: number): string[] {
78
+ const height = Math.max(
79
+ 1,
80
+ Math.floor((this.dependencies.tui.terminal.rows * AGENTS_OVERLAY_MAX_HEIGHT_PERCENT) / 100),
81
+ );
82
+ if (this.#cachedLines && this.#cachedWidth === width && this.#cachedHeight === height) {
83
+ return this.#cachedLines;
84
+ }
85
+ this.#cachedLines =
86
+ this.#tab() === "agents" || this.#tab() === "reviews"
87
+ ? this.#runViewer.render({
88
+ width,
89
+ height,
90
+ header: AGENTS_TITLE,
91
+ tabs: this.#tabs(),
92
+ notice: this.#notice,
93
+ steeringLines: this.#steeringLines(width),
94
+ steeringActive: this.#steeringInput !== undefined,
95
+ stopConfirmation: this.#stopConfirmKey !== undefined,
96
+ })
97
+ : this.#renderCataloguePage(width, height);
98
+ this.#cachedWidth = width;
99
+ this.#cachedHeight = height;
100
+ return this.#cachedLines;
101
+ }
102
+
103
+ handleInput(data: string): void {
104
+ if (this.#handleSteeringInput(data)) return;
105
+ if (this.#stopConfirmKey) {
106
+ this.#handleStopConfirmation(data);
107
+ return;
108
+ }
109
+ if (this.#runViewer.isList && this.#handleTabInput(data, false)) return;
110
+ if (this.#tab() === "agents" || this.#tab() === "reviews") {
111
+ this.#handleRunInput(data);
112
+ return;
113
+ }
114
+ if (this.#handleCloseInput(data) || this.#handleTabInput(data)) return;
115
+ this.#handleCatalogueInput(data);
116
+ }
117
+
118
+ /** Route fullscreen mouse input to the run and transcript panes. */
119
+ handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {
120
+ if (
121
+ (this.#tab() !== "agents" && this.#tab() !== "reviews") ||
122
+ this.#steeringInput ||
123
+ this.#stopConfirmKey
124
+ )
125
+ return undefined;
126
+ return this.#runViewer.handleMouse(event);
127
+ }
128
+
129
+ /** Replace live registry data while preserving the selected run when possible. */
130
+ updateData(data: AgentsOverlayData): void {
131
+ this.data = data;
132
+ this.#runViewer.updateData(data);
133
+ this.#updateElapsedTimer();
134
+ this.#diagnosticIndex = Math.min(
135
+ this.#diagnosticIndex,
136
+ Math.max(0, (data.profilePages?.diagnostics.length ?? 0) - 1),
137
+ );
138
+ this.#profileIndex = Math.min(
139
+ this.#profileIndex,
140
+ Math.max(0, (data.profilePages?.profiles.length ?? 0) - 1),
141
+ );
142
+ this.#tabIndex = Math.min(this.#tabIndex, this.#tabsList().length - 1);
143
+ this.#changed();
144
+ }
145
+
146
+ /** Release the live registry subscription when Pi removes the overlay. */
147
+ dispose(): void {
148
+ this.#unsubscribe?.();
149
+ this.#unsubscribe = undefined;
150
+ if (this.#elapsedInterval) clearInterval(this.#elapsedInterval);
151
+ this.#elapsedInterval = undefined;
152
+ this.#runViewer.dispose();
153
+ this.#clearSteering();
154
+ }
155
+
156
+ invalidate(): void {
157
+ this.#cachedLines = undefined;
158
+ this.#steeringInput?.invalidate();
159
+ this.#runViewer.invalidate();
160
+ }
161
+
162
+ #renderCataloguePage(width: number, height: number): string[] {
163
+ const theme = this.dependencies.theme;
164
+ const header =
165
+ height <= 3
166
+ ? [this.#line(`${AGENTS_TITLE} ${this.#tabs()}`, width)]
167
+ : [
168
+ this.#line(AGENTS_TITLE, width),
169
+ this.#line(this.#tabs(), width),
170
+ ...new DynamicBorder((text: string) => theme.fg("accent", text)).render(width),
171
+ ];
172
+ const footer = [CATALOGUE_HINT].map((hint) => centerLegend(theme.fg("dim", hint), width));
173
+ const bodyHeight = Math.max(0, height - header.length - footer.length);
174
+ const body = new Container();
175
+ if (this.#tab() === "profiles") {
176
+ renderProfilesSection(body, this.data, this.#profileIndex, theme);
177
+ } else {
178
+ renderDiagnosticsSection(body, this.data, this.#diagnosticIndex, theme);
179
+ }
180
+ const lines = [...header, ...body.render(width).slice(0, bodyHeight), ...footer].slice(
181
+ 0,
182
+ height,
183
+ );
184
+ while (lines.length < height) lines.push("");
185
+ return lines.map((line) => truncateToWidth(line, width));
186
+ }
187
+
188
+ #handleRunViewerAction(action: AgentsRunViewerAction | undefined): boolean {
189
+ if (action === "handled") {
190
+ this.#changed();
191
+ return true;
192
+ }
193
+ if (action === "close") {
194
+ this.dependencies.done();
195
+ return true;
196
+ }
197
+ const run = this.#runViewer.selectedRun;
198
+ if (action === "steer" && run) {
199
+ this.#beginSteering(run);
200
+ return true;
201
+ }
202
+ if (action === "stop" && run) {
203
+ this.#stopConfirmKey = run.runKey ?? run.taskId;
204
+ this.#notice = `Stop ${run.taskId}? Press Enter or y to confirm; Esc to cancel.`;
205
+ this.#changed();
206
+ return true;
207
+ }
208
+ return false;
209
+ }
210
+
211
+ #handleSteeringInput(data: string): boolean {
212
+ if (!this.#steeringInput) return false;
213
+ if (matchesKey(data, Key.ctrl("c"))) this.#cancelSteering();
214
+ else {
215
+ this.#steeringInput.handleInput(data);
216
+ this.#changed();
217
+ }
218
+ return true;
219
+ }
220
+
221
+ #handleCloseInput(data: string): boolean {
222
+ const interrupted = this.#matches(data, "app.interrupt", Key.escape);
223
+ const canceled = this.#matches(data, "tui.select.cancel", Key.ctrl("c"));
224
+ if (!interrupted && !canceled) return false;
225
+ this.dependencies.done();
226
+ return true;
227
+ }
228
+
229
+ #handleRunInput(data: string): boolean {
230
+ if (this.#tab() !== "agents" && this.#tab() !== "reviews") return false;
231
+ const action = this.#runViewer.handleInput(data, !this.#busy);
232
+ return this.#handleRunViewerAction(action);
233
+ }
234
+
235
+ #handleTabInput(data: string, allowArrows = true): boolean {
236
+ if (matchesKey(data, Key.tab) || (allowArrows && matchesKey(data, Key.right))) {
237
+ this.#tabIndex = (this.#tabIndex + 1) % this.#tabsList().length;
238
+ } else if (matchesKey(data, Key.shift("tab")) || (allowArrows && matchesKey(data, Key.left))) {
239
+ this.#tabIndex = (this.#tabIndex + this.#tabsList().length - 1) % this.#tabsList().length;
240
+ } else {
241
+ return false;
242
+ }
243
+ if (this.#runViewer.isList) {
244
+ this.#runViewer.setSection(this.#tab() === "reviews" ? "reviews" : "agents");
245
+ }
246
+ this.#changed();
247
+ return true;
248
+ }
249
+
250
+ #handleCatalogueInput(data: string): void {
251
+ if (this.#matches(data, "tui.select.up", Key.up)) {
252
+ this.#moveCatalogueSelection(-1);
253
+ } else if (this.#matches(data, "tui.select.down", Key.down)) {
254
+ this.#moveCatalogueSelection(1);
255
+ }
256
+ }
257
+
258
+ #moveCatalogueSelection(delta: number): void {
259
+ const pages = this.data.profilePages;
260
+ const length =
261
+ this.#tab() === "profiles" ? (pages?.profiles.length ?? 0) : (pages?.diagnostics.length ?? 0);
262
+ if (length === 0) return;
263
+ if (this.#tab() === "profiles") {
264
+ this.#profileIndex = clamp(this.#profileIndex + delta, 0, length - 1);
265
+ } else {
266
+ this.#diagnosticIndex = clamp(this.#diagnosticIndex + delta, 0, length - 1);
267
+ }
268
+ this.#changed();
269
+ }
270
+
271
+ #beginSteering(run: AgentsOverlayRun): void {
272
+ const input = new AgentsSteeringInput(run.taskId, this.dependencies.theme, {
273
+ onSubmit: (message) => {
274
+ this.#clearSteering();
275
+ this.#runControl("Sending steering…", () =>
276
+ this.dependencies.onSteer(run.runKey ?? run.taskId, message),
277
+ );
278
+ },
279
+ onEmpty: () => {
280
+ this.#notice = "Enter a steering message or press Esc to cancel.";
281
+ this.#changed();
282
+ },
283
+ onCancel: () => this.#cancelSteering(),
284
+ });
285
+ input.focused = this.#focused;
286
+ this.#steeringInput = input;
287
+ this.#notice = undefined;
288
+ this.#changed();
289
+ }
290
+
291
+ #handleStopConfirmation(data: string): void {
292
+ if (this.#matches(data, "app.interrupt", Key.escape)) {
293
+ this.#stopConfirmKey = undefined;
294
+ this.#notice = "Stop canceled.";
295
+ this.#changed();
296
+ return;
297
+ }
298
+ if (!this.#matches(data, "tui.select.confirm", Key.enter) && data.toLowerCase() !== "y") return;
299
+ const runKey = this.#stopConfirmKey;
300
+ this.#stopConfirmKey = undefined;
301
+ if (runKey) this.#runControl("Stopping selected run…", () => this.dependencies.onStop(runKey));
302
+ }
303
+
304
+ #cancelSteering(): void {
305
+ this.#clearSteering();
306
+ this.#notice = "Control canceled.";
307
+ this.#changed();
308
+ }
309
+
310
+ #clearSteering(): void {
311
+ if (this.#steeringInput) this.#steeringInput.focused = false;
312
+ this.#steeringInput = undefined;
313
+ }
314
+
315
+ #steeringLines(width: number): string[] {
316
+ if (!this.#steeringInput) return [];
317
+ return this.#steeringInput
318
+ .render(Math.max(1, width - 2))
319
+ .map((line) => this.#line(line, width));
320
+ }
321
+
322
+ #runControl(message: string, action: () => Promise<AgentOverlayControlResult>): void {
323
+ this.#busy = true;
324
+ this.#notice = message;
325
+ this.#changed();
326
+ void action()
327
+ .then((result) => {
328
+ this.#notice =
329
+ result === "accepted"
330
+ ? "Control accepted."
331
+ : result === "canceled"
332
+ ? "Control canceled."
333
+ : "Selected run is not running.";
334
+ })
335
+ .catch(() => {
336
+ this.#notice = "Control failed.";
337
+ })
338
+ .finally(() => {
339
+ this.#busy = false;
340
+ this.#changed();
341
+ });
342
+ }
343
+
344
+ #tabs(): string {
345
+ return this.#tabsList()
346
+ .map((tab, index) => {
347
+ const value = title(tab);
348
+ return index === this.#tabIndex ? `[${value}]` : value;
349
+ })
350
+ .join(this.dependencies.theme.fg("dim", " "));
351
+ }
352
+
353
+ #tabsList(): AgentsTab[] {
354
+ return this.data.profilePages
355
+ ? ["agents", "reviews", "profiles", "diagnostics"]
356
+ : ["agents", "reviews"];
357
+ }
358
+
359
+ #tab(): AgentsTab {
360
+ return this.#tabsList()[this.#tabIndex] ?? "agents";
361
+ }
362
+
363
+ #matches(
364
+ data: string,
365
+ action: Parameters<NonNullable<AgentsDialogDependencies["keybindings"]>["matches"]>[1],
366
+ fallback: KeyId,
367
+ ): boolean {
368
+ return typeof this.dependencies.keybindings?.matches === "function"
369
+ ? this.dependencies.keybindings.matches(data, action)
370
+ : matchesKey(data, fallback);
371
+ }
372
+
373
+ #line(text: string, width: number): string {
374
+ return truncateToWidth(` ${text}`, width);
375
+ }
376
+
377
+ #updateElapsedTimer(): void {
378
+ if (!this.data.runs.some((run) => run.active)) {
379
+ clearInterval(this.#elapsedInterval);
380
+ this.#elapsedInterval = undefined;
381
+ } else if (!this.#elapsedInterval) {
382
+ this.#elapsedInterval = setInterval(() => this.#runViewer.refreshElapsed(), 1_000);
383
+ this.#elapsedInterval.unref?.();
384
+ }
385
+ }
386
+
387
+ #changed(): void {
388
+ this.#cachedLines = undefined;
389
+ this.#steeringInput?.invalidate();
390
+ this.dependencies.tui.requestRender();
391
+ }
392
+ }
393
+
394
+ function clamp(value: number, minimum: number, maximum: number): number {
395
+ return Math.min(maximum, Math.max(minimum, value));
396
+ }
397
+
398
+ function title(tab: AgentsTab): string {
399
+ return tab[0]?.toUpperCase() + tab.slice(1);
400
+ }
@@ -0,0 +1,95 @@
1
+ import type { SelectItem } from "@earendil-works/pi-tui";
2
+ import type { AgentsOverlayRun } from "./agents-overlay-data.ts";
3
+
4
+ export type RunSection = "agents" | "reviews";
5
+
6
+ export function orderedRuns(
7
+ runs: readonly AgentsOverlayRun[],
8
+ section: RunSection,
9
+ query: string,
10
+ ): AgentsOverlayRun[] {
11
+ const normalizedQuery = query.trim().toLowerCase();
12
+ const groups = new Map<string, AgentsOverlayRun[]>();
13
+ for (const run of runs) {
14
+ if (isReviewRun(run) !== (section === "reviews") || !matchesQuery(run, normalizedQuery))
15
+ continue;
16
+ const group = groups.get(run.batchId) ?? [];
17
+ group.push(run);
18
+ groups.set(run.batchId, group);
19
+ }
20
+ return [...groups.values()]
21
+ .sort((left, right) => latestStarted(right) - latestStarted(left))
22
+ .flat();
23
+ }
24
+
25
+ export function runListItem(run: AgentsOverlayRun, now = Date.now()): SelectItem {
26
+ const batchLabel = run.display?.batchLabel ?? run.batchId.slice(0, 8);
27
+ return {
28
+ value: run.key,
29
+ label: `${run.taskId} — ${run.label}`,
30
+ description: `${run.status} · ${formatElapsed(run, now)} · batch ${batchLabel}`,
31
+ };
32
+ }
33
+
34
+ export function runListPreview(run: AgentsOverlayRun): string[] {
35
+ const display = run.result?.display;
36
+ const target = display?.target ?? run.display?.target;
37
+ const findings =
38
+ display?.findingCount === undefined
39
+ ? undefined
40
+ : `${display.findingCount} findings${display.blockingFindingCount ? `, ${display.blockingFindingCount} blocking` : ""}`;
41
+ const review = [
42
+ target ? `Target: ${target}` : undefined,
43
+ display?.verdict ? `verdict ${display.verdict}` : undefined,
44
+ findings,
45
+ ]
46
+ .filter((value): value is string => value !== undefined)
47
+ .join(" · ");
48
+ const summary = run.active
49
+ ? run.recentActivity?.at(-1)
50
+ : run.result?.finalText?.split("\n", 1)[0]?.trim();
51
+ return [
52
+ ...(review ? [review] : []),
53
+ ...(summary ? [`${run.active ? "Activity" : "Result"}: ${summary}`] : []),
54
+ ];
55
+ }
56
+
57
+ function matchesQuery(run: AgentsOverlayRun, query: string): boolean {
58
+ if (!query) return true;
59
+ return [
60
+ run.taskId,
61
+ run.label,
62
+ run.kind,
63
+ run.batchId,
64
+ run.taskDescription,
65
+ run.display?.target,
66
+ run.result?.display?.target,
67
+ run.result?.display?.verdict,
68
+ run.result?.finalText,
69
+ ...(run.recentActivity ?? []),
70
+ ]
71
+ .filter((value): value is string => typeof value === "string")
72
+ .join(" ")
73
+ .toLowerCase()
74
+ .includes(query);
75
+ }
76
+
77
+ function isReviewRun(run: AgentsOverlayRun): boolean {
78
+ return run.kind.toLowerCase().includes("review");
79
+ }
80
+
81
+ function latestStarted(group: readonly AgentsOverlayRun[]): number {
82
+ return Math.max(...group.map((run) => run.startedAt));
83
+ }
84
+
85
+ export function formatElapsed(run: AgentsOverlayRun, now = Date.now()): string {
86
+ const end = run.finishedAt ?? now;
87
+ const seconds = Math.max(0, Math.floor((end - run.startedAt) / 1_000));
88
+ if (seconds < 60) return `${seconds}s`;
89
+ const minutes = Math.floor(seconds / 60);
90
+ const remainder = seconds % 60;
91
+ if (minutes < 60) return remainder === 0 ? `${minutes}m` : `${minutes}m ${remainder}s`;
92
+ const hours = Math.floor(minutes / 60);
93
+ const remainingMinutes = minutes % 60;
94
+ return remainingMinutes === 0 ? `${hours}h` : `${hours}h ${remainingMinutes}m`;
95
+ }
@@ -0,0 +1,29 @@
1
+ import type { AgentsOverlayRun } from "./agents-overlay-data.ts";
2
+
3
+ /** Return the next selected run and its index for wrapped or bounded navigation. */
4
+ export function nextRunSelection(
5
+ runs: readonly AgentsOverlayRun[],
6
+ currentIndex: number,
7
+ delta: number,
8
+ wrap: boolean,
9
+ ): { readonly index: number; readonly run: AgentsOverlayRun } | undefined {
10
+ if (runs.length === 0) return undefined;
11
+ const startIndex = wrap && currentIndex < 0 ? (delta < 0 ? 0 : runs.length - 1) : currentIndex;
12
+ const index = wrap
13
+ ? (((startIndex + delta) % runs.length) + runs.length) % runs.length
14
+ : Math.max(0, Math.min(runs.length - 1, startIndex + delta));
15
+ const run = runs[index];
16
+ return run ? { index, run } : undefined;
17
+ }
18
+
19
+ /** Describe where the selected run sits in the filtered result set. */
20
+ export function runSwitcherPosition(
21
+ runs: readonly AgentsOverlayRun[],
22
+ selectedKey: string | undefined,
23
+ ): { readonly position?: number; readonly canSwitch: boolean } {
24
+ const index = runs.findIndex((run) => run.key === selectedKey);
25
+ return {
26
+ ...(index >= 0 ? { position: index + 1 } : {}),
27
+ canSwitch: runs.length > 1 || (runs.length > 0 && index < 0),
28
+ };
29
+ }