Skip to content

Commit 567a105

Browse files
authored
fix(client-runtime): expire missing file metadata per thread (#30606)
1 parent 647bd14 commit 567a105

2 files changed

Lines changed: 62 additions & 7 deletions

File tree

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from "@effect/vitest";
1+
import { describe, expect, it, vi } from "@effect/vitest";
22
import {
33
EnvironmentId,
44
ThreadId,
@@ -17,6 +17,7 @@ import * as Option from "effect/Option";
1717
import * as Stream from "effect/Stream";
1818
import * as Schema from "effect/Schema";
1919
import * as SubscriptionRef from "effect/SubscriptionRef";
20+
import * as TestClock from "effect/testing/TestClock";
2021
import { Atom, AtomRegistry } from "effect/reactivity";
2122

2223
import { AVAILABLE_CONNECTION_STATE } from "../connection/model.ts";
@@ -118,6 +119,42 @@ const makeHarness = Effect.fnUntraced(function* () {
118119
});
119120

120121
describe("file metadata", () => {
122+
it.effect("refreshes expired mounted misses while retaining known thread entries", () =>
123+
Effect.gen(function* () {
124+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
125+
yield* Effect.addFinalizer(() => Effect.sync(() => vi.useRealTimers()));
126+
const h = yield* makeHarness();
127+
const threadId = ThreadId.make("new-directory-thread");
128+
h.registry.mount(h.atoms.retainThread({ environmentId: ENVIRONMENT_ID, threadId }));
129+
const path = "/home/user/games/hl1";
130+
h.registry.mount(h.atom(path, ENVIRONMENT_ID, threadId));
131+
h.values.set("/workspace/known", { kind: "file", byteLength: 5 });
132+
expect(yield* h.read(path, ENVIRONMENT_ID, threadId)).toBeNull();
133+
yield* h.read("/workspace/known", ENVIRONMENT_ID, threadId);
134+
h.values.set(path, { kind: "directory" });
135+
h.registry.refresh(h.atom(path, ENVIRONMENT_ID, threadId));
136+
expect(yield* h.read(path, ENVIRONMENT_ID, threadId)).toBeNull();
137+
h.failure.denied = true;
138+
yield* TestClock.adjust(60_001);
139+
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(60_000));
140+
expect(yield* h.read(path, ENVIRONMENT_ID, threadId).pipe(Effect.flip)).toBeInstanceOf(
141+
EnvironmentAuthorizationError,
142+
);
143+
h.failure.denied = false;
144+
yield* TestClock.adjust(60_001);
145+
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(60_000));
146+
expect(yield* h.read(path, ENVIRONMENT_ID, threadId)).toEqual({ kind: "directory" });
147+
h.registry.refresh(h.atom("/workspace/known", ENVIRONMENT_ID, threadId));
148+
expect(yield* h.read("/workspace/known", ENVIRONMENT_ID, threadId)).toEqual({
149+
kind: "file",
150+
byteLength: 5,
151+
});
152+
yield* TestClock.adjust(60_001);
153+
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(60_000));
154+
expect(h.batches).toEqual([[path], ["/workspace/known"], [path], [path]]);
155+
}).pipe(Effect.scoped),
156+
);
157+
121158
it.effect("does not seed a different path from a literal name with a numeric suffix", () =>
122159
Effect.gen(function* () {
123160
const h = yield* makeHarness();

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import { normalizeProjectPathForComparison, resolveWorkspaceFilePath } from "@t3
1111
import * as Clock from "effect/Clock";
1212
import * as Effect from "effect/Effect";
1313
import * as Exit from "effect/Exit";
14+
import * as Option from "effect/Option";
1415
import * as Request from "effect/Request";
1516
import * as RequestResolver from "effect/RequestResolver";
1617
import * as SubscriptionRef from "effect/SubscriptionRef";
17-
import { Atom, AtomRegistry } from "effect/reactivity";
18+
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
1819

1920
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
2021
import type { EnvironmentRegistry } from "../connection/registry.ts";
@@ -200,8 +201,10 @@ export function createFileMetadataAtoms<R, E>(
200201
thread.sessions.set(session.value, retained);
201202
}
202203
const retainedEntry = retained?.get(input.path);
203-
if (retainedEntry) return retainedEntry.value;
204204
const now = yield* Clock.currentTimeMillis;
205+
// Agents mention paths before creating them, so misses must still expire within a thread.
206+
if (retainedEntry && (retainedEntry.value !== null || retainedEntry.expires > now))
207+
return retainedEntry.value;
205208
const cached = knownEntry(session.value, input.path, now);
206209
if (cached) {
207210
retained?.set(input.path, cached);
@@ -225,14 +228,29 @@ export function createFileMetadataAtoms<R, E>(
225228
input: FileMetadataInput;
226229
}) => revision(environmentId, input.path),
227230
});
231+
const refreshMissing = Atom.family((query: ReturnType<typeof metadataFamily>) => {
232+
const retry = query.pipe(Atom.withRefresh(METADATA_STALE_TIME_MS), Atom.setIdleTTL(0));
233+
return Atom.transform(
234+
query,
235+
(get) => {
236+
const result = get(query);
237+
return Option.exists(AsyncResult.value(result), (value) => value === null)
238+
? get(retry)
239+
: result;
240+
},
241+
{ initialValueTarget: query },
242+
).pipe(Atom.setIdleTTL(0));
243+
});
228244
const metadata = (target: {
229245
environmentId: EnvironmentId;
230246
input: { path: string; threadId?: ThreadId };
231247
}) =>
232-
metadataFamily({
233-
...target,
234-
input: { ...target.input, path: normalizeProjectPathForComparison(target.input.path) },
235-
});
248+
refreshMissing(
249+
metadataFamily({
250+
...target,
251+
input: { ...target.input, path: normalizeProjectPathForComparison(target.input.path) },
252+
}),
253+
);
236254

237255
const rememberEntries = Effect.fnUntraced(function* (
238256
cwd: string,

0 commit comments

Comments
 (0)