Skip to content

Commit ed5b006

Browse files
fix(server): the PR cache sweep only removes real entry files
The sweep matched any file starting with the store id, so a stray file such as `pr-v2-backup.json` in the cache directory could be deleted. It now matches the store id followed by the 64-character key digest. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 758dc29 commit ed5b006

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

‎apps/server/src/pullRequest/PullRequestReadCache.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,21 @@ it.layer(NodeServices.layer)("PR filesystem cache", (it) => {
4444
);
4545
const unrelated = "unrelated.json";
4646
yield* fs.writeFileString(`${directory}/${unrelated}`, "{}");
47+
// Starts with the store id but is not an entry file.
48+
const prefixed = "pr-v2-backup.json";
49+
yield* fs.writeFileString(`${directory}/${prefixed}`, "{}");
4750
const justExpired = Duration.sum(
4851
PullRequestReadCache.ENTRY_FILE_MAX_AGE,
4952
Duration.seconds(1),
5053
);
51-
yield* ageFiles(directory, [stale!, revisions!, unrelated], justExpired);
54+
yield* ageFiles(directory, [stale!, revisions!, unrelated, prefixed], justExpired);
5255
yield* ageFiles(directory, [fresh!], PullRequestReadCache.ENTRY_FILE_MAX_AGE);
5356

5457
yield* PullRequestReadCache.pruneExpiredEntryFiles(directory);
5558

5659
assert.deepStrictEqual(
5760
(yield* fs.readDirectory(directory)).toSorted(),
58-
[fresh!, revisions!, unrelated].toSorted(),
61+
[fresh!, revisions!, unrelated, prefixed].toSorted(),
5962
);
6063
}),
6164
);

‎apps/server/src/pullRequest/PullRequestReadCache.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const CONCURRENT_READS = 512;
2727
// Persistence prefixes every entry key with the store id, so each entry file
2828
// name starts with it. The scope `revisions` file does not.
2929
const STORE_ID = "pr-v2";
30+
// An entry file is the store id followed by the SHA-256 hex digest of its key.
31+
const ENTRY_FILE_NAME = new RegExp(`^${STORE_ID}[0-9a-f]{64}$`);
3032
/**
3133
* Entries expire a minute after they are written, and a write sets the file's
3234
* mtime. A file untouched for a day is long expired; the day only leaves slack
@@ -186,7 +188,7 @@ export const pruneExpiredEntryFiles = Effect.fn("PullRequestReadCache.pruneExpir
186188
const path = yield* Path.Path;
187189
const cutoff = (yield* Clock.currentTimeMillis) - Duration.toMillis(ENTRY_FILE_MAX_AGE);
188190
const entries = (yield* fileSystem.readDirectory(directory)).filter((name) =>
189-
name.startsWith(STORE_ID),
191+
ENTRY_FILE_NAME.test(name),
190192
);
191193
// One file at a time, and `partition` visits every file, so one locked file
192194
// does not stop the sweep.

0 commit comments

Comments
 (0)