Skip to content

refactor(git): close remaining cache-staleness vectors after RefSnapshot cutover - #2530

Merged
max-sixty merged 2 commits into
mainfrom
cache-staleness-followups
May 2, 2026
Merged

max-sixty merged 2 commits into
mainfrom
cache-staleness-followups

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Summary

Two follow-ups deferred from #2528 (the RefSnapshot cutover that closed the ambient ref-keyed cache staleness class), plus reviewer cleanups.

  • Thread RefSnapshot through prepare_worktree_removal — adds snapshot: Option<&RefSnapshot>; step_prune and validate_remove_targets capture once and reuse across candidates instead of re-running for-each-ref per candidate (N+1 → 1).
  • Drop head_shas cache — the per-worktree HEAD SHA cache had no invalidation. Any flow that moved HEAD mid-command (rebase, hook-emitted commits) left a stale value that surfaced in {{ commit }} template variables for later hooks. WorkingTree::head_sha now always reads fresh; new head_sha_tracks_head_movement test guards against re-introducing a cache.
  • Reviewer cleanups — refreshed four stale comments referring to the deleted cache, collapsed a now-redundant short-circuit in command_executor.rs::build_hook_context, and replaced Arc::new(snapshot.clone()) in step_prune's rayon worker with Arc::clone (shares by refcount, no deep copy).

Test plan

  • cargo run -- hook pre-merge --yes — full test + lint suite (3425 tests pass; clippy / doc / doctest with -Dwarnings clean)
  • New unit test head_sha_tracks_head_movement exercises the freshness contract directly
  • Manual review of stale-comment fixes vs current behavior

🤖 Generated with Claude Code

…hot cutover

Two follow-ups deferred from #2528, plus reviewer cleanups.

## 1. Thread `RefSnapshot` through `prepare_worktree_removal`

`prepare_worktree_removal` gains `snapshot: Option<&RefSnapshot>`;
callers that loop over candidates capture once and pass through:

- `step_prune` already captured a snapshot at command entry — now also
  passes it into `try_remove`, so the inline `prepare_worktree_removal`
  call reuses it instead of re-running `for-each-ref` per candidate.
  N+1 captures → 1.
- `validate_remove_targets` (multi-target `wt remove`) now captures once
  at the top of the validation loop.
- Single-removal sites (`wt remove`, picker) pass `None` and capture
  internally — same shape as the existing `worktrees: Option<&[…]>`
  optional-prefetch parameter.

## 2. Drop the `head_shas` cache

The per-worktree `DashMap<PathBuf, Option<String>>` populated by
`WorkingTree::prewarm_info` had no invalidation. Anything that moved
HEAD mid-command (`wt merge`'s rebase, hook-emitted commits, `wt step
commit`) left a stale cache that surfaced in `{{ commit }}` template
variables for hooks running after the move.

- Field removed from `RepoCache`.
- `prewarm_info`'s rev-parse batch drops the bare `HEAD` selector
  (5 selectors → 4); kept `--symbolic-full-name HEAD` for branch.
- `WorkingTreeGitInfo.head_sha` gone (no production consumers; tests
  updated).
- `WorkingTree::head_sha` always reads fresh `git rev-parse HEAD`.
  Cost is ~5 ms on the few paths that need it; staleness vector is
  gone.
- New test `head_sha_tracks_head_movement` commits between two
  `head_sha()` calls and asserts the SHA changed — guards against
  re-introducing a cache.

## 3. Reviewer cleanups

- Stale comments at four sites (`command_executor.rs`, `config.rs`,
  `picker/mod.rs`, `eval.rs`) referenced the deleted HEAD-SHA cache;
  rewritten to describe current behavior.
- `command_executor.rs` `{{ commit }}` resolution: collapsed the
  branch-matches-running-worktree short-circuit. The arm existed only
  to reuse the deleted cache; with both arms equally fresh, a single
  `rev-parse <branch>` covers the branched case.
- `step_prune` no longer deep-clones the snapshot for the rayon
  worker. `Arc::new(snapshot)` + `Arc::clone(&snapshot_arc)` shares by
  refcount; `try_remove` takes `&RefSnapshot` and auto-derefs from the
  `Arc`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

@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.

One doc nit on the eval test, otherwise the change looks clean. The snapshot threading reuses ref state across prepare_worktree_removal calls correctly: validate_remove_targets only reads, and in step_prune the compute_integration_reason calls inside prepare_worktree_removal are informational (the actual delete decision was already taken by the upstream parallel integration_reason pass).

Comment thread tests/integration_tests/eval.rs Resolved
The on-branch path in `build_hook_context` resolves `{{ commit }}` via
`git rev-parse <branch>`, not `WorkingTree::head_sha`. Both are fresh
reads, but the test description should match the actual code path.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@max-sixty

Copy link
Copy Markdown
Owner Author

Applied — d399ef9 → 3c0b424. Doc-only fix; the on-branch path goes through rev-parse <branch>, kept the freshness framing intact.

@max-sixty
max-sixty merged commit ee242f3 into main May 2, 2026
28 checks passed
@max-sixty
max-sixty deleted the cache-staleness-followups branch May 2, 2026 18:46
@max-sixty max-sixty mentioned this pull request May 3, 2026
max-sixty added a commit that referenced this pull request May 3, 2026
Cuts v0.47.0. Highlights:

- **Integration detection fix** across `wt list`, `wt remove`, `wt
merge`, `wt step prune` for the local/upstream-diverged case (#2507,
#2513, #2515).
- **Faster shell-command dispatch** (~10ms per invocation, 1.49× on
trivial aliases) via event-driven signal forwarding (#2537, #2538), plus
parallel config load (#2543) and merged cold-start prewarm rev-parse
(#2541).
- **`wt switch <number>` suggests `pr:N` / `mr:N`** instead of just
`--create` (#2516).
- **Library API breaking changes**: `RefSnapshot` cutover (#2528, #2530)
and the `for-each` direct-exec change (#2465). Full list in
`CHANGELOG.md`.

See `CHANGELOG.md` for the complete entry.

> _This was written by Claude Code on behalf of @max-sixty_

---------

Co-authored-by: Claude <noreply@anthropic.com>
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