The best way to control Claude Code, Codex and more from any computer or phone

main
ts 367 lines 13.4 KB
Raw
1 import {
2 FILESYSTEM_METADATA_BATCH_LIMIT,
3 type EnvironmentId,
4 type FilesystemEntryMetadata,
5 type ProjectEntry,
6 type ScopedThreadRef,
7 type ThreadId,
8 WS_METHODS,
9 } from "@t3tools/contracts";
10 import { normalizeProjectPathForComparison, resolveWorkspaceFilePath } from "@t3tools/shared/path";
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";
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";
22 import {
23 request,
24 type EnvironmentRpcFailure,
25 EnvironmentRpcUnavailableError,
26 } from "../rpc/client.ts";
27 import type { RpcSession } from "../rpc/session.ts";
28 import { createEnvironmentQueryAtomFamily } from "./runtime.ts";
29
30 const METADATA_STALE_TIME_MS = 60_000;
31 const METADATA_CACHE_LIMIT = 1024;
32
33 interface CachedMetadata {
34 value: FilesystemEntryMetadata | null;
35 expires: number;
36 }
37
38 type RetainedMetadata = Map<string, CachedMetadata>;
39
40 interface FileMetadataInput {
41 path: string;
42 threadId?: ThreadId;
43 }
44
45 class FileMetadataRequest extends Request.Class<
46 {
47 path: string;
48 session: RpcSession;
49 supervisor: EnvironmentSupervisor.EnvironmentSupervisor["Service"];
50 retained?: RetainedMetadata;
51 },
52 FilesystemEntryMetadata | null,
53 EnvironmentRpcFailure<typeof WS_METHODS.filesystemGetMetadata> | EnvironmentRpcUnavailableError
54 > {}
55
56 export function createFileMetadataAtoms<R, E>(
57 runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
58 ) {
59 const cache = new WeakMap<RpcSession, Map<string, CachedMetadata>>();
60 const invalidationVersions = new WeakMap<RpcSession, number>();
61 const inFlight = new WeakMap<RpcSession, Map<string, Set<{ valid: boolean }>>>();
62 const retainedThreads = new Map<
63 string,
64 { readers: number; sessions: WeakMap<RpcSession, RetainedMetadata> }
65 >();
66 const threadKey = (ref: ScopedThreadRef) => JSON.stringify([ref.environmentId, ref.threadId]);
67 const retainThreadFamily = Atom.family((key: string) =>
68 Atom.make((get) => {
69 let retained = retainedThreads.get(key);
70 if (!retained) {
71 retained = { readers: 0, sessions: new WeakMap() };
72 retainedThreads.set(key, retained);
73 }
74 retained.readers++;
75 get.addFinalizer(() => {
76 if (--retained.readers === 0) retainedThreads.delete(key);
77 });
78 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`file-metadata-thread:${key}`)),
79 );
80 const revisions = Atom.family((key: string) =>
81 Atom.make(0).pipe(Atom.withLabel(`file-metadata-revision:${key}`)),
82 );
83 const revision = (environmentId: EnvironmentId, path: string) =>
84 revisions(JSON.stringify([environmentId, normalizeProjectPathForComparison(path)]));
85 const knownEntry = (session: RpcSession, path: string, now: number) => {
86 const key = normalizeProjectPathForComparison(path);
87 const cached = cache.get(session)?.get(key);
88 return cached && cached.expires > now ? cached : undefined;
89 };
90 const remember = (
91 session: RpcSession,
92 path: string,
93 value: FilesystemEntryMetadata | null,
94 now: number,
95 partial = false,
96 ) => {
97 let entries = cache.get(session);
98 if (!entries) {
99 entries = new Map();
100 cache.set(session, entries);
101 }
102 const key = normalizeProjectPathForComparison(path);
103 entries.delete(key);
104 const entry = { value, expires: now + METADATA_STALE_TIME_MS };
105 entries.set(key, entry);
106 for (const thread of retainedThreads.values()) {
107 const retained = thread.sessions.get(session);
108 const previous = retained?.get(key);
109 if (retained && previous)
110 retained.set(key, {
111 ...entry,
112 value:
113 partial && value && previous.value?.kind === value.kind
114 ? { ...previous.value, ...value }
115 : value,
116 });
117 }
118 if (entries.size > METADATA_CACHE_LIMIT) {
119 const oldest = entries.keys().next().value;
120 if (oldest !== undefined) entries.delete(oldest);
121 }
122 };
123 const resolver = RequestResolver.makeWith<FileMetadataRequest>({
124 batchKey: (entry) => entry.request.session,
125 delay: Effect.yieldNow,
126 collectWhile: (entries) => entries.size < FILESYSTEM_METADATA_BATCH_LIMIT,
127 runAll: Effect.fnUntraced(function* (entries) {
128 const session = entries[0].request.session;
129 let pending = [...entries];
130 while (pending.length > 0) {
131 const paths = [...new Set(pending.map((entry) => entry.request.path))];
132 let requests = inFlight.get(session);
133 if (!requests) {
134 requests = new Map();
135 inFlight.set(session, requests);
136 }
137 const tokens = new Map(paths.map((path) => [path, { valid: true }]));
138 for (const [path, token] of tokens) {
139 let active = requests.get(path);
140 if (!active) requests.set(path, (active = new Set()));
141 active.add(token);
142 }
143 const activeRequests = requests;
144 pending = yield* Effect.gen(function* () {
145 const result = yield* request(WS_METHODS.filesystemGetMetadata, { paths }).pipe(
146 Effect.provideService(
147 EnvironmentSupervisor.EnvironmentSupervisor,
148 entries[0].request.supervisor,
149 ),
150 );
151 const now = yield* Clock.currentTimeMillis;
152 const byPath = new Map(paths.map((path, index) => [path, result.entries[index] ?? null]));
153 const retry: typeof pending = [];
154 for (const entry of pending) {
155 // Retry only paths invalidated during this request. Never retain the
156 // old response or complete a waiting chip with pre-write metadata.
157 if (!tokens.get(entry.request.path)!.valid) {
158 retry.push(entry);
159 continue;
160 }
161 const value = byPath.get(entry.request.path) ?? null;
162 remember(session, entry.request.path, value, now);
163 entry.request.retained?.set(entry.request.path, {
164 value,
165 expires: now + METADATA_STALE_TIME_MS,
166 });
167 entry.completeUnsafe(Exit.succeed(value));
168 }
169 return retry;
170 }).pipe(
171 Effect.ensuring(
172 Effect.sync(() => {
173 for (const [path, token] of tokens) {
174 const active = activeRequests.get(path);
175 active?.delete(token);
176 if (active?.size === 0) activeRequests.delete(path);
177 }
178 }),
179 ),
180 );
181 }
182 }),
183 });
184
185 const metadataFamily = createEnvironmentQueryAtomFamily(runtime, {
186 label: "environment-data:filesystem:metadata",
187 staleTimeMs: METADATA_STALE_TIME_MS,
188 idleTtlMs: METADATA_STALE_TIME_MS,
189 execute: Effect.fnUntraced(function* (input: FileMetadataInput) {
190 const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
191 const session = yield* SubscriptionRef.get(supervisor.session);
192 if (session._tag === "None") return null;
193 const thread = input.threadId
194 ? retainedThreads.get(
195 threadKey({ environmentId: supervisor.target.environmentId, threadId: input.threadId }),
196 )
197 : undefined;
198 let retained = thread?.sessions.get(session.value);
199 if (thread && !retained) {
200 retained = new Map();
201 thread.sessions.set(session.value, retained);
202 }
203 const retainedEntry = retained?.get(input.path);
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);
211 return cached.value;
212 }
213 return yield* Effect.request(
214 new FileMetadataRequest({
215 path: input.path,
216 session: session.value,
217 supervisor,
218 ...(retained ? { retained } : {}),
219 }),
220 resolver,
221 );
222 }),
223 refreshTrigger: ({
224 environmentId,
225 input,
226 }: {
227 environmentId: EnvironmentId;
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 }) =>
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,
257 entries: ReadonlyArray<ProjectEntry>,
258 isCurrent?: (session: RpcSession) => boolean,
259 ) {
260 const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
261 const session = yield* SubscriptionRef.get(supervisor.session);
262 if (session._tag === "None") return;
263 const now = yield* Clock.currentTimeMillis;
264 if (isCurrent && !isCurrent(session.value)) return;
265 for (const entry of entries) {
266 const path = resolveWorkspaceFilePath(entry.path, cwd);
267 if (entry.kind === "directory") {
268 if (knownEntry(session.value, path, now)?.value?.kind !== "directory")
269 remember(session.value, path, { kind: "directory" }, now, true);
270 continue;
271 }
272
273 // A file listing can invalidate the cached kind but only a stat supplies full metadata.
274 const key = normalizeProjectPathForComparison(path);
275 const cached = cache.get(session.value);
276 if (cached?.get(key)?.value?.kind === "file") continue;
277 cached?.delete(key);
278 for (const thread of retainedThreads.values()) {
279 const retained = thread.sessions.get(session.value);
280 const previous = retained?.get(key);
281 if (previous && previous.value?.kind !== "file") retained?.delete(key);
282 }
283 }
284 });
285
286 const invalidate = Effect.fnUntraced(function* (path: string) {
287 const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
288 const session = yield* SubscriptionRef.get(supervisor.session);
289 if (session._tag === "Some") {
290 invalidationVersions.set(session.value, (invalidationVersions.get(session.value) ?? 0) + 1);
291 const key = normalizeProjectPathForComparison(path);
292 for (const token of inFlight.get(session.value)?.get(key) ?? []) token.valid = false;
293 cache.get(session.value)?.delete(key);
294 for (const thread of retainedThreads.values())
295 thread.sessions.get(session.value)?.delete(normalizeProjectPathForComparison(path));
296 }
297 });
298
299 const rememberFile = Effect.fnUntraced(function* (
300 cwd: string,
301 file: { relativePath: string; byteLength: number },
302 isCurrent?: (session: RpcSession) => boolean,
303 ) {
304 const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
305 const session = yield* SubscriptionRef.get(supervisor.session);
306 if (session._tag === "None") return;
307 const path = resolveWorkspaceFilePath(file.relativePath, cwd);
308 const now = yield* Clock.currentTimeMillis;
309 if (isCurrent && !isCurrent(session.value)) return;
310 const previous = knownEntry(session.value, path, now)?.value;
311 remember(
312 session.value,
313 path,
314 {
315 ...previous,
316 kind: "file",
317 byteLength: file.byteLength,
318 },
319 now,
320 true,
321 );
322 });
323
324 const seedAfterRead =
325 /* Metadata seeding runs on every file read and is covered by the read operation. */
326 Effect.fnUntraced(function* <A, ReadError, ReadServices>(
327 read: Effect.Effect<A, ReadError, ReadServices>,
328 seed: (
329 result: A,
330 isCurrent: (session: RpcSession) => boolean,
331 ) => Effect.Effect<void, never, EnvironmentSupervisor.EnvironmentSupervisor>,
332 ) {
333 const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
334 const session = yield* SubscriptionRef.get(supervisor.session);
335 const version = session._tag === "Some" ? (invalidationVersions.get(session.value) ?? 0) : 0;
336 const result = yield* read;
337 // Seeding is optional. Any intervening invalidation or reconnect makes
338 // the old response unsuitable for the current metadata cache.
339 yield* seed(
340 result,
341 (current) =>
342 session._tag === "Some" &&
343 current === session.value &&
344 (invalidationVersions.get(current) ?? 0) === version,
345 );
346 return result;
347 });
348
349 const refreshPath = (
350 environmentId: EnvironmentId,
351 path: string,
352 registry: AtomRegistry.AtomRegistry,
353 ) => {
354 const signal = revision(environmentId, path);
355 registry.set(signal, registry.get(signal) + 1);
356 };
357
358 return {
359 metadata,
360 retainThread: (ref: ScopedThreadRef) => retainThreadFamily(threadKey(ref)),
361 rememberEntries,
362 rememberFile,
363 seedAfterRead,
364 invalidate,
365 refreshPath,
366 };
367 }