@devries-cameron20 / t3code-1 / commits / 1a684f0039
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): PR cache cleanup runs once per shared layer (#19791)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Arav Jain committed Oct 19, 2026 at 23:10 UTC 1a684f0039b2e28069b60351adc636fba4aef7d0
2 files changed +100 -8
apps/server/src/pullRequest/PullRequestReadCache.test.ts
+91
@@ -13,6 +13,7 @@ import * as Queue from "effect/Queue";
13 import * as TestClock from "effect/testing/TestClock";
14 import * as KeyValueStore from "effect/persistence/KeyValueStore";
15 import * as ServerConfig from "../config.ts";
16 +import { ServerActivation } from "../serverActivation.ts";
17 import * as PullRequestReadCache from "./PullRequestReadCache.ts";
18
19 const cacheLayer = (directory: string) =>
@@ -61,6 +62,96 @@ it.layer(NodeServices.layer)("PR filesystem cache", (it) => {
62 }),
63 );
64
65 + it.effect("shares one scoped prune loop across cache layer consumers", () =>
66 + Effect.gen(function* () {
67 + const fs = yield* FileSystem.FileSystem;
68 + const activation = yield* Deferred.make<void>();
69 + let scans = 0;
70 + const observedFs = FileSystem.FileSystem.of({
71 + ...fs,
72 + // An empty directory keeps every sweep synchronous under the test clock.
73 + readDirectory: () =>
74 + Effect.sync(() => {
75 + scans++;
76 + return [];
77 + }),
78 + });
79 + const caches: Array<PullRequestReadCache.PullRequestReadCache["Service"]> = [];
80 + const sharedLayer = PullRequestReadCache.layer.pipe(
81 + Layer.provide(Layer.succeed(FileSystem.FileSystem, observedFs)),
82 + );
83 + const consumers = Array.from({ length: 6 }, () =>
84 + Layer.effectDiscard(
85 + Effect.gen(function* () {
86 + caches.push(yield* PullRequestReadCache.PullRequestReadCache);
87 + }),
88 + ).pipe(Layer.provide(sharedLayer)),
89 + );
90 + const scanCounts: Array<number> = [];
91 +
92 + yield* Effect.scoped(
93 + Effect.gen(function* () {
94 + yield* Layer.build(Layer.mergeAll(consumers[0]!, ...consumers.slice(1)));
95 + assert.strictEqual(caches.length, 6);
96 + assert.strictEqual(new Set(caches).size, 1);
97 + scanCounts.push(scans);
98 + yield* Deferred.succeed(activation, undefined);
99 + yield* TestClock.adjust(Duration.zero);
100 + scanCounts.push(scans);
101 + yield* TestClock.adjust("59 minutes");
102 + scanCounts.push(scans);
103 + yield* TestClock.adjust("1 minute");
104 + scanCounts.push(scans);
105 + yield* TestClock.adjust("1 hour");
106 + scanCounts.push(scans);
107 + }).pipe(Effect.provideService(ServerActivation, Deferred.await(activation))),
108 + );
109 + yield* TestClock.adjust("2 hours");
110 + assert.deepStrictEqual([...scanCounts, scans], [0, 1, 1, 2, 3, 3]);
111 + }).pipe(
112 + Effect.scoped,
113 + Effect.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-pr-cache-shared-" })),
114 + ),
115 + );
116 +
117 + it.effect("uses memory without starting cleanup when the cache directory is unavailable", () =>
118 + Effect.gen(function* () {
119 + const fs = yield* FileSystem.FileSystem;
120 + const { providerStatusCacheDir } = yield* ServerConfig.ServerConfig;
121 + yield* fs.remove(providerStatusCacheDir, { recursive: true });
122 + yield* fs.writeFileString(providerStatusCacheDir, "blocks the cache directory");
123 + let scans = 0;
124 + const observedFs = FileSystem.FileSystem.of({
125 + ...fs,
126 + readDirectory: () =>
127 + Effect.sync(() => {
128 + scans++;
129 + return [];
130 + }),
131 + });
132 + yield* Effect.gen(function* () {
133 + const cache = yield* PullRequestReadCache.PullRequestReadCache;
134 + let reads = 0;
135 + const lookup = Effect.sync(() => String(++reads));
136 + assert.strictEqual(yield* cache.get("summary", lookup, ["pr"]), "1");
137 + assert.strictEqual(yield* cache.get("summary", lookup, ["pr"]), "1");
138 + yield* cache.invalidate("pr");
139 + assert.strictEqual(yield* cache.get("summary", lookup, ["pr"]), "2");
140 + yield* TestClock.adjust("1 hour");
141 + assert.strictEqual(scans, 0);
142 + }).pipe(
143 + Effect.provide(
144 + PullRequestReadCache.layer.pipe(
145 + Layer.provide(Layer.succeed(FileSystem.FileSystem, observedFs)),
146 + ),
147 + ),
148 + );
149 + }).pipe(
150 + Effect.scoped,
151 + Effect.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-pr-cache-fallback-" })),
152 + ),
153 + );
154 +
155 it.effect("sweeps the cache directory every hour once the layer is built", () =>
156 Effect.gen(function* () {
157 const fs = yield* FileSystem.FileSystem;
apps/server/src/pullRequest/PullRequestReadCache.ts
+9 -8
@@ -214,17 +214,18 @@ export const layer = Layer.unwrap(
214 const directory = path.join(config.providerStatusCacheDir, "pull-requests");
215 return Layer.effect(PullRequestReadCache, make).pipe(
216 Layer.provide(
217 - KeyValueStore.layerFileSystem(directory).pipe(
218 - Layer.tap(() =>
219 - forkParked(
220 - pruneExpiredEntryFiles(directory).pipe(
221 - Effect.catch((cause) =>
222 - Effect.logWarning("Failed to prune PR cache files", { cause }),
223 - ),
224 - Effect.repeat(Schedule.spaced(Duration.hours(1))),
217 + // Memoize cleanup acquisition with the store; Layer.tap runs per consumer.
218 + Layer.effectDiscard(
219 + forkParked(
220 + pruneExpiredEntryFiles(directory).pipe(
221 + Effect.catch((cause) =>
222 + Effect.logWarning("Failed to prune PR cache files", { cause }),
223 ),
224 + Effect.repeat(Schedule.spaced(Duration.hours(1))),
225 ),
226 ),
227 + ).pipe(
228 + Layer.provideMerge(KeyValueStore.layerFileSystem(directory)),
229 Layer.catch(() =>
230 Layer.effectDiscard(
231 Effect.logWarning("PR cache directory unavailable; using memory cache"),