Skip to content

fix(hooks): honour exclude_commands for head and tail - #3324

Merged
KuSh merged 3 commits into
rtk-ai:developfrom
katspa-sc:fix/head-tail-exclude-commands
Sep 1, 2026
Merged

KuSh merged 3 commits into
rtk-ai:developfrom
katspa-sc:fix/head-tail-exclude-commands

Conversation

@katspa-sc

@katspa-sc katspa-sc commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2823. Fixes #3371. Fixes #2363.

Partially addresses #3107 — this closes the exclude_commands half only. The memory half
(rtk read buffering the whole file) is unaffected and tracked separately.

Problem

head -N <file> and tail -N <file> are rewritten to rtk read, but listing head or tail in [hooks] exclude_commands has no effect — they are still rewritten.

Root cause is ordering in rewrite_segment_inner (src/discover/registry.rs): the head - / tail branch returns early, before the is_excluded() check in the Classification::Supported arm below it.

Reproduce with exclude_commands = ["head", "tail"]:

$ rtk hook check "head -20 src/main.rs"
rtk read src/main.rs --max-lines 20     # expected: no rewrite

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 Supported arm 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/tail suppressed when excluded, git status unaffected
  • test_head_tail_rewrite_when_not_excluded — normal rewrite still happens when they are not in the list

Pre-commit gate

cargo fmt --all --check and cargo clippy --all-targets are clean.

cargo test passes except 3 pre-existing failures in hooks::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 of develop with this branch stashed — they assert evaluate("git status", &[], &[]) is Ask, but check_command consults the developer's real agent permission settings, so the outcome is Allow on a machine where git status is pre-allowed. Happy to file that separately.

`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.
@KuSh

KuSh commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

Picked this one out of the seven PRs open against the head/tail fast path — it's the one that strips the env prefix before matching, so sudo head -20 f and FOO=1 head -20 f are covered, and mirroring the Supported arm is the right call given head/tail resolve to rtk read. I've added the Fixes lines to the description; the others are closed pointing here.

One thing to add before this lands: also match the path-stripped form, the way the Unsupported arm does with strip_absolute_path. Right now /usr/bin/head -20 f still rewrites past the exclusion. #3719 had that piece and nothing else did, so it's worth carrying over — roughly:

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 context == RewriteContext::Normal, so inside a pipeline head -20 already falls through to classify_command and gets exclusion-checked correctly today. Same command, opposite outcomes:

["head","tail","read"]   head -20 f.txt        →  rtk read f.txt --max-lines 20
                         cat f.txt | head -20  →  EXCLUDED

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 hooks::rewrite_cmd::tests::unattestable_passthrough — your diagnosis is right, and it's a known separate problem: those tests read the developer's real agent permission settings. No need to file it, #3638 is already open against it. Nothing for you to do here.

@bharvey42

Copy link
Copy Markdown

Confirming this is still needed as of develop @ aa40853 — worth stating explicitly, because #3749 landed a few hours ago and looks like it might have subsumed this one. It doesn't. The fast path still returns before any exclusion check:

  • src/discover/registry.rs:1364-1368 — the head - / tail branch returns rewrite_line_range(...) directly
  • src/discover/registry.rs:1385-1387 — the is_excluded check, in the Classification::Supported arm below it

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 Supported arm this PR mirrors no longer reads the way it did on 2026-08-30. It is now

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 tool_form (registry.rs:1516-1520) already applies strip_absolute_path internally. If that reads right to you, mirroring the arm as it now stands would cover the /usr/bin/head -20 f case that was asked for, without hand-rolling a second strip_absolute_path call.

It looks like it would also pick up something the review's own example assumed — that read in exclude_commands suppresses head/tail, since rules.rs:105-108 maps cat|head|tail to rtk_cmd: "rtk read" and tool_form normalizes to that. cat gets that today via the classify path; head/tail don't, because of the same early return. I haven't run that case, so treat it as a thing to confirm rather than a claim — but it seems to fall out of the mirror for free rather than needing extra code.

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. exclude_commands is load-bearing for me — I exclude grep/rg there to work around #3185/#3161 — so an exclusion that silently doesn't apply is a sharp edge. Would be glad to see this land.

KuSh and others added 2 commits September 1, 2026 13:09
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 KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 through registry::rewrite_command, so the single-site fix covers every hook/agent path. No other head/tail rewrite site exists.
  • PR head: cargo fmt --check, cargo clippy --all-targets and cargo test --all all 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 f all pass through. Controls hold: cat f still rewrites, and head -20 f still rewrites when only cat is excluded. Pipelines unchanged.
  • The Fixes claims check out — #2823, #3371 and #2363 are three duplicates of this bug, and #3107's exclude_commands half is genuinely closed.

Two follow-up commits pushed to this branch

  1. refactor: drop the no-op env strip in the head/tail exclude check — ENV_PREFIX is anchored on sudo/env/VAR=val, and the branch is only reached once cmd_part starts with head - or tail , so the replace could never fire. strip_disabled_prefix already peels those prefixes at the top of rewrite_segment_inner and recurses. Matching cmd_part directly is equivalent; added tests for sudo head … and RUST_LOG=… tail … so the recursion is what carries the exclusion.

  2. fix: keep exclude_commands honoured under routable wrappers — the exclusion was still bypassed under uv run. Because uv run is a routable wrapper, dropping the inner rewrite fell through and re-tested uv run head -20 f.rs as a uv invocation, yielding rtk uv run head -20 f.rs; the file contents went through an RTK filter despite the exclusion. Pre-existing (excluded cat behaved 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.

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

Labels

None yet

Projects

None yet

3 participants