import { homedir } from "node:os"; import { readFileSync } from "node:fs"; import { fingerprint, loadSavingsSnapshot } from "./calibration.ts"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { getAgentDir, keyHint, type ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { Type, type TSchema } from "typebox"; import { Container } from "@earendil-works/pi-tui"; import { assertNotNested, resolveChildCwd, truncateOutput } from "../child-runtime/policy.ts"; import { copyFinalizationProgress } from "../child-runtime/guard-protocol.ts"; import { loadDelegateConfig, resolveAgent, resolveCheckpointAgent, type Kind } from "./config.ts"; import { createCheckpointHooks } from "./checkpoints.ts"; import { delegateTargetLine, durationContent, knownKind, } from "./display.ts"; import { JobScheduler, parseDelegateCall, type JobSnapshot } from "./jobs.ts"; import { NOTIFY_CUSTOM_TYPE, NotifyGate, shouldConsume, type NotifyDetails } from "./notify.ts"; import { runChild } from "./spawn.ts"; import { agentPrompt, enqueueDelegate } from "./service.ts"; import { Accounting } from "./accounting.ts"; import { registerDelegateCommand } from "./command.ts"; import { showStats } from "./stats-view.ts"; import { showJobs } from "./jobs-view.ts"; import { archiveRoot } from "./archive.ts"; import { isLocalModel } from "./tg.ts"; import { renderChildCall, renderChildResult, renderJobBoard, renderNotifyMessage, type RowState } from "./view.ts"; import { CARD_STATE_TYPE, JobCards, isTerminal, type CardDetails } from "./cards.ts"; import { JobBoard, plainBoardTheme, type BoardUi } from "./board.ts"; import { projectJobBoard } from "./panel.ts"; import { ModelCommand } from "./model-command.ts"; import { capabilityContent, copyCapabilities, describeCapabilities, type CapabilityManifest } from "./capabilities.ts"; import { copyOutcome, outcomeContent } from "./outcomes.ts"; import { respondToBusyQuery } from "./busy-guard.ts"; import { completionHookContent } from "./completion-hook.ts"; import { FileCapacityBroker } from "./capacity.ts"; const EXTENSION_DIR = dirname(fileURLToPath(import.meta.url)); // Validation happens before execute: admit empty placeholders at the schema boundary too. const optionalArgument = (schema: T) => Type.Optional(Type.Union([ schema, Type.Null(), Type.String({ pattern: "^\\s*$" }), ], { description: schema.description })); function agentDir(): string { return typeof getAgentDir === "function" ? getAgentDir() : join(homedir(), ".pi", "agent"); } function errorResult(message: string, requested?: CapabilityManifest) { const capabilities = copyCapabilities(requested); return { content: [{ type: "text" as const, text: message }, ...capabilityContent(capabilities)], details: { ok: false, ...(capabilities ? { capabilities } : {}) }, isError: true, }; } type ViewContext = { toolCallId: string; state: Record; args?: Record; isPartial: boolean; expanded: boolean; isError?: boolean; invalidate?: () => void; }; function receiptText(snap: JobSnapshot): string { if (snap.status === "queued") { const why = snap.reason ? ` ${snap.reason}` : ""; const wait = snap.reason === "resource" ? `Waiting for shared resource${snap.resource ? ` ${snap.resource.key}` : ""}.` : snap.reason === "gpu" ? "Waiting for gpu." : snap.reason === "slot" ? "Waiting for slot." : "Waiting."; return `bg ${snap.id} queued${why}\nquietForMs: ${snap.quietForMs ?? 0}\n${wait} jobId waits, wrap:true wraps, cancel:true kills. timeoutMs 0 peeks.`; } if (snap.status === "running") { const lines = [`${snap.cancellationRequested ? "job" : "bg"} ${snap.id} running`, `quietForMs: ${snap.quietForMs ?? 0}`]; if (snap.cancellationRequested) lines.push("Cancellation requested; waiting for child cleanup. Slot still held."); if (snap.resource) lines.push(`resource: ${snap.resource.key} · shared capacity ${snap.resource.capacity} · ${snap.resource.state}`); if (snap.resourceError) lines.push(snap.resourceError); if (snap.finalization?.phase === "requested") lines.push("finalization requested; enforcement not yet acknowledged"); else if (snap.finalization?.phase === "draining") lines.push(`finalization enforced; ${snap.finalization.activeTools ?? "unknown"} current tools draining`); else if (snap.finalization?.phase === "answering") lines.push("finalization enforced; waiting for the final answer"); else if (snap.wrapped) lines.push("wrap queued (current tool may finish first)"); const headroom = snap.finalization?.headroom; if (headroom) { lines.push(`context policy: ${headroom.phase}${headroom.inputBytes === undefined ? "" : `; ${headroom.inputBytes}/${headroom.inputLimitBytes} payload bytes`}`); if (headroom.clippedToolResults) lines.push(`tool results shortened in request: ${headroom.clippedToolResults}; native evidence retained`); } for (const item of snap.activity.slice(-3)) { lines.push(`${item.mark} ${item.name}${item.args ? ` ${item.args}` : ""}`); } if (snap.current) { lines.push(`current: ${snap.current.name}${snap.current.args ? ` ${snap.current.args}` : ""}`); } lines.push("Slot still held. jobId waits, wrap:true wraps, cancel:true kills. timeoutMs 0 peeks."); return lines.join("\n"); } const answer = snap.answer || snap.stderrTail || snap.stopReason || "(no output)"; return snap.resourceError ? `${snap.resourceError}\n\n${answer}` : answer; } function formatOutput(input: { text: string; failed: boolean; stopReason?: string; exitCode: number; maxBytes: number; kind: Kind; model?: string; details: Record; }) { const body = [ delegateTargetLine(input.kind, input.model), input.details.responseFile ? input.text : truncateOutput(input.text || "(no output)", input.maxBytes), ].join("\n\n"); return { content: [ { type: "text" as const, text: input.failed ? `delegate failed (${input.stopReason || input.exitCode}): ${body}` : body, }, ...durationContent(input.details.durationMs), ...capabilityContent(input.details.capabilities), ...outcomeContent(input.details.outcome), ...completionHookContent(input.details.completionHook), ], details: { ok: !input.failed, ...input.details }, isError: input.failed, }; } function detailsFromSnap(snap: JobSnapshot, extra: Record = {}): Record { const details: Record = { kind: snap.kind, model: snap.model, ...(snap.reasoning === undefined ? {} : { reasoning: snap.reasoning }), jobId: snap.id, status: snap.status, activity: [...snap.activity], task: snap.task, ...extra, }; if (snap.archive) { details.runId = snap.archive.runId; details.sessionFile = snap.archive.sessionFile; } if (snap.capabilities) details.capabilities = copyCapabilities(snap.capabilities); if (snap.outcome) details.outcome = copyOutcome(snap.outcome); if (snap.recordingError) details.recordingError = snap.recordingError; if (snap.responseFile) details.responseFile = snap.responseFile; if (snap.completionHook) details.completionHook = { ...snap.completionHook }; if (snap.current) details.current = snap.current; if (snap.thinking) details.phase = "thinking"; if (snap.tg) details.tg = snap.tg; if (snap.reason) details.reason = snap.reason; if (snap.exitCode !== undefined) details.exitCode = snap.exitCode; if (snap.stopReason) details.stopReason = snap.stopReason; if (snap.stderrTail) details.stderrTail = snap.stderrTail; if (snap.answer) details.answer = snap.answer; details.terminal = snap.status === "done" || snap.status === "failed"; if (snap.quietForMs !== undefined) details.quietForMs = snap.quietForMs; if (snap.cancellationRequested) details.cancellationRequested = true; if (snap.wrapped) details.wrapped = true; if (snap.finalization) details.finalization = copyFinalizationProgress(snap.finalization); if (snap.resource) details.resource = { ...snap.resource }; if (snap.resourceError) details.resourceError = snap.resourceError; return details; } export default function delegate(pi: ExtensionAPI, childRunner: typeof runChild = runChild) { if (process.env.PI_DELEGATE_CHILD === "1") return; const configPaths = { shippedPath: join(EXTENSION_DIR, "config.json"), userPath: process.env.PI_DELEGATE_SKIP_USER_CONFIG === "1" ? undefined : join(agentDir(), "delegate.json"), }; const config = loadDelegateConfig(configPaths); const modelCommand = new ModelCommand(config, configPaths, () => registerDelegateTool()); const accounting = new Accounting(archiveRoot(agentDir())); const cards = new JobCards(); const origins = new Map(); const lastChecks = new Map(); const uiDetails = (snap: JobSnapshot, extra: CardDetails = {}): CardDetails => detailsFromSnap(snap, { originToolCallId: snap.archive ? origins.get(snap.archive.runId) : undefined, background: snap.background, callType: "spawn", ...extra, ...((snap.status === "done" || snap.status === "failed") ? { durationMs: accounting.durationMs(snap.archive?.runId) } : {}), }); const updateCard = (snap: JobSnapshot): void => { const origin = snap.archive ? origins.get(snap.archive.runId) : undefined; if (!origin) return; const previous = cards.get(origin); const wasTerminal = isTerminal(previous ?? {}); const previousHook = (previous?.completionHook as { status?: string } | undefined)?.status; const terminal = snap.status === "done" || snap.status === "failed"; const details = uiDetails(snap, { ok: !snap.failed, displayWarning: cards.get(origin)?.displayWarning, answer: terminal ? receiptText(snap) : undefined }); cards.update(origin, details); if (isTerminal(details) && (!wasTerminal || previousHook !== snap.completionHook?.status)) { try { pi.appendEntry(CARD_STATE_TYPE, details); } catch { cards.update(origin, { ...details, displayWarning: "Could not save delegate display state; the child archive is separate." }); } } }; const board = new JobBoard(renderJobBoard, () => new Container(), (state) => renderJobBoard(state, 100, 10, plainBoardTheme, false, "")); let ui: BoardUi | undefined; let mode: string | undefined; let hasUI = false; let shuttingDown = false; let sessionCtx: { isIdle?: () => boolean } | undefined; let boardTimer: ReturnType | undefined; const paintBoard = (): void => { if (!ui?.setWidget) return; const state = shuttingDown ? undefined : projectJobBoard(scheduler.active(), { maxLocalConcurrent: config.maxLocalConcurrent }, mode === "tui" ? Date.now() : undefined); board.paint(ui, mode, state); if (state && mode === "tui" && !shuttingDown) { boardTimer ??= setInterval(paintBoard, 1000); boardTimer.unref(); } else { clearInterval(boardTimer); boardTimer = undefined; } }; const gate = new NotifyGate({ isLive: () => !shuttingDown && hasUI && typeof pi.sendMessage === "function", isBusy: () => { try { return sessionCtx?.isIdle?.() === false; } catch { return false; } }, send: (payload) => { pi.sendMessage(payload, { deliverAs: "followUp", triggerTurn: true }); }, }); const scheduler = new JobScheduler({ // All local providers intentionally share one user-scoped slot, independent of archive paths. // Lazy acquisition leaves hosted work usable when Linux/flock is unavailable. capacity: { tryAcquire: group => new FileCapacityBroker(join(agentDir(), "delegate-capacity")).tryAcquire(group) }, maxConcurrent: config.maxConcurrent, maxLocalConcurrent: config.maxLocalConcurrent, maxQueued: config.maxQueued, onChange: (snap) => { if (snap) updateCard(snap); paintBoard(); }, onTerminal: (snap) => gate.schedule(snap), onSettled: (snap) => accounting.terminal(snap.archive?.runId, snap.id, { status: snap.failed ? "failed" : "done", stopReason: snap.stopReason, exitCode: snap.exitCode, finalization: snap.finalization, evidence: snap.outcome?.evidence, }), }); // A tool wait can end long before its child does. Observe each parent run instead // of tying background cancellation to the spawn/collect call that happened to be active. let parentSignal: AbortSignal | undefined; const onParentAbort = (): void => { // Include completed jobs whose notices are still waiting for the parent to go idle. for (const snap of scheduler.list()) gate.consume(snap.id); scheduler.cancelAll(); }; const detachParentAbort = (): void => { parentSignal?.removeEventListener("abort", onParentAbort); parentSignal = undefined; }; pi.on("agent_start", (_event, ctx) => { detachParentAbort(); parentSignal = ctx.signal; if (parentSignal?.aborted) onParentAbort(); else parentSignal?.addEventListener("abort", onParentAbort, { once: true }); }); pi.on("agent_settled", () => { detachParentAbort(); checkpoints.settled(); }); const bindUi = (ctx: { ui?: BoardUi; mode?: string; hasUI?: boolean; isIdle?: () => boolean }): void => { if (ctx.ui && typeof ctx.ui.setWidget === "function") ui = ctx.ui; if (ctx.mode) mode = ctx.mode; if (typeof ctx.hasUI === "boolean") hasUI = ctx.hasUI; if (typeof ctx.isIdle === "function") sessionCtx = ctx; }; if (typeof pi.registerMessageRenderer === "function") { pi.registerMessageRenderer(NOTIFY_CUSTOM_TYPE, (message, { expanded }, theme) => { const details = message.details; if (!details) return undefined; return renderNotifyMessage({ theme, details, expanded }); }); } const readRow = (context: ViewContext): RowState => { const args = context.args ?? (context.state.delegateArgs as Record | undefined) ?? {}; const saved = context.state.delegateResult as { details?: CardDetails; content?: RowState["content"] } | undefined; const jobId = typeof args.jobId === "string" ? args.jobId.trim() || undefined : undefined; const collect = saved?.details?.callType === "collect" || (saved?.details?.callType !== "spawn" && jobId !== undefined); const current = !collect ? cards.get(context.toolCallId) : undefined; // Accepted failed jobs still receive hook updates; unrelated tool errors stay immutable. const failedOrigin = current?.status === "failed" && saved?.details?.status === "failed" && saved.details.callType === "spawn" && saved.details.jobId === current.jobId; const snapshot = failedOrigin || (!context.isError && saved?.details?.ok !== false) ? current : undefined; const details: CardDetails = { ...saved?.details, ...snapshot }; const kind = knownKind(details.kind) ?? knownKind(args.kind); details.kind ??= kind; details.model ??= args.model ?? (kind && Object.hasOwn(config.agents, kind) ? config.agents[kind].model : undefined); details.task ??= args.task; details.jobId ??= jobId; details.operation ??= args.cancel === true ? "cancel" : args.wrap === true ? "wrap" : args.timeoutMs === 0 ? "peek" : "wait"; if (!snapshot && !collect && !context.isPartial && (details.status === "queued" || details.status === "running")) details.historical = true; return { details, content: snapshot ? undefined : saved?.content, collect, expanded: context.expanded, live: Boolean(snapshot && cards.isLive(context.toolCallId)), isPartial: snapshot ? !isTerminal(snapshot) : context.isPartial, isError: context.isError }; }; let busyUnsubscribe: (() => void) | undefined; pi.on("session_start", async (_event, ctx) => { shuttingDown = false; busyUnsubscribe?.(); const events = pi.events; if (events?.on) { busyUnsubscribe = events.on("delegate:query-busy", (payload) => { respondToBusyQuery(payload, shuttingDown || scheduler.active().length > 0 || scheduler.hasPendingHooks()); }); } bindUi(ctx); cards.restore(ctx.sessionManager.getBranch?.() ?? []); await accounting.activate(ctx.sessionManager.getSessionId(), ctx.hasUI ? ctx.ui : undefined); checkpoints.restore(_event, ctx); }); pi.on("session_tree", (event, ctx) => { cards.restore(ctx.sessionManager.getBranch()); checkpoints.restore(event, ctx); }); const checkpoints = createCheckpointHooks(pi, config, async (task, origin, ctx) => { bindUi(ctx); const { kind, agent, model } = resolveCheckpointAgent(config); ctx.signal?.throwIfAborted(); const accepted = enqueueDelegate({ scheduler, accounting, agent, task, config, childRunner, background: false, timeoutMs: config.defaultTimeoutMs, signal: ctx.signal, identity: { parentSessionId: ctx.sessionManager.getSessionId(), parentSessionFile: ctx.sessionManager.getSessionFile(), toolCallId: origin, kind, cwd: ctx.cwd, requestedModel: model, thinking: agent.thinking, }, }); // Checkpoints deliver their own result at this boundary, never a follow-up notice. gate.consume(accepted.id); const snap = await scheduler.wait(accepted.id, { timeoutMs: config.defaultTimeoutMs, signal: ctx.signal }); if (snap.status === "queued" || snap.status === "running") { scheduler.cancel(snap.id); return { failed: true, text: "Advisor wait ended before completion; cancellation requested.", jobId: snap.id, model }; } const text = snap.responseFile ? readFileSync(snap.responseFile, "utf8") : receiptText(snap); return { failed: snap.failed || !text.trim(), text, jobId: snap.id, model, durationMs: accounting.durationMs(snap.archive?.runId) }; }); pi.on("turn_end", checkpoints.turnEnd); const dialogs = new AbortController(); registerDelegateCommand(pi, [{ name: "roles", description: "Edit roles, models, reasoning, tools, extensions and prompts", handler: (args, ctx) => modelCommand.command(args, ctx, true), }, { name: "checkpoints", description: "Automatic advisory checks during main-agent work (opt-in)", handler: (args, ctx) => modelCommand.checkpoints(args, ctx), }, { name: "models", description: "Choose role models and reasoning levels", handler: (args, ctx) => modelCommand.command(args, ctx), }, { name: "stats", description: "Recorded child usage (no model calls)", complete: (prefix) => ["session", "today", "all", "rebuild"].filter(value => value.startsWith(prefix)), handler: async (args, ctx) => { const scope = args.trim() || "session"; if (scope !== "session" && scope !== "today" && scope !== "all" && scope !== "rebuild") { ctx.ui.notify("Usage: /pi-delegate stats [session|today|all|rebuild]", "warning"); return; } const report = await accounting.report(scope === "rebuild" ? "all" : scope, ctx.sessionManager.getSessionId(), scope === "rebuild"); await showStats(ctx, report, { scope: scope === "rebuild" ? "all" : scope, signal: dialogs.signal, load: next => accounting.report(next, ctx.sessionManager.getSessionId()), }); }, }, { name: "jobs", description: "Browse active and queued delegates", handler: async (args, ctx) => { if (args.trim()) { ctx.ui.notify("Usage: /pi-delegate jobs", "warning"); return; } await showJobs(ctx, () => projectJobBoard(scheduler.active(), { maxLocalConcurrent: config.maxLocalConcurrent }), dialogs.signal); }, }], dialogs.signal); // Pi ignores isError on execute() return values. Keep our structured details // and mark failed results through the supported result-event hook instead. pi.on("tool_result", (event) => { if (event.toolName === "delegate" && (event.details as { ok?: boolean } | undefined)?.ok === false) { return { isError: true }; } }); pi.on("session_shutdown", async () => { shuttingDown = true; checkpoints.stop(); clearInterval(boardTimer); boardTimer = undefined; dialogs.abort(); busyUnsubscribe?.(); busyUnsubscribe = undefined; detachParentAbort(); modelCommand.stop(); gate.shutdown(); await scheduler.shutdown(); accounting.close(); board.close(ui); ui = undefined; mode = undefined; hasUI = false; sessionCtx = undefined; cards.clear(); origins.clear(); lastChecks.clear(); }); const registerDelegateTool = () => pi.registerTool({ name: "delegate", label: "Delegate", renderShell: "self", description: `Child agent. Roles: ${Object.keys(config.agents).join(", ")}. Model from config. Background by default; returns jobId. background:false waits. jobId waits/peeks/wraps/cancels. timeoutMs is wait budget, never kills. Interactive mode may inject a completion notice. Local models share maxLocalConcurrent. No nesting.`, promptSnippet: "Route isolated work to a named delegate agent. Model comes only from role configuration.", promptGuidelines: [ "Before each substantial task, assess delegate; use it for isolated child work only when expected speed or quality gain exceeds coordination and validation cost.", "Use delegate as the only parent-child tool. One child per call. No nesting.", "With delegate, the parent owns decomposition, architecture and security judgment, ambiguity resolution, main implementation, destructive or external actions, integration, final validation and the final answer.", "Use delegate kind recon for read-only repo mapping, file lookup, failure reproduction notes and narrow review evidence; never edits or broad judgment. Do not send recon images or long inherited context.", "Use delegate kind implement only for bounded edits and tests. Give parallel children disjoint files and scopes; avoid parent/child write races.", "Use delegate kind review only if implementation failed or independent judgment is required; review and oracle remain read-only.", "For delegate reviews, supply requirements, acceptance criteria, exact diff/artifact paths and scope, relevant repository constraints, and permitted validation commands. Keep independent first-pass handoffs neutral: omit requests for approval, implementer self-assessments, and other reviewers' verdicts. For fix verification, supply the prior finding and reproduction, and require independent reassessment rather than agreement.", "Use delegate kind oracle only as a last resort, without parallel delegates.", "Roles can be disabled in configuration; do not retry a disabled role with another model. No automatic diff or extra task context is prepared; supply readable artifacts or file paths and scope in the task. Review remains read-only by prompt policy.", "Delegate models come only from role configuration; per-call model overrides are not supported. To use another model, ask the user to configure the role with /pi-delegate roles. Never silently substitute a model.", "Give every delegate a self-contained task with the goal, exact cwd/targets, relevant context, evidence or checks required, acceptance criteria and stop rules. Distinguish allowed edits from validation commands, including permission for new directories, reversible setup and isolated Pi integration tests without delegated work or model requests. Children must not commit, push, merge, publish, release or expand scope.", "Inspect every delegate result and diff, rerun relevant checks and validate the integrated result. Child reports and completion receipts are evidence, not proof of correctness.", "For review loops, use kind review with its configured model; address findings and review again until none important remain. If a requested model differs from the role configuration, report the blocker rather than overriding or substituting a model or claiming a clean review.", "delegate spawns default to background:true and return jobId immediately; the child keeps running. Set background:false to wait on spawn.", "delegate timeoutMs is a wait budget, never a kill. Foreground expiry auto-backgrounds and returns a short check-in.", "With delegate jobId, omit timeoutMs to wait until done or 60s quiet; timeoutMs:0 peeks.", "On a nonterminal delegate check-in, inspect current/recent tools and quietForMs. Wait while useful progress continues; silence or wait expiry alone does not prove a stall.", "For a suspected delegate stall, request wrap:true once, then wait again in a separate call; allow the current turn/tools and final report time to finish.", "Before delegate cancel:true for a suspected stall, fetch and inspect a fresh jobId/timeoutMs:0 snapshot in a separate call. Cancel only if post-wrap checks still show a stall; explicit user stop or unsafe/out-of-scope work may cancel immediately.", "delegate wrap:true is advisory steering, not an interrupt or delivery acknowledgement. cancel:true aborts and kills.", "Collect full delegate results with jobId even after an interactive completion notice; print/JSON stays pull-only.", "Use recorded delegate durationMs and Duration as authoritative elapsed timing; recover missing timing from archived metadata.json. Use Unknown only when timing cannot be recovered.", "Local delegate jobs may queue under maxLocalConcurrent; hosted jobs can run independently. Running children retain their slots until they stop.", ], parameters: Type.Object({ task: optionalArgument(Type.String({ description: "Task for the child. Required to spawn. Max 20000 chars." })), kind: optionalArgument(Type.String({ description: `Configured role: ${Object.keys(config.agents).join(", ")}. Required to spawn.` })), cwd: optionalArgument( Type.String({ description: "Child working directory. Relative paths resolve against parent cwd." }), ), timeoutMs: optionalArgument( Type.Integer({ description: "Wait budget, never a kill. Spawn/fg: first wait. jobId: max wait (omit = until done or quiet). 0 with jobId = peek.", }), ), background: optionalArgument( Type.Boolean({ description: "Spawn default: true, return jobId now. Set false to wait. Omit with jobId." }), ), jobId: optionalArgument(Type.String({ description: "Wait, peek, wrap, or cancel an existing job." })), wrap: optionalArgument( Type.Boolean({ description: "With jobId: steer child to wrap up. Does not interrupt the current tool." }), ), cancel: optionalArgument(Type.Boolean({ description: "With jobId: abort and kill the child." })), }), async execute(toolCallId, params, signal, onUpdate, ctx) { let capabilities: CapabilityManifest | undefined; const update = (result: Parameters>[0]): void => { try { onUpdate?.(result); } catch { /* Progress observers must never change job acceptance or outcomes. */ } }; try { assertNotNested(process.env, "delegate"); bindUi(ctx); const parsed = parseDelegateCall(params, config); const operation = parsed.mode === "collect" ? parsed.cancel ? "cancel" : parsed.wrap ? "wrap" : parsed.peek ? "peek" : "wait" : "wait"; const publish = (snap: JobSnapshot, background: boolean, pending: boolean): void => { updateCard(snap); update({ content: [{ type: "text" as const, text: delegateTargetLine(snap.kind, snap.model) }, ...capabilityContent(snap.capabilities)], details: { ...uiDetails(snap, { pending, background, callType: parsed.mode, operation }), }, }); }; let snap: JobSnapshot; if (parsed.mode === "collect") { const current = scheduler.get(parsed.jobId); capabilities = copyCapabilities(current.capabilities); publish(current, true, current.status === "queued" || current.status === "running"); if (parsed.cancel) scheduler.cancel(parsed.jobId); else if (parsed.wrap) scheduler.wrap(parsed.jobId); const peek = parsed.peek === true; snap = await scheduler.wait(parsed.jobId, { timeoutMs: peek ? 0 : parsed.waitMs, quietMs: peek || parsed.cancel ? undefined : config.checkIntervalMs, signal: peek ? undefined : signal, onSnapshot: (next) => publish(next, true, next.status === "queued" || next.status === "running"), }); } else { const kind = parsed.kind; const cwd = resolveChildCwd(parsed.cwd, ctx.cwd, "delegate"); const resolved = resolveAgent(kind, config); const tools = [...resolved.agent.tools]; capabilities = describeCapabilities(tools); const local = isLocalModel(resolved.model); update({ content: [{ type: "text" as const, text: delegateTargetLine(kind, resolved.model) }, ...capabilityContent(capabilities)], details: { kind, model: resolved.model, reasoning: resolved.agent.thinking, task: parsed.task, pending: true, background: parsed.background, capabilities: copyCapabilities(capabilities) }, }); let savingsInfo: ReturnType = {}; if (local) { const alternative = config.localAlternatives[resolved.model]; try { const slash = alternative?.model.indexOf("/") ?? -1; const pricedModel = alternative && ctx.modelRegistry.find(alternative.model.slice(0, slash), alternative.model.slice(slash + 1)); savingsInfo = alternative && !isLocalModel(alternative.model) ? loadSavingsSnapshot({ key: { localModel: resolved.model, alternativeModel: alternative.model, kind, localThinking: resolved.agent.thinking, alternativeThinking: alternative.thinking, tools, promptHash: fingerprint(agentPrompt(resolved.agent)) }, files: config.calibrationProfiles, pricing: pricedModel?.cost, }) : { reason: "No hosted alternative configured for this local model" }; } catch { savingsInfo = { reason: "Alternative pricing/calibration unavailable" }; } } snap = enqueueDelegate({ scheduler, accounting, agent: resolved.agent, task: parsed.task, identity: { parentSessionId: ctx.sessionManager.getSessionId(), parentSessionFile: ctx.sessionManager.getSessionFile(), toolCallId, kind, cwd, requestedModel: resolved.model, thinking: resolved.agent.thinking, savings: savingsInfo.snapshot, savingsUnavailable: savingsInfo.reason, }, timeoutMs: parsed.timeoutMs, background: parsed.background, signal, config, childRunner, onAccepted: archive => { origins.set(archive.data.runId, toolCallId); cards.begin(toolCallId, { kind, model: resolved.model, reasoning: resolved.agent.thinking, task: parsed.task, status: "queued", capabilities: copyCapabilities(capabilities) }); }, }); publish(snap, parsed.background, snap.status === "queued" || snap.status === "running"); if (!parsed.background) { snap = await scheduler.wait(snap.id, { timeoutMs: parsed.timeoutMs, signal, onSnapshot: (next) => publish(next, false, next.status === "queued" || next.status === "running"), }); if (!signal?.aborted && (snap.status === "queued" || snap.status === "running")) { snap = scheduler.promoteBackground(snap.id); } } } const collect = parsed.mode === "collect"; const pending = snap.status === "queued" || snap.status === "running"; // Freeze timing when a check returns, never when its history row repaints. const checkTiming: CardDetails = {}; if (collect && pending && !parsed.wrap && !parsed.cancel) { const checkedAt = Date.now(); const previous = lastChecks.get(snap.id); checkTiming.checkedAt = checkedAt; checkTiming.elapsedMs = Math.max(0, checkedAt - (snap.startedAt ?? snap.queuedAt ?? checkedAt)); if (previous !== undefined) checkTiming.sincePreviousCheckMs = Math.max(0, checkedAt - previous); lastChecks.set(snap.id, checkedAt); } const failed = !pending && snap.failed; const exitCode = snap.exitCode ?? (failed ? 1 : 0); // Preserve foreground answer capping and the richer empty-answer fallback on collection. const text = collect || pending || snap.responseFile ? receiptText(snap) : truncateOutput(snap.answer || snap.stderrTail || "(no output)", config.maxOutputBytes); updateCard(snap); if (collect && shouldConsume(snap)) gate.consume(snap.id); return formatOutput({ text, failed, stopReason: snap.stopReason, exitCode, maxBytes: config.maxOutputBytes, kind: snap.kind, model: snap.model, details: uiDetails(snap, collect ? { callType: "collect", operation, background: true, pending, ...checkTiming, answer: pending ? snap.answer : text, } : pending ? { background: snap.background, pending: true } : { exitCode, answer: text }), }); } catch (error) { cards.forget(toolCallId); const message = error instanceof Error ? error.message : String(error); return errorResult(message, capabilities); } }, renderCall(args, theme, context) { context.state.delegateArgs = args; cards.watch(context.toolCallId, context.invalidate); return renderChildCall({ theme, read: () => readRow(context) }); }, renderResult(result, { expanded, isPartial }, theme, context) { context.state.delegateResult = result; return renderChildResult({ theme, read: () => readRow({ ...context, expanded, isPartial }), expandHint: keyHint("app.tools.expand", "full result and tool details") }); }, }); registerDelegateTool(); }