Skip to content

fix(agents): guard null response body in proxy stream reader - #107325

Merged
steipete merged 2 commits into
openclaw:mainfrom
maweibin:fix/proxy-null-body-guard
Jul 16, 2026
Merged

steipete merged 2 commits into
openclaw:mainfrom
maweibin:fix/proxy-null-body-guard

Conversation

@maweibin

@maweibin maweibin commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

streamProxy() in src/agents/runtime/proxy.ts accesses response.body! with a non-null assertion after checking response.ok, but response.ok === true does not guarantee response.body !== null. A real HTTP 204 response (or a misconfigured reverse proxy that returns an empty 200 envelope) produces ok=true, body=null through the real node:http → undici fetch stack.

When triggered, the caller receives "Cannot read properties of null (reading 'getReader')" — a JavaScript TypeError that describes an implementation detail (null reference on getReader) rather than the actual problem (the upstream proxy returned no body). A developer seeing this would debug proxy.ts internals rather than the proxy server configuration.

Sibling transports in this repository already guard this case:

File Guard
provider-transport-fetch.ts:148 if (!response.body) return response;
provider-transport-fetch.ts:385 if (!params.response.ok || !params.response.body)
provider-transport-fetch.ts:541 if (!response.body)
anthropic-transport-stream.ts:847 if (!response.body)
openai-completions-transport.ts:272 if (!response.body || !response.ok)
mcp-http-fetch.ts:60 if (response.body != null)
mcp-http-fetch.ts:76 if (!response.body)

Three merged PRs applied this same pattern to transports that previously lacked it:

proxy.ts:343 was the last remaining transport without this guard.

Why This Change Was Made

A 4-line null guard is added before getReader() so the caller receives a business-level error ("Proxy returned an empty response body") instead of a JS internal TypeError. The existing try-catch in streamProxy already converts thrown errors to stream error events — no new error propagation path is introduced.

streamProxy is a public SDK API: src/agents/runtime/proxy.ts → src/plugin-sdk/agent-core.ts (re-export) → packages/plugin-sdk/dist/ (compiled type declarations). A public API should not leak implementation-level error messages.

A committed regression test (proxy.test.ts) ensures the business-level error is preserved through refactoring.

User Impact

Operators debugging proxy connection failures see "Proxy returned an empty response body" — a message that identifies the upstream proxy as the problem. Previously they saw "Cannot read properties of null (reading 'getReader')" — a message that suggests a code bug. Normal streaming paths are unaffected.

Evidence

Real HTTP server proof — ok=true, body=null through real fetch

A real node:http server returns 204 No Content and the response is fetched through the real network stack (no stub, no mock):

$ node --import tsx /tmp/proxy-proof-server.mts

[proof] server at http://127.0.0.1:36747/api/stream
[proof] ok=true status=204 body=NULL
[proof] CONFIRMED: real HTTP response ok=true, body=null
[proof] WITHOUT the guard, calling response.body!.getReader() throws:
[proof]   → TypeError: Cannot read properties of null (reading 'getReader')
[proof] WITH the guard, streamProxy throws:
[proof]   → "Proxy returned an empty response body"

This proves that a real HTTP exchange through the full Node.js fetch → undici → TCP stack produces the ok=true, body=null shape. The guard replaces an internal TypeError with a business-level diagnostic.

Negative control — upstream main source + new regression test

The new test applied to upstream/main source (before the fix):

$ node scripts/run-vitest.mjs src/agents/runtime/proxy.test.ts --run -t "null response body"

 FAIL  streamProxy > emits a business-level error when the proxy returns a null response body
AssertionError: expected … to match object …

- Expected  |  + Received
  "errorMessage": "Proxy returned an empty response body",
  "errorMessage": "Cannot read properties of null (reading 'getReader')",

Upstream main leaks the JS runtime TypeError — the caller sees "Cannot read properties of null" instead of identifying the upstream proxy as the cause.

After fix — full test suite

$ node scripts/run-vitest.mjs src/agents/runtime/proxy.test.ts --run
 Test Files  1 passed (1)
      Tests  14 passed (14)

The new test ("emits a business-level error when the proxy returns a null response body") is included in the 14. The existing 13 tests pass unchanged.

Lint

$ node scripts/run-oxlint.mjs src/agents/runtime/proxy.ts
exit 0

$ node scripts/run-oxlint.mjs src/agents/runtime/proxy.test.ts
exit 0

Delta

$ git diff --stat upstream/main..
 src/agents/runtime/proxy.test.ts | 29 +++++++++++++++++++++++++++++
 src/agents/runtime/proxy.ts      |  6 +++++-
 2 files changed, 34 insertions(+), 1 deletion(-)

Source: +4 lines (guard). Test: +29 lines (regression test).

Observed result after fix: Null-body proxy responses produce "Proxy returned an empty response body". Normal streaming paths are unchanged. The regression test locks the business-level error message.

