Skip to content

Commit 6963228

Browse files
committed
fix(server): recover PR cache from corrupt scope revisions
1 parent 6d82a22 commit 6963228

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,27 @@ it.layer(NodeServices.layer)("PR filesystem cache", (it) => {
123123
}),
124124
);
125125

126+
it.effect.each(["", "{", '{"pr":{"revision":42}}'])(
127+
"recovers from malformed scope revisions without reusing stale entries: %j",
128+
(corrupt) =>
129+
Effect.gen(function* () {
130+
const fs = yield* FileSystem.FileSystem;
131+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pr-cache-" });
132+
const original = yield* cacheLayer(directory);
133+
yield* original.get("summary", Effect.succeed("stale"), ["pr"]);
134+
yield* fs.writeFileString(directory + "/revisions", corrupt);
135+
const recovered = yield* cacheLayer(directory);
136+
let reads = 0;
137+
const lookup = Effect.sync(() => String(++reads));
138+
assert.strictEqual(yield* recovered.get("summary", lookup, ["pr"]), "1");
139+
assert.strictEqual(yield* recovered.get("summary", lookup, ["pr"]), "1");
140+
yield* recovered.invalidate("pr");
141+
assert.strictEqual(yield* recovered.get("summary", lookup, ["pr"]), "2");
142+
const restarted = yield* cacheLayer(directory);
143+
assert.strictEqual(yield* restarted.get("summary", lookup, ["pr"]), "2");
144+
assert.strictEqual(reads, 2);
145+
}),
146+
);
126147
it.effect("resumes caching after a failed scope read", () =>
127148
Effect.gen(function* () {
128149
const backing = yield* KeyValueStore.KeyValueStore;

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,17 @@ export const make = Effect.gen(function* () {
8888
crypto.digest("SHA-256", new TextEncoder().encode(key)).pipe(Effect.map(Hex.encode));
8989
const revisions = yield* Cache.makeWith(
9090
() =>
91-
backing
92-
.get("revisions")
93-
.pipe(Effect.flatMap((raw) => Schema.decodeEffect(revisionCodec)(raw ?? "{}"))),
91+
backing.get("revisions").pipe(
92+
Effect.flatMap((raw) => Schema.decodeEffect(revisionCodec)(raw ?? "{}")),
93+
Effect.catchTags({
94+
SchemaError: Effect.fn("PullRequestReadCache.recoverRevisions")(function* () {
95+
// Without scope revisions, persisted PR entries cannot be trusted.
96+
yield* backing.clear;
97+
yield* Effect.logWarning("PR cache reset after invalid scope revisions");
98+
return yield* Schema.decodeEffect(revisionCodec)("{}");
99+
}),
100+
}),
101+
),
94102
{
95103
capacity: 1,
96104
timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero),

0 commit comments

Comments
 (0)