mirror of
https://github.com/max-sixty/worktrunk.git
synced 2026-11-26 05:25:35 +08:00
## Summary Replaces the ambient ref-keyed caches in `RepoCache` with an explicit, point-in-time `RefSnapshot` value threaded through read paths. This closes the bug class behind PR #2507 (post-`update-ref` integration check seeing pre-write SHAs) — and several latent variants — by making the caches that could go stale unrepresentable. ## Why `RepoCache` cached ref-name → SHA in `commit_shas: DashMap<String, String>`, plus composite caches keyed off it (`integration_reasons`, `effective_integration_targets`, `ahead_behind`, `integration_target` OnceCell, `tree_shas`, `resolved_refs`). When wt itself moved a ref mid-command — most prominently `wt merge` running `git update-ref refs/heads/<target>` to advance the local target — those caches went stale, and any downstream read returned pre-write SHAs. PR #2507 patched one symptom by adding `ref_is_ancestor` (a cache-bypass helper) at the integration-check call site. That works locally; every other ref-name read after a write was a latent bug. The structural fix: there is no longer an ambient ref-name → SHA cache. Callers explicitly capture a `RefSnapshot` at known points and thread it through the read paths. After ref-mutating operations, callers capture a fresh snapshot — the discipline lives at the small set of write boundaries (currently one site, `finish_after_merge`), not at every ref-name accessor. Background analysis: `docs/dev/cache-staleness.md` (commit ba8b239) walks through six options and the trade-offs. ## What changed - **New `src/git/repository/ref_snapshot.rs`** — `RefSnapshot` value type. One `for-each-ref` over local + remote refs, optional ahead/behind batch. Cloned by value, no `OnceCell`/`Arc`. ~330 lines including unit tests (the freshness contract test mutates a ref and re-captures). - **Two-tier API** — `_by_sha` siblings of cached methods (`is_ancestor_by_sha`, `merge_base_by_sha`, `branch_diff_stats_by_sha`, `ahead_behind_by_sha`, `has_added_changes_by_sha`, `trees_match_by_sha`, `merge_integration_probe_by_sha`, …). The persistent on-disk `sha_cache` (already SHA-keyed) sits behind these. - **Snapshot-taking integration path** — `compute_integration_lazy`, `integration_reason`, `compute_integration_reason_uncached`, `integration_targets`, `delete_branch_if_safe`, `remove_worktree_with_cleanup` all take `&RefSnapshot`. - **Post-write fresh-snapshot pattern** — `wt merge`'s `finish_after_merge` captures a *fresh* snapshot AFTER `handle_no_ff_merge`/`handle_push` runs `update-ref`. This is the structural fix for PR #2507. The `ref_is_ancestor` workaround is deleted. - **`wt list` migration** — pre-skeleton phase captures one snapshot; `TaskContext` carries it; tasks call `_by_sha` siblings via the snapshot. No regression intended (one extra `for-each-ref` upfront, lose per-task `rev-parse` fallbacks). - **Cache deletions** — `commit_shas`, `tree_shas`, `effective_integration_targets`, `integration_reasons`, `ahead_behind`, `integration_target` OnceCell, `resolved_refs` all gone from `RepoCache`. `LocalBranchInventory`'s priming side-effects (writes to `commit_shas`/`resolved_refs`) removed. - **Uncached primitives** — `rev_parse_commit`, `rev_parse_tree`, `resolve_preferring_branch` no longer cache; consumers that need SHA-stable lookups thread a `RefSnapshot`. - **Persistent caches retained** — on-disk `sha_cache::*` and the in-memory `merge_base` / `branch_diff_stats` caches keep their SHA-keyed contract. ## Snapshot test deltas Three `list_*_with_nonexistent_default_branch.snap` snapshots no longer show the same-commit `_` symbol when the configured default branch is missing — that was a false positive (no ref to compare against). `list_warns_when_commit_details_batch_fails.snap` now embeds the resolved SHA in the error rather than the literal `"main"` ref name (strict improvement — the SHA is what was passed to git). ## Regression coverage - PR #2507's regression test (`test_remove_merged_locally_when_upstream_diverged`) passes without `ref_is_ancestor`. - PR #2513's `test_list_integrated_when_merged_locally_with_upstream_diverged` passes. - New unit test: `RefSnapshot` capture-mutate-recapture confirms two captures observe distinct SHAs after `update-ref`. ## Out of scope (deferred) Both flagged by `/reviewing-code` and explicitly deferred: - `prepare_worktree_removal` re-captures a snapshot per candidate inside `step_prune` — N+1 `for-each-ref` calls. Threading an optional `&RefSnapshot` would make `step_prune` reuse its entry snapshot. Affects display consistency in unusual hook setups (a `post-remove` hook that runs `git fetch` between candidates can desync the displayed integration target from the safety decision); not a safety bug. - `head_shas` cache retained. Different staleness model (per-worktree, mutated by `commit`/`reset` rather than `update-ref`); design doc lists it as a remaining vector. Out of scope for this PR's integration-target focus. ## Test plan - [x] `cargo run -- hook pre-merge --yes` (3413 tests pass before the merge of `origin/main`) - [x] 1071 lib + 607 bin + 1588 integration tests pass after merging `origin/main` - [x] `cargo check --all-targets` clean - [ ] CI green --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>