Skip to content

fix(cli): preserve machine output through fatal terminal restores - #117487

Merged
steipete merged 1 commit into
mainfrom
codex/qa402-runtime-machine-output-ownership
Aug 1, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/qa402-runtime-machine-output-ownership

Conversation

@steipete

@steipete steipete commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Machine-readable CLI commands could append terminal-reset escape sequences to stdout during normal, deferred, uncaught-exception, unhandled-rejection, configuration-failure, or legacy exits. Configuration path and schema commands also failed to reserve their machine-output stream. Downstream operators therefore received invalid JSON precisely when commands succeeded, failed, or terminated fatally.

Why This Change Was Made

Move terminal-reset stream selection into the existing application runtime owner and route every normal and fatal CLI exit through that single boundary. Preserve explicitly selected reset streams, human terminal behavior, interactive onboarding, public plugin SDK contracts, dynamic startup imports, existing exit codes, and config-set JSON parsing behavior.

User Impact

Automation receives clean JSON or raw machine output even on fatal exit 1 and invalid-configuration exit 78. Terminal cleanup still occurs on stderr for machine output and on stdout for human sessions; config get, file, and schema consistently reserve their output stream.

Evidence

  • Authentic baseline: 12 failing regressions, including real dual-TTY subprocess exits 1 and 78.
  • Fixed tree: 140 focused passing tests across runtime restoration, fatal and benign rejection handling, uncaught exceptions, deferred/macOS exits, machine-output detection, JSON compatibility, cold startup, and existing partial-runtime mocks.
  • Configured type-aware lint, exact-file formatting, and whitespace checks passed.
  • Exact frozen commit 59c106c and tree 569216cabcce765625e6fa73e390083682af4e5d were verified on a dedicated Blacksmith Testbox before running the complete package build.
  • Full pnpm build passed in 2 minutes 58.3 seconds, including all dynamic-import bundles, CLI bootstrap validation, UI build, and all 148 public plugin SDK entrypoints.
  • Separate plugin-SDK surface gate passed: 321 entrypoints, 7,589 exports, and zero forbidden public subpaths.
  • At least nine independent exact-commit hostile reviews reported no remaining P0–P3 findings; the earlier missing-build hold was explicitly resolved after successful remote verification.
  • Verification: https://github.com/openclaw/openclaw/actions/runs/30705742496

@openclaw-barnacle openclaw-barnacle Bot added cli CLI command changes size: M maintainer Maintainer-authored PR labels Aug 1, 2026
@clawsweeper clawsweeper Bot added 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. P1 High-priority user-facing bug, regression, or broken workflow. labels Aug 1, 2026
@clawsweeper

clawsweeper Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 1, 2026, 12:03 PM ET / 16:03 UTC.

ClawSweeper review

What this changes

Routes CLI terminal restoration through the shared runtime so JSON and other machine-readable stdout stays free of ANSI reset bytes during normal, deferred, and fatal exits; it also classifies config file and config schema as machine-output commands.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

Keep this PR open for its protected maintainer review path. The patch appears to correctly move terminal-reset stream selection to the shared runtime owner, covering normal exits and fatal handlers while preserving an explicitly supplied reset stream. Likely related people: steipete (high confidence).

Priority: P1
Reviewed head: 59c106c05a0d3ebd629d2bd80ee860039d18a8fe

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-boundary fix with strong targeted terminal-process coverage; remaining merge readiness depends on the still-running exact-head checks.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (terminal): The PR documents exact-head remote verification and includes direct dual-TTY subprocess coverage that parses post-fix stdout as JSON while observing terminal restoration on stderr.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The PR documents exact-head remote verification and includes direct dual-TTY subprocess coverage that parses post-fix stdout as JSON while observing terminal restoration on stderr.
Evidence reviewed 5 items Current-main failure boundary: Current main routes defaultRuntime.exit directly to restoreTerminalState, whose default reset stream is stdout; the same direct restore is used by the legacy entrypoint and fatal rejection handler, so a TTY reset can contaminate structured stdout.
Canonical runtime-owner fix: The proposed restoreRuntimeTerminalState respects an explicit reset stream, otherwise uses stderr while the existing machine-output console-routing flag is active; normal runtime exits, legacy entrypoint failures, CLI uncaught exceptions, and unhandled rejections all use that one owner boundary.
Machine-output lifecycle and config coverage: runCli retains the existing console-to-stderr state until process exit for the legacy entrypoint, and config file writes a path while config schema writes JSON to stdout. The branch adds both commands to the existing built-in machine-output resolver.
Findings None None.
Security None None.

How this fits together

OpenClaw CLI commands establish whether stdout is human-facing or machine-consumed before command execution. Normal exits, deferred finalizers, and process-level fatal handlers restore terminal state afterward, so this boundary determines whether reset bytes go to stdout or stderr.

flowchart LR
  A[CLI invocation] --> B[Machine-output detection]
  B --> C[Console routing state]
  C --> D[Command output]
  D --> E[Normal or fatal exit]
  E --> F[Runtime terminal restoration]
  F --> G[Clean stdout and terminal reset stream]
