fix(sync): stop directory cache thrashing when a project is expanded

Expanding a project with more worktrees and sessions than MAX_DIR_STORES put
the sidebar into an endless request loop (#1472).

Every sidebar row calls ensureChild during render, but the pin that protects
the directory is only taken in an effect after commit. ensureChild marked the
directory and ran eviction synchronously, so directories that were actively
rendering looked unpinned and were disposed. The next render recreated them
with a loading status, which issued another bootstrap request, and the cycle
repeated for as long as the project stayed expanded.

Raising the limit only moves the cliff, so the limit is now a soft target
instead: a directory touched within a grace window is never an overflow
victim. A burst of live directories overflows the cache briefly rather than
thrashing, while idle-time eviction still bounds it. Eviction is also coalesced
into one deferred pass per tick, so a render that mounts many rows no longer
sorts and scans every directory once per row, and a whole commit's pin effects
settle before anything is considered for disposal. Releasing the final consumer
stays synchronous, since that is an explicit lifecycle edge.

The idle profiler gains --expand-projects to reach this state.

Not yet verified end to end: reproducing the loop needs many worktrees under
one project, which this development environment does not have.
This commit is contained in:
Bohdan Triapitsyn
2026-08-03 16:28:44 +03:00
parent fe9e2471cb
commit ea9bb52fe7
5 changed files with 170 additions and 5 deletions
+8 -1
View File
@@ -26,6 +26,7 @@ export function hasPendingBlockingRequests(state: State | undefined): boolean {
export function pickDirectoriesToEvict(input: EvictPlan) {
const overflow = Math.max(0, input.stores.length - input.max)
let pendingOverflow = overflow
const graceMs = input.graceMs ?? 0
const sorted = input.stores
.filter((dir) => !input.pins.has(dir))
.filter((dir) => !input.hasPendingBlockingRequests?.(dir))
@@ -34,8 +35,14 @@ export function pickDirectoriesToEvict(input: EvictPlan) {
const output: string[] = []
for (const dir of sorted) {
const last = input.state.get(dir)?.lastAccessAt ?? 0
const idle = input.now - last >= input.ttl
const age = input.now - last
const idle = age >= input.ttl
if (!idle && pendingOverflow <= 0) continue
// A directory touched moments ago is almost certainly still mounted and
// merely waiting for its pin effect to run. Evicting it starts the
// recreate/bootstrap loop this grace window exists to prevent; going over
// the limit for a while is the cheaper failure.
if (!idle && age < graceMs) continue
output.push(dir)
if (pendingOverflow > 0) pendingOverflow -= 1
}