DSH / Atlas
2026-11-27implementedbug-fix

Linear Statistics folding for a wide PTC run

统计面板对宽 PTC 调用的线性折叠

The Statistics view folds every Session window into one node per step and one node per Code-Mode root call. A single PTC `run_code` call can dispatch tens of thousands of nested subcalls, and the [fold](../../../../packages/experimental/client-ui-stats/src/client/stats-fold.ts) copied the two collections that grow with that count on every event: `starts` in `tool/ptc-dispatch-start`, and `calls` in `tool/ptc-dispatch

Source compatibility: Chinese title uses a compatible non-canonical header

English

Problem

The Statistics view folds every Session window into one node per step and one node per Code-Mode root call. A single PTC run_code call can dispatch tens of thousands of nested subcalls, and the fold copied the two collections that grow with that count on every event: starts in tool/ptc-dispatch-start, and calls in tool/ptc-dispatch. One root call with n subcalls therefore cost O(n²) shallow copies, and the fold alone took seconds before the panel could render.

The Chat and Trajectory Tool Definitions already fold their tool trees linearly; this package folds its own per-step records, so neither Definition's fix covered it.

Publication carried the same cost. codeModeDefinition.buildViewNode called stepMetrics(state) for every dispatch, which materializes and sorts the complete call index to produce a node that lists at most CALL_LIMIT (12) rows. The Definition declared no publication cadence, so the assembler flushed and rebuilt the target snapshot once per dispatch event.

Decision

The four fold collections of one Context are Maps it owns and mutates in place: pending and tools for the tool tally, calls for recorded calls, and starts for dispatch start times. update() returns the same State object, which the Assembler update rule permits; start() still creates a new State, so a replay folds from empty collections. A dispatch start is deleted once its settle event reads it, because the pair exists only to measure one duration.

The call list no longer sorts the index. A Context receives its matches in ascending seq, and a call id is created once by the event that first records it, so insertion order is the order the table lists. callList walks the index and stops at CALL_LIMIT rows, applying the per-step text budget on the way.

The Code-Mode node now materializes that bounded prefix directly through innerCalls, which also drops the nested run_code calls the step row replaces with their real subcalls; stepMetrics no longer runs for a publication that discards all of its other fields. While the visible prefix, the anchor, and the Location stay the same, the Definition republishes the node object it returned before, and the target builder keeps its current snapshot when every upserted node is the one it already holds. That reuse reads the node the Assembler keeps for this Context, so it holds no cache of its own. Dispatch updates request animation-frame publication, and the Assembler materializes such a publication after three browser animation frames, so a wide run's dispatches coalesce into at most one publication every three frames.

A Code-Mode Context retains at most CALL_LIMIT call records. The event that settles a dispatch records it only when it can still reach the published prefix: a name the step row displays (!isCodeMode(name)) and an index shorter than CALL_LIMIT. Records already retained are replaced in place, so a repeated settle still updates its row. The counters that the dispatch branch itself maintains keep counting every dispatch: tools, which tallies each reported name, and toolErrors, which counts every failed one. The call-path counters toolPairs, toolMs, toolOpen, and toolMissing are written by the native tool/call and tool/result branches, which a Code-Mode Context never receives, so the retention bound cannot affect them. A step Context keeps every outer call: its own tool/call and tool/result path is untouched, and the retention rule sits on the dispatch branch that only a Code-Mode Context (or its fallback replay) reaches.

The bound is safe because nothing inside the Context consumes the dropped records: the published inner contribution and the step merge in stats-aggregate both consume only the retained first CALL_LIMIT displayable dispatches. A nested run_code dispatch therefore does not take one of those slots, which is why a run that dispatches a nested runner lists up to CALL_LIMIT real calls where it listed fewer before. What the bound gives up is a later "expand to every nested call" view: that view must derive a wider list from the Session events (or carry its own retention) instead of recovering it from this State.

Calibration

All rows come from the package diagnostic on macOS arm64 with Node 24.18.0, run through pnpm exec vitest run --config vitest.web.perf.config.ts packages/experimental/client-ui-stats/tests/ptc-dispatch-fold.perf.client.ts. Before values restore the pre-change sources into the worktree (git checkout <diagnostic commit> -- packages/experimental/client-ui-stats/src); the workload, harness, and assertions are otherwise identical. fold folds the same events into one Code-Mode State, publish builds the node once, republish is the amortized cost of 50 further builds of the same Context, and live appends every event through the real ConversationNodeAssembler with the Statistics target active.

Nested subcallsEventsfold beforefold afterpublish beforepublish afterrepublish beforelive beforelive after
1,0002,006101.6 ms0.9 ms0.311 ms0.058 ms0.068 ms244 ms9 ms
3,0006,0061,035.9 ms0.8 ms0.476 ms0.002 ms0.507 ms4,371 ms13 ms
10,00020,00613,945.5 ms2.2 ms2.507 ms0.004 ms2.139 ms63,434 ms35 ms

Before the change each tripling of the dispatch count multiplied the fold by roughly 10 and 13; after it the fold stays under 3 ms for every size and the live window grows from 9 ms to 35 ms. The retained records stay at CALL_LIMIT (12) at every size, with no dispatch start left outstanding. The live window still publishes once per event in this harness because Node has no requestAnimationFrame, where animation-frame degrades to immediate; a browser instead materializes at most one publication every three frames.

Alternatives considered

  • Keep the records and copy them once at publication. Copying per publication still walks the growing record for every event that publishes, so the quadratic factor moves rather than disappears, and the Code-Mode node would have to rebuild the whole index to list 12 rows.
  • Give the Code-Mode Context a shared DispatchTree module. The two preceding Definitions (Chat and Trajectory) fold a tool tree with parents, children, and acceptance rules; this fold is a flat per-step list with no nesting, and a shared module would add a cross-package API for one consumer shape.
  • Keep the call list sorted by seq. Sorting defends against an index whose insertion order differs from the event order, which the append-only fold cannot produce: a Context rejects a non-appended Match, and every record's seq is the sequence of the event that created it. The invariant is now a comment on callList instead of a per-publication O(n log n) cost.
  • Bound only the published prefix. It fixes the publication cost but leaves the Context holding one record per nested subcall, so memory still tracks the run length; the fold's own per-event work is already constant either way.
  • Defer publication with 'none' for dispatches. It starves a live run of updates until some other event requests a flush, so a running run_code row would show no inner calls until it settles. animation-frame keeps the panel live and bounds the rate instead.
  • Cache the published node in the Context State. The State is immutable surface for every other branch; a WeakMap keyed by the State, as the Chat Definition already does for projected blocks, keeps the cache out of the contract.

Consequences

One dispatch costs constant index work, so a wide PTC run opens in proportion to its event count, and the panel no longer rebuilds the complete call list to show 12 rows. Each Code-Mode Context retains at most CALL_LIMIT calls and no settled start times; the run's true totals stay in the scalar counters and in the tools tally.

The existing packages/experimental/client-ui-stats suites pass with the same published output. Three assertions changed with the implementation they pin: the State collections are Maps, the call-order case now builds its index in seq order because the old fixture ordered keys against their seq, and the Code-Mode Definition now declares a publication cadence. Package tests add the retention bound, in-place replacement of a retained dispatch, same-node republication, and the snapshot rebuild skipped for unchanged nodes; the package's 17 source files keep 100% per-file coverage.

The diagnostic is threshold-free and stays outside every CI lane, so the linear behaviour is verified by running it rather than by a gate. It covers one root call with settled dispatches in a cold fold and a live append; it does not cover several roots in one step, dispatch events still streaming, error results, or the text budget shared across a full prefix.

中文

问题

统计视图把每个 Session 窗口折叠成「每个 step 一个节点、每个 Code-Mode 根调用一个节点」。一次 PTC run_code 调用可能派发上万个嵌套子调用,而折叠对每个事件都浅拷贝两个随子调用数增长的对象:tool/ptc-dispatch-start 里的 starts,以及 tool/ptc-dispatch 里的 calls。于是单个根调用(n 个子调用)要做 O(n²) 次浅拷贝,仅折叠就要耗时数秒,面板迟迟无法渲染。

Chat 与 Trajectory 的 Tool Definition已经把各自的调用树折叠线性化;本包折叠的是自己的 per-step 记录,那两处修复并不覆盖它。

发布侧同样如此。codeModeDefinition.buildViewNode 在每个分发事件上都调用 stepMetrics(state),把完整调用索引物化并排序,最终却只输出至多 CALL_LIMIT(12)行;该 Definition 又没有声明发布节奏,装配器于每个分发事件都 flush 一次并重建目标快照。

决策

同一个 Context 的四个折叠集合改为它独占、就地增长的 Map:pending、tools 负责工具计数,calls 保存已记录的调用,starts 保存分发开始时间。update() 返回同一个 State 对象,Assembler 更新规则允许这样做;start() 仍新建 State,因此重放时从空集合开始折叠。分发开始时间在对应的结算事件读出后立即 delete,这条记录的唯一用途是量出一次耗时。

调用列表不再对索引排序。同一个 Context 收到的 match 一律按 seq 递增到达,且每个调用 id 只由首次记录它的事件创建一次,因此插入顺序就是表格列出的顺序。callList 顺着索引遍历,取到 CALL_LIMIT 行就停止,途中顺带应用单个 step 的文本预算。

Code-Mode 节点改为通过 innerCalls 直接物化这段有界前缀,同时滤掉 step 行要用真实子调用替换的嵌套 run_code;发布时不再调用 stepMetrics,因为它的其余字段在这一类节点上全部被丢弃。只要可见前缀、锚点和 Location 不变,该 Definition 就复用上一次返回的节点对象;目标构建器在整批 upsert 都是已持有节点时,也保持当前快照不变。这份复用读的是装配器为该 Context 保存的那个节点,Definition 自己不另建缓存。分发更新改申请 animation-frame 发布,装配器会在三个浏览器动画帧之后才物化这样的发布,因此宽运行的多次分发合并成每三帧至多一次发布。

一个 Code-Mode Context 至多保留 CALL_LIMIT 条调用记录。结算事件只在「这条记录还进得了已发布前缀」时才写入:名字要能被 step 行展示(!isCodeMode(name)),且索引当前短于 CALL_LIMIT。已经保留的记录仍就地替换,所以重复结算照样更新自己那一行。分发分支自己维护的计数照旧覆盖每一次分发:tools 统计每个上报的工具名,toolErrors 记下每一次失败。调用路径上的 toolPairs、toolMs、toolOpen、toolMissing 由原生 tool/call、tool/result 分支更新,Code-Mode Context 根本收不到这些事件,所以保留上限影响不到它们。step Context 保留全部外层调用,因为它的 tool/call、tool/result 路径没有改动,保留上限只落在只有 Code-Mode Context(及其 fallbackState 重放)会走到的分发分支上。

这个上限是安全的:Context 内部没有任何读者使用被丢弃的记录——发布出的 inner 贡献和 stats-aggregate 里的 step 合并,消费的都只是保留下来的前 CALL_LIMIT 条可展示分发。也正因为嵌 run_code 的分发不占这些槽位,派发了嵌套 runner 的运行会比以前多列出几条真实调用。代价是放弃了「展开查看全部嵌套调用」这类视图:将来要做,必须从 Session 事件里重新推导(或另建保留策略),不能指望从这份 State 里恢复。

校准

表中所有数字都来自包内诊断,机器为 macOS arm64、Node 24.18.0,运行命令 pnpm exec vitest run --config vitest.web.perf.config.ts packages/experimental/client-ui-stats/tests/ptc-dispatch-fold.perf.client.ts。「改造前」的数值是把改造前的源码取回工作树后跑同一份诊断得到的,负载、harness 与断言完全一致。fold 指把同一批事件折叠进一个 Code-Mode State,publish 指构建一次节点,republish 指对同一 Context 再构建 50 次的平均耗时,live 指把每个事件逐个 append 进真实的 ConversationNodeAssembler 并激活统计目标。

子调用数事件数折叠前折叠后发布前发布后复发布前live 前live 后
1 0002 006101.6 ms0.9 ms0.311 ms0.058 ms0.068 ms244 ms9 ms
3 0006 0061 035.9 ms0.8 ms0.476 ms0.002 ms0.507 ms4 371 ms13 ms
10 00020 00613 945.5 ms2.2 ms2.507 ms0.004 ms2.139 ms63 434 ms35 ms

改造前,子调用数每翻三倍,折叠耗时约涨到 10~13 倍;改造后任何规模都稳定在 3 ms 以内,live 窗口从 9 ms 增长到 35 ms。各规模下保留的调用记录恒为 CALL_LIMIT(12)条,也没有残留的分发开始时间。这套 harness 里 live 仍然是每个事件发布一次,因为 Node 没有 requestAnimationFrame,animation-frame 会退化为立即发布;浏览器端则按帧合并这些发布。

备选方案

  • 保留 Record,发布时复制一次。 每个触发发布的事件仍要遍历一个不断增长的对象,平方项只是换了位置;而且 Code-Mode 节点为了列出 12 行不得不重建整个索引。
  • 给 Code-Mode Context 复用共享的 DispatchTree 模块。 先前的两个 Definition(Chat 和 Trajectory)折叠的是带父子关系与准入规则的调用树;这里折叠的是每个 step 的扁平列表,没有嵌套,为一个消费方引入跨包模块并不划算。
  • 继续按 seq 排序。 排序防御的是「插入顺序与事件顺序不一致」,而只追加的折叠产生不了这种状态:Context 拒绝非追加的 match,每条记录的 seq 就是创建它的那个事件的序号。现在这条不变式写在 callList 的注释里,而不是每次发布付一次 O(n log n)。
  • 只限制发布出的前缀。 这样发布成本是降下来了,但 Context 仍按子调用数保留每个记录,内存依旧随运行规模增长;而折叠本身在两种做法下都已经是常数。
  • 分发更新返回 'none' 完全不发布。 正在运行的 run_code 行会一直看不到 inner 调用,直到有其他事件触发 flush 才更新。animation-frame 既保持了面板的实时性,又把频率限住。
  • 把发布过的节点缓存进 Context State。 State 对其他分支是只读契约;按 State 索引的 WeakMap(Chat Definition 已用同样方式缓存投影块)能让缓存留在契约之外。

后果

一次分发只做常数次索引操作,因此宽 PTC 调用的开关成本与事件数成正比,面板也不必为了显示 12 行而重建整张调用表。每个 Code-Mode Context 最多保留 CALL_LIMIT 条调用、且不再残留已结算的开始时间;运行的真实总量仍记在标量计数与工具计数里。

packages/experimental/client-ui-stats 的既有测试全部通过,发布输出不变。有三处断言随它们所固定的实现一起调整:State 集合改为 Map;调用顺序用例的 fixture 改为按 seq 递增构造,因为旧 fixture 故意让 key 顺序与 seq 相反;Code-Mode Definition 新增了发布节奏声明。包内测试补充了保留上限、已保留分发的就地替换、同节点复发布,以及节点未变时跳过快照重建;该包 17 个源文件的逐文件覆盖率仍为 100%。

诊断不设阈值、不进入任何 CI 通道,所以线性度靠复跑诊断来验证,而不是靠门禁。它覆盖的是单个根调用在冷折叠与 live 追加下的情形;没有覆盖同一 step 内的多个根调用、仍在流中的分发事件、错误结果,以及整段前缀共享文本预算的情况。