Repository navigation
Description
Summary
Follow-up to #630 / #639. The defensive fix in #639 (per-line reader isolation + result-metadata recovery from raw_messages) covers the case where a stream-json line is parsed but processing is interrupted mid-loop. It does not address the underlying root cause flagged in #630: stdio MCP server children inherit Claude Code's stdout file descriptor and concurrent writes from Claude + the MCP child interleave on the wire, producing invalid JSON that json.loads silently skips.
When the result line itself is the casualty of interleaving, recovery cannot help — the line never lands in raw_messages because the JSON parse failed.
Root cause
When agent-server launches Claude Code via subprocess.Popen(stdout=PIPE), Claude inherits fd 1 = the pipe write-end going back to agent-server. When Claude then spawns an stdio MCP server (e.g. npx -y some-mcp-package):
- Claude opens a new pipe for the MCP-protocol channel
dup2s the new pipe onto the MCP child's fd 0/1- Anything the MCP child (or its npm bootstrap) writes to fd 1 goes through the protocol channel, not to agent-server
So fd 1 inheritance is not the direct path. The actual leak is one of:
- The MCP child has Claude's original stdout fd inherited as some fd > 2 (no
close_fds/FD_CLOEXEC), and the package writes to it via/proc/self/fd/Nor similar - The MCP child's stderr is dup2'd from Claude's stderr (which is also a pipe to agent-server), and a noisy package writes to stderr — but stderr is a separate reader, that goes into the stderr buffer, not stdout
- Claude itself buffers and flushes the result line in a way that interleaves with concurrent assistant-message streaming when an MCP server is producing stderr-on-stdout via npm wrapper
Need bisect to confirm which mechanism. The reporter in #630 saw consistent corruption with npx-launched MCP servers; reproduction with a known-pollutant MCP server is the first task.
Acceptance criteria
- Reliable reproduction: a minimal agent template + chat task that triggers "Failed to parse line as JSON" + missing
cost_usdconsistently (≥3/5 runs) - Root cause confirmed via fd inspection in the MCP child process (
/proc/{pid}/fd) or strace - Fix prevents corruption at source. Candidates (in order of preference):
- Wrap each
.mcp.jsoncommandso the spawned binary never inherits Claude's stdout/stderr — e.g. rewritecommandtosetsid bash -c 'exec "$0" "$@" 1>&3 2>&4 3>&- 4>&-'with appropriate fd plumbing for the MCP protocol - Patch the agent's CLAUDE.md / .mcp.json template so all MCP servers are launched via a wrapper script the agent ships
- If Claude Code supports
close_fds-style options for MCP launch, enable them
- Wrap each
- Existing chat / task / scheduling paths continue to work (no MCP regression)
- Test that asserts: with a deliberately-noisy stdio MCP server installed, a 50+ turn chat completes with
metadata.cost_usd != None
Related
- bug: stdio MCP subprocess stdout inheritance causes 'Execution completed without a result message' #630 — original report
- fix(agent-server): recover lost result metadata + harden per-line reader (#630) #639 — partial fix (per-line reader isolation + raw_messages recovery)
- bug(agent-runtime): npx MCP server subprocesses outside claude pgid hold stdout pipe open — reader stuck after #531 grace period #618 / bug: child processes inherit agent-server stdout pipe → reader thread stuck, false '0 tool calls' failures #548 — earlier bug: agent-server.py spins at ~83% CPU after claude CLI subprocess becomes defunct #407 / pgroup-escape fixes (different failure mode)
- @docs/memory/architecture.md — Agent Runtime + MCP integration sections
Why split from #639
#639 ships a defensive layer that's safe and unit-testable today and improves the failure UX (publish exceptions no longer silently kill the reader). The wire-corruption work needs:
- Reliable reproduction harness (more investigation than coding)
- Possibly cross-cutting changes to agent template defaults /
.mcp.jsonshape - Coordination with the agent-base-image build + rollout
That's a separate effort that shouldn't block #639.
Activity
fix(agent-runtime): surface stdout parse failures + orphan identity for
test(harness): document negative results for #640 — 6 hypotheses fals…
dolho commentedon May 7, 2026
Status update — synthetic-hypothesis investigation hit a wall
PR #693 ships a deterministic repro harness (
tests/harness/640/) and documents 6 falsified hypotheses from running it + statically inspecting@anthropic-ai/claude-codecli.js.Falsified
spawn(...)site.process.stdout.writesites in cli.js inspected. Real-code sites all gate on subcommands (mcp add,login, TUI) that don't fire under--print --output-format stream-json. Sites that look problematic (event.delta.textraw writes) are markdown documentation strings, not real code.stderr.on("data")handler appends to a 64MB in-memory ring (if($.length<67108864) $+=h.toString()); never re-emits.npm-wrappervariant, 0/50 null-cost. Claude tolerates pre-protocol garbage at handshake.setsid-childvariant. 53 grandchildren forked + escaped pgid (verified via sidecar log + broken-pipe events). Pollution lands on MCP-protocol pipe (claude-side), not on agent-server's claude-stdout pipe. Claude skips non-JSON frames silently./proc/self/fd/Nuserspace bypass — smoke test returnedENXIO(No such device or address). Linux refuses to re-open a pipe via/proc/self/fd/*. Kernel-level isolation; this hypothesis is impossible to exercise from userspace.Why the harness can't reach the bug
Every leak path the harness exercises lands on the MCP protocol pipe (Claude-side), not on the agent-server claude-stdout pipe where #640's wire corruption actually manifests. Claude+SDK isolate MCP child stdio correctly. No userspace synthetic variant from a child process can corrupt agent-server's read end of Claude's stdout.
What's still open (real remaining hypothesis space)
bash -c "node x.js") where shell init writes to a fd we haven't simulated. Need a real cmdline from production.Recommended path forward
parse_failures=Ncount + orphan cmdline).ability-websiteagent (Async chat_with_agent: long execution silently fails with null response (reader-thread) #678 victim). Reduces frequency of this failure mode.Cost recap
Issue stays open — root cause still unknown. This comment closes the synthetic-hypothesis-hunting angle.
test(harness): repro scaffolding + negative results for #640 — 6 hypo…
vybe commentedon Jun 1, 2026
Shipped in v0.6.0 (released via #998).
Release: v0.6.1 (#1171)