Skip to content

perf(list): cold-start primer for the Remote⇅ column, per-upstream-group - #2718

Merged
max-sixty merged 2 commits into
mainfrom
remote-ahead-behind-batch
May 12, 2026
Merged

max-sixty merged 2 commits into
mainfrom
remote-ahead-behind-batch

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Summary

  • Mirrors the main↕ cold-start primer from perf(list): cache ahead/behind counts SHA-keyed; skip the for-each-ref walk on warm runs #2704 for the Remote⇅ column. Repository::prime_upstream_ahead_behind_cache runs one for-each-ref %(ahead-behind:UPSTREAM_SHA) walk per unique upstream SHA (scoped to that upstream's branches) and bulk-writes the ahead-behind/ cache. Per-row UpstreamTask::compute reads the cache via ahead_behind_by_sha.
  • Nested inside the snapshot spawn in the post-skeleton rayon::scope so it reads the snapshot's already-scanned local/remote inventories — no extra for-each-ref refs/remotes/ fork. Candidates scoped to branches that actually render an UpstreamTask row (worktree-attached when !show_branches). Primer honors list.task-timeout-ms via set_command_timeout.
  • Cache-correctness defenses match perf(list): cache ahead/behind counts SHA-keyed; skip the for-each-ref walk on warm runs #2704: %(ahead-behind:UPSTREAM_SHA) (not :refname) so git counts against the SHA the cache will be keyed by; cache key uses the %(objectname) git reports in the same walk; writes flow through put_ahead_behind_bulk (one sweep_lru at end); orphan branches normalize to (0, 0) via per-upstream memoized rev-list --count.

Known limitation

When every branch tracks its own remote (distinct upstream SHA per branch), every group has refs.len() == 1 and the primer skips all of them — those fall through to the per-row parallel path. A %(upstream:track) walk could batch them, but git computes that atom against the upstream's current value at walk time, which we can't pin to the SHA the cache will be keyed by; the resulting race would break the cache invariant compute_ahead_behind establishes on a miss. Documented in the primer's docstring.

Before/after on a 10-branch shared-upstream fixture

Cold wt list --branches trace (RUST_LOG=worktrunk=debug):

  • for-each-ref --format=%(refname) %(objectname) %(ahead-behind:<main_sha>) refs/heads/ — snapshot's main↕ walk (unscoped)
  • for-each-ref --format=%(refname) %(objectname) %(ahead-behind:<feat-base_sha>) refs/heads/feat-base refs/heads/work1 ... refs/heads/work10 — primer's scoped Remote⇅ walk
  • 23 cache entries written (12 keyed by main, 11 keyed by feat-base)

Warm run: grep ahead-behind: returns 0 — both walks skipped, cache hits everywhere.

Test plan

  • 9 new primer tests in ref_snapshot.rs mirror the capture_ahead_behind patterns (cold→warm tampered-cache survival, no-upstream / [gone] skips, orphan normalization, sub-threshold skip, two-upstream-group routing, equal-branch (0,0), local-upstream branch.X.remote = . resolution)
  • cargo run -- hook pre-merge --yes — 3664 tests pass
  • /review-codex iterated to convergence on cache correctness, candidate scoping, redundant remote-scan, and timeout coverage

