createXXXHook(deps) → HookFunction.
Hook Tiers
Hooks are composed in three layers:Core Hooks
37 hooks: Session (23), Tool Guard (10), Transform (4)
Continuation Hooks
7 hooks for background tasks and boulder sessions
Skill Hooks
2 hooks for skill system integration
Hook Events
Hooks attach to OpenCode events:chat.message- First message in sessionchat.params- Model parameters before API callevent- Session lifecycle (created, deleted, idle, error)tool.execute.before- Before tool executiontool.execute.after- After tool executionexperimental.chat.messages.transform- Message transformation
Session Hooks (23)
Defined insrc/plugin/hooks/create-session-hooks.ts
Context Management (5)
Context Management (5)
context-window-monitor
Event:session.idlePurpose: Track context window usage percentage and warn when approaching limits
preemptive-compaction
Event:session.idlePurpose: Trigger compaction before hitting hard context limit
Config: Requires
experimental.preemptive_compaction: trueanthropic-context-window-limit-recovery
Event:session.errorPurpose: Multi-strategy recovery (truncation, compaction, summarization) when hitting context limitsFiles: 31 files, ~2232 LOC
compaction-context-injector
Event:session.compactedPurpose: Re-inject AGENTS.md/README.md after compaction
compaction-todo-preserver
Event:session.compactedPurpose: Preserve todo items through compaction process
Model & Runtime (5)
Model & Runtime (5)
think-mode
Event:chat.paramsPurpose: Dynamic thinking budget adjustment based on task complexity
Switches: Extended thinking mode for complex tasks
model-fallback
Event:chat.paramsPurpose: Provider-level model fallback on API errors
Chain: Claude → OpenAI → Gemini → Copilot → OpenCode Zen → Z.ai → Kimi
runtime-fallback
Event:eventPurpose: Auto-switch models on API provider errors
anthropic-effort
Event:chat.paramsPurpose: Adjust Anthropic reasoning effort level
no-sisyphus-gpt
Event:chat.messagePurpose: Block Sisyphus from using GPT models (shows toast warning)
no-hephaestus-non-gpt
Event:chat.messagePurpose: Block Hephaestus from using non-GPT models
Session Recovery (3)
Session Recovery (3)
session-recovery
Event:session.errorPurpose: Auto-recover from crashes and API errors
Strategies: Empty content recovery, thinking block order fix, tool result missingFiles: 30 files, ~1800 LOC
edit-error-recovery
Event:tool.execute.afterPurpose: Retry failed file edits with corrected parameters
json-error-recovery
Event:tool.execute.afterPurpose: Detect JSON parse errors, inject correction reminder
Task Management (6)
Task Management (6)
todo-continuation-enforcer
Event:session.idlePurpose: Boulder mechanism - forces agent to continue when todos remain incomplete
Behavior: 2s countdown toast → continuation injection
Backoff: Exponential backoff: 30s base, ×2 per failure, max 5 consecutive failures then 5min pauseFiles: 13 files, ~2061 LOC
atlas
Event:eventPurpose: Master orchestrator for boulder/background sessions
Gates: Session type → abort check → failure count → background tasks → agent match → plan completeness → cooldown (5s)Files: 17 files, ~1976 LOC
unstable-agent-babysitter
Event:session.idlePurpose: Monitor unstable agent behavior across sessions
delegate-task-retry
Event:tool.execute.afterPurpose: Retry failed task delegations
task-resume-info
Event:chat.messagePurpose: Inject task context when resuming cancelled tasks
tasks-todowrite-disabler
Event:tool.execute.beforePurpose: Disable TodoWrite when new task system is active
Developer Experience (9)
Developer Experience (9)
session-notification
Event:session.idlePurpose: OS notifications on session completion
Config:
notification.force_enable to override external pluginsbackground-notification
Event:eventPurpose: Background task completion notifications
auto-update-checker
Event:session.createdPurpose: Check npm for plugin updates
Files: Checks weekly, shows toast on new version
agent-usage-reminder
Event:chat.messagePurpose: Remind about available specialized agents
start-work
Event:chat.messagePurpose:
/start-work command handler for Sisyphus work sessionsralph-loop
Event:eventPurpose: Self-referential dev loop via
/ralph-loop commandState: Persisted in
.sisyphus/ralph-loop.local.mdCompletion: Detects
<promise>DONE</promise> in AI outputLimit: Max 100 iterations (default)Files: 14 files, ~1687 LOC
non-interactive-env
Event:chat.messagePurpose: Adjust behavior for non-TTY environments (
run command)interactive-bash-session
Event:tool.executePurpose: Tmux session management for interactive tools
question-label-truncator
Event:tool.execute.beforePurpose: Auto-truncate long question labels to prevent context bloat
Content & Prompts (5)
Content & Prompts (5)
keyword-detector
Event:messages.transformPurpose: Detect modes from user input: ultrawork, search, analyze, prove-yourself
Behavior: Injects mode-specific system promptsFiles: ~1665 LOC
rules-injector
Event:tool.execute.beforePurpose: Conditional rules injection from AGENTS.md, config, skill rules
Evaluation: Evaluates conditions to determine which rules applyFiles: 19 files, ~1604 LOC
prometheus-md-only
Event:tool.execute.beforePurpose: Enforce .md-only writes for Prometheus agent (planner read-only mode)
sisyphus-junior-notepad
Event:chat.messagePurpose: Notepad injection for Sisyphus Junior subagents
thinking-block-validator
Event:messages.transformPurpose: Validate thinking block structure and placement
Tool Guard Hooks (10)
Defined insrc/plugin/hooks/create-tool-guard-hooks.ts
File Safety (3)
File Safety (3)
write-existing-file-guard
Event:tool.execute.beforePurpose: Require Read before Write on existing files
Prevents: Overwriting files without reading them firstLocation:
src/hooks/write-existing-file-guard/comment-checker
Event:tool.execute.afterPurpose: Block AI-generated comment patterns (“slop prevention”)
Detects: Generic comments like “Initialize variables”, “Helper function”, etc.
src/hooks/comment-checker/hashline-read-enhancer
Event:tool.execute.afterPurpose: Enhance Read output with line hashes for precise editing
Format: Adds content-based hashes to each line
Output Processing (3)
Output Processing (3)
tool-output-truncator
Event:tool.execute.afterPurpose: Truncate oversized tool output to prevent context bloat
Limits: 2000 lines or 51200 bytes per tool callLocation:
src/hooks/tool-output-truncator.tsempty-task-response-detector
Event:tool.execute.afterPurpose: Detect empty or failed task responses
Action: Injects retry promptLocation:
src/hooks/empty-task-response-detector.tsjson-error-recovery
Event:tool.execute.afterPurpose: Detect JSON parse errors, inject correction reminder
Context Injection (4)
Context Injection (4)
directory-agents-injector
Event:tool.execute.beforePurpose: Auto-inject directory AGENTS.md files into context
Behavior: Searches up directory tree for AGENTS.md
directory-readme-injector
Event:tool.execute.beforePurpose: Auto-inject directory README.md files into context
Behavior: Injects README.md from current/parent directories
context-injector-messages-transform
Event:messages.transformPurpose: Inject AGENTS.md/README.md into message context
rules-injector
Event:tool.execute.beforePurpose: Conditional rules from AGENTS.md, config, skills
Transform Hooks (4)
Defined insrc/plugin/hooks/create-transform-hooks.ts
claude-code-hooks
Event:messages.transformPurpose: Claude Code settings.json compatibility layer
Maps: Settings → plugin behavior Location:
src/hooks/claude-code-hooks/ - See AGENTS.md for full mapping
keyword-detector
Event:messages.transformPurpose: Detect ultrawork/search/analyze/prove-yourself modes
Action: Inject mode-specific system prompts
context-injector
Event:messages.transformPurpose: Inject AGENTS.md/README.md into context
thinking-block-validator
Event:messages.transformPurpose: Validate
<thinking> block structure and order
Skill Hooks (2)
Defined insrc/plugin/hooks/create-skill-hooks.ts
category-skill-reminder
Event:chat.messagePurpose: Remind about category + skill delegation patterns
Example: “For browser tasks, use agent-browser skill”
auto-slash-command
Event:chat.messagePurpose: Auto-detect
/command patterns in user inputBehavior: Maps to registered slash commands
Hook Composition
Hooks are composed in three factory files:Configuration
Enable/disable hooks inoh-my-opencode.jsonc:
Creating Custom Hooks
- Create
src/hooks/{name}/index.tswith factory:
- Register in appropriate tier (
create-*-hooks.ts) - Add to
src/config/schema/hooks.tsHookNameSchema
Source Files
- Hook Definitions:
src/hooks/(39 directories + 6 standalone files) - Hook Composition:
src/plugin/hooks/create-*-hooks.ts - Hook Handlers:
src/plugin/*.ts(8 OpenCode hook handlers) - Schema:
src/config/schema/hooks.ts(HookNameSchema)
Related
Skills
Skill system and built-in skills
Tools
26 tools that hooks can modify