Skip to content

Commit 1e96606

Browse files
fix(client-runtime): reopened files show host edits instead of the first read (pingdotgg#29409)
1 parent 8a227eb commit 1e96606

6 files changed

Lines changed: 166 additions & 40 deletions

File tree

‎apps/web/src/state/revisionedQuery.test.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { describe, expect, it } from "vite-plus/test";
44
import { revisionedQuery } from "./revisionedQuery";
55

66
// A stand-in query that counts its fetches.
7-
function setup() {
7+
function setup(readQuery: (cache: Atom.Atom<number>) => Atom.Atom<number> = (cache) => cache) {
88
let fetches = 0;
99
// Node removal runs on the registry's scheduler; releasing flushes it so the
1010
// next mount of the same revision builds a fresh node, as a React remount does.
@@ -15,10 +15,12 @@ function setup() {
1515
return () => {};
1616
},
1717
});
18-
const query = Atom.make(() => {
19-
fetches += 1;
20-
return fetches;
21-
}).pipe(Atom.keepAlive);
18+
const query = readQuery(
19+
Atom.make(() => {
20+
fetches += 1;
21+
return fetches;
22+
}).pipe(Atom.keepAlive),
23+
);
2224
const mount = async (revision: string | null | undefined) => {
2325
const release = registry.mount(revisionedQuery(query, revision));
2426
// A stale read refetches in a microtask, after the render that read it.
@@ -77,6 +79,17 @@ describe("revisionedQuery", () => {
7779
expect(fetches()).toBe(2);
7880
});
7981

82+
it("counts a query as cached while the atom it reads through keeps the value", async () => {
83+
// Like an environment query: its own node goes with its last reader, the cache stays.
84+
const { mount, fetches } = setup((cache) =>
85+
Atom.transform(cache, (get) => get(cache), { initialValueTarget: cache }),
86+
);
87+
88+
(await mount(undefined))();
89+
(await mount("mutation-1"))();
90+
expect(fetches()).toBe(2);
91+
});
92+
8093
it("does not refetch a cached query on a first read before any workspace change", async () => {
8194
const { mount, fetches } = setup();
8295

‎apps/web/src/state/revisionedQuery.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ import { Atom, type AtomRegistry } from "effect/reactivity";
66
const readRevisions = new WeakMap<AtomRegistry.AtomRegistry, WeakMap<object, string | null>>();
77

88
// The registry keys serializable atoms' nodes by their key, not the atom.
9-
const queryKey = (query: Atom.Atom<unknown>) =>
10-
Atom.isSerializable(query) ? query[Atom.SerializableTypeId].key : query;
9+
const hasNode = (registry: AtomRegistry.AtomRegistry, atom: Atom.Atom<unknown>) =>
10+
registry.getNodes().has(Atom.isSerializable(atom) ? atom[Atom.SerializableTypeId].key : atom);
11+
12+
// A query's own node can go with its last reader while the atom it takes its value from
13+
// (an environment query's cache, on an idle TTL) still holds the value.
14+
const isCached = (registry: AtomRegistry.AtomRegistry, query: Atom.Atom<unknown>) =>
15+
hasNode(registry, query) ||
16+
(query.initialValueTarget !== undefined && hasNode(registry, query.initialValueTarget));
1117

1218
const makeRevisionedFamily = (query: Atom.Atom<unknown>) =>
1319
Atom.family((revision: string | null) =>
@@ -25,14 +31,14 @@ const makeRevisionedFamily = (query: Atom.Atom<unknown>) =>
2531
? revisions.get(query) !== revision
2632
: // A reader without a revision may have cached it before the latest
2733
// workspace change; a query nobody has read yet fetches fresh anyway.
28-
revision !== null && get.registry.getNodes().has(queryKey(query));
34+
revision !== null && isCached(get.registry, query);
2935
// Deferred: this builds while React renders, and a refresh would synchronously
3036
// re-render the query's other readers, which React rejects.
3137
if (stale) {
3238
const registry = get.registry;
3339
queueMicrotask(() => {
3440
// No node, as after the registry is disposed: nothing cached to refetch.
35-
if (registry.getNodes().has(queryKey(query))) registry.refresh(query);
41+
if (isCached(registry, query)) registry.refresh(query);
3642
});
3743
}
3844
revisions.set(query, revision);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import type { ProjectFaviconCache, ProjectFaviconTarget } from "../projectFavico
2828
import { createEnvironmentQueryAtomFamily } from "./runtime.ts";
2929

3030
const ASSET_URL_REFRESH_INTERVAL_MS = 30 * 60_000;
31-
const ASSET_URL_STALE_TIME_MS = 5 * 60_000;
3231
const ASSET_URL_IDLE_TTL_MS = 60 * 60_000;
3332
export const ASSET_URL_MAX_IN_FLIGHT = 6;
3433

@@ -291,7 +290,8 @@ export function createAssetEnvironmentAtoms<R, E>(
291290
const createUrl = createEnvironmentQueryAtomFamily(runtime, {
292291
label: "environment-data:assets:create-url",
293292
execute,
294-
staleTimeMs: ASSET_URL_STALE_TIME_MS,
293+
// Each mint signs a new URL, so a remounted reader reuses its URL until renewal is due.
294+
staleTimeMs: ASSET_URL_REFRESH_INTERVAL_MS,
295295
idleTtlMs: ASSET_URL_IDLE_TTL_MS,
296296
refreshIntervalMs: ASSET_URL_REFRESH_INTERVAL_MS,
297297
refreshTrigger: ({ input }) =>

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ export function createFilesystemEnvironmentAtoms<R, E>(
154154
// lists it again. The short TTL only keeps the old listing on screen while it refreshes.
155155
idleTtlMs: BROWSE_IDLE_TTL_MS,
156156
staleTimeMs: 0,
157-
revalidateOnRemount: true,
158157
}),
159158
};
160159
}

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { describe, expect, it } from "@effect/vitest";
2+
import * as NodeV8 from "node:v8";
3+
import * as NodeVM from "node:vm";
4+
import { vi } from "vite-plus/test";
25
import {
36
DEFAULT_MAX_TERMINAL_BUFFER_BYTES,
47
EnvironmentId,
@@ -163,6 +166,7 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness"
163166

164167
return {
165168
runtime,
169+
family,
166170
atom: family({ environmentId: QUERY_ENVIRONMENT.environmentId, input: undefined }),
167171
supervisorSession,
168172
supervisorState,
@@ -1132,6 +1136,87 @@ describe("environment query lifecycle", () => {
11321136
),
11331137
);
11341138

1139+
it.effect("refetches when mounted again after its stale time, while still cached", () =>
1140+
Effect.scoped(
1141+
Effect.gen(function* () {
1142+
let now = 0;
1143+
using _now = vi.spyOn(Date, "now").mockImplementation(() => now);
1144+
let contents = "before edit";
1145+
let fetches = 0;
1146+
const harness = yield* makeEnvironmentQueryHarness(
1147+
Effect.sync(() => {
1148+
fetches += 1;
1149+
return contents;
1150+
}),
1151+
);
1152+
const registry = AtomRegistry.make();
1153+
yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
1154+
const read = readEnvironmentQuery(registry, harness.atom);
1155+
const remount = Effect.gen(function* () {
1156+
const unmount = registry.mount(harness.atom);
1157+
yield* Effect.yieldNow;
1158+
const value = yield* read;
1159+
unmount();
1160+
yield* Effect.yieldNow;
1161+
return value;
1162+
});
1163+
1164+
expect(yield* remount).toBe("before edit");
1165+
contents = "after edit";
1166+
1167+
// Inside the harness's 60s stale time: the cached value stands.
1168+
now = 30_000;
1169+
expect(yield* remount).toBe("before edit");
1170+
expect(fetches).toBe(1);
1171+
1172+
// Past the stale time, well inside the default 5 minute idle TTL.
1173+
now = 61_000;
1174+
expect(yield* remount).toBe("after edit");
1175+
expect(fetches).toBe(2);
1176+
}),
1177+
),
1178+
);
1179+
1180+
it.effect("keeps its cache when collected while unmounted, inside its stale time", () =>
1181+
Effect.scoped(
1182+
Effect.gen(function* () {
1183+
NodeV8.setFlagsFromString("--expose-gc");
1184+
const gc = NodeVM.runInNewContext("gc") as () => void;
1185+
using _now = vi.spyOn(Date, "now").mockReturnValue(0);
1186+
let fetches = 0;
1187+
const harness = yield* makeEnvironmentQueryHarness(
1188+
Effect.sync(() => {
1189+
fetches += 1;
1190+
return fetches;
1191+
}),
1192+
);
1193+
const registry = AtomRegistry.make();
1194+
yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
1195+
// `Atom.family` holds members weakly, so the test must hold none: another environment
1196+
// than `harness.atom`'s, which the harness holds.
1197+
const environmentId = EnvironmentId.make("unheld-environment");
1198+
const query = () => harness.family({ environmentId, input: undefined });
1199+
const first = new WeakRef(query());
1200+
const remount = Effect.gen(function* () {
1201+
const unmount = registry.mount(query());
1202+
yield* Effect.yieldNow;
1203+
const value = yield* readEnvironmentQuery(registry, query());
1204+
unmount();
1205+
return value;
1206+
});
1207+
// A WeakRef target survives until the current job ends, so collect after it.
1208+
const collect = Effect.yieldNow.pipe(Effect.andThen(Effect.sync(gc)));
1209+
1210+
expect(yield* remount).toBe(1);
1211+
yield* collect;
1212+
expect(yield* remount).toBe(1);
1213+
yield* collect;
1214+
expect(first.deref()).toBe(query());
1215+
expect(fetches).toBe(1);
1216+
}),
1217+
),
1218+
);
1219+
11351220
it.effect.each([true, false])("loads thread link metadata with server support %s", (supported) =>
11361221
Effect.scoped(
11371222
Effect.gen(function* () {

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

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
import * as Cause from "effect/Cause";
1212
import * as Effect from "effect/Effect";
1313
import * as Exit from "effect/Exit";
14-
import { identity } from "effect/Function";
1514
import * as Option from "effect/Option";
1615
import * as Schema from "effect/Schema";
1716
import * as Stream from "effect/Stream";
@@ -97,13 +96,6 @@ interface EnvironmentQueryAtomOptions<Input, A, E, R> extends Omit<
9796
readonly persistence?: QueryPersistence<A>;
9897
readonly staleTimeMs?: number;
9998
readonly idleTtlMs?: number;
100-
/**
101-
* Check staleness each time the query is mounted again. By default the idle
102-
* TTL keeps the stale-while-revalidate node warm, so a remount within it
103-
* reads the cached value without checking. The cached value still shows
104-
* while the query revalidates.
105-
*/
106-
readonly revalidateOnRemount?: boolean;
10799
readonly refreshIntervalMs?: number;
108100
readonly refreshTrigger?: (target: {
109101
readonly environmentId: EnvironmentIdType;
@@ -567,6 +559,45 @@ const refreshEveryUntilFinal =
567559
{ initialValueTarget: self },
568560
);
569561

562+
const isStale = (result: AsyncResult.AsyncResult<unknown, unknown>, staleTimeMs: number) => {
563+
if (result.waiting) return false;
564+
const success =
565+
result._tag === "Failure" ? Option.getOrUndefined(result.previousSuccess) : result;
566+
if (success?._tag !== "Success") return result._tag === "Failure";
567+
// @effect-diagnostics-next-line globalDate:off - `AsyncResult` timestamps come from `Date.now`.
568+
return Date.now() - success.timestamp >= staleTimeMs;
569+
};
570+
571+
/**
572+
* Stale-while-revalidate on every mount. `Atom.swr` checks only when its node builds, which an idle
573+
* TTL skips on a remount; this layer has no TTL (a transform drops it) while `self` keeps the cache.
574+
* It reads `self` as a dependency, not a subscription, so `self` goes idle with its last reader.
575+
*/
576+
const revalidateOnMount =
577+
(staleTimeMs: number) =>
578+
<A, E>(
579+
self: Atom.Atom<AsyncResult.AsyncResult<A, E>>,
580+
): Atom.Atom<AsyncResult.AsyncResult<A, E>> =>
581+
Atom.transform(
582+
self,
583+
(get) => {
584+
const current = get(self);
585+
if (Option.isNone(get.self()) && isStale(current, staleTimeMs)) {
586+
let active = true;
587+
get.addFinalizer(() => {
588+
active = false;
589+
});
590+
// Deferred out of the build, which runs while React renders. A plain read drops this
591+
// node before the task runs, so only a mount refetches.
592+
get.registry.schedulerAsync.makeDispatcher().scheduleTask(() => {
593+
if (active && isStale(get.once(self), staleTimeMs)) get.refresh(self);
594+
}, 0);
595+
}
596+
return current;
597+
},
598+
{ initialValueTarget: self },
599+
);
600+
570601
/**
571602
* A query result's data alone. A refetch always publishes a new result (a new
572603
* timestamp, `waiting` on and off), while equal data keeps its identity, so a
@@ -685,15 +716,12 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
685716
readonly input: Input;
686717
}) => Atom.Atom<AsyncResult.AsyncResult<A, E | ER | Error>> {
687718
const connectionAtom = environmentConnectionAtoms(runtime);
719+
// `Atom.family` holds members weakly and the mounted layer has no TTL, so each member lives
720+
// as long as its cache: keyed weakly, this lets both go once the cache's TTL ends.
721+
const membersByCache = new WeakMap<Atom.Atom<unknown>, Atom.Atom<unknown>>();
688722
const family = Atom.family((key: string) => {
689723
const target = parseEnvironmentRpcKey<Input>(key);
690724
const idleTtlMs = options.idleTtlMs ?? 5 * 60_000;
691-
// `Atom.swr` checks staleness only when its node builds. To revalidate on
692-
// remount, the wrappers drop on unmount and the source alone keeps the
693-
// cached value warm.
694-
const keepWarm = Atom.setIdleTTL(idleTtlMs);
695-
const keepSourceWarm = options.revalidateOnRemount ? keepWarm : identity;
696-
const keepWrapperWarm = options.revalidateOnRemount ? identity : keepWarm;
697725
const queryAtom = runtime
698726
.atom<
699727
A,
@@ -817,26 +845,25 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
817845
);
818846
});
819847
})
820-
.pipe(
821-
keepSourceWarm,
822-
Atom.swr({
823-
staleTime: options.staleTimeMs ?? 30_000,
824-
revalidateOnMount: true,
825-
}),
826-
keepWrapperWarm,
827-
);
848+
.pipe(Atom.setIdleTTL(idleTtlMs));
828849
const isFinal = (result: AsyncResult.AsyncResult<A, unknown>) =>
829850
options.isFinal !== undefined && Option.exists(AsyncResult.value(result), options.isFinal);
830851
const intervalQuery =
831852
options.refreshIntervalMs === undefined
832853
? queryAtom
833854
: queryAtom.pipe(refreshEveryUntilFinal(options.refreshIntervalMs, isFinal));
834855
const refreshTrigger = options.refreshTrigger?.(target);
835-
return (
856+
const cache = (
836857
refreshTrigger === undefined
837858
? intervalQuery
838859
: intervalQuery.pipe(refreshOnSignalWhileRead(refreshTrigger, isFinal))
839-
).pipe(keepWrapperWarm, Atom.withLabel(`${options.label}:${key}`));
860+
).pipe(Atom.setIdleTTL(idleTtlMs));
861+
const member = cache.pipe(
862+
revalidateOnMount(options.staleTimeMs ?? 30_000),
863+
Atom.withLabel(`${options.label}:${key}`),
864+
);
865+
membersByCache.set(cache, member);
866+
return member;
840867
});
841868
return (target) => family(environmentRpcKey(target));
842869
}
@@ -934,7 +961,6 @@ export function createEnvironmentRpcQueryAtomFamily<
934961
readonly persistence?: QueryPersistence<EnvironmentRpcSuccess<TTag>>;
935962
readonly staleTimeMs?: number;
936963
readonly idleTtlMs?: number;
937-
readonly revalidateOnRemount?: boolean;
938964
readonly refreshIntervalMs?: number;
939965
readonly refreshTrigger?: (target: {
940966
readonly environmentId: EnvironmentIdType;
@@ -949,9 +975,6 @@ export function createEnvironmentRpcQueryAtomFamily<
949975
...(options.persistence === undefined ? {} : { persistence: options.persistence }),
950976
...(options.staleTimeMs === undefined ? {} : { staleTimeMs: options.staleTimeMs }),
951977
...(options.idleTtlMs === undefined ? {} : { idleTtlMs: options.idleTtlMs }),
952-
...(options.revalidateOnRemount === undefined
953-
? {}
954-
: { revalidateOnRemount: options.revalidateOnRemount }),
955978
...(options.refreshIntervalMs === undefined
956979
? {}
957980
: { refreshIntervalMs: options.refreshIntervalMs }),

0 commit comments

Comments
 (0)