diff --git a/src/client/FileTree.tsx b/src/client/FileTree.tsx index 281d9542..b429ab81 100644 --- a/src/client/FileTree.tsx +++ b/src/client/FileTree.tsx @@ -1480,6 +1480,13 @@ export function FileTree(props: { const [archiveBusy, setArchiveBusy] = useState(false) const [archiveProgress, setArchiveProgress] = useState<{ done: number; total: number } | null>(null) const archiveBusyRef = useRef(false) + /** + * Bumped by a session/cwd swap. A build still starting up when the reader + * leaves must not pump its job back in: the swap already settled the archive, + * and the resurrected id would restart the poller in the new project — asking + * with the wrong scope, then reporting a zip failure nobody asked for. + */ + const archiveGenRef = useRef(0) /** The job being polled (state so the poller starts/stops with it). */ const [archiveJobId, setArchiveJobId] = useState(null) const archiveJobRef = useRef<{ id: string; name: string } | null>(null) @@ -1499,11 +1506,13 @@ export function FileTree(props: { setArchiveProgress(null) }, []) - // A session/cwd swap abandons the archive job with it: the poller would keep - // asking with the NEW scope for the PREVIOUS session's job (the host refuses - // that), and the strip would report a zip failure in a project that never - // asked for one. + // A session/cwd swap abandons the archive job with it: the progress strip + // belongs to the project that started the zip, and a leftover `archiveJobId` + // would keep the poller running — whose failures land as `zipFailed` in a + // project that never asked for one. The generation bump retires a build that + // is still starting up, so it cannot pump its job back in after this. useEffect(() => { + archiveGenRef.current += 1 settleArchive() }, [sessionId, cwd, settleArchive]) @@ -1583,17 +1592,20 @@ export function FileTree(props: { const downloadArchive = (paths: readonly string[]): void => { if (archiveBusyRef.current) return const name = paths.length === 1 ? `${baseName(paths[0]!)}.zip` : 'archive.zip' + const generation = archiveGenRef.current archiveBusyRef.current = true archiveHandedOffRef.current = false setArchiveBusy(true) setArchiveProgress(null) void archiveBuild({ sessionId, cwd }, paths, name) .then(({ id, entries }) => { + if (generation !== archiveGenRef.current) return archiveJobRef.current = { id, name } setArchiveProgress({ done: 0, total: entries }) setArchiveJobId(id) }) .catch((error: unknown) => { + if (generation !== archiveGenRef.current) return failArchive(error instanceof Error ? error.message : String(error)) }) } diff --git a/tests/file-tree-archive.spec.tsx b/tests/file-tree-archive.spec.tsx index 64af27c4..dbbbade0 100644 --- a/tests/file-tree-archive.spec.tsx +++ b/tests/file-tree-archive.spec.tsx @@ -81,6 +81,8 @@ const POLL_MS = 250 interface Harness { container: HTMLDivElement + /** Render — or RE-render in place, as a session swap does. */ + show: (sessionId: string, cwd: string) => Promise unmount: () => void } @@ -88,23 +90,27 @@ async function mountTree(): Promise { const container = document.createElement('div') document.body.append(container) const root: Root = createRoot(container) - await act(async () => { - root.render(createElement(FileTree, { - sessionId: 's1', - cwd: '/tmp', - expanded: [], - revealed: [], - onToggle: () => {}, - onOpenFile: () => {}, - onReferenceFile: () => {}, - refreshTick: 0, - onUploadRequest: () => {}, - busy: false, - })) - await Promise.resolve() - }) + const show = async (sessionId: string, cwd: string): Promise => { + await act(async () => { + root.render(createElement(FileTree, { + sessionId, + cwd, + expanded: [], + revealed: [], + onToggle: () => {}, + onOpenFile: () => {}, + onReferenceFile: () => {}, + refreshTick: 0, + onUploadRequest: () => {}, + busy: false, + })) + await Promise.resolve() + }) + } + await show('s1', '/tmp') return { container, + show, unmount: () => { act(() => { root.unmount() }); container.remove() }, } } @@ -422,4 +428,31 @@ describe('FileTree zip and download', () => { await poll() await flush() }) + + it('does not resurrect a build that was still starting when the scope changed', async () => { + // The swap settles the archive. A build answering only afterwards used to + // pump its job id back in, restarting the poller in a project that never + // asked for a zip — and reporting the failure there. + let resolveBuild!: (value: { id: string; entries: number }) => void + archiveBuild.mockImplementationOnce(() => new Promise((resolve) => { resolveBuild = resolve })) + + harness = await mountTree() + click(rowByName(harness.container, 'a.ts'), { ctrlKey: true }) + click(rowByName(harness.container, 'b.ts'), { ctrlKey: true }) + rightClick(rowByName(harness.container, 'b.ts')) + clickMenuitem('Zip and download (2 items)') + expect(archiveBuild).toHaveBeenCalledTimes(1) + + // Another session takes over the same mounted instance. + await harness.show('s2', '/tmp/beta') + await flush() + + // The build answers only now. + await act(async () => { resolveBuild({ id: 'job-stale', entries: 2 }) }) + await flush() + await poll() + + expect(archiveStatus).not.toHaveBeenCalled() + expect(progressLine(harness.container)).toBeNull() + }) })