Plan: Add Claude Code Hooks Suite to claude-agent-runner
Context
The agent runner currently has only 2 hooks (block-destructive.sh, scope-guard.sh) — both PreToolUse:Bash guards. Claude Code supports 16 hook event types with rich control (blocking, context injection, input rewriting, async execution). This plan adds 9 new hooks across 7 event types to improve agent safety, observability, and self-correction. It also fixes the broken settings.json registration format in setup.sh.
New Hooks (9 files)
1. hooks/stop-gate.sh — Stop event
Gate: CLAUDE_AGENT_MODE=1
Checks stop_hook_active to prevent infinite loops
Parses transcript JSONL for evidence of CI/test runs (ci-gate, npm test, pytest, cargo test, vitest, jest, go test)
If no test evidence found → {"decision": "block", "reason": "Run CI checks before finishing"}
Falls back gracefully (allows stop) if transcript unreadable
Agent runner creates worktrees via git worktree add directly — these hooks only fire for Claude Code's built-in worktree feature, so they're not applicable.
Modified Files (2)
setup.sh — Fix registration format + register all hooks
Replace flat-format registration (lines 46-63) with register_hook() function using the correct nested format: hooks.<EventName>[{matcher, hooks:[{type, command}]}]
Register all 11 hooks (2 existing + 9 new) with correct event types and matchers
Idempotent: check for existing command path before adding
Update summary section to list all hooks
bin/claude-agent-runner — Export agent identity env vars
At all 4 claude invocation sites (lines ~795, 900, 1315, 1748), add exports:
After setup.sh changes: run twice, verify no duplicate registrations in ~/.claude/settings.json.
If you need specific details from before exiting plan mode (like exact code snippets, error messages, or content you generated), read the full transcript at: /Users/lswhiteh/.claude/projects/-Users-lswhiteh-Work-claude-agent-runner/73abad2c-f532-4ae6-8bb3-fece63c41856.jsonl
Assistant
I'll implement this plan in phases. Let me start by reading the existing files to understand the current state.
Assistant
Now let me find the claude invocation sites in the agent runner.
Assistant
Let me check the variable names available at each invocation site.
Assistant
Good. Now I have enough context. Let me create all the hooks and modifications. I'll work in phases as planned.
Phase 3 — creating session-context.sh and exporting env vars in the agent runner.
Assistant
Now let me add the env var exports at the 4 claude invocation sites. Let me re-read the exact context around each site to make precise edits.
Assistant
Now let me check what ISSUE_ID variable is available at the auto_fix_loop site.
Assistant
The auto_fix_loop doesn't receive ISSUE_ID. Let me check the other sites more carefully. For the auto_fix_loop, IDENTIFIER is available (e.g., "ENG-123"). Let me check the orchestrator and scoped worker sites.
Assistant
Now I need to check the main loop site (line ~1748) for available variables.
Assistant
Good. Now I'll add the env var exports at all 4 sites. Let me also check the BRANCH variable name in the scoped worker context.
Assistant
The scoped worker (run_scoped_worker) doesn't have BRANCH or ISSUE_ID as locals — it gets WORKTREE, REPO_NAME, IDENTIFIER, TITLE, DESCRIPTION. The orchestrator has ISSUE_ID. The main loop (line ~1748) has BRANCH, IDENTIFIER, REPO_NAME, ISSUE_ID, WORKTREE. Let me now make the edits.
Assistant
Phase 4 — overhauling setup.sh hook registration. Let me first check what the correct Claude Code settings.json hook format looks like.
Assistant
Now I have the correct format. Let me rewrite setup.sh.
Assistant
Now update the summary section.
Assistant
Phase 5 — update CLAUDE.md with the new hooks.
Assistant
Now update the hook documentation sections.
Assistant
Now update the editing guidelines to cover new hooks.
Assistant
Now let me make all hook files executable and run basic validation.
Assistant
Now let me test a few hooks to verify they work correctly.
Assistant
The command-rewriter isn't inserting --save-exact. Let me check.
Assistant
The issue is that macOS sed -E doesn't handle \s. Let me fix.
Assistant
Now let me test a few more hooks.
Assistant
All hooks working. Let me clean up temp files and verify the setup.sh is valid.
Assistant
Let me verify bash syntax on all files.
Assistant
All 11 hooks and setup.sh pass syntax checks. Let me verify the agent runner also passes.
Assistant
Let me do a quick test of setup.sh idempotency with a mock settings.json.
Assistant
All 11 hooks register correctly across 8 event types, and the second run is fully idempotent.
Here's a summary of everything implemented:
9 new hooks created:
Hook
Event
Purpose
rtk-failure-hint.sh
PostToolUseFailure:Bash
Suggests rtk proxy on RTK failures
command-rewriter.sh
PreToolUse:Bash
Adds --save-exact to npm install
command-budget.sh
PreToolUse:Bash
Denies after N commands (stuck agent detection)
pre-compact.sh
PreCompact
Saves git state snapshot before compaction
subagent-collector.sh
SubagentStop
Logs subagent results to JSONL
auto-lint.sh
PostToolUse:Edit|Write
Async linting on file changes
stop-gate.sh
Stop
Blocks finish without test evidence
task-validator.sh
TaskCompleted
Blocks task completion without tests
session-context.sh
SessionStart
Injects agent identity context
2 modified files:
bin/claude-agent-runner — Exports CLAUDE_AGENT_ISSUE_ID, CLAUDE_AGENT_REPO, CLAUDE_AGENT_BRANCH, CLAUDE_AGENT_WORKTREE at all 4 claude invocation sites
setup.sh — Replaced broken flat-format registration with correct nested hooks.<Event>[{matcher, hooks:[{type, command}]}] format, registers all 11 hooks idempotently, migrates old format automatically
Updated:CLAUDE.md with full documentation for all hooks and editing guidelines.
User
push
Assistant
Pushed to origin/main — commit 4304a58.
User
make PRs draft PRs, humans will mark as ready for review
Assistant
Three gh pr create calls. Adding --draft to each.
Assistant
Done. All 3 gh pr create calls now include --draft. Pushed as 2d83b55.
User
PR names should have the ticket but also brief description
User
[Request interrupted by user]
User
branch names, that is
Assistant
I need to slugify the title into the branch name. Both sites have TITLE available.
Assistant
Now verify the slugify works and check syntax.
Assistant
Done. Branch names now follow agent/ENG-123-add-user-auth format — slugified, lowercase, truncated to 50 chars. Pushed as 0185edd.
User
Do we have a way to visualize/trace what the agent did/decided to do at any given step?
User
[Request interrupted by user]
User
Especially with the more complex orchestration tickets that would be useful
Assistant
Let me check what observability already exists.
Assistant
Current state: there are text log files, Linear comments, Slack notifications, and per-agent report files — but no structured timeline or trace you can visualize. For orchestration especially, you'd want to see the decomposition decision, subtask creation, worker progress, CI results, and PR creation as a unified timeline.
What would be most useful to you?
Structured event log (JSONL) — each decision/action emitted as {timestamp, event, actor, details}, queryable with jq. Could add a bin/agent-trace CLI to filter/format it.
Linear timeline — richer Linear comments at each step (already partial), so the full story is visible on the ticket itself.
HTML report — a generated HTML page per run with a visual timeline (collapsible sections for decomposition, each worker, CI, PR).
The JSONL approach is the lightest to implement and composes well (you can build HTML/dashboards on top later). Want me to go with that, or do you have a preference?