Repository navigation
fix(cli): answer a body-less middleware reject with its status, not 500 - #711
Merged
Merged
Conversation
`statusResponse` built every middleware reject with `Response.json(body)`, which throws when `body` is `undefined`. The documented body-omitted form of `reject(status)` therefore landed in the catch and answered 500 "Unexpected runtime server failure" instead of the 401/403 the app asked for, on the AG-UI and both Agent Protocol run endpoints. A body-less reject now sends an empty payload under the JSON content-type at the requested status, matching the pre-fetch-core `sendJson`, which did `res.end(JSON.stringify(undefined))`. The branch sits inside the existing `try`, so an out-of-range status still becomes the standard 500. Rejects that supply a body are untouched. Regressed in #373, when the Node listener became an adapter over the web-standard core. Co-Authored-By: Claude Opus 5 <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
blove
had a problem deploying
to
vercel-preview
September 17, 2026 20:28 — with
GitHub Actions
Error
Review follow-up to the previous commit, which claimed exact wire parity for a body-less reject. It was not: `writeNodeResponse`'s `!response.body` branch calls `writeHead` before Node can see the body is empty, so Node picked `Transfer-Encoding: chunked` where the old `res.end(JSON.stringify(undefined))` sent `Content-Length: 0`. Verified against a real `http.createServer`. That also broke the adapter's own asserted invariant that JSON replies use content-length framing — a body-less reject is a JSON reply. Set `content-length: 0` explicitly on the body-less branch, guarded on status: Node forwards an explicit content-length even on 204/205/304, where the old path never sent one and RFC 7230 3.3.2 forbids it, so those keep Node's own suppression. Also pins the null-vs-undefined distinction, which nothing tested: `==` in place of `===` silently turned `reject(401, null)`'s "null" body into an empty one and left all tests green. Each new test was mutation-checked against the code it covers. Co-Authored-By: Claude Opus 5 <[email protected]>
blove
temporarily deployed
to
vercel-preview
September 17, 2026 21:00 — with
GitHub Actions
Inactive
blove
added a commit
that referenced
this pull request
Sep 17, 2026
None of the three touches this page: verify registry error codes, body-less middleware reject status, and a k8s smoke watchdog. Only the generated SEO manifest conflicted, resolved by regenerating per the procedure #712 added to AGENTS.md. Co-Authored-By: Claude Opus 5 <[email protected]>
blove
added a commit
that referenced
this pull request
Sep 18, 2026
`POST /threads/:id/runs/wait` serialized the route's return value with `Response.json`, which throws on `undefined`. A route that returned nothing therefore reported a successful run as `500 Unexpected runtime server failure`, while `/runs/stream` reported the same run as done. Such a run now answers 200 with the body `null`, so callers reading a property off the parsed body should narrow it first. Falsy outputs (0, false, "") are unaffected. `null` rather than the pre-`fetch`-core empty body: `normalizeServerResult`, B4's own client for this endpoint, parses every 200 body and would read an empty one as a malformed payload, trading the 500 for a transport error. `undefined` was not the only value `JSON.stringify` refuses. A circular object or a BigInt hit the same catch with the same opaque message; serialization now runs under its own catch and names the route and the reason. One fix covers all five build targets, which share this code path. Also guards the shipped dispatch-from-route recipe against a null body, and documents the endpoint's response contract. Closes #714. Same bug class as #711. Merged with admin: the required `validate` check is green, and the advisory `review` check failed only because the org's Anthropic credit balance is exhausted. An independent review stood in for it and caught the unserializable-output gap and the broken docs recipe.
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
statusResponsebuilt every middleware reject withResponse.json(body), which throws whenbodyisundefined:reject(status)with the body omitted is a supported, documented call — the middleware docs showreturn reject(403) // body omitted, and the SDK helper deliberately returns{ action: "reject", status }with nobodykey. That throw landed in the existing catch, so an app gating a request withreject(401)sent the caller500 Unexpected runtime server failureinstead of the 401 it asked for.It reached all three gated endpoints: AG-UI, and both Agent Protocol run endpoints (
runs/stream,runs/wait).Regressed in #373 (df54695), when the Node listener became an adapter over the web-standard core.
The fix
Commit 1 — when the body is
undefined, buildnew Response(null, ...)directly, inside the existingtryso an out-of-range status still becomes the standard 500. Rejects that supply a body are untouched.Commit 2 — the first commit claimed exact wire parity, and that was wrong.
writeNodeResponse's body-less branch callswriteHeadbefore Node can see the body is empty, so Node pickedTransfer-Encoding: chunkedwhere the oldres.end(JSON.stringify(undefined))sentContent-Length: 0. Measured against a realhttp.createServer:That also broke the adapter's own asserted invariant that JSON replies use content-length framing, since a body-less reject is a JSON reply. The fix sets
content-length: 0on that branch, guarded on status: Node forwards an explicit content-length even on 204/205/304, where the old path never sent one and RFC 7230 §3.3.2 forbids it, so those keep Node's own suppression.Tests
Written first, and confirmed failing against the old code.
runtime-fetch-parity.test.ts): an omitted body across five statuses, an explicitnullbody (valid JSON, must not be swallowed), an omitted body on the null-body statuses, an omitted body on an inexpressible status, and a circular body that must still 500.node-web-adapter.test.ts): a body-less JSON reply is content-length framed, and the null-body statuses carry no content-length.middleware-lifecycle.test.ts):createRuntimeFetchHandlerdriven with the realrejecthelper across AG-UI,runs/streamandruns/wait. A sibling case asserts a supplied body still arrives, which is what proves middleware actually runs on all three endpoints rather than the requests failing earlier.Every new assertion was mutation-checked against the code it covers, including the
===vs==null distinction, which nothing pinned before.Notes
The comment in
thread-gate.tsdocumented this exact bug as the reason for itsbody !== undefinedguard. The guard stays, since a deny deserves a named reason rather than an empty payload, but the rationale no longer states something untrue.A separate live instance of the same bug class is filed as #714: a route that returns
undefinedmakesPOST /threads/:id/runs/waitanswer 500 whileruns/streamanswers 200 for the same run. Same regression from #373, but on the route-output path, and the right behavior there is a design question rather than a parity restoration — so it is deliberately not in this PR.The automated reviewer could not run on this PR: its preflight got HTTP 400, "Your credit balance is too low to access the Anthropic API." Both defects above were instead found by an independent adversarial review of the diff.
Verification
Node 24.
pnpm lint,pnpm build,pnpm typecheckall pass. CLI suite: 1932 passed, 4 skipped. The esbuild error lines in that output come from negative-path fixtures and are expected.Patch changeset for
@b4run/cliincluded.🤖 Generated with Claude Code