From 7c92bb81517d69ef579cdc98e51081d05ba4444d Mon Sep 17 00:00:00 2001 From: Ziphyrien <111620796+Ziphyrien@users.noreply.github.com> Date: Sat, 21 Mar 2026 00:12:24 +0800 Subject: [PATCH] fix(subagent): reuse current pi invocation for child agents closes #2464 (#2465) --- .../examples/extensions/subagent/index.ts | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/coding-agent/examples/extensions/subagent/index.ts b/packages/coding-agent/examples/extensions/subagent/index.ts index 316979c4..d7d7c43d 100644 --- a/packages/coding-agent/examples/extensions/subagent/index.ts +++ b/packages/coding-agent/examples/extensions/subagent/index.ts @@ -217,6 +217,21 @@ async function writePromptToTempFile(agentName: string, prompt: string): Promise return { dir: tmpDir, filePath }; } +function getPiInvocation(args: string[]): { command: string; args: string[] } { + const currentScript = process.argv[1]; + if (currentScript && fs.existsSync(currentScript)) { + return { command: process.execPath, args: [currentScript, ...args] }; + } + + const execName = path.basename(process.execPath).toLowerCase(); + const isGenericRuntime = /^(node|bun)(\.exe)?$/.test(execName); + if (!isGenericRuntime) { + return { command: process.execPath, args }; + } + + return { command: "pi", args }; +} + type OnUpdateCallback = (partial: AgentToolResult) => void; async function runSingleAgent( @@ -286,7 +301,12 @@ async function runSingleAgent( let wasAborted = false; const exitCode = await new Promise((resolve) => { - const proc = spawn("pi", args, { cwd: cwd ?? defaultCwd, shell: false, stdio: ["ignore", "pipe", "pipe"] }); + const invocation = getPiInvocation(args); + const proc = spawn(invocation.command, invocation.args, { + cwd: cwd ?? defaultCwd, + shell: false, + stdio: ["ignore", "pipe", "pipe"], + }); let buffer = ""; const processLine = (line: string) => {