import { randomUUID } from "node:crypto"; import { getAgentDir, isToolCallEventType, type ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { Type } from "typebox"; import { pathsFor, loadConfig } from "./config/loader.js"; import { UltraController, type PendingRootTelemetry } from "./controller/controller.js"; import { assertModelAllowed, modelRank, rosterFor } from "./models/roster.js"; import { PiAgentsHost } from "./backends/pi-agents-backend.js"; import { assertAutoModeProfile, profileForAgentDir } from "./security/profiles.js"; import { runUltraConfigCommand, ultraConfigCompletions } from "./ui/commands.js"; import { markInterruptedRunsStopped } from "./context/recovery.js"; import { evaluateRunningExperiments } from "./experiments/runtime.js"; import { extractRootAttribution, ROOT_ATTRIBUTION_INSTRUCTION } from "./context/root-attribution.js"; import { shouldSwitchRoot } from "./models/stickiness.js"; import { guardedCommand } from "./security/permissions.js"; import { analyzeTask } from "./controller/task-shape.js"; import { isHardDirect } from "./controller/routing.js"; import { explicitAcceptanceCommand, inferAcceptanceCommand } from "./verification/infer-command.js"; import { formatUsd } from "./ui/status-widget.js"; import { clearHud, createHudPainter, type HudSurface } from "./ui/hud.js"; import { clearPanel, createPanelPainter, type PanelSurface } from "./ui/panel.js"; import { RUN_ENTRY_TYPE, runEntryLine } from "./ui/run-entry.js"; import { traceLine } from "./telemetry/live-trace.js"; import type { RawTraceRecord } from "./security/privacy.js"; import type { AttributionManifest, DispatchRequest, DispatchResult } from "./types.js"; const ORCHESTRATION_TOOLS = new Set(["workflow", "steer", "swarm", "sendmessage", "crew", "company", "subagent", "delegate_task", "agent_team"]); const PI_AGENTS_APPENDIX = "\n\nThe following reusable agent profiles are available to the `workflow` tool"; export const ROOT_PRESENTER_CONTRACT = `For the final user-facing answer only: give a short plain-language outcome, then the mechanism and necessary technical detail, concrete verification evidence, and honest risks or limits. Do not repeat worker reports or use this format in tools or handoffs. ${ROOT_ATTRIBUTION_INSTRUCTION}`; export function delegatedChild(): boolean { const depth = Number.parseInt(process.env.PI_AGENTS_DEPTH ?? "0", 10); return Number.isFinite(depth) && depth > 0; } export function stripPiAgentsAppendix(systemPrompt: string): string { const marker = systemPrompt.indexOf(PI_AGENTS_APPENDIX); return marker === -1 ? systemPrompt : systemPrompt.slice(0, marker); } function activeWithoutCompetingTools(pi: ExtensionAPI): void { pi.setActiveTools(pi.getActiveTools().filter((name) => !ORCHESTRATION_TOOLS.has(name.toLowerCase()))); } function modelId(model: { provider: string; id: string } | undefined): string | undefined { return model ? `${model.provider}/${model.id}` : undefined; } function messageModel(message: { provider?: unknown; model?: unknown; responseModel?: unknown }): string | undefined { const provider = typeof message.provider === "string" ? message.provider : undefined; const model = typeof message.responseModel === "string" ? message.responseModel : typeof message.model === "string" ? message.model : undefined; return provider && model ? `${provider}/${model}` : undefined; } type RootOutcome = NonNullable>>; export function rootOutcomeSummary(outcome: RootOutcome): string { const completedWork = outcome.changedFiles.length ? outcome.changedFiles.join(", ") : outcome.acceptedFacts.length ? `${outcome.acceptedFacts.length} accepted facts` : "none recorded"; if (outcome.state === "COMPLETED") return `UltraPi controller: ${outcome.state} · verified=${outcome.verified ? "yes" : "no"} · est. ${formatUsd(outcome.spentUsd)}`; if (!outcome.terminal) return `UltraPi controller: work continues (${outcome.state}); the final verified result will follow · est. ${formatUsd(outcome.spentUsd)} · run=${outcome.runId}`; // Refusing to hand back an unverified result is the point of the controller, and it only // reads as strength if the refusal says what happened and what to do about it. return `UltraPi controller: ${outcome.state} · completed=${completedWork} · verified=no · blocker=${outcome.remainingBlockers?.join(",") ?? outcome.state.toLowerCase()} · est. ${formatUsd(outcome.spentUsd)} · run=${outcome.runId}\nWhat is required to continue: ${outcome.requiredToContinue ?? "Run /ultra-config runs to inspect this run, then rerun /ultra."}`; } async function enforceProfile(pi: ExtensionAPI, agentDir: string): Promise { const config = await loadConfig(pathsFor(agentDir)); await assertAutoModeProfile(agentDir, config.mode); const profile = await profileForAgentDir(agentDir); if (profile && profile !== config.profile) throw new Error(`Profile config mismatch: expected ${profile}, found ${config.profile}`); pi.on("before_provider_request", async (_event, ctx) => { const selected = modelId(ctx.model); if (selected) assertModelAllowed(config, selected); }); } export async function installUltraPi(pi: ExtensionAPI, agentDir = process.env.PI_CODING_AGENT_DIR ?? getAgentDir()): Promise { if (delegatedChild()) { await enforceProfile(pi, agentDir); return; } await enforceProfile(pi, agentDir); // The HUD renders wherever the last event came from. Pi hands a ui to every handler, but the // controller's trace sink has none, so the surface is remembered rather than passed around. let hudUi: HudSurface | undefined; // The panel is a terminal component, so it only exists in the TUI; the one-line status keeps // every other mode informed. let panelUi: PanelSurface | undefined; let liveTraceMode: "live" | "raw-live" | undefined; let liveTraceRunId: string | undefined; let rawLiveNext = false; let liveTraceNotify: ((line: string) => void) | undefined; const controller = new UltraController(agentDir, pi, (entry) => { paintHud(hudUi); paintPanel(panelUi); if (liveTraceMode === "raw-live" && rawLiveNext && entry.eventType === "request.received") { liveTraceRunId = entry.runId; rawLiveNext = false; } if (liveTraceMode !== "live") return; if (entry.runId !== liveTraceRunId) return; const line = traceLine(entry); if (line) liveTraceNotify?.(line); }, (record: RawTraceRecord) => { if (liveTraceMode !== "raw-live" || record.runId !== liveTraceRunId || (record.kind !== "task" && record.kind !== "result")) return; liveTraceNotify?.(`[${record.timestamp}] ${record.kind} ${record.fromNodeId} → ${record.toNodeId}\n${record.content}`); }); const paintHud = createHudPainter(controller); const paintPanel = createPanelPainter(controller); await controller.ready(); type PendingRootTurn = { kind: "dispatch" } | { kind: "direct" | "recovery" | "presentation"; runId: string }; let rootRunId: string | undefined; const pendingRootTurns: PendingRootTurn[] = []; let rootGuardActive = false; let presentationTurnActive = false; let directTurnActive = false; let rootContinuationPending = false; let rootBudgetStopActive = false; let toolsBeforeDirectRun: string[] | undefined; const hideDispatchForDirectRun = () => { const active = pi.getActiveTools().filter((name) => !ORCHESTRATION_TOOLS.has(name.toLowerCase())); if (!active.includes("ultra_dispatch")) return; toolsBeforeDirectRun = active; pi.setActiveTools(active.filter((name) => name !== "ultra_dispatch")); }; const restoreToolsAfterDirectRuns = () => { if (!toolsBeforeDirectRun || directTurnActive || pendingRootTurns.some((turn) => turn.kind === "direct")) return; pi.setActiveTools(toolsBeforeDirectRun); toolsBeforeDirectRun = undefined; }; const removePendingRootTurn = (turn: PendingRootTurn) => { const index = pendingRootTurns.indexOf(turn); if (index >= 0) pendingRootTurns.splice(index, 1); }; const queueDelegatedPresentation = (piAgentsRunId: string) => { for (let attempt = 0; attempt < 2; attempt += 1) { const presentation = controller.takeDelegatedPresentation(piAgentsRunId); if (!presentation) return; const pendingTurn = { kind: "presentation", runId: presentation.runId } as const; pendingRootTurns.push(pendingTurn); try { pi.sendMessage( { customType: "ultrapi-completion", content: `UltraPi delegated work is terminal. Do not call ultra_dispatch or any tools. Present completed work, terminal state, verification status, remaining blockers, pre-presentation estimated USD from Pi usage, and what is required to continue. Do not emit ULTRAPI_ATTRIBUTION; this run is already terminal.\n\n${JSON.stringify(presentation)}`, display: false }, { triggerTurn: true, deliverAs: "followUp" }, ); return; } catch (error) { removePendingRootTurn(pendingTurn); controller.releaseDelegatedPresentation(piAgentsRunId); if (attempt === 1) throw error; } } }; const host = new PiAgentsHost(pi, async (event) => { try { await controller.onPiAgentsEvent(event); if (event.type === "run_completed") { queueDelegatedPresentation(event.runId); await evaluateRunningExperiments(controller.paths); } } catch {} }); host.install(); let runtimeSessionId: string | undefined; let turnIndex = 0; let callIndex = 0; let providerStartedAt = 0; let rootSample: { model: string; thinkingLevel: string; turnIndex: number; callIndex: number; startedAt: number } | undefined; type PendingRootSample = PendingRootTelemetry["samples"][number]; const pendingRootSamples: PendingRootSample[] = []; const pendingProviders: PendingRootTelemetry["providers"] = []; let pendingRootFailure: PendingRootSample | undefined; let providerFailureRecorded = false; const toolTimers = new Map(); const pendingRootWrites = new Map(); const observedRootChanges = new Map>(); const emitRootSample = async (runId: string, pending: PendingRootSample) => { const metadata = { thinkingLevel: pending.sample.thinkingLevel, latencyMs: Math.max(0, Date.now() - pending.sample.startedAt), turnIndex: pending.sample.turnIndex, callIndex: pending.sample.callIndex, observedModel: pending.observedModel, observedProvider: pending.observedProvider }; if (!pending.started) await controller.recordRootModelStarted(runId, pending.sample.model, { ...metadata, latencyMs: 0 }); if (pending.errorClass) await controller.recordRootModelFailed(runId, pending.sample.model, { ...metadata, errorClass: pending.errorClass }, pending.usage); else await controller.recordRootUsage(runId, pending.sample.model, pending.usage, metadata); }; const flushPendingRootSamples = async (runId: string) => { while (pendingRootSamples.length) { const pending = pendingRootSamples.shift()!; await emitRootSample(runId, pending); if (pendingRootFailure === pending) pendingRootFailure = undefined; } }; const resolveContextModel = (ctx: Parameters[0], target: string) => { assertModelAllowed(controller.getConfig(), target); const separator = target.indexOf("/"); if (separator <= 0 || separator >= target.length - 1) return undefined; const scoped = ctx.scopedModels ?? []; return scoped.length > 0 ? scoped.find((entry) => modelId(entry.model) === target)?.model : ctx.modelRegistry.find(target.slice(0, separator), target.slice(separator + 1)); }; const activateDispatch = async (result: DispatchResult, ctx: Parameters[0], continuationPending: boolean) => { if (continuationPending) rootRunId = result.runId; try { if (result.piAgentsRunId) queueDelegatedPresentation(result.piAgentsRunId); const selection = result.rootSelection; const current = modelId(ctx.model); const contextPercent = ctx.getContextUsage()?.percent; const cold = callIndex <= 1 && typeof contextPercent === "number" && contextPercent < 15; if (selection) { // Rank is the model's position in the roster the user declared, so escalation reads the // same ordering for a Codex tier list and for someone else's Anthropic or Ollama one. const roster = rosterFor(controller.getConfig()); const currentRank = modelRank(roster, current); const targetRank = modelRank(roster, selection.model); const requiredUpgrade = selection.model === selection.stableModel && currentRank >= 0 && targetRank > currentRank; const optionalDowngrade = selection.model === selection.cheapModel && current === selection.stableModel && cold && currentRank < roster.tiers.length - 1 && shouldSwitchRoot(current, selection.model, selection.expectedBenefit); let actualModel = current; let outcome: "selected" | "switched" | "preserved" | "blocked" = current === selection.model ? "selected" : "preserved"; if (current === selection.model) { pi.setThinkingLevel(selection.thinking); } else if (optionalDowngrade || requiredUpgrade) { let targetModel; try { targetModel = resolveContextModel(ctx, selection.model); } catch {} let switched = false; if (targetModel) { try { switched = await pi.setModel(targetModel); } catch { switched = false; } } if (!switched && requiredUpgrade) { await controller.recordRootSelectionCompleted(result.runId, selection, current, "blocked").catch(() => {}); await controller.blockRootModelSelection(result.runId); throw new Error(`Required root model ${selection.model} is unavailable`); } if (switched) { pi.setThinkingLevel(selection.thinking); actualModel = selection.model; outcome = "switched"; } } await controller.recordRootSelectionCompleted(result.runId, selection, actualModel, outcome); } if (continuationPending) rootContinuationPending = Boolean(result.rootSelection); pi.appendEntry("ultrapi-run", { runId: result.runId, topology: result.topology, status: result.status, piAgentsRunId: result.piAgentsRunId }); } catch (error) { if (rootRunId === result.runId) rootRunId = undefined; if (continuationPending) rootContinuationPending = false; await controller.stop(result.runId).catch(() => {}); throw error; } }; const LEVEL_LITERAL = Type.Union([Type.Literal("low"), Type.Literal("medium"), Type.Literal("high")]); pi.registerTool({ name: "ultra_dispatch", label: "UltraPi dispatch", description: "Classify a bounded task and select the smallest safe execution topology. It is the only root orchestration entry point.", promptSnippet: "ultra_dispatch: classify a task and invoke UltraPi's bounded orchestration when delegation materially helps.", promptGuidelines: ["For an ambiguous task, pass exactly one explicit bounded mode proposal: direct, scout, swarm, deep, or warroom. Never omit mode and never use auto.", "Prefer DIRECT for known, local work.", "Pass an executable acceptanceCommand whenever one is known.", "Use declaredShape to report what the task actually needs when the wording alone would understate it. The controller only ever escalates from a declaration, so a declaration can cost more but can never route below what the repository signals require.", "Do not use any other orchestration tool.", ROOT_PRESENTER_CONTRACT], parameters: Type.Object({ objective: Type.String({ minLength: 1, maxLength: 12_000 }), mode: Type.Union([Type.Literal("direct"), Type.Literal("scout"), Type.Literal("swarm"), Type.Literal("deep"), Type.Literal("warroom")]), policy: Type.Optional(Type.Union([Type.Literal("economy"), Type.Literal("balanced"), Type.Literal("quality"), Type.Literal("max")])), paths: Type.Optional(Type.Array(Type.String({ minLength: 1 }), { maxItems: 32 })), acceptanceCommand: Type.Optional(Type.String({ minLength: 1, maxLength: 2_000 })), privacyClass: Type.Optional(Type.Union([Type.Literal("public"), Type.Literal("internal"), Type.Literal("restricted"), Type.Literal("secret")])), declaredShape: Type.Optional(Type.Object({ intent: Type.Optional(Type.Union([Type.Literal("answer"), Type.Literal("investigate"), Type.Literal("review"), Type.Literal("change"), Type.Literal("fix")])), risk: Type.Optional(LEVEL_LITERAL), coupling: Type.Optional(LEVEL_LITERAL), uncertainty: Type.Optional(LEVEL_LITERAL), independentUnits: Type.Optional(Type.Integer({ minimum: 1, maximum: 64 })), })), }), executionMode: "sequential", async execute(_id, params, _signal, _update, ctx) { try { const { force: _ignoredForce, ...request } = params as DispatchRequest; if (!request.mode || request.mode === "auto") throw new Error("ultra_dispatch requires one explicit bounded mode; omit/auto is not allowed"); const pendingRootTelemetry = { samples: pendingRootSamples.splice(0), providers: pendingProviders.splice(0) }; pendingRootFailure = undefined; const result = await controller.dispatch(ctx, request, pendingRootTelemetry); await activateDispatch(result, ctx, true); const facts = result.facts?.map((fact) => ({ id: fact.id, claim: fact.claim, confidence: fact.confidence })) ?? []; return { content: [{ type: "text", text: `${result.summary}\nrun=${result.runId}${facts.length ? `\nscout_facts=${JSON.stringify(facts)}` : ""}` }], details: { runId: result.runId, topology: result.topology, status: result.status, reasonCodes: result.reasonCodes } }; } catch (error) { return { content: [{ type: "text", text: error instanceof Error ? error.message : String(error) }], details: {}, isError: true }; } }, }); pi.registerCommand("ultra", { description: "Run a task through UltraPi: /ultra ", handler: async (args, ctx) => { const task = args.trim(); if (!task) { ctx.ui.notify("Usage: /ultra ", "warning"); return; } const contextRatio = (ctx.getContextUsage()?.percent ?? 0) / 100; const initialShape = analyzeTask({ objective: task, contextRatio }); const explicitCommand = explicitAcceptanceCommand(task); const acceptanceCommand = explicitCommand ?? (initialShape.intent === "fix" || initialShape.intent === "change" ? await inferAcceptanceCommand(ctx.cwd, ctx.isProjectTrusted?.() === true) : undefined); const request: DispatchRequest = { objective: task, ...(initialShape.mentionedPaths.length ? { paths: initialShape.mentionedPaths } : {}), ...(acceptanceCommand ? { acceptanceCommand } : {}) }; const shape = acceptanceCommand ? analyzeTask({ ...request, contextRatio }) : initialShape; if (controller.getConfig().mode !== "auto" || isHardDirect(shape)) { if (!ctx.isIdle()) await ctx.waitForIdle(); const result = await controller.dispatch(ctx, request); await activateDispatch(result, ctx, false); if (!result.rootSelection) return; const facts = result.facts?.map((fact) => ({ id: fact.id, claim: fact.claim, confidence: fact.confidence })) ?? []; const pendingTurn = { kind: "direct", runId: result.runId } as const; pendingRootTurns.push(pendingTurn); hideDispatchForDirectRun(); try { pi.sendMessage( { customType: "ultrapi-task", content: `UltraPi already dispatched this task as ${result.topology} (run=${result.runId}). Do not call ultra_dispatch. Perform the assigned root work and honor the controller result below. ${ROOT_PRESENTER_CONTRACT}\n\n${result.summary}${acceptanceCommand ? `\nAcceptance command: ${acceptanceCommand}` : ""}${facts.length ? `\nScout facts: ${JSON.stringify(facts)}` : ""}\n\nTask:\n${task}`, display: false }, { triggerTurn: true }, ); } catch (error) { removePendingRootTurn(pendingTurn); restoreToolsAfterDirectRuns(); await controller.stop(result.runId); throw error; } return; } await controller.reserveAutoLaunch(); try { const stableModel = controller.getConfig().root.model; if (modelId(ctx.model) !== stableModel) { const targetModel = resolveContextModel(ctx, stableModel); let switched = false; if (targetModel) { try { switched = await pi.setModel(targetModel); } catch { switched = false; } } if (!switched) throw new Error(`Stable decision model ${stableModel} is unavailable`); } } catch (error) { controller.releaseAutoLaunchReservation(); throw error; } pi.setThinkingLevel("high"); const pendingTurn = { kind: "dispatch" } as const; pendingRootTurns.push(pendingTurn); try { pi.sendMessage( { customType: "ultrapi-task", content: `Analyze the task, then call ultra_dispatch exactly once. mode is REQUIRED and must be exactly one of direct|scout|swarm|deep|warroom; never omit mode and never use auto. Propose the smallest bounded topology; the controller will validate or safely override it.${acceptanceCommand ? ` Pass acceptanceCommand exactly as: ${acceptanceCommand}.` : ""} Then perform any root-agent work it assigns. ${ROOT_PRESENTER_CONTRACT}\n\nTask:\n${task}`, display: false }, ctx.isIdle() ? { triggerTurn: true } : { triggerTurn: true, deliverAs: "followUp" }, ); } catch (error) { removePendingRootTurn(pendingTurn); controller.releaseAutoLaunchReservation(); throw error; } }, }); pi.registerCommand("ultra-config", { description: "UltraPi status, settings, runs, and diagnostics", getArgumentCompletions: (prefix) => ultraConfigCompletions(prefix), handler: async (args, ctx) => { await runUltraConfigCommand(args, ctx, controller, async (result) => { if (!result.rootHandoff) return; const pendingTurn = { kind: "recovery", runId: result.runId } as const; pendingRootTurns.push(pendingTurn); try { pi.appendEntry("ultrapi-run", { runId: result.runId, recoveredFromRunId: result.recoveredFromRunId, topology: result.topology, status: result.status }); pi.sendMessage( { customType: "ultrapi-recovery", content: `${result.rootHandoff}\n\n${ROOT_PRESENTER_CONTRACT}`, display: false }, ctx.isIdle() ? { triggerTurn: true } : { triggerTurn: true, deliverAs: "followUp" }, ); } catch (error) { removePendingRootTurn(pendingTurn); await controller.stop(result.runId); throw error; } }, (runId, notify, mode) => { liveTraceMode = runId ? mode ?? "live" : undefined; rawLiveNext = runId === "next" && liveTraceMode === "raw-live"; liveTraceRunId = rawLiveNext ? undefined : runId; liveTraceNotify = runId ? notify : undefined; }); }, }); // These entries have been appended at every dispatch and rendered as nothing, because no renderer // was ever registered for the type. Registration is optional the same way painting is: a host // without the surface loses a line of transcript decoration, not the extension. pi.registerEntryRenderer?.(RUN_ENTRY_TYPE, (entry, _options, theme) => { try { const line = runEntryLine(entry.data, (colour, value) => theme.fg(colour as never, value)); return line === undefined ? undefined : { render: () => [line], invalidate: () => {} }; } catch { return undefined; } }); pi.on("session_start", async (event, ctx) => { hudUi = ctx.ui; panelUi = ctx.mode === "tui" ? ctx.ui : undefined; activeWithoutCompetingTools(pi); runtimeSessionId = ctx.sessionManager?.getSessionId?.() ?? runtimeSessionId ?? randomUUID(); await controller.recordSessionLifecycle(runtimeSessionId, "session.started", event.reason ?? "unknown").catch(() => {}); await markInterruptedRunsStopped(controller.paths.events, controller.paths.ledgers); await controller.rebuildProjection(); await evaluateRunningExperiments(controller.paths); }); pi.on("before_agent_start", (event, ctx) => { activeWithoutCompetingTools(pi); const selected = modelId(ctx.model); if (selected) assertModelAllowed(controller.getConfig(), selected); return { systemPrompt: stripPiAgentsAppendix(event.systemPrompt) }; }); pi.on("turn_start", (event, ctx) => { hudUi = ctx.ui ?? hudUi; if (ctx.mode === "tui" && ctx.ui) panelUi = ctx.ui; paintHud(hudUi); paintPanel(panelUi); if (rootContinuationPending) rootContinuationPending = false; else { const pendingTurn = pendingRootTurns.shift(); if (pendingTurn) { rootRunId = "runId" in pendingTurn ? pendingTurn.runId : undefined; rootGuardActive = pendingTurn.kind !== "presentation"; presentationTurnActive = pendingTurn.kind === "presentation"; directTurnActive = pendingTurn.kind === "direct"; } } turnIndex = event.turnIndex; }); pi.on("tool_call", async (event, ctx) => { if (presentationTurnActive) return { block: true, reason: "UltraPi terminal presentation is read-only", terminate: true }; if (rootBudgetStopActive) return { block: true, reason: "UltraPi stopped the root turn at the task budget limit", terminate: true }; if (isToolCallEventType("edit", event) || isToolCallEventType("write", event)) { if (rootGuardActive && !rootRunId) return { block: true, reason: "UltraPi root writes require a dispatched scope", terminate: true }; if (rootRunId) { try { pendingRootWrites.set(event.toolCallId, { runId: rootRunId, path: await controller.normalizeRootWritePath(rootRunId, event.input.path) }); } catch { return { block: true, reason: "UltraPi blocked an out-of-scope root write", terminate: true }; } } } if ((!rootRunId && !rootGuardActive) || !isToolCallEventType("bash", event)) return; const decision = guardedCommand(event.input.command); if (decision === "allow") return; if (decision === "block") return { block: true, reason: "UltraPi blocked an unsafe root command", terminate: true }; let approved = false; try { approved = await ctx.ui.confirm("UltraPi command approval", event.input.command); } catch { approved = false; } return approved ? undefined : { block: true, reason: "UltraPi root command was not approved", terminate: true }; }); pi.on("before_provider_request", async (_event, ctx) => { if (rootBudgetStopActive) throw new Error("UltraPi stopped the root turn at the task budget limit"); const selected = modelId(ctx.model); if (!selected) return; if (rootRunId) await flushPendingRootSamples(rootRunId); providerStartedAt = Date.now(); providerFailureRecorded = false; rootSample = { model: selected, thinkingLevel: ctx.thinkingLevel ?? "unknown", turnIndex, callIndex: ++callIndex, startedAt: providerStartedAt }; if (rootRunId) await controller.recordRootModelStarted(rootRunId, selected, { thinkingLevel: rootSample.thinkingLevel, latencyMs: 0, turnIndex, callIndex }); }); pi.on("after_provider_response", async (event) => { const latencyMs = providerStartedAt ? Math.max(0, Date.now() - providerStartedAt) : 0; const health = controller.providerResponse(event.status, latencyMs); if (rootRunId) await controller.recordProviderHealth(rootRunId, event.status, latencyMs, "root", health).catch(() => {}); else pendingProviders.push({ status: event.status, latencyMs, health }); if (event.status < 400 || !rootSample) return; const errorClass = event.status === 429 ? "http-429" : event.status >= 500 ? "http-5xx" : "http-4xx"; const pending = { sample: rootSample, errorClass, started: Boolean(rootRunId) }; pendingRootFailure = pending; pendingRootSamples.push(pending); rootSample = undefined; providerFailureRecorded = true; }); pi.on("message_end", async (event, ctx) => { if (event.message.role !== "assistant") return; if (rootRunId) rootContinuationPending = false; let replacement: typeof event.message | undefined; if (rootRunId && event.message.stopReason === "stop") { try { let manifest: AttributionManifest | undefined; const content = event.message.content.map((part) => { if (part.type !== "text") return part; const extracted = extractRootAttribution(part.text); manifest ??= extracted.manifest; return { ...part, text: extracted.visibleText }; }); // The manifest is the model's own account of what it changed, and it was trusted // whenever it was present -- synthesis only filled in for a missing one. That gap is // load-bearing: runVerification treats an informational run with an empty change set // as COMPLETED/success with no acceptance command executed, so a model that edited // the user's files and declared `changes: []` got a verified-free success over a // modified repository. The worktree path already refuses a manifest that disagrees // with git; the root path had no equivalent. // // These writes were observed by the harness at tool_result, not claimed, so the // manifest is reconciled against them rather than believed. if (controller.isDirectRootRun(rootRunId)) { const observed = [...(observedRootChanges.get(rootRunId) ?? [])].sort(); const declared = new Set(manifest?.changes.map((change) => change.path) ?? []); const undeclared = observed.filter((path) => !declared.has(path)); if (undeclared.length) { const decisionId = "root-observed-change"; const decision = { decisionId, description: "Apply the observed scoped root change", supportingFactIds: [] }; const changes = undeclared.map((path) => ({ path, supportingDecisionIds: [decisionId] })); manifest = manifest ? { ...manifest, decisions: [...manifest.decisions, decision], changes: [...manifest.changes, ...changes] } : { usedFactIds: [], rejectedFactIds: [], decisions: [decision], changes }; } } if (manifest) await controller.recordRootAttribution(rootRunId, manifest); replacement = { ...event.message, content }; } catch {} } if (!rootSample && providerFailureRecorded && (event.message.stopReason === "error" || event.message.stopReason === "aborted")) { providerFailureRecorded = false; if (pendingRootFailure) { pendingRootFailure.usage = event.message.usage; pendingRootFailure.observedModel = messageModel(event.message); pendingRootFailure.observedProvider = typeof event.message.provider === "string" ? event.message.provider : undefined; } if (rootRunId) await flushPendingRootSamples(rootRunId).catch(() => {}); pendingRootFailure = undefined; const outcome = rootRunId ? await controller.finishRootRun(rootRunId) : undefined; if (outcome?.state === "BUDGET_EXHAUSTED") return { message: { ...event.message, content: [{ type: "text", text: rootOutcomeSummary(outcome) }] } }; return; } const runId = rootRunId; const observedModel = messageModel(event.message); const observedProvider = typeof event.message.provider === "string" ? event.message.provider : undefined; const billingModel = rootSample?.model ?? modelId(ctx.model); const sample = billingModel ? rootSample ?? { model: billingModel, thinkingLevel: ctx.thinkingLevel ?? "unknown", turnIndex, callIndex: ++callIndex, startedAt: Date.now() } : undefined; const errorClass = event.message.stopReason === "error" || event.message.stopReason === "aborted" ? event.message.stopReason : undefined; const metadata = sample ? { thinkingLevel: sample.thinkingLevel, latencyMs: Math.max(0, Date.now() - sample.startedAt), turnIndex: sample.turnIndex, callIndex: sample.callIndex, observedModel, observedProvider } : undefined; if (billingModel && sample && metadata && runId) { if (errorClass) await controller.recordRootModelFailed(runId, billingModel, { ...metadata, errorClass }, event.message.usage).catch(() => {}); else { const mayContinue = await controller.recordRootUsage(runId, billingModel, event.message.usage, metadata).catch(() => false); if (event.message.stopReason === "toolUse") { rootContinuationPending = mayContinue; rootBudgetStopActive = !mayContinue; } } } else if (billingModel && sample) pendingRootSamples.push({ sample, usage: event.message.usage, observedModel, observedProvider, ...(errorClass ? { errorClass } : {}) }); rootSample = undefined; if (runId && event.message.stopReason === "stop" && !presentationTurnActive) { const outcome = await controller.finishRootRun(runId); if (outcome) { const base = replacement ?? event.message; const summary = rootOutcomeSummary(outcome); const originalContent = base.content ?? []; const textIndex = originalContent.findIndex((part) => part.type === "text"); if (textIndex >= 0) { const content = originalContent.map((part, index) => part.type !== "text" ? part : outcome.state === "COMPLETED" ? index === textIndex ? { ...part, text: `${part.text}\n\n${summary}` } : part : { ...part, text: index === textIndex ? summary : "" }); replacement = { ...base, content }; } else replacement = { ...base, content: [{ type: "text", text: summary }] }; } } return replacement ? { message: replacement } : undefined; }); pi.on("tool_execution_start", async (event) => { const timer = { toolName: event.toolName, startedAt: Date.now(), recorded: Boolean(rootRunId) }; toolTimers.set(event.toolCallId, timer); if (rootRunId) await controller.recordRootTool(rootRunId, "tool.started", event.toolCallId, event.toolName, 0).catch(() => {}); }); pi.on("tool_execution_end", async (event) => { paintHud(hudUi); paintPanel(panelUi); const timer = toolTimers.get(event.toolCallId); toolTimers.delete(event.toolCallId); if (!rootRunId || !timer) return; if (!timer.recorded) await controller.recordRootTool(rootRunId, "tool.started", event.toolCallId, timer.toolName, 0).catch(() => {}); await controller.recordRootTool(rootRunId, event.isError ? "tool.failed" : "tool.completed", event.toolCallId, timer.toolName, Math.max(0, Date.now() - timer.startedAt)).catch(() => {}); }); pi.on("tool_result", (event) => { const pending = pendingRootWrites.get(event.toolCallId); pendingRootWrites.delete(event.toolCallId); if (!pending || event.isError) return; const changes = observedRootChanges.get(pending.runId) ?? new Set(); changes.add(pending.path); observedRootChanges.set(pending.runId, changes); }); pi.on("model_select", async (event) => { const previous = modelId(event.previousModel); const selected = modelId(event.model); if (rootRunId && previous && selected && previous !== selected) await controller.recordRootModelChanged(rootRunId, previous, selected, event.source).catch(() => {}); }); pi.on("turn_end", async () => { paintHud(hudUi); paintPanel(panelUi); rootBudgetStopActive = false; pendingRootSamples.length = 0; pendingProviders.length = 0; pendingRootFailure = undefined; if (rootContinuationPending) { rootGuardActive = false; presentationTurnActive = false; await evaluateRunningExperiments(controller.paths); return; } const runId = rootRunId; rootRunId = undefined; rootGuardActive = false; presentationTurnActive = false; directTurnActive = false; try { if (runId) await controller.finishRootRun(runId); } finally { restoreToolsAfterDirectRuns(); if (runId) { observedRootChanges.delete(runId); for (const [toolCallId, pending] of pendingRootWrites) if (pending.runId === runId) pendingRootWrites.delete(toolCallId); } await evaluateRunningExperiments(controller.paths); } }); pi.on("session_shutdown", async (event) => { clearHud(hudUi); clearPanel(panelUi); if (runtimeSessionId) await controller.recordSessionLifecycle(runtimeSessionId, "session.ended", event.reason ?? "unknown").catch(() => {}); controller.close(); }); } export default installUltraPi;