repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/ultra/ui.ts
RawFile outline
- STATUS_ICON line 51
- PHASE_ICON line 58
- RESET line 64
- fitLine line 66
- phaseCounter line 73
- summaryLine line 82
- headerLine line 95
- descriptionLine line 103
- compactTokens line 107
- elapsed line 113
- agentDetailLines line 119
- label line 120
- value line 120
- DisplayRow line 158
- rowsInPhase line 162
- displayRows line 166
- phaseLabel line 191
- phaseDetail line 223
- queuedDetail line 231
- automaticPhase line 237
- toolPhaseExpanded line 245
- aggregateUsage line 251
- metricsLine line 261
- divider line 276
- expandHint line 286
- renderToolBoard line 305
- UltraResultComponent line 382
- constructor line 383
- render line 389
- invalidate line 393
- asReducerState line 408
- renderUltraResult line 455
- OverlayAgentControls line 479
- followUp line 480
- OverlayHost line 488
- requestRender line 489
- terminal line 490
- UltraOverlayOptions line 493
- host line 494
- theme line 495
- onRunAbort line 497
- now line 499
- throttleMs line 501
- workflowName line 502
- workflowDescription line 503
- toolUi line 505
- toolsExpanded line 506
- hideToolPreviews line 507
- isToolsExpandKey line 508
- onToolsExpand line 509
- cwd line 510
- DEFAULT_THROTTLE_MS line 513
- transcriptLines line 516
- normalizeToolResult line 523
- content line 525
- details line 525
- isError line 525
- type line 529
- text line 530
- data line 531
- mimeType line 532
- UltraOverlayComponent line 551
- state line 552
- focusKey line 553
- expandedPhases line 554
- collapsedPhases line 555
- followMode line 556
- scrollOffset line 557
- lastMaxScroll line 558
- lastRenderAt line 559
- steerMode line 560
- steerBuffer line 561
- steeredFlag line 562
- abortArmedAt line 563
- transcriptTools line 564
- host line 576
- theme line 577
- onRunAbort line 578
- now line 579
- throttleMs line 580
- toolUi line 581
- toolsExpanded line 582
- hideToolPreviews line 583
- isToolsExpandKey line 584
- onToolsExpand line 585
- cwd line 586
- constructor line 588
- steered line 607
- reducerState line 612
- handleProgress line 618
- handleInput line 644
- phaseExpanded line 693
- visibleRows line 699
- displayKey line 703
- ensureVisibleFocus line 709
- focusBy line 724
- toggleFocusedPhase line 744
- scrollBy line 758
- focusedRow line 769
- focusedPhase line 775
- openSteer line 782
- abortFocused line 789
- handleSteerInput line 796
- submitSteer line 823
- transcriptTool line 845
- transcriptBodyLines line 900
- flushText line 911
- render line 939
- invalidate line 1075
// ultra — live progress sinks (Task 7). TWO surface-specific views over the
// shared `progress.ts` reducer, render + input only, NO orchestration:
//
// • TOOL sink (Layer 7a) — a `renderResult(result, opts, theme, ctx)` that
// folds NOTHING itself: it reads the reducer state off `result.details`
// (pushed through the SDK `onUpdate` channel by the engine) and draws a
// collapsed/expanded board. Read-only — no `controls`, no steering, and it
// NEVER renders per-agent bodies/transcripts. Modeled on `tune`'s tools.
//
// • COMMAND overlay (Layers 7b/7c) — a `ctx.ui.custom` `Component` that folds
// the `onProgress` stream itself, renders the board + a focused-agent
// drill-in transcript (follow-mode + throttle), and is the ONLY surface
// with per-agent steering/abort. Modeled on `devil/ui.ts`.
//
// Both sinks consume only what `progress.ts` defines; this module never
// constructs an SDK session and never holds orchestration logic.
import type {
AgentToolResult,
Theme,
ToolRenderResultOptions,
} from "@earendil-works/pi-coding-agent";
import {
getMarkdownTheme,
keyHint,
ToolExecutionComponent,
} from "@earendil-works/pi-coding-agent";
import {
type Component,
Markdown,
matchesKey,
type TUI,
truncateToWidth,
} from "@earendil-works/pi-tui";
import {
type AgentControls,
type AgentStatus,
initialReducerState,
type PlannedPhase,
type ReducerState,
reduceProgress,
type TranscriptItem,
type UltraProgressEvent,
} from "./progress.ts";
import { formatUltraResultMarkdown } from "./wire.ts";
// ---------------------------------------------------------------------------
// Shared board helpers (used by both sinks)
// ---------------------------------------------------------------------------
const STATUS_ICON: Record<AgentStatus, string> = {
queued: "",
running: "",
done: "",
dropped: "",
};
const PHASE_ICON = {
"agents-pending": "",
"condition-pending": "",
skipped: "−",
} as const;
const RESET = /\x1b\[0m/g;
function fitLine(text: string, width: number): string {
const w = Math.max(1, width);
const out = truncateToWidth(text, w);
return text.includes("\x1b") ? out : out.replace(RESET, "");
}
/** `phase N/len` from the reducer's ordered phase plan. */
function phaseCounter(state: ReducerState): string {
if (state.phases.length === 0) return "";
const idx = state.phase
? state.phases.findIndex((phase) => phase.phase === state.phase) + 1
: 0;
return `phase ${idx}/${state.phases.length}`;
}
/** One-line aggregate with terminal, active, queued, and later work counts. */
function summaryLine(state: ReducerState): string {
const parts: string[] = [];
const pc = phaseCounter(state);
if (pc) parts.push(pc);
parts.push(`${state.done}/${state.total} done`);
if (state.running > 0) parts.push(`${state.running} active`);
if (state.queued > 0) parts.push(`${state.queued} queued`);
if (state.later > 0)
parts.push(`${state.later} later phase${state.later === 1 ? "" : "s"}`);
if (state.dropped > 0) parts.push(`${state.dropped}✗`);
return parts.join(" · ");
}
function headerLine(state: ReducerState, brand = "ultra"): string {
const wallTime =
state.startedAt === undefined
? undefined
: `${elapsed(state.startedAt, state.endedAt)} elapsed`;
return [brand, state.workflowName, wallTime].filter(Boolean).join(" · ");
}
function descriptionLine(state: ReducerState): string | undefined {
return state.workflowDescription;
}
function compactTokens(tokens: number): string {
if (tokens < 1000) return String(tokens);
const value = tokens / 1000;
return `${value >= 10 ? Math.round(value) : value.toFixed(1).replace(/\.0$/, "")}k`;
}
function elapsed(startedAt: number, endedAt = Date.now()): string {
const seconds = Math.max(0, Math.floor((endedAt - startedAt) / 1000));
if (seconds < 60) return `${seconds}s`;
return `${Math.floor(seconds / 60)}m ${seconds % 60}s`;
}
function agentDetailLines(row: ReducerState["rows"][number]): string[] {
const details: { label: string; value: string }[] = [];
const failureModel = row.failure?.model;
const model = row.tier
? `${row.tier}${row.model ? ` → ${row.model}` : ""}`
: (row.model ?? failureModel);
const thinkingLevel = row.thinkingLevel ?? row.failure?.thinkingLevel;
const modelValue = [model, thinkingLevel].filter(Boolean).join(" · ");
if (modelValue) details.push({ label: "model", value: modelValue });
const usageValue = [
row.startedAt === undefined
? undefined
: elapsed(row.startedAt, row.endedAt),
row.usage ? `${compactTokens(row.usage.total)} tokens` : undefined,
]
.filter(Boolean)
.join(" · ");
if (usageValue) details.push({ label: "usage", value: usageValue });
if (row.failure) {
details.push({
label: "failure",
value: `${row.failure.code}: ${row.failure.message}`,
});
details.push({
label: "retry",
value: `${row.failure.retryable ? "retryable" : "terminal"} · ${row.failure.attempts} attempt${row.failure.attempts === 1 ? "" : "s"}`,
});
if (row.item !== undefined)
details.push({ label: "item", value: JSON.stringify(row.item) });
if (row.failure.transcriptTail)
details.push({ label: "tail", value: row.failure.transcriptTail });
}
if (row.action) details.push({ label: "action", value: row.action });
return details.map(
(detail, index) =>
`${index === details.length - 1 ? "└" : "├"} ${detail.label.padEnd(6)} ${detail.value}`,
);
}
type DisplayRow =
| { kind: "agent"; row: ReducerState["rows"][number] }
| { kind: "phase"; phase: PlannedPhase; expanded: boolean };
function rowsInPhase(state: ReducerState, phase: string) {
return state.rows.filter((row) => row.phase === phase);
}
function displayRows(
state: ReducerState,
expanded: (phase: PlannedPhase) => boolean,
): DisplayRow[] {
const out: DisplayRow[] = [];
const seen = new Set<string>();
for (const phase of state.phases) {
const open = expanded(phase);
out.push({ kind: "phase", phase, expanded: open });
if (!open) continue;
for (const row of rowsInPhase(state, phase.phase)) {
seen.add(row.agentId);
out.push({ kind: "agent", row });
}
}
for (const row of state.rows) {
if (
!seen.has(row.agentId) &&
!state.phases.some((p) => p.phase === row.phase)
)
out.push({ kind: "agent", row });
}
return out;
}
function phaseLabel(
state: ReducerState,
phase: PlannedPhase,
expanded: boolean,
): string {
if (phase.status === "agents-pending")
return `${PHASE_ICON[phase.status]} ${phase.phase} · agents not planned yet`;
if (phase.status === "condition-pending")
return `${PHASE_ICON[phase.status]} ${phase.phase} · conditional`;
if (phase.status === "skipped")
return `${PHASE_ICON[phase.status]} ${phase.phase} · skipped`;
const rows = rowsInPhase(state, phase.phase);
const done = rows.filter((row) => row.status === "done").length;
const running = rows.filter((row) => row.status === "running").length;
const dropped = rows.filter((row) => row.status === "dropped").length;
const queued = rows.filter((row) => row.status === "queued").length;
const counts = [
`${done}/${rows.length} done`,
running ? `${running} active` : "",
queued ? `${queued} queued` : "",
dropped ? `${dropped} dropped` : "",
]
.filter(Boolean)
.join(" · ");
if (phase.status === "done")
return `${dropped ? STATUS_ICON.dropped : STATUS_ICON.done} ${phase.phase}${counts ? ` · ${counts}` : " · done"}`;
if (state.phase === phase.phase)
return `${expanded ? "▼" : "▶"} ${phase.phase}${counts ? ` · ${counts}` : ""}`;
return `${STATUS_ICON.queued} ${phase.phase}${rows.length ? ` · ${rows.length} queued` : ""}`;
}
function phaseDetail(phase: PlannedPhase): string | undefined {
if (phase.status === "agents-pending")
return "earlier results determine which agents are needed";
if (phase.status === "condition-pending")
return "earlier results determine whether this phase runs";
return undefined;
}
function queuedDetail(state: ReducerState, phase: string): string {
return state.phase === phase
? "waiting for concurrency slot"
: "starts when phase begins";
}
function automaticPhase(state: ReducerState): string | undefined {
const current = state.phases.find(
(phase) => phase.phase === state.phase && phase.status === "resolved",
);
return (current ?? state.phases.find((phase) => phase.status === "resolved"))
?.phase;
}
function toolPhaseExpanded(state: ReducerState, phase: PlannedPhase): boolean {
if (rowsInPhase(state, phase.phase).some((row) => row.status === "dropped"))
return true;
return phase.phase === automaticPhase(state);
}
function aggregateUsage(state: ReducerState): { tokens: number; cost: number } {
let tokens = 0;
let cost = 0;
for (const row of state.rows) {
tokens += row.usage?.total ?? 0;
cost += row.usage?.cost ?? 0;
}
return { tokens, cost };
}
function metricsLine(state: ReducerState): string | undefined {
if (state.total === 0) return undefined;
const usage = aggregateUsage(state);
const resumed = state.rows.filter((row) => row.cached).length;
if (usage.tokens === 0 && state.done === 0 && resumed === 0) return undefined;
return [
usage.tokens ? `${compactTokens(usage.tokens)} tokens` : "",
usage.cost ? `$${usage.cost.toFixed(4)}` : "",
`checkpoint ${state.done}/${state.total}`,
resumed ? `${resumed} resumed` : "",
]
.filter(Boolean)
.join(" · ");
}
function divider(label: string, width: number): string {
const w = Math.max(1, width);
const text = ` ${label} `;
if (text.length >= w) return fitLine(label, w);
const left = Math.floor((w - text.length) / 2);
const right = w - text.length - left;
return `${"─".repeat(left)}${text}${"─".repeat(right)}`;
}
/** The collapsed expand hint. Guarded: `keyHint` needs an initialised theme. */
function expandHint(): string {
try {
return keyHint("app.tools.expand", "to expand");
} catch {
return "to expand";
}
}
// ---------------------------------------------------------------------------
// Layer 7a — TOOL sink: `renderResult` over the reducer state
// ---------------------------------------------------------------------------
/**
* Pure board renderer for the tool surface. Collapsed = aggregate counts +
* one-line summary + the expand hint (NEVER any per-agent line or transcript
* body). Expanded = phase-first summaries with active and failed phases opened,
* plus model/usage metadata and the final outcome. The drill-in transcript is
* the OVERLAY's job, never the tool sink's, so `state.transcripts` is never read here.
*/
function renderToolBoard(
state: ReducerState,
options: ToolRenderResultOptions,
theme: Theme,
width: number,
): string[] {
const w = Math.max(1, width);
const lines: string[] = [];
const header = `${headerLine(state, theme.bold("ultra"))} · ${summaryLine(state)}`;
lines.push(theme.fg("accent", fitLine(header, w)));
const description = descriptionLine(state);
if (description) lines.push(theme.fg("muted", fitLine(description, w)));
if (!options.expanded) {
// Collapsed: summary + expand hint only. No bodies, ever.
lines.push(theme.fg("muted", fitLine(expandHint(), w)));
return lines.map((l) => fitLine(l, w));
}
// Expanded: one phase row each, with only active or failed phases opened.
for (const display of displayRows(state, (phase) =>
toolPhaseExpanded(state, phase),
)) {
if (display.kind === "phase") {
const color =
display.phase.status === "done"
? "success"
: state.phase === display.phase.phase
? "accent"
: "dim";
lines.push(
theme.fg(
color,
fitLine(phaseLabel(state, display.phase, display.expanded), w),
),
);
const detail = phaseDetail(display.phase);
if (detail) lines.push(theme.fg("muted", fitLine(` └ ${detail}`, w)));
continue;
}
const { row } = display;
const icon = STATUS_ICON[row.status];
let identity = ` ${icon} ${row.agentId} · ${row.status}`;
if (row.summary) identity += ` · ${row.summary}`;
if (row.cached) identity += " · checkpoint reused";
if (row.status === "queued")
identity += ` · ${queuedDetail(state, row.phase)}`;
lines.push(theme.fg("dim", fitLine(identity, w)));
for (const detail of agentDetailLines(row)) {
lines.push(theme.fg("muted", fitLine(` ${detail}`, w)));
}
}
const metrics = metricsLine(state);
if (metrics) lines.push(theme.fg("muted", fitLine(metrics, w)));
if (state.workflowResult) {
lines.push(theme.fg("border", divider("outcome", w)));
lines.push(
...new Markdown(
formatUltraResultMarkdown(state.workflowResult),
0,
0,
getMarkdownTheme(),
).render(w),
);
}
return lines.map((l) => fitLine(l, w));
}
/**
* A throwaway `Component` that draws the tool-surface board for ONE render.
* It holds no cache: every `render(width)` recomputes from the snapshot it was
* built with, so a fresh `renderResult` call (one per `onUpdate`, P2) with fresh
* `details` always tracks the latest reducer state (no frozen snapshot).
*/
class UltraResultComponent implements Component {
constructor(
private readonly state: ReducerState,
private readonly options: ToolRenderResultOptions,
private readonly theme: Theme,
) {}
render(width: number): string[] {
return renderToolBoard(this.state, this.options, this.theme, width);
}
invalidate(): void {
// No cached state — nothing to drop.
}
}
/**
* Accept `result.details` only when it structurally IS a reducer state. It
* reaches us over an SDK-controlled channel (structured-cloned per `onUpdate`
* tick, and re-hydrated from session persistence on resume), so a truthy-but-
* foreign value can arrive — a resumed/foreign tool result, a payload from
* another producer, a serialization wrapper. The board reads `state.phases`
* and `state.rows` unguarded, so anything lacking them falls back to an empty
* board. This is the single choke point: an uncaught throw in `renderResult`
* escapes tool-error handling and crashes the whole TUI render loop.
*/
function asReducerState(details: unknown): ReducerState {
const d = details as Partial<ReducerState> | null | undefined;
const phaseStatuses: readonly string[] = [
"resolved",
"agents-pending",
"condition-pending",
"done",
"skipped",
];
const agentStatuses: readonly string[] = [
"queued",
"running",
"done",
"dropped",
];
const phasesValid =
Array.isArray(d?.phases) &&
d.phases.every(
(phase) =>
typeof phase === "object" &&
phase !== null &&
typeof phase.phase === "string" &&
phaseStatuses.includes(phase.status) &&
Array.isArray(phase.agentIds) &&
phase.agentIds.every((agentId: unknown) => typeof agentId === "string"),
);
const rowsValid =
Array.isArray(d?.rows) &&
d.rows.every(
(row) =>
typeof row === "object" &&
row !== null &&
typeof row.agentId === "string" &&
typeof row.phase === "string" &&
agentStatuses.includes(row.status),
);
return d && phasesValid && rowsValid
? (d as ReducerState)
: initialReducerState();
}
/**
* Tool-surface `renderResult`: reads the reducer state off `result.details`
* (pushed through `onUpdate`) and returns a board `Component`. Read-only — no
* `controls`, no steering. Assignable to `ToolDefinition.renderResult` (the
* unused 4th `context` arg is accepted but never read on this surface).
*/
export function renderUltraResult(
result: AgentToolResult<ReducerState>,
options: ToolRenderResultOptions,
theme: Theme,
_context?: unknown,
): Component {
return new UltraResultComponent(
asReducerState(result?.details),
options,
theme,
);
}
// ---------------------------------------------------------------------------
// Layers 7b/7c — COMMAND overlay: board + drill-in transcript + steer/abort
// ---------------------------------------------------------------------------
/**
* Per-agent controls as the OVERLAY consumes them. Extends the locked
* `AgentControls` (`{steer, abort}`) with an optional `followUp` for re-prompting
* an agent that has already finished. The runner binds `followUp` on a finished
* agent's controls (cross-task contract — see module notes); when absent the
* follow-up affordance is simply inert.
*/
export interface OverlayAgentControls extends AgentControls {
followUp?(text: string): Promise<void> | void;
}
/**
* Minimal host surface the overlay needs. The real `TUI` (passed by
* `ctx.ui.custom`) satisfies this structurally; tests inject a fake with a
* `requestRender` spy + a fixed terminal size.
*/
export interface OverlayHost {
requestRender(): void;
terminal: { rows: number; columns: number };
}
export interface UltraOverlayOptions {
host: OverlayHost;
theme: Theme;
/** Confirmed `Esc Esc` run-level abort through the command path's AbortController. */
onRunAbort: () => void;
/** Injectable clock for the render throttle (default `Date.now`). */
now?: () => number;
/** Render-throttle window in ms (default 80, matching devil). */
throttleMs?: number;
workflowName?: string;
workflowDescription?: string;
/** Optional Pi TUI handle; when present, tool transcript rows use Pi's native tool renderer. */
toolUi?: TUI;
toolsExpanded?: () => boolean;
hideToolPreviews?: boolean;
isToolsExpandKey?: (data: string) => boolean;
onToolsExpand?: () => void;
cwd?: string;
}
const DEFAULT_THROTTLE_MS = 80;
/** Render transcript markdown with Pi's own markdown renderer. */
function transcriptLines(text: string, width: number): string[] {
if (!text) return [];
return new Markdown(text, 0, 0, getMarkdownTheme()).render(
Math.max(1, width),
);
}
function normalizeToolResult(item: TranscriptItem & { kind: "tool" }) {
const r = item.result as
| { content?: unknown; details?: unknown; isError?: boolean }
| undefined;
const content = Array.isArray(r?.content)
? (r.content as Array<{
type: string;
text?: string;
data?: string;
mimeType?: string;
}>)
: item.fallback
? [{ type: "text", text: item.fallback }]
: [];
return {
content,
details: r?.details,
isError: item.isError ?? r?.isError ?? false,
};
}
/**
* The `ctx.ui.custom` overlay (command surface only). Folds the `onProgress`
* stream with the shared reducer, renders the board + a focused-agent drill-in
* transcript (follow-mode), and exposes per-agent steer/abort + global abort.
* Render + input only: it holds NO orchestration — the run is driven elsewhere
* and pushes events in via `handleProgress`.
*/
export class UltraOverlayComponent implements Component {
private state: ReducerState;
private focusKey: string | undefined;
private readonly expandedPhases = new Set<string>();
private readonly collapsedPhases = new Set<string>();
private followMode = true;
private scrollOffset = 0;
private lastMaxScroll = 0;
private lastRenderAt = Number.NEGATIVE_INFINITY;
private steerMode = false;
private steerBuffer = "";
private steeredFlag = false;
private abortArmedAt: number | undefined;
private readonly transcriptTools = new Map<
string,
{
component: ToolExecutionComponent;
args: unknown;
result: unknown;
isError: boolean | undefined;
isPartial: boolean | undefined;
expanded: boolean;
}
>();
private readonly host: OverlayHost;
private readonly theme: Theme;
private readonly onRunAbort: () => void;
private readonly now: () => number;
private readonly throttleMs: number;
private readonly toolUi: TUI | undefined;
private readonly toolsExpanded: () => boolean;
private readonly hideToolPreviews: boolean;
private readonly isToolsExpandKey: (data: string) => boolean;
private readonly onToolsExpand: () => void;
private readonly cwd: string | undefined;
constructor(options: UltraOverlayOptions) {
this.state = initialReducerState({
workflowName: options.workflowName,
workflowDescription: options.workflowDescription,
});
this.host = options.host;
this.theme = options.theme;
this.onRunAbort = options.onRunAbort;
this.now = options.now ?? Date.now;
this.throttleMs = options.throttleMs ?? DEFAULT_THROTTLE_MS;
this.toolUi = options.toolUi;
this.toolsExpanded = options.toolsExpanded ?? (() => false);
this.hideToolPreviews = options.hideToolPreviews ?? false;
this.isToolsExpandKey = options.isToolsExpandKey ?? (() => false);
this.onToolsExpand = options.onToolsExpand ?? (() => {});
this.cwd = options.cwd;
}
/** True once the human has submitted a steer this run (enqueue-time taint). */
get steered(): boolean {
return this.steeredFlag;
}
/** Test/inspection accessor for the folded reducer state. */
get reducerState(): ReducerState {
return this.state;
}
// ---- progress sink: fold + throttled re-render -----------------------
handleProgress(event: UltraProgressEvent): void {
const previousPhase = this.state.phase;
this.state = reduceProgress(this.state, event);
if (event.kind === "start" && event.phase !== previousPhase) {
if (previousPhase) {
this.expandedPhases.delete(previousPhase);
this.collapsedPhases.delete(previousPhase);
}
this.collapsedPhases.delete(event.phase);
this.focusKey = `agent:${event.agentId}`;
this.followMode = true;
this.scrollOffset = 0;
}
this.ensureVisibleFocus();
const now = this.now();
// Terminal events bypass the throttle so the final board is never lost
// behind the render window.
const force = event.kind === "end" || event.kind === "phase";
if (force || now - this.lastRenderAt >= this.throttleMs) {
this.lastRenderAt = now;
this.host.requestRender();
}
}
// ---- input ------------------------------------------------------------
handleInput(data: string): void {
if (this.isToolsExpandKey(data)) {
this.onToolsExpand();
this.host.requestRender();
return;
}
if (this.steerMode) {
this.handleSteerInput(data);
return;
}
if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
const now = this.now();
if (this.abortArmedAt !== undefined && now - this.abortArmedAt <= 1500) {
this.abortArmedAt = undefined;
this.onRunAbort();
} else {
this.abortArmedAt = now;
this.host.requestRender();
}
return;
}
this.abortArmedAt = undefined;
if (matchesKey(data, "tab")) {
this.focusBy(1, true);
} else if (matchesKey(data, "down")) {
this.focusBy(1, false);
} else if (matchesKey(data, "up")) {
this.focusBy(-1, false);
} else if (matchesKey(data, "return") || matchesKey(data, "enter")) {
this.toggleFocusedPhase();
} else if (
matchesKey(data, "pageUp") ||
matchesKey(data, "alt+up") ||
matchesKey(data, "ctrl+u")
) {
this.scrollBy(-10);
} else if (
matchesKey(data, "pageDown") ||
matchesKey(data, "alt+down") ||
matchesKey(data, "ctrl+d")
) {
this.scrollBy(10);
} else if (data === "s") {
this.openSteer();
} else if (data === "x") {
this.abortFocused();
}
}
private phaseExpanded(phase: PlannedPhase): boolean {
if (this.collapsedPhases.has(phase.phase)) return false;
if (this.expandedPhases.has(phase.phase)) return true;
return phase.phase === automaticPhase(this.state);
}
private visibleRows(): DisplayRow[] {
return displayRows(this.state, (phase) => this.phaseExpanded(phase));
}
private displayKey(display: DisplayRow): string {
return display.kind === "phase"
? `phase:${display.phase.phase}`
: `agent:${display.row.agentId}`;
}
private ensureVisibleFocus(): void {
const visible = this.visibleRows();
if (visible.length === 0) {
this.focusKey = undefined;
return;
}
if (
this.focusKey &&
visible.some((row) => this.displayKey(row) === this.focusKey)
)
return;
const firstAgent = visible.find((row) => row.kind === "agent");
this.focusKey = this.displayKey(firstAgent ?? visible[0]);
}
private focusBy(delta: number, agentsOnly: boolean): void {
const candidates = this.visibleRows().filter(
(row) => !agentsOnly || row.kind === "agent",
);
if (candidates.length === 0) return;
const current = candidates.findIndex(
(row) => this.displayKey(row) === this.focusKey,
);
const next =
current < 0
? 0
: agentsOnly
? (current + delta + candidates.length) % candidates.length
: Math.min(candidates.length - 1, Math.max(0, current + delta));
this.focusKey = this.displayKey(candidates[next]);
this.followMode = true;
this.scrollOffset = 0;
this.host.requestRender();
}
private toggleFocusedPhase(): void {
const phase = this.focusedPhase();
if (!phase) return;
if (this.phaseExpanded(phase)) {
this.expandedPhases.delete(phase.phase);
this.collapsedPhases.add(phase.phase);
this.focusKey = `phase:${phase.phase}`;
} else {
this.collapsedPhases.delete(phase.phase);
this.expandedPhases.add(phase.phase);
}
this.host.requestRender();
}
private scrollBy(delta: number): void {
if (delta < 0) {
this.followMode = false;
this.scrollOffset = Math.max(0, this.scrollOffset + delta);
} else {
this.scrollOffset += delta;
if (this.scrollOffset >= this.lastMaxScroll) this.followMode = true;
}
this.host.requestRender();
}
private focusedRow() {
if (!this.focusKey?.startsWith("agent:")) return undefined;
const id = this.focusKey.slice("agent:".length);
return this.state.rows.find((row) => row.agentId === id);
}
private focusedPhase(): PlannedPhase | undefined {
if (!this.focusKey) return undefined;
const row = this.focusedRow();
const name = row?.phase ?? this.focusKey.slice("phase:".length);
return this.state.phases.find((phase) => phase.phase === name);
}
private openSteer(): void {
if (!this.focusedRow()) return;
this.steerMode = true;
this.steerBuffer = "";
this.host.requestRender();
}
private abortFocused(): void {
const controls = this.focusedRow()?.controls as
| OverlayAgentControls
| undefined;
if (controls?.abort) void Promise.resolve(controls.abort()).catch(() => {});
}
private handleSteerInput(data: string): void {
if (matchesKey(data, "escape")) {
// Cancel the buffer back to normal mode — does NOT abort the run.
this.steerMode = false;
this.steerBuffer = "";
this.host.requestRender();
return;
}
if (matchesKey(data, "return") || matchesKey(data, "enter")) {
this.submitSteer();
return;
}
if (matchesKey(data, "backspace")) {
this.steerBuffer = this.steerBuffer.slice(0, -1);
this.host.requestRender();
return;
}
// Accept a single printable character (excludes control bytes + DEL).
if (data.length === 1) {
const code = data.charCodeAt(0);
if (code >= 0x20 && code !== 0x7f) {
this.steerBuffer += data;
this.host.requestRender();
}
}
}
private submitSteer(): void {
const text = this.steerBuffer;
const row = this.focusedRow();
this.steerMode = false;
this.steerBuffer = "";
this.host.requestRender();
if (!row || !text) return;
const controls = row.controls as OverlayAgentControls | undefined;
if (row.status === "running") {
// Taint the run synchronously, at enqueue time (before the steer
// promise settles) — this is what the test asserts and what v2
// caching keys off.
this.steeredFlag = true;
if (controls?.steer)
void Promise.resolve(controls.steer(text)).catch(() => {});
} else {
// A finished agent is re-prompted as a follow-up, not steered.
if (controls?.followUp)
void Promise.resolve(controls.followUp(text)).catch(() => {});
}
}
private transcriptTool(
agentId: string,
item: TranscriptItem & { kind: "tool" },
index: number,
): ToolExecutionComponent | undefined {
if (!this.toolUi || !this.cwd) return undefined;
const key = `${agentId}:${item.toolCallId ?? index}`;
let cached = this.transcriptTools.get(key);
if (!cached) {
const component = new ToolExecutionComponent(
item.toolName,
item.toolCallId ?? key,
item.args ?? {},
undefined,
undefined,
this.toolUi,
this.cwd,
);
component.markExecutionStarted();
component.setArgsComplete();
cached = {
component,
args: item.args,
result: undefined,
isError: undefined,
isPartial: undefined,
expanded: false,
};
this.transcriptTools.set(key, cached);
} else if (item.args !== undefined && item.args !== cached.args) {
cached.component.updateArgs(item.args);
cached.args = item.args;
}
if (
item.result !== undefined &&
(item.result !== cached.result ||
item.isError !== cached.isError ||
item.isPartial !== cached.isPartial)
) {
cached.component.updateResult(
normalizeToolResult(item),
item.isPartial ?? false,
);
cached.result = item.result;
cached.isError = item.isError;
cached.isPartial = item.isPartial;
}
const expanded = this.toolsExpanded();
if (expanded !== cached.expanded) {
cached.component.setExpanded(expanded);
cached.expanded = expanded;
}
return cached.component;
}
private transcriptBodyLines(
agentId: string | undefined,
width: number,
): string[] {
if (!agentId) return [];
const items = this.state.transcriptItems[agentId];
if (!items || items.length === 0) {
return transcriptLines(this.state.transcripts[agentId] ?? "", width);
}
const lines: string[] = [];
let text = "";
const flushText = () => {
if (!text) return;
lines.push(...transcriptLines(text, width));
text = "";
};
for (const [index, item] of items.entries()) {
if (item.kind === "text") {
text += item.text;
continue;
}
flushText();
if (item.kind === "user") {
lines.push(this.theme.fg("accent", "user"));
lines.push(...transcriptLines(item.text, width));
continue;
}
if (this.hideToolPreviews && !this.toolsExpanded()) continue;
const tool = this.transcriptTool(agentId, item, index);
if (tool) lines.push(...tool.render(width));
else if (item.fallback)
lines.push(...transcriptLines(`> ${item.fallback}`, width));
}
flushText();
return lines;
}
// ---- render -----------------------------------------------------------
render(width: number): string[] {
const w = Math.max(1, width);
const state = this.state;
const lines: string[] = [];
lines.push(this.theme.fg("accent", fitLine(headerLine(state), w)));
lines.push(this.theme.fg("muted", fitLine(summaryLine(state), w)));
const description = descriptionLine(state);
if (description)
lines.push(this.theme.fg("muted", fitLine(description, w)));
const metrics = metricsLine(state);
if (metrics) lines.push(this.theme.fg("muted", fitLine(metrics, w)));
const maxLines = Math.max(1, this.host.terminal.rows - 3);
const fixedAfterRows = 4; // transcript divider + body allowance + two footer rows
const display = this.visibleRows();
const maxWorkRows = Math.min(
display.length,
Math.max(0, maxLines - lines.length - fixedAfterRows),
);
const focusedDisplay = display.findIndex(
(item) => this.displayKey(item) === this.focusKey,
);
const rowStart =
maxWorkRows >= display.length
? 0
: Math.min(
Math.max(0, focusedDisplay - Math.floor(maxWorkRows / 2)),
Math.max(0, display.length - maxWorkRows),
);
for (const item of display.slice(rowStart, rowStart + maxWorkRows)) {
const focused = this.displayKey(item) === this.focusKey;
if (item.kind === "phase") {
const marker = focused ? "▶" : " ";
const color = focused
? "accent"
: item.phase.status === "done"
? "success"
: "dim";
lines.push(
this.theme.fg(
color,
fitLine(
`${marker} ${phaseLabel(state, item.phase, item.expanded)}`,
w,
),
),
);
continue;
}
const { row } = item;
const marker = focused ? "▶" : " ";
const icon = STATUS_ICON[row.status];
let line = `${marker} ${icon} ${row.agentId} · ${row.status}`;
if (row.summary) line += ` · ${row.summary}`;
if (row.cached) line += " · checkpoint reused";
if (row.status === "queued")
line += ` · ${queuedDetail(state, row.phase)}`;
if (row.action) line += ` · ${row.action}`;
lines.push(this.theme.fg(focused ? "accent" : "dim", fitLine(line, w)));
}
const focusedRow = this.focusedRow();
const focusedPhase = this.focusedPhase();
lines.push(
this.theme.fg(
"border",
divider(
focusedRow
? `transcript: ${focusedRow.agentId}`
: `phase: ${focusedPhase?.phase ?? "—"}`,
w,
),
),
);
const body = this.transcriptBodyLines(focusedRow?.agentId, w - 2);
// Reserve both footer rows and keep a 3-row terminal safety margin.
const maxVisible = Math.max(0, maxLines - lines.length - 2);
const maxScroll = Math.max(0, body.length - maxVisible);
this.lastMaxScroll = maxScroll;
if (this.followMode) this.scrollOffset = maxScroll;
this.scrollOffset = Math.min(Math.max(0, this.scrollOffset), maxScroll);
for (const l of body.slice(
this.scrollOffset,
this.scrollOffset + maxVisible,
)) {
lines.push(this.theme.fg("dim", fitLine(` ${l}`, w)));
}
if (this.steerMode) {
const verb =
focusedRow && focusedRow.status === "running" ? "steer" : "follow-up";
lines.push(
this.theme.fg(
"warning",
fitLine(
`${verb} ${focusedRow?.agentId ?? ""}> ${this.steerBuffer}`,
w,
),
),
);
} else if (
this.abortArmedAt !== undefined &&
this.now() - this.abortArmedAt <= 1500
) {
lines.push(
this.theme.fg(
"warning",
fitLine("Press Esc again to abort all agents", w),
),
);
} else {
const follow = this.followMode ? "follow" : "paused";
lines.push(
this.theme.fg(
"dim",
fitLine(
`↑↓ focus · Tab agents · Enter phase · Alt+↑/↓ transcript (${follow})`,
w,
),
),
this.theme.fg(
"dim",
fitLine(
`s steer · x abort agent · ${keyHint("app.tools.expand", "tools")} · Esc Esc abort all`,
w,
),
),
);
}
return lines.map((l) => fitLine(l, w));
}
invalidate(): void {
for (const { component } of this.transcriptTools.values())
component.invalidate();
}
}