fix(files): 换会话时,正在起步的打包任务不许把 job 塞回来

切换会话会 settleArchive() 清掉打包状态,但 archiveBuild() 的回调没有守卫:网络答
回来时无条件写 archiveJobRef / setArchiveProgress / setArchiveJobId,把刚清掉的状态
原样盖回去。archiveJobId 一非空,usePolling 就在新会话里重新武装,去轮询一个用户
根本没发起的打包;轮询失败则在新项目弹出「打包失败」。

时序上这是常见结果而非窄窗口:effect 在 commit 时同步执行,回调要等网络。

加 archiveGenRef,切换时递增(与批删、读目录同一套做法);回调过期就直接丢弃 —— 失败
的构建也不再写错误条,它属于已经离开的那个项目。

顺带改掉那条注释里不成立的理由:原文说「轮询会拿新 scope 去问旧任务」,而轮询根本不
发送 scope(那是另一个既有问题,本次不动)。换成真正的理由:进度条属于发起它的项目,
遗留的 archiveJobId 会让轮询继续跑并把失败报在新项目上。

守护用例:file-tree-archive 新增一条,把 build 挂在延迟 promise 上,中途换会话再放行 ——
去掉修复后实测 archiveStatus 会被调用。mountTree 顺势扩出 show(sessionId, cwd) 以支持
就地换会话,现有用例不受影响。
This commit is contained in:
unsc9 committed 2026-10-05 11:31:54 +08:00
1 parent 44d9e7f408
commit 58c4e7b49b
2 files changed
+64 -19

No files matched your search

+16 -4
View File
@@ -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<string | null>(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))
})
}
+48 -15
View File
@@ -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<void>
unmount: () => void
}
@@ -88,23 +90,27 @@ async function mountTree(): Promise<Harness> {
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<void> => {
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()
})
})