anomalyco / anomalyco/opencode

[FEATURE]: PreToolUse, Stop, and SessionStart hook events with hook router

Open
#39,275 1 comment 1 reaction 1 assignee View on GitHub

@rekram1-node is already working on this.

Since Jul 28, 2026.

Dominant language
TypeScript
Stars
209k
Forks
27.5k
PR merge metrics
PR metrics pending

Description

Feature hasn't been suggested before.
  • I have verified this feature I'm about to request hasn't been suggested before (searched for "PreToolUse", "Stop hook", "SessionStart", "hook router" -- none match exactly; #34890 asks for Claude-like hook commands for custom scripting; #35540 asks for a session.finalizing plugin hook; neither covers tool-level lifecycle guards).
Describe the enhancement you want to request

Add three lifecycle hook events and a hook router to OpenCode, matching the pattern that both Claude Code (PreToolUse/Stop/UserPromptSubmit/etc.) and Codex (.codex/hooks.json event router) provide:

  1. PreToolUse -- fires before every tool execution. The hook receives the tool name + input payload and can return allow, deny (with reason), or ask. This is the primary safety mechanism -- it enables blocking dangerous tool calls (e.g., raw MediaWiki writes, secret-bearing file writes, pywikibot invocations) before they happen.

  2. Stop -- fires at session end. The hook receives the session transcript and can perform final checks: closeout compliance (dirty tree, missing inline paste, missing resume prompt), file link formatting, and other post-session verification.

  3. SessionStart -- fires at session start. The hook receives session metadata and can verify session readiness: correct worktree, installed pre-commit hooks, required tools present, bootstrap checks.

  4. Hook router -- a dispatch mechanism (e.g., a .opencode/hooks.json or opencode.jsonc hooks key) that maps events to handler scripts with optional matching/filtering. Each handler is a command or script invoked with structured JSON on stdin; it prints a JSON decision to stdout. The router can route one event to multiple handlers and aggregate decisions.

Use case

Without these events, OpenCode has no mechanical enforcement surface for safety guards. Guards expressed as prose in instruction files are advisory -- the agent must self-enforce, and self-enforcement is not enforcement. This is the same gap Antigravity has (documented as a declared gap in their parity matrix). Claude Code and Codex both use these events to implement Tier A guards (blocking wiki AI traces, blocking secret writes, enforcing closeout standards, verifying session shape). OpenCode sessions cannot touch wiki or deploy work until mechanical guards exist -- these three events are the minimum needed to close that gap.

Suggested priority

High -- this is a parity blocker for safety-conscious workflows that run across multiple coding agents.

Related
  • #34890 (Claude-like hook commands for non-LLM scripting -- complementary, different lifecycle point)
  • #35540 (session.finalizing plugin hook -- complementary, different mechanism)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.