Skip to content

[PR] fix(advisory,update-notes): truncate remote reason/summary and collapse newlines into spaces before writing to acp_statu | fix(advisory,update-notes): 远程的 reason/summary 进 acp_status 之前先限长、把换行并成空格 - #6316

Closed
BBleae wants to merge 2 commits into
ranxianglei:masterfrom
mochi-fish:2026-11-12_advisory-notes-cap-remote-text

Conversation

@BBleae

@BBleae BBleae commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #6306

改了什么、为什么

acp_status 是模型调用的工具,它打印的东西都是模型上下文。它的输出里有两部分来自 npm 伴生文档,原来除了 trim() 之外原样保留:

  • CRITICAL ADVISORY (instance-level) 那一行嵌着严重缺陷公告条目的 id 和 reason(describeAdvisory);
  • CRITICAL UPDATE READY / AVAILABLE 下面,待更新区间里每个 release-notes 条目打印一行 · <version> [tier] <summary>。

这两个字段原来都没有长度上限,换行也保留。一个过长或多行的条目,就足以给每个调用 acp_status 的会话塞进几万个字符,还能在 CRITICAL … 标题下另起新行,读起来像是代理自己写的。同样的字符串还会进到单行的日志警告和 bili doctor 报告里。

本 PR 在解析两份文档的地方就把文字限住,所以每个使用方拿到的都是限过的值,acp-status.ts 本身没动:

  • 每一段连续的空白 / C0 / C1 控制字符(CR、LF、TAB、NEL、U+2028、U+2029 …)变成一个空格;
  • 超过上限的文字在码点边界处截断,末尾加 …;
  • 条目保留。文字过长绝不是跳过公告(它仍然要强制安装它的目标版本)或 critical 说明的理由。

上限:summary / summaryZh 400(release-notes/README.md 和 release-notes workflow 在生产端已经在执行的上限),reason 500(advisories 文档原来没写上限;README 里的例子是 83 字符),id 80(它和 reason 在同一行)。

同一份输入分别在 master 和本分支上过两个解析函数(一段 30 000 字符、带两个换行的文字;一条公告,20 个 release-notes 条目):

describeAdvisory acp_status 会打印的区间行
master 30 133 字符,3 行 601 130 字符,60 行
本 PR 598 字符,1 行 8 430 字符,20 行

和 #6153 / PR #6156 的关系:那边为 web 面板的 innerHTML 转义同样这些字段(只改 src/web/client.ts);本 PR 是模型上下文和日志这一面。两者不碰同一批文件,谁也不替代谁。

行为变化说明

行为 旧 新 原因
公告 reason 超过 500 字符 整段保留 前 499 字符 + … acp_status、/acp 面板、doctor、日志、web 横幅、/__bili/status 里那一行有上限
公告 id 超过 80 字符 整段保留 前 79 字符 + … 和 reason 同一行;它也是每个进程的警告去重键(前 80 字符相同的两个 id 会被当成同一个去重)
release-notes summary / summaryZh 超过 400 字符 整段保留 前 399 字符 + … 生产端的约定,现在读取时也执行
这四个字段里的换行、制表符、控制字符、连续空格 保留 一个空格 一个条目 = 一行
文字本来就在这些限制以内的条目 — 不变 —
条目是否被接受 — 不变(字段为空或只有空白仍然拒绝) —
affected、target、version、tier、minVersion、maxVersion、date、publishedAt — 不变 acp_status 不打印它们

release-notes/package.json 里现有的 17 个条目解析结果逐字节不变(最长的 summary 399 字符,没有换行,没有连续两个空格);advisories/package.json 是空的。有一条测试钉住这一点。

没有新增配置项,没有改 wire 协议或持久化,没有改 kernel。

改动

  • src/text-safe.ts:在已有的 safePrefix 旁边加 boundedLine(text, cap)(截断用的就是 safePrefix)。
  • src/advisory.ts:parseAdvisoryDoc 让 id 和 reason 过一遍它(CAP_ADVISORY_ID = 80,CAP_ADVISORY_REASON = 500)。
  • src/update-notes.ts:parseReleaseNotesDoc 让 summary 和 summaryZh 过一遍它(CAP_RELEASE_NOTE_SUMMARY = 400)。
  • advisories/README.md、release-notes/README.md:字段表里写明客户端怎么处理过长或多行的文字(reason 原来写的是 "shown verbatim")。
  • tests/remote-text-bounds.test.ts:8 个用例。
    • boundedLine 本身(限制以内的不变、压平、在上限处截断、不拆开 surrogate 对);
    • 公告 reason 和 id:限长、单行、条目保留、describeAdvisory 里仍有升级命令;
    • release-notes summary / summaryZh:限长、单行、条目保留;
    • 两个解析函数的反方向:普通条目和正好卡在上限的条目与输入 deepEqual;只有空白的字段仍然拒绝;
    • 本仓库自带的文档解析结果不变;
    • 把解析结果当 state 走一遍 handleAcpStatus:正好一行公告、区间里每个条目一行,每行都有上限,文档里的内容不会自己起一行。

