fix: clean up orphaned worktrees on bootstrap failure#14649
Open
alexhraber wants to merge 3 commits intoanomalyco:devfrom
Open
fix: clean up orphaned worktrees on bootstrap failure#14649alexhraber wants to merge 3 commits intoanomalyco:devfrom
alexhraber wants to merge 3 commits intoanomalyco:devfrom
Conversation
When worktree bootstrap fails (either at git reset or Instance.provide), the worktree directory, git worktree entry, git branch, and database sandbox record were all left behind permanently. Each failed attempt creates a new randomly-named worktree, so repeated failures cause unbounded disk consumption in ~/.local/share/opencode/worktree/. Add a cleanupFailedWorktree helper that runs on every failure path in the deferred bootstrap, removing: - the worktree directory (with retries) - the git worktree entry (+ prune) - the git branch - the project sandbox database record
fix: clean up orphaned worktrees on bootstrap failure
Author
|
This typecheck failure is not caused by the worktree change. It's a pre-existing issue in the upstream repo. Here's why:
This cannot be fixed by changing packages/opencode/src/worktree/index.ts - the worktree file has nothing to do with TypeScript type exports from the SDK package. The fix needs to happen in one of:
|
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #14648
Type of change
What does this PR do?
Worktree.createinpackages/opencode/src/worktree/index.tsfires off bootstrap work (git reset + Instance.provide) inside asetTimeout(..., 0)and returns immediately. When that deferred bootstrap fails — eithergit reset --hardexits non-zero orInstance.provide()throws — the function just logs, emits aworktree.failedevent, and returns. Nothing cleans up the artifacts that were already created synchronously before the setTimeout:~/.local/share/opencode/worktree/<project-id>/<random-name>/.git/worktrees/opencode/<random-name>Each retry generates a new random name (
brave-cabin,calm-cactus, etc.), so repeated failures accumulate orphaned full-repo clones with no upper bound and no automatic cleanup.This is what causes the disk fillups.
The fix adds a
cleanupFailedWorktreeasync helper that tears down all four artifacts. It's called from every failure path: git reset failure, Instance.provide failure, and the outer.catch()on the start promise. The cleanup is intentionally best-effort (each step catches its own errors) so a partial cleanup failure doesn't mask the original bootstrap error.I captured
Instance.worktreeintoworktreeCwdbefore entering the setTimeout to ensure the git commands in cleanup have a stable cwd reference.How did you verify your code works?
Worktree.removelogic to match the same cleanup steps (directory rm with retries, git worktree remove --force, git worktree prune, git branch -D, Project.removeSandbox)packages/opencode/test/project/worktree-remove.test.tsexercises the remove path and confirms directory, git entry, and branch are all gone after removalScreenshots / recordings
N/A — no UI changes.
Checklist