@devries-cameron20 / t3code Mirror · read-only / commits / 6a29d58d14
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 07:15 UTC

fix(client-runtime): closed issue searches no longer build up in memory (#38493)

Co-authored-by: Grok 4.7 <noreply@x.ai> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 18, 2026 at 12:19 UTC 6a29d58d147522f85112c0c7149f2f9032cd955f
2 files changed +91 -8
packages/client-runtime/src/state/issues.test.ts
+82
@@ -1,3 +1,7 @@
1 +// @effect-diagnostics nodeBuiltinImport:off - The retention test forces garbage collection in Node.
2 +import * as NodeTimersPromises from "node:timers/promises";
3 +import * as NodeV8 from "node:v8";
4 +import * as NodeVM from "node:vm";
5 import {
6 EnvironmentId,
7 IssueTrackerKind,
@@ -5,6 +9,7 @@ import {
9 type IssueComment,
10 type IssueCommentsPage,
11 type IssueListEntry,
12 + type IssueListResult,
13 } from "@t3tools/contracts";
14 import * as Effect from "effect/Effect";
15 import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
@@ -13,8 +18,10 @@ import { describe, expect, it } from "vite-plus/test";
18 import {
19 createIssueCommentPager,
20 createMergedIssueCandidatesAtoms,
21 + createMergedIssueListAtoms,
22 issueEntryKey,
23 mergeIssueListPage,
24 + type IssueListTarget,
25 } from "./issues.ts";
26
27 function entry(overrides: Partial<IssueListEntry> & { key: string }): IssueListEntry {
@@ -169,3 +176,78 @@ describe("createMergedIssueCandidatesAtoms", () => {
176 }
177 });
178 });
179 +
180 +describe("createMergedIssueListAtoms", () => {
181 + const answer: IssueListResult = {
182 + viewers: {},
183 + trackers: [],
184 + entries: [],
185 + errors: [],
186 + nextCursors: {},
187 + };
188 +
189 + it("refreshes the open listings of the environment written to", () => {
190 + const reads = new Map<string, number>();
191 + const lists = new Map<string, Atom.Atom<AsyncResult.AsyncResult<IssueListResult>>>();
192 + const listings = createMergedIssueListAtoms((target) => {
193 + const key = `${target.environmentId}:${target.input.query}`;
194 + const existing = lists.get(key);
195 + if (existing !== undefined) return existing;
196 + const atom = Atom.make(() => {
197 + reads.set(key, (reads.get(key) ?? 0) + 1);
198 + return AsyncResult.success(answer);
199 + });
200 + lists.set(key, atom);
201 + return atom;
202 + }, String);
203 + const registry = AtomRegistry.make();
204 + const written = EnvironmentId.make("written");
205 + const other = EnvironmentId.make("other");
206 + try {
207 + const listing = listings.atom([
208 + { environmentId: written, input: { state: "open", query: "bug" } },
209 + { environmentId: other, input: { state: "open", query: "bug" } },
210 + ]);
211 + registry.mount(listing);
212 +
213 + listings.refreshEnvironment(registry, written);
214 + registry.get(listing);
215 + expect(Object.fromEntries(reads)).toEqual({ "written:bug": 2, "other:bug": 1 });
216 + } finally {
217 + registry.dispose();
218 + }
219 + });
220 +
221 + it("releases historical search targets when the list closes", async () => {
222 + NodeV8.setFlagsFromString("--expose-gc");
223 + const gc = NodeVM.runInNewContext("gc") as () => void;
224 + const listings = createMergedIssueListAtoms(
225 + () => Atom.make(AsyncResult.success(answer)),
226 + String,
227 + );
228 + const registry = AtomRegistry.make();
229 + const environmentId = EnvironmentId.make("environment");
230 + try {
231 + const historical = ["search-one", "search-two", "search-three"].map((query) => {
232 + const targets = [
233 + { environmentId, input: { state: "open", query } },
234 + ] satisfies IssueListTarget[];
235 + const close = registry.mount(listings.atom(targets));
236 + close();
237 + return new WeakRef(targets);
238 + });
239 + // Let the registry remove all three unmounted queries and the WeakRefs leave their creation job.
240 + // oxlint-disable-next-line t3code/no-real-time-wait-in-tests -- a zero-delay macrotask boundary, not a wait for time
241 + await NodeTimersPromises.setImmediate();
242 + gc();
243 +
244 + listings.refreshEnvironment(registry, environmentId);
245 + expect(
246 + historical.filter((target) => target.deref() !== undefined).length,
247 + "historical issue searches retained after closing",
248 + ).toBe(0);
249 + } finally {
250 + registry.dispose();
251 + }
252 + });
253 +});
packages/client-runtime/src/state/issues.ts
+9 -8
@@ -300,21 +300,22 @@ export function createMergedIssueListAtoms<E>(
300 ) => {
301 for (const target of targets) registry.refresh(list(target));
302 };
303 - // Every listing asked for, so a write made elsewhere can refresh the ones still shown.
304 - const asked = new Map<string, ReadonlyArray<IssueListTarget>>();
303 + // The targets behind each listing atom, so a write made elsewhere can refresh the ones still
304 + // shown. Weak, so a closed search goes with its atom once the registry drops it.
305 + const targetsOf = new WeakMap<Atom.Atom<MergedIssueList>, ReadonlyArray<IssueListTarget>>();
306 return {
307 atom: (targets: ReadonlyArray<IssueListTarget>) => {
308 if (targets.length === 0) return empty;
308 - const key = JSON.stringify(targets);
309 - asked.set(key, targets);
310 - return family(key);
309 + const atom = family(JSON.stringify(targets));
310 + targetsOf.set(atom, targets);
311 + return atom;
312 },
313 refresh,
314 /** Refreshes each listing of `environmentId` that something still reads. */
315 refreshEnvironment: (registry: AtomRegistry.AtomRegistry, environmentId: EnvironmentId) => {
315 - const live = new Set(registry.getNodes().keys());
316 - for (const [key, targets] of asked) {
317 - if (!live.has(family(key))) continue;
316 + for (const node of registry.getNodes().values()) {
317 + const targets = targetsOf.get(node.atom);
318 + if (targets === undefined) continue;
319 refresh(
320 registry,
321 targets.filter((target) => target.environmentId === environmentId),