-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
Summary
Extend the plugin to support git worktrees as an alternative to devcontainer clones for projects that don't use devcontainers. This provides the same isolated branch workflow with session state management.
Motivation
- Not all projects use devcontainers, but still benefit from isolated branch work
- Worktrees are simpler (local filesystem) but need similar session management
- Users ask "set up a worktree for feature-x" and expect the agent to track it
- Without session state, the agent may forget the worktree context after compaction
Proposed Design
New /worktree command
/worktree <branch> # Create worktree, set as active session
/worktree off # Clear active worktree
/worktree # Show current status
New worktree tool
Similar to devcontainer tool but:
- Creates worktree in standard location:
~/.local/share/opencode/worktree/<repo>/<branch>/ - Copies gitignored secrets from main repo (e.g., credentials, local env files)
- Runs
direnv allowfor environment setup
Bash interception
When session has an active worktree:
- Add
workdirparameter to bash commands (vs wrapping withdevcontainer exec) - Same
shouldRunOnHostlogic for git commands etc. HOST:escape hatch works the same way
Session state
{
type: "worktree", // vs "devcontainer"
branch: "feature-x",
workspace: "~/.local/share/opencode/worktree/<repo>/<branch>",
repoName: "<repo>",
mainRepo: "/path/to/main/repo", // for worktree removal
}Worktree setup steps
- Validate: in git repo, not already in a worktree
- Create:
git worktree add <path> -b <branch>(or without-bfor existing) - Copy secrets: Find gitignored-but-tracked files, copy from main repo
- Environment:
direnv allowin new worktree
Port & Database isolation
Projects using worktrees can configure their .envrc to derive PORT and database settings from the worktree name to avoid conflicts when running multiple branches simultaneously. The plugin could:
- Report the computed PORT after setup (if detectable via
source .envrc && echo $PORT) - Suggest adding isolation if multiple worktrees detected without it
Additional scope
Workspace cleanup
Add a /workspaces or /workspaces cleanup command for managing stale workspaces:
- List worktrees and devcontainer clones with last access time, branch, uncommitted changes
- Identify safe-to-delete vs active workspaces
- Handle proper worktree removal (must run from main repo)
- Clean up associated Docker resources for devcontainers
Plugin rebrand
Consider renaming to opencode-workspaces to reflect the broader scope (devcontainers + worktrees + cleanup).
Implementation notes
- Reuse existing session management, job tracking infrastructure
- Worktree creation is fast (no container build), so might not need background jobs
Metadata
Metadata
Assignees
Labels
No labels