没覆盖的

  • advisories workflow 在生产端没有 reason / id 的长度检查(release-notes workflow 有 summary 的)。CI 我没动;如果希望放在同一个 PR 里,我可以把这个检查加上。
  • describeUpdateReady(/acp 面板那一行)和 web 横幅用的是同样的解析结果,所以拿到的也是限过的文字;它们自己的代码没改。
  • 零宽字符和 bidi 格式字符没有去掉,只处理了空白和控制字符。

验证

  • 新的 8 个用例里有 4 个在 master 上失败(三个「限长」用例和 acp_status 那个);4 个反方向用例在两边都通过。加上修复后 8 个全部通过。
  • npm run typecheck、check:unused-exports、check:test-import-graph、tools/check-architecture-docs.mjs、git diff --check:干净。
  • 主套件在本机 Windows / Node 22、并发 4 下跑过:11 013 个测试,10 909 通过,67 跳过,8 失败,29 取消。取消的测试在 4 个文件里(dsh-native、web-sessions、runtime-info、preflight-nonstream-400-probe);后三个单独重跑时通过,dsh-native 在同一环境下没改过的 master 上也是 72 通过 / 4 取消。8 个失败和没改过的 master 在这台机器上的失败相同(杀毒软件对回环 TLS 重新签名:3 个;我跑套件时套着的离线网络守卫:3 个;log-retention.test.ts 里一个依赖本地时区的用例(本机不在 UTC):1 个;默认端口上已经有另一个 bili 实例在监听,issue3544-stale-proxy-rotation.test.ts 里的健康探测连到了它:1 个)。advisory / update-notes 相关的文件(advisory、advisory-panel、advisory-endpoint-parity、issue2462-plugin-advisory、update-notes)不用改,都通过。
  • 没跑:AGENTS.md 要求改 src/advisory.ts 时跑的 registry e2e 套件(ACP_TEST_REGISTRY=1)——它需要本地的 verdaccio,我没有搭;改动只限于 parseAdvisoryDoc 里的两个字符串字段。Linux / macOS 留给 CI。

…mote reason/summary

acp_status prints an advisory's id + reason on its CRITICAL ADVISORY line
and one line per release-note summary under CRITICAL UPDATE, straight
into model context. Both parsers kept that text as-is apart from a trim:
no length cap, line breaks preserved.

Bound it where the documents are parsed (boundedLine in text-safe.ts):
whitespace / control runs become one space, over-long text is cut on a
code-point boundary and ends in an ellipsis. Entries are kept, never
skipped. Caps: summary / summaryZh 400 (the producer-side contract),
reason 500, id 80. Text inside the bounds is unchanged.
Copilot AI balanced review requested due to automatic review settings November 11, 2026 23:49
@ranxianglei ranxianglei changed the title fix(advisory,update-notes): cap length and collapse line breaks of remote reason/summary before they reach acp_status [PR] fix(advisory,update-notes): cap length and collapse line breaks of remote reason/summary before they reach acp_status Nov 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🟡 Changes recommended

A moderate-severity flaw in boundedLine() allows non-positive cap values to produce output longer than the intended bound due to negative slicing via safePrefix(), indicating the function’s “bounded” contract isn’t reliably enforced.

2 open issues
What changed in this PR

Caps and flattens free-text fields from remote advisories and release-notes at parse time to prevent multi-line/overlong content from inflating model-context (acp_status) and other line-oriented outputs.

Changes:

  • Add boundedLine(text, cap) to normalize whitespace/control characters to single spaces and truncate on code-point boundaries with an ellipsis.
  • Apply bounded single-line normalization in advisory (id, reason) and release-note (summary, summaryZh) parsers.
  • Update companion-doc READMEs to describe the enforced bounds/normalization and add regression tests covering truncation/flattening and acp_status line safety.
