Opt-in Claude Code hooks: refuse attribution lines in commits and PRs, refuse staging COMMITS.md, warn on commits in a main checkout, refuse a subagent call…

Opt-in Claude Code hooks: refuse attribution lines in commits and PRs, refuse staging COMMITS.md, warn on commits in a main checkout, refuse a subagent call that would silently inherit the session's model, restart idle sessions onto an updated Claude Code, and route work to cheaper models while the usage allowance burns too fast.
/plugin marketplace add ajbarea/techne
/plugin install phylax@techne
Then run /phylax:setup. Install, first use and troubleshooting: https://ajbarea.github.io/techne/plugins/phylax/. Setup runbook: AGENTS.md.
hooks/register.ts 85 lines1import type { PluginOptions, Register } from 'claude-code'
2
3// The guard options, as the manifest's userConfig names them.
4const GUARDS = ['block_attribution_trailers', 'block_commits_md', 'warn_main_checkout_commit'] as const
5// The guards that refuse a command; warn_main_checkout_commit only warns.
6const BLOCKING = ['block_attribution_trailers', 'block_commits_md'] as const
7// git_guards.py looks no further at a command without one of these words.
8const GIT_OR_GH = /\b(git|gh)\b/
9// A commit makes at most six git calls of 5 s each inside git_guards.py.
10const GUARD_TIMEOUT_MS = 40_000
11
12type Verdict = { deny?: string; warning?: string }
13
14const isOn = (options: PluginOptions, key: string): boolean => options[key] === true
15
16// Reads git_guards.py's PreToolUse JSON: a deny reason, or a warning for the model.
17export function readVerdict(stdout: string): Verdict {
18 if (stdout.trim() === '') return {}
19 const out = JSON.parse(stdout) as {
20 hookSpecificOutput?: { permissionDecision?: string; permissionDecisionReason?: string; additionalContext?: string }
21 }
22 const spec = out.hookSpecificOutput ?? {}
23 if (spec.permissionDecision === 'deny') return { deny: spec.permissionDecisionReason ?? 'phylax refused this command.' }
24 if (spec.additionalContext) return { warning: spec.additionalContext }
25 return {}
26}
27
28export const register: Register = (on, options) => {
29 // Claude Code reads the commit trailer and PR footer from here, so blanking them stops the
30 // lines before Claude writes them; the Bash guard below still catches one written by hand.
31 if (isOn(options, 'block_attribution_trailers')) {
32 on('attribution.text', { kind: ['commit', 'pr'] }, () => ({ text: '' }))
33 }
34
35 if (!GUARDS.some(key => isOn(options, key))) return
36 const blocking = BLOCKING.some(key => isOn(options, key))
37 const failed = (why: string) => ({
38 deny:
39 `phylax: the git guard ${why}, so this command was not run. Retry it in a simpler form, ` +
40 'or switch the guard off in /config.',
41 })
42
43 on('tool.call', { tool: 'Bash' }, async ($, e, next) => {
44 // A subagent's call goes to the hooks.json PreToolUse fallback, which gets its own cwd;
45 // $.session.cwd() is the main thread's.
46 if (e.agentId !== undefined || !GIT_OR_GH.test(e.command)) return next(e)
47 const cwd = await $.session.cwd()
48 // The guard takes the folder from stdin and refuses itself when the folder a guarded
49 // command runs in is gone, so python3 starts from the plugin: a removed worktree as
50 // the session folder would fail the spawn as if python3 were missing.
51 const run = await $.process.run(['python3', `${$.plugin.root}/hooks/git_guards.py`], {
52 cwd: $.plugin.root,
53 stdin: JSON.stringify({ hook_event_name: 'PreToolUse', tool_name: 'Bash', tool_input: { command: e.command }, cwd }),
54 env: Object.fromEntries(GUARDS.map(key => [`CLAUDE_PLUGIN_OPTION_${key.toUpperCase()}`, String(isOn(options, key))])),
55 timeoutMs: GUARD_TIMEOUT_MS,
56 })
57 const verdict = readVerdict(run.stdout)
58 if (verdict.deny !== undefined) return { deny: verdict.deny }
59 if (run.exitCode !== 0) {
60 const reason = run.stderr.trim().split('\n')[0] || `exit ${run.exitCode}`
61 if (blocking) return failed(`failed (${reason})`)
62 $.ui.log(`phylax: the git guard failed (${reason}); the command ran unchecked.`)
63 }
64 // The hooks.json fallback runs inside next(e). Naming this call there makes it stand down
65 // for this call alone: any call this hook did not pass, it still checks.
66 if (e.tool_use_id !== undefined) {
67 const stamped = await $.env.set('PHYLAX_GUARD_CHECKED', e.tool_use_id)
68 // A held write only costs a second check: the fallback runs the guard on this call too.
69 if (stamped.deny !== undefined) $.ui.log(`phylax: ${stamped.deny}; the fallback checks this call again.`, { to: 'debug' })
70 }
71 let result
72 try {
73 result = await next(e)
74 } finally {
75 await $.env.set('PHYLAX_GUARD_CHECKED', undefined)
76 }
77 if (verdict.warning === undefined || result.deny !== undefined) return result
78 $.ui.log(verdict.warning)
79 return { ...result, context: [...(result.context ?? []), verdict.warning] }
80 }).catch(($, e, next) => {
81 if (next.called || !blocking) return next(e)
82 return failed(`failed (${next.error.message})`)
83 })
84}
85