Skip to content

feat(hooks): pass sessionStart stdout to the model in OpenCode-style plugins - #3926

Merged
dyoshikawa merged 4 commits into
dyoshikawa:mainfrom
Lartweib:fix/opencode-session-start-context
Nov 22, 2026
Merged

dyoshikawa merged 4 commits into
dyoshikawa:mainfrom
Lartweib:fix/opencode-session-start-context

Conversation

@Lartweib

@Lartweib Lartweib commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In OpenCode-style targets (OpenCode, OpenCode V1, Kilo and MiMo Code), a sessionStart hook'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 a sessionStart hook that loads project context.

Example (.rulesync/hooks.jsonc, then rulesync 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 main the answer is UNKNOWN; with this change it is BLUEHERON.

Changes

  • Capture (4505e8d): a sessionStart command's stdout is now read instead of inherited, with the same 1 MiB cap as preToolUse. stderr still goes to the host (on Bun Shell, once the command ends). Every other hook runs exactly as on main.

  • What becomes context: on exit code 0, the trimmed stdout, or, when it prints a JSON object, its hookSpecificOutput.additionalContext string. 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 several sessionStart commands 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:

    Target Hook
    OpenCode (2.x) ctx.session.hook("context"): pushes a { type: "text" } part to system. Covers agent-loop requests, not compaction or title requests.
    OpenCode V1 (1.x) experimental.chat.system.transform: pushes to output.system
    Kilo experimental.chat.system.transform (present in @kilocode/plugin 7.8.27)
    MiMo Code experimental.chat.system.transform (triggered in packages/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:

    • Context is keyed by the ID of the session whose session.created event ran the command (event.data.sessionID on 2.x, event.properties.info.id on 1.x).
    • A request waits for that session's commands if they are still running. Each command is still bounded by its timeout and the existing kill path, so the wait always ends.
    • Only sessions with non-empty context are kept, at most the 100 most recently used; a running or empty one never evicts another session's context. A session's entry is dropped on session.deleted; on OpenCode 2.x the plugin now also subscribes to that event when a sessionStart hook exists.
  • Unchanged: a plugin without a sessionStart hook is generated exactly as before apart from the shared runner's new echo: "stderr" mode, which only sessionStart uses. Nothing gating a tool call or a prompt changes, so the plugin is no more permissive than main.

  • Docs: a new sessionStart note in docs/reference/file-formats.md lists what is and is not covered. The execution note now mentions the captured stdout.

Design choices

  • System prompt rather than the first message. The context is added to each request's system prompt through the host's hook, instead of being written into the first user message. It stays out of the transcript, does not depend on which message comes first, and survives compaction.
  • 1 MiB read, 10,000 characters kept. The 1 MiB read cap matches preToolUse, so a noisy command cannot exhaust memory. The 10,000-character cap is Claude Code's limit for injected hook output.
  • Always on rather than opt-in. A sessionStart hook 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):

    • On OpenCode 2.x, through bun exec and through the Bun Shell fallback: a slow command's context reaches the first request, which waits for it. A JSON additionalContext is joined after it, while a JSON object without it and a failing command add nothing. Another session gets nothing, and the context is gone after session.deleted. The stdout is not printed.
    • On OpenCode 1.x (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 after session.deleted.
    • The 100-session limit: an empty context evicts nothing, and the least recently used context is the one dropped (9ff7dda).
    • On Bun Shell, a timed-out command's stderr still reaches the host when it ends (e7c57ab).
    • runIf(win32): output printed through cmd.exe is 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 a sessionStart hook and not at all without one.

  • Live, OpenCode 2.0.30 TUI (Docker, GitHub Copilot claude-haiku-5.5, driven through tmux). The sessionStart command 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 a preToolUse hook that appends its stdin to a file:

    Session 1 Session 2 (/new)
    main UNKNOWN UNKNOWN
    This branch "The project codename is BLUEHERON." (no tool calls, 0.9 s) UNKNOWN

    In both runs the preToolUse hook received its PreToolUse JSON on stdin for the shell call and the call went through.

  • Windows (Bun 1.4.2): opencode-hooks.test.ts passes (29 passed, 4 POSIX-only skipped), including the new cmd.exe test and the existing Windows tree-kill test. CI runs on Linux only, so this was run locally.

  • pnpm cicheck on Linux: passes (619 test files, 17,190 passed, 5 skipped; format, lint, typecheck, cspell and secretlint clean).

Known limitations

  • On OpenCode 2.x, session.created reaches 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.
  • A session OpenCode resumes after a restart gets no context, because sessionStart does not run for it again. Neither does a session that dropped out of the 100 kept.
  • Not supported: other Claude Code SessionStart fields (systemMessage, continue, …), a JSON object on a non-zero exit, output above the cap saved to a file, and the SessionStart stdin input.

Size

+448/−43 across 6 files (about 261 of the added lines are tests).

…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.
Copilot AI balanced review requested due to automatic review settings November 20, 2026 15:21
…start-context

# Conflicts:
#	src/generated/docs-content.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Copilot AI balanced review requested due to automatic review settings November 20, 2026 15:26
…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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Pending hook runs can unnecessarily evict valid cached session context, while timed-out session-start commands can lose stderr diagnostics.

2 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 20, 2026 15:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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 open issue
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.
Copilot AI balanced review requested due to automatic review settings November 20, 2026 15:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the session-scoped hook context changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@dyoshikawa
dyoshikawa merged commit dbe8d30 into dyoshikawa:main Nov 22, 2026
6 checks passed
@dyoshikawa

Copy link
Copy Markdown
Owner

@Lartweib Thank you!

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.

3 participants