Repository navigation
.NET: Add request-time agent selection for OpenAI Responses hosting - #9526
Open
Marco Minerva (marcominerva) wants to merge 3 commits into
Open
Marco Minerva (marcominerva) wants to merge 3 commits into
Marco Minerva (marcominerva) wants to merge 3 commits into
Conversation
…on for OpenAI Responses hosting Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Marco Minerva (marcominerva)
deployed
to
github-app-auth
October 19, 2026 09:07 — with
GitHub Actions
Active
Marco Minerva (marcominerva)
deployed
to
github-app-auth
October 19, 2026 09:07 — with
GitHub Actions
Active
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Selected agents and session stores use different scopes, and disconnected streams can continue using disposed request-scoped dependencies.
2 open issues
What changed in this PR
Adds request-time agent selection to .NET OpenAI Responses hosting as part of #7733.
Changes:
- Adds a selector overload covering all response operations.
- Binds stored responses and session identity to the selected agent’s name.
- Releases completed executors and adds selection and persistence regression tests.
| File | Description |
|---|---|
| dotnet/tests/Microsoft.Agents.AI.Hosting.OpenAI.UnitTests/ResponseSessionRegressionTests.cs | Tests selection, isolation, and session continuity. |
| dotnet/tests/Microsoft.Agents.AI.Hosting.OpenAI.UnitTests/InMemoryResponsesServiceTests.cs | Tests executor release after completion. |
| dotnet/src/Shared/HostedAgents/HostedAgentResolution.cs | Adds shared agent identity validation. |
| dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/ResponsesHttpHandler.cs | Selects agents for every operation. |
| dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/IResponsesService.cs | Adds request-selected executor overloads. |
| dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/InMemoryResponsesService.cs | Partitions responses and releases executors. |
| dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/AIAgentResponseExecutor.cs | Supports explicit session storage identity. |
| dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Microsoft.Agents.AI.Hosting.OpenAI.csproj | Includes shared hosting helpers. |
| dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/EndpointRouteBuilderExtensions.Responses.cs | Adds selector mapping and endpoint wiring. |
| dotnet/eng/MSBuild/Shared.props | Enables shared hosting source inclusion. |
Review effort: Balanced
Marco Minerva (marcominerva)
deployed
to
github-app-auth
October 19, 2026 09:14 — with
GitHub Actions
Active
Merged
4 of 5 tasks
…quest Agents selected for OpenAI Responses can be resolved from HttpContext.RequestServices, but their keyed session store was resolved from separate per-operation scopes, so a store depending on request-initialized state received a different, uninitialized dependency. Validation and responses that are not run in the background now resolve the store from the request services of the selected agent, while background responses keep using a scope they own because they outlive the request. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Marco Minerva (marcominerva)
deployed
to
github-app-auth
October 19, 2026 13:32 — with
GitHub Actions
Active
… run completes Responses for selected agents that are not run in the background use services owned by the HTTP request, but streaming execution kept running after the client disconnected while the request ended, and a canceled non-streaming request ended while its execution was still unwinding. Executors now report whether an execution is bound to the request, and the responses service waits for such executions before the request ends: streaming runs are not canceled and complete normally, while canceled non-streaming runs are awaited until they stop. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Marco Minerva (marcominerva)
deployed
to
github-app-auth
October 19, 2026 14:03 — with
GitHub Actions
Active
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Motivation & Context
The OpenAI Responses endpoints are bound to a single agent at mapping time, so applications that need to choose the agent per request (for example from a route value, header or claim) have to map one set of endpoints per agent.
This PR is the OpenAI Responses part of #9382, which was split per hosting protocol to make review easier. It is independent from the other PRs of the split.
Description & Review Guide
MapOpenAIResponses(responsesPath, Func<HttpContext, AIAgent?> agentSelector, mapOptions)overload. The selector applies to every operation (create, get, cancel, delete, list input items); anullselection returns404before any response, session or conversation state is loaded.Name(InMemoryResponsesService.ForAgent), so a response can only be retrieved, canceled or deleted through requests that select the same agent. The agent name also keys the session store and partitions persisted sessions across new agent instances.IResponsesService/ResponsesHttpHandleraccept the executor selected for the request; after execution the stored response releases the executor so the agent graph is not kept alive by stored responses.src/Shared/HostedAgents/HostedAgentResolution.cs(identical in each PR of this split, so they merge cleanly in any order).Related Issue
Part of #7733 (split from #9382).
Contribution Checklist