Skip to content

Commit 6e630f4

Browse files
authored
.NET: Isolate Foundry toolbox response cache (#8848)
* fix(foundry): isolate toolbox response cache Request-opened toolbox clients must not outlive the response context that created them. Keep startup cache reuse while disposing scoped clients on every response exit. * fix(foundry): harden toolbox scope lifecycle Allow independent response scopes to open concurrently. Cleanup attempts every scoped resource, preserves response outcomes, and aggregates failures only during service shutdown.
1 parent ca936db commit 6e630f4

6 files changed

Lines changed: 1043 additions & 62 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.Foundry.Hosting/AgentFrameworkResponseHandler.cs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ public override async IAsyncEnumerable<ResponseStreamEvent> CreateAsync(
186186
// point below: AsyncLocal writes made in this streaming iterator are reverted across yield
187187
// boundaries, so a single up-front assignment would be lost before the toolbox/MCP calls run.
188188
var platformCallId = context.PlatformContext?.CallId;
189+
var toolboxRequestScope = this._toolboxService?.CreateRequestScope()
190+
?? FoundryToolboxService.RequestToolboxScope.CreateInactive();
191+
await using var configuredToolboxRequestScope = toolboxRequestScope.ConfigureAwait(false);
189192
HostedCallContext.CallId = platformCallId;
193+
HostedCallContext.ToolboxCacheScopeId = toolboxRequestScope?.Id;
190194

191195
// Stamp/validate the hosted identity only when one was resolved. Locally (non-hosted) there is
192196
// no user identity, so there is nothing to partition or tamper-check and the session is shared.
@@ -278,6 +282,7 @@ public override async IAsyncEnumerable<ResponseStreamEvent> CreateAsync(
278282
// Re-apply the call id: the EmitCreated/EmitInProgress yields above reverted the ambient
279283
// value, and the toolbox tools/list + consent egress below must carry it per request.
280284
HostedCallContext.CallId = platformCallId;
285+
HostedCallContext.ToolboxCacheScopeId = toolboxRequestScope?.Id;
281286

282287
var toolboxResolution = await this.ResolveToolboxToolsAsync(
283288
this._toolboxService,
@@ -382,6 +387,7 @@ public override async IAsyncEnumerable<ResponseStreamEvent> CreateAsync(
382387
// below revert the ambient AsyncLocal, but the MCP tools/call egress that happens
383388
// inside MoveNextAsync must carry the platform call id on every request.
384389
HostedCallContext.CallId = platformCallId;
390+
HostedCallContext.ToolboxCacheScopeId = toolboxRequestScope?.Id;
385391

386392
bool shutdownDetected = false;
387393
McpConsentInfo? consentInfo = null;
@@ -754,7 +760,7 @@ await toolboxService
754760
return new(Tools: [], Consents: pendingConsents, Error: null);
755761
}
756762

757-
List<AITool> toolsToAdd = [.. toolboxService.Tools];
763+
List<AITool> toolsToAdd = [.. toolboxService.Tools, .. toolboxService.GetCurrentRequestTools()];
758764
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
759765
List<McpConsentInfo>? markerConsents = null;
760766

0 commit comments

Comments
 (0)