import * as fs from "node:fs"; import * as path from "node:path"; import type { ExtensionAPI } from "@selesai/code"; import { registerNativeSupervisorClient } from "../../intercom/native-supervisor-channel.ts"; import { consumeSteerRequestsFromDir, writeSteerRequestToDir, type SteerRequest } from "../background/control-channel.ts"; import { SUBAGENT_FANOUT_CHILD_ENV, SUBAGENT_STEER_INBOX_ENV } from "./pi-args.ts"; import { STRUCTURED_OUTPUT_CAPTURE_ENV, STRUCTURED_OUTPUT_SCHEMA_ENV, validateStructuredOutputValue } from "./structured-output.ts"; import { TOOL_BUDGET_ENV, decodeToolBudgetEnv, shouldBlockToolForBudget, toolBudgetBlockedMessage, toolBudgetSoftNudge } from "./tool-budget.ts"; import type { JsonSchemaObject, ResolvedToolBudget } from "../../shared/types.ts"; const SUBAGENT_INHERIT_PROJECT_CONTEXT_ENV = "SELESAI_SUBAGENT_INHERIT_PROJECT_CONTEXT"; const SUBAGENT_INHERIT_SKILLS_ENV = "SELESAI_SUBAGENT_INHERIT_SKILLS"; export const SUBAGENT_INTERCOM_SESSION_NAME_ENV = "SELESAI_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 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 }; return m?.role === "custom" && typeof m.customType === "string" && 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) }; }); } function registerSteeringInbox(pi: ExtensionAPI): void { const steerInbox = process.env[SUBAGENT_STEER_INBOX_ENV]?.trim(); if (!steerInbox) return; const sendUserMessage = (pi as { sendUserMessage?: (content: string, options: { deliverAs: "steer" }) => unknown }).sendUserMessage; if (typeof sendUserMessage !== "function") return; let canSteer = false; let disposed = false; let flushing = false; let started = false; let watcher: fs.FSWatcher | undefined; let interval: NodeJS.Timeout | undefined; const flush = (): void => { if (disposed || flushing || !canSteer) return; flushing = true; try { const requests = consumeSteerRequestsFromDir(steerInbox); for (let index = 0; index < requests.length; index++) { const request = requests[index]!; try { sendUserMessage(formatSteerMessage(request), { deliverAs: "steer" }); } catch { for (const pending of requests.slice(index)) writeSteerRequestToDir(steerInbox, pending); break; } } } finally { flushing = false; } }; const start = (): void => { if (started || disposed) return; try { fs.mkdirSync(steerInbox, { recursive: true }); } catch { return; } started = true; try { watcher = fs.watch(steerInbox, () => flush()); watcher.on("error", () => {}); } catch { watcher = undefined; } interval = setInterval(flush, 250); interval.unref?.(); }; const activate = (): undefined => { start(); canSteer = true; flush(); return undefined; }; const onRuntimeEvent = pi.on as unknown as (event: string, handler: (event: unknown) => unknown) => void; 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])); 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", registerNativeSupervisorClientOnce); 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 = { type: "object", properties: { value: schema }, required: ["value"], additionalProperties: false, }; 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 = 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); if (inheritProjectContext === undefined && inheritSkills === undefined && fanoutChild === undefined) return; const rewritten = rewriteSubagentPrompt(event.systemPrompt, { inheritProjectContext: inheritProjectContext ?? true, inheritSkills: inheritSkills ?? true, fanoutChild: fanoutChild === true, }); if (rewritten === event.systemPrompt) return; return { systemPrompt: rewritten }; }); }