Repository navigation
fix(hooks): honour exclude_commands for head and tail - #3324
Conversation
`head -N <file>` and `tail -N <file>` rewrite to `rtk read`, but their branch in rewrite_segment_inner returned before the is_excluded() check further down, so listing "head" or "tail" in [hooks] exclude_commands had no effect. Users who want file contents read unfiltered had no way to opt out. Adds the exclusion check to that branch, mirroring how the Supported branch strips an env prefix before matching, plus regression tests covering both the excluded and non-excluded paths.
|
Picked this one out of the seven PRs open against the One thing to add before this lands: also match the path-stripped form, the way the let stripped = ENV_PREFIX.replace(cmd_part, "");
let cmd_clean = stripped.trim();
if is_excluded(cmd_clean, excluded)
|| is_excluded(&strip_absolute_path(cmd_clean), excluded)
{
return None;
}plus a test for the path-prefixed form. Worth knowing about the fast path while you're in there: it's guarded on So this is removing a special case rather than adding one, which is a good sign for the approach. On the three failures you flagged in |
|
Confirming this is still needed as of
So #3371 is live on mainline today, and this PR is still the fix. One thing that may have shifted under the review comment above, since #3749 merged after it was written: the let stripped = ENV_PREFIX.replace(cmd_part, "");
let cmd_clean = stripped.trim();
if !excluded.is_empty()
&& (is_excluded(cmd_clean, excluded)
|| is_excluded(&tool_form(cmd_clean, rtk_equivalent), excluded))
{
return None;
}and It looks like it would also pick up something the review's own example assumed — that User-side context: I've been carrying the equivalent fix (from the now-closed #3378) in a local fork build since 2026-08-08 on Windows 11. |
ENV_PREFIX is anchored on sudo/env/VAR=val, and this branch is only reached once cmd_part starts with "head -" or "tail ", so the replace could never match. strip_disabled_prefix already peels any such prefix at the top of rewrite_segment_inner and recurses on the remainder. Match cmd_part directly and cover the prefixed forms with tests so the recursion carries the exclusion, not a strip that never fires. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`uv run` is a routable wrapper, so when the inner rewrite is dropped the loop falls through and re-tests `uv run <cmd>` as a `uv` invocation. With "head" excluded, `uv run head -20 f.rs` still became `rtk uv run head -20 f.rs` — the file contents went through an RTK filter anyway. Stop the fall-through when the wrapped command is excluded, so the exclusion holds whether or not a wrapper is in front of it. Same fix covers the identical hole for excluded `cat` under `uv run`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
KuSh
left a comment
There was a problem hiding this comment.
Reviewed at high effort. No correctness bugs — the fix is minimal, correctly placed before rewrite_line_range, and consistent with the Supported/Unsupported arms.
Verification
- All rewrite entry points (
src/hooks/rewrite_cmd.rs,src/hooks/hook_cmd.rs,src/main.rs) funnel throughregistry::rewrite_command, so the single-site fix covers every hook/agent path. No otherhead/tailrewrite site exists. - PR head:
cargo fmt --check,cargo clippy --all-targetsandcargo test --allall clean. - Merged latest
develop: no conflicts, full suite green. - Probes with
exclude_commands = ["head", "tail"]:head -20 f,tail -20 f,tail -n 20 f,sudo head -20 f,RUST_LOG=x head -20 f,head -20 f 2>&1,nocorrect head -20 fall pass through. Controls hold:cat fstill rewrites, andhead -20 fstill rewrites when onlycatis excluded. Pipelines unchanged. - The
Fixesclaims check out — #2823, #3371 and #2363 are three duplicates of this bug, and #3107'sexclude_commandshalf is genuinely closed.
Two follow-up commits pushed to this branch
-
refactor: drop the no-op env strip in the head/tail exclude check—ENV_PREFIXis anchored onsudo/env/VAR=val, and the branch is only reached oncecmd_partstarts withhead -ortail, so thereplacecould never fire.strip_disabled_prefixalready peels those prefixes at the top ofrewrite_segment_innerand recurses. Matchingcmd_partdirectly is equivalent; added tests forsudo head …andRUST_LOG=… tail …so the recursion is what carries the exclusion. -
fix: keep exclude_commands honoured under routable wrappers— the exclusion was still bypassed underuv run. Becauseuv runis a routable wrapper, dropping the inner rewrite fell through and re-testeduv run head -20 f.rsas auvinvocation, yieldingrtk uv run head -20 f.rs; the file contents went through an RTK filter despite the exclusion. Pre-existing (excludedcatbehaved the same way), but it left a hole in exactly what this PR promises, so I closed it here rather than filing a follow-up. Stopping the fall-through when the wrapped command is excluded fixes both.
Both are separate commits, so drop either if you'd rather keep the scope tight. Full suite green on the branch and after merging develop.
Thanks for the clean fix and the two-directional tests — nice catch on a bug that had three open duplicates.
Fixes #2823. Fixes #3371. Fixes #2363.
Partially addresses #3107 — this closes the
exclude_commandshalf only. The memory half(
rtk readbuffering the whole file) is unaffected and tracked separately.Problem
head -N <file>andtail -N <file>are rewritten tortk read, but listingheadortailin[hooks] exclude_commandshas no effect — they are still rewritten.Root cause is ordering in
rewrite_segment_inner(src/discover/registry.rs): thehead -/tailbranch returns early, before theis_excluded()check in theClassification::Supportedarm below it.Reproduce with
exclude_commands = ["head", "tail"]:This leaves no way to opt out of routing file contents through
rtk read— relevant when you want a file read verbatim and not filtered.Fix
Applies the exclusion check inside that branch, mirroring how the
Supportedarm strips an env prefix before matching, so behaviour is consistent between the two paths.Tests
Two regression tests in
src/discover/registry.rs:test_head_tail_honour_exclude_commands—head/tailsuppressed when excluded,git statusunaffectedtest_head_tail_rewrite_when_not_excluded— normal rewrite still happens when they are not in the listPre-commit gate
cargo fmt --all --checkandcargo clippy --all-targetsare clean.cargo testpasses except 3 pre-existing failures inhooks::rewrite_cmd::tests::unattestable_passthrough(test_plain_command_still_rewrites,test_fd_dup_redirect_still_rewrites,test_dollar_substitution_passthrough). These fail identically on a clean checkout ofdevelopwith this branch stashed — they assertevaluate("git status", &[], &[])isAsk, butcheck_commandconsults the developer's real agent permission settings, so the outcome isAllowon a machine wheregit statusis pre-allowed. Happy to file that separately.