Repository navigation
feat(hooks): pass sessionStart stdout to the model in OpenCode-style plugins - #3926
Merged
dyoshikawa merged 4 commits intoNov 22, 2026
Merged
Conversation
…plugins In the generated OpenCode, OpenCode V1, Kilo and MiMo Code plugins, a sessionStart command's stdout now reaches the model as context, as a SessionStart hook's does in Claude Code. Until now the command ran with its stdout inherited, so the output went to OpenCode's own stdout. The plugin reads that stdout (1 MiB kept, as for preToolUse), takes the text or a JSON object's hookSpecificOutput.additionalContext on exit code 0, caps it at 10,000 characters and adds it to the system prompt of each model request in that session: experimental.chat.system.transform on OpenCode 1.x, Kilo and MiMo Code, and the session context hook on OpenCode 2.x. A request waits for the session's running commands, which stay bounded by their timeout. The context is kept for the 100 most recently used sessions and dropped on session.deleted. stderr still goes to the host. Plugins without a sessionStart hook are unchanged.
…start-context # Conflicts: # src/generated/docs-content.ts
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
A pending hook execution can prematurely evict a valid retained context even if it produces no usable output, permanently losing session context at the 100-entry boundary.
1 open issue
What changed in this PR
This PR makes sessionStart hook output available as model context in Rulesync-generated plugins for OpenCode, OpenCode V1, Kilo, and MiMo Code.
Changes:
- Captures successful hook stdout, extracts supported JSON context, and applies output limits.
- Adds session-scoped context through system-prompt hooks, with pending-command waits and deletion cleanup.
- Adds generator and runtime coverage, plus documentation of behavior and limitations.
| File | Description |
|---|---|
src/features/hooks/opencode-v2-plugin-generator.ts |
Registers context injection and session-deletion cleanup for OpenCode 2.x. |
src/features/hooks/opencode-style-generator.ts |
Adds shared output capture, context storage, and V1-style prompt injection. |
src/features/hooks/opencode-style-generator.test.ts |
Checks conditional context-hook generation across all four targets. |
src/features/hooks/opencode-hooks.test.ts |
Tests context delivery, session isolation, output limits, and Windows capture. |
docs/reference/file-formats.md |
Documents session context behavior and command-output handling. |
Review effort: Balanced
…sion limit A running, empty or failed sessionStart no longer evicts another session's context: a session is evicted only when a non-empty context is admitted past 100.
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
Timed-out Bun Shell hooks permanently lose stderr diagnostics because the early return skips forwarding output suppressed by quiet(), regressing previously visible error reporting.
1 resolved since last review
Review effort: Balanced
…Shell On Bun Shell a sessionStart command runs quietly so its stdout can be read; its stderr is now written to the host's when the command ends, also after its timeout, instead of only on completion within it.
5 tasks done
6 tasks
Owner
|
@Lartweib Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
In OpenCode-style targets (OpenCode, OpenCode V1, Kilo and MiMo Code), a
sessionStarthook's stdout now reaches the model as context, as it does in Claude Code. Until now the generated plugin ran the command with its stdout inherited (echo: true,stdout: "inherit", also on the Bun Shell fallback), so the output went to OpenCode's own stdout. This came up while using the generated plugin with asessionStarthook that loads project context.Example (
.rulesync/hooks.jsonc, thenrulesync generate -t opencode -f hooks):{ "version": 1, "hooks": { "sessionStart": [{ "command": "echo 'SESSION-CONTEXT-MARKER-7341: the project codename is BLUEHERON'" }] } }Start OpenCode in that project and ask "What is the project codename? If you don't know, say UNKNOWN." On
mainthe answer is UNKNOWN; with this change it is BLUEHERON.Changes
Capture (4505e8d): a
sessionStartcommand's stdout is now read instead of inherited, with the same 1 MiB cap aspreToolUse. stderr still goes to the host (on Bun Shell, once the command ends). Every other hook runs exactly as onmain.What becomes context: on exit code 0, the trimmed stdout, or, when it prints a JSON object, its
hookSpecificOutput.additionalContextstring. As in Claude Code, only an object counts as JSON. The text is capped at 10,000 characters, Claude Code's limit. A non-zero exit, a timeout, empty output, or more than 1 MiB of stdout (logged) adds nothing. The output of severalsessionStartcommands is joined in order.Injection, per target, through the host's system-prompt hook, so the context sits once in the system prompt of each model request of that session and is not written into the transcript:
ctx.session.hook("context"): pushes a{ type: "text" }part tosystem. Covers agent-loop requests, not compaction or title requests.experimental.chat.system.transform: pushes tooutput.systemexperimental.chat.system.transform(present in@kilocode/plugin7.8.27)experimental.chat.system.transform(triggered inpackages/cli/src/session/llm.ts)All four support it, so no target needs a new warning. Hermes and the other targets use their own generators and are unchanged.
Session scoping and bounds:
session.createdevent ran the command (event.data.sessionIDon 2.x,event.properties.info.idon 1.x).timeoutand the existing kill path, so the wait always ends.session.deleted; on OpenCode 2.x the plugin now also subscribes to that event when asessionStarthook exists.Unchanged: a plugin without a
sessionStarthook is generated exactly as before apart from the shared runner's newecho: "stderr"mode, which onlysessionStartuses. Nothing gating a tool call or a prompt changes, so the plugin is no more permissive thanmain.Docs: a new
sessionStartnote indocs/reference/file-formats.mdlists what is and is not covered. The execution note now mentions the captured stdout.Design choices
preToolUse, so a noisy command cannot exhaust memory. The 10,000-character cap is Claude Code's limit for injected hook output.sessionStarthook written for Claude Code expects its stdout to reach the model, so this follows that contract without extra configuration. A hook whose stdout is not meant as context can write to stderr instead.I'm happy to change any of these if you'd prefer another approach.
Testing
Real-Bun runtime tests (
opencode-hooks.test.ts):bun execand through the Bun Shell fallback: a slow command's context reaches the first request, which waits for it. A JSONadditionalContextis joined after it, while a JSON object without it and a failing command add nothing. Another session gets nothing, and the context is gone aftersession.deleted. The stdout is not printed.experimental.chat.system.transform): the 10,000-character cap, and more than 1 MiB of stdout logged and ignored. A second session gets nothing, and the context is dropped aftersession.deleted.runIf(win32): output printed throughcmd.exeis read without its CRLF.Generator tests (
opencode-style-generator.test.ts): for OpenCode, OpenCode V1, Kilo and MiMo Code, the hook is emitted once with asessionStarthook and not at all without one.Live, OpenCode 2.0.30 TUI (Docker, GitHub Copilot
claude-haiku-5.5, driven through tmux). ThesessionStartcommand printed the marker line once and then deleted its source, which lived outside the project, so neither the model nor a later session could find it on disk. Each run had two sessions in the same TUI, plus apreToolUsehook that appends its stdin to a file:/new)mainIn both runs the
preToolUsehook received itsPreToolUseJSON on stdin for theshellcall and the call went through.Windows (Bun 1.4.2):
opencode-hooks.test.tspasses (29 passed, 4 POSIX-only skipped), including the newcmd.exetest and the existing Windows tree-kill test. CI runs on Linux only, so this was run locally.pnpm cicheckon Linux: passes (619 test files, 17,190 passed, 5 skipped; format, lint, typecheck, cspell and secretlint clean).Known limitations
session.createdreaches the plugin through the event stream, so a request that starts before the plugin receives that event gets no context. In the live run, the first request already had it.sessionStartdoes not run for it again. Neither does a session that dropped out of the 100 kept.SessionStartfields (systemMessage,continue, …), a JSON object on a non-zero exit, output above the cap saved to a file, and theSessionStartstdin input.Size
+448/−43 across 6 files (about 261 of the added lines are tests).