Skip to content

[PR] fix: three performance and ID alignment issues (#5898 review fixes) | fix: 三处性能与 id 对齐问题(#5898 评审修复) - #5902

Merged
ranxianglei merged 3 commits into
masterfrom
2026-11-08_5898-review-p2-fixes
Nov 7, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
2026-11-08_5898-review-p2-fixes

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

这是什么

对 #5898(v0.1.212 → v0.1.213 大 diff)评审发现的三个问题,每个一个独立 commit,都带回归测试:

  1. 热路径上白付的一次大 JSON 扫描([bug][dsh] 429-hold + host retry ladder re-upload 51.2 MiB per retry (36/37 attempts failed) | 429-hold 与宿主重试叠加:每次重试重传 51.2 MiB,单步放大 2-5 倍 #5795 早退判定)

    • 现象:每个经过代理的 POST 请求都会把整个请求体解析一遍来找 model 字段,但早退限流判定在请求体小于 1 MiB 时根本不会用到这个值——而绝大多数请求体都小于 1 MiB,等于 99% 的请求白白付了 O(请求体大小) 的解析成本。
    • 修复:src/server/handle.ts 不再预先解析,改传一个惰性求值函数;src/server/relay.ts 的 tryAnswerEarlyHold 只在协议已知、字节数过门槛之后才真正调用它。
    • 验证:新增测试用 requestModelScanCountForTest() 钉住——过门槛的 body 扫描 1 次、低于门槛 0 次、协议未知 0 次;旧版直接传字符串的调用方式行为完全不变。
  2. 渠道 id 对齐的两个漏洞,会把 id 贴错到错误的行上([PR] chore: empty PR — trigger ci:full on master head #5865 渠道收获适配器,默认关闭的灰度通道)

    • 现象:把客户端 msgid 对齐到存储行时,原来只抽查最后 4 个位置的角色(user/assistant)。在 user/assistant 交替的正常会话里,一个"偶数行的错位"(比如客户端在请求发出后又追加了一行)会让所有抽查的角色都匹配,检查通过、但 id 贴错了行。另外存储侧发生压缩重写(行被移动)时也没有任何跨轮次的校验。
    • 修复:① 角色检查改为覆盖两侧都有角色的全部位置;② 每个会话记住上次贴过的 id 和当时的行数,下次对齐时重叠行上的 id 必须与上次一致,不一致就本轮放弃对齐(退化成修复前的行为,绝不会把别的会话的 id 错贴过来——id 从不复用)。
    • 验证:5 个新测试覆盖偶数错位、窗口外的角色翻转、压缩重写移动已固定行、纯追加正常路径、按会话记忆与超限淘汰。
  3. 0.1.212: per-turn ingest is still O(history) — 96.5% of proxy CPU in StatementSync.run, event loop starves with 8 concurrent sessions (#4528 / #5469 follow-up) #5744 重构丢掉的一个 O(1) 集合,每次入库都变成 O(N×M)

能不能合并

  • 三个改动都最小化、各自独立 commit,任何一个是坏的回退不影响其他两个。
  • 本地全量测试(干净配置):与本分支相同测试在纯 master 上的失败集合完全一致(全部是本机网络/环境噪音),本分支 0 新增失败;目标测试 37/37 通过;tsc --noEmit 干净。
  • 未触碰 kernel/ 与 golden 账本;不涉及内核变更。
  • 建议先合并本 PR,再在 [PR] release v0.1.213 #5898 上 rebase/重跑,使两个 PR 都绿。

…pter

The tail-alignment check that binds client msgids to store rows had two
loopholes, both of which let an even row shift pass undetected:

1. It role-checked only the last four positions where both sides expose a
   role. In an alternating user/assistant transcript an even shift
   preserves every role check, including those four. Now the full role
   sequence is checked wherever both sides have a role.

2. Nothing compared ids across turns. A store-side rewrite that moved a
   pinned row (compaction) could still hand its new ids to the client's
   rows. Each session now remembers the ids it last stamped plus the
   store row count, and the next alignment must agree with that record
   on every overlapping row. Append-only stores pass trivially; a moved
   row degrades the turn instead of misbinding (cost: that turn's
   pinning is skipped — the pre-fix residual behavior, which cannot leak
   ids across sessions because ids are never reused).

Header doc no longer claims misbinding is impossible: it now states the
cross-session guarantee (id reuse would be required) and the residual
pure-even-late-append case, which no role observation can decide and
which the continuity check bounds to pin drift.

Default-off rollout lane (identityChannelHarvest 0%); tests cover the
new even-shift, mid-window role-flip, and rewrite-moved-row paths plus
the per-session state/eviction behavior.
…fire

tryAnswerEarlyHold received a pre-parsed request model, so handle.ts ran
requestModelFromBody — a full-document O(body) JSON walk — on every
proxied POST. The early-hold gate itself bails for bodies under 1 MiB
before the model is ever consulted, so sub-MiB traffic paid for a value
that is discarded.

The call site now passes a supplier; relay resolves it only after the
protocol and byte-size thresholds clear. Sub-threshold and
unknown-protocol bodies are never scanned (pinned by
requestModelScanCountForTest: 0 scans below the gate, 1 at it).
Plain-string callers keep the exact old behavior.
The #5744 rewrite compared every stored block against the incoming
blocks with Array.some, making the stale sweep O(stored x incoming) per
ingest; the pre-#5744 code had a Set for exactly this. Both the SQLite
and MySQL engines get the Set back (behavior identical, membership only
changed from a linear scan to O(1)).
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-11-08_5898-review-p2-fixes (8c6f07a)

Latest build — install this exact version

npm install -g billion-context@0.0.8555-pr.5902.285201b

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

Recent builds (commit → version)

commit version build
8c6f07a billion-context@0.0.8555-pr.5902.285201b build

Download artifact

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

Install instructions are refreshed on each push.

@ranxianglei ranxianglei changed the title fix: 三处性能与 id 对齐问题(#5898 评审修复) [PR] fix: three performance and ID alignment issues (#5898 review fixes) | fix: 三处性能与 id 对齐问题(#5898 评审修复) Nov 7, 2026
@ranxianglei ranxianglei added bug Something isn't working p2 labels Nov 7, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

查重: 不重复。 无其他在途 PR 修复同样这三处。#5907 是同一 #5898 评审的另一批发现(429 throttle 饱和/作用域、media-refs 携带过滤、oversize 拒绝文案、doc typo)——主题不同、互补不冲突;本 PR 只覆盖性能/id 对齐这三处。

结论先行:可合(建议合并)

三个 commit 各自独立、最小化、互不影响回退。我逐个做了代码级回归审查,未发现回归;CI 在分支头 8c6f07ab0c 全绿(30 checks,0 failed / 0 pending,含与本 PR 直接相关的 mysql-contract)。mergeable_state=blocked 是分支保护等人工审批,不是 CI 红。已给本 PR(GitHub #5902)打上 bug + p2。

下面按三处分别给判断,再给结构化八问。

① #5795 热路径白付的大 JSON 扫描 —— 干净,可合

  • 根因:handle.ts 对每个经代理的 POST 都预先跑一次全量 body 扫描找 model(requestModelFromBody,O(body));但早退限流判定对 <1 MiB 的 body 根本用不到这个值——绝大多数请求都 <1 MiB,等于白付 O(body)。
  • 修法:预解析改成惰性 supplier;relay.ts 的 tryAnswerEarlyHold 只在「协议已知 + 字节过门槛」之后才真正调用它。
  • 回归判断:安全。requestModelFromBody 返回对给定字节是确定性的,它的两个副作用(scan 计数 + WeakMap memo)都是纯增量、不改返回值;tryAnswerEarlyHold 唯一调用点就是 handle.ts 那一处。拒绝决策逐字节不变,只是子门槛/未知协议请求少付一次扫描。测试用本地 scan 计数器钉死:子门槛 0 次、未知协议 0 次、过门槛且命中 hold 恰好 1 次。

② #5865 id 对齐两漏洞 —— 正确、测得扎实,默认关闭零影响

  • 根因:把客户端 msgid 对齐到存储行时,(a) 只抽查末尾 4 个位置的角色,交替会话里「偶数行错位」会让所有抽查角色都对上、检查通过但 id 贴错行;(b) 存储侧压缩重写移动了已固定行时没有任何跨轮校验。
  • 修法:(a) 角色检查扩到两侧都有角色的全部位置;(b) 每个会话记住上次贴过的 id + 当时行数,下次对齐时重叠行的 id 必须与上次一致,不一致就本轮放弃对齐(退化到修复前行为)。
  • 回归判断:安全且更保守——两处改动都只会让「本可错绑」的情形退化为「本轮不贴」,绝不会把别会话的 id 错贴过来(id 从不复用);索引数学我核对过,重叠区间边界及收缩/增长/移位各情形都在界内。整条 harvest 路径被 identityChannelHarvest(默认 0%)+ env 双门控住,默认安装下这段代码根本不执行 → 对外零影响。header 注释也诚实地把「不可能错绑」的过度声明改成了正确的跨会话保证 + 残留说明。5 个新测试覆盖偶数错位、窗口外角色翻转、移动已固定行、纯追加正常路径、按会话记忆+超限淘汰,正负方向都有断言。
  • 一个小观察(非阻塞):lastAligned 的淘汰是「首次插入序 FIFO」而非真 LRU(JS Map.set 对已存在 key 保留原位置)——长期活跃会话的条目理论上可能在活跃期被淘汰;后果只是最多一轮 pin drift 后重新武装(安全、文档已说清)。可选优化:delete+re-set 变 LRU。不值得为此挡合并。

③ #5744 丢掉的 O(1) 集合 —— 铁板一块,可合

结构化八问

  1. 是什么问题:三处独立缺陷——每次代理 POST 白付一次大 body 解析(性能)、id 对齐在偶数错位/存储重写时会把 msgid 贴错行(正确性)、存储入库退化回 O(N×M)(性能)。
  2. 什么原因导致的:①早退判定不需要 model 却预先解析;②对齐校验抽查面太窄(仅末尾 4 位角色)+ 缺跨轮连续性校验;③0.1.212: per-turn ingest is still O(history) — 96.5% of proxy CPU in StatementSync.run, event loop starves with 8 concurrent sessions (#4528 / #5469 follow-up) #5744 重构把 O(1) Set 换成了线性查找。
  3. 影响范围与严重程度:①所有经代理 POST(热路径),纯性能成本、无功能损坏;p2。②仅在默认关闭的 identityChannelHarvest 灰度车道生效,当前用户零影响,属潜在正确性洞;p2。③统一存储入库路径,规模化下的成本问题;p2。三处均非 p1(无可用性中断/数据丢失/缓存泄露)。
  4. 怎么修复的:①惰性求值,只在判定真正需要时才扫;②角色检查全覆盖 + 按会话记住上次贴过的 id 做跨轮一致性门(不一致即本轮弃权);③把 O(1) Set 加回两个引擎。
  5. 是否有回归问题:无。逐项过既有行为——①拒绝决策逐字节不变、唯一调用点、副作用纯增量;②只会更保守地降级、绝不跨会话错绑、默认关;③成员判断结果等价。高危模式(语义漂移 / fail-fast 变吞错 / 阈值·时序·输出格式变化)均未触及。
  6. 是否破坏缓存:不涉及。三处都不碰前缀缓存接缝、cache ref、增量游标或去重键。(②的 pin drift 只影响该灰度车道当轮的 id 绑定,不涉及缓存。)
  7. 是否改了设计方向:不涉及。①③是纯性能等价改写;②是在既有身份信道的既有边界内收紧校验,未转向。
  8. 是否引入新的配置:不涉及。无新 env/config/默认值;②复用的是既有 identityChannelHarvest 灰度键(仍默认关)。

交付状态:PR 已打 bug + p2;CI 全绿;mergeable_state=blocked 待人工审批(Problem ② 触及身份/session 绑定逻辑,按 §7.4 属人工层,不走自动合并)。建议合并,再按你 PR 里的计划在 #5898 上 rebase/重跑使两者都绿。

(说明:本地无 node_modules,未重复本地跑测试——全量裁决以远程 CI 为准,我做的是代码级回归审查 + CI 核实;若你要我装依赖定向复跑某一条,我可以补。)

@ranxianglei
ranxianglei merged commit 0ca35ee into master Nov 7, 2026
90 of 129 checks passed
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1077-master.0ca35ee (rolling master tag).

npm install -g billion-context@0.0.1077-master.0ca35ee  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Louie0806 added a commit to Louie0806/billion-context that referenced this pull request Nov 7, 2026
…es and the v0.1.213 release bump

Upstream delta cda6e73..133f352 (13 files, 6 commits):
- 8bf5317 fix: ranxianglei#5795 pay the early-hold body-model scan only when the gate can fire
  (tryEarlyHold now accepts `requestModel?: string | (() => string | undefined)`)
- 8c6f07a fix: ranxianglei#5744 rebuild the O(1) incoming-block-id set the ingest diff lost
- d29c73c fix: ranxianglei#5865 close the id-misalignment holes in the channel-harvest adapter
- 98cd8a1 release v0.1.213

Overlap with the lane footprint was src/server/relay.ts and src/storage/mysql.ts; both hunks are disjoint from the lane's, and the merge was
automatic. Verified after the merge: the lane's non-streaming egress is still at
src/server/relay.ts:2070, MYSQL_SCHEMA_VERSION is still
exported at src/storage/mysql.ts:77 with 6 consumers in the ranxianglei#4666 test, and the
upstream requestModel supplier branch plus the incomingBlockIds Set are both present.

Gates: tsc 0, four guards 0, gen-config-docs fixed point.
Tests: issue5175 58/0, tag-echo 79/0, issue2190 32/0, golden-storage-render 5/0,
storage-engine-proofs 12/0, issue4666 2/0, native-compact-terminal 1/0,
issue5744-ingest-diff 5/0, issue5865-model-scan-memo 4/0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant