diff --git a/.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.i18n.yaml b/.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.i18n.yaml index b091330ead..104fe53627 100644 --- a/.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.i18n.yaml +++ b/.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.i18n.yaml @@ -12,8 +12,8 @@ en: 83d40f59669f4d33 zh: 1e8de8d1b7e62861 /agent-note-right-sidebar-drag-restyle-budget/calibration: - en: dc1b35868c5ea353 - zh: 546aca16b16ca98b + en: 843539a8446bbe65 + zh: 806c97cb8fecdb61 /agent-note-right-sidebar-drag-restyle-budget/alternatives-considered: en: 721cbfb3276fcb5b zh: 9557ee132f77f5a6 diff --git a/.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.md b/.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.md index dcc55127af..502a331260 100644 --- a/.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.md +++ b/.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.md @@ -30,6 +30,8 @@ In the 120-turn Chromium trace, 40 `Layout` events per direction ran inside scri At 2560px with 120 turns and a 720px rendered column, the required benchmark traces the full drag against built Web artifacts, including the press and release. Before the fix, widening restyled 18,239 / 18,428 = 0.98974, failing the 0.10 budget. A diagnostic trace attributed 14,911 restyled elements to a single reserve-room flip. After the fix, widening restyled 380 / 18,428 = 0.02062 and narrowing restyled 379 / 18,428 = 0.02057. The Sidebar moved 80px each way, the column stayed at 720px, and the probe switched in both directions. The budget allows 1,842 restyles: nearly five times either passing count, but less than 40 updates of 82 elements even without the single large recalculation. +The fixed-column peak has an absolute budget of 512 elements in any one completed `UpdateLayoutTree` event. In a 120-turn local widening trace, 40 passes visited one element each, 40 visited three each, two visited four each, and one visited 212 (380 total). Restoring only inheritance of the dock probe's room value into the message flow in the same browser workload left the small buckets unchanged but replaced the 212-element peak with 14,911. The budget leaves more than twice the healthy peak without growing with the size of unrelated page content. A separate control with 600 total visits passes the cumulative budget either way: 200 passes of three pass the peak budget, one pass of 600 fails it. + ## Alternatives considered - Budget frame gaps or style milliseconds. Rejected for the gate: they need browser-runner calibration that does not exist yet, and they also include the transcript reflow at each new column width, which a resize legitimately pays. They remain diagnostics. diff --git a/.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.zh.md b/.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.zh.md index 39d852ed5c..9b6ec141d5 100644 --- a/.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.zh.md +++ b/.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.zh.md @@ -30,6 +30,8 @@ Status: implemented 在 2560px 视口中完整加载 120 轮、正文实际列宽固定为 720px 时,必需基准使用构建后的 Web 产物,追踪包含按下和松开鼠标的整个拖动过程。修复前,加宽方向的重算比值为 18,239 / 18,428 = 0.98974,超过 0.10 的预算。诊断追踪显示,仅一次预留状态翻转就重算了 14,911 个元素。修复后,加宽方向为 380 / 18,428 = 0.02062,缩窄方向为 379 / 18,428 = 0.02057。两个方向的侧栏都移动了 80px,正文列保持 720px,探针均发生翻转。预算允许重算 1,842 个元素,接近两个实测值的五倍;即使没有那次大幅重算,每步重算 82 个元素、连续 40 步也会超限。 +正文列固定时,单个完整 `UpdateLayoutTree` 事件最多可重算 512 个元素。在本地 120 轮的加宽追踪中,40 次各重算 1 个元素,40 次各重算 3 个,2 次各重算 4 个,1 次重算 212 个(累计 380 个)。在相同浏览器负载中,只恢复消息流对停靠探针空间标记的继承,小规模重算的分布不变,峰值却从 212 变为 14,911。预算为健康峰值留出两倍以上余量,且不随页面上无关内容的增多而放宽。另一组对照的累计计数同为 600,两者均满足累计预算:分成 200 次、每次 3 个满足峰值预算,集中在一次重算 600 个则失败。 + ## 备选方案 - 为帧间隔或样式毫秒数设预算。门禁不采用:它们需要尚不存在的浏览器 runner 校准,而且还包含按每个新列宽重排正文的成本,这是缩放本应付出的。它们保留为诊断输出。 diff --git a/benchmarks/rightbar-drag-browser/README.i18n.yaml b/benchmarks/rightbar-drag-browser/README.i18n.yaml index 0c75dcd69d..d7a6028a8d 100644 --- a/benchmarks/rightbar-drag-browser/README.i18n.yaml +++ b/benchmarks/rightbar-drag-browser/README.i18n.yaml @@ -9,5 +9,5 @@ en: 0dce8b92617ea78b zh: 22ce9ccc29acda36 /right-sidebar-drag-browser-benchmark/measurements: - en: ece05c9d313f7906 - zh: 0c54ddf5bb75b162 + en: a46b806f1f9ae765 + zh: f4a81542a960a55c diff --git a/benchmarks/rightbar-drag-browser/README.md b/benchmarks/rightbar-drag-browser/README.md index 97b7ec9248..678d9fbea0 100644 --- a/benchmarks/rightbar-drag-browser/README.md +++ b/benchmarks/rightbar-drag-browser/README.md @@ -16,8 +16,9 @@ Each Chromium trace spans one trusted drag through two animation frames after re - **Fixed column:** The first sample also opens the same 120 turns in a separate page with fresh storage, a 2560×1000 viewport and a saved 720px content width. After the opening slide settles, the visible Sidebar must reach each of 40 steps of 2px, from 1152px to 1232px and back. Every delivered transcript column width must be 720px. The Session panel's reserve-room value at its dock probe must switch from 1px to 0px and back. - **Restyles:** Sum the elements reported by each style recalculation and divide by the page's element count. For the changing column, the upper median per direction must not exceed 1 (one page's worth). For the fixed column, each direction must not exceed 0.10. +- **Individual style passes:** Every sample prints `restylePassesByElementCount`: a bucket such as `"3": 40` means 40 complete Chromium `UpdateLayoutTree` events each reported three restyled elements. These are element visits, not distinct DOM nodes across a drag or invalidation reasons. `peakStyledElements` is the largest count in one event; `peakCountPassMs` is the duration of that event (the longer duration wins a tie). Each fixed-column direction must keep every pass at or below 512 elements, even if the cumulative ratio passes. - **Layouts:** Each changing-column drag must produce at least one `Layout` event. `layoutCount` includes them all; `scriptNestedLayoutCount` counts each layout fully enclosed by a script event on the same process and thread, including animation frame callbacks. This subset must not exceed 20 per drag (half the mouse steps). Nesting is a proxy for synchronous layout, not evidence that every nested layout is unnecessary; browser reflow outside script does not consume the budget. -Frame gaps and style and layout durations are diagnostics only. Neither budget scales with CPU speed. +The p95 and longest animation-frame gaps (`frameP95Ms`, `maxFrameGapMs`) span the gesture through the two frames after release, excluding trace startup and decoding. Frame gaps and style and layout durations are diagnostics only; the work-count budgets do not scale with CPU speed. The fixture contains mixed-language prose, lists, 12 code fences, 8 tables, reasoning, and 24 tool results. No model, tool, external network, recorded Session, or private Harness home supplies its content. The [decision record](../../.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.md) owns the workload choice, calibration, alternatives, and exclusions. diff --git a/benchmarks/rightbar-drag-browser/README.zh.md b/benchmarks/rightbar-drag-browser/README.zh.md index cf0a9633bd..22763db2df 100644 --- a/benchmarks/rightbar-drag-browser/README.zh.md +++ b/benchmarks/rightbar-drag-browser/README.zh.md @@ -16,8 +16,9 @@ - **列宽固定:** 第一个样本还在单独页面中打开相同的 120 轮记录;该页面使用独立存储,视口为 2560×1000,预存的正文宽度为 720px。侧栏展开动画结束后,从 1152px 到 1232px 再拖回,每个方向 40 步,每步 2px,可见侧栏必须逐步到达目标宽度。每次观察到的正文列宽都必须是 720px。会话面板的停靠探针上,标记预留空间是否足够的值必须从 1px 变为 0px,再变回 1px。 - **样式重算:** 各次样式重算报告的元素数之和除以页面元素数。列宽变化时,每个方向的上中位数不得超过 1(即一整个页面的元素数)。列宽固定时,每个方向的比值不得超过 0.10。 +- **单次样式重算:** 每个样本都输出 `restylePassesByElementCount`。例如 `"3": 40` 表示 Chromium 有 40 个完整的 `UpdateLayoutTree` 事件各自重算了 3 个元素。这不是整段拖动去重后的 DOM 节点数,也不是失效原因条数。`peakStyledElements` 是单个事件的最大元素数;`peakCountPassMs` 是该事件的耗时(元素数并列时取较长者)。正文固定时,加宽和缩窄的每次重算都不得超过 512 个元素,即使累计比值合格也一样。 - **布局:** 列宽变化时,每次拖动必须产生至少一个 `Layout` 事件。`layoutCount` 统计所有布局;`scriptNestedLayoutCount` 只统计起止时间均落在同一进程、同一线程的脚本事件内的布局,每次布局只计一次,动画帧回调也算脚本事件。每次拖动此项不得超过 20(鼠标步数的一半)。嵌套关系是判断同步布局的间接指标,并不说明每次嵌套布局都多余;脚本以外的浏览器重排不占用该预算。 -帧间隔、样式重算耗时和布局耗时仅作诊断。两项预算都不随 CPU 速度缩放。 +帧间隔的 p95 和最大值(`frameP95Ms`、`maxFrameGapMs`)覆盖手势至松开后两个动画帧,不包含追踪启动和解析。帧间隔、样式重算耗时和布局耗时仅作诊断;工作量预算不随 CPU 速度缩放。 夹具包含中英混排的正文、列表、12 个代码块、8 个表格、推理内容和 24 个工具结果。内容不来自任何模型、工具、外部网络、录制会话或私有 Harness home。[决策记录](../../.agents/notes/implemented/testing/2026-11-20-rightbar-drag-restyle-budget.zh.md)负责说明负载选择、校准、备选方案和排除项。 diff --git a/benchmarks/rightbar-drag-browser/rightbar-drag.bench.ts b/benchmarks/rightbar-drag-browser/rightbar-drag.bench.ts index f5ed1431a4..51257d0c99 100644 --- a/benchmarks/rightbar-drag-browser/rightbar-drag.bench.ts +++ b/benchmarks/rightbar-drag-browser/rightbar-drag.bench.ts @@ -14,6 +14,8 @@ const FIXED_COLUMN_PX = 720 const FIXED_PANEL_START_PX = 1152 const FIXED_STEP_PX = 2 const FIXED_RESTYLE_BUDGET = 0.1 +/** One fixed-column style pass, independent of the cumulative page-ratio budget. */ +const FIXED_PEAK_ELEMENT_BUDGET = 512 const STEPS = 40 const STEP_PX = 8 const STEP_GAP_MS = 8 @@ -40,6 +42,11 @@ interface TraceEvent { interface TraceSummary { styledElements: number + /** Element count → number of completed UpdateLayoutTree events reporting that count. */ + restylePassesByElementCount: Record + peakStyledElements: number + /** Duration of the pass with the highest element count; the longer pass wins ties. */ + peakCountPassMs: number styleMs: number layoutMs: number layoutCount: number @@ -49,6 +56,7 @@ interface TraceSummary { interface DragSample extends TraceSummary { restyleRatio: number frameP95Ms: number + maxFrameGapMs: number columnWidthBefore: number columnWidth: number } @@ -57,6 +65,10 @@ function expectRestylesWithinBudget(ratio: number, budget = RESTYLE_BUDGET): voi expect(ratio).toBeLessThanOrEqual(budget) } +function expectFixedPeakWithinBudget(count: number): void { + expect(count, 'most elements restyled in one fixed-column UpdateLayoutTree event').toBeLessThanOrEqual(FIXED_PEAK_ELEMENT_BUDGET) +} + function median(values: number[]): number { return values.toSorted((a, b) => a - b)[Math.floor(values.length / 2)]! } @@ -71,8 +83,25 @@ function summarizeTrace(buffer: Buffer): TraceSummary { expect(scripts.length, 'drag trace must contain script events').toBeGreaterThan(0) const end = (event: TraceEvent): number => event.ts + (event.dur ?? 0) const ms = (list: TraceEvent[]): number => list.reduce((total, event) => total + (event.dur ?? 0), 0) / 1000 + const restylePassesByElementCount: Record = {} + let styledElements = 0 + let peakStyledElements = 0 + let peakCountPassMs = 0 + for (const event of styles) { + const count = event.args?.elementCount + if (count === undefined || !Number.isSafeInteger(count) || count < 0) { + throw new Error('Chromium UpdateLayoutTree is missing a valid elementCount') + } + const passMs = (event.dur ?? 0) / 1000 + restylePassesByElementCount[count] = (restylePassesByElementCount[count] ?? 0) + 1 + styledElements += count + if (count > peakStyledElements || (count === peakStyledElements && passMs > peakCountPassMs)) { + peakStyledElements = count + peakCountPassMs = passMs + } + } return { - styledElements: styles.reduce((total, event) => total + (event.args?.elementCount ?? 0), 0), + styledElements, restylePassesByElementCount, peakStyledElements, peakCountPassMs, styleMs: ms(styles), layoutMs: ms(layouts), layoutCount: layouts.length, @@ -120,7 +149,8 @@ async function drag(browser: Browser, page: Page, direction: 1 | -1, elements: n const y = box.y + box.height / 2 const readColumnWidth = (): Promise => page.locator('[data-chat-flow]').first().evaluate(element => element.getBoundingClientRect().width) const columnWidthBefore = await readColumnWidth() - await page.evaluate(() => { + await browser.startTracing(page, { categories: ['devtools.timeline', 'disabled-by-default-devtools.timeline'] }) + await page.evaluate(() => new Promise((resolve) => { const probe = { frames: [] as number[], running: true } ;(window as FrameProbe).benchmarkFrames = probe const tick = (time: number): void => { @@ -128,9 +158,8 @@ async function drag(browser: Browser, page: Page, direction: 1 | -1, elements: n probe.frames.push(time) requestAnimationFrame(tick) } - requestAnimationFrame(tick) - }) - await browser.startTracing(page, { categories: ['devtools.timeline', 'disabled-by-default-devtools.timeline'] }) + requestAnimationFrame((time) => { tick(time); resolve() }) + })) await page.mouse.move(x, y) await page.mouse.down() for (let step = 1; step <= STEPS; step++) { @@ -144,18 +173,20 @@ async function drag(browser: Browser, page: Page, direction: 1 | -1, elements: n } await page.mouse.up() // Two animation frames let the release reach style and layout before the trace closes. - await page.evaluate(() => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(() => resolve())))) + const frames = await page.evaluate(() => new Promise((resolve) => { + requestAnimationFrame(() => requestAnimationFrame(() => { + const probe = (window as FrameProbe).benchmarkFrames! + probe.running = false + resolve(probe.frames) + })) + })) const trace = summarizeTrace(await browser.stopTracing()) - const frames = await page.evaluate(() => { - const probe = (window as FrameProbe).benchmarkFrames! - probe.running = false - return probe.frames - }) const gaps = frames.slice(1).map((time, index) => time - frames[index]!).sort((a, b) => a - b) return { ...trace, restyleRatio: trace.styledElements / elements, frameP95Ms: gaps[Math.min(gaps.length - 1, Math.floor(gaps.length * 0.95))] ?? 0, + maxFrameGapMs: gaps.at(-1) ?? 0, columnWidthBefore, columnWidth: await readColumnWidth(), } @@ -203,13 +234,18 @@ async function measureFixedColumn(browser: Browser, url: string): Promise const { panel, column } = (window as FixedWidthProbe).benchmarkWidths return { panelWidth: panel, columnWidths: [...column] } }) - console.log(JSON.stringify({ benchmark: 'rightbar-drag-browser/fixed-column', direction, elements, budget: FIXED_RESTYLE_BUDGET, panelStart, ...geometry, reserveBefore, reserveAfter, ...result })) + console.log(JSON.stringify({ + benchmark: 'rightbar-drag-browser/fixed-column', direction, elements, + budget: FIXED_RESTYLE_BUDGET, peakElementBudget: FIXED_PEAK_ELEMENT_BUDGET, + panelStart, ...geometry, reserveBefore, reserveAfter, ...result, + })) panelStart += direction * STEPS * FIXED_STEP_PX expect(geometry.panelWidth).toBe(panelStart) expect(geometry.columnWidths).toEqual([FIXED_COLUMN_PX]) expect([result.columnWidthBefore, result.columnWidth]).toEqual([FIXED_COLUMN_PX, FIXED_COLUMN_PX]) expect([reserveBefore, reserveAfter]).toEqual(direction === 1 ? ['1px', '0px'] : ['0px', '1px']) expect(result.styledElements).toBeGreaterThan(0) + expectFixedPeakWithinBudget(result.peakStyledElements) expectRestylesWithinBudget(result.restyleRatio, FIXED_RESTYLE_BUDGET) } expect(consoleWatch.pageErrors).toEqual([]) @@ -233,6 +269,41 @@ it('rejects the fixed-column drag that restyled the transcript when reserve room for (const restyled of [380, 379]) expectRestylesWithinBudget(restyled / elements, FIXED_RESTYLE_BUDGET) }) +/** A Chromium trace fragment with completed style passes and a separate invalidation notification. */ +function restyleTrace(passes: readonly (readonly [number | undefined, number])[]): TraceSummary { + const span = { pid: 1, tid: 1, ph: 'X' } + const events = [ + { ...span, ts: 0, name: 'FunctionCall', dur: 1 }, + { ...span, ts: 0, ph: 'I', name: 'ScheduleStyleInvalidationTracking', args: { elementCount: 99_999 } }, + ...passes.map(([elementCount, dur], index) => ({ + ...span, ts: (index + 1) * 10_000, name: 'UpdateLayoutTree', dur, args: { elementCount }, + })), + ] + return summarizeTrace(Buffer.from(JSON.stringify({ traceEvents: events }))) +} + +it('reports the breadth of each style pass and the duration of the broadest pass, not the slowest pass', () => { + expect(restyleTrace([[3, 9_000], [14_911, 1_200], [3, 500], [14_911, 2_300], [0, 100]])).toMatchObject({ + styledElements: 29_828, + restylePassesByElementCount: { 0: 1, 3: 2, 14911: 2 }, + peakStyledElements: 14_911, + peakCountPassMs: 2.3, + }) + expect(() => restyleTrace([[undefined, 100]])).toThrow('elementCount') +}) + +it('distinguishes one oversized style pass from the same total work spread over small passes', () => { + const elements = 18_428 + const distributed = restyleTrace(Array.from({ length: 200 }, () => [3, 100] as const)) + const spike = restyleTrace([[600, 100]]) + for (const result of [distributed, spike]) { + expect(result.styledElements).toBe(600) + expectRestylesWithinBudget(result.styledElements / elements, FIXED_RESTYLE_BUDGET) + } + expectFixedPeakWithinBudget(distributed.peakStyledElements) + expect(() => expectFixedPeakWithinBudget(spike.peakStyledElements)).toThrow() +}) + it('counts each layout enclosed by script on the same thread once, including frame callbacks', () => { const event = (name: string, ts: number, dur: number, pid = 1, tid = 1): TraceEvent => ({ name, ph: 'X', ts, dur, pid, tid }) const events = [ @@ -267,6 +338,7 @@ it(`bounds restyles and script-nested layouts while the right Sidebar is dragged expect(Math.max(result.columnWidthBefore, result.columnWidth)).toBeLessThan(CONTENT_WIDTH_FLOOR_PX) if (key === 'widen') expect(result.columnWidth).toBeLessThan(result.columnWidthBefore) else expect(result.columnWidth).toBeGreaterThan(result.columnWidthBefore) + expect(result.styledElements).toBeGreaterThan(0) expect(result.layoutCount).toBeGreaterThan(0) expect(result.scriptNestedLayoutCount).toBeLessThanOrEqual(SCRIPT_NESTED_LAYOUT_BUDGET) samples[key].push(result)