Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion packages/client-runtime/src/state/fileMetadata.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from "@effect/vitest";
import { describe, expect, it, vi } from "@effect/vitest";
import {
EnvironmentId,
ThreadId,
Expand All @@ -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";
Expand Down Expand Up @@ -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();
Expand Down
30 changes: 24 additions & 6 deletions packages/client-runtime/src/state/fileMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down
Loading