Stops the Bash shapes that fail in corgi runs (zsh set -- $pair, unquoted --flag=*.ts, rm -rf on corgi worktrees) and adds the corgi fix to errors it knows

A Claude Code mod that stops the Bash shapes corgi runs lose turns to, and hands the model the corgi fix:
set -- $pair - zsh does not split it, so the second word arrives empty. Refused, with read -r a b <<< "$pair".grep --include=*.ts - zsh stops on "no matches found". The glob is quoted before it runs (--include='*.ts'), which bash reads the same way.rm -rf /tmp/corgi-wt/... or /tmp/corgi-review/... - refused in a hardened workspace anyway. Refused, with the fresh-path recipe.corgi agent watch pr threads.gh pr checks right after a push ("no checks reported") - the result carries the wait-for-checks loop./plugin install corgi-guard --marketplace Andriiklymiuk/corgihooks/register.ts 23 lines1import type { Register } from 'claude-code'
2
3import { hint, quoteFlagGlobs, refusal } from './rules'
4
5export const register: Register = on => {
6 on('tool.call', { tool: 'Bash' }, async ($, e, next) => {
7 const why = refusal(e.command)
8 if (why !== undefined) {
9 return { deny: `corgi-guard: ${why}` }
10 }
11 const command = quoteFlagGlobs(e.command)
12 const ran = await next(command === e.command ? e : { ...e, command })
13 if (ran.deny !== undefined) {
14 return ran
15 }
16 const extra = hint(e.command, ran.text ?? '')
17 if (extra === undefined) {
18 return ran
19 }
20 return { ...ran, context: [...(ran.context ?? []), `corgi-guard: ${extra}`] }
21 }).catch(($, e, next) => next(e))
22}
23hooks/rules.ts 63 lines1const unsplitSet = /\bset\s+--\s+"?\$(?!\{=)[A-Za-z_]\w*/
2const rmCorgiTree = /\brm\s+-(?:[a-zA-Z]*r[a-zA-Z]*f|[a-zA-Z]*f[a-zA-Z]*r)[a-zA-Z]*\s+[^;&|]*\/tmp\/corgi-(?:wt|review)\b/
3
4const heredocBody = /<<-?\s*(['"]?)(\w+)\1([^\n]*)\n[\s\S]*?\n\s*\2[ \t]*(?=\n|$)/g
5const singleQuoted = /'[^']*'/g
6
7export function shellOnly(command: string): string {
8 return command.replace(heredocBody, '<<$2$3').replace(singleQuoted, "''")
9}
10
11export function refusal(raw: string): string | undefined {
12 const command = shellOnly(raw)
13 if (unsplitSet.test(command)) {
14 return 'zsh does not split `$var` in `set -- $var`, so the second word arrives empty. ' +
15 'Use `read -r repo n <<< "$pair"` (bash and zsh), or loop over `repo:n` and take `${p%%:*}` / `${p##*:}`.'
16 }
17 if (rmCorgiTree.test(command)) {
18 return '`rm -rf` is refused in a hardened workspace. Take a fresh path instead: ' +
19 '`W=/tmp/corgi-wt/<id>; [ -e "$W" ] && W="$W-$(date +%s)"`, and `git -C <repo> worktree remove "$W"` (no --force) when done.'
20 }
21 return undefined
22}
23
24const flagGlob = /(?<=\s)(--[A-Za-z][\w-]*=)([^\s'"`$;&|(){}<>]*[*?[][^\s'"`$;&|(){}<>]*)(?=[\s;&|)]|$)/g
25
26export function quoteFlagGlobs(command: string): string {
27 if (command.includes('<<')) {
28 return command
29 }
30 const bare: boolean[] = []
31 let quote = ''
32 for (let i = 0; i < command.length; i++) {
33 const c = command[i]
34 bare.push(quote === '')
35 if (c === '\\' && quote !== "'") {
36 bare.push(false)
37 i++
38 } else if (quote === '' && (c === "'" || c === '"')) {
39 quote = c
40 } else if (c === quote) {
41 quote = ''
42 }
43 }
44 return command.replace(flagGlob, (word, flag: string, glob: string, at: number) =>
45 bare[at] ? `${flag}'${glob}'` : word)
46}
47
48export function hint(command: string, text: string): string | undefined {
49 if (/graphql/.test(command) && /Could not coerce value|provided invalid value/.test(text)) {
50 return 'A GraphQL variable arrived empty. For review threads, `corgi agent watch pr threads <url> <url>...` ' +
51 'reads every open thread of every PR/MR with the thread id and reply-to id, GitHub or GitLab, in one call.'
52 }
53 if (/\bgh\s+pr\s+checks\b/.test(command) && /no checks reported/.test(text)) {
54 return 'The checks are not registered yet right after a push. Wait for them inside one background command: ' +
55 '`until [ "$(gh pr checks <n> --json name -q length 2>/dev/null)" -gt 0 ] 2>/dev/null; do sleep 10; done; gh pr checks <n> --watch --fail-fast`.'
56 }
57 if (/\bcorgi\b.*\bchat\b/.test(command) && /token_revoked|invalid_auth|account_inactive/.test(text)) {
58 return 'The stored Slack token is dead, so nothing was posted. Tell the user in one line, with the text to paste themselves, ' +
59 'and the fix: `corgi agent watch auth slack --token <new token>` (`corgi agent doctor` names which token).'
60 }
61 return undefined
62}
63