Skip to content

fix(plugin): reopen an existing worktree from the WorktreeCreate hook - #4487

Merged
max-sixty merged 1 commit into
mainfrom
fix/issue-4486
Oct 16, 2026
Merged

max-sixty merged 1 commit into
mainfrom
fix/issue-4486

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Since Claude Code 2.1.300, plugin WorktreeCreate hooks run for claude --worktree NAME, and Claude Code fires the hook again when NAME is a worktree an earlier session kept. The plugin always ran wt switch --create NAME, so reopening failed with Branch NAME already exists. The hook now omits --create when local branch NAME exists, so wt switch prints the existing worktree's path, matching Claude Code's built-in behavior ("Passing --worktree a name whose directory already exists opens that existing worktree", worktrees docs).

The fix covers both hook paths:

test_claude_hook_worktree_create previously asserted that a second create with the same name fails. It now asserts that the second create reopens the same path. Before the fix it failed with the error from the issue. The failure path (no stdout and a nonzero exit, #3545) is still asserted, using a project dir that is not inside a repository.

What wasn't verified: the run inside Claude Code itself. I ran the hooks.json command by hand against a scratch repo with CLAUDE_PLUGIN_ROOT/CLAUDE_PROJECT_DIR set: create, reopen, an existing branch without a worktree, and a non-repo project dir. I did not test it under claude --worktree.

One behavior to weigh: claude --worktree NAME where NAME is a pre-existing branch the user made by hand now opens (or creates a worktree for) that branch rather than failing. A clean exit then fires WorktreeRemove on it. That hook runs wt remove without force, so it refuses dirty worktrees and keeps unmerged branches (#2939).

Closes #4486 — automated triage

Claude Code 2.1.300 runs plugin WorktreeCreate hooks for `claude --worktree
NAME`, including when NAME names a worktree a previous session kept. The
hook always passed `--create`, so reopening failed with "Branch NAME
already exists". Both the shipped hooks.json command and the
`wt config plugins claude hook` handler now drop `--create` when the local
branch exists, so `wt switch` returns the existing worktree's path.

Closes #4486
@worktrunk-bot worktrunk-bot added the automated-fix Automated CI fix label Oct 16, 2026

@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.

The reopen check is "local branch NAME exists", which is wider than "a worktree an earlier claude --worktree session kept". That widens what the shipped WorktreeRemove hook can delete. Take a clean linked worktree the user made by hand, such as wt switch --create feature, or the default-branch worktree in a bare-repo layout. Before this PR, claude --worktree feature failed with Branch feature already exists. Now wt switch feature returns that worktree's path, and Claude Code opens the session there. When the session exits clean, Claude Code fires WorktreeRemove for the worktree (the handle_claude_hook doc comment says it does this "for any clean worktree"). The hook then runs wt remove --foreground <path>, which removes the user's own worktree, including its ignored files (.env, build outputs), and deletes the branch if it is integrated. Only the main worktree is still safe, because wt remove refuses it.

One narrower option: reopen only when the existing worktree is at NAME's worktree-path template location, which is where the hook's own --create put it. That keeps the #4486 fix and excludes most hand-made worktrees. It still changes which sessions reopen, so it's a call for a maintainer to make.

This change is shipped hook automation that can now delete a worktree it used to leave alone, so it's held for maintainer review.

@max-sixty
max-sixty merged commit 56890db into main Oct 16, 2026
38 of 48 checks passed
@max-sixty
max-sixty deleted the fix/issue-4486 branch October 16, 2026 20:39
max-sixty pushed a commit that referenced this pull request Oct 17, 2026
…ove (#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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automated-fix Automated CI fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Claude plugin WorktreeCreate hook fails when claude --worktree NAME reopens an existing worktree

2 participants