fix(memory): bound shared directory cache

This commit is contained in:
m4air
2026-09-19 16:14:23 -07:00
parent ad0129e002
commit e4947ce2de
2 changed files with 27 additions and 0 deletions
@@ -6,6 +6,8 @@ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi }
import {
clearConfiguredWorktreeSharedDirectoriesCacheForTests,
getConfiguredWorktreeSharedDirectories,
getConfiguredWorktreeSharedDirectoriesCacheSizeForTests,
MAX_CONFIGURED_SHARED_DIRECTORIES_CACHE_ENTRIES,
getWorktreeSharedLinkPaths,
resolveWorktreeSharedDirectories
} from './worktree-shared-directories'
@@ -175,6 +177,15 @@ describe('resolveWorktreeSharedDirectories', () => {
})
describe('getConfiguredWorktreeSharedDirectories', () => {
it('bounds cache growth when repository paths churn', () => {
for (let index = 0; index < MAX_CONFIGURED_SHARED_DIRECTORIES_CACHE_ENTRIES + 4; index += 1) {
getConfiguredWorktreeSharedDirectories(`/repo-${index}`)
}
expect(getConfiguredWorktreeSharedDirectoriesCacheSizeForTests()).toBe(
MAX_CONFIGURED_SHARED_DIRECTORIES_CACHE_ENTRIES
)
})
let repo: string
beforeEach(() => {
@@ -10,6 +10,7 @@ import { mapWithConcurrency } from '../../shared/map-with-concurrency'
// duplicates disk; `orca.yaml` names the ones every worktree should share instead.
const CONFIGURED_SHARED_DIRECTORIES_CACHE_TTL_MS = 30_000
export const MAX_CONFIGURED_SHARED_DIRECTORIES_CACHE_ENTRIES = 512
// Why: resolving a worktree may list many generated directories; overlap
// independent local probes without flooding the filesystem threadpool.
const SHARED_DIRECTORY_STAT_CONCURRENCY = 8
@@ -33,6 +34,8 @@ export function getConfiguredWorktreeSharedDirectories(repoPath: string): readon
const cached = configuredSharedDirectoriesByRepoPath.get(repoPath)
const now = Date.now()
if (cached && cached.expiresAt > now) {
configuredSharedDirectoriesByRepoPath.delete(repoPath)
configuredSharedDirectoriesByRepoPath.set(repoPath, cached)
return cached.directories
}
const configured = loadHooks(repoPath)?.worktree?.sharedDirectories ?? []
@@ -40,6 +43,15 @@ export function getConfiguredWorktreeSharedDirectories(repoPath: string): readon
directories: configured,
expiresAt: now + CONFIGURED_SHARED_DIRECTORIES_CACHE_TTL_MS
})
while (
configuredSharedDirectoriesByRepoPath.size > MAX_CONFIGURED_SHARED_DIRECTORIES_CACHE_ENTRIES
) {
const oldest = configuredSharedDirectoriesByRepoPath.keys().next()
if (oldest.done || oldest.value === repoPath) {
break
}
configuredSharedDirectoriesByRepoPath.delete(oldest.value)
}
return configured
}
@@ -48,6 +60,10 @@ export function clearConfiguredWorktreeSharedDirectoriesCacheForTests(): void {
configuredSharedDirectoriesByRepoPath.clear()
}
export function getConfiguredWorktreeSharedDirectoriesCacheSizeForTests(): number {
return configuredSharedDirectoriesByRepoPath.size
}
/** Every path Orca may have symlinked into a worktree: the per-user Worktree
* Shared Paths setting plus the repo's `orca.yaml` shared directories.
*