`-p --output-format stream-json` exits before flushing stdout, so a slow reader loses the end of the reply and the result line
Claude Code 2.1.296, macOS. Probably the same root cause as #77112, which was closed as stale without a fix.
In print mode with --output-format stream-json, the CLI exits about 2 seconds after its turn ends, with exit code 0, whether or not its stdout has been read. Any output it hasn't written into the pipe yet is lost. A reader that falls behind gets a prefix of the stream: no message_stop, no result line, and sometimes a last line cut off mid-JSON. The complete reply is in the session transcript (~/.claude/projects/…/<session_id>.jsonl), so the turn finished. Only the stdout delivery was dropped.
Reproduce: claude -p --safe-mode --tools "" --max-turns 1 --model haiku --output-format stream-json --verbose --include-partial-messages < long-prompt.txt, read stdout, and stop reading for N seconds after line 100:
- no pause: 373 KB,
resultline present, clean - 8 s pause: 350 KB,
resultline present, clean - 40 s pause: 142 KB, no
resultline, exit 0 - 60 s pause: 107 KB, no
resultline, exit 0, last line cut mid-JSON. The turn ended at about 28.9 s and the process exited at 31.1 s while the reader was still paused.
Expected: write all of stdout before exiting. That means blocking on the pipe, or flushing it, especially for the final result line, which consumers use to know the stream is complete.
Impact: any program embedding the CLI that briefly falls behind its stdout, for example under load, silently gets a truncated reply with a success exit code.