Skip to content

fix(hook): rewrite the first command of a loop body - #5113

Open
seragAhmed wants to merge 1 commit into
rtk-ai:developfrom
seragAhmed:fix/hook-rewrite-loop-bodies
Open

seragAhmed wants to merge 1 commit into
rtk-ai:developfrom
seragAhmed:fix/hook-rewrite-loop-bodies

Conversation

@seragAhmed

Copy link
Copy Markdown

Summary

Fixes #5110

  • The first command of a for/while/until body is now rewritten: for f in a b; do rg -n x "$f"; done → for f in a b; do rtk rg -n x "$f"; done. After splitting on ;, that command sits behind do, so do joins the keyword prefixes (exec, command, noglob, ...). Deny/ask rule matching reads that list too, so a deny rule for git push still fires on do git push ....
  • A loop whose output is piped (...; done | wc -l) keeps its whole body raw, since that output goes to the consumer, not the agent. Before this, commands after the first one in such a body were already rewritten.
  • $(...) and backticks are left alone, as agreed in the issue. The loop header (while git diff --quiet; do) is unchanged too. Multi-line loops still pass through as a whole, as before.

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test. Clippy is clean. All discover/hooks tests pass. Two tests fail on Windows (find_cmd::run_from_args_propagates_find_exit_status, stream::test_read_lines_lossy_preserves_lines_after_invalid_utf8), and they fail on unmodified develop too.
  • New tests: loop bodies (for/while/until, nested, && inside), header and substitutions untouched, piped loops raw, and a deny rule firing on a loop body. The rewrite and deny tests fail without the change, and the piped-loop test fails without the guard.
  • Manual: the issue's repro through rtk hook claude:
Input Result
for f in a b; do rg -n x "$f"; done for f in a b; do rtk rg -n x "$f"; done
for f in a b; do rg -n x "$f"; done | wc -l no rewrite
x=$(rg -n foo) no rewrite

🤖 Generated with Claude Code

`for f in a b; do rg -n x "$f"; done` was passed through unchanged: once the
line is split on `;`, the body's first command sits behind `do`, which the
rewrite did not read past. `do` now joins the keyword prefixes (`exec`,
`command`, ...), so the rewrite reads past it, and so does deny/ask rule
matching.

The body of a loop whose output is piped (`...; done | wc -l`) writes to the
consumer, not the agent, so its commands are left raw.

Command substitutions are left alone, as agreed in the issue.

Fixes rtk-ai#5110

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CLAassistant

CLAassistant commented Nov 14, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working hooks security labels Nov 14, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes a hook command-rewrite bug where the first command inside a for/while/until loop body was not being prefixed/matched because it sits behind the do keyword after splitting on ;. Adds do to the shell keyword prefix list and excludes loop bodies whose output is piped to a consumer from rewriting, leaving headers and command substitutions untouched.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #5110


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

@seragAhmed

Copy link
Copy Markdown
Author

Hi, a gentle follow-up on this one. #5117 (keeping backslash-newline inside quotes and comments) is ready for review too. Happy to adjust either if you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working hooks security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hook: commands inside $(...) and loop bodies are not rewritten

2 participants