Skip to content

feat(hook-augment): augment Bash tool searches via graph - #1337

Open
abendrothj wants to merge 2 commits into
DeusData:mainfrom
abendrothj:bash-tool-hook-augment
Open

feat(hook-augment): augment Bash tool searches via graph#1337
abendrothj wants to merge 2 commits into
DeusData:mainfrom
abendrothj:bash-tool-hook-augment

Conversation

@abendrothj

Copy link
Copy Markdown

Fixes #1082.

What

Claude Code frequently runs searches through the Bash tool (rg -n Foo ., grep -rn Foo ., etc.) rather than the native Grep/Glob tools. Because hook-augment's PreToolUse matcher only covered Grep and Glob, these searches silently bypassed graph augmentation.

This PR adds detection directly in hook_augment itself (Q1 from my earlier comment — I went with the former approach since it reuses the existing search_graph path without duplicating resolver logic). Pattern extraction happens via a small shell tokenizer and flag parser; the result feeds into the same ha_extract_tokensearch_graph path Grep already uses.

Approach

ha_tokenize — minimal shell tokenizer: whitespace split with '…'/"…" quoting and backslash handling. No evaluation, no expansion.

ha_parse_bash_search_pattern — pure string parser, never touches the command:

  • Strips leading VAR=value env assignments
  • Strips wrappers: env, nice, time, command, rtk, tokf run
  • Identifies the binary (grep/egrep/fgrep, rg, ag, ack, ugrep/ug, git grep)
  • Walks flags: bails on -f/--file (pattern file), bails on multiple -e, handles embedded (-ePATTERN) and space-separated (-e PATTERN) forms
  • Per-binary value-taking flag tables so e.g. grep -A 5 PATTERN correctly skips 5
  • rtk grep -l <N>: treats -l as value-taking (consumes N, arrives at PATTERN)

ha_tool_event_supportedBash added alongside Grep/Glob for PreToolUse in HA_DIALECT_EVENT.

ha_process — Bash branch extracts pattern from tool_input.command; everything after is identical to the Grep path.

Scope (Q2)

grep/egrep/fgrep, rg, ag, ack, ugrep/ug, git grep — plus the wrapper handling dergachoff noted in the thread.

Testing

cli_hook_augment_bash_pattern_extractor in tests/test_cli.c covers: common invocation forms, value-taking flag skipping, env-var prefixes, all wrappers, the rtk grep -l <N> case, bail-out cases (-f, multiple -e, non-search binary, empty/NULL), and -- end-of-flags. Verified clean compile and production binary build on macOS; full test run blocked by an ASan/dyld hang on macOS 26 pre-release — CI should cover it.

@abendrothj
abendrothj requested a review from DeusData as a code owner July 29, 2026 12:19
@abendrothj
abendrothj force-pushed the bash-tool-hook-augment branch 2 times, most recently from 20a8cc1 to f126bf7 Compare July 29, 2026 23:15
Bash-based searches (rg, grep, ag, ack, ugrep, git grep) bypass
hook-augment because the PreToolUse matcher only covers Grep/Glob.
Detection lives in hook_augment itself, reusing the existing
search_graph path without duplicating the resolver.

- ha_tokenize: minimal shell tokenizer (quote/backslash handling,
  no evaluation)
- ha_parse_bash_search_pattern: strips VAR=val prefixes and
  wrappers (env, nice, time, command, rtk, tokf run), identifies
  the binary, walks flags, bails on -f/--file and multiple -e,
  handles rtk grep -l <N> as value-taking
- ha_tool_event_supported: Bash added alongside Grep/Glob for
  PreToolUse in HA_DIALECT_EVENT
- ha_process: Bash branch routes tool_input.command through the
  extractor before the shared ha_extract_token + search_graph path

Binaries covered: grep/egrep/fgrep, rg, ag, ack, ugrep/ug, git grep
Wrappers covered: env, nice, time, command, rtk, tokf run

Closes DeusData#1082

Signed-off-by: Jake Abendroth <contact@jakea.net>
Signed-off-by: Jake Abendroth <contact@jakea.net>
@abendrothj
abendrothj force-pushed the bash-tool-hook-augment branch from dbbd5d9 to eb12d6d Compare July 29, 2026 23:16
@abendrothj

Copy link
Copy Markdown
Author

The cppcheck failure is pre-existing in src/main.c lines 283 and 2049; introduced by a recent merge to main, not by this PR.

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.

Claude Code Bash searches bypass codebase-memory hook augmentation

1 participant