Skip to content

agentHost: preserve BYOK state through tool continuations - #331749

Merged
Vritant Bhardwaj (vritant24) merged 2 commits into
microsoft:mainfrom
sgent-epic:codex/fix-agent-host-stateful-marker
Oct 8, 2026
Merged

Vritant Bhardwaj (vritant24) merged 2 commits into
microsoft:mainfrom
sgent-epic:codex/fix-agent-host-stateful-marker

Conversation

@sgent-epic

Copy link
Copy Markdown
Contributor

Summary

  • retain provider response IDs only while tool calls are pending
  • recover the Copilot SDK immediate tool-result replay by supplying previous_response_id and forwarding only the matching tool outputs
  • scope pending state by authenticated SDK session, vendor, and model
  • preserve explicit continuation IDs and clear recovered state after a terminal response

Why

The bundled Copilot SDK currently replays the full Responses history after a tool call and omits previous_response_id. Extension-provided stateful models then receive no stateful_marker and reject the continuation.

The integration test reproduces the SDK request shape directly. Before the proxy change, it failed because the second request replayed the message, reasoning, and tool call with no response ID. It now reaches the provider with the original response ID and only the tool output.

Fixes #329283

Testing

  • npm run compile-client
  • scripts\test.bat --run src\vs\platform\agentHost\test\node\byokLmProxyService.test.ts
  • scripts\test-integration.bat --run src\vs\platform\agentHost\test\node\providerIntegration\copilotByokResponses.integrationTest.ts
  • precommit hygiene
  • git diff --check upstream/main...HEAD

Copilot AI balanced review requested due to automatic review settings August 20, 2026 05:06

Copilot AI left a comment

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.

Pull request overview

Preserves BYOK provider state through Copilot SDK tool continuations, addressing #329283.

Changes:

  • Tracks pending response IDs by session, vendor, and model.
  • Recovers matching tool-output continuations without overriding explicit state.
  • Adds unit and SDK integration coverage.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
byokLmProxyService.ts Implements continuation recovery and scoped state tracking.
byokLmProxyService.test.ts Tests recovery, scoping, explicit IDs, and cleanup.
copilotByokResponses.integrationTest.ts Verifies SDK tool continuation behavior end-to-end.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch 2 times, most recently from e547f44 to 9886540 Compare August 20, 2026 14:23
@sgent-epic

Copy link
Copy Markdown
Contributor Author

Hi Vritant Bhardwaj (@vritant24), I hope you're doing well. Is there any way this could be prioritized? It's a blocker for wider piloting of agents in VS Code at Epic. I'm personally really liking the VS Code environment over other options, and I'd like to enable other folks to give it a try as well. Thanks for your time!

@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch 3 times, most recently from 4ec345b to 67cd0c4 Compare August 20, 2026 19:35
}

