Skip to content

test(filters): assert all builtin toml match_command patterns are anchored - #3807

Merged
KuSh merged 3 commits into
rtk-ai:developfrom
amandeavor:fix/anchored-toml-filters
Sep 2, 2026
Merged

KuSh merged 3 commits into
rtk-ai:developfrom
amandeavor:fix/anchored-toml-filters

Conversation

@amandeavor

@amandeavor amandeavor commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Pins that every built-in TOML filter match_command is anchored on every top-level branch, so a pattern cannot match a path component or subcommand mid-line. A filter selects on argv[0], but its regex runs against the whole command line, so an unanchored branch makes timeout 5 /usr/bin/liquibase update activate the liquibase filter and rewrite the wrapper.

The anchoring rule is now enforced at load time by a shared is_fully_anchored predicate applied to both loaders (match_patterns_in and TomlFilterRegistry::parse_and_compile), so it also covers project-local and global filters. Those are user-authored and were previously unguarded — which is where #3757 said the durable fix belonged. Unanchored filters are skipped with a warning rather than silently misfiring.

Refs #3757

Validation

  • test_builtin_all_filters_match_command_anchored — checks every built-in filter, reading raw patterns from BUILTIN_TOML so the load-time gate cannot hide an unanchored built-in from the assertion. Accepts per-branch anchoring (^pnpm\b|^npm\b) and inline flags ((?i)^tool\b); rejects ^liquibase\b|/liquibase\b and (?:^|/)liquibase(?:\s|$).
  • test_unanchored_user_filter_is_rejected_by_both_loaders — pins that a user-authored unanchored filter is dropped by both load paths, keeping MATCH_SET and the compiled registry in agreement.
  • test_liquibase_match_command_ignores_path_substring — extended with the wrapper cases (timeout, nohup) alongside the existing path-substring assertions and their rationale comment.
  • test_toml_filter_rewrites_bare_command_but_not_wrapped_invocations — exercises the rewrite path end to end via jj, which is covered only by a TOML filter and never by the native RULES table. The bare case acts as a positive control so the wrapper assertions cannot pass vacuously.
  • test_path_qualified_liquibase_is_not_rewritten — pins the current None for /usr/bin/liquibase update, with a comment recording that this diverges from Unanchored match_command in liquibase.toml rewrites any command containing /liquibase #3757's requested behavior.

cargo fmt --all, cargo clippy --all-targets (clean), cargo test --all (all passing), verified both on this branch and merged onto the latest develop.

Note on #3757

Changed Closes to Refs. That issue's third required-coverage bullet asks for /usr/bin/liquibase update to still be rewritten; this branch pins the current None as a characterization test instead.

That None is itself a bug, tracked in #3757 rather than fixed here. classify_command correctly reports the command as Supported — it is a liquibase invocation and RTK can filter it — but the rewrite path matches RULES.rewrite_prefixes against the un-normalized cmd_part, finds no prefix, and drops it. So every path-qualified invocation of every supported command silently skips filtering (/usr/bin/grep, /usr/bin/make behave the same), and rtk discover advertises an optimization the hook then declines.

The fix is to normalize cmd_part before that match, which is a behavior change beyond this PR's test-and-guard scope. When it lands, test_path_qualified_liquibase_is_not_rewritten should be updated to assert the rewrite rather than deleted, so the path-qualified case stays covered. #3757 stays open until then.

Copilot AI lite review requested due to automatic review settings September 1, 2026 16:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

Test-only diff (31 lines) — the anchor fix itself already landed in cab2283. I validated it in a throwaway worktree: merges cleanly onto latest develop, full suite green (2931 passed), and both new tests are non-vacuous — reverting src/filters/liquibase.toml to the unanchored (?:^|/)liquibase(?:\s|$) makes both fail. So the guards do catch the exact bug from #3757.

No correctness issues. Requesting changes on four coverage gaps, three of them empirically verified — the anchor assertion is weaker than the class of pattern it's meant to prevent, the discover test largely tests a path other than the one it claims, and #3757's required /usr/bin/liquibase update case is missing (and behaves opposite to what the issue states).

Comment thread src/core/toml_filter.rs Resolved
Comment thread src/discover/registry.rs Resolved
Comment thread src/discover/registry.rs Resolved
Comment thread src/discover/registry.rs Resolved
@amandeavor

Copy link
Copy Markdown
Contributor Author

Added direct built-in TOML coverage so the Liquibase tests cannot be bypassed by the native registry or RTK_NO_TOML. The anchor check now rejects top-level alternation, and the current path-qualified behavior is pinned explicitly. Formatting, Linux clippy, and all three focused regression tests pass.

amandeavor and others added 2 commits September 2, 2026 09:03
An unanchored top-level branch matches a path component or wrapper
argument mid-command, so a filter selecting on argv[0] activates on
`timeout 5 /usr/bin/liquibase update` and rewrites the wrapper.

Gate both loaders on a shared `is_fully_anchored` predicate, which
splits on top-level `|` and requires every branch to be `^`-anchored
(inline flag groups such as `(?i)` are stripped first). This covers
project-local and global filters, which are user-authored and were
previously unguarded; the built-in invariant test reads raw patterns so
the gate cannot hide an unanchored built-in from it.

Cover the rewrite path end to end with a bare `jj` control, fold the
wrapper cases into the existing liquibase path test, and accept
per-branch anchoring such as `^pnpm\b|^npm\b`.

Co-Authored-By: Claude Opus 5 <[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.

Verified in a dedicated worktree. All four round-one comments are fixed, each confirmed by mutation rather than by reading:

  • Reverting src/filters/liquibase.toml to the unanchored (?:^|/)liquibase(?:\s|$) fails the guards.
  • Setting it to ^liquibase\b|/liquibase\b — the top-level-alternation case that passed the old starts_with('^') check — now fails too.
  • Both still fail with RTK_NO_TOML=1, so the coverage is no longer env-dependent.

cargo fmt --all, cargo clippy --all-targets (clean) and cargo test --all pass, both on the branch and merged onto the latest develop (2981 passing).

I pushed one commit (3522477) addressing the follow-up review findings:

  • The anchoring rule moved into a shared is_fully_anchored predicate applied at load time in both loaders, so it now covers project-local and global filters. Those are user-authored and were unguarded — which is where #3757 said the durable fix belonged. Unanchored filters are skipped with a warning instead of misfiring.
  • The built-in invariant test now reads raw patterns from BUILTIN_TOML, so the new gate cannot hide an unanchored built-in from the assertion.
  • Per-branch anchoring (^pnpm\b|^npm\b) and inline flags ((?i)^tool\b) are accepted rather than rejected with a misleading message.
  • The rewrite path is covered end to end through jj (TOML-only, never in the RULES table), with the bare case as a positive control so the wrapper assertions cannot pass vacuously.
  • The wrapper cases are folded into the existing test_liquibase_match_command_ignores_path_substring, next to its rationale comment.

@KuSh
KuSh merged commit f83c0cf into rtk-ai:develop Sep 2, 2026
12 of 13 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Sep 2, 2026
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.

3 participants