diff --git a/src/wrapper/cli.mjs b/src/wrapper/cli.mjs index 67770d6..9eeaaa4 100644 --- a/src/wrapper/cli.mjs +++ b/src/wrapper/cli.mjs @@ -3,6 +3,13 @@ import { spawn } from "node:child_process"; import { normalizePiEvent } from "./normalize.mjs"; import { renderHeader, renderEventLine } from "./render.mjs"; +const SEMANTIC_EXIT_GRACE_MS = 250; +const FORCED_EXIT_GRACE_MS = 250; + +async function sleep(ms) { + await new Promise((resolve) => setTimeout(resolve, ms)); +} + async function appendJsonLine(path, value) { await appendBestEffort(path, `${JSON.stringify(value)}\n`); } @@ -68,8 +75,26 @@ async function runWrapper(meta, startedAt) { let stdoutBuffer = ""; let stderrText = ""; let spawnError; + let childClosed = false; + let completionCleanupStarted = false; + let child; let queue = Promise.resolve(); + const forceChildExitAfterSemanticCompletion = async () => { + if (completionCleanupStarted) return; + completionCleanupStarted = true; + + await sleep(SEMANTIC_EXIT_GRACE_MS); + if (childClosed) return; + + child.kill("SIGTERM"); + + await sleep(FORCED_EXIT_GRACE_MS); + if (childClosed) return; + + child.kill("SIGKILL"); + }; + const enqueue = (work) => { queue = queue.then(work, work); return queue; @@ -98,6 +123,10 @@ async function runWrapper(meta, startedAt) { resolvedModel = normalized.model ?? resolvedModel; stopReason = normalized.stopReason ?? stopReason; usage = normalized.usage ?? usage; + + if (normalized.stopReason) { + void forceChildExitAfterSemanticCompletion(); + } } }; @@ -112,7 +141,7 @@ async function runWrapper(meta, startedAt) { childEnv.PI_SUBAGENTS_GITHUB_COPILOT_INITIATOR = "agent"; } - const child = spawn("pi", args, { + child = spawn("pi", args, { cwd: meta.cwd, env: childEnv, stdio: ["ignore", "pipe", "pipe"], @@ -151,10 +180,12 @@ async function runWrapper(meta, startedAt) { child.on("error", (error) => { spawnError = error; + childClosed = true; finish(1); }); child.on("close", (code) => { + childClosed = true; finish(code ?? (spawnError ? 1 : 0)); }); }); diff --git a/src/wrapper/cli.test.ts b/src/wrapper/cli.test.ts index 2e64db5..91114ac 100644 --- a/src/wrapper/cli.test.ts +++ b/src/wrapper/cli.test.ts @@ -191,6 +191,66 @@ test("wrapper exits and writes result.json when the pi child cannot be spawned", assert.match(result.errorMessage ?? "", /ENOENT|not found|spawn pi/i); }); +test("wrapper exits and writes result.json after terminal output even if the pi child lingers", async () => { + const dir = await mkdtemp(join(tmpdir(), "pi-subagents-wrapper-")); + const metaPath = join(dir, "meta.json"); + const resultPath = join(dir, "result.json"); + const piPath = join(dir, "pi"); + + await writeFile( + piPath, + [ + `#!${process.execPath}`, + "console.log(JSON.stringify({type:'message_end',message:{role:'assistant',content:[{type:'text',text:'done'}],model:'openai/gpt-5',stopReason:'stop'}}));", + "setTimeout(() => {}, 10_000);", + ].join("\n"), + "utf8", + ); + await chmod(piPath, 0o755); + + await writeFile( + metaPath, + JSON.stringify( + { + runId: "run-1", + mode: "single", + agent: "scout", + agentSource: "builtin", + task: "inspect auth", + cwd: dir, + requestedModel: "openai/gpt-5", + resolvedModel: "openai/gpt-5", + startedAt: "2026-04-09T00:00:00.000Z", + sessionPath: join(dir, "child-session.jsonl"), + eventsPath: join(dir, "events.jsonl"), + resultPath, + stdoutPath: join(dir, "stdout.log"), + stderrPath: join(dir, "stderr.log"), + transcriptPath: join(dir, "transcript.log"), + systemPromptPath: join(dir, "system-prompt.md"), + }, + null, + 2, + ), + "utf8", + ); + + const wrapperPath = join(dirname(fileURLToPath(import.meta.url)), "cli.mjs"); + const child = spawn(process.execPath, [wrapperPath, metaPath], { + env: { ...process.env, PATH: dir }, + stdio: ["ignore", "pipe", "pipe"], + }); + + const exitCode = await waitForExit(child); + assert.equal(exitCode, 0); + + const result = JSON.parse(await readFile(resultPath, "utf8")); + assert.equal(result.exitCode, 0); + assert.equal(result.stopReason, "stop"); + assert.equal(result.finalText, "done"); + assert.equal(result.resolvedModel, "openai/gpt-5"); +}); + test("wrapper still writes result.json when transcript/stdout artifact writes fail", async () => { const dir = await mkdtemp(join(tmpdir(), "pi-subagents-wrapper-")); const metaPath = join(dir, "meta.json");