Repository navigation
Reclaim stale temporary indexes with age-based cleanup - #4469
Conversation
`TempIndex`'s `Drop` is the only thing that removes the index copy `temp_index()` writes, so a `SIGKILL` strands it. A host that renders the statusline in the background does exactly that as a matter of course: it supersedes a slow `wt list statusline` on the next refresh. Each orphan is a full copy of the repository index, so the files accumulate without bound — measured at 52,786 files and 127 GB of `/tmp` over three days on a machine running ~25 concurrent sessions against a 2.6 MB index. Name each temp index after its creating PID and sweep the directory before creating the next one, reclaiming entries whose owner is gone. Only worktrunk's own files are candidates, so a foreign temp file in a shared directory is never touched, and an age backstop covers a reused PID. The sweep is best-effort in the manner of `cache::sweep_lru`: errors are logged at debug and the next call tries again. Drop still handles every ordinary exit; this only covers the kill. Co-Authored-By: Claude Opus 5 <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
temp_index() is called once per worktree during wt list (the conflict probe in src/commands/list/collect/tasks.rs calls write_tracked_worktree_tree / write_index_tree, and working_tree_diff_stats calls it again when untracked files exist). The picker preview calls it too. Because the sweep runs inside temp_index(), each of those calls reads the whole system temp directory, mostly in parallel threads. That cost grows with the number of worktrees and the size of /tmp, and it lands on the statusline path this PR is meant to help. A per-process Once keeps the reclaim and cuts this to one sweep per wt invocation. Orphans can only come from other processes, so sweeping more often within a single process doesn't reclaim anything extra.
temp_index() runs once per worktree during wt list, mostly in parallel, and each call re-read the whole temp directory. Orphans only come from other processes, so the first caller sweeps and the rest skip it without waiting. Collapse the sweep's logging and cover process_is_alive and the failed-remove path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
process_is_alive's test is Unix-only, so importing it at module scope left an unused import on Windows. Log a NotFound remove like any other error, and fold the root check into the assertion, so no line is left that tests never reach. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@jonah-osmo thanks! @worktrunk-bot please explain the impact of this? where are temp indexes accruing / how would that be happening? something needs to be killing remind me: are the indexes always specific to a single |
|
Short version: every Where they accrue. What kills Does it delete files a
Are they per-process? They're narrower than that: each one lasts for a single operation, and one Why the system temp dir rather than an XDG/worktrunk-owned path? That choice came from #3906. Probe storage goes to system temp first so Two related gaps, from reading the code:
|
|
|
Short answers: (1) They're already in the OS temp dir, but the OS clears it far too slowly for this leak, and git needs a real path, so there's no "delete when the process dies" option. (2) Yes, age-only works. The one thing to get right is the threshold: it trades simplicity against how many orphans pile up. (3) It sweeps once per 1. OS cleanup. 2. Age-only. No current holder lives long:
Hooks and aliases run as child processes and never receive a temp index, so a long-running hook doesn't matter here. The real hazard with age-only is deleting the file while a git command is about to read it. That fails silently instead of with an error: git treats a missing What the PID check buys is a shorter steady state. Under age-only, orphans build up to roughly threshold × leak rate. The report's rate (127 GB over three days) works out to about 1.8 GB per hour, so a 1-hour threshold would leave about 1.8 GB on that machine and the PR's current 24 h about 42 GB. The PID check reclaims an orphan on the next run instead. Without it, the change drops 3. Cadence. The sweep runs inside
|
|
one more idea: why are we producing so many & large temp index files? I thought these were really small? is there anything we can do there? would much rather rely on OS cleanups than managing it ourselves, if possible |
|
Short version: each copy is the whole index, and an index holds about 100 bytes for every tracked file. It's small in a small repo; the reporter's 2.6 MB index means roughly 25k tracked files. We can cut how often copies are made, and on the statusline path we can stop making them, which is the lever that would let OS cleanup be enough. Making each copy smaller isn't practical. Why each copy is large. Git has no partial index. Why there are so many. The statusline renders one worktree but runs every
A worktree an agent is editing nearly always meets both conditions. A copy leaks only if the render is killed partway. Claude Code debounces updates at 300 ms and cancels an in-flight script when a new update fires. So while an agent is streaming, any render slower than the update rate gets cancelled again and again, and each cancellation can leave ~5 MB behind. Across ~25 sessions that adds up to the reported 52k files. What we could do, roughly in order of effect:
With 1 and 2, the statusline creates no temp index at all, so there's nothing on that path for a cancellation to leave behind. The remaining leaks would come from a killed I haven't measured where the reporter's render time goes. A Dead ends checked
|
Tend's review approved any PR with no defect. Submit said "If there are no issues, approve with an empty body — silence means correct", and a `code-review` finding needs a file, line and failure scenario, so a correct change that wasn't worth its cost got an `APPROVE` unless another check, such as the duplication check, turned its cost into a finding. A repository overlay asking for a `COMMENT` on such changes didn't override that default: worktrunk's reviews opened its proportionality rule and approved anyway (max-sixty/worktrunk#4664, max-sixty/worktrunk#4665, max-sixty/worktrunk#4666, max-sixty/worktrunk#4670, max-sixty/worktrunk#4689, max-sixty/worktrunk#4694). Submit now says an `APPROVE` tells the maintainer the whole PR is worth merging. Before one, including after a trivial increment, the session says who loses what if the PR never merges and how they come to need it, what it adds that later changes must keep working, and the cheapest other way to get the benefit. It approves when the benefit pays for what the PR adds and nothing materially cheaper exists; otherwise it posts a `COMMENT` naming the cost or the cheaper way. On a self-authored PR, where the review stays silent instead of approving, the same weighing runs before that silence, so a correct bot PR that isn't worth merging is raised as a concern, which Review's "carry its weight" bullet used to ask for. That bullet goes, since Submit now owns the judgment. A `COMMENT` that holds the verdict names its reason once, as a merge-readiness hold already did, so a later push doesn't restate it, and a re-targeted review that empties takes its verdict from Submit instead of approving. ## Evidence Eleven trajectory cases (`evals/cases/review-worth-*`) replay real worktrunk PRs at their reviewed heads. On 20 Nov, with main (including #1646 and #1712) merged in, this PR's review skill ran against main's, three attempts per case per arm. On the four PRs that independent reviewers judged not worth merging as written (max-sixty/worktrunk#4664, max-sixty/worktrunk#4665, max-sixty/worktrunk#4666, max-sixty/worktrunk#4694), these attempts held the verdict and named the cost or a cheaper route: | Executor | main | This PR | | --- | --- | --- | | Codex gpt-6.1-sol | 6 of 10 (2 attempts failed on model capacity) | 10 of 12 | | Claude Opus 5.5 | 2 of 12 | 7 of 12 | - Main's six Codex holds are on max-sixty/worktrunk#4666 and max-sixty/worktrunk#4694, which the old text approved in all three attempts on the 5 Nov base. Each review of max-sixty/worktrunk#4666 names the second cache as a copy of the existing cached `git remote get-url` lookup, the kind of copy #1646 widened the duplication check to flag; the reviews of max-sixty/worktrunk#4694 name the hand-written URL parsing as more than the IPv6 fix needs. This PR also holds max-sixty/worktrunk#4665 and max-sixty/worktrunk#4664 (2 of 3 each), which main's text approved in every attempt that completed. - On the PRs that should be approved (max-sixty/worktrunk#4334, max-sixty/worktrunk#4469, max-sixty/worktrunk#4668, max-sixty/worktrunk#4669, max-sixty/worktrunk#4680), Codex approved in all 15 attempts on each arm. Claude, run on this PR's text only, held max-sixty/worktrunk#4668 in 3 of 3, citing a `git rev-parse` per noted directory on every pane refresh and a narrower fix, and held max-sixty/worktrunk#4669 (2 of 3) and max-sixty/worktrunk#4680 (1 of 3) over defects. - On the 5 Nov base, not re-run: a standing hold that should survive a test-only push stayed silent 6/6, a hold on a per-render cost stayed held 6/6, and `review-landing-continuation` passed 3/3 on Codex. ## Costs A trivial follow-up push now gets a whole-PR read. > _This was written by Claude Code on behalf of max-sixty_
Abruptly terminated commands can leave full temporary Git index copies behind. The contributor reported 52,786 files totaling 127 GB after three days of frequent statusline cancellations.
Reclaim index copies and their Git lock siblings older than one hour when a command next creates a temporary index. Keep the existing filename prefix so cleanup also handles files left by older versions. Sweep the system temp directory once per process, without making other probe threads wait, and retain best-effort cleanup in the Git-directory fallback. Normal exits still remove their own temporary indexes.
This uses age alone: no PID-tagged filenames, process checks, or platform-specific liveness behavior. Move commit-preview approval before temporary-index creation so an unattended approval prompt cannot age out an index needed afterward. The one-hour limit is a retention policy; a suspended operation or unusually slow Git filter can still exceed it. Statusline computations are unchanged.
Validation: regression tests cover stale indexes and locks, recent/future/unrelated files, both storage locations, cleanup failures, real-index preservation, and the actual approval wait. The approval test fails with the old ordering and passes with the fix. Independent review, lint, docs sync, and docs build passed. The local full gate passed 5,412 tests; three PowerShell tests failed because the installed pwsh also crashes with Stack overflow on a standalone arithmetic command outside the repository.