Skip to content

rtk rewrite "rtk <builtin>" returns unchanged (exit 3) instead of stripping prefix — cd and other builtins silently no-op #2508

Closed as not planned

Description

@Starefossen

Bug

rtk rewrite treats a command that already starts with rtk as a valid rewrite target and returns it unchanged with exit 3 ("handled"). When the inner command is a shell builtin (cd, export, source, etc.) this silently breaks the builtin's effect, because the model's manually-prefixed rtk cd /path executes rtk as a subprocess — the directory change is lost when that subprocess exits.

Reproduction

$ rtk rewrite "rtk cd /tmp"
rtk cd /tmp          # ← same as input, exit 3 ("handled")

$ rtk rewrite "cd /tmp"
                     # exit 1 (passthrough — correct)

$ rtk cd /tmp; pwd
/original/dir        # ← directory unchanged despite exit 0

Expected behaviour

rtk rewrite "rtk <builtin> ..." should either:

  • Return passthrough (exit 1) so the shell runs the bare builtin, or
  • Strip the redundant prefix and return cd /tmp (exit 3)

Either way, the shell builtin should execute in the current process rather than a dead-end subprocess.

Context

The opencode plugin calls rtk rewrite on every bash tool call and replaces the command if the output differs from input. When a model manually prefixes rtk cd (following the "always use rtk" instruction in AGENTS.md), rtk rewrite returns the same string with exit 3 — so the plugin leaves the command as rtk cd /path, which executes with no net directory change.

Observed during GLM-4.7-Flash evaluation (2026-06-19): model ran rtk cd /path && rtk npm init -y, npm init ran in the wrong directory, model then invented a non-existent --workdir flag to compensate.

Workaround (local opencode plugin)

const SHELL_BUILTINS = /^rtk\s+(cd|pushd|popd|export|source|alias|unset|ulimit|umask)\b/
if (SHELL_BUILTINS.test(command)) {
  command = command.replace(/^rtk\s+/, "")
  ;(args as Record<string, unknown>).command = command
  return
}

Version

rtk 0.42.4

Activity

  1. pszymkowiak commented on Jun 25, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 93%

    Summary

    rtk rewrite returns 'rtk ' unchanged (exit 3) instead of stripping the prefix, causing shell builtins like cd to execute in a subprocess with no effect; PR #2534 is in progress.

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • src/rewrite/mod.rs
    • src/rewrite/builtin.rs

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  2. rmsc commented on Jun 27, 2026

    @rmsc

    This is actually also a security bug for hooks, allowing the LLM to circumvent its own allowlist:

    $ rtk rewrite "rm file"
                   # exit code 1, passthrough, the command is checked against the allowlist
    
    $ rtk rewrite "rtk rm file"
    rtk rm file    #exit code 3, if rtk is allowed 'rm' isn't checked against the allowlist

    Hooks can't just test for exit code 3, because the LLM might correctly want to invoke rtk ls -la instead of relying on the hook.

  3. KuSh commented on Sep 9, 2026

    @KuSh
    Collaborator

    Closing as a duplicate of #3969, which carries the full picture — this report, #3331, the three instruction blocks that generate these invocations, and the wording fix.

    The rtk rewrite change proposed here (and implemented in #2534) was rejected: stripping the prefix inverts the direction of every rewrite, and since the permission verdict is computed on the raw input while the rewritten string is what gets emitted, an explicitly denied command comes back stamped "allow". Reproduction in #3969.

    @rmsc — your allowlist point is now #3970, filed as its own issue precisely so it does not close with this one. It has your rtk rm file case as instance 1, plus the second instance found while reviewing #2534, and the reason each of the obvious fixes (gate on exit 3, strip unconditionally) does not work.

    Worth noting the symptom is worse than "silently no-op" as written above: rtk execs a binary named cd, so the ENOENT takes the rest of the compound with it.

    $ rtk cd /tmp && echo REACHED
    [rtk: No such file or directory (os error 2)]     # exit 127, REACHED never prints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:clibugpriority:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions