Skip to content

Cache remaining expensive tasks, unify wt list and picker - #2098

Merged
max-sixty merged 2 commits into
mainfrom
list-cache-keys
Apr 11, 2026
Merged

max-sixty merged 2 commits into
mainfrom
list-cache-keys

Conversation

@max-sixty

@max-sixty max-sixty commented Apr 11, 2026 •

Copy link
Copy Markdown
Owner

Adds persistent SHA-keyed caching for is_ancestor, has_added_changes, and branch_diff_stats — the last three operations still running unconditionally in both wt list and the wt switch picker. With all five merge-base-dependent tasks now cached (joining merge-tree-conflicts and merge-add-probe from PR #2085), the stale_branches skip mechanism becomes redundant: the picker's "skip tasks on branches > 50 behind main" optimization was papering over slow first-run cost that the cache now handles eternally.

What changed

New caches (src/git/repository/probe_cache.rs): three new asymmetric SHA-keyed kinds — is-ancestor, has-added-changes, diff-stats — all using the same LRU-swept persistent file layout as the existing kinds. branch_diff_stats skips its cache when sparse checkout is active (path filters make the result environment-dependent).

Dead plumbing removed: EXPENSIVE_TASKS constant, CollectOptions::stale_branches field, skip_expensive_for_stale parameter on collect(), the env-var gate in list/mod.rs, and two obsolete tests + snapshots. Both wt list and the picker now run the same task set.

batch_ahead_behind unconditional: previously only the picker called it. Making it unconditional replaces N rev-list --count calls with one for-each-ref, a pure win for wt list too.

Branch-ref semantics unified across all tasks: all task implementations now prefer the branch name over ctx.branch_ref.commit_sha when one is present. Previously, IsAncestorTask, BranchDiffTask, MergeTreeConflictsTask, and CommittedTreesMatchTask used the worktree's current HEAD sha, which during a rebase-in-progress is transiently at the replayed commit rather than the branch tip. This produced contradictory rows: is_ancestor=true + 1 ahead / 1 behind. Now all tasks consistently report the branch's state, matching git status semantics. Addressed worktrunk-bot review feedback.

Caching docs updated: the ## Caching section in collect/mod.rs (from PR #2097) now lists the three tasks as cached rather than "cacheable but uncached". Also addressed worktrunk-bot PR overlap observation.

Benchmarks: invalidate_caches_auto() now clears .git/wt/cache/ so cold-cache benchmarks exercise cold state. The warm_optimized variant in real_repo_many_branches collapsed since it no longer measures anything different from warm.

Testing

New probe_cache unit tests cover roundtrip + tamper-based cache consultation for each new kind, plus a clear_all_covers_all_kinds regression test. Snapshot updates for test_list_maximum_status_with_git_operation and test_list_json_with_git_operation reflect the branch-ref fix — mid-rebase worktrees now show ✗ (real conflicts) and actual diff stats instead of the transient HEAD's empty state.

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

Adds persistent SHA-keyed caching for `is_ancestor`, `has_added_changes`,
and `branch_diff_stats` — the last three operations still running
unconditionally in both `wt list` and the `wt switch` picker. With all
five merge-base-dependent tasks now cached (joining `merge-tree-conflicts`
and `merge-add-probe`), the `stale_branches` skip mechanism becomes
redundant: the picker's "skip tasks on branches > 50 behind main"
optimization was papering over slow first-run cost that the cache now
handles eternally.

Removes the dead plumbing: `EXPENSIVE_TASKS` constant,
`CollectOptions::stale_branches` field, `skip_expensive_for_stale`
parameter, and the env-var gate that only enabled it in tests. Both
`wt list` and the picker now run the same task set and share the same
cache. `batch_ahead_behind` is promoted to unconditional — one
`git for-each-ref` replaces N individual `rev-list --count` calls for
all callers.

Also fixes a latent inconsistency in `AheadBehindTask`: the non-batch
fallback was computing counts against `wt.head` (the worktree's current
HEAD sha), which during a rebase-in-progress is transiently at the
replayed commit rather than the branch tip — producing misleading
`0/0 same_commit` output when the branch was actually diverged. Unified
both paths to use branch names, matching `git status` semantics.

Benchmark `invalidate_caches_auto()` now also clears `.git/wt/cache/` so
cold-cache benchmarks actually exercise cold state.

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

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

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

A couple of observations rather than blockers:

Cross-task inconsistency during git operations. This PR fixes AheadBehindTask to use the branch name instead of the worktree's transient HEAD sha during rebase/merge — good. But the other tasks that read ctx.branch_ref.commit_sha (IsAncestor, CommittedTreesMatch, HasFileChanges, BranchDiff, MergeTreeConflicts, WouldMergeAdd) still resolve against HEAD. The new list_json_with_git_operation.snap shows the resulting contradiction in a single row: "main_state": "integrated" + "integration_reason": "ancestor" (HEAD ≡ main during rebase) sitting next to "main": { "ahead": 1, "behind": 1 } (branch is actually diverged). Worth deciding whether the other tasks should also prefer the branch tip when one is set, or whether this row is intentionally a "rebase in progress" snapshot. If it's a follow-up, a TODO on BranchRef or in the mid-operation section of the test would point future readers at it.

Overlap with #2097. That PR's new ## Caching doc section in src/commands/list/collect/mod.rs lists IsAncestor, HasFileChanges, and BranchDiff under "Cacheable but uncached" with a recommendation to reuse probe_cache. This PR does exactly that for those three. Whichever lands second should update those bullets so the docs don't immediately go stale.

let (ahead, behind) = if let Some(branch) = ctx.branch_ref.branch.as_deref() {
if let Some(counts) = repo.cached_ahead_behind(&base, branch) {
counts
} else {

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.

Nice fix here. One thing to consider: the orphan check a few lines above (repo.merge_base(&base, &ctx.branch_ref.commit_sha) around line 150) still uses commit_sha, so within this same function the orphan probe references HEAD while the ahead/behind probe references the branch tip. During a conflicted rebase those are different commits — the orphan check could classify the worktree as orphaned (or not) based on the wrong reference. Worth flipping it to the branch name as well for internal consistency.

Addresses worktrunk-bot review feedback on PR #2098:
- Updates `collect/mod.rs` caching docs (from PR #2097) to reflect that
  `IsAncestor`, `HasFileChanges`, and `BranchDiff` are now cached via
  `probe_cache` (no longer "cacheable but uncached")
- Fixes the cross-task inconsistency where `IsAncestorTask`,
  `BranchDiffTask`, `MergeTreeConflictsTask`, and `CommittedTreesMatchTask`
  used `ctx.branch_ref.commit_sha` (the worktree's transient HEAD during a
  rebase-in-progress) instead of the branch name. This caused a contradictory
  row during rebase: `is_ancestor=true` + `1 ahead / 1 behind`. All five
  tasks now prefer the branch name when present, with commit_sha as a
  fallback for detached HEAD items.

The snapshot for `test_list_maximum_status_with_git_operation` updates
accordingly — the feature worktree mid-rebase now shows `✗` (real conflicts
with main) instead of `↕`, plus `+3 -2` diff stats reflecting the branch's
actual changes rather than an empty diff from the transient HEAD.

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

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit d50bdfc into main Apr 11, 2026
33 of 34 checks passed
@max-sixty
max-sixty deleted the list-cache-keys branch April 11, 2026 23:58
max-sixty added a commit that referenced this pull request Apr 13, 2026
The `TaskInput<T>` enum TODO predicted that persistent caching would
force the refactor — a "fourth source" needing real-vs-fabricated
distinction. Caching has since landed (#2085, #2098) without bugs:
the cache is content-addressed (SHA-keyed), so a cache hit is a real
measurement, not a fabrication.

The fabrication risk it warned about is already mitigated case-by-case
in `seed_skipped_task_defaults`, where each "do/don't seed" decision
is documented inline (e.g., the explicit non-seed for `WorkingTreeDiff`
that would otherwise misreport dirty worktrees as clean).

The companion TODO about stale-branch seeding is dormant — no signal
that the current trade-off is wrong.

Drop both. If the type-system pressure ever becomes real, the design
can be re-derived from first principles.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Apr 13, 2026
The `TaskInput<T>` enum TODO predicted that persistent caching would
force the refactor — cached values becoming a "fourth source" needing
the same real-vs-fabricated distinction as live tasks. Caching has since
landed (#2085, #2098) without surfacing the bug class the TODO warned
about: the cache is content-addressed (SHA-keyed), so a cache hit *is* a
real measurement, not a fabrication. The "natural inflection point" came
and went.

The fabrication risk that motivated the TODO is already mitigated
case-by-case in `seed_skipped_task_defaults` — each "do/don't seed"
decision documented inline (e.g., the explicit non-seed for
`WorkingTreeDiff` that would otherwise misreport dirty worktrees as
clean).

The companion TODO about stale-branch seeding is dormant — no signal the
current trade-off is wrong.

Drop both. If the type-system pressure ever becomes real, the design can
be re-derived from first principles.

> _This was written by Claude Code on behalf of Maximilian_

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
max-sixty pushed a commit that referenced this pull request Oct 3, 2026
…#4359)

`wt list --help` (and the generated `list.md` pages, skill mirrors, and
`list-v2.json` schema) explains `·`, JSON `null`, and
`default_branch.integration` in ways the collector no longer matches.
This corrects three claims. No behavior changes.

- **"branch too stale" for `·` and `null`.** #2098 removed the
stale-branch skip (`stale_branches`), and nothing in
`src/commands/list/collect/` skips tasks by branch age now. The
remaining sources of `null` are a timeout, a skipped probe (column plan
or unborn branch), or a failed forge fetch.
- **`integration` "null when a dirty tree skipped the checks".** Dirty
trees don't skip the probes. They run on the committed HEAD either way,
as `json_default_branch` says and
`test_integration_positive_match_ignores_cleanliness` pins with a dirty
`trees_match` result. A dirty integrated branch reports its reason, not
`null`.
- **"every reason renders as the same `⊂`".** `"same_commit"` renders as
`_` (or `–` with uncommitted changes), per
`IntegrationReason::SameCommit` and the Default-branch symbol table in
the same help page. `⊂` also needs a clean tree.

The same commit updates the `JsonDefaultBranch::integration` docstring,
which generates the published schema description, and four internal
comments in `collect/execution.rs` that still listed the stale-branch
skip.

No test is added because this changes only prose. Help snapshots and doc
mirrors are regenerated, and `test_docs_are_in_sync` passes.

---------

Co-authored-by: worktrunk-bot <[email protected]>
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