diff --git a/packages/client-runtime/src/state/fileMetadata.test.ts b/packages/client-runtime/src/state/fileMetadata.test.ts index daf50845e2c05..c662fa26ee429 100644 --- a/packages/client-runtime/src/state/fileMetadata.test.ts +++ b/packages/client-runtime/src/state/fileMetadata.test.ts @@ -346,15 +346,54 @@ describe("file metadata", () => { }), ); - it.effect("uses search and tree kinds without another request", () => + it.effect("uses search and tree kinds for directories but still stats found files", () => Effect.gen(function* () { const h = yield* makeHarness(); yield* h.seed("folder.ts", "directory"); expect(yield* h.read("/workspace/folder.ts")).toEqual({ kind: "directory" }); expect(h.batches).toHaveLength(0); + h.values.set("/workspace/notes.md", { kind: "file", byteLength: 2048 }); + yield* h.seed("notes.md", "file"); + expect(yield* h.read("/workspace/notes.md")).toEqual({ kind: "file", byteLength: 2048 }); }), ); + it.effect( + "stats listed files that replace directories or misses and reuses known file sizes", + () => + Effect.gen(function* () { + const h = yield* makeHarness(); + const threadId = ThreadId.make("listed-file-thread"); + h.registry.mount(h.atoms.retainThread({ environmentId: ENVIRONMENT_ID, threadId })); + const directory = "/workspace/was-directory"; + const missing = "/workspace/was-missing"; + const file = "/workspace/known-file"; + yield* h.seed("was-directory", "directory"); + expect(yield* h.read(missing, ENVIRONMENT_ID, threadId)).toBeNull(); + h.values.set(file, { kind: "file", byteLength: 100 }); + yield* h.read(file, ENVIRONMENT_ID, threadId); + + h.values.set(directory, { kind: "file", byteLength: 2048 }); + h.values.set(missing, { kind: "file", byteLength: 4096 }); + yield* h.seed("was-directory", "file"); + yield* h.seed("was-missing", "file"); + yield* h.seed("known-file", "file"); + + expect(yield* h.read(directory)).toEqual({ kind: "file", byteLength: 2048 }); + h.registry.refresh(h.atom(missing, ENVIRONMENT_ID, threadId)); + expect(yield* h.read(missing, ENVIRONMENT_ID, threadId)).toEqual({ + kind: "file", + byteLength: 4096, + }); + h.registry.refresh(h.atom(file, ENVIRONMENT_ID, threadId)); + expect(yield* h.read(file, ENVIRONMENT_ID, threadId)).toEqual({ + kind: "file", + byteLength: 100, + }); + expect(h.batches.flat()).toEqual([missing, file, directory, missing]); + }), + ); + it.effect("splits large mounts into bounded batches and caches misses", () => 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 5d20d3188909a..88bc3f6fc0a2d 100644 --- a/packages/client-runtime/src/state/fileMetadata.ts +++ b/packages/client-runtime/src/state/fileMetadata.ts @@ -264,9 +264,22 @@ export function createFileMetadataAtoms( if (isCurrent && !isCurrent(session.value)) return; for (const entry of entries) { const path = resolveWorkspaceFilePath(entry.path, cwd); - const previous = knownEntry(session.value, path, now); - if (previous?.value?.kind === entry.kind) continue; - remember(session.value, path, { kind: entry.kind }, now, true); + if (entry.kind === "directory") { + if (knownEntry(session.value, path, now)?.value?.kind !== "directory") + remember(session.value, path, { kind: "directory" }, now, true); + continue; + } + + // A file listing can invalidate the cached kind but only a stat supplies full metadata. + const key = normalizeProjectPathForComparison(path); + const cached = cache.get(session.value); + if (cached?.get(key)?.value?.kind === "file") continue; + cached?.delete(key); + for (const thread of retainedThreads.values()) { + const retained = thread.sessions.get(session.value); + const previous = retained?.get(key); + if (previous && previous.value?.kind !== "file") retained?.delete(key); + } } }); diff --git a/scripts/scenarios/cited-file-tooltip-keeps-size-after-search.e2e.ts b/scripts/scenarios/cited-file-tooltip-keeps-size-after-search.e2e.ts new file mode 100644 index 0000000000000..bff8319ab1d84 --- /dev/null +++ b/scripts/scenarios/cited-file-tooltip-keeps-size-after-search.e2e.ts @@ -0,0 +1,33 @@ +import { expect } from "e2e"; + +import { test, WEB } from "./t3.ts"; + +const FILE = "quarterly-figures.md"; + +test( + "The file citation tooltip shows the size even after workspace search finds the file", + WEB, + async ({ t3, app, browser, screen }) => { + const project = await t3.project("demo", { [FILE]: "x".repeat(2048) }); + const thread = await t3.thread({ + project, + title: "Review figures", + messages: [`See :codex-file-citation{path="${FILE}" line_range_start="1"}`], + }); + await t3.open(thread); + + await screen.getByRole("textbox").pressSequentially(`@${FILE}`); + await screen.getByRole("option", FILE, { exact: false }).hover(); + await browser.keyboard.press("Escape"); + await screen.getByRole("button", /^Agent See/).tap(); + await screen.getByRole("link", /^quarterly-figures\.md/).hover(); + + try { + await expect( + browser.locator('[data-slot="tooltip-viewport"]:has-text("quarterly-figures.md")'), + ).toContainText("2 KB"); + } finally { + await app.screenshot("cited-file-tooltip"); + } + }, +);