@@ -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 ( / d i s c a r d e d a t t h e 6 4 K i B r e t e n t i o n c a p / ) ;
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 ( / t o o l _ s e a r c h _ c o d e c h i l d e x i t e d w i t h 0 / ) ;
392+ } ) ;
287393} ) ;
0 commit comments