Mirrors the `main↕` primer from #2704 for the `Remote⇅` column. On a cold
cache, `Repository::prime_upstream_ahead_behind_cache` runs one
`for-each-ref %(ahead-behind:UPSTREAM_SHA)` walk per unique upstream SHA
(scoped to that upstream's branches) and bulk-writes the
`ahead-behind/` cache. Per-row `UpstreamTask::compute` then reads the
cache via `ahead_behind_by_sha` instead of falling through to
per-branch `compute_ahead_behind` (merge-base + 2× rev-list).

Wired into the post-skeleton `rayon::scope`, nested inside the
snapshot spawn so it reads the snapshot's already-scanned local/remote
inventories — no extra `for-each-ref refs/remotes/` fork on the warm
path. Candidates scoped to branches that will actually render an
`UpstreamTask` row (worktree-attached when `!show_branches`), and the
primer honors `list.task-timeout-ms` via `set_command_timeout`.

Cache-correctness defenses match #2704: `%(ahead-behind:UPSTREAM_SHA)`
(not `:refname`) so git counts against the SHA the cache will be keyed
by; cache key uses the `%(objectname)` git reports in the same walk;
writes flow through `put_ahead_behind_bulk` for one `sweep_lru` at the
end; orphan branches normalize to `(0, 0)` via per-upstream memoized
`rev-list --count`.

Known limitation, documented in the primer docstring: when every
branch tracks its own remote (distinct upstream SHA per branch), every
group has size 1 and the primer skips all of them — those fall through
to the per-row parallel path. A `%(upstream:track)` walk could batch
them, but git computes that atom against the upstream's current value
at walk time, which we can't pin to the SHA the cache will be keyed
by — the resulting race would break the cache invariant
`compute_ahead_behind` establishes on a miss.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes May 11, 2026

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

codecov/patch is failing, but most of it is a stale base-pointer artifact rather than a real coverage gap.

Why codecov reports 58 missed lines in ref_snapshot.rs: codecov set its base for this PR to bfb117a6 (the commit before #2704 merged), so every line #2704 added to ref_snapshot.rs is being charged to this PR's patch. The actual merge-base in git is 5384f62b. You can see this in the commit_uploads API response: "parent": "bfb117a665bd68df3abcd56cda0ea3d0b3d36ceb" even though git merge-base origin/main remote-ahead-behind-batch returns 5384f62b.

Mixed in: a handful of lines inside the new function (412 base_total = ... or_insert_with(...), 415 .and_then(|s| s.trim().parse().ok()), 437 writes.iter().map(...)) appear twice in cobertura.xml — once with hits (1, 1, 10 respectively) and once with hits="0". Codecov picks the 0-hit entry. This is the cargo-llvm-cov "N functions have mismatched data" issue (LLVM #97574) already documented in CLAUDE.md.

Actual coverage from cobertura.xml for the new function (lines 348-440):

  • Function body: 408–417 hit, 425/428/430 hit 10× per group, 434–439 hit 76× — the success path is well-tested.
  • Untested branches: line 410 (git < 2.36 or batch failed) and line 419 (fresh.get miss) — both are defensive paths that won't fire on CI's git.
  • Line 426 ((0, 0) orphan arm) is genuinely 0 hits in cobertura, even though prime_upstream_normalizes_orphan_to_zero looks like it should exercise it. Worth a glance — possible that the orphan branch isn't reaching cold_by_upstream for some reason (e.g. its upstream resolves to a different SHA than the other 9, dropping it into a size-1 group below threshold).

Dismissing the approval per the CLAUDE.md "never merge with failing codecov/patch without explicit user approval" rule. If you confirm this is the codecov base-pointer issue (and want to investigate line 426), I'll re-approve.

@worktrunk-bot
worktrunk-bot dismissed their stale review May 11, 2026 20:22

codecov/patch failed — analysis in follow-up COMMENT review. Looks like a stale-base artifact, but per CLAUDE.md dismissing pending your confirmation.

@max-sixty

Copy link
Copy Markdown
Owner Author

Confirming both:

  1. codecov base-pointer: yes, the parent: bfb117a665... in commit_uploads is stale — git merge-base origin/main remote-ahead-behind-batch returns 5384f62b, so every line perf(list): cache ahead/behind counts SHA-keyed; skip the for-each-ref walk on warm runs #2704 added is being charged to this PR's patch. Existing infrastructure issue, not a real coverage gap on this PR.

  2. line 426 ((0, 0) orphan arm): cargo-llvm-cov mismatched-data, same class as 412/415/437. Verified by running the path: with main at N commits and an orphan branch, git for-each-ref --format='%(ahead-behind:main_sha)' refs/heads/orphanbr returns 1 N (ahead=1, behind=N), and rev-list --count main_sha returns N. Both equal → line 426 fires, producing (0, 0) — which is exactly what prime_upstream_normalizes_orphan_to_zero asserts. The else arm on 428 would have produced (1, N), failing the assert.

Local task coverage reports warning: 25 functions have mismatched data and the same lines (412, 415, 426, 437) are missing in local cobertura too, matching #2704's CLAUDE.md note about LLVM #97574.

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

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

Thanks for verifying both — the stale codecov parent (bfb117a6, pre-#2704) and the cargo-llvm-cov mismatched-data on 412/415/426/437 match what I'd be looking for, and the manual trace of the orphan path (%(ahead-behind:main_sha) returns 1 N, rev-list --count returns N, line 426 fires producing (0, 0)) is the right way to confirm the assert in prime_upstream_normalizes_orphan_to_zero exercises that arm. Re-approving.

@max-sixty
max-sixty merged commit c7403f4 into main May 12, 2026
32 of 33 checks passed
@max-sixty
max-sixty deleted the remote-ahead-behind-batch branch May 12, 2026 00:40
@max-sixty max-sixty mentioned this pull request May 13, 2026
max-sixty added a commit that referenced this pull request May 13, 2026
Release v0.50.0.

Highlights:

- Experimental Azure DevOps support (#1256, thanks @mikeyroush; fixes
#1144 from @dlecan) — `wt switch pr:<N>`, `wt list --full`, and `wt
config show --full` recognize Azure DevOps via the `az` CLI.
- Experimental Gitea CI-status detection (#2702) on top of the Gitea
`pr:` shortcut (#1320, thanks @SjB).
- Hooks now resolve `.config/wt.toml` from the worktree they act on —
the primary-worktree fallback is gone, and `post-remove` reads the
removed worktree's config (snapshotted before removal). Approval prompts
collect hook commands from the same worktree. Breaking change for setups
that relied on the primary-worktree fallback; the changelog entry has
the recovery action. (#2690, #2703, #2714, #2717, #2701, #2708, #2727,
#2736, #2748)
- The `wt switch` picker's `alt-r` removal no longer runs unapproved
project hooks (#2746) — the picker's removal path is now routed through
`handle_remove_output` and consults the existing approval state
read-only.
- `wt config alias show` with no name lists every alias's full
definition (#2684, #2691); `wt --help` switches to a compact aliases
pointer (#2688).
- `wt list --branches` warm-run perf: SHA-keyed cache for `main↕` and
`Remote⇅` ahead/behind counts; shared push-remote URL and local-branch
scan (#2704, #2718, #2673).
- Claude Code plugin ships the `wt-switch-create` skill (#2737, thanks
@onetom for #2631).

See `CHANGELOG.md` for the full list (8 Improved, 5 Fixed, 5 Internal).

semver-checks reports breaking library-API changes (new enum variants
without `#[non_exhaustive]`, removed `Branch::github_push_url`, new
trait method on `RemoteRefProvider`), which mandates at minimum a minor
bump pre-1.0.
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