Skip to content

Commit 74a1440

Browse files
atty57westey-m
andauthored
.NET: Bound the tool-approval auto-approval loop (#7472) (#7474)
* .NET: Bound the tool-approval auto-approval loop (#7472) `ToolApprovalAgent` re-invoked the inner agent from two unbounded `while (true)` loops whenever every surfaced approval request was auto-approved. Each pass is a fresh `InnerAgent.RunAsync` / `RunStreamingAsync` call, so a per-request cap such as `FunctionInvokingChatClient.MaximumIterationsPerRequest` restarts every time and cannot bound the chain. Under `AllToolsAutoApprovalRule` a model that keeps requesting an auto-approved tool therefore drives billable model calls indefinitely; the reporter measured 100M+ tokens over three days. Adds `ToolApprovalAgentOptions.MaxAutoApprovalIterations` (default `ToolApprovalAgent.DefaultMaxAutoApprovalIterations`, 10) and bounds both loops. Naming, default and `Throw.IfLessThan` validation follow the existing `LoopAgent.DefaultMaxIterations` / `LoopAgentOptions.MaxIterations` convention in this assembly. On reaching the cap the agent takes one final inner turn without auto-approving again, so a remaining approval request is surfaced to the caller to decide. Returning early instead would hand back an empty response, because `ProcessAndQueueOutboundApprovalRequestsAsync` strips every approval request once they are all auto-approved -- the case the loop exists to avoid. This mirrors the Python behaviour, which logs and issues one final request with tools disabled once its iteration budget is spent (`_tools.py`). Python is not affected: it caps at `DEFAULT_MAX_ITERATIONS` (40) and persists `attempt_count` in the budget state across approval resumes, so a resumed run continues the count rather than restarting it. Tests: the runaway is reproduced on both the streaming and non-streaming paths with an inner agent that never stops requesting an auto-approved tool. Inner invocations equal the cap plus the final turn, and scale with the configured cap, so the assertions fail if the bound is removed. No sample changes: with the loop bounded, Agent_Step01, Agent_Step06, Agent_Step07 and Hosted-AgentSkills are safe as written. * .NET: Add Arrange/Act/Assert comments to the cap constructor test Matches the test convention documented in dotnet/AGENTS.md and used by the surrounding tests in this file. * Increase default max auto approval iterations to 40 * Apply suggestion from @westey-m Co-authored-by: westey <[email protected]> * Update comments in ToolApprovalAgent.cs --------- Co-authored-by: westey <[email protected]>
1 parent 5949547 commit 74a1440

3 files changed

Lines changed: 209 additions & 2 deletions

File tree

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading;
1010
using System.Threading.Tasks;
1111
using Microsoft.Extensions.AI;
12+
using Microsoft.Shared.Diagnostics;
1213

1314
namespace Microsoft.Agents.AI;
1415

@@ -28,6 +29,9 @@ namespace Microsoft.Agents.AI;
2829
/// first is returned to the caller while the rest are queued. On subsequent calls, queued items are re-evaluated
2930
/// against rules (which may have been updated by the caller's "always approve" response) and presented one at a time.
3031
/// Once all queued requests are resolved, the collected responses are injected and the inner agent is called again.
32+
/// This one-at-a-time behavior no longer applies once the auto-approval cap
33+
/// (<see cref="ToolApprovalAgentOptions.MaxAutoApprovalIterations"/>) is reached: the final inner turn is returned
34+
/// as-is, so more than one approval request may be surfaced to the caller at once.
3135
/// </item>
3236
/// <item>
3337
/// <b>Inbound (caller to agent):</b> When the caller sends an <see cref="AlwaysApproveToolApprovalResponseContent"/>,
@@ -47,9 +51,13 @@ namespace Microsoft.Agents.AI;
4751
/// </remarks>
4852
public sealed class ToolApprovalAgent : DelegatingAIAgent
4953
{
54+
/// <summary>The default value used for <see cref="ToolApprovalAgentOptions.MaxAutoApprovalIterations"/> when none is specified.</summary>
55+
public const int DefaultMaxAutoApprovalIterations = 40;
56+
5057
private readonly ProviderSessionState<ToolApprovalState> _sessionState;
5158
private readonly JsonSerializerOptions _jsonSerializerOptions;
5259
private readonly Func<ToolAutoApprovalRuleContext, ValueTask<bool>>[]? _autoApprovalRules;
60+
private readonly int _maxAutoApprovalIterations;
5361

5462
/// <summary>
5563
/// Initializes a new instance of the <see cref="ToolApprovalAgent"/> class.
@@ -65,6 +73,8 @@ public ToolApprovalAgent(AIAgent innerAgent, ToolApprovalAgentOptions? options =
6573
{
6674
this._jsonSerializerOptions = options?.JsonSerializerOptions ?? AgentJsonUtilities.DefaultOptions;
6775
this._autoApprovalRules = options?.AutoApprovalRules?.ToArray();
76+
this._maxAutoApprovalIterations = Throw.IfLessThan(
77+
options?.MaxAutoApprovalIterations ?? DefaultMaxAutoApprovalIterations, 1);
6878
this._sessionState = new ProviderSessionState<ToolApprovalState>(
6979
_ => new ToolApprovalState(),
7080
"toolApprovalState",
@@ -125,11 +135,25 @@ protected override async Task<AgentResponse> RunCoreAsync(
125135
// 3. Call the inner agent in a loop. If the inner agent returns approval requests
126136
// that are ALL auto-approved by standing rules, we immediately re-call with the
127137
// collected approval responses injected. This avoids returning empty responses.
128-
while (true)
138+
//
139+
// The loop is bounded by _maxAutoApprovalIterations. Each pass is a fresh inner
140+
// invocation, so a per-request cap (FunctionInvokingChatClient.MaximumIterationsPerRequest)
141+
// restarts every time and cannot bound it; without a cap here a model that keeps
142+
// requesting an auto-approved tool bills indefinitely.
143+
for (int iteration = 0; ; iteration++)
129144
{
130145
// Inject any collected approval responses as a user message ahead of the caller's messages.
131146
var processedMessages = this.InjectCollectedResponses(callerMessages, state, session);
132147

148+
if (iteration >= this._maxAutoApprovalIterations)
149+
{
150+
// Cap reached: take one final turn without auto-approving again, so any approval
151+
// request it surfaces goes to the caller to decide rather than continuing the chain.
152+
// Returning here without this call would hand back a response whose approval requests
153+
// were already stripped — the empty response the loop exists to avoid.
154+
return await this.InnerAgent.RunAsync(processedMessages, session, options, cancellationToken).ConfigureAwait(false);
155+
}
156+
133157
var response = await this.InnerAgent.RunAsync(processedMessages, session, options, cancellationToken).ConfigureAwait(false);
134158

135159
// Classify approval requests: auto-approve matching, queue excess, keep first unapproved.
@@ -173,11 +197,26 @@ protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingA
173197
// 3. Stream from the inner agent in a loop. If all approval requests from the stream
174198
// are auto-approved by standing rules, we immediately re-stream with the collected
175199
// approval responses injected. This avoids returning empty streams.
176-
while (true)
200+
//
201+
// Bounded by _maxAutoApprovalIterations for the same reason as the non-streaming path:
202+
// every pass is a fresh inner invocation, so no per-request cap can bound it.
203+
for (int iteration = 0; ; iteration++)
177204
{
178205
// Inject any collected approval responses as a user message ahead of the caller's messages.
179206
var processedMessages = this.InjectCollectedResponses(callerMessages, state, session);
180207

208+
if (iteration >= this._maxAutoApprovalIterations)
209+
{
210+
// Cap reached: take one final turn without auto-approving again. Updates are yielded
211+
// as-is, so any approval request reaches the caller to decide instead of continuing.
212+
await foreach (var update in this.InnerAgent.RunStreamingAsync(processedMessages, session, options, cancellationToken).ConfigureAwait(false))
213+
{
214+
yield return update;
215+
}
216+
217+
yield break;
218+
}
219+
181220
// Stream from the inner agent. Non-approval content is yielded immediately.
182221
// Approval requests are collected (not yielded) so we can classify the full batch.
183222
List<ToolApprovalRequestContent> streamedApprovalRequests = [];

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,24 @@ public class ToolApprovalAgentOptions
4646
/// </para>
4747
/// </remarks>
4848
public IEnumerable<Func<ToolAutoApprovalRuleContext, ValueTask<bool>>>? AutoApprovalRules { get; set; }
49+
50+
/// <summary>
51+
/// Gets or sets the safety cap on how many times the inner agent is re-invoked within a single run
52+
/// because every surfaced approval request was auto-approved, or <see langword="null"/> to use
53+
/// <see cref="ToolApprovalAgent.DefaultMaxAutoApprovalIterations"/>.
54+
/// </summary>
55+
/// <remarks>
56+
/// <para>
57+
/// Each re-invocation is a fresh call to the inner agent, so a per-request cap such as
58+
/// <c>FunctionInvokingChatClient.MaximumIterationsPerRequest</c> restarts every time and cannot bound this
59+
/// loop. Without this cap a model that keeps requesting an auto-approved tool, drives an unbounded sequence of
60+
/// billable model calls.
61+
/// </para>
62+
/// <para>
63+
/// On reaching the cap the agent performs one final inner invocation without auto-approving again, so any
64+
/// remaining approval request is surfaced to the caller to decide instead of being approved automatically.
65+
/// Raise this value if you intend to allow longer auto-approval chains.
66+
/// </para>
67+
/// </remarks>
68+
public int? MaxAutoApprovalIterations { get; set; }
4969
}

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

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,4 +2392,152 @@ public async Task RunStreamingAsync_AutoApprovalRule_ApprovesMatchingToolAsync()
23922392
}
23932393

23942394
#endregion
2395+
2396+
#region Auto-approval iteration cap
2397+
2398+
/// <summary>
2399+
/// Verify that a model which never stops requesting an auto-approved tool cannot drive an
2400+
/// unbounded number of inner invocations. Regression test for the runaway auto-approval loop
2401+
/// where every pass is a fresh inner call, so no per-request cap can bound it.
2402+
/// </summary>
2403+
[Fact]
2404+
public async Task RunAsync_AutoApprovedToolRequestedForever_StopsAtIterationCapAsync()
2405+
{
2406+
// Arrange — the inner agent always asks for an auto-approved tool and never answers.
2407+
var session = new ChatClientAgentSession();
2408+
var callCount = 0;
2409+
var innerAgent = new Mock<AIAgent>();
2410+
innerAgent
2411+
.Protected()
2412+
.Setup<Task<AgentResponse>>("RunCoreAsync",
2413+
ItExpr.IsAny<IEnumerable<ChatMessage>>(),
2414+
ItExpr.IsAny<AgentSession?>(),
2415+
ItExpr.IsAny<AgentRunOptions?>(),
2416+
ItExpr.IsAny<CancellationToken>())
2417+
.ReturnsAsync(() =>
2418+
{
2419+
callCount++;
2420+
return new AgentResponse([new ChatMessage(ChatRole.Assistant,
2421+
[new ToolApprovalRequestContent($"req{callCount}", new FunctionCallContent($"call{callCount}", "load_skill"))])]);
2422+
});
2423+
2424+
var options = new ToolApprovalAgentOptions
2425+
{
2426+
AutoApprovalRules = [ToolApprovalAgent.AllToolsAutoApprovalRule],
2427+
MaxAutoApprovalIterations = 3,
2428+
};
2429+
var agent = new ToolApprovalAgent(innerAgent.Object, options);
2430+
2431+
// Act
2432+
var response = await agent.RunAsync([new ChatMessage(ChatRole.User, "Hi")], session);
2433+
2434+
// Assert — 3 auto-approving passes plus one final turn that does not auto-approve.
2435+
Assert.Equal(4, callCount);
2436+
2437+
// The final turn's approval request is surfaced to the caller instead of being auto-approved,
2438+
// so the caller regains control rather than receiving the stripped (empty) response.
2439+
Assert.NotEmpty(response.Messages.SelectMany(m => m.Contents).OfType<ToolApprovalRequestContent>());
2440+
}
2441+
2442+
/// <summary>
2443+
/// Verify the streaming path is bounded by the same cap as the non-streaming path.
2444+
/// </summary>
2445+
[Fact]
2446+
public async Task RunStreamingAsync_AutoApprovedToolRequestedForever_StopsAtIterationCapAsync()
2447+
{
2448+
// Arrange
2449+
var session = new ChatClientAgentSession();
2450+
var callCount = 0;
2451+
var innerAgent = new Mock<AIAgent>();
2452+
innerAgent
2453+
.Protected()
2454+
.Setup<IAsyncEnumerable<AgentResponseUpdate>>("RunCoreStreamingAsync",
2455+
ItExpr.IsAny<IEnumerable<ChatMessage>>(),
2456+
ItExpr.IsAny<AgentSession?>(),
2457+
ItExpr.IsAny<AgentRunOptions?>(),
2458+
ItExpr.IsAny<CancellationToken>())
2459+
.Returns(() =>
2460+
{
2461+
callCount++;
2462+
return ToAsyncEnumerableAsync([
2463+
new AgentResponseUpdate(ChatRole.Assistant,
2464+
new List<AIContent> { new ToolApprovalRequestContent($"req{callCount}", new FunctionCallContent($"call{callCount}", "load_skill")) })
2465+
]);
2466+
});
2467+
2468+
var options = new ToolApprovalAgentOptions
2469+
{
2470+
AutoApprovalRules = [ToolApprovalAgent.AllToolsAutoApprovalRule],
2471+
MaxAutoApprovalIterations = 3,
2472+
};
2473+
var agent = new ToolApprovalAgent(innerAgent.Object, options);
2474+
2475+
// Act
2476+
var updates = new List<AgentResponseUpdate>();
2477+
await foreach (var update in agent.RunStreamingAsync([new ChatMessage(ChatRole.User, "Hi")], session))
2478+
{
2479+
updates.Add(update);
2480+
}
2481+
2482+
// Assert — bounded the same way, and the final turn's request reaches the caller.
2483+
Assert.Equal(4, callCount);
2484+
Assert.NotEmpty(updates.SelectMany(u => u.Contents).OfType<ToolApprovalRequestContent>());
2485+
}
2486+
2487+
/// <summary>
2488+
/// Verify that the cap defaults to <see cref="ToolApprovalAgent.DefaultMaxAutoApprovalIterations"/>
2489+
/// when the caller does not configure one.
2490+
/// </summary>
2491+
[Fact]
2492+
public async Task RunAsync_NoCapConfigured_UsesDefaultMaxAutoApprovalIterationsAsync()
2493+
{
2494+
// Arrange
2495+
var session = new ChatClientAgentSession();
2496+
var callCount = 0;
2497+
var innerAgent = new Mock<AIAgent>();
2498+
innerAgent
2499+
.Protected()
2500+
.Setup<Task<AgentResponse>>("RunCoreAsync",
2501+
ItExpr.IsAny<IEnumerable<ChatMessage>>(),
2502+
ItExpr.IsAny<AgentSession?>(),
2503+
ItExpr.IsAny<AgentRunOptions?>(),
2504+
ItExpr.IsAny<CancellationToken>())
2505+
.ReturnsAsync(() =>
2506+
{
2507+
callCount++;
2508+
return new AgentResponse([new ChatMessage(ChatRole.Assistant,
2509+
[new ToolApprovalRequestContent($"req{callCount}", new FunctionCallContent($"call{callCount}", "load_skill"))])]);
2510+
});
2511+
2512+
var agent = new ToolApprovalAgent(innerAgent.Object, new ToolApprovalAgentOptions
2513+
{
2514+
AutoApprovalRules = [ToolApprovalAgent.AllToolsAutoApprovalRule],
2515+
});
2516+
2517+
// Act
2518+
await agent.RunAsync([new ChatMessage(ChatRole.User, "Hi")], session);
2519+
2520+
// Assert
2521+
Assert.Equal(ToolApprovalAgent.DefaultMaxAutoApprovalIterations + 1, callCount);
2522+
}
2523+
2524+
/// <summary>
2525+
/// Verify that a cap below 1 is rejected, since it would leave no turn to run.
2526+
/// </summary>
2527+
[Theory]
2528+
[InlineData(0)]
2529+
[InlineData(-1)]
2530+
public void Constructor_MaxAutoApprovalIterationsBelowOne_Throws(int value)
2531+
{
2532+
// Arrange
2533+
var innerAgent = new Mock<AIAgent>().Object;
2534+
2535+
// Act & Assert
2536+
Assert.Throws<ArgumentOutOfRangeException>(() => new ToolApprovalAgent(innerAgent, new ToolApprovalAgentOptions
2537+
{
2538+
MaxAutoApprovalIterations = value,
2539+
}));
2540+
}
2541+
2542+
#endregion
23952543
}

0 commit comments

Comments
 (0)