fix(agent): web_fetch returns the real body; zero-byte writes fail loudly
Closes the web_fetch row of chat#1918. No docs change — web_fetch is an internal sandbox agent tool with no OpenAPI contract.
The bug
webFetchTool.ts L66 captured the response body with bash process substitution:
"-o", `>(head -c ${MAX_BODY_LENGTH} >&3)`,
That does not expand in the sandbox's exec shell. curl writes to a file literally named >(head -c 10000 >&3), fails, and exits 23. L100 then whitelisted exit 23 as "expected truncation" and L117 set truncated: true, so a hard write failure was reported as { success: true, status: 200, body: "", truncated: true }.
Every URL returned an empty body with a plausible status. Agents concluded the whole internet was unreachable — including docs.recoupable.dev, so they could not even discover the API surface.
Proof, under the actual shell
New command:
exit=0
200
559
<!doctype html><html lang="en"><head><title>Example Domain</title>…
Old command, same shell:
exit=1
sh: syntax error near unexpected token `('
Observed identically in a live sandbox run on 2026-07-30 (chat 329c4760-646b-4749-a7e7-82fd64f9cf72): bash curl https://example.com returned HTTP 200 / 559 bytes, while web_fetch on the same URL returned {"body": "", "status": 200, "success": true, "truncated": true}.
The fix
- Body goes to a
mktempfile. stdout contract isstatus\nbyteSize\n<body>— the real size survives even though the body is capped atMAX_BODY_LENGTHfor context. truncatedis derived from the byte size, not curl's exit code.- A nonzero exit that produced zero bytes returns
success: falsewith stderr, instead of a fake 200. - Body is parsed from the third line onward, so bodies containing newlines are preserved (previously it split on the last newline).
Tests
RED→GREEN, 7 tests in lib/agent/tools/__tests__/webFetchTool.test.ts (5 were RED before the change):
| test | covers |
|---|---|
| parses status, size and body on success | happy path |
| preserves a body that itself contains newlines | old last-newline split bug |
| marks truncated by BYTE SIZE, not by exit code | fix #2 |
| returns success:false when curl exits 23 with an empty body | the regression this PR exists for |
| returns success:false on any nonzero exit with an empty body | fix #3 |
| does not use bash process substitution | guards the root cause |
| passes the request body for POST | unchanged behavior |
The previous test suite asserted the buggy contract (exit 23 → success: true); that assertion is replaced.
pnpm exec vitest run lib/agent→ 50 files, 310 tests, all passpnpm exec eslinton both files → cleanpnpm exec tsc --noEmit→ the 3TS2769errors onwebFetchTool.tsL48-50 are pre-existing onmain(verified by stashing: identical lines and count), untouched by this change
Remaining verification
Done-when in the issue also asks for confirmation inside a real sandbox run. The local POSIX-sh repro above reproduces the failure and the fix exactly, but I have not yet exercised the deployed preview — worth doing before merge.
🤖 Generated with Claude Code
Summary by cubic
Fixes the web_fetch sandbox tool so it returns real response bodies and treats zero-byte writes as failures. Resolves the regression where every URL appeared as an empty 200 (chat#1918).
- Bug Fixes
- Write response to
mktempand emitstatus\nbyteSize\n<body>on stdout. - Derive
truncatedfrom byte size, not curl exit code. - Return
success: falseon any nonzero exit when size is 0; include stderr. - Parse the body from the third line to preserve newlines.
- Add tests covering status/size/body parsing, truncation, zero-byte exits, POST body, and guarding against process substitution.
- Write response to
Summary by CodeRabbit
- Bug Fixes
- Improved web content retrieval reliability across different environments.
- HTTP status codes are now validated more accurately, including failed or unavailable responses.
- Responses are truncated based on the actual downloaded size, improving consistency for large pages.
- Requests that return no content now report failure instead of appearing successful.