The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 04:30 UTC
fix(client-runtime): expire missing file metadata per thread (#30606)
Yash Singh committed
Nov 3, 2026 at 22:18 UTC
567a105705bc7c8e963387f69c484178e0d8ae55
2 files changed
+62
-7
packages/client-runtime/src/state/fileMetadata.test.ts
+38
-1
@@ -1,4 +1,4 @@
1
-import { describe, expect, it } from "@effect/vitest";
1
+import { describe, expect, it, vi } from "@effect/vitest";
2
import {
3
EnvironmentId,
4
ThreadId,
@@ -17,6 +17,7 @@ import * as Option from "effect/Option";
17
import * as Stream from "effect/Stream";
18
import * as Schema from "effect/Schema";
19
import * as SubscriptionRef from "effect/SubscriptionRef";
20
+import * as TestClock from "effect/testing/TestClock";
21
import { Atom, AtomRegistry } from "effect/reactivity";
22
23
import { AVAILABLE_CONNECTION_STATE } from "../connection/model.ts";
@@ -118,6 +119,42 @@ const makeHarness = Effect.fnUntraced(function* () {
119
});
120
121
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
+
158
it.effect("does not seed a different path from a literal name with a numeric suffix", () =>
159
Effect.gen(function* () {
160
const h = yield* makeHarness();
packages/client-runtime/src/state/fileMetadata.ts
+24
-6
@@ -11,10 +11,11 @@ import { normalizeProjectPathForComparison, resolveWorkspaceFilePath } from "@t3
11
import * as Clock from "effect/Clock";
12
import * as Effect from "effect/Effect";
13
import * as Exit from "effect/Exit";
14
+import * as Option from "effect/Option";
15
import * as Request from "effect/Request";
16
import * as RequestResolver from "effect/RequestResolver";
17
import * as SubscriptionRef from "effect/SubscriptionRef";
17
-import { Atom, AtomRegistry } from "effect/reactivity";
18
+import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
19
20
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
21
import type { EnvironmentRegistry } from "../connection/registry.ts";
@@ -200,8 +201,10 @@ export function createFileMetadataAtoms<R, E>(
201
thread.sessions.set(session.value, retained);
202
}
203
const retainedEntry = retained?.get(input.path);
203
- if (retainedEntry) return retainedEntry.value;
204
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;
208
const cached = knownEntry(session.value, input.path, now);
209
if (cached) {
210
retained?.set(input.path, cached);
@@ -225,14 +228,29 @@ export function createFileMetadataAtoms<R, E>(
228
input: FileMetadataInput;
229
}) => revision(environmentId, input.path),
230
});
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
+ });
244
const metadata = (target: {
245
environmentId: EnvironmentId;
246
input: { path: string; threadId?: ThreadId };
247
}) =>
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
+ );
254
255
const rememberEntries = Effect.fnUntraced(function* (
256
cwd: string,