Skip to content

fix(cli): answer a body-less middleware reject with its status, not 500 - #711

Merged
blove merged 2 commits into
mainfrom
blove/agitated-darwin-b773bd
Sep 17, 2026
Merged

blove merged 2 commits into
mainfrom
blove/agitated-darwin-b773bd

Conversation

@blove

@blove blove commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

The bug

statusResponse built every middleware reject with Response.json(body), which throws when body is undefined:

node -e 'Response.json(undefined, { status: 401 })'
# TypeError: Value is not JSON serializable

reject(status) with the body omitted is a supported, documented call — the middleware docs show return reject(403) // body omitted, and the SDK helper deliberately returns { action: "reject", status } with no body key. That throw landed in the existing catch, so an app gating a request with reject(401) sent the caller 500 Unexpected runtime server failure instead 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, build new Response(null, ...) directly, inside the existing try so 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 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. Measured against a real http.createServer:

401  old: content-type: application/json | Content-Length: 0
401  new: content-type: application/json | Transfer-Encoding: chunked

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: 0 on 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.

  • Unit (runtime-fetch-parity.test.ts): an omitted body across five statuses, an explicit null body (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.
  • Framing (node-web-adapter.test.ts): a body-less JSON reply is content-length framed, and the null-body statuses carry no content-length.
  • End to end (middleware-lifecycle.test.ts): createRuntimeFetchHandler driven with the real reject helper across AG-UI, runs/stream and runs/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.ts documented this exact bug as the reason for its body !== undefined guard. 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 undefined makes POST /threads/:id/runs/wait answer 500 while runs/stream answers 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 typecheck all 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/cli included.

🤖 Generated with Claude Code

`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]>
@vercel

vercel Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
b4-run Ready Ready Preview Sep 17, 2026 8:34pm UTC

Request Review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated approval: this PR received an intelligent (AI) code review. See the review comments on this PR.

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
blove merged commit 4b1b398 into main Sep 17, 2026
29 of 30 checks passed
@blove
blove deleted the blove/agitated-darwin-b773bd branch September 17, 2026 21:42
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

1 active and 1 inactive deployments
vercel-preview — d08e8d5b Deployed Sep 17, 2026 by blove via vercel-native #2009
Preview — d08e8d5b Deployed Sep 17, 2026 by vercel[bot]
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.

1 participant