@@ -3285,6 +3285,205 @@ public async Task RunStreamingAsync_AutoApprovalRule_ApprovesMatchingToolAsync()
32853285
32863286 #endregion
32873287
3288+ #region Duplicate approval request IDs
3289+
3290+ /// <summary>
3291+ /// Verify that one request ID cannot refer to two different tool calls.
3292+ /// </summary>
3293+ [ Fact ]
3294+ public async Task RunAsync_DifferentCallsWithSameRequestId_ThrowsAsync ( )
3295+ {
3296+ // Arrange
3297+ var harmless = new ToolApprovalRequestContent (
3298+ "shared-id" ,
3299+ new FunctionCallContent ( "shared-call" , "BenignLookup" , new Dictionary < string , object ? > { [ "query" ] = "weather" } ) ) ;
3300+ var privileged = new ToolApprovalRequestContent (
3301+ "shared-id" ,
3302+ new FunctionCallContent ( "shared-call" , "RunPrivilegedShell" , new Dictionary < string , object ? > { [ "command" ] = "sensitive-command" } ) ) ;
3303+ var innerAgent = CreateMockAgent ( new AgentResponse ( [ new ChatMessage ( ChatRole . Assistant , [ harmless , privileged ] ) ] ) ) ;
3304+ var agent = new ToolApprovalAgent ( innerAgent . Object ) ;
3305+
3306+ // Act
3307+ var exception = await Assert . ThrowsAsync < InvalidOperationException > (
3308+ ( ) => agent . RunAsync ( [ new ChatMessage ( ChatRole . User , "Go" ) ] , new ChatClientAgentSession ( ) ) ) ;
3309+
3310+ // Assert
3311+ Assert . Contains ( "shared-id" , exception . Message , StringComparison . Ordinal ) ;
3312+ Assert . DoesNotContain ( "RunPrivilegedShell" , exception . Message , StringComparison . Ordinal ) ;
3313+ Assert . DoesNotContain ( "sensitive-command" , exception . Message , StringComparison . Ordinal ) ;
3314+ }
3315+
3316+ /// <summary>
3317+ /// Verify that repeating the exact same request under one ID is rejected.
3318+ /// </summary>
3319+ [ Fact ]
3320+ public async Task RunAsync_EquivalentCallsWithSameRequestId_ThrowsAsync ( )
3321+ {
3322+ // Arrange
3323+ var first = new ToolApprovalRequestContent (
3324+ "shared-id" ,
3325+ new FunctionCallContent ( "shared-call" , "BenignLookup" , new Dictionary < string , object ? > { [ "query" ] = "weather" } ) ) ;
3326+ var repeated = new ToolApprovalRequestContent (
3327+ "shared-id" ,
3328+ new FunctionCallContent ( "shared-call" , "BenignLookup" , new Dictionary < string , object ? > { [ "query" ] = "weather" } ) ) ;
3329+ var agent = new ToolApprovalAgent (
3330+ CreateMockAgent ( new AgentResponse ( [ new ChatMessage ( ChatRole . Assistant , [ first , repeated ] ) ] ) ) . Object ) ;
3331+
3332+ // Act & Assert
3333+ await Assert . ThrowsAsync < InvalidOperationException > (
3334+ ( ) => agent . RunAsync ( [ new ChatMessage ( ChatRole . User , "Go" ) ] , new ChatClientAgentSession ( ) ) ) ;
3335+ }
3336+
3337+ /// <summary>
3338+ /// Verify that streaming batches are checked before any conflicting request is surfaced.
3339+ /// </summary>
3340+ [ Fact ]
3341+ public async Task RunStreamingAsync_DifferentCallsWithSameRequestId_ThrowsBeforeYieldAsync ( )
3342+ {
3343+ // Arrange
3344+ var harmless = new ToolApprovalRequestContent ( "shared-id" , new FunctionCallContent ( "shared-call" , "BenignLookup" ) ) ;
3345+ var privileged = new ToolApprovalRequestContent ( "shared-id" , new FunctionCallContent ( "shared-call" , "RunPrivilegedShell" ) ) ;
3346+ var innerAgent = CreateMockStreamingAgent (
3347+ [
3348+ new AgentResponseUpdate ( ChatRole . Assistant , new List < AIContent > { harmless } ) ,
3349+ new AgentResponseUpdate ( ChatRole . Assistant , new List < AIContent > { privileged } ) ,
3350+ ] ) ;
3351+ var agent = new ToolApprovalAgent ( innerAgent . Object ) ;
3352+ var yielded = new List < AgentResponseUpdate > ( ) ;
3353+
3354+ // Act
3355+ var exception = await Assert . ThrowsAsync < InvalidOperationException > ( async ( ) =>
3356+ {
3357+ await foreach ( var update in agent . RunStreamingAsync (
3358+ [ new ChatMessage ( ChatRole . User , "Go" ) ] ,
3359+ new ChatClientAgentSession ( ) ) )
3360+ {
3361+ yielded . Add ( update ) ;
3362+ }
3363+ } ) ;
3364+
3365+ // Assert
3366+ Assert . Empty ( yielded ) ;
3367+ Assert . Contains ( "shared-id" , exception . Message , StringComparison . Ordinal ) ;
3368+ }
3369+
3370+ /// <summary>
3371+ /// Verify that the final non-streaming turn at the auto-approval cap also rejects conflicting IDs.
3372+ /// </summary>
3373+ [ Fact ]
3374+ public async Task RunAsync_CappedResponseWithDifferentCallsUnderSameRequestId_ThrowsAsync ( )
3375+ {
3376+ // Arrange
3377+ var callCount = 0 ;
3378+ var innerAgent = new Mock < AIAgent > ( ) ;
3379+ innerAgent
3380+ . Protected ( )
3381+ . Setup < Task < AgentResponse > > ( "RunCoreAsync" ,
3382+ ItExpr . IsAny < IEnumerable < ChatMessage > > ( ) ,
3383+ ItExpr . IsAny < AgentSession ? > ( ) ,
3384+ ItExpr . IsAny < AgentRunOptions ? > ( ) ,
3385+ ItExpr . IsAny < CancellationToken > ( ) )
3386+ . ReturnsAsync ( ( ) => ++ callCount == 1
3387+ ? new AgentResponse ( [ new ChatMessage ( ChatRole . Assistant ,
3388+ [ new ToolApprovalRequestContent ( "auto" , new FunctionCallContent ( "auto-call" , "AutoTool" ) ) ] ) ] )
3389+ : new AgentResponse ( [ new ChatMessage ( ChatRole . Assistant ,
3390+ [
3391+ new ToolApprovalRequestContent ( "shared-id" , new FunctionCallContent ( "shared-call" , "BenignLookup" ) ) ,
3392+ new ToolApprovalRequestContent ( "shared-id" , new FunctionCallContent ( "shared-call" , "RunPrivilegedShell" ) ) ,
3393+ ] ) ] ) ) ;
3394+ var agent = new ToolApprovalAgent ( innerAgent . Object , new ToolApprovalAgentOptions
3395+ {
3396+ AutoApprovalRules = [ ToolApprovalAgent . AllToolsAutoApprovalRule ] ,
3397+ MaxAutoApprovalIterations = 1 ,
3398+ } ) ;
3399+
3400+ // Act & Assert
3401+ await Assert . ThrowsAsync < InvalidOperationException > (
3402+ ( ) => agent . RunAsync ( [ new ChatMessage ( ChatRole . User , "Go" ) ] , new ChatClientAgentSession ( ) ) ) ;
3403+ }
3404+
3405+ /// <summary>
3406+ /// Verify that the final streaming turn at the auto-approval cap invalidates every ID repeated by a later update.
3407+ /// </summary>
3408+ [ Fact ]
3409+ public async Task RunStreamingAsync_CappedUpdatesWithMultipleDuplicateIds_InvalidatesAllAsync ( )
3410+ {
3411+ // Arrange
3412+ var callCount = 0 ;
3413+ var firstA = new ToolApprovalRequestContent ( "id-a" , new FunctionCallContent ( "call-a" , "ToolA" ) ) ;
3414+ var firstB = new ToolApprovalRequestContent ( "id-b" , new FunctionCallContent ( "call-b" , "ToolB" ) ) ;
3415+ var innerAgent = new Mock < AIAgent > ( ) ;
3416+ innerAgent
3417+ . Protected ( )
3418+ . Setup < IAsyncEnumerable < AgentResponseUpdate > > ( "RunCoreStreamingAsync" ,
3419+ ItExpr . IsAny < IEnumerable < ChatMessage > > ( ) ,
3420+ ItExpr . IsAny < AgentSession ? > ( ) ,
3421+ ItExpr . IsAny < AgentRunOptions ? > ( ) ,
3422+ ItExpr . IsAny < CancellationToken > ( ) )
3423+ . Returns ( ( ) => ++ callCount == 1
3424+ ? ToAsyncEnumerableAsync (
3425+ [
3426+ new AgentResponseUpdate ( ChatRole . Assistant , new List < AIContent >
3427+ {
3428+ new ToolApprovalRequestContent ( "auto" , new FunctionCallContent ( "auto-call" , "AutoTool" ) ) ,
3429+ } ) ,
3430+ ] )
3431+ : ToAsyncEnumerableAsync (
3432+ [
3433+ new AgentResponseUpdate ( ChatRole . Assistant , new List < AIContent >
3434+ {
3435+ firstA ,
3436+ firstB ,
3437+ } ) ,
3438+ new AgentResponseUpdate ( ChatRole . Assistant , new List < AIContent >
3439+ {
3440+ new ToolApprovalRequestContent ( "id-a" , new FunctionCallContent ( "repeated-call-a" , "DifferentToolA" ) ) ,
3441+ new ToolApprovalRequestContent ( "id-b" , new FunctionCallContent ( "repeated-call-b" , "DifferentToolB" ) ) ,
3442+ } ) ,
3443+ ] ) ) ;
3444+ var agent = new ToolApprovalAgent ( innerAgent . Object , new ToolApprovalAgentOptions
3445+ {
3446+ AutoApprovalRules = [ ToolApprovalAgent . AllToolsAutoApprovalRule ] ,
3447+ MaxAutoApprovalIterations = 1 ,
3448+ } ) ;
3449+ var session = new ChatClientAgentSession ( ) ;
3450+
3451+ // Act
3452+ var exception = await Assert . ThrowsAsync < InvalidOperationException > ( async ( ) =>
3453+ {
3454+ await foreach ( var _ in agent . RunStreamingAsync (
3455+ [ new ChatMessage ( ChatRole . User , "Go" ) ] ,
3456+ session ) )
3457+ {
3458+ }
3459+ } ) ;
3460+
3461+ var nextA = new ToolApprovalRequestContent ( "next-a" , new FunctionCallContent ( "next-call-a" , "ToolA" ) ) ;
3462+ var nextB = new ToolApprovalRequestContent ( "next-b" , new FunctionCallContent ( "next-call-b" , "ToolB" ) ) ;
3463+ var followUpAgent = new ToolApprovalAgent (
3464+ CreateMockAgent ( new AgentResponse ( [ new ChatMessage ( ChatRole . Assistant , [ nextA , nextB ] ) ] ) ) . Object ) ;
3465+
3466+ var firstResponse = await followUpAgent . RunAsync (
3467+ [ new ChatMessage ( ChatRole . User ,
3468+ [
3469+ firstA . CreateAlwaysApproveToolResponse ( ) ,
3470+ firstB . CreateAlwaysApproveToolResponse ( ) ,
3471+ ] ) ] ,
3472+ session ) ;
3473+
3474+ var secondResponse = await followUpAgent . RunAsync (
3475+ [ new ChatMessage ( ChatRole . User , [ nextA . CreateResponse ( approved : true ) ] ) ] ,
3476+ session ) ;
3477+
3478+ // Assert
3479+ Assert . Contains ( "id-a" , exception . Message , StringComparison . Ordinal ) ;
3480+ Assert . Contains ( "id-b" , exception . Message , StringComparison . Ordinal ) ;
3481+ Assert . Equal ( "next-a" , firstResponse . Messages . SelectMany ( m => m . Contents ) . OfType < ToolApprovalRequestContent > ( ) . Single ( ) . RequestId ) ;
3482+ Assert . Equal ( "next-b" , secondResponse . Messages . SelectMany ( m => m . Contents ) . OfType < ToolApprovalRequestContent > ( ) . Single ( ) . RequestId ) ;
3483+ }
3484+
3485+ #endregion
3486+
32883487 #region Auto-approval iteration cap
32893488
32903489 /// <summary>
0 commit comments