← ClaudeAtlas

command-boundary-hook-matchinglisted

Pattern-match what a Bash command actually does in a PreToolUse hook without false-firing on mentions inside quoted args, commit messages, or echo strings. Use when building any hook that decides based on command semantics.
CarlosCaPe/octorato · ★ 5 · AI & Automation · score 73
Install: claude install-skill CarlosCaPe/octorato
# Command-Boundary Hook Matching ## Problem A naive `"gh pr merge" in command` check fires on: ```bash git commit -m "do not run gh pr merge 96 yet" # mention in commit message echo "about to run gh pr merge" # echo for pr in 95 96; do gh pr merge $pr; done # loop — fires twice, wrong context ``` None of these are the real invocation you want to intercept. The hook either over-fires (false positives trigger unnecessary blocks) or under-fires (quoted indirection evades detection). ## Fix — Quote-Aware Sub-Command Splitter Split the raw command string on **unquoted** shell separators only, then anchor the pattern at the **start** of each sub-command. ### Step 1 — Join continuations ```python command = command.replace("\\\n", " ") ``` ### Step 2 — Quote-aware split on unquoted separators Separators: `;` `&&` `||` `|` `\n` and grouping `(` `)` `{` `}`. ```python import re def _split_subcmds(cmd: str) -> list[str]: parts, buf, depth, in_sq, in_dq = [], [], 0, False, False i = 0 while i < len(cmd): c = cmd[i] if in_sq: buf.append(c) if c == "'": in_sq = False elif in_dq: buf.append(c) if c == '"' and (i == 0 or cmd[i-1] != '\\'): in_dq = False elif c == "'": in_sq = True; buf.append(c) elif c == '"': in_dq = True; buf.append(c) elif c in '({': depth += 1; buf.append(c)