fix(agent): web_fetch returns the real body; zero-byte writes fail loudly

SW
4mo ago
+103-33

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 mktemp file. stdout contract is status\nbyteSize\n<body> — the real size survives even though the body is capped at MAX_BODY_LENGTH for context.
  • truncated is derived from the byte size, not curl's exit code.
  • A nonzero exit that produced zero bytes returns success: false with 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):

testcovers
parses status, size and body on successhappy path
preserves a body that itself contains newlinesold last-newline split bug
marks truncated by BYTE SIZE, not by exit codefix #2
returns success:false when curl exits 23 with an empty bodythe regression this PR exists for
returns success:false on any nonzero exit with an empty bodyfix #3
does not use bash process substitutionguards the root cause
passes the request body for POSTunchanged 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 pass
  • pnpm exec eslint on both files → clean
  • pnpm exec tsc --noEmit → the 3 TS2769 errors on webFetchTool.ts L48-50 are pre-existing on main (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 mktemp and emit status\nbyteSize\n<body> on stdout.
    • Derive truncated from byte size, not curl exit code.
    • Return success: false on 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.

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.