Skip to content

Commit 17d1198

Browse files
committed
.NET: Bound the tool-approval auto-approval loop (microsoft#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.
1 parent 5949547 commit 17d1198

3 files changed

Lines changed: 205 additions & 2 deletions

File tree

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

Lines changed: 38 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

@@ -47,9 +48,13 @@ namespace Microsoft.Agents.AI;
4748
/// </remarks>
4849
public sealed class ToolApprovalAgent : DelegatingAIAgent
4950
{
51+
/// <summary>The default value used for <see cref="ToolApprovalAgentOptions.MaxAutoApprovalIterations"/> when none is specified.</summary>
52+
public const int DefaultMaxAutoApprovalIterations = 10;
53+
5054
private readonly ProviderSessionState<ToolApprovalState> _sessionState;
5155
private readonly JsonSerializerOptions _jsonSerializerOptions;
5256
private readonly Func<ToolAutoApprovalRuleContext, ValueTask<bool>>[]? _autoApprovalRules;
57+
private readonly int _maxAutoApprovalIterations;
5358

5459
/// <summary>
5560
/// Initializes a new instance of the <see cref="ToolApprovalAgent"/> class.
@@ -65,6 +70,8 @@ public ToolApprovalAgent(AIAgent innerAgent, ToolApprovalAgentOptions? options =
6570
{
6671
this._jsonSerializerOptions = options?.JsonSerializerOptions ?? AgentJsonUtilities.DefaultOptions;
6772
this._autoApprovalRules = options?.AutoApprovalRules?.ToArray();
73+
this._maxAutoApprovalIterations = Throw.IfLessThan(
74+
options?.MaxAutoApprovalIterations ?? DefaultMaxAutoApprovalIterations, 1);
6875
this._sessionState = new ProviderSessionState<ToolApprovalState>(
6976
_ => new ToolApprovalState(),
7077
"toolApprovalState",
@@ -125,11 +132,25 @@ protected override async Task<AgentResponse> RunCoreAsync(
125132
// 3. Call the inner agent in a loop. If the inner agent returns approval requests
126133
// that are ALL auto-approved by standing rules, we immediately re-call with the
127134
// collected approval responses injected. This avoids returning empty responses.
128-
while (true)
135+
//
136+
// The loop is bounded by _maxAutoApprovalIterations. Each pass is a fresh inner
137+
// invocation, so a per-request cap (FunctionInvokingChatClient.MaximumIterationsPerRequest)
138+
// restarts every time and cannot bound it; without a cap here a model that keeps
139+
// requesting an auto-approved tool bills indefinitely.
140+
for (int iteration = 0; ; iteration++)
129141
{
130142
// Inject any collected approval responses as a user message ahead of the caller's messages.
131143
var processedMessages = this.InjectCollectedResponses(callerMessages, state, session);
132144

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

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

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

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,25 @@ 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 — for example a skill-loading
60+
/// tool under <see cref="ToolApprovalAgent.AllToolsAutoApprovalRule"/> — drives an unbounded sequence of
61+
/// billable model calls.
62+
/// </para>
63+
/// <para>
64+
/// On reaching the cap the agent performs one final inner invocation without auto-approving again, so any
65+
/// remaining approval request is surfaced to the caller to decide instead of being approved automatically.
66+
/// Raise this value if you intend to allow longer auto-approval chains.
67+
/// </para>
68+
/// </remarks>
69+
public int? MaxAutoApprovalIterations { get; set; }
4970
}

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

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,4 +2392,150 @@ 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+
var innerAgent = new Mock<AIAgent>().Object;
2533+
2534+
Assert.Throws<ArgumentOutOfRangeException>(() => new ToolApprovalAgent(innerAgent, new ToolApprovalAgentOptions
2535+
{
2536+
MaxAutoApprovalIterations = value,
2537+
}));
2538+
}
2539+
2540+
#endregion
23952541
}

0 commit comments

Comments
 (0)