fix(opencode): migrate hooks plugin to V2 API and stop duplicate registrations

OpenCode 2.x only runs V2 plugin definitions ({ id, setup(ctx) }); the V1
function-export form failed schema validation at load with
"PluginModule.LoadError: Plugin must export a default definition with an id
and an effect or setup function".

- Rewrite .opencode/plugins/ecc-hooks.ts as a native V2 plugin: V1 hook keys
  now register on their V2 domains (ctx.tool.hook execute.before/after,
  ctx.shell.hook create.before, ctx.permission.hook evaluate,
  ctx.session.hook compaction, ctx.event.subscribe for file/session/todo
  events, ctx.tool.transform for custom tools). Windows-safe: replaces Bun
  dollar-shell and grep with child_process.spawn + fs; every callback is
  try/catch wrapped and every domain registration is guarded so a missing
  capability degrades instead of crashing setup.
- Stop registering the same plugin id three times: OpenCode scans root-level
  plugin files AND loads the directory through index.ts, so the directory
  form of the plugin config produced two red "Duplicate plugin ID:
  ecc-hooks" entries in the TUI panel. Config now points directly at
  ./plugins/ecc-hooks.ts and plugins/index.ts becomes an inert distinct-id
  stub (kept on disk because install/uninstall plans and consent safety
  tests reference it).
- Document the failure modes and the /api/plugin plus "opencode plugin list"
  diagnostic playbook in TROUBLESHOOTING.md; align .opencode/README.md.

Verified: hook-consent, opencode-hook-consent-safety, install-lifecycle,
install-executor, harness-adapter-compliance tests pass (197 assertions);
check-hooks-schema-keys, validate-install-manifests, validate-hooks and
unicode-safety CI validators pass.
This commit is contained in:
MohammadAminDHM committed 2026-11-03 16:58:51 +03:30
1 parent b38be6a7f6
commit 03cfca273b
6 files changed
+717 -628

No files matched your search

+8 -4
View File
@@ -224,7 +224,7 @@ Full configuration in `opencode.json`:
```json
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["./plugins"],
"plugin": ["./plugins/ecc-hooks.ts"],
"instructions": [
"skills/tdd-workflow/SKILL.md",
"skills/security-review/SKILL.md"
@@ -243,6 +243,10 @@ selection, and its subagents inherit the invoking primary agent's model.
MIT
Home-target installations keep `plugins/index.ts` inert even with hooks enabled.
OpenCode automatically loads `plugins/ecc-hooks.ts`; loading the barrel as well
would initialize every ECC hook twice. The source and published package barrel
remain available for module imports.
OpenCode 2.x scans every root-level file in a plugins directory **and** loads the
directory through `index.ts`, so the config points directly at
`plugins/ecc-hooks.ts` and the barrel stays a distinct-id stub
(`ecc-hooks-entry`). Re-exporting the real definition from `index.ts` would
register the id `ecc-hooks` two-to-three times, and OpenCode marks every entry
after the first as failed (`Duplicate plugin ID`). The source and published
package barrel remain available for module imports.
+1 -1
View File
@@ -37,4 +37,4 @@
// Export the main plugin
// opencode's legacy plugin loader iterates every module export and throws if
// any is not a plugin function, so only the plugin function may be exported.
export { default } from "./plugins/index.ts"
export { default } from "./plugins/ecc-hooks.ts"
+1 -1
View File
@@ -18,7 +18,7 @@
"skills/eval-harness/SKILL.md"
],
"plugin": [
"./plugins"
"./plugins/ecc-hooks.ts"
],
"skills": {
"paths": [
File diff suppressed because it is too large. Load diff
+23 -8
View File
@@ -1,12 +1,27 @@
/**
* ECC Plugins for OpenCode
* ECC plugins directory entry for OpenCode 2.x.
*
* This module exports all ECC plugins for OpenCode integration.
* Plugins provide hook-based automation that mirrors Claude Code's hook system
* while taking advantage of OpenCode's more sophisticated 20+ event types.
* This file is intentionally inert in normal installs. OpenCode 2.x loads
* every root-level `.ts` file in a plugins directory AND the directory
* itself through this entry point, so re-exporting the real plugin from
* here would register the id `ecc-hooks` twice and OpenCode would mark the
* extra entries `failed: Duplicate plugin ID` (shown in red in the TUI
* plugins panel).
*
* The configuration in `.opencode/opencode.json` therefore points directly
* at `./plugins/ecc-hooks.ts`. This stub keeps:
* - the file present for the installer/uninstall plan (`ecc repair`), and
* - a distinct id so a stray directory load can never collide with or
* shadow the real hooks plugin, and never throws the V2 schema error
* ("Plugin must export a default definition with an id and an effect or
* setup function") that a plain non-plugin module would cause.
*
* Do not add hook logic here; it belongs in `ecc-hooks.ts` only.
*/
export { ECCHooksPlugin, default } from "./ecc-hooks.ts"
// Re-export for named imports
export * from "./ecc-hooks.ts"
export default {
id: "ecc-hooks-entry",
setup() {
return () => {}
},
}
+80
View File
@@ -343,6 +343,86 @@ third-party [`oh-my-opencode-slim`](https://github.com/alvinunreal/oh-my-opencod
plugin, not ECC — ECC never writes to `~/.config/opencode/`. Fix the model prefix
(`opencode/...` instead of `openai/...`) there, or file it against that project.
### OpenCode 2.x: `PluginModule.LoadError` and red "Duplicate plugin ID" entries
**Symptoms (two distinct failures, often in sequence):**
1. Red startup banner / log lines:
```
PluginModule.LoadError: Plugin must export a default definition with an id and
an effect or setup function. (cause: SchemaError(Expected object at ["default"]))
```
reported against `plugins/ecc-hooks.ts` (global or project `.opencode/plugins/`).
2. After a first fix attempt, the TUI plugins panel shows `ecc-hooks` multiple
times with red entries reading `Duplicate plugin ID: ecc-hooks`, even though
the hooks themselves are working.
**Causes:**
- **V1 plugin on a V2 runtime.** OpenCode 1.x plugins default-export an async
function returning a keyed hook map. OpenCode 2.x requires a definition object
`{ id, setup(ctx) }` and does not run V1 implementations (see
[migrate-v1](https://opencode.ai/v2/docs/build/plugins/migrate-v1)). Cause A
produces the `Expected object at ["default"]` schema error.
- **Triple registration.** OpenCode 2.x scans every root-level `.ts` file in a
plugins directory **and** loads the directory itself through its `index.ts`.
If `index.ts` re-exports the same definition (or points at `ecc-hooks.ts`),
the id `ecc-hooks` registers two-to-three times; all but the first entry are
marked `failed: Duplicate plugin ID`.
**Fix layout (current design):**
- `.opencode/plugins/ecc-hooks.ts` is the only file carrying hook logic. V1 →
V2 registration mapping: `tool.execute.before/after` →
`ctx.tool.hook(...)`, `shell.env` → `ctx.shell.hook("create.before", ...)`,
`permission.ask` → `ctx.permission.hook("evaluate", ...)` (mutate
`event.effect`), keyed event handlers (`file.edited`, `session:*`,
`todo.updated`) → `ctx.event.subscribe()` filtered on `event.type`.
- `opencode.json` lists `"plugin": ["./plugins/ecc-hooks.ts"]` — the exact
file, **not** `"./plugins"` — so the directory is never loaded as a second
package.
- `plugins/index.ts` is an inert stub with the distinct id
`ecc-hooks-entry`. Never make it re-export the real definition: that is
exactly what triggers the duplicate-id failures. It must keep a default
export with an id + setup (a plain non-plugin module in a loaded directory
would throw the schema error again), and it must remain present on disk for
the install/uninstall plans.
**Diagnosing (do this before editing anything):**
1. Read the server log, and check the `run=` id and timestamp of every error
line — TUI banners survive from a process that predates your fix, so match
errors against the *current* run only:
```
~/.local/share/opencode/log/opencode.log # Windows, Linux, Termux
```
2. Ignore unrelated network noise when triaging plugins: `Failed to fetch
models.dev`, `failed to sync GitHub Copilot models: 403`, and
`update check failed` are connectivity/auth issues, not hook failures.
3. Query the live plugin table — this is the ground truth behind the TUI
panel. The server port comes from `opencode service status`; credentials
are Basic auth with the `password` field of `~/.config/opencode/service.json`:
```bash
curl -s -u "opencode:$(sed -n 's/.*"password": "\(.*\)".*/\1/p' ~/.config/opencode/service.json)" \
http://127.0.0.1:<port>/api/plugin | jq '[.data[] | select(.state.status != "active")]'
```
4. `opencode plugin list` shows each registered entry with its source path.
The same id appearing from two-plus sources predicts a duplicate-ID
rejection.
**Environment-variable note:** `ECC_HOOK_PROFILE` / `ECC_DISABLED_HOOKS` are
read once at plugin setup. On Windows, `setx` affects only *new* processes, and
a service restarted from a terminal that predates the `setx` still inherits the
stale environment block — restart from a freshly opened terminal.
---
## Performance Issues