Skip to content

feat(hooks): map OpenCode 2.x stop to experimental.idle and close out OpenCode follow-up items - #3986

Merged
dyoshikawa merged 3 commits into
mainfrom
resolve-issue-2592-opencode-followup
Nov 26, 2026
Merged

dyoshikawa merged 3 commits into
mainfrom
resolve-issue-2592-opencode-followup

Conversation

@dyoshikawa

@dyoshikawa dyoshikawa commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Summary

Works through the items still open on the OpenCode follow-up issue (#2592) after the 2026-11-08 re-check:

  • hooks (opencode): stop now maps to OpenCode 2.x's experimental.idle session hook. A stop command that exits with code 2 continues the agent once, the way it already does on Pi (agent_before_settle) and oh-my-pi (session_stop). Until now, stop read the observe-only session.execution.succeeded event.
  • commands (docs): a note that OpenCode 2.x does not expand @path in stored command templates, while OpenCode 1.x attaches the file.
  • config keys (docs): a note on references, subagent_depth / experimental.subagent_depth, skills.paths / skills.urls and policies / experimental.policies. Rulesync does not generate these keys and leaves them as written, so users can set them by hand.

Changes

  • src/types/hooks.ts: CANONICAL_TO_OPENCODE_V2_EVENT_NAMES.stop changes from event:session.execution.succeeded to session:experimental.idle.
  • src/features/hooks/opencode-v2-plugin-generator.ts: the plugin registers ctx.session.hook("experimental.idle", ...) for stop handlers:
    • It runs every stop command with Claude Code's Stop payload (session_id, stop_hook_active, cwd).
    • It combines the sanitized stderr (or stdout) of each command that exited with 2 into one Stop hook feedback: ... message and admits it with ctx.session.synthetic({ sessionID, text }).
    • Continuation happens at most once per idle. The hook runs again after the admitted input, and that run sees stop_hook_active: true and lets the session go idle.
    • While the session's own subagents or other background jobs are running, it waits: the hook runs again when they finish. It does not wait for shells.
    • It skips subagent (child) sessions, looked up with ctx.session.get() (a session with a parentID). Claude Code's Stop does not fire for subagents, but OpenCode runs the idle hook for child sessions too.
    • It logs every failure instead of throwing, because a throwing idle hook fails the execution.
    • Before 2.2.1 the host accepts the unknown registration and never triggers it (v2.2.0 plugin/hooks.ts stores it in a Map). For those hosts the plugin keeps an observe-only fallback on session.execution.succeeded, which stands down once the idle hook has run. OpenCode publishes Succeeded when the execution ends (session/execution.ts), after the drain loop that triggers the idle hook, so on 2.2.1+ the commands do not run twice.
    • The continue-once guard is also cleared on session.execution.failed / session.execution.interrupted, so a failed or interrupted continued run does not leave the next turn's first stop with stop_hook_active: true.
    • It reuses the shared sanitizeBlockReason helper and emits it only once when preToolUse already defines it.
  • src/features/hooks/opencode-style-generator.ts: factored commandAndInputOf() out of buildRunStatement() so the stop runner reuses the same command escaping and stdin expression.
  • Tests:
    • Static tests updated.
    • New real-Bun runtime tests cover the continue-once loop with stop_hook_active, the background-job wait vs. shells, a throwing synthetic, and a host without the hook.
    • Event-stream ordering tests that used stop now use afterError / permissionDenied.
    • The e2e expectation is updated.
  • docs/reference/file-formats.md: updated the V2 hook mapping and added the stop, @path and config-key notes. src/generated/docs-content.ts is regenerated.

Design decisions

  • Map stop to experimental.idle despite the experimental label. This follows project precedent:
    • rulesync already maps preCompact to OpenCode 1.x's experimental.session.compacting.
    • The Pi mapping moved stop to the actionable agent_before_settle and documented a minimum version (Pi v0.87.0), even though older Pi never fires it.
    • Upstream's built-in /goal command (packages/core/src/plugin/goal.ts, v2.4.2) is built on this hook, so it is load-bearing upstream.
    • Hosts before 2.2.1 keep the old observe-only behaviour through the session.execution.succeeded fallback. The one documented overlap: an execution that does no model work (for example a manual compaction) before any session has gone idle runs its stop commands once through the fallback.
  • Exit-code contract mirrors Pi / omp. Only exit 2 continues. Any other failure is only logged and never continues, because a stop hook should fail safe by letting the agent stop. Unlike Pi, other failures are logged rather than rethrown, because a throwing experimental.idle hook fails the whole execution upstream.
  • Continue-once guard keyed by session. After a continuation, the next idle run executes the commands with stop_hook_active: true and does not continue. This is the same bound oh-my-pi uses, and it is what upstream asks for ("a plugin that always admits input must bound its own loop"). The guard is set only after synthetic() succeeds.
  • Background work. The hook returns early for the session's own non-shell background jobs (subagent, image, video, codemode), which wake it again when they finish. It does not wait for shells, because a background dev server may never finish. This is the same rule as upstream's /goal plugin.
  • @path: docs note only, no generate-time warning. Scanning bodies for @ would misfire on emails, @mentions, decorators and npm scopes, and the canonical command body is shared across targets.
  • references, subagent_depth, skills.paths / skills.urls, policies: documented as hand-authored, not generated.
    • They are project-wide config keys with no counterpart in the Rulesync source files or in any other target.
    • policies is a managed-policy layer whose action set keeps changing (the v2.2.3 integration.use split, then websearch.use in v2.2.6). Generating it would encode rulesync's permission rules a second time.
    • Rulesync's merge into opencode.json(c) leaves unknown top-level keys untouched. I checked this with a scratch generate -t opencode -f rules,mcp,permissions,hooks: hand-written references, experimental.subagent_depth, skills.paths/urls and policies all survived. So users already have a working path.

Skipped items

None remain open. The items above are either implemented or explicitly declined, with reasons, as "not generated; set by hand" in the docs.

Note: evicts sessionStart contexts in completion order (OpenCode V1, unrelated to this change) times out locally under machine load on origin/main as well.

Closes #2592

🤖 Generated with Claude Code

https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4

dyoshikawa and others added 3 commits November 26, 2026 16:45
…open OpenCode items

Map the canonical stop hook on the opencode target to OpenCode's
experimental.idle session hook so a stop command exiting with code 2
continues the agent once, like Pi and oh-my-pi. Document that OpenCode
2.x does not expand @path in stored command templates, and that
references, subagent_depth, skills.paths/urls and policies are left to
hand-authored opencode.json keys.

Refs #2592

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
… stop hooks

Skip stop commands for child sessions, fall back to the observe-only
session.execution.succeeded event until experimental.idle has run (older
OpenCode 2.x silently never triggers it), and clear the continue-once
guard when a continued run fails or is interrupted.

Refs #2592

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
…encode-followup

# Conflicts:
#	src/generated/docs-content.ts
@dyoshikawa
dyoshikawa merged commit 112bb2e into main Nov 26, 2026
10 checks passed
@dyoshikawa
dyoshikawa deleted the resolve-issue-2592-opencode-followup branch November 26, 2026 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Follow up OpenCode upstream updates: notification/permissionDenied hook mappings, references key, V2 beta tracking

1 participant