Skip to content

fix: index stripKernelSummaries carrier evidence in one pass (#2042) - #2043

Merged
ranxianglei merged 1 commit into
masterfrom
2026-10-04_strip-carrier-single-pass
Oct 4, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-10-04_strip-carrier-single-pass

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Oct 4, 2026 •

Copy link
Copy Markdown
Owner

Closes #2042

What

stripKernelSummaries() (src/server.ts) looked up each active block's summary carrier by rescanning the entire post-kernel history once per block: plain blocks via messages.some(...), plugin-fold blocks via inboundCompressPairPresent() which also re-parsed every compress call's args per block. With B active blocks and N messages the worst case was O(B×N) plus O(B×C) JSON parses (C = compress calls). The shared function sits on all four protocol prepare paths, count_tokens, and the post-compress re-request path.

Fix

Single pass at function entry (buildCarrierIndex) building:

  1. a Set of tool-call ids → plain-block lookup is one Set hit;
  2. the list of compress-named tool calls (presence flag for legacy loose-match blocks);
  3. the parsed startId/endId pair index, built lazily (only when an active plugin block with recorded refs needs exact matching) and parsing each call's args at most once (flat {startId,endId} and {content:[...]} formats).

Block loop then does O(1) lookups; final order-preserving filter unchanged. Cost: O(N+B+R), R = total ranges across compress args, paid only when plugin exact-matching is needed.

Semantics preserved exactly (pinned by tests):

  • no real carrier → fallback summary kept (no unconditional delete);
  • preflight/no-compressCallId blocks skip; only active blocks considered;
  • plugin exact-range match, flat/content formats, bad-JSON fail-safe (anchor kept), legacy no-refs "any compress call present" degradation;
  • plain match still ignores tool name (any tool-call id counts);
  • message order / tool args / wire bytes untouched; both compression modes × four protocols;
  • index is rebuilt from the post-kernel history on every call — no pre-prepare cache reuse (kernel may have pruned earlier pairs).

Behavior changes

None on the wire: output is byte-identical to the old implementation for every input (old-vs-new equivalence pinned per case). Performance-only change:

  • carrier lookup: O(B×N) element touches + O(B×C) arg parses → O(N+B+R) with ≤1 parse per compress call. Deterministic probe (B=1000, N=4000): 4,000,000+ element visits before → 4,001 after.
  • proxy-mode sessions (no plugin blocks): range-pair parsing stays lazy. The call-id Set costs ONE full N pass instead of B short-circuiting .some scans — cheaper for B≥2, tied in the worst case at B=1, and one extra O(N) pass on fresh pre-compression sessions (the old code scanned nothing when no active block existed).

Config surface

Not applicable — no config fields added/renamed/re-semantics'd.

Verification (rebased head b464af4 on master d19a546)

  • npm run typecheck clean; npm run build clean.
  • New tests/issue2042-carrier-index.test.ts (16 cases): carrier hit/miss per branch, flat/content range args, bad JSON, legacy refs, preflight, inactive, non-tool-call decoy, mixed-branch order preservation, parse-once pin, deterministic visit-count probes (no timing thresholds).
  • Targeted suites green: issue2042 + tight-fold-summaries + responses-fold-cache-seam + issue564-responses-ordering + plugin-protocol + cache-friendly-proxy = 44/44.
  • codex e2e: local run blocked in this environment (documented local upstream 127.0.0.1:8199 ECONNREFUSED — endpoint down here, failure occurs before any request processing); ci-e2e.yml auto-runs on this diff (touches src/server.ts) with the real upstream.

@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-04_strip-carrier-single-pass (b464af4)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2082-pr.2043.f9a413e

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr2043.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review started on PR #2043 (head b464af44, base master d19a5468).

Initial checks done:

  • Diff is clean and scoped: single commit, src/server.ts (+73/−22) + new tests/issue2042-carrier-index.test.ts (16 cases). No dangling references to the removed inboundCompressPairPresent helper; all 8 stripKernelSummaries call sites keep the unchanged signature.
  • Local verification on the rebased head: npm run typecheck clean; targeted suites (issue2042 + tight-fold-summaries + responses-fold-cache-seam + issue564-responses-ordering + plugin-protocol + cache-friendly-proxy) = 44/44 pass.
  • CI so far: all e2e lanes green (real codex e2e ✅ — the lane blocked locally ran green on CI, plus registry/image-billing/opencode/pi/dsh lanes, version-guard, build-artifact); the three-platform test jobs were still running when this was written.

Semantic-equivalence audit (old vs new branch by branch) is complete and holds — details in the final review reply once the CI test jobs land.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR review 完成(agent-authored PR,按「拉分支核对代码 + CI 确认 + 报告结论」执行,未重做)。head b464af44(issue 正文写的 d3595772 已过期),base master d19a5468,单提交,diffstat:src/server.ts +73/−22、新增 tests/issue2042-carrier-index.test.ts(189 行)。

结构化七问

  1. 是什么问题:stripKernelSummaries()(src/server.ts,四个协议 prepare 路径 + count_tokens + 压缩后 re-request 共用的热路径)为每个 active block 独立重扫整段 post-kernel 历史找 summary carrier——plain block 走 messages.some(...),plugin-fold block 走 inboundCompressPairPresent() 且每个 block 都重新 JSON.parse 一遍所有 compress call 的 args。B 个 active block × N 条消息 = O(B×N) 元素访问 + O(B×C) 重复解析。
  2. 什么原因导致的:per-block 查找结构本身。每个 block 各自重复做同一份全历史扫描与参数解析——冗余工作的生产者就是「每 block 一次查找」的设计,不是某处低效调用。修法是消除该机制而非在别处拦截症状(疏不堵)✓。issue [Performance] Summary-carrier lookup rescans full history per active block (O(B*N)) #2042 自带确定性函数级探针(B=250/500/1000 → 25万/100万/400万+ 次访问)并直接给出了「入口单次索引」方案;本 PR 1:1 实现该方案,方向正确、打到根因。
  3. 怎么修复的:函数入口单次遍历建索引(buildCarrierIndex):① tool-call id 集合(plain 查找 O(1));② compress-named call 存在性列表(legacy 松散匹配用);③ startId/endId 对索引惰性构建(仅当有带 refs 的 active plugin block 时才解析),每个 call 的 args 至多解析一次(flat 与 content-array 两种格式)。块循环改 O(1) 查找,末尾保序 filter 原样不动。总成本 O(N+B+R),R 仅在需要精确匹配时支付。
  4. 是否有回归问题:无。逐项审过的既有行为及结论:carrier 命中→删 summary(保持);无 carrier→fallback anchor 保留、绝不无条件删(保持);preflight/无 compressCallId 跳过(保持);inactive block 保留(保持);legacy no-refs「任意 compress call 存在即算」含不可解析 args 也算(保持);bad-JSON fail-safe 保留 anchor(保持);plain 匹配忽略工具名、任何 tool-call id 都算(保持);消息顺序/tool args/wire 字节不变(保持);索引每次从 post-kernel 历史重建、无 pre-prepare 缓存复用(保持);两种压缩模式 × 四协议(保持);未顺手收紧 tool-result 校验(issue 明确禁止,未动 ✓)。证据链:本地 typecheck clean + 定向套件 44/44;我另写了独立的 old-vs-new 模糊探针(内联 origin/master 旧实现,20000 组随机 case:bad/null/primitive args、空串 id、非字符串 startId、content 里塞 null、缺 toolCallId、同 id 非 tool-call decoy、孤儿/decoy summary id、loose/exact/ghost refs、active/inactive)——输出逐元素同一引用、同序,零差异;CI 16/16 check runs 全绿,含真实客户端 e2e(codex/dsh/opencode/pi lane)、registry e2e、image billing、ubuntu22/24 + windows22/24 测试矩阵(本地 codex e2e 因本环境 upstream 不可达跑不了,CI 已用真实 upstream 覆盖)。
  5. 是否破坏缓存:不涉及——输出对任意输入逐字节等同旧实现,prefix-cache 行为不变;缓存/重试/增量游标/去重键策略均未触碰。
  6. 是否改了设计方向:不涉及——单个共享 helper 的内部实现替换,签名不变,8 个调用点不受影响;无架构/边界/数据流转向。
  7. 是否引入了新的配置:不涉及——无新 env/config/默认值。

其他审查项

  • 测试有效性:16 case 有牙——语义等价逐分支钉死 + parse-once pin(monkeypatch JSON.parse 计次)+ 确定性 Proxy 访问计数探针(断言 .some 次数==0 且总访问 ≤ 2N+4B;旧代码 B×N ≥ 25 万必炸),无 timing 阈值,回归时抓得住。
  • diff 整洁度:干净。单提交两文件,无跑题改动、无空白 churn、无锁文件连带。
  • 行为变更披露:PR 已按规范披露「wire 零变化、纯性能」,与我的独立验证一致。
  • 小问题直接修(已处置):PR 描述中「proxy-mode sessions pay zero new cost」表述与代码事实不符——Set 构建是恒定一次 O(N) 遍历:B≥2 比旧的 B 次短路扫描便宜、B=1 最坏持平,但全新未压缩会话(无 active block)旧代码一次都不扫、新代码多一遍 O(N)。已直接 PATCH PR 描述为准确的常数因子表述(updated_at 2026-10-04T06:28Z),代码无需改动(该开销相对请求处理可忽略,不值得为它加特殊路径)。
  • 合并顺序提示:fix: prefer usage-grade anchor over anonymous raw-history fallback (#2033) #2036(anonymous-anchor-fallback)改的是 src/server.ts 同一区域(其分支上 inboundCompressPairPresent 在 3424 行 vs 本 PR 3410 行),[PR] refactor: extract prepare*/inject* pipelines into src/server/ (#1440) #1449(prepare-extract 重构)会把 prepare 管线抽到 src/server/prepare-*.ts。两者当前均 mergeable against master,与本 PR 暂无文本冲突;若它们先合入,本 PR 需小幅 rebase(语义冲突风险集中在这一 hunk)。建议三选一排序,避免三方撞车。
  • Auto-merge gate 评估:单模块、回归测试复现原缺陷性质(复杂度探针)、rebased head 全绿、无 config/persistence/wire/跨仓变更、clean diff、引用 issue——基本达标;边界点在 gate feat: persistent + namespaced sessions, multi-tenant session isolation, stream robustness #5(性能重构披 fix: 标签,严格说属 refactor)。判定:可合,merge 留给人。

收工判定:建议合并。 方向正确、根因修复、语义等价经独立模糊验证 + 全套 CI 双重钉死;唯一注意点是与 #2036/#1449 的合并顺序。

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.

[Performance] Summary-carrier lookup rescans full history per active block (O(B*N))

1 participant