Skip to content

fix(hooks): honor exclude_commands for head/tail line-range fast path#2887

Open
AZERDSQ131 wants to merge 1 commit into
rtk-ai:developfrom
AZERDSQ131:fix/hooks-exclude-commands-head-tail-bypass
Open

fix(hooks): honor exclude_commands for head/tail line-range fast path#2887
AZERDSQ131 wants to merge 1 commit into
rtk-ai:developfrom
AZERDSQ131:fix/hooks-exclude-commands-head-tail-bypass

Conversation

@AZERDSQ131

Copy link
Copy Markdown

Closes #2823

[hooks] exclude_commands is bypassed for head/tail because the line-range fast path in rewrite_segment_inner returns before the exclusion check:

if cmd_part.starts_with("head -") || cmd_part.starts_with("tail ") {
    return rewrite_line_range(cmd_part).map(|r| format!("{}{}", r, redirect_suffix));
}

is_excluded is only consulted in the Classification::Supported and Unsupported arms further down, both downstream of this early return.

Repro before this fix (config: [hooks] exclude_commands = ["tail", "head", "rg"]):

$ rtk rewrite "tail -3 sample.txt"
rtk read sample.txt --tail-lines 3     # rewritten despite the exclusion

Fix: moved the same is_excluded check the Supported arm already uses (ENV_PREFIX-stripped command matched against the configured ExcludePattern list) in front of the fast path's early return, so excluded head/tail invocations fall through to the raw-passthrough path like every other excluded command.

Adds three regression tests in src/discover/registry.rs:

  • test_exclude_tail_line_range_fast_path — excluded tail no longer rewrites
  • test_exclude_head_line_range_fast_path — excluded head no longer rewrites
  • test_head_line_range_fast_path_still_rewrites_when_not_excluded — guards against fixing this by disabling the fast path entirely

Ran the mandatory gate locally:

  • cargo fmt --all — no changes needed
  • cargo clippy --all-targets — clean
  • cargo test --all — 2396 passed (+ 6/11/11/14/5/11 in the other suites), 0 failed

The head/tail line-range rewrite in rewrite_segment_inner returned
before the exclusion check, which only runs in the
Classification::Supported and Unsupported arms further down. Any
command routed through the head/tail fast path (cmd_part.starts_with
("head -") || cmd_part.starts_with("tail ")) was rewritten to
`rtk read ... --max-lines/--tail-lines` even when explicitly listed
in [hooks] exclude_commands, both via `rtk rewrite` and the Claude
Code hook path.

Moved the same is_excluded check already used by the Supported arm
(ENV_PREFIX-stripped command, matched against the configured
ExcludePattern list) in front of the fast path's early return.

Adds three regression tests: tail and head are no longer rewritten
when excluded, and head still rewrites normally when nothing is
excluded (guards against fixing this by breaking the fast path
entirely).
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[hooks] exclude_commands is bypassed for head/tail (line-range fast path skips the exclusion check)

2 participants