Stream output to stdout: support -o - / /dev/stdout (#223 part 2)#239
Conversation
`-o -` (and `-o /dev/stdout`) now streams the rendered document to stdout so it can be piped, e.g. `claude-code-log session.jsonl -f markdown -o - | pbcopy`. Previously `/dev/stdout` hung (the version sniff, fixed in part 1) and progress text polluted stdout. Implementation reuses the full conversion pipeline via a throwaway temp file (_render_to_stdout): render with use_cache=False (so cache_manager is None → no pagination, a single document), force_regenerate, no individual session files, embedded images (the temp dir is discarded), then copy the file's bytes to sys.stdout. Supported for the main convert path and --session-id; `--all-projects` with `-o -` is a clear UsageError (it's a multi-file export). Status hygiene is behavior-preserving: only in stream mode does the converter run silent and the CLI route its confirmation to stderr, so stdout carries only the document. Every other invocation keeps status on stdout exactly as today (no unconditional stderr sweep, so no surprises for scripts — including PowerShell — parsing normal-run output). `--output` help documents `-`. Tests in test/test_output_stdout.py. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds CLI support for streaming rendered output to stdout for ChangesCLI stdout streaming
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@claude_code_log/cli.py`:
- Around line 1234-1255: The stdout conversion path in cli.py’s
_render_to_stdout / convert_jsonl_to call is silently overriding an explicit
--combined no by forcing write_combined=True. Add a pre-check in the
stdout-target branch to detect when combined output was explicitly disabled and
reject it with a clear error instead of proceeding. Use the existing stdout
handling logic and the write_combined / generate_individual_sessions options to
locate the incompatible path.
- Around line 922-928: The stdout guard in cli.py is too broad because
will_run_all_projects becomes true for global --session-id usage, so it blocks
valid single-session exports before the session-resolution flow can run. Update
the check around _is_stdout_target(output) to reject only real multi-file
--all-projects exports, and allow the single-session path used by the later
session lookup logic so claude-code-log --session-id <id> -o - continues to
work.
- Around line 70-76: The _render_to_stdout path is treating the rendered file as
text instead of bytes, which can re-encode or corrupt output on non-UTF-8
stdout. Update the _render_to_stdout logic in cli.py to read the temp file as
raw bytes from the Path returned by written, then write those bytes directly to
stdout via sys.stdout.buffer while leaving the captured progress handling
unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 9bd51d24-f803-4768-89f1-d3816055e8f7
📒 Files selected for processing (3)
claude_code_log/cli.pytest/test_output_stdout.pywork/tui-output-fixes.md
…ined guard Three functional-correctness findings on the stdout-streaming PR: - Stream the document as raw bytes. _render_to_stdout read the temp file as text and wrote via sys.stdout.write, which re-encodes through stdout's locale encoding — mangling/raising on non-ASCII (transcripts are emoji-heavy) under a non-UTF-8 stdout. Now read_bytes() + sys.stdout.buffer.write() passes the UTF-8 bytes through verbatim (with a text-shim fallback). - Don't reject global `--session-id <id> -o -`. The --all-projects+stream guard fired whenever input_path is None, blocking the global session-from-cache export before it could resolve. Exempt --session-id (a single-session export) from that guard. - Reject `--combined no` with `-o -` instead of silently overriding. The stream forces a single combined document, so `--combined no` (per-session only) is incompatible — fail fast rather than do the opposite. Tests: unicode round-trip, global-session-id-not-guard-rejected, and combined-no-incompatible added to test_output_stdout.py. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Completes #223 (part 1 — the
/dev/stdoutversion-sniff hang — landed in #237).What
-o -(and-o /dev/stdout) streams the rendered document to stdout so it can be piped:Treated as: stream to stdout, always regenerate, no cache, no browser, and status/progress on stderr so stdout carries only the document. Supported for the main convert path and
--session-id;--all-projectswith-o -is a clearUsageError(it's a multi-file export).How
Implemented in
cli.pyby reusing the full conversion pipeline via a throwaway temp file (_render_to_stdout): render withuse_cache=False(so no pagination → a single document),force_regenerate, no individual session files, embedded images (the temp dir is discarded), then copy the file's bytes tosys.stdout. This keeps the streamed document byte-identical to the equivalent-o fileoutput.Status hygiene (only when streaming): the render runs inside
contextlib.redirect_stdout, and any captured progress is forwarded to stderr — so an in-renderprintfrom any callee (e.g. the per-fileProcessing …) can't pollute the document stream. The pre-renderConverting project path …echo is routed to stderr too when streaming. Every non-stream invocation keeps status on stdout exactly as today (no unconditional sweep — no behavior change for normal runs, including on PowerShell).Cross-platform
-works everywhere./dev/stdoutis POSIX-only (on Windows the device doesn't exist and the path normalizes with backslashes, so detection won't match it there) — its test isskipif(win32);-is the portable form. No FIFO/mkfifoin these tests.Tests
test/test_output_stdout.py(7): markdown/HTML/JSON document on stdout with stdout clean of status noise + confirmation on stderr;/dev/stdoutno-hang (POSIX);-has no suffix so format inference doesn't fire;--all-projects+-o -errors;--session-idstream is clean (asserts noProcessing/Loadingon stdout).just cigreen; no snapshot churn.Closes #223
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
-o -and/dev/stdoutnow output only the rendered document bytes, with render-related messages sent to stderr.-.Bug Fixes
--combined nowith streaming./dev/stdouthang issue on supported platforms.Tests