From 0667687ac5917a3038fad9c1e897e5a3bf14ed8b Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Tue, 3 Nov 2026 21:50:50 -0600 Subject: [PATCH 1/3] fix(client-runtime): expire missing file metadata per thread - Recheck missing paths after their cache entry expires - Keep known thread metadata available --- .../src/state/fileMetadata.test.ts | 25 +++++++++++++++++++ .../client-runtime/src/state/fileMetadata.ts | 4 ++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/client-runtime/src/state/fileMetadata.test.ts b/packages/client-runtime/src/state/fileMetadata.test.ts index c9536650e3a89..fd95d5f4486a2 100644 --- a/packages/client-runtime/src/state/fileMetadata.test.ts +++ b/packages/client-runtime/src/state/fileMetadata.test.ts @@ -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,30 @@ const makeHarness = Effect.fnUntraced(function* () { }); describe("file metadata", () => { + it.effect("rechecks expired missing paths while retaining known thread entries", () => + Effect.gen(function* () { + 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.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(); + yield* TestClock.adjust(60_001); + h.registry.refresh(h.atom(path, ENVIRONMENT_ID, threadId)); + 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, + }); + expect(h.batches).toEqual([[path], ["/workspace/known"], [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 d079f08c00bdf..e030c4c8c6f4c 100644 --- a/packages/client-runtime/src/state/fileMetadata.ts +++ b/packages/client-runtime/src/state/fileMetadata.ts @@ -200,8 +200,10 @@ export function createFileMetadataAtoms( 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); From 00576c4d9f585aa9fce8e485ee37f2cb0818087e Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Tue, 3 Nov 2026 21:59:19 -0600 Subject: [PATCH 2/3] fix(client-runtime): retry mounted missing metadata --- .../src/state/fileMetadata.test.ts | 11 ++++++--- .../client-runtime/src/state/fileMetadata.ts | 23 +++++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/packages/client-runtime/src/state/fileMetadata.test.ts b/packages/client-runtime/src/state/fileMetadata.test.ts index fd95d5f4486a2..564afd4443b74 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, @@ -119,12 +119,15 @@ const makeHarness = Effect.fnUntraced(function* () { }); describe("file metadata", () => { - it.effect("rechecks expired missing paths while retaining known thread entries", () => + 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); @@ -132,13 +135,15 @@ describe("file metadata", () => { h.registry.refresh(h.atom(path, ENVIRONMENT_ID, threadId)); expect(yield* h.read(path, ENVIRONMENT_ID, threadId)).toBeNull(); yield* TestClock.adjust(60_001); - h.registry.refresh(h.atom(path, ENVIRONMENT_ID, threadId)); + 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]]); }).pipe(Effect.scoped), ); diff --git a/packages/client-runtime/src/state/fileMetadata.ts b/packages/client-runtime/src/state/fileMetadata.ts index e030c4c8c6f4c..52a167db0785d 100644 --- a/packages/client-runtime/src/state/fileMetadata.ts +++ b/packages/client-runtime/src/state/fileMetadata.ts @@ -14,7 +14,7 @@ import * as Exit from "effect/Exit"; 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"; @@ -227,14 +227,27 @@ export function createFileMetadataAtoms( input: FileMetadataInput; }) => revision(environmentId, input.path), }); + const refreshMissing = Atom.family((query: ReturnType) => { + const retry = query.pipe(Atom.withRefresh(METADATA_STALE_TIME_MS), Atom.setIdleTTL(0)); + return Atom.transform( + query, + (get) => { + const result = get(query); + return AsyncResult.isSuccess(result) && result.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, From abc71d0e747403997694ff0dd0d54f549acfe66e Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Tue, 3 Nov 2026 22:08:35 -0600 Subject: [PATCH 3/3] fix(client-runtime): retry missing metadata after refresh errors --- packages/client-runtime/src/state/fileMetadata.test.ts | 9 ++++++++- packages/client-runtime/src/state/fileMetadata.ts | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/client-runtime/src/state/fileMetadata.test.ts b/packages/client-runtime/src/state/fileMetadata.test.ts index 564afd4443b74..7df58b04a2e6b 100644 --- a/packages/client-runtime/src/state/fileMetadata.test.ts +++ b/packages/client-runtime/src/state/fileMetadata.test.ts @@ -134,6 +134,13 @@ describe("file metadata", () => { 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" }); @@ -144,7 +151,7 @@ describe("file metadata", () => { }); yield* TestClock.adjust(60_001); yield* Effect.promise(() => vi.advanceTimersByTimeAsync(60_000)); - expect(h.batches).toEqual([[path], ["/workspace/known"], [path]]); + expect(h.batches).toEqual([[path], ["/workspace/known"], [path], [path]]); }).pipe(Effect.scoped), ); diff --git a/packages/client-runtime/src/state/fileMetadata.ts b/packages/client-runtime/src/state/fileMetadata.ts index 52a167db0785d..5d20d3188909a 100644 --- a/packages/client-runtime/src/state/fileMetadata.ts +++ b/packages/client-runtime/src/state/fileMetadata.ts @@ -11,6 +11,7 @@ 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"; @@ -233,7 +234,9 @@ export function createFileMetadataAtoms( query, (get) => { const result = get(query); - return AsyncResult.isSuccess(result) && result.value === null ? get(retry) : result; + return Option.exists(AsyncResult.value(result), (value) => value === null) + ? get(retry) + : result; }, { initialValueTarget: query }, ).pipe(Atom.setIdleTTL(0));