@@ -487,16 +487,31 @@ await Assert.ThrowsAsync<InvalidOperationException>(() =>
487487 public async Task ExecuteAsync_OnContinuation_WhenOperationCancelled_DoesNotEmitFailedAsync ( )
488488 {
489489 // Arrange
490+ var mockSessionStore = new Mock < AgentSessionStore > { CallBase = true } ;
491+ mockSessionStore
492+ . Setup ( x => x . GetSessionAsync (
493+ It . IsAny < AIAgent > ( ) ,
494+ It . IsAny < AgentSessionStoreKey > ( ) ,
495+ It . IsAny < CancellationToken > ( ) ) )
496+ . ReturnsAsync ( new TestAgentSession ( ) ) ;
497+ mockSessionStore
498+ . Setup ( x => x . SaveSessionAsync (
499+ It . IsAny < AIAgent > ( ) ,
500+ It . IsAny < AgentSessionStoreKey > ( ) ,
501+ It . IsAny < AgentSession > ( ) ,
502+ It . IsAny < CancellationToken > ( ) ) )
503+ . Returns ( ( ) => new ValueTask ( Task . FromException ( new TimeoutException ( "Session save failed" ) ) ) ) ;
504+
490505 int callCount = 0 ;
491506 Mock < AIAgent > agentMock = CreateAgentMockWithCallCount ( ref callCount , _ =>
492507 throw new OperationCanceledException ( "Cancelled" ) ) ;
493- A2AAgentHandler handler = CreateHandler ( agentMock ) ;
508+ A2AAgentHandler handler = CreateHandler ( agentMock , agentSessionStore : mockSessionStore . Object ) ;
494509
495510 // Act & Assert
496511 var events = new EventCollector ( ) ;
497512 var eventQueue = new AgentEventQueue ( ) ;
498513 var readerTask = ReadEventsAsync ( eventQueue , events ) ;
499- await Assert . ThrowsAsync < OperationCanceledException > ( ( ) =>
514+ var exception = await Assert . ThrowsAsync < OperationCanceledException > ( ( ) =>
500515 handler . ExecuteAsync (
501516 new RequestContext
502517 {
@@ -509,11 +524,19 @@ await Assert.ThrowsAsync<OperationCanceledException>(() =>
509524 } ,
510525 eventQueue ,
511526 CancellationToken . None ) ) ;
527+ Assert . Equal ( "Cancelled" , exception . Message ) ;
512528 eventQueue . Complete ( null ) ;
513529 await readerTask ;
514530
515531 // Assert - should NOT have emitted any status (OperationCanceledException is re-thrown without marking Failed)
516532 Assert . Empty ( events . StatusUpdates ) ;
533+ mockSessionStore . Verify (
534+ x => x . SaveSessionAsync (
535+ It . IsAny < AIAgent > ( ) ,
536+ It . Is < AgentSessionStoreKey > ( key => key . SessionId == "ctx-1" ) ,
537+ It . IsAny < AgentSession > ( ) ,
538+ It . Is < CancellationToken > ( ct => ct == CancellationToken . None ) ) ,
539+ Times . Once ) ;
517540 }
518541
519542 /// <summary>
@@ -2161,6 +2184,54 @@ public async Task ExecuteAsync_OnContinuation_DoesNotInvokeDynamicModeCallbackAs
21612184 Assert . False ( callbackInvoked ) ;
21622185 }
21632186
2187+ /// <summary>
2188+ /// Verifies that a session save failure still propagates when agent execution succeeds.
2189+ /// </summary>
2190+ [ Fact ]
2191+ public async Task ExecuteAsync_NonStreaming_WhenRunSucceedsAndSaveFails_PropagatesSaveFailureAsync ( )
2192+ {
2193+ // Arrange
2194+ var mockSessionStore = new Mock < AgentSessionStore > { CallBase = true } ;
2195+ mockSessionStore
2196+ . Setup ( x => x . GetSessionAsync (
2197+ It . IsAny < AIAgent > ( ) ,
2198+ It . IsAny < AgentSessionStoreKey > ( ) ,
2199+ It . IsAny < CancellationToken > ( ) ) )
2200+ . ReturnsAsync ( new TestAgentSession ( ) ) ;
2201+ mockSessionStore
2202+ . Setup ( x => x . SaveSessionAsync (
2203+ It . IsAny < AIAgent > ( ) ,
2204+ It . IsAny < AgentSessionStoreKey > ( ) ,
2205+ It . IsAny < AgentSession > ( ) ,
2206+ It . IsAny < CancellationToken > ( ) ) )
2207+ . Returns ( ( ) => new ValueTask ( Task . FromException ( new TimeoutException ( "Session save failed" ) ) ) ) ;
2208+
2209+ AgentResponse response = new ( [ new ChatMessage ( ChatRole . Assistant , "Reply" ) ] ) ;
2210+ A2AAgentHandler handler = CreateHandler ( CreateAgentMockWithResponse ( response ) , agentSessionStore : mockSessionStore . Object ) ;
2211+
2212+ // Act
2213+ var eventQueue = new AgentEventQueue ( ) ;
2214+ var exception = await Assert . ThrowsAsync < TimeoutException > ( ( ) =>
2215+ handler . ExecuteAsync (
2216+ new RequestContext
2217+ {
2218+ TaskId = "" , ContextId = "ctx-save-fail" , StreamingResponse = false ,
2219+ Message = new Message { MessageId = "test-id" , Role = Role . User , Parts = [ new Part { Text = "Hello" } ] }
2220+ } ,
2221+ eventQueue ,
2222+ CancellationToken . None ) ) ;
2223+
2224+ // Assert
2225+ Assert . Equal ( "Session save failed" , exception . Message ) ;
2226+ mockSessionStore . Verify (
2227+ x => x . SaveSessionAsync (
2228+ It . IsAny < AIAgent > ( ) ,
2229+ It . Is < AgentSessionStoreKey > ( key => key . SessionId == "ctx-save-fail" ) ,
2230+ It . IsAny < AgentSession > ( ) ,
2231+ It . Is < CancellationToken > ( ct => ct == CancellationToken . None ) ) ,
2232+ Times . Once ) ;
2233+ }
2234+
21642235 /// <summary>
21652236 /// Verifies that in the non-streaming endpoint path, SaveSessionAsync is called with
21662237 /// CancellationToken.None even when RunStreamingAsync throws an exception.
0 commit comments