fix(#168c): emit error envelopes to stdout under --output-format json

Under --output-format json, error envelopes were emitted to stderr via
eprintln!. This violated the emission contract: stdout should carry the
contractual envelope (success OR error); stderr is reserved for
non-contractual diagnostics.

Cycle #87 controlled matrix audit found bootstrap/dump-manifests/state
exhibited this pattern (exit 1, stdout 0 bytes, stderr N bytes under
--output-format json).

Fix: change eprintln! to println! for the JSON error envelope path in main().
Text mode continues to route errors to stderr (conventional).

Verification:
- bootstrap --output-format json: stdout now carries envelope, exit 1
- dump-manifests --output-format json: stdout now carries envelope, exit 1
- Text mode: errors still on stderr with [error-kind: ...] prefix (no regression)

Tests:
- Updated assert_json_error_envelope helper to read from stdout (was stderr)
- Added error_envelope_emitted_to_stdout_under_output_format_json_168c
  regression test that asserts envelope on stdout + non-JSON on stderr
- All 16 output_format_contract tests pass

Phase 0 Task 1 complete: emission routing fixed across all error-path verbs.
Phase 0 Task 2 (no-silent CI guarantee) remains.

Refs: #168c (cycle #87 filing), cycle #88 emission contract framing
This commit is contained in:
YeonGyu-Kim
2026-04-23 06:03:24 +09:00
parent 8307715962
commit 960290a2f3
2 changed files with 74 additions and 5 deletions

View File

@@ -223,7 +223,12 @@ fn main() {
if hint.is_none() && kind == "cli_parse" && !short_reason.contains("`claw --help`") {
hint = Some("Run `claw --help` for usage.".to_string());
}
eprintln!(
// #168c: Under --output-format json, emit the error envelope to
// stdout so JSON consumers can parse it without reading stderr.
// Text mode continues to route errors to stderr (conventional).
// Emission contract: when --output-format json, stdout carries the
// envelope (success OR error); stderr is for non-contractual diagnostics only.
println!(
"{}",
serde_json::json!({
"type": "error",