Skip to content

opencode: find the dispatcher wherever the plugin is installed - #229

Open
Have-Purple wants to merge 1 commit into
microsoft:mainfrom
Have-Purple:fix/opencode-dispatcher-path
Open

Have-Purple wants to merge 1 commit into
microsoft:mainfrom
Have-Purple:fix/opencode-dispatcher-path

Conversation

@Have-Purple

@Have-Purple Have-Purple commented Nov 14, 2026 •

Copy link
Copy Markdown

Fixes #228.

Problem

The opencode plugin assumed its launchers live at <plugin dir>/../scripts:

const SCRIPTS = join(HERE, "..", "scripts");

That is true in the checkout, but not for the copy opencode loads from ~/.config/opencode/plugins, where it resolves to ~/.config/opencode/scripts — which does not exist. Every hook then failed to spawn its dispatcher, and the fail-open path answered {}, so there was no fold, no injection, no loop guard, and no log entry to notice. Details and repro: #228.

Change

Resolve the dispatcher instead of assuming a layout, first hit wins:

  1. TE_PLUGIN_ROOT → <root>/scripts
  2. TE_CORE_PATH → <root>/plugin/scripts (the checkout pointer the bootstrap already documents)
  3. the layout the plugin ships in: <here>/../scripts
  4. the state-root launcher: ~/.tokeneconomizer/bin/te is exec '<plugin>/scripts/te' "$@", so its directory is the dispatcher's

Nothing walks up from the plugin's directory. An unrelated ancestor would win over the dispatcher the plugin ships with, and on Windows the walk ends at C:\, where a standard user can create folders by default — a planted scripts/te_bootstrap.py would run in every hook. The last-resort expression is unchanged.

plugin/README.md now covers the copied plugin: TE_CORE_PATH (or TE_PLUGIN_ROOT) set where the 2.x service starts, plus opencode service restart. The state launcher cannot make the first hook work, since it is written once a hook has reached the dispatcher.

Tests

In tests/test_opencode_plugin.py (run by the existing opencode CI job):

  • an installed copy with no launchers beside it resolves through TE_CORE_PATH and writes dispatcher state;
  • the existing bundle layout (launchers + lib beside the plugin) still resolves with TE_CORE_PATH unset;
  • a scripts/te_bootstrap.py planted above the copy is not run;
  • the state launcher resolves when its exec target holds a space (the shlex.quote form);
  • with no anchor at all the session still runs and the dispatcher starts nothing (fail-open).

The assertion is dispatcher state, not hook-process count: a hook that cannot find te_bootstrap.py still starts a Python, which would make a process-count test pass with the bug.

Verification

Rebased onto main 583fff7. opencode 2.3.0 on Windows: the six plugin tests (all but the two service tests) pass. Full offline suite: 1167 passed, 40 skipped, 0 failed. Restoring the ancestor walk turns the planted-stub test red; removing the quote strip from the launcher parse turns the space-path test red.

@Have-Purple

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@Have-Purple Have-Purple reopened this Nov 14, 2026

@aaron-jx-li Aaron Li (aaron-jx-li) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the contribution and for the clear diagnosis in #228. This PR needs a few changes before it can be merged:

  1. plugin/opencode/tokeneconomizer.ts:72-88 walks up to 16 ancestors of the plugin's directory and takes the first scripts/te_bootstrap.py or plugin/scripts/te_bootstrap.py it finds, before it looks at the launcher. The walk reaches the filesystem root, so a te_bootstrap.py in any unrelated ancestor wins over the person's own ~/.tokeneconomizer/bin/te (a copy under <x>/home/u/.config/opencode/plugins/ with <x>/scripts/te_bootstrap.py present resolves to <x>/scripts). On Windows the walk ends at C:\, where standard users can create folders by default, so another account could plant C:\scripts\te_bootstrap.py and every hook would run it. Please check only the layouts the plugin ships in (<here>/../scripts), then the launcher, and drop the open-ended walk.
  2. The case in #228 (a copy with nothing beside it, on a fresh install) still fails silently unless TE_CORE_PATH or TE_PLUGIN_ROOT is set: the launcher only exists after a hook has reached the dispatcher (tokeneconomizer/plugin/controller.py:105). Neither variable is mentioned in the opencode section of plugin/README.md, which still says to keep the plugin next to scripts/ and lib/. Please add a line there for a copied plugin: set TE_CORE_PATH to the checkout, and on 2.x restart the service so it reaches the plugin.
  3. The launcher fallback (tokeneconomizer.ts:89-103) parses the file paths.ensure_launcher writes, and no test covers it. Please add one with no TE_CORE_PATH and a launcher whose target path contains a space (the quoted form shlex.quote writes).

The plugin assumed its launchers sit at <plugin dir>/../scripts. That holds in the checkout,
but not for the copy opencode loads from ~/.config/opencode/plugins, where it resolves to
~/.config/opencode/scripts -- which does not exist. Every hook then failed to spawn, and the
fail-open path answered {}, so there was no fold, no injection, no loop guard, and no log to
notice.

Resolve instead: TE_PLUGIN_ROOT, TE_CORE_PATH (the checkout pointer the bootstrap already
reads), the layout the plugin ships in, and the state-root launcher
(~/.tokeneconomizer/bin/te -> <plugin>/scripts/te). Nothing walks up from the plugin's
directory: an unrelated ancestor would win over the dispatcher the plugin ships with, and on
Windows the walk ends at C:\, where a standard user can create folders by default, so a planted
scripts/te_bootstrap.py would run in every hook.

Tests assert dispatcher state, not hook-process count: a hook that cannot find
te_bootstrap.py still starts a Python, so counting processes would pass with the bug. The
review's cases are covered: a planted ancestor must not run, and the launcher's quoted form
(shlex.quote) must resolve when the target path holds a space.
@Have-Purple
Have-Purple force-pushed the fix/opencode-dispatcher-path branch from 35b4e50 to 2f0fb5a Compare November 16, 2026 13:33
@Have-Purple

Copy link
Copy Markdown
Author

All three items are in 2f0fb5a, rebased onto 583fff7.

  1. The ancestor walk is gone. The order is TE_PLUGIN_ROOT, TE_CORE_PATH, the shipped layout <here>/../scripts, then the launcher; the last-resort expression is unchanged. test_a_scripts_directory_above_the_copy_is_not_an_anchor plants a stub at <tmp>/scripts/te_bootstrap.py one level above the copy and asserts it never runs — red against the previous commit.
  2. The docs cover the copied plugin. plugin/README.md gained "A copied plugin, not a link": TE_CORE_PATH or TE_PLUGIN_ROOT, the note that the launcher cannot help the first hook (it is written once a hook has reached the dispatcher), and the 2.x opencode service restart.
  3. The launcher parse has a test. test_the_state_launcher_is_read_when_its_target_has_a_space writes the launcher as exec '<checkout with a space>/plugin/scripts/te' "$@"; removing the quote strip from the parse turns it red.

The two TE_ variables stay first: without one, a copy on a fresh install cannot reach the dispatcher on its first hook at all.

Evidence: full offline suite 1167 passed, 40 skipped, 0 failed; the six non-service plugin tests pass against opencode 2.3.0 on Windows.

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: plugin loads but nothing fires when installed as a copy (dispatcher path)

2 participants