import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent"; import type { DispatchResult, Mode, Policy } from "../types.js"; import { formatExportPreview } from "./export-preview.js"; import { formatUsd, renderLiveStatus, renderStatus } from "./status-widget.js"; import type { UltraController } from "../controller/controller.js"; import { createConfigVersion, loadConfigVersion } from "../config/loader.js"; import { evaluateRunningExperiments, listExperiments, readExperiment, recordExperimentLifecycle, startExperiment, stopExperiment } from "../experiments/runtime.js"; import { applyRecommendation, loadRecommendation, recordRecommendationDecision } from "../experiments/recommendation-schema.js"; import { loadRoleDefinitions } from "../workflows/role-definition.js"; import { loadPlaybooks } from "../workflows/playbook.js"; const MODES: readonly Mode[] = ["auto", "direct", "scout", "swarm", "deep", "warroom"]; const POLICIES: readonly Policy[] = ["economy", "balanced", "quality", "max"]; /** * The verbs and their arguments, for completion. `registerCommand` has always accepted a completer * and none was supplied, so around thirty subcommands had to be typed from memory. */ export const ULTRA_CONFIG_COMPLETIONS: ReadonlyArray<{ value: string; description: string }> = [ { value: "status", description: "active runs, provider health, estimated spend" }, { value: "status --live", description: "the same, expanded one line per delegated node" }, { value: "doctor", description: "profile, pins, MCP, price coverage, projection integrity" }, { value: "runs", description: "every run this session, with its nodes" }, { value: "trace ", description: " [raw|live|raw-live] — replay or stream one run" }, { value: "trace off", description: "stop streaming" }, { value: "recover", description: "list unfinished runs, or continue one by id" }, { value: "stop ", description: " — stop a run" }, { value: "steer ", description: " — send a bounded instruction" }, { value: "mode ", description: `<${MODES.join("|")}> — save the default topology` }, { value: "policy ", description: `<${POLICIES.join("|")}> — save how much one task may spend` }, { value: "budget", description: "show caps, or [daily] to set them" }, { value: "budget acknowledge", description: "allow exactly one task without a saved budget" }, { value: "feedback ", description: " [comment]" }, { value: "config show", description: "the active configuration" }, { value: "config rollback ", description: " — repoint the champion at an earlier config" }, { value: "playbook list", description: "project playbooks" }, { value: "playbook show ", description: " — one playbook in full" }, { value: "roles list", description: "project role definitions" }, { value: "roles show ", description: " — one role definition in full" }, { value: "experiment list", description: "champion/challenger experiments" }, { value: "experiment start ", description: " " }, { value: "experiment stop ", description: " — stop an experiment without promoting it" }, { value: "experiment promote ", description: " — make the challenger the champion" }, { value: "recommendation accept ", description: " [id]" }, { value: "recommendation reject ", description: " [id]" }, { value: "report", description: "write a sanitized analysis bundle for a week" }, { value: "export preview", description: "what a weekly export would contain" }, { value: "export week", description: "write the sanitized weekly export" }, ]; export function ultraConfigCompletions(prefix: string): Array<{ value: string; label: string; description: string }> { const typed = prefix.trimStart().toLowerCase(); return ULTRA_CONFIG_COMPLETIONS .filter((entry) => entry.value.toLowerCase().startsWith(typed)) .map((entry) => ({ value: entry.value, label: entry.value.trim(), description: entry.description })); } const HELP = "status [--live] | playbook list|show | roles list|show | report [YYYY-Www] | trace [raw|live|raw-live] | trace next raw-live | trace off | recover [run-id] | mode | policy | budget [weekly [daily]|acknowledge] | runs | stop | steer | feedback [comment] | recommendation accept|reject [id] | experiment list|start |stop |promote | export preview [YYYY-Www] | export week [YYYY-Www] | config show | config rollback | doctor"; function isoWeek(now = new Date()): string { const date = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())); date.setUTCDate(date.getUTCDate() + 4 - (date.getUTCDay() || 7)); const year = date.getUTCFullYear(); const start = new Date(Date.UTC(year, 0, 1)); return `${year}-W${String(Math.ceil((((date.getTime() - start.getTime()) / 86_400_000) + 1) / 7)).padStart(2, "0")}`; } async function show(ctx: ExtensionCommandContext, text: string, type: "info" | "warning" | "error" = "info"): Promise { ctx.ui.notify(text, type); } /** * A readable rendering of a nested value. These commands used to hand `JSON.stringify` output * straight to `notify`, which renders as one unwrapped line — `runs` had no indentation at all. */ export function describe(value: unknown, indent = ""): string { if (value === null || value === undefined) return "unset"; if (typeof value !== "object") return String(value); if (Array.isArray(value)) { return value.length ? value.map((entry) => `${indent}- ${describe(entry, `${indent} `).replace(/^\s+/, "")}`).join("\n") : "none"; } const entries = Object.entries(value as Record); if (!entries.length) return "none"; const width = Math.max(...entries.map(([key]) => key.length)); return entries.map(([key, entry]) => entry !== null && typeof entry === "object" ? `${indent}${key}\n${describe(entry, `${indent} `)}` : `${indent}${key.padEnd(width)} ${describe(entry, indent)}`).join("\n"); } export async function runUltraConfigCommand(args: string, ctx: ExtensionCommandContext, controller: UltraController, continueRecovered?: (result: DispatchResult) => void | Promise, setLiveTrace?: (runId?: string, notify?: (line: string) => void, mode?: "live" | "raw-live") => void): Promise { const [verb = "status", ...rest] = args.trim().split(/\s+/).filter(Boolean); if (verb === "status") { if (rest[0] === "--live") return await show(ctx, renderLiveStatus(controller)); if (rest.length) throw new Error("Usage: /ultra-config status [--live]"); return await show(ctx, renderStatus(controller)); } if (verb === "report") { if (rest.length > 1) throw new Error("Usage: /ultra-config report [YYYY-Www]"); const week = rest[0] ?? isoWeek(); const bundle = await controller.exportPreview(week); const approved = ctx.hasUI && await ctx.ui.confirm("Create sanitized UltraPi report", `Create the sanitized analysis dataset for ${week}? Raw vault data is excluded.`); if (!approved) throw new Error("Weekly report was not approved"); const output = await controller.exportWeek(week, true, bundle); return await show(ctx, `Report created: ${output}\nCanonical event log: ${output}/events.jsonl\nDataset manifest: ${output}/manifest.json`); } if (verb === "trace") { if (rest[0] === "off") { if (rest.length !== 1) throw new Error("Usage: /ultra-config trace off"); setLiveTrace?.(); return await show(ctx, "Live trace disabled."); } const [runId, mode] = rest; if (!runId || (mode !== undefined && mode !== "raw" && mode !== "live" && mode !== "raw-live") || (runId === "next" && mode !== "raw-live")) throw new Error("Usage: /ultra-config trace [raw|live|raw-live] | trace next raw-live"); if (mode === "raw-live") { if (!setLiveTrace) throw new Error("Live trace is unavailable"); setLiveTrace(runId, (line) => ctx.ui.notify(line, "info"), "raw-live"); return await show(ctx, runId === "next" ? "Raw live trace armed for the next run." : `Raw live trace enabled for ${runId}.`); } if (mode === "live") { if (!setLiveTrace) throw new Error("Live trace is unavailable"); setLiveTrace(runId, (line) => ctx.ui.notify(line, "info")); return await show(ctx, `Live trace enabled for ${runId}.`); } return await show(ctx, await controller.trace(runId, mode === "raw")); } if (verb === "recover") { if (rest.length > 1) throw new Error("Usage: /ultra-config recover [run-id]"); if (!rest[0]) { const decisions = await controller.recoveryDecisions(); return await show(ctx, decisions.length ? decisions.map((decision) => `${decision.runId ?? "unknown"}: ${decision.kind}${decision.state ? ` ${decision.state}` : ""}${decision.kind === "blocked" ? ` (${decision.reason})` : ""}`).join("\n") : "No unfinished UltraPi runs."); } const result = await controller.recover(ctx, rest[0]); if (result.rootHandoff) { await continueRecovered?.(result); return await show(ctx, `Continuing ${rest[0]} as ${result.runId}: root implementation and verification are scheduled.`); } return await show(ctx, `Recovered ${rest[0]} as ${result.runId}: read-only SCOUT handoff (${result.facts?.length ?? 0} facts).`); } if (verb === "mode") { const mode = rest[0] as Mode | undefined; if (!mode || !MODES.includes(mode)) throw new Error(`Usage: /ultra-config mode ${MODES.join("|")}`); await controller.setMode(mode); return await show(ctx, `UltraPi mode: ${mode}`); } if (verb === "policy") { const policy = rest[0] as Policy | undefined; if (!policy || !POLICIES.includes(policy)) throw new Error(`Usage: /ultra-config policy ${POLICIES.join("|")}`); await controller.setPolicy(policy); return await show(ctx, `UltraPi policy: ${policy}`); } if (verb === "budget") { if (rest.length === 0) { const status = controller.status(); return await show(ctx, `Estimated spend: ${formatUsd(status.spentUsd)}\nWeekly credit cap: ${status.budget.weeklyCreditBudget ?? "unset"}\nDaily credit cap: ${status.budget.dailyCreditBudget ?? "unset"}`); } if (rest[0] === "acknowledge") { await controller.setBudget(undefined, undefined, true); return await show(ctx, "One-task force acknowledgement saved."); } const weekly = Number(rest[0]); const daily = rest[1] === undefined ? undefined : Number(rest[1]); if (!Number.isFinite(weekly) || weekly <= 0 || (daily !== undefined && (!Number.isFinite(daily) || daily <= 0))) throw new Error("Budget values must be positive"); await controller.setBudget(weekly, daily, true); return await show(ctx, `Budget saved: weekly credit cap=${weekly}${daily === undefined ? "" : ` daily credit cap=${daily}`}`); } if (verb === "runs") { const runs = await controller.runs(); return await show(ctx, runs.length ? runs.map((run) => [`${run.runId} · ${run.topology}/${run.state} · [${run.progress.done}/${run.progress.total}] · est. ${formatUsd(run.spentUsd)}`, ...run.nodes.map((node) => ` ${node.nodeId} · ${node.role} · ${node.model ?? "model unselected"} · ${node.credits.toFixed(2)} cr · ${node.state}`)].join("\n")).join("\n") : "No UltraPi runs."); } if (verb === "stop") { if (!rest[0]) throw new Error("Usage: /ultra-config stop "); await controller.stop(rest[0]); await evaluateRunningExperiments(controller.paths); return await show(ctx, `Stopped ${rest[0]}`); } if (verb === "steer") { const [runId, ...message] = rest; if (!runId || message.length === 0) throw new Error("Usage: /ultra-config steer "); await controller.steer(runId, message.join(" ")); return await show(ctx, `Steered ${runId}`); } if (verb === "feedback") { const [kind, ...comment] = rest; if (!kind || !["good", "fixed", "bad"].includes(kind)) throw new Error("Usage: /ultra-config feedback good|fixed|bad [comment]"); await controller.feedback(kind as "good" | "fixed" | "bad", comment.join(" ")); await evaluateRunningExperiments(controller.paths); return await show(ctx, "Feedback recorded."); } if (verb === "recommendation") { const [action, file, recommendationId] = rest; if ((action !== "accept" && action !== "reject") || !file || rest.length > 3) throw new Error("Usage: /ultra-config recommendation accept|reject [id]"); const recommendation = await loadRecommendation(file, recommendationId); if (action === "reject") { await recordRecommendationDecision(controller.paths, recommendation, "rejected"); return await show(ctx, `Recommendation ${recommendation.id} rejected.`); } const experiments = await listExperiments(controller.paths); if (recommendation.experimentRequired && experiments.some((record) => record.status === "running")) throw new Error(`Experiment already running: ${experiments.find((record) => record.status === "running")!.experimentId}`); if (recommendation.experimentRequired && experiments.some((record) => record.experimentId === recommendation.id)) throw new Error(`Experiment ${recommendation.id} already exists`); const champion = controller.getConfig(); const challenger = await createConfigVersion(controller.paths, champion, (config) => applyRecommendation(config, recommendation), `recommendation:${recommendation.id}`); if (recommendation.experimentRequired) { const result = await startExperiment(controller.paths, { experimentId: recommendation.id, name: recommendation.id, championVersion: champion.configVersion, challengerVersion: challenger.configVersion }); await recordExperimentLifecycle(controller.paths, result); await recordRecommendationDecision(controller.paths, recommendation, "accepted", { challengerVersion: challenger.configVersion, experimentStarted: true }); return await show(ctx, `Recommendation ${recommendation.id} accepted: challenger ${challenger.configVersion}; experiment ${recommendation.id} started.`); } await controller.rollbackConfig(challenger.configVersion); await recordRecommendationDecision(controller.paths, recommendation, "accepted", { challengerVersion: challenger.configVersion, experimentStarted: false }); return await show(ctx, `Recommendation ${recommendation.id} accepted and activated as ${challenger.configVersion}.`); } if (verb === "experiment") { const [action, experimentId, version] = rest; if (action === "list" && rest.length === 1) { const records = await listExperiments(controller.paths); return await show(ctx, records.length ? records.map((record) => `${record.experimentId}: ${record.status} ${record.championVersion} → ${record.challengerVersion} (${record.factorPath})${record.stopReason ? ` [${record.stopReason}]` : ""}`).join("\n") : "No UltraPi experiments."); } if (action === "start" && experimentId && version && rest.length === 3) { const champion = await loadConfigVersion(controller.paths, controller.getConfig().configVersion); const challenger = await loadConfigVersion(controller.paths, version); const result = await startExperiment(controller.paths, { experimentId, name: experimentId, championVersion: champion.configVersion, challengerVersion: challenger.configVersion }); await recordExperimentLifecycle(controller.paths, result); return await show(ctx, `Experiment ${experimentId} started: ${result.record.championVersion} → ${result.record.challengerVersion} (${result.record.factorPath}).`); } if (action === "stop" && experimentId && rest.length === 2) { const result = await stopExperiment(controller.paths, experimentId, "manual"); await recordExperimentLifecycle(controller.paths, result); return await show(ctx, `Experiment ${experimentId} stopped manually.`); } if (action === "promote" && experimentId && rest.length === 2) { const current = await readExperiment(controller.paths, experimentId); if (current.status !== "running") throw new Error(`Experiment ${experimentId} is not running`); await controller.rollbackConfig(current.challengerVersion); const result = await stopExperiment(controller.paths, experimentId, "completed"); await recordExperimentLifecycle(controller.paths, result); return await show(ctx, `Experiment ${experimentId} promoted ${current.challengerVersion} and completed.`); } throw new Error("Usage: /ultra-config experiment list|start |stop |promote "); } if (verb === "export" && rest[0] === "preview") { if (rest.length > 2) throw new Error("Usage: /ultra-config export preview [YYYY-Www]"); return await show(ctx, formatExportPreview(await controller.exportPreview(rest[1] ?? isoWeek()))); } if (verb === "export" && rest[0] === "week") { const week = rest[1] ?? isoWeek(); const approved = ctx.hasUI && await ctx.ui.confirm("Export sanitized UltraPi telemetry", `Create ${week}? Preview before export if needed.`); if (!approved) throw new Error("Weekly export was not approved"); return await show(ctx, `Export created: ${await controller.exportWeek(week, true)}`); } if (verb === "config" && rest[0] === "show") return await show(ctx, describe(controller.getConfig())); if (verb === "config" && rest[0] === "rollback") { if (!rest[1] || rest.length !== 2) throw new Error("Usage: /ultra-config config rollback "); return await show(ctx, `UltraPi config rolled back to ${(await controller.rollbackConfig(rest[1])).configVersion}.`); } if (verb === "playbook") { const playbooks = await loadPlaybooks(ctx.cwd); if (rest[0] === "list" && rest.length === 1) return await show(ctx, playbooks.length ? playbooks.map((entry) => `${entry.name} ${entry.argumentHint} · topology=${entry.topology} · acceptance=${entry.acceptance}`).join("\n") : "No project playbooks."); if (rest[0] === "show" && rest[1] && rest.length === 2) { const found = playbooks.find((entry) => entry.name === rest[1]); if (!found) throw new Error(`Unknown playbook ${rest[1]}`); return await show(ctx, describe(found)); } throw new Error("Usage: /ultra-config playbook list|show "); } if (verb === "roles") { const roles = await loadRoleDefinitions(ctx.cwd); if (rest[0] === "list" && rest.length === 1) return await show(ctx, roles.length ? roles.map((role) => `${role.name} · ${role.kind}/${role.lens} · model=${role.model} · scope=${role.narrowScope.join(",") || "inherited"}`).join("\n") : "No project role definitions."); if (rest[0] === "show" && rest[1] && rest.length === 2) { const role = roles.find((entry) => entry.name === rest[1]); if (!role) throw new Error(`Unknown role ${rest[1]}`); return await show(ctx, describe(role)); } throw new Error("Usage: /ultra-config roles list|show "); } if (verb === "doctor") return await show(ctx, (await controller.doctor(ctx)).join("\n")); throw new Error(`Usage: /ultra-config ${HELP}`); }