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

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

test(server): count leaked PR cache caller data without WeakRefs (#35694)

Julius Marminge committed Nov 12, 2026 at 15:52 UTC f70e349ecaef6560aecb889f77d6f03b9251edc9
1 file changed +4 -13
apps/server/src/pullRequest/PullRequestReadCache.test.ts
+4 -13
@@ -1,5 +1,4 @@
1 import * as NodeV8 from "node:v8";
2 -import * as NodeVM from "node:vm";
2 import { assert, it } from "@effect/vitest";
3 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
4 import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -16,9 +15,6 @@ import * as KeyValueStore from "effect/persistence/KeyValueStore";
15 import * as TestClock from "effect/testing/TestClock";
16 import * as PullRequestReadCache from "./PullRequestReadCache.ts";
17
19 -NodeV8.setFlagsFromString("--expose-gc");
20 -const collectGarbage = NodeVM.runInNewContext("gc") as () => void;
21 -
18 const cacheLayer = (directory: string) =>
19 PullRequestReadCache.make.pipe(Effect.provide(KeyValueStore.layerFileSystem(directory)));
20
@@ -203,13 +199,10 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("PR filesystem ca
199 it.effect("does not keep a caller's data alive after its reads and invalidations", () =>
200 Effect.gen(function* () {
201 const cache = yield* PullRequestReadCache.make;
206 - const sweeps: Array<WeakRef<object>> = [];
202 + class SweepData extends Map<string, number> {}
203 // Like a sync sweep: an Effect.fn made per run that closes over that run's data.
204 const sweep = Effect.fn("sweep")(function* (index: number) {
209 - const groups = new Map<string, number>(
210 - Array.from({ length: 1_000 }, (_, id) => [`group-${id}`, id]),
211 - );
212 - sweeps.push(new WeakRef(groups));
205 + const groups = new SweepData(Array.from({ length: 1_000 }, (_, id) => [`group-${id}`, id]));
206 const read = Effect.fn("read")(function* (pr: string) {
207 const size = groups.size;
208 yield* cache.invalidate(pr);
@@ -219,10 +212,8 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("PR filesystem ca
212 yield* Effect.forEach(["pr-1", "pr-2"], read, { discard: true });
213 });
214 for (let index = 0; index < 5; index++) yield* sweep(index);
222 - // WeakRef targets stay alive until the current job ends, so yield before collecting.
223 - yield* Effect.yieldNow;
224 - collectGarbage();
225 - assert.strictEqual(sweeps.filter((sweepData) => sweepData.deref() !== undefined).length, 0);
215 + // Counts after a full GC. A WeakRef would keep its target alive until the job ends.
216 + assert.strictEqual(NodeV8.queryObjects(SweepData, { format: "count" }), 0);
217 }).pipe(Effect.provide(KeyValueStore.layerMemory)),
218 );
219