private _updateToolContinuation(state: ByokLmProxyState, key: string, result: IByokLmChatResult): void {
if (!result.responseId) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

result.responseId is not always evidence that the downstream response is resumable.

  • Explicit-ZDR Responses requests use store: false and deliberately remove previous_response_id.
  • The Responses processor still emits the returned ID as stateful_marker.
  • This proxy then caches that ID and replaces the SDK replay with only tool outputs.
  • On the next request, the ZDR endpoint drops the injected marker, leaving orphaned tool outputs without the original history.

Suggested fix:

  • Define stateful_marker as “resumable provider state”: do not emit it for explicit-ZDR/non-stored responses.
  • With no marker, IByokLmChatResult.responseId remains absent and this proxy preserves the full stateless replay.
  • Do not infer this from the local body.store: the SDK intentionally sends store: false to this facade even when the downstream provider is stateful.
  • Add a ZDR Responses tool-continuation test verifying that no marker is cached and the full replay reaches the provider.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in bf9576442cb. responseId is now explicitly an opt-in resumable-state contract. The component that knows whether a response is resumable owns whether to report it. The proxy does not infer this from store, and the regression verifies that absence preserves full replay without previous_response_id.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Following up here because my earlier inline reply described the contract change, not the producer fix that landed afterward. Current head 18387e9 includes the actual ZDR marker suppression and direct coverage:

  • ChatEndpoint disables marker emission when zeroDataRetentionEnabled is explicitly true.
  • The Responses processor then omits statefulMarker, even when the provider returns a response ID.
  • The endpoint regression processes a real-shaped response.completed event containing resp_123. It expects no marker with ZDR enabled and the marker with ZDR disabled.
  • The proxy regression verifies that absent resumable state preserves the complete stateless replay without injecting previous_response_id.

This covers the producer and proxy boundaries separately, rather than only assuming the producer omitted its response ID. The proxy still does not infer downstream retention from the SDK facade's store field.

Could you confirm whether this resolves the ZDR concern? Leaving the thread open for your review.

// A session can disappear after receiving a tool call, so keep abandoned
// continuations from growing for the lifetime of the shared proxy.
state.delete(key);
state.set(key, { responseId: result.responseId, calls });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This retains only one continuation per (sessionId, vendor, modelId), but root, background, and subagent trajectories share the same session-scoped provider token.

A normal nested flow can therefore lose the parent state even without an HTTP race:

parent tool call   -> stores resp_parent
subagent response  -> overwrites/deletes the same entry
parent tool result -> recovery misses -> strict stateful provider rejects it

Suggested fix:

  • Retain multiple pending responses and match trailing tool outputs against the exact call-ID/kind set.
  • Recover only when exactly one candidate in the same session/vendor/model scope matches.
  • Remove only the matched predecessor after a successful continuation; unrelated terminal responses must not clear other entries.
  • Keep memory bounded with a flat global limit of 256 pending responses, not unbounded arrays per scope, and store only IDs/kinds rather than history.
  • Add an interleaving test with parent and subagent continuations using the same session/vendor/model.

@sgent-epic sgent-epic Aug 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 6a805a3423f. The proxy now keeps a flat globally bounded set of candidates, resumes only a unique exact call-ID/kind match within the same scope, and removes only the matched predecessor after a successful bridge call. Tests cover parent/subagent interleaving, ambiguous matches, and the 256-entry bound.

@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch from 6a805a3 to 83216ae Compare August 20, 2026 23:15
@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch from 83216ae to e7008ba Compare August 20, 2026 23:33
@sgent-epic

Copy link
Copy Markdown
Contributor Author

Hi Vritant Bhardwaj (@vritant24), please let me know if there are any changes required. Thanks!

@vritant24

Copy link
Copy Markdown
Member

Thanks for the update. The parent/subagent collision looks fixed now. I think two things remain:

  • The ZDR path still emits every Responses ID as a statefulMarker, even though responses created with store: false cannot be resumed. The new test assumes responseId is already absent, but the real producer still supplies one. Could we suppress the marker for ZDR responses and test that path directly?

  • Explicit continuations do not clear their cached entry. We only delete state when a continuation was implicitly recovered, so a successful request with previous_response_id leaves stale state behind. Could we remove the matching entry after that request succeeds, while retaining it on errors?

The latest rebase doesn’t appear to change either path.

@sgent-epic

Copy link
Copy Markdown
Contributor Author

Thanks, Vritant Bhardwaj (@vritant24). Both are addressed in e2cee5c. ZDR responses no longer emit markers, with direct ZDR/stored coverage. Explicit continuations now remove matching cached state after success while retaining it on errors. Full compile and focused tests pass.

@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch from e2cee5c to abd344d Compare August 21, 2026 23:22

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified the diff. Changes align with project standards.

@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch 2 times, most recently from d509d36 to 7a302ea Compare August 24, 2026 16:38
@sgent-epic

Copy link
Copy Markdown
Contributor Author

Hi Vritant Bhardwaj (@vritant24) can we run the test suites again? Thanks!

@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch 2 times, most recently from 0d98e35 to 8922007 Compare August 25, 2026 01:41
@sgent-epic
sgent-epic requested a balanced review from Copilot August 25, 2026 01:42

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.

@sgent-epic

Copy link
Copy Markdown
Contributor Author

Hi Vritant Bhardwaj (@vritant24), can you please help me with the next steps? Apologies for the nagging, but it's a blocker for us. TIA!

@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch from 8922007 to 05a545b Compare August 25, 2026 20:48
@sgent-epic

Copy link
Copy Markdown
Contributor Author

Hi Vritant Bhardwaj (@vritant24) is there anything else I need to do? Can you please approve the workflows to run?

@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch 2 times, most recently from 7f3cc34 to 769a7b6 Compare August 31, 2026 21:00
@sgent-epic

Copy link
Copy Markdown
Contributor Author

Hi Vritant Bhardwaj (@vritant24), I am not quite sure what is next for this? Can you please assist?

@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch from 769a7b6 to ca28a0d Compare September 2, 2026 14:21
@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch from ca28a0d to 18387e9 Compare September 9, 2026 22:02
@sgent-epic

Copy link
Copy Markdown
Contributor Author

Vritant Bhardwaj (@vritant24), I revalidated the core continuation fix today against the versions now pinned by main: Copilot SDK 1.0.13 and runtime 1.0.84-2.

An isolated A/B probe used the PR's tool-continuation fixture, a local fake provider, and an actual SDK echo tool round over VS Code's stdio runtime path:

Proxy code Observed continuation Continuation assertion
Upstream 585a254 No previous_response_id, with message, reasoning, tool call, and tool output replayed Fails
This PR Restores resp_provider_1 and forwards only the matching tool output Passes

The SDK still omits the ID in both runs, so the runtime upgrade has not superseded this fix. The tested proxy source is byte-identical to current head 18387e9. This was a focused probe, not a full-suite CI run.

For the earlier review concerns:

Could you please:

  1. Confirm any remaining technical concerns and resolve the addressed review threads when satisfied.
  2. Approve the pending workflow runs, including Code OSS. The current runs report action_required.
  3. Help identify the additional reviewers needed. The PR policy check currently reports 0/3 collaborator approvals.

Thanks for helping us get this through review.

@sgent-epic

Copy link
Copy Markdown
Contributor Author

Hi Dmitriy Vasyura (@dmitrivMS) I would appreciate your review if you have time. Thanks!

@dmitrivMS

Copy link
Copy Markdown
Collaborator

Hi Dmitriy Vasyura (Dmitriy Vasyura (@dmitrivMS)) I would appreciate your review if you have time. Thanks!

Vritant should review when he's available, not me.

@sgent-epic

Copy link
Copy Markdown
Contributor Author

Sorry to keep pinging Vritant Bhardwaj (@vritant24), but this is still a blocker.

@vritant24

Copy link
Copy Markdown
Member

Hi sgent-epic apologies for the delay, I've got two more comments for this PR since there have been some changes that have gone into insiders

1. In extensions/copilot/src/platform/endpoint/node/chatEndpoint.ts

The proxy now trims the history and sends only the tool outputs whenever the renderer reports a responseId. So responseId has to mean "this endpoint will chain from this ID on the next request." Turning the marker off only for ZDR doesn't cover every case where OpenAIEndpoint ignores it:

  • statefulResponses: false (e.g. DeepSeek, on main). OpenAIEndpoint.createRequestBody sets ignoreStatefulMarker when !this.supportsStatefulResponses.
  • IDs that don't start with resp_. OpenAIEndpoint drops the marker and rebuilds the full history from the messages.

In both cases the proxy has already removed the history. The rebuilt request contains only function_call_output items, with no matching function_call, so the provider rejects every tool continuation. These configurations work today because the full history is replayed. BYOK is now on by default in the agent host, so this would hit existing users.

Could the marker be emitted only when the endpoint will actually use it? For example, add a protected canResumeResponses(responseId) hook on ChatEndpoint that OpenAIEndpoint overrides with !zdr && this.supportsStatefulResponses && responseId.startsWith('resp_'), and pass its result to processResponseFromChatEndpoint. Please add tests for statefulResponses: false and for a non-resp_ ID, checking that no statefulMarker is emitted.

2. in src/vs/platform/agentHost/test/node/providerIntegration/copilotByokResponses.integrationTest.ts

This rewrites the existing bundled SDK consumes structured reasoning and text from the proxy test instead of adding a new one. That removes coverage unrelated to this fix:

  • reasoning being surfaced through assistant.reasoning
  • usage being reported
  • reasoning items (rs_provider / encryptedContent) being replayed on the next user turn

Could you restore the original test and add the tool-continuation scenario as a separate test(...)? That keeps the existing checks and makes the new behavior explicit.

@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch from 18387e9 to 367b9e1 Compare October 7, 2026 19:57

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since the rebase, tool outputs can be content-part arrays (text plus images). _recoverToolContinuation still rejects any non-string output, so a tool round that returns an image (e.g. view on an image file) falls back to full replay without previous_response_id, and a stateful provider would still fail with the original error. responsesRequestToBridge already validates array outputs, so could the matcher accept string | array here? Please add a unit test for a continuation whose tool output contains an input_image part.

@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch from 367b9e1 to 07fa7cf Compare October 7, 2026 22:16
@sgent-epic
sgent-epic force-pushed the codex/fix-agent-host-stateful-marker branch from 07fa7cf to 47d8cb1 Compare October 8, 2026 13:53
@sgent-epic

Copy link
Copy Markdown
Contributor Author

Since the rebase, tool outputs can be content-part arrays (text plus images). _recoverToolContinuation still rejects any non-string output, so a tool round that returns an image (e.g. view on an image file) falls back to full replay without previous_response_id, and a stateful provider would still fail with the original error. responsesRequestToBridge already validates array outputs, so could the matcher accept string | array here? Please add a unit test for a continuation whose tool output contains an input_image part.

Thanks Vritant Bhardwaj (@vritant24) I believe I have everything addressed.

@vritant24
Vritant Bhardwaj (vritant24) merged commit 636f811 into microsoft:main Oct 8, 2026
35 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.142.0 milestone Oct 8, 2026
@sgent-epic

Copy link
Copy Markdown
Contributor Author

Thanks folks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Agent Host BYOK] Responses reasoning continuation loses its stateful marker

7 participants