Skip to content

Backfill every worktree of a repository, live or removed - #6

Merged
frenchie4111 merged 2 commits into
mainfrom
backfill-across-worktrees
Nov 20, 2026
Merged

frenchie4111 merged 2 commits into
mainfrom
backfill-across-worktrees

Conversation

@frenchie4111

Copy link
Copy Markdown
Contributor

Why

Most agent work happens in a worktree that is merged and then deleted. loch backfill only saw sessions whose cwd was inside the checkout it was run from. On claude-harness that meant 17 turns, and loch blame src/main/loch.ts credited 0 of 439 lines, even though a Claude session in a since-removed worktree wrote the whole file.

What

Backfill run from any checkout now covers sessions whose cwd was in:

  • the checkout itself (unchanged),
  • any worktree git worktree list --porcelain names, wherever it lives,
  • a worktree that has since been removed.

Removed worktrees are recognized by what was written, not by where. This assumes no path layout (<repo>-worktrees/, Ness's nested feat/x dirs, .claude/worktrees, /tmp/... are all just paths):

  • Each component prefix of the gone cwd is a candidate worktree root.
  • A candidate wins when it has at least 5 distinct substantive lines the session wrote, found in this repo at the same relative path, at the tip of any branch, tag or worktree. It must also score strictly higher than every other prefix.
  • A tie or less evidence includes nothing.
  • A cwd inside another repository's checkout is never considered, even if it wrote identical text.

Edit paths are made relative to the worktree they were written in. Records use the repo's project id, which is shared across worktrees, and blame matches by project + path + line hash. So a turn from any worktree blames in main once its lines are merged; tests/backfill.rs checks this through a squash merge.

Performance on this machine (3800 transcripts): about 8s for claude-harness.

  • Reading session metadata is 3.7s; the old code pays that too.
  • The repo's 666 ref tips are indexed in 0.3s by walking trees per depth level through cat-file --batch, instead of 7s for one ls-tree -r per tip.
  • A transcript from a gone directory is fully parsed only if a file_path in its raw text could land on a path the repo has.

JSON: each entry in sessions gains a root field (the worktree it worked in, which may no longer exist). That field is additive, so schema stays the same.

Not done

  • Squash-merge commit credit. A squash commit's time is the merge time, not the time of the Bash call that committed, so tolerating a (#N) suffix on the subject would never match anyway. Those lines come from the session's tool edits. Commits still on any ref were already matched via git log --all.
  • Unmerged, removed worktrees. If a removed worktree's work never reached any ref, there is no evidence to recognize it by, and nothing in the repo for it to blame either.

Results on claude-harness

  • 185 turns across 45 sessions in 39 worktrees (was 17 turns).
  • src/main/loch.ts: 383 of 439 lines attributed (was 0).
  • One recognized root, /private/tmp/ev-hotfix, is outside the Ness layout. I checked it: it was a real worktree on fix/evidence-fork-checkout.

Ness

No change is required, since --cwd semantics are unchanged (any checkout of the repo works). Already opted-in repos do need loch backfill run once more after this ships. Re-runs only replace earlier backfilled turns, never hook-recorded ones.

Tests

New in tests/backfill.rs:

  • a session in a live sibling worktree outside any naming convention,
  • a removed worktree at a nested path, with the session cwd in a subdirectory, then squash-merged and its branch deleted (the root is found and paths are relativized to src/feature.py),
  • another repo's sessions not being picked up: a live foreign checkout writing identical text, and a gone directory writing a shared path with different text.

cargo fmt --check, cargo clippy --release --all-targets -D warnings and cargo test --release pass.

🤖 Generated with Claude Code

frenchie4111 and others added 2 commits November 19, 2026 19:14
Most agent work happens in a worktree that is merged and then deleted, and
backfill only looked at sessions inside the checkout it was pointed at: on
claude-harness it found 17 turns, and blame credited none of
src/main/loch.ts, written in a since-removed worktree.

A backfill from any checkout now covers sessions whose cwd was in any
worktree `git worktree list` names, and those in a worktree since removed.
A removed one is recognized by what the session wrote rather than where it
was, so no layout is assumed: each prefix of the gone cwd is a candidate
root, and wins if at least 5 distinct substantive lines the session wrote
are in the repository's files at the same relative paths, at some ref's
tip, strictly more than any other prefix. A cwd inside another
repository's checkout is never considered. Edit paths are made relative to
the worktree they were written in.

On claude-harness: 185 turns across 45 sessions in 39 worktrees, and
loch.ts goes from 0 to 383 of 439 lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From review on #6:
- Blobs are sized with `cat-file --batch-check` first; anything over 1MB is
  skipped as evidence and the rest are read 16MB at a time, so a lockfile
  at hundreds of tips is never held whole.
- A cwd that still exists but is no checkout of this repository is
  `Elsewhere`, as the docs said: only a gone directory can be a worktree
  git has forgotten.
- Tests for a removed worktree nested in the main checkout
  (`.claude/worktrees/<x>`), for a tie deciding nothing, and for an
  existing non-checkout directory, the last two with a positive control so
  neither can pass vacuously.
- CLAUDE.md names the Bash-only-session limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@frenchie4111
frenchie4111 merged commit 25ad792 into main Nov 20, 2026
2 checks passed
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.

1 participant