Skip to content

fix: replay stateless OpenAI reasoning across chat turns - #30298

Merged
ibetitsmike merged 15 commits into
mainfrom
mike/openai-reasoning-continuity
Oct 13, 2026
Merged

ibetitsmike merged 15 commits into
mainfrom
mike/openai-reasoning-continuity

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

OpenAI Responses chats default to store=false, and chatd only replayed reasoning as an item_reference, which needs provider-side storage. Reasoning was therefore dropped between tool steps and turns, so reasoning models lost their chain of thought mid-task.

This pins coder/fantasy coder_2_33 at 6ba8681, which includes fantasy #63 (and #64, #65, and #66). #63 captures each completed reasoning item (ID, encrypted_content, summary) and replays it in full regardless of store. In chatd:

  • Every Responses call requests reasoning.encrypted_content. The store default and configured includes are unchanged, and Chat Completions requests are untouched.
  • Prompt rebuilds keep reasoning parts without visible text when they carry finalized encrypted state, so reasoning survives durable history and coderd restarts.
  • The provider-switch sanitizer also strips OpenAI reasoning from history produced by a different or unknown provider instance, since reasoning IDs and encrypted content only resolve where they were issued.
  • A compaction model override on a different provider instance, or with a different model, gets a compaction prompt without the chat's OpenAI reasoning.
  • Each saved reasoning part records the provider instance, model, and credential (the deployment's or the user's personal API key) that produced it, and the sanitizer keeps it only when all three match the current turn, because another model, or the organization behind another credential, can reject the encrypted content. Switching models, adding or removing a personal key, or editing a config's model or provider drops the earlier reasoning; switching back restores it. The record is never returned by the API. Reasoning saved before this change has no record and follows its model config.
  • If OpenAI rejects replayed reasoning it cannot decrypt (invalid_encrypted_content), for example after the deployment's key is replaced with one from another organization, the request is retried once without reasoning, and the reasoning that step produces is saved with the provider's next rejection epoch. Epochs are kept separately for each provider, model, and credential. Later steps and turns leave out reasoning from earlier epochs, on any replica and after restarts, so only the first request after the change is rejected and newer reasoning keeps replaying. A retried step that returns no reasoning saves its epoch on its first part. The advisor's prompt never includes OpenAI reasoning, as on main.

Testing

