Repository navigation
fix(hook): collapse bash line continuations before matching (#1564) - #1572
Conversation
📊 Automated PR Analysis
SummaryFixes bash line-continuation sequences (backslash followed by newline) defeating the hook command matcher by adding a normalization step that collapses them into single spaces before trimming and classifying the command. This mirrors bash's own behavior and restores correct rewriting for commands split across multiple lines. Review Checklist
Linked issues: #1564 Analyzed automatically by wshm · This is an automated analysis, not a human review. |
KuSh
left a comment
There was a problem hiding this comment.
Apart from my regexp suggestion, you should remove changes to files others than src/discover/registry.rs that comes from your change of branch target. After that it will be ready to merge
| /// trailing whitespace prevents double spaces in cases like | ||
| /// `git diff \<NL>HEAD~1`. | ||
| static ref LINE_CONTINUATION_RE: Regex = | ||
| Regex::new(r"[ \t]*\\(?:\r\n|\n)[ \t]*").unwrap(); |
There was a problem hiding this comment.
| Regex::new(r"[ \t]*\\(?:\r\n|\n)[ \t]*").unwrap(); | |
| Regex::new(r"(?m)[ \t\x0B\x0C]*\\\r?\n[ \t\x0B\x0C]*").unwrap(); |
will also match vertical tab (0x0B) or form feed (0x0C) which is considered whitespace and simplify the newline pattern
e9ba852 to
d1d1800
Compare
|
Done, rebased onto current |
|
Applied the regex suggestion: now uses |
) Closes rtk-ai#1564. `rtk hook check` rejected commands that began with a bash line continuation (`\<NL>`) because the matcher saw the literal `\` as the first token and bailed out: $ rtk hook check $'\\\ngit diff HEAD~1' No rewrite for: \\ngit diff HEAD~1 Bash collapses `\<NL>` (and `\<CRLF>`) plus surrounding horizontal whitespace to a single space before dispatching the command, so the matcher should do the same. Internal continuations (e.g. `git diff \<NL>HEAD~1`) are equally common when Claude Code formats long invocations. Add a small `collapse_line_continuations` helper that runs a single regex over the input before `cmd.trim()`. The regex eats the trailing whitespace on the previous line and the leading indentation on the next line so the no-double-space invariant holds. The fast path returns `Cow::Borrowed` when no continuation is present, so commands without `\<NL>` allocate nothing. Six regression tests pin: leading \\<NL\>, leading \\<CRLF\>, internal \\<NL\> between subcommand and args, \\<NL\> followed by indentation, the no-op fast path, and the helper-level Cow::Borrowed contract.
f18ea1a to
2387594
Compare
|
Rebased onto current develop (was 181 commits behind, now current). Three commits replayed cleanly. |
|
Both items are done: the diff is now limited to |
| let cmd_clean = cmd_normalized.as_str(); | ||
|
|
||
| // Exclude cat/head/tail with redirect operators — these are writes, not reads (#315) | ||
| // Exclude cat/head/tail with redirect operators, these are writes, not reads (#315) |
There was a problem hiding this comment.
please avoid including changes like that, that only adds noise.
| // Extract env prefix (sudo, env VAR=val, etc.) | ||
| let stripped_cow = ENV_PREFIX.replace(cmd_part, ""); | ||
| let env_prefix_len = cmd_part.len() - stripped_cow.len(); | ||
| let env_prefix = &cmd_part[..env_prefix_len]; | ||
| let cmd_clean = stripped_cow.trim(); | ||
|
|
||
| // #345: RTK_DISABLED=1 in env prefix → skip rewrite entirely | ||
| // #508: warn on stderr so agents learn to stop overusing it | ||
| if has_rtk_disabled_prefix(cmd_part) { | ||
| eprintln!( | ||
| "[rtk] RTK_DISABLED=1 detected, skipping filter for this command. \ | ||
| Remove RTK_DISABLED=1 to restore token savings." | ||
| ); | ||
| return None; | ||
| } | ||
|
|
||
| if let Some(parts) = parse_golangci_run_parts(cmd_clean) { |
There was a problem hiding this comment.
This seems to be a rebase error. Please review
| assert_eq!(rewrite_command("cat -A file.cpp", &[]), None); | ||
| assert_eq!(rewrite_command("cat -v file.txt", &[]), None); | ||
| assert_eq!(rewrite_command("cat -e file.txt", &[]), None); | ||
| assert_eq!(rewrite_command("cat -t file.txt", &[]), None); | ||
| assert_eq!(rewrite_command("cat -s file.txt", &[]), None); | ||
| assert_eq!(rewrite_command("cat --show-all file.txt", &[]), None); |
There was a problem hiding this comment.
Again, rebase error, which would have been avoided by refraining on out of scope changes
| // The exact reproduction from #1564: a leading `\<NL>` made | ||
| // the matcher see `\` as the command and bail out. | ||
| assert_eq!( | ||
| rewrite_command("\\\ngit diff HEAD~1", &[]), |
There was a problem hiding this comment.
Doesn't compile after rebase. You should use rewrite_command_no_prefixes
Signed-off-by: Sai Asish Y <[email protected]>
Signed-off-by: Sai Asish Y <[email protected]>
2387594 to
0ec99de
Compare
|
Fixed all four points: removed the em-dash chore commit from history, corrected |
Closes #1564.
Reproduction (from the issue)
Without the leading
\\<NL>, the same command rewrites cleanly tortk git diff HEAD~1.Root cause
rewrite_commandcallscmd.trim()and then immediately classifies the leading token. Backslash is not a whitespace character, sotrim()does not unwrap a leading\\<NL>, and the matcher sees\\as the command name and bails out. The same issue affects internal continuations likegit diff \\<NL>HEAD~1, which Claude Code emits routinely when formatting long invocations.rtk discover -a -s 30shows ~15 calls slipping past the hook this way in 30 days, almost allgit,gh,find, i.e. commands with working handlers.Fix
Add a small helper:
Called before
cmd.trim()inrewrite_command. The regex consumes the trailing whitespace on the previous line and the leading indentation on the next line so a continuation never expands to a double space (e.g.git diff \\<NL>HEAD~1collapses togit diff HEAD~1, notgit diff HEAD~1).The fast path returns
Cow::Borrowedwhen no continuation is present, so commands that never had\\<NL>allocate nothing.Test plan
Six regression cases under
discover::registry::tests:test_rewrite_leading_backslash_newline, exact repro from the issue.test_rewrite_leading_backslash_crlf, Windows / Git Bash line endings.test_rewrite_internal_backslash_newline,git diff \\<NL>HEAD~1.test_rewrite_backslash_newline_with_indent, continuation followed by indentation.test_rewrite_no_line_continuation_unchanged, pins that the no-op fast path still works.test_collapse_line_continuations_no_op, pins theCow::Borrowedcontract for the helper.Results:
cargo test --bin rtk -- discover::registry→ 238 passed; 0 failed (6 new + 232 existing).cargo fmtclean.Notes
\\<NL>in the input.