Skip to content

fix(opencode): load the plugin on OpenCode 2 and refresh installed copies (#3554, #3732) - #4039

Closed
bercedev wants to merge 1 commit into
Graphify-Labs:v8from
bercedev:fix/opencode-v2-plugin
Closed

bercedev wants to merge 1 commit into
Graphify-Labs:v8from
bercedev:fix/opencode-v2-plugin

Conversation

@bercedev

@bercedev bercedev commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #3554 and #3732.

graphify opencode install writes a plugin in the OpenCode 1 shape. On OpenCode 2 that install fails twice on every start. This is the server log of OpenCode 2.0.22 opened in a project that graphify 0.9.74 set up:

failed to load plugin target=<project>/.opencode/plugins/graphify.js
  PluginModule.LoadError: Plugin must export a default definition with an id and an effect or setup function.
  (cause: SchemaError(Missing key at ["default"]))
failed to load plugin target=.opencode/plugins/graphify.js
  NpmInstallFailedError (cause: Error: Could not read package.json: Error: ENOTDIR: not a directory,
  open '<project>/.opencode/plugins/graphify.js/package.json')

The first error is the plugin file, which only has a named export. The second is the "plugin": [".opencode/plugins/graphify.js"] entry in .opencode/opencode.json: OpenCode 2 reads a bare path as an npm package name.

There are three changes.

1. One plugin file for both runtimes. The default export has id, setup(ctx) for OpenCode 2 and server() for OpenCode 1. Each runtime ignores the other's function. On OpenCode 2 the command tool is called shell, and the server is a background service that outlives a session, so the reminder is tracked per session there. The file imports nothing from an opencode package, because a plain file in .opencode/plugins/ can't resolve one.

The plugin id carries the file's own location (graphify-<hash>). OpenCode loads .opencode/plugins/ from the project and from each directory above it, and OpenCode 2 fails a second plugin with the same id: Duplicate plugin ID: graphify, shown as "1 plugin failed". With one install in ~ and one in a project, that happened in every such project. The copies share one record of who was reminded, so a command still gets a single reminder. On OpenCode 1 two copies used to add it twice.

2. No opencode.json entry. Both runtimes load every file in .opencode/plugins/ on their own (OpenCode 1.2.0 and later), so the entry is not needed. Install no longer writes it. Install and uninstall remove the entry older releases wrote, under plugin or plugins, and leave everything else in the file alone. If the file has comments, it is not rewritten, because that would drop them. Install prints what to remove instead.

3. Installed copies are refreshed. Upgrading graphify does not touch a plugin that is already on disk. So someone who upgrades OpenCode keeps both errors until they find out they have to run the install again in each project. The CLI now rewrites an old-shape plugin on its next run, next to the stale-skill refresh from #1805:

$ graphify --version
graphify: refreshed <project>/.opencode/plugins/graphify.js so OpenCode 2 can load it; set GRAPHIFY_NO_AUTO_REFRESH=1 to disable
graphify <version>
$ graphify --version
graphify <version>

So nobody has to reinstall by hand: each project is upgraded the first time graphify runs in it after the upgrade, whether that is a graphify query, an update, or the post-commit hook.

It looks in the working directory and in the home directory. Home matters because graphify opencode install run from ~ puts the plugin in ~/.opencode/plugins/ (#1412), and OpenCode loads that one for every project below home. That is how I ran into this. GRAPHIFY_NO_AUTO_REFRESH=1 turns it off, the same switch as the skill refresh.

An old plugin is replaced only when the opencode on PATH reports 1.3.4 or later. Older OpenCode 1 releases can't load the new file, and the plugin they have still works, so it stays. The version is asked once (opencode --version), and only when an old plugin is actually found. If there is no opencode on PATH, nothing is replaced.

Only a plugin that graphify wrote is touched. The stale entry is also dropped when the plugin next to it is already current, because a checkout can bring opencode.json back on its own. If the rewrite fails, for example in a read-only checkout, the command still runs and stderr gets one graphify: could not refresh ... line. The rewrite takes the same per-directory lock as the skill refresh, without waiting, and both files are replaced with a temp file and one rename (write_text_atomic), so OpenCode, which watches them, never sees half a plugin.

About the other open PRs

#3792, #3819 and #3896 are open for the same issues, so I checked what was still missing before sending another one:

Happy to rebase on top of one of them or to split this up if you prefer a different order.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

Invariant: after graphify opencode install, and after any graphify CLI run in a project that has an older install, OpenCode 1 (1.3.4 and later) and OpenCode 2 both load .opencode/plugins/graphify.js without an error, and the first shell command of a session carries the reminder.

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

Limitations:

  • OpenCode 1 before 1.3.4 can't load the new file. Those releases call every export as a function, and OpenCode 2 needs the default export to be an object, so one file can't serve both. I took the releases that read server() from a default export (1.3.4, March 2026) as the floor. I got this from the OpenCode source. I did not run a release that old.
  • The refresh leaves such an old OpenCode alone, but an explicit graphify opencode install there still writes a plugin that release can't load.
  • The refresh needs opencode on PATH to ask for the version. Where it isn't (some hook environments, a desktop-only install), nothing is refreshed and graphify opencode install is still the way.
  • This is the first refresh that writes inside a project. The skill refresh from Package upgrade leaves installed skill stale — version-mismatch warning never reaches the agent that consumes the skill #1805 only writes under the user's home. If .opencode/plugins/graphify.js or .opencode/opencode.json is tracked in git, the first graphify run after the upgrade leaves them modified, also when that run comes from the post-commit hook.
  • The refresh only looks in the working directory and in home. A plugin in some other parent directory is refreshed when graphify next runs there.
  • Only .opencode/opencode.json is cleaned, as before. An entry that someone moved to opencode.jsonc or to the global config stays, and so does an entry in a file with comments.
  • A plugin that was edited by hand is replaced too, if it still starts with graphify's header and has no default export. OpenCode 2 could not load it either way.
  • An opencode.json that only held graphify's entry is left as {}, the same as uninstall did before.

How was this tested?

# new and changed tests against the unchanged v8 sources
.venv/bin/python -m pytest -q tests/test_opencode_plugin_refresh.py tests/test_install.py -k opencode
47 failed, 9 passed

# with the fix
.venv/bin/python -m pytest -q tests/test_opencode_plugin_refresh.py tests/test_install.py tests/test_install_strings.py tests/test_skill_auto_refresh.py
204 passed

.venv/bin/python -m pytest -q
6339 passed, 14 skipped

.venv/bin/ruff check graphify/install.py graphify/__main__.py tests/test_install.py tests/test_opencode_plugin_refresh.py
All checks passed!

.venv/bin/pyright graphify/install.py graphify/__main__.py
5 errors (the same 5 as on v8, none in the changed lines)

Three tests drive the installed file with node the way each runtime calls it: one for OpenCode 2, one for OpenCode 1, and one with two copies of the file at different paths. They are skipped when node is missing, like the node tests in test_export.py.

I also ran both runtimes for real, each with its own XDG directories, in a scratch project that has graphify-out/graph.json:

  • OpenCode 2.0.22 with the 0.9.74 install: the two errors at the top.

  • graphify --version from this branch in that project: the refresh line shown above, nothing on the second run, and opencode.json no longer has the entry.

  • The same refresh with no opencode on PATH, and with a fake opencode that prints 1.3.3: the old plugin and the entry stay as they are.

  • OpenCode 2.0.22 again: the plugin loads with no error. I asked for two shell commands. The server log shows what was spawned:

    echo "[graphify] knowledge graph at graphify-out/. For focused questions, ..." ; echo first-call
    echo second-call
    
  • OpenCode 1.18.34 with a fresh graphify opencode install from this branch, so with no opencode.json at all: the plugin is discovered, the first bash call got the reminder and the second did not.

  • Two copies, one in the parent directory and one in the project. OpenCode 2.0.22 lists both as active (with a shared id the second one was failed: Duplicate plugin ID: graphify), and the first shell command carries one reminder. OpenCode 1.18.34 loads both, and the first bash command carries one reminder.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments.
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

Note: I used Claude Code while working on this fix. I reviewed and ran all of it myself.

@bercedev
bercedev requested a review from safishamsi as a code owner October 3, 2026 17:55
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown

Thanks for the pull request, @bercedev. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 2 advisory finding(s) below merit a look before merge.

Formal verification. PR-changed functions: 3/4 verified (0 proven, 3 may-equivalent, 0 distinguished) · 1 not verified (1 witness not reproduced).

Not verified on this run: \_install\_opencode\_plugin (witness not reproduced).


Graphify review — findings

Rewrites the OpenCode plugin as a single default export that works on both runtimes: setup() for OpenCode 2, which hooks shell and reminds once per session, and server() for OpenCode 1.3.4+, which hooks bash. The install no longer registers the plugin in opencode.json, since OpenCode finds files in .opencode/plugins/ on its own. On every CLI run, _refresh_v1_opencode_plugins checks the working and home directories, replaces any graphify-written v1-only plugin, and drops the stale config entry; a commented (non-JSON) config is left alone with a "remove by hand" hint, and GRAPHIFY_NO_AUTO_REFRESH=1 keeps the old plugin for older OpenCode 1 users.

Worth a look

  • Every CLI run silently rewrites the OpenCode plugin, which can break OpenCode 1 versions older than 1.3.4 — graphify/__main__.py:798 · Escalate · medium
    • agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
  • Unlocked read-modify-write of OpenCode plugin and opencode.json on every CLI run — graphify/__main__.py:424 · Escalate · medium
    • agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 653 functions depend on the 391 functions this change touches.

Health — this change adds coupling hotspots:

  • new: main() — 98 callers, 3 callees
  • new: dispatch_command() — 2 callers, 126 callees
  • new: install() — 14 callers, 11 callees
  • new: _refresh_stale_skills() — 25 callers, 5 callees
  • new: _copy_skill_file() — 15 callers, 7 callees
  • new: codebuddy_install() — 20 callers, 5 callees
  • new: gemini_install() — 11 callers, 7 callees
  • new: claude_install() — 19 callers, 4 callees
  • …and 21 more — each is listed as a finding

Verification — 653 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 653 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

33 of 319 test file(s) selected (10%) via static blast radius.

  • tests/test_affected_cli.py — impact
  • tests/test_agents_platform.py — impact
  • tests/test_atomic_version_stamp.py — impact
  • tests/test_claude_md.py — impact
  • tests/test_codebuddy.py — impact
  • tests/test_devin.py — impact
  • tests/test_explain_cli.py — impact
  • tests/test_extract_cli.py — impact
  • tests/test_gemini_hook.py — impact
  • tests/test_global_add_tag_inference.py — impact
  • tests/test_god_nodes_cli.py — impact
  • tests/test_hollow_chunks_arm_shrink_guard.py — impact
  • tests/test_home_sandbox.py — impact
  • tests/test_hook_strict.py — impact
  • tests/test_incomplete_build_guard.py — impact
  • tests/test_install.py — impact, changed-test
  • tests/test_install_references.py — impact
  • tests/test_install_strings.py — impact
  • tests/test_install_version_warning.py — impact
  • tests/test_merge_chunks_validation.py — impact
  • tests/test_multigraph_diagnostics.py — impact
  • tests/test_no_dedup_flag.py — impact
  • tests/test_opencode_plugin_refresh.py — impact, changed-test
  • tests/test_path_cli.py — impact
  • tests/test_query_cli.py — impact
  • tests/test_query_induced_edges.py — impact
  • tests/test_read_hook.py — impact
  • tests/test_replace_or_append_section.py — impact
  • tests/test_search_hook.py — impact
  • tests/test_skill_auto_refresh.py — impact
  • tests/test_skill_version_warning.py — impact
  • tests/test_uninstall_scope.py — impact
  • tests/test_unverified_semantic_shrink.py — impact

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Docs that may be stale (advisory)

…and 10 more.

Formal verification

Could not verify: \_install\_opencode\_plugin: a possible difference could NOT be confirmed.

The solver proposed a distinguishing input for \_install\_opencode\_plugin, but re-running both versions on it did NOT reproduce a difference. Treat as unconfirmed, not a behavior change.

Guarantee:

Note: Solver-proposed witness refuted on replay — not a hard failure.

No difference found (not proven): No behavior difference found in \_refresh\_stale\_skills (not a proof).

The verifier ran both versions of \_refresh\_stale\_skills on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

No difference found (not proven): No behavior difference found in \_run\_cli (not a proof).

The verifier ran both versions of \_run\_cli on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

No difference found (not proven): No behavior difference found in \_uninstall\_opencode\_plugin (not a proof).

The verifier ran both versions of \_uninstall\_opencode\_plugin on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

· 2 grounded finding(s) anchored inline below; 27 more finding(s) on lines outside this diff (see the check run).

Comment thread graphify/__main__.py
return os.environ.get("GRAPHIFY_NO_AUTO_REFRESH", "").strip().lower() in ("1", "true", "yes")


def _refresh_stale_skills() -> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Health regression — _refresh_stale_skills()

25 callers depend on it (afferent coupling).

Grounded coupling-delta finding (deterministic), not an LLM guess.

Comment thread graphify/__main__.py
print(line, file=sys.stderr)


def _refresh_v1_opencode_plugins() -> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Health regression — _refresh_v1_opencode_plugins()

12 callers depend on it (afferent coupling).

Grounded coupling-delta finding (deterministic), not an LLM guess.

@bercedev
bercedev force-pushed the fix/opencode-v2-plugin branch 2 times, most recently from 58a27d3 to e86a05d Compare October 3, 2026 22:41
…pies (Graphify-Labs#3554, Graphify-Labs#3732)

`graphify opencode install` wrote a plugin with only a named export, the
OpenCode 1 shape. OpenCode 2 rejects it ("Plugin must export a default
definition with an id and an effect or setup function") and reads the bare
path registered in opencode.json as an npm package name, so it fails twice
on every start.

The plugin is now one default export with `id`, `setup()` for OpenCode 2
and `server()` for OpenCode 1 (1.3.4 and later). The installer no longer
writes the opencode.json entry, because both runtimes discover
.opencode/plugins/ on their own, and it removes the entry older releases
wrote.

OpenCode loads .opencode/plugins/ from the project and from every directory
above it, and OpenCode 2 fails a second plugin with the same id ("Duplicate
plugin ID"). An install in ~ and one in a project are both normal, so the id
carries the file's location and the copies share one record of who was
reminded: both load, and a command still gets a single reminder.

Upgrading graphify does not touch a plugin already on disk. So the CLI now
rewrites an old-shape plugin in the working directory or in the home
directory on its next run, and drops the stale entry there, next to the
stale-skill refresh from Graphify-Labs#1805. It does so only when the `opencode` on PATH
reports 1.3.4 or later, because older OpenCode 1 releases cannot load the
new plugin and the one they have still works. The rewrite takes the same
per-directory lock as the skill refresh and replaces both files atomically,
because OpenCode watches them. GRAPHIFY_NO_AUTO_REFRESH=1 turns it off.
@bercedev
bercedev force-pushed the fix/opencode-v2-plugin branch from e86a05d to 0e539e1 Compare October 3, 2026 22:42
@bercedev

bercedev commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

Pushed an update. It is a single commit now (0e539e1), and the description above matches it.

It covers both advisory findings from the review bot:

  • The refresh rewrites an old plugin only when the opencode on PATH reports 1.3.4 or later. An older OpenCode 1 keeps the plugin that works for it.
  • The rewrite takes the same per-directory lock as the skill refresh and replaces both files atomically.

It also fixes something I hit on my own machine. With one install in ~ and one in a project, OpenCode 2 failed the second copy with Duplicate plugin ID: graphify. The plugin id now carries the file's location, so both copies load, and together they still add the reminder only once.

safishamsi pushed a commit that referenced this pull request Oct 10, 2026
@safishamsi

Copy link
Copy Markdown
Member

Landed in 0.9.84 (now on PyPI) via an authorship-preserving cherry-pick (commit 652020e), so your authorship is retained on the v8 history and the contributor graph. The plugin now loads on OpenCode 2 (a default export with setup()) while keeping the v1 server() hook, and graphify install refreshes an already-installed v1-only copy so the graph tools reappear for users upgraded past the v1 contract. Closing the PR since the change has shipped. Thank you, @bercedev.

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.

Opencode v2 plugin support

2 participants