Skip to content

[PR] perf: deduplicate captured subagent sessions in linear time - #6668

Merged
ranxianglei merged 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-14_linear-subagent-capture
Nov 14, 2026
Merged

ranxianglei merged 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-14_linear-subagent-capture

Conversation

@Afloat16

Copy link
Copy Markdown
Contributor

Closes #6666

Problem

After an OpenCode fold, each newly captured child-session ID is checked with Array.includes against all IDs already found. Capturing many distinct dispatches is therefore quadratic before the existing 100-ID sidecar limit applies, adding synchronous work to the compression path.

Changes

Replace the growing-array membership checks with an insertion-ordered Set, then return its ordered IDs. The collector still returns every captured ID; the persistence limit remains where it was. No protocol, configuration, kernel or dependency changes.

Add a 5,000-session mixed-carrier regression covering duplicate IDs within and across call/result records, coverage filtering independent of Set input order, and the stored 100-ID prefix. A standalone benchmark exercises the real syncSubagentSessions production hook and validates both its persisted sidecar and the full capture result.

Validation

  • All 8 subagent-session tests pass, including the existing applyRanges integration that checks the real fold, receipt and byte-clean summary.
  • Focused strict TypeScript check, benchmark syntax check, npm run build and git diff --check: pass.
  • The real OpenCode v2 binary E2E and the full CI matrix were not run locally.

Local benchmark

node --import tsx scripts/bench-subagent-capture.mjs 20000

Node v24.22.1, Linux; 20,000 unique subagent sessions represented by 40,000 structured dispatch call/result messages. Three iterations per implementation, timing the production post-fold hook (including covered-set creation, extraction, deduplication and metadata update). Fixture construction is outside the timed interval; the expected 100 persisted IDs are checked after every iteration.

Implementation Iterations (ms) Median (ms)
Unchanged base 1264.7, 1285.6, 1269.8 1269.8
This change 64.9, 56.4, 46.0 56.4

These local measurements describe this long-history hook only. Typical smaller histories have less work to remove, and overall model/compression latency is not measured by this benchmark.

@ranxianglei ranxianglei changed the title perf: deduplicate captured subagent sessions in linear time [PR] perf: deduplicate captured subagent sessions in linear time Nov 14, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重: 不重复 — searched open+closed PRs/issues for captureSubagentSessionIds / subagent capture dedup; the only hit is the source issue #6666 itself. No parallel submission for this change.

Reviewed the diff, checked out the PR head in an isolated worktree, and ran targeted verification. This is a correct, minimal, well-tested perf fix.

Verified live on the PR head (d672592):

  • All 8 subagent-session tests pass, including the new 5000-session regression (first-seen order across carriers, the 100-ID cap, coverage filtering independent of Set input order) and the existing applyRanges fold/receipt/byte-clean integration.
  • A/B of old array vs new Set on 20,000 unique ids: byte-identical output, order-preserving, 898.8 ms → 26.1 ms (~34×). The quadratic baseline is real.
  • The shipped benchmark runs clean (~23 ms here on Node v22; PR reports 56 ms median on v24 vs 1270 ms baseline).
  • Typecheck clean on the PR head (the two initial billion-context-pi-subagents errors were a missing local pi-subagents build, not the PR).
  • Call path confirmed: src/stream.ts:1638 invokes syncSubagentSessions in the applyRanges success path with the full message array — genuinely on the compression hot path.

Regression surface (existing behaviors of captureSubagentSessionIds): all preserved — full capture result in first-seen order (Set is insertion-ordered), dedup (Set.add idempotent), coverage filtering (still iterates messages, not covered), structured-carrier validation (toolName gate / SES_ID_RE / TASK_ENVELOPE_RE untouched), return type string[], and the 100-ID cap (still applied downstream at src/subagent-sessions.ts:99). The persisted sidecar is byte-identical. No semantic drift, no fail-fast→swallow, no threshold/timing/format change.

Diff: 3 files, +40/−4, every line on-purpose (the Set swap, the regression test, the benchmark). No unrelated / whitespace / lockfile churn. perf: is an accepted convention here (3 prior commits).

One note (not a blocker): the PR is 17 commits behind master, but master has not touched any of the 3 files since the base → a clean rebase before merge.


八问(结构化审核结论)

  1. 是什么问题:OpenCode 折叠后,子会话 id 采集用 Array.includes 对不断增长的数组做成员检查,大量不同 dispatch 时呈二次方,把同步开销加到压缩路径上。
  2. 什么原因导致的:out.includes(id) 每次 O(n),D 个不同 id 总成本 O(D²);而 100-id 持久化上限是在 captureSubagentSessionIds 返回之后(下游 syncSubagentSessions)才切的,所以二次方工作发生在完整采集集上,而非仅持久化的 100 个。
  3. 影响范围与严重程度:仅影响 OpenCode 插件模式下、单次折叠含大量不同子 dispatch 的大历史。20,000 个不同 id 的折叠约增加 ~1.2s 同步工作(本地 A/B ~0.9s)。p2——特定条件下的性能退化,不是正确性缺陷;持久化结果字节相同,只是计算它的时间不同。
  4. 怎么修复的:把增长数组 + includes 换成按插入顺序的 Set<string>;Set.add O(1) 且幂等(天然去重),[...ids] 保首见顺序。采集器仍返回全部 id,100-id 切片仍在下游 syncSubagentSessions 不动。
  5. 是否有回归问题:无。逐项核过——首见顺序(Set 插入序,A/B 字节相同)、去重(幂等)、覆盖过滤(仍遍历 messages 非 covered)、结构化载体校验(toolName/两个正则未动)、返回类型 string[]、100-id 上限(下游未动)。无高危模式(无语义漂移、无 fail-fast 变吞错、无阈值/时序/输出格式变化)。
  6. 是否破坏缓存:不涉及。这是元数据 sidecar 计算,非 wire/缓存路径;持久化 sidecar 内容(首 100、首见序)字节不变,无缓存失效。
  7. 是否改了设计方向:不涉及。同一机制、同一载体、同一上限、同一顺序,只换了去重用的数据结构。
  8. 是否引入了新的配置:不涉及。无新 env/config/默认值。

判定:可合(mergeable)。 正确、最小、测试充分,无回归。合并前建议 rebase 到当前 master(无文件冲突,干净)。

@ranxianglei
ranxianglei merged commit 33c2f61 into ranxianglei:master Nov 14, 2026
89 of 104 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Subagent-session capture rescans earlier IDs for every dispatch

2 participants