What was not tested: A live misconfigured proxy server in production returning empty 200 envelopes. The 204 No Content response from a real node:http server proves the exact ok=true, body=null shape through the real fetch/undici/TCP stack — the same code path that streamProxy's fetch() call exercises.

Regression Test Plan

$ node scripts/run-vitest.mjs src/agents/runtime/proxy.test.ts --run
 Test Files  1 passed (1)
      Tests  14 passed (14)

$ node scripts/run-oxlint.mjs src/agents/runtime/proxy.ts
exit 0

$ node scripts/run-oxlint.mjs src/agents/runtime/proxy.test.ts
exit 0

Root Cause

In streamProxy(), response.ok is checked (line 328) before entering the SSE read loop. When ok is true, the code calls response.body!.getReader() (line 343) with a non-null assertion, assuming a 2xx response always carries a readable stream body. The Fetch Standard does not guarantee this — response.body is null for 204 No Content, 304 Not Modified, responses with an empty payload, and responses from reverse proxies that return header-only status envelopes.

The 9 other response.body access sites across the repository's transport layer all perform an explicit null check. PRs #98143, #97851, and #99340 applied this pattern to files that previously lacked it. proxy.ts:343 inherits the identical risk but was not updated in those rounds.

AI Assistance

Implementation, test, real-server proof, and PR body were drafted with agent assistance. All commands were run locally.

The proxy transport used response.body! (non-null assertion) to enter the
SSE read loop without checking whether the body is present.  response.ok
does not guarantee response.body is non-null — empty 200 responses and
prematurely-closed connections leave body=null while ok=true.

Sibling transports in the same repository already guard this case:
  - provider-transport-fetch.ts:148,385,541
  - anthropic-transport-stream.ts:847
  - openai-completions-transport.ts:272
  - mcp-http-fetch.ts:60,76  (added in openclaw#98143)

Add the same guard before getReader() so the caller receives a business-
level diagnostic ("Proxy returned an empty response body") instead of
the misleading JS runtime error "Cannot read properties of null (reading
'getReader')".  The try-catch in streamProxy already catches the error
and pushes it as a stream event, so this does not introduce a new crash
— it replaces an internal TypeError with a meaningful error.
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Jul 14, 2026
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. labels Jul 14, 2026
@clawsweeper

clawsweeper Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 15, 2026, 10:13 PM ET / July 16, 2026, 02:13 UTC.

Summary
The PR guards a null HTTP response body before reading the agent proxy SSE stream and adds regression coverage for the resulting proxy-specific error.

PR surface: Source +4, Tests +29. Total +33 across 2 files.

Reproducibility: yes. A real local HTTP 204 response reproduces ok=true with body=null, and the focused negative control against current main records the existing getReader TypeError.

Review metrics: none identified.

Merge readiness
Overall: 🦞 diamond lobster
Proof: 🦞 diamond lobster
Patch quality: 🦞 diamond lobster
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Next step before merge

  • [P2] No repair lane is needed; the patch is correct and sufficiently proven, leaving ordinary maintainer merge review and current-head gating.

Security
Cleared: The focused null check and test introduce no concrete security or supply-chain concern and add no dependencies, permissions, secrets handling, or executable infrastructure.

Review details

Best possible solution:

Land the narrow null-body guard with its focused regression test, preserving the existing streaming and error-event behavior for all responses that provide a readable body.

Do we have a high-confidence way to reproduce the issue?

Yes. A real local HTTP 204 response reproduces ok=true with body=null, and the focused negative control against current main records the existing getReader TypeError.

Is this the best way to solve the issue?

Yes. Checking the body at the single ownership point immediately before getReader() is the narrowest maintainable fix and matches established sibling transport handling without adding fallback behavior or new API surface.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 08ecf63bb608.

Label changes

Label changes:

  • add proof: sufficient: Contributor real behavior proof is sufficient. The updated PR body supplies after-fix terminal proof through a real local HTTP server and Node fetch path, with a pre-fix negative control and focused production-path result; the displayed localhost endpoint contains no sensitive data.
  • add rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The updated PR body supplies after-fix terminal proof through a real local HTTP server and Node fetch path, with a pre-fix negative control and focused production-path result; the displayed localhost endpoint contains no sensitive data.
  • remove rating: 🦪 silver shellfish: Current PR rating is rating: 🦞 diamond lobster, so this older rating label is no longer current.
  • remove status: 📣 needs proof: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P2: This is a valid, bounded agent-runtime diagnostic bug with limited blast radius and no evidence of data loss, security bypass, or core-runtime unavailability.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The updated PR body supplies after-fix terminal proof through a real local HTTP server and Node fetch path, with a pre-fix negative control and focused production-path result; the displayed localhost endpoint contains no sensitive data.
  • proof: sufficient: Contributor real behavior proof is sufficient. The updated PR body supplies after-fix terminal proof through a real local HTTP server and Node fetch path, with a pre-fix negative control and focused production-path result; the displayed localhost endpoint contains no sensitive data.
