Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"dotnet.defaultSolution": "dotnet/agent-framework-src-and-tests.slnf"
"dotnet.defaultSolution": "dotnet/agent-framework-src-and-tests.slnf",
"$comment": "Revisit disabling the lightweight test experience when upgrading to C# Dev Kit 11. Remove the override only after verifying test discovery, execution, and debugging with the default experience.",
"dotnet.testWindow.enableNextTest": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,53 +84,63 @@ private ValueTask HandleUserInputResponseAsync(
IWorkflowContext context,
CancellationToken cancellationToken)
{
if (!this._userInputHandler!.MarkRequestAsHandled(response.RequestId, out var request))
if (!this._userInputHandler!.TryRecordResponse(response.RequestId, response))
{
throw new InvalidOperationException($"No pending ToolApprovalRequest found with id '{response.RequestId}'.");
}

// Merge the external response with any already-buffered regular messages so mixed-content
// resumes can be processed in one invocation.
return this.ProcessTurnMessagesAsync(async (pendingMessages, ctx, ct) =>
{
ChatMessage responseMessage = new(ChatRole.User, [response])
{
CreatedAt = DateTimeOffset.UtcNow,
MessageId = Guid.NewGuid().ToString("N"),
};
pendingMessages.Add(responseMessage);

await this.ContinueTurnAsync(
pendingMessages, ctx, this._currentTurnEmitEvents ?? false, ct,
(responseMessage, response, request.ToolCall.CallId)).ConfigureAwait(false);

// Clear the buffered turn messages because they were consumed by ContinueTurnAsync.
return null;
}, context, cancellationToken);
return this.ResumeWithResponsesAsync(context, cancellationToken);
}

private ValueTask HandleFunctionResultAsync(
FunctionResultContent result,
IWorkflowContext context,
CancellationToken cancellationToken)
{
if (!this._functionCallHandler!.MarkRequestAsHandled(result.CallId))
if (!this._functionCallHandler!.TryRecordResponse(result.CallId, result))
{
throw new InvalidOperationException($"No pending FunctionCall found with id '{result.CallId}'.");
}

// Merge the external response with any already-buffered regular messages so mixed-content
// resumes can be processed in one invocation.
return this.ResumeWithResponsesAsync(context, cancellationToken);
}

