Git worktrees for isolated AI sessions, without terminal spawning.
An OpenCode plugin that creates isolated git worktrees, syncs selected files, runs lifecycle hooks, and tracks cleanup state.
Manual worktrees are powerful but repetitive for AI workflows: create the worktree, set up context, and remember cleanup. This plugin turns that into a predictable tool-based flow.
worktree_createcreates a branch worktree and forks session context.worktree_deletemarks the current worktree for cleanup and removes it on idle.- Hook support (
postCreate,preDelete) lets each repo define setup/teardown steps.
Add to your opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["@rocket-sensei/opencode-worktree"]
}Create a loader file in one of OpenCode's local plugin directories:
- Project-local:
.opencode/plugins/worktree.ts - Global:
~/.config/opencode/plugins/worktree.ts
Example global setup:
export { WorktreePlugin } from "./worktree/worktree.js"Then copy dist/worktree.js to ~/.config/opencode/plugins/worktree/worktree.js.
Install runtime dependencies into ~/.config/opencode:
cd ~/.config/opencode
bun add @opencode-ai/plugin @opencode-ai/sdk jsonc-parser zodThe plugin adds two tools:
| Tool | Purpose |
|---|---|
worktree_create(branch, baseBranch?) |
Create a new git worktree and fork session context for isolated work. |
worktree_delete(reason) |
Mark current worktree for cleanup; commits and removal happen when session idles. |
worktree_create:
branch: "feature/dark-mode"
baseBranch: "main" # optional, defaults to HEADWhen called, this:
- Creates git worktree at
~/.local/share/opencode/worktree/<project-id>/feature/dark-mode - Syncs files based on
.opencode/worktree.jsonc - Runs
postCreatehooks - Forks session context and records the new worktree session mapping
worktree_delete:
reason: "Feature complete, merging to main"When called, this:
- Marks current worktree for deletion
- Runs
preDeletehooks onsession.idle - Commits snapshot changes
- Removes git worktree with
--force - Clears tracked session state
Auto-creates .opencode/worktree.jsonc on first use:
It standardizes AI worktree lifecycle management with file sync, hooks, and cleanup tracking.
No. It uses standard git worktree operations. Branches and merges work normally.
Yes. This plugin does not open terminals. You can open any shell/editor workflow you want in the created worktree path.
bun install
bun unittest
bun typecheck
bun lint
bun format:checkInspired by opencode-worktree-session by Felix Anhalt.
This project is not built by the OpenCode team and is not affiliated with OpenCode in any way.
MIT
{ "$schema": "https://registry.kdco.dev/schemas/worktree.json", "sync": { // Files to copy from main worktree "copyFiles": [], // Directories to symlink "symlinkDirs": [], // Patterns to exclude "exclude": [] }, "hooks": { // Run after creation "postCreate": [], // Run before deletion "preDelete": [] } }