@devries-cameron20 / t3code-1 / commits / 08544ba3e5
Fork 0

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

fix(web): open documents no longer reload after every agent command (#32693)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Yash Singh <saiansh2525@gmail.com>

Julius Marminge committed Nov 13, 2026 at 00:01 UTC 08544ba3e59c7b5e718e2348e7bb5c44a7575fae
2 files changed +154 -19
apps/web/src/components/files/FilePreviewPanel.browser.test.tsx
+86 -2
@@ -17,6 +17,8 @@ const state = vi.hoisted(() => ({
17 file: null as ProjectReadFileResult | null,
18 optimistic: null as ProjectReadFileResult | null,
19 writeFile: vi.fn(),
20 + assetUrl: { _tag: "Loading" } as import("~/assets/assetUrls").AssetUrlState,
21 + mintUrl: vi.fn<() => Promise<string | null>>(),
22 }));
23
24 vi.mock(import("@t3tools/client-runtime/state/runtime"), async (original) => ({
@@ -58,6 +60,15 @@ vi.mock(import("~/hooks/useTheme"), async (original) => ({
60 vi.mock(import("~/hooks/useWorkspaceMutationRefresh"), () => ({
61 useWorkspaceMutationRefresh: vi.fn(),
62 }));
63 +vi.mock(import("~/assets/assetUrls"), async (original) => ({
64 + ...(await original()),
65 + useAssetUrlRefresh: () => state.mintUrl,
66 + useAssetUrlState: () => state.assetUrl,
67 +}));
68 +// The Word engine compiles a large WebAssembly bundle; these tests only need the URL it loads.
69 +vi.mock(import("./OfficeFilePreview"), () => ({
70 + OfficeFilePreview: ({ src }: { src: string }) => <div data-testid="office-preview">{src}</div>,
71 +}));
72 vi.mock(import("./FileBreadcrumbs"), () => ({ FileBreadcrumbs: () => <></> }));
73 vi.mock(import("./FileBrowserPanel"), () => ({
74 default: () => (
@@ -109,6 +120,9 @@ function setup() {
120 localStorage.setItem(keys[1]!, "true");
121 state.file = null;
122 state.optimistic = null;
123 + state.assetUrl = { _tag: "Loading" };
124 + let mints = 0;
125 + state.mintUrl.mockReset().mockImplementation(async () => `/assets/report.docx?mint=${++mints}`);
126 state.writeFile.mockReset().mockResolvedValue(AsyncResult.success(undefined));
127 return {
128 [Symbol.dispose]() {
@@ -131,7 +145,7 @@ function markdownPreview(truncated: boolean, contents = "- [ ] Keep the rest of
145 return filePreview("tasks.md");
146 }
147
134 -function filePreview(relativePath: string) {
148 +function filePreview(relativePath: string, workspaceMutationId: string | null = null) {
149 return (
150 <FilePreviewPanel
151 environmentId={environmentId}
@@ -150,11 +164,22 @@ function filePreview(relativePath: string) {
164 folderTrail={undefined}
165 onPendingChange={vi.fn()}
166 selectedFilePending={false}
153 - workspaceMutationId={null}
167 + workspaceMutationId={workspaceMutationId}
168 />
169 );
170 }
171
172 +function documentPreview(workspaceMutationId: string, fileRevision: string) {
173 + state.file = null;
174 + state.assetUrl = {
175 + _tag: "Success",
176 + url: "/assets/report.docx",
177 + expiresAt: Number.MAX_SAFE_INTEGER,
178 + fileRevision,
179 + };
180 + return filePreview("report.docx", workspaceMutationId);
181 +}
182 +
183 function deepActiveElement() {
184 let active = document.activeElement;
185 while (active?.shadowRoot?.activeElement) active = active.shadowRoot.activeElement;
@@ -279,6 +304,65 @@ describe("rendered Markdown task saves", () => {
304 });
305 });
306
307 +describe("workspace document previews", () => {
308 + it("keeps an unchanged document loaded when another workspace change lands", async () => {
309 + using _settings = setup();
310 + await using screen = await render(documentPreview("mutation-1", "revision-1"));
311 + const preview = screen.getByTestId("office-preview");
312 + await expect.element(preview).toHaveTextContent("/assets/report.docx?mint=1");
313 + const loaded = preview.element();
314 +
315 + await screen.rerender(documentPreview("mutation-2", "revision-1"));
316 +
317 + expect(state.mintUrl, "an unchanged document re-minted its URL").toHaveBeenCalledOnce();
318 + expect(screen.container.contains(loaded), "an unchanged document was remounted").toBe(true);
319 + });
320 +
321 + it("keeps an unchanged document mounted while its revision check reloads", async () => {
322 + using _settings = setup();
323 + await using screen = await render(documentPreview("mutation-1", "revision-1"));
324 + const preview = screen.getByTestId("office-preview");
325 + await expect.element(preview).toHaveTextContent("/assets/report.docx?mint=1");
326 + const loaded = preview.element();
327 +
328 + state.assetUrl = { _tag: "Loading" };
329 + await screen.rerender(filePreview("report.docx", "mutation-1"));
330 + await screen.rerender(documentPreview("mutation-1", "revision-1"));
331 +
332 + expect(
333 + state.mintUrl,
334 + "a reloading revision check re-minted the document URL",
335 + ).toHaveBeenCalledOnce();
336 + expect(
337 + screen.container.contains(loaded),
338 + "a reloading revision check remounted the document",
339 + ).toBe(true);
340 + });
341 +
342 + it("reloads the document when its URL can no longer be minted", async () => {
343 + using _settings = setup();
344 + await using screen = await render(documentPreview("mutation-1", "revision-1"));
345 + const preview = screen.getByTestId("office-preview");
346 + await expect.element(preview).toHaveTextContent("/assets/report.docx?mint=1");
347 +
348 + state.assetUrl = { _tag: "Failure" };
349 + await screen.rerender(filePreview("report.docx", "mutation-2"));
350 +
351 + await expect.element(preview).toHaveTextContent("/assets/report.docx?mint=2");
352 + });
353 +
354 + it("loads the new bytes when the document itself changes", async () => {
355 + using _settings = setup();
356 + await using screen = await render(documentPreview("mutation-1", "revision-1"));
357 + const preview = screen.getByTestId("office-preview");
358 + await expect.element(preview).toHaveTextContent("/assets/report.docx?mint=1");
359 +
360 + await screen.rerender(documentPreview("mutation-2", "revision-2"));
361 +
362 + await expect.element(preview).toHaveTextContent("/assets/report.docx?mint=2");
363 + });
364 +});
365 +
366 describe("rendered Markdown select all", () => {
367 it("selects only the rendered file, including after a task toggle", async () => {
368 using _settings = setup();
apps/web/src/components/files/FilePreviewPanel.tsx
+68 -17
@@ -398,6 +398,64 @@ function WorkspaceAudioPreview(props: {
398 );
399 }
400
401 +function WorkspaceDocumentPreview(props: {
402 + readonly environmentId: EnvironmentId;
403 + readonly threadRef: ScopedThreadRef;
404 + readonly draft: boolean;
405 + readonly document: boolean;
406 + readonly absolutePath: string;
407 + readonly workspaceRoot: string;
408 + readonly name: string;
409 + readonly workspaceMutationId: string | null;
410 + readonly refreshRevision: number;
411 +}) {
412 + const resource =
413 + props.draft && props.document
414 + ? {
415 + _tag: "draft-workspace-file" as const,
416 + cwd: props.workspaceRoot,
417 + path: props.absolutePath,
418 + }
419 + : {
420 + _tag: "media-file" as const,
421 + threadId: props.threadRef.threadId,
422 + path: props.absolutePath,
423 + ...(props.document ? {} : { disposition: "attachment" as const }),
424 + };
425 + // Drafts keep reloading on every workspace mutation, as before.
426 + const revisioned = resource._tag === "media-file";
427 + const assetUrl = useAssetUrlState(
428 + props.environmentId,
429 + revisioned ? resource : null,
430 + props.workspaceMutationId ?? undefined,
431 + );
432 + const resolved =
433 + assetUrl._tag === "Success" ? (assetUrl.fileRevision ?? props.workspaceMutationId) : undefined;
434 + // A reconnect reports Loading and keeps the last revision, so the open file stays loaded;
435 + // a failure (say the file was deleted) remounts so the preview shows its error.
436 + const [shown, setShown] = useState(resolved);
437 + if (resolved !== undefined && resolved !== shown) setShown(resolved);
438 + const revision = !revisioned
439 + ? props.workspaceMutationId
440 + : assetUrl._tag === "Failure"
441 + ? `failure:${props.workspaceMutationId}`
442 + : (resolved ?? shown);
443 + // Waiting on the first revision keeps the preview from mounting twice when it opens.
444 + if (revisioned && revision === undefined && assetUrl._tag === "Loading") {
445 + return <FileSurfaceLoading />;
446 + }
447 + return (
448 + <AttachmentFilePreview
449 + key={`${revision}:${props.refreshRevision}`}
450 + name={props.name}
451 + mimeType="application/octet-stream"
452 + sizeBytes={0}
453 + asset={{ environmentId: props.environmentId, resource }}
454 + origin="File"
455 + />
456 + );
457 +}
458 +
459 function clampFileLine(contents: string, requestedLine: number): number {
460 let lineCount = 1;
461 for (let index = 0; index < contents.length; index += 1) {
@@ -1538,24 +1596,17 @@ export default function FilePreviewPanel({
1596 ) : relativePath &&
1597 absolutePath &&
1598 (isDocument || file.readError?.failure === "binary_file") ? (
1541 - <AttachmentFilePreview
1542 - key={`${environmentId}:${absolutePath}:${workspaceMutationId}:${fileRefreshRevision}`}
1599 + <WorkspaceDocumentPreview
1600 + key={`${environmentId}:${absolutePath}`}
1601 + environmentId={environmentId}
1602 + threadRef={threadRef}
1603 + draft={draft}
1604 + document={isDocument}
1605 + absolutePath={absolutePath}
1606 + workspaceRoot={cwd}
1607 name={relativePath.split(/[\\/]/).at(-1) || relativePath}
1544 - mimeType="application/octet-stream"
1545 - sizeBytes={0}
1546 - asset={{
1547 - environmentId,
1548 - resource:
1549 - draft && isDocument
1550 - ? { _tag: "draft-workspace-file", cwd, path: absolutePath }
1551 - : {
1552 - _tag: "media-file",
1553 - threadId: threadRef.threadId,
1554 - path: absolutePath,
1555 - ...(isDocument ? {} : { disposition: "attachment" as const }),
1556 - },
1557 - }}
1558 - origin="File"
1608 + workspaceMutationId={workspaceMutationId}
1609 + refreshRevision={fileRefreshRevision}
1610 />
1611 ) : relativePath && file.error && file.data === null ? (
1612 <div