2026-02-23 20:39:58 +08:00
# Agent Hooks Setup
If your AI coding assistant supports lifecycle hooks, configure these for this repo.
## Recommended Hooks
| Hook | Command | Purpose |
|---|---|---|
2026-03-17 19:58:30 +08:00
| `afterFileEdit` | `scripts/agent-hooks/format.sh` | Auto-format files after AI edits |
2026-03-19 13:41:03 +08:00
| `afterFileEdit` | `scripts/agent-hooks/yarn-install.sh` | Run `corepack yarn install` when `package.json` changes |
2026-04-15 13:31:13 +07:00
| `afterFileEdit` | `scripts/agent-hooks/react-pattern-review.sh` | When a diff adds `useEffect` /memo primitives in `src/` , remind the agent to reconsider with the React review skills |
2026-03-17 19:58:30 +08:00
| `stop` | `scripts/agent-hooks/sync-git-branches.sh` | Prune stale refs and delete integrated temporary task branches |
2026-04-15 13:31:13 +07:00
| `stop` | `scripts/agent-hooks/react-pattern-review.sh` | Re-scan the current diff for new React effects/memos before the final verify gate |
2026-03-17 19:58:30 +08:00
| `stop` | `scripts/agent-hooks/verify.sh` | Hard-gate build, lint, and type-check; keep `yarn audit` informational |
2026-02-23 20:39:58 +08:00
## Why
- Consistent formatting
- Lockfile stays in sync
2026-04-15 13:31:13 +07:00
- New `useEffect` /memo additions get an explicit second look before the agent finishes
2026-02-23 20:39:58 +08:00
- Build/lint/type issues caught early
2026-03-19 13:41:03 +08:00
- Security visibility via `corepack yarn npm audit`
2026-04-23 14:14:55 +07:00
- One shared hook implementation for Codex, Cursor, and Claude
2026-03-17 19:58:30 +08:00
- Temporary task branches stay aligned with the repo's worktree workflow
2026-02-23 20:39:58 +08:00
## Example Hook Scripts
### Format Hook
```bash
#!/bin/bash
# Auto-format JS/TS files after AI edits
# Hook receives JSON via stdin with file_path
input = $( cat)
file_path = $( echo " $input " | grep -o '"file_path"[[:space:]]*:[[:space:]]*"[^"]*"' | sed 's/.*:.*"\([^"]*\)"/\1/' )
case " $file_path " in
*.js| *.ts| *.tsx| *.mjs) npx oxfmt " $file_path " 2>/dev/null ;;
esac
exit 0
```
### Verify Hook
```bash
#!/bin/bash
# Run build, lint, type-check, and security audit when agent finishes
cat > /dev/null # consume stdin
2026-03-17 19:58:30 +08:00
status = 0
2026-03-19 13:41:03 +08:00
corepack yarn build || status = 1
corepack yarn lint || status = 1
corepack yarn type-check || status = 1
echo "=== corepack yarn npm audit ===" && ( corepack yarn npm audit || true ) # informational
2026-03-17 19:58:30 +08:00
exit $status
2026-02-23 20:39:58 +08:00
```
2026-03-19 13:41:03 +08:00
By default, `scripts/agent-hooks/verify.sh` exits non-zero when `corepack yarn build` , `corepack yarn lint` , or `corepack yarn type-check` fails. Set `AGENT_VERIFY_MODE=advisory` only when you intentionally need signal from a broken tree without blocking the hook.
2026-03-17 19:58:30 +08:00
2026-04-10 14:31:49 +07:00
Lifecycle hooks do not replace manual browser verification. For UI or visual changes, still run `playwright-cli` checks across `chrome` , `firefox` , and `webkit` , plus a mobile viewport flow in each engine when responsiveness or touch behavior changed.
2026-02-23 20:39:58 +08:00
### Yarn Install Hook
```bash
#!/bin/bash
2026-03-19 13:41:03 +08:00
# Run Corepack-managed Yarn install when package.json is changed
2026-02-23 20:39:58 +08:00
# Hook receives JSON via stdin with file_path
input = $( cat)
file_path = $( echo " $input " | grep -o '"file_path"[[:space:]]*:[[:space:]]*"[^"]*"' | sed 's/.*:.*"\([^"]*\)"/\1/' )
if [ -z " $file_path " ] ; then
exit 0
fi
if [ " $file_path " = "package.json" ] ; then
cd " $( dirname " $0 " ) /../.." || exit 0
2026-03-19 13:41:03 +08:00
echo "package.json changed - running corepack yarn install to update yarn.lock..."
corepack yarn install
2026-02-23 20:39:58 +08:00
fi
exit 0
```
Configure hook wiring according to your agent tool docs (`hooks.json` , equivalent, etc.).
2026-03-17 19:58:30 +08:00
2026-04-23 14:14:55 +07:00
In this repo, `.codex/hooks/*.sh` , `.cursor/hooks/*.sh` , and `.claude/hooks/*.sh` should stay as thin wrappers that delegate to the shared implementations under `scripts/agent-hooks/` . Harness-specific startup hooks such as Claude's `SessionStart` can live alongside those wrappers when the other harnesses do not have an equivalent entry point.