Skip to content
TypeScript

fix(client-runtime): expire missing file metadata per thread (#30606)

Yash Singh567a105705bc
diff --git a/packages/client-runtime/src/state/fileMetadata.test.ts b/packages/client-runtime/src/state/fileMetadata.test.ts
index c9536650e3a..7df58b04a2e 100644
--- a/packages/client-runtime/src/state/fileMetadata.test.ts
+++ b/packages/client-runtime/src/state/fileMetadata.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it } from "@effect/vitest";
+import { describe, expect, it, vi } from "@effect/vitest";
import {
EnvironmentId,
ThreadId,
@@ -17,6 +17,7 @@ import * as Option from "effect/Option";
import * as Stream from "effect/Stream";
import * as Schema from "effect/Schema";
import * as SubscriptionRef from "effect/SubscriptionRef";
+import * as TestClock from "effect/testing/TestClock";
import { Atom, AtomRegistry } from "effect/reactivity";
import { AVAILABLE_CONNECTION_STATE } from "../connection/model.ts";
@@ -118,6 +119,42 @@ const makeHarness = Effect.fnUntraced(function* () {
});
describe("file metadata", () => {
+ it.effect("refreshes expired mounted misses while retaining known thread entries", () =>
+ Effect.gen(function* () {
+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
+ yield* Effect.addFinalizer(() => Effect.sync(() => vi.useRealTimers()));
+ const h = yield* makeHarness();
+ const threadId = ThreadId.make("new-directory-thread");
+ h.registry.mount(h.atoms.retainThread({ environmentId: ENVIRONMENT_ID, threadId }));
+ const path = "/home/user/games/hl1";
+ h.registry.mount(h.atom(path, ENVIRONMENT_ID, threadId));
+ h.values.set("/workspace/known", { kind: "file", byteLength: 5 });
+ expect(yield* h.read(path, ENVIRONMENT_ID, threadId)).toBeNull();
+ yield* h.read("/workspace/known", ENVIRONMENT_ID, threadId);
+ h.values.set(path, { kind: "directory" });
+ h.registry.refresh(h.atom(path, ENVIRONMENT_ID, threadId));
+ expect(yield* h.read(path, ENVIRONMENT_ID, threadId)).toBeNull();
+ h.failure.denied = true;
+ yield* TestClock.adjust(60_001);
+ yield* Effect.promise(() => vi.advanceTimersByTimeAsync(60_000));
+ expect(yield* h.read(path, ENVIRONMENT_ID, threadId).pipe(Effect.flip)).toBeInstanceOf(
+ EnvironmentAuthorizationError,
+ );
+ h.failure.denied = false;
+ yield* TestClock.adjust(60_001);
+ yield* Effect.promise(() => vi.advanceTimersByTimeAsync(60_000));
+ expect(yield* h.read(path, ENVIRONMENT_ID, threadId)).toEqual({ kind: "directory" });
+ h.registry.refresh(h.atom("/workspace/known", ENVIRONMENT_ID, threadId));
+ expect(yield* h.read("/workspace/known", ENVIRONMENT_ID, threadId)).toEqual({
+ kind: "file",
+ byteLength: 5,
+ });
+ yield* TestClock.adjust(60_001);
+ yield* Effect.promise(() => vi.advanceTimersByTimeAsync(60_000));
+ expect(h.batches).toEqual([[path], ["/workspace/known"], [path], [path]]);
+ }).pipe(Effect.scoped),
+ );
+
it.effect("does not seed a different path from a literal name with a numeric suffix", () =>
Effect.gen(function* () {
const h = yield* makeHarness();
diff --git a/packages/client-runtime/src/state/fileMetadata.ts b/packages/client-runtime/src/state/fileMetadata.ts
index d079f08c00b..5d20d318890 100644
--- a/packages/client-runtime/src/state/fileMetadata.ts
+++ b/packages/client-runtime/src/state/fileMetadata.ts
@@ -11,10 +11,11 @@ import { normalizeProjectPathForComparison, resolveWorkspaceFilePath } from "@t3
import * as Clock from "effect/Clock";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
+import * as Option from "effect/Option";
import * as Request from "effect/Request";
import * as RequestResolver from "effect/RequestResolver";
import * as SubscriptionRef from "effect/SubscriptionRef";
-import { Atom, AtomRegistry } from "effect/reactivity";
+import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
import type { EnvironmentRegistry } from "../connection/registry.ts";
@@ -200,8 +201,10 @@ export function createFileMetadataAtoms<R, E>(
thread.sessions.set(session.value, retained);
}
const retainedEntry = retained?.get(input.path);
- if (retainedEntry) return retainedEntry.value;
const now = yield* Clock.currentTimeMillis;
+ // Agents mention paths before creating them, so misses must still expire within a thread.
+ if (retainedEntry && (retainedEntry.value !== null || retainedEntry.expires > now))
+ return retainedEntry.value;
const cached = knownEntry(session.value, input.path, now);
if (cached) {
retained?.set(input.path, cached);
@@ -225,14 +228,29 @@ export function createFileMetadataAtoms<R, E>(
input: FileMetadataInput;
}) => revision(environmentId, input.path),
});
+ const refreshMissing = Atom.family((query: ReturnType<typeof metadataFamily>) => {
+ const retry = query.pipe(Atom.withRefresh(METADATA_STALE_TIME_MS), Atom.setIdleTTL(0));
+ return Atom.transform(
+ query,
+ (get) => {
+ const result = get(query);
+ return Option.exists(AsyncResult.value(result), (value) => value === null)
+ ? get(retry)
+ : result;
+ },
+ { initialValueTarget: query },
+ ).pipe(Atom.setIdleTTL(0));
+ });
const metadata = (target: {
environmentId: EnvironmentId;
input: { path: string; threadId?: ThreadId };
}) =>
- metadataFamily({
- ...target,
- input: { ...target.input, path: normalizeProjectPathForComparison(target.input.path) },
- });
+ refreshMissing(
+ metadataFamily({
+ ...target,
+ input: { ...target.input, path: normalizeProjectPathForComparison(target.input.path) },
+ }),
+ );
const rememberEntries = Effect.fnUntraced(function* (
cwd: string,