Skip to content

Commit 6d15c5b

Browse files
authored
fix(server): preserve usage cache outside walked roots (#8540)
Co-authored-by: Simone <[email protected]>
1 parent f47e740 commit 6d15c5b

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎apps/server/src/usage/usageScanCache.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ describe("pruneScanCache with an unwalked root", () => {
193193
expect(removed).toBe(0);
194194
expect(cache.size).toBe(1);
195195
});
196+
197+
it("keeps entries under a sibling path that only shares the walked root prefix", () => {
198+
const cache = cacheWith([["/claude/projects-copy/a.jsonl", 5000, [record()]]]);
199+
200+
const removed = pruneScanCache(cache, {
201+
livePaths: new Set(),
202+
walkedRoots: ["/claude/projects"],
203+
windowStartMs: 4000,
204+
retentionCutoffMs: 1000,
205+
});
206+
207+
expect(removed).toBe(0);
208+
expect(cache.size).toBe(1);
209+
});
196210
});
197211

198212
describe("dedupeWithinFile", () => {

‎apps/server/src/usage/usageScanCache.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
*
1515
* @module usageScanCache
1616
*/
17+
// @effect-diagnostics nodeBuiltinImport:off
18+
import * as NodePath from "node:path";
19+
1720
import type { UsageProviderKind } from "@t3tools/contracts";
1821

1922
import type { UsageRecord } from "./usageTranscripts.ts";
@@ -229,7 +232,15 @@ export function pruneScanCache(cache: ScanCache, options: PruneOptions): number
229232
let removed = 0;
230233
for (const [path, entry] of cache) {
231234
const agedOut = entry.mtimeMs < options.retentionCutoffMs;
232-
const underWalkedRoot = options.walkedRoots.some((root) => path.startsWith(root));
235+
const underWalkedRoot = options.walkedRoots.some((root) => {
236+
const relative = NodePath.relative(root, path);
237+
return (
238+
relative === "" ||
239+
(relative !== ".." &&
240+
!relative.startsWith(`..${NodePath.sep}`) &&
241+
!NodePath.isAbsolute(relative))
242+
);
243+
});
233244
const deleted =
234245
underWalkedRoot && entry.mtimeMs >= options.windowStartMs && !options.livePaths.has(path);
235246
if (agedOut || deleted) {

0 commit comments

Comments
 (0)