worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Summary cache: detached worktrees evict each other's cached summary, so every wt list --full re-calls the LLM

Open worktrunk-bot opened this issue 2 weeks ago • 2 comments

With [list] summary = true and an LLM command configured, two worktrees that map to the same summary cache directory evict each other's cached summary. The result is a fresh model call on every wt list --full and every picker Summary tab, even when neither diff has changed. The cached text is still correct, because files are named by diff hash. The cost is paid model calls and slower rows.

How it happens. CachedSummary::write writes summary/<sanitized branch>/<diff hash>.json and then trims that directory to one entry (cache::sweep_lru(.., 1)). The directory is keyed only by the branch string the caller passes:

So every detached worktree with changes shares summary/(detached)/. Two checkouts of the same branch (git worktree add --force) with different uncommitted changes also share a directory. Each run writes A's hash, which trims B's file, and then writes B's hash, which trims A's. On the next run both miss.

Reproduce (by reading; not run against a live LLM). Create two detached worktrees, give each a different uncommitted change, enable summaries with any LLM command, and run wt list --full twice. The second run calls the LLM command for both rows again. A counting command such as sh -c 'echo call >> /tmp/calls; echo summary' shows it.

Possible fix. Key the cache directory by something unique per row when the branch doesn't identify one. For example, rows without a branch could use a hash of the worktree path. The branch field inside the JSON already carries the display name, so wt config state get would not change. Keeping named-branch rows under their existing directory avoids a one-time regeneration for every branch. The duplicate-branch case would need the worktree path in the key too, and that is a trade-off for a maintainer to weigh against that regeneration cost.

worktrunk-bot avatar Nov 18 '26 07:11 worktrunk-bot

Confirmed: every detached worktree wrote to the same summary/(detached)/ directory. A unit test with two detached worktrees shows the second summary re-running the LLM command on the old code.

#4840 fixes the detached case. A detached row's cache directory key now includes a hash of its worktree path, and named branches keep their existing directories, so no cached summary is regenerated. A follow-up commit on the PR also removes the old shared (detached) directory after the first new-style write, so wt config state get doesn't keep showing a stale entry. On the original commit, Linux and Windows tests, lint, and codecov/patch passed. The macOS jobs were cancelled when the branch moved before they finished. CI on the new head is still running, and a maintainer decides whether to merge.

The PR uses Refs, not Closes, because the duplicate-branch case is still open. Two worktrees on the same branch (git worktree add --force) still share a directory. Fixing that means keying every row by worktree path, which costs one regeneration of every cached summary. As the issue says, that trade-off is for a maintainer to weigh.

worktrunk-bot avatar Nov 18 '26 07:11 worktrunk-bot

#4840 fixed the detached case. Two worktrees on one branch is outside the one-branch-per-worktree model, so it isn't worth regenerating every cached summary for. Closing.

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

max-sixty avatar Nov 20 '26 18:11 max-sixty