File Description
tests/​remote-text-bounds.test.ts Adds regression tests for boundedLine() behavior and ensures advisory/release-note parsing (and acp_status output shape) remains single-line and length-capped under hostile inputs.
src/​update-notes.ts Updates release-notes parsing to normalize and cap summary/summaryZh using the shared bounded single-line sanitizer.
src/​text-safe.ts Introduces boundedLine(text, cap) to collapse whitespace/control characters and truncate overly long strings safely with an ellipsis.
src/​advisory.ts Updates advisory document parsing to apply bounded single-line normalization to id and reason with explicit caps.
release-notes/​README.md Updates documentation to reflect that release-note summaries are consumed as single-line, capped text (including truncation behavior).
advisories/​README.md Updates documentation for advisory fields to describe client-side enforcement of single-line normalization and length limits.

Review effort: Balanced

Comment thread src/text-safe.ts
Comment on lines +125 to +129
export function boundedLine(text: string, cap: number): string {
const flat = text.replace(/[\s\x00-\x1f\x7f-\x9f]+/g, " ").trim();
if (flat.length <= cap) return flat;
return `${safePrefix(flat, cap - 1).trimEnd()}\u2026`;
}
Comment thread src/text-safe.ts
Comment on lines +122 to +124
* one space; anything past `cap` code units is cut on a code-point boundary
* and marked with an ellipsis. Text already inside both bounds is returned
* unchanged apart from the outer trim callers did before. */
@BBleae BBleae changed the title [PR] fix(advisory,update-notes): cap length and collapse line breaks of remote reason/summary before they reach acp_status fix(advisory,update-notes): 远程的 reason/summary 进 acp_status 之前先限长、把换行并成空格 Nov 12, 2026
@ranxianglei ranxianglei changed the title fix(advisory,update-notes): 远程的 reason/summary 进 acp_status 之前先限长、把换行并成空格 [PR] fix(advisory,update-notes): truncate remote reason/summary and replace newlines with spaces before writing to acp_status | fix(advisory,update-notes): 远程的 reason/summary 进 acp_status 之前先限长、把换行并成空格 Nov 12, 2026
@ranxianglei ranxianglei changed the title [PR] fix(advisory,update-notes): truncate remote reason/summary and replace newlines with spaces before writing to acp_status | fix(advisory,update-notes): 远程的 reason/summary 进 acp_status 之前先限长、把换行并成空格 [PR] fix(advisory,update-notes): truncate remote reason/summary and collapse newlines into spaces before writing to acp_statu | fix(advisory,update-notes): 远程的 reason/summary 进 acp_status 之前先限长、把换行并成空格 Nov 12, 2026
…g the client caps (ranxianglei#6306)

The release-notes workflow already rejects summary/summaryZh over 400 chars
at publish time; the advisories workflow had no length check at all, so an
over-long reason/id would ship and only get truncated client-side (and the
truncated id would then collide in the per-process warn dedupe). Same
mechanism as the client caps in this PR — one producer-side mirror, same
numbers (id 80, reason 500).
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

Duplicate screening: not a duplicate. Source issue #6306 is yours and this PR fixes it. PR #6156 touches the same feed fields but only src/web/client.ts (browser-side innerHTML escaping) — different surface, different files, neither replaces the other, as your body states. No other open PR caps these fields.

Review (independent pass over the diff, not the issue's analysis)

Direction (①): correct. The root cause is that the two parsers (parseAdvisoryDoc, parseReleaseNotesDoc) kept the four free-text fields as-is apart from trim(), and the producer-side contract (release-notes workflow's 400-char cap) was never enforced on the client while the advisories workflow had no length check at all. Bounding at the parse seam is the right fix point — single decision point, every consumer (acp_status, plugin pre-footer, doctor, log warns, /__bili/status payload, web banners) gets the bounded value, and the in-memory state itself can't balloon. Truncating instead of skipping is the right call: an advisory must still force its target.

Regression review (②) — existing behaviors on the touched paths, each walked:

  1. Entry acceptance/rejection — unchanged: whitespace-only fields still flatten to "" and are still rejected (!id || !reason / !summary); pinned by both negative-direction tests.
  2. Advisory force-install, advisoryBlocksVersion, deferral logic — use only affected/target/versions, which are NOT truncated; untouched.
  3. Per-process warn dedupe key = id (src/advisory.ts:364) — now the truncated id. Your disclosure is accurate: two ids sharing an 80-char prefix would dedupe together; worst case is one suppressed log line, both advisories still apply. Acceptable.
  4. Shipped documents — the test pins all 17 current release-notes entries (and the empty advisories doc) as byte-identical through the new parsers, read from the real repo files.
  5. In-bounds text containing internal whitespace runs — now flattened to single spaces. Intended and disclosed; no shipped entry is affected (test proves it).
  6. Persistence / wire / cache — none of the four fields is persisted or used as a cache/dedup key beyond the warn set above; the status endpoint is a read-only view. No format change.

Code + tests (③④⑤⑤b): clean. boundedLine reuses safePrefix (code-point-safe cut, no lone surrogate — the #816/#828 family), constants are named and follow the existing style, comments explain why, no dead code, no new deps, diff is exactly the six declared files. The 8 new cases assert both directions (per the gate-test discipline), include exactly-at-cap cases, surrogate-pair safety, and an integration case through handleAcpStatus with exact line-count arithmetic.

Verification I ran (on head d5248f1, before my commit):

  • tests/remote-text-bounds.test.ts + tests/text-safe.test.ts: 26/26 pass.
  • tests/advisory*.test.ts (4 files) + tests/update-notes.test.ts: 55/55 pass.
  • tests/acp-status.test.ts: 9/9 pass.
  • npm run typecheck: clean.
  • Mutation check: I reverted just the two parser call-sites to master's trim() behavior and re-ran the new file — exactly 4 of 8 cases fail (the three "bounded" cases + the acp_status case), the 4 negative-direction cases pass. Matches your claim; the tests have teeth.
  • CI on the previous head: 30 checks, 0 failed, 0 pending (green).

One addition I made (additive commit on your branch)

Your "Not covered" note flagged that the advisories workflow has no producer-side length check — and you offered to add it. Same mechanism family as this PR (one issue = one scope), so instead of splitting it off I pushed it as a separate labeled commit on your branch:

  • 7b7b337e4b — .github/workflows/advisories.yml: the "Validate advisory payload" step now rejects id > 80 and reason > 500 at publish time, mirroring the client caps and the existing release-notes.yml 400-char check (same message style). Verified: the script passes on the current (empty) advisories doc and exits 1 with a clear message on an 81-char id.

If you'd rather keep the PR strictly client-side, say so and I'll drop that commit — but a publish-time failure is cheaper than a truncated banner, and the two workflows should be symmetric.

结构化八问

  1. 是什么问题:npm 伴生文档里的四个自由文本字段(advisory 的 id/reason、release-notes 的 summary/summaryZh)不限长、保留换行,原样进入 acp_status(模型上下文)、单行日志警告、bili doctor 和 web 横幅——一条格式不对的条目就能给每个调用 acp_status 的会话注入数万字符,并在 CRITICAL … 标题下伪造"代理自己写的"新行。
  2. 什么原因导致的:两个解析函数对这四个字段只做 trim();生产端约定(release-notes workflow 的 400 上限)客户端从不校验,advisories workflow 则完全没有长度检查。
  3. 影响范围与严重程度:文档中存在超长/多行条目期间,所有调用 acp_status 的会话(advisoryCheck / release-notes watcher 默认开启);p2——不可用性和数据丢失都没有,是模型上下文膨胀 + CRITICAL 标题下的伪行注入;信任域与 bili 已自动安装的 registry 相同,不构成新信任边界。
  4. 怎么修复的:在唯一入口(两个解析函数)限住——所有空白/C0/C1 控制字符序列并成单个空格,超出上限在码点边界截断加省略号(复用 safePrefix,不会切出孤立代理项);条目保留、绝不跳过(公告仍须强制其目标版本)。上限:summary/summaryZh 400(生产端既有约定)、reason 500、id 80;另在生产端 advisories workflow 镜像同样上限(我补的 commit)。
  5. 是否有回归问题:无。逐项过:条目接受/拒绝语义不变(纯空白仍被拒,测试钉死);advisory 强制安装/advisoryBlocksVersion/deferral 只用 affected/target,未触碰;warn 去重键改为截断后的 id(已披露,最坏丢一行日志,两条公告都仍生效);仓库现发的 17 条 release-notes 解析后逐字节不变(测试对真实文件钉死);界内文本的内部空白序列会被并成单空格(有意变更,已披露,现发条目不受影响);无持久化/线协议/缓存格式变化。
  6. 是否破坏缓存:不。四个字段不进持久化、不作缓存键;唯一涉及去重键的是上面第 5 条披露的 id 前缀情形,进程内、无跨进程状态。
  7. 是否改了设计方向:没有。同一架构,限制点放在解析缝(单点、所有消费者受益),无新配置面、无线协议变更。
  8. 是否引入了新的配置:没有。

Verdict: mergeable — after CI re-runs green on the new head (7b7b337). It's an external PR and now also touches a CI publish workflow, so it stays at the human-review tier either way; nothing here blocks the owner's review.

@ranxianglei

Copy link
Copy Markdown
Owner

Landed via batch PR #6455 (merged) — closing as superseded.

@ranxianglei ranxianglei reopened this Nov 14, 2026
@BBleae
BBleae deleted the 2026-11-12_advisory-notes-cap-remote-text branch November 20, 2026 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants