senpi icon indicating copy to clipboard operation
senpi copied to clipboard

host session --json output truncated at 64 KiB when stdout is a pipe (emit ignores short writeSync)

Open Momentum96 opened this issue 2 weeks ago • 0 comments

What happened?

senpi host session <verb> --json (also reached as omo host session ...) loses its answer when stdout is a pipe and the JSON line is larger than the pipe buffer. The output is cut at exactly 65536 bytes and the process still exits 0, so the caller gets invalid JSON with no error.

$ senpi host session read <ref> --tail 200 --json | wc -c
   65536
$ senpi host session read <ref> --tail 200 --json > /tmp/x.json; wc -c < /tmp/x.json
 1908621

Cause: emit() in packages/coding-agent/src/cli/host-emit.ts calls writeSync(1, line) once and ignores the return value. Once anything has touched process.stdout, the runtime puts a pipe stdout in non-blocking mode, so writeSync returns a short count (whatever fits in the pipe buffer) or throws EAGAIN. The rest of the line is dropped. A redirect to a file never hits this because file writes are not short.

Minimal check outside senpi (same result in Bun and Node):

// ws.mjs
import { writeSync } from "node:fs";
process.stdout.write("");
const n = writeSync(1, "x".repeat(300000) + "\n");
process.stderr.write(`returned ${n}\n`);
$ bun ws.mjs | (sleep 0.5; wc -c)
returned 65536
   65536

Steps to reproduce

  1. Have a host session with a large transcript (any session where read --tail 200 --json exceeds 64 KiB).
  2. senpi host session read <ref> --tail 200 --json | wc -c prints 65536.
  3. senpi host session read <ref> --tail 200 --json > out.json; wc -c < out.json prints the full size.

Expected behavior

The full JSON line reaches the pipe (piped byte count equals the file byte count), or the command fails with a non-zero exit. It should never exit 0 with a truncated line.

Version

senpi 2026.11.18-3 (via omo 5.1.79), Bun runtime, macOS arm64.

I'd like to fix this myself: make emit() loop until every byte is written, retrying on EAGAIN, while keeping it synchronous. PR with a regression test to follow.

Momentum96 avatar Nov 20 '26 04:11 Momentum96