import * as fs from "node:fs"; import * as path from "node:path"; import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { registerNativeSupervisorClient } from "../../intercom/native-supervisor-channel.ts"; import { consumeSteerRequestsFromDir, steerAckPathFromDir, writeSteerAckAt, writeSteerCapabilityAt, writeSteerRequestToDir, type SteerRequest } from "../background/control-channel.ts"; import { SUBAGENT_CHILD_AGENT_ENV, SUBAGENT_CHILD_INDEX_ENV, SUBAGENT_FANOUT_CHILD_ENV, SUBAGENT_STEER_ACK_DIR_ENV, SUBAGENT_STEER_CAPABILITY_ENV, SUBAGENT_STEER_INBOX_ENV } from "./pi-args.ts"; import { createStructuredOutputToolParameters, STRUCTURED_OUTPUT_CAPTURE_ENV, STRUCTURED_OUTPUT_SCHEMA_ENV, validateStructuredOutputValue } from "./structured-output.ts"; import { CHILD_TOOL_DIAGNOSTIC_PATH_ENV, MCP_DIRECT_CHILD_TOOLS_ENV, REQUIRED_CHILD_TOOLS_ENV, writeChildToolDiagnostic, type ChildToolDiagnostic, } from "./tool-availability.ts"; import { TOOL_BUDGET_ENV, TOOL_BUDGET_ZERO_AUTH_ENV, decodeToolBudgetEnv, shouldBlockToolForBudget, toolBudgetBlockedMessage, toolBudgetSoftNudge } from "./tool-budget.ts"; import type { JsonSchemaObject, ResolvedToolBudget, SubagentState } from "../../shared/types.ts"; import { resolveCurrentSessionId } from "../../shared/session-identity.ts"; import { resolveWatchPath } from "../../shared/utils.ts"; import { registerChildWatchdog } from "../../watchdog/register-child.ts"; import { SUBAGENT_WATCHDOG_WARNING_TYPE } from "../../watchdog/types.ts"; import { resolveWaitToolConfig } from "../background/wait-config.ts"; import { registerWaitTool } from "../background/wait-tool.ts"; import { drainOutstandingWork } from "../background/auto-drain.ts"; const SUBAGENT_INHERIT_PROJECT_CONTEXT_ENV = "PI_SUBAGENT_INHERIT_PROJECT_CONTEXT"; const SUBAGENT_INHERIT_SKILLS_ENV = "PI_SUBAGENT_INHERIT_SKILLS"; export const SUBAGENT_INTERCOM_SESSION_NAME_ENV = "PI_SUBAGENT_INTERCOM_SESSION_NAME"; const STRUCTURED_OUTPUT_INSTRUCTIONS = [ "This subagent step has a strict structured output contract.", "Your final action must be to call the `structured_output` tool with JSON matching the provided schema.", "Do not rely on prose-only completion; if you do not call `structured_output`, the parent will fail this step.", ].join("\n"); export const CHILD_SUBAGENT_BOUNDARY_INSTRUCTIONS = [ "You are a child subagent, not the parent orchestrator.", "The parent session owns delegation, orchestration, review fanout, and follow-up worker launches.", "Ignore prior parent-only orchestration instructions in inherited conversation history.", "Do not propose or run subagents. Complete only your assigned role-specific task with the tools available to you.", "If you need to edit files, use the available editing tools. Do not print tool-call syntax, patches, or pseudo-tool calls as text.", ].join("\n"); export const CHILD_FANOUT_BOUNDARY_INSTRUCTIONS = [ "You are a child subagent with explicit fanout responsibility for this assigned task.", "The parent session owns final orchestration, acceptance, and follow-up implementation launches.", "You may use the `subagent` tool only for the fanout work explicitly requested in this task.", "Do not broaden yourself into general parent orchestration. Do not launch follow-up workers unless the task explicitly asks for that.", "The maxSubagentDepth cap still applies and may block further fanout.", "If you need to edit files, use the available editing tools. Do not print tool-call syntax, patches, or pseudo-tool calls as text.", ].join("\n"); const PARENT_ONLY_CUSTOM_MESSAGE_TYPES = new Set([ "subagent-orchestration-instructions", "subagent-slash-result", "subagent-slash-text-result", "subagent-notify", "subagent_control_notice", "subagent-control", "subagent-control-notice", ]); const SUBAGENT_ORCHESTRATION_SKILL_NAME_PATTERN = /\s*pi-subagents\s*<\/name>/; const PROJECT_CONTEXT_HEADER = "\n\n# Project Context\n\nProject-specific instructions and guidelines:\n\n"; const SKILLS_HEADER = "\n\nThe following skills provide specialized instructions for specific tasks."; const DATE_HEADER = "\nCurrent date:"; function readBooleanEnv(name: string): boolean | undefined { const value = process.env[name]; if (value === undefined) return undefined; return value !== "0"; } function readRequiredChildTools(): string[] | undefined { const encoded = process.env[REQUIRED_CHILD_TOOLS_ENV]?.trim(); if (!encoded) return undefined; const required = JSON.parse(encoded) as unknown; if (!Array.isArray(required) || required.some((name) => typeof name !== "string" || !name)) { throw new Error(`Invalid ${REQUIRED_CHILD_TOOLS_ENV} payload.`); } return required; } function readMcpDirectChildTools(): string[] | undefined { const encoded = process.env[MCP_DIRECT_CHILD_TOOLS_ENV]?.trim(); if (!encoded) return undefined; try { const tools = JSON.parse(encoded) as unknown; if (!Array.isArray(tools) || tools.some((name) => typeof name !== "string" || !name)) return undefined; return tools; } catch { return undefined; } } function refreshChildToolDiagnostic(pi: ExtensionAPI): ChildToolDiagnostic | undefined { const filePath = process.env[CHILD_TOOL_DIAGNOSTIC_PATH_ENV]?.trim(); const required = readRequiredChildTools(); if (!filePath || !required) return undefined; const available = pi.getAllTools().map((tool) => tool.name); return writeChildToolDiagnostic(filePath, required, available, process.env[SUBAGENT_CHILD_AGENT_ENV]?.trim(), readMcpDirectChildTools()); } function findSectionEnd(prompt: string, startIndex: number, nextHeaders: string[]): number { let endIndex = prompt.length; for (const header of nextHeaders) { const index = prompt.indexOf(header, startIndex); if (index !== -1 && index < endIndex) { endIndex = index; } } return endIndex; } export function stripProjectContext(prompt: string): string { const startIndex = prompt.indexOf(PROJECT_CONTEXT_HEADER); if (startIndex === -1) return prompt; const endIndex = findSectionEnd(prompt, startIndex + PROJECT_CONTEXT_HEADER.length, [SKILLS_HEADER, DATE_HEADER]); return `${prompt.slice(0, startIndex)}${prompt.slice(endIndex)}`; } export function stripInheritedSkills(prompt: string): string { const startIndex = prompt.indexOf(SKILLS_HEADER); if (startIndex === -1) return prompt; const endIndex = findSectionEnd(prompt, startIndex + SKILLS_HEADER.length, [DATE_HEADER]); return `${prompt.slice(0, startIndex)}${prompt.slice(endIndex)}`; } export function stripSubagentOrchestrationSkill(prompt: string): string { return prompt .replace(/\n{0,2}]*>[\s\S]*?<\/skill>\n{0,2}/g, "\n\n") .replace(/[ \t]*\s*[\s\S]*?<\/skill>\s*/g, (block) => SUBAGENT_ORCHESTRATION_SKILL_NAME_PATTERN.test(block) ? "" : block); } function stripChildBoundaryInstructions(prompt: string): string { let rewritten = prompt; for (const boundary of [CHILD_SUBAGENT_BOUNDARY_INSTRUCTIONS, CHILD_FANOUT_BOUNDARY_INSTRUCTIONS]) { rewritten = rewritten.split(boundary).join(""); } return rewritten.replace(/^(?:[ \t]*\r?\n)+/, ""); } export function rewriteSubagentPrompt( prompt: string, options: { inheritProjectContext: boolean; inheritSkills: boolean; fanoutChild?: boolean }, ): string { let rewritten = prompt; if (!options.inheritProjectContext) { rewritten = stripProjectContext(rewritten); } if (!options.inheritSkills) { rewritten = stripInheritedSkills(rewritten); } rewritten = stripSubagentOrchestrationSkill(rewritten); rewritten = stripChildBoundaryInstructions(rewritten); const boundary = options.fanoutChild ? CHILD_FANOUT_BOUNDARY_INSTRUCTIONS : CHILD_SUBAGENT_BOUNDARY_INSTRUCTIONS; const structured = process.env[STRUCTURED_OUTPUT_CAPTURE_ENV] ? `\n\n${STRUCTURED_OUTPUT_INSTRUCTIONS}` : ""; return `${boundary}${structured}\n\n${rewritten}`; } function isParentOnlySubagentMessage(message: unknown): boolean { const m = message as { role?: string; customType?: string }; if (m?.role !== "custom" || typeof m.customType !== "string") return false; if (m.customType === SUBAGENT_WATCHDOG_WARNING_TYPE) return true; return PARENT_ONLY_CUSTOM_MESSAGE_TYPES.has(m.customType); } function isSubagentToolResultMessage(message: unknown): boolean { const m = message as { role?: string; toolName?: string }; return m?.role === "toolResult" && m.toolName === "subagent"; } function isSubagentToolCallBlock(block: unknown): boolean { const b = block as { type?: string; name?: string }; return b?.type === "toolCall" && b.name === "subagent"; } function stripAssistantSubagentToolCallBlocks(message: unknown): unknown | undefined { const m = message as { role?: string; content?: unknown }; if (m?.role !== "assistant" || !Array.isArray(m.content)) return message; const filteredContent = m.content.filter((block) => !isSubagentToolCallBlock(block)); if (filteredContent.length === m.content.length) return message; if (filteredContent.length === 0) return undefined; return { ...m, content: filteredContent }; } export function stripParentOnlySubagentMessages(messages: unknown[]): unknown[] { const preserveCurrentFanoutToolHistory = process.env[SUBAGENT_FANOUT_CHILD_ENV] === "1"; let changed = false; const filtered: unknown[] = []; for (const message of messages) { if (isParentOnlySubagentMessage(message) || (!preserveCurrentFanoutToolHistory && isSubagentToolResultMessage(message))) { changed = true; continue; } const stripped = preserveCurrentFanoutToolHistory ? message : stripAssistantSubagentToolCallBlocks(message); if (stripped === undefined) { changed = true; continue; } if (stripped !== message) changed = true; filtered.push(stripped); } return changed ? filtered : messages; } export function formatSteerMessage(request: SteerRequest): string { return [ "Mid-run steering from the parent orchestrator:", "", request.message, "", "Incorporate this guidance at the next safe point. Do not restart the task unless the guidance explicitly asks you to.", ].join("\n"); } function registerToolBudget(pi: ExtensionAPI, budget: ResolvedToolBudget | undefined): void { if (!budget) return; let toolCount = 0; let softNudged = false; const sendUserMessage = (pi as { sendUserMessage?: (content: string, options: { deliverAs: "steer" }) => unknown }).sendUserMessage; const onRuntimeEvent = pi.on as unknown as (event: string, handler: (event: { toolName?: string }) => unknown) => void; onRuntimeEvent("tool_call", (event) => { const toolName = typeof event.toolName === "string" ? event.toolName : "tool"; toolCount++; if (budget.soft !== undefined && toolCount >= budget.soft && !softNudged) { softNudged = true; try { sendUserMessage?.(toolBudgetSoftNudge(budget, toolCount), { deliverAs: "steer" }); } catch { // Budget nudges are advisory; blocking below remains authoritative. } } if (!shouldBlockToolForBudget(budget, toolName, toolCount)) return undefined; return { block: true, reason: toolBudgetBlockedMessage(budget, toolName, toolCount) }; }); } export function registerSteeringInbox( pi: ExtensionAPI, deps: { watch?: typeof fs.watch; nativeRealpath?: (filePath: string) => string } = {}, ): void { const steerInbox = process.env[SUBAGENT_STEER_INBOX_ENV]?.trim(); if (!steerInbox) return; const capabilityPath = process.env[SUBAGENT_STEER_CAPABILITY_ENV]?.trim(); const ackDir = process.env[SUBAGENT_STEER_ACK_DIR_ENV]?.trim(); const sendUserMessage = (pi as { sendUserMessage?: (content: string, options: { deliverAs: "steer" }) => unknown }).sendUserMessage; const childIndex = Number(process.env[SUBAGENT_CHILD_INDEX_ENV]); const pending = new Map(); let disposed = false; let flushing = false; let started = false; let canSteer = typeof sendUserMessage === "function"; let watcher: fs.FSWatcher | undefined; let interval: NodeJS.Timeout | undefined; const acknowledge = (request: SteerRequest, state: "delivered" | "failed", message: string): void => { if (!ackDir || !Number.isInteger(childIndex) || childIndex < 0) return; writeSteerAckAt(steerAckPathFromDir(ackDir, request.id), { requestId: request.id, index: childIndex, ts: Date.now(), state, message, }); }; const publishCapability = (): void => { if (!capabilityPath || !Number.isInteger(childIndex) || childIndex < 0) return; writeSteerCapabilityAt(capabilityPath, { index: childIndex, pid: process.pid, readyAt: Date.now(), supported: canSteer }); }; const flush = (): void => { if (disposed || flushing) return; flushing = true; try { const requests = consumeSteerRequestsFromDir(steerInbox); for (let index = 0; index < requests.length; index++) { const request = requests[index]!; if (!canSteer || typeof sendUserMessage !== "function") { acknowledge(request, "failed", "Child Pi session does not support sendUserMessage steering."); continue; } const formatted = formatSteerMessage(request); const ids = pending.get(formatted) ?? []; ids.push(request.id); pending.set(formatted, ids); try { sendUserMessage(formatted, { deliverAs: "steer" }); } catch (error) { ids.pop(); if (ids.length === 0) pending.delete(formatted); acknowledge(request, "failed", error instanceof Error ? error.message : String(error)); for (const retry of requests.slice(index + 1)) writeSteerRequestToDir(steerInbox, retry); break; } } } finally { flushing = false; } }; const onInput = (event: unknown): undefined => { if (disposed || !event || typeof event !== "object") return undefined; const input = event as { source?: unknown; streamingBehavior?: unknown; text?: unknown; content?: unknown }; if (input.source !== "extension" || input.streamingBehavior !== "steer") return undefined; const text = typeof input.text === "string" ? input.text : typeof input.content === "string" ? input.content : undefined; if (!text) return undefined; const ids = pending.get(text); const requestId = ids?.shift(); if (!requestId) return undefined; if (ids?.length === 0) pending.delete(text); acknowledge({ type: "steer", id: requestId, ts: Date.now(), message: text }, "delivered", "Pi accepted the correlated steering input."); return undefined; }; const start = (): void => { if (started || disposed) return; try { fs.mkdirSync(steerInbox, { recursive: true }); publishCapability(); } catch { return; } started = true; try { watcher = (deps.watch ?? fs.watch)(resolveWatchPath(steerInbox, deps.nativeRealpath), () => flush()); watcher.on("error", () => {}); } catch { watcher = undefined; } interval = setInterval(flush, 250); interval.unref?.(); }; const activate = (): undefined => { start(); flush(); return undefined; }; const onRuntimeEvent = pi.on as unknown as (event: string, handler: (event: unknown) => unknown) => void; // Register input before the watcher so an accepted extension input cannot race request dispatch. onRuntimeEvent("input", onInput); onRuntimeEvent("session_start", () => start()); for (const eventName of ["message_start", "message_update", "message_end", "tool_execution_start", "tool_execution_end", "turn_end"] as const) { onRuntimeEvent(eventName, activate); } onRuntimeEvent("session_shutdown", () => { disposed = true; try { watcher?.close(); } catch {} if (interval) clearInterval(interval); }); } export default function registerSubagentPromptRuntime(pi: ExtensionAPI): void { registerSteeringInbox(pi); registerToolBudget(pi, decodeToolBudgetEnv(process.env[TOOL_BUDGET_ENV], { allowZero: process.env[TOOL_BUDGET_ZERO_AUTH_ENV] === "1" })); registerChildWatchdog(pi); const waitToolEnabled = resolveWaitToolConfig().enabled; const waitState = { baseCwd: "", currentSessionId: null, asyncJobs: new Map(), foregroundControls: new Map(), lastForegroundControlId: null, cleanupTimers: new Map(), lastUiContext: null, poller: null, completionSeen: new Map(), watcher: null, watcherRestartTimer: null, resultFileCoalescer: { schedule: () => false, clear: () => {} }, } as unknown as SubagentState; if (typeof pi.registerTool === "function") registerWaitTool(pi, waitState, waitToolEnabled); let nativeSupervisorClientRegistered = false; let nativeSupervisorFallbackRegistered = false; const registerNativeSupervisorClientOnce = (): void => { if (nativeSupervisorClientRegistered) return; nativeSupervisorClientRegistered = true; registerNativeSupervisorClient(pi, { includeIntercomFallback: false }); }; const registerNativeSupervisorFallbackOnce = (): void => { registerNativeSupervisorClientOnce(); if (nativeSupervisorFallbackRegistered) return; nativeSupervisorFallbackRegistered = true; registerNativeSupervisorClient(pi); }; const onRuntimeEvent = pi.on as unknown as (event: string, handler: (event: unknown) => unknown) => void; onRuntimeEvent("session_start", (_event: unknown, ctx: unknown) => { const sessionManager = (ctx as { sessionManager?: Parameters[0] } | undefined)?.sessionManager; waitState.currentSessionId = sessionManager ? resolveCurrentSessionId(sessionManager) : null; registerNativeSupervisorClientOnce(); if (readRequiredChildTools()?.includes("intercom")) registerNativeSupervisorFallbackOnce(); }); onRuntimeEvent("agent_start", () => { refreshChildToolDiagnostic(pi); }); onRuntimeEvent("agent_end", async (_event: unknown, ctx: unknown) => { if ((ctx as { hasUI?: boolean } | undefined)?.hasUI === true) return; await drainOutstandingWork({ state: waitState, events: pi.events }); }); const structuredOutputPath = process.env[STRUCTURED_OUTPUT_CAPTURE_ENV]; const structuredSchemaPath = process.env[STRUCTURED_OUTPUT_SCHEMA_ENV]; if (structuredOutputPath && structuredSchemaPath) { const schema = JSON.parse(fs.readFileSync(structuredSchemaPath, "utf-8")) as JsonSchemaObject; const parameters = createStructuredOutputToolParameters(schema); const registerTool = pi.registerTool as unknown as (tool: { name: string; label: string; description: string; parameters: unknown; execute: (_id: string, params: { value: unknown }) => Promise; }) => void; registerTool({ name: "structured_output", label: "Structured Output", description: "Submit the required final structured output for this subagent step. This terminates the step.", parameters: parameters as never, async execute(_id: string, params: { value: unknown }) { const validation = await validateStructuredOutputValue(schema, params.value); if (validation.status === "invalid") { throw new Error(`Structured output validation failed: ${validation.message}`); } fs.mkdirSync(path.dirname(structuredOutputPath), { recursive: true }); fs.writeFileSync(structuredOutputPath, JSON.stringify(params.value), { mode: 0o600 }); return { content: [{ type: "text", text: "Structured output captured." }], details: { path: structuredOutputPath }, terminate: true, }; }, }); } onRuntimeEvent("context", (event: { messages: unknown[] }) => { const messages = stripParentOnlySubagentMessages(event.messages); if (messages === event.messages) return undefined; return { messages }; }); onRuntimeEvent("before_agent_start", async (event: { systemPrompt: string }) => { registerNativeSupervisorFallbackOnce(); const intercomSessionName = process.env[SUBAGENT_INTERCOM_SESSION_NAME_ENV]?.trim(); if (intercomSessionName && typeof pi.setSessionName === "function") { pi.setSessionName(intercomSessionName); } const inheritProjectContext = readBooleanEnv(SUBAGENT_INHERIT_PROJECT_CONTEXT_ENV); const inheritSkills = readBooleanEnv(SUBAGENT_INHERIT_SKILLS_ENV); const fanoutChild = readBooleanEnv(SUBAGENT_FANOUT_CHILD_ENV); let rewritten = event.systemPrompt; if (inheritProjectContext !== undefined || inheritSkills !== undefined || fanoutChild !== undefined) { rewritten = rewriteSubagentPrompt(event.systemPrompt, { inheritProjectContext: inheritProjectContext ?? true, inheritSkills: inheritSkills ?? true, fanoutChild: fanoutChild === true, }); } if (rewritten === event.systemPrompt) return; return { systemPrompt: rewritten }; }); }