fix(tasks): coalesce pointer drags to one update per frame

Background pans, the floating-window title drag and the anchored-popover drag committed on every pointermove, so a high-report-rate mouse re-rendered the tasks canvas at event cadence. Compose the latest position in a ref and commit it through the existing frame batcher; release and cancel flush synchronously. Click suppression still updates per event.

Regression: same-frame 100-move burst commits drop 98 -> 1; full suite 2720 passed.
This commit is contained in:
Menghuan1918 committed 2026-10-23 21:28:22 +08:00
1 parent 787d873a8c
commit dc385130a0
6 files changed
+107 -8

No files matched your search

+1
View File
@@ -6,6 +6,7 @@
- **任务图展开修复**:平移画布后,聚合卡和 Agent 卡的下一次点击不再被误判为拖动;展开聚合与查看详情恢复正常。
- **任务图性能**:卡片内容使用独立 memo 边界,平移与布局补间不再重复渲染内容;布局用索引和显式栈替代逐边查找与深递归,同次宽度适配复用相同列数的布局。2000 个非根 Agent 的纯布局对照及 200/500 Agent 真浏览器证据见[验证记录](docs/plans/2026-10-23-tasks-folding-performance-validation.md),布局加速不外推为整页加速。
- **任务页拖拽改为每帧一次更新**:图模式背景平移、后台任务浮窗标题拖动、锚定弹层拖动不再按鼠标事件频率重绘(高回报率鼠标下此前每个 pointermove 都提交整块画布);松手同步落位,点击判定与最终位置不变。实测同一帧内 100 次移动的画布提交由 98 次降到 1 次。
- **已结束分支整体聚合**:只有整条分支全部结束才自动聚合;仍有运行后代、当前查看路径或未知子代理目录的分支保持可见,聚合计数覆盖隐藏的 Agent 后代。
- **四项独立自动规则**:复用设置齿轮与现有偏好存储,分别控制已结束叶子聚合、已结束分支聚合、同一父级至少 3 个空闲队友聚合(workflow 内沿用原规则,也受空闲开关控制),以及 Agent 总数至少 8 个时后台任务抽屉自动收起,默认全部开启。未手动操作时设置即时生效;手动展开或收起始终优先。
@@ -76,6 +76,12 @@ bash scripts/e2e-context.sh --variant no-context --grep 'tasks graph'
单命令 `bash /tmp/tasks-layout-bench/run.sh`,exit 0。原始 11 样本、输入/source/bundle SHA-256 与运行环境在 `/tmp/tasks-final-layout-benchmark.json`。同机当时有浏览器验证,loadavg 约 12.5/10 核,绝对毫秒仅参考,结论基于同进程交错的相对中位。2000 个非根 Agent 的布局耗时至少减少 50% 的目标达成;布局坐标/顺序及深链安全由对应 spec 验证,不以计时 harness 代替行为测试。
## 拖拽渲染帧率(后续修复)
用户反馈任务页拖拽按鼠标回报率渲染。真机探针(Playwright + 真实 rc.1 预览,确定性 MutationObserver 统计画布 style 提交)在修复前测到:真实鼠标拖拽 **121 次 pointermove → 123 次画布提交**;用 MessageChannel 模拟高回报率鼠标(100 次移动全部落在两帧内)时 **约 98 次提交 / 4 帧**。图模式平移的 `pointermove` 每次都 `setTf`,没有合并,而同仓库的侧栏分隔条、浮窗 resize、编辑器 gutter 早已用 `createFrameBatcher` 合并到每帧。
修复沿用该既有模式:平移、浮窗标题拖动、锚定弹层拖动把最新位置写进 ref,每帧最多提交一次;`pointerup` / `pointercancel` 调用 `flushNow()`,最终位置不丢;点击抑制的 `travelRef` 仍在事件内同步更新。同探针复测:同一帧内 100 次移动的提交 **98 → 1 次**(2 帧窗口内 2 次提交)。新增 jsdom 回归 `tests/tasks-page.spec.tsx > coalesces background pans into one update per animation frame and flushes on release`,先红(帧前同步提交 72px)后绿;`tests/tasks-page.spec.tsx` 64/64、`tests/floating-window.spec.tsx` 与 `frame-batcher` 合计 76/76 通过。真实鼠标在无头 Chromium 中本就按帧派发,因此该场景前后提交数接近;判别性证据来自同帧突发。
## 既有 fs-watch 偶发失败
没有修改 watcher 或它的测试。首次全量失败后,独立复跑得到:工作树 18 轮,14 通过 / 4 失败;未改动的基点 worktree 12 轮,11 通过 / 1 失败。失败同为单次写入后 15 秒未收到 notice(spec 的断言行 50 或 111),基线也会复现,不能算本批引入的回归,也不能把这些失败记作通过。
+18 -3
View File
@@ -20,6 +20,7 @@ import {
type PointerEvent as ReactPointerEvent, type ReactNode,
} from 'react'
import { createPortal } from 'react-dom'
import { createFrameBatcher } from './frame-batcher.ts'
/** The popover's default visual width (viewport-clamped). */
const POP_WIDTH = 264
@@ -53,6 +54,10 @@ export function AnchoredPopover(props: AnchoredPopoverProps): ReactNode {
const [offset, setOffset] = useState({ x: 0, y: 0 })
const offsetRef = useRef(offset)
offsetRef.current = offset
/** Drag moves land at most once per frame (see ./frame-batcher.ts). */
const dragBatcher = useRef(createFrameBatcher()).current
const pendingDragRef = useRef<{ offset: { x: number; y: number }; pos: { left: number; top: number } } | null>(null)
useEffect(() => () => { dragBatcher.dispose() }, [dragBatcher])
/** The anchored baseline: a drag always starts from the measured position. */
const baseRef = useRef({ left: 0, top: 0 })
@@ -101,18 +106,28 @@ export function AnchoredPopover(props: AnchoredPopoverProps): ReactNode {
Math.max(start.y + (move.clientY - startY), VIEWPORT_MARGIN - base.top),
Math.max(VIEWPORT_MARGIN - base.top, window.innerHeight - 48 - base.top),
)
setOffset({ x: nextX, y: nextY })
setPos({ left: base.left + nextX, top: base.top + nextY })
pendingDragRef.current = {
offset: { x: nextX, y: nextY },
pos: { left: base.left + nextX, top: base.top + nextY },
}
dragBatcher.schedule(() => {
const pending = pendingDragRef.current
if (pending === null) return
pendingDragRef.current = null
setOffset(pending.offset)
setPos(pending.pos)
})
}
const onUp = (): void => {
window.removeEventListener('pointermove', onMove)
window.removeEventListener('pointerup', onUp)
window.removeEventListener('pointercancel', onUp)
dragBatcher.flushNow()
}
window.addEventListener('pointermove', onMove)
window.addEventListener('pointerup', onUp)
window.addEventListener('pointercancel', onUp)
}, [draggable, cardWidth])
}, [cardWidth, dragBatcher, draggable])
// Global dismissal (see the file header for the contract).
useEffect(() => {
+14 -2
View File
@@ -211,6 +211,9 @@ export function FloatingWindow(props: FloatingWindowProps): ReactNode {
}, [])
/** Drag the window by its title bar (buttons inside it keep working). */
const dragBatcher = useRef(createFrameBatcher()).current
const pendingDragRef = useRef<WindowBox | null>(null)
useEffect(() => () => { dragBatcher.dispose() }, [dragBatcher])
const onTitlePointerDown = useCallback((event: ReactPointerEvent<HTMLDivElement>): void => {
if (event.button !== 0) return
const target = event.target as HTMLElement
@@ -222,21 +225,30 @@ export function FloatingWindow(props: FloatingWindowProps): ReactNode {
const startX = event.clientX
const startY = event.clientY
const onMove = (move: PointerEvent): void => {
setBox({
// A pointer stream outruns the display; compose the latest box here and
// commit it once per frame, not once per event (see ./frame-batcher.ts).
pendingDragRef.current = {
...start,
left: clampOrigin(start.left + (move.clientX - startX), start.width, window.innerWidth),
top: clampOrigin(start.top + (move.clientY - startY), start.height, window.innerHeight),
}
dragBatcher.schedule(() => {
const pending = pendingDragRef.current
if (pending === null) return
pendingDragRef.current = null
setBox(pending)
})
}
const onUp = (): void => {
window.removeEventListener('pointermove', onMove)
window.removeEventListener('pointerup', onUp)
window.removeEventListener('pointercancel', onUp)
dragBatcher.flushNow()
}
window.addEventListener('pointermove', onMove)
window.addEventListener('pointerup', onUp)
window.addEventListener('pointercancel', onUp)
}, [])
}, [dragBatcher])
/** Resize from one edge handle: `axis` picks which dimension the handle owns. */
const resizeBatcher = useRef(createFrameBatcher()).current
+28 -3
View File
@@ -41,6 +41,7 @@ import {
GRAPH_FIT_MIN_SCALE, GRAPH_NODE_H, GRAPH_NODE_W, GRAPH_PAD, layoutTasksGraphForWidth,
textTailLines, type GraphBox,
} from './tasks-graph-layout.ts'
import { createFrameBatcher } from './frame-batcher.ts'
import { useTweenedLayout } from './tasks-layout-tween.ts'
import { easeOut as glideEase } from './tasks-layout-tween.ts'
import { fitTransform, GLIDE_MS, lerpTransform, type ViewTransform } from './tasks-view-glide.ts'
@@ -416,6 +417,19 @@ export function TasksGraph(props: TasksGraphProps): ReactNode {
refit()
}, [refit])
/**
* The pan is applied at most once per animation frame. A pointer stream fires
* several times faster than the display (a 1000 Hz mouse, a trackpad, some
* Electron builds), and committing every move re-renders the whole canvas —
* cards and edges — at event cadence. The latest position is composed in a
* ref and the frame task reads it, so coalescing loses nothing; release
* flushes synchronously so the final position is never dropped.
*/
const panBatcher = useRef(createFrameBatcher()).current
const pendingPanRef = useRef<{ x: number; y: number; k: number } | null>(null)
const draggingRef = useRef(false)
useEffect(() => () => { panBatcher.dispose() }, [panBatcher])
const onPointerDown = useCallback((event: ReactPointerEvent<HTMLDivElement>): void => {
if (event.button !== 0) return
stopGlide()
@@ -434,19 +448,30 @@ export function TasksGraph(props: TasksGraphProps): ReactNode {
travelRef.current = Math.max(travelRef.current, Math.abs(dx) + Math.abs(dy))
if (travelRef.current <= CLICK_TOLERANCE_PX) return
touchedRef.current = true
setDragging(true)
setTf(clampTf({ x: x + dx, y: y + dy, k: tfRef.current.k }))
if (!draggingRef.current) {
draggingRef.current = true
setDragging(true)
}
pendingPanRef.current = { x: x + dx, y: y + dy, k: tfRef.current.k }
panBatcher.schedule(() => {
const pending = pendingPanRef.current
if (pending === null) return
pendingPanRef.current = null
setTf(clampTf(pending))
})
}
const onUp = (): void => {
window.removeEventListener('pointermove', onMove)
window.removeEventListener('pointerup', onUp)
window.removeEventListener('pointercancel', onUp)
panBatcher.flushNow()
draggingRef.current = false
setDragging(false)
}
window.addEventListener('pointermove', onMove)
window.addEventListener('pointerup', onUp)
window.addEventListener('pointercancel', onUp)
}, [clampTf, stopGlide])
}, [clampTf, panBatcher, stopGlide])
/** Suppress node clicks that ended a drag gesture. */
const clickAllowed = useCallback((): boolean => travelRef.current <= CLICK_TOLERANCE_PX, [])
+40
View File
@@ -574,6 +574,46 @@ describe('Tasks page graph interactions and team board', () => {
unmount()
})
it('coalesces background pans into one update per animation frame and flushes on release', async () => {
const store = makeStore(snapshotWithChildren(2))
const { container, unmount } = renderRoot(
createElement(SubagentView, { sessionId: 'root', active: true, ctx: makeCtx(store) }),
)
const canvas = container.querySelector('[role="group"]') as HTMLElement
const inner = canvas.querySelector('div[style*="transform"]') as HTMLElement
expect(inner).not.toBeNull()
// Let the mount-time fit (and any glide frame) settle before measuring.
await act(async () => { await new Promise<void>(resolve => requestAnimationFrame(() => resolve())) })
const settled = inner.getAttribute('style')
// A high-report-rate mouse delivers many moves between two frames. None of
// them may commit on its own: one batched update owns the whole frame.
await act(async () => {
canvas.dispatchEvent(new MouseEvent('pointerdown', { bubbles: true, clientX: 5, clientY: 5 }))
for (let step = 1; step <= 20; step += 1) {
window.dispatchEvent(new MouseEvent('pointermove', {
bubbles: true, clientX: 5 + step * 10, clientY: 5 + step * 10,
}))
}
})
expect(inner.getAttribute('style'), 'no commit before the frame').toBe(settled)
await act(async () => { await new Promise<void>(resolve => requestAnimationFrame(() => resolve())) })
const afterFrame = inner.getAttribute('style')
expect(afterFrame, 'the frame applies the batch').not.toBe(settled)
// Release before the next frame must still apply the LAST position (back to
// the gesture origin, unclamped — distinct from the framed position).
await act(async () => {
window.dispatchEvent(new MouseEvent('pointermove', {
bubbles: true, clientX: 5, clientY: 5,
}))
window.dispatchEvent(new MouseEvent('pointerup', { bubbles: true }))
})
expect(inner.getAttribute('style'), 'release flushes the pending move').not.toBe(afterFrame)
unmount()
})
it('activates a graph node after a background pointerdown (no click theft)', async () => {
const navigation: NavigationSpy = { opened: [] }
const store = makeStore(snapshotWithChildren(1))