Rebased onto main at 98332c7 as 5735acb (same tree as merging the pre-rebase head a378071 with main; older SHAs below refer to the pre-rebase history). 3c17d80 then adds the model to the reasoning record, 21156b2 pins Fantasy d55a867, which treats a response that echoes store: false as unstored, bf795da adds the retry and the advisor change, 7eb8b1d moves the pin to 88cffb5 (coder_2_33 merged into #63; its OpenAI provider code is identical to d55a867), and f4f4073 pins coder_2_33 at 6ba8681 once #63 merged. That adds fantasy #66, which replays the Responses message phase on assistant text. abccede makes the retry remember rejected reasoning in the process. The branch was then rebased onto main at abc2fae, so every SHA above refers to the pre-rebase history. 5fb3ce1 replaces that memory with the credential and a rejection epoch saved on each reasoning part, and 62b5cb5 keeps epochs per provider, model, and credential and saves the epoch when the retried step returns no reasoning. 1057beb hides the four internal fields from the Swagger spec and API reference, and 29a705e merges main at d68b2c8. Its only conflicts were the fantasy pin (kept at 6ba8681, whose coder_2_33 history includes main's 3510552) and test imports.

  • Tests in chatprompt, chatprovider, chatsanitize, the provider-switch sanitizer, and integration_responses_test.go cover tool-step and durable replay, legacy and incomplete metadata, provider switches, and a web search chat switching from store=false to store=true. Integration tests also cover the retry, a later tool step after it, the next turn on a fresh server against the same database (with and without reasoning in the retried step), a personal key added mid-chat, and the advisor prompt. Red-green verified, including a config whose model is edited in place. The compaction override's model check has unit tests and UAT coverage but no integration test. go vet, targeted race tests, and the pre-commit hook (gen, fmt, lint, build) pass. Local pre-push hooks were skipped; CI covers them.
  • Remote UAT on dogfood with real gpt-5-mini and gpt-5.4 behind a logging proxy that checks every request body. Rounds 1 and 2 failed on dangling item references (both fixed). Rounds 3 to 5 passed (ee3c0bf, 0580870, a378071): tool-step and durable replay, restarts, interrupts, provider switches and config repoints, web search history, and compaction overrides. Both test providers shared one upstream account, so cross-provider checks cover request shape, not a real rejection. Round 6 at 21156b2 (model record) with gpt-5-mini and gpt-5-nano: PASS. 151 Responses requests, all 200; no request carried reasoning from a different model except an older, unrecorded part that follows its config by design. Same-model config switches, switching back, edits to a config's model, a mid-turn switch, restarts, and a same-provider compaction override with a different model all behaved as described. Round 7 at bf795da (retry and advisor): PASS. Through AI Gateway, a corrupted saved blob is rejected with HTTP 400 invalid_encrypted_content. All 14 rejected requests were retried once without reasoning and returned 200, a different 400 was not retried, and advisor requests on the chat model, a same-provider override, and another provider carried no reasoning.
  • f4f4073 changes only the pin. Its new OpenAI code is fantasy Front-end: Add /workspaces page #66, which UAT rounds 1 to 7 did not run. A live check of fantasy 6ba8681 through AI Gateway, two turns with tool calls on gpt-5.5 and gpt-5-mini with store false and true: all 18 requests returned 200, and the gpt-5.5 requests replayed encrypted reasoning followed by assistant messages with the commentary and final_answer phases. go test ./coderd/x/chatd/... ./codersdk/ passes (21 packages) on the same fantasy tree.
  • UAT round 8 at abccede (remembered rejection, and fantasy Front-end: Add /workspaces page #66 through chatd) with gpt-5.5, gpt-5-mini, and gpt-5-nano: PASS. 110 requests; the 6 rejections were 5 corrupted saved blobs, each retried once without reasoning and answered 200, and one unrelated temperature error that was not retried. After the first rejection, later steps and turns in that chat were never rejected again and replayed newer reasoning. A coderd restart cost one more rejection, which also dropped reasoning saved after the first one, and a second chat was unaffected. gpt-5.5 requests replayed assistant messages with the phases the model returned, with store false and true.
  • UAT round 9 at 62b5cb5 (saved rejection epoch, personal key switch) with gpt-5.5, gpt-5-mini, and gpt-5-nano: PASS. 158 Responses and 7 Chat Completions requests; the 6 rejections were corrupted saved blobs, each retried once without reasoning and answered 200, and an unrelated temperature error was not retried. After a coderd restart, and on a second coderd against the same database, the chat was not rejected again and kept replaying the reasoning saved after the rejection. Adding a personal key left out the deployment key's reasoning before sending, and removing it brought that reasoning back. A retried step without reasoning, two chats rejected at once, compaction, a model switch after a rejection, and a switch to Chat Completions and back all behaved as described.

Review record

  • Independent full-branch test audit at bbe1a1b: PASS.
  • Codex through a378071: compaction overrides kept foreign reasoning and configs could be repointed to another provider (both fixed); a request for documentation TODOs and two behaviors that already exist on main (declined).
  • Codex on 5735acb: two findings. Reasoning replayed after a same-provider model switch: fixed in 3c17d80. A provider's base URL edited in place keeps its reasoning: covered by the retry in bf795da.
  • Codex on 21156b2, bf795da, and 7eb8b1d: no major issues. Codex on f4f4073: rejected reasoning stayed in the chat's history, so each later request in that chat was rejected and retried again without any reasoning. Fixed in abccede. Codex on abccede: that memory was lost on a restart or another replica, where the next rejection also dropped newer reasoning, and it grew without bound. Fixed in 5fb3ce1. Codex on 5fb3ce1: a rejection advanced the epoch for every model and credential on the provider, and a retried step without reasoning lost the new epoch. Both fixed in 62b5cb5. Codex on 62b5cb5: keep reasoning out of a config switched from Responses to Chat Completions (declined: the Chat Completions converter already skips reasoning parts, confirmed with a probe test). Codex on 29a705e: no major issues.
  • ethanndickson: retry without reasoning on invalid_encrypted_content, and keep OpenAI reasoning out of the advisor prompt. Both fixed in bf795da. Then: in-process memory is not enough because dogfood restarts on every merge and runs several replicas. Fixed in 5fb3ce1 with his proposed rejection epoch in chat history, plus the credential record, and tested with the next turn on a fresh server. Then: hide the internal fields from Swagger, fixed in 1057beb. Approved on 62b5cb5.
  • fix: replay OpenAI Responses reasoning from encrypted content fantasy#63 review: honoring an echoed store: false is fixed in d55a867; replaying web searches that had no preceding reasoning is a pre-existing gap, tracked in chatd: OpenAI web searches without a preceding reasoning item are never replayed #30528.
  • CI on 5735acb: green after rerunning two failures outside this change (a cli TestSSH data race, and a test-go-pg job timeout that also fails on main). CI on 21156b2: all checks pass. CI on bf795da: all checks pass. CI on 7eb8b1d: all checks pass. CI on f4f4073: all checks pass. CI on abccede did not run, because the branch conflicted with main (fix(coderd/x/chatd): return database errors from turn preparation #30492 changed the provider-switch sanitizer to return database errors). CI on 62b5cb5: all checks pass. CI on 29a705e: all checks pass.

Xum, an AI coding agent, implemented, tested, and opened this PR on behalf of @ibetitsmike.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T07:46:18.018976Z 29a705e Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ee3c0bf3d6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/generation_preparer.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 208cca3c85

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 0580870b5f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a378071862

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/generation_preparer.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: a378071862

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike marked this pull request as ready for review October 3, 2026 06:24

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a378071862

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5735acb36e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 21156b2b32

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/compaction_sanitize.go Resolved
@linear-code

linear-code Bot commented Oct 6, 2026

Copy link
Copy Markdown

CODAGT-1350

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

Xum requested this review on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: bf795daa92

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

Xum requested this review on behalf of @ibetitsmike.

…ompts

A compaction override on a different provider instance cannot resolve
the chat provider's reasoning item IDs or encrypted content, so remove
those parts from the compaction prompt alongside flattening
provider-executed tool history.
Stamp persisted reasoning parts with the provider identity resolved for
the step, and let the provider-switch sanitizer prefer that stamp over
the row's current model config. Repointing a model config to another AI
provider no longer replays the old provider's encrypted reasoning.
Stamp persisted reasoning parts with the model that produced them next
to the provider identity, and replay OpenAI reasoning only when both
match the current turn. Another model on the same provider can reject
the encrypted content with invalid_encrypted_content. Unstamped parts
fall back to the row's model config for both values, and a compaction
override on the same provider with a different model drops OpenAI
reasoning as well.
coder/fantasy d55a867 records a response as unstored when it echoes
store=false, as OpenAI does for zero data retention organizations, so
later requests do not reference its web search items.
Encrypted reasoning only decrypts for the OpenAI organization that
produced it, which can change without a provider or model change, for
example when a user adds or removes a personal API key. When a Responses
request is rejected with invalid_encrypted_content before any output,
retry it once without OpenAI reasoning.

The advisor reads the parent transcript as text, so drop OpenAI
reasoning from its snapshot. Replayed reasoning could be foreign to the
advisor model, and textualized tool calls left reasoning items without
their required following item.
coder/fantasy#63 now targets coder_2_33 and merges in #64 and #65, so the
pin carries the merged base plus the reasoning replay changes.
coder/fantasy#63 is merged into coder_2_33, which now also carries #66,
so the pin moves from the PR branch to the coder_2_33 head.
A rejected reasoning item stays in the chat history, so every later
step and turn replayed it, was rejected again, and retried without any
reasoning, including reasoning produced after the credential change.

chatd now remembers the item IDs of rejected reasoning for each chat
and leaves them out of later requests, so newer reasoning keeps
replaying and only the first request after the change is rejected.
The memory is per process, like the Anthropic drop_block hints.
chatd kept rejected encrypted reasoning in process memory, so a restart
or another replica forgot it. The next rejection then marked every
reasoning item in the prompt as rejected, including reasoning produced
after the credential change, and the memory grew until the process
exited.

Reasoning parts now also record the credential that produced them (the
deployment's or the user's personal key) and a rejection epoch, next to
the provider and model. A turn on a different credential leaves out the
other credential's reasoning before sending. When a provider rejects
encrypted reasoning, chatd retries without reasoning as before and
stamps the step's new reasoning with the provider's next epoch, so later
steps on any replica leave out the older reasoning and replay the newer.

A retried step that returns no reasoning records no new epoch, so the
next step is rejected and retried once more.
@ibetitsmike
ibetitsmike force-pushed the mike/openai-reasoning-continuity branch from abccede to 5fb3ce1 Compare October 12, 2026 15:19
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

Xum requested this review on behalf of @ibetitsmike.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5fb3ce1689

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/message_conversion.go Resolved
A rejection advanced the epoch for every model and credential on the
provider, although the rejected request only replayed reasoning from its
own model and credential. Switching back to another model, or from a
personal key back to the deployment's credential, then dropped reasoning
that was still valid. Each provider, model, and credential now has its
own epochs.

A retried step that returned no reasoning also lost the new epoch, so
the next step replayed the rejected reasoning and was rejected again.
Such a step now records the epoch on its first part.
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

Xum requested this review on behalf of @ibetitsmike.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 62b5cb532a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if part.ProviderIdentity != "" {
source = stampedReasoningProvenance(part)
}
return source != target && hasOpenAIReasoningState(part)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Include the OpenAI transport in reasoning provenance

When an administrator changes only openai_config.use_responses_api on an existing model config, the provider UUID, model, credential, and epoch still match here, so Responses-only reasoning is retained even when the target now uses Chat Completions. Empty-summary encrypted reasoning is deliberately preserved by this change, but Chat Completions cannot replay that Responses item and may receive an empty or invalid assistant-history entry. Include the resolved transport in the provenance comparison, or drop OpenAI reasoning whenever the target transport is not Responses.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not changing this. Only the openai provider's use_responses_api override can change the transport without changing the model (Azure follows the model list, and openai-compat never uses Responses). After that switch, fantasy's Chat Completions converter (DefaultToPrompt) skips reasoning parts, and it drops an assistant message left with no text and no tool calls, with a warning.

I checked this with a throwaway test against 62b5cb5: one turn on Responses with encrypted reasoning before a tool call, then use_responses_api set to false on the same config, then another turn. That request went to /chat/completions without the reasoning item or blob, with no empty assistant entry, and the chat finished waiting with no error. Switching back to Responses replays the reasoning, which is still valid for that provider, model, and credential.

Xum replied on behalf of @ibetitsmike.

@ethanndickson ethanndickson left a comment

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.

👍

Comment thread coderd/apidoc/swagger.json Resolved
ProviderIdentity, ProviderModel, ProviderCredential, and ReasoningEpoch
are stripped before API responses, so they no longer appear in the
Swagger spec or the generated API reference.
…-continuity

# Conflicts:
#	coderd/x/chatd/integration_responses_test.go
#	go.mod
#	go.sum
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

Xum requested this review on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 29a705e099

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike merged commit b2c8798 into main Oct 13, 2026
37 checks passed
@ibetitsmike
ibetitsmike deleted the mike/openai-reasoning-continuity branch October 13, 2026 11:49
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants