Skip to content

Scope the branch-inventory staleness contract to membership - #3988

Merged
max-sixty merged 2 commits into
mainfrom
faq-branch-snapshot
Sep 2, 2026
Merged

max-sixty merged 2 commits into
mainfrom
faq-branch-snapshot

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Repository::local_branches scans refs/heads/ once and caches the result for the
lifetime of the Repository. Its docstring already scoped a staleness contract to
commit_sha — "a snapshot at scan time", resolve through a RefSnapshot instead — but
then said of the inventory itself:

The inventory itself is used for branch listing and upstream-tracking metadata, both of
which are stable for the duration of a command.

That holds only for a command that runs no hooks. Hooks run inside a command, so a
pre-switch or pre-remove hook that adds, deletes, or renames a branch leaves the
cached membership stale, not just the SHAs.

Where it shows up

The behaviour is intended — one scan per command is the point — but the docstring said
membership was stable, which sends a reader looking for a bug when they hit it. Two
consumers see it:

  • detect_branch_namespace_conflict (src/commands/worktree/switch.rs) reads the
    inventory to name the branch blocking a git worktree add -b. A hook that deletes
    feature/nested and creates feature in the same command yields "collides with
    existing branch feature/nested" where "already exists" is the truth. Cosmetic.
  • wt step prune iterates all_branches() to choose what to prune, so a branch created
    by a hook mid-command isn't considered. Conservative, but a selection difference rather
    than a message one — which is why the note says membership rather than "messages can be
    wrong".

What this changes

The docstring, and nothing else. No behaviour change, no new test.

This came out of reviewing #3984, where the same staleness was flagged against the
namespace-conflict mapping. A doc entry was considered and dropped: the FAQ's questions
sit a level up from one cache's staleness, and there's no broader question this is a
detail of.

🤖 Generated with Claude Code

`local_branches`'s docstring claimed branch listing and upstream metadata
were "stable for the duration of a command", which holds only for a command
that runs no hooks. A hook that adds, deletes, or renames a branch leaves
the cached list stale, so an error message built from it can name a branch
that is already gone. The docstring already scoped the same contract to
`commit_sha`; membership goes stale the same way.

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

src/git/repository/mod.rs carries the same claim this PR corrects, near-verbatim, on the RepoCache::local_branches field the accessor reads:

// src/git/repository/mod.rs — RepoCache::local_branches
/// The inventory is used for branch-name listing and upstream-tracking
/// metadata, both of which are stable for the duration of a command.

That's the copy CLAUDE.md routes a reader to — "the # Caching section in src/git/repository/mod.rs" — and this field doc sits inside that section, so the reader most likely to hit hook-induced staleness and go looking for a bug is still told membership is stable. Same "keep it current" rule the accessor docstring is being fixed under. Happy to push the matching edit if you want it in this PR; having the field doc cross-reference local_branches instead of restating the contract would also stop the pair drifting apart again.

Smaller, and a wording call rather than a defect: upstream_short / upstream_track is a third thing a hook can invalidate — git branch -u, or a fetch --prune that turns the upstream [gone] — and Branch::upstream() resolves it O(1) out of this same inventory. "unless that command runs a hook" already withdraws the blanket claim, so this only matters if "the membership stale too, not just the SHAs" reads to you as enumerating the whole surface.

`RepoCache::local_branches` carried the same "stable for the duration of a
command" claim near-verbatim, and CLAUDE.md routes readers into that section
for caching, so the reader most likely to hit hook-induced staleness was
still told membership was stable. Cross-reference `local_branches` rather
than syncing a second copy, so the pair can't drift again.

Also drop the enumeration from the accessor: upstream tracking goes stale
the same way — `git branch -u`, or a prune that leaves an upstream gone —
so name what a hook invalidates rather than listing SHAs and membership.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@max-sixty

Copy link
Copy Markdown
Owner Author

Folded both in — good catch on the duplicate.

RepoCache::local_branches now cross-references Repository::local_branches instead of restating the contract, which is the version you suggested: syncing two copies is what let them drift in the first place, and CLAUDE.md's caching pointer routes readers into exactly that field doc.

On the second point — you're right that "membership too, not just the SHAs" reads as enumerating the surface. The accessor now says a hook that touches refs leaves the rest of the inventory stale, "which branches are in it, and where each one's upstream points", so git branch -u and a prune that leaves an upstream gone are covered without claiming the list is complete.

Gate green: 4,713 passed, 1 skipped.

This was written by Claude Code on behalf of max-sixty

@max-sixty
max-sixty merged commit 7a3ad6b into main Sep 2, 2026
40 checks passed
@max-sixty
max-sixty deleted the faq-branch-snapshot branch September 2, 2026 05:46
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.

2 participants