Skip to content

fix(cli): devices --json refusals print an empty stdout - #159489

Closed
masatohoshino wants to merge 1 commit into
openclaw:mainfrom
masatohoshino:fix/devices-json-failures
Closed

masatohoshino wants to merge 1 commit into
openclaw:mainfrom
masatohoshino:fix/devices-json-failures

Conversation

@masatohoshino

@masatohoshino masatohoshino commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Closes #159488

What Problem This Solves

Fixes: openclaw devices commands run with --json exit 1 with an empty stdout when they refuse (for example devices clear without --yes, devices approve with no pending or no matching request, a blank device or request id), instead of the JSON failure envelope documented in docs/cli/index.md ("JSON failures").

User Impact

User impact: a script that runs openclaw devices ... --json now gets {"ok": false, "error": {"type": "cli_error", "message": "..."}} on stdout when a devices command refuses, like other JSON-mode commands. Exit codes, successful JSON output, and output without --json are unchanged.

Why This Change Was Made

Each refusal in src/cli/devices-cli.runtime.ts called defaultRuntime.error(...) and defaultRuntime.exit(1) without looking at --json, so the error never reached the root CLI failure handler that writes the envelope. #159212 fixed the same shape in openclaw agent --json.

The refusals now go through one module-private helper, failDevicesCommand: with --json it throws the same message (multi-line guidance such as the node reapproval notice joined into it) so the root handler writes the envelope; without --json it keeps the current stderr lines and exit(1). Refusals covered: clear without --yes, approve with no pending request, a scope upgrade the device cannot approve itself, and no matching request (with node reapproval notices), plus blank ids for remove, reject, rename, rotate/revoke. The approve preview, which already writes JSON, and Gateway transport errors, which already have their own JSON output, are unchanged.

With --json, stderr now carries the root handler's standard failure lines instead of the bare message, as for other JSON-mode failures. Two existing tests asserted the old stderr-only output for JSON-mode approval errors; they move, with the new cases, into src/cli/devices-cli.json-failures.test.ts, which keeps devices-cli.test.ts under its line cap and asserts the same guidance and secret redaction on the envelope message.

Evidence

Tested at 5372c248432dceebc1583d03adc504645a735328 (base upstream/main 804376566a8189f80e8a0d0f3a3172c7f97de876).

Both sides were built from source on Linux (Node 24.18.0) by a before/after harness that runs each side in its own isolated home (OPENCLAW_STATE_DIR / OPENCLAW_CONFIG_PATH inside it, stdin /dev/null); every command runs through pnpm --silent openclaw. No Gateway is needed for these refusals. Nothing is mocked.

Before (base):

$ openclaw devices clear --json
(stdout empty)                      exit 1
stderr: Refusing to clear pairing table without --yes

$ openclaw devices remove "" --json
(stdout empty)                      exit 1
stderr: deviceId is required. Run openclaw devices list to choose a paired device.

After (this PR):

$ openclaw devices clear --json
{
  "ok": false,
  "error": {
    "type": "cli_error",
    "message": "Refusing to clear pairing table without --yes"
  }
}                                   exit 1

$ openclaw devices remove "" --json
{
  "ok": false,
  "error": {
    "type": "cli_error",
    "message": "deviceId is required. Run openclaw devices list to choose a paired device."
  }
}                                   exit 1

Control, identical on both sides: openclaw devices clear (no --json) exits 1 with the refusal on stderr.

Focused tests and measured cost

src/cli/devices-cli.json-failures.test.ts (4 cases: clear without --yes, approve with no pending request, no matching request with node reapproval guidance and redacted connection secrets, operator-label-only query) fails 4 of 4 on the base runtime and passes on this branch. src/cli/devices-cli.test.ts passes 63 of 63.

