Files
Worktrunk Botandworktrunk-bot ca0563dc8d feat(plugin): route OpenCode 2 worktrees through wt switch and wt remove (#4489)
Under OpenCode 2, the plugin that `wt config plugins opencode install`
writes now registers a `worktrunk` worktree strategy. Worktrees that
OpenCode creates and removes then go through `wt switch --create` and
`wt remove`, the same way the Claude Code plugin's
`WorktreeCreate`/`WorktreeRemove` hooks already route them. This was
requested in #4488. I ran the strategy locally against a scratch repo
with this branch's `wt` and a fake OpenCode context. I haven't run it
inside a real OpenCode 2 session, which CI can't drive.

How each callback maps
([`dev/opencode-plugin.ts`](https://github.com/max-sixty/worktrunk/blob/1c906f5599d7a16df832c56c51aa82d13c82bbd3/dev/opencode-plugin.ts)):

- **create**: runs `wt switch [--create] <name> [--base <ref>] --no-cd
--format=json` in the source checkout and returns `.path`. OpenCode's
`branch` input is a [starting ref, not a branch
name](https://github.com/anomalyco/opencode/blob/dev/packages/plugin/src/worktree.ts),
so `<name>` is the basename of the directory OpenCode suggests (the name
the user typed, or a generated slug). The path comes from
`worktree-path`, since OpenCode accepts a different directory than the
one it suggested. An existing branch is reopened, as in #4487. Passing a
starting ref for an existing branch is refused rather than ignored.
- **remove**: runs `wt remove --foreground [--force] <dir>`. It does
nothing if the checkout is already gone.
- **list**: reads `git worktree list --porcelain -z` and reports the
first record as `root`. Bare and prunable entries are skipped.

Things a maintainer may want to weigh:

- **On by default.** OpenCode selects a strategy as soon as a plugin
adds one. Every OpenCode 2 user with the plugin installed switches to
worktrunk-managed worktrees, and OpenCode's `worktree.directory` setting
stops applying. This matches the Claude Code plugin. The docs page says
so.
- **No force prompt on a dirty worktree.** OpenCode only offers to force
when the error is an instance of its own `Worktree.OperationError`
([`operationError`](https://github.com/anomalyco/opencode/blob/dev/packages/core/src/worktree.ts)
uses `instanceof`). A standalone plugin file can't import that class:
the CLI's runtime shim for `@opencode/plugin` doesn't export `Worktree`.
A dirty removal fails with worktrunk's own message instead (`Cannot
remove worktree: … has uncommitted changes`), and nothing is deleted.
- **Default base.** With no starting ref, the new branch starts from
worktrunk's default (the default branch). OpenCode's `git` strategy uses
the source checkout's HEAD instead.

<details><summary>Local check</summary>

A Node harness imported the plugin, called `setup` with a stub
`ctx.worktree.transform`, and ran each callback against a fresh repo
with `target/debug/wt` on PATH:

- create → `ocrepo.brave-tiger` (the `worktree-path` default), and
`list` returned root + that worktree
- create with `branch: "brave-tiger"` → a second worktree based on it
- create for an existing branch with a starting ref → refused. Without a
ref → reopened the existing worktree
- remove of a dirty worktree → wt's error. With `force` → removed. A
repeat remove → no-op
- final `list` → root only

`test_docs_are_in_sync` and the 14 `opencode` integration tests pass.

</details>

Closes #4488

Co-authored-by: worktrunk-bot <254187624+worktrunk-bot@users.noreply.github.com>
2026-10-17 15:06:01 -07:00
..