Skip to content

Studio: report a Claude reply cut off by the context window as truncated - #10557

Merged
Lyxot merged 5 commits into
unslothai:mainfrom
NilayYadav:fix-anthropic-context-window-truncation
Sep 9, 2026
Merged

Lyxot merged 5 commits into
unslothai:mainfrom
NilayYadav:fix-anthropic-context-window-truncation

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

When a long thread fills the context window, Anthropic ends the reply with a stop reason Studio did not know about, so it fell through to "stop". The message rendered as finished with no notice, and API clients got finish_reason stop for a reply that ended mid sentence.

It now maps to "length" like any other cut off reply, so the truncation notice and Continue button appear. Automatic continuation is not triggered, because replaying the partial into a full window fails the same way each time and would cost three extra requests.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 8, 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-09-09T10:34:28.677038Z 19a1abb 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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: a54f179601

ℹ️ 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".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026

@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: 1a5bd654d2

ℹ️ 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 on lines +7745 to +7748
const finalIncompleteReason = resolveIncompleteReason(
incompleteReason,
contextWindowExceeded,
);

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 Preserve the context-window marker during durable recovery

When the tab reloads, closes, or otherwise hands an in-progress durable generation to scheduleGenerationRecovery, this live-adapter resolution is bypassed. The recovery loop in runtime-provider.tsx replays the new _toolEvent without recording it, and generationRecoveryMetadata stamps every completed run whose finishReason is "length" as { reason: "length" }; consequently, a recovered context-window-exhausted response is automatically continued up to three times—the behavior this change is intended to prevent. The durable recovery path must latch context_window_exceeded and persist context_window as well.

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.

Checked, and this path cannot be reached: the two conditions are mutually exclusive.

context_window_exceeded has exactly one producer, _stream_anthropic (external_provider.py:3227), so it only ever rides an external-connection turn. Durable recovery only ever runs on a message whose metadata carries generationRunId (runtime-provider.tsx:1634 and :1981), and that id is assigned in a single place, chat-adapter.ts:6249, inside the durable admission branch. Admission requires generationCandidate, whose first clause is !isExternalRequest (chat-adapter.ts:4963).

So an external turn is never a durable candidate, never gets a generationRunId, and is never handed to scheduleGenerationRecovery -- generationRecoveryMetadata has no way to see a context-window-exhausted run and restamp it. If durable runs are ever extended to external connections the latch will have to travel with them, but today there is nothing to preserve here.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@NilayYadav
NilayYadav force-pushed the fix-anthropic-context-window-truncation branch from 1a5bd65 to 00f5e1a Compare September 8, 2026 22:37
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 00f5e1a0f7

ℹ️ 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".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 00f5e1a0f7

ℹ️ 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".

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Reproduction on GitHub Actions + before/after UI evidence

Reviewed head 00f5e1a0f7438612baba922e7b530bfe891d6a5c, merge base 580cffabac84cf0d373d27ebed2af4251dbbf88e.

The defect on Actions. Two branches off the same fresh upstream main, same workflow, same runner. The only difference is whether the fix is applied; the negative branch carries this PR's tests and nothing else.

  • Fix absent — failing run: the backend test fails at assert ['stop'] == ['length'], and "_toolEvent": {"type": "context_window_exceeded"} is absent from the stream. Frontend: 84 passed, 4 failed.
  • PR head — passing run: backend 4 passed, frontend 88 passed, 0 failed.

What the user sees. The turn is driven for real through Studio's own backend; the connection's base URL points at a stand-in Messages API that streams a sentence ending mid-word and then reports stop_reason: "model_context_window_exceeded". Nothing reaches Anthropic and no key is needed, but the request is built by the real frontend and translated by the real _stream_anthropic.

PR 10557 before/after

before after
truncation_bar_present false true
truncation_bar_text "" Response filled the model's context window. Continue
continue_button_present false true

The reply itself is identical on both sides, ending at ...and third, the decommis — what changed is that the truncation is now reported instead of the half answer being painted as a finished one.

Verified against the API docs. model_context_window_exceeded is a documented stop reason meaning the response filled the model's context window, returned without a beta header on Sonnet 4.5 and newer. Older models require the model-context-window-exceeded-2025-08-26 beta header, which Studio does not send, so their behaviour is unchanged by this PR.

Automatic continuation stays off it. shouldAutoContinue returns early unless the reason is exactly length, so a context_window cut renders the notice and the manual Continue button without spending paid rounds replaying the partial into a window that just overflowed.

Not reachable, checked: durable-run recovery cannot restamp such a turn as length — generationRunId is assigned only inside the durable admission branch, which is gated on !isExternalRequest, while the tool event has exactly one producer in _stream_anthropic. The two sets are disjoint.

@Lyxot Lyxot self-assigned this Sep 9, 2026
The stop_reason table falls back to finish_reason "stop", which is why a reply that filled the context window read as a finished answer until it was mapped. The fallback stays right for most reasons, but a truncating one added upstream is invisible until someone notices the symptom. Warn instead, naming the reason and the model.

The context-window cases move in beside the refusal ones, which already cover streaming stop_reason translation through the same transport harness, rather than repeating that harness in a file of their own.
Mapping the stop reason to `length` made the turn read as truncated, but left it offering Continue, and left the reason itself easy to lose before the turn was saved.

Continue cannot help here. Resuming replays the partial as more prompt against the window it just filled, and a hosted window is not the "Context Length" setting the other context messages point at, so the bar names the way out instead of offering the button. It is named even when nothing could be resumed: a tool-calling turn never can be, and Anthropic's hosted search and code execution make that the likeliest way to fill a window at all.

The reason also has to survive to the saved turn. It now outranks whatever the client infers about how the stream ended, at every site that decides a reason, and is published the moment it arrives rather than at a terminal yield an abort skips -- with or without a body, since redacted thinking spends context while rendering as no text. `isProviderReportedReason` states the same rule for the bar, which holds a stamped reason rather than the latch.
@Lyxot

Lyxot commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

Pushed two commits on top of your branch after reviewing it. Your commit is unchanged; this is a fast-forward.

181ad61e19 — log a stop reason nothing maps. _finish_reason_map.get(stop_reason, "stop") is the mechanism that hid this reason in the first place, and it will hide the next one. The stop default stays, but an unmapped reason now warns with the reason and the model. The new tests moved in beside the refusal ones in test_anthropic_fast_mode_and_refusal.py, which already covers streaming stop-reason translation through the same harness, so test_anthropic_context_window_exceeded.py is gone and its duplicated harness with it.

19a1abba48 — report the turn as one no continuation can undo. Two things beyond the mapping:

The bar offered Continue for a cut that cannot be continued. Resuming replays the partial as more prompt against the window it just filled, which is the same reasoning behind keeping auto-continue off it, so the bar now names the way out instead of offering the button. It is named even when nothing could be resumed — isContinuableContent rejects a tool-call part, so a turn that ran hosted web search or code execution was never resumable and rendered no bar at all, which is the likeliest way to fill a window in the first place.

The reason also had to survive to the saved turn. It was reaching the terminal yield and being dropped everywhere else: the error path reclassified it, an abort saved the reason latched before it arrived, and it was only published at a yield that nothing between the event and [DONE] has to produce. It now outranks what the client infers at every site that decides a reason, and is published the moment it arrives, with or without a body — redacted thinking spends context while rendering as no text.

Not done, deliberately: anthropicRefusalSeen has the same latch-without-publish shape and loses the flag on Stop, so pruneOutboundHistory cannot find the refused turn. It predates this PR, so it is left for separate work.

Validation: backend 45 passed, frontend 1320/1321 (the failure is find-in-page's time-budgeted segmenter test, which fails identically without this branch), tsc -b clean. Every added test was mutation-checked — reverting the production line it covers fails exactly that test.

@Lyxot

Lyxot commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 19a1abba48

ℹ️ 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".

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving. The stop reason is real and documented, the mapping is right, and the truncation now reaches the UI and OpenAI-compatible clients as a cut rather than a finished answer.

Note the last two commits are mine, so this approval is on the original change; a second maintainer eye on those two would be welcome.

CI is green apart from Repo tests (CPU), whose five failures (core-dump policy, installer handoff ordering, ROCm gfx1151) are pre-existing on main and unrelated to this branch.

@Lyxot
Lyxot merged commit dc1b0be into unslothai:main Sep 9, 2026
35 of 41 checks passed
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.

2 participants