Repository navigation
fix(policy): emit JSON for command-owned CLI failures - #181758
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. What this changesPolicy command failures emit a compact JSON error on machine-readable stdout while retaining stderr diagnostics and exit code 2. Example: Run openclaw policy check --json --severity-min invalid.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review This PR repairs the documented JSON failure contract through the existing Policy error owner, with no actionable findings; exact-head CI remains pending. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherThe bundled Policy plugin registers check, compare, and watch commands; parsed options enter their shared error boundary, which emits reports or failures to CLI streams and records the exit status. flowchart TD
A[Policy CLI arguments] --> B[Commander parsing]
B --> C[Shared command boundary]
C --> D[Policy evaluation or comparison]
D --> E[Report output]
C --> F[Machine JSON and stderr diagnostic]
E --> G[Process exit status]
F --> G
Technical reviewBest possible solution: Keep the existing shared Policy error boundary and emit the documented compact failure envelope using the runtime JSON writer. Do we have a high-confidence way to reproduce the issue? Current-main source directly shows the stderr-only failure path, and the supplied built-CLI before/after captures exercise invalid severity, interval, and baseline inputs; no target code was run during this read-only review. Is this the best way to solve the issue? The patch is the smallest complete repair: pass output options to the existing shared catch, emit compact JSON through the existing writer, and preserve stderr and exit 2. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against bf93c438e2fc. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +8, Tests +62, Docs +6. Total +76 across 4 files. Added test files: 0. View PR surface stats
Review metrics
LabelsLabel changes: No label changes. Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReview history (1 earlier review cycle)
Reviewed October 23, 2026, 4:54 PM ET / 20:54 UTC (Revision 2). |
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Landed in a339211. Thanks @Leon-SK668. policy check/compare/watch now return JSON errors on stdout. |
|
Merged. Thanks @Leon-SK668 for the focused shared-handler repair and stream/framing regressions. Built-CLI captures confirm machine-readable errors for all three commands while preserving stderr, exit status and human terminal behavior. |
fix(policy): print JSON errors on stdout for policy check, compare and watch failures `openclaw policy check`, `policy compare` and `policy watch` printed nothing on stdout when the command itself failed, even with --json or with output piped to another program, which broke scripts that read the documented machine-readable error. Each failure now prints one standard cli_error JSON document on stdout; piped output matches --json, and stderr and terminal output are unchanged. Evidence: built CLI. On main the three failures printed 0 bytes on stdout (twice); with this change they print one cli_error document each (123, 95 and 89 bytes) (twice), with the same result when piped. Real terminal runs and all stderr output are unchanged. Thanks @Leon-SK668. Co-authored-by: Ayaan Zaidi <[email protected]>
fix(policy): print JSON errors on stdout for policy check, compare and watch failures `openclaw policy check`, `policy compare` and `policy watch` printed nothing on stdout when the command itself failed, even with --json or with output piped to another program, which broke scripts that read the documented machine-readable error. Each failure now prints one standard cli_error JSON document on stdout; piped output matches --json, and stderr and terminal output are unchanged. Evidence: built CLI. On main the three failures printed 0 bytes on stdout (twice); with this change they print one cli_error document each (123, 95 and 89 bytes) (twice), with the same result when piped. Real terminal runs and all stderr output are unchanged. Thanks @Leon-SK668. Co-authored-by: Ayaan Zaidi <[email protected]> Co-authored-by: RomneyDa <[email protected]>
What Problem This Solves
Command-owned failures in
policy check,policy compare, andpolicy watchreturn exit 2 and a stderr diagnostic but leave stdout empty, even when JSON was explicitly requested or stdout is piped. Scripts cannot consume the ordinary machine-readable failure envelope.What Changed
The existing shared failure handler now receives the command's output options and uses the existing runtime JSON writer for explicit JSON or non-terminal stdout. It preserves stderr and exit 2. Human terminal failures remain text diagnostics, and a watch failure is one compact NDJSON record rather than a multiline fragment.
Contributor commits and credit are preserved. No policy evaluation, enforcement, configuration, dependency, persisted-format or SDK change.
Evidence
Built Linux CLI, Node 24.22.1, fresh isolated state authored through shipped config commands. Main
a8aee07e387023fc4fb11335fdcd0d26292ee09aran twice; candidatee1b761e2ffeec60e6cddc2c9a23ec02172f344c0ran the same scenarios. Each arm captured stdout and stderr separately for all three commands in four modes: piped--json, piped without--json, real PTY human output, and real PTY with--json. No process.stdout.isTTY mock was used. The existing OPENCLAW_HIDE_BANNER setting consistently excludes the unrelated startup banner; NO_COLOR is set.Actual captured stdout sizes for both piped modes:
policy check --severity-min invalidpolicy watch --interval-ms invalid --oncepolicy compare --baseline ''Main's complete stdout capture is the empty byte string (hex: empty). Candidate's complete severity-error stdout, followed by exactly one LF byte (0a), is:
{"ok":false,"error":{"type":"cli_error","message":"Invalid --severity-min value. Expected one of: info, warning, error."}}Its captured raw hexadecimal bytes are:
All candidate machine failures parse as exactly one object with top-level keys
okanderror;ok=false, and the nested keys aretypeandmessage, withtype=cli_error. Piped automatic output is byte-identical to explicit JSON. PTY JSON has the same object and terminal CRLF framing (124/96/90 bytes). Human PTY failures produce zero stdout bytes on both revisions. All twelve stderr captures are byte-identical between both main runs and the candidate.Valid
policy check --jsonandpolicy watch --json --oncecontrols retain their ordinary report shapes rather than a cli_error. With no authored policy file, they report the expected policy-jsonc-missing warning and exit 1 on both arms. No policy file or enforcement rule was handwritten for this proof.Full main/candidate builds, 35 focused tests across the two changed test files, and focused formatting/lint passed. The running-watch regression also preserves the earlier report before its terminal JSON error. A real OpenAI gpt-5-mini turn through the built candidate CLI returned LIVE_GATE_OK, exit 0.
Simplification
Removed the test helper's combined stdout/stderr compatibility result and migrated its consumers to the actual stream, including separate checks that neither stream exposes route identifiers. The existing shared production owner remains the only error path. Production +13/-5; tests +88/-26; docs +6. Remaining growth carries output mode into that owner and independently asserts framing, stream choice and unchanged diagnostics.
Merge Risk
Two-way door: a revert restores the previous output without rewriting state. Blast radius is machine-readable command-owned failures in the three Policy CLI commands. Commander parser-owned failures and policy evaluation/enforcement are unchanged.