// ultra runtime: loaded only when the human invokes or completes /ultra. import { dirname, resolve } from "node:path"; import type { JsonObject } from "@earendil-works/pi-ai"; import { createAgentSession, DefaultPackageManager, DefaultResourceLoader, type ExtensionAPI, type ExtensionCommandContext, type ExtensionContext, getAgentDir, parseArgs, SessionManager, SettingsManager, } from "@earendil-works/pi-coding-agent"; import { Type } from "typebox"; import { createActionSummarizer } from "./action-summarizer.ts"; import { renderUltraCall } from "./call.ts"; import { formatUsage } from "./display.ts"; import { BUILTIN_TOOL_NAMES, extensionLabel, loadSubagentFlagCatalog, makeResolveModel, makeSessionFactory, makeSubagentSessionManager, type SessionFactoryDeps, type SubagentFlagEntry, subagentFlagPrompt, } from "./drivers.ts"; import type { DynamicExtensionResolver } from "./dynamic-types.ts"; import { runWorkflow, type WorkflowResult } from "./engine.ts"; import { hasInterpolationToken } from "./interp.ts"; import { createSessionWorkflowJournal, type SessionWorkflowJournal, } from "./journal.ts"; import { initialReducerState } from "./progress.ts"; import { type BaseStepOptions, makeStepRunner, type RunnerContext, resolveModelTier, type StepDrivers, type StepRunner, } from "./runner.ts"; import { effectiveModelTiers, loadUltraSettings, type UltraSettings, } from "./settings.ts"; import { DYNAMIC_EXTENSION_TOOL_NAMES, discoverWorkflowCatalog, missingWorkflowTools, type WorkflowCatalog, type WorkflowSpec, WorkflowSpecSchema, workflowUsesDynamicExtensions, } from "./spec.ts"; import { span } from "./src/debug.ts"; import { reviewTimeout, WorkflowReview } from "./supervision.ts"; import { renderUltraResult, UltraOverlayComponent } from "./ui.ts"; import { type AutocompleteItem, compactToolPresentation, completeUltraCommand, formatWorkflowToolResult, type makeToolProgressSink, parseCommandLine, parseUltraCommand, resolveWorkflowInput, workflowExecutionStatus, } from "./wire.ts"; export interface UltraRuntime { activateWorkflowTool(): void; subagentFlagSection(ctx: ExtensionContext): Promise; getArgumentCompletions( prefix: string, projectTrusted: boolean, ): AutocompleteItem[] | null; handleCommand( argString: string, ctx: ExtensionCommandContext, ): Promise< | "completed" | "incomplete" | "aborted" | "disabled" | "invalid" | "failed" | "skipped" >; } /** Pi's initial built-in selection when no `tools` allowlist or `defaultTools` setting applies. */ const PI_DEFAULT_ACTIVE_TOOLS: ReadonlySet = new Set([ "read", "bash", "edit", "write", ]); interface SubagentExtensionSelection { all: boolean; paths: string[]; pathKeys: Set; } function pathKey(path: string): string { const absolute = resolve(path).replaceAll("\\", "/"); return process.platform === "win32" ? absolute.toLowerCase() : absolute; } function notifyWorkflowCompletion( ctx: ExtensionContext, result: WorkflowResult, ): void { if (!ctx.hasUI) return; const dropped = result.phases.reduce( (count, phase) => count + phase.dropped, 0, ); const status = result.aborted ? "aborted" : dropped > 0 ? "incomplete" : "complete"; const usage = formatUsage(result.tokenUsage).summary; ctx.ui.notify( `ultra: "${result.workflow}" ${status} · ${result.phases.length} phases · ${usage}.`, status === "complete" ? "info" : "warning", ); } export async function resolveSubagentExtensions( setting: UltraSettings["subagentExtensions"], options: { cwd: string; agentDir: string; projectTrusted: boolean; }, ): Promise { if (setting === "all") return { all: true, paths: [], pathKeys: new Set() }; if (setting === "none") return { all: false, paths: [], pathKeys: new Set() }; const settingsManager = SettingsManager.create( options.cwd, options.agentDir, { projectTrusted: options.projectTrusted, }, ); const packages = new DefaultPackageManager({ cwd: options.cwd, agentDir: options.agentDir, settingsManager, }); const paths = new Set(); for (const source of setting) { const resolved = await packages.resolveExtensionSources([source], { temporary: true, }); const enabled = resolved.extensions.filter( (extension) => extension.enabled, ); if (!enabled.length) { if (/^[a-z0-9][a-z0-9-]*$/u.test(source)) throw new Error( `ultra: subagentExtensions no longer accepts bare extension nickname "${source}"; replace it with a Pi extension source path or package source.`, ); throw new Error( `ultra: no enabled extensions resolved from ${JSON.stringify(source)}`, ); } for (const extension of enabled) paths.add(extension.path); } return { all: false, paths: [...paths], pathKeys: new Set([...paths].map(pathKey)), }; } export function createUltraRuntime(pi: ExtensionAPI): UltraRuntime { let closed = false; let preparing = 0; const journalOwners = new Map void }>(); type OwnershipToken = { owner: string; anchor: string | undefined }; type LiveRun = { kind: "live"; review: WorkflowReview; isOwned: () => boolean; ownership: () => OwnershipToken; }; type TerminalReceipt = { kind: "terminal"; status: "finished" | "incomplete" | "aborted"; path?: string; ownership: OwnershipToken; }; const retained = new Map(); const owns = (ctx: ExtensionContext, token: OwnershipToken) => !closed && ctx.sessionManager.getSessionId() === token.owner && (!token.anchor || ctx.sessionManager .getBranch() .some((entry) => entry.id === token.anchor)); const storeReceipt = (runId: string, receipt: TerminalReceipt) => { retained.delete(runId); retained.set(runId, receipt); let receipts = 0; for (const entry of retained.values()) if (entry.kind === "terminal") receipts++; while (receipts > 64) for (const [id, entry] of retained) if (entry.kind === "terminal") { retained.delete(id); receipts--; break; } }; const pruneRetained = (ctx: ExtensionContext) => { for (const [runId, entry] of retained) if (entry.kind === "live") { // Keep capacity reserved until foreign work actually settles. if (!entry.isOwned()) { entry.review.stop(); if (entry.review.isSettled) retained.delete(runId); } } else if (!owns(ctx, entry.ownership)) retained.delete(runId); }; pi.on("session_shutdown", () => { closed = true; for (const entry of retained.values()) if (entry.kind === "live") entry.review.stop(); retained.clear(); }); pi.on("session_tree", (_event, ctx) => pruneRetained(ctx)); const settings = (ctx: ExtensionContext): UltraSettings => { const cfg = loadUltraSettings(pi, ctx); return { ...cfg, modelTiers: effectiveModelTiers(cfg, ctx.model?.provider), }; }; // Workflow files are tiny and human-authored during live sessions. Rescan at // each use so a newly saved workflow immediately works without `/reload`. // Project workflows are repo-controlled: an untrusted project never // contributes runnable workflows (see `discoverWorkflowCatalog`). const discover = (cwd: string, projectTrusted: boolean): WorkflowCatalog => discoverWorkflowCatalog(cwd, projectTrusted); const activateWorkflowTool = () => { const active = pi.getActiveTools(); if (!active.includes("run_workflow")) { pi.setActiveTools([...active, "run_workflow"]); } }; const deactivateWorkflowTool = () => { const active = pi.getActiveTools(); if (active.includes("run_workflow")) { pi.setActiveTools(active.filter((name) => name !== "run_workflow")); } }; const availableSubagentTools = ( extensions: SubagentExtensionSelection, ): string[] => pi .getAllTools() .filter( (tool) => tool.name !== "run_workflow" && (extensions.all || BUILTIN_TOOL_NAMES.has(tool.name) || extensions.pathKeys.has(pathKey(tool.sourceInfo.path))), ) .map((tool) => tool.name); // One extension load per cwd and selection; children load the same set per step. const flagCatalogs = new Map< string, ReturnType >(); const subagentFlagCatalog = async (ctx: ExtensionContext) => { const extensions = await resolveSubagentExtensions( settings(ctx).subagentExtensions, { cwd: ctx.cwd, agentDir: getAgentDir(), projectTrusted: ctx.isProjectTrusted(), }, ); const key = JSON.stringify([ctx.cwd, extensions.all, extensions.paths]); let catalog = flagCatalogs.get(key); if (!catalog) { catalog = loadSubagentFlagCatalog({ DefaultResourceLoader: DefaultResourceLoader as unknown as SessionFactoryDeps["DefaultResourceLoader"], getAgentDir, cwd: ctx.cwd, loadExtensions: extensions.all, extensionPaths: extensions.paths, }); flagCatalogs.set(key, catalog); // A failed load is retried on the next turn instead of being cached. catalog.catch(() => flagCatalogs.delete(key)); } return catalog; }; // Children inherit this Pi's CLI flags, so a tool an extension switched off // here at startup is switched off in every child too. Refuse before creating // any child unless the step sets its own flags (then the child-startup check // in drivers.ts is the authority). Pi activates its default builtins and // every extension tool at startup; the remaining builtins (grep, find, ls, // powershell) are inactive by default and say nothing about extensions, and // children activate them explicitly. const inactiveToolsError = async ( ctx: ExtensionContext, spec: WorkflowSpec, ): Promise => { const all = new Set(pi.getAllTools().map((tool) => tool.name)); const active = new Set(pi.getActiveTools()); const activeByDefault = (name: string) => !BUILTIN_TOOL_NAMES.has(name) || PI_DEFAULT_ACTIVE_TOOLS.has(name); const inactive = [ ...new Set( spec.phases .filter((phase) => phase.step.flags === undefined) .flatMap((phase) => phase.step.tools ?? []) .filter( (name) => name !== "run_workflow" && all.has(name) && !active.has(name) && activeByDefault(name), ), ), ]; if (inactive.length === 0) return undefined; const catalog = await Promise.resolve() .then(() => subagentFlagCatalog(ctx)) .catch((): SubagentFlagEntry[] => []); const candidates = catalog .filter((entry) => entry.flags.length > 0) .map( (entry) => `${entry.extension} (${entry.flags .map( (flag) => `--${flag.name}${flag.description ? `: ${flag.description}` : ""}`, ) .join(", ")})`, ); const shellInactive = inactive.some( (name) => name === "bash" || name === "powershell", ); return [ `ultra: step tools are inactive in this Pi and children inherit its flags: ${inactive.join(", ")}.`, ...(candidates.length ? [ `Possibly deactivated by an extension with startup flags: ${candidates.join("; ")}.`, ] : []), "Use an active alternative or set step.flags.", ...(shellInactive && active.has("nu") ? ["The nu tool is active; prefer it."] : []), ].join(" "); }; const workflowError = async ( ctx: ExtensionContext, spec: WorkflowSpec, extensions: SubagentExtensionSelection, cfg: UltraSettings, ): Promise => { const cwd = ctx.cwd; try { for (const phase of spec.phases) { if (phase.step.model && hasInterpolationToken(phase.step.model)) continue; resolveModelTier(phase.step, cfg.modelTiers); } } catch (error) { return error instanceof Error ? error.message : String(error); } const available = [ ...availableSubagentTools(extensions), ...DYNAMIC_EXTENSION_TOOL_NAMES, ]; const missing = missingWorkflowTools(spec, available); if (missing.length === 0) return inactiveToolsError(ctx, spec); const owners = pi .getAllTools() .filter((tool) => missing.includes(tool.name)); const selectedEntriesForOwner = (ownerPath: string): string[] => extensions.paths.filter( (path) => pathKey(path) !== pathKey(ownerPath) && pathKey(dirname(path)) === pathKey(dirname(ownerPath)), ); const inspectionPaths = new Set(); const details = [ `ultra: workflow "${spec.name}" requires unavailable tools: ${missing.join(", ")}`, ...missing.map((name) => { const owner = owners.find((tool) => tool.name === name); if (!owner) return `${name}: no registered owning extension; check step.tools and installed extensions.`; const ownerPath = owner.sourceInfo.path; const selectedEntries = selectedEntriesForOwner(ownerPath); if (selectedEntries.length) { for (const path of selectedEntries) inspectionPaths.add(path); return `${name}: loaded owning extension "${extensionLabel(ownerPath)}" at ${JSON.stringify(ownerPath)} does not match selected extension entries ${selectedEntries.map((path) => JSON.stringify(path)).join(", ")}; runtime may be stale or explicit selection differs. Reload/restart Pi and verify the selected entry; do not blindly add the obsolete loaded path.`; } inspectionPaths.add(ownerPath); const label = extensionLabel(ownerPath); return `${name}: owning extension "${label}" is excluded by ultra.subagentExtensions; include ${JSON.stringify(ownerPath)} in that setting before using its step.flags.`; }), ]; if (inspectionPaths.size > 0) { try { const catalog = await loadSubagentFlagCatalog({ DefaultResourceLoader: DefaultResourceLoader as unknown as SessionFactoryDeps["DefaultResourceLoader"], getAgentDir, cwd, extensionPaths: [...inspectionPaths], }); if (catalog.length) details.push(subagentFlagPrompt(catalog)); } catch (error) { details.push( `Could not inspect startup flags: ${error instanceof Error ? error.message : String(error)}`, ); } } return details.join("\n"); }; const createJournal = ( ctx: ExtensionContext, spec: WorkflowSpec, cfg: UltraSettings, args?: Record, ) => { const owner = ctx.sessionManager.getSessionId(); let anchor = ctx.sessionManager.getBranch().at(-1)?.id; let invalidated = false; const isOwned = () => !invalidated && !closed && ctx.sessionManager.getSessionId() === owner && (!anchor || ctx.sessionManager.getBranch().some((entry) => entry.id === anchor)); const journal = createSessionWorkflowJournal({ cwd: ctx.cwd, spec, args, modelRouting: { inheritedModel: spec.phases.some( ({ step }) => step.model && hasInterpolationToken(step.model), ) ? ctx.model ? `${ctx.model.provider}/${ctx.model.id}` : undefined : undefined, tiers: cfg.modelTiers, phases: spec.phases.map(({ step }) => { if (step.model && hasInterpolationToken(step.model)) return { modelTemplate: step.model, thinkingLevel: step.thinkingLevel, }; const resolved = resolveModelTier(step, cfg.modelTiers); return { model: resolved.model ?? (ctx.model ? `${ctx.model.provider}/${ctx.model.id}` : undefined), thinkingLevel: resolved.thinkingLevel, }; }), }, entries: ctx.sessionManager.getBranch(), appendEntry: (customType, data) => { if (!isOwned()) { invalidated = true; journalOwners.get(`${owner}:${journal.id}`)?.stop?.(); return; } pi.appendEntry(customType, data); anchor = ctx.sessionManager.getBranch().at(-1)?.id; }, }); return Object.assign(journal, { isOwned, ownership: (): OwnershipToken => ({ owner, anchor }), claim() { // An empty root has no entry to fence a later branch switch against. if (!anchor) { pi.appendEntry("ultra-run-owner", { id: journal.id }); anchor = ctx.sessionManager.getBranch().at(-1)?.id; } }, }); }; // Assemble a real `StepRunner` from the injected SDK drivers + this ctx. const buildStepRunner = ( ctx: ExtensionContext, spec: WorkflowSpec, cfg: UltraSettings, journal: SessionWorkflowJournal, extensions: SubagentExtensionSelection, dynamicExtensions?: DynamicExtensionResolver, ): StepRunner => { // The injected-dep types are deliberately broader than the SDK's specific // signatures (so the factories unit-test with simple fakes); the real SDK // values are adapted to them here at the boundary. Runtime correctness of // these calls is the Task-10 live gate. const drivers: StepDrivers = { sessionFactory: makeSessionFactory({ createAgentSession: createAgentSession as unknown as SessionFactoryDeps["createAgentSession"], DefaultResourceLoader: DefaultResourceLoader as unknown as SessionFactoryDeps["DefaultResourceLoader"], getAgentDir, cwd: ctx.cwd, loadExtensions: extensions.all, extensionPaths: extensions.paths, loadSkills: cfg.subagentSkills === "all", // Children inherit the parent's CLI extension flags, parsed exactly as Pi parses them. inheritedFlags: parseArgs(process.argv.slice(2)).unknownFlags, }), resolveModel: makeResolveModel({ modelRegistry: ctx.modelRegistry, defaultModel: ctx.model, }), }; const manageSubagentSession = makeSubagentSessionManager({ SessionManager, cwd: ctx.cwd, parentSession: ctx.sessionManager.getSessionFile(), sessionDir: ctx.sessionManager.getSessionDir(), retention: cfg.subagentSessionRetention, }); const ctxLike: RunnerContext = { newSessionManager(sessionKey, agentId, fresh) { const retained = fresh ? undefined : journal.getSession(sessionKey); const child = manageSubagentSession({ name: `ultra ${spec.name} ${journal.id.slice(0, 8)} ${agentId}`, retained, }); if ( child.ref && (!retained || child.ref.id !== retained.id || child.ref.path !== retained.path) ) journal.setSession(sessionKey, child.ref); return child; }, }; const baseOpts: BaseStepOptions = { maxRetries: cfg.maxRetries, schemas: spec.schemas, modelTiers: cfg.modelTiers, actionSummarizer: createActionSummarizer(ctx, cfg.actionSummarizerModel), dynamicExtensionResolver: dynamicExtensions, }; return makeStepRunner(drivers, ctxLike, baseOpts); }; const dynamicResolver = async ( ctx: ExtensionContext, spec: WorkflowSpec, journal: SessionWorkflowJournal, ): Promise => { if (!workflowUsesDynamicExtensions(spec)) return undefined; const { createDynamicExtensionCoordinator } = await import( "./dynamic-catalog.ts" ); return createDynamicExtensionCoordinator({ agentDir: getAgentDir(), parentSessionId: ctx.sessionManager.getSessionId(), workflowRunId: journal.id, }); }; const claimJournalOwner = ( ctx: ExtensionContext, journal: ReturnType, owner: { id: string; stop?: () => void }, ) => { const key = `${ctx.sessionManager.getSessionId()}:${journal.id}`; const existing = journalOwners.get(key); if (existing) throw new Error( `ultra: journal already owned; control existing runId ${existing.id}`, ); journalOwners.set(key, owner); return () => { if (journalOwners.get(key) === owner) journalOwners.delete(key); }; }; const prepareRun = async ( ctx: ExtensionContext, spec: WorkflowSpec, cfg: UltraSettings, args: Record | undefined, extensions: SubagentExtensionSelection, fresh = false, journal = createJournal(ctx, spec, cfg, args), ): Promise<{ journal: ReturnType; dynamicExtensions?: DynamicExtensionResolver | undefined; stepRunner: StepRunner; }> => { journal.claim(); const dynamicExtensions = await dynamicResolver(ctx, spec, journal); if (fresh) journal.reset(); return { journal, dynamicExtensions, stepRunner: buildStepRunner( ctx, spec, cfg, journal, extensions, dynamicExtensions, ), }; }; const reviewResult = async ( review: WorkflowReview, timeoutMs: number, signal: AbortSignal | undefined, onUpdate: Parameters[0] | undefined, ctx: ExtensionContext, ownership: () => OwnershipToken, ) => { const progress = review.display; try { const result = await review.wait(timeoutMs, signal, onUpdate); if (!result) { const envelope = review.snapshot(); return { content: [{ type: "text" as const, text: JSON.stringify(envelope) }], structuredContent: { ...envelope }, details: { // Leave room for the bounded review envelope in Pi's 50KB details. ...progress.getState(16_000), review: envelope, reviewTimeoutMs: timeoutMs, reviewObservedAt: Date.now(), }, terminate: false, }; } const receipt: TerminalReceipt = { kind: "terminal", status: workflowExecutionStatus(result), ownership: ownership(), }; // Publish terminal metadata before formatter I/O, so polling never sees a gap. if (owns(ctx, receipt.ownership)) storeReceipt(review.id, receipt); const output = await formatWorkflowToolResult(result, review.id); if (retained.get(review.id) === receipt && owns(ctx, receipt.ownership)) receipt.path = output.fullOutputPath; notifyWorkflowCompletion(ctx, result); return { content: [{ type: "text" as const, text: output.text }], structuredContent: JSON.parse(output.text) as JsonObject, details: progress.finish({ ...result, fullOutputPath: output.fullOutputPath, }), terminate: false, }; } catch (error) { if (retained.get(review.id)?.kind === "live") retained.delete(review.id); review.stop(); throw error; } }; // ── `run_workflow` tool (model-callable, mid-turn, read-only onUpdate surface) ── pi.registerTool({ name: "run_workflow", label: "run_workflow", exposure: "direct", annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: true, }, outputSchema: Type.Object( { executionStatus: Type.Union( [ "finished", "incomplete", "aborted", "disabled", "running", "pause-requested", "paused", "cancelling", ].map((status) => Type.Literal(status)), ), runId: Type.Optional(Type.String()), message: Type.Optional(Type.String()), agents: Type.Optional( Type.Array( Type.Object({ agentId: Type.String(), status: Type.String() }), { maxItems: 100 }, ), ), agentsTruncated: Type.Optional(Type.Boolean()), childSessions: Type.Optional( Type.Array( Type.Object({ agentId: Type.String(), id: Type.String(), path: Type.Optional(Type.String()), }), ), ), childSessionsTruncated: Type.Optional(Type.Boolean()), availableActions: Type.Optional(Type.Array(Type.String())), fullOutputPath: Type.Optional(Type.String()), truncated: Type.Optional(Type.Boolean()), workflow: Type.Optional(Type.String()), result: Type.Optional(Type.Unknown()), phases: Type.Optional(Type.Array(Type.Unknown())), phaseFailures: Type.Optional( Type.Record(Type.String(), Type.Unknown()), ), }, { additionalProperties: true }, ), description: "Run inline spec or saved name; spec wins. Return bounded structured results/failures/usage; fullOutputPath holds complete evidence and childSessions locators. Saved child paths enable on-demand inspection with pi-sessions/read; absent path means no retained transcript. " + "Pass runId alone for read-only status without waiting or control. Control live runId with resume|retry|cancel, without name/spec/args/fresh. Retry requires agentId; terminal runs require a new workflow. " + "Timeout requests pause, not abort; active work may continue. Controls are session/branch-local; branch departure requests cancellation, reload loses controls. Cancellation may remain pending; no process suspension/background guarantee. At codemode worker capacity, call controls directly.", promptSnippet: "run saved or inline multi-agent workflows.", promptGuidelines: [ "Use ultra-authoring to delegate when isolation or independent judgment outweighs handoff cost.", "Prefer shared dynamic capabilities for repeated domain operations; ordinary tools for one-offs.", ], parameters: Type.Object({ timeoutMs: Type.Optional( Type.Integer({ minimum: 1, maximum: 2147483647, default: 3600000, description: "Milliseconds until cooperative pause request; default 60 minutes, not cancellation.", }), ), runId: Type.Optional(Type.String({ minLength: 1 })), action: Type.Optional( Type.Union([ Type.Literal("resume"), Type.Literal("retry"), Type.Literal("cancel"), ]), ), agentId: Type.Optional(Type.String({ minLength: 1 })), name: Type.Optional( Type.String({ description: "Saved workflow name (project/global/bundled).", }), ), spec: Type.Optional(WorkflowSpecSchema), fresh: Type.Optional( Type.Boolean({ default: false, description: "Reset checkpoints/child references; preserve historical files and effects.", }), ), args: Type.Optional( Type.Record(Type.String(), Type.Unknown(), { description: "Workflow inputs, available as {args.NAME}.", }), ), }), renderCall: renderUltraCall, renderResult: renderUltraResult, async execute(_toolCallId, params, signal, onUpdate, ctx) { const finish = span?.("workflow.execute"); try { const cfg = settings(ctx); if (!cfg.enabled) { finish?.("finish", { outcome: "disabled" }); return { content: [ { type: "text", text: JSON.stringify({ executionStatus: "disabled", message: "ultra is disabled (ultra.enabled = false).", }), }, ], structuredContent: { executionStatus: "disabled", message: "ultra is disabled (ultra.enabled = false).", }, details: initialReducerState(), }; } const timeoutMs = reviewTimeout(params.timeoutMs); const hasRunId = params.runId !== undefined; const hasAction = params.action !== undefined; if (params.agentId !== undefined && !hasAction) throw new Error("ultra: agentId requires action"); if (hasRunId || hasAction) { if ( params.name !== undefined || params.spec !== undefined || params.args !== undefined || params.fresh !== undefined ) throw new Error( "ultra: status/control cannot include name, spec, args, or fresh", ); if (!params.runId) throw new Error("ultra: status/control requires runId"); const runId = params.runId; const entry = retained.get(runId); const owned = entry?.kind === "live" ? entry.isOwned() && owns(ctx, entry.ownership()) : entry !== undefined && owns(ctx, entry.ownership); if (!entry || !owned) { if (entry?.kind === "live") { if (!entry.isOwned()) { entry.review.stop(); if (entry.review.isSettled) retained.delete(runId); } } else if ( entry && ctx.sessionManager.getSessionId() === entry.ownership.owner ) retained.delete(runId); throw new Error("ultra: missing, foreign, or stale runId"); } if (!hasAction) { const envelope = entry.kind === "live" ? entry.review.snapshot() : { runId, executionStatus: entry.status, message: "Workflow has finished. Start a new workflow for another execution.", agents: [], agentsTruncated: false, availableActions: [], ...(entry.path ? { fullOutputPath: entry.path } : {}), }; finish?.("finish"); return { content: [ { type: "text" as const, text: JSON.stringify(envelope) }, ], structuredContent: envelope as unknown as JsonObject, details: { ...(entry.kind === "live" ? entry.review.display.getState(16_000) : initialReducerState()), review: envelope, reviewObservedAt: Date.now(), }, terminate: false, }; } const action = params.action; if (action !== "resume" && action !== "retry" && action !== "cancel") throw new Error("ultra: action must be resume, retry, or cancel"); if (entry.kind === "terminal") { if (action === "retry") throw new Error( "ultra: run finished/terminal; start a new workflow", ); throw new Error("ultra: stale runId; run has already settled"); } entry.review.control(action, params.agentId); const reviewed = await reviewResult( entry.review, action === "cancel" ? 0 : timeoutMs, signal, onUpdate, ctx, entry.ownership, ); finish?.("finish"); return reviewed; } if ( [...retained.values()].filter((entry) => entry.kind === "live") .length + preparing >= 16 ) throw new Error( "ultra: too many retained runs; cancel an existing run first", ); if (params.fresh !== undefined && typeof params.fresh !== "boolean") throw new Error("ultra: fresh must be a boolean"); if ( params.args !== undefined && (params.args === null || typeof params.args !== "object" || Array.isArray(params.args)) ) throw new Error("ultra: args must be an object"); const catalog = discover(ctx.cwd, ctx.isProjectTrusted()); const { spec, args } = resolveWorkflowInput( params, catalog.available, undefined, cfg.fanoutToolAllowlist, catalog.refused, ); const extensions = await resolveSubagentExtensions( cfg.subagentExtensions, { cwd: ctx.cwd, agentDir: getAgentDir(), projectTrusted: ctx.isProjectTrusted(), }, ); const error = await workflowError(ctx, spec, extensions, cfg); if (error) throw new Error(error); if (closed) throw new Error("ultra: parent session closed during setup"); if ( [...retained.values()].filter((entry) => entry.kind === "live") .length + preparing >= 16 ) throw new Error( "ultra: too many retained runs; cancel an existing run first", ); const journal = createJournal(ctx, spec, cfg, args); const review = new WorkflowReview({ workflowName: spec.name, workflowDescription: spec.description, }); const releaseJournalOwner = claimJournalOwner(ctx, journal, review); let prepared: Awaited>; preparing++; try { prepared = await prepareRun( ctx, spec, cfg, args, extensions, params.fresh, journal, ); } catch (error) { releaseJournalOwner(); throw error; } finally { preparing--; } if (closed) { releaseJournalOwner(); await prepared.stepRunner.dispose?.(); throw new Error("ultra: parent session closed during setup"); } retained.set(review.id, { kind: "live", review, isOwned: prepared.journal.isOwned, ownership: prepared.journal.ownership, }); review.start(() => runWorkflow(spec, args, { stepRunner: review.wrap(prepared.stepRunner), concurrency: cfg.concurrency, journal: prepared.journal, modelTiers: cfg.modelTiers, dynamicExtensions: prepared.dynamicExtensions, signal: review.controller.signal, boundary: () => review.boundary(), onSession: (session) => review.observeSession(session), onProgress: (event) => review.progress(event), }).finally(() => { releaseJournalOwner(); if ( !prepared.journal.isOwned() && retained.get(review.id)?.kind === "live" ) retained.delete(review.id); }), ); const reviewed = await reviewResult( review, timeoutMs, signal, onUpdate, ctx, prepared.journal.ownership, ); finish?.("finish"); return reviewed; } catch (error) { finish?.("error", { outcome: "failed" }); throw error; } }, }); deactivateWorkflowTool(); const sendExecPrompt = (request: string) => { activateWorkflowTool(); pi.sendUserMessage(`# ultra exec\n\n# user request\n\n${request}`); }; // ── `/ultra [exec | run]` command ── return { activateWorkflowTool, /** Undefined when no sub-agent extension owns both tools and flags, or loading failed. */ subagentFlagSection: async (ctx: ExtensionContext) => { const catalog = await subagentFlagCatalog(ctx).catch(() => []); return catalog.length ? subagentFlagPrompt(catalog) : undefined; }, getArgumentCompletions: (prefix: string, projectTrusted: boolean) => completeUltraCommand( discover(process.cwd(), projectTrusted).available, prefix, ), handleCommand: async (argString: string, ctx: ExtensionCommandContext) => { const cfg = settings(ctx); if (!cfg.enabled) { if (ctx.hasUI) ctx.ui.notify( "ultra is disabled (ultra.enabled = false).", "warning", ); return "disabled"; } // The command surface is the focus-grabbing overlay — interactive only. // Headless / non-UI callers use the `run_workflow` tool instead. if (!ctx.hasUI) { return "skipped"; } const command = parseUltraCommand(argString); const fresh = command.subcommand === "fresh"; if (command.subcommand === "activate") { activateWorkflowTool(); ctx.ui.notify("ultra: run_workflow tool enabled.", "info"); return "completed"; } if (command.subcommand === "exec") { sendExecPrompt(command.instructions); return "completed"; } if (command.subcommand === "invalid") { ctx.ui.notify( "Usage: /ultra | /ultra exec | /ultra run [args] | /ultra fresh [args]", "warning", ); return "invalid"; } const catalog = discover(ctx.cwd, ctx.isProjectTrusted()); const parsed = parseCommandLine(command.invocation); let resolved: { spec: WorkflowSpec; args?: Record | undefined; }; try { resolved = resolveWorkflowInput( { name: parsed.name, args: parsed.args }, catalog.available, parsed.rest, cfg.fanoutToolAllowlist, catalog.refused, ); } catch (err) { ctx.ui.notify( err instanceof Error ? err.message : String(err), "error", ); return "invalid"; } const { spec, args: runArgs } = resolved; let extensions: SubagentExtensionSelection; try { extensions = await resolveSubagentExtensions(cfg.subagentExtensions, { cwd: ctx.cwd, agentDir: getAgentDir(), projectTrusted: ctx.isProjectTrusted(), }); } catch (error) { ctx.ui.notify( error instanceof Error ? error.message : String(error), "error", ); return "failed"; } const error = await workflowError(ctx, spec, extensions, cfg); if (error) { ctx.ui.notify(error, "error"); return "invalid"; } const journal = createJournal(ctx, spec, cfg, runArgs); const releaseJournalOwner = claimJournalOwner(ctx, journal, { id: "command", }); let prepared: Awaited>; try { prepared = await prepareRun( ctx, spec, cfg, runArgs, extensions, fresh, journal, ); } catch (error) { releaseJournalOwner(); throw error; } // The command owns its AbortController (idle ⇒ ctx.signal is undefined). // Global Esc / per-agent abort fire it; the engine fans session.abort(). const controller = new AbortController(); const outcome = await ctx.ui .custom< | { status: "completed"; result: WorkflowResult } | { status: "aborted"; result?: WorkflowResult } | { status: "failed"; message: string } | null >( (tui, theme, keybindings, done) => { const overlay = new UltraOverlayComponent({ host: tui, theme, onRunAbort: () => controller.abort(), workflowName: spec.name, workflowDescription: spec.description, toolUi: tui, toolsExpanded: () => ctx.ui.getToolsExpanded(), hideToolPreviews: cfg.actionSummarizerModel !== null, isToolsExpandKey: (data) => keybindings.matches(data, "app.tools.expand"), onToolsExpand: () => ctx.ui.setToolsExpanded(!ctx.ui.getToolsExpanded()), cwd: ctx.cwd, }); // P3: return the Component SYNCHRONOUSLY; run the engine DETACHED, or // the overlay never renders. `done` resolves the custom() promise. void (async () => { try { const r = await runWorkflow(spec, runArgs, { stepRunner: prepared.stepRunner, concurrency: cfg.concurrency, journal: prepared.journal, modelTiers: cfg.modelTiers, dynamicExtensions: prepared.dynamicExtensions, signal: controller.signal, onProgress: (e) => overlay.handleProgress(e), }); done( controller.signal.aborted ? { status: "aborted", result: r } : { status: "completed", result: r }, ); } catch (error) { done( controller.signal.aborted ? { status: "aborted" } : { status: "failed", message: error instanceof Error ? error.message : String(error), }, ); } })(); return overlay; }, { overlay: true, overlayOptions: { anchor: "bottom-left", width: "100%", maxHeight: "100%", }, }, ) .finally(releaseJournalOwner); if (!outcome) { ctx.ui.notify(`ultra: "${spec.name}" aborted.`, "warning"); return "aborted"; } if (outcome.status === "failed") { ctx.ui.notify(outcome.message, "error"); return "failed"; } const result = outcome.result; if (!result) { ctx.ui.notify(`ultra: "${spec.name}" aborted.`, "warning"); return "aborted"; } const output = await formatWorkflowToolResult(result); pi.sendMessage({ customType: "ultra-result", content: output.text, display: true, details: compactToolPresentation( initialReducerState(), { ...result, fullOutputPath: output.fullOutputPath }, output.fullOutputPath, ), }); notifyWorkflowCompletion(ctx, result); return result.aborted ? "aborted" : result.phases.some((phase) => phase.dropped > 0) ? "incomplete" : "completed"; }, }; }