rtk-ai/rtk

Feature: rewrite subcommands inside multi-line / heredoc Bash blocks

Open

#1243 aperta il 12 apr 2026

Vedi su GitHub
 (2 commenti) (1 reazione) (0 assegnatari)Rust (2914 fork)batch import
area:clieffort-largeenhancementhelp wantedpriority:medium

Metriche repository

Star
 (48.085 star)
Metriche merge PR
 (Merge medio 11g 1h) (45 PR mergiate in 30 g)

Descrizione

Context

I'm using the RTK Claude Code hook (rtk-rewrite.sh v3) with RTK 0.35.0. The hook works well for single-line commands, but many real-world Claude Code sessions generate multi-line Bash blocks that contain rewritable subcommands which currently pass through unrewritten.

Problem

rtk rewrite treats the entire Bash input as a single command string. When Claude Code generates compound or multi-line commands, RTK either:

  • Returns exit 1 (no equivalent) for the whole block, even though individual lines inside are rewritable
  • Misses optimization opportunities on the most token-heavy commands (heredocs with surrounding setup)

Examples from real sessions

Heredoc Python blocks (most common — 16 occurrences in 30 days)

python3 << 'PYEOF'
import json
# ... 40 lines of Python ...
PYEOF

Not rewritable as a whole, but the output could still benefit from RTK filtering.

For-loops with rewritable inner commands

for f in *.sh; do ls -la "$f"; done

Exit 1 — but ls -la inside is a known RTK command.

Sequential commands with ; separator

cd /some/path; ls -la; grep -r "pattern" .

The ls -la and grep -r parts are individually rewritable.

What works today

Compound commands with && and pipes are handled (exit 0):

ls -la . && echo done       # ✅ rewritten
ls -la ~/.claude/ | head -5  # ✅ rewritten
cd /tmp && ls -la            # ✅ rewritten

Proposal

Consider parsing multi-line and compound Bash inputs to identify and rewrite individual subcommands where possible. Possible approaches:

  1. Line-by-line rewrite for multi-line blocks: split on \n, rewrite each line independently, rejoin
  2. Statement-level rewrite for ;-separated commands
  3. Output-only filtering for heredoc blocks: even if the command can't be rewritten, the output could be filtered through RTK's token reduction

Impact estimate

From rtk discover on my setup:

  • 175 Bash commands over 30 days
  • 91 rewritable commands identified → ~11.9K tokens saveable
  • 16 Python heredocs are the single largest category of unrewritable commands

Environment

  • RTK 0.35.0
  • Claude Code with Opus 4.6 (1M context)
  • macOS Darwin 25.4.0
  • Hook: rtk-rewrite.sh (PreToolUse Bash, hook-version 3)

Guida contributor