Skip to content

fix(opencode plugin) parameter + compact#279

Merged
mrsimpson merged 3 commits into
mainfrom
fix/opencode-plugin-parameter-descriptions
May 7, 2026
Merged

fix(opencode plugin) parameter + compact#279
mrsimpson merged 3 commits into
mainfrom
fix/opencode-plugin-parameter-descriptions

Conversation

@mrsimpson
Copy link
Copy Markdown
Collaborator

No description provided.

mrsimpson and others added 3 commits May 7, 2026 18:09
…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
@mrsimpson mrsimpson merged commit 08126a0 into main May 7, 2026
3 checks passed
@mrsimpson mrsimpson deleted the fix/opencode-plugin-parameter-descriptions branch May 7, 2026 16:45
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.

1 participant