import { assistantTokens } from "./usage.ts"; import { processSignature } from "./process-signature.ts"; import { PI_CEILING_KIND, PI_CEILING_UNIT } from "./ceiling.ts"; type ExtensionAPI = any; type BudgetUnit = "model_calls" | "tool_calls"; type BridgeEvent = { type: string; text?: string; sessionId?: string; sessionFile?: string; error?: string }; const bridgeUrl = process.env.AGENTHUB_PI_BRIDGE_URL ?? ""; const bridgeToken = process.env.AGENTHUB_PI_BRIDGE_TOKEN ?? ""; const ownerToken = process.env.AGENTHUB_PI_OWNER_TOKEN ?? ""; const relayUrl = process.env.AGENTHUB_PI_RELAY_URL ?? ""; const relayToken = process.env.AGENTHUB_PI_RELAY_TOKEN ?? ""; const models = (() => { try { return JSON.parse(process.env.AGENTHUB_PI_MODELS ?? "[]") as unknown[]; } catch { return []; } })(); let pollStarted = false; let pollStopped = false; let toolSteps = 0; let lastActivity = 0; let usageSeq = 0; let shellSeq = 0; let forcedFailure = ""; let turnGeneration = 0; let sessionId = ""; let titleRevision = 0; let titleEpoch = ""; let pendingPromptId: string | undefined; let activePromptId: string | undefined; let settlement: Promise | undefined; const maxSteps = Number(process.env.AGENTHUB_PI_MAX_STEPS ?? 100); let shutdown: (() => void) | undefined; let runtimeCtx: any; let modelRegistry: any; function contextObservation(message: any, ctx: any): Record { const usage = message?.role === "assistant" && typeof message.model === "string" && message.model === ctx?.model?.id ? message.usage : undefined; return { input: usage?.input, cacheRead: usage?.cacheRead, cacheWrite: usage?.cacheWrite, contextWindow: ctx?.model?.contextWindow }; } async function post(path: string, body: unknown): Promise { if (!bridgeUrl || !bridgeToken) throw new Error("Pi bridge is not configured"); const response = await fetch(`${bridgeUrl}${path}`, { method: "POST", headers: { "content-type": "application/json", authorization: `Bearer ${bridgeToken}` }, body: JSON.stringify(body) }); if (!response.ok) throw new Error(`Pi bridge HTTP ${response.status}`); return response.json(); } async function admitBudget(unit: BudgetUnit, idleUserBash = false, generation = turnGeneration, callSessionId = sessionId): Promise<{ allowed: boolean; reservation?: string; reason?: string }> { const current = () => generation === turnGeneration && callSessionId === sessionId; const stale = () => ({ allowed: false, reason: "Pi tool lineage ended during admission; do not retry the old call" }); if (!current()) return stale(); try { const result = await post("/budget", { unit, generation, ...(idleUserBash ? { idleUserBash: true } : {}) }); if (!current()) return stale(); const decisions = Array.isArray(result?.decisions) ? result.decisions : []; const denied = decisions.find((item: any) => item?.allowed === false); if (!denied) return { allowed: true, ...(idleUserBash && typeof result?.reservation === "string" ? { reservation: result.reservation } : {}) }; const reason = `execution budget ${denied.reason ?? "exhausted"}: ${denied.scope} ${denied.unit ?? unit} used ${denied.used}${denied.limit === null ? "" : ` of ${denied.limit}`}`; if (idleUserBash) return { allowed: false }; forcedFailure = reason; } catch (error) { if (!current()) return stale(); if (idleUserBash) return { allowed: false }; forcedFailure = `execution budget admission unavailable: ${(error as Error).message}`; } const reason = forcedFailure; try { await post("/event", { type: "agent_end", generation, failed: true, error: reason }); } catch { /* abort remains authoritative when the bridge is unavailable */ } if (current()) runtimeCtx?.abort?.(); return { allowed: false, reason }; } async function poll(pi: ExtensionAPI): Promise { while (!pollStopped) { try { const response = await fetch(`${bridgeUrl}/commands`, { headers: { authorization: `Bearer ${bridgeToken}` } }); const payload = await response.json() as { command?: any }; const command = payload.command; if (!command) continue; try { if (command.type === "prompt" || command.type === "steer") { if (command.type === "prompt") { // Let our settled callback return to native Pi before the normal queued successor starts. if (settlement) { let timer: ReturnType | undefined; try { await Promise.race([settlement, new Promise(resolve => { timer = setTimeout(resolve, 5_000); })]); } finally { clearTimeout(timer); } } if (settlement || runtimeCtx?.isIdle?.() !== true || (command.sessionId !== undefined && command.sessionId !== sessionId) || (command.generation !== undefined && command.generation !== turnGeneration)) { await post("/ack", { id: command.id, ok: false, error: "Pi prompt was not inserted because its native session is no longer idle at the expected generation", refusal: { kind: "prompt_not_started", promptId: command.id, sessionId, generation: turnGeneration } }); continue; } // Pinned Pi 1.0.1 starts _runAgentPrompt synchronously before its first await. // Keep this check and insertion in one JS turn; snapshots cannot reserve native idleness. pendingPromptId = command.id; } pi.sendMessage({ customType: "agent-hub", content: String(command.message ?? ""), display: true }, { triggerTurn: command.type === "prompt", deliverAs: command.type === "steer" ? "steer" : "followUp" }); if (command.type === "prompt" && runtimeCtx?.isIdle?.() !== false) { // Another extension's async settled hook can make native Pi defer this insertion. // Our callback barrier is not an atomic native reservation: keep that effect uncertain. if (pendingPromptId === command.id) pendingPromptId = undefined; if (activePromptId === command.id) activePromptId = undefined; await post("/ack", { id: command.id, ok: false, error: "Pi prompt insertion did not synchronously claim a native turn", refusal: { kind: "prompt_uncertain", promptId: command.id, sessionId, generation: turnGeneration } }); continue; } } else if (command.type === "set_session_title") { const refuse = (cause: string, error: string) => post("/ack", { id: command.id, ok: true, result: { ok: false, reason: "invalid", cause, error, sessionId, titleEpoch, titleRevision } }); const nativeId = runtimeCtx?.sessionManager?.getHeader?.()?.id; if (command.sessionId !== sessionId || nativeId !== sessionId || command.titleEpoch !== undefined && command.titleEpoch !== titleEpoch) { await refuse("session", "Pi native session changed before the title write; check ahub status and reconnect the current session before retrying."); continue; } if (command.generation !== turnGeneration) { await refuse("generation", "Pi native turn changed before the title write; wait for the current turn and check ahub status before retrying."); continue; } if (typeof command.title !== "string" || !command.title.trim() || [...command.title].length > 80 || typeof command.onlyIfUnnamed !== "boolean") { await refuse("arguments", "Pi title command arguments are invalid; use a 1-80 character normalized title and a conditional-write flag before retrying."); continue; } if (typeof pi.getSessionName !== "function" || typeof pi.setSessionName !== "function") { await refuse("native_unavailable", "Pi native title API is unavailable; restart Pi with the supported hub extension and check ahub status before retrying."); continue; } // The native read and synchronous write share one JS turn: a manual rename always wins against the agent. const before = pi.getSessionName() ?? ""; if (command.onlyIfUnnamed && before) { await post("/ack", { id: command.id, ok: true, result: { ok: false, reason: "already_named", error: "This session already has a title; keep it or ask the person to rename it with ahub title.", sessionId, titleEpoch, titleRevision, sessionName: before } }); continue; } pi.setSessionName(command.title); const sessionName = pi.getSessionName() ?? ""; await post("/ack", { id: command.id, ok: true, result: { ok: sessionName === command.title, ...(sessionName === command.title ? {} : { reason: "invalid", cause: "native_readback", mayHaveApplied: true, error: "Pi native title readback did not confirm the write; the title may have been applied; check ahub status before retrying." }), sessionId, titleEpoch, titleRevision, sessionName } }); continue; } else if (command.type === "get_session_state") { const entries = runtimeCtx.sessionManager.getEntries(); await post("/ack", { id: command.id, ok: true, result: { sessionId: runtimeCtx.sessionManager.getHeader()?.id, sessionFile: runtimeCtx.sessionManager.getSessionFile(), ...(typeof pi.getSessionName === "function" ? { sessionName: pi.getSessionName() ?? "", titleEpoch, titleRevision } : {}), idle: runtimeCtx.isIdle() === true, empty: entries.every((entry: any) => ["model_change", "thinking_level_change"].includes(entry.type)), model: runtimeCtx?.model ? { id: runtimeCtx.model.id } : undefined, context: contextObservation(entries.slice().reverse().find((entry: any) => entry.type === "message" && entry.message?.role === "assistant")?.message, runtimeCtx), } }); continue; } else if (command.type === "set_model") { const model = modelRegistry?.find(String(command.provider), String(command.modelId)); if (!model || !(await (pi as any).setModel?.(model))) throw new Error("Pi model is not available"); } else if (command.type === "shutdown") { pollStopped = true; shutdown?.(); } else if (command.type === "abort_budget") { if (Number.isSafeInteger(command.generation) && command.generation === turnGeneration) { if (typeof runtimeCtx?.abort !== "function") throw new Error("Pi runtime cannot abort the current turn"); const generation = turnGeneration, callSessionId = sessionId; const reason = command.cause === "approval" && typeof command.reason === "string" && command.reason ? command.reason : "execution budget exhausted: elapsed_ms wall cap reached"; forcedFailure = reason; await post("/event", { type: "agent_end", generation, failed: true, error: reason }); if (generation === turnGeneration && callSessionId === sessionId) runtimeCtx?.abort?.(); } } await post("/ack", { id: command.id, ok: true }); } catch (error) { if (command.type === "set_session_title") { await post("/ack", { id: command.id, ok: true, result: { ok: false, reason: "invalid", cause: "native_failed", mayHaveApplied: true, error: "Pi native title API failed without confirming the write; the title may have been applied; check ahub status before retrying.", sessionId, titleEpoch, titleRevision } }); continue; } if (pendingPromptId === command.id) pendingPromptId = undefined; await post("/ack", { id: command.id, ok: false, error: (error as Error).message }); } } catch { await new Promise((resolve) => setTimeout(resolve, 250)); } } } export default function(pi: ExtensionAPI): void { if (relayUrl) { pi.registerProvider("agent-hub-local", { baseUrl: relayUrl, api: "openai-completions", apiKey: relayToken || "agent-hub-local", models: models.filter((m): m is any => !!m && typeof m === "object" && typeof (m as any).id === "string").map((m: any) => ({ id: m.id, name: m.name ?? m.id, reasoning: !!m.reasoning, input: ["text"], cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, contextWindow: m.contextWindow ?? 131072, maxTokens: m.maxTokens ?? 8192, })), }); } pi.on("session_start", async (_event: any, ctx: any) => { runtimeCtx = ctx; modelRegistry = ctx.modelRegistry; shutdown = ctx.shutdown; const state = ctx.sessionManager.getHeader(); sessionId = String(state?.id ?? ""); titleRevision = 0; titleEpoch = crypto.randomUUID(); try { const claimed = await post("/event", { type: "session_start", ownerToken, pid: process.pid, signature: processSignature(process.pid), ...(typeof pi.getSessionName === "function" ? { sessionName: pi.getSessionName() ?? "", titleEpoch, titleRevision } : {}), sessionId: state?.id, sessionFile: ctx.sessionManager.getSessionFile(), model: ctx.model ? { id: ctx.model.id } : undefined, approvalTurnAbort: typeof ctx.abort === "function", fencedPrompt: true }); if (claimed?.ok === false) { ctx.shutdown?.(); return; } } catch (error) { ctx.shutdown?.(); throw error; } if (!pollStarted) { pollStarted = true; void poll(pi); } }); pi.on("agent_end", async (event: any) => { const message = (event as any).messages?.slice().reverse().find((m: any) => m?.role === "assistant"); const text = message?.content?.filter((c: any) => c?.type === "text").map((c: any) => c.text).join("")?.trim(); const failed = !!forcedFailure || message?.stopReason === "error"; const cancelled = !forcedFailure && message?.stopReason === "aborted"; await post("/event", { type: "agent_end", generation: turnGeneration, ...(activePromptId ? { promptId: activePromptId } : {}), text: text ?? "", failed, cancelled, ...(failed ? { error: forcedFailure || message?.errorMessage || message?.stopReason } : {}) }); }); pi.on("message_end", async (event: any, ctx: any) => { if (ctx) runtimeCtx = ctx; if (event.message?.role === "assistant") { try { await post("/event", { type: "pi_usage", sessionId, generation: turnGeneration, model: (ctx ?? runtimeCtx)?.model ? { id: (ctx ?? runtimeCtx).model.id } : undefined, context: contextObservation(event.message, ctx ?? runtimeCtx) }); } catch { /* telemetry must not break a native turn */ } } const tokens = assistantTokens(event.message); if (tokens === undefined) return; // Message-end is the sole usage source: agent_end carries those same messages again. try { await post("/event", { type: "tokens", id: `usage-${++usageSeq}`, tokens }); } catch { /* telemetry must not break a native turn */ } }); pi.on("message_update", async () => { const now = Date.now(); if (now - lastActivity < 1000) return; lastActivity = now; try { await post("/event", { type: "activity" }); } catch { /* shutdown owns bridge cleanup */ } }); pi.on("session_info_changed", async () => { if (runtimeCtx?.sessionManager?.getHeader?.()?.id !== sessionId || typeof pi.getSessionName !== "function") return; try { await post("/event", { type: "session_title", sessionId, titleEpoch, titleRevision: ++titleRevision, sessionName: pi.getSessionName() ?? "" }); } catch { /* title observation must not disrupt the native session */ } }); // Native retries/continuations emit more starts before the one logical agent_settled. pi.on("agent_start", async () => { toolSteps = 0; forcedFailure = ""; turnGeneration++; activePromptId = pendingPromptId ?? activePromptId; pendingPromptId = undefined; await post("/event", { type: "agent_start", sessionId, ...(typeof pi.getSessionName === "function" ? { sessionName: pi.getSessionName() ?? "", titleEpoch, titleRevision } : {}), generation: turnGeneration, ...(activePromptId ? { promptId: activePromptId } : {}) }); }); pi.on("model_select", async (_event: any, ctx: any) => { runtimeCtx = ctx; if (ctx.model?.provider && ctx.model.provider !== "agent-hub-local") ctx.shutdown?.(); else try { await post("/event", { type: "pi_model", sessionId, model: ctx.model ? { id: ctx.model.id } : undefined }); } catch { /* optional telemetry */ } }); pi.on("before_agent_start", async (event: any, ctx: any) => { if (ctx.model?.provider && ctx.model.provider !== "agent-hub-local") { ctx.abort?.(); return { systemPrompt: event.systemPrompt }; } return undefined; }); pi.on("before_provider_request", async (_event: any, ctx: any) => { if (ctx.model?.provider && ctx.model.provider !== "agent-hub-local") { ctx.abort?.(); return undefined; } const generation = turnGeneration, callSessionId = sessionId; const current = () => generation === turnGeneration && callSessionId === sessionId; try { if (!(await admitBudget("model_calls", false, generation, callSessionId)).allowed && current()) ctx.abort?.(); } catch (error) { if (current()) { forcedFailure = `execution budget admission failed: ${(error as Error).message}`; ctx.abort?.(); } } return undefined; }); pi.on("agent_settled", async (_event: any, ctx: any) => { let release!: () => void; const current = new Promise(resolve => { release = resolve; }); settlement = current; const entries = ctx.sessionManager?.getEntries?.() ?? []; const message = entries.slice().reverse().find((entry: any) => entry.type === "message" && entry.message?.role === "assistant")?.message; const text = message?.content?.filter((c: any) => c?.type === "text").map((c: any) => c.text).join("")?.trim(); try { await post("/event", { type: "agent_settled", generation: turnGeneration, ...(activePromptId ? { promptId: activePromptId } : {}), ...(text ? { text } : {}) }); } finally { activePromptId = undefined; // Release after this callback returns, not inside its awaited POST continuation. setTimeout(() => { if (settlement === current) settlement = undefined; release(); }, 0); } }); pi.on("user_bash", async (event: any) => { const generation = turnGeneration, callSessionId = sessionId; const admission = await admitBudget("tool_calls", true, generation, callSessionId); if (!admission.allowed || !admission.reservation) return { cancel: true }; const result = await post("/tool", { name: "bash", args: { command: event.command, cwd: event.cwd }, toolCallId: `pi-shell-${Date.now()}-${++shellSeq}`, purpose: "idle_user_bash", generation, sessionId: callSessionId, reservation: admission.reservation }); return { result: { output: String(result.text ?? result), exitCode: typeof result.exitCode === "number" ? result.exitCode : undefined, cancelled: result.exitCode === null, truncated: false } }; }); // Managed sessions may only be handed over by PiPeer after it has fenced the // owner and verified the replacement identity. User /fork and /resume would // otherwise silently detach the hub from its recorded session. pi.on("session_before_switch", async () => ({ cancel: true })); pi.on("session_before_fork", async () => ({ cancel: true })); pi.on("session_shutdown", async () => { pollStopped = true; await post("/event", { type: "session_shutdown" }); }); for (const raw of (() => { try { return JSON.parse(process.env.AGENTHUB_PI_TOOLS ?? "[]") as any[]; } catch { return []; } })()) { if (!raw || typeof raw.name !== "string" || !raw.parameters) continue; pi.registerTool({ name: raw.name, label: raw.name, description: raw.description ?? raw.name, parameters: raw.parameters, async execute(toolCallId: string, params: unknown, signal?: AbortSignal) { const generation = turnGeneration, callSessionId = sessionId; if (signal?.aborted) return { content: [{ type: "text", text: "error: tool call cancelled before execution" }], details: {}, isError: true }; const admission = await admitBudget("tool_calls", false, generation, callSessionId); if (!admission.allowed) return { content: [{ type: "text", text: `error: ${admission.reason ?? forcedFailure}` }], details: {}, isError: true }; if (toolSteps++ >= maxSteps) { const reason = `Pi tool step limit ${maxSteps} reached`; forcedFailure = reason; // #179: the structured ceiling signal at the actual rejection boundary, before the failure // event, so the adapter binds it while the turn still owns it. The counter already counts the // rejected pre-effect invocation (toolSteps++ above); its side effect never executes. The // reason text stays the failure record; the signal adds the validated counts. try { await post("/event", { type: "ceiling", kind: PI_CEILING_KIND, unit: PI_CEILING_UNIT, count: toolSteps, limit: maxSteps, sessionId: callSessionId, generation }); } catch { /* the agent_end failure below remains authoritative */ } await post("/event", { type: "agent_end", generation, failed: true, error: reason }); if (generation === turnGeneration && callSessionId === sessionId) runtimeCtx?.abort?.(); return { content: [{ type: "text", text: `error: ${reason}` }], details: {}, isError: true }; } const aborted = () => { void post("/event", { type: "tool_abort", sessionId: callSessionId, generation, toolCallId }).catch(() => undefined); }; if (signal?.aborted) return { content: [{ type: "text", text: "error: tool call cancelled before execution" }], details: {}, isError: true }; signal?.addEventListener("abort", aborted, { once: true }); let result: any; try { result = await post("/tool", { name: raw.name, args: params, toolCallId, sessionId: callSessionId, generation }); if (signal?.aborted) aborted(); } finally { signal?.removeEventListener("abort", aborted); } const text = String(result.text ?? result); // Pi 1.0.1 reads isError === true alone (#181). The bridge computes `failed` with the // managed-tool failure contract (toolResultFailed); the extension never re-parses the text. // A bridge too old to send `failed` leaves the result unflagged, exactly as before the fix. return { content: [{ type: "text", text }], details: {}, isError: result.failed === true }; } }); } }