Evidence reviewed

PR surface:

Source +4, Tests +29. Total +33 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 5 1 +4
Tests 1 29 0 +29
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 34 1 +33

What I checked:

  • Guarded stream read: The patch checks response.body before getReader() and converts the null-body condition into the existing stream error path with a proxy-specific diagnostic. (src/agents/runtime/proxy.ts:343, 243a2469ba55)
  • Regression coverage: The added test verifies that a successful response with body=null ends with stopReason=error and the expected proxy-specific message instead of leaking a getReader TypeError. (src/agents/runtime/proxy.test.ts:602, 243a2469ba55)
  • Real HTTP proof: Contributor-supplied terminal output uses a real node:http server and the Node fetch/Undici network path to show ok=true, status=204, and body=null, then records the pre-fix TypeError and post-fix proxy-specific error. (243a2469ba55)
  • Current-main negative control: The contributor ran the new focused regression against the upstream main implementation and observed the internal null getReader TypeError instead of the intended proxy diagnostic, demonstrating that the central problem remains on main. (src/agents/runtime/proxy.ts:343, 08ecf63bb608)
  • Focused validation: The PR reports all 14 proxy runtime tests passing after the change, both touched files passing the repository lint wrapper, and the repository CI gate succeeding. (src/agents/runtime/proxy.test.ts:602, 243a2469ba55)
  • Existing architectural provenance: The current OpenClaw-owned agent runtime boundary, including the relevant runtime area, dates through the merged agent-runtime internalization work. (src/agents/runtime/proxy.ts:1, bb46b79d3c14)

Likely related people:

  • steipete: The merged agent-runtime internalization work established the current OpenClaw-owned runtime structure containing this proxy path. (role: introduced current runtime ownership boundary; confidence: medium; commits: bb46b79d3c14; files: src/agents/runtime/proxy.ts)
  • Pick-cat: Recent merged work hardened several closely related null-body and body-less HTTP transport paths, making this contributor relevant for consistency review across sibling implementations. (role: recent adjacent transport contributor; confidence: medium; commits: 54d2fa240d8f, f13ce2db96ca, 7c9c2ef0c369; files: extensions/mattermost/src/mattermost/client.ts, src/agents/mcp-http-fetch.ts, src/infra/http-error-body.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.
Review history (1 earlier review cycle)
  • reviewed 2026-07-14T13:25:31.527Z sha 28dcb38 :: needs real behavior proof before merge. :: none

@maweibin

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 16, 2026
@steipete steipete self-assigned this Jul 16, 2026
@steipete
steipete merged commit f9e7a4c into openclaw:main Jul 16, 2026
197 of 204 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 17, 2026
…w#107325)

* fix(agents): guard null response body in proxy stream reader

The proxy transport used response.body! (non-null assertion) to enter the
SSE read loop without checking whether the body is present.  response.ok
does not guarantee response.body is non-null — empty 200 responses and
prematurely-closed connections leave body=null while ok=true.

Sibling transports in the same repository already guard this case:
  - provider-transport-fetch.ts:148,385,541
  - anthropic-transport-stream.ts:847
  - openai-completions-transport.ts:272
  - mcp-http-fetch.ts:60,76  (added in openclaw#98143)

Add the same guard before getReader() so the caller receives a business-
level diagnostic ("Proxy returned an empty response body") instead of
the misleading JS runtime error "Cannot read properties of null (reading
'getReader')".  The try-catch in streamProxy already catches the error
and pushes it as a stream event, so this does not introduce a new crash
— it replaces an internal TypeError with a meaningful error.

* test(agents): cover null response body in proxy stream reader
zooqueen pushed a commit to zooqueen/openclaw that referenced this pull request Sep 11, 2026
…w#107325)

* fix(agents): guard null response body in proxy stream reader

The proxy transport used response.body! (non-null assertion) to enter the
SSE read loop without checking whether the body is present.  response.ok
does not guarantee response.body is non-null — empty 200 responses and
prematurely-closed connections leave body=null while ok=true.

Sibling transports in the same repository already guard this case:
  - provider-transport-fetch.ts:148,385,541
  - anthropic-transport-stream.ts:847
  - openai-completions-transport.ts:272
  - mcp-http-fetch.ts:60,76  (added in openclaw#98143)

Add the same guard before getReader() so the caller receives a business-
level diagnostic ("Proxy returned an empty response body") instead of
the misleading JS runtime error "Cannot read properties of null (reading
'getReader')".  The try-catch in streamProxy already catches the error
and pushes it as a stream event, so this does not introduce a new crash
— it replaces an internal TypeError with a meaningful error.

* test(agents): cover null response body in proxy stream reader
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: XS status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants