Skip to content

fix(responses): refuse input beyond the advertised context window (#1128) - #1167

Closed
HoshimiRox1 wants to merge 2 commits into
lidge-jun:devfrom
HoshimiRox1:fix(codex)/responses-input-guard-compaction
Closed

fix(responses): refuse input beyond the advertised context window (#1128)#1167
HoshimiRox1 wants to merge 2 commits into
lidge-jun:devfrom
HoshimiRox1:fix(codex)/responses-input-guard-compaction

Conversation

@HoshimiRox1

@HoshimiRox1 HoshimiRox1 commented Aug 7, 2026

Copy link
Copy Markdown

Summary

  • Reject parsed /v1/responses text input when the shared model-aware token estimate exceeds the final routed model's advertised context window.
  • Run the guard after previous_response_id expansion and final route selection, but before authentication, adapter construction, or upstream I/O.
  • Contain abnormal replay expansion observed in production: a roughly 400k-token conversation became a 1.6M-token request, ballooned Bun RSS on Windows, and native-crashed the proxy. The guard returns a clean 413 without calling upstream.
  • This PR is now intentionally limited to the independent input guard. The earlier DeepSeek compaction reframe was removed after upstream dev commit 0b8e608 restored live Responses streaming. Refs [Bug] DeepSeek V4 Flash remote auto-compaction needs v2.11+ reproduction #1128.

Verification

  • bun test tests/responses-input-guard.test.ts — 2 pass, 0 fail.
  • bun run typecheck — pass.
  • bun run privacy:scan — pass.
  • git diff --check — pass.
  • Full bun run test was attempted on Windows: unrelated codex-journal restoration tests remained red in isolation (8 failures), and the full run ended in a Bun 1.3.14 index-out-of-bounds crash after 816 seconds. The focused Responses tests and typecheck remain green, so this PR stays draft for CI confirmation.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed. No user-facing configuration or API contract changed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults. No credential or logging path changed.

Review readiness checklist

This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:

  • All CI tests are green on my local testing.

  • I pushed my PR to the latest dev commit.

  • I resolved all correct Codex and CodeRabbit findings.

  • My PR is ready for review.

A chained-turn replay can balloon a request far past the model's context window (observed: a 4x expansion pushed a ~400k-token conversation to 1.6M input tokens). The proxy forwarded it verbatim; processing it on Windows ballooned bun RSS and native-crashed the whole service (upstream Bun memory bug, lidge-jun#314), taking every active thread down until restart.

Reject the request with a clean 413 before any upstream I/O when the parsed input exceeds the model's configured modelContextWindows value. The client compacts well before the window, so the guard only fires on abnormal duplication.
The lidge-jun#875 transport policy forces a bounded JSON upstream for providers like deepseek (modelResponsesUpstreamStreaming: false), even when the client asked for SSE. The passthrough branch already reframes that JSON back to the canonical terminal SSE sequence, but routed-compaction turns skip the passthrough branch: they returned application/json, so Codex SSE parser hit EOF at the first byte and the remote compact task failed with "stream closed before response.completed", retrying 6x and leaving the thread unusable.

Mirror the passthrough reframe in both routed branches (runTurn and parseStream): when the client requested stream and the upstream policy forced JSON, emit response.created, one output_item.done per output item (including the synthetic compaction item), response.completed, then [DONE].
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: b7b4c56f-818a-46c2-a54b-61996c3ad713

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

⏳ DRAFT

  • review readiness checklist open (1/4 boxes ticked).

What to do

  • Tick all four boxes in the PR description once you're done (currently 1/4).

Review readiness checklist

  • ⬜ All CI tests are green on my local testing.
  • ✅ I pushed my PR to the latest dev commit.
  • ⬜ I resolved all correct Codex and CodeRabbit findings.
  • ⬜ My PR is ready for review.

1/4 boxes ticked.

This PR stays in draft until every box above is ticked.

@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Aug 7, 2026
@HoshimiRox1

Copy link
Copy Markdown
Author

Closing for now — will re-open after reviewing the process. Thanks!

@HoshimiRox1 HoshimiRox1 closed this Aug 7, 2026
@HoshimiRox1 HoshimiRox1 changed the title fix(responses): refuse oversized input and reframe routed compaction to SSE (#1128) fix(responses): refuse input beyond the advertised context window (#1128) Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant