Skip to content

Commit 067c74b

Browse files
committed
fix(toolkit): robust ls/grep argument extraction in claim-verification hook (DOJ-4872)
Round-2 bot review at 4/5 confidence flagged 2 P3 polish items on the suggester's argument extraction. Both addressed; suggestions applied verbatim with explanatory comments. P3 — hooks/pre-tool-use-claim-verification.sh:145 (ls path extraction) - Old: sed prefix-strip + awk first-token, which fails on commands like `ls -la` (no path arg, trailing flags) — sed regex `-flags` requires a trailing space, so the flags leak through and `-la` ends up as the "path arg" in the suggested replacement. - New: awk-only nested loop that locates the literal `ls` token, then walks forward skipping any token starting with `-`, and emits the first non-flag token. Empty when only flags are present (correct), so the suggester falls through to the `<path>` placeholder. P3 — hooks/pre-tool-use-claim-verification.sh:158 (grep pattern extraction) - Old: awk fallback matched the first `-FLAGS` token and emitted the immediately following token, which on `grep -i -r "Symbol" src/` is `-r` — wrong. - New: nested awk loop that, after finding the flag token, walks forward skipping additional flag tokens before emitting the first non-flag one. Correctly isolates `Symbol` for `grep -i -r "Symbol"`, still correct for the single-flag-block form `grep -rn "Symbol"`. Verification: - shellcheck → 0 issues - "ls -la" → suggester emits <path> placeholder (was: `-la`) - "ls src/foo.tsx" → suggester emits `src/foo.tsx` (regression) - "grep -i -r \"Symbol\" src/" → emits "Symbol" (was: `-r`) - "grep -rn \"Symbol\" src/" → emits "Symbol" (regression) - "echo hello" → silent (regression) Created by Claude Code on behalf of @lapc506.
1 parent 31f10c9 commit 067c74b

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

hooks/pre-tool-use-claim-verification.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,11 @@ fi
154154
# Best-effort suggested replacement, branched by detected op.
155155
SUGGESTED=""
156156
if printf '%s' "$COMMAND" | grep -qE '(^|[;&|][[:space:]]*)ls([[:space:]]|$)'; then
157-
# Extract the path argument (best-effort, first non-flag token after ls).
157+
# Extract the path argument: first non-flag token AFTER `ls`. Awk-only
158+
# avoids the trailing-flag-without-space edge case (`ls -la` would have
159+
# leaked `-la` through the sed prefix-strip).
158160
path_arg="$(printf '%s' "$COMMAND" \
159-
| sed -E 's/^.*ls[[:space:]]+(-[A-Za-z]+[[:space:]]+)*//' \
160-
| awk '{print $1}')"
161+
| awk '{for(i=1;i<=NF;i++) if ($i == "ls") {for(j=i+1;j<=NF;j++) if ($j !~ /^-/) {print $j; exit}}}')"
161162
if [ -n "$path_arg" ]; then
162163
SUGGESTED="git fetch origin <branch> --quiet && git ls-tree origin/<branch> -- $path_arg"
163164
else
@@ -169,8 +170,10 @@ elif printf '%s' "$COMMAND" | grep -qE 'grep[[:space:]]+-[rRnliN]+'; then
169170
| head -1 \
170171
| sed -E 's/^grep[[:space:]]+-[rRnliN]+[[:space:]]+"(.+)"$/\1/')"
171172
if [ -z "$pattern_arg" ]; then
173+
# Nested awk loop: skip subsequent flag tokens so multi-flag forms
174+
# like `grep -i -r "Symbol" src/` don't mis-extract `-r` as the pattern.
172175
pattern_arg="$(printf '%s' "$COMMAND" \
173-
| awk '{for(i=1;i<=NF;i++) if ($i ~ /^-[rRnliN]+$/) {print $(i+1); exit}}' \
176+
| awk '{for(i=1;i<=NF;i++) if ($i ~ /^-[rRnliN]+$/) {for(j=i+1;j<=NF;j++) if ($j !~ /^-/) {print $j; exit}}}' \
174177
| tr -d '"')"
175178
fi
176179
if [ -n "$pattern_arg" ]; then

0 commit comments

Comments
 (0)