rtk-ai/rtk

fix(openclaw): rtk-rewrite plugin silently drops all rewrites — exit code 3 treated as error

Open

Aperta il 1 giu 2026

Vedi su GitHub
 (2 commenti) (0 reazioni) (0 assegnatari)Rust (48.085 star) (2914 fork)batch import
area:clibuggood first issuepriority:high

Descrizione

Summary

The rtk-rewrite OpenClaw plugin (openclaw/index.ts) 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 — the plugin is effectively dead code.

The problem

rtk rewrite returns:

  • exit code 3 when it has a rewrite suggestion (known command)
  • exit code 1 when it has no rewrite (unknown command)
  • exit code 0 — never

The plugin's tryRewrite() function:

function tryRewrite(command: string): string | null {
  try {
    const result = execSync(`rtk rewrite ${JSON.stringify(command)}`, {
      encoding: "utf-8",
      timeout: 2000,
    }).trim();
    return result && result !== command ? result : null;
  } catch {
    return null; // ← always lands here for exit code 3
  }
}

execSync throws on any non-zero exit code. The catch block discards the rewrite. The command passes through unchanged.

How we found it

We discovered this while investigating why our RTK token savings weren't growing. We:

  1. Ran rtk rewrite "git log --oneline -5" → returns rtk git log --oneline -5 with exit code 3
  2. Simulated the plugin's tryRewrite() in Node.js → execSync threw, catch returned null
  3. Tested all common commands (git, kubectl, find, cat, grep) — all return exit code 3, all silently dropped

The 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;
  }

What we verified

Tested the fix against all common shell commands:

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.

Environment

  • RTK: 0.42.0
  • OpenClaw: 2026.5.28
  • Node.js: v24.14.0
  • OS: Linux (Debian, k3s)

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)

Guida contributor