import { isAbsolute, relative, resolve } from "node:path"; const MAX_ACCESSED_PATHS = 500; const accessedPaths = new Set(); function normalizeRelativePath(path: string): string { return path.replace(/\\/g, "/").replace(/^\.\//, ""); } function isInsideCwd(cwd: string, absolutePath: string): boolean { const relativePath = relative(resolve(cwd), absolutePath); return relativePath === "" || (!relativePath.startsWith("..") && !isAbsolute(relativePath)); } export function recordAccess(cwd: string, absolutePath: string): void { const resolvedCwd = resolve(cwd); const resolvedPath = isAbsolute(absolutePath) ? resolve(absolutePath) : resolve(resolvedCwd, absolutePath); if (!isInsideCwd(resolvedCwd, resolvedPath)) return; const relativePath = normalizeRelativePath(relative(resolvedCwd, resolvedPath)); if (!relativePath) return; if (accessedPaths.has(relativePath)) accessedPaths.delete(relativePath); accessedPaths.add(relativePath); while (accessedPaths.size > MAX_ACCESSED_PATHS) { const oldest = accessedPaths.values().next().value as string | undefined; if (!oldest) break; accessedPaths.delete(oldest); } } export function getAccessedPaths(): string[] { return Array.from(accessedPaths); } export function resetTracker(): void { accessedPaths.clear(); }