Skip to content

Commit 9d7384d

Browse files
fix(client-runtime): reopening a folder picker refreshes its listing (pingdotgg#23289)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 20081ef commit 9d7384d

3 files changed

Lines changed: 74 additions & 4 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,11 @@ export function createFilesystemEnvironmentAtoms<R, E>(
150150
browse: createEnvironmentRpcQueryAtomFamily(runtime, {
151151
label: "environment-data:filesystem:browse",
152152
tag: WS_METHODS.filesystemBrowse,
153-
// A listing kept alive while unread is never revalidated, so reopening the browser
154-
// would show folders as they were. Keep it only long enough for a prefetch to land.
153+
// A listing mirrors a directory that can change outside the app, so reopening a picker
154+
// lists it again. The short TTL only keeps the old listing on screen while it refreshes.
155155
idleTtlMs: BROWSE_IDLE_TTL_MS,
156+
staleTimeMs: 0,
157+
revalidateOnRemount: true,
156158
}),
157159
};
158160
}

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "@effect/vitest";
22
import {
33
EnvironmentId,
4+
type FilesystemBrowseEntry,
45
ORCHESTRATION_V2_WS_METHODS,
56
ThreadId,
67
ProjectId,
@@ -42,6 +43,7 @@ import type * as RpcSession from "../rpc/session.ts";
4243
import { createOrchestrationEnvironmentAtoms } from "./orchestration.ts";
4344
import { createReviewEnvironmentAtoms } from "./review.ts";
4445
import { createPreviewEnvironmentAtoms } from "./preview.ts";
46+
import { createFilesystemEnvironmentAtoms } from "./filesystem.ts";
4547
import type { QueryPersistence } from "./queryPersistence.ts";
4648
import type { EnvironmentShellState } from "./shell.ts";
4749
import { createTerminalEnvironmentAtoms } from "./terminal.ts";
@@ -1053,6 +1055,53 @@ describe("environment query lifecycle", () => {
10531055
),
10541056
);
10551057

1058+
it.effect("filesystem browse lists the directory afresh when a picker reopens it", () =>
1059+
Effect.scoped(
1060+
Effect.gen(function* () {
1061+
const harness = yield* makeEnvironmentQueryHarness(Effect.void);
1062+
const entry = (name: string): FilesystemBrowseEntry => ({
1063+
name,
1064+
fullPath: `/tmp/t3-11476-repro/${name}`,
1065+
});
1066+
let onDisk = [entry("existing")];
1067+
const client = {
1068+
[WS_METHODS.filesystemBrowse]: () =>
1069+
Effect.sync(() => ({ parentPath: "/tmp/t3-11476-repro/", entries: onDisk })),
1070+
} as unknown as RpcSession.RpcSession["client"];
1071+
yield* SubscriptionRef.set(
1072+
harness.supervisorSession,
1073+
Option.some({ ...QUERY_RPC_SESSION, client }),
1074+
);
1075+
const atom = createFilesystemEnvironmentAtoms(harness.runtime).browse({
1076+
environmentId: QUERY_ENVIRONMENT.environmentId,
1077+
input: { partialPath: "/tmp/t3-11476-repro/" },
1078+
});
1079+
const registry = AtomRegistry.make();
1080+
yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
1081+
// Stale-while-revalidate refreshes from a task on the registry scheduler;
1082+
// queue behind it so the read sees the revalidation instead of racing it.
1083+
const afterScheduledTasks = Effect.callback<void>((resume) => {
1084+
registry.schedulerAsync.makeDispatcher().scheduleTask(() => resume(Effect.void), 0);
1085+
});
1086+
const listNames = afterScheduledTasks.pipe(
1087+
Effect.andThen(readEnvironmentQuery(registry, atom)),
1088+
Effect.map((result) => result.entries.map((browseEntry) => browseEntry.name)),
1089+
);
1090+
1091+
const closePicker = registry.mount(atom);
1092+
expect(yield* listNames).toEqual(["existing"]);
1093+
closePicker();
1094+
// A closed picker stays closed past the registry's scheduled node
1095+
// removal; a remount within the same tick keeps the live node.
1096+
yield* afterScheduledTasks;
1097+
1098+
onDisk = [entry("created-externally"), entry("existing")];
1099+
registry.mount(atom);
1100+
expect(yield* listNames).toEqual(["created-externally", "existing"]);
1101+
}),
1102+
),
1103+
);
1104+
10561105
it.effect("refreshes on its trigger while read, and once on its next read when idle", () =>
10571106
Effect.scoped(
10581107
Effect.gen(function* () {

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ 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";
1415
import * as Option from "effect/Option";
1516
import * as Schema from "effect/Schema";
1617
import * as Stream from "effect/Stream";
@@ -96,6 +97,13 @@ interface EnvironmentQueryAtomOptions<Input, A, E, R> extends Omit<
9697
readonly persistence?: QueryPersistence<A>;
9798
readonly staleTimeMs?: number;
9899
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;
99107
readonly refreshIntervalMs?: number;
100108
readonly refreshTrigger?: (target: {
101109
readonly environmentId: EnvironmentIdType;
@@ -673,6 +681,12 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
673681
const family = Atom.family((key: string) => {
674682
const target = parseEnvironmentRpcKey<Input>(key);
675683
const idleTtlMs = options.idleTtlMs ?? 5 * 60_000;
684+
// `Atom.swr` checks staleness only when its node builds. To revalidate on
685+
// remount, the wrappers drop on unmount and the source alone keeps the
686+
// cached value warm.
687+
const keepWarm = Atom.setIdleTTL(idleTtlMs);
688+
const keepSourceWarm = options.revalidateOnRemount ? keepWarm : identity;
689+
const keepWrapperWarm = options.revalidateOnRemount ? identity : keepWarm;
676690
const queryAtom = runtime
677691
.atom<
678692
A,
@@ -797,11 +811,12 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
797811
});
798812
})
799813
.pipe(
814+
keepSourceWarm,
800815
Atom.swr({
801816
staleTime: options.staleTimeMs ?? 30_000,
802817
revalidateOnMount: true,
803818
}),
804-
Atom.setIdleTTL(idleTtlMs),
819+
keepWrapperWarm,
805820
);
806821
const isFinal = (result: AsyncResult.AsyncResult<A, unknown>) =>
807822
options.isFinal !== undefined && Option.exists(AsyncResult.value(result), options.isFinal);
@@ -814,7 +829,7 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
814829
refreshTrigger === undefined
815830
? intervalQuery
816831
: intervalQuery.pipe(refreshOnSignalWhileRead(refreshTrigger, isFinal))
817-
).pipe(Atom.setIdleTTL(idleTtlMs), Atom.withLabel(`${options.label}:${key}`));
832+
).pipe(keepWrapperWarm, Atom.withLabel(`${options.label}:${key}`));
818833
});
819834
return (target) => family(environmentRpcKey(target));
820835
}
@@ -912,6 +927,7 @@ export function createEnvironmentRpcQueryAtomFamily<
912927
readonly persistence?: QueryPersistence<EnvironmentRpcSuccess<TTag>>;
913928
readonly staleTimeMs?: number;
914929
readonly idleTtlMs?: number;
930+
readonly revalidateOnRemount?: boolean;
915931
readonly refreshIntervalMs?: number;
916932
readonly refreshTrigger?: (target: {
917933
readonly environmentId: EnvironmentIdType;
@@ -926,6 +942,9 @@ export function createEnvironmentRpcQueryAtomFamily<
926942
...(options.persistence === undefined ? {} : { persistence: options.persistence }),
927943
...(options.staleTimeMs === undefined ? {} : { staleTimeMs: options.staleTimeMs }),
928944
...(options.idleTtlMs === undefined ? {} : { idleTtlMs: options.idleTtlMs }),
945+
...(options.revalidateOnRemount === undefined
946+
? {}
947+
: { revalidateOnRemount: options.revalidateOnRemount }),
929948
...(options.refreshIntervalMs === undefined
930949
? {}
931950
: { refreshIntervalMs: options.refreshIntervalMs }),

0 commit comments

Comments
 (0)