From 58c4e7b49bb79398304524699fe8066f30f02ebf Mon Sep 17 00:00:00 2001 From: unsc9 <438590997@qq.com> Date: Mon, 5 Oct 2026 10:49:24 +0800 Subject: [PATCH] =?UTF-8?q?fix(files):=20=E6=8D=A2=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E6=AD=A3=E5=9C=A8=E8=B5=B7=E6=AD=A5=E7=9A=84?= =?UTF-8?q?=E6=89=93=E5=8C=85=E4=BB=BB=E5=8A=A1=E4=B8=8D=E8=AE=B8=E6=8A=8A?= =?UTF-8?q?=20job=20=E5=A1=9E=E5=9B=9E=E6=9D=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 切换会话会 settleArchive() 清掉打包状态,但 archiveBuild() 的回调没有守卫:网络答 回来时无条件写 archiveJobRef / setArchiveProgress / setArchiveJobId,把刚清掉的状态 原样盖回去。archiveJobId 一非空,usePolling 就在新会话里重新武装,去轮询一个用户 根本没发起的打包;轮询失败则在新项目弹出「打包失败」。 时序上这是常见结果而非窄窗口:effect 在 commit 时同步执行,回调要等网络。 加 archiveGenRef,切换时递增(与批删、读目录同一套做法);回调过期就直接丢弃 —— 失败 的构建也不再写错误条,它属于已经离开的那个项目。 顺带改掉那条注释里不成立的理由:原文说「轮询会拿新 scope 去问旧任务」,而轮询根本不 发送 scope(那是另一个既有问题,本次不动)。换成真正的理由:进度条属于发起它的项目, 遗留的 archiveJobId 会让轮询继续跑并把失败报在新项目上。 守护用例:file-tree-archive 新增一条,把 build 挂在延迟 promise 上,中途换会话再放行 —— 去掉修复后实测 archiveStatus 会被调用。mountTree 顺势扩出 show(sessionId, cwd) 以支持 就地换会话,现有用例不受影响。 --- src/client/FileTree.tsx | 20 ++++++++-- tests/file-tree-archive.spec.tsx | 63 ++++++++++++++++++++++++-------- 2 files changed, 64 insertions(+), 19 deletions(-) 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() + }) })