Skip to content

Commit 4160b6b

Browse files
committed
fix(agent): make opencode-worker the only caller of MCP delegate
Restore upstream's flow: the main thread spawns the background opencode-worker, which calls MCP delegate, verifies the changes and iterates at most twice. A PreToolUse guard denies delegate and delegate_async on the main thread (no agent_id), where they block the session and async tasks are lost when the MCP server restarts.
1 parent c988e16 commit 4160b6b

12 files changed

Lines changed: 259 additions & 99 deletions

File tree

‎CLAUDE.md‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ Bun runs `.ts` directly. Nothing is compiled or bundled (`tsconfig` sets `noEmit
1515

1616
The plugin's runtime behaviour lives in the markdown files below plus the bundled MCP server:
1717

18-
| Surface | File | Role |
19-
| --------- | ----------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
20-
| Skill | `skills/opencode-delegate/SKILL.md` | Model picking, brief template, review-after-return rules |
21-
| Agent | `agents/opencode-worker.md` | Thin forwarder: one `opencode run` (write: `build --auto`; read: `plan`), report |
22-
| Command | `commands/opencode.md` | `/opencode <model> -- <task>`: splits on `--`, spawns `opencode-worker` |
23-
| Hook | `scripts/route-to-opencode.sh`, `plugin-hooks/hooks.json` | PreToolUse `Agent\|Task`: rewrites only built-in subagent calls (general-purpose, claude, Explore, Plan, claude-code-guide, statusline-setup) to `opencode-worker` on Haiku (Explore/Plan/claude-code-guide forced read-only); custom, plugin, reviewer and SDD agents never routed; pass-through guards: `OPENCODE_DELEGATE_ROUTE=off`, missing opencode, `[no-opencode]`, no `jq` |
24-
| MCP | `plugin.json` `mcpServers`, `src/index.ts`, `dist/index.js` | `opencode` MCP server (stdio, `bun ${CLAUDE_PLUGIN_ROOT}/dist/index.js`) exposing `delegate`, `delegate_async`, `task_result`, `task_list`, `task_cancel`, `list_models`, `list_sessions` to the main thread; its `instructions` (always in the system prompt) make delegation the default for implementation |
25-
| Manifests | `.claude-plugin/plugin.json`, `marketplace.json` | Plugin identity; `plugin.json` carries homepage/repository shown by Claude Code |
26-
27-
`src/` backs the MCP server (`src/index.ts`). The installed plugin cache has no `node_modules`, so what runs is the committed bundle `dist/index.js`: changing `src/` changes plugin behaviour only after `bun run build`, and CI fails if `dist/` is stale. Its exports (`delegate`, `resolveModel`, `buildArgs`, `EventCollector`) are also the reference for how `opencode run` must be invoked and how its JSON stream is parsed; `opencode-worker` shells out to `opencode run` itself, so keep its command consistent with `buildArgs`.
18+
| Surface | File | Role |
19+
| --------- | --------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
20+
| Skill | `skills/opencode-delegate/SKILL.md` | Model picking, brief template, review-after-return rules |
21+
| Agent | `agents/opencode-worker.md` | Supervisor (`model: haiku`, `background: true`, inherits all tools incl. MCP): the only caller of MCP `delegate`; verifies git changes and tests, iterates at most twice, reports |
22+
| Command | `commands/opencode.md` | `/opencode <model> -- <task>`: splits on `--`, spawns `opencode-worker` |
23+
| Hook | `scripts/route-to-opencode.sh`, `scripts/guard-main-thread-mcp.sh`, `plugin-hooks/hooks.json` | PreToolUse `Agent\|Task`: rewrites only built-in subagent calls (general-purpose, claude, Explore, Plan, claude-code-guide, statusline-setup) to `opencode-worker` on Haiku (Explore/Plan/claude-code-guide forced read-only); custom, plugin, reviewer and SDD agents never routed; pass-through guards: `OPENCODE_DELEGATE_ROUTE=off`, missing opencode, `[no-opencode]`, no `jq`. Second PreToolUse entry (`guard-main-thread-mcp.sh`) denies this plugin's MCP `delegate`/`delegate_async` when the input has no `agent_id` (main thread); calls from `opencode-worker` pass |
24+
| MCP | `plugin.json` `mcpServers`, `src/index.ts`, `dist/index.js` | `opencode` MCP server (stdio, `bun ${CLAUDE_PLUGIN_ROOT}/dist/index.js`) exposing `delegate`, `delegate_async`, `task_result`, `task_list`, `task_cancel`, `list_models`, `list_sessions` to the worker (the main thread never calls them); its `instructions` (always in the system prompt) make spawning the background worker the default for implementation |
25+
| Manifests | `.claude-plugin/plugin.json`, `marketplace.json` | Plugin identity; `plugin.json` carries homepage/repository shown by Claude Code |
26+
27+
`src/` backs the MCP server (`src/index.ts`). The installed plugin cache has no `node_modules`, so what runs is the committed bundle `dist/index.js`: changing `src/` changes plugin behaviour only after `bun run build`, and CI fails if `dist/` is stale. Its exports (`delegate`, `resolveModel`, `buildArgs`, `EventCollector`) are also the reference for how `opencode run` must be invoked and how its JSON stream is parsed; `opencode-worker` calls the MCP `delegate` tool, which uses them.
2828

2929
- `plugin-hooks/` holds the plugin's Claude Code hooks (registered via the `hooks` field in `plugin.json`); `hooks/` holds this repo's git hooks. Never put `hooks.json` in `hooks/`. The routing script is tested by `test/route-hook.test.ts`.
3030

0 commit comments

Comments
 (0)