Skip to content

fix(hooks): register OpenCode 2.x sessionStart context without waiting on earlier events - #3932

Merged
dyoshikawa merged 3 commits into
mainfrom
resolve-issue-3927-v2-session-start-await
Nov 22, 2026
Merged

dyoshikawa merged 3 commits into
mainfrom
resolve-issue-3927-v2-session-start-await

Conversation

@dyoshikawa

Copy link
Copy Markdown
Owner

Summary

Follow-ups from #3926 (OpenCode-style sessionStart context). Every item of #3927 is addressed:

  1. (mid) OpenCode 2.x: a slow earlier event delayed sessionStart context registration. The V2 event loop awaited each handler, so a slow command of an earlier event (e.g. a stop hook on session.execution.succeeded) held back session.created, and the session's first model requests got "" from __rulesyncSessionContext. Making only sessionStart fire-and-forget would not help, because the loop would still be blocked on the earlier handler. The loop now never waits for a command:
    • sessionStart runs as void __rulesyncSessionStart(...), which registers the pending context synchronously as soon as the event is read.
    • All other event handlers are chained on __rulesyncEventQueue, so they still run one after another in event order, as before. A throw is caught so it does not end the queue.
    • session.deleted still drops the context inline, so created/deleted ordering is unchanged.
  2. (low) 1 MiB cap on the Bun Shell path. Documented: on OpenCode 1.x / Kilo / MiMo Code, sessionStart runs on Bun Shell, which reads the whole stdout before the check. Streaming with a limit there would mean replacing the in-process Bun Shell runner that the event hooks rely on, so I documented the limit instead.
  3. (low) Unguarded ctx.session.hook("context", ...). The registration is now wrapped in try/catch. A host with no ctx.session.hook, or one that rejects the context hook point, logs an error instead of making setup throw, so the other hooks (preToolUse guards included) still load. A typeof check would not catch the second case.
  4. (low) Duplicated logic. Added usesSessionContext(handlers) and sessionContextExtrasFor(canonicalEvent) in opencode-style-generator.ts, used by both generators.
  5. (low) Eviction order. A kept context moves to the end of the map when it becomes ready, so the 100-session eviction follows completion order.
  6. (low, info) Unsanitised stdout. Added a docs note: as with Claude Code, the context is only trimmed and capped, so a hook that prints external data can inject instructions into the system prompt.

Tests

  • New runtime test (real Bun): a slow stop command is still running when session.created arrives, and the session's first context request gets the context right away. The stop handler after it still runs. This test fails on main.
  • New runtime test: a host whose ctx.session.hook("context") throws still loads the plugin, and preToolUse still denies.
  • New runtime test: a sessionStart that starts first but finishes last is kept, and the oldest completed one is evicted. This test fails on main.
  • The generator test now checks await (V1 event hook) and void (V2 loop) for each target.
  • pnpm cicheck passes.

Closes #3927

🤖 Generated with Claude Code

https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4

dyoshikawa and others added 2 commits November 22, 2026 17:30
…g on earlier events

The OpenCode 2.x event loop awaited every handler, so a slow command of an
earlier event (a stop hook on session.execution.succeeded, say) held back
session.created, and a session's first model requests got no sessionStart
context. The loop now never waits: sessionStart starts right away and
registers its pending context, while the other event handlers still run one
after another on a queue in event order.

Also:
- Guard the session context hook registration so a 2.x host that cannot
  register it logs an error instead of breaking every other hook.
- Evict kept sessionStart contexts in completion order.
- Share the context check and sessionStart extras between the generators.
- Document the Bun Shell memory caveat of the 1 MiB cap and the prompt
  injection risk of hooks that print external data.

Closes #3927

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

- Log a throw that reaches the event queue instead of swallowing it.
- Skip queued event handlers that have not started when the plugin is
  disposed.
- Document that sessionStart runs outside the event order and can overlap
  other event commands; align the helper JSDoc with the eviction docs.
- Test cross-event ordering past a slow and a throwing handler, and wait on
  a marker file instead of a fixed sleep in the eviction test.

Refs #3927

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
" if (__rulesyncEvents.signal.aborted) return;",
...statements.map((line) => ` ${line}`),
" })",
` .catch((error) => console.error(${JSON.stringify(`[rulesync-hooks] ${eventType} event hook failed:`)}, error));`,
…-session-start-await

# Conflicts:
#	src/generated/docs-content.ts
@dyoshikawa
dyoshikawa merged commit 4164784 into main Nov 22, 2026
10 checks passed
@dyoshikawa
dyoshikawa deleted the resolve-issue-3927-v2-session-start-await branch November 22, 2026 08:57
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-ups for sessionStart context in OpenCode-style plugins (#3926)

2 participants