Loading

Before merge

  • Resolve merge risk (P1) - GitHub currently reports the branch as behind main and several exact-head checks are still in progress; refresh mergeability and required checks against the final head before merging.
  • Complete next step (P2) - The protected maintainer label and incomplete exact-head check set require a human merge review; no discrete code defect needs an automated repair lane.
Agent review details

Security

None.

PR surface

Source +15, Tests +228. Total +243 across 10 files.

View PR surface stats
Area Files Added Removed Net
Source 5 27 12 +15
Tests 5 283 55 +228
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 10 310 67 +243

Review metrics

Metric Value Why it matters
Built-in machine-output coverage 2 commands added config file and config schema newly reserve stdout, extending fatal-exit protection to their path and schema output.

Stored data model

Persistent data-model change detected: serialized state: src/cli/one-shot-exit.test.ts. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Retain the single runtime-owned restoration wrapper, land it once the final exact-head checks are green, and keep structured-output detection responsible for setting the existing console-routing state rather than adding per-exit stream decisions.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Retain the single runtime-owned restoration wrapper, land it once the final exact-head checks are green, and keep structured-output detection responsible for setting the existing console-routing state rather than adding per-exit stream decisions.

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

Yes — current source shows terminal restoration defaults to stdout, and the PR supplies focused dual-TTY subprocess cases that exercise JSON output followed by normal, deferred, and fatal exits.

Is this the best way to solve the issue?

Yes — centralizing stream selection in the existing runtime owner is narrower and safer than teaching every fatal handler or command to choose a reset stream independently.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 30972edaea78.

Labels

Label changes:

  • add P1: A fatal CLI path can corrupt machine-readable stdout used by automation, while the patch has a focused repair and direct regression coverage.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR documents exact-head remote verification and includes direct dual-TTY subprocess coverage that parses post-fix stdout as JSON while observing terminal restoration on stderr.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR documents exact-head remote verification and includes direct dual-TTY subprocess coverage that parses post-fix stdout as JSON while observing terminal restoration on stderr.

Label justifications:

  • P1: A fatal CLI path can corrupt machine-readable stdout used by automation, while the patch has a focused repair and direct regression coverage.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR documents exact-head remote verification and includes direct dual-TTY subprocess coverage that parses post-fix stdout as JSON while observing terminal restoration on stderr.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR documents exact-head remote verification and includes direct dual-TTY subprocess coverage that parses post-fix stdout as JSON while observing terminal restoration on stderr.

Evidence

What I checked:

  • Current-main failure boundary: Current main routes defaultRuntime.exit directly to restoreTerminalState, whose default reset stream is stdout; the same direct restore is used by the legacy entrypoint and fatal rejection handler, so a TTY reset can contaminate structured stdout. (src/runtime.ts:102, 30972edaea78)
  • Canonical runtime-owner fix: The proposed restoreRuntimeTerminalState respects an explicit reset stream, otherwise uses stderr while the existing machine-output console-routing flag is active; normal runtime exits, legacy entrypoint failures, CLI uncaught exceptions, and unhandled rejections all use that one owner boundary. (src/runtime.ts:100, 59c106c05a0d)
  • Machine-output lifecycle and config coverage: runCli retains the existing console-to-stderr state until process exit for the legacy entrypoint, and config file writes a path while config schema writes JSON to stdout. The branch adds both commands to the existing built-in machine-output resolver. (src/cli/run-main.ts:1039, 59c106c05a0d)
  • Direct terminal behavior coverage: The PR adds subprocess tests that set both output streams as TTYs, emit JSON, trigger deferred and fatal exits, parse stdout as JSON, and assert the terminal reset reaches stderr. It also preserves explicit reset-stream selection and human stdout restoration. (src/cli/one-shot-exit.test.ts:300, 59c106c05a0d)
  • Feature provenance: The current runtime and config-output owner paths are attributed to Peter Steinberger in commit 597699401a5fda97296118286c081357fa2055a5; the proposed focused repair is commit 59c106c05a0d3ebd629d2bd80ee860039d18a8fe at 2026-08-01T08:12:48-07:00. (src/runtime.ts:1, 597699401a5f)

Likely related people:

  • steipete: Peter Steinberger authored the current-main runtime and config-output paths identified by blame, and the proposed repair follows that owner boundary. (role: current runtime-path author and recent area contributor; confidence: high; commits: 597699401a5f, 59c106c05a0d; files: src/runtime.ts, src/cli/config-output-mode.ts, src/index.ts)

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.

@steipete
steipete merged commit e9b959a into main Aug 1, 2026
131 of 141 checks passed
@steipete
steipete deleted the codex/qa402-runtime-machine-output-ownership branch August 1, 2026 17:03
@steipete

steipete commented Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

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

Labels

cli CLI command changes maintainer Maintainer-authored PR P1 High-priority user-facing bug, regression, or broken workflow. 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.

1 participant