# resume picker: attribute default-branch checkpoints correctly (legacy fallback)

`098e392`→[main](/content/gh/entireio/cli/commits/main/index.html)·

Soph·1mo ago·2 files·+131 added/-28 removed

The legacy branch-resolution fallback skipped the default branch entirely but still walked default-branch history through every feature branch. A pre-Branch-field session whose checkpoint was committed on main/master would either show "no branch" or be mis-mapped to a feature branch that merely contains the commit — and selecting it then checked out the wrong branch before restoring the checkpoint.

Now buildCheckpointBranchIndex indexes the default branch FIRST (so its checkpoints map to the default branch) and records its commit hashes as a stop-set; feature-branch walks halt at shared default history, so they only claim their own branch-only checkpoints. The default branch commit is resolved from the local ref or origin's remote-tracking ref. Still no go-git MergeBase (too slow at scale) — the default-commit set is the cheap stand-in for branch-only scoping.

Test: a checkpoint committed on the default branch maps to it, while a feature-only checkpoint still maps to the feature branch.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

## Sessions

39489eeb2fdfView transcript

[?\\
Interactive Resume Picker for SessionsClaude Code·Opus 4.8[1m]·1 step](/content/gh/entireio/cli/session/d7eb2c7b-65ce-4dbe-b20a-285326019897#timeline-39489eeb2fdf/index.html)

## Changes

2

- cmd/entire/cli

- Mresume_picker.go+80/-28

- Mresume_picker_test.go+51

```plaintext
215 unmodified lines

return err == nil
}

// buildCheckpointBranchIndex maps committed checkpoint IDs to the local branch whose recent history carries them. Each non-default branch is walked from its tip back a bounded depth; the default branch is skipped so the index favors feature branches, and the first branch to claim a checkpoint wins.
const (
    defaultBranchScanDepth = 500
    featureBranchScanDepth = 50
)

// buildCheckpointBranchIndex maps committed checkpoint IDs to the branch whose history carries them, for the legacy fallback that resolves a session with no stored Branch. The default branch is indexed FIRST (so a checkpoint committed to main/master maps to the default branch, not to some feature branch that merely contains the commit), and its commit hashes seed a stop-set: feature branch walks halt when they reach shared default history, so they only claim their own branch-only checkpoints. First branch to claim a checkpoint wins.

// It deliberately does NOT compute merge bases to scope to branch-only commits: go-git's MergeBase walks full history and, run once per branch, turns this into an O(branches × history) operation that hangs on large repos. A session's last checkpoint sits near its branch tip, so a shallow tip walk finds it; the only cost of skipping merge-base scoping is that a checkpoint shared with the base branch may be attributed to a feature branch, which is harmless for lookups keyed on a specific session's checkpoint ID.
// This deliberately avoids go-git's MergeBase (which walks full history and, run once per branch, becomes O(branches × history) and hangs on large repos); the precomputed default-commit set is a cheap stand-in for branch-only scoping.
// Internal entire/ refs (checkpoint metadata + shadow branches) are never indexed — they are not resumable and number in the hundreds.
func buildCheckpointBranchIndex(repo *git.Repository) map[string]string {
    index := map[string]string{}

defaultBranch := getDefaultBranchFromRemote(repo)
    if defaultBranch == "" {
        for _, name := range []string{defaultBaseBranch, masterBaseBranch} {
            if _, err := repo.Reference(plumbing.NewBranchReferenceName(name), true); err == nil {
                defaultBranch = name
                break
            }
        }
    }
    
    defaultCommits := map[plumbing.Hash]bool{}
    if defaultBranch != "" {
        if c := resolveBranchCommit(repo, defaultBranch); c != nil {
            indexBranchCheckpoints(c, defaultBranch, defaultBranchScanDepth, nil, defaultCommits, index)
        }
    }

forEachErr := iter.ForEach(func(ref *plumbing.Reference) error {
        branchName := ref.Name().Short()
        if branchName == defaultBranch || strings.HasPrefix(branchName, "entire/") {
            return nil
        }
        if err != nil {
            return nil //nolint:nilerr // skip unreadable branch, keep indexing others
        }
        indexBranchCheckpoints(headCommit, branchName, index)
        indexBranchCheckpoints(headCommit, branchName, featureBranchScanDepth, defaultCommits, nil, index)
        return nil
    })
    if forEachErr != nil {

return index
}

}

// indexBranchCheckpoints walks history from start back a bounded depth and records each checkpoint trailer it finds under branch (first writer wins).
func indexBranchCheckpoints(start *object.Commit, branch string, index map[string]string) {
const maxCommits = 50
}

// resolveDefaultBranchName returns the repo's default branch name (from origin's HEAD when available, else the first of main/master that exists locally), or "" if none can be determined.
func resolveDefaultBranchName(repo *git.Repository) string {
if name := getDefaultBranchFromRemote(repo); name != "" {
return name
}
for _, name := range []string{defaultBaseBranch, masterBaseBranch} {
if _, err := repo.Reference(plumbing.NewBranchReferenceName(name), true); err == nil {
return name
}
}
return ""
}

// resolveBranchCommit returns the commit a branch points at, preferring the local ref and falling back to origin's remote-tracking ref (the default branch may not be checked out locally in a worktree-only setup).
func resolveBranchCommit(repo *git.Repository, name string) *object.Commit {
for _, ref := range []plumbing.ReferenceName{
plumbing.NewBranchReferenceName(name),
plumbing.NewRemoteReferenceName("origin", name),
} {
if r, err := repo.Reference(ref, true); err == nil {
if c, err := repo.CommitObject(r.Hash()); err == nil {
return c
}
}
}
return nil
}

// indexBranchCheckpoints walks history from start back maxCommits commits, recording each checkpoint trailer under branch (first writer wins). It stops when it reaches a commit in stopAt (shared default history). When recordVisited is non-nil, every visited commit hash is added to it (used while seeding the default branch so feature walks can later stop at those commits).
func indexBranchCheckpoints(
start *object.Commit,
branch string,
maxCommits int,
stopAt map[plumbing.Hash]bool,
recordVisited map[plumbing.Hash]bool,
index map[string]string,
) {
current := start
for i := 0; current != nil && i < maxCommits; i++ {
if stopAt[current.Hash] {
return
}
if recordVisited != nil {
recordVisited[current.Hash] = true
}
for _, cpID := range trailers.ParseAllCheckpoints(current.Message) {
key := cpID.String()
if _, ok := index[key]; !ok {
```

Mcmd/entire/cli/resume_picker.go+80/-28

```

429 unmodified lines

}

// TestBuildCheckpointBranchIndex_DefaultBranchCheckpoint covers the legacy fallback for a pre-Branch-field session whose checkpoint was committed on the default branch: it must map to the default branch, not to a feature branch that merely contains the commit (which would check out the wrong branch).
func TestBuildCheckpointBranchIndex_DefaultBranchCheckpoint(t *testing.T) {
t.Parallel()

const featBranch = "feat-legacy"

tmpDir := t.TempDir()
testutil.InitRepo(t, tmpDir)
testutil.WriteFile(t, tmpDir, "base.txt", "base")
testutil.GitAdd(t, tmpDir, "base.txt")
testutil.GitCommit(t, tmpDir, "init")

// A checkpoint committed on the default branch.
cpMain, err := id.Generate()
if err != nil {
t.Fatalf("generate checkpoint id: %v", err)
}
testutil.WriteFile(t, tmpDir, "m.txt", "m")
testutil.GitAdd(t, tmpDir, "m.txt")
testutil.GitCommit(t, tmpDir, "main work\n\nEntire-Checkpoint: "+cpMain.String())

// A feature branch off that commit, with its own checkpoint. Its history contains cpMain, so a naive walk would mis-attribute cpMain to it.
testutil.GitCheckoutNewBranch(t, tmpDir, featBranch)
cpFeat, err := id.Generate()
if err != nil {
t.Fatalf("generate checkpoint id: %v", err)
}
testutil.WriteFile(t, tmpDir, "f.txt", "f")
testutil.GitAdd(t, tmpDir, "f.txt")
testutil.GitCommit(t, tmpDir, "feat work\n\nEntire-Checkpoint: "+cpFeat.String())

repo, err := git.PlainOpen(tmpDir)
if err != nil {
t.Fatalf("open repo: %v", err)
}
defer repo.Close()

defaultBranch := resolveDefaultBranchName(repo)
index := buildCheckpointBranchIndex(repo)
if got := index[cpMain.String()]; got != defaultBranch {
t.Errorf("default-branch checkpoint should map to default branch %q, got %q (index=%v)", defaultBranch, got, index)
}
if got := index[cpFeat.String()]; got != featBranch {
t.Errorf("feature-only checkpoint should map to %q, got %q (index=%v)", featBranch, got, index)
}
}

// TestBranchCheckedOutElsewhere verifies worktree awareness: a branch checked out in another worktree is detected (with its path), while the current worktree's own branch and unknown branches are not flagged.
