Skip to content

Worktree bootstrap failures leak orphaned directories causing disk fillup #14648

@alexhraber

Description

@alexhraber

Description

When Worktree.create fails during its deferred bootstrap phase (the setTimeout(..., 0) at line 357 of packages/opencode/src/worktree/index.ts), no cleanup runs. The synchronous setup that already completed — directory creation, git worktree add, git branch, and Project.addSandbox() — is left behind permanently.

There are three failure points that all have this problem:

  1. git reset --hard exits non-zero (line 359-372) — logs + emits event, returns without cleanup
  2. Instance.provide() throws (line 375-394) — logs + emits event, returns without cleanup
  3. Unexpected exception in the outer start() promise (line 411-413) — logs, no cleanup

Each failed attempt generates a unique random name (brave-cabin, calm-cactus, etc.), so retries never reuse the previous directory. Every failure leaves behind:

  • A worktree directory at ~/.local/share/opencode/worktree/<project-id>/<name>/ containing a full repo clone
  • A git worktree entry in .git/worktrees/
  • A git branch opencode/<name>
  • A sandbox record in the project database

There is no startup garbage collection, no periodic pruning, and no detection of orphaned worktrees. For a large repo, each orphan can be hundreds of MB. Users experiencing repeated bootstrap failures (network issues, disk pressure, permission errors) will see unbounded disk consumption until the volume fills up.

Plugins

None

OpenCode version

Latest // v1.2.10

Steps to reproduce

  1. Open a git repository in opencode
  2. Create a worktree (any method that triggers Worktree.create)
  3. Have the bootstrap fail — this can happen due to network issues, disk pressure, permission errors, or a corrupt repo state causing git reset --hard or Instance.provide() to fail
  4. Observe that the worktree directory remains at ~/.local/share/opencode/worktree/<project-id>/<random-name>/
  5. Run git worktree list in the parent repo — the orphaned entry is still registered
  6. Run git branch -a | grep opencode/ — the orphaned branch is still present
  7. Retry creating a worktree — a new random name is generated, the previous orphan is never cleaned up
  8. Repeat — each failure adds another full repo clone to disk with no upper bound

Screenshot and/or share link

N/A

Operating System

Linux (reproducible on all platforms)

Terminal

Any

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcoreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions