/** * pi-shannon-statusline — Cyberpunk HUD for Pi * Ported from shannon-statusline (Claude Code). */ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { execFile } from "node:child_process"; import { promisify } from "node:util"; import { readdirSync, existsSync, readFileSync } from "node:fs"; import { join } from "node:path"; import { homedir } from "node:os"; import { createThroughputState, finishAssistantStream, getThroughputParts, markProviderRequest, recordThroughputDelta, startAssistantStream, } from "./throughput.ts"; import { describeRunState, initialSnapshot, resolveRunState, type RunState, type RunStateSnapshot, } from "./run-state.ts"; const execFileAsync = promisify(execFile); // ═══════════════════════════════════════════════════════════════ // Types // ═══════════════════════════════════════════════════════════════ interface GitStatus { branch: string; isDirty: boolean; ahead: number; behind: number; modified: number; added: number; deleted: number; untracked: number; } interface AgentRecord { status: "running" | "completed"; startTime: number; endTime?: number; } interface ToolRecord { name: string; target: string | null; status: "running" | "completed" | "error"; startTime: number; endTime?: number; } // ═══════════════════════════════════════════════════════════════ // State // ═══════════════════════════════════════════════════════════════ let sessionStartTime = 0; let turnIndex = 0; let tools: ToolRecord[] = []; let agents: AgentRecord[] = []; let modelProvider = ""; let modelId = ""; let cwd = ""; let waitingPrompt: { kind: string; title?: string } | null = null; /** * The run state machine, mirroring Pi's OSC 7501 reporter. Kept in one object so * the HUD and the tests read exactly the reporter's rules. */ let runState: RunStateSnapshot = initialSnapshot(); let throughputState = createThroughputState(); let hudRefreshTimer: ReturnType | undefined; let hudRefreshContext: any; let hudRenderSequence = 0; let footerVisible = true; // ═══════════════════════════════════════════════════════════════ // ANSI palette const R = "\x1b[0m"; const D = "\x1b[2m"; function rgb(r: number, g: number, b: number) { return `\x1b[38;2;${r};${g};${b}m`; } // Monokai Pro const FG = "\x1b[38;5;252m"; const COMMENT = "\x1b[38;5;243m"; const PINK = "\x1b[38;5;198m"; const GREEN = "\x1b[38;5;154m"; const ORANGE = "\x1b[38;5;208m"; const CYAN = "\x1b[38;5;123m"; const PURPLE = "\x1b[38;5;141m"; const YELLOW = "\x1b[38;5;221m"; const BLUE = "\x1b[38;5;111m"; function c(text: string, color: string) { return `${color}${text}${R}`; } function dim(text: string) { return `${D}${text}${R}`; } // Icons (aligned with shannon-statusline) const I_MODEL = "λ"; const I_PATH = "⌘"; const I_BRANCH = "⎇"; const I_CLOCK = "✦"; const I_CTX = "⊡"; const I_IN = "↑"; const I_OUT = "↓"; const I_CACHE = "⊗"; const I_SPEED = "»"; const I_DONE = "✔"; const I_RUN = "↻"; const I_CLAUDE = "※"; const I_MCP = "⊕"; const I_SKILL = "★"; const I_EXT = "◈"; const I_WAIT = "⧗"; // ═══════════════════════════════════════════════════════════════ // Fish-style path shortening (from original shannon-statusline) // ═══════════════════════════════════════════════════════════════ function abbreviateSegment(segment: string): string { if (segment.length <= 1) return segment; const extra = segment.match(/[-.](.)/); return extra ? `${segment[0]}${extra[0]}` : segment[0]; } function truncateTailSegment(segment: string, maxLen: number): string { if (segment.length <= maxLen) return segment; if (maxLen <= 1) return "…"; const extStart = segment.lastIndexOf("."); const hasExt = extStart > 0 && extStart < segment.length - 1; if (!hasExt) return `…${segment.slice(-(maxLen - 1))}`; const ext = segment.slice(extStart); const base = segment.slice(0, extStart); const budget = maxLen - ext.length - 1; if (budget <= 0) return `…${ext.slice(-(maxLen - 1))}`; return `…${base.slice(-budget)}${ext}`; } function shortenDisplayPath(fullPath: string, home: string, maxLen: number): string { if (!fullPath) return ""; let display = fullPath; if (home && fullPath === home) return "~"; if (home && fullPath.startsWith(home + "/")) { display = "~" + fullPath.slice(home.length); } const prefix = display.startsWith("~") ? "~" : display.startsWith("/") ? "/" : ""; const rawParts = display.split("/").filter(Boolean); const parts = prefix === "~" ? rawParts.slice(1) : rawParts; if (parts.length <= 1) return display; const tail = parts.slice(-1); const head = parts.slice(0, -1).map(abbreviateSegment); let shortened = [...head, ...tail].join("/"); if (prefix) shortened = prefix + "/" + shortened; if (shortened.length <= maxLen) return shortened; const ellipsis = prefix + "/…/" + tail.join("/"); if (ellipsis.length <= maxLen) return ellipsis; const budget = Math.max(1, maxLen - (prefix ? prefix.length + 4 : 3)); return `${prefix ? prefix + "/" : ""}…/${truncateTailSegment(tail[0]!, budget)}`; } // ═══════════════════════════════════════════════════════════════ // Context bar // ═══════════════════════════════════════════════════════════════ function ctxBar(percent: number, width: number): string { const safeP = Math.min(100, Math.max(0, percent)); const filled = Math.round((safeP / 100) * width); const empty = width - filled; let r0: number, g0: number, b0: number; let r1: number, g1: number, b1: number; if (safeP >= 85) { [r0, g0, b0] = [90, 0, 48]; [r1, g1, b1] = [255, 0, 144]; } else if (safeP >= 70) { [r0, g0, b0] = [122, 21, 0]; [r1, g1, b1] = [255, 107, 0]; } else { [r0, g0, b0] = [0, 51, 0]; [r1, g1, b1] = [57, 255, 20]; } const cells: string[] = []; for (let i = 0; i < filled; i++) { const t = filled > 1 ? i / (filled - 1) : 1; cells.push(`${rgb(Math.round(r0 + (r1 - r0) * t), Math.round(g0 + (g1 - g0) * t), Math.round(b0 + (b1 - b0) * t))}█`); } return `${cells.join("")}${D}${"░".repeat(empty)}${R}`; } function ctxPctColor(percent: number): string { if (percent >= 85) return rgb(255, 0, 144); if (percent >= 70) return rgb(255, 107, 0); return rgb(57, 255, 20); } // ═══════════════════════════════════════════════════════════════ // Tool whitelist — only Pi-native tools shown in HUD // ═══════════════════════════════════════════════════════════════ const TOOL_WHITELIST = new Set([ "read", "write", "edit", "bash", "grep", "ls", "find", ]); // ═══════════════════════════════════════════════════════════════ // Formatters // ═══════════════════════════════════════════════════════════════ function fmtTokens(n: number): string { if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`; if (n >= 1000) return `${(n / 1000).toFixed(1)}k`; return `${n}`; } function fmtDuration(ms: number): string { if (ms < 1000) return `${ms}ms`; const s = ms / 1000; if (s < 60) return `${s.toFixed(0)}s`; const m = Math.floor(s / 60); if (m < 60) return `${m}m ${Math.round(s % 60)}s`; const h = Math.floor(m / 60); return `${h}h ${m % 60}m`; } interface ContextUsageSnapshot { percent: number; contextWindow: number; tokens: number; } function readContextUsage(ctx: any): ContextUsageSnapshot | null { try { const usage = ctx.getContextUsage?.(); if (!usage) return null; return { percent: usage.percent ?? 0, contextWindow: usage.contextWindow ?? 0, tokens: usage.tokens ?? 0, }; } catch { return null; } } // ═══════════════════════════════════════════════════════════════ // User config (~/.pi/agent/shannon-statusline.json) // ═══════════════════════════════════════════════════════════════ interface ShannonConfig { rain: boolean; rainChars: string; footer: boolean; throughput: boolean; } const DEFAULT_RAIN_CHARS = "ヲァィゥェォャュョッーアイウエオカキクケコサシスセソ0123456789λΨΩΔΦ"; function loadConfig(): ShannonConfig { const cfg: ShannonConfig = { rain: true, rainChars: DEFAULT_RAIN_CHARS, footer: true, throughput: true }; try { const cfgPath = join(homedir(), ".pi", "agent", "shannon-statusline.json"); if (existsSync(cfgPath)) { const raw = JSON.parse(readFileSync(cfgPath, "utf8")) as Partial; if (typeof raw.rain === "boolean") cfg.rain = raw.rain; if (typeof raw.rainChars === "string" && raw.rainChars.length > 0) cfg.rainChars = raw.rainChars; if (typeof raw.footer === "boolean") cfg.footer = raw.footer; if (typeof raw.throughput === "boolean") cfg.throughput = raw.throughput; } } catch { /* ignore - fall back to defaults */ } return cfg; } function syncFooter(ctx: any, showFooter: boolean): void { if (footerVisible === showFooter) return; if (showFooter) { ctx.ui.setFooter(undefined); } else { ctx.ui.setFooter(() => ({ render(_width: number): string[] { return []; }, invalidate() {}, })); } footerVisible = showFooter; } // ═══════════════════════════════════════════════════════════════ // Matrix rain (6 columns like original) // ═══════════════════════════════════════════════════════════════ const RAIN_COLS = 6; const RAIN_SPEED_MS = 900; const RAIN_COL_OFFSET_MS = 280; function rainCell(row: number, col: number, now: number, total: number, chars: string): string { const colPhase = ((now + col * RAIN_COL_OFFSET_MS) / RAIN_SPEED_MS) % total; const headRow = Math.floor(colPhase); const dist = (row - headRow + total) % total; const ch = chars[Math.floor(now / 350 + row * 7 + col * 13) % chars.length] ?? " "; if (dist === 0) return `${rgb(200, 255, 200)}${ch}${R}`; if (dist === 1) return `${rgb(57, 255, 20)}${ch}${R}`; if (dist === 2) return `${rgb(0, 200, 0)}${ch}${R}`; if (dist === 3) return `${rgb(0, 160, 0)}${ch}${R}`; if (dist === 4) return `${rgb(0, 100, 0)}${ch}${R}`; if (dist === total - 1) return `${rgb(20, 20, 20)}${ch}${R}`; if (dist === total - 2) return `${rgb(0, 0, 0)}${ch}${R}`; return `${rgb(8, 8, 8)}${ch}${R}`; } function makeRain(row: number, total: number, chars: string): string { const now = Date.now(); const cells: string[] = []; for (let c = 0; c < RAIN_COLS; c++) cells.push(rainCell(row, c, now, total, chars)); return `${cells.join(" ")} `; } // ═══════════════════════════════════════════════════════════════ // Git // ═══════════════════════════════════════════════════════════════ async function getGit(dir: string): Promise { if (!dir) return null; try { const { stdout: branchOut } = await execFileAsync("git", ["rev-parse", "--abbrev-ref", "HEAD"], { cwd: dir, timeout: 1500, encoding: "utf8", }); const branch = branchOut.trim(); if (!branch) return null; let isDirty = false, modified = 0, added = 0, deleted = 0, untracked = 0; try { const { stdout: statusOut } = await execFileAsync("git", ["--no-optional-locks", "status", "--porcelain"], { cwd: dir, timeout: 1500, encoding: "utf8", }); const lines = statusOut.trim().split("\n").filter(Boolean); isDirty = lines.length > 0; for (const line of lines) { if (line.startsWith("??")) untracked++; else if (line[0] === "A") added++; else if (line[0] === "D" || line[1] === "D") deleted++; else if (line[0] === "M" || line[1] === "M" || line[0] === "R" || line[0] === "C") modified++; } } catch { /* ignore */ } let ahead = 0, behind = 0; try { const { stdout: revOut } = await execFileAsync("git", ["rev-list", "--left-right", "--count", "@{upstream}...HEAD"], { cwd: dir, timeout: 1500, encoding: "utf8", }); const parts = revOut.trim().split(/\s+/); if (parts.length === 2) { behind = parseInt(parts[0]!, 10) || 0; ahead = parseInt(parts[1]!, 10) || 0; } } catch { /* no upstream */ } return { branch, isDirty, ahead, behind, modified, added, deleted, untracked }; } catch { return null; } } // ═══════════════════════════════════════════════════════════════ // Config counter // ═══════════════════════════════════════════════════════════════ function countConfigs(dir: string) { let agentsMd = 0, mcps = 0, skills = 0, extensions = 0; const home = homedir(); try { // AGENTS.md in project if (existsSync(join(dir, "AGENTS.md"))) agentsMd++; if (existsSync(join(dir, "CLAUDE.md"))) agentsMd++; // MCPs from pi cache try { const mcpCache = JSON.parse(readFileSync(join(home, ".pi", "agent", "mcp-cache.json"), "utf8")); const servers = mcpCache?.servers; if (servers && typeof servers === "object") mcps = Object.keys(servers).length; } catch { /* ignore */ } // Skills from pi skills directory const skillsDir = join(home, ".pi", "agent", "skills"); if (existsSync(skillsDir)) { skills = readdirSync(skillsDir).filter(f => !f.startsWith(".")).length; } // Extensions from pi settings.json packages try { const settings = JSON.parse(readFileSync(join(home, ".pi", "agent", "settings.json"), "utf8")); const packages: string[] = settings?.packages ?? []; extensions = packages.length; } catch { /* ignore */ } } catch { /* ignore */ } return { agentsMd, mcps, skills, extensions }; } // ═══════════════════════════════════════════════════════════════ // HUD Renderer // ═══════════════════════════════════════════════════════════════ async function buildHud(ctx: any, config: ShannonConfig): Promise { const lines: string[] = []; const dir = cwd; const sep = `${COMMENT}│${R}`; const contextUsage = readContextUsage(ctx); // ── Line 1: Project + Git + Context tokens + Duration ── const parts1: string[] = []; if (dir) { const home = homedir(); parts1.push(`${c(I_PATH, ORANGE)} ${c(shortenDisplayPath(dir, home, 30), ORANGE)}`); } const git = await getGit(dir); if (git) { const dirty = git.isDirty ? "*" : ""; const branchColor = CYAN; let gitStr = `${c(I_BRANCH, branchColor)} ${c(`${git.branch}${dirty}`, branchColor)}`; const details: string[] = []; if (git.ahead > 0) details.push(c(`↑${git.ahead}`, GREEN)); if (git.behind > 0) details.push(c(`↓${git.behind}`, PINK)); if (git.modified > 0) details.push(c(`!${git.modified}`, PINK)); if (git.added > 0) details.push(c(`+${git.added}`, GREEN)); if (git.deleted > 0) details.push(c(`✘${git.deleted}`, PINK)); if (git.untracked > 0) details.push(c(`?${git.untracked}`, COMMENT)); if (details.length > 0) gitStr += ` ${details.join(" ")}`; parts1.push(gitStr); } if (contextUsage) { parts1.push(`${c(I_IN, CYAN)} ${c(fmtTokens(contextUsage.tokens), FG)}`); } if (sessionStartTime > 0) { // Turn count before duration if (turnIndex > 0) parts1.push(`${c(`↺ loop`, PURPLE)} ${c(`×${turnIndex}`, FG)}`); parts1.push(`${c(I_CLOCK, COMMENT)} ${c(fmtDuration(Date.now() - sessionStartTime), COMMENT)}`); } lines.push(parts1.join(` ${sep} `)); // ── Line 2: Model + Context ── const providerColor = COMMENT; let modelStr: string; if (modelProvider && modelId) { modelStr = `${c(I_MODEL, BLUE)} ${c(modelProvider, providerColor)}${dim("/")}${c(modelId, BLUE)}`; } else if (modelId) { modelStr = `${c(I_MODEL, BLUE)} ${c(modelId, BLUE)}`; } else if (modelProvider) { modelStr = `${c(I_MODEL, BLUE)} ${c(modelProvider, BLUE)}`; } else { modelStr = `${c(I_MODEL, BLUE)} ${c("pi", BLUE)}`; } // Thinking level removed — Pi doesn't expose real-time value in event context // (getThinkingLevel() only on command ctx, ctx.model has no current level) const line2Parts = [modelStr]; if (contextUsage) { const bar = ctxBar(contextUsage.percent, 10); const winLabel = contextUsage.contextWindow >= 1_000_000 ? `${(contextUsage.contextWindow / 1_000_000).toFixed(1)}M` : contextUsage.contextWindow >= 1000 ? `${Math.round(contextUsage.contextWindow / 1000)}k` : ""; let ctxStr = `${c(I_CTX, CYAN)} ${bar} ${c(`${contextUsage.percent.toFixed(1)}%`, ctxPctColor(contextUsage.percent))}`; if (winLabel) ctxStr += ` ${dim(`(${winLabel})`)}`; line2Parts.push(ctxStr); } lines.push(line2Parts.join(` ${sep} `)); // ── Line 3: Throughput ── if (config.throughput) { const throughputParts = getThroughputParts(throughputState); if (throughputParts?.length) { const throughputLine = throughputParts .map(({ key, value }) => `${c(key, COMMENT)} ${c(value, FG)}`) .join(` ${sep} `); lines.push(`${c(I_SPEED, COMMENT)} ${throughputLine}`); } } // ── Line 4: Config counts ── const configs = countConfigs(dir); const cfgParts: string[] = []; if (configs.agentsMd > 0) cfgParts.push(`${c(I_CLAUDE, BLUE)} ${c(`×${configs.agentsMd}`, FG)} ${dim("AGENTS.md")}`); if (configs.mcps > 0) cfgParts.push(`${c(I_MCP, ORANGE)} ${c(`×${configs.mcps}`, FG)} ${dim("MCPs")}`); if (configs.skills > 0) cfgParts.push(`${c(I_SKILL, PURPLE)} ${c(`×${configs.skills}`, FG)} ${dim("Skills")}`); if (configs.extensions > 0) cfgParts.push(`${c(I_EXT, YELLOW)} ${c(`×${configs.extensions}`, FG)} ${dim("extensions")}`); if (cfgParts.length > 0) lines.push(cfgParts.join(` ${sep} `)); // ── Separator + Tool counts ── const completed = tools.filter(t => t.status === "completed" && TOOL_WHITELIST.has(t.name)); const toolCounts = new Map(); for (const t of completed) toolCounts.set(t.name, (toolCounts.get(t.name) ?? 0) + 1); const toolLineParts: string[] = []; for (const name of toolCounts.keys()) { const count = toolCounts.get(name) ?? 0; if (count > 0) toolLineParts.push(`${GREEN} ${c(name, FG)}${count > 1 ? ` ${c(`×${count}`, COMMENT)}` : ""}`); } // Active agent count on the right side const activeAgents = agents.filter(a => a.status === "running").length; if (activeAgents > 0) { toolLineParts.push(`${c(I_RUN, PURPLE)} ${c("agent", PURPLE)} ${c(`×${activeAgents}`, PURPLE)}`); } if (toolLineParts.length > 0) { lines.push(`${COMMENT}${"─".repeat(67)}${R}`); lines.push(toolLineParts.join(` ${sep} `)); } // ── Running tools ── const running = tools.filter(t => t.status === "running"); for (const t of running.slice(-2)) { const elapsed = fmtDuration(Date.now() - t.startTime); const target = t.target ? `: ${shortenDisplayPath(t.target, homedir(), 22)}` : ""; lines.push(`${c(I_RUN, YELLOW)} ${c(t.name, CYAN)}${target} ${c(`(${elapsed})`, COMMENT)}`); } // ── Run state, mirroring Pi's own program status ── // `done` here means one run finished; it says nothing about acceptance. const state: RunState = resolveRunState({ ...runState, blocked: waitingPrompt !== null, blockedKind: waitingPrompt?.kind, blockedTitle: waitingPrompt?.title, }); const described = describeRunState(state); if (state !== "idle") { const tone = described.tone === "error" ? PINK : described.tone === "done" ? GREEN : described.tone === "busy" ? YELLOW : described.tone === "blocked" ? YELLOW : COMMENT; let line = `${c(described.icon, tone)} ${c(described.label, tone)}`; if (state === "blocked" && waitingPrompt) { if (waitingPrompt.kind) line += ` ${c(`(${waitingPrompt.kind})`, COMMENT)}`; if (waitingPrompt.title) { const title = waitingPrompt.title.length > 40 ? `${waitingPrompt.title.slice(0, 39)}…` : waitingPrompt.title; line += ` ${c(title, FG)}`; } } lines.push(line); } // ── Matrix rain (configurable via ~/.pi/agent/shannon-statusline.json) ── if (config.rain) { const total = lines.length; for (let i = 0; i < total; i++) { lines[i] = `${makeRain(i, total, config.rainChars)}${lines[i]}`; } } return lines; } // ═══════════════════════════════════════════════════════════════ // HUD refresh // ═══════════════════════════════════════════════════════════════ const HUD_STREAM_REFRESH_MS = 200; function refreshHud(ctx: any) { if (hudRefreshTimer) { clearTimeout(hudRefreshTimer); hudRefreshTimer = undefined; } hudRefreshContext = undefined; const sequence = ++hudRenderSequence; const config = loadConfig(); syncFooter(ctx, config.footer); buildHud(ctx, config).then(lines => { if (sequence !== hudRenderSequence) return; if (lines.length > 0) ctx.ui.setWidget("shannon-hud", lines, { placement: "belowEditor" }); }).catch(() => {}); } function refreshHudThrottled(ctx: any) { hudRefreshContext = ctx; if (hudRefreshTimer) return; hudRefreshTimer = setTimeout(() => { hudRefreshTimer = undefined; const pendingContext = hudRefreshContext; hudRefreshContext = undefined; if (pendingContext) refreshHud(pendingContext); }, HUD_STREAM_REFRESH_MS); } // ═══════════════════════════════════════════════════════════════ // Extension entry // ═══════════════════════════════════════════════════════════════ export default function (pi: ExtensionAPI) { // No slash commands — always-on cyberpunk HUD // ── Events ── pi.on("session_start", (_event, ctx) => { sessionStartTime = Date.now(); turnIndex = 0; cwd = ctx.cwd; tools = []; waitingPrompt = null; runState = initialSnapshot(); throughputState = createThroughputState(); // Pi resets extension UI before a new session starts. footerVisible = true; if (ctx.model) { modelProvider = (ctx.model as any).provider ?? ""; modelId = (ctx.model as any).id ?? ""; } refreshHud(ctx); }); pi.on("model_select", (event, ctx) => { if (event.model) { modelProvider = (event.model as any).provider ?? ""; modelId = (event.model as any).id ?? ""; } throughputState = createThroughputState(); refreshHud(ctx); }); pi.on("before_provider_request", () => { markProviderRequest(throughputState, Date.now()); }); pi.on("message_start", (event, ctx) => { if (event.message.role !== "assistant") return; startAssistantStream(throughputState, Date.now()); refreshHud(ctx); }); pi.on("message_update", (event, ctx) => { if (event.message.role !== "assistant") return; const text = recordThroughputDelta(throughputState, event.assistantMessageEvent, Date.now()); if (text !== undefined) refreshHudThrottled(ctx); }); pi.on("message_end", (event, ctx) => { if (event.message.role !== "assistant") return; finishAssistantStream(throughputState, event.message.usage); // The latest assistant response decides the run's outcome, so a retried // error is replaced by its successful retry. runState = { ...runState, outcome: (event.message as { stopReason?: string }).stopReason === "error" ? "error" : "done", }; refreshHud(ctx); }); pi.on("thinking_level_select", (_event) => { // Event fires on manual change; no real-time read API in ExtensionContext }); pi.on("turn_start", (_event, ctx) => { turnIndex = (_event as any).turnIndex ?? (turnIndex + 1); refreshHud(ctx); }); pi.on("tool_call", (event, ctx) => { const tool: ToolRecord = { name: event.toolName, target: null, status: "running", startTime: Date.now() }; if (event.input && typeof event.input === "object") { const inp = event.input as Record; if (typeof inp.path === "string") tool.target = inp.path; else if (typeof inp.filePath === "string") tool.target = inp.filePath; } tools.push(tool); // Ponytail: cap at 500 to prevent unbounded growth over long sessions if (tools.length > 500) tools = tools.slice(-400); refreshHud(ctx); }); pi.on("tool_result", (event, ctx) => { for (let i = tools.length - 1; i >= 0; i--) { if (tools[i]!.name === event.toolName && tools[i]!.status === "running") { tools[i]!.status = event.isError ? "error" : "completed"; tools[i]!.endTime = Date.now(); break; } } refreshHud(ctx); }); pi.on("turn_end", (_event, ctx) => refreshHud(ctx)); pi.on("agent_end", (_event, ctx) => { // `agent_end` is not final: retries, recovery, compaction and queued work can // still follow, so it marks activity but not settlement. const running = agents.find(a => a.status === "running"); if (running) { running.status = "completed"; running.endTime = Date.now(); } refreshHud(ctx); }); pi.on("agent_start", (_event, ctx) => { agents.push({ status: "running", startTime: Date.now() }); // A new run clears the previous run's outcome and becomes active. runState = { ...runState, active: true, compacting: false, outcome: "done" }; refreshHud(ctx); }); // `agent_settled` is Pi's final boundary: nothing will continue automatically, // and `aborted` says whether the person cancelled the run. pi.on("agent_settled", (event, ctx) => { const aborted = (event as { aborted?: boolean }).aborted === true; // End the run and lock in its outcome: the reporter's `resting` is the // aborted run's `idle`, otherwise the outcome the responses produced. runState = { ...runState, active: false, compacting: false, resting: aborted ? "idle" : runState.outcome, }; refreshHud(ctx); }); // Compaction reports `working` with its own message; a failed recovery can end // the run, so the outcome is updated the same way the reporter does it. pi.on("session_before_compact", (_event, ctx) => { runState = { ...runState, compacting: true }; refreshHud(ctx); }); pi.on("session_compact", (_event, ctx) => { runState = { ...runState, compacting: false }; refreshHud(ctx); }); pi.on("session_compact_failed", (event, ctx) => { const failed = event as { aborted?: boolean; errorMessage?: string }; runState = { ...runState, compacting: false, // A failed compaction while a run is active ends it: aborted is idle, // an error keeps the error outcome. ...(runState.active ? { outcome: failed.aborted ? "idle" : failed.errorMessage ? "error" : runState.outcome } : failed.errorMessage ? { resting: "error" as const } : {}), }; refreshHud(ctx); }); // ── Waiting-on-user lifecycle (pi >= 0.84.4) ── pi.on("ui_prompt_start", (event, ctx) => { waitingPrompt = { kind: event.kind, title: event.title }; refreshHud(ctx); }); pi.on("ui_prompt_end", (_event, ctx) => { waitingPrompt = null; refreshHud(ctx); }); }