diff --git a/src/renderer/src/components/terminal-pane/terminal-link-handlers.test.ts b/src/renderer/src/components/terminal-pane/terminal-link-handlers.test.ts index 73e1227e9..054a9086f 100644 --- a/src/renderer/src/components/terminal-pane/terminal-link-handlers.test.ts +++ b/src/renderer/src/components/terminal-pane/terminal-link-handlers.test.ts @@ -12,6 +12,7 @@ import { openFilePathLinkAtBufferPosition, openDetectedFilePath } from './terminal-link-handlers' +import { TERMINAL_PATH_EXISTS_CACHE_MAX_ENTRIES } from './terminal-path-exists-cache' import { handleOscLink } from './terminal-osc-link-routing' import { installHttpLinkClickFallback } from './terminal-url-link-hit-testing' import { registerHttpLinkStoreAccessor } from '@/lib/http-link-routing' @@ -976,6 +977,39 @@ describe('createFilePathLinkProvider range bounds', () => { expect(linkTooltip.textContent).toBe('/repo/CLAUDE.md (⌘+click to open in Orca)') }) + it('bounds the terminal path-exists cache while preserving recent probes', async () => { + const pathExistsCache = new Map() + for (let index = 0; index < TERMINAL_PATH_EXISTS_CACHE_MAX_ENTRIES; index += 1) { + pathExistsCache.set(`active\0/repo/old-${index}.ts`, true) + } + const pane = makePane([makeBufferLine('fresh.ts')]) + const managerRef = { + current: { getPanes: () => [pane] } as unknown as PaneManager + } + const provider = createFilePathLinkProvider( + 1, + { + worktreeId: 'wt-1', + worktreePath: '/repo', + startupCwd: '/repo', + managerRef, + linkProviderDisposablesRef: { current: new Map() }, + pathExistsCache + }, + { textContent: '', style: { display: '' } } as unknown as HTMLElement, + getTerminalFileOpenHint() + ) + + const links = await new Promise((resolve) => { + provider.provideLinks(1, (provided) => resolve(provided ?? [])) + }) + + expect(links.map((link) => link.text)).toEqual(['fresh.ts']) + expect(pathExistsCache.size).toBe(TERMINAL_PATH_EXISTS_CACHE_MAX_ENTRIES) + expect(pathExistsCache.has('active\0/repo/old-0.ts')).toBe(false) + expect(pathExistsCache.get('active\0/repo/fresh.ts')).toBe(true) + }) + it('opens a single-row file path from a direct modifier-click fallback', async () => { setPlatform('Macintosh') const pathExists = createDeferred() diff --git a/src/renderer/src/components/terminal-pane/terminal-link-handlers.ts b/src/renderer/src/components/terminal-pane/terminal-link-handlers.ts index 38cae910d..c4a2f8fca 100644 --- a/src/renderer/src/components/terminal-pane/terminal-link-handlers.ts +++ b/src/renderer/src/components/terminal-pane/terminal-link-handlers.ts @@ -23,6 +23,10 @@ import { rangeForParsedFileLink, type WrappedLogicalLine } from './wrapped-terminal-link-ranges' +import { + readTerminalPathExistsCache, + writeTerminalPathExistsCache +} from './terminal-path-exists-cache' export { openDetectedFilePath } from './terminal-file-open-routing' export { openFilePathLinkAtBufferPosition } from './terminal-file-link-hit-testing' @@ -150,7 +154,7 @@ export function createFilePathLinkProvider( const runtimeEnvironmentId = deps.getRuntimeEnvironmentIdForPane?.(paneId) ?? deps.runtimeEnvironmentId ?? null const cacheKey = `${runtimeEnvironmentId ?? 'active'}\0${resolved.absolutePath}` - const cachedExists = pathExistsCache.get(cacheKey) + const cachedExists = readTerminalPathExistsCache(pathExistsCache, cacheKey) const fileContext = getTerminalFileContext( worktreeId, worktreePath, @@ -162,7 +166,7 @@ export function createFilePathLinkProvider( isRemoteRuntimeFileOperation(fileContext, resolved.absolutePath) ? await runtimePathExists(fileContext, resolved.absolutePath) : await window.api.shell.pathExists(resolved.absolutePath)) - pathExistsCache.set(cacheKey, exists) + writeTerminalPathExistsCache(pathExistsCache, cacheKey, exists) if (!exists) { return null } diff --git a/src/renderer/src/components/terminal-pane/terminal-path-exists-cache.ts b/src/renderer/src/components/terminal-pane/terminal-path-exists-cache.ts new file mode 100644 index 000000000..3bd135b89 --- /dev/null +++ b/src/renderer/src/components/terminal-pane/terminal-path-exists-cache.ts @@ -0,0 +1,34 @@ +export const TERMINAL_PATH_EXISTS_CACHE_MAX_ENTRIES = 1024 + +export function readTerminalPathExistsCache( + cache: Map, + key: string +): boolean | undefined { + const value = cache.get(key) + if (value !== undefined) { + cache.delete(key) + cache.set(key, value) + } + return value +} + +export function writeTerminalPathExistsCache( + cache: Map, + key: string, + exists: boolean +): void { + if (cache.has(key)) { + cache.delete(key) + } else { + // Why: terminal output can contain unbounded unique paths during long + // sessions; keep recent link probes without retaining every path forever. + while (cache.size >= TERMINAL_PATH_EXISTS_CACHE_MAX_ENTRIES) { + const oldestKey = cache.keys().next().value + if (oldestKey === undefined) { + break + } + cache.delete(oldestKey) + } + } + cache.set(key, exists) +}