rtk icon indicating copy to clipboard operation
rtk copied to clipboard

Feature Request: Add sudo prefix support for command rewriting

Open tmaarcxs opened this issue 9 months ago • 3 comments

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

tmaarcxs avatar Feb 16 '26 12:02 tmaarcxs

personally they are re written now, but it doesn't work as rtk isn't in the root user PATH. maybe it should re write rtk sudo instead of sudo rtk ?

Image

mirsella avatar Mar 15 '26 08:03 mirsella

wshm · Automated triage by AI

🔍 Automated Triage

✨ Category feature
🟢 Priority low
🎯 Confidence 93%

Summary

Feature request to handle sudo-prefixed commands in rtk-rewrite.sh; well-scoped and self-contained, but the linked PR (#1806) addresses Cursor hook detection rather than this issue, so no active implementation is in progress for this specific request.

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

📁 Relevant files
  • .claude/hooks/rtk-rewrite.sh

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

pszymkowiak avatar Mar 20 '26 16:03 pszymkowiak

The direction changed since this was filed. #2810 (merged into develop on 2026-09-04) makes sudo-prefixed commands pass through unrewritten: rtk is not on sudo's secure_path, and putting rtk behind sudo would run its filtering and tracking as root. The planned replacement is the other way around, a dedicated rtk sudo <cmd> subcommand that keeps sudo, its TTY and its privileges on the inner command only. Keeping this open to track that.

KuSh avatar Sep 05 '26 13:09 KuSh