@@ -432,11 +432,18 @@ public async Task SendAndWait_Throws_On_Timeout()
432432 {
433433 var session = await CreateSessionAsync ( ) ;
434434
435+ var sessionIdleTask = TestHelper . GetNextEventOfTypeAsync < SessionIdleEvent > ( session ) ;
436+
435437 // Use a slow command to ensure timeout triggers before completion
436438 var ex = await Assert . ThrowsAsync < TimeoutException > ( ( ) =>
437439 session . SendAndWaitAsync ( new MessageOptions { Prompt = "Run 'sleep 2 && echo done'" } , TimeSpan . FromMilliseconds ( 100 ) ) ) ;
438440
439441 Assert . Contains ( "timed out" , ex . Message ) ;
442+
443+ // The timeout only cancels the client-side wait; abort the agent and wait for idle
444+ // so leftover requests don't leak into subsequent tests.
445+ await session . AbortAsync ( ) ;
446+ await sessionIdleTask ;
440447 }
441448
442449 [ Fact ]
@@ -446,6 +453,7 @@ public async Task SendAndWait_Throws_OperationCanceledException_When_Token_Cance
446453
447454 // Set up wait for tool execution to start BEFORE sending
448455 var toolStartTask = TestHelper . GetNextEventOfTypeAsync < ToolExecutionStartEvent > ( session ) ;
456+ var sessionIdleTask = TestHelper . GetNextEventOfTypeAsync < SessionIdleEvent > ( session ) ;
449457
450458 using var cts = new CancellationTokenSource ( ) ;
451459
@@ -461,6 +469,12 @@ public async Task SendAndWait_Throws_OperationCanceledException_When_Token_Cance
461469 cts . Cancel ( ) ;
462470
463471 await Assert . ThrowsAnyAsync < OperationCanceledException > ( ( ) => sendTask ) ;
472+
473+ // Cancelling the token only cancels the client-side wait, not the server-side agent loop.
474+ // Explicitly abort so the agent stops, then wait for idle to ensure we're not still
475+ // running this agent's operations in the context of a subsequent test.
476+ await session . AbortAsync ( ) ;
477+ await sessionIdleTask ;
464478 }
465479
466480 [ Fact ]
0 commit comments