From f2226ff48ef3de4521ad87496b0f662d2f3a7d24 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 15 Oct 2026 01:23:42 +0800 Subject: [PATCH 1/2] docs: document ACP_RENDER_NONE as the dsh render-seam mitigation (#3198) --- CONFIGURATION.md | 4 ++-- CONFIGURATION.zh-CN.md | 4 ++-- src/cache-ledger.ts | 3 ++- src/web/i18n.ts | 4 ++-- tests/cache-seam-client-insert.test.ts | 3 +++ 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index cf2f3435df..fe18ea54ce 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -200,7 +200,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `diagnostics.dump4xxMaxBytes` | number | 2097152 (floor 1024) | BILI_DUMP_4XX_MAX_BYTES | Size cap for a single 4xx dump file. | | `diagnostics.maxTotalBytes` | number | unset (off) | BILI_DUMP_MAX_TOTAL_BYTES | Total-size cap for debug dump dirs (dumps/, raw/, SSE dump dir); oldest files deleted first when exceeded (floor 1 MiB). Dumps have no rotation by default and can grow tens of GB/day under active capture (#2412). | | `diagnostics.maxAgeDays` | number | unset (off) | BILI_DUMP_MAX_AGE_DAYS | Age cap in days for debug dump dirs; files older than this are deleted (floor 1 h). Off by default. | -| `diagnostics.renderNone` | boolean | false | ACP_RENDER_NONE | Disable all ACP tag rendering (raw wire study mode). | +| `diagnostics.renderNone` | boolean | false | ACP_RENDER_NONE | Disable all ACP tag rendering (raw wire study mode; also the mitigation for host re-render cache seams, e.g. dsh — #3198). | | `diagnostics.noInjectTool` | boolean | false | ACP_NO_INJECT_TOOL | Stop injecting the acp_compress tool definition into requests. | | `diagnostics.noCompressPrompt` | boolean | false | ACP_NO_COMPRESS_PROMPT | Stop appending the compression doctrine to system prompts. | | `diagnostics.countTokensPassthrough` | boolean | false | ACP_COUNT_TOKENS_PASSTHROUGH | Forward /v1/messages/count_tokens to the upstream instead of answering locally. | @@ -1755,7 +1755,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes | `BILI_ALLOW_PRIVATE_HTTP_PROVIDERS` | Opt-in switch (#2984): set to `1`/`true` to let plain-HTTP provider baseUrls on private networks (RFC1918, 127/8, link-local, IPv6 ULA/link-local, `*.local`/`.lan`/`.internal`) pass the agent-registry intake and summary-endpoint guards, which otherwise trust loopback plaintext only. Public IPs/domains and CGNAT (100.64/10) stay refused regardless. Default off — behavior identical to pre-#2984. A set env value owns the knob over `network.allowPrivateHttpProviders`. | | `ACP_SESSION_HEADER` | Conversation-id header name (default `x-acp-session`). | | `ACP_REASONING_KEEP` | Responses API only: set `none` to drop all reasoning items. Default routes reasoning through the compression pipeline so it is hidden automatically once its turn is summarized (prevents the unbounded accumulation that broke Codex's prompt-cache prefix). | -| `ACP_RENDER_NONE` | Set to `1` to stop injecting per-message render tags (the `` `` `` markers carrying `mNNNNN` refs) into outgoing request history — applies to every wire format (OpenAI chat, Anthropic, Responses) and compact rebuilds (#933). Default is `text-only`: the model reads these refs to cite messages in `compress` calls, so only disable them once you've confirmed your workflow doesn't need ref-based compression (e.g. tag echoes leaking into client-visible output). Previously this variable was honored only on the Responses path and compact; #933 extended it to all paths. | +| `ACP_RENDER_NONE` | Set to `1` to stop injecting per-message render tags (the `` `` `` markers carrying `mNNNNN` refs) into outgoing request history — applies to every wire format (OpenAI chat, Anthropic, Responses) and compact rebuilds (#933). Default is `text-only`: the model reads these refs to cite messages in `compress` calls, so only disable them once you've confirmed your workflow doesn't need ref-based compression (e.g. tag echoes leaking into client-visible output). Previously this variable was honored only on the Responses path and compact; #933 extended it to all paths. A third use case (#3198): hosts that re-render ACP tags inside their own resent history (dsh) re-render bili's injected tags between resends and produce cache seams from their own churn — with the flag set the injection disappears and so do the seams (A/B: session hit rate 85.6% → 91.2%). Refs stay assigned internally and growth-gated nudges still list compressible ranges, so folding keeps working; what you give up is citing individual messages by ref. Expect exactly ONE full replay when flipping the flag mid-session. | | `ACP_LOG_FILE` | Log file path (default XDG state path; `off` disables the file, keeps stderr). Auto-rotates at 10 MB. | | `ACP_DUMP_SSE` | Directory to dump raw SSE frames for debugging — including loop-originated upstream responses (compress re-requests and truncation retries, named `--loop-raw.sse`), which the outer forward tee cannot see (#1455). | | `BILI_STREAM_ERROR_SHAPE` | Set `"completion"` to restore the legacy failure shape on the anthropic/openai wire (failure text inside a synthesized successful completion); default `"protocol"` = protocol-native error frames (#1455). Same knob as `compat.streamErrorShape`; this env var wins. | diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index e7dbcf71ce..f4fae66825 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -200,7 +200,7 @@ | `diagnostics.dump4xxMaxBytes` | number | 2097152 (floor 1024) | BILI_DUMP_4XX_MAX_BYTES | 单个 4xx 转储文件的大小上限。 | | `diagnostics.maxTotalBytes` | number | unset (off) | BILI_DUMP_MAX_TOTAL_BYTES | 调试转储目录(dumps/、raw/、SSE dump 目录)的总大小上限;超限时按最旧优先删除(下限 1 MiB)。转储默认无任何轮转,持续捕获下每天可增长数十 GB(#2412)。 | | `diagnostics.maxAgeDays` | number | unset (off) | BILI_DUMP_MAX_AGE_DAYS | 调试转储目录的保留天数上限;超过该天数的文件被删除(下限 1 小时)。默认关闭。 | -| `diagnostics.renderNone` | boolean | false | ACP_RENDER_NONE | 禁用所有 ACP 标签渲染(原始线路研究模式)。 | +| `diagnostics.renderNone` | boolean | false | ACP_RENDER_NONE | 禁用所有 ACP 标签渲染(原始线路研究模式;也是宿主重渲染缓存缝的缓解手段,如 dsh —— #3198)。 | | `diagnostics.noInjectTool` | boolean | false | ACP_NO_INJECT_TOOL | 停止向请求注入 acp_compress 工具定义。 | | `diagnostics.noCompressPrompt` | boolean | false | ACP_NO_COMPRESS_PROMPT | 停止向系统提示词附加压缩教条文本。 | | `diagnostics.countTokensPassthrough` | boolean | false | ACP_COUNT_TOKENS_PASSTHROUGH | 把 /v1/messages/count_tokens 转发给上游,而不是本地应答。 | @@ -1751,7 +1751,7 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `BILI_ALLOW_PRIVATE_HTTP_PROVIDERS` | 可选开关(#2984):设为 `1`/`true` 后,私网(RFC1918、127/8、link-local、IPv6 ULA/link-local、`*.local`/`.lan`/`.internal`)上的明文 HTTP provider baseUrl 可通过 agent-registry 接收与摘要端点守卫——这些守卫默认只信任回环明文。公网 IP/域名与 CGNAT(100.64/10)无论如何都拒。默认关闭——行为与 #2984 之前逐字一致。已设置的环境变量值优先于 `network.allowPrivateHttpProviders`。 | | `ACP_SESSION_HEADER` | 会话 id 请求头名称(默认 `x-acp-session`)。 | | `ACP_REASONING_KEEP` | 仅 Responses API:设 `none` 丢弃全部 reasoning 项。默认让 reasoning 走压缩管道,其轮次被摘要后自动隐藏(避免无限累积破坏 Codex 的 prompt-cache 前缀)。 | -| `ACP_RENDER_NONE` | 设为 `1` 停止向出站请求历史注入逐消息渲染标签(承载 `mNNNNN` ref 的 `` `` `` 标记)——适用于所有线格式(OpenAI chat、Anthropic、Responses)及 compact 重建(#933)。默认 `text-only`:模型靠这些 ref 在 `compress` 调用中引用消息,只有确认自己的工作流不需要基于 ref 的压缩(例如标签回声泄漏到客户端可见输出)后才应禁用。此前该变量仅在 Responses 路径与 compact 上生效;#933 扩展到了所有路径。 | +| `ACP_RENDER_NONE` | 设为 `1` 停止向出站请求历史注入逐消息渲染标签(承载 `mNNNNN` ref 的 `` `` `` 标记)——适用于所有线格式(OpenAI chat、Anthropic、Responses)及 compact 重建(#933)。默认 `text-only`:模型靠这些 ref 在 `compress` 调用中引用消息,只有确认自己的工作流不需要基于 ref 的压缩(例如标签回声泄漏到客户端可见输出)后才应禁用。此前该变量仅在 Responses 路径与 compact 上生效;#933 扩展到了所有路径。第三种用法(#3198):在自己重发的历史里重渲染 ACP 标签的宿主(如 dsh)会在每次重发之间重新渲染 bili 注入的标签,从而产生源自宿主自身变化的缓存缝——设置该标志后注入消失、缝随之消失(A/B 实测:会话命中率 85.6% → 91.2%)。refs 仍在内部分配、growth-gated nudge 仍列出可压缩区间,所以折叠照常工作;代价是无法再按 ref 引用单条消息。会话中途翻转该标志时预期恰好一次完整重放。 | | `ACP_LOG_FILE` | 日志文件路径(默认 XDG state 路径;`off` 关闭文件只保留 stderr)。10 MB 自动轮转。 | | `ACP_DUMP_SSE` | 调试用:转储原始 SSE 帧的目录——含压缩重发/截断重试的循环内上游响应(命名 `--loop-raw.sse`),外层 tee 看不到(#1455)。 | | `BILI_STREAM_ERROR_SHAPE` | 设为 `"completion"` 恢复 anthropic/openai 线上旧的失败形状(失败文本包在合成的成功完成里);默认 `"protocol"` = 协议原生错误帧(#1455)。与 `compat.streamErrorShape` 同一开关,此环境变量优先。 | diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index e4f0624a86..0f11cfb630 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1968,11 +1968,12 @@ function formatSeam(r: BiliCacheReport): string { out.push(` stateful-upstream check (#2483): if your upstream keeps a live session per conversation and validates the echoed history against its own transcript (claude-code-based relays such as magpie), bili's render tags on assistant text trip that check even though bili's own list is unchanged — set diagnostics.renderNone=true until per-route render-tags control ships; expect exactly ONE replay when flipping the flag mid-session`); } if (r.seam.client.count > 0) { - out.push("✎ CLIENT MID-HISTORY REWRITE (host-side insert — NOT a bili seam)"); + out.push("✎ CLIENT MID-HISTORY REWRITE (host-side insert/rewrite — NOT a bili seam)"); out.push(` ${r.seam.client.count} sample(s) · ${fmtTok(r.seam.client.missed)} tok — the client's own resent history broke mid-list, so bili's faithful rebuild carried the break to the wire (host plugin insert / paste hoisting / re-render). Client-owned re-bill, not a rebuild seam.`); for (const e of r.seam.events) { if (e.client) out.push(` #${e.seq} ${fmtTime(e.at)} hit ${e.hitPct.toFixed(1)}% · input ${fmtTok(e.input)} · inbound message[${e.client.idx}] ${e.client.role}/${e.client.cls} ${e.client.len}B JSON · outbound ${fmtTok(e.lcpBytes)}B @ message[${e.msgIndex ?? "?"}] of ${e.prevMsgs}→${e.curMsgs}`); } + out.push(` render-tag check (#3198): if your host re-renders ACP render tags inside its own resent history (dsh), these breaks are the host re-rendering bili's injected tags — set diagnostics.renderNone=true (ACP_RENDER_NONE=1) to remove the injection; refs stay assigned internally so growth-gated folds keep working, at the cost of per-message ref citation; expect exactly ONE replay when flipping the flag mid-session`); } if (r.seam.abortCorrelated > 0) { out.push("⏻ ABORT-CORRELATED"); diff --git a/src/web/i18n.ts b/src/web/i18n.ts index 8e0ced233e..cc603194b0 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -277,7 +277,7 @@ export const MESSAGES: Record> = { "det.attr_seam_tip": "出站请求体在历史中段出现字节分叉,且无 fold/模型切换/重启归因——疑似重建缝。数值为嫌疑样本重计费的 token。若此前几分钟内发生过折叠,多半是折叠后客户端回显的重付(#2929 后已归入折叠后重建)", "det.attr_provider_tip": "与上一请求间隔很短,且整条消息列表逐字节保留(整体一致,或前缀一致、仅尾部追加新消息;超过 512KiB 取证上限的请求以记录头稳定 + 精确消息数增长判定)却仍 miss——异常:上游淘汰/中继节点轮换,非 bili 重建缝;长间隔的同类样本归入「上游 TTL 过期」", "det.attr_rewind_tip": "客户端 revert/trim 使消息数下降——合法的一次性重计费(保留前缀),非缺陷", - "det.attr_client_tip": "客户端自己重发的历史在中段分叉(插入/改写,如宿主其他插件注入、粘贴挂载、重渲染)——入站消息指纹对在该索引处失配,bili 的忠实重建把这个改动带到了线上。客户端自担的重计费,非 bili 重建缝(#3023)", + "det.attr_client_tip": "客户端自己重发的历史在中段分叉(插入/改写,如宿主其他插件注入、粘贴挂载、重渲染)——入站消息指纹对在该索引处失配,bili 的忠实重建把这个改动带到了线上。客户端自担的重计费,非 bili 重建缝(#3023)。若分叉来自宿主重渲染 bili 注入的 ACP 标签(如 dsh),设 ACP_RENDER_NONE=1 移除注入即可消除缝源(#3198 实测命中率 85.6% → 91.2%)", "det.attr_abort_tip": "客户端流中 abort 后 30s 内结算的 miss 样本数——重试重写重发尾部;相关性而非因果,叠加标记不计入分桶", "det.attr_restart": "⟳ 重启丢窗", "det.attr_restart_tip": "bili 重启或会话恢复后,账本窗口断裂:重启后的首个请求无法与重启前样本配对,miss 按首账计费(初始内容)", @@ -744,7 +744,7 @@ export const MESSAGES: Record> = { "det.attr_seam_tip": "Outbound bodies diverged mid-history with no fold/model-switch/restart attribution — suspected rebuild seam. Value = tokens re-billed on suspect samples. If a fold landed minutes earlier this is usually the post-fold client-echo re-bill (fold-owned since #2929)", "det.attr_provider_tip": "Arrived shortly after the previous request, whose entire message list came back byte-identical (whole body, or its prefix with only tail messages appended; above the 512 KiB forensics cap, proven by the stable recorded head plus exact message-count growth) yet the upstream did not serve its cache — abnormal: eviction / relay node rotation. Long-gap siblings are booked under upstream TTL expiry", "det.attr_rewind_tip": "Client revert/trim shrank the message count — sanctioned one-time re-bill of the retained prefix, not a defect", - "det.attr_client_tip": "The client's own resent history diverged mid-list (insert/rewrite — another host plugin, paste hoisting, re-render): the inbound message-fingerprint pair mismatches at that index, and bili's faithful rebuild carried the change to the wire. Client-owned re-bill, not a bili rebuild seam (#3023)", + "det.attr_client_tip": "The client's own resent history diverged mid-list (insert/rewrite — another host plugin, paste hoisting, re-render): the inbound message-fingerprint pair mismatches at that index, and bili's faithful rebuild carried the change to the wire. Client-owned re-bill, not a bili rebuild seam (#3023). If the divergence comes from your host re-rendering bili's injected ACP tags (dsh), set ACP_RENDER_NONE=1 to remove the injection and the seam source with it (#3198: hit rate 85.6% → 91.2%)", "det.attr_abort_tip": "Missed samples settled within 30s of a client mid-stream abort — retry churn rewrites the resent tail; correlation, not causation (overlay marker, not a bucket)", "det.attr_restart": "⟳ restart drop", "det.attr_restart_tip": "After a bili restart or session restore the ledger window breaks: the first post-restart request cannot pair with pre-restart samples, so its miss is billed as a first bill (initial content)", diff --git a/tests/cache-seam-client-insert.test.ts b/tests/cache-seam-client-insert.test.ts index fe9c222ef7..37c297888d 100644 --- a/tests/cache-seam-client-insert.test.ts +++ b/tests/cache-seam-client-insert.test.ts @@ -61,6 +61,9 @@ test("client insert: inbound mid-list break books the client bucket, not the sea // /acp-cache text + web payload surface the bucket. const text = handleAcpCache(s).text; assert.match(text, /CLIENT MID-HISTORY REWRITE/); + // #3198: the section carries the renderNone mitigation for hosts that re-render ACP tags (dsh). + assert.match(text, /render-tag check \(#3198\)/); + assert.match(text, /diagnostics\.renderNone=true/); const report = buildSessionCacheReport(s); assert.equal(report.seam.client.count, 1); assert.ok(report.seam.client.missed > 0); From 3d4b9bca5ac9347702a5e6d16310fce6ffc1772b Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 15 Oct 2026 02:08:56 +0800 Subject: [PATCH 2/2] fix: dsh sessions default to renderTags=none without any env var (#3198) dsh re-serializes tag-shaped bytes in its own persisted history non-byte-stably, so every newly-tagged message breaks the upstream prefix cache on first resend (A/B: hit rate 85.6% -> 91.2% with renderTags=none). Make the validated treatment the DEFAULT for dsh-stamped sessions (x-bili-plugin: dsh -> sticky session.metadata.pluginAgent): renderNone resolution is now env > file > host-default(dsh:true, else false); an explicit diagnostics.renderNone (true OR false) or ACP_RENDER_NONE always beats the host default. All ten strategy/prompt-decision sites follow the same effective value (#1881: the NEVER-echo prompt follows the rendered tags). Refs stay assigned internally, so growth-gated folds keep working; cost is losing per-message ref citation in the model view for dsh sessions. One-time re-pay on upgrade for affected dsh sessions (~23K tok in the A/B), then stable. Docs: CONFIGURATION.md en+zh. --- CONFIGURATION.md | 8 +- CONFIGURATION.zh-CN.md | 8 +- src/knobs.ts | 12 +- src/server.ts | 2 +- src/server/inject.ts | 3 +- src/server/prepare-anthropic.ts | 4 +- src/server/prepare-google.ts | 4 +- src/server/prepare-openai.ts | 4 +- src/server/prepare-responses.ts | 6 +- tests/issue3198-dsh-render-default.test.ts | 195 +++++++++++++++++++++ tests/knobs-config-precedence.test.ts | 18 ++ 11 files changed, 242 insertions(+), 22 deletions(-) create mode 100644 tests/issue3198-dsh-render-default.test.ts diff --git a/CONFIGURATION.md b/CONFIGURATION.md index fe18ea54ce..15540578a4 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -200,7 +200,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `diagnostics.dump4xxMaxBytes` | number | 2097152 (floor 1024) | BILI_DUMP_4XX_MAX_BYTES | Size cap for a single 4xx dump file. | | `diagnostics.maxTotalBytes` | number | unset (off) | BILI_DUMP_MAX_TOTAL_BYTES | Total-size cap for debug dump dirs (dumps/, raw/, SSE dump dir); oldest files deleted first when exceeded (floor 1 MiB). Dumps have no rotation by default and can grow tens of GB/day under active capture (#2412). | | `diagnostics.maxAgeDays` | number | unset (off) | BILI_DUMP_MAX_AGE_DAYS | Age cap in days for debug dump dirs; files older than this are deleted (floor 1 h). Off by default. | -| `diagnostics.renderNone` | boolean | false | ACP_RENDER_NONE | Disable all ACP tag rendering (raw wire study mode; also the mitigation for host re-render cache seams, e.g. dsh — #3198). | +| `diagnostics.renderNone` | boolean | false (dsh sessions: true since #3198) | ACP_RENDER_NONE | Disable all ACP tag rendering (raw wire study mode). Host-aware default since #3198: dsh-stamped sessions render none by default because dsh re-serializes its own resent history non-byte-stably around injected tags, breaking the prefix cache; set an explicit `false` to keep tags on a dsh session. | | `diagnostics.noInjectTool` | boolean | false | ACP_NO_INJECT_TOOL | Stop injecting the acp_compress tool definition into requests. | | `diagnostics.noCompressPrompt` | boolean | false | ACP_NO_COMPRESS_PROMPT | Stop appending the compression doctrine to system prompts. | | `diagnostics.countTokensPassthrough` | boolean | false | ACP_COUNT_TOKENS_PASSTHROUGH | Forward /v1/messages/count_tokens to the upstream instead of answering locally. | @@ -882,7 +882,7 @@ Since #2030 every pure-behavior knob has a config-file key alongside its env var - **Type:** `{ dumpBody?: boolean; dumpReq?: boolean; rawDumpDir?: string; dump4xx?: boolean; dump4xxMaxBytes?: number; maxTotalBytes?: number; maxAgeDays?: number; renderNone?: boolean; noInjectTool?: boolean; noCompressPrompt?: boolean; countTokensPassthrough?: boolean; compressProtocol?: "tools" | "text" }` - **Default:** `{ dumpBody: false, dumpReq: true, rawDumpDir: /raw, dump4xx: false, dump4xxMaxBytes: 2097152, maxTotalBytes: null, maxAgeDays: null, renderNone: false, noInjectTool: false, noCompressPrompt: false, countTokensPassthrough: false, compressProtocol: "tools" }` - **Status:** ACTIVE -- **Description:** Debug/diagnostic toggles that used to be env-only (`ACP_DUMP_BODY`, `ACP_DUMP_REQ`, `ACP_RAW_DUMP_DIR`, `BILI_DUMP_4XX`, `BILI_DUMP_4XX_MAX_BYTES`, `ACP_RENDER_NONE`, `ACP_NO_INJECT_TOOL`, `ACP_NO_COMPRESS_PROMPT`, `ACP_COUNT_TOKENS_PASSTHROUGH`, `ACP_COMPRESS_PROTOCOL`). Mostly read live per request (no restart needed), except `compressProtocol` which resolves once at boot like the env var did. `renderNone` stops the `mNNNNN` render tags from being injected into outgoing history — disable only if your workflow doesn't need ref-based compression (#933). Retention bounds (#2412): dump dirs are write-only with no rotation by default — active body/SSE capture measured at ~63 GB/day can fill a disk in days. Set `maxTotalBytes` and/or `maxAgeDays` to bound them (oldest-first deletion, `err-*`/`summary-err-*` incident dumps protected, deletions logged at warn level); leave both unset for unbounded retention. Details in the [env table](#config-file-keys-for-environment-knobs-2030). +- **Description:** Debug/diagnostic toggles that used to be env-only (`ACP_DUMP_BODY`, `ACP_DUMP_REQ`, `ACP_RAW_DUMP_DIR`, `BILI_DUMP_4XX`, `BILI_DUMP_4XX_MAX_BYTES`, `ACP_RENDER_NONE`, `ACP_NO_INJECT_TOOL`, `ACP_NO_COMPRESS_PROMPT`, `ACP_COUNT_TOKENS_PASSTHROUGH`, `ACP_COMPRESS_PROTOCOL`). Mostly read live per request (no restart needed), except `compressProtocol` which resolves once at boot like the env var did. `renderNone` stops the `mNNNNN` render tags from being injected into outgoing history — disable only if your workflow doesn't need ref-based compression (#933). Since #3198 the resolution is env > file > **host default**: dsh-stamped sessions (the `x-bili-plugin: dsh` header) default to `true` because dsh re-serializes tag-shaped bytes in its own persisted history non-byte-stably, so every newly-tagged message breaks the upstream prefix cache on first resend (A/B: session hit rate 85.6% → 91.2%); every other host stays `false`, and an explicit file/env value always beats the host default. Refs stay assigned internally under `none`, so growth-gated folds keep working; the cost is losing per-message ref citation in the model's view. Retention bounds (#2412): dump dirs are write-only with no rotation by default — active body/SSE capture measured at ~63 GB/day can fill a disk in days. Set `maxTotalBytes` and/or `maxAgeDays` to bound them (oldest-first deletion, `err-*`/`summary-err-*` incident dumps protected, deletions logged at warn level); leave both unset for unbounded retention. Details in the [env table](#config-file-keys-for-environment-knobs-2030). ### `fakeCompletion` @@ -1620,7 +1620,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes | `ACP_PROMPT_CACHE_ROUTING` | `promptCache.routing` | auto | | `ACP_PROVIDERS` | `providersPath` | unset | | `ACP_RAW_DUMP_DIR` | `diagnostics.rawDumpDir` | /raw | -| `ACP_RENDER_NONE` | `diagnostics.renderNone` | false | +| `ACP_RENDER_NONE` | `diagnostics.renderNone` | false (dsh sessions: true since #3198) | | `ACP_SESSION_HEADER` | `sessionHeader` | x-acp-session | | `ACP_UPDATE_TAG` | `updateTag` | latest | | `ACP_UPSTREAM` | `upstream` | https://api.anthropic.com | @@ -1755,7 +1755,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes | `BILI_ALLOW_PRIVATE_HTTP_PROVIDERS` | Opt-in switch (#2984): set to `1`/`true` to let plain-HTTP provider baseUrls on private networks (RFC1918, 127/8, link-local, IPv6 ULA/link-local, `*.local`/`.lan`/`.internal`) pass the agent-registry intake and summary-endpoint guards, which otherwise trust loopback plaintext only. Public IPs/domains and CGNAT (100.64/10) stay refused regardless. Default off — behavior identical to pre-#2984. A set env value owns the knob over `network.allowPrivateHttpProviders`. | | `ACP_SESSION_HEADER` | Conversation-id header name (default `x-acp-session`). | | `ACP_REASONING_KEEP` | Responses API only: set `none` to drop all reasoning items. Default routes reasoning through the compression pipeline so it is hidden automatically once its turn is summarized (prevents the unbounded accumulation that broke Codex's prompt-cache prefix). | -| `ACP_RENDER_NONE` | Set to `1` to stop injecting per-message render tags (the `` `` `` markers carrying `mNNNNN` refs) into outgoing request history — applies to every wire format (OpenAI chat, Anthropic, Responses) and compact rebuilds (#933). Default is `text-only`: the model reads these refs to cite messages in `compress` calls, so only disable them once you've confirmed your workflow doesn't need ref-based compression (e.g. tag echoes leaking into client-visible output). Previously this variable was honored only on the Responses path and compact; #933 extended it to all paths. A third use case (#3198): hosts that re-render ACP tags inside their own resent history (dsh) re-render bili's injected tags between resends and produce cache seams from their own churn — with the flag set the injection disappears and so do the seams (A/B: session hit rate 85.6% → 91.2%). Refs stay assigned internally and growth-gated nudges still list compressible ranges, so folding keeps working; what you give up is citing individual messages by ref. Expect exactly ONE full replay when flipping the flag mid-session. | +| `ACP_RENDER_NONE` | Set to `1` to stop injecting per-message render tags (the `` `` `` markers carrying `mNNNNN` refs) into outgoing request history — applies to every wire format (OpenAI chat, Anthropic, Responses) and compact rebuilds (#933). Default is `text-only`: the model reads these refs to cite messages in `compress` calls, so only disable them once you've confirmed your workflow doesn't need ref-based compression (e.g. tag echoes leaking into client-visible output). Previously this variable was honored only on the Responses path and compact; #933 extended it to all paths. Since #3198 the effective default is host-aware: a dsh-stamped session (`x-bili-plugin: dsh`) resolves to `none` with NO variable set at all, because dsh re-renders tag-shaped bytes in its own persisted history non-byte-stably and each newly-tagged message then breaks the upstream prefix cache on first resend (A/B: session hit rate 85.6% → 91.2%, seams gone). All other hosts are unchanged; setting this variable or `diagnostics.renderNone` explicitly (including `0`/`false`) overrides the host default. Refs stay assigned internally and growth-gated nudges still list compressible ranges, so folding keeps working; what you give up is citing individual messages by ref. Expect exactly ONE full replay when flipping the flag mid-session, or when this default first applies after an upgrade. | | `ACP_LOG_FILE` | Log file path (default XDG state path; `off` disables the file, keeps stderr). Auto-rotates at 10 MB. | | `ACP_DUMP_SSE` | Directory to dump raw SSE frames for debugging — including loop-originated upstream responses (compress re-requests and truncation retries, named `--loop-raw.sse`), which the outer forward tee cannot see (#1455). | | `BILI_STREAM_ERROR_SHAPE` | Set `"completion"` to restore the legacy failure shape on the anthropic/openai wire (failure text inside a synthesized successful completion); default `"protocol"` = protocol-native error frames (#1455). Same knob as `compat.streamErrorShape`; this env var wins. | diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index f4fae66825..49c0ddb1b0 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -200,7 +200,7 @@ | `diagnostics.dump4xxMaxBytes` | number | 2097152 (floor 1024) | BILI_DUMP_4XX_MAX_BYTES | 单个 4xx 转储文件的大小上限。 | | `diagnostics.maxTotalBytes` | number | unset (off) | BILI_DUMP_MAX_TOTAL_BYTES | 调试转储目录(dumps/、raw/、SSE dump 目录)的总大小上限;超限时按最旧优先删除(下限 1 MiB)。转储默认无任何轮转,持续捕获下每天可增长数十 GB(#2412)。 | | `diagnostics.maxAgeDays` | number | unset (off) | BILI_DUMP_MAX_AGE_DAYS | 调试转储目录的保留天数上限;超过该天数的文件被删除(下限 1 小时)。默认关闭。 | -| `diagnostics.renderNone` | boolean | false | ACP_RENDER_NONE | 禁用所有 ACP 标签渲染(原始线路研究模式;也是宿主重渲染缓存缝的缓解手段,如 dsh —— #3198)。 | +| `diagnostics.renderNone` | boolean | false(dsh 会话:自 #3198 起为 true) | ACP_RENDER_NONE | 禁用所有 ACP 标签渲染(原始线路研究模式)。#3198 起默认值按宿主感知:dsh 会话默认不渲染标签——dsh 对自身持久化历史的再序列化对注入的标签非字节稳定,会打断前缀缓存;显式设 `false` 可让 dsh 会话保留标签。 | | `diagnostics.noInjectTool` | boolean | false | ACP_NO_INJECT_TOOL | 停止向请求注入 acp_compress 工具定义。 | | `diagnostics.noCompressPrompt` | boolean | false | ACP_NO_COMPRESS_PROMPT | 停止向系统提示词附加压缩教条文本。 | | `diagnostics.countTokensPassthrough` | boolean | false | ACP_COUNT_TOKENS_PASSTHROUGH | 把 /v1/messages/count_tokens 转发给上游,而不是本地应答。 | @@ -882,7 +882,7 @@ - **类型:** `{ dumpBody?: boolean; dumpReq?: boolean; rawDumpDir?: string; dump4xx?: boolean; dump4xxMaxBytes?: number; maxTotalBytes?: number; maxAgeDays?: number; renderNone?: boolean; noInjectTool?: boolean; noCompressPrompt?: boolean; countTokensPassthrough?: boolean; compressProtocol?: "tools" | "text" }` - **默认:** `{ dumpBody: false, dumpReq: true, rawDumpDir: /raw, dump4xx: false, dump4xxMaxBytes: 2097152, maxTotalBytes: null, maxAgeDays: null, renderNone: false, noInjectTool: false, noCompressPrompt: false, countTokensPassthrough: false, compressProtocol: "tools" }` - **状态:** ACTIVE -- **说明:** 此前仅有环境变量形态的调试/诊断开关(`ACP_DUMP_BODY`、`ACP_DUMP_REQ`、`ACP_RAW_DUMP_DIR`、`BILI_DUMP_4XX`、`BILI_DUMP_4XX_MAX_BYTES`、`ACP_RENDER_NONE`、`ACP_NO_INJECT_TOOL`、`ACP_NO_COMPRESS_PROMPT`、`ACP_COUNT_TOKENS_PASSTHROUGH`、`ACP_COMPRESS_PROTOCOL`)。除 `compressProtocol` 在启动时一次性解析(同旧环境变量行为)外,其余均按请求实时读取,无需重启。`renderNone` 停止向外发历史注入 `mNNNNN` 渲染标签 —— 仅当你的工作流不需要基于 ref 的压缩时才关闭(#933)。保留期限约束(#2412):转储目录只写不删、默认无轮转 —— 实测持续开启 body/SSE 捕获约 63 GB/天,数天内即可写满磁盘。设置 `maxTotalBytes` 和/或 `maxAgeDays` 可为其设限(最旧优先删除,`err-*`/`summary-err-*` 事故证据文件受保护不删,删除动作以 warn 级别记录日志);两者都不设置则保持无限期保留。详见[环境变量表](#环境变量的配置键对照-2030)。 +- **说明:** 此前仅有环境变量形态的调试/诊断开关(`ACP_DUMP_BODY`、`ACP_DUMP_REQ`、`ACP_RAW_DUMP_DIR`、`BILI_DUMP_4XX`、`BILI_DUMP_4XX_MAX_BYTES`、`ACP_RENDER_NONE`、`ACP_NO_INJECT_TOOL`、`ACP_NO_COMPRESS_PROMPT`、`ACP_COUNT_TOKENS_PASSTHROUGH`、`ACP_COMPRESS_PROTOCOL`)。除 `compressProtocol` 在启动时一次性解析(同旧环境变量行为)外,其余均按请求实时读取,无需重启。`renderNone` 停止向外发历史注入 `mNNNNN` 渲染标签 —— 仅当你的工作流不需要基于 ref 的压缩时才关闭(#933)。#3198 起解析顺序为 env > 文件 > **宿主默认**:dsh 会话(`x-bili-plugin: dsh` 头)默认为 `true`——dsh 对自身持久化历史中标签形字节的再序列化非字节稳定,每条新注入标签的消息首次重发即打断上游前缀缓存(A/B:会话命中率 85.6% → 91.2%);其他宿主保持 `false`,显式的文件/env 值始终优先于宿主默认。`none` 下 ref 仍在内部分配,增长门控折叠照常工作;代价是模型视图中失去逐消息 ref 引用。保留期限约束(#2412):转储目录只写不删、默认无轮转 —— 实测持续开启 body/SSE 捕获约 63 GB/天,数天内即可写满磁盘。设置 `maxTotalBytes` 和/或 `maxAgeDays` 可为其设限(最旧优先删除,`err-*`/`summary-err-*` 事故证据文件受保护不删,删除动作以 warn 级别记录日志);两者都不设置则保持无限期保留。详见[环境变量表](#环境变量的配置键对照-2030)。 ### `fakeCompletion` @@ -1619,7 +1619,7 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `ACP_PROMPT_CACHE_ROUTING` | `promptCache.routing` | auto | | `ACP_PROVIDERS` | `providersPath` | unset | | `ACP_RAW_DUMP_DIR` | `diagnostics.rawDumpDir` | /raw | -| `ACP_RENDER_NONE` | `diagnostics.renderNone` | false | +| `ACP_RENDER_NONE` | `diagnostics.renderNone` | false(dsh 会话:自 #3198 起为 true) | | `ACP_SESSION_HEADER` | `sessionHeader` | x-acp-session | | `ACP_UPDATE_TAG` | `updateTag` | latest | | `ACP_UPSTREAM` | `upstream` | https://api.anthropic.com | @@ -1751,7 +1751,7 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `BILI_ALLOW_PRIVATE_HTTP_PROVIDERS` | 可选开关(#2984):设为 `1`/`true` 后,私网(RFC1918、127/8、link-local、IPv6 ULA/link-local、`*.local`/`.lan`/`.internal`)上的明文 HTTP provider baseUrl 可通过 agent-registry 接收与摘要端点守卫——这些守卫默认只信任回环明文。公网 IP/域名与 CGNAT(100.64/10)无论如何都拒。默认关闭——行为与 #2984 之前逐字一致。已设置的环境变量值优先于 `network.allowPrivateHttpProviders`。 | | `ACP_SESSION_HEADER` | 会话 id 请求头名称(默认 `x-acp-session`)。 | | `ACP_REASONING_KEEP` | 仅 Responses API:设 `none` 丢弃全部 reasoning 项。默认让 reasoning 走压缩管道,其轮次被摘要后自动隐藏(避免无限累积破坏 Codex 的 prompt-cache 前缀)。 | -| `ACP_RENDER_NONE` | 设为 `1` 停止向出站请求历史注入逐消息渲染标签(承载 `mNNNNN` ref 的 `` `` `` 标记)——适用于所有线格式(OpenAI chat、Anthropic、Responses)及 compact 重建(#933)。默认 `text-only`:模型靠这些 ref 在 `compress` 调用中引用消息,只有确认自己的工作流不需要基于 ref 的压缩(例如标签回声泄漏到客户端可见输出)后才应禁用。此前该变量仅在 Responses 路径与 compact 上生效;#933 扩展到了所有路径。第三种用法(#3198):在自己重发的历史里重渲染 ACP 标签的宿主(如 dsh)会在每次重发之间重新渲染 bili 注入的标签,从而产生源自宿主自身变化的缓存缝——设置该标志后注入消失、缝随之消失(A/B 实测:会话命中率 85.6% → 91.2%)。refs 仍在内部分配、growth-gated nudge 仍列出可压缩区间,所以折叠照常工作;代价是无法再按 ref 引用单条消息。会话中途翻转该标志时预期恰好一次完整重放。 | +| `ACP_RENDER_NONE` | 设为 `1` 停止向出站请求历史注入逐消息渲染标签(承载 `mNNNNN` ref 的 `` `` `` 标记)——适用于所有线格式(OpenAI chat、Anthropic、Responses)及 compact 重建(#933)。默认 `text-only`:模型靠这些 ref 在 `compress` 调用中引用消息,只有确认自己的工作流不需要基于 ref 的压缩(例如标签回声泄漏到客户端可见输出)后才应禁用。此前该变量仅在 Responses 路径与 compact 上生效;#933 扩展到了所有路径。#3198 起有效默认值按宿主感知:dsh 会话(`x-bili-plugin: dsh`)**无需设置任何变量**即解析为 `none`——dsh 对自身持久化历史中标签形字节的重渲染非字节稳定,每条新注入标签的消息首次重发即打断上游前缀缓存(A/B 实测:会话命中率 85.6% → 91.2%,缝消失)。其他宿主不变;显式设置本变量或 `diagnostics.renderNone`(含 `0`/`false`)可覆盖宿主默认。refs 仍在内部分配、growth-gated nudge 仍列出可压缩区间,所以折叠照常工作;代价是无法再按 ref 引用单条消息。会话中途翻转该标志、或升级后此默认首次生效时,预期恰好一次完整重放。 | | `ACP_LOG_FILE` | 日志文件路径(默认 XDG state 路径;`off` 关闭文件只保留 stderr)。10 MB 自动轮转。 | | `ACP_DUMP_SSE` | 调试用:转储原始 SSE 帧的目录——含压缩重发/截断重试的循环内上游响应(命名 `--loop-raw.sse`),外层 tee 看不到(#1455)。 | | `BILI_STREAM_ERROR_SHAPE` | 设为 `"completion"` 恢复 anthropic/openai 线上旧的失败形状(失败文本包在合成的成功完成里);默认 `"protocol"` = 协议原生错误帧(#1455)。与 `compat.streamErrorShape` 同一开关,此环境变量优先。 | diff --git a/src/knobs.ts b/src/knobs.ts index a16b096e7d..caaaab6ade 100644 --- a/src/knobs.ts +++ b/src/knobs.ts @@ -411,11 +411,17 @@ export function dumpGcMaxAgeMs(): number | null { return null; } -/** ACP_RENDER_NONE > diagnostics.renderNone > false (any non-empty env value enables). */ -export function renderNone(): boolean { +/** ACP_RENDER_NONE > diagnostics.renderNone > host default (#3198: dsh sessions + * default to "none" — dsh re-serializes its own resent history non-byte-stably + * around injected ACP tags, breaking the prefix cache; refs stay assigned + * internally so growth-gated folds keep working) > false. An explicit file + * boolean (true OR false) always beats the host default. */ +export function renderNone(dshHost = false): boolean { const envRaw = process.env.ACP_RENDER_NONE; if (envRaw !== undefined) return envRaw.length > 0; - return fileDiag().renderNone === true; + const f = fileDiag().renderNone; + if (typeof f === "boolean") return f; + return dshHost; } /** ACP_NO_INJECT_TOOL > diagnostics.noInjectTool > false. */ diff --git a/src/server.ts b/src/server.ts index 18bfa16398..77bdea4dff 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2154,7 +2154,7 @@ export function prepareCountTokens( try { const { msgs, cacheControls } = anthropicToCore(parsed); // Read-only preview: same policy as the google twin above. - const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: usageGradeInputBaseline(session), renderTags: knobRenderNone() ? "none" : "text-only", contentStore: contentStoreOf(session) }); + const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: usageGradeInputBaseline(session), renderTags: knobRenderNone(session.metadata["pluginAgent"] === "dsh") ? "none" : "text-only", contentStore: contentStoreOf(session) }); const stripped = stripKernelSummaries(turn.messages as BiliMessage[], turn.state); const rebuiltMessages = coreToAnthropic(stripped, cacheControls); log("info", `[${sessionId}] count_tokens pruned: ${msgs.length} → ${stripped.length} msgs`); diff --git a/src/server/inject.ts b/src/server/inject.ts index 935e3dfb71..77bc8538a7 100644 --- a/src/server/inject.ts +++ b/src/server/inject.ts @@ -15,6 +15,7 @@ export function injectSystem( config: Config, surface?: PackSurface, visibilityMarkers = true, + dshHost = false, ): string | AnthropicRequestBody["system"] { // ONLY the static compress prompt goes into the system block — it is the // prefix-cache anchor and must stay byte-stable across turns. The nudge @@ -22,7 +23,7 @@ export function injectSystem( // the caller (prepareAnthropic), never merged into system. const parts: string[] = []; if (opts.compress.injectTool) parts.push(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)); - else if (!knobRenderNone()) { + else if (!knobRenderNone(dshHost)) { // #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch. const tagsOnly = buildAcpTagsOnlyPrompt("function", prompts, surface?.promptSections); if (tagsOnly) parts.push(tagsOnly); diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index 9a0e53083c..c1543fb2a0 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -90,7 +90,7 @@ export async function prepareAnthropic( // #2483: single source for the effective render strategy — processTurn, the // diag log and Prepared must all report what was actually rendered (the // log label used to hardcode "text-only" and lie under renderNone). - const renderStrategy = knobRenderNone() ? "none" : "text-only"; + const renderStrategy = knobRenderNone(session.metadata["pluginAgent"] === "dsh") ? "none" : "text-only"; // #1085: sticky head-system anchor — freeze the client's own `system` text // at first sight and forward it byte-stable; detected changes ride as @@ -309,7 +309,7 @@ export async function prepareAnthropic( rebuiltMessages = [...rebuiltMessages, ...sysNotes.map((text) => ({ role: "user" as const, content: text }))]; } - systemOut = injectSystem(parsed, opts, prompts, loopConfig, surface, visibilityMarkers); + systemOut = injectSystem(parsed, opts, prompts, loopConfig, surface, visibilityMarkers, session.metadata["pluginAgent"] === "dsh"); // #1637: stamp the steady body's system. NOTE: parsed.system stays the // #1085 frozen CLIENT head (injectSystem's in-place view) — the round-2 // adapter consumes that frozen head and stamps its own rebuild, so NO diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 4ef4edfb4d..f7e9d38f6a 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -90,7 +90,7 @@ export async function prepareGoogle( const injectTools = shouldInject && !pluginMode; // #2483: this lane used to ignore renderNone entirely (hardcoded "text-only" // in processTurn, Prepared and countTokens) — the knob was dead config on /v1beta. - const renderStrategy = knobRenderNone() ? "none" : "text-only"; + const renderStrategy = knobRenderNone(session.metadata["pluginAgent"] === "dsh") ? "none" : "text-only"; // #2798: google previously only stripped chain carriers — panels, status // marker lines and report bodies rode the rebuild unseen (the old generic @@ -340,7 +340,7 @@ export function prepareGoogleCountTokens( const { msgs } = googleToCore(parsed); // Read-only preview: the store rides in so placeholder substitution is // counted, but nothing is adopted (state is discarded here too). - const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: usageGradeInputBaseline(session), renderTags: knobRenderNone() ? "none" : "text-only", contentStore: contentStoreOf(session) }); + const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: usageGradeInputBaseline(session), renderTags: knobRenderNone(session.metadata["pluginAgent"] === "dsh") ? "none" : "text-only", contentStore: contentStoreOf(session) }); const stripped = stripKernelSummaries(turn.messages, turn.state); const rebuilt: GoogleRequestBody = { ...parsed, contents: coreToGoogle(stripped as BiliMessage[]) }; log("info", `[${sessionId}] countTokens pruned: ${msgs.length} → ${stripped.length} msgs`); diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 35cf6dc025..abd0d61776 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -66,7 +66,7 @@ export async function prepareOpenai( // #2483: single source for the effective render strategy — processTurn, the // diag log and Prepared must all report what was actually rendered (the // log label used to hardcode "text-only" and lie under renderNone). - const renderStrategy = knobRenderNone() ? "none" : "text-only"; + const renderStrategy = knobRenderNone(session.metadata["pluginAgent"] === "dsh") ? "none" : "text-only"; const maxTokens = typeof parsed.max_tokens === "number" ? parsed.max_tokens : 8192; // Title-generation requests (tiny max_tokens) get no compress tooling so @@ -248,7 +248,7 @@ export async function prepareOpenai( const sysParts: string[] = []; if (openaiSystemText) sysParts.push(openaiSystemText); if (shouldInject) sysParts.push(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)); - else if (!isTitleGen && !knobRenderNone()) { + else if (!isTitleGen && !knobRenderNone(session.metadata["pluginAgent"] === "dsh")) { // #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch. const tagsOnly = buildAcpTagsOnlyPrompt("function", prompts, surface?.promptSections); if (tagsOnly) sysParts.push(tagsOnly); diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index f134f29f0c..071bc83327 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -121,7 +121,7 @@ export async function prepareResponses( // first-wins and would silently ignore the new relay's route settings). const responsesTextProtocol = FORCE_TEXT_PROTOCOL || resolveCompressProtocol(opts.routes, upstreamOrigin) === "marker"; - const renderTags: "text-only" | "none" = knobRenderNone() || isCompactionTrigger ? "none" : "text-only"; + const renderTags: "text-only" | "none" = knobRenderNone(session.metadata["pluginAgent"] === "dsh") || isCompactionTrigger ? "none" : "text-only"; try { // [#1638] Plugin mode: position-preserve mid-history system/developer @@ -318,7 +318,7 @@ export async function prepareResponses( ? [] : (session.metadata.codexForgedSummaries as string[] | undefined) ?? []; // #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch. - const tagsOnlyPrompt = !shouldInject && !isCompactionTrigger && !knobNoCompressPrompt() && !knobRenderNone() + const tagsOnlyPrompt = !shouldInject && !isCompactionTrigger && !knobNoCompressPrompt() && !knobRenderNone(session.metadata["pluginAgent"] === "dsh") ? buildAcpTagsOnlyPrompt(responsesTextProtocol ? "hybrid" : "function", prompts, surface?.promptSections) : ""; if (shouldInject && !isCompactionTrigger && !knobNoCompressPrompt()) { @@ -657,7 +657,7 @@ export function prepareResponsesCompact( // no [ACP absorb] instruction bakes into the forged history, and run // the absorb view so absorbed pairs stay hidden in it (wire parity). const compactConfig = ccrLoopConfig(session, { ...config, absorb: undefined }); - const turn = core.processTurn({ messages: projection.msgs, state: session.state, config: compactConfig, tokenCount: usageGradeInputBaseline(session), renderTags: knobRenderNone() ? "none" : "text-only", contentStore: contentStoreOf(session) }); + const turn = core.processTurn({ messages: projection.msgs, state: session.state, config: compactConfig, tokenCount: usageGradeInputBaseline(session), renderTags: knobRenderNone(session.metadata["pluginAgent"] === "dsh") ? "none" : "text-only", contentStore: contentStoreOf(session) }); session.state = turn.state; adoptContentStore(session, turn.contentStore); transformOk = true; diff --git a/tests/issue3198-dsh-render-default.test.ts b/tests/issue3198-dsh-render-default.test.ts new file mode 100644 index 0000000000..d284469a56 --- /dev/null +++ b/tests/issue3198-dsh-render-default.test.ts @@ -0,0 +1,195 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import { loadRoutes, type ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +// #3198: dsh re-serializes its own resent history non-byte-stably around +// injected ACP tags, so every newly-tagged message breaks the upstream prefix +// cache on its first resend (A/B: hit rate 85.6% -> 91.2% with renderTags=none). +// The mitigation used to require ACP_RENDER_NONE=1 per host; these tests pin +// the host-aware DEFAULT: a dsh-stamped session (x-bili-plugin: dsh) renders +// none WITHOUT any env var or config, every other host keeps text-only, and an +// EXPLICIT diagnostics.renderNone=false still disables the default. + +function close(server: http.Server): Promise { + return new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve())); +} + +async function freePort(): Promise { + const server = http.createServer(); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const port = (server.address() as { port: number }).port; + await close(server); + return port; +} + +function upstreamServer(onBody: (path: string, body: unknown) => void): Promise { + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + let parsed: unknown = null; + try { parsed = JSON.parse(Buffer.concat(chunks).toString("utf8")); } catch { /* ignore */ } + onBody(req.url ?? "", parsed); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: "chatcmpl-test", object: "chat.completion", created: 0, model: "test", + choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], + usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 }, + })); + }); + }); + return new Promise((resolve) => server.listen(0, "127.0.0.1", () => resolve(server))); +} + +interface Harness { port: number; stop: () => Promise; cleanup: () => void } + +async function startProxy(upstream: http.Server, configJson: string): Promise { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const root = path.join(tmpdir(), `bili-3198-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`); + const biliConfig = path.join(root, "billion-context.json"); + mkdirSync(root, { recursive: true }); + writeFileSync(biliConfig, configJson, "utf8"); + const previous = process.env.BILI_CONFIG_FILE; + process.env.BILI_CONFIG_FILE = biliConfig; + delete process.env.ACP_RENDER_NONE; + const upstreamPort = (upstream.address() as { port: number }).port; + const port = await freePort(); + const opts: ProxyOptions = { + port, + host: "127.0.0.1", + upstream: `http://127.0.0.1:${upstreamPort}`, + routes: loadRoutes(), + proxy: "", + proxyMode: "direct", + proxySource: "direct", + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + chainContentDetection: false, + passthroughSource: null, + compat: { roles: {} }, + streamErrorShape: "protocol", + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + if (!proxy.listening) await once(proxy, "listening"); + return { + port, + stop: async () => { await close(proxy); }, + cleanup: () => { + if (previous === undefined) delete process.env.BILI_CONFIG_FILE; else process.env.BILI_CONFIG_FILE = previous; + rmSync(root, { recursive: true, force: true }); + }, + }; +} + +const CHAT_BODY = JSON.stringify({ + model: "test", + max_tokens: 1000, + messages: [ + { role: "system", content: "You are helpful." }, + { role: "user", content: "hello" }, + { role: "assistant", content: "hi there" }, + { role: "user", content: "what did we say?" }, + ], +}); + +type Msg = { role?: string; content?: unknown }; + +function chatAssistantText(fwd: Record): string { + const msgs = fwd.messages as Msg[] | undefined; + assert.ok(Array.isArray(msgs), "forwarded body carries messages"); + const a = msgs.find((m) => m.role === "assistant"); + assert.ok(a, "the assistant message reaches the upstream"); + return typeof a.content === "string" ? a.content : JSON.stringify(a.content); +} + +function chatSystemText(fwd: Record): string { + const msgs = fwd.messages as Msg[] | undefined; + assert.ok(Array.isArray(msgs), "forwarded body carries messages"); + const sys = msgs.find((m) => m.role === "system"); + assert.ok(sys, "a system message reaches the upstream"); + return typeof sys.content === "string" ? sys.content : JSON.stringify(sys.content); +} + +test("#3198 dsh-stamped session: renderTags defaults to none without any env var", async () => { + const seen: Array> = []; + const upstream = await upstreamServer((_path, body) => seen.push(body as Record)); + const harness = await startProxy(upstream, `{"providers":{}}`); + try { + const res = await fetch(`http://127.0.0.1:${harness.port}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-session-id": "issue3198-dsh-default", "x-bili-plugin": "dsh" }, + body: CHAT_BODY, + }); + assert.equal(res.status, 200); + assert.equal(seen.length, 1); + assert.equal(chatAssistantText(seen[0]!), "hi there", "dsh default forwards the assistant text byte-identical (no tag injection)"); + assert.ok(!chatSystemText(seen[0]!).includes("NEVER echo"), "tags-only prompt follows the rendered tags (#1881)"); + } finally { + await harness.stop(); + harness.cleanup(); + await close(upstream); + } +}); + +test("#3198 non-dsh session: renderTags stays text-only by default (no behavior drift)", async () => { + const seen: Array> = []; + const upstream = await upstreamServer((_path, body) => seen.push(body as Record)); + const harness = await startProxy(upstream, `{"providers":{}}`); + try { + const res = await fetch(`http://127.0.0.1:${harness.port}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-session-id": "issue3198-nondsh-default" }, + body: CHAT_BODY, + }); + assert.equal(res.status, 200); + assert.equal(seen.length, 1); + assert.ok(chatAssistantText(seen[0]!).startsWith(" { + const seen: Array> = []; + const upstream = await upstreamServer((_path, body) => seen.push(body as Record)); + const harness = await startProxy(upstream, `{"providers":{},"diagnostics":{"renderNone":false}}`); + try { + const res = await fetch(`http://127.0.0.1:${harness.port}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-session-id": "issue3198-dsh-explicit-off", "x-bili-plugin": "dsh" }, + body: CHAT_BODY, + }); + assert.equal(res.status, 200); + assert.equal(seen.length, 1); + assert.ok(chatAssistantText(seen[0]!).startsWith(" { + clearConfig(); + assert.equal(knobs.renderNone(), false, "non-dsh default unchanged"); + assert.equal(knobs.renderNone(true), true, "dsh host default: none"); + setConfig({ diagnostics: { renderNone: false } }); + assert.equal(knobs.renderNone(true), false, "explicit file false beats the dsh host default"); + assert.equal(knobs.renderNone(), false, "explicit file false, non-dsh"); + setConfig({ diagnostics: { renderNone: true } }); + assert.equal(knobs.renderNone(), true, "explicit file true beats the non-dsh default"); + withEnv({ ACP_RENDER_NONE: "" }, () => { + assert.equal(knobs.renderNone(true), false, "a SET env var owns the knob over the host default"); + }); + withEnv({ ACP_RENDER_NONE: "1" }, () => { + assert.equal(knobs.renderNone(), true, "a SET env var enables for everyone"); + }); + clearConfig(); +}); + test("teardown", () => { if (previousConfigFile === undefined) delete process.env.BILI_CONFIG_FILE; else process.env.BILI_CONFIG_FILE = previousConfigFile;