diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/HandoffAgentExecutor.cs b/dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/HandoffAgentExecutor.cs index 4370e54b41..3ba6603b73 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/HandoffAgentExecutor.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/HandoffAgentExecutor.cs @@ -342,12 +342,6 @@ internal sealed class HandoffAgentExecutor : skipAddIncoming: true).ConfigureAwait(false); } - // Reset the counter when ending the turn (handoff requested or turn limit reached). - // This also covers the case where the turn is interrupted by outstanding requests: - // the counter is reset at the start of the next HandleAsync call, but we still - // clean up here on a normal turn exit for clarity. - this._autonomousModeTurnCount = 0; - HandoffState outgoingState = new(state.IncomingState.TurnToken, result.HandoffTargetId, this._agent.Id); await context.SendMessageAsync(outgoingState, cancellationToken).ConfigureAwait(false); diff --git a/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/HandoffAgentExecutorTests.cs b/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/HandoffAgentExecutorTests.cs index 21ebb46d2c..9ce9918d16 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/HandoffAgentExecutorTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/HandoffAgentExecutorTests.cs @@ -242,7 +242,11 @@ public class HandoffAgentExecutorTests : AIAgentHostingExecutorTestsBase public async Task Test_HandoffAgentExecutor_AutonomousMode_InvokesAgentUpToTurnLimitPlusOne(int turnLimit) { // Arrange: agent with many prepared turns; no handoff ever requested; autonomous mode ON - int totalTurns = turnLimit + 2; // More turns prepared than the limit to detect over-invocation + // We prepare (turnLimit + 2) turns so that if the implementation over-invokes by one, + // TestReplayAgent.Turn will be (turnLimit + 2) rather than the expected (turnLimit + 1). + // TestReplayAgent does NOT increment Turn when it runs out of prepared messages, so preparing + // exactly (turnLimit + 1) would make an extra invocation silently undetectable. + int totalTurns = turnLimit + 2; TestReplayAgent agent = new( Enumerable.Range(0, totalTurns) .Select(i => TestReplayAgent.ToChatMessages($"Turn {i} response"))