Skip to content

Commit 292c38e

Browse files
committed
fix(agents): preserve the zero-exit IPC grace across stderr close
Round-5 review finding [P2]: the stderr close handler finalized failure for any pending exit status, including clean exits, cutting short the 250 ms post-exit grace window a final IPC result may still need. Close-triggered finalization now skips clean exits entirely; their sole race breaker remains the grace timer armed on exit. A shared isCleanExit predicate keeps both call sites aligned. Adds red/green regression tests for exit->close->late-result delivery in both orderings plus a result-less grace-expiry case.
1 parent da6a547 commit 292c38e

2 files changed

Lines changed: 120 additions & 1 deletion

File tree

‎src/agents/tool-search-code-mode.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ export function appendToolSearchCodeStderrTail(
110110
return appendBoundedTextTailTracked(current, chunk, SESSION_TOOL_STDERR_TAIL_BYTES);
111111
}
112112

113+
function isCleanExit(
114+
status: { code: number | null; signal: NodeJS.Signals | null } | undefined,
115+
): boolean {
116+
return status !== undefined && status.code === 0 && status.signal === null;
117+
}
118+
113119
export function runCodeModeChild(params: {
114120
code: string;
115121
config: ToolSearchConfig;
@@ -209,6 +215,13 @@ export function runCodeModeChild(params: {
209215
};
210216
child.stderr?.on("close", () => {
211217
stderrClosed = true;
218+
if (isCleanExit(exitStatus)) {
219+
// A clean exit keeps its own outcome path: closing stderr must not
220+
// cut short the 250 ms post-exit grace window while its final IPC
221+
// result may still be queued for parent-side delivery. The grace
222+
// timer armed on exit stays the last-resort race breaker there.
223+
return;
224+
}
212225
finalizeExitFailure(true);
213226
});
214227
child.on("error", (error) => {
@@ -219,7 +232,7 @@ export function runCodeModeChild(params: {
219232
return;
220233
}
221234
exitStatus = { code, signal };
222-
if (code === 0 && signal === null) {
235+
if (isCleanExit(exitStatus)) {
223236
// A clean exit can race the final IPC result. This timer is a
224237
// last-resort race breaker: it does not wait for stderr close so a
225238
// stalled stream cannot hang resolution past this deadline.

‎src/agents/tool-search.stream-errors.test.ts‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,110 @@ describe("tool-search code-mode stream errors", () => {
284284
expect(caught?.message).toContain("short stderr note");
285285
expect(caught?.message).not.toMatch(/discarded at the 64 KiB retention cap/);
286286
});
287+
288+
it("resolves a clean exit whose stderr closed before the final IPC result arrives", async () => {
289+
testing.setToolSearchCodeModeSupportedForTest(true);
290+
testing.setToolSearchMinCodeTimeoutMsForTest(1000);
291+
292+
// Real-world ordering: the child exits cleanly, its stderr stream closes
293+
// right after, and only then does the final IPC result land inside the
294+
// parent's clean-exit grace window. The stderr close must not cut that
295+
// window short and reject with "child exited with 0".
296+
spawnMock.mockImplementationOnce(
297+
(_command: string, _args: readonly string[], _options: SpawnOptions): ChildProcess => {
298+
const { child, stderr } = createMockSpawnChild();
299+
process.nextTick(() => {
300+
child.emit("exit", 0, null);
301+
process.nextTick(() => {
302+
stderr?.emit("close");
303+
process.nextTick(() => {
304+
child.emit("message", { type: "result", ok: true, value: 42 });
305+
});
306+
});
307+
});
308+
return child as unknown as ChildProcess;
309+
},
310+
);
311+
312+
const runtime = new toolSearch.ToolSearchRuntime({}, toolSearch.resolveToolSearchConfig({}));
313+
314+
await expect(
315+
testing.runCodeModeChild({
316+
code: "return 42;",
317+
config: toolSearch.resolveToolSearchConfig({}),
318+
logs: [],
319+
parentToolCallId: "call-clean-exit-stderr-close-late-result",
320+
runtime,
321+
}),
322+
).resolves.toBe(42);
323+
});
324+
325+
it("resolves when stderr closes before a clean exit and the final IPC result arrives", async () => {
326+
testing.setToolSearchCodeModeSupportedForTest(true);
327+
testing.setToolSearchMinCodeTimeoutMsForTest(1000);
328+
329+
// Inverse ordering: stderr reaches 'close' while no exit status exists yet.
330+
// Close-triggered rendering must stay inert until exit proves failure;
331+
// a subsequent clean exit still resolves from its late IPC result.
332+
spawnMock.mockImplementationOnce(
333+
(_command: string, _args: readonly string[], _options: SpawnOptions): ChildProcess => {
334+
const { child, stderr } = createMockSpawnChild();
335+
process.nextTick(() => {
336+
stderr?.emit("close");
337+
process.nextTick(() => {
338+
child.emit("exit", 0, null);
339+
process.nextTick(() => {
340+
child.emit("message", { type: "result", ok: true, value: 43 });
341+
});
342+
});
343+
});
344+
return child as unknown as ChildProcess;
345+
},
346+
);
347+
348+
const runtime = new toolSearch.ToolSearchRuntime({}, toolSearch.resolveToolSearchConfig({}));
349+
350+
await expect(
351+
testing.runCodeModeChild({
352+
code: "return 43;",
353+
config: toolSearch.resolveToolSearchConfig({}),
354+
logs: [],
355+
parentToolCallId: "call-stderr-close-before-clean-exit-late-result",
356+
runtime,
357+
}),
358+
).resolves.toBe(43);
359+
});
360+
361+
it("still rejects a clean exit after the IPC grace window when no result arrives", async () => {
362+
testing.setToolSearchCodeModeSupportedForTest(true);
363+
testing.setToolSearchMinCodeTimeoutMsForTest(1000);
364+
365+
// Gating close-triggered finalization to nonzero/signaled exits must not
366+
// swallow genuinely result-less children: the clean-exit grace timer is
367+
// still the last-resort race breaker that reports "exited with 0".
368+
spawnMock.mockImplementationOnce(
369+
(_command: string, _args: readonly string[], _options: SpawnOptions): ChildProcess => {
370+
const { child, stderr } = createMockSpawnChild();
371+
process.nextTick(() => {
372+
child.emit("exit", 0, null);
373+
process.nextTick(() => {
374+
stderr?.emit("close");
375+
});
376+
});
377+
return child as unknown as ChildProcess;
378+
},
379+
);
380+
381+
const runtime = new toolSearch.ToolSearchRuntime({}, toolSearch.resolveToolSearchConfig({}));
382+
383+
await expect(
384+
testing.runCodeModeChild({
385+
code: "return null;",
386+
config: toolSearch.resolveToolSearchConfig({}),
387+
logs: [],
388+
parentToolCallId: "call-clean-exit-grace-expiry",
389+
runtime,
390+
}),
391+
).rejects.toThrow(/tool_search_code child exited with 0/);
392+
});
287393
});

0 commit comments

Comments
 (0)