rtk-ai / rtk-ai/rtk

Feature Request: Add sudo prefix support for command rewriting

Open Beginner friendly
#146 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

area:cli effort-small enhancement good first issue P3-nice-to-have priority:low
Dominant language
Rust
Stars
81.1k
Forks
5.1k
Avg merge
4d 21h
Merged PRs (30d)
35

Description

Problem

The RTK auto-rewrite hook currently doesn't handle commands prefixed with sudo. This means commands like:

  • sudo docker ps
  • sudo docker logs <container>
  • sudo git status

are not rewritten to their RTK equivalents, resulting in missed token savings when running commands that require elevated privileges.

Proposed Solution

Add logic to strip and preserve the sudo prefix (including flags like sudo -u user) before pattern matching, then restore it in the rewritten command.

Changes to .claude/hooks/rtk-rewrite.sh
  1. Extract sudo prefix after env var extraction:
# Strip leading sudo (with optional flags) for pattern matching
# e.g., "sudo docker ps" → match against "docker ps"
# e.g., "sudo -u root docker ps" → match against "docker ps"
# Handles: sudo, sudo -u user, sudo -i, sudo -E, sudo -v, sudo -k, etc.
SUDO_PREFIX=""
if echo "$MATCH_CMD" | grep -qE '^sudo([[:space:]]|$)'; then
  # Extract sudo and any flags (but stop at the actual command)
  SUDO_PREFIX=$(echo "$MATCH_CMD" | grep -oE '^sudo([[:space:]]+-[A-Za-z]+([[:space:]]+[^[:space:]]+)?)*[[:space:]]+' || echo "")
  if [ -n "$SUDO_PREFIX" ]; then
    MATCH_CMD="${MATCH_CMD:${#SUDO_PREFIX}}"
    CMD_BODY="${CMD_BODY:${#SUDO_PREFIX}}"
  fi
fi
  1. Update all REWRITTEN assignments to include ${SUDO_PREFIX}:
# Before
REWRITTEN="${ENV_PREFIX}rtk $CMD_BODY"

# After
REWRITTEN="${ENV_PREFIX}${SUDO_PREFIX}rtk $CMD_BODY"
Example Transformations
Input Output
sudo docker ps sudo rtk docker ps
sudo docker logs container sudo rtk docker logs container
sudo -u root git status sudo -u root rtk git status

Testing

Verified working with:

echo '{"tool_input":{"command":"sudo docker ps"}}' | ~/.claude/hooks/rtk-rewrite.sh
# Output: {"hookSpecificOutput":{..."updatedInput":{"command":"sudo rtk docker ps"}}}

Notes

  • The regex handles sudo, sudo -u user, sudo -i, sudo -E, and combinations
  • Follows the same pattern already used for env var prefix handling

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with .claude/hooks/rtk-rewrite.sh and trace the existing environment-variable prefix handling and every REWRITTEN assignment. Run the provided JSON echo command, then check sudo, sudo -u root, and the listed Docker and Git examples; done means the sudo prefix is preserved while the command is rewritten to use rtk.

Written by the indexing model from the issue text.

Assessment

Tech stack
shell
Domain
cli, tooling
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
82/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.