Skip to content

Restore a deleted thread worktree from its preserved branch - #1622

Merged
currentbits merged 3 commits into
mainfrom
coder/p1-rematerialize-a-missing-worktree-from-e01625
Oct 13, 2026
Merged

currentbits merged 3 commits into
mainfrom
coder/p1-rematerialize-a-missing-worktree-from-e01625

Conversation

@currentbits

Copy link
Copy Markdown
Owner

What changed

If a thread's worktree folder disappeared (deleted by hand, removed with git worktree remove, or cleaned up by retention GC), the next run created a new branch with a suffixed name, cut from the start snapshot. Every commit on the old branch was left out. Now the thread picks up where it stopped, on its retainedBranch at the tip (#1400).

electron/worktrees-lifecycle.js: setupWorktree

  • If the thread has a retainedBranch, the worktree is checked out from that branch with git worktree add <path> <branch>. It no longer creates a new branch from resolveWorktreeStart, and it doesn't claim a pooled worktree.
  • It runs git worktree prune first so the deleted folder's stale registration doesn't block the checkout. If the branch is checked out in a live worktree, git's own error is still shown unchanged.
  • If refs/heads/<retainedBranch> no longer exists, setup fails with a clear error: either recreate the branch (git branch <name> <commit>) or continue in a new thread. The thread's flags stay as they were.
  • retainedBranch is cleared only after the checkout succeeds. Failures read Failed to restore branch <name> rather than Failed to create worktree, and still include git's full stderr (Worktree creation failure must never silently run the agent in the main checkout #511).

electron/worktrees-gc.js: gcClean

  • When GC removes a worktree whose thread has a branch, it now also sets pendingWorktree: true next to retainedBranch. The next run then restores that branch instead of falling back to the project checkout.

Tests

  • electron/test/worktree-cleanup.test.js: covers rematerializing from a retained branch and the error when the branch is missing.
  • electron/test/worktree-gc.test.js: checks that gcClean sets pendingWorktree along with retainedBranch.

🤖 Generated with Claude Code

A thread whose worktree folder vanished (hand delete, `git worktree
remove`, or retention GC) now resumes on its retainedBranch at its tip
instead of a suffixed branch cut from the start snapshot, which dropped
every commit made on the old branch. A missing ref fails with an
actionable error and keeps the flags; retainedBranch clears only after
the checkout succeeds. gcClean also re-arms pendingWorktree so a GC'd
thread no longer falls back to the project checkout (#511).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T14:12:04.873183Z 777e358 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 777e3580ea

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


// A worktree lost to GC or a hand delete resumes on its own branch, not a
// fresh one from the start snapshot that would drop its commits (#1400).
const retained = thread.retainedBranch || null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Clear retained refs when moving threads between projects

When GC has removed a thread's worktree, the thread has worktreePath: null, pendingWorktree: true, and a retainedBranch belonging to its original repository. setThreadProject permits that thread to move, deliberately preserves pendingWorktree, and clears other repository bindings but not retainedBranch; the next send therefore treats the old branch name as a branch in the destination repository and either fails permanently at this check or, if a same-named branch exists, restores unrelated history. Clear retainedBranch as part of project recategorization, or otherwise associate it with and validate its source project.

Useful? React with 👍 / 👎.

Willem and others added 2 commits October 13, 2026 16:32
macOS CI failed the afterEach with ENOTEMPTY: the store's async flush
dropped a .tmp into messages/ while rmSync was deleting the tree. Use
the shared rmTree helper, which retries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@currentbits
currentbits merged commit fcb01ff into main Oct 13, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant