import { createHash } from "node:crypto"; import type { ThinkingLevel } from "@earendil-works/pi-agent-core"; import { type Api, clampThinkingLevel, type Model, type ToolResultMessage, } from "@earendil-works/pi-ai"; import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, DynamicBorder, type ExtensionAPI, type ExtensionContext, type ExtensionFactory, getMarkdownTheme, keyHint, type Theme, type ToolDefinition, truncateHead, } from "@earendil-works/pi-coding-agent"; import type { AutocompleteItem } from "@earendil-works/pi-tui"; import { Box, Container, Loader, Markdown, matchesKey, Text, } from "@earendil-works/pi-tui"; import { Type } from "typebox"; import type { ConsultationMetadata, ConsultationProgress, ConsultationRequest, ConsultationResult, RunConsultationOptions, } from "./advisor.ts"; import { closeDebug, dbg, span } from "./src/debug.ts"; import { resolveSetting, type SettingDeclaration, } from "./src/pi-ext-settings.ts"; interface DilemmaSettings { pairs?: Array<{ executor: string; advisor: string }>; enabled?: boolean; thinkingLevel?: ThinkingLevel; subagentExtensions?: "none" | "all" | string[]; devilThreshold?: number; } interface Binding { executor: Model; advisor: Model; } type SubagentExtensionSelection = "all" | string[]; interface AdviceDisplay { version: 1; advice: string; metadata: ConsultationMetadata; } interface ToolDetails { kind: "progress" | "complete"; progress?: ConsultationProgress; result?: ConsultationResult; } interface StatusDisplay { role?: "angel" | "devil"; version: 1; kind: "cancelled" | "failed"; message: string; } export interface DilemmaDependencies { runConsultation?: ( ctx: ExtensionContext, request: ConsultationRequest, options: RunConsultationOptions, ) => Promise; loadSettings?: (ctx: ExtensionContext) => DilemmaSettings; } const ADVICE_TYPE = "dilemma-advice"; const STATUS_TYPE = "dilemma-status"; const CHILD_MARKER = "dilemma-child"; const STATUS_ID = "dilemma"; const ANGEL_ICON = "󰛓"; const DEVIL_ICON = "󰇴"; const PREVIEW_LINES = 5; function roleIcon(role: "angel" | "devil" | undefined): string { return role === "devil" ? DEVIL_ICON : ANGEL_ICON; } /** Bound rendered rows, including wrapping, not source characters. */ export function streamingPreview(text: string, expanded = false): Text { const component = new Text(text, 0, 0); if (!expanded) { const render = component.render.bind(component); component.render = (width) => render(width).slice(-PREVIEW_LINES); } return component; } const DEFAULT_THINKING_LEVEL: ThinkingLevel = "max"; const LOW_SIGNAL_BUILTINS = new Set([ "read", "edit", "write", "grep", "find", "ls", ]); const TOOL_DESCRIPTION = `Consult Angel for deep reasoning and independent investigation: difficult diagnosis, competing explanations, architectural trade-offs, contradictory evidence, or a second opinion before committing to an approach. Angel receives the focused assignment, loads project instructions, and can inspect the pinned parent session on demand alongside project and web evidence. Provide the question or decision you need resolved, including any competing hypotheses. Returns evidence-backed advice, not implementation or permission.`; const TOOL_GUIDELINES: string[] = [ "Call angel when independent investigation can materially improve the next decision, including difficult diagnosis, competing explanations, architectural trade-offs, contradictory evidence, or consequential uncertainty.", "Give angel a concrete question, uncertainty, or competing hypothesis; do not call it merely for generic reassurance.", "Do not copy the parent transcript into angel arguments; Angel can retrieve relevant entries from the pinned parent session on demand.", "Evaluate angel advice against primary evidence, user instructions, and observed results; Angel advises but does not override them.", ]; function isPlainObject(value: unknown): value is Record { return value !== null && typeof value === "object" && !Array.isArray(value); } function canonicalValue(value: unknown): unknown { if (Array.isArray(value)) return value.map(canonicalValue); if (!isPlainObject(value)) return value; return Object.fromEntries( Object.keys(value) .sort() .map((key) => [key, canonicalValue(value[key])]), ); } function operationKey( toolName: string, arguments_: Record, ): string | undefined { try { const encoded = JSON.stringify(canonicalValue(arguments_)); if (encoded === undefined) return undefined; return createHash("sha256") .update(toolName) .update("\0") .update(encoded) .digest("hex"); } catch { return undefined; } } function toolCallArguments( message: unknown, ): Map> { const calls = new Map>(); if (!isPlainObject(message) || message["role"] !== "assistant") return calls; if (!Array.isArray(message["content"])) return calls; for (const part of message["content"]) { if (!isPlainObject(part) || part["type"] !== "toolCall") continue; if (typeof part["id"] !== "string" || !isPlainObject(part["arguments"])) continue; calls.set(part["id"], part["arguments"]); } return calls; } const THINKING_LEVELS = new Set([ "off", "minimal", "low", "medium", "high", "xhigh", "max", ]); function parseDilemmaSettings(raw: unknown): DilemmaSettings { if (!isPlainObject(raw)) throw new Error("expected an object"); const { pairs, enabled, thinkingLevel, subagentExtensions, devilThreshold } = raw; if ( devilThreshold !== undefined && (!Number.isSafeInteger(devilThreshold) || (devilThreshold as number) < 1) ) throw new Error("devilThreshold must be a positive safe integer"); if ( pairs !== undefined && !( Array.isArray(pairs) && pairs.every( (pair) => isPlainObject(pair) && typeof pair["executor"] === "string" && typeof pair["advisor"] === "string", ) ) ) throw new Error("pairs must be an array of { executor, advisor } strings"); if (enabled !== undefined && typeof enabled !== "boolean") throw new Error("enabled must be a boolean"); if (thinkingLevel !== undefined && !THINKING_LEVELS.has(thinkingLevel)) throw new Error( `thinkingLevel must be one of ${[...THINKING_LEVELS].join(", ")}`, ); if ( subagentExtensions !== undefined && subagentExtensions !== "all" && subagentExtensions !== "none" && !( Array.isArray(subagentExtensions) && subagentExtensions.every((name) => typeof name === "string") ) ) throw new Error( 'subagentExtensions must be "all", "none", or a Pi source array', ); return raw as DilemmaSettings; } // Precedence: trusted project deep-merged over user, then {}. const DILEMMA_SETTING: SettingDeclaration = { key: "dilemma", parse: parseDilemmaSettings, default: {}, }; function resolveSubagentExtensions( setting: DilemmaSettings["subagentExtensions"], ): SubagentExtensionSelection | undefined { if (setting === undefined || setting === "none") return undefined; if (setting === "all") return "all"; return [...new Set(setting.map((source) => source.trim()).filter(Boolean))]; } function modelKey(model: Model): string { return `${model.provider}/${model.id}`; } function modelMatches(spec: string, model: Model): boolean { return spec === model.id || spec === modelKey(model); } function resolveBinding( ctx: ExtensionContext, settings: DilemmaSettings, executor: Model | undefined = ctx.model, ): Binding | undefined { if (!executor) return undefined; for (const pair of settings.pairs ?? []) { if (!modelMatches(pair.executor, executor)) continue; const advisor = pair.advisor.includes("/") ? ctx.modelRegistry .getAll() .find((model) => modelKey(model) === pair.advisor) : ctx.modelRegistry.find(executor.provider, pair.advisor); return advisor ? { executor, advisor } : undefined; } return undefined; } function metadataValues(metadata: ConsultationMetadata) { return { seconds: (metadata.runtimeMs / 1_000).toFixed(1), cost: metadata.cost === undefined ? "cost unavailable" : `$${metadata.cost.toFixed(4)}`, }; } function compactTokenCount(total: number): string { if (total < 1_000) return total.toLocaleString("en-US"); return `${(total / 1_000).toFixed(1).replace(/\.0$/u, "")}k`; } function metadataFooterLines(metadata: ConsultationMetadata): string[] { const { seconds, cost } = metadataValues(metadata); const session = metadata.childSessionId.length > 14 ? `${metadata.childSessionId.slice(0, 13)}…` : metadata.childSessionId; return [ `${metadata.model}/${metadata.thinkingLevel} · ${compactTokenCount(metadata.tokens.total)} tokens · ${cost} · ${seconds}s`, `${metadata.origin} · session ${session}`, ]; } function metadataDetailLines(metadata: ConsultationMetadata): string[] { const { seconds, cost } = metadataValues(metadata); return [ `${metadata.origin} · ${metadata.model} · ${metadata.thinkingLevel}`, `${metadata.tokens.total.toLocaleString("en-US")} tokens · ${cost} · ${seconds}s`, `child ${metadata.childSessionId}`, ]; } function adviceComponent( data: AdviceDisplay, theme: Theme, padding: number, ): Container { const box = new Box(padding, 1, (text) => theme.bg("customMessageBg", text)); box.addChild( new Text( theme.fg( "accent", theme.bold( `${roleIcon(data.metadata.role)} ${data.metadata.role === "devil" ? "Devil advice" : "Angel advice"}`, ), ), 0, 0, ), ); box.addChild(new Markdown(data.advice, 0, 0, getMarkdownTheme())); box.addChild(new Text("", 0, 0)); box.addChild(new DynamicBorder((text) => theme.fg("dim", text))); box.addChild( new Text( theme.fg("dim", metadataFooterLines(data.metadata).join("\n")), 0, 0, ), ); const container = new Container(); container.addChild(box); return container; } function statusComponent( data: StatusDisplay, theme: Theme, padding: number, ): Container { const box = new Box(padding, 1, (text) => theme.bg("customMessageBg", text)); const role = data.role === "devil" ? "Devil" : "Angel"; const label = `${role} ${data.kind === "failed" ? "failed" : "cancelled"}`; const color = data.kind === "failed" ? "error" : "muted"; box.addChild(new Text(theme.fg(color, theme.bold(label)), 0, 0)); if (data.message !== label) box.addChild(new Text(theme.fg("dim", `\n${data.message}`), 0, 0)); const container = new Container(); container.addChild(box); return container; } function boundedAdvice(result: ConsultationResult): string { const truncated = truncateHead(result.advice, { maxBytes: DEFAULT_MAX_BYTES, maxLines: DEFAULT_MAX_LINES, }); if (!truncated.truncated) return truncated.content; return `${truncated.content}\n\n[${result.metadata.role === "devil" ? "Devil" : "Angel"} advice truncated to ${truncated.outputLines}/${truncated.totalLines} lines for tool context. Full advice is preserved in child session ${result.metadata.childSessionId}.]`; } function textFromToolResult(result: { content: Array<{ type: string; text?: string }>; }): string { return result.content .filter( (part): part is { type: "text"; text: string } => part.type === "text" && typeof part.text === "string", ) .map((part) => part.text) .join("\n"); } function isExplicitCancellation(result: { isError: boolean; details?: unknown; content: Array<{ type: string; text?: string }>; }): boolean { if (!result.isError) return false; const details = result.details; if (isPlainObject(details)) { if (details["cancelled"] === true || details["aborted"] === true) return true; } const status = textFromToolResult(result) .trim() .toLowerCase() .split("\n") .at(-1) ?.trim(); return /^(?:(?:(?:tool execution|command|operation|(?:angel|devil|dilemma) consultation) )?(?:cancelled|canceled|aborted)|this operation was aborted)[.!]?$/u.test( status ?? "", ); } function abortError(): DOMException { return new DOMException("Dilemma consultation cancelled", "AbortError"); } function isAbortError(error: unknown): boolean { return error instanceof Error && error.name === "AbortError"; } async function withAbort( operation: Promise, signal: AbortSignal, ): Promise { signal.throwIfAborted(); return new Promise((resolve, reject) => { let settled = false; const finish = (callback: () => void) => { if (settled) return; settled = true; signal.removeEventListener("abort", onAbort); callback(); }; const onAbort = () => finish(() => reject(abortError())); signal.addEventListener("abort", onAbort, { once: true }); operation.then( (value) => finish(() => resolve(value)), (error) => finish(() => reject(error)), ); }); } function completedBatchContext( results: ToolResultMessage[], repeatedFailures: ToolResultMessage[], ): string { const repeatedIds = new Set( repeatedFailures.map((result) => result.toolCallId), ); const references = results.map((result) => { const status = result.isError ? "error" : "success"; const repeated = repeatedIds.has(result.toolCallId) ? " · repeated" : ""; return `- ${result.toolName} · ${result.toolCallId} · ${status}${repeated}`; }); return [ "The completed executor tool batch is available in the pinned parent session.", "Use these references with parent_session to locate every sibling result without duplicating result content:", ...references, ].join("\n"); } function isAdviceDisplay(value: unknown): value is AdviceDisplay { if (!isPlainObject(value)) return false; return ( value["version"] === 1 && typeof value["advice"] === "string" && isPlainObject(value["metadata"]) && typeof value["metadata"]["childSessionId"] === "string" ); } function isStatusDisplay(value: unknown): value is StatusDisplay { return ( isPlainObject(value) && value["version"] === 1 && (value["kind"] === "cancelled" || value["kind"] === "failed") && typeof value["message"] === "string" ); } function isCompleteToolDetails(value: unknown): value is ToolDetails & { kind: "complete"; result: ConsultationResult; } { if (!isPlainObject(value) || value["kind"] !== "complete") return false; if (!isPlainObject(value["result"])) return false; const result = value["result"]; const metadata = result["metadata"]; if (!isPlainObject(metadata) || !isPlainObject(metadata["tokens"])) return false; return ( typeof result["advice"] === "string" && (metadata["role"] === undefined || metadata["role"] === "angel" || metadata["role"] === "devil") && typeof metadata["origin"] === "string" && typeof metadata["model"] === "string" && typeof metadata["thinkingLevel"] === "string" && typeof metadata["runtimeMs"] === "number" && typeof metadata["tokens"]["total"] === "number" && (metadata["cost"] === undefined || typeof metadata["cost"] === "number") && typeof metadata["childSessionId"] === "string" && typeof metadata["childSessionFile"] === "string" ); } function questionSummary(question: unknown): string { if (typeof question !== "string") return ""; const normalized = question.replaceAll(/\s+/g, " ").trim(); return normalized.length <= 72 ? normalized : `${normalized.slice(0, 69)}...`; } // Shared so concurrent consultations evaluate ./advisor.ts once. // Pi's jiti loader (module cache disabled) can otherwise hand one caller a // partially initialized module whose imports are still undefined. let advisorModule: Promise | undefined; const loadAdvisor = (): Promise => { advisorModule ??= import("./advisor.ts"); return advisorModule; }; async function realRunConsultation( ctx: ExtensionContext, request: ConsultationRequest, options: RunConsultationOptions, ): Promise { const implementation = await loadAdvisor(); return implementation.runConsultation(ctx, request, options); } function isChildSession(ctx: ExtensionContext): boolean { return ctx.sessionManager .getBranch() .some( (entry) => entry.type === "custom" && entry.customType === CHILD_MARKER, ); } export function createDilemmaExtension( dependencies: DilemmaDependencies = {}, ): ExtensionFactory { const consult = dependencies.runConsultation ?? realRunConsultation; return function dilemmaExtension(pi: ExtensionAPI) { const readSettings = dependencies.loadSettings ?? ((ctx: ExtensionContext): DilemmaSettings => { const setting = resolveSetting(pi, ctx, DILEMMA_SETTING); if (setting.ok) return setting.value; if (ctx.hasUI) ctx.ui.notify(`${setting.error}; Angel unconfigured`, "warning"); return {}; }); let settings: DilemmaSettings = {}; let sessionEnabled = true; let childSession = false; let active = true; let sessionGeneration = 0; let consultationSequence = 0; let completedTurnSequence = 0; let taskGeneration = 0; let cancellationGeneration = 0; let activeTurnIndex: number | undefined; let cleanBatches = 0; let devilLatched = false; let lastAvailability: boolean | undefined; const resetDevil = () => { cleanBatches = 0; devilLatched = false; }; const consultations = new Set(); const operationKeysByCall = new Map(); const pendingFailures = new Map(); const consultedFailures = new Set(); const taskGenerationByTurn = new Map(); const enabled = () => active && !childSession && sessionEnabled; const clearFailureTracking = () => { resetDevil(); operationKeysByCall.clear(); pendingFailures.clear(); consultedFailures.clear(); }; const invalidateTask = () => { taskGeneration += 1; clearFailureTracking(); }; const cancelConsultations = () => { cancellationGeneration += 1; for (const controller of consultations) controller.abort(); }; const tracksFailure = (toolName: string) => { if (toolName === "angel" || toolName === "devil") return false; const tool = pi .getAllTools() .find((candidate) => candidate.name === toolName); return !( tool?.sourceInfo.source === "builtin" && LOW_SIGNAL_BUILTINS.has(toolName) ); }; function isAvailable(ctx: ExtensionContext, executor = ctx.model): boolean { const binding = resolveBinding(ctx, settings, executor); return ( enabled() && !!binding && ctx.modelRegistry.hasConfiguredAuth(binding.advisor) ); } function clearAvailabilityStatus(ctx?: ExtensionContext): void { pi.events.emit("model-route:dilemma", { advisor: undefined }); if (ctx?.hasUI) ctx.ui.setStatus(STATUS_ID, undefined); } function syncAvailability( ctx: ExtensionContext, executor = ctx.model, ): void { const binding = resolveBinding(ctx, settings, executor); const available = enabled() && !!binding && ctx.modelRegistry.hasConfiguredAuth(binding.advisor); if (lastAvailability !== available) resetDevil(); lastAvailability = available; const tools = pi .getActiveTools() .filter((tool) => tool !== "angel" && tool !== "devil"); pi.setActiveTools(available ? [...tools, "angel", "devil"] : tools); if (!available || !binding) { clearAvailabilityStatus(ctx); return; } const thinkingLevel = clampThinkingLevel( binding.advisor, settings.thinkingLevel ?? DEFAULT_THINKING_LEVEL, ); if (ctx.hasUI) ctx.ui.setStatus( STATUS_ID, `${ANGEL_ICON} dilemma: ${binding.advisor.id}:${thinkingLevel}`, ); pi.events.emit("model-route:dilemma", { advisor: { modelId: binding.advisor.id, thinkingLevel }, }); } async function run( ctx: ExtensionContext, request: ConsultationRequest, options: { signal?: AbortSignal | undefined; onProgress?: ((progress: ConsultationProgress) => void) | undefined; } = {}, ): Promise { const generation = sessionGeneration; const label = request.role === "devil" ? "Devil" : "Angel"; if (request.role === "devil") { cleanBatches = 0; devilLatched = true; } else resetDevil(); if (!enabled()) { dbg?.("consultation.skip", { reason: "disabled" }); throw new Error(`${label} is disabled in this session`); } const binding = resolveBinding(ctx, settings); if (!binding) { dbg?.("consultation.skip", { reason: "unconfigured" }); throw new Error( `${label} has no advisor configured for ${ctx.model ? modelKey(ctx.model) : "the current model"}`, ); } const controller = new AbortController(); const finish = span?.("consultation", { kind: request.origin }); consultations.add(controller); const abort = () => controller.abort(options.signal?.reason); if (options.signal?.aborted) abort(); else options.signal?.addEventListener("abort", abort, { once: true }); try { const auth = await withAbort( ctx.modelRegistry.getApiKeyAndHeaders(binding.advisor), controller.signal, ); if (!auth.ok) throw new Error( `${label} advisor authentication failed: ${auth.error}`, ); if ( !active || generation !== sessionGeneration || controller.signal.aborted ) throw abortError(); const extensionSources = resolveSubagentExtensions( settings.subagentExtensions, ); const additionalExtensionSources = extensionSources === "all" ? [ ...new Set( [ ...pi.getAllTools().map((tool) => tool.sourceInfo), ...pi .getCommands() .filter((command) => command.source === "extension") .map((command) => command.sourceInfo), ] .filter( (info) => info.scope === "temporary" && info.source !== "builtin" && !info.path.startsWith("<"), ) .map((info) => info.path), ), ] : []; const result = await consult(ctx, request, { advisor: binding.advisor, thinkingLevel: settings.thinkingLevel ?? DEFAULT_THINKING_LEVEL, signal: controller.signal, extensionSources, additionalExtensionSources, runtimeApiKey: ctx.modelRegistry.isUsingOAuth(binding.advisor) ? undefined : auth.apiKey, onProgress: (progress) => { if ( active && generation === sessionGeneration && !controller.signal.aborted ) options.onProgress?.(progress); }, }); if ( !active || generation !== sessionGeneration || controller.signal.aborted ) throw abortError(); finish?.("finish", { status: "success" }); return result; } catch (error) { finish?.("error", { status: isAbortError(error) ? "cancelled" : "failed", }); throw error; } finally { options.signal?.removeEventListener("abort", abort); consultations.delete(controller); } } pi.registerMessageRenderer( ADVICE_TYPE, (message, { outputPad }, theme) => { if (!isAdviceDisplay(message.details)) return undefined; return adviceComponent(message.details, theme, outputPad); }, ); pi.registerEntryRenderer( ADVICE_TYPE, (entry, _options, theme) => { if (!isAdviceDisplay(entry.data)) return undefined; return adviceComponent(entry.data, theme, 1); }, ); pi.registerMessageRenderer( STATUS_TYPE, (message, { outputPad }, theme) => { if (!isStatusDisplay(message.details)) return undefined; return statusComponent(message.details, theme, outputPad); }, ); pi.on("session_start", (_event, ctx) => { sessionGeneration += 1; taskGeneration += 1; active = true; clearFailureTracking(); taskGenerationByTurn.clear(); activeTurnIndex = undefined; settings = readSettings(ctx); sessionEnabled = settings.enabled !== false; childSession = isChildSession(ctx); dbg?.("session.start"); syncAvailability(ctx); }); pi.on("session_shutdown", (_event, ctx) => { clearAvailabilityStatus(ctx); sessionGeneration += 1; taskGeneration += 1; active = false; clearFailureTracking(); taskGenerationByTurn.clear(); activeTurnIndex = undefined; for (const controller of consultations) controller.abort(); consultations.clear(); dbg?.("session.shutdown"); closeDebug(); }); pi.on("session_before_tree", () => { sessionGeneration += 1; taskGeneration += 1; clearFailureTracking(); taskGenerationByTurn.clear(); activeTurnIndex = undefined; for (const controller of consultations) controller.abort(); consultations.clear(); }); pi.on("input", (event) => { if (event.source !== "extension") invalidateTask(); }); pi.on("message_start", (event) => { if (event.message.role !== "user") return; invalidateTask(); if (activeTurnIndex !== undefined) taskGenerationByTurn.set(activeTurnIndex, taskGeneration); }); pi.on("turn_start", (event) => { activeTurnIndex = event.turnIndex; taskGenerationByTurn.set(event.turnIndex, taskGeneration); }); pi.on("model_select", (event, ctx) => { invalidateTask(); cancelConsultations(); syncAvailability(ctx, event.model); }); pi.on("session_compact", () => invalidateTask()); pi.on("tool_result", (event, ctx) => { if ( event.parentToolCallId || !isAvailable(ctx) || !tracksFailure(event.toolName) ) return; const key = operationKey(event.toolName, event.input); if (key) operationKeysByCall.set(event.toolCallId, key); }); pi.on("turn_end", async (event, ctx) => { const generation = sessionGeneration; const task = taskGenerationByTurn.get(event.turnIndex) ?? taskGeneration; taskGenerationByTurn.delete(event.turnIndex); if (activeTurnIndex === event.turnIndex) activeTurnIndex = undefined; if (childSession) return; for (const result of event.toolResults) { if (result.toolName !== "angel" && result.toolName !== "devil") continue; if (!isCompleteToolDetails(result.details)) continue; pi.appendEntry(ADVICE_TYPE, { version: 1, advice: result.details.result.advice, metadata: result.details.result.metadata, }); } if (task !== taskGeneration) { for (const result of event.toolResults) operationKeysByCall.delete(result.toolCallId); return; } const turnSequence = completedTurnSequence++; syncAvailability(ctx); if (!isAvailable(ctx)) { for (const result of event.toolResults) operationKeysByCall.delete(result.toolCallId); return; } const argumentsByCall = toolCallArguments(event.message); const keyedResults = event.toolResults.flatMap((result) => { if (!tracksFailure(result.toolName)) return []; const observedKey = operationKeysByCall.get(result.toolCallId); const arguments_ = argumentsByCall.get(result.toolCallId); const key = observedKey ?? (arguments_ ? operationKey(result.toolName, arguments_) : undefined); return key ? [{ key, result }] : []; }); for (const result of event.toolResults) operationKeysByCall.delete(result.toolCallId); const successfulKeys = new Set(); for (const { key, result } of keyedResults) { if (result.isError) continue; successfulKeys.add(key); pendingFailures.delete(key); consultedFailures.delete(key); } const repeatedFailures: Array<{ key: string; result: ToolResultMessage; }> = []; for (const { key, result } of keyedResults) { if ( !result.isError || successfulKeys.has(key) || isExplicitCancellation(result) ) continue; if (consultedFailures.has(key)) continue; const previousTurn = pendingFailures.get(key); if (previousTurn !== undefined && previousTurn !== turnSequence) { repeatedFailures.push({ key, result }); continue; } pendingFailures.set(key, turnSequence); } const batch = event.toolResults.filter( (result) => result.toolName !== "angel" && result.toolName !== "devil", ); const failed = event.outcome !== "completed" || batch.some( (result) => result.isError || result.nestedCalls?.calls.some((call) => call.status !== "ok"), ); if (failed) resetDevil(); const qualifying = batch.some( (result) => !["read", "ls", "find", "grep"].includes(result.toolName), ); const advisorBatch = batch.length !== event.toolResults.length; if (!failed && qualifying && !advisorBatch && !devilLatched) cleanBatches += 1; const autoDevil = repeatedFailures.length === 0 && !advisorBatch && !failed && !devilLatched && cleanBatches >= (settings.devilThreshold ?? 5); if (repeatedFailures.length === 0 && !autoDevil) return; for (const { key } of repeatedFailures) { pendingFailures.delete(key); consultedFailures.add(key); } const label = autoDevil ? "Devil" : "Angel"; const widgetKey = `dilemma-consultation-${generation}-${consultationSequence++}`; const progress = (update: ConsultationProgress) => { if ( !ctx.hasUI || !active || generation !== sessionGeneration || task !== taskGeneration ) return; showWorking(ctx, widgetKey, `${label} · ${update.message}`); const lines = [ ctx.ui.theme.fg( "accent", `${roleIcon(autoDevil ? "devil" : "angel")} ${autoDevil ? "Devil" : "Angel"}`, ), ctx.ui.theme.fg("dim", update.message), ]; ctx.ui.setWidget(widgetKey, () => { const container = new Container(); container.addChild(new Text(lines.join("\n"), 0, 0)); if (update.preview) container.addChild( streamingPreview(ctx.ui.theme.fg("toolOutput", update.preview)), ); // One column of padding on each side. return { render: (width: number) => container .render(Math.max(1, width - 2)) .map((line) => ` ${line}`), invalidate: () => container.invalidate(), }; }); }; try { const result = await run( ctx, { origin: autoDevil ? "success" : "error", role: autoDevil ? "devil" : "angel", question: autoDevil ? "Challenge the executor's apparent clean progress. Inspect pinned evidence for unsupported inference, overconfidence, and missing verification; identify only substantiated weaknesses and the next decisive check." : `Diagnose this completed tool batch containing ${repeatedFailures.length} operation${repeatedFailures.length === 1 ? "" : "s"} that failed again after an earlier attempt, then recommend the executor's next step.`, extraContext: completedBatchContext( event.toolResults, repeatedFailures.map(({ result }) => result), ), }, { signal: ctx.signal, onProgress: progress }, ); if ( !active || generation !== sessionGeneration || task !== taskGeneration ) return; pi.sendMessage( { customType: ADVICE_TYPE, content: result.advice, display: true, details: { version: 1, advice: result.advice, metadata: result.metadata, }, }, { deliverAs: "steer" }, ); } catch (error) { if ( !active || generation !== sessionGeneration || task !== taskGeneration ) return; if (isAbortError(error)) { pi.sendMessage( { customType: STATUS_TYPE, content: `${label} consultation cancelled.`, display: true, details: { version: 1, kind: "cancelled", role: autoDevil ? "devil" : "angel", message: `${label} cancelled`, }, }, { triggerTurn: false }, ); return; } const message = error instanceof Error ? error.message : String(error); const failure = `${label} consultation failed: ${message}`; pi.sendMessage( { customType: STATUS_TYPE, content: failure, display: true, details: { version: 1, kind: "failed", role: autoDevil ? "devil" : "angel", message, }, }, { deliverAs: "steer" }, ); } finally { showWorking(ctx, widgetKey, undefined); if (ctx.hasUI) ctx.ui.setWidget(widgetKey, undefined); } }); const advisorParameters = Type.Object({ question: Type.String({ minLength: 1, description: "Concrete question, uncertainty, decision, or competing hypotheses for the advisor to investigate", }), context: Type.Optional( Type.String({ description: "Concise context that makes the assignment more precise; do not copy the parent transcript", }), ), }); // Concurrent consultations share Pi's single working label; latest progress wins. const workingLabels = new Map(); function showWorking( ctx: ExtensionContext, id: string, label: string | undefined, ): void { if (!ctx.hasUI) return; if (label === undefined) workingLabels.delete(id); else workingLabels.set(id, label); ctx.ui.setWorkingMessage([...workingLabels.values()].at(-1)); } function advisorTool( role: "angel" | "devil", ): ToolDefinition { const label = role === "angel" ? "Angel" : "Devil"; return { name: role, label, exposure: "model-only", annotations: { readOnlyHint: true }, description: role === "angel" ? TOOL_DESCRIPTION : "Consult Devil for blunt, adversarial, evidence-based critique of an overconfident executor: unsupported inference, weak verification, unjustified conclusions. Same read-only investigative access as Angel. No manufactured objections or personal insults.", promptSnippet: "Investigate difficult questions independently and return evidence-backed advice", promptGuidelines: role === "angel" ? TOOL_GUIDELINES : [ "Call devil to attack unsupported conclusions and verification gaps, not for reassurance or automatic debate with Angel.", "Give devil a concrete claim to challenge; evaluate criticism against primary evidence.", ], parameters: advisorParameters, renderCall(args, theme, context) { const question = typeof args.question === "string" ? args.question : ""; const extraContext = typeof args.context === "string" ? args.context : ""; let output = theme.fg( "toolTitle", theme.bold(`${roleIcon(role)} ${role}`), ); const summary = questionSummary(question); if (!context.expanded && summary) output += ` ${theme.fg("dim", summary)}`; if (context.expanded) { if (question) output += `\n${theme.fg("dim", "question: ")}${theme.fg("toolOutput", question)}`; if (extraContext) output += `\n${theme.fg("dim", "context: ")}${theme.fg("toolOutput", extraContext)}`; } else { output += theme.fg( "dim", ` (${context.keyHint?.("app.tools.expand", "to expand") ?? keyHint("app.tools.expand", "to expand")})`, ); } return new Text(output, 0, 0); }, renderResult(result, { expanded, isPartial }, theme, context) { const details = result.details as ToolDetails | undefined; if (isPartial || details?.kind === "progress") { const progress = details?.progress; const output = theme.fg( "accent", `${roleIcon(role)} ${progress?.message ?? `Consulting ${label}`}`, ); const container = new Container(); container.addChild(new Text(output, 0, 0)); if (progress?.preview) { container.addChild(new Text("", 0, 0)); container.addChild( streamingPreview( theme.fg("toolOutput", progress.preview), expanded, ), ); } return container; } const isError = context?.isError ?? result.isError === true; if (isError || !isCompleteToolDetails(details)) { const cancellation = isError && isExplicitCancellation({ content: result.content, details: result.details, isError, }); const message = cancellation ? `${label} cancelled` : isError ? `${label} failed` : `${label} result unavailable`; let output = theme.fg( cancellation ? "muted" : isError ? "error" : "dim", `${roleIcon(role)} ${message}`, ); const messages = result.content .filter((part) => part.type === "text" && part.text.trim()) .map((part) => (part.type === "text" ? part.text : "")); const firstLine = messages .flatMap((text) => text.split(/\r?\n/u)) .map((line) => line.trim()) .find(Boolean); if (!expanded && firstLine) output += ` · ${theme.fg("toolOutput", firstLine)}`; if (expanded && messages.length > 0) output += `\n${theme.fg("toolOutput", messages.join("\n"))}`; return new Text(output, 0, 0); } const metadata = details.result.metadata; let output = theme.fg("success", `${roleIcon(role)} Advice ready`); output += theme.fg( "dim", ` · ${metadata.tokens.total.toLocaleString("en-US")} tokens`, ); if (expanded) { output += `\n${theme.fg("dim", metadataDetailLines(metadata).join("\n"))}`; output += `\n${theme.fg("dim", metadata.childSessionFile)}`; } return new Text(output, 0, 0); }, async execute(id, params, signal, onUpdate, ctx) { showWorking(ctx, id, `${label} · consulting`); let result: ConsultationResult; try { result = await run( ctx, { origin: "executor", role, question: params.question, extraContext: params.context, }, { signal, onProgress: (progress) => { showWorking(ctx, id, `${label} · ${progress.message}`); onUpdate?.({ content: [{ type: "text", text: progress.message }], details: { kind: "progress", progress }, }); }, }, ); } finally { showWorking(ctx, id, undefined); } return { content: [{ type: "text", text: boundedAdvice(result) }], details: { kind: "complete", result } satisfies ToolDetails, }; }, }; } pi.registerTool(advisorTool("angel")); pi.registerTool(advisorTool("devil")); function advisorCommand( role: "angel" | "devil", ): Parameters[1] { const label = role === "angel" ? "Angel" : "Devil"; return { description: `Ask ${label} a question directly, or enable/disable both advisors for this session`, getArgumentCompletions: (prefix): AutocompleteItem[] | null => { const items: AutocompleteItem[] = [ { value: "on", label: "on" }, { value: "off", label: "off" }, { value: "cancel", label: "cancel" }, ]; const filtered = items.filter((item) => item.value.startsWith(prefix.toLowerCase()), ); return filtered.length > 0 ? filtered : null; }, handler: async (args, ctx) => { const question = args.trim(); const control = question.toLowerCase(); if (control === "cancel") { invalidateTask(); cancelConsultations(); if (ctx.hasUI) ctx.ui.notify("Dilemma consultations cancelled", "info"); return; } if (control === "on" || control === "off") { sessionEnabled = control === "on"; invalidateTask(); cancelConsultations(); syncAvailability(ctx); if (ctx.hasUI) { const available = pi.getActiveTools().includes("angel"); const message = !sessionEnabled ? "Dilemma disabled" : available ? "Dilemma enabled" : "Dilemma remains unavailable for the current session and model"; ctx.ui.notify( message, available || !sessionEnabled ? "info" : "warning", ); } return; } if (!question) { if (ctx.hasUI) ctx.ui.notify( "Usage: /dilemma |on|off|cancel; /devil ", "error", ); return; } const generation = sessionGeneration; const cancellation = cancellationGeneration; await ctx.waitForIdle(); if ( !active || generation !== sessionGeneration || cancellation !== cancellationGeneration ) return; const parentLeafId = ctx.sessionManager.getLeafId(); if (!enabled()) { if (ctx.hasUI) ctx.ui.notify(`${label} is disabled`, "error"); return; } const execute = async ( signal: AbortSignal | undefined, onProgress?: (progress: ConsultationProgress) => void, ) => run( ctx, { origin: "human", role, question }, { signal, onProgress }, ); let outcome: | { result: ConsultationResult } | { error: unknown } | undefined; if (ctx.mode === "tui") { outcome = await ctx.ui.custom((tui, theme, _keybindings, done) => { const controller = new AbortController(); const container = new Container(); const borderColor = (text: string) => theme.fg("accent", text); const loader = new Loader( tui, (text) => theme.fg("accent", text), (text) => theme.fg("muted", text), `Starting ${label}`, ); const preview = streamingPreview(""); container.addChild(new DynamicBorder(borderColor)); container.addChild(loader); container.addChild(preview); container.addChild(new Text(theme.fg("dim", "esc cancel"), 1, 0)); container.addChild(new DynamicBorder(borderColor)); loader.start(); execute(controller.signal, (progress) => { loader.setMessage(progress.message); preview.setText( progress.preview ? theme.fg("dim", progress.preview) : "", ); tui.requestRender(); }) .then((result) => done({ result })) .catch((error: unknown) => done({ error })); return { render: (width) => container.render(width), invalidate: () => container.invalidate(), handleInput: (data) => { if (matchesKey(data, "escape")) controller.abort(); }, dispose: () => { loader.stop(); controller.abort(); }, }; }); } else { try { outcome = { result: await execute(undefined) }; } catch (error) { outcome = { error }; } } if ( !active || generation !== sessionGeneration || cancellation !== cancellationGeneration || parentLeafId !== ctx.sessionManager.getLeafId() ) return; if (!outcome || ("error" in outcome && isAbortError(outcome.error))) { if (ctx.mode === "print") process.stdout.write(`${label} consultation cancelled.\n`); pi.sendMessage( { customType: STATUS_TYPE, content: `${label} consultation cancelled.`, display: true, details: { version: 1, kind: "cancelled", role, message: `${label} cancelled`, }, }, { triggerTurn: false }, ); return; } if ("error" in outcome) { const message = outcome.error instanceof Error ? outcome.error.message : String(outcome.error); const failure = `${label} consultation failed: ${message}`; if (ctx.mode === "print") process.stdout.write(`${failure}\n`); pi.sendMessage( { customType: STATUS_TYPE, content: failure, display: true, details: { version: 1, kind: "failed", role, message }, }, { triggerTurn: false }, ); return; } const result = outcome.result; if (ctx.mode === "print") process.stdout.write(`${result.advice}\n`); pi.sendMessage( { customType: ADVICE_TYPE, content: result.advice, display: true, details: { version: 1, advice: result.advice, metadata: result.metadata, }, }, { triggerTurn: false }, ); }, }; } pi.registerCommand("dilemma", { ...advisorCommand("angel"), description: "Ask Angel a question; on/off/cancel controls both advisors", }); pi.registerCommand("devil", { ...advisorCommand("devil"), description: "Ask Devil for evidence-based adversarial critique", }); }; } const dilemmaExtensionFactory: ExtensionFactory = createDilemmaExtension(); export default dilemmaExtensionFactory; export const __test: { ADVICE_TYPE: typeof ADVICE_TYPE; STATUS_TYPE: typeof STATUS_TYPE; TOOL_DESCRIPTION: typeof TOOL_DESCRIPTION; TOOL_GUIDELINES: typeof TOOL_GUIDELINES; boundedAdvice: typeof boundedAdvice; resolveBinding: typeof resolveBinding; resolveSubagentExtensions: typeof resolveSubagentExtensions; } = { ADVICE_TYPE, STATUS_TYPE, TOOL_DESCRIPTION, TOOL_GUIDELINES, boundedAdvice, resolveBinding, resolveSubagentExtensions, };