Skip to content

fix(openclaw): handle exit code 3 from rtk rewrite - #2202

Merged
KuSh merged 1 commit into
rtk-ai:developfrom
kzzalews:fix/openclaw-exit-code-3-clean
Jun 30, 2026
Merged

KuSh merged 1 commit into
rtk-ai:developfrom
kzzalews:fix/openclaw-exit-code-3-clean

Conversation

@kzzalews

@kzzalews kzzalews commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The rtk-rewrite OpenClaw plugin silently drops all command rewrites. Every rtk rewrite call that produces a suggestion returns exit code 3, which execSync treats as an exception. The catch block returns null, so no command is ever rewritten.

See #2200 for full analysis.

Fix

Check e.stdout in the catch handler — exit code 3 with stdout output is a valid rewrite suggestion, not an error:

} catch (e: any) {
    if (e?.stdout) {
      const result = String(e.stdout).trim();
      if (result && result !== command) return result;
    }
    return null;
  }

Verified

All common shell commands now correctly rewritten:

git log --oneline -5     → rtk git log --oneline -5     ✅
kubectl get pods -A      → rtk kubectl get pods -A      ✅
cat /tmp/test            → rtk read /tmp/test           ✅
ls -la /tmp              → rtk ls -la /tmp              ✅
find /tmp -name "*.log"  → rtk find /tmp -name "*.log"  ✅
grep -r test /tmp        → rtk grep -r test /tmp        ✅

All previously returned null (no rewrite) with the upstream code.


Aiko — AI assistant in OpenClaw
Running RTK on k3s with Kilo Gateway

Config: RTK 0.42.0 | OpenClaw 2026.5.28 | Node.js v24.14.0 | Model: MiMo-V2.5-Pro

Reviewed and verified by Zal (@kzzalews)

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

Thanks for your PR! While your current solution works as a temporary workaround, it’s not yet production-ready. I’ve left some suggestions to help guide you toward a more robust, final implementation.

Let me know if you’d like further clarification or assistance!

Comment thread openclaw/index.ts Resolved
@KuSh KuSh self-assigned this Jun 28, 2026
kzzalews added a commit to kzzalews/rtk that referenced this pull request Jun 29, 2026
Address KuSh review feedback on rtk-ai#2202:
- tryRewrite now returns [string | null, "ask"?] tuple
- Exit code 3 specifically checked (e.status === 3)
- before_tool_call returns requireApproval when rewrite
  is a suggestion (exit 3), per OpenClaw hook docs
- 60s approval timeout with deny on timeout

See: rtk-ai#2202 (review)

Signed-off-by: Karol Zalewski <[email protected]>
kzzalews added a commit to kzzalews/rtk that referenced this pull request Jun 29, 2026
Address KuSh review feedback on rtk-ai#2202:
- tryRewrite now returns [string | null, "ask"?] tuple
- Exit code 3 specifically checked (e.status === 3)
- before_tool_call returns requireApproval when rewrite
  is a suggestion (exit 3), per OpenClaw hook docs
- 60s approval timeout with deny on timeout

See: rtk-ai#2202 (review)

Signed-off-by: Karol Zalewski <[email protected]>
@kzzalews
kzzalews force-pushed the fix/openclaw-exit-code-3-clean branch from 1162f2d to 2256da9 Compare June 29, 2026 05:35
kzzalews added a commit to kzzalews/rtk that referenced this pull request Jun 29, 2026
Rework the plugin to respect RTK's full exit code protocol
(src/hooks/rewrite_cmd.rs):

Exit codes:
  0 + stdout  Allow — auto-apply rewrite (no approval)
  1           No match — pass through unchanged
  2           Deny — block the call (new)
  3 + stdout  Ask — rewrite with requireApproval (KuSh review)

Changes:
- tryRewrite returns [string | null, "ask" | "deny" | undefined]
- Exit 3: requireApproval with configurable timeout (default 120s)
- Exit 2: block:true (was silently passed through — security gap)
- allowedDecisions: ["allow-once", "deny"] — no allow-always
  (plugin does not persist trust)
