2025-12-24 17:11:18 +09:00
|
|
|
# HOOKS KNOWLEDGE BASE
|
|
|
|
|
|
|
|
|
|
## OVERVIEW
|
2026-01-17 22:01:56 +09:00
|
|
|
|
|
|
|
|
31 lifecycle hooks intercepting/modifying agent behavior. Events: PreToolUse, PostToolUse, UserPromptSubmit, Stop, onSummarize.
|
2025-12-24 17:11:18 +09:00
|
|
|
|
|
|
|
|
## STRUCTURE
|
2026-01-17 22:01:56 +09:00
|
|
|
|
2025-12-24 17:11:18 +09:00
|
|
|
```
|
|
|
|
|
hooks/
|
2026-01-20 16:52:20 +09:00
|
|
|
├── atlas/ # Main orchestration & delegation (771 lines)
|
2026-01-17 22:01:56 +09:00
|
|
|
├── anthropic-context-window-limit-recovery/ # Auto-summarize at token limit
|
|
|
|
|
├── todo-continuation-enforcer.ts # Force TODO completion
|
|
|
|
|
├── ralph-loop/ # Self-referential dev loop until done
|
2026-01-22 22:48:50 +09:00
|
|
|
├── claude-code-hooks/ # settings.json hook compat layer (14 files) - see AGENTS.md
|
2026-01-17 22:01:56 +09:00
|
|
|
├── comment-checker/ # Prevents AI slop/excessive comments
|
|
|
|
|
├── auto-slash-command/ # Detects /command patterns
|
2025-12-24 17:11:18 +09:00
|
|
|
├── rules-injector/ # Conditional rules from .claude/rules/
|
2026-01-17 22:01:56 +09:00
|
|
|
├── directory-agents-injector/ # Auto-injects AGENTS.md files
|
|
|
|
|
├── directory-readme-injector/ # Auto-injects README.md files
|
|
|
|
|
├── preemptive-compaction/ # Triggers summary at 85% context
|
|
|
|
|
├── edit-error-recovery/ # Recovers from tool failures
|
2026-01-13 21:00:00 +09:00
|
|
|
├── thinking-block-validator/ # Ensures valid <thinking> format
|
|
|
|
|
├── context-window-monitor.ts # Reminds agents of remaining headroom
|
2026-01-17 22:01:56 +09:00
|
|
|
├── session-recovery/ # Auto-recovers from crashes
|
|
|
|
|
├── think-mode/ # Dynamic thinking budget
|
|
|
|
|
├── keyword-detector/ # ultrawork/search/analyze modes
|
2026-01-13 21:00:00 +09:00
|
|
|
├── background-notification/ # OS notification on task completion
|
2026-01-22 22:48:50 +09:00
|
|
|
├── prometheus-md-only/ # Enforces planner read-only mode
|
|
|
|
|
├── agent-usage-reminder/ # Reminds to use specialized agents
|
|
|
|
|
├── auto-update-checker/ # Checks for plugin updates
|
2026-01-17 22:01:56 +09:00
|
|
|
└── tool-output-truncator.ts # Prevents context bloat
|
2025-12-24 17:11:18 +09:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## HOOK EVENTS
|
2026-01-17 22:01:56 +09:00
|
|
|
|
|
|
|
|
| Event | Timing | Can Block | Use Case |
|
|
|
|
|
|-------|--------|-----------|----------|
|
|
|
|
|
| PreToolUse | Before tool | Yes | Validate/modify inputs, inject context |
|
|
|
|
|
| PostToolUse | After tool | No | Append warnings, truncate output |
|
|
|
|
|
| UserPromptSubmit | On prompt | Yes | Keyword detection, mode switching |
|
|
|
|
|
| Stop | Session idle | No | Auto-continue (todo-continuation, ralph-loop) |
|
|
|
|
|
| onSummarize | Compaction | No | Preserve critical state |
|
|
|
|
|
|
|
|
|
|
## EXECUTION ORDER
|
|
|
|
|
|
|
|
|
|
**chat.message**: keywordDetector → claudeCodeHooks → autoSlashCommand → startWork → ralphLoop
|
|
|
|
|
|
|
|
|
|
**tool.execute.before**: claudeCodeHooks → nonInteractiveEnv → commentChecker → directoryAgentsInjector → directoryReadmeInjector → rulesInjector
|
|
|
|
|
|
|
|
|
|
**tool.execute.after**: editErrorRecovery → delegateTaskRetry → commentChecker → toolOutputTruncator → emptyTaskResponseDetector → claudeCodeHooks
|
2026-01-02 10:42:38 +09:00
|
|
|
|
|
|
|
|
## HOW TO ADD
|
2026-01-17 22:01:56 +09:00
|
|
|
|
|
|
|
|
1. Create `src/hooks/name/` with `index.ts` exporting `createMyHook(ctx)`
|
|
|
|
|
2. Implement event handlers: `"tool.execute.before"`, `"tool.execute.after"`, etc.
|
|
|
|
|
3. Add hook name to `HookNameSchema` in `src/config/schema.ts`
|
|
|
|
|
4. Register in `src/index.ts`:
|
|
|
|
|
```typescript
|
|
|
|
|
const myHook = isHookEnabled("my-hook") ? createMyHook(ctx) : null
|
|
|
|
|
// Add to event handlers
|
|
|
|
|
```
|
2025-12-24 17:11:18 +09:00
|
|
|
|
2026-01-02 10:42:38 +09:00
|
|
|
## PATTERNS
|
2026-01-17 22:01:56 +09:00
|
|
|
|
|
|
|
|
- **Session-scoped state**: `Map<sessionID, Set<string>>` for tracking per-session
|
|
|
|
|
- **Conditional execution**: Check `input.tool` before processing
|
|
|
|
|
- **Output modification**: `output.output += "\n${REMINDER}"` to append context
|
|
|
|
|
- **Async state**: Use promises for CLI path resolution, cache results
|
2025-12-24 17:11:18 +09:00
|
|
|
|
2026-01-02 10:42:38 +09:00
|
|
|
## ANTI-PATTERNS
|
2026-01-17 22:01:56 +09:00
|
|
|
|
|
|
|
|
- **Blocking non-critical**: Use PostToolUse warnings instead of PreToolUse blocks
|
|
|
|
|
- **Heavy computation**: Keep PreToolUse light - slows every tool call
|
|
|
|
|
- **Redundant injection**: Track injected files to prevent duplicates
|
|
|
|
|
- **Verbose output**: Keep hook messages technical, brief
|