Skip to content

Commit 8ab068b

Browse files
authored
.NET: Reject duplicate tool approval request IDs (#9542)
* Add error handling for duplicate approval request ids * Address PR comments
1 parent 809cc04 commit 8ab068b

2 files changed

Lines changed: 269 additions & 6 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI/Harness/ToolApproval/ToolApprovalAgent.cs‎

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingA
263263

264264
if (cappedRequests is not null)
265265
{
266+
this.EnsureRequestIdsUnique(
267+
cappedRequests,
268+
state,
269+
session,
270+
includeSurfacedRequests: recordedAnyCappedRequest);
271+
266272
// The first update carrying requests supersedes any earlier batch; later updates in this
267273
// same turn add to it, since every one of them reaches the caller.
268274
if (recordedAnyCappedRequest)
@@ -348,6 +354,8 @@ protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingA
348354
yield break;
349355
}
350356

357+
this.EnsureRequestIdsUnique(streamedApprovalRequests, state, session, includeSurfacedRequests: false);
358+
351359
// 4. Classify the collected approval requests against standing rules and auto-approval rules.
352360
List<ToolApprovalRequestContent> unapproved = [];
353361
foreach (var tarc in streamedApprovalRequests)
@@ -418,6 +426,7 @@ private void RecordSurfacedApprovalRequestsFromMessages(
418426

419427
if (requests is not null)
420428
{
429+
this.EnsureRequestIdsUnique(requests, state, session, includeSurfacedRequests: false);
421430
ResetSurfacedApprovalRequests(state, requests);
422431
this._sessionState.SaveState(session, state);
423432
}
@@ -431,12 +440,6 @@ private void RecordSurfacedApprovalRequestsFromMessages(
431440
/// <remarks>
432441
/// A new batch from the inner agent supersedes any previous one, so stale entries from an abandoned
433442
/// approval cycle cannot later authorize a standing rule.
434-
/// <para>
435-
/// Request ids are unique by construction, so keying by id loses nothing: they derive from tool call ids, and
436-
/// inference services correlate a tool call to its result by that id alone. A duplicate id would already have
437-
/// broken that correlation upstream. Nothing a caller sends reaches this dictionary — every entry originates
438-
/// from the inner agent's own response — so a caller cannot manufacture a collision here.
439-
/// </para>
440443
/// </remarks>
441444
private static void ResetSurfacedApprovalRequests(ToolApprovalState state, IReadOnlyList<ToolApprovalRequestContent> requests)
442445
{
@@ -477,6 +480,61 @@ private static ToolApprovalRequestContent SnapshotRequest(ToolApprovalRequestCon
477480
return request;
478481
}
479482

483+
/// <summary>
484+
/// Rejects duplicate active approval request IDs.
485+
/// </summary>
486+
private void EnsureRequestIdsUnique(
487+
List<ToolApprovalRequestContent> requests,
488+
ToolApprovalState state,
489+
AgentSession? session,
490+
bool includeSurfacedRequests)
491+
{
492+
if (requests.Count == 0)
493+
{
494+
return;
495+
}
496+
497+
var knownRequestIds = new HashSet<string>(StringComparer.Ordinal);
498+
if (includeSurfacedRequests)
499+
{
500+
foreach (string requestId in state.SurfacedApprovalRequests.Keys)
501+
{
502+
knownRequestIds.Add(requestId);
503+
}
504+
}
505+
506+
HashSet<string>? duplicateRequestIds = null;
507+
508+
foreach (var request in requests)
509+
{
510+
if (knownRequestIds.Add(request.RequestId))
511+
{
512+
continue;
513+
}
514+
515+
(duplicateRequestIds ??= new(StringComparer.Ordinal)).Add(request.RequestId);
516+
}
517+
518+
if (duplicateRequestIds is null || duplicateRequestIds.Count == 0)
519+
{
520+
return;
521+
}
522+
523+
foreach (string requestId in duplicateRequestIds)
524+
{
525+
state.SurfacedApprovalRequests.Remove(requestId);
526+
state.QueuedApprovalRequests.RemoveAll(
527+
queued => string.Equals(queued.RequestId, requestId, StringComparison.Ordinal));
528+
state.CollectedApprovalResponses.RemoveAll(
529+
response => string.Equals(response.RequestId, requestId, StringComparison.Ordinal));
530+
}
531+
532+
this._sessionState.SaveState(session, state);
533+
534+
throw new InvalidOperationException(
535+
$"Tool approval request IDs '{string.Join("', '", duplicateRequestIds.OrderBy(id => id, StringComparer.Ordinal))}' were used more than once.");
536+
}
537+
480538
/// <summary>
481539
/// Re-evaluates queued approval requests against current rules and auto-approval rules, and auto-approves any that now match.
482540
/// </summary>
@@ -606,6 +664,12 @@ private async ValueTask<bool> ProcessAndQueueOutboundApprovalRequestsAsync(
606664
AgentRunOptions? options,
607665
IReadOnlyCollection<ChatMessage> requestMessages)
608666
{
667+
var requests = responseMessages
668+
.SelectMany(message => message.Contents)
669+
.OfType<ToolApprovalRequestContent>()
670+
.ToList();
671+
this.EnsureRequestIdsUnique(requests, state, session, includeSurfacedRequests: false);
672+
609673
// Pass 1: Scan all response messages and classify each approval request.
610674
// Auto-approved requests (matching a standing rule or auto-approval rule) have their
611675
// responses collected immediately, preserving the original request order, and are

‎dotnet/tests/Microsoft.Agents.AI.UnitTests/Harness/ToolApproval/ToolApprovalAgentTests.cs‎

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)