pnpm test src/cli/devices-cli.json-failures.test.ts --maxWorkers=1: 2.0 s wall; the cases take 193 ms (first, includes module load), then 4 ms each. In CI (run https://github.com/openclaw/openclaw/actions/runs/36301082504, job checks-node-changed-1) its 4 tests take 0.41 s and src/cli/devices-cli.test.ts (63 tests) 0.23 s in total.

pnpm check:changed passes (including the line-cap ratchet: devices-cli.runtime.ts does not grow and devices-cli.test.ts shrinks). The repository's autoreview skill (branch mode, up to P2) reported no actionable findings.

CI seconds (per-test durations reported by Vitest, summed per file from the CI job log on head 5372c248432; module import and worker startup are not included):

@clawsweeper

clawsweeper Bot commented Sep 27, 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 cli CLI command changes size: M labels Sep 27, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 27, 2026
@clawsweeper

clawsweeper Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 27, 2026, 11:54 AM ET / 15:54 UTC (Revision 3).

ClawSweeper review

What this changes

Device commands now route local refusals through OpenClaw’s JSON failure output when --json is set, with regression tests for refusal messages and redacted guidance.

Merge readiness

✅ Ready for maintainer review

The linked device-command bug remains on current main. This PR has a focused repair, direct before-and-after CLI proof, and no identified blocking patch defect.

Priority: P2
Reviewed head: 5372c248432dceebc1583d03adc504645a735328

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) Direct before-and-after CLI evidence supports a narrow repair, and source review found no blocking defect.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): The changed device CLI refusal path was exercised through real openclaw devices clear --json and blank-ID removal commands on isolated Linux source builds. Recorded after-fix stdout contains the JSON failure envelope with exit 1, while a text-mode control is unchanged; no stored-data contract changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The changed device CLI refusal path was exercised through real openclaw devices clear --json and blank-ID removal commands on isolated Linux source builds. Recorded after-fix stdout contains the JSON failure envelope with exit 1, while a text-mode control is unchanged; no stored-data contract changes.
Evidence reviewed 8 items Introduced repair: The pinned PR delta adds a helper that throws local refusal messages in JSON mode and retains the existing text-mode error and exit path.
Current main still has the refusal: Current main still writes the clear refusal directly to stderr and exits, without the PR helper.
Documented CLI contract: The CLI reference promises one JSON error document on stdout when a JSON-output command fails; device commands expose --json for scripting.
Findings None None.
Security None None.

How this fits together

The openclaw devices CLI accepts pairing and token commands, sends eligible operations to the Gateway, and reports results to the operator or a script. The root CLI failure handler writes the JSON error document when a command raises an error in JSON mode.

flowchart LR
  A[Device command and options] --> B[Device CLI]
  B --> C{Local refusal?}
  C -->|Yes, JSON mode| D[Root failure handler]
  D --> E[JSON error on stdout]
  C -->|Yes, text mode| F[Error on stderr]
  C -->|No| G[Gateway operation]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source -2, Tests +100. Total +98 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 26 28 -2
Tests 2 141 41 +100
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 167 69 +98

Review metrics

Metric Value Why it matters
Focused test cost 4 cases; 0.41 s of reported CI test time The PR reports measured cost for the added regression suite, as required for changed tests.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #159488
Summary: The linked issue reports the same device-command refusals that this PR repairs; the earlier agent-command fix concerns a separate CLI path.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Technical review

Best possible solution:

Use the shared JSON failure envelope for device-command refusals while retaining text-mode behavior and sanitized approval guidance.

Do we have a high-confidence way to reproduce the issue?

Yes. Current main still sends these refusals directly to stderr, and the PR provides before-and-after runs of the affected CLI commands; this read-only review did not execute contributor code.

Is this the best way to solve the issue?

Yes. The patch uses the existing root JSON failure owner and preserves the device CLI’s text-mode refusal behavior.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against b8894086496e.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: The broken JSON refusal affects scripts using device commands but has a limited blast radius and retains a nonzero exit code.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The changed device CLI refusal path was exercised through real openclaw devices clear --json and blank-ID removal commands on isolated Linux source builds. Recorded after-fix stdout contains the JSON failure envelope with exit 1, while a text-mode control is unchanged; no stored-data contract changes.
  • proof: sufficient: Contributor real behavior proof is sufficient. The changed device CLI refusal path was exercised through real openclaw devices clear --json and blank-ID removal commands on isolated Linux source builds. Recorded after-fix stdout contains the JSON failure envelope with exit 1, while a text-mode control is unchanged; no stored-data contract changes.

Evidence

What I checked:

  • Introduced repair: The pinned PR delta adds a helper that throws local refusal messages in JSON mode and retains the existing text-mode error and exit path. (src/cli/devices-cli.runtime.ts:719, 5372c248432d)
  • Current main still has the refusal: Current main still writes the clear refusal directly to stderr and exits, without the PR helper. (src/cli/devices-cli.runtime.ts:879, b8894086496e)
  • Documented CLI contract: The CLI reference promises one JSON error document on stdout when a JSON-output command fails; device commands expose --json for scripting. Public docs: docs/cli/index.md. (docs/cli/index.md:79, 5372c248432d)
  • Production error boundary: The root handler formats a caught command error as a JSON failure and sets exit code 1 when JSON mode is active. (src/entry.ts:352, 5372c248432d)
  • Real CLI proof: The PR body records isolated Linux source builds run through pnpm --silent openclaw: clear and blank-ID refusals had empty stdout before the patch and JSON error documents with exit 1 afterward. The text-mode clear refusal was an unchanged control. (5372c248432d)
  • Regression coverage: The new tests cover clear, approval refusal, node guidance and secret redaction, and an operator-label-only query; they supplement the direct CLI proof. (src/cli/devices-cli.json-failures.test.ts:64, 5372c248432d)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Vincent Koc: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (2 earlier review cycles)
  • reviewed 2026-09-27T06:50:56.382Z sha 5372c24 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-27T09:26:17.765Z sha 5372c24 :: needs maintainer review before merge. :: none

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M 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.

devices commands with --json print nothing on stdout when they refuse

1 participant