Skip to content

fix(design-code-proxy): don't forward the backend's Content-Length for re-serialized bodies (#684) - #685

Merged
dezren39 merged 1 commit into
mainfrom
sweep/684-design-content-length
Oct 23, 2026
Merged

dezren39 merged 1 commit into
mainfrom
sweep/684-design-content-length

Conversation

@dezren39

@dezren39 dezren39 commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Fixes #684

Problem

PR #682's writeHead() helper in bin/design-code-proxy.mjs spread the backend's response headers verbatim — including its Content-Length — but the non-stream success path then sends a different body: the parsed JSON re-serialized with model rewritten and pretty-printing (JSON.stringify(json, null, 2)). Whenever the backend sets Content-Length (the normal behaviour of a buffered JSON server), the client reads only the backend's byte count and gets truncated, unparseable JSON; on a keep-alive connection the surplus bytes also corrupt the next response's framing.

Reproduced on main c6ec7bd6 with a stub backend returning a 191-byte compact JSON body with an explicit Content-Length: 191: the proxy forwarded content-length: 191 with a 283-byte re-serialized body — JSON.parse of the received body fails at position 191, and a raw HTTP client hits HPE_INVALID_CONSTANT parsing the leftover bytes as a new response. The existing suite couldn't see it: its stubs never set Content-Length, so Node answered chunked.

Solution

writeHead(sentBody) now owns the framing headers:

  • When the exact body being sent is known (non-stream JSON success, and the non-JSON byte-for-byte catch branch), it advertises that body's real byte length.
  • For streaming (chunks are also model-rewritten, total length unknown), it drops the stale Content-Length and lets Node chunk the response.
  • Transfer-Encoding from the backend is never forwarded alongside the new framing.

Streaming success behaviour is otherwise unchanged, and the byte-for-byte catch branch keeps a correct length (its own byte count).

Tests

  • New stub mode ok-length + regression test backend Content-Length does not truncate the re-serialized success body (design-code-proxy: forwarded Content-Length truncates the re-serialized success body (PR #682) #684): asserts the advertised length (when present) equals the received byte count, the body parses, content and rewritten model are correct, and no fallback call happens.
  • Ablation: with only bin/design-code-proxy.mjs reverted, exactly the new test fails, with the issue's exact symptom (JSON.parse error at position 191); the other 5 design tests still pass.
  • Full proxy suites on the branch: node --test bin/design-code-proxy.test.mjs bin/muse-proxy.test.mjs bin/codex-proxy.test.mjs bin/proxy-port.test.mjs → 25/25.
  • Standalone repro against the fixed proxy: content-length: 283, full body received, parses, model: design-code-flash.

Single-repo: design-code-proxy has no copy in developing-today/opencode, developing-today/openchamber, or dezren39/nix (git-grep on each clone's origin/main).


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

Summary by CodeRabbit

  • Bug Fixes
    • Improved response handling for proxied requests by ensuring content-length headers match the bytes sent for buffered responses and are omitted when they could be inaccurate for streamed responses.
    • Removed transfer-encoding headers before forwarding responses, helping prevent conflicting or misleading header information.
    • Added coverage for successful responses with an explicit content length, including verification that the rewritten response is complete and reaches the expected backend.

…r re-serialized bodies (#684)

writeHead() spread the backend's headers verbatim, including its
Content-Length, but the non-stream success path sends a re-serialized
body (model rewritten, pretty-printed) that is longer than the
backend's compact body — clients read only the backend's byte count
and got truncated, unparseable JSON (and corrupt keep-alive framing).

writeHead now takes the exact body being sent when it is known and
advertises its real length, and drops the stale Content-Length /
Transfer-Encoding for the streaming path, whose chunks are also
rewritten. Byte-for-byte forwarded bodies keep a correct length
(their own byte count).

New regression test: a stub backend that sets an explicit
Content-Length on its compact JSON success body; the client must
receive the full model-rewritten body. Ablation: with only the proxy
change reverted, exactly that test fails (JSON.parse at position 191,
the backend's length).
@dezren39
dezren39 requested a review from a team as a code owner October 23, 2026 20:01
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 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-23T20:02:07.261537Z 3a563eb PR opened
ℹ️ 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.

@coderabbitai

coderabbitai Bot commented Oct 23, 2026

Copy link
Copy Markdown

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c935467d-981b-4e73-9618-a3414590ed14
📥 Commits

Reviewing files that changed from the base of the PR and between c6ec7bd and 3a563eb.

📒 Files selected for processing: 2
  • bin/design-code-proxy.mjs
  • bin/design-code-proxy.test.mjs
+----------------------------------------------------------------------------------+
| This is the beginning of a beautiful friendship...between you and bug-free code. |
+----------------------------------------------------------------------------------+
  \
   \   (\__/)
       (•ㅅ•)
       /   づ
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

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.

design-code-proxy: forwarded Content-Length truncates the re-serialized success body (PR #682)

1 participant