memorix icon indicating copy to clipboard operation
memorix copied to clipboard

[Bug] OpenCode plugin not auto-loaded: `memorix hooks install` generates local JS file but OpenCode plugin array requires npm package names

Open skywalker-35 opened this issue 7 months ago • 1 comments

Description

memorix hooks install --agent opencode --global generates a local plugin file at ~/.config/opencode/plugins/memorix.js, but OpenCode never loads it. OpenCode's plugin array in opencode.json expects npm package names (e.g., oh-my-openagent, opencode-mem), not local file paths. OpenCode does not auto-scan the plugins/ directory.

Adding "memorix" to the plugin array causes OpenCode to hang for 5+ minutes on startup (it tries to resolve it from npm, and since memorix is a CLI tool, not an OpenCode plugin package, the resolution hangs/times out).

Environment

  • OS: Windows 11 (PowerShell)
  • OpenCode: v1.4.6 (via opencode-windows-x64)
  • Memorix: v1.0.6 (globally installed via npm)
  • Node/Bun: OpenCode uses Bun runtime for plugins

Steps to Reproduce

  1. Install memorix globally: npm install -g memorix
  2. Run: memorix hooks install --agent opencode --global
  3. Verify the file was created: ~/.config/opencode/plugins/memorix.js exists ✅
  4. Verify hooks status: memorix hooks status shows [OK] Opencode ✅
  5. Start OpenCode — the plugin file is never loaded (no session.created events captured)
  6. (Optional) Add "memorix" to opencode.json plugin array → OpenCode hangs on startup for 5+ minutes

Root Cause Analysis

The installer generates a local JS file at ~/.config/opencode/plugins/memorix.js:

export const MemorixPlugin = async ({ project, client, $, directory, worktree }) => {
  // ... hooks into OpenCode events, pipes to `memorix hook`
};

But OpenCode's plugin array expects npm package names. Looking at the source:

// src/hooks/installers/index.ts
function getGlobalConfigPath(agent: AgentName): string {
  // ...
  case 'opencode':
    return path.join(home, '.config', 'opencode', 'plugins', 'memorix.js');
}

The file is written to the plugins directory, but OpenCode's plugin loading only resolves entries from the plugin array as npm packages. The plugins directory is not auto-scanned.

Other agents (Claude, Cursor, Gemini, etc.) use JSON-based hooks configs that are properly merged into the agent's settings file. OpenCode is the only agent that uses a JS plugin file, but the installer doesn't register it in opencode.json.

Expected Behavior

Either:

  1. The installer should also add the local plugin path to opencode.json (if OpenCode supports local file references in the plugin array, e.g., file:///path/to/memorix.js or ./plugins/memorix.js)
  2. Or publish an npm package (e.g., memorix-opencode) that re-exports the plugin, so "memorix-opencode" can be added to the plugin array

Current Workaround

Manually call memorix_store via MCP tools in conversations (the AGENTS.md rules prompt the agent to do this). This works but defeats the purpose of automatic capture.

Additional Context

  • memorix hooks status reports [OK] Opencode even though the plugin isn't loaded — the status check only verifies the file exists, not that OpenCode actually loads it
  • Formation Pipeline shows "No metrics collected yet" because no hook events are being captured
  • Hook traces from manual memorix hook CLI calls (stdin piping) do get recorded, confirming the backend works — only the OpenCode→memorix bridge is broken

skywalker-35 avatar Apr 16 '26 07:04 skywalker-35

This has been fixed in the 1.0.7 development cycle. The fix involved several changes:

Root cause: The original plugin used an invalid catch-all \event\ handler that OpenCode never called. Additionally, the issue title references \opencode.json\ plugin array but that array is for npm package names only. Local JS plugins in .opencode/plugins/\ are auto-loaded by OpenCode at startup and do NOT need registration in \opencode.json.

What was fixed:

  1. Plugin now uses individual event-name keys (\session.created, \ ile.edited, \command.executed, etc.) instead of the invalid catch-all \event\ handler
  2. Replaced \Bun.spawn\ with \child_process.spawnSync\ for cross-runtime compatibility (OpenCode may fall back to Node.js on Windows where Bun segfaults)
  3. Fixed \ ile.edited\ field extraction (\input.path\ before \input.file)
  4. Added exit code and stderr diagnostic logging + 10s timeout to prevent hangs
  5. Hooks status now distinguishes verified (config-based) vs unverified (plugin-based) agents
  6. Added generated-version markers so outdated plugin installs are detected and can be reinstalled

How it works now: \memorix hooks install\ (or \memorix integrate) writes .opencode/plugins/memorix.js. OpenCode auto-loads local plugins from that directory no \opencode.json\ registration needed. The \plugin\ array in \opencode.json\ is exclusively for npm packages.

Closing as fixed.

AVIDS2 avatar Apr 18 '26 08:04 AVIDS2