private ValueTask ResumeWithResponsesAsync(IWorkflowContext context, CancellationToken cancellationToken)
{
if (this.HasOutstandingRequests)
{
return default;
}

return this.ProcessTurnMessagesAsync(async (pendingMessages, ctx, ct) =>
{
pendingMessages.Add(new ChatMessage(ChatRole.Tool, [result])
List<(AIContent Request, AIContent Response, int Order)> responses = [];
responses.AddRange(this._userInputHandler!.TakeResponses().Select(item => ((AIContent)item.Request, (AIContent)item.Response, item.Order)));
responses.AddRange(this._functionCallHandler!.TakeResponses().Select(item => ((AIContent)item.Request, (AIContent)item.Response, item.Order)));
if (responses.Count == 0)
{
AuthorName = this._agent.Name ?? this._agent.Id,
CreatedAt = DateTimeOffset.UtcNow,
MessageId = Guid.NewGuid().ToString("N"),
});
return pendingMessages;
}

List<(ChatMessage Message, ToolApprovalResponseContent Response, string CallId)> answeredApprovals = [];
foreach (var item in responses.OrderBy(item => item.Order))
{
bool isFunctionResult = item.Response is FunctionResultContent;
ChatMessage message = new(isFunctionResult ? ChatRole.Tool : ChatRole.User, [item.Response])
{
AuthorName = isFunctionResult ? this._agent.Name ?? this._agent.Id : null,
CreatedAt = DateTimeOffset.UtcNow,
MessageId = Guid.NewGuid().ToString("N"),
};
pendingMessages.Add(message);
if (item.Response is ToolApprovalResponseContent approval &&
item.Request is ToolApprovalRequestContent request)
{
answeredApprovals.Add((message, approval, request.ToolCall.CallId));
}
}

await this.ContinueTurnAsync(pendingMessages, ctx, this._currentTurnEmitEvents ?? false, ct).ConfigureAwait(false);
await this.ContinueTurnAsync(pendingMessages, ctx, this._currentTurnEmitEvents ?? false, ct, answeredApprovals).ConfigureAwait(false);

// Clear the buffered turn messages because they were consumed by ContinueTurnAsync.
return null;
Expand Down Expand Up @@ -184,13 +194,13 @@ private async ValueTask ContinueTurnAsync(
IWorkflowContext context,
bool emitEvents,
CancellationToken cancellationToken,
(ChatMessage Message, ToolApprovalResponseContent Response, string CallId)? answeredApproval = null)
IReadOnlyList<(ChatMessage Message, ToolApprovalResponseContent Response, string CallId)>? answeredApprovals = null)
{
this._currentTurnEmitEvents = emitEvents;
if (this._options.ForwardIncomingMessages)
{
List<ChatMessage> incomingMessages = answeredApproval is { } answer
? messages.Where(m => !ReferenceEquals(m, answer.Message)).ToList()
List<ChatMessage> incomingMessages = answeredApprovals is not null
? messages.Where(m => !answeredApprovals.Any(answer => ReferenceEquals(m, answer.Message))).ToList()
: messages;
if (incomingMessages.Count > 0)
{
Expand All @@ -209,12 +219,18 @@ private async ValueTask ContinueTurnAsync(
// causes invalid request errors when the receiving agent uses the Responses API,
// because these item types are not valid as input items.
List<ChatMessage> forwardableMessages = FilterForwardableMessages(response.Messages);
if (answeredApproval is { } approval)
if (answeredApprovals is not null)
{
// Other agents received the approval request; they also need its answer before the tool result.
ChatMessage completedAnswer = approval.Message.Clone();
completedAnswer.Contents = [CreateCompletedApprovalResponse(approval.Response, approval.CallId, response.Messages)];
forwardableMessages.InsertRange(0, FilterForwardableMessages([completedAnswer]));
List<ChatMessage> completedAnswers = [];
foreach (var approval in answeredApprovals)
{
ChatMessage completedAnswer = approval.Message.Clone();
completedAnswer.Contents = [CreateCompletedApprovalResponse(approval.Response, approval.CallId, response.Messages)];
completedAnswers.Add(completedAnswer);
}

forwardableMessages.InsertRange(0, FilterForwardableMessages(completedAnswers));
}

if (forwardableMessages.Count > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,29 @@ internal sealed class AIAgentUnservicedRequestsCollector(AIContentExternalHandle
{
private readonly Dictionary<string, ToolApprovalRequestContent> _userInputRequests = [];
private readonly Dictionary<string, FunctionCallContent> _functionCalls = [];
private readonly List<AIContent> _requestsInOrder = [];

public Task SubmitAsync(IWorkflowContext context, CancellationToken cancellationToken)
{
Task userInputTask = userInputHandler != null && this._userInputRequests.Count > 0
? userInputHandler.ProcessRequestContentsAsync(this._userInputRequests, context, cancellationToken)
: Task.CompletedTask;

Task functionCallTask = functionCallHandler != null && this._functionCalls.Count > 0
? functionCallHandler.ProcessRequestContentsAsync(this._functionCalls, context, cancellationToken)
: Task.CompletedTask;
List<Task> tasks = [];
for (int order = 0; order < this._requestsInOrder.Count; order++)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This restarts ordering at zero for every agent invocation. If another TurnToken reaches the host while an earlier request remains pending, the new requests collide with the old order values; the later merge adds approval responses before function results, so a newer approval can be replayed ahead of an older function result. Please either defer/reject turns while requests are outstanding or assign a checkpointed executor-wide sequence shared by both handlers.

{
AIContent content = this._requestsInOrder[order];
if (content is ToolApprovalRequestContent approval && userInputHandler is not null &&
this._userInputRequests.TryGetValue(approval.RequestId, out var pendingApproval) &&
ReferenceEquals(approval, pendingApproval))
{
tasks.Add(userInputHandler.ProcessRequestContentAsync(approval.RequestId, approval, context, order, cancellationToken).AsTask());
}
else if (content is FunctionCallContent call && functionCallHandler is not null &&
this._functionCalls.TryGetValue(call.CallId, out var pendingCall) &&
ReferenceEquals(call, pendingCall))
{
tasks.Add(functionCallHandler.ProcessRequestContentAsync(call.CallId, call, context, order, cancellationToken).AsTask());
}
}

return Task.WhenAll(userInputTask, functionCallTask);
return Task.WhenAll(tasks);
}

public void ProcessAgentResponseUpdate(AgentResponseUpdate update, Func<FunctionCallContent, bool>? functionCallFilter = null)
Expand All @@ -47,6 +58,7 @@ public void ProcessAIContents(IEnumerable<AIContent> contents, Func<FunctionCall

// It is an error to simultaneously have multiple outstanding user input requests with the same ID.
this._userInputRequests.Add(userInputRequest.RequestId, userInputRequest);
this._requestsInOrder.Add(userInputRequest);
}
else if (content is ToolApprovalResponseContent userInputResponse)
{
Expand All @@ -67,6 +79,7 @@ public void ProcessAIContents(IEnumerable<AIContent> contents, Func<FunctionCall
}

this._functionCalls.Add(functionCall.CallId, functionCall);
this._requestsInOrder.Add(functionCall);
}
}
else if (content is FunctionResultContent functionResult)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand All @@ -17,7 +16,9 @@ internal sealed class AIContentExternalHandler<TRequestContent, TResponseContent
{
private readonly PortBinding? _portBinding;
private readonly string _portId;
private ConcurrentDictionary<string, TRequestContent> _pendingRequests = new();
private ConcurrentOrderedDictionary<string, RequestState> _pendingRequests = new();

internal sealed record RequestState(TRequestContent Request, int Order, TResponseContent? Response = null);

public AIContentExternalHandler(ref ProtocolBuilder protocolBuilder, string portId, bool intercepted, Func<TResponseContent, IWorkflowContext, CancellationToken, ValueTask> handler)
{
Expand Down Expand Up @@ -45,20 +46,11 @@ void ConfigureRoutes(RouteBuilder routeBuilder, out PortBinding? portBinding)
}
}

public bool HasPendingRequests => !this._pendingRequests.IsEmpty;

public Task ProcessRequestContentsAsync(Dictionary<string, TRequestContent> requests, IWorkflowContext context, CancellationToken cancellationToken = default)
{
IEnumerable<Task> requestTasks = from string requestId in requests.Keys
select this.ProcessRequestContentAsync(requestId, requests[requestId], context, cancellationToken)
.AsTask();

return Task.WhenAll(requestTasks);
}
public bool HasPendingRequests => this._pendingRequests.ToArray().Any(item => item.Value.Response is null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every delivered response calls this property, which allocates and scans a snapshot of the entire batch. Answering an N-request batch therefore copies roughly N entries N times, causing quadratic allocation/CPU and avoidable GC pressure for large model-generated batches. Please expose a lock-protected Any/unanswered count so this check does not materialize the collection on every response.


public ValueTask ProcessRequestContentAsync(string id, TRequestContent requestContent, IWorkflowContext context, CancellationToken cancellationToken = default)
public ValueTask ProcessRequestContentAsync(string id, TRequestContent requestContent, IWorkflowContext context, int order = 0, CancellationToken cancellationToken = default)
{
if (!this._pendingRequests.TryAdd(id, requestContent))
if (!this._pendingRequests.TryAdd(id, new(requestContent, order)))
{
// Request is already pending; treat as an idempotent re-emission.
// Do not repost to the sink because request IDs must remain unique while pending.
Expand All @@ -76,7 +68,36 @@ public bool MarkRequestAsHandled(string id)
}

public bool MarkRequestAsHandled(string id, [NotNullWhen(true)] out TRequestContent? request)
=> this._pendingRequests.TryRemove(id, out request);
{
if (this._pendingRequests.TryRemove(id, out RequestState? state))
{
request = state.Request;
return true;
}

request = null;
return false;
}

public bool TryRecordResponse(string id, TResponseContent response)
{
while (this._pendingRequests.TryGetValue(id, out RequestState? state) && state.Response is null)
{
if (this._pendingRequests.TryUpdate(id, state with { Response = response }, state))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This retains each full external response until every sibling answers. A caller can submit large results for a large batch, withhold one final response, and keep all payloads resident and serialized into checkpoints indefinitely; there is no size/count limit, expiry, cancellation cleanup, or ResetChat cleanup, so process memory shared by unrelated workflows can be exhausted. Please bound retained batch bytes/count and reject oversized responses, and ensure abandoned or cancelled batches release this state.

{
return true;
}
}

return false;
}

public (TRequestContent Request, TResponseContent Response, int Order)[] TakeResponses()
{
return this._pendingRequests.TryDrain(state => state.Response is not null, out var entries)
? entries.Select(item => (item.Value.Request, item.Value.Response!, item.Value.Order)).ToArray()
: [];
}

[MemberNotNullWhen(false, nameof(_portBinding))]
private bool IsIntercepted => this._portBinding == null;
Expand All @@ -87,20 +108,19 @@ public bool MarkRequestAsHandled(string id, [NotNullWhen(true)] out TRequestCont

public async ValueTask OnCheckpointingAsync(string id, IWorkflowContext context, CancellationToken cancellationToken = default)
{
Dictionary<string, TRequestContent> pendingRequestsCopy = new(this._pendingRequests);
await context.QueueStateUpdateAsync(MakeKey(id), pendingRequestsCopy, cancellationToken: cancellationToken)
await context.QueueStateUpdateAsync(MakeKey(id), this._pendingRequests.ToArray(), cancellationToken: cancellationToken)
.ConfigureAwait(false);
}

public async ValueTask OnCheckpointRestoredAsync(string id, IWorkflowContext context, CancellationToken cancellationToken = default)
{
Dictionary<string, TRequestContent>? loadedState =
await context.ReadStateAsync<Dictionary<string, TRequestContent>>(MakeKey(id), cancellationToken: cancellationToken)
KeyValuePair<string, RequestState>[]? loadedState =
await context.ReadStateAsync<KeyValuePair<string, RequestState>[]>(MakeKey(id), cancellationToken: cancellationToken)
.ConfigureAwait(false);

if (loadedState != null)
{
this._pendingRequests = new ConcurrentDictionary<string, TRequestContent>(loadedState);
this._pendingRequests = new ConcurrentOrderedDictionary<string, RequestState>(loadedState);
}
}
}
Loading
Loading