Skip to content

Commit 731dcf0

Browse files
committed
fix(client): retain warm cache lookups after atom collection
Give resume atoms their own family so a collected raw definition does not create another cache holder. Verify raw-family misses and real Node garbage collection without extra snapshot loads. Created with GPT-6 Astra (preview) in Codex.
1 parent 15e94b7 commit 731dcf0

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

‎packages/client-runtime/src/state/threads-atoms.test.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ const makeHarness = Effect.fn("TestThreadAtoms.makeHarness")(function* (options?
222222
return {
223223
registry,
224224
makeRegistry,
225+
rawAtoms: raw,
225226
stateAtom,
226227
details,
227228
ref,
@@ -251,7 +252,10 @@ function currentThread(
251252
}
252253

253254
describe("createEnvironmentThreadStateAtoms", () => {
254-
afterEach(() => vi.useRealTimers());
255+
afterEach(() => {
256+
vi.useRealTimers();
257+
vi.restoreAllMocks();
258+
});
255259

256260
it.effect("shares one live stream and closes it after the last detail consumer leaves", () =>
257261
Effect.gen(function* () {
@@ -315,6 +319,38 @@ describe("createEnvironmentThreadStateAtoms", () => {
315319
}),
316320
);
317321

322+
it.effect("keeps warm data when the raw atom family's weak entry is collected", () =>
323+
Effect.gen(function* () {
324+
const h = yield* makeHarness();
325+
const oldRaw = h.rawAtoms.stateAtom(TARGET.environmentId, THREAD_ID);
326+
const unmount = h.registry.mount(h.stateAtom);
327+
const first = yield* Queue.take(h.subscriptions);
328+
const latest = { ...THREAD, title: "Newer cached thread" };
329+
yield* Queue.offer(first.events, {
330+
kind: "snapshot",
331+
snapshot: { snapshotSequence: 8, thread: latest },
332+
});
333+
yield* Queue.offer(first.events, { kind: "synchronized" });
334+
yield* observeState(h.registry, h.stateAtom, (value) => value.status === "live");
335+
unmount();
336+
yield* Deferred.await(first.closed);
337+
338+
// Force the weak-family miss without depending on host GC timing.
339+
const deref = WeakRef.prototype.deref;
340+
vi.spyOn(WeakRef.prototype, "deref").mockImplementation(function (this: WeakRef<object>) {
341+
const value = deref.call(this);
342+
return value === oldRaw ? undefined : value;
343+
});
344+
const remount = h.registry.mount(h.stateAtom);
345+
expect(currentThread(h.registry, h.stateAtom)).toBe(latest);
346+
const next = yield* Queue.take(h.subscriptions);
347+
expect(next.afterSequence).toBe(8);
348+
expect(h.counts()).toEqual({ httpLoads: 1, diskLoads: 1, opened: 2, active: 1 });
349+
remount();
350+
yield* Deferred.await(next.closed);
351+
}),
352+
);
353+
318354
it.effect("keeps cached snapshots local to each registry", () =>
319355
Effect.gen(function* () {
320356
const h = yield* makeHarness();

‎packages/client-runtime/src/state/threads.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -813,17 +813,20 @@ export function createEnvironmentThreadStateAtoms<R, E>(
813813
E
814814
>,
815815
) {
816-
const family = Atom.family((key: string) => {
817-
const { environmentId, threadId } = parseThreadKey(key);
818-
// This node retains plain snapshots, not environment scopes or RPC streams.
819-
// A factory gives each registry its own cache and ownership token.
820-
const resumeAtom = Atom.make((): ThreadResumeCache => ({
816+
// Cache definitions must outlive collectible live-atom definitions. The
817+
// registry retains these nodes without retaining environment or RPC scopes.
818+
const resumeFamily = Atom.family((key: string) =>
819+
Atom.make((): ThreadResumeCache => ({
821820
snapshot: undefined,
822821
owner: undefined,
823822
})).pipe(
824823
Atom.setIdleTTL(THREAD_SNAPSHOT_IDLE_TTL_MS),
825824
Atom.withLabel(`environment-thread-resume:${key}`),
826-
);
825+
),
826+
);
827+
const family = Atom.family((key: string) => {
828+
const { environmentId, threadId } = parseThreadKey(key);
829+
const resumeAtom = resumeFamily(key);
827830
return runtime
828831
.atom(
829832
(get) => {

0 commit comments

Comments
 (0)