Skip to content

fix(hook): collapse bash line continuations before matching (#1564) - #1572

Merged
KuSh merged 4 commits into
rtk-ai:developfrom
SAY-5:fix/hook-line-continuation-1564
May 25, 2026
Merged

KuSh merged 4 commits into
rtk-ai:developfrom
SAY-5:fix/hook-line-continuation-1564

Conversation

@SAY-5

@SAY-5 SAY-5 commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1564.

Reproduction (from the issue)

$ rtk hook check $'\\\\\\ngit diff HEAD~1'
No rewrite for: \\\ngit diff HEAD~1

Without the leading \\<NL>, the same command rewrites cleanly to rtk git diff HEAD~1.

Root cause

rewrite_command calls cmd.trim() and then immediately classifies the leading token. Backslash is not a whitespace character, so trim() does not unwrap a leading \\<NL>, and the matcher sees \\ as the command name and bails out. The same issue affects internal continuations like git diff \\<NL>HEAD~1, which Claude Code emits routinely when formatting long invocations.

rtk discover -a -s 30 shows ~15 calls slipping past the hook this way in 30 days, almost all git, gh, find, i.e. commands with working handlers.

Fix

Add a small helper:

static ref LINE_CONTINUATION_RE: Regex =
    Regex::new(r\"[ \\t]*\\\\(?:\\r\\n|\\n)[ \\t]*\").unwrap();

fn collapse_line_continuations(s: &str) -> Cow<'_, str> {
    LINE_CONTINUATION_RE.replace_all(s, \" \")
}

Called before cmd.trim() in rewrite_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~1 collapses to git diff HEAD~1, not git diff HEAD~1).

The fast path returns Cow::Borrowed when 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 the Cow::Borrowed contract for the helper.

Results:

  • cargo test --bin rtk -- discover::registry → 238 passed; 0 failed (6 new + 232 existing).
  • End-to-end smoke test on macOS:
    $ rtk hook check $'\\\\\\ngit diff HEAD~1'
    rtk git diff HEAD~1
    exit=0
    
  • cargo fmt clean.

Notes

@CLAassistant

CLAassistant commented Apr 28, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@pszymkowiak pszymkowiak added bug Something isn't working effort-small Quelques heures, 1 fichier labels Apr 28, 2026
@pszymkowiak

Copy link
Copy Markdown
Collaborator

[w] wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

Fixes 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

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #1564


Analyzed automatically by wshm · This is an automated analysis, not a human review.

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

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

Comment thread src/discover/registry.rs Outdated
/// 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();

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.

Suggested change
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

@KuSh KuSh self-assigned this May 2, 2026
@SAY-5
SAY-5 force-pushed the fix/hook-line-continuation-1564 branch from e9ba852 to d1d1800 Compare May 2, 2026 18:22
@SAY-5

SAY-5 commented May 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Done, rebased onto current develop and force-pushed so the PR diff now contains only src/discover/registry.rs (92 insertions, 1 deletion). Will look at the regex suggestion in a separate commit.

@SAY-5

SAY-5 commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

Applied the regex suggestion: now uses r"(?m)[ \t\x0B\x0C]*\\\r?\n[ \t\x0B\x0C]*" (vertical tab, form feed, and simplified newline pattern). Cargo check and the targeted line-continuation test pass.

)

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.
@SAY-5
SAY-5 force-pushed the fix/hook-line-continuation-1564 branch from f18ea1a to 2387594 Compare May 25, 2026 03:19
@SAY-5

SAY-5 commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current develop (was 181 commits behind, now current). Three commits replayed cleanly.

@SAY-5

SAY-5 commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

Both items are done: the diff is now limited to src/discover/registry.rs and the regex suggestion is applied in the latest commit. CI is green. Happy for you to re-review.

Comment thread src/discover/registry.rs Outdated
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)

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.

please avoid including changes like that, that only adds noise.

Comment thread src/discover/registry.rs Outdated
Comment on lines +819 to +835
// 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) {

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.

This seems to be a rebase error. Please review

Comment thread src/discover/registry.rs Outdated
Comment on lines +1415 to +1420
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);

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.

Again, rebase error, which would have been avoided by refraining on out of scope changes

Comment thread src/discover/registry.rs Outdated
// 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", &[]),

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.

Doesn't compile after rebase. You should use rewrite_command_no_prefixes

@SAY-5
SAY-5 force-pushed the fix/hook-line-continuation-1564 branch from 2387594 to 0ec99de Compare May 25, 2026 13:16
@SAY-5

SAY-5 commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

Fixed all four points: removed the em-dash chore commit from history, corrected has_rtk_disabled_prefix to cmd_has_rtk_disabled_prefix (compile fix), and switched the 11 line-continuation test assertions to rewrite_command_no_prefixes. 1934 tests pass locally.

@KuSh
KuSh merged commit 39f044e into rtk-ai:develop May 25, 2026
11 checks passed
@aeppling aeppling mentioned this pull request May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working effort-small Quelques heures, 1 fichier

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hook matcher: leading backslash-newline defeats command rewrite

4 participants