Skip to content

[PR] fix: streaming orphan-ref drop no longer depends on SSE chunk boundary (#4179) - #4181

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-10-21_orphan-ref-close-mid-run-rescan
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-10-21_orphan-ref-close-mid-run-rescan

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What / why (one line)

The streaming tag-echo filter sometimes failed to strip an echoed render-tag unit because where the SSE stream was split decided whether the drop happened — the same logical model output persisted differently across retries, opening a prefix-cache seam (#3198 contract). This makes that drop independent of the chunk boundary.

Symptom

Input like textm1111–m2222</acp> (prose glued to a multi-ref run + orphan close):

  • whole-buffer push → textm1111– (drops m2222</acp>)
  • split right after prose (text | m1111–m2222</acp>) → textm1111–m2222 (m2222 survives)

Same text, different chunking → different bytes. Whole-text stripAcpTags always dropped m2222; streaming under-deleted on certain boundaries.

Root cause

src/loop/tag-echo-filter.ts, ORPHAN_REF_CLOSE handling (~line 1139). When the leftmost match sits at the buffer head and its run-head is word/tag-adjacent across the chunk boundary (lastEmitted ∈ [\w>], the #644 cross-chunk lookbehind extension), the code voided the whole match and stopped. But REFS_RUN may begin at any ref (joiners aren't \w), so a legitimate mid-run start (m2222, preceded by –) sat inside the voided span and was never retried — the deletion got diluted by the chunk boundary. Whole-text has the full string, so its regex engine naturally skips the word-adjacent head and finds the mid-run start; streaming lost it.

Fix

After voiding the head match, re-scan from just past it (lastIndex = match.index + 1) so the valid mid-run start is found instead of abandoned. Any match at index > 0 already passes the lookbehind gate by construction, so one re-scan suffices. ~8 lines inside the matcher; no other code path touched.

Behavior change (intended)

For prose word-glued to a multi-ref run + orphan close:

Both compression modes

The filter runs on model prose (content/reasoning/text fields) in both plugin mode and proxy mode; the change is internal to the shared createTagEchoFilter.process() matcher and is mode-agnostic. It does not touch tool-call arguments (#1039 byte-exact), signed thinking/reasoning channels (KDD #10), config, persistence format, or any wire shape beyond the existing prose-strip contract.

Duplicate screening

Not a duplicate. Same family as #4162 (chunk-split determinism, #3198) but opposite direction and different mechanism: #4162 = over-deletion via PARTIAL_TAIL's missing cross-chunk lookbehind (awaiting owner A/B decision, no PR yet); #4179 = under-deletion via ORPHAN_REF_CLOSE not re-scanning after a voided head. Different lines, orthogonal fixes; shipped independently because #4179's target is pinned by whole-text parity and needs no decision. Together they fully close the family.

Tests

  • New dedicated every-split test: streaming filter matches stripAcpTags for a word-glued multi-ref orphan run at every split position (#4179) — enumerates every single-point split for en-dash/comma/hyphen joiners, a 3-ref deep case, a space-prefixed control, and a single-ref negative control; plus explicit semantic pins. Verified it fails on the unfixed source and passes with the fix.
  • Fuzz corpus entry added to tests/tag-echo-chunking-determinism.test.ts.
  • Targeted suites green: tag-echo, tag-echo-chunking-determinism, e2e-tag-echo, issue2023-refs-residue, issue2190-degen-close, issue2756-degenerate-gate, loop-degenerate-retry (existing word-char protection pins untouched). Typecheck clean. Full-suite verdict via CI on the pushed head.

Fixes #4179

Related: #4162 (opposite direction, same family) · #2023 (orphan unit) · #644 (cross-chunk lookbehind) · #3198 (chunk-split determinism contract)

#4179)

The streaming tag-echo filter voided the leftmost ORPHAN_REF_CLOSE match when
its run head was word/tag-adjacent across a chunk boundary (#644), then
abandoned the run entirely. Because REFS_RUN may begin at any ref (joiners are
not word chars), a valid mid-run start (a ref preceded by a joiner) could sit
inside the voided span and was lost — so whether the unit was dropped depended
on where the SSE chunk boundary fell: under-deletion vs whole-text, breaking
the chunk-split determinism contract (#3198).

Re-scan from just past the voided head so that mid-run start is found instead
of abandoned; any match at index > 0 already passes the lookbehind gate by
construction, so one re-scan suffices. Streaming now matches stripAcpTags for
this shape at every split position.

Behavior change (intended): for prose word-glued to a multi-ref run + orphan
close, streaming previously kept the tail ref on some chunkings; it now drops
the joiner+tail-ref+close as one unit, matching whole-text in all chunkings.

Regression pins: dedicated every-split test + fuzz corpus entry.
@ranxianglei ranxianglei changed the title fix: streaming orphan-ref drop no longer depends on SSE chunk boundary (#4179) [PR] fix: streaming orphan-ref drop no longer depends on SSE chunk boundary (#4179) Oct 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-21_orphan-ref-close-mid-run-rescan (ce324a6)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.4977-pr.4181.28e33ad

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-pr4181.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

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

查重: 不重复。 本 PR 是 #4179 的修复(方向 a)。与同族 #4162(PARTIAL_TAIL 缺跨块 lookbehind → 过删除)机制不同:本条是 ORPHAN_REF_CLOSE 头部 match 被词相邻判定 void 后未从 match.index+1 重扫、中 run 起点丢失 → 欠删除。两者互不覆盖(#4162 修了 PARTIAL_TAIL 本条仍在),各自独立成立。

标签: bug / p1 / scope/wiring / pr(与源 issue #4179、同族 #4162 已打的标签对齐;p1 按 owner 2026-10-19 修订——chunk 边界决定字节 = 前缀缓存接缝类)。

八问

  1. 是什么问题:流式 prose 过滤器对「散文紧贴多 ref 孤儿 run + 孤儿 close」(textm1111–m2222</acp>)的删除结果取决于 SSE 切块位置——整块推删 m2222,在 text 后切开则 m2222 存活。同一逻辑文本两种字节输出,违反 A/B experiment: ACP_RENDER_NONE=1 (renderTags=none) eliminates dsh render seams — hit rate 85.6% -> 91.2% #3198 chunk-split determinism 契约。
  2. 什么原因:REFS_RUN 可从 run 内任意 ref 起配(joiner 非 \w);切块落在散文与 run 之间时,buffer 头部左起 match 被跨块 lookbehind 扩展(void 条件 pos===0 ⇒ index===0,charBeforeRun 拿 lastEmitted='t' 判词相邻)置空,代码没有从 match.index+1 继续 exec,合法的中 run 起点(m2222,前缀 –)被放弃,只剩 LONE_CLOSE 兜底 → 欠删。整文路径 stripAcpTags 用同形正则找左起合法起点,永远删对——流式与整文在此分叉。
  3. 影响范围与严重程度:触发条件=多 ref run 紧贴词字符 + 孤儿 close + 切块恰好落在两者之间(概率性,取决于网络分片)。不丢数据、不断会话;后果是前缀缓存接缝(重试/重放同段文本出现两种字节)→ 缓存稳定类,p1 口径(与 tag-echo streaming filter: when a quoted string + truncated closing tag spans a chunk boundary, the discard decision dri | tag-echo 流式过滤器:引用串+截断闭合标签跨 chunk 边界时,丢弃决策随分块漂移 #4162/tag-echo filter: orphan removal for mid-string quoted references vanishes after chunking — same text, different chunking | tag-echo 过滤器:引号串中段引用的孤儿删除在分块后消失——同文本不同分块,drop 被分块『稀释』(方向与 #4162 相反) #4179 一致)。
  4. 怎么修复的:方向 (a)——头部 match 被 void 后以 lastIndex=r.index+1 重扫一次。一次重扫即够的论证我独立验证过:void 只可能发生在 index===0;重扫所得任何 match 其 pos≥1,charBeforeRun 改用 buffer 内真实前字符,而正则自带 (?<![\w>]) 保证该字符 ∉[\w>] → 不可能再被 void,无需循环。流式与整文都收敛到「左起合法起点」,parity 是构造性的。
  5. 是否有回归(逐项审过既有行为):① [bug] #361 residual, form 3: turn whose entire text is a render-tag echo strips to an EMPTY message — agent records a marker-only turn, stalls until manual nudge (v0.1.92, post-#365) #644 词相邻保护——单词紧贴单 ref(textm00124</acp>)仍保 ref 只死 close,负向控制钉住;无 joiner 连写(textm1111m2222</acp>)无合法中点、两 ref 均保留,与整文一致;空格前缀行为不变。② 兜底路径(LONE_CLOSE 【bug】最终版issue: 模型生成的 <acp …> 标签会漏到用户可见输出(附根因、量化与修法建议) #2190 / DEGEN_PAIR / MANGLED_OPEN):仅当 r 由 null 变为有效 mid-run match 时优先级改变(候选竞争按最早 index,mid-run r 早于 close c)——正是 PR 披露的预期变更(旧:某些切法尾部 ref 存活;新:joiner+尾 ref+close 整体删除,逐 split 与整文一致)。③ stripDeadRefCloses(src/loop/tag-echo-filter.ts:1022-1035)有同样的「void 后 continue 不重扫」形态,但其 DEAD_REF_CLOSE(:223)只匹配单 ref、span 内不存在可丢失的中 run 起点——同模式不同缺陷,不受影响;全文件未见其他实例。④ 工具调用参数通道、thinking 签名通道:不涉及。
  6. 是否破坏缓存:不破坏,此修复本身就是消除一种缓存接缝来源。
  7. 是否改了设计方向:否。共享 createTagEchoFilter.process(),plugin/proxy 双模式同享,mode-agnostic;无架构/数据流/wire 形状变化。
  8. 是否引入新配置:无。

测试有效性(真测试核验)

CI 状态(关键判定)

head ce324a6:除 test (windows-latest, 22, 1) 外全绿(static-gates、kernel-guard、config-surface-guard、其余四个测试分片等)。唯一红 = hermes-native(python 子进程工具注册,tools=[] vs 期望三件,~22s)。该红非本 PR 引入,证据链:① PR 未触碰任何 hermes 文件(hermes 文件与分支基点逐字节相同);② 同一 suite 在同一 windows shard 1、分支基点 56a0f5a 的运行里是绿的(ok 988,2026-10-21T01:46Z);③ 分支基点上五个测试分片当时全红、当前 master ci.yml 运行同样红——存量红风暴(#4338 open,修复 #4350/#4378 在途),新签名观测已补到 #4338。

处置建议与判定

代码层面审核通过:机制正确、一次重扫论证成立、测试有牙(红绿双向实测)、无回归、diff 干净。本次未 rebase:三个触碰文件自基点以来零漂移(已核实 diff 为空),现在 rebase 只会对着一套红的管道烧一轮矩阵。最终 green 待:① #4378(或 #4350)合入、master 转绿;② rebase 到 master 重跑 CI。两步完成后请 owner 合并。

判定:代码可合(mergeable,behind);等红风暴清理后 rebase + rerun 拿最终 green 再合。

@ranxianglei

Copy link
Copy Markdown
Owner Author

(ai: zhipuai-lb/glm-5.3)

已并入合并列车 #4525(master 6d7c587,合并方式 merge commit,提交保留原作者署名)。本 PR 关单收尾;若列车中对应提交出问题,回滚点为 #4525 单提交 revert。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working p1 scope/wiring

Projects

None yet

1 participant