Skip to content

fix(policy): emit JSON for command-owned CLI failures - #181758

Merged
obviyus merged 4 commits into
openclaw:mainfrom
Leon-SK668:leon/fix-policy-json-20261023
Oct 23, 2026
Merged

obviyus merged 4 commits into
openclaw:mainfrom
Leon-SK668:leon/fix-policy-json-20261023

Conversation

@Leon-SK668

@Leon-SK668 Leon-SK668 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Command-owned failures in policy check, policy compare, and policy watch return 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 a8aee07e387023fc4fb11335fdcd0d26292ee09a ran twice; candidate e1b761e2ffeec60e6cddc2c9a23ec02172f344c0 ran 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:

Command-owned failure Main run 1 Main run 2 Candidate Exit, all arms
policy check --severity-min invalid 0 bytes 0 bytes 123 bytes 2
policy watch --interval-ms invalid --once 0 bytes 0 bytes 95 bytes 2
policy compare --baseline '' 0 bytes 0 bytes 89 bytes 2

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:

7b226f6b223a66616c73652c226572726f72223a7b2274797065223a22636c695f6572726f72222c226d657373616765223a22496e76616c6964202d2d73657665726974792d6d696e2076616c75652e204578706563746564206f6e65206f663a20696e666f2c207761726e696e672c206572726f722e227d7d0a

All candidate machine failures parse as exactly one object with top-level keys ok and error; ok=false, and the nested keys are type and message, with type=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 --json and policy watch --json --once controls 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.

@clawsweeper

clawsweeper Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation extensions: policy size: S labels Oct 23, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 23, 2026
@clawsweeper

clawsweeper Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Policy 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.

  • Before: The command exits 2 with a stderr diagnostic and empty stdout.
  • After: The command retains that diagnostic and exit status, and stdout contains {"ok":false,"error":{"type":"cli_error","message":"Invalid --severity-min value. Expected one of: info, warning, error."}}.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) A focused contract repair with shipped CLI proof, meaningful boundary regressions, and no actionable findings.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The supplied Linux Node 24.22.1 built-CLI captures bind the candidate head to all three command-owned validation failures in piped and real PTY modes, showing parseable JSON, unchanged diagnostics and exit 2, plus ordinary-report controls; registered-command coverage additionally checks a watch report followed by a terminal error.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Scripts requesting Policy JSON output receive empty stdout when command-owned validation fails.
Reason: The change restores the documented CLI failure envelope without changing evaluation, enforcement, configuration, or human-terminal behavior.

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
Reviewed head: e1b761e2ffeec60e6cddc2c9a23ec02172f344c0

Before merge

None.

Findings

None.

Agent review details

How this fits together

The 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
Loading

Technical review

Best 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

  • Policy command error boundary changes intended behavior with a stated reason (007a8c5: Consolidate private command runtime plumbing into one error boundary while preserving existing output and exit statuses, as recorded in refactor(policy): remove unused CLI runtime plumbing #135317.)
  • Registered CLI test ownership keeps the original intent (5d12f6b: Remove test-only handler exports and exercise the public registerPolicyCli Commander path; the patch retains that boundary and separates captured output streams.)
  • Watch timer regression keeps the original intent (7de09d5: Prevent oversized intervals from overflowing Node timers; the existing injected stop also now verifies that a terminal JSON failure follows the earlier report.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 007a8c5 adds extensions/policy/src/cli.ts:136 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 007a8c5bfb4e; files: extensions/policy/src/cli.ts)
  • Gio Della-Libera: Raw commit cbf72e5 adds extensions/policy/src/cli.ts:21 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: cbf72e5e26ee; files: extensions/policy/src/cli.ts)

PR surface

Source +8, Tests +62, Docs +6. Total +76 across 4 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 13 5 +8
Tests 2 88 26 +62
Docs 1 6 0 +6
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 107 31 +76

Review metrics

Metric Value Why it matters
Production versus regression coverage Production +13/-5; tests +88/-26 The small production change retains one error owner; test growth covers command integration, output selection, separate streams, and watch framing without a new production seam.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: Missing structured failures disrupt scripts consuming Policy CLI output, with limited scope and preserved nonzero exit status.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/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.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (1 earlier review cycle)
  • reviewed 2026-10-23T16:45:49.144Z sha 97fe93e :: needs maintainer review before merge. :: none

Reviewed October 23, 2026, 4:54 PM ET / 20:54 UTC (Revision 2).

@obviyus

obviyus commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 23, 2026
@obviyus
obviyus merged commit a339211 into openclaw:main Oct 23, 2026
98 checks passed
@obviyus

obviyus commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

Landed in a339211. Thanks @Leon-SK668. policy check/compare/watch now return JSON errors on stdout.

@obviyus

obviyus commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

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.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 24, 2026
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]>
RomneyDa added a commit that referenced this pull request Oct 28, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation extensions: policy P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants