Skip to content

feat(plugin): route OpenCode 2 worktrees through wt switch and wt remove - #4489

Merged
max-sixty merged 1 commit into
mainfrom
feat/opencode-worktree-strategy
Oct 17, 2026
Merged

max-sixty merged 1 commit into
mainfrom
feat/opencode-worktree-strategy

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

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):

  • 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, 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 fix(plugin): reopen an existing worktree from the WorktreeCreate hook #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 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.
Local check

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.

Closes #4488

The OpenCode plugin now registers a `worktrunk` worktree strategy through
`ctx.worktree.transform`, the OpenCode 2 counterpart of the Claude Code
plugin's WorktreeCreate/WorktreeRemove hooks:

- create: `wt switch [--create] <name> [--base <ref>] --no-cd --format=json`
  in the source checkout, where <name> is the basename of OpenCode's
  suggested directory and the returned path comes from `worktree-path`.
  An existing branch is reopened; a starting ref for one is refused.
- remove: `wt remove --foreground [--force] <dir>`, a no-op once the
  checkout is gone.
- list: `git worktree list --porcelain -z`, first record as root, skipping
  bare and prunable entries.

Closes #4488

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Holding this for maintainer review because it adds a deletion path. The OpenCode plugin now runs wt remove --foreground [--force] <dir> whenever OpenCode removes a worktree. It does this for every OpenCode 2 user who has the plugin installed, with no opt-in. OpenCode's built-in git strategy only runs git worktree remove. wt remove also deletes the branch once it is integrated, and runs any approved pre-remove hooks. So removing a workspace in OpenCode now deletes more than it did before.

The force flag passes straight through to wt remove --force, which discards untracked files. A name that matches an existing branch reopens that branch's worktree. That worktree is then registered as OpenCode-managed and can be removed from OpenCode's UI. That matches the Claude Code WorktreeCreate hook and OpenCode's own list adoption, but it widens the same surface.

@max-sixty

Copy link
Copy Markdown
Owner

The OpenCode plugin now runs wt remove --foreground [--force]

it runs with force?

@max-sixty

Copy link
Copy Markdown
Owner

OpenCode 2 user who has the plugin installed, with no opt-in. OpenCode's built-in git strategy only runs git worktree remove

i.e. the standard one doesnt use force?

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Only when OpenCode passes force: true, and its built-in git strategy forces in exactly the same cases. My review comment made that sound new; it isn't. The plugin adds --force only when OpenCode sets force (dev/opencode-plugin.ts:131). The git strategy maps the same flag to git worktree remove --force.

OpenCode sets force in two places:

  • TUI workspace dialog: it first removes with force: false. It retries with force: true only after a forceRequired error and a confirmation showing the changed files. Our plugin's errors can't carry forceRequired, which is the "no force prompt" caveat in the PR body. So the TUI never sends --force to wt, and a dirty worktree simply fails to remove.
  • Desktop/web settings: deleting a single workspace from its confirmation dialog passes force: true unconditionally. "Delete all" passes false. With the git strategy that confirmed delete already discards dirty files. With ours it runs wt remove --force, which discards the same staged, modified, and untracked files.

So force is not where the deletion surface widens. What wt remove adds over git worktree remove: it deletes the branch once it is integrated (we never pass -D, so unmerged branches stay), and it runs approved pre-remove hooks.

@max-sixty
max-sixty merged commit ca0563d into main Oct 17, 2026
38 checks passed
@max-sixty
max-sixty deleted the feat/opencode-worktree-strategy branch October 17, 2026 22:06
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 added built-in worktree management

2 participants