diff --git a/.gitignore b/.gitignore
index fb4a6ff38a..7e2336bcda 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@ dist/
.bili-coop-build-stamp
tmp/
tests/.pp-stamp-state-*/
+.tmp/
diff --git a/CLIENTS.md b/CLIENTS.md
index 9b3d830a5f..7411a09274 100644
--- a/CLIENTS.md
+++ b/CLIENTS.md
@@ -249,6 +249,12 @@ two small node scripts that do the work around the client:
against the live proxy manifest, the routed entries gain
`headers["x-bili-plugin"] = "zcode"` — until then traffic rides wire mode.
Tool calls bind via the per-call `conversation_id` argument (#760).
+- **What gets cleaned vs. never touched (unified ACP invariants):** model
+ prose may have bili render-tag echoes / marker lines / degenerate residue
+ stripped in transit; the **thinking channel** (anthropic `thinking`, google
+ `thought`, openai `reasoning_content`/`reasoning`, responses reasoning
+ summaries), **tool-call arguments** (model-written file content) and
+ **user messages** are byte-for-byte untouched.
- **Routing scope (#1622):** native mode wraps **every** provider entry with
a usable http(s) `baseURL` — the same "all providers ride compression"
semantics as the in-process natives (pi/dsh) — not just the bigmodel
diff --git a/CLIENTS.zh-CN.md b/CLIENTS.zh-CN.md
index af02512878..06f9058bc7 100644
--- a/CLIENTS.zh-CN.md
+++ b/CLIENTS.zh-CN.md
@@ -95,6 +95,7 @@ fork 继承同一面;Antigravity 自带用户插件系统 —— `plugins/
- **卸载后的已开会话(#2155):**`bili plugin remove zcode` 无法触及已开着的客户端窗口 —— 它们的 MCP 子进程已死,但代理侧的会话绑定(session metadata)仍在。这类僵尸会话在连续 5 轮“有 nudge、无插件头、线上无 bili 工具、零压缩”后被检测到,随即**自愈降级为代理模式** —— ACP 工具改由代理线上回注、压缩重新由代理接管(nudge 保留)。若线上注入不可用(`compress.injectTool=false`)则改为静默 nudge,直到出现一次压缩缩减自动解除。插件头重现(客户端重启/重装)即在同一请求内恢复 plugin 模式。remove 命令在近 10 分钟内见过活跃会话时会打印提示;web UI 会为这类会话挂“自愈”徽标。
- **每会话自举:** 每个 ZCode 会话把 MCP 子进程作为直接子进程拉起;启动时附着到健康代理(`BILLION_CONTEXT_PROXY`)或在临时端口自拉起,然后在 mkdir 锁文件下对生效的 provider store 做幂等 JSON 手术:每个可路由 provider 条目的 `baseURL` 变为 `http://127.0.0.1:/bili/<上游>`(你设的自定义 baseURL 原样保留在包装之内)。两代 store 都处理:legacy `~/.zcode/v2/config.json`(`provider..options.baseURL`)与 v3.14+ personal store `~/.zcode/v2/provider_config.json`(`config.providerConfigRules.providerRules[].config.api.baseUrl`)—— 两者并存时以新 store 为准(其中哪些条目路由、哪些被跳过见下方路由范围)。原始文件按每次用户编辑快照到 `.bili-bak`(快照永远反映你最后一次真实状态,绝不记录 bili 自己的写入);其余所有键逐字节保留。旧世代客户端在启动时加载 provider 配置 —— 安装后重启一次 ZCode;新版构建可在会话中途感知路由变化(约 1 s 轮询)。`SessionStart` hook 机会性地跑同一套自举(仅附着 —— 绝不 spawn);它的非阻塞竞态被设计为可容忍:第一轮可以走 wire 模式,不变量是 `baseURL` 永不指向死端口。
- **Plugin 模式盖章:** MCP 子进程对着存活代理清单核验 ACP 工具列表之后,才给路由条目加 `headers["x-bili-plugin"] = "zcode"` —— 此前流量走 wire 模式。工具调用经每次调用的 `conversation_id` 参数绑定(#760)。
+- **清洗什么、绝不动什么(统一 ACP 不变量):** 模型正文中 bili 渲染标签回声 / marker 行 / 退化残骸可在传输中被剥离;**思考通道**(anthropic `thinking`、google `thought`、openai `reasoning_content`/`reasoning`、responses reasoning summaries)、**工具调用参数**(模型写往文件的内容)与**用户消息**逐字节原样透传。
- **路由范围(#1622):** 原生模式包装**每一个**有可用 http(s) `baseURL` 的 provider 条目 —— 与进程内原生(pi/dsh)的「所有 provider 都吃压缩」语义一致 —— 而不是只包 bigmodel coding-plan 账号。无法安全包装的条目会被跳过并记录原因,而非静默丢弃:
- **客户端签名账号(#1621):** v3.14+ personal store 上的 coding-plan 账号保持直连(见已知局限);其余 provider 照常路由。
- **环回目标(#809):** http 环回 `baseURL`(localhost / 127.x.x.x / ::1)永不二次代理 —— 包装它会把 bili 叠在自己或你自己的本地中继上。
diff --git a/TECHNICAL-NOTES.md b/TECHNICAL-NOTES.md
index 55b3798c4c..376ca421a4 100644
--- a/TECHNICAL-NOTES.md
+++ b/TECHNICAL-NOTES.md
@@ -342,3 +342,36 @@ transition logs one `[self-heal]` line (detect + recovery). `bili plugin
remove ` best-effort queries the live proxy (instance file →
`GET /__bili/sessions`, 2.5 s timeout, silent on failure) and prints a note
when sessions of that client were active within the last **10 minutes**.
+
+## Unified ACP tag-echo remediation — the four invariants (#2023/#2066/#2190/#2248)
+
+One PR folds the three remediation legs together (refs-run residue #2023,
+degenerate-close stripping + egress audit net #2190, emission drop +
+signed-thinking verbatim #2066) under a single set of hard rules that govern
+EVERY text-rewriting surface (loop adapters, plugin passthrough, JSON strip
+functions, exit audits):
+
+1. **The thinking channel is byte-verbatim.** Anthropic `thinking`, Google
+ `thought`, OpenAI `reasoning_content`/`reasoning`, and Responses reasoning
+ summaries ride as-is — echo-shaped reasoning included. Rationale: reasoning
+ replay (DeepSeek-style) and signature verification validate these bytes;
+ any rewrite desyncs them and bricks the session (#1960/KDD#10). The
+ adapters route these fields through `createIdentityStreamFilter`, and the
+ strip functions (`stripOpenaiChatText`, `stripResponsesText`) leave them
+ untouched (defense in depth). NOTE: this REVERSES the pre-#1881-era
+ reasoning-echo strip — the thinking channel is no longer cleaned.
+2. **Model prose is manageable.** Render-tag echoes, marker lines,
+ bili-internal text, degenerate open/close residue, and (when
+ absorb-instructed) whole tool-call emissions are stripped from the visible
+ text channel only.
+3. **Model output bound for files is byte-identical (#1039).** Tool-call
+ arguments never enter any filter, whatever bytes they carry.
+4. **User-sent bytes are never rewritten.** A verbatim echo of the user's own
+ fragment survives even when stripping is armed (#463 exemption, keyed on
+ the request text, not shape).
+
+The egress audit net (#2248) stays log-only: raw exits (unrecognized frames,
+parse failures) forward as-is and log `[tag-echo] raw exit` so new leak shapes
+become visible without changing the wire. Pinned by
+`tests/unified-acp-invariants.test.ts` (eight tests, both lanes).
+
diff --git a/TECHNICAL-NOTES.zh-CN.md b/TECHNICAL-NOTES.zh-CN.md
index 411459a06c..b7a166b13b 100644
--- a/TECHNICAL-NOTES.zh-CN.md
+++ b/TECHNICAL-NOTES.zh-CN.md
@@ -105,3 +105,15 @@ bili 永不拥有用户数据:每个被启动的客户端都跑在**真实 home*
- **D3 网关杀摘要** —— **非流式** preflight 摘要以 HTTP 524/504(Cloudflare 类)死亡时不再原地重试(旧的 transient 重试对着死网关每次烧 ~100 秒);会话在**首次**命中即学习 `metadata.preflightStreamSummary = true` 并改用 SSE 重取摘要 —— 与手动 `compress.streamSummary` 旋钮(#2133)同一学习存储,自动武装。级联双向被尊重:任一级显式 `streamSummary: false`(global → provider → model)即操作员 opt-out —— 两条学习路径都不武装、已学习的旧标志被忽略(`PreflightDeps.streamSummaryOff`)。
可观测:`session.metadata.selfHeal = { detected, action, since }` 进 `/__bili/sessions`、web UI 挂"自愈"徽标、每次转换一行 `[self-heal]` 日志(检测+恢复)。`bili plugin remove ` 尽力查询活代理(instance 文件 → `GET /__bili/sessions`,2.5 秒超时,失败静默),该客户端近 **10 分钟**内有活跃会话时打印提示。
+
+## ACP 标签回声统一整治 —— 四条不变量(#2023/#2066/#2190/#2248)
+
+一个 PR 把三条整治腿合并(refs-run 残留 #2023、退化闭名剥离+出口审计网 #2190、emission 整段丢弃+签名思考 verbatim #2066),并统一受四条硬规则约束 —— 覆盖**所有**文本改写面(loop 适配器、插件直通、JSON strip 函数、出口审计):
+
+1. **思考通道逐字节不动。** Anthropic `thinking`、Google `thought`、OpenAI `reasoning_content`/`reasoning`、Responses reasoning summaries 原样透传 —— 哪怕长得像标签回声。理由:reasoning 回放(DeepSeek 式)与签名校验验证的就是这些字节,任何改写都会让它们失步并卡死会话(#1960/KDD#10)。适配器侧走 `createIdentityStreamFilter`,strip 函数(`stripOpenaiChatText`、`stripResponsesText`)同样不动它们(纵深防御)。**注意:这翻转了 #1881 时代的 reasoning 回声剥离 —— 思考通道不再清洗。**
+2. **模型的正文输出可以治理。** 渲染标签回声、marker 行、bili 内部文本、退化开/闭名残骸,以及(absorb 受指令时)整段工具调用 emission,只在可见文本通道剥离。
+3. **模型写入文件的内容逐字节不变(#1039)。** 工具调用 arguments 无论携带什么字节都不进任何过滤器。
+4. **用户发送的消息永不被改写。** 用户自己片段的逐字回声在武装清洗下也保留(#463 豁免,按请求文本匹配,不按形状)。
+
+出口审计网(#2248)保持仅记日志:裸出口(未识别帧、解析失败)原样转发并打 `[tag-echo] raw exit` 日志,新泄漏形状可见而不改线上行为。由 `tests/unified-acp-invariants.test.ts` 钉住(8 个测试,两条 lane)。
+
diff --git a/src/loop/adapter-anthropic.ts b/src/loop/adapter-anthropic.ts
index 3cc73f6bb5..14ae000eec 100644
--- a/src/loop/adapter-anthropic.ts
+++ b/src/loop/adapter-anthropic.ts
@@ -152,7 +152,7 @@ function buildTextDeltaEvent(index: number, text: string): Buffer {
);
}
-export function createAnthropicAdapter(requestBody: Record, originalSystem?: AnthropicRequestBody["system"], notes?: string[], errorShape: "protocol" | "completion" = "protocol", cacheMarks?: Map): CompressLoopAdapter {
+export function createAnthropicAdapter(requestBody: Record, originalSystem?: AnthropicRequestBody["system"], notes?: string[], errorShape: "protocol" | "completion" = "protocol", cacheMarks?: Map, absorbArmed?: boolean): CompressLoopAdapter {
const model = (requestBody.model as string) ?? undefined;
let messageId: string | undefined;
let clientIndex = 0;
@@ -284,11 +284,20 @@ export function createAnthropicAdapter(requestBody: Record, ori
// per prose field — text and thinking deltas interleave across one
// stream, so a shared instance would hold a tag-shaped tail against
// the wrong field's bytes.
+ // m00885: whole-field absorb-emission drop is armed only when this
+ // request instructed the model about absorb (server-side provenance).
+ const absorbArmedLocal = absorbArmed === true;
+ // m00885: the shipped request text lets the filter keep an
+ // emission-shaped span the user asked to be output verbatim.
+ const requestText = JSON.stringify(requestBody);
const makeFilter = () => composeStreamFilters(
composeStreamFilters(
createTagEchoFilter((snippet) => {
loggerLog("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
- }),
+ }, (snippet) => {
+ // #2190: residue audit — log-only, see plugin.ts twins.
+ loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
+ }, absorbArmedLocal, requestText),
createMarkerLineFilter((snippet) => {
loggerLog("warn", `[marker-echo] stripped model-emitted ACP confirmation marker: ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
}),
diff --git a/src/loop/adapter-google.ts b/src/loop/adapter-google.ts
index bc10dbb358..f888b39f28 100644
--- a/src/loop/adapter-google.ts
+++ b/src/loop/adapter-google.ts
@@ -294,11 +294,22 @@ export function createGoogleAdapter(
// text from signature and bricks the session. Thought echoes are
// forwarded verbatim; cosmetic leaks inside a thought pane are
// accepted (#1960 verdict).
+ // m00885: whole-field absorb-emission drop is armed only when this
+ // request instructed the model about absorb (server-side provenance
+ // — the absorb section exists on this request's wire iff absorbName
+ // was resolved for it).
+ const absorbArmedLocal = absorbName !== undefined;
+ // m00885: the shipped request text lets the filter keep an
+ // emission-shaped span the user asked to be output verbatim.
+ const requestText = JSON.stringify(requestBody);
const makeFilter = () => composeStreamFilters(
composeStreamFilters(
createTagEchoFilter((snippet) => {
loggerLog("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
- }),
+ }, (snippet) => {
+ // #2190: residue audit — log-only, see plugin.ts twins.
+ loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
+ }, absorbArmedLocal, requestText),
createMarkerLineFilter((snippet) => {
loggerLog("warn", `[marker-echo] stripped model-emitted ACP confirmation marker: ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
}),
diff --git a/src/loop/adapter-openai.ts b/src/loop/adapter-openai.ts
index 3ffb7e0b75..ccebec3493 100644
--- a/src/loop/adapter-openai.ts
+++ b/src/loop/adapter-openai.ts
@@ -1,7 +1,7 @@
import type { CoreMessage } from "acp-kernel";
import { coreToOpenai, injectOpenaiSystem } from "acp-kernel/wire";
import { buildVisibilityMarker } from "./core.js";
-import { composeStreamFilters, createBiliArtifactFilter, createMarkerLineFilter, createTagEchoFilter } from "./tag-echo-filter.js";
+import { composeStreamFilters, createBiliArtifactFilter, createIdentityStreamFilter, createMarkerLineFilter, createTagEchoFilter } from "./tag-echo-filter.js";
import { degenerateTurnWarning } from "../degenerate-turn.js";
import { log as loggerLog } from "../logger.js";
import { hardenOpenaiAssistantContent, systemToUser } from "../util.js";
@@ -249,11 +249,25 @@ export function createOpenaiAdapter(requestBody: Record, client
// shared instance would hold a tag-shaped tail against the wrong
// field's bytes (residue leak or content loss). Tool-call
// arguments never enter any filter (#1039 invariant).
+ // Unified ACP invariant: the reasoning channel rides an IDENTITY
+ // filter — thinking is never rewritten (matches signed-thinking
+ // treatment in the anthropic/google adapters, #1960/KDD#10).
+ // m00885: whole-field absorb-emission drop is armed only when this
+ // request instructed the model about absorb (server-side provenance
+ // — the absorb section exists on this request's wire iff absorbName
+ // was resolved for it).
+ const absorbArmedLocal = absorbName !== undefined;
+ // m00885: the shipped request text lets the filter keep an
+ // emission-shaped span the user asked to be output verbatim.
+ const requestText = JSON.stringify(requestBody);
const makeFieldFilter = () => composeStreamFilters(
composeStreamFilters(
createTagEchoFilter((snippet) => {
loggerLog("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
- }),
+ }, (snippet) => {
+ // #2190: residue audit — log-only, see plugin.ts twins.
+ loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
+ }, absorbArmedLocal, requestText),
createMarkerLineFilter((snippet) => {
loggerLog("warn", `[marker-echo] stripped model-emitted ACP confirmation marker: ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
}),
@@ -267,7 +281,7 @@ export function createOpenaiAdapter(requestBody: Record, client
// only turn must still report an empty text channel, not a missing
// one. Thinking/reasoning presence reaches the gate via sawReasoning.
const contentFilter = makeFieldFilter();
- const reasoningFilter = makeFieldFilter();
+ const reasoningFilter = createIdentityStreamFilter();
const flushFilter = function* (): Generator {
const ct = contentFilter.flush();
if (ct.length > 0) yield { kind: "text", delta: ct, raw: buildContent(ct) } as ParsedStreamEvent;
diff --git a/src/loop/adapter-responses.ts b/src/loop/adapter-responses.ts
index 666953a08d..ff7d92f18d 100644
--- a/src/loop/adapter-responses.ts
+++ b/src/loop/adapter-responses.ts
@@ -5,7 +5,7 @@ import { coreToResponsesWithToolImages as coreToResponses, patchResponsesInputWi
import { buildVisibilityMarker } from "./core.js";
import { hoistTrappedToolItems } from "../tool-pair-order.js";
import { hashId, strippedResponseIdWarning } from "../util.js";
-import { composeStreamFilters, createBiliArtifactFilter, createMarkerLineFilter, createTagEchoFilter, stripResponsesText, containsBiliInternalText, containsMarkerLineText, containsRenderTagText, ACP_NAME_ALT } from "./tag-echo-filter.js";
+import { composeStreamFilters, createBiliArtifactFilter, createIdentityStreamFilter, createMarkerLineFilter, createTagEchoFilter, stripResponsesText, containsBiliInternalText, containsMarkerLineText, containsRenderTagText, containsToolCallEmissionText, ACP_NAME_ALT, type TagEchoFilter } from "./tag-echo-filter.js";
import { degenerateTurnWarning } from "../degenerate-turn.js";
import { log as loggerLog } from "../logger.js";
import { extractResponsesTextTriggers, PROXY_TOOL_NAMES } from "../compress-tool.js";
@@ -273,6 +273,10 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
// crashes strict clients (codex) — same class as the anthropic gap (#413).
let createdForwarded = false;
let createdRespId: string | null = null;
+ // m00885: the client request bytes as shipped (user messages included,
+ // kernel prompt section excluded) — buildRequest runs before parseStream,
+ // so the filters below can echo-check an emission-shaped span against it.
+ let shippedRequestText: string | undefined;
const ensureCreated = (): Buffer | null => {
if (createdForwarded) return null;
createdForwarded = true;
@@ -285,6 +289,7 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
return {
buildRequest(coreMessages, systemPrompt, requestBody) {
+ shippedRequestText = JSON.stringify(requestBody);
const customToolCallIds = new Set();
for (const m of coreMessages) {
const bm = m as BiliMessage;
@@ -342,11 +347,19 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
// are stripped wholesale via stripResponsesText. #1881: reasoning
// summaries stream through their own per-summary filters — the old
// generic raw passthrough leaked model-emitted tags on this wire.
+ // m00885: whole-field absorb-emission drop is armed only when this
+ // request instructed the model about absorb (server-side provenance
+ // — the absorb section exists on this request's wire iff absorbName
+ // was resolved for it).
+ const absorbArmedLocal = absorbName !== undefined;
const makeFilter = () => composeStreamFilters(
composeStreamFilters(
createTagEchoFilter((snippet) => {
loggerLog("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
- }),
+ }, (snippet) => {
+ // #2190: residue audit — log-only, see plugin.ts twins.
+ loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
+ }, absorbArmedLocal, shippedRequestText),
createMarkerLineFilter((snippet) => {
loggerLog("warn", `[marker-echo] stripped model-emitted ACP confirmation marker: ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
}),
@@ -356,7 +369,10 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
}),
);
const tagFilter = makeFilter();
- const reasoningFilters = new Map; ref: { itemId: string; outputIndex: number; summaryIndex: number } }>();
+ // Unified ACP invariant: reasoning summaries are the thinking channel —
+ // identity filters, byte-verbatim (never rewritten; matches the
+ // openai adapter's reasoning_filter and #1960/KDD#10).
+ const reasoningFilters = new Map();
let lastTextRef: { itemId: string; outputIndex: number } | null = null;
const flushFilter = function* (): Generator {
const tail = tagFilter.flush();
@@ -458,7 +474,7 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
// this fallthrough — strip on detection like the message
// branches above (stripResponsesText never touches tool
// argument fields).
- const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr) || containsBiliInternalText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf;
+ const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr) || containsBiliInternalText(eventStr) || (absorbArmedLocal && containsToolCallEmissionText(eventStr))) ? rebuildResponsesEvent(type, stripResponsesText(obj, absorbArmedLocal, shippedRequestText)) : rawBuf;
yield { kind: "meta", chunk, firstRoundOnly: true } as ParsedStreamEvent;
}
} else if (
@@ -470,7 +486,7 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
if (mapped) {
yield { kind: "meta", chunk: rewriteRefEvent(type, stripResponsesText(obj), mapped), firstRoundOnly: false } as ParsedStreamEvent;
} else if (!suppressTextLifecycle) {
- const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr) || containsBiliInternalText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf;
+ const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr) || containsBiliInternalText(eventStr) || (absorbArmedLocal && containsToolCallEmissionText(eventStr))) ? rebuildResponsesEvent(type, stripResponsesText(obj, absorbArmedLocal, shippedRequestText)) : rawBuf;
yield { kind: "meta", chunk, firstRoundOnly: true } as ParsedStreamEvent;
}
} else if (type === "response.output_text.delta") {
@@ -572,7 +588,7 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
remapped.delete(origId);
yield { kind: "meta", chunk: rewriteItemEvent(type, stripResponsesText(obj), mapped), firstRoundOnly: false } as ParsedStreamEvent;
} else if (!suppressTextLifecycle) {
- const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr) || containsBiliInternalText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf;
+ const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr) || containsBiliInternalText(eventStr) || (absorbArmedLocal && containsToolCallEmissionText(eventStr))) ? rebuildResponsesEvent(type, stripResponsesText(obj, absorbArmedLocal, shippedRequestText)) : rawBuf;
yield { kind: "meta", chunk, firstRoundOnly: true } as ParsedStreamEvent;
}
} else if (item?.type !== "message" || !suppressTextLifecycle) {
@@ -580,13 +596,13 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
// this fallthrough — strip on detection like the message
// branches above (stripResponsesText never touches tool
// argument fields).
- const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr) || containsBiliInternalText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf;
+ const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr) || containsBiliInternalText(eventStr) || (absorbArmedLocal && containsToolCallEmissionText(eventStr))) ? rebuildResponsesEvent(type, stripResponsesText(obj, absorbArmedLocal, shippedRequestText)) : rawBuf;
yield { kind: "meta", chunk, firstRoundOnly: true } as ParsedStreamEvent;
}
} else if (type === "response.completed") {
yield* flushFilter();
yield* settleNamelessDiag();
- responseObj = stripResponsesText((obj.response as Record) ?? null);
+ responseObj = stripResponsesText((obj.response as Record) ?? null, absorbArmedLocal, shippedRequestText);
terminalKind = "completed";
terminalRaw = null;
const respUsage = (responseObj as Record | null)?.usage as
@@ -624,7 +640,7 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
let entry = reasoningFilters.get(key);
if (!entry) {
entry = {
- filter: makeFilter(),
+ filter: createIdentityStreamFilter(),
ref: {
itemId: typeof obj.item_id === "string" ? obj.item_id : "",
outputIndex: typeof obj.output_index === "number" ? obj.output_index : 0,
@@ -638,9 +654,10 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
const chunk = clean === rsDelta ? rawBuf : rebuildResponsesEvent(type, { ...obj, delta: clean });
yield { kind: "meta", chunk, firstRoundOnly: true } as ParsedStreamEvent;
}
- } else if (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr) || containsBiliInternalText(eventStr)) {
- yield { kind: "meta", chunk: rebuildResponsesEvent(type, stripResponsesText(obj)), firstRoundOnly: true } as ParsedStreamEvent;
} else {
+ // Unified ACP invariant: non-delta reasoning-summary
+ // events (.done with whole text) are thinking channel —
+ // byte-verbatim, no strip pass.
yield { kind: "meta", chunk: rawBuf, firstRoundOnly: true } as ParsedStreamEvent;
}
} else {
diff --git a/src/loop/index.ts b/src/loop/index.ts
index 97c1df89c0..8d4a5721c9 100644
--- a/src/loop/index.ts
+++ b/src/loop/index.ts
@@ -32,13 +32,14 @@ export function pickAdapter(
systemNotes?: string[],
streamErrorShape?: "protocol" | "completion",
anthropicCacheMarks?: Map,
+ absorbArmed?: boolean,
): CompressLoopAdapter {
// #1455: how upstream stream failures are presented to the client —
// protocol-native error frames (default) or legacy synthesized completion.
const shape = streamErrorShape ?? "protocol";
if (protocol === "responses") return createResponsesAdapter(textProtocol, responsesProjection, absorbName, systemNotes);
if (protocol === "openai") return createOpenaiAdapter(requestBody, openaiSystem, absorbName, systemNotes, shape);
- if (protocol === "anthropic") return createAnthropicAdapter(requestBody, anthropicSystem, systemNotes, shape, anthropicCacheMarks);
+ if (protocol === "anthropic") return createAnthropicAdapter(requestBody, anthropicSystem, systemNotes, shape, anthropicCacheMarks, absorbArmed);
if (protocol === "google") return createGoogleAdapter(requestBody, google?.system, absorbName, google?.model, systemNotes);
throw new Error(`[acp-loop] unknown protocol: ${protocol}`);
}
diff --git a/src/loop/tag-echo-filter.ts b/src/loop/tag-echo-filter.ts
index c9220d50c7..fe247312cd 100644
--- a/src/loop/tag-echo-filter.ts
+++ b/src/loop/tag-echo-filter.ts
@@ -50,6 +50,24 @@ import { CODEX_FORGED_HANDOFF_HEADER, FORGED_SUMMARY_HEADER } from "../codex-com
function buildAcplikeName(): string {
const cores = ["acp", "apc", "cap", "cpa", "pac", "pca"];
const ci = (ch: string) => `[${ch}${ch.toUpperCase()}]`;
+ // #2190 round 2: field-attested drift names OUTSIDE the core+insertion
+ // set. Inclusion rule (round 3 decontamination): a name enters ONLY on
+ // CLEAN spontaneous evidence — block-initial emission, tokens= attribute,
+ // deduped n>=4 — because open-side grep counts are inflated by the model
+ // quoting tag names inside its own reasoning. Qualifying: accessp, acacp,
+ // acb. Considered and EXCLUDED: acpx (raw census ~217 fails the clean
+ // filter — self-referential/constructed hits; one case-variant sighting
+ // only) and acpaa (n=1). Excluded forms are not silent: the fast-path and
+ // filter-release residue audits (#2190) warn on m\d{4,} shapes, so
+ // any production appearance is observable and can earn inclusion later.
+ // Exact enumeration only — every character-class generalization that
+ // covers these also matches real prose words (acgroup/acmap/acstep are
+ // structurally identical to acacp).
+ const attestedDrift = [
+ "[aA][cC][cC][eE][sS][sS][pP]",
+ "[aA][cC][aA][cC][pP]",
+ "[aA][cC][bB]",
+ ];
const fourLetter = new Set();
const threeLetter = new Set();
for (const c of cores) {
@@ -60,7 +78,7 @@ function buildAcplikeName(): string {
);
}
}
- return [...fourLetter, ...threeLetter].join("|");
+ return [...attestedDrift, ...fourLetter, ...threeLetter].join("|");
}
/** Longest-first alternation of every tolerated render-tag name (#673), case-folded via letter classes (#1731). */
@@ -76,9 +94,10 @@ const NAME = ACP_NAME_ALT;
// and the #644 release rules. Close-side tails keep their {0,32} bound — that
// one is load-bearing (#644: an unbounded close-side tail ate real content
// after a malformed close).
-// A render tag wraps exactly one bare ref: the kernel emits mNNNNN and nothing else between the tags (#1720). Content that
-// is not a ref is prose wearing tags — the tags go, the content stays. No g
+// is not refs-only is prose wearing tags — the tags go, the content stays
+// (#1720/#2023). No g
// flag: createTagEchoFilter drives it with exec() on a sliding buffer. It is
// flag-free by construction — case folding lives inside ACP_NAME_ALT's letter
// classes (#1731) — so every .source reconstruction below preserves behavior
@@ -86,17 +105,60 @@ const NAME = ACP_NAME_ALT;
// Attrs are OPTIONAL: the kernel always emits them, but models imitate the
// bare form mNNNNN (#1881) — whole-span strip must cover it or
// the interior ref leaks as residue after the lone tags go.
-const PAIRED = new RegExp("\x3c" + NAME + "(?:\\s[^<>]*)?>(\\s*m\\d{4,}\\s*)\x3c\\/" + NAME + ">");
-const REF_LIKE = /^\s*m\d{4,}\s*$/;
+const REF_TOKEN = "m\\d{4,}";
+// One kernel ref, or a run of them joined by whitespace / dash / comma — the
+// shapes the compress machinery prints (single ref, mNNNNN–mMMMMM ranges,
+// comma-separated range lists) that models imitate inside or beside render
+// tags (#2023). A bare run is never prose: prose cites ONE ref among words,
+// never a sequence of them.
+const REFS_RUN = REF_TOKEN + "(?:\\s*[\\u2013\\u2014,\\-]?\\s*" + REF_TOKEN + ")*";
+// Whole string is a refs run (optionally padded): tag content, not prose.
+const REFS_ONLY = new RegExp("^\\s*" + REFS_RUN + "\\s*$");
+// Body of a paired render tag: refs-only or empty (#1720/#2023). Anything
+// else is prose wearing tags — PAIRED must not match it.
+const PAIRED_BODY = "\\s*(?:" + REFS_RUN + ")?\\s*";
+const PAIRED = new RegExp("\x3c" + NAME + "(?:\\s[^<>]*)?>(" + PAIRED_BODY + ")" + "\x3c\\/" + NAME + ">");
+// An orphan refs run sitting directly against a close tag: the open half was
+// consumed by another branch (or never came), and stripping the close alone
+// leaves the refs behind as residue (#2023). The unit — run plus close — is
+// dead markup; the lookbehind keeps word-like tokens (xm01233) and refs
+// adjacent to a tag terminator out of it — a run right after \x3e belongs to
+// tag structure (an over-cap-dropped opening, #644) and stays lossless.
+// Streaming: consulted only when no opening is live (the swallowing state
+// owns paired closes, #1720). Whole-text: applied to UNMATCHED closes only
+// (see orphanCloseSpans) so a ref cited inside a prose-wearing pair survives.
+const ORPHAN_REF_CLOSE = new RegExp("(?])\\s*" + REFS_RUN + "\\s*\x3c\\/" + NAME + "(?=[\\s>])[^<>]{0,32}>");
+// Same unit anchored to the END of a string: the refs run immediately
+// preceding an unmatched close (whole-text orphan pass).
+const ORPHAN_RUN_BEFORE = new RegExp("(?])\\s*" + REFS_RUN + "\\s*$");
const LONE_OPEN = new RegExp("\x3c" + NAME + "(?:\\s[^<>]*)?>");
const LONE_CLOSE = new RegExp("\x3c\\/" + NAME + "(?=[\\s>])[^<>]{0,32}>");
+// #2190: a ref body closed by a DEGENERATE close name (any short word — the
+// drift set is open; the attested census alone has p/a/ap/apc/cap/ck/div/
+// warn/aph/ambient/apm). Whole-span strip only: the open must be a valid
+// acplike name and the body exactly one bare ref, so genuine HTML prose such
+// as "see
" or "m1234 text" is untouched. Runs BEFORE the lone
+// passes in stripAcpTags so the pair dies atomically instead of leaving the
+// ref behind.
+const DEGEN_PAIR = new RegExp("\x3c" + NAME + "(?:\\s[^<>]*)?>\\s*m\\d{4,}\\s*\x3c\\/[a-zA-Z][a-zA-Z0-9]{0,15}>", "g");
// A suffix of the buffer that could still grow into a render tag: either an
-// unterminated \x3c … opening (attrs so far, no \x3e yet) or a short
-// ambiguous prefix like \x3c, \x3ca, \x3c/ac, \x3cacip, …
-const PARTIAL_TAIL = new RegExp("(\x3c" + NAME + "\\s[^<>]*|\x3c\\/" + NAME + "(?:\\s[^<>]{0,32})?|\x3c\\/?[aAcCpPiI]*)$");
+// unterminated \x3c … opening (attrs so far, no \x3e yet — the
+// mangled \x3c=… form counts too, #2066), a short
+// ambiguous prefix like \x3c, \x3ca, \x3c/ac, \x3cacip, …, a trailing refs
+// run whose close may arrive in the next chunk (#2023 orphan unit) — alone or
+// already followed by the tag fragment that opens the split close — or a
+// partial ref prefix (a bare m or m + 1-3 digits) that may complete across
+// the boundary.
+// Refs right after a tag terminator (\x3e) are NOT held: they belong to tag
+// structure (over-cap-dropped openings, #644) and stay lossless. Held on the
+// small cap so a split unit can be stripped whole; a lone trailing ref with
+// no tag context is released at EOF, prose-safe.
+const PARTIAL_TAIL = new RegExp("(\x3c" + NAME + "\\s[^<>]*|\x3c" + NAME + "\\s*=\\s*[^<>]*|\x3c\\/" + NAME + "(?:\\s[^<>]{0,32})?|\x3c\\/?[aAcCpPiI]*|(?])\\s*" + REFS_RUN + "\\s*\x3c[^<>]*|(?])\\s*" + REFS_RUN + "|(?])m\\d{0,3})$");
// An unterminated render-tag opening at the end of a string: \x3c plus
// attrs, no \x3e — a truncated imitation, never prose (triggers use \x3cacp_).
-const TRUNC_OPEN = new RegExp("\x3c" + NAME + "\\s[^<>]*$");
+// The mangled \x3c=… form counts too: once the `=` is there the tail is
+// tag content, not an element.
+const TRUNC_OPEN = new RegExp("\x3c" + NAME + "\\s[^<>]*$|\x3c" + NAME + "\\s*=\\s*[^<>]*$");
// A truncated render-tag CLOSE at the end of a string: \x3c/ optionally
// plus truncated attrs — a truncated imitation close, never prose. Mirrors
// TRUNC_OPEN on the close side.
@@ -111,7 +173,15 @@ const TRUNC_CLOSE = new RegExp("\x3c\\/" + NAME + "(?:\\s[^<>]{0,32})?$");
// next `<`. A properly terminated opening never matches: its attribute list
// ends at a `>`, and no `<` can be reached from there within the class.
const BROKEN_ATTRS = new RegExp("\x3c" + NAME + "\\s[^<>]*(?=\x3c)");
-const DEFINITE_TAIL = new RegExp("^\x3c" + NAME + "\\s|^\x3c\\/" + NAME);
+// A mangled render-tag OPENING: the name, then `=` where the attribute list's
+// first whitespace belongs, e.g. ``. The kernel never emits it — the
+// model mangles the framing it learned from the wire (production session
+// ses_efe4cfcbdffe, 2026-10-03: 5 assistant turns opening with ``).
+// The `=` anchor is what keeps plain HTML safe: ``, `` and any
+// real element name carry no `=`, and a properly formed render tag matches
+// LONE_OPEN/PAIRED, not this.
+const MANGLED_OPEN = new RegExp("\x3c" + NAME + "\\s*=\\s*[^<>]*>");
+const DEFINITE_TAIL = new RegExp("^\x3c" + NAME + "\\s|^\x3c" + NAME + "=|^\x3c\\/" + NAME);
const OPEN_WITH_ATTRS = new RegExp("^\x3c" + NAME + "\\s");
const CLOSE_HEAD = "\x3c/";
const CLOSE_NAME_ANCHORED = new RegExp("^" + NAME);
@@ -145,6 +215,56 @@ function looseCloseEnd(s: string): number {
const span = looseCloseSpan(s);
return span === null ? -1 : span.end;
}
+// #2190: models imitate the CLOSE with arbitrary short words instead of an
+// acplike name — field census (session-A, 2026-10-05): ap/p/a/apc/cap/ck/div/
+// warn/aph/ambient/apm, an open set a whitelist cannot keep up with. Accepted
+// as a swallow terminator ONLY when the whole body since the opening is one
+// bare ref (the kernel's emit shape, #1720): ref-body + any short close is an
+// echo, never prose — a prose body stays indistinguishable from legitimate
+// markup and keeps the existing hold/budget/flush behavior. Strict >
+// termination, same discipline as looseCloseSpan (a partial close at the
+// buffer end is still undecidable and stays held).
+const DEGEN_CLOSE_NAME = /^[a-zA-Z][a-zA-Z0-9]{0,15}>/;
+// Single bare ref, exactly one (#2190 DEGEN_PAIR body rule); standalone const so
+// this stays valid when the master REF_* token constants churn (#2025 stack compat, #2229).
+const SINGLE_REF_BODY = /^\s*m\d{4,}\s*$/;
+function degenCloseAfterRef(s: string): { start: number; end: number } | null {
+ let idx = s.indexOf(CLOSE_HEAD);
+ while (idx >= 0) {
+ const m = DEGEN_CLOSE_NAME.exec(s.slice(idx + 2));
+ if (m && SINGLE_REF_BODY.test(s.slice(0, idx))) return { start: idx, end: idx + 2 + m[0].length };
+ idx = s.indexOf(CLOSE_HEAD, idx + 1);
+ }
+ return null;
+}
+
+/** Spans of every close tag in s with NO unmatched open before it (#2023).
+ * Opens and closes are paired greedily in document order (echoed tags are
+ * flat; kernel tags are well-nested), and only depth-zero closes count as
+ * orphans — a close inside any pair protects the refs run it terminates
+ * (#1720 prose-wearing pairs keep their content). */
+function orphanCloseSpans(s: string): { start: number; end: number }[] {
+ const events: { idx: number; open: boolean }[] = [];
+ const reO = new RegExp(LONE_OPEN.source, "g");
+ const reC = new RegExp(LONE_CLOSE.source, "g");
+ let m: RegExpExecArray | null;
+ while ((m = reO.exec(s)) !== null) events.push({ idx: m.index, open: true });
+ while ((m = reC.exec(s)) !== null) events.push({ idx: m.index, open: false });
+ events.sort((a, b) => a.idx - b.idx);
+ const spans: { start: number; end: number }[] = [];
+ let depth = 0;
+ for (const ev of events) {
+ if (ev.open) {
+ depth++;
+ } else if (depth === 0) {
+ const cm = new RegExp(LONE_CLOSE.source).exec(s.slice(ev.idx));
+ if (cm) spans.push({ start: ev.idx, end: ev.idx + cm[0].length });
+ } else {
+ depth--;
+ }
+ }
+ return spans;
+}
/** The span of one wrapped-turn imitation in `s`: where it starts, and the span
* that has to go — its head plus, when no loose close follows, the rest of the
@@ -232,7 +352,16 @@ export function stripMarkerLines(text: string): string {
return text.replace(MARKER_LINE, "");
}
-export function stripAcpTags(text: string): string {
+export function stripAcpTags(text: string, dropToolCallEmission = false, requestText?: string): string {
+ // A whole-field tool-call emission first: the field IS the call, so the
+ // span goes whole (see toolCallEmissionSpan). Gated by the caller; the
+ // m00885 echo check keeps a span the user asked to be emitted verbatim.
+ if (dropToolCallEmission) {
+ const span = toolCallEmissionSpan(text);
+ if (span !== null && !emissionEchoesRequest(text.slice(span.start, span.end), requestText)) {
+ text = text.slice(span.end);
+ }
+ }
// A wrapped-turn imitation first, whole: it swallows the model's turn, and
// leaving its payload behind hands the client the orphan markup that makes
// the turn unusable. Each pass removes at least the head, so this ends.
@@ -242,9 +371,27 @@ export function stripAcpTags(text: string): string {
if (wrapped === null) break;
out = out.slice(0, wrapped.start) + out.slice(wrapped.end);
}
+ out = out.replace(new RegExp(PAIRED.source, "g"), "");
+ // #2023: a refs run sitting directly against an UNMATCHED close is residue
+ // (its open was consumed elsewhere or never came) and goes with the close.
+ // A close inside any pair — even a prose-wearing one — belongs to that
+ // pair's content and stays (#1720), so only depth-zero closes qualify.
+ // Delete right-to-left so earlier offsets stay valid.
+ {
+ const spans = orphanCloseSpans(out);
+ for (let i = spans.length - 1; i >= 0; i--) {
+ const sp = spans[i];
+ const pre = out.slice(0, sp.start);
+ const rm = ORPHAN_RUN_BEFORE.exec(pre);
+ if (!rm) continue;
+ out = out.slice(0, pre.length - rm[0].length) + out.slice(sp.end);
+ }
+ }
out = out
.replace(new RegExp(PAIRED.source, "g"), "")
+ .replace(DEGEN_PAIR, "")
.replace(new RegExp(LONE_OPEN.source, "g"), "")
+ .replace(new RegExp(MANGLED_OPEN.source, "g"), "")
.replace(new RegExp(LONE_CLOSE.source, "g"), "")
.replace(new RegExp(TRUNC_OPEN.source), "")
.replace(new RegExp(TRUNC_CLOSE.source), "")
@@ -270,6 +417,16 @@ export function containsRenderTagText(s: string): boolean {
return RENDER_TAG_DETECT.test(s);
}
+// #2190: post-audit shape check — a bare ref immediately followed by ANY close
+// tag is echo residue by construction (genuine prose never puts mNNNNN right
+// before ; the kernel wraps refs in tags, so a naked ref IS the leak).
+// Used where bytes bypassed or escaped the filter and must be logged, not
+// dropped: fast-path gate sites and filter release points.
+const ECHO_RESIDUE = /m\d{4,}\s*\x3c\/[a-zA-Z]/;
+export function containsEchoResidue(s: string): boolean {
+ return ECHO_RESIDUE.test(s);
+}
+
// #468: some upstreams stream a model-imitated render tag in tokenizer-sized
// fragments ("\x3cac", "p tokens", ...) so no single chunk ever trips
// RENDER_TAG_DETECT. Per-chunk gates must also engage when the chunk contains
@@ -278,7 +435,23 @@ export function containsRenderTagText(s: string): boolean {
// (byte-identical passthrough); only chunks with a tag-head tail ("\x3c", "\x3c/",
// "\x3ca", "\x3cac", "\x3cacp ...attrs", "\x3c/acp ...") engage it.
export function mayStartRenderTag(s: string): boolean {
- return RENDER_TAG_DETECT.test(s) || PARTIAL_TAIL.test(s);
+ // MANGLED_OPEN carries its own ">" (PARTIAL_TAIL only sees unterminated
+ // tails), so it is tested directly: a chunk with a complete mangled open
+ // must engage the machine to drop it.
+ return RENDER_TAG_DETECT.test(s) || MANGLED_OPEN.test(s) || PARTIAL_TAIL.test(s);
+}
+
+// #2190: a BROADER ambiguous head, used by the streaming gate and the filter's
+// hold decision only. Degenerate imitations drift the name beyond the acplike
+// mutation set (the observed close census already proves free-form name drift),
+// so a head like \x3cck must engage the state machine or the rest of the tag
+// rides the fast path verbatim. Deliberately NOT folded into PARTIAL_TAIL /
+// mayStartRenderTag: isOrphanMarkupText (#1760) consumes those for
+// degenerate-turn accounting, where a prose tail such as "a\nm00608\n\n…prose…\n
+// \n\n standing alone in the prose
+// channel). That field IS the call, not prose quoting a call. Its shape is
+// the absorb tool's signature — the only bili tool with a ref+summary
+// parameter pair — so a whole-field shape is a tight discriminator: a
+// tool-call open at the field start, a ref parameter whose body is a kernel
+// ref, a summary parameter, and a call close. Embedded quotes never start
+// the field, so they are untouched. Dropping the span is additionally
+// gated on provenance (the request carried the [ACP absorb] instruction);
+// shape alone never decides.
+export const ABSORB_INSTRUCTION_MARKER = "[ACP absorb]";
+const TC_OPEN_AT_START = /^\x3c(?:antml:)?(?:function_calls|function|parameter|parameters|invoke|tool_calls|tool_call)\b/;
+const TC_PARAM_REF = /\x3c(?:antml:)?parameter(?:\s*=\s*["']?ref["']?|\s+name\s*=\s*["']ref["'])?[^<>]*\x3e\s*m\d{4,}\s*\x3c\/(?:antml:)?parameter/i;
+const TC_PARAM_SUMMARY = /\x3c(?:antml:)?parameter(?:\s*=\s*["']?summary["']?|\s+name\s*=\s*["']summary["'])?[^<>]*\x3e/i;
+const TC_CLOSE = /\x3c\/(?:antml:)?(?:function_calls|function|parameter|parameters|invoke|tool_calls|tool_call)\b[^<>]*\x3e/i;
+const TC_ORPHAN_HEAD = /^\s*\x3c\/(?:antml:)?(?:function_calls|function|parameter|parameters|invoke|tool_calls|tool_call)\b/;
+const TC_TAG_STRIP = /\x3c\/?(?:antml:)?(?:function_calls|function|parameter|parameters|invoke|tool_calls|tool_call)\b[^<>]*\x3e/gi;
+// Raw-wire whitespace: inside a JSON body, the gap between the markup and
+// the ref body arrives as two-char escape pairs (\n \r \t) that \s cannot
+// see. Brackets are never JSON-escaped, so a raw presence test is sound.
+const RAW_WS = "(?:\\\\[a-z]|\\s|\\\\)*";
+const TC_PARAM_REF_RAW = new RegExp(
+ "\\x3c(?:antml:)?parameter(?:\\s*=\\s*[\"']?ref[\"']?|\\s+name\\s*=\\s*[\"']ref[\"'])?[^<>]*\\x3e" + RAW_WS + "m\\d{4,}" + RAW_WS + "\\x3c\\/(?:antml:)?parameter",
+ "i"
+);
+
+/** Span of a whole-field tool-call emission: from the field start (after
+ * leading whitespace) through the last call close. Null when the field is
+ * not shaped as one. Pure shape — the caller gates the drop on
+ * provenance. */
+export function toolCallEmissionSpan(text: string): { start: number; end: number } | null {
+ const lead = text.length - text.trimStart().length;
+ const t = text.slice(lead);
+ if (t.length === 0 || !TC_OPEN_AT_START.test(t)) return null;
+ if (!TC_PARAM_REF.test(t) || !TC_PARAM_SUMMARY.test(t)) return null;
+ let end = -1;
+ for (const m of t.matchAll(new RegExp(TC_CLOSE.source, "gi"))) end = m.index + m[0].length;
+ if (end === -1) return null;
+ return { start: lead, end: lead + end };
+}
+
+/** Whole field is a tool-call emission (see above). */
+export function isToolCallEmission(text: string): boolean {
+ return toolCallEmissionSpan(text) !== null;
+}
+
+/** A field that opens on a tool-call CLOSE whose remainder is nothing but
+ * refs and whitespace — the tail half of an emission split across a field
+ * boundary (defensive; no production record yet). */
+export function isOrphanToolCallTail(text: string): boolean {
+ if (!TC_ORPHAN_HEAD.test(text)) return false;
+ return /^\s*(?:m\d{4,}\s*)*$/.test(text.replace(TC_TAG_STRIP, ""));
+}
+
+/** Raw-wire pre-check for the emission shape inside an SSE/JSON body (the
+ * gate runs before parsing; the exact field-level decision is
+ * toolCallEmissionSpan on parsed field text). */
+export function containsToolCallEmissionText(s: string): boolean {
+ return TC_PARAM_REF_RAW.test(s) && TC_PARAM_SUMMARY.test(s) && TC_CLOSE.test(s);
+}
+
+// Canonical comparison form for the m00885 echo check. The request side is
+// JSON text, where a newline in the user's quote is the two-char escape
+// pair `\n` — dropping the backslash alone would leave a stray `n` between
+// the markup and the ref body and break containment, so escape pairs are
+// resolved first (\uXXXX to the char, \n \t \r and the other pairs to a
+// space) and only then do backslashes and whitespace vanish. Both sides
+// end up in the same form, so a fragment quoted in the request and
+// re-emitted by the model — literal, JSON-escaped, or re-spaced — compares
+// equal.
+function canonicalEmissionForm(t: string): string {
+ return t
+ .replace(/\\u([0-9a-fA-F]{4})/g, (_m, h: string) => String.fromCharCode(parseInt(h, 16)))
+ .replace(/\\n/g, " ")
+ .replace(/\\t/g, " ")
+ .replace(/\\r/g, " ")
+ .replace(/\\[\"\\/btf]/g, " ")
+ .replace(/[\\\s]/g, "");
+}
+
+/** m00885: an emission-shaped span is KEPT when it echoes the shipped
+ * request — the user asked the model to output a tool-call-shaped fragment
+ * verbatim as the response, so the fragment is user intent, not a leaked
+ * internal call. The span (markup + ref body + summary + close) must be
+ * contained in the request bytes, whitespace/escape-insensitively; a
+ * model-invented emission (the production shape, where the ref points at a
+ * tool result the user never wrote) cannot match. */
+export function emissionEchoesRequest(spanText: string, requestText: string | undefined): boolean {
+ if (typeof requestText !== "string" || requestText.length === 0 || spanText.length === 0) return false;
+ if (requestText.includes(spanText)) return true;
+ const needle = canonicalEmissionForm(spanText);
+ if (needle.length === 0) return false;
+ return canonicalEmissionForm(requestText).includes(needle);
+}
+
+const TC_OPEN_NAMES = ["function_calls", "function", "tool_calls", "tool_call", "parameters", "parameter", "invoke"];
+
+/** Whether an accumulated field head could still be, or already is, the
+ * start of a tool call: "open" when a name head has reached its boundary
+ * (space/=/>), "prefix" while it may still grow into one, "none" when no
+ * name head remains possible. */
+export function toolCallOpenStatus(s: string): "none" | "prefix" | "open" {
+ let prefix = false;
+ for (const name of TC_OPEN_NAMES) {
+ for (const head of ["\x3c" + name, "\x3cantml:" + name]) {
+ if (s === head) {
+ prefix = true;
+ continue;
+ }
+ if (s.startsWith(head)) {
+ const nxt = s[head.length];
+ if (nxt === undefined) {
+ prefix = true;
+ continue;
+ }
+ if (nxt === " " || nxt === "\t" || nxt === "\n" || nxt === "\r" || nxt === "=" || nxt === ">") return "open";
+ } else if (head.startsWith(s)) {
+ prefix = true;
+ }
+ }
+ }
+ return prefix ? "prefix" : "none";
+}
+
+/** Per-chunk gate: does the chunk contain or end with a tool-call open, so
+ * the streaming machine must engage to decide whether the field is a
+ * whole-field emission? */
+export function mayStartToolCallEmission(s: string): boolean {
+ for (let i = s.indexOf("\x3c"); i !== -1; i = s.indexOf("\x3c", i + 1)) {
+ if (toolCallOpenStatus(s.slice(i)) !== "none") return true;
+ }
+ return false;
+}
+
// ─── #1634: bili-owned internal artifacts echoed by the model ───────────────
// The proxy injects three artifact families into model-visible payloads: the
// outbound checkpoint carrier (\x3cbili-chain \u2026/\x3e stamped onto forwarded requests),
@@ -425,8 +737,14 @@ export function mayStartBiliInternal(s: string): boolean {
// literal marker line, truncated internal-artifact open/header) is dead to the
// host like an empty turn, so degenerate-turn detection counts it as residue;
// plain prose (CJK leads included) is visible output, not residue.
+// #2023 review: the probe is PARTIAL_TAIL's \x3c -prefixed alternatives ONLY.
+// Its refs-run alternatives match genuine citations released at EOF; counting
+// those as residue made every bare-citation answer read as degenerate and fire
+// the one-shot retry (#732/#821) on a healthy turn. Tagged echoes need no help
+// here: their drop already sets sawStrippedEcho upstream.
+const TAG_PARTIAL_TAIL = new RegExp("(\x3c" + NAME + "\\s[^<>]*|\x3c\\/" + NAME + "(?:\\s[^<>]{0,32})?|\x3c\\/?[aAcCpPiI]*)$");
export function isOrphanMarkupText(s: string): boolean {
- return mayStartRenderTag(s) || containsMarkerLineText(s) || mayStartBiliInternal(s);
+ return RENDER_TAG_DETECT.test(s) || TAG_PARTIAL_TAIL.test(s) || containsMarkerLineText(s) || mayStartBiliInternal(s);
}
function tailHoldLen(s: string): number {
@@ -523,8 +841,21 @@ export function createBiliArtifactFilter(onDrop?: (snippet: string) => void): Ta
};
}
-export function createTagEchoFilter(onDrop?: (snippet: string) => void): TagEchoFilter {
+export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidueWarn?: (snippet: string) => void, absorbInstructed?: boolean, requestText?: string): TagEchoFilter {
let held = "";
+ /** Last character actually emitted ("" at stream start). Extends the
+ * [\w>] orphan-unit lookbehind ACROSS chunk boundaries: a refs run
+ * arriving at the buffer head was preceded by whatever was emitted
+ * before, and a \x3e there marks tag-structure residue (#644). */
+ let lastEmitted = "";
+ // Whole-field tool-call emission hold: when the request carried the
+ // [ACP absorb] instruction, the first non-whitespace head of each field
+ // that could be a tool-call open is held until the field is complete, so
+ // the emission decision runs on the WHOLE field (a mid-stream shape
+ // check could never see the closing tags). "pending" = not yet seen a
+ // non-whitespace byte; "committed" = a tool-call open is in the head;
+ // "off" = decided not one (or no instruction in the request).
+ let tcHold: "off" | "pending" | "committed" = absorbInstructed === true ? "pending" : "off";
let swallowUntilClose = false;
let swallowed = "";
/** Which budget the current swallow answers to (SWALLOW_CAP or
@@ -550,6 +881,14 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void): TagEcho
onDrop(snippet);
}
};
+ /** Character immediately before the refs run of an orphan-unit match —
+ * within the buffer, or the last emitted char when the run sits at the
+ * buffer head (chunk boundary). */
+ const charBeforeRun = (mm: RegExpExecArray, s: string): string => {
+ const lead = mm[0].match(/^\s*/)?.[0].length ?? 0;
+ const pos = mm.index + lead;
+ return pos === 0 ? lastEmitted : s[pos - 1];
+ };
const process = (input: string): string => {
let buf = input;
let out = "";
@@ -558,12 +897,13 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void): TagEcho
const combined = swallowed + buf;
const span = looseCloseSpan(combined);
if (span !== null) {
- // Only a ref-shaped body is tag content (#1720): a prose
- // body between paired tags is released and just the close
- // goes. An attested imitation (swallowReleases=false)
- // discards whatever the body is.
+ // Only a refs-only body is tag content (#1720/#2023): a
+ // prose body between paired tags is released and just the
+ // close goes — a ref cited inside such a body survives.
+ // An attested imitation (swallowReleases=false) discards
+ // whatever the body is.
const inner = combined.slice(0, span.start);
- if (REF_LIKE.test(inner) || !swallowReleases) {
+ if (REFS_ONLY.test(inner) || !swallowReleases) {
drop(combined.slice(0, span.end));
} else {
out += inner;
@@ -574,11 +914,34 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void): TagEcho
buf = combined.slice(span.end);
continue;
}
+ if (swallowReleases) {
+ // #2190: a degenerate close ends the span too. The body is
+ // ref-shaped by construction (degenCloseAfterRef), so the
+ // drop-whole rule applies unconditionally — wrapped mode
+ // never reaches here (its payload may contain arbitrary
+ // markup and must be discarded whole).
+ const dspan = degenCloseAfterRef(combined);
+ if (dspan !== null) {
+ drop(combined.slice(0, dspan.end));
+ swallowed = "";
+ swallowUntilClose = false;
+ buf = combined.slice(dspan.end);
+ continue;
+ }
+ }
if (combined.length > swallowLimit) {
swallowed = "";
if (swallowReleases) {
+ // A refs-only tail past the #644 budget is an imitated
+ // marker list, not prose — discard it. Mixed tails are
+ // content and still release losslessly.
+ if (REFS_ONLY.test(combined)) {
+ drop(combined);
+ return out;
+ }
swallowUntilClose = false;
buf = combined;
+ if (onResidueWarn && containsEchoResidue(combined)) onResidueWarn(combined);
continue;
}
// The span is an attested imitation's payload: discard it and
@@ -592,8 +955,20 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void): TagEcho
const p = PAIRED.exec(buf);
const o = LONE_OPEN.exec(buf);
const c = LONE_CLOSE.exec(buf);
+ // Orphan unit (#2023): only reached when no opening is live — a
+ // close inside a swallow belongs to that pair's content (#1720).
+ // The lookbehind class extends across the chunk boundary: a run at
+ // the buffer head preceded by an emitted word char is word-like
+ // (xm01233), and one preceded by \x3e is tag-structure residue
+ // (#644) — neither is an orphan.
+ let r = ORPHAN_REF_CLOSE.exec(buf);
+ if (r !== null && /[\w>]/.test(charBeforeRun(r, buf))) r = null;
+ // A mangled open () is a complete self-contained tag:
+ // drop it in place, no swallow (a following close dies via
+ // LONE_CLOSE on its own).
+ const g = MANGLED_OPEN.exec(buf);
let m: RegExpExecArray | null = null;
- for (const cand of [p, o, c]) {
+ for (const cand of [p, o, c, r, g]) {
if (cand && (m === null || cand.index < m.index)) m = cand;
}
// An opening whose attribute list never terminates (see
@@ -623,7 +998,7 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void): TagEcho
continue;
}
if (!m) {
- const t = PARTIAL_TAIL.exec(buf);
+ const t = PARTIAL_TAIL.exec(buf) ?? BROAD_TAG_HEAD_TAIL.exec(buf);
if (t) {
// A definite \x3c opening is never prose — hold it far
// past HOLD_LIMIT (drop it past TAG_OPEN_CAP); a short
@@ -641,6 +1016,7 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void): TagEcho
out += buf;
}
} else {
+ if (onResidueWarn && containsEchoResidue(buf)) onResidueWarn(buf);
out += buf;
}
break;
@@ -680,13 +1056,65 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void): TagEcho
return {
push(delta: string): string {
inputChars += delta.length;
+ if (tcHold !== "off") {
+ held += delta;
+ const t = held.trimStart();
+ if (t.length === 0) return "";
+ if (t[0] !== "\x3c") tcHold = "off";
+ else {
+ const st = toolCallOpenStatus(t);
+ if (st === "open") tcHold = "committed";
+ else if (st === "none") tcHold = "off";
+ }
+ if (tcHold !== "off") return "";
+ // Clear BEFORE processing: process() can stash a partial tag-head
+ // tail into the shared held buffer (PARTIAL_TAIL hold); clearing
+ // after would wipe it and re-emit the tag head as fresh prose on
+ // the next delta (#2267 review fix, mirrors normal-push/flush paths).
+ const chunk = held;
+ held = "";
+ const r = process(chunk);
+ outputChars += r.length;
+ return r;
+ }
const chunk = held + delta;
held = "";
const r = process(chunk);
+ if (r.length > 0) lastEmitted = r[r.length - 1];
outputChars += r.length;
return r;
},
flush(): string {
+ if (tcHold !== "off") {
+ // The field ended while its head was held: decide the whole
+ // field, then hand any non-emission rest to the normal path.
+ tcHold = "off";
+ const field = held;
+ held = "";
+ const t = field.trimStart();
+ if (t.length > 0 && t[0] === "\x3c") {
+ const span = toolCallEmissionSpan(field);
+ if (span !== null) {
+ if (!emissionEchoesRequest(field.slice(span.start, span.end), requestText)) {
+ drop(field.slice(0, span.end));
+ const rest = field.slice(span.end);
+ if (rest.length === 0) return "";
+ const r = process(rest);
+ outputChars += r.length;
+ return r;
+ }
+ // The span mirrors the request verbatim (m00885): the
+ // user asked for this fragment as the whole response —
+ // it is user intent, so it flows through untouched.
+ } else if (isOrphanToolCallTail(field)) {
+ drop(field);
+ return "";
+ }
+ }
+ const r = process(field);
+ outputChars += r.length;
+ return r;
+ }
const rest = swallowed + held;
const wasSwallowing = swallowUntilClose;
swallowed = "";
@@ -702,8 +1130,9 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void): TagEcho
result = "";
} else if (wasSwallowing) {
// A BARE opening (#1881): prose may genuinely wear one, so an
- // over-budget-or-EOF tail is content unless it is exactly a
- // ref; only a truncated open/close tail is dead markup.
+ // over-budget-or-EOF tail is content unless it is refs-only —
+ // an imitated marker list (#2023), not a citation; only a
+ // truncated open/close tail is dead markup.
const t = new RegExp(TRUNC_OPEN.source).exec(rest);
if (t) {
drop(t[0]);
@@ -713,6 +1142,9 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void): TagEcho
if (tc) {
drop(tc[0]);
result = rest.slice(0, tc.index);
+ } else if (REFS_ONLY.test(rest)) {
+ drop(rest);
+ result = "";
} else {
result = rest;
}
@@ -732,6 +1164,8 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void): TagEcho
}
}
}
+ if (result.length > 0) lastEmitted = result[result.length - 1];
+ if (result.length > 0 && onResidueWarn && containsEchoResidue(result)) onResidueWarn(result);
outputChars += result.length;
return result;
},
@@ -747,17 +1181,17 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void): TagEcho
};
}
-function stripParts(content: unknown): unknown {
+function stripParts(content: unknown, drop: boolean, requestText?: string): unknown {
if (!Array.isArray(content)) return content;
return content.map((part) => {
if (part && typeof part === "object" && typeof (part as Record).text === "string") {
- return { ...(part as Record), text: stripAcpTags((part as Record).text as string) };
+ return { ...(part as Record), text: stripAcpTags((part as Record).text as string, drop, requestText) };
}
return part;
});
}
-function stripItemContent(it: unknown): unknown {
+function stripItemContent(it: unknown, drop: boolean, requestText?: string): unknown {
if (!it || typeof it !== "object") return it;
const io = it as Record;
let out: Record | undefined;
@@ -765,22 +1199,16 @@ function stripItemContent(it: unknown): unknown {
out ??= { ...io };
out[k] = v;
};
- if (Array.isArray(io.content)) set("content", stripParts(io.content));
- if (Array.isArray(io.summary)) {
- set(
- "summary",
- io.summary.map((s) =>
- s && typeof s === "object" && typeof (s as Record).text === "string"
- ? { ...(s as Record), text: stripAcpTags((s as Record).text as string) }
- : s,
- ),
- );
- }
+ if (Array.isArray(io.content)) set("content", stripParts(io.content, drop, requestText));
+ // Unified ACP invariant: reasoning summaries are the thinking channel —
+ // byte-verbatim, never rewritten (matches stripOpenaiChatText leaving
+ // reasoning_content alone and the adapters' identity filters).
+ // `content` on a message item is visible prose and stays strippable.
return out ?? it;
}
-function stripIfString(v: unknown): unknown {
- return typeof v === "string" ? stripAcpTags(v) : v;
+function stripIfString(v: unknown, drop: boolean, requestText?: string): unknown {
+ return typeof v === "string" ? stripAcpTags(v, drop, requestText) : v;
}
// Plugin-passthrough parity for the OpenAI chat-completions wire (issue #14:
@@ -792,7 +1220,7 @@ function stripIfString(v: unknown): unknown {
// execute/persist, so a shape-based false positive would silently corrupt
// data — only model prose is stripped. Mutates in place, mirroring
// stripResponsesText.
-export function stripOpenaiChatText(obj: T): T {
+export function stripOpenaiChatText(obj: T, drop: boolean = false, requestText?: string): T {
if (!obj || typeof obj !== "object") return obj;
const o = obj as Record;
if (!Array.isArray(o["choices"])) return obj;
@@ -803,9 +1231,12 @@ export function stripOpenaiChatText(obj: T): T {
const h = ch[holder];
if (h && typeof h === "object") {
const hh = { ...(h as Record) };
- hh["content"] = stripIfString(hh["content"]);
- hh["reasoning_content"] = stripIfString(hh["reasoning_content"]);
- hh["reasoning"] = stripIfString(hh["reasoning"]);
+ hh["content"] = stripIfString(hh["content"], drop, requestText);
+ // Unified ACP invariant (owner directive, #2229): the
+ // thinking/reasoning channel is byte-verbatim — it is the
+ // model's private channel, not prose, and reasoning replay
+ // (DeepSeek-style) or signature checks can validate it. Only
+ // the visible text channel is ever rewritten.
ch[holder] = hh;
}
}
@@ -814,25 +1245,27 @@ export function stripOpenaiChatText(obj: T): T {
return obj;
}
-// Plugin-passthrough parity for the Anthropic wire: strip `delta.{text,
-// thinking}` on content_block_delta streams and `content[].{text,thinking}`
-// on non-streaming message bodies. Mutates in place.
-export function stripAnthropicText(obj: T): T {
+// Plugin-passthrough parity for the Anthropic wire: strip `delta.text` on
+// content_block_delta streams and `content[].text` on non-streaming message
+// bodies. Signed `thinking` / `redacted_thinking` blocks are LEFT byte-for-byte
+// (#1960/KDD#10): they are verified against their signature on replay, so any
+// rewrite desyncs them and bricks the session — matching the loop adapters'
+// "prose filters never touch thinking" treatment. Mutates in place.
+export function stripAnthropicText(obj: T, drop: boolean = false, requestText?: string): T {
if (!obj || typeof obj !== "object") return obj;
const o = obj as Record;
const d = o["delta"];
if (d && typeof d === "object") {
const dd = { ...(d as Record) };
- dd["text"] = stripIfString(dd["text"]);
- dd["thinking"] = stripIfString(dd["thinking"]);
+ dd["text"] = stripIfString(dd["text"], drop, requestText);
o["delta"] = dd;
}
if (Array.isArray(o["content"])) {
o["content"] = (o["content"] as unknown[]).map((c) => {
if (!c || typeof c !== "object") return c;
const cc = c as Record;
- if (typeof cc["text"] !== "string" && typeof cc["thinking"] !== "string") return c;
- return { ...cc, text: stripIfString(cc["text"]), thinking: stripIfString(cc["thinking"]) };
+ if (typeof cc["text"] !== "string") return c;
+ return { ...cc, text: stripIfString(cc["text"], drop, requestText) };
});
}
return obj;
@@ -846,25 +1279,25 @@ export function stripAnthropicText(obj: T): T {
// fragment carriers) are deliberately untouched (#1039): they carry user
// intent that hosts execute/persist, so a shape-based false positive would
// silently corrupt data — only model prose is stripped.
-export function stripResponsesText(obj: T): T {
+export function stripResponsesText(obj: T, drop: boolean = false, requestText?: string): T {
if (!obj || typeof obj !== "object") return obj;
const o = obj as Record;
- if (typeof o.text === "string") o.text = stripAcpTags(o.text);
+ if (typeof o.text === "string") o.text = stripAcpTags(o.text, drop, requestText);
if (o.part && typeof o.part === "object" && typeof (o.part as Record).text === "string") {
- o.part = { ...(o.part as Record), text: stripAcpTags((o.part as Record).text as string) };
+ o.part = { ...(o.part as Record), text: stripAcpTags((o.part as Record).text as string, drop, requestText) };
}
if (o.item && typeof o.item === "object") {
- o.item = stripItemContent(o.item);
+ o.item = stripItemContent(o.item, drop, requestText);
}
if (o.response && typeof o.response === "object") {
const resp = { ...(o.response as Record) };
if (Array.isArray(resp.output)) {
- resp.output = resp.output.map(stripItemContent);
+ resp.output = (resp.output as unknown[]).map((it) => stripItemContent(it, drop, requestText));
}
o.response = resp;
}
if (Array.isArray(o.output)) {
- o.output = o.output.map(stripItemContent);
+ o.output = (o.output as unknown[]).map((it) => stripItemContent(it, drop, requestText));
}
return obj;
}
@@ -977,3 +1410,24 @@ export function composeStreamFilters(a: TagEchoFilter, b: TagEchoFilter): TagEch
}),
};
}
+
+// #1960/KDD#10 — signature-verified channels (signed Anthropic `thinking`
+// deltas, Gemini `thought` parts) must ride byte-for-byte: any rewrite
+// desyncs them from their signature and bricks replay. This identity filter
+// passes every delta through unchanged while keeping honest char accounting,
+// so degenerate-turn detection (#673) still sees real input/output sizes.
+export function createIdentityStreamFilter(): TagEchoFilter {
+ let inputChars = 0;
+ let outputChars = 0;
+ return {
+ push: (delta: string) => {
+ inputChars += delta.length;
+ outputChars += delta.length;
+ return delta;
+ },
+ flush: () => "",
+ dropped: () => false,
+ pending: () => false,
+ stats: () => ({ inputChars, outputChars, dropped: false }),
+ };
+}
diff --git a/src/plugin.ts b/src/plugin.ts
index db36733631..07c46ff465 100644
--- a/src/plugin.ts
+++ b/src/plugin.ts
@@ -16,7 +16,7 @@ import { effectiveRulesEnabled, rulesEnabled } from "./rules-feature.js";
import { executeProxyTool } from "./loop/core.js";
import type { ProxyToolResult } from "./proxy-tool-result.js";
import { normalizeSseLineEndings } from "./sse-util.js";
-import { composeStreamFilters, containsBiliInternalText, containsMarkerLineText, containsRenderTagText, containsToolCallXmlFragment, createBiliArtifactFilter, createMarkerLineFilter, createTagEchoFilter, isOrphanMarkupText, mayStartBiliInternal, mayStartMarkerLine, mayStartRenderTag, stripAcpTags, stripAnthropicText, stripOpenaiChatText, stripResponsesText, type TagEchoFilter } from "./loop/tag-echo-filter.js";
+import { composeStreamFilters, containsBiliInternalText, containsEchoResidue, containsMarkerLineText, containsRenderTagText, containsToolCallEmissionText, containsToolCallXmlFragment, createBiliArtifactFilter, createIdentityStreamFilter, createMarkerLineFilter, createTagEchoFilter, isOrphanMarkupText, mayStartBiliInternal, mayStartDegenerateRenderTag, mayStartMarkerLine, mayStartRenderTag, mayStartToolCallEmission, stripAcpTags, stripAnthropicText, stripOpenaiChatText, stripResponsesText, type TagEchoFilter } from "./loop/tag-echo-filter.js";
import { log as loggerLog } from "./logger.js";
import { ccrEnabled, ccrLoopConfig, contentStoreOf, retrieveToolName } from "./store.js";
import { imageUsageSuffix } from "./image-compress.js";
@@ -1723,6 +1723,14 @@ export async function pipePluginChatWithStrip(
log?: (msg: string) => void,
refetch?: () => Promise | null>,
upstreamOrigin?: string,
+ // True when the outbound request carried the [ACP absorb] instruction:
+ // the model may then answer with a tool call written as prose, and the
+ // filters apply the whole-field emission drop (gated on request
+ // provenance, not string shape — m00885).
+ absorbInstructed?: boolean,
+ // The shipped request text: an emission-shaped span the user asked to
+ // output verbatim is echoed, not dropped (m00885).
+ requestText?: string,
): Promise {
let reader = stream.getReader();
let decoder = new TextDecoder("utf-8");
@@ -1735,6 +1743,12 @@ export async function pipePluginChatWithStrip(
loggerLog("warn", `[tag-echo] stripped model-emitted render tag (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
log?.(`[tag-echo] stripped model-emitted render tag from plugin passthrough text`);
};
+ // #2190: the filter released bytes that still carry echo-residue shape —
+ // the leak is now observable instead of silent. Log-only; the bytes were
+ // already decided by the state machine.
+ const onResidueWarn = (snippet: string) => {
+ loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
+ };
const onMarkerDrop = (snippet: string) => {
sawStrippedEcho = true;
loggerLog("warn", `[marker-echo] stripped model-emitted ACP confirmation marker (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
@@ -1758,7 +1772,15 @@ export async function pipePluginChatWithStrip(
const key = `${field}:${index}`;
let s = streams.get(key);
if (!s) {
- s = { filter: composeStreamFilters(composeStreamFilters(createTagEchoFilter(onTagDrop), createMarkerLineFilter(onMarkerDrop)), createBiliArtifactFilter(onBiliDrop)), field, index };
+ // #1960/KDD#10: signed thinking (Anthropic `thinking_delta`, Gemini
+ // `thought` parts) must ride byte-for-byte — any rewrite desyncs its
+ // signature and bricks replay. Route it through an identity filter
+ // instead of the prose filters (matches the loop adapters).
+ // Unified ACP invariant (#2229): the same treatment extends to the
+ // openai reasoning fields — the thinking channel is NEVER
+ // rewritten, only the visible text channel is.
+ const signedThinking = field === "thinking" || (protocol === "openai" && (field === "reasoning_content" || field === "reasoning"));
+ s = { filter: signedThinking ? createIdentityStreamFilter() : composeStreamFilters(composeStreamFilters(createTagEchoFilter(onTagDrop, onResidueWarn, absorbInstructed, requestText), createMarkerLineFilter(onMarkerDrop)), createBiliArtifactFilter(onBiliDrop)), field, index };
streams.set(key, s);
}
return s;
@@ -2079,6 +2101,9 @@ export async function pipePluginChatWithStrip(
}
const choices = ev["choices"];
if (!Array.isArray(choices)) {
+ // #2190: no-choices frames bypass the state machine — audit them.
+ // No tool_calls exclusion needed: arguments live under choices.
+ auditRawForward(rawEvent);
return anyPending() ? flushTails() + rawEvent + "\n\n" : rawEvent + "\n\n";
}
let rebuilt: Record | null = null;
@@ -2135,10 +2160,17 @@ export async function pipePluginChatWithStrip(
// leap ahead of a held tail.
if (v.length > 0) hadText = true;
if (field !== "content" && v.length > 0) sawThinking = true;
- if (!mayStartRenderTag(v) && !mayStartMarkerLine(v) && !mayStartBiliInternal(v) && !anyPending()) {
+ // An absorb-instructed request may carry a whole-field
+ // tool-call emission: route it to the filter so the
+ // whole-field decision runs (m00885). Degenerate-close
+ // residue (#2190) rides the same routing.
+ if (!mayStartRenderTag(v) && !mayStartMarkerLine(v) && !mayStartBiliInternal(v) && !mayStartDegenerateRenderTag(v) && !(absorbInstructed === true && mayStartToolCallEmission(v)) && !anyPending()) {
if (v.length > 0) {
keptText = true;
proseAcc += v;
+ // #2190: residue audit — bytes that bypassed the state
+ // machine must be observable, not silent.
+ if (containsEchoResidue(v)) loggerLog("warn", `[tag-echo] fast path forwarded echo-residue-shaped bytes (#2190): ${v.slice(0, 80).replace(/\n/g, " ")}`);
}
if (field === "content") visibleTextChars += v.length;
continue;
@@ -2199,6 +2231,9 @@ export async function pipePluginChatWithStrip(
}
return drain + rebuildEvent(rawEvent, rebuilt);
}
+ // #2190: these exits forward frames whose text lived outside the
+ // managed fields — audit them (argument frames excluded: #1039).
+ if (!openaiFrameHasToolCalls(choices)) auditRawForward(rawEvent);
if (drain.length > 0) return drain + rawEvent + "\n\n";
if (!hadText && anyPending()) return flushTails() + rawEvent + "\n\n";
return rawEvent + "\n\n";
@@ -2220,9 +2255,13 @@ export async function pipePluginChatWithStrip(
args: "",
});
} else if (bt === "thinking" || bt === "redacted_thinking") sawThinking = true;
+ // #2248: raw exit — content_block_start payloads never enter the filter.
+ auditRawForward(rawEvent);
return anyPending() ? flushTails() + rawEvent + "\n\n" : rawEvent + "\n\n";
}
if (ev["type"] !== "content_block_delta") {
+ // #2248: raw exit — non-delta events never enter the filter.
+ auditRawForward(rawEvent);
return anyPending() ? flushTails() + rawEvent + "\n\n" : rawEvent + "\n\n";
}
const d = ev["delta"] as Record | undefined;
@@ -2238,12 +2277,22 @@ export async function pipePluginChatWithStrip(
}
const field = d?.["type"] === "thinking_delta" ? "thinking" : d?.["type"] === "text_delta" ? "text" : null;
if (field === null || typeof d?.[field] !== "string") {
+ // #2248: raw exit — unmanaged delta types never enter the filter.
+ // input_json_delta excluded: tool-call arguments are user intent (#1039).
+ if (d?.["type"] !== "input_json_delta") auditRawForward(rawEvent);
return rawEvent + "\n\n";
}
const raw = d[field] as string;
if (field === "thinking" && raw.length > 0) sawThinking = true;
- if (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !anyPending()) {
- if (raw.length > 0) proseAcc += raw;
+ // Whole-field tool-call emission on an absorb-instructed request
+ // (m00885): the filter's field-start hold decides on the complete
+ // field, so the fast path must not pre-empt it.
+ if (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !mayStartDegenerateRenderTag(raw) && !(absorbInstructed === true && mayStartToolCallEmission(raw)) && !anyPending()) {
+ if (raw.length > 0) {
+ proseAcc += raw;
+ // #2190: residue audit — see the twin above.
+ if (containsEchoResidue(raw)) loggerLog("warn", `[tag-echo] fast path forwarded echo-residue-shaped bytes (#2190): ${raw.slice(0, 80).replace(/\n/g, " ")}`);
+ }
if (field === "text" && raw.length > 0) visibleTextChars += raw.length;
return rawEvent + "\n\n";
}
@@ -2276,6 +2325,8 @@ export async function pipePluginChatWithStrip(
if (ev["error"] !== undefined) sawTerminal = true;
const candidates = ev["candidates"];
if (!Array.isArray(candidates)) {
+ // #2248: raw exit — non-candidates frames never enter the filter.
+ auditRawForward(rawEvent);
return anyPending() ? flushTails() + rawEvent + "\n\n" : rawEvent + "\n\n";
}
let rebuilt: Record | null = null;
@@ -2329,10 +2380,15 @@ export async function pipePluginChatWithStrip(
// field, so an interleaved thought/text pair in one frame never
// shares held-back state.
const field = p["thought"] === true ? "thinking" : "text";
- if (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !anyPending()) {
+ // Whole-field tool-call emission on an absorb-instructed
+ // request (m00885) — see processOpenai. Degenerate-close
+ // residue (#2190) rides the same routing.
+ if (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !mayStartDegenerateRenderTag(raw) && !(absorbInstructed === true && mayStartToolCallEmission(raw)) && !anyPending()) {
if (raw.length > 0) {
keptText = true;
proseAcc += raw;
+ // #2190: residue audit — see the twin above.
+ if (containsEchoResidue(raw)) loggerLog("warn", `[tag-echo] fast path forwarded echo-residue-shaped bytes (#2190): ${raw.slice(0, 80).replace(/\n/g, " ")}`);
}
if (field === "text") visibleTextChars += raw.length;
continue;
@@ -2397,6 +2453,10 @@ export async function pipePluginChatWithStrip(
if (droppedText && !keptText && !googleFrameHasNonText(rebuilt)) return "";
return drain + rebuildEvent(rawEvent, rebuilt);
}
+ // #2248: raw exit — text outside managed parts never enters the filter.
+ // Frames carrying non-text parts (functionCall/functionResponse, ...) hold
+ // structured or user data (#1039) and are excluded from the audit.
+ if (!googleFrameHasNonText(ev)) auditRawForward(rawEvent);
if (drain.length > 0) return drain + rawEvent + "\n\n";
if (!hadText && anyPending()) return flushTails() + rawEvent + "\n\n";
return rawEvent + "\n\n";
@@ -2429,6 +2489,8 @@ export async function pipePluginChatWithStrip(
try {
ev = JSON.parse(jsonStr) as Record;
} catch {
+ // #2190: unparseable frames bypass every filter — audit them.
+ auditRawForward(rawEvent);
await write(rawEvent + "\n\n");
continue;
}
@@ -2546,6 +2608,44 @@ function hadTextOtherThanTextFields(choices: unknown): boolean {
return false;
}
+// #2190: residue audit for frames forwarded verbatim by a raw exit — i.e. a
+// frame whose text lives OUTSIDE the managed fields (or is not parseable) and
+// therefore never touches the tag-echo state machine. Log-only: the bytes are
+// forwarded exactly as received (#1039 wire fidelity); the point is that such
+// a leak is observable in the log instead of silent.
+function auditRawForward(rawEvent: string): void {
+ if (containsEchoResidue(rawEvent)) {
+ loggerLog("warn", `[tag-echo] raw forward carried echo-residue-shaped bytes (#2190): ${rawEvent.slice(0, 80).replace(/\n/g, " ")}`);
+ }
+}
+
+// #2190 companion to auditRawForward: tool-call argument fragments are user
+// intent (#1039) and may legitimately match the residue shape, so frames
+// carrying them are excluded from the audit.
+function openaiFrameHasToolCalls(choices: unknown): boolean {
+ if (!Array.isArray(choices)) return false;
+ for (const c of choices) {
+ const d = c && typeof c === "object" ? (c as Record)["delta"] : undefined;
+ if (d && typeof d === "object" && (d as Record)["tool_calls"] !== undefined) return true;
+ }
+ return false;
+}
+
+// #2248 companion to auditRawForward (responses wire): tool-call argument
+// streams (response.function_call_arguments.*, response.custom_tool_call_input.*)
+// fall through the unrecognized-type exit and are user intent (#1039) — exclude.
+function responsesFrameHasToolCalls(ev: Record): boolean {
+ const t = ev["type"];
+ if (typeof t === "string" && (t.includes("function_call") || t.includes("custom_tool_call"))) return true;
+ if (ev["arguments"] !== undefined) return true;
+ const item = ev["item"];
+ if (item && typeof item === "object") {
+ const it = (item as Record)["type"];
+ if (it === "function_call" || it === "custom_tool_call") return true;
+ }
+ return false;
+}
+
/** The Gemini counterpart of hadTextOtherThanTextFields: does a frame whose
* text parts were all stripped still carry something the client needs? A
* finishReason, a functionCall/functionResponse part, usageMetadata or
@@ -2601,6 +2701,11 @@ export async function pipePluginResponsesWithStrip(
log?: (msg: string) => void,
refetch?: () => Promise | null>,
upstreamOrigin?: string,
+ // See pipePluginChatWithStrip: whole-field tool-call emission drop,
+ // gated on the request carrying the [ACP absorb] instruction (m00885).
+ absorbInstructed?: boolean,
+ // m00885: echoed (user-requested verbatim) emission spans survive.
+ requestText?: string,
): Promise {
let reader = stream.getReader();
let decoder = new TextDecoder("utf-8");
@@ -2610,13 +2715,17 @@ export async function pipePluginResponsesWithStrip(
loggerLog("warn", `[tag-echo] stripped model-emitted render tag (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
log?.(`[tag-echo] stripped model-emitted render tag from plugin passthrough text`);
};
+ // #2190: log-only residue audit — see the twin above.
+ const onResidueWarn = (snippet: string) => {
+ loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
+ };
const onBiliDrop = (snippet: string) => {
loggerLog("warn", `[bili-artifact] stripped model-emitted internal artifact (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
log?.(`[bili-artifact] stripped model-emitted internal artifact from plugin passthrough text`);
};
const tagFilter = composeStreamFilters(
composeStreamFilters(
- createTagEchoFilter(onTagDrop),
+ createTagEchoFilter(onTagDrop, onResidueWarn, absorbInstructed, requestText),
createMarkerLineFilter((snippet) => {
loggerLog("warn", `[marker-echo] stripped model-emitted ACP confirmation marker (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
log?.(`[marker-echo] stripped model-emitted ACP confirmation marker from plugin passthrough text`);
@@ -2905,7 +3014,7 @@ export async function pipePluginResponsesWithStrip(
for (const k of ["item_id", "output_index", "summary_index"]) {
if (ev[k] !== undefined) meta[k] = ev[k];
}
- s = { filter: composeStreamFilters(createTagEchoFilter(onTagDrop), createBiliArtifactFilter(onBiliDrop)), type, field, meta };
+ s = { filter: composeStreamFilters(createTagEchoFilter(onTagDrop, onResidueWarn, absorbInstructed, requestText), createBiliArtifactFilter(onBiliDrop)), type, field, meta };
argStreams.set(key, s);
}
return s;
@@ -2949,6 +3058,8 @@ export async function pipePluginResponsesWithStrip(
try {
ev = JSON.parse(jsonStr) as Record;
} catch {
+ // #2190: unparseable frames bypass every filter — audit them.
+ auditRawForward(rawEvent);
await write(rawEvent + "\n\n");
continue;
}
@@ -3000,12 +3111,12 @@ export async function pipePluginResponsesWithStrip(
// The done is not visible text to the degenerate-turn retry below, so it
// is stripped and released directly.
if (type === "response.reasoning_summary_part.done") {
- const hadEcho = containsRenderTagText(jsonStr) || containsMarkerLineText(jsonStr) || containsBiliInternalText(jsonStr);
- if (hadEcho) sawStrippedEcho = true;
- const evOut = hadEcho ? stripResponsesText(ev) : ev;
- proseAcc += responsesEventText(evOut);
- const out = hadEcho ? rebuildEvent(rawEvent, evOut) : rawEvent + "\n\n";
- await write(flushArgTails() + flushTail(out));
+ // Unified ACP invariant: reasoning summaries are the
+ // thinking channel — byte-verbatim, no strip pass. The
+ // text still feeds proseAcc so degenerate-turn
+ // accounting sees it.
+ proseAcc += responsesEventText(ev);
+ await write(flushArgTails() + flushTail(rawEvent + "\n\n"));
continue;
}
if (type === "response.output_text.done" || type === "response.content_part.done" || type === "response.output_item.done") {
@@ -3013,11 +3124,15 @@ export async function pipePluginResponsesWithStrip(
// retryEmptyTurn): releasing it earlier would hand the
// client the echo's own text exactly when the retry is
// about to replace it.
- const hadEchoText = containsRenderTagText(jsonStr) || containsMarkerLineText(jsonStr) || containsBiliInternalText(jsonStr);
+ // #2190: degenerate-close residue never trips RENDER_TAG_DETECT — check its shape too.
+ // Whole-field tool-call emission (m00885) — an absorb-instructed
+ // turn may answer as prose; drop the span, not just the tags.
+ const dropEmission = absorbInstructed === true && containsToolCallEmissionText(jsonStr);
+ const hadEchoText = containsRenderTagText(jsonStr) || containsMarkerLineText(jsonStr) || containsBiliInternalText(jsonStr) || containsEchoResidue(jsonStr) || dropEmission;
if (hadEchoText) sawStrippedEcho = true;
let evOut = ev;
let rebuild = hadEchoText || retryRewritePending();
- if (rebuild) evOut = stripResponsesText(ev);
+ if (rebuild) evOut = stripResponsesText(ev, dropEmission, requestText);
rewriteRetryIds(evOut);
const doneText = responsesEventText(evOut);
heldVisibleChars += doneText.length;
@@ -3063,11 +3178,15 @@ export async function pipePluginResponsesWithStrip(
heldVisibleChars = 0;
// The completion frame itself closes the turn: strip it if it
// carries echoed text, rewrite retry ids onto the first attempt's.
- const hadEchoText = containsRenderTagText(jsonStr) || containsMarkerLineText(jsonStr) || containsBiliInternalText(jsonStr);
+ // #2190: degenerate-close residue never trips RENDER_TAG_DETECT — check its shape too.
+ // Whole-field tool-call emission (m00885) — an absorb-instructed
+ // turn may answer as prose; drop the span, not just the tags.
+ const dropEmission = absorbInstructed === true && containsToolCallEmissionText(jsonStr);
+ const hadEchoText = containsRenderTagText(jsonStr) || containsMarkerLineText(jsonStr) || containsBiliInternalText(jsonStr) || containsEchoResidue(jsonStr) || dropEmission;
if (hadEchoText) sawStrippedEcho = true;
let evOut = ev;
let rebuild = hadEchoText || retryRewritePending();
- if (rebuild) evOut = stripResponsesText(ev);
+ if (rebuild) evOut = stripResponsesText(ev, dropEmission, requestText);
rewriteRetryIds(evOut);
// Hosts finish on this frame, before EOF settles billing and late usage.
if (session && acc.inputTokens !== undefined && acc.inputTokens > 0) {
@@ -3084,10 +3203,15 @@ export async function pipePluginResponsesWithStrip(
await write(rawEvent + "\n\n");
continue;
}
- if (!retryRewritePending() && !mayStartRenderTag(delta) && !mayStartMarkerLine(delta) && !mayStartBiliInternal(delta) && !tagFilter.pending()) {
+ // Whole-field emission head (m00885): the filter's
+ // field-start hold must see the first bytes. Degenerate
+ // close (#2190) rides the same routing.
+ if (!retryRewritePending() && !mayStartRenderTag(delta) && !mayStartMarkerLine(delta) && !mayStartBiliInternal(delta) && !mayStartDegenerateRenderTag(delta) && !(absorbInstructed === true && mayStartToolCallEmission(delta)) && !tagFilter.pending()) {
proseAcc += delta;
visibleTextChars += delta.length;
fastPathChars += delta.length;
+ // #2190: residue audit — see the twin above.
+ if (delta.length > 0 && containsEchoResidue(delta)) loggerLog("warn", `[tag-echo] fast path forwarded echo-residue-shaped bytes (#2190): ${delta.slice(0, 80).replace(/\n/g, " ")}`);
await write(rawEvent + "\n\n");
continue;
}
@@ -3112,32 +3236,21 @@ export async function pipePluginResponsesWithStrip(
await write(rebuildEvent(rawEvent, rebuilt));
continue;
}
- // Reasoning summary deltas carry visible prose; function_call
- // argument deltas are user intent and pass through verbatim
- // (#1039), falling into the generic rawEvent write below.
- const argField = type === "response.reasoning_summary_text.delta" ? "delta" : null;
- if (argField !== null && typeof type === "string") {
- const v = ev[argField];
- if (typeof v !== "string" || v.length === 0) {
- await write(rawEvent + "\n\n");
- continue;
- }
- if (!mayStartRenderTag(v) && !mayStartBiliInternal(v) && !argAnyPending() && !tagFilter.pending()) {
- proseAcc += v;
- await write(rawEvent + "\n\n");
- continue;
- }
- const s = argStreamFor(type, argField, ev);
- const clean = s.filter.push(v);
- if (clean.length === 0) continue;
- proseAcc += clean;
- if (clean === v) {
- await write(flushArgTails() + rawEvent + "\n\n");
- continue;
- }
- await write(flushArgTails() + rebuildEvent(rawEvent, { ...ev, [argField]: clean }));
+ // Reasoning summary deltas are the thinking channel —
+ // byte-verbatim under the unified ACP invariant (never
+ // filtered). Function_call argument deltas are user intent
+ // and pass through verbatim (#1039), falling into the
+ // generic rawEvent write below.
+ if (type === "response.reasoning_summary_text.delta" && typeof ev["delta"] === "string") {
+ const v = ev["delta"] as string;
+ if (v.length > 0) proseAcc += v;
+ await write(rawEvent + "\n\n");
continue;
}
+ // #2248: raw exit — unrecognized event types never enter the
+ // filter. Tool-call argument streams are user intent (#1039)
+ // and are excluded from the audit.
+ if (!responsesFrameHasToolCalls(ev)) auditRawForward(rawEvent);
await write(rawEvent + "\n\n");
}
}
@@ -3210,7 +3323,7 @@ function rebuildEvent(rawEvent: string, ev: Record): string {
// `thought:true` reasoning parts share that one field. Mirrors
// stripOpenaiChatText / stripAnthropicText; mutates in place (the returned
// reference is the input's).
-function stripGoogleChunk(obj: T): T {
+function stripGoogleChunk(obj: T, drop: boolean = false, requestText?: string): T {
if (!obj || typeof obj !== "object") return obj;
const o = obj as Record;
const candidates = o["candidates"];
@@ -3227,8 +3340,9 @@ function stripGoogleChunk(obj: T): T {
content: {
...cont,
parts: (cont["parts"] as unknown[]).map((p) =>
- p && typeof p === "object" && typeof (p as Record)["text"] === "string"
- ? { ...(p as Record), text: stripAcpTags((p as Record)["text"] as string) }
+ // #1960/KDD#10: thought parts carry a thoughtSignature — leave them byte-for-byte.
+ p && typeof p === "object" && (p as Record)["thought"] !== true && typeof (p as Record)["text"] === "string"
+ ? { ...(p as Record), text: stripAcpTags((p as Record)["text"] as string, drop, requestText) }
: p,
),
},
@@ -3239,14 +3353,14 @@ function stripGoogleChunk(obj: T): T {
// Without `alt=sse` a Gemini streaming response is a JSON ARRAY of the same
// chunk objects, so the non-stream strip applies to each element as well.
-function stripGoogleText(obj: T): T {
+function stripGoogleText(obj: T, drop: boolean = false, requestText?: string): T {
if (Array.isArray(obj)) {
obj.forEach((c) => {
- stripGoogleChunk(c);
+ stripGoogleChunk(c, drop, requestText);
});
return obj;
}
- return stripGoogleChunk(obj);
+ return stripGoogleChunk(obj, drop, requestText);
}
export async function pipePluginJson(
@@ -3255,6 +3369,12 @@ export async function pipePluginJson(
session?: Session,
protocol?: WireProtocol,
upstreamOrigin?: string,
+ // m00885 provenance gate: the request body carried the kernel's
+ // "[ACP absorb]" instruction. Off ⇒ whole-field tool-call text in the
+ // JSON body is legitimate prose and survives the strip.
+ absorbInstructed?: boolean,
+ // m00885: echoed (user-requested verbatim) emission spans survive.
+ requestText?: string,
): Promise {
// Also serves proxy-mode JSON responses that skipped compress injection
// (#460 residual) — pass no session there so usage accounting stays off.
@@ -3321,16 +3441,21 @@ export async function pipePluginJson(
// one full-body parse this path already does. Observe-only (#1039).
if (session) recordJsonToolWitnesses(session.id, json, protocol);
} catch { /* non-JSON body — forward verbatim */ }
- if (json && (containsRenderTagText(text) || containsMarkerLineText(text) || containsBiliInternalText(text))) {
+ // #2190: degenerate-close residue never trips RENDER_TAG_DETECT — check its shape too.
+ // m00885: an absorb-instructed turn may answer with a whole-field
+ // tool-call emission written as prose — the raw-body probe is
+ // escape-tolerant so it fires on the serialized JSON too.
+ const dropEmission = absorbInstructed === true && containsToolCallEmissionText(text);
+ if (json && (dropEmission || containsRenderTagText(text) || containsMarkerLineText(text) || containsBiliInternalText(text) || containsEchoResidue(text))) {
// #206 parity for the non-streaming plugin path: the compress loop's
// JSON branch strips render tags from every round; a verbatim plugin
// JSON response would re-feed the model's tag echoes. Strips mutate in
// place, so this composes with the #408 usage backfill above — one
// parse, one reserialize.
- json = protocol === "responses" ? stripResponsesText(json)
- : protocol === "anthropic" ? stripAnthropicText(json)
- : protocol === "google" ? stripGoogleText(json)
- : stripOpenaiChatText(json);
+ json = protocol === "responses" ? stripResponsesText(json, dropEmission, requestText)
+ : protocol === "anthropic" ? stripAnthropicText(json, dropEmission, requestText)
+ : protocol === "google" ? stripGoogleText(json, dropEmission, requestText)
+ : stripOpenaiChatText(json, dropEmission, requestText);
mutated = true;
}
if (mutated && json) {
diff --git a/src/server.ts b/src/server.ts
index 34e641991e..0ac4090184 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -96,7 +96,7 @@ import { hoistTrappedToolItems } from "./tool-pair-order.js";
import { runCompressLoop, pickAdapter } from "./loop/index.js";
import { computeAnthropicMessageMarks, stampAnthropicSystemCacheControl, anthropicToolsCarryCacheControl } from "./loop/cache-control.js";
import { reconcileSystemAnchor } from "./system-anchor.js";
-import { containsToolCallXmlFragment } from "./loop/tag-echo-filter.js";
+import { ABSORB_INSTRUCTION_MARKER, containsToolCallXmlFragment } from "./loop/tag-echo-filter.js";
import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoReasoning, normalizeStrictEchoResponsesInput } from "./strict-echo.js";
export { isStrictReasoningEcho, normalizeStrictEchoReasoning, normalizeStrictEchoResponsesInput };
import { isFakeCompletion, injectFakeCompletionHint, maxFakeCompletionRetries, fakeBufCap } from "./fake-completion.js";
@@ -7092,6 +7092,17 @@ async function forward(
// opt-in #371 fake-completion backstop buffers + retries first, same as
// proxy mode (#473).
if (prepared?.pluginMode) {
+ // m00885 provenance gate: the request body is where the kernel
+ // injects its "[ACP absorb]" instruction, so only an absorb-instructed
+ // request may have its whole-field tool-call prose dropped. wireBody
+ // is the exact shipped bytes (string or Buffer — .includes(string)
+ // works on both).
+ const absorbInstructed = wireBody.includes(ABSORB_INSTRUCTION_MARKER);
+ // m00885: the shipped request text doubles as the echo provenance — a
+ // tool-call-shaped span the user asked to output verbatim sits in the
+ // request, so an identical span in the response is an echo, not a
+ // model-invented emission.
+ const wireBodyText = typeof wireBody === "string" ? wireBody : wireBody.toString("utf8");
// #411: clear the idle timer on every path — resolveFakeCompletion and
// other failures still escape these pipes; without a finally each one
// leaked a live idle timer. (#721: the SSE pipes themselves no longer
@@ -7140,6 +7151,8 @@ async function forward(
label: prepared.session.id,
}),
targetOrigin,
+ absorbInstructed,
+ wireBodyText,
);
} else {
// #732/#821: the plugin pipe re-issues the agent's own body
@@ -7164,10 +7177,12 @@ async function forward(
label: prepared.session.id,
}),
targetOrigin,
+ absorbInstructed,
+ wireBodyText,
);
}
} else {
- await pipePluginJson(pluginBody, res, prepared.session, prepared.protocol, targetOrigin);
+ await pipePluginJson(pluginBody, res, prepared.session, prepared.protocol, targetOrigin, absorbInstructed, wireBodyText);
}
} finally {
clearUpstreamTimer();
@@ -7384,7 +7399,7 @@ async function forward(
: "";
const visibilityMarkers = resolveCompress(opts.routes, route?.rewrittenUrl, (parsedReq as { model?: string }).model, opts.compress).visibilityMarkers ?? true;
const systemPrompt = withMarkerIntegrityNote(withSummaryBudgetNote(textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections)), visibilityMarkers) + absorbSection;
- const adapter = pickAdapter(prepared.protocol, parsedReq, textProtocol, prepared.responsesProjection, prepared.anthropicSystem, prepared.openaiSystemText, absorbActive ? absorbToolName(loopConfig) : undefined, prepared.google, prepared.systemNotes, opts.streamErrorShape, prepared.anthropicCacheMarks);
+ const adapter = pickAdapter(prepared.protocol, parsedReq, textProtocol, prepared.responsesProjection, prepared.anthropicSystem, prepared.openaiSystemText, absorbActive ? absorbToolName(loopConfig) : undefined, prepared.google, prepared.systemNotes, opts.streamErrorShape, prepared.anthropicCacheMarks, absorbActive);
const refreshFolded = async (current: CoreMessage[]): Promise => {
return withSessionLock(prepared.session, async () => {
// #422: mirror the prepare's fold with the post-compress state so
diff --git a/src/stream-google.ts b/src/stream-google.ts
index b4ef9505d3..9bd6f34900 100644
--- a/src/stream-google.ts
+++ b/src/stream-google.ts
@@ -1,11 +1,11 @@
import type { CompressionCore, Config, CoreMessage } from "acp-kernel";
import type { GooglePart } from "acp-kernel/wire";
import type { Session } from "./session.js";
-import { isProxyToolFor } from "./absorb.js";
+import { effectiveAbsorbConfig, isProxyToolFor } from "./absorb.js";
import { executeProxyTool } from "./loop/core.js";
import { drainPendingRetrievals } from "./store.js";
import type { RewriteCtx } from "./stream.js";
-import { containsBiliInternalText, containsMarkerLineText, containsRenderTagText, stripAcpTags } from "./loop/tag-echo-filter.js";
+import { containsBiliInternalText, containsMarkerLineText, containsRenderTagText, containsToolCallEmissionText, stripAcpTags } from "./loop/tag-echo-filter.js";
export function rewriteGoogleJsonResponse(body: unknown, ctx: RewriteCtx): unknown {
if (!body || typeof body !== "object") return body;
@@ -20,6 +20,10 @@ export function rewriteGoogleJsonResponse(body: unknown, ctx: RewriteCtx): unkno
let sawReal = false;
const noteParts: string[] = [];
const keptParts: GooglePart[] = [];
+ // Absorb signature in whole text (m00885): drop only when the request
+ // actually instructed the model about absorb.
+ const absorbArmed = effectiveAbsorbConfig(ctx.session, ctx.config)?.enabled === true;
+ const requestText = JSON.stringify(ctx.messages);
for (const part of parts) {
if (!part || typeof part !== "object") continue;
const fc = part.functionCall as { name?: unknown; args?: unknown; id?: unknown } | undefined;
@@ -30,9 +34,12 @@ export function rewriteGoogleJsonResponse(body: unknown, ctx: RewriteCtx): unkno
continue;
}
if (fc) sawReal = true;
- if (typeof part.text === "string" && (containsRenderTagText(part.text) || containsMarkerLineText(part.text) || containsBiliInternalText(part.text))) {
+ // #1960/KDD#10: a thought part carries a thoughtSignature the client
+ // echoes back verbatim — rewriting its text desyncs the signature and
+ // bricks replay. Strip prose only from non-thought parts.
+ if ((part as { thought?: boolean }).thought !== true && typeof part.text === "string" && (containsRenderTagText(part.text) || containsMarkerLineText(part.text) || containsBiliInternalText(part.text) || (absorbArmed && containsToolCallEmissionText(part.text)))) {
ctx.log(`[warn: tag echo] non-stream google output contains ACP echo (render tags/markers/internal artifacts), stripped: ${part.text.slice(0, 120).replace(/\n/g, " ")}`);
- part.text = stripAcpTags(part.text);
+ part.text = stripAcpTags(part.text, absorbArmed, requestText);
}
keptParts.push(part);
}
diff --git a/src/stream-openai.ts b/src/stream-openai.ts
index b3bcd4c2eb..5102f7b6d1 100644
--- a/src/stream-openai.ts
+++ b/src/stream-openai.ts
@@ -2,7 +2,8 @@ import type { CompressionCore, Config, CoreMessage } from "acp-kernel";
import type { Session } from "./session.js";
import { COMPRESS_TOOL_NAME, parseCompressInput } from "./compress-tool.js";
import { applyRanges, type RewriteCtx } from "./stream.js";
-import { containsBiliInternalText, containsMarkerLineText, containsRenderTagText, stripAcpTags } from "./loop/tag-echo-filter.js";
+import { effectiveAbsorbConfig } from "./absorb.js";
+import { containsBiliInternalText, containsMarkerLineText, containsRenderTagText, containsToolCallEmissionText, stripAcpTags } from "./loop/tag-echo-filter.js";
export function rewriteOpenaiJsonResponse(body: unknown, ctx: RewriteCtx): unknown {
if (!body || typeof body !== "object") return body;
@@ -41,9 +42,14 @@ export function rewriteOpenaiJsonResponse(body: unknown, ctx: RewriteCtx): unkno
ctx.log(`[warn: nameless tool call] non-stream openai response carries ${namelessToolCallIdx.length} nameless tool_call(s) at index ${namelessToolCallIdx.join(",")} — forwarded verbatim (#1501 observe-only)`);
}
}
- if (existingText && (containsRenderTagText(existingText) || containsMarkerLineText(existingText) || containsBiliInternalText(existingText))) {
+ // Absorb signature in whole text (m00885): drop only when the request
+ // actually instructed the model about absorb — the shape check alone
+ // cannot tell an emission from a user-quoted fragment.
+ const absorbArmed = effectiveAbsorbConfig(ctx.session, ctx.config)?.enabled === true;
+ const requestText = JSON.stringify(ctx.messages);
+ if (existingText && (containsRenderTagText(existingText) || containsMarkerLineText(existingText) || containsBiliInternalText(existingText) || (absorbArmed && containsToolCallEmissionText(existingText)))) {
ctx.log(`[warn: tag echo] non-stream openai output contains ACP echo (render tags/markers/internal artifacts), stripped: ${existingText.slice(0, 120).replace(/\n/g, " ")}`);
- existingText = stripAcpTags(existingText);
+ existingText = stripAcpTags(existingText, absorbArmed, requestText);
msg.content = existingText;
}
if (!converted) return body;
diff --git a/src/stream-responses.ts b/src/stream-responses.ts
index 58d474049e..45a51b2d74 100644
--- a/src/stream-responses.ts
+++ b/src/stream-responses.ts
@@ -2,7 +2,8 @@ import type { CompressionCore, Config, CoreMessage } from "acp-kernel";
import type { Session } from "./session.js";
import { COMPRESS_TOOL_NAME, parseCompressInput } from "./compress-tool.js";
import { applyRanges, type RewriteCtx } from "./stream.js";
-import { containsBiliInternalText, containsMarkerLineText, containsRenderTagText, stripResponsesText } from "./loop/tag-echo-filter.js";
+import { effectiveAbsorbConfig } from "./absorb.js";
+import { containsBiliInternalText, containsMarkerLineText, containsRenderTagText, containsToolCallEmissionText, stripResponsesText } from "./loop/tag-echo-filter.js";
/**
* Responses API (non-streaming) JSON rewriter: strips compress function_call
@@ -25,9 +26,13 @@ export function rewriteResponsesJsonResponse(body: unknown, ctx: RewriteCtx): un
// every body — before the compress note is synthesized (so the record we
// inject is never edited) and before the !converted early return below
// (which previously handed a tag-bearing body back untouched).
- if (containsRenderTagText(probe) || containsMarkerLineText(probe) || containsBiliInternalText(probe)) {
+ // Absorb signature in whole text (m00885): drop only when the request
+ // actually instructed the model about absorb.
+ const absorbArmed = effectiveAbsorbConfig(ctx.session, ctx.config)?.enabled === true;
+ const requestText = JSON.stringify(ctx.messages);
+ if (containsRenderTagText(probe) || containsMarkerLineText(probe) || containsBiliInternalText(probe) || (absorbArmed && containsToolCallEmissionText(probe))) {
ctx.log(`[warn: tag echo] non-stream responses output contains ACP echo (render tags/markers/internal artifacts), stripped: ${probe.slice(0, 120).replace(/\n/g, " ")}`);
- stripResponsesText(b);
+ stripResponsesText(b, absorbArmed, requestText);
}
let converted = false;
let sawReal = false;
diff --git a/src/stream.ts b/src/stream.ts
index 1a41b6488d..67cfe6c0f9 100644
--- a/src/stream.ts
+++ b/src/stream.ts
@@ -8,7 +8,7 @@ import { effectiveAbsorbConfig, executeAbsorb, isProxyToolFor } from "./absorb.j
import { executeSearchContextTarget, resolveDecompress } from "./decompress-shared.js";
import { adoptContentStore, contentStoreOf, ccrEnabled, drainPendingRetrievals, executeRetrieve, retrieveToolName } from "./store.js";
import { IMAGE_FULL_TOOL_NAME, executeImageFull, imageCompressionEnabled } from "./image-compress.js";
-import { containsBiliInternalText, containsMarkerLineText, containsRenderTagText, stripAcpTags } from "./loop/tag-echo-filter.js";
+import { containsBiliInternalText, containsMarkerLineText, containsRenderTagText, containsToolCallEmissionText, stripAcpTags } from "./loop/tag-echo-filter.js";
import { maxShrinkPerCompress } from "./fetch-util.js";
import { compressResult, toolFail, type ProxyToolResult } from "./proxy-tool-result.js";
import { attachSubagentSessions, subagentSessionNote, subagentSessionsOf, syncSubagentSessions } from "./subagent-sessions.js";
@@ -601,11 +601,16 @@ export function rewriteJsonResponse(body: unknown, ctx: RewriteCtx): unknown {
}
b.content = newContent;
if (converted && !sawRealToolUse) b.stop_reason = "end_turn";
+ // Absorb signature in whole text (m00885): drop only when the request
+ // actually instructed the model about absorb — the shape check alone
+ // cannot tell an emission from a user-quoted fragment.
+ const absorbArmed = effectiveAbsorbConfig(ctx.session, ctx.config)?.enabled === true;
+ const requestText = JSON.stringify(ctx.messages);
for (const blk of newContent) {
const t = (blk as { type?: string; text?: string }).text;
- if (typeof t === "string" && (containsRenderTagText(t) || containsMarkerLineText(t) || containsBiliInternalText(t))) {
+ if (typeof t === "string" && (containsRenderTagText(t) || containsMarkerLineText(t) || containsBiliInternalText(t) || (absorbArmed && containsToolCallEmissionText(t)))) {
ctx.log(`[warn: tag echo] non-stream model output contains ACP echo (render tags/markers/internal artifacts), stripped: ${t.slice(0, 120).replace(/\n/g, " ")}`);
- (blk as { text?: string }).text = stripAcpTags(t);
+ (blk as { text?: string }).text = stripAcpTags(t, absorbArmed, requestText);
}
}
return body;
diff --git a/tests/absorb-emission.test.ts b/tests/absorb-emission.test.ts
new file mode 100644
index 0000000000..49876375c8
--- /dev/null
+++ b/tests/absorb-emission.test.ts
@@ -0,0 +1,416 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import type { Config, CoreMessage } from "acp-kernel";
+import { createCore, createInitialState } from "acp-kernel";
+import type { Session } from "../src/session.ts";
+import { runCompressLoop, createOpenaiAdapter } from "../src/loop/index.ts";
+import {
+ toolCallEmissionSpan,
+ isToolCallEmission,
+ isOrphanToolCallTail,
+ containsToolCallEmissionText,
+ emissionEchoesRequest,
+ toolCallOpenStatus,
+ mayStartToolCallEmission,
+ mayStartRenderTag,
+ stripAcpTags,
+ createTagEchoFilter,
+ stripOpenaiChatText,
+ stripAnthropicText,
+ stripResponsesText,
+} from "../src/loop/tag-echo-filter.ts";
+import { buildCompressSystemPrompt } from "../src/compress-tool.ts";
+
+const LT = "\x3c";
+const GT = ">";
+const TAG = (ref: string, tokens = 177) => `${LT}acp tokens="${tokens}" type="text">${ref}${LT}/acp>`;
+
+// The production hybrid emission (opencode session ses_efe4cfcbdffe,
+// 2026-10-03): the absorb tool call written as chat-template text in the
+// prose channel, standing alone in the field.
+const EMISSION =
+ `\n${LT}parameter=ref${GT}\nm00608\n${LT}/parameter${GT}\n` +
+ `${LT}parameter=summary${GT}\nconsume the read output\n${LT}/parameter${GT}\n` +
+ `${LT}/function${GT}\n${LT}/function_calls${GT}`;
+
+function makeCtx(id: string): {
+ core: ReturnType;
+ config: Config;
+ messages: CoreMessage[];
+ session: Session;
+ log: (m: string) => void;
+} {
+ return {
+ core: createCore(),
+ config: { modelContextLimit: 200000 } as Config,
+ messages: [],
+ session: {
+ id,
+ meta: {},
+ stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0, compressCreditTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 },
+ pendingRetrievals: [],
+ metadata: {},
+ state: createInitialState(),
+ createdAt: Date.now(),
+ lastSeen: Date.now(),
+ blockContents: new Map(),
+ inFlight: 0,
+ persisted: false,
+ },
+ log: () => {},
+ };
+}
+
+function sseFromStrings(parts: string[]): ReadableStream {
+ const encoder = new TextEncoder();
+ let i = 0;
+ return new ReadableStream({
+ pull(controller) {
+ if (i >= parts.length) {
+ controller.close();
+ return;
+ }
+ controller.enqueue(encoder.encode(parts[i++]));
+ },
+ });
+}
+
+async function drain(stream: ReadableStream, adapter: Parameters[4]): Promise {
+ const ctx = makeCtx("absorb-emission-test");
+ const chunks: Buffer[] = [];
+ for await (const chunk of runCompressLoop(stream, ctx, {}, { url: "http://mock", headers: {} }, adapter, buildCompressSystemPrompt())) {
+ chunks.push(chunk);
+ }
+ return Buffer.concat(chunks).toString("utf8");
+}
+
+function sseDelta(content: string): string {
+ return `data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { content }, finish_reason: null }] })}\n\n`;
+}
+
+function sseParts(content: string): string[] {
+ return [
+ `data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }] })}\n\n`,
+ sseDelta(content),
+ `data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })}\n\n`,
+ `data: [DONE]\n\n`,
+ ];
+}
+
+function contentOf(out: string): string {
+ return [...out.matchAll(/"content":"((?:[^"\\]|\\.)*)"/g)].map((m) => JSON.parse(`"${m[1]}"`) as string).join("");
+}
+
+// ─── shape recognition ──────────────────────────────────────────────────────
+
+test("toolCallEmissionSpan recognizes the production hybrid shape (category 1)", () => {
+ const span = toolCallEmissionSpan(EMISSION);
+ assert.ok(span !== null);
+ assert.equal(span!.start, 1);
+ assert.equal(span!.end, EMISSION.length);
+ assert.equal(isToolCallEmission(EMISSION.trim()), true);
+});
+
+test("toolCallEmissionSpan keeps prose after the last close", () => {
+ const span = toolCallEmissionSpan(`\n ${EMISSION.trim()} and done.`);
+ assert.ok(span !== null);
+ assert.equal(span!.start, 3);
+ assert.equal(span!.end, 3 + EMISSION.trim().length);
+});
+
+test("toolCallEmissionSpan covers antml: and name= attribute forms (category 12)", () => {
+ const antml =
+ `${LT}antml:function_calls${GT}\n` +
+ `${LT}antml:parameter=ref${GT}\nm00101\n${LT}/antml:parameter${GT}\n` +
+ `${LT}antml:parameter=summary${GT}\ns\n${LT}/antml:parameter${GT}\n` +
+ `${LT}/antml:function${GT}\n${LT}/antml:function_calls${GT}`;
+ assert.ok(toolCallEmissionSpan(antml) !== null);
+ const named =
+ `${LT}function${GT}\n` +
+ `${LT}parameter name="ref"${GT} m00608 ${LT}/parameter${GT}\n` +
+ `${LT}parameter name="summary"${GT} s ${LT}/parameter${GT}\n` +
+ `${LT}/function${GT}`;
+ assert.ok(toolCallEmissionSpan(named) !== null);
+});
+
+test("toolCallEmissionSpan rejects non-emission fields", () => {
+ assert.equal(toolCallEmissionSpan("prose without markup"), null);
+ assert.equal(toolCallEmissionSpan(`${LT}parameter=ref${GT}abc123${LT}/parameter${GT}`), null);
+ assert.equal(toolCallEmissionSpan(`${LT}parameter=summary${GT}hello${LT}/parameter${GT}`), null);
+ assert.equal(toolCallEmissionSpan(
+ `mid ${LT}parameter=ref${GT}m00608${LT}/parameter${GT}${LT}parameter=summary${GT}s${LT}/parameter${GT}${LT}/function${GT}`
+ ), null);
+ // ref+summary signature without a call close is still the emission (span ends at the last close)
+ assert.equal(toolCallEmissionSpan(`${LT}parameter=ref${GT}m00608${LT}/parameter${GT}${LT}parameter=summary${GT}s${LT}/parameter${GT}`)!.end,
+ `${LT}parameter=ref${GT}m00608${LT}/parameter${GT}${LT}parameter=summary${GT}s${LT}/parameter${GT}`.length);
+ // no closes at all → not an emission
+ assert.equal(toolCallEmissionSpan(`${LT}parameter=ref${GT}m00608${LT}parameter=summary${GT}s`), null);
+});
+
+test("ordinary XML in the summary body does not defeat the shape (category 9)", () => {
+ const xml = EMISSION.replace("consume the read output", `${LT}div class="x"${GT}hi${LT}/div${GT}`);
+ assert.ok(isToolCallEmission(xml.trim()));
+ assert.equal(stripAcpTags(xml, true), "");
+ assert.equal(stripAcpTags(xml, false), xml);
+});
+
+// ─── stripAcpTags: armed drop, echo, not-armed ──────────────────────────────
+
+test("stripAcpTags drops the whole emission when armed (category 1)", () => {
+ assert.equal(stripAcpTags(EMISSION, true), "");
+ assert.equal(stripAcpTags(EMISSION.trim() + " and done.", true), " and done.");
+});
+
+test("stripAcpTags leaves the emission untouched when not armed (shape alone never decides)", () => {
+ assert.equal(stripAcpTags(EMISSION, false), EMISSION);
+ assert.equal(stripAcpTags(EMISSION), EMISSION);
+});
+
+test("stripAcpTags keeps an emission that echoes the request verbatim (m00885, category 10)", () => {
+ const requestText = JSON.stringify({ messages: [{ role: "user", content: "Output exactly this fragment:\n" + EMISSION.trim() }] });
+ assert.equal(stripAcpTags(EMISSION, true, requestText), EMISSION);
+});
+
+test("stripAcpTags drops a model-invented emission when the request quotes a different ref (m00885 negative)", () => {
+ const other = EMISSION.trim().replace("m00608", "m00999");
+ const requestText = JSON.stringify({ messages: [{ role: "user", content: "Output exactly this fragment:\n" + other }] });
+ assert.equal(stripAcpTags(EMISSION, true, requestText), "");
+});
+
+test("stripAcpTags armed: prose discussing the markup passes untouched (category 10)", () => {
+ const prose = `The markup looks like ${LT}parameter=ref${GT}m00608${LT}/parameter${GT} inside a call.`;
+ assert.equal(stripAcpTags(prose, true), prose);
+});
+
+test("stripAcpTags armed: legitimate HTML passes untouched (category 11)", () => {
+ assert.equal(stripAcpTags(`${LT}div class="x"${GT}hi${LT}/div${GT}`, true), `${LT}div class="x"${GT}hi${LT}/div${GT}`);
+});
+
+test("stripAcpTags armed: render tag adjacent to an emission (category 6)", () => {
+ assert.equal(stripAcpTags(EMISSION.trim() + ` ${TAG("m00155")}ok`, true), " ok");
+});
+
+test("mangled open is stripped and seen by the fast-path gate (family A, category 3)", () => {
+ assert.equal(stripAcpTags(`${LT}acp=1${GT}text`), "text");
+ assert.equal(stripAcpTags(`text${LT}/acp${GT}`), "text");
+ assert.ok(mayStartRenderTag(`${LT}acp=1${GT}`));
+ assert.ok(mayStartRenderTag(`${LT}acp=`));
+});
+
+// ─── emissionEchoesRequest ──────────────────────────────────────────────────
+
+test("emissionEchoesRequest: literal containment", () => {
+ assert.ok(emissionEchoesRequest(EMISSION.trim(), `prefix ${EMISSION.trim()} suffix`));
+});
+
+test("emissionEchoesRequest: JSON-escaped form of the same fragment", () => {
+ const requestText = JSON.stringify({ content: EMISSION.trim() });
+ assert.ok(emissionEchoesRequest(EMISSION.trim(), requestText));
+ assert.ok(!emissionEchoesRequest(EMISSION.trim(), JSON.stringify({ content: "nothing here" })));
+ assert.ok(!emissionEchoesRequest(EMISSION.trim(), undefined));
+ assert.ok(!emissionEchoesRequest(EMISSION.trim(), ""));
+});
+
+// ─── streaming filter ───────────────────────────────────────────────────────
+
+test("streaming: armed filter drops an emission split across chunks (category 4)", () => {
+ const f = createTagEchoFilter(undefined, undefined, true);
+ let out = "";
+ for (let i = 0; i < EMISSION.length; i += 7) out += f.push(EMISSION.slice(i, i + 7));
+ out += f.flush();
+ assert.equal(out, "");
+});
+
+test("streaming: armed filter drops the emission and keeps the trailing prose", () => {
+ const f = createTagEchoFilter(undefined, undefined, true);
+ let out = f.push(EMISSION.trim());
+ out += f.push(` ${TAG("m00155")}ok`);
+ out += f.flush();
+ assert.equal(out, " ok");
+});
+
+test("streaming: armed filter keeps a verbatim echo of the request's fragment (m00885)", () => {
+ const requestText = JSON.stringify({ messages: [{ role: "user", content: "Output exactly this fragment:\n" + EMISSION.trim() }] });
+ const f = createTagEchoFilter(undefined, undefined, true, requestText);
+ let out = "";
+ for (let i = 0; i < EMISSION.length; i += 11) out += f.push(EMISSION.slice(i, i + 11));
+ out += f.flush();
+ assert.equal(out, EMISSION);
+});
+
+test("streaming: armed filter keeps prose heads flowing immediately (no delay, no loss)", () => {
+ const f = createTagEchoFilter(undefined, undefined, true);
+ assert.equal(f.push("Hello "), "Hello ");
+ assert.equal(f.push("world."), "world.");
+ assert.equal(f.flush(), "");
+});
+
+test("streaming: armed filter disambiguates from {
+ const f = createTagEchoFilter(undefined, undefined, true);
+ let out = f.push(`${LT}paragraph${GT}`);
+ out += f.push(`hello${LT}/paragraph${GT}`);
+ out += f.flush();
+ assert.equal(out, `${LT}paragraph${GT}hello${LT}/paragraph${GT}`);
+});
+
+test("streaming: not-armed filter passes the emission through", () => {
+ const f = createTagEchoFilter();
+ let out = "";
+ for (let i = 0; i < EMISSION.length; i += 13) out += f.push(EMISSION.slice(i, i + 13));
+ out += f.flush();
+ assert.equal(out, EMISSION);
+});
+
+// ─── unit machinery ─────────────────────────────────────────────────────────
+
+test("toolCallOpenStatus: open / prefix / none", () => {
+ assert.equal(toolCallOpenStatus(`${LT}parameter=ref${GT}`), "open");
+ assert.equal(toolCallOpenStatus(`${LT}function${GT}`), "open");
+ assert.equal(toolCallOpenStatus(`${LT}function `), "open");
+ assert.equal(toolCallOpenStatus(`${LT}parameter`), "prefix");
+ assert.equal(toolCallOpenStatus(`${LT}para`), "prefix");
+ assert.equal(toolCallOpenStatus(`${LT}p`), "prefix");
+ assert.equal(toolCallOpenStatus(`${LT}`), "prefix");
+ assert.equal(toolCallOpenStatus(`${LT}div${GT}hi${LT}/div${GT}`), "none");
+ assert.equal(toolCallOpenStatus(`${LT}paragraph${GT}`), "none");
+ assert.equal(toolCallOpenStatus(`${LT}/function${GT}`), "none");
+ assert.equal(toolCallOpenStatus(`${LT}antml:function`), "prefix");
+ assert.equal(toolCallOpenStatus(`${LT}antml:function `), "open");
+});
+
+test("mayStartToolCallEmission flags chunks containing or ending on a tool-call open", () => {
+ assert.ok(mayStartToolCallEmission(`x ${LT}function_calls`));
+ assert.ok(mayStartToolCallEmission(`${LT}antml:parameter=ref`));
+ assert.ok(!mayStartToolCallEmission("no markup here"));
+ assert.ok(!mayStartToolCallEmission(`${LT}div${GT}hi${LT}/div${GT}`));
+});
+
+test("isOrphanToolCallTail: close-headed field of refs and whitespace only (category 7)", () => {
+ assert.ok(isOrphanToolCallTail(`${LT}/function${GT}\nm00608\n${LT}/parameter${GT}`));
+ assert.ok(isOrphanToolCallTail(`${LT}/function_calls${GT}m00608`));
+ assert.ok(!isOrphanToolCallTail(`${LT}/function${GT}hello there`));
+ assert.ok(!isOrphanToolCallTail(`${LT}parameter=ref${GT}m00608${LT}/parameter${GT}`));
+ assert.ok(!isOrphanToolCallTail("m00608 only"));
+});
+
+test("containsToolCallEmissionText sees the emission shape in raw JSON wire text (category 13)", () => {
+ assert.ok(containsToolCallEmissionText(JSON.stringify({ content: EMISSION.trim() })));
+ assert.ok(!containsToolCallEmissionText(JSON.stringify({ content: "plain prose" })));
+});
+
+// ─── JSON field strippers ───────────────────────────────────────────────────
+
+type OpenAiBody = {
+ choices: Array<{
+ delta?: { content?: string; reasoning_content?: string; reasoning?: string };
+ message?: { content?: string; reasoning_content?: string; reasoning?: string };
+ }>;
+};
+
+test("stripOpenaiChatText: armed drop / echo / not-armed (delta and message)", () => {
+ const make = (): OpenAiBody => JSON.parse(JSON.stringify({
+ choices: [
+ { delta: { content: EMISSION, reasoning_content: EMISSION } },
+ { message: { content: EMISSION } },
+ ],
+ }));
+ const armed = stripOpenaiChatText(make(), true);
+ assert.equal(armed.choices[0].delta?.content, "");
+ // Unified ACP invariant: reasoning fields ride verbatim in the strip
+ // functions too (defense in depth with the adapters' identity filters).
+ assert.equal(armed.choices[0].delta?.reasoning_content, EMISSION);
+ assert.equal(armed.choices[1].message?.content, "");
+ const echo = JSON.stringify({ messages: [{ role: "user", content: EMISSION.trim() }] });
+ const kept = stripOpenaiChatText(make(), true, echo);
+ assert.equal(kept.choices[0].delta?.content, EMISSION);
+ assert.equal(kept.choices[1].message?.content, EMISSION);
+ const unarmed = stripOpenaiChatText(make(), false);
+ assert.equal(unarmed.choices[0].delta?.content, EMISSION);
+});
+
+type AnthropicBody = {
+ delta?: { type: string; thinking?: string; text?: string };
+ content?: Array<{ type: string; thinking?: string; text?: string }>;
+};
+
+test("stripAnthropicText: signed thinking stays byte-for-byte even when armed (#1960)", () => {
+ const make = (): AnthropicBody => JSON.parse(JSON.stringify({
+ delta: { type: "thinking_delta", thinking: EMISSION },
+ content: [
+ { type: "thinking", thinking: EMISSION },
+ { type: "text", text: EMISSION },
+ ],
+ }));
+ // #1960/KDD#10: signed thinking is verified against its signature on replay —
+ // any rewrite desyncs it and bricks the session. So the emission drop and tag
+ // strip apply to the TEXT channel only; thinking rides byte-for-byte whether
+ // armed or not (matching the loop adapters' treatment).
+ const armed = stripAnthropicText(make(), true);
+ assert.equal(armed.delta?.thinking, EMISSION, "armed leaves the signed thinking delta byte-for-byte");
+ assert.equal(armed.content![0].thinking, EMISSION, "armed leaves the signed thinking block byte-for-byte");
+ assert.equal(armed.content![1].text, "", "text channel still drops the whole emission when armed");
+});
+
+type ResponsesEvent = {
+ type: string;
+ text?: string;
+ part?: { type: string; text?: string };
+ item?: { type: string; content?: Array<{ type: string; text?: string }> };
+ response?: { output?: Array<{ content?: Array<{ type: string; text?: string }> }> };
+ output?: Array<{ content?: Array<{ type: string; text?: string }> }>;
+};
+
+test("stripResponsesText: armed drop / echo / not-armed (category 13)", () => {
+ const make = (): ResponsesEvent => JSON.parse(JSON.stringify({
+ type: "response.output_text.done",
+ text: EMISSION,
+ part: { type: "output_text", text: EMISSION },
+ }));
+ const armed = stripResponsesText(make(), true);
+ assert.equal(armed.text, "");
+ assert.equal(armed.part?.text, "");
+ const echo = JSON.stringify({ messages: [{ role: "user", content: EMISSION.trim() }] });
+ assert.equal(stripResponsesText(make(), true, echo).text, EMISSION);
+ assert.equal(stripResponsesText(make(), false).text, EMISSION);
+ const done = (): ResponsesEvent => JSON.parse(JSON.stringify({
+ type: "response.completed",
+ response: { output: [{ content: [{ type: "output_text", text: EMISSION }] }] },
+ }));
+ assert.equal(stripResponsesText(done(), true).response?.output?.[0].content?.[0].text, "");
+});
+
+// ─── full loop through the real adapter (category 13) ──────────────────────
+
+test("openai adapter loop: armed drop removes the emission from the wire", async () => {
+ const adapter = createOpenaiAdapter({ model: "gpt" }, undefined, "absorb");
+ const out = await drain(sseFromStrings(sseParts(EMISSION.trim())), adapter);
+ assert.ok(!out.includes("parameter=ref"), out);
+ assert.equal(contentOf(out), "");
+});
+
+test("openai adapter loop: verbatim echo of the user's fragment survives armed (m00885, category 10)", async () => {
+ const reqBody = {
+ model: "gpt",
+ messages: [{ role: "user", content: "Output exactly this fragment and nothing else:\n" + EMISSION.trim() }],
+ };
+ const adapter = createOpenaiAdapter(reqBody, undefined, "absorb");
+ const out = await drain(sseFromStrings(sseParts(EMISSION.trim())), adapter);
+ assert.equal(contentOf(out), EMISSION.trim());
+});
+
+test("field-start release keeps a partial tag head held across the boundary (no wipe)", () => {
+ // Absorb-instructed: the first field bytes are prose, so the whole-field
+ // hold releases mid-chunk. A tag-head-shaped tail released that way must
+ // stay held for the next delta — clearing the shared buffer after
+ // processing would wipe it and leak the tag head as fresh prose.
+ const f = createTagEchoFilter(undefined, undefined, true);
+ assert.equal(f.push("Check m00001rest"), "rest");
+ assert.equal(f.flush(), "");
+});
+
+test("openai adapter loop: not-armed passes the emission through (shape alone never decides)", async () => {
+ const adapter = createOpenaiAdapter({ model: "gpt" });
+ const out = await drain(sseFromStrings(sseParts(EMISSION.trim())), adapter);
+ assert.equal(contentOf(out), EMISSION.trim());
+});
diff --git a/tests/fix-1039-toolargs-verbatim.test.ts b/tests/fix-1039-toolargs-verbatim.test.ts
index 07a98ef9da..1848208014 100644
--- a/tests/fix-1039-toolargs-verbatim.test.ts
+++ b/tests/fix-1039-toolargs-verbatim.test.ts
@@ -238,7 +238,7 @@ test("#1039 responses: function_call_arguments deltas and done payloads pass thr
const completed = parsed.find((e) => e["type"] === "response.completed") ?? {};
const outItem = asObj(asArr(asObj(completed["response"])["output"])[0]);
assert.equal(asString(outItem["arguments"]), dirtyArgs, "response.completed arguments untouched");
- assert.ok(!text.includes("m00009"), "reasoning summary tag ref still stripped");
+ assert.ok(text.includes("m00009"), "unified invariant: reasoning summary rides verbatim (#2267)");
});
test("#1039 non-streaming: pipePluginJson leaves tool-call argument payloads verbatim, strips prose tags", async () => {
diff --git a/tests/fix-1760-cjk-single-line-visible-turn.test.ts b/tests/fix-1760-cjk-single-line-visible-turn.test.ts
index 9c5787b567..a8cd22590b 100644
--- a/tests/fix-1760-cjk-single-line-visible-turn.test.ts
+++ b/tests/fix-1760-cjk-single-line-visible-turn.test.ts
@@ -92,6 +92,12 @@ test("isOrphanMarkupText classifies a released tail by its bytes, not its origin
assert.equal(isOrphanMarkupText("OK"), false);
// A lone astral icon held to terminal is preserved content the client sees.
assert.equal(isOrphanMarkupText("📦"), false);
+ // A refs run released at EOF is a genuine citation — visible prose
+ // (#2023 review: counting it as residue fired the one-shot retry on
+ // bare-citation answers).
+ assert.equal(isOrphanMarkupText("m01233"), false, "a lone ref citation is prose");
+ assert.equal(isOrphanMarkupText(" m01217\u2013m01233"), false, "a ref range is prose");
+ assert.equal(isOrphanMarkupText("m01095\u2013m01215, m01217\u2013m01233"), false, "a comma range list is prose");
// Markup-shaped tails stay residue (the #870 contract).
assert.equal(isOrphanMarkupText("\x3ca"), true, "partial render-tag head");
assert.equal(isOrphanMarkupText('\x3cacp tokens="1" type="text"'), true, "unclosed render-tag opening");
@@ -177,6 +183,28 @@ test("#870 kept: a tail shaped like an unclosed render tag still retries once",
assert.ok(!text.includes("[ACP] stream error"), "one retry suffices — no in-band error");
});
+test("#2023 review: a turn whose only answer is a bare ref citation does not retry", async () => {
+ const out: string[] = [];
+ let calls = 0;
+ const refetch = () => {
+ calls += 1;
+ return Promise.resolve(streamOf(proseTurn("unwanted")));
+ };
+ await pipePluginChatWithStrip(
+ streamOf([chatChunk({ role: "assistant" }), ...[..."m01233"].map((c) => chatChunk({ content: c })), chatStop(), DONE]),
+ makeRes(out),
+ "openai",
+ makeSession(),
+ undefined,
+ refetch,
+ );
+ const text = out.join("");
+ assert.equal(calls, 0, "a released refs-run tail is visible output — no nudge re-issue");
+ assert.ok(!text.includes("[ACP] stream error"), "no in-band degenerate error");
+ assert.equal(textDeltas(text), "m01233", "the citation reaches the client");
+ assert.equal((text.match(/\[DONE\]/g) ?? []).length, 1, "exactly one terminator");
+});
+
test("multi-line CJK answer in one chunk stays clean (never affected)", async () => {
const out: string[] = [];
let calls = 0;
diff --git a/tests/issue1881-reasoning-tag-leak.test.ts b/tests/issue1881-reasoning-tag-leak.test.ts
index bfcda4a221..d6352b084a 100644
--- a/tests/issue1881-reasoning-tag-leak.test.ts
+++ b/tests/issue1881-reasoning-tag-leak.test.ts
@@ -21,6 +21,8 @@ import { stripAcpTags, createTagEchoFilter } from "../src/loop/tag-echo-filter.t
const TAG = "\x3cacp tokens=\"2\" type=\"text\"\x3em00001\x3c/acp\x3e";
const BARE = "\x3cacp\x3em00001\x3c/acp\x3e";
+const TAG_JSON = JSON.stringify(TAG).slice(1, -1); // TAG as it appears in JSON frame bytes
+
async function collect(adapter: CompressLoopAdapter, body: string): Promise {
const events: ParsedStreamEvent[] = [];
for await (const ev of adapter.parseStream(new Response(body, { status: 200 }).body!, 1)) events.push(ev);
@@ -59,7 +61,7 @@ test("#1881 streaming filter: a bare pair splits cleanly at EVERY split point",
}
});
-test("#1881 openai: reasoning_content echo is stripped; same-frame content arrives once, cleaned", async () => {
+test("unified invariant: openai reasoning_content rides VERBATIM; same-frame content arrives once, cleaned", async () => {
const adapter = createOpenaiAdapter({ model: "gpt" });
const events = await collect(
adapter,
@@ -68,11 +70,14 @@ test("#1881 openai: reasoning_content echo is stripped; same-frame content arriv
"data: [DONE]\n\n",
);
const out = clientBytes(events);
- assert.ok(!out.includes("\x3cacp"), `no render tag may reach the client, got: ${out}`);
+ // Thinking channel: byte-verbatim (unified ACP invariant, #2229 direction).
+ assert.ok(out.includes(TAG_JSON), `reasoning_content is the thinking channel and rides verbatim, got: ${out}`);
+ // Visible text channel: still cleaned, exactly once.
assert.equal((out.match(/kept/g) ?? []).length, 1, `content arrives once, got: ${out}`);
+ assert.ok(out.includes(`\"content\":\"kept\"`), `content arrives cleaned of the tag, got: ${out}`);
});
-test("#1881 openai: a tool-frame replay carries cleaned reasoning bytes, arguments verbatim", async () => {
+test("unified invariant: a tool-frame replay carries reasoning VERBATIM, arguments verbatim", async () => {
const adapter = createOpenaiAdapter({ model: "gpt" });
const args = JSON.stringify({ path: "src/plugin.ts" });
const events = await collect(
@@ -82,7 +87,7 @@ test("#1881 openai: a tool-frame replay carries cleaned reasoning bytes, argumen
"data: [DONE]\n\n",
);
const out = clientBytes(events);
- assert.ok(!out.includes("\x3cacp"), `replayed frame must not carry the echo, got: ${out}`);
+ assert.ok(out.includes(TAG_JSON), `reasoning bytes ride verbatim (thinking channel), got: ${out}`);
assert.ok(out.includes('"name":"read"'), `the real call still reaches the client: ${out}`);
// args is a STRING field of the frame: byte-exact means its JSON-escaped
// form inside the frame equals JSON.stringify(args) (#1039).
@@ -106,7 +111,7 @@ test("#1960 anthropic: signed thinking_delta echo rides VERBATIM (filtering bric
assert.ok(out.includes("plan first"), `thinking prose survives: ${out}`);
});
-test("#1881 responses: reasoning_summary_text.delta echo is stripped, prose survives", async () => {
+test("unified invariant: responses reasoning_summary_text.delta rides VERBATIM", async () => {
const adapter = createResponsesAdapter(false);
const events = await collect(
adapter,
@@ -114,7 +119,8 @@ test("#1881 responses: reasoning_summary_text.delta echo is stripped, prose surv
sse({ type: "response.completed", response: { id: "r1", status: "completed", output: [], usage: { input_tokens: 1, output_tokens: 1 } } }),
);
const out = clientBytes(events);
- assert.ok(!out.includes("\x3cacp"), `no render tag may reach the client, got: ${out}`);
+ // Thinking channel: byte-verbatim (unified ACP invariant).
+ assert.ok(out.includes(TAG_JSON), `reasoning summary deltas ride verbatim, got: ${out}`);
assert.ok(out.includes("think more"), `reasoning prose survives: ${out}`);
});
diff --git a/tests/issue2023-refs-residue.test.ts b/tests/issue2023-refs-residue.test.ts
new file mode 100644
index 0000000000..a5eb22f74d
--- /dev/null
+++ b/tests/issue2023-refs-residue.test.ts
@@ -0,0 +1,113 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { stripAcpTags, createTagEchoFilter } from "../src/loop/tag-echo-filter.ts";
+
+const LT = "\x3c";
+const OPEN = `${LT}acp tokens="2" type="text">`;
+const BARE_OPEN = `${LT}acp>`;
+const CLOSE = `${LT}/acp>`;
+
+function streamAll(input: string): string {
+ const f = createTagEchoFilter();
+ let out = "";
+ for (const ch of input) out += f.push(ch);
+ out += f.flush();
+ return out;
+}
+
+function streamChunks(chunks: string[]): string {
+ const f = createTagEchoFilter();
+ let out = "";
+ for (const c of chunks) out += f.push(c);
+ out += f.flush();
+ return out;
+}
+
+test("whole-text: orphan refs run + unmatched close is stripped as a unit (#2023)", () => {
+ assert.equal(stripAcpTags(`done folding m01233${CLOSE} next`), `done folding next`);
+ assert.equal(stripAcpTags(`m01233${CLOSE}`), "");
+});
+
+test("whole-text: orphan unit survives case drift, typo close, junk close", () => {
+ assert.equal(stripAcpTags(`m01233${LT}/ACP>`), "");
+ assert.equal(stripAcpTags(`m01233${LT}/cap>`), "");
+ assert.equal(stripAcpTags(`m01233${LT}/acp junk>`), "");
+});
+
+test("whole-text: multi-ref and range bodies are tag content (refs-only PAIRED)", () => {
+ assert.equal(stripAcpTags(`${OPEN}m01233 m01234${CLOSE}`), "");
+ assert.equal(stripAcpTags(`${BARE_OPEN}m01217\u2013m01233${CLOSE}`), "");
+ assert.equal(stripAcpTags(`${BARE_OPEN}m01217\u2014m01233${CLOSE}`), "");
+ assert.equal(stripAcpTags(`${BARE_OPEN}m01217-m01233${CLOSE}`), "");
+ assert.equal(stripAcpTags(`${OPEN}m01095\u2013m01215, m01217\u2013m01233${CLOSE}`), "");
+});
+
+test("whole-text: #1720 prose-wearing pairs keep their content, cited refs included", () => {
+ assert.equal(stripAcpTags(`${OPEN}x m01233${CLOSE}`), "x m01233");
+ assert.equal(stripAcpTags(`${BARE_OPEN}x m01233${CLOSE}`), "x m01233");
+ assert.equal(stripAcpTags(`see m01233 for details`), `see m01233 for details`);
+ assert.equal(stripAcpTags(`xm01233${CLOSE}`), "xm01233");
+ assert.equal(stripAcpTags(`m01233${LT}/kem>`), `m01233${LT}/kem>`);
+});
+
+test("whole-text: run before an empty pair is a documented residual (leaks as before)", () => {
+ assert.equal(stripAcpTags(`m01233${BARE_OPEN}${CLOSE}`), "m01233");
+});
+
+test("whole-text: a ref right after a tag terminator stays lossless (#644 over-cap residue)", () => {
+ assert.equal(stripAcpTags(`>m01233${CLOSE}`), ">m01233");
+ assert.equal(stripAcpTags(`a>m01233${CLOSE}`), "a>m01233");
+});
+
+test("whole-text: multiple unmatched closes — only the ref-adjacent one forms a unit", () => {
+ assert.equal(stripAcpTags(`m01233${CLOSE} extra${CLOSE}`), " extra");
+});
+
+test("streaming char-by-char matches whole-text on every #2023 shape", () => {
+ const cases = [
+ `done folding m01233${CLOSE} next`,
+ `m01233${CLOSE}`,
+ `m01233${LT}/ACP>`,
+ `m01233${LT}/cap>`,
+ `${OPEN}m01233 m01234${CLOSE}`,
+ `${BARE_OPEN}m01217\u2013m01233${CLOSE}`,
+ `${OPEN}x m01233${CLOSE}`,
+ `${BARE_OPEN}x m01233${CLOSE}`,
+ `see m01233 for details`,
+ `xm01233${CLOSE}`,
+ `m01233${BARE_OPEN}${CLOSE}`,
+ `m01233${CLOSE} extra${CLOSE}`,
+ `>m01233${CLOSE}`,
+ `a>m01233${CLOSE}`,
+ ];
+ for (const input of cases) {
+ assert.equal(streamAll(input), stripAcpTags(input), `streaming diverged for: ${JSON.stringify(input)}`);
+ }
+});
+
+test("streaming: chunk boundary splits do not leak the orphan unit", () => {
+ assert.equal(streamChunks(["folded m01233", `${CLOSE} tail`]), "folded tail");
+ assert.equal(streamChunks(["m012", `33${CLOSE}`]), "");
+ assert.equal(streamChunks(["m0123", `3${CLOSE}`]), "");
+});
+
+test("streaming: EOF after an opening — refs-only tails drop, mixed tails release", () => {
+ assert.equal(streamChunks([BARE_OPEN, "m01233"]), "");
+ assert.equal(streamChunks([`${LT}acp tokens="2" type="text">`, "m01233"]), "");
+ assert.equal(streamChunks([`${LT}acp tokens="2" type="text">`, "m01233 m01234"]), "");
+ assert.equal(streamChunks([BARE_OPEN, "see m01233"]), "see m01233");
+ assert.equal(streamChunks(["note m01233"]), "note m01233");
+});
+
+test("streaming: swallow inner decision — refs-only body drops, prose body releases", () => {
+ assert.equal(streamChunks([`${LT}acp tokens="2" type="text">`, `m01233 m01234${CLOSE}`]), "");
+ assert.equal(streamChunks([`${LT}acp tokens="2" type="text">`, `x m01233${CLOSE}`]), "x m01233");
+});
+
+test("streaming: over-budget refs-only tail discards, over-budget mixed tail releases", () => {
+ const refsTail = Array.from({ length: 25 }, (_, i) => `m${String(10001 + i).padStart(5, "0")} `).join("");
+ assert.ok(refsTail.length > 80);
+ assert.equal(streamChunks([BARE_OPEN, refsTail]), "");
+ const mixedTail = `see ${refsTail}`;
+ assert.equal(streamChunks([BARE_OPEN, mixedTail]), mixedTail);
+});
diff --git a/tests/issue2190-degen-close.test.ts b/tests/issue2190-degen-close.test.ts
new file mode 100644
index 0000000000..0137ce55ac
--- /dev/null
+++ b/tests/issue2190-degen-close.test.ts
@@ -0,0 +1,413 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import {
+ composeStreamFilters,
+ containsEchoResidue,
+ createBiliArtifactFilter,
+ createMarkerLineFilter,
+ createTagEchoFilter,
+ isOrphanMarkupText,
+ mayStartBiliInternal,
+ mayStartDegenerateRenderTag,
+ mayStartMarkerLine,
+ mayStartRenderTag,
+ stripAcpTags,
+} from "../src/loop/tag-echo-filter.ts";
+import { pipePluginResponsesWithStrip } from "../src/plugin.ts";
+import type { Session } from "../src/session.ts";
+
+const OPEN = "\x3cacp tokens=\"23\" type=\"text\"\x3e";
+const REF = "m05712";
+
+function streamThrough(chunks: string[]): string {
+ const tagFilter = composeStreamFilters(
+ composeStreamFilters(createTagEchoFilter(), createMarkerLineFilter()),
+ createBiliArtifactFilter(),
+ );
+ let out = "";
+ for (const delta of chunks) {
+ if (!mayStartRenderTag(delta) && !mayStartMarkerLine(delta) && !mayStartBiliInternal(delta) && !mayStartDegenerateRenderTag(delta) && !tagFilter.pending()) {
+ out += delta;
+ continue;
+ }
+ out += tagFilter.push(delta);
+ }
+ return out + tagFilter.flush();
+}
+
+test("#2190 streaming: real field chunk sequence with degenerate close strips the residue", () => {
+ const chunks = ["\x3c", "ac", "p", " tokens", "=\u0022", "173", "\u0022", " type", "=\u0022", "text", "\u0022", "\u003e", "m", "057", "12", "\u003c/", "ap", "\u003e\n", "Let me first survey the workspace."];
+ const out = streamThrough(chunks);
+ assert.ok(!out.includes(REF), "ref must not leak");
+ assert.ok(!out.includes("\u003c/ap\u003e"), "degenerate close must not leak");
+ assert.ok(!out.includes("\u003cacp"), "open tag must not leak");
+ assert.equal(out, "\nLet me first survey the workspace.", "surrounding prose survives verbatim");
+});
+
+test("#2190 streaming: every attested degenerate close name is stripped character-by-character", () => {
+ for (const name of ["ap", "p", "a", "apc", "cap", "ck", "div", "warn", "aph", "ambient", "apm"]) {
+ const whole = `intro ${OPEN}${REF}\u003c/${name}\u003e after prose here.`;
+ const out = streamThrough([...whole]);
+ assert.ok(!out.includes(REF), `${name}: ref leaked`);
+ assert.ok(!out.includes(`\u003c/${name}\u003e`), `${name}: close leaked`);
+ assert.ok(out.startsWith("intro "), `${name}: leading prose lost`);
+ assert.ok(out.endsWith("after prose here."), `${name}: trailing prose lost`);
+ }
+});
+
+test("#2190 streaming: multi-tag blob with mixed valid and degenerate closes", () => {
+ const blob = `\u003cacp tokens="419" type="text"\u003em00501\u003c/acp\u003e \u003cacp tokens="193" type="text"\u003em00503\u003c/acp\u003e \u003cacp tokens="0"\u003em00505\u003c/p\u003e \u003cacp tokens="291"\u003em00502\u003c/p\u003e done.`;
+ assert.equal(streamThrough([blob]), " done.");
+});
+
+test("#2190 streaming: degenerate close near stream end no longer eats the following prose", () => {
+ const out = streamThrough([`\u003cacp tokens="1" type="text"\u003em00042\u003c/ck\u003e End of message.`]);
+ assert.equal(out, " End of message.");
+});
+
+test("#2190 streaming: prose body with a valid close keeps the prose (#1720 guard)", () => {
+ const out = streamThrough([`\u003cacp tokens="1" type="text"\u003eLet me check the file.\u003c/acp\u003e`]);
+ assert.equal(out, "Let me check the file.");
+});
+
+test("#2190 fast path: prose with stray HTML closes or comparison tails passes byte-identical", () => {
+ const prose = "The model wrote \u003c/p\u003e and \u003c/a\u003e in its reply.";
+ assert.equal(streamThrough([prose]), prose);
+ assert.equal(streamThrough(["if a\u003cb"]), "if a\u003cb");
+ assert.equal(streamThrough([" and c\u003ed then ok"]), " and c\u003ed then ok");
+});
+
+test("#2190 gate predicates: broad tail engages the machine while orphan-markup accounting stays prose", () => {
+ assert.equal(mayStartDegenerateRenderTag("\u003c"), true);
+ assert.equal(mayStartDegenerateRenderTag("\u003c/"), true);
+ assert.equal(mayStartDegenerateRenderTag("a\u003cb"), true);
+ assert.equal(mayStartDegenerateRenderTag("\u003cabcdefghijklmnopq"), false);
+ assert.equal(mayStartDegenerateRenderTag("\u003cdiv class="), false);
+ assert.equal(mayStartDegenerateRenderTag("OK"), false);
+ assert.equal(isOrphanMarkupText("\u003c/p\u003e"), false);
+ assert.equal(mayStartRenderTag("a\u003cb"), false);
+ assert.equal(containsEchoResidue("m05712\u003c/ap\u003e"), true);
+ assert.equal(containsEchoResidue("m05712 \u003c/ap\u003e"), true);
+ assert.equal(containsEchoResidue("see m1234"), false);
+ assert.equal(containsEchoResidue("m12345\u003cp\u003e"), false);
+});
+
+test("#2190 wrapped imitation: payload with ref-shaped content is still swallowed whole", () => {
+ const f = createTagEchoFilter();
+ let out = f.push(`\u003cacp tokens="1" type="text \u003cfoo\u003e${REF}\u003c/p\u003e more payload`);
+ out += f.flush();
+ assert.equal(out, "");
+});
+
+test("#2190 stripAcpTags: degenerate pairs die atomically, genuine HTML untouched", () => {
+ assert.equal(stripAcpTags(`\u003cacp tokens="1" type="text"\u003em05712\u003c/ck\u003e`), "");
+ assert.equal(stripAcpTags(`before \u003cacp\u003em00042\u003c/p\u003e after`), "before after");
+ assert.equal(stripAcpTags(`\u003cacp tokens="419" type="text"\u003em00501\u003c/acp\u003e \u003cacp tokens="0"\u003em00505\u003c/p\u003e`), " ");
+ assert.equal(stripAcpTags(`\u003cdiv class="x"\u003ehello\u003c/div\u003e`), `\u003cdiv class="x"\u003ehello\u003c/div\u003e`);
+ assert.equal(stripAcpTags(`see \u003c/p\u003e and \u003ca\u003em1234 text\u003c/a\u003e`), `see \u003c/p\u003e and \u003ca\u003em1234 text\u003c/a\u003e`);
+ assert.equal(stripAcpTags(`\u003cacp tokens="5" type="text"\u003em00001\u003c/acp\u003e`), "");
+});
+
+function makeSession(): Session {
+ return {
+ id: "testsess",
+ protocol: "responses",
+ upstreamOrigin: "http://127.0.0.1:9/v1",
+ label: "test",
+ createdAt: 0,
+ lastUsedAt: 0,
+ requests: 0,
+ lastInputTokens: 0,
+ stats: {},
+ dirty: false,
+ } as unknown as Session;
+}
+
+function makeRes(chunks: string[]) {
+ return {
+ writes: chunks,
+ write(b: Buffer | string) {
+ chunks.push(typeof b === "string" ? b : b.toString("utf8"));
+ return true;
+ },
+ end(b?: Buffer | string) {
+ if (b !== undefined) chunks.push(typeof b === "string" ? b : b.toString("utf8"));
+ },
+ once() {},
+ destroyed: false,
+ writableEnded: false,
+ } as unknown as import("node:http").ServerResponse;
+}
+
+function sse(ev: Record): string {
+ return `event: ${String(ev.type)}\ndata: ${JSON.stringify(ev)}\n\n`;
+}
+
+function streamOf(events: string[]): ReadableStream {
+ const enc = new TextEncoder();
+ let i = 0;
+ return new ReadableStream({
+ pull(controller) {
+ if (i < events.length) {
+ controller.enqueue(enc.encode(events[i]));
+ i += 1;
+ } else {
+ controller.close();
+ }
+ },
+ });
+}
+
+test("#2190 responses pipe: micro-fragmented degenerate tag never reaches the client", async () => {
+ const out: string[] = [];
+ const res = makeRes(out);
+ const session = makeSession();
+ const whole = `ok \u003cacp tokens="23" type="text"\u003em05712\u003c/ck\u003e tail`;
+ const micro = whole.match(/.{1,4}/gs) ?? [];
+ const events = [
+ ...micro.map((piece) => sse({ type: "response.output_text.delta", item_id: "msg_1", output_index: 0, delta: piece })),
+ sse({ type: "response.completed", response: { usage: { input_tokens: 7, output_tokens: 3 } } }),
+ ];
+ await pipePluginResponsesWithStrip(streamOf(events), res, session);
+ const text = out.join("");
+ assert.ok(!text.includes("m05712"), "ref never reaches the client");
+ const joined = text
+ .split("\n")
+ .filter((l) => l.startsWith("data:"))
+ .map((l) => JSON.parse(l.slice(5).trim()) as { type?: string; delta?: string })
+ .filter((ev) => ev.type === "response.output_text.delta" && typeof ev.delta === "string")
+ .map((ev) => ev.delta as string)
+ .join("");
+ assert.equal(joined, "ok tail", "surrounding prose reassembles to the tag-free text");
+});
+
+test("#2190 responses pipe: done-family full-text payload with degenerate pair is cleaned", async () => {
+ const out: string[] = [];
+ const res = makeRes(out);
+ const session = makeSession();
+ const events = [
+ sse({ type: "response.output_text.delta", item_id: "msg_1", output_index: 0, delta: "clean" }),
+ sse({ type: "response.output_text.done", item_id: "msg_1", output_index: 0, text: `clean \u003cacp tokens="1" type="text"\u003em00001\u003c/p\u003e` }),
+ sse({ type: "response.completed", response: { usage: { input_tokens: 1, output_tokens: 1 } } }),
+ ];
+ await pipePluginResponsesWithStrip(streamOf(events), res, session);
+ const text = out.join("");
+ assert.ok(!text.includes("m00001"), "done-family payload must be cleaned");
+ assert.ok(text.includes("clean "), "prose survives");
+});
+
+// #2190 round 2: field-attested drift names outside the core+insertion set.
+// Round 3 decontamination (#2190 thread): inclusion requires CLEAN spontaneous
+// evidence (block-initial, tokens= attribute, deduped n>=4) because open-side
+// grep counts are inflated by models quoting tag names in their own reasoning.
+// Qualifying: acacp/accessp/acb. acpx was added in round 2 and REMOVED in
+// round 3 (its raw census ~217 proved self-referential/constructed; one
+// case-variant sighting only) — excluded forms stay observable via the
+// residue audits, so a clean attestation earns re-inclusion later. Every
+// name-derived matcher consumes ACP_NAME_ALT: one point covers gate+filter.
+const DRIFT_FORMS: Array<[string, string, string]> = [
+ ["acacp", "\x3cacacp tokens=\"34\" type=\"text\"\x3em00375\x3c/acacp\x3e", "m00375"],
+ ["accessp", "\x3caccessp tokens=\"37\" type=\"text\"\x3em07960\x3c/accessp\x3e", "m07960"],
+ ["acb", "\x3c" + "acb" + "\x3e" + "m00123" + "\x3c/" + "acb" + "\x3e", "m00123"],
+];
+
+test("#2190 round 2 streaming: attested drift-name forms are stripped character-by-character", () => {
+ for (const [name, whole, ref] of DRIFT_FORMS) {
+ const out = streamThrough([...whole]);
+ assert.ok(!out.includes(ref), `${name}: ref leaked`);
+ assert.ok(!out.toLowerCase().includes(`\x3c${name}`), `${name}: open leaked`);
+ assert.equal(out, "", `${name}: whole span must die, got ${JSON.stringify(out)}`);
+ const withProse = [`before ${whole} after`];
+ const out2 = streamThrough(withProse[0].split(""));
+ assert.equal(out2, "before after", `${name}: surrounding prose must survive verbatim`);
+ }
+});
+
+test("#2190 round 2 streaming: case-folded drift names strip; cross-name pairing strips", () => {
+ const cases = [
+ "\x3cACACP tokens=\"34\"\x3em00375\x3c/aCaCp\x3e",
+ "\x3cacP tokens=\"1\" type=\"text\"\x3em00001\x3c/acacp\x3e",
+ "\x3cAcAcP tokens=\"2\"\x3em00002\x3c/ACCESSP\x3e",
+ ];
+ for (const c of cases) {
+ const out = streamThrough([...c]);
+ assert.equal(out, "", `case/cross form must die, got ${JSON.stringify(out)}`);
+ }
+});
+
+test("#2190 round 2 stripAcpTags: drift forms die whole-text, genuine words survive byte-identical", () => {
+ const leaky = DRIFT_FORMS.map(([, w]) => w).join(" ");
+ assert.equal(stripAcpTags(leaky), " ", "all three drift spans strip atomically");
+ const legit = [
+ "\x3caccount\x3em00375\x3c/account\x3e",
+ "\x3caction\x3em00375\x3c/action\x3e",
+ "\x3cACTIVE\x3em00375\x3c/active\x3e",
+ "\x3cacgroup\x3em00375\x3c/acgroup\x3e",
+ "\x3cacmap\x3em00375\x3c/acmap\x3e",
+ "\x3cacstep\x3em00375\x3c/acstep\x3e",
+ "\x3cacpipe\x3em00375\x3c/acpipe\x3e",
+ ];
+ for (const l of legit) {
+ assert.equal(stripAcpTags(l), l, `legit word must stay verbatim: ${l}`);
+ assert.equal(streamThrough([...l]), l, `legit word must stream verbatim: ${l}`);
+ }
+});
+
+// #2190 round 4: H2-exclusion invariant (#2248 debate). Every state-machine
+// path that TOUCHES a well-formed acplike pair must fire at least one onDrop
+// (i.e. leave a [tag-echo] log line): swallow-mode entry always follows a
+// logged drop (match path / BROKEN_ATTRS), the definite-tail hold budget is
+// TAG_OPEN_CAP with DROP-not-release over-cap, and the 80-char release budget
+// only exists inside swallow mode. A zero-log stream therefore cannot have
+// leaked a complete pair through the filter — the residual question (why the
+// bytes never entered) is answered by the exit audits, not by a silent
+// internal path. Probed empirically on the v0.1.185-identical filter.
+test("#2190 round 4 invariant: touching a well-formed pair always fires >=1 drop callback", () => {
+ const LT = String.fromCharCode(0x3c), GT = String.fromCharCode(0x3e);
+ const runScenario = (chunks: string[]): { drops: number; out: string } => {
+ let drops = 0;
+ const f = createTagEchoFilter(() => { drops++; });
+ let out = "";
+ for (const c of chunks) out += f.push(c);
+ out += f.flush();
+ return { drops, out };
+ };
+ const openTrunc = LT + "acp tokens=\"2\"";
+ const openFull = openTrunc + GT;
+ const close = LT + "/" + "acp" + GT;
+ // A: truncated open + short body, stream ends -> EOF rule drops, logged
+ let r = runScenario([openTrunc, "x".repeat(90)]);
+ assert.equal(r.drops, 1, "A: EOF drop must be logged");
+ assert.equal(r.out.includes("m"), false);
+ // B: truncated open + body past TAG_OPEN_CAP -> over-cap drop, logged
+ r = runScenario([openTrunc, "x".repeat(4200)]);
+ assert.equal(r.drops, 1, "B: over-cap definite-tail drop must be logged");
+ assert.equal(r.out.length, 0, "B: nothing released");
+ // C: complete pair char-by-char -> stripped, logged
+ r = runScenario([...(openFull + "m00001" + close)]);
+ assert.equal(r.drops, 1, "C: pair strip must be logged");
+ assert.equal(r.out, "", "C: pair dies");
+ // D: complete open + body past SWALLOW_CAP, no close -> budget RELEASES
+ // the prose body, but the open's drop already logged
+ r = runScenario([openFull, "y".repeat(90)]);
+ assert.ok(r.drops >= 1, "D: swallow entry drop must be logged");
+ assert.equal(r.out, "y".repeat(90), "D: body is prose, released verbatim");
+ // E: truncated open + long body, close arrives late -> ref leaks as BARE
+ // prose (no pair) but drops are logged
+ r = runScenario([openTrunc, "z".repeat(4200), GT + "m00001" + close]);
+ assert.ok(r.drops >= 1, "E: drops must be logged");
+ assert.ok(!r.out.includes(LT), "E: no tag markup survives");
+});
+
+// #2190 round 8: field replay — session-1f3d2c0f seq=22409, the exact 71
+// tokenizer shards of a turn whose fourth tag closes with the degenerate
+// name (k outside every acplike class). Proves the close side needs NO
+// list membership: the ref-anchored open-set rule (degenCloseAfterRef /
+// DEGEN_PAIR) strips it because the body is one bare ref. Output must be the
+// three inter-tag newlines only.
+test("#2190 round 8 field replay: 71 real shards ending in degenerate close strip fully", () => {
+ const AK_SHARDS = [
+ "<",
+ "ac",
+ "p",
+ " tokens",
+ "=\"",
+ "299",
+ "\"",
+ " type",
+ "=\"",
+ "text",
+ "\">",
+ "m",
+ "122",
+ "07",
+ "",
+ "ac",
+ "p",
+ ">\n",
+ "<",
+ "ac",
+ "p",
+ " tokens",
+ "=\"",
+ "473",
+ "\"",
+ " type",
+ "=\"",
+ "text",
+ "\">",
+ "m",
+ "122",
+ "05",
+ "",
+ "ac",
+ "p",
+ ">\n",
+ "<",
+ "ac",
+ "p",
+ " tokens",
+ "=\"",
+ "221",
+ "\"",
+ " type",
+ "=\"",
+ "text",
+ "\">",
+ "m",
+ "122",
+ "06",
+ "",
+ "ac",
+ "p",
+ ">\n",
+ "<",
+ "ac",
+ "p",
+ " tokens",
+ "=\"",
+ "120",
+ "\"",
+ " type",
+ "=\"",
+ "text",
+ "\">",
+ "m",
+ "122",
+ "11",
+ "",
+ "ak",
+ ">"
+ ];
+ let drops = 0;
+ const tagFilter = composeStreamFilters(
+ composeStreamFilters(createTagEchoFilter(() => { drops++; }), createMarkerLineFilter()),
+ createBiliArtifactFilter(),
+ );
+ let out = "";
+ for (const delta of AK_SHARDS) {
+ if (!mayStartRenderTag(delta) && !mayStartMarkerLine(delta) && !mayStartBiliInternal(delta) && !mayStartDegenerateRenderTag(delta) && !tagFilter.pending()) {
+ out += delta;
+ continue;
+ }
+ out += tagFilter.push(delta);
+ }
+ out += tagFilter.flush();
+ assert.equal(out, "\n\n\n", "only the three inter-tag newlines may survive");
+ assert.ok(drops >= 1, "drop must be logged");
+});
+
+test("#2190 round 2 gate: drift-name heads engage the render-tag predicates once spaced", () => {
+ assert.equal(mayStartRenderTag("\x3cacacp "), true);
+ assert.equal(mayStartRenderTag("\x3caccessp x"), true);
+ assert.equal(mayStartRenderTag("\x3c" + "acb" + " "), true);
+ // acpx boundary pin (round 3 exclusion): NOT in the name set anymore —
+ // the render-tag predicates must stay false for it, while the BROAD tail
+ // keeps engaging the gate so such bytes at least enter the state machine
+ // (and the residue audits observe any release).
+ assert.equal(mayStartRenderTag("\x3cacpx"), false, "excluded form: render-tag predicate stays false");
+ assert.equal(mayStartDegenerateRenderTag("\x3cacpx"), true, "BROAD tail still engages on the bare head");
+ assert.equal(mayStartRenderTag("\x3cacpx\x3e"), false, "excluded form: terminated head stays prose");
+ assert.equal(mayStartRenderTag("\x3caccount"), false, "genuine word stays prose");
+ assert.equal(mayStartRenderTag("\x3cacgroup"), false, "custom tag stays prose");
+});
diff --git a/tests/issue2248-exit-audit.test.ts b/tests/issue2248-exit-audit.test.ts
new file mode 100644
index 0000000000..d5efce443d
--- /dev/null
+++ b/tests/issue2248-exit-audit.test.ts
@@ -0,0 +1,200 @@
+// #2248: every raw-forward exit of the plugin pipes must leave a trace when it
+// carries echo-residue-shaped bytes — otherwise a leak through an unmanaged
+// field is invisible in the log (the zero-log leak class from #2190). The suite
+// pins the exit audits on all wires (openai E1/E5 + parse-fail, anthropic
+// non-delta / unmanaged-delta, google non-candidates / tail fallthrough,
+// responses unrecognized-type + parse-fail). Every audited frame must ALSO stay
+// byte-exact (#1039 wire fidelity) and argument frames must stay excluded.
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import type { Session } from "../src/session.ts";
+import { pipePluginChatWithStrip, pipePluginResponsesWithStrip } from "../src/plugin.ts";
+import { setLogCapture } from "../src/logger.ts";
+
+// Matches containsEchoResidue (/m\d{4,}\s*<\/[A-Za-z]/) but ends in plain prose
+// so the tag-head/tail heuristics stay quiet on managed-field fast paths.
+const RESIDUE = "m1234 trailing prose";
+
+const DONE = "data: [DONE]\n\n";
+const noRefetch = () => Promise.resolve(null);
+
+function makeSession(): Session {
+ return {
+ id: "testsess",
+ protocol: "openai",
+ upstreamOrigin: "http://127.0.0.1:9/v1",
+ label: "test",
+ createdAt: 0,
+ lastUsedAt: 0,
+ requests: 0,
+ lastInputTokens: 0,
+ stats: {},
+ dirty: false,
+ } as unknown as Session;
+}
+
+function makeRes(chunks: string[]) {
+ return {
+ writes: chunks,
+ write(b: Buffer | string) {
+ chunks.push(typeof b === "string" ? b : b.toString("utf8"));
+ return true;
+ },
+ end(b?: Buffer | string) {
+ if (b !== undefined) chunks.push(typeof b === "string" ? b : b.toString("utf8"));
+ },
+ once() {},
+ destroyed: false,
+ writableEnded: false,
+ } as unknown as import("node:http").ServerResponse;
+}
+
+function streamOf(events: string[]): ReadableStream {
+ const enc = new TextEncoder();
+ let i = 0;
+ return new ReadableStream({
+ pull(controller) {
+ if (i < events.length) {
+ controller.enqueue(enc.encode(events[i]));
+ i += 1;
+ } else {
+ controller.close();
+ }
+ },
+ });
+}
+
+async function runChat(frames: string[], protocol: "openai" | "anthropic" | "google"): Promise<{ out: string; rawForwardWarns: number }> {
+ const out: string[] = [];
+ const warns: string[] = [];
+ setLogCapture((level, msg) => {
+ if (level === "warn") warns.push(msg);
+ });
+ try {
+ await pipePluginChatWithStrip(streamOf(frames), makeRes(out), protocol, makeSession(), undefined, noRefetch);
+ } finally {
+ setLogCapture(null);
+ }
+ return { out: out.join(""), rawForwardWarns: warns.filter((m) => m.includes("raw forward carried")).length };
+}
+
+async function runResponses(frames: string[]): Promise<{ out: string; rawForwardWarns: number }> {
+ const out: string[] = [];
+ const warns: string[] = [];
+ setLogCapture((level, msg) => {
+ if (level === "warn") warns.push(msg);
+ });
+ try {
+ await pipePluginResponsesWithStrip(streamOf(frames), makeRes(out), makeSession(), undefined, noRefetch);
+ } finally {
+ setLogCapture(null);
+ }
+ return { out: out.join(""), rawForwardWarns: warns.filter((m) => m.includes("raw forward carried")).length };
+}
+
+const finOpenai = `data: ${JSON.stringify({ id: "x", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })}\n\n`;
+
+test("#2248 openai E1: no-choices frame carrying residue is audited and forwarded verbatim", async () => {
+ const frame = `data: ${JSON.stringify({ id: "x", message: { role: "assistant", content: RESIDUE } })}\n\n`;
+ const { out, rawForwardWarns } = await runChat([frame, finOpenai, DONE], "openai");
+ assert.equal(rawForwardWarns, 1, "exactly one raw-forward audit warn");
+ assert.equal(out, frame + finOpenai + DONE, "bytes forwarded exactly as received");
+});
+
+test("#2248 openai E5: delta with unmanaged text field is audited and forwarded verbatim", async () => {
+ const frame = `data: ${JSON.stringify({ id: "x", choices: [{ index: 0, delta: { text: RESIDUE }, finish_reason: null }] })}\n\n`;
+ const { out, rawForwardWarns } = await runChat([frame, finOpenai, DONE], "openai");
+ assert.equal(rawForwardWarns, 1);
+ assert.equal(out, frame + finOpenai + DONE);
+});
+
+test("#2248 openai E5 exclusion: tool_calls argument frames are NOT audited (#1039)", async () => {
+ const frame = `data: ${JSON.stringify({ id: "x", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, function: { arguments: `"${RESIDUE}"` } }] }, finish_reason: null }] })}\n\n`;
+ const { out, rawForwardWarns } = await runChat([frame, finOpenai, DONE], "openai");
+ assert.equal(rawForwardWarns, 0, "argument bytes never flagged");
+ assert.equal(out, frame + finOpenai + DONE);
+});
+
+test("#2248 openai negative control: clean role-only frame produces no audit warn", async () => {
+ const frame = `data: ${JSON.stringify({ id: "x", choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }] })}\n\n`;
+ const { out, rawForwardWarns } = await runChat([frame, finOpenai, DONE], "openai");
+ assert.equal(rawForwardWarns, 0);
+ assert.equal(out, frame + finOpenai + DONE);
+});
+
+test("#2248 anthropic non-delta exit: message_start-shaped frame with residue is audited verbatim", async () => {
+ const start = `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_1", content: [{ type: "text", text: RESIDUE }] } })}\n\n`;
+ const stop = `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`;
+ const { out, rawForwardWarns } = await runChat([start, stop], "anthropic");
+ assert.equal(rawForwardWarns, 1);
+ assert.equal(out, start + stop);
+});
+
+test("#2248 anthropic unmanaged-delta exit: citation_delta residue is audited verbatim", async () => {
+ const delta = `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "citation_delta", citation: RESIDUE } })}\n\n`;
+ const stop = `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`;
+ const { out, rawForwardWarns } = await runChat([delta, stop], "anthropic");
+ assert.equal(rawForwardWarns, 1);
+ assert.equal(out, delta + stop);
+});
+
+test("#2248 anthropic input_json_delta exclusion: argument fragments are NOT audited (#1039)", async () => {
+ const delta = `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: RESIDUE } })}\n\n`;
+ const stop = `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`;
+ const { out, rawForwardWarns } = await runChat([delta, stop], "anthropic");
+ assert.equal(rawForwardWarns, 0, "argument bytes never flagged");
+ assert.equal(out, delta + stop);
+});
+
+test("#2248 google non-candidates exit: usage-shaped frame with residue is audited verbatim", async () => {
+ const frame = `data: ${JSON.stringify({ modelVersion: "gm", usageMetadata: { promptTokenCount: 1 }, note: RESIDUE })}\n\n`;
+ const fin = `data: ${JSON.stringify({ candidates: [{ content: { parts: [] }, finishReason: "STOP" }] })}\n\n`;
+ const { out, rawForwardWarns } = await runChat([frame, fin], "google");
+ assert.equal(rawForwardWarns, 1);
+ assert.equal(out, frame + fin);
+});
+
+test("#2248 google tail fallthrough: text-only parts frame with residue is audited verbatim", async () => {
+ const frame = `data: ${JSON.stringify({ candidates: [{ content: { parts: [{ text: RESIDUE }] } }] })}\n\n`;
+ const fin = `data: ${JSON.stringify({ candidates: [{ content: { parts: [] }, finishReason: "STOP" }] })}\n\n`;
+ const { out, rawForwardWarns } = await runChat([frame, fin], "google");
+ assert.ok(rawForwardWarns >= 1, "exit-level audit fired");
+ assert.equal(out, frame + fin);
+});
+
+test("#2248 chat parse-fail: unparseable frame with residue is audited and forwarded verbatim", async () => {
+ const bad = `data: {"choices":[{"delta":{"content":"${RESIDUE}"\n\n`;
+ const { out, rawForwardWarns } = await runChat([bad, finOpenai, DONE], "openai");
+ assert.equal(rawForwardWarns, 1);
+ assert.equal(out, bad + finOpenai + DONE);
+});
+
+test("#2248 chat parse-fail negative control: unparseable clean frame produces no audit warn", async () => {
+ const bad = `data: {"foo": \n\n`;
+ const { out, rawForwardWarns } = await runChat([bad, finOpenai, DONE], "openai");
+ assert.equal(rawForwardWarns, 0);
+ assert.equal(out, bad + finOpenai + DONE);
+});
+
+const completedResponses = `data: ${JSON.stringify({ type: "response.completed", response: { id: "r1", status: "completed" } })}\n\n`;
+
+test("#2248 responses unrecognized-type exit: vendor event with residue is audited verbatim", async () => {
+ const ev = `data: ${JSON.stringify({ type: "response.vendor_debug_event", detail: RESIDUE })}\n\n`;
+ const { out, rawForwardWarns } = await runResponses([ev, completedResponses]);
+ assert.equal(rawForwardWarns, 1);
+ assert.equal(out, ev + completedResponses);
+});
+
+test("#2248 responses argument-stream exclusion: custom_tool_call_input.delta is NOT audited (#1039)", async () => {
+ const ev = `data: ${JSON.stringify({ type: "response.custom_tool_call_input.delta", item_id: "ci1", output_index: 0, delta: RESIDUE })}\n\n`;
+ const { out, rawForwardWarns } = await runResponses([ev, completedResponses]);
+ assert.equal(rawForwardWarns, 0, "argument bytes never flagged");
+ assert.equal(out, ev + completedResponses);
+});
+
+test("#2248 responses parse-fail: unparseable frame with residue is audited and forwarded verbatim", async () => {
+ const bad = `data: {"type":"response.output_text.delta","delta":"${RESIDUE}"\n\n`;
+ const { out, rawForwardWarns } = await runResponses([bad, completedResponses]);
+ assert.equal(rawForwardWarns, 1);
+ assert.equal(out, bad + completedResponses);
+});
diff --git a/tests/nameless-toolcall-observability.test.ts b/tests/nameless-toolcall-observability.test.ts
index f8ac3c2a5a..466b9c1e19 100644
--- a/tests/nameless-toolcall-observability.test.ts
+++ b/tests/nameless-toolcall-observability.test.ts
@@ -23,6 +23,7 @@ function makeSession(): Session {
requests: 0,
lastInputTokens: 0,
stats: {},
+ metadata: {},
dirty: false,
} as unknown as Session;
}
diff --git a/tests/plugin-passthrough-tag-strip-chat.test.ts b/tests/plugin-passthrough-tag-strip-chat.test.ts
index 6b6207e301..a7173febc9 100644
--- a/tests/plugin-passthrough-tag-strip-chat.test.ts
+++ b/tests/plugin-passthrough-tag-strip-chat.test.ts
@@ -97,7 +97,7 @@ test("plugin chat passthrough strips tags split across deltas and flushes tail b
assert.ok(flushIdx !== -1 && doneIdx !== -1 && flushIdx < doneIdx, "flushed tail delta must precede [DONE]");
});
-test("plugin chat passthrough strips echo from reasoning_content too", async () => {
+test("unified invariant: plugin chat passthrough leaves reasoning_content VERBATIM", async () => {
const out: string[] = [];
const res = makeRes(out);
const session = makeSession();
@@ -107,8 +107,9 @@ test("plugin chat passthrough strips echo from reasoning_content too", async ()
];
await pipePluginChatWithStrip(streamOf(events), res, "openai", session);
const text = out.join("");
- assert.ok(!text.includes("m00007"), "reasoning echo stripped");
- assert.ok(text.includes("think done"), "reasoning prose survives");
+ // Thinking channel: byte-verbatim — echo-shaped reasoning rides as-is.
+ assert.ok(text.includes("m00007"), "reasoning rides verbatim (thinking channel)");
+ assert.ok(text.includes("think "), "reasoning prose rides verbatim");
});
test("plugin chat passthrough drops a chunk whose delta stripped to empty", async () => {
@@ -141,11 +142,13 @@ test("plugin chat passthrough keeps sibling text when one field is a pure tag ec
];
await pipePluginChatWithStrip(streamOf(events), res, "openai", session);
const text = out.join("");
- assert.ok(!text.includes("m00044"), "echo-only sibling field stripped");
+ // Unified invariant: reasoning_content is verbatim (thinking channel);
+ // only the visible content channel is managed.
+ assert.ok(text.includes("m00044"), "echo-only reasoning sibling rides verbatim (thinking channel)");
assert.ok(text.includes("real answer"), "sibling field with real content must survive");
});
-test("plugin chat passthrough still drops a chunk where every managed field is a pure tag echo (#463)", async () => {
+test("unified invariant: all-echo content + verbatim reasoning keeps the chunk (reasoning alone no longer drops it)", async () => {
const out: string[] = [];
const res = makeRes(out);
const session = makeSession();
@@ -155,9 +158,12 @@ test("plugin chat passthrough still drops a chunk where every managed field is a
];
await pipePluginChatWithStrip(streamOf(events), res, "openai", session);
const text = out.join("");
- assert.ok(!text.includes("m00123") && !text.includes("m00124"), "echoes stripped");
+ // Unified invariant: content is managed (pure echo -> stripped), reasoning
+ // is verbatim — so the chunk survives carrying the reasoning field alone.
+ assert.ok(!text.includes("m00123"), "content echo still stripped");
+ assert.ok(text.includes("m00124"), "reasoning echo rides verbatim (thinking channel)");
const dataLines = text.split("\n").filter((l) => l.startsWith("data:")).filter((l) => !l.includes("[DONE]"));
- assert.equal(dataLines.length, 0, "all-echo chunk dropped");
+ assert.equal(dataLines.length, 1, "chunk survives carrying the verbatim reasoning field");
});
test("plugin chat passthrough keeps tool_calls deltas byte-identical", async () => {
@@ -233,7 +239,7 @@ test("plugin chat passthrough resolves held tail at stream end without [DONE]",
assert.ok(!text.includes("m0"), "unclosed tag content dropped at stream end");
});
-test("plugin anthropic passthrough strips render tags from text_delta and thinking_delta", async () => {
+test("plugin anthropic passthrough strips render tags from text_delta but keeps signed thinking_delta byte-for-byte (#1960)", async () => {
const out: string[] = [];
const res = makeRes(out);
const session = makeSession();
@@ -246,7 +252,10 @@ test("plugin anthropic passthrough strips render tags from text_delta and thinki
];
await pipePluginChatWithStrip(streamOf(events), res, "anthropic", session);
const text = out.join("");
- assert.ok(!text.includes("m00009") && !text.includes("m00010"), "render tags stripped from both delta types");
+ // #1960/KDD#10: signed thinking rides byte-for-byte — rewriting it desyncs
+ // its signature and bricks replay. Only the text channel is stripped.
+ assert.ok(text.includes("m00009"), "signed thinking_delta stays byte-for-byte (#1960)");
+ assert.ok(!text.includes("m00010"), "render tags stripped from the text channel");
assert.ok(text.includes("hi bye"), "text prose survives");
assert.ok(text.includes("hmm "), "thinking prose survives");
assert.equal((session.stats as Record)["lastInputTokens"], 42, "usage sampled from message_start");
diff --git a/tests/unified-acp-invariants.test.ts b/tests/unified-acp-invariants.test.ts
new file mode 100644
index 0000000000..941acdc561
--- /dev/null
+++ b/tests/unified-acp-invariants.test.ts
@@ -0,0 +1,122 @@
+// Unified ACP remediation invariants (owner directive, #2229/#2248 family).
+//
+// Four hard rules govern every text-rewriting surface (loop adapters, plugin
+// passthrough, JSON strip functions, exit audits):
+// I1. The thinking channel is byte-verbatim: anthropic thinking, google
+// thought, openai reasoning_content/reasoning, responses reasoning
+// summaries. Never stripped, never rewritten (echo-shaped reasoning
+// rides as-is; signature/replay validation would break otherwise).
+// I2. Model PROSE output is manageable: render-tag echoes, marker lines,
+// bili-internal text and (when absorb-instructed) whole emissions are
+// stripped from the visible text channel.
+// I3. Model output bound for files (tool arguments) is byte-identical —
+// (#1039) tool_calls arguments never enter any filter.
+// I4. User-sent bytes are never rewritten: a verbatim echo of the user's
+// own fragment survives even when stripping is armed (#463 exemption).
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { createOpenaiAdapter, createResponsesAdapter } from "../src/loop/index.ts";
+import type { CompressLoopAdapter, ParsedStreamEvent } from "../src/loop/index.ts";
+import { stripOpenaiChatText, stripResponsesText } from "../src/loop/tag-echo-filter.ts";
+
+const TAG = "\x3cacp tokens=\"2\" type=\"text\"\x3em00001\x3c/acp\x3e";
+const TAG_JSON = JSON.stringify(TAG).slice(1, -1); // TAG as it appears in JSON frame bytes
+const EMISSION = "\x3cacp_tool_call name=\"write\"\x3e\x3cparameter=ref\x3em00999\x3c/parameter\x3e\x3c/acp_tool_call\x3e";
+
+// ── helpers ────────────────────────────────────────────────────────────────
+async function collect(adapter: CompressLoopAdapter, body: string): Promise {
+ const events: ParsedStreamEvent[] = [];
+ for await (const ev of adapter.parseStream(new Response(body, { status: 200 }).body!, 1)) events.push(ev);
+ return events;
+}
+function clientBytes(events: ParsedStreamEvent[]): string {
+ return events.map((e) => {
+ const b = (e as { raw?: Buffer; chunk?: Buffer });
+ return (b.raw ?? b.chunk)?.toString("utf8") ?? "";
+ }).join("");
+}
+function sse(obj: unknown): string {
+ return `data: ${JSON.stringify(obj)}\n\n`;
+}
+
+// ── I1: thinking channel is byte-verbatim ─────────────────────────────────
+test("I1 adapter lane: openai reasoning_content echo rides VERBATIM while content is cleaned", async () => {
+ const adapter = createOpenaiAdapter({ model: "gpt" });
+ const out = clientBytes(await collect(
+ adapter,
+ sse({ choices: [{ index: 0, delta: { reasoning_content: TAG, content: `${TAG}kept` } }] }) +
+ sse({ choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }) +
+ "data: [DONE]\n\n",
+ ));
+ assert.ok(out.includes(TAG_JSON), `thinking channel verbatim, got: ${out}`);
+ assert.ok(out.includes('"content":"kept"'), `visible text channel still cleaned, got: ${out}`);
+});
+
+test("I1 adapter lane: responses reasoning_summary delta AND done-event ride VERBATIM", async () => {
+ const adapter = createResponsesAdapter(false);
+ const out = clientBytes(await collect(
+ adapter,
+ sse({ type: "response.reasoning_summary_text.delta", item_id: "rs_1", output_index: 0, summary_index: 0, delta: `${TAG}think` }) +
+ sse({ type: "response.reasoning_summary_part.done", item_id: "rs_1", output_index: 0, summary_index: 0, part: { type: "summary_text", text: `${TAG} done` } }) +
+ sse({ type: "response.completed", response: { id: "r1", status: "completed", output: [], usage: { input_tokens: 1, output_tokens: 1 } } }),
+ ));
+ const tagCount = out.split(TAG_JSON).length - 1;
+ assert.equal(tagCount, 2, `both reasoning summary frames carry the tag verbatim, got: ${out}`);
+});
+
+test("I1 strip functions (defense in depth): reasoning fields survive the strip pass", () => {
+ const body = { choices: [{ delta: { content: `${TAG}kept`, reasoning_content: `${TAG}think`, reasoning: `${TAG}plan` } }] };
+ const out = stripOpenaiChatText(body);
+ const d = out.choices[0].delta!;
+ assert.equal(d.content, "kept", "prose channel still stripped");
+ assert.equal(d.reasoning_content, `${TAG}think`, "reasoning_content verbatim");
+ assert.equal(d.reasoning, `${TAG}plan`, "reasoning verbatim");
+});
+
+test("I1 strip functions: responses reasoning summary survives armed strip", () => {
+ const body = { output: [{ type: "reasoning", id: "rs_1", summary: [{ type: "summary_text", text: EMISSION }] }] };
+ const armed = stripResponsesText(body, true);
+ const rs = (armed.output as Array<{ summary: Array<{ text: string }> }>)[0];
+ assert.equal(rs.summary[0].text, EMISSION, "reasoning summary verbatim");
+});
+
+// ── I2: model prose remains manageable ─────────────────────────────────────
+test("I2 adapter lane: render-tag echo is still stripped from the content channel", async () => {
+ const adapter = createOpenaiAdapter({ model: "gpt" });
+ const out = clientBytes(await collect(
+ adapter,
+ sse({ choices: [{ index: 0, delta: { content: `${TAG}kept` } }] }) +
+ sse({ choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }) +
+ "data: [DONE]\n\n",
+ ));
+ assert.ok(!out.includes('"content":" {
+ const args = JSON.stringify({ content: `write ${TAG} and ${EMISSION} to disk` });
+ const adapter = createOpenaiAdapter({ model: "gpt" });
+ const out = clientBytes(await collect(
+ adapter,
+ sse({ choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "write", arguments: args } }] } }] }) +
+ sse({ choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }) +
+ "data: [DONE]\n\n",
+ ));
+ assert.ok(out.includes(JSON.stringify(args)), `file-bound arguments byte-identical (#1039): ${out}`);
+});
+
+test("I3 strip functions: tool_calls never rewritten even when armed", () => {
+ const args = JSON.stringify({ content: `${TAG}${EMISSION}` });
+ const body = { choices: [{ delta: { tool_calls: [{ index: 0, function: { name: "write", arguments: args } }] } }] };
+ const armed = stripOpenaiChatText(body, true);
+ const got = armed.choices[0].delta!.tool_calls![0].function.arguments;
+ assert.equal(got, args, "arguments untouched");
+});
+
+// ── I4: user-sent bytes never rewritten ────────────────────────────────────
+test("I4 strip functions: the #463 user-echo exemption keys on request text, not shape", () => {
+ const reqBody = { messages: [{ role: "user", content: `Output exactly:\n${EMISSION}` }] };
+ const armed = stripOpenaiChatText({ choices: [{ delta: { content: EMISSION } }] }, true, JSON.stringify(reqBody.messages[0].content));
+ assert.equal(armed.choices[0].delta!.content, EMISSION, "verbatim echo of the user's own bytes survives armed strip");
+});