Skip to content

Unify ACP tag-echo remediation under four invariants (#2023 #2066 #2190 #2248) - #2267

Merged
ranxianglei merged 19 commits into
masterfrom
2026-10-07_acp-tag-unified
Oct 6, 2026
Merged

ranxianglei merged 19 commits into
masterfrom
2026-10-07_acp-tag-unified

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

为什么

owner 定调(#2229 线程):acp 标签问题族(#2023/#2066/#2190/#2248)统一整治,一个 PR 收编全部三条已审腿;内核可改;四条硬规则:

  1. 思考不能动 —— thinking/reasoning 通道逐字节 verbatim
  2. 模型输出可以动 —— 可见正文通道可剥标签/回声/残骸
  3. 模型输出到文件不能动 —— 工具调用参数逐字节不变([bug] 插件透传会剥掉工具调用参数里的 render tag(arguments 流挂了 tag-echo 过滤器) #1039)
  4. 用户发送的消息不能动 —— 用户片段的逐字回声保留(plugin 模式 openai SSE:混合文本字段 chunk 因单字段纯标签 echo 被整体丢弃(丢失同 chunk 真实输出) #463 豁免)

内容

行为变更披露

验证

Closes #2023
Closes #2066
Closes #2190
Closes #2248
Supersedes #2025 #2067 #2211

ework-agent and others added 17 commits October 4, 2026 11:41
…er (#2023)

The filter only treated a body of EXACTLY ONE bare ref as tag content
(#1720 REF_LIKE), so refs-only text adjacent to render-tag structure was
released as prose: orphan ref+close units, multi-ref/range pair bodies,
EOF tails after an opening, and chunk-boundary splits all leaked mNNNNN
remnants to the client reasoning stream.

- Generalize single-ref to refs-run (REFS_RUN/REFS_ONLY: lists, en/em-dash
  ranges, comma-separated range lists) at every decision point: PAIRED
  body, swallow inner decision, over-budget release, flush EOF branch,
  PARTIAL_TAIL hold (incl. run+tag-fragment composite and bare-m/partial
  ref prefixes so any split position holds toward the unit).
- New orphan unit: a refs run directly against an UNMATCHED close is dead
  markup and is stripped with the close. Whole-text applies it only to
  depth-zero closes (orphanCloseSpans greedy pairing); streaming only when
  no opening is live — so a ref cited inside a prose-wearing pair survives
  (#1720) in both modes.
- The [\w>] lookbehind extends across chunk boundaries via lastEmitted:
  word-like tokens (xm01233) and tag-terminator residue (#644 over-cap
  drop) are never orphan-stripped or held.

Tests: tests/issue2023-refs-residue.test.ts pins every shape whole-text AND
streaming (char-by-char equivalence + explicit chunk splits), plus #1720
protection pins, #644 lossless pins, and documented residual pins.
…ounting (#2023)

Review finding on the #2025 head: the three new PARTIAL_TAIL refs-run
alternatives flow into mayStartRenderTag, which isOrphanMarkupText used as
its render-tag probe. A released tail shaped like a refs run (a genuine
citation held to EOF) was therefore counted as dead markup residue, so a
tool-call-free turn whose only visible output was a bare ref citation read
as degenerate and burned the one-shot retry (#732/#821) — and could end in
the #870 in-band error instead of delivering the citation.

isOrphanMarkupText now probes with TAG_PARTIAL_TAIL (PARTIAL_TAIL's
\x3c-prefixed alternatives only): byte-identical classification to pre-#2023
for every tag-shaped tail, refs-run tails flip to prose. Tagged echoes need
no help from this path — their drop already sets sawStrippedEcho upstream.
mayStartRenderTag stays broad (fast-path gating must keep engaging on
refs-run tails so split units are still stripped).

Pinned: unit classification + a full openai-lane pipe test proving a
bare-citation answer reaches the client with zero refetches; both fail on
the previous head. #870 unclosed-tag retry pin stays green.
Production sessions (ses_efe4cfcbdffe) show the model re-emitting
internal ACP artifacts as assistant prose: mangled render tags
(<acp=1>) and the absorb tool call written as chat-template text
(<parameter=ref>...mNNNN...</parameter><parameter=summary>...</parameter>
</function></function_calls>). Both leaked into client-visible history.

- stripAcpTags: MANGLED_OPEN pattern (name=... form) + fast-path gate
- whole-field emission discriminator: field opens with a tool-call
  open and carries the absorb ref+summary signature (unique to absorb)
- armed only when the shipped request carries the [ACP absorb]
  instruction (proxy: wireBody; plugin: per-request marker)
- echo guard: an emission verbatim echoing a fragment from the
  request is preserved, not dropped (raw + canonical JSON-escaped
  containment)
- streaming tcHold (pending/committed) across chunk boundaries;
  orphan close-tail drop (defensive)
- drop/requestText threaded through all adapters, plugin pipes,
  JSON rewriters and field strippers
- tests/absorb-emission.test.ts: 31 tests over 13 regression
  categories incl. user-requested verbatim emission survival
- nameless-toolcall-observability: session stub gains metadata {}
  (Session.metadata is required)
…+ plugin-passthrough paths (#1960)

PR #2067 added whole-field emission-drop to the prose filters, and the
non-streaming / plugin-passthrough rewriters already applied render-tag +
marker + bili-artifact stripping to signed Anthropic `thinking` and Gemini
`thought` channels. Both can rewrite signature-verified bytes: on replay the
client echoes the modified thinking back with its original signature, which
desyncs and bricks the session (#1960/KDD#10). The loop streaming adapters
already keep these channels verbatim ("prose filters never touch thinking");
this aligns the remaining paths.

- createIdentityStreamFilter(): pass-through filter with honest char accounting
  (keeps the #673 degenerate-turn stats correct) for signed channels.
- pipePluginChatWithStrip: route anthropic/google `thinking` through the
  identity filter instead of the prose-filter stack.
- stripAnthropicText: leave delta.thinking / content[].thinking byte-for-byte
  (the text channel is still stripped).
- rewriteGoogleJsonResponse + stripGoogleChunk: skip `thought` parts.

OpenAI/Responses reasoning is not byte-signature-verified, so it stays stripped
as before.

Behavior change (old -> new): model-emitted render tags / markers / whole-field
emissions inside SIGNED thinking or Gemini thought parts are no longer stripped
on the plugin-passthrough and non-streaming wire paths — they now ride
byte-for-byte, matching the loop adapters. Cosmetic tag echoes may remain inside
thinking panes (accepted per #1881/#1960); replay no longer risks a signature
mismatch.

Tests updated to pin the corrected contract:
- absorb-emission.test.ts (category 8)
- plugin-passthrough-tag-strip-chat.test.ts (anthropic thinking_delta case)
Models imitate ACP render tags with drifted close names (field census:
ap/p/a/apc/cap/ck/div/warn/aph/ambient/apm) that no matcher recognizes.
The stream filter then swallows the open tag, hits its budget or EOF,
and either releases the 'mNNNNN</word>' residue verbatim (mid-stream)
or silently drops the following prose (flush). Whole-text stripAcpTags
had no pair rule for degenerate closes either.

- swallow mode now accepts any short close name as terminator when the
  body since the opening is exactly one bare ref (degenCloseAfterRef);
  wrapped-imitation mode is excluded (payload may hold arbitrary markup)
- stripAcpTags gains DEGEN_PAIR: acplike open + bare ref + any short
  close removed atomically, before the lone passes
- fast path gains mayStartDegenerateRenderTag (broad <word tail <=16) so
  degenerate heads engage the state machine instead of riding through;
  kept out of PARTIAL_TAIL/mayStartRenderTag so #1760 orphan-markup
  accounting still classifies prose tails like 'a<b' as prose
- completion pre-checks gain containsEchoResidue so residue-shaped final
  artifacts go through the whole-text rebuild path
- new warn logs at fast-path forward and filter release points make the
  formerly silent leak observable (#2190 fix suggestion 4)

Behavior change: ref-body spans closed by a degenerate name are now
stripped instead of leaking or evicting adjacent prose; fast path engages
one fragment earlier on ambiguous '<word' tails (bounded delay, no byte
loss). No config surface change; kernel untouched.
…tack compat)

One-line compat for the agreed #2025 -> #2211 -> #2067 merge order: #2025 folds the
master REF_LIKE constant into REF_TOKEN/REFS_RUN, which would leave this call dangling
(silent tsc break once stacked, each PR green alone). Standalone const carries the
identical regex /^\s*m\d{4,}\s*$/; behavior unchanged.
…audit raw-forward exits

- buildAcplikeName: add acacp/accessp/acpx/acb (case-folded, exact enumeration; char-class generalization rejected — FP risk on real words like acgroup/acmap). All name-derived matchers pick them up; 229-session census validated 10/10 leak forms cleared, 170/170 legit-word contexts preserved.

- plugin.ts: log-only residue audits at the unfiltered raw-forward exits (processOpenai E1 no-choices branch + E5 tail fallthrough, chat/responses JSON.parse-fail branches); openaiFrameHasToolCalls excludes argument frames (#1039 byte-exact).

- tests: +4 drift-name cases in issue2190 suite (streaming char-by-char, cross-name pairing, whole-text, gate predicates, legit-word survival); tag-echo.test.ts fixture <acpx> -> <abc> since acpx is now a recognized echo name by design.
… ignore .tmp/

The stacked compat commit accidentally committed node compile-cache binaries and test artifacts under .tmp/ (diff-cleanliness gate). Untrack only; .gitignore gains .tmp/ (it had tmp/ but not .tmp/, which is how they got in).
…es-wire exits

The #2190 exit audit (auditRawForward) covered only the openai E1/E5 and both
parse-fail exits; the isomorphic raw exits named in the #2248 mechanism table
were still silent, leaving holes in the zero-log-leak attribution net. Log-only,
zero byte change (#1039 wire fidelity preserved by construction):

- processAnthropic: content_block_start / non-delta events / unmanaged delta
  types (input_json_delta excluded — tool-call arguments are user intent).
- processGoogle: non-candidates frames + tail fallthrough (frames carrying
  non-text parts excluded via googleFrameHasNonText — functionCall/
  functionResponse are structured or user data).
- responses pipe: unrecognized-type fallthrough (responsesFrameHasToolCalls
  excludes function_call/custom_tool_call argument streams).

Tests: new issue2248 suite pins all ten audited exits (warn fires AND byte-exact
forward) plus the three argument-frame exclusions and two clean negative
controls; also pins the four #2190 sites which shipped without direct tests.
…amination

Open-side census counts are inflated by models quoting tag names inside
their own reasoning. Under the decontaminated standard (block-initial
emission + tokens= attribute + deduped n>=4), only accessp/acacp/acb
qualify; acpx's raw census (~217) proved self-referential/constructed
with one case-variant sighting only. Excluded forms stay observable via
the #2190 residue audits, so a clean attestation earns re-inclusion.
tag-echo.test.ts fixture reverts to its original <acpx> sample, which is
ordinary markup again under the reduced name set.
# Conflicts:
#	src/loop/tag-echo-filter.ts
# Conflicts:
#	src/loop/adapter-anthropic.ts
#	src/loop/adapter-google.ts
#	src/loop/adapter-openai.ts
#	src/loop/adapter-responses.ts
#	src/loop/tag-echo-filter.ts
#	src/plugin.ts
…esidueWarn, absorbInstructed, requestText) signature
#2248)

Fold the three remediation legs (refs-run residue, degen-close strip +
egress audit net, emission drop + signed-thinking verbatim) into one
branch and pin four hard rules over every text-rewriting surface:

1. thinking channel byte-verbatim everywhere — extends #1960 signed
   thinking treatment to openai reasoning_content/reasoning and
   responses reasoning summaries (adapters route them through
   createIdentityStreamFilter; stripOpenaiChatText/stripResponsesText
   leave them untouched). REVERSES the #1881 reasoning-echo strip.
2. model prose remains manageable (tags/markers/residue/emissions).
3. tool-call arguments byte-identical (#1039) — never filtered.
4. user-sent bytes never rewritten (#463 exemption).

Plugin passthrough: reasoning summary deltas/done events forward
verbatim; the argStream strip path is retired (identity semantics).
Docs: TECHNICAL-NOTES en/zh (invariants section), CLIENTS en/zh
(user-facing summary). Pinned by tests/unified-acp-invariants.test.ts
(8 tests, both lanes). Full affected net: 401 pass / 0 fail.
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-07_acp-tag-unified (000dfe8)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2370-pr.2267.ae18ae7

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-pr2267.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(PR #2267,head 2026-10-07_acp-tag-unified)。按 review 规范逐项进行:①原 issue 方向(#2023/#2066/#2190/#2248)→②回归审查(触碰路径既有行为清单)→③代码审查→④测试有效性→⑤diff 整洁度。结论稍后按结构化七问给出。

@ranxianglei ranxianglei added bug Something isn't working p1 labels Oct 6, 2026
…survives (#2267 review)

The absorb field-start release ran process(held) and only then cleared the
shared held buffer. process() itself can stash a PARTIAL_TAIL fragment into
that same buffer when a release chunk ends in a tag-head-shaped tail (e.g. an
absorb-instructed first delta "Check <ac"); the post-clear wiped the stash,
so the next delta arrived tag-less and the head was re-emitted as fresh prose
(tag-echo residue leak). Every other path clears before processing (normal
push, flush tcHold path); mirror that here. Regression test added: red on the
old ordering, green on the fix.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review 完成 —— 结论:可合并(等人工 merge)。审的是最终 head 000dfe84c(19 commits,含 review 期间补推的两个 commit b291d448f/000dfe84c,已单独审过)。

验证面:本地 checkout 全读 L3 unify diff;三腿机制并解决一致性抽查(refs-run / DEGEN_PAIR+degenCloseAfterRef+mayaStartDegenerateRenderTag / emission-drop+MANGLED / auditRawForward+frame 排除,全部在最终树中);既有行为逐项回归清点;定向测试 326 pass / 0 fail(unified-acp-invariants、issue1881×2、issue2023、issue2190、issue2248、absorb-emission/absorb×3、passthrough-tag-strip×3、proxy-noinject、tag-echo、bili-artifact、fix-1760、fix-1039、nameless-toolcall、loop/pck/degenerate-retry 全家族);tsc --noEmit(build+root)rc=0;build 绿;CI 25/25 绿(旧 head d10fc01c9 曾红 1 处,见下)。

Review 期间发现并已处置的两件事:

  1. CI 红(旧 head):tests/fix-1039-toolargs-verbatim.test.ts:241 钉的是 [bug] ACP 标签泄漏:<acp tokens=...> 标签被模型原样复述到回复正文,仅靠系统提示劝阻不足 #1881 旧契约(reasoning summary tag ref "still stripped"),L3 翻转后必挂 —— PR 翻转了 issue1881/passthrough/absorb-emission 三个姊妹文件但漏了这一个。我从 CI 日志独立定位并准备了同一修法,发现作者已推 b291d448f(同位同修),我的重复 commit 已丢弃。
  2. tcHold 缓冲序 bug(000dfe84c,作者 review-response):field-start release 路径先 process(held) 后清共享 held 缓冲,而 process() 会把 PARTIAL_TAIL 暂存回 held → 清后丢失,下一 delta 把标签头当新 prose 放出。改为快照-再清,与 normal-push/flush 两个调用点一致;新增回归测试有牙(旧码下第二 push 会泄漏标签碎片)。判定:根因修复非掩蔽,仅收紧泄漏,无其他行为变化。

结构化七问:

  1. 是什么问题:ACP 标签回声族(tag-echo filter occasionally lets m01233</acp> remnant through to the client reasoning stream (model echoes refs from compress confirmation markers) #2023/Model re-emits internal ACP artifacts into assistant prose (mangled render tags + absorb tool-call text) #2066/【bug】最终版issue: 模型生成的 <acp …> 标签会漏到用户可见输出(附根因、量化与修法建议) #2190/[bug] 出口层缺口:文本不在受管字段的帧绕过 tag-echo 状态机(E1/E5 原始转发出口 + 零日志真泄漏样本) #2248)——模型自产的内部工件(refs-run 残留、退化开/闭名、整段 emission、带外出口)以各种形状漏进客户端可见通道;三条整治腿各自为政,各改写面对"什么可改写"的判断不一致。
  2. 什么原因导致的:缺统一契约 + 历史边界画错——[bug] ACP 标签泄漏:<acp tokens=...> 标签被模型原样复述到回复正文,仅靠系统提示劝阻不足 #1881 把 openai/responses reasoning 当"该剥的 prose",[acp-loop] In-turn compress re-request edits the latest assistant message → Anthropic 400 "thinking blocks … cannot be modified" (session stays broken) #1960/fix: drop model-emitted ACP tool-call emissions from the prose channel #2067 的思考边界按"是否签名载荷"画而非按通道语义画,导致同一思考通道在不同 protocol/lane 待遇分裂。owner [meta] ACP 标签泄漏综合治理:#2023/#2066/#2190 三切面统筹(修复 #2025/#2067/#2211) #2229 按通道语义重划边界是本 PR 的立法依据。
  3. 怎么修复的:四条硬规则统一所有文本改写面(loop 适配器、插件直通、JSON strip 函数、出口审计);四协议思考通道一律走 createIdentityStreamFilter(直通+诚实字符记账,[bug] #644 follow-up (post-#646, v0.1.97): typo'd render-tag echo (<acip …) bypasses the strip regex and passes through as a 43-char degenerate end_turn turn — agent stalls mid-orchestration until manual nudge #673 退化回合检测不受影响),纵深防御 strip 函数同样不动 reasoning 字段;merge commit 保留三腿出处;[bug] 出口层缺口:文本不在受管字段的帧绕过 tag-echo 状态机(E1/E5 原始转发出口 + 零日志真泄漏样本) #2248 保持 log-only 审计网,per-shape 语义扩展明确留 follow-up 等 audit 数据。
  4. 是否有回归问题:逐项清点,无意回归为零。高危模式逐个过:reasoning strip→verbatim 是 owner 定调的有意变更(PR body + docs en/zh 均披露 old→new+原因);retryEmptyTurn/reissue 门禁纯-reasoning turn 从 sawStrippedEcho 抑制转为 sawThinking/sawReasoning 抑制——机制换位、结果等价(plugin.ts:1869/1885/2960/2967/3070 逐点验证);all-echo chunk 不再因 reasoning 在场整块丢弃(content 仍剥)——已披露;fail-fast/阈值/时序/wire 形状无变化;kernel/、config schema、persistence format 零触碰(kernel-guard 绿)。已知代价(设计接受):openai/responses wire 上 echo 形状残骸将到达客户端 reasoning 面板——与 [acp-loop] In-turn compress re-request edits the latest assistant message → Anthropic 400 "thinking blocks … cannot be modified" (session stays broken) #1960 以来 anthropic thinking 同类 cosmetic noise。
  5. 是否破坏缓存:否。前缀缓存稳定前缀不变;会话持久化格式、去重键、增量游标零改动;唯一字节面变化即有意披露的 reasoning verbatim。
  6. 是否改了设计方向:没有转向——本 PR 就是 owner 定调方向([meta] ACP 标签泄漏综合治理:#2023/#2066/#2190 三切面统筹(修复 #2025/#2067/#2211) #2229)的实现本身,做的是统一而非改道。边界澄清:[bug] 出口层缺口:文本不在受管字段的帧绕过 tag-echo 状态机(E1/E5 原始转发出口 + 零日志真泄漏样本) #2248 的"哪个出口泄漏"裁决等 auditRawForward 数据回来,未塞进本 PR。
  7. 是否引入新配置:无。零新 env/config/默认值。

合并后动作:关闭被取代的三腿 PR #2025/#2067/#2211(#2067 外部贡献者 yukhymenko-yurii 的作者身份已在 merge history 中保留,无需替换);四个源 issue 由 Closes 标记自动关闭。

@ranxianglei
ranxianglei merged commit c564b18 into master Oct 6, 2026
25 checks passed
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.81-master.c564b18 (rolling master tag).

npm install -g billion-context@0.0.81-master.c564b18  # 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.

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