- onResolution callback for verbose logging
- approvalTimeoutMs config option in openclaw.plugin.json
- Documented exit code protocol in file header

Address KuSh review: rtk-ai#2202 (review)

Signed-off-by: Karol Zalewski <[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.

Just a few remaining nitpicks and questions

Comment thread openclaw/index.ts Resolved
Comment thread openclaw/index.ts Resolved
Comment thread openclaw/index.ts Resolved
kzzalews added a commit to kzzalews/rtk that referenced this pull request Jun 29, 2026
…t timeoutMs

- Extract RewriteVerdict type per KuSh suggestion
- Remove timeoutMs from requireApproval (omit to use Gateway default)
- Remove approvalTimeoutMs config option from plugin.json
- allowedDecisions still excludes allow-always: OpenClaw docs confirm
  plugin allow-always does not auto-persist; offering it without our own
  persistence would mislead users

Address KuSh review: rtk-ai#2202 (review-4588193872)

Signed-off-by: Karol Zalewski <[email protected]>
@kzzalews

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review, KuSh — really appreciate the guidance.

1. RewriteVerdict type — done in 9827755. Extracted type RewriteVerdict = "ask" | "deny" and updated the tryRewrite signature accordingly.

2. timeoutMs omission — done in the same commit. Removed timeoutMs from requireApproval and the corresponding approvalTimeoutMs config option from openclaw.plugin.json. The approval prompt now falls back to the Gateway's default timeout.

3. allow-always omission — intentional, but I should have documented the reasoning. The OpenClaw docs are explicit that allow-always does not auto-persist for plugin hooks:

"allow-always is only durable when the requesting plugin or runtime implements that persistence. For ordinary before_tool_call.requireApproval hooks, OpenClaw treats allow-once and allow-always as approval decisions for the current call and passes the resolved value to onResolution."
— Plugin permission requests — Decision behavior

The troubleshooting section reinforces this:

"allow-always appears but the next call prompts again. The generic plugin approval flow does not automatically persist trust for arbitrary hooks."
— Plugin permission requests — Troubleshooting

Since this plugin doesn't implement its own trust persistence, offering allow-always would mislead users — the button would look like it remembers the decision, but the next call would prompt again. I'd rather be honest and offer only allow-once + deny. If persistent trust is desired, I can add a config-based allowlist of trusted command patterns in a follow-up.

@kzzalews
kzzalews force-pushed the fix/openclaw-exit-code-3-clean branch from 9827755 to 6b2851c Compare June 29, 2026 09:18
kzzalews added a commit to kzzalews/rtk that referenced this pull request Jun 29, 2026
Address KuSh review feedback on rtk-ai#2202:
- tryRewrite now returns [string | null, "ask"?] tuple
- Exit code 3 specifically checked (e.status === 3)
- before_tool_call returns requireApproval when rewrite
  is a suggestion (exit 3), per OpenClaw hook docs
- 60s approval timeout with deny on timeout

See: rtk-ai#2202 (review)

Signed-off-by: Karol Zalewski <[email protected]>
kzzalews added a commit to kzzalews/rtk that referenced this pull request Jun 29, 2026
Rework the plugin to respect RTK's full exit code protocol
(src/hooks/rewrite_cmd.rs):

Exit codes:
  0 + stdout  Allow — auto-apply rewrite (no approval)
  1           No match — pass through unchanged
  2           Deny — block the call (new)
  3 + stdout  Ask — rewrite with requireApproval (KuSh review)

Changes:
- tryRewrite returns [string | null, "ask" | "deny" | undefined]
- Exit 3: requireApproval with configurable timeout (default 120s)
- Exit 2: block:true (was silently passed through — security gap)
- allowedDecisions: ["allow-once", "deny"] — no allow-always
  (plugin does not persist trust)
- onResolution callback for verbose logging
- approvalTimeoutMs config option in openclaw.plugin.json
- Documented exit code protocol in file header

Address KuSh review: rtk-ai#2202 (review)

Signed-off-by: Karol Zalewski <[email protected]>
kzzalews added a commit to kzzalews/rtk that referenced this pull request Jun 29, 2026
…t timeoutMs

- Extract RewriteVerdict type per KuSh suggestion
- Remove timeoutMs from requireApproval (omit to use Gateway default)
- Remove approvalTimeoutMs config option from plugin.json
- allowedDecisions still excludes allow-always: OpenClaw docs confirm
  plugin allow-always does not auto-persist; offering it without our own
  persistence would mislead users

Address KuSh review: rtk-ai#2202 (review-4588193872)

Signed-off-by: Karol Zalewski <[email protected]>
@kzzalews
kzzalews requested a review from KuSh June 29, 2026 09:44
@kzzalews
kzzalews force-pushed the fix/openclaw-exit-code-3-clean branch from 6b2851c to c059525 Compare June 29, 2026 10:45
@CLAassistant

CLAassistant commented Jun 29, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@kzzalews
kzzalews force-pushed the fix/openclaw-exit-code-3-clean branch 3 times, most recently from f5c6eab to 97aaac8 Compare June 29, 2026 11:38
Comment thread openclaw/index.ts Resolved
Comment thread openclaw/index.ts Resolved
@kzzalews
kzzalews force-pushed the fix/openclaw-exit-code-3-clean branch from 0d50a72 to 8b632e9 Compare June 30, 2026 05:31
Interpret all RTK rewrite exit codes and respond appropriately:
- Exit 0 (Allow): auto-apply rewrite
- Exit 1 (Passthrough): no RTK equivalent, pass through unchanged
- Exit 2 (Deny): block the call entirely
- Exit 3 (Ask): rewrite available, require user approval via
  requireApproval with allow-once/deny decisions

Uses execFileSync (not execSync) to avoid shell injection. Returns
a [string | null, RewriteVerdict?] tuple from tryRewrite so the
before_tool_call hook can react to each verdict type.

"allow-always" omitted from allowedDecisions because OpenClaw does
not auto-persist approval for plugin hooks — see:
https://docs.openclaw.ai/plugins/plugin-permission-requests#troubleshooting

Closes rtk-ai#2202
@kzzalews
kzzalews force-pushed the fix/openclaw-exit-code-3-clean branch from 8b632e9 to 776a4b1 Compare June 30, 2026 05:47
@kzzalews

Copy link
Copy Markdown
Contributor Author

Thanks KuSh! All three rounds of feedback are now addressed in a single squashed commit (776a4b1):

  1. execFileSync restored — shell interpolation removed, .toString() added for Buffer safety
  2. timeoutMs removed — falling back to Gateway's default decision timeout
  3. allow-always omitted — inline comment added explaining OpenClaw doesn't auto-persist trust for plugin hooks
  4. Type safety cleanups — e.stdout.toString() for consistency, redundant optional chaining removed, explicit undefined trimmed from tuple returns

@kzzalews
kzzalews requested a review from KuSh June 30, 2026 10:15

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

LGTM, thanks for keeping up!

@KuSh
KuSh merged commit 487a2e7 into rtk-ai:develop Jun 30, 2026
10 checks passed
@kzzalews
kzzalews deleted the fix/openclaw-exit-code-3-clean branch July 10, 2026 23:43
iliaal pushed a commit to iliaal/rtk that referenced this pull request Jul 20, 2026
Interpret all RTK rewrite exit codes and respond appropriately:
- Exit 0 (Allow): auto-apply rewrite
- Exit 1 (Passthrough): no RTK equivalent, pass through unchanged
- Exit 2 (Deny): block the call entirely
- Exit 3 (Ask): rewrite available, require user approval via
  requireApproval with allow-once/deny decisions

Uses execFileSync (not execSync) to avoid shell injection. Returns
a [string | null, RewriteVerdict?] tuple from tryRewrite so the
before_tool_call hook can react to each verdict type.

"allow-always" omitted from allowedDecisions because OpenClaw does
not auto-persist approval for plugin hooks — see:
https://docs.openclaw.ai/plugins/plugin-permission-requests#troubleshooting

Closes rtk-ai#2202
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
rtk 0.44.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## [0.44.0](rtk-ai/rtk@v0.43.0...v0.44.0) (2026-07-26)


### Features

* add git checkout support ([be1844c](rtk-ai/rtk@be1844c))
* add uv run support ([a5f0774](rtk-ai/rtk@a5f0774))
* **cargo:** route --message-format=json builds through explicit arg detection ([5ea03f3](rtk-ai/rtk@5ea03f3))
* **hook:** add Cargo.lock ([531bc93](rtk-ai/rtk@531bc93))
* **hook:** add support for Kimi AI agent ([7624c43](rtk-ai/rtk@7624c43))
* **hooks:** add Factory Droid integration ([8a8b356](rtk-ai/rtk@8a8b356))
* **hook:** wire TOML filters + better tee tail hint ([31f9d43](rtk-ai/rtk@31f9d43))
* **hook:** wire TOML filters into the rewrite path + reversible truncation ([73b8cb3](rtk-ai/rtk@73b8cb3))
* **php:** consolidated PHP tooling (php, artisan, phpunit, phpstan, pest, paratest, ecs, pint) ([1052c1c](rtk-ai/rtk@1052c1c))
* **pipe:** expose PHP tool filters as stdin pipe filters ([214a79a](rtk-ai/rtk@214a79a))
* **sbt:** add SBT (Scala Build Tool) support ([4f86523](rtk-ai/rtk@4f86523))
* **trust:** gate custom filters (project + user-global) + init opt-in flags ([1130a7c](rtk-ai/rtk@1130a7c))
* **trust:** rtk trust uses the same opt-in prompt as init ([e0a83ac](rtk-ai/rtk@e0a83ac))


### Bug Fixes

* **analytics:** floor prefix slices to char boundary instead of full-string fallback ([c9468ee](rtk-ai/rtk@c9468ee))
* **analytics:** truncate display strings on UTF-8 char boundaries ([47b22e0](rtk-ai/rtk@47b22e0)), closes [#2787](rtk-ai/rtk#2787)
* **benchmark:** use deterministic curl and wget responses ([6c57836](rtk-ai/rtk@6c57836))
* **benchmark:** use stable indexed MockHTTP responses ([8dd5a34](rtk-ai/rtk@8dd5a34))
* **cargo:** give the json batch filter the exit code ([a3e65e9](rtk-ai/rtk@a3e65e9))
* **cargo:** honor last --message-format when the flag is repeated ([25d6a09](rtk-ai/rtk@25d6a09))
* **cargo:** keep "No issues found" wording for clean clippy json runs ([1f74614](rtk-ai/rtk@1f74614))
* **cargo:** label check output as "check" instead of "build" ([90b4f5f](rtk-ai/rtk@90b4f5f))
* **cargo:** match the human path when skipping the json warning summary ([59ed885](rtk-ai/rtk@59ed885))
* **cargo:** restore failure check before trusting reused build filter ([e4bfe35](rtk-ai/rtk@e4bfe35))
* **cargo:** skip "N warnings generated" record in json diagnostics ([9ae0872](rtk-ai/rtk@9ae0872))
* **cargo:** strip ansi from json rendered diagnostics ([b47653a](rtk-ai/rtk@b47653a))
* **cargo:** surface --message-format=json test compile errors ([f9b720c](rtk-ai/rtk@f9b720c))
* **cargo:** tee-hint dropped json diagnostics beyond the cap ([cf1caf9](rtk-ai/rtk@cf1caf9))
* **cargo:** tighten json warning-summary skip to ends_with ([6c9a60d](rtk-ai/rtk@6c9a60d))
* **ccusage:** accept `period` key from current ccusage ([d823aaf](rtk-ai/rtk@d823aaf))
* **cicd:** next release pr target fork compatibel ([6e34bba](rtk-ai/rtk@6e34bba))
* **copilot:** add missing 'get' to kubectl example in init template ([f9d8c77](rtk-ai/rtk@f9d8c77))
* **copilot:** remove unnecessary test ([b49568f](rtk-ai/rtk@b49568f))
* **copilot:** support IDE terminal hooks ([1d6798d](rtk-ai/rtk@1d6798d))
* detect absolute rtk path in Claude hook settings ([5d32d07](rtk-ai/rtk@5d32d07))
* **filter:** address review findings in the TOML filter path ([315a943](rtk-ai/rtk@315a943))
* **gain:** note untrusted custom filters so they can be re-trusted ([a4872d9](rtk-ai/rtk@a4872d9))
* **git:** compact git stash show instead of forcing -p ([1b38eec](rtk-ai/rtk@1b38eec))
* **grep:** only insert -- separator when context flags are active ([34a0f0e](rtk-ai/rtk@34a0f0e))
* **grep:** preserve -- separator between non-adjacent match blocks ([6871e55](rtk-ai/rtk@6871e55))
* **hook:** handle AskRewrite in Cursor hook when no rules configured ([ff0b9ac](rtk-ai/rtk@ff0b9ac)), closes [#2372](rtk-ai/rtk#2372)
* **hook:** include ask rules in cursor_has_explicit_rules check ([3362325](rtk-ai/rtk@3362325))
* **hook:** kimi init writes AGENTS.md instead of dead .kimirules ([8fe4a40](rtk-ai/rtk@8fe4a40))
* **hooks:** distinguish explicit ask from default in AskRewrite ([0df6929](rtk-ai/rtk@0df6929))
* **hooks:** don't force-allow Droid rewrites ([9dd8211](rtk-ai/rtk@9dd8211))
* **hooks:** emit permissionDecision allow for simple Copilot CLI rewrites ([23d1e89](rtk-ai/rtk@23d1e89))
* **hooks:** source Droid verdicts from Droid's own permission lists ([70ed82a](rtk-ai/rtk@70ed82a))
* **hook:** use ask action in audit log for AskRewrite verdict ([36dd8f2](rtk-ai/rtk@36dd8f2))
* **hook:** use ask permission for AskRewrite in Cursor hook ([a0c16ef](rtk-ai/rtk@a0c16ef))
* **init:** honor RTK_TELEMETRY_DISABLED in consent prompt ([#1307](rtk-ai/rtk#1307)) ([debac0f](rtk-ai/rtk@debac0f))
* **openclaw:** handle exit code 3 from rtk rewrite ([487a2e7](rtk-ai/rtk@487a2e7)), closes [#2202](rtk-ai/rtk#2202)
* **parser:** use byte offsets instead of char indices in extract_json_object ([32dda24](rtk-ai/rtk@32dda24))
* **parser:** use byte offsets instead of char indices in extract_json_object ([27f9739](rtk-ai/rtk@27f9739)), closes [#2509](rtk-ai/rtk#2509)
* **permissions:** stop extra whitespace from evading deny rules ([3483786](rtk-ai/rtk@3483786))
* **permissions:** stop extra whitespace from evading deny rules ([f6b9290](rtk-ai/rtk@f6b9290))
* **php:** address phpstan review feedback (resolution, text fallback, path compaction) ([07c231e](rtk-ai/rtk@07c231e))
* **php:** anchor phpunit failure-heading detection to the "N) " format ([128d04f](rtk-ai/rtk@128d04f))
* **php:** classify php subcommands in the PASSTHROUGH list ([4a37246](rtk-ai/rtk@4a37246))
* **php:** default pint applied_fixers when key is absent ([0cc15dc](rtk-ai/rtk@0cc15dc))
* **php:** detect phpstan analyse after global flags; avoid duplicate format ([7cc46b4](rtk-ai/rtk@7cc46b4))
* **php:** drop bogus pest.php check in test-runner detection ([28366ce](rtk-ai/rtk@28366ce))
* **php:** rewrite ./vendor/bin/&lt;tool&gt; form for phpunit/pest/paratest/ecs/pint ([88e56ce](rtk-ai/rtk@88e56ce))
* **php:** route php_tool_command through resolved_command ([8eae6b7](rtk-ai/rtk@8eae6b7

See merge request: Harmonybrew/homebrew-core!15172
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.

3 participants