fix(opencode plugin) parameter + compact#279
Merged
Merged
Conversation
…ed_to_phase
## High level changes
- `plugin.ts`: Added `lastKnownAgent` variable that caches the last enabled agent name seen in `chat.message`
- `plugin.ts`: Post-compaction `promptAsync` now passes `agent: lastKnownAgent` in the request body so the continue message runs under the original workflow agent
- `plugin.ts`: Added `postCompactionAutoResume` flag set on `session.compacted` to bypass the agent filter for OpenCode's own auto-continue message
- `plugin.ts`: `chat.message` hook checks `bypassAgentFilter` before the `isAgentEnabled` guard so phase instructions are injected instead of a suppression/no-workflow notice
## Motivation
After `proceed_to_phase` triggers implicit compaction, OpenCode resumes the session using its default agent (e.g. `build`) rather than the original workflow agent. This caused two problems:
1. OpenCode's built-in auto-continue `chat.message` arrived with agent `build`, hit the `WORKFLOW_AGENTS` filter, and injected "No Active Workflow Detected" instead of phase instructions.
2. The plugin's own follow-up `promptAsync` (sent on `session.idle`) also omitted the agent field, so it too ran under the wrong agent, losing system prompt and tool access.
## Details
- `lastKnownAgent` is only updated when `isAgentEnabled` is true, ensuring a subagent name (e.g. `general`, `explore`) never overwrites the cached workflow agent.
- `postCompactionAutoResume` is set to `true` on `session.compacted` and consumed (set back to `false`) by the very next `chat.message` invocation. This is a one-shot bypass: it covers exactly OpenCode's implicit auto-continue turn and nothing more.
- The `promptAsync` body now spreads `{ agent: lastKnownAgent }` when a cached agent is available. `PromptInput.agent` is an optional string field in the OpenCode API, so this is a safe additive change with no effect when `lastKnownAgent` is `null`.
…host
## High level changes
- `packages/opencode-plugin/src/plugin.ts`: Added `'tool.definition'` hook that dynamically
imports `'zod'` at hook-call time and registers each tool parameter description into the
host's (OpenCode's) `globalRegistry`, making descriptions visible to `z.toJSONSchema()`
## Motivation
The previous peerDependency fix (moving `zod` from `dependencies` to `peerDependencies`) was
insufficient: OpenCode is distributed as a compiled Bun binary with zod bundled internally, so
module hoisting / peerDep resolution does not apply. As a result, the plugin and OpenCode continued
to use separate zod instances with separate `globalRegistry` singletons, meaning `.describe()`
annotations on tool parameters never reached the LLM's JSON Schema.
## Details
- The `tool.definition` hook fires after OpenCode wraps plugin args with `z.object(def.args)`
(its own zod) but before `z.toJSONSchema(item.parameters)` is called in `prompt.ts`
- `output.parameters._zod.def.shape` contains the original plugin field schemas; their
`.description` getter reads from the plugin's `globalRegistry` and works cross-instance
- Dynamic `import('zod')` at runtime: when the plugin has no local `node_modules/zod`
(peerDependency, correctly installed), ESM resolution walks up to the host's (OpenCode's) zod
module, which is already in the module cache — returning the same `globalRegistry` singleton
that `z.toJSONSchema()` will read from
- Errors are silently swallowed — descriptions are a nice-to-have for LLM guidance, not critical
for tool execution
- All 64 existing tests pass
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.
No description provided.