Skip to content

docs(hook): document all four rtk rewrite exit codes in --help - #4204

Merged
KuSh merged 3 commits into
rtk-ai:developfrom
Protocol-zero-0:docs/rewrite-help-exit-codes
Oct 9, 2026
Merged

KuSh merged 3 commits into
rtk-ai:developfrom
Protocol-zero-0:docs/rewrite-help-exit-codes

Conversation

@Protocol-zero-0

Copy link
Copy Markdown
Contributor

What

rtk rewrite --help still describes the original two-code protocol:

Exits 0 and prints the rewritten command if supported.
Exits 1 with no output if the command has no RTK equivalent.

Used by Claude Code, Gemini CLI, and other LLM hooks:
  REWRITTEN=$(rtk rewrite "$CMD") || exit 0

The command actually returns four codes (the table on rewrite_cmd::run, checked end to end by tests/hook_decision_protocol_test.rs), and with no permission rules configured it exits 3, not 0 — deliberately, per #1155, so a machine without rules never auto-allows. A hook author who follows the help text and writes $(rtk rewrite "$CMD") || exit 0 therefore discards every default rewrite.

This PR:

  • rewrites the Rewrite variant's doc comment (which is what --help prints) to list all four codes, say that no-rule falls into 3, and point at hooks/claude/rtk-rewrite.sh as the reference handling
  • makes two rows of the table on rewrite_cmd::run match the tests: 0 is "allow rule matched" (not just "rewrite allowed"), and 3 also covers the no-rule default

No behaviour change. The shipped hook scripts already branch on all four codes, so they are left untouched.

New --help

Exit codes (a hook must handle all four):

0 - rewrite printed, an allow rule matched: the hook may auto-allow it.

1 - nothing printed, no RTK equivalent: run the command unchanged.

2 - nothing printed, a deny rule matched: defer to the agent's own deny.

3 - rewrite printed, an ask rule matched or no rule matched at all (the default): use the rewrite, but let the agent prompt the user.

Exit 3 is still a rewrite, so `$(rtk rewrite "$CMD") || exit 0` drops it. See hooks/claude/rtk-rewrite.sh for reference handling.

Verification

cargo fmt --all, cargo clippy --all-targets and cargo test --all pass locally.

`rtk rewrite --help` still described a two-code protocol (0 rewritten,
1 no equivalent) and suggested `$(rtk rewrite "$CMD") || exit 0`. The
command returns four codes, and with no permission rules it exits 3 by
design (rtk-ai#1155), so a hook following the help text drops every default
rewrite. List all four codes in the help, note that the no-rule default
is 3, and align the table on `rewrite_cmd::run` with the protocol tests.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01WcTsSr2cKxstnvgXk2bP88
Copilot AI lite review requested due to automatic review settings September 22, 2026 21:04
@CLAassistant

CLAassistant commented Sep 22, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Correct the shell example, referenced protocol comments, and host-specific handling description before approval.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity · 1 Low severity

Open (2)
What changed in this PR

Documentation-only update to align rtk rewrite --help and protocol documentation with all four exit codes.

Changes:

  • Documents all four rewrite exit codes and default behavior.
  • Corrects the rewrite_cmd exit-code table.
  • No runtime behavior changes.
File Summary
src/​main.rs Updates CLI help text.
src/​hooks/​rewrite_cmd.rs Corrects exit-code documentation.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/main.rs Resolved
Comment thread src/main.rs Resolved
Protocol-zero-0 and others added 2 commits October 9, 2026 14:41
Exit 0 needs allow rules covering every command of a chain, only
RTK_REWRITE_HOST=openclaw relaxes the default 3, and exit 1 also covers
excluded commands, `$(...)` and file redirects. Say so in --help and on
the rewrite_cmd::run table, update the outdated FEATURES.md section, and
test that --help gives each exit code its own paragraph.

Co-authored-by: tandede <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Round 1 — APPROVED

Claim: rtk rewrite --help documents all four exit codes, and the table on rewrite_cmd::run matches them.
Scope: accept.
Ran: develop merge-base vs this head merged onto develop e12ca86, built separately: exit-code matrix over allow / ask / deny / no rules, chains (&&, ;, |), $(...), redirects, excluded commands, already-rtk commands and RTK_REWRITE_HOST values (16 rows, then 75 cases by a second run); exit codes and output identical on both sides. REWRITTEN=$(rtk rewrite "$CMD") || exit 0 and hooks/claude/rtk-rewrite.sh run against the new binary. Docs only, no savings change.
Pins: no runtime decision added; the new help layout is pinned by test_rewrite_help_documents_each_exit_code (4 of 4 mutations of the help text fail it).
CI @d0abeebf0: all green (12 checks, tests on macOS, Ubuntu and Windows).

Checked and correct — no need to re-verify

  • Each code and its stdout as stated: 0 rewrite + allow, 1 nothing, 2 nothing + deny, 3 rewrite + ask or default.
  • REWRITTEN=$(rtk rewrite "$CMD") || exit 0 drops an exit-3 rewrite (default, no rules).
  • With RTK_REWRITE_HOST, explicit ask stays 3 and deny stays 2.
  • The two Copilot threads: the example keeps its REWRITTEN= assignment, and hooks/claude/rtk-rewrite.sh describes 0 and 3 the same way since your develop merge. Resolved.

Filed as follow-ups

  • #4500 openclaw docs say RTK_REWRITE_HOST relaxes only commands no rule matched; it relaxes every default ask, partial allow included — outside this PR's hunks.
  • rtk rewrite "-h" / "--help" prints the help on stdout with exit 0 — absorbed by #4351, comment added.

Fixup d0abeeb — what it changed

Running chains and host values showed the text was narrower than the code in four places; the fixup states them in --help, on the run table and in the paragraph under it:

  • exit 0 needs allow rules covering every command of a chain: git status && cargo test with only Bash(git status) allowed exits 3;
  • only RTK_REWRITE_HOST=openclaw relaxes (pi, hermes and unknown names stay 3), and it turns every 3 but an ask rule's into 0, so a hook that auto-allows on 0 runs rtk with the variable unset, as hooks/claude/rtk-rewrite.sh does;
  • exit 1 also covers nothing left to rewrite, a command excluded in config and constructs RTK does not rewrite ($(...), file redirects), and deny is checked before all of them;
  • a command already in RTK form can come back as itself under 0 or 3, so a hook compares the rewrite with the original.

It also carries in, co-authored by @tandede from #3639, the docs/usage/FEATURES.md section (it still described two codes and terraform plan as unsupported) and a test that each code renders as its own paragraph in --help.

Next

Nothing — fixup d0abeeb pushed and code-reviewed; approving.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants