fix(cli): await the stdout flush so large --json output isn't truncated at exit

Merged
KI
3mo ago
+48-5

Summary

superset <cmd> --json silently truncates large output when stdout is a pipe whose reader is momentarily slow (command substitution, | jq under load). superset tasks list --json (~157KB) reliably comes back cut at a 16KB multiple (81920, 114688, ...), so $(...) capture and jq see unparseable JSON. Redirecting to a file never truncates.

Root cause: the runner prints the result with console.log and returns; nothing awaits the write. Bun queues pipe writes it can't complete immediately, and when the process exits with the reader still catching up, the queued tail is dropped.

Fix: execute() now writes the formatted output via process.stdout.write and awaits its completion callback, keeping the process alive until the whole payload has reached the pipe. One-line call-site change plus a small helper.

Verification

Repro: tasks list --json | (sleep 0.5; cat) | wc -c against a workspace with a full task list (~157KB expected).

run 1run 2run 3
dev CLI, before81920114688—
dev CLI, after157612157612157612
compiled darwin-arm64, after157612157612157612

out=$(superset tasks list --json) with the fixed binary parses cleanly (jq length = 50); the currently installed CLI truncates on the same machine.

No regression test is included: a hermetic fixture CLI (300KB payload through run(), slow shell reader, with and without a keep-alive fetch) does not reproduce the truncation — the trigger needs something about the full CLI's process state I couldn't isolate. Verified behaviorally as above instead; existing runner.test.ts still passes and tsc --noEmit is clean.

Context

Found while shipping #6762: the standup/10x skill scripts hit this and now capture CLI stdout via a temp file. With this fix that workaround is belt-and-suspenders rather than load-bearing.

https://claude.ai/code/session_01SudpHaYT4rQ1msxeVpWwC1


Summary by cubic

Prevents truncation of large --json output and error messages on slow pipes and ensures prompt exit on interrupt. Previously we returned after unawaited writes and dropped queued bytes; we now await stdout/stderr writes and abort a stuck drain on SIGINT/SIGTERM.

  • Replace console.log(output) with an awaited writeStream(process.stdout, ...).
  • Errors use the same helper and are awaited in run(), draining the message and hint in one stderr write and preserving a non-zero exit even if the write fails.
  • SIGINT/SIGTERM abort a drain-write stuck on a full pipe so Ctrl-C exits promptly.
  • Side effect: under backpressure the process may exit slightly later; interrupted runs may drop the tail by design. File redirects behave the same.
  • No migration required; consumers of --json and error output now receive complete payloads when runs complete.

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of command output when writing to slow or buffered streams.
    • Commands now respond promptly to cancellation, even when output streams do not drain.
    • Improved handling of standard output and error messages during shutdown.
    • Error output failures are handled gracefully without interrupting command termination.
    • Commands continue to preserve exit code 1 when error output cannot be written.