Skip to content

[PR] fix(loop): drop hallucinated turn-bleed tail in tag-echo filter - #7417

Open
sx4im wants to merge 1 commit into
ranxianglei:masterfrom
sx4im:fix-7416-tag-echo-turn-bleed
Open

sx4im wants to merge 1 commit into
ranxianglei:masterfrom
sx4im:fix-7416-tag-echo-turn-bleed

Conversation

@sx4im

@sx4im sx4im commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Fixes #7416

What broke
After ~100 tagged messages in a long Google session, the model sometimes misses its turn stop and emits a hallucinated "\nuser\n" turn followed by a fake user request and real functionCall parts in the same response. The tag-echo filter stripped the echoed tags but let the fake user prose and the trailing calls through, so the loop executed them as tool calls.

What changed
Added a code-fence-aware turn-bleed circuit breaker in createGoogleAdapter.parseStream. Outside fenced code blocks, a "user" line followed by a render tag or a client user-turn envelope (USER_REQUEST, ADDITIONAL_METADATA, SYSTEM_MESSAGE, or the "The following is a SYSTEM_MESSAGE" sentence) ends the turn: visible text is cut right before "\nuser", later parts in the chunk and stream are dropped, the upstream is cancelled, and the round settles with finishReason STOP. Short ambiguous prefixes are held across SSE chunk boundaries so a bleed split across chunks is still cut whole. Calls buffered before the bleed still settle normally; tool-call arguments are never touched.

How verified
New tests/issue7416-turn-bleed.test.ts, 9 tests: bleed cut with the trailing call suppressed and the upstream cancelled (single chunk, split across chunks, bare envelope, SYSTEM_MESSAGE sentence, CRLF); a pre-bleed proxy call still executes while a post-bleed call does not; fenced examples, innocent "user" lines, and genuine real-turn calls pass through byte-exact. Targeted suites green locally: adapter-google, verif-tag-echo-filter, absorb-emission, tag-echo, loop-adapters, e2e-tag-echo (203 tests total). Full suite verdict is CI.

Notes
No model-facing text changed. Signed text parts are forwarded verbatim per KDD #10 and are only fence-synced, so a bleed inside signed text is not truncated, though later post-bleed calls are still suppressed.

@ranxianglei ranxianglei changed the title fix(loop): drop hallucinated turn-bleed tail in tag-echo filter [PR] fix(loop): drop hallucinated turn-bleed tail in tag-echo filter Nov 20, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷
(ai: vllm-qwen/qwen3.8-27b)

查重: 不重复 — searched open+closed issues and PRs for "turn bleed" / "tag-echo" / "USER_REQUEST": only #7416 (source issue) and #7417 (this PR) exist for this topic. No prior fix, no parallel PR.

标签: bug + antigravity + scope/wiring + p1 — defect fix in the Google wire adapter; the affected client is Antigravity (agy) on the v1internal lane; p1 because triggering means the client executes tool calls the user never issued (core promise failure).

Review of PR #7417

① Original issue (direction gate). #7416 is real and well-evidenced (sanitized logs, 3 repros, exact mechanism). Root-cause layering: the model-side conditioning (≈100+ render tags → missed native turn stop) is upstream and unfixable here; the in-repo root cause is that the loop has no turn-boundary judgment — every functionCall part in the stream is settled as belonging to the current turn, and the tag-echo filter only sees tag bytes, not the turn boundary. The PR fixes exactly that producer at the decision point (adapter stream parsing) with the issue's proposed design (fence-aware breaker, chunk-boundary hold, cut before \nuser, post-bleed call suppression, upstream cancel, STOP settle). It is a fix at the source, not a payload mask. Direction: correct.

② Regression review (existing behavior of touched paths, pre-change code read):

  • Non-bleed traffic: byte-identical. The breaker emits everything except a bounded held tail — an incomplete column-0 line that is a strict prefix of "user", a fence delimiter still forming, or a "user" line whose follower has not arrived. Released on the next chunk or at flush. New cost: ≤1 chunk of latency on lines starting u/us/use/user. All four terminal paths (finishReason / streamError / blockReason / stream end) release the held tail before settling — verified each has the flushBleed() call.
  • Signed text (KDD release v0.1.3 #10): unchanged. Was clean = part.text verbatim; now fenceSync(part.text) + verbatim — identical output, only internal fence state moves.
  • Tool-call arguments: untouched ([bug] 插件透传会剥掉工具调用参数里的 render tag(arguments 流挂了 tag-echo 过滤器) #1039). The if (turnBled) continue; skip drops only parts buffered AFTER a proven bleed; pre-bleed calls settle exactly as before.
  • Double flushFilter() on the bleed path (bleed block + shared tail): idempotent — verified the filter flush() resets its buffer, second call returns "". No duplication.
  • Bleed-path done without suppressCompletion is correct, not a gap: the loop core then synthesizes the finish frame via emitCompletion (STOP + usageMetadata) — necessary because the upstream finish frame was never replayed (we broke out early). The normal path suppresses precisely because it replays the upstream finish frame verbatim. Same wire shape the client expects.
  • [BUG] In some cases, with Codex's auto model + DeepSeek, it will suddenly terminate automatically. #732 degenerate retry cannot fire on the google wire (sawThinking only comes from done.thinking, which the google adapter never sets) — no new retry behavior.
  • Intended behavior change (the fix itself): old = fake user prose forwarded + post-bleed calls executed → new = text cut before \nuser, all post-bleed parts dropped, upstream cancelled, round settles STOP + warn log [tag-echo] turn-bleed: truncated response at hallucinated user turn.

③ Code review. Proper streaming state machine: line-based scan, CommonMark-faithful fence approximation (≥3 backticks/tildes, closing fence same char + ≥ length + whitespace-only tail, backtick info string may not contain a backtick), CRLF-aware, re-scans from the held prefix on every feed() so no state drift. Follower decision is conservative (match/ambiguous/no); the acp-name follower reuses ACP_NAME_ALT — the same case-folded name set the tag-echo filter uses, so the breaker and filter can't disagree on what a tag is; the 0–7-letter prefix hold matches the longest attested acp-like name (accessp). False-positive surface is bounded: a column-0 user line + proven follower outside a fence — a genuine unfenced response containing that exact shape is indistinguishable from a bleed, and cutting is the right call given the cost asymmetry (truncation vs unauthorized execution). upstream.cancel() fire-and-forget with catch — best-effort, correct. No as any/@ts-ignore, no console.error (uses loggerLog), hex escapes for angle brackets (KDD #2), no new deps, no config surface, no kernel changes.

④ Test validity. 9 tests with real teeth, both directions per gate discipline #3617: 5 positive bleed variants (single chunk, split across chunks, bare envelope, SYSTEM_MESSAGE sentence, CRLF) asserting the exact cut text, zero fake bytes on the wire, no post-bleed tool_call events, exactly one terminal STOP, upstream cancelled, and the read count on a highWaterMark: 0 tracking stream — proving the early stop, not just emission suppression. Pre/post-bleed split: the pre-bleed compress call settles, the post-bleed write_file does not. Negatives: fenced examples pass byte-exact (no cancel, full read), innocent user line with prose follower passes, genuine call forwarded byte-exact including tag-shaped literals in arguments (#1039 pinned). Minor gap (non-blocking): no test for a user line as the stream's final bytes (flush-release at stream end without a following chunk) — the release path is covered via the next-chunk case.

⑤ Diff cleanliness. 2 files, +526/−2, single commit, base current (0 behind master), mergeable: true — the blocked state is branch protection (required review), not a conflict. Every line on-topic; no lockfile/generated churn.

⑤b Code cleanliness. Naming consistent with the repo's BLEED_*/scan-feed-flush vocabulary; no dead code; no duplication; comments explain why (mechanism, KDD #10/#1039 refs, the documented fence-sync blind spot) in the repo's established style; the only spec-derived literals are documented (CommonMark {0,3} indent; the 0–7 name-prefix tied to the longest attested name). Pass.

CI: 30/30 checks completed on the pushed head, 0 failures — all test shards (ubuntu/windows × node 22/24), static-gates, gate, mysql-contract green; e2e lanes skip-gated as usual.

Two disclosure notes:

  1. Residual boundary (inherent, KDD release v0.1.3 #10): a bleed pattern lying entirely inside a signed text part is neither cut nor detected — those bytes cannot be touched, so post-bleed calls in that exact shape would still execute. Low probability (the reported bleed is in unsigned visible text), same accepted class as [acp-loop] In-turn compress re-request edits the latest assistant message → Anthropic 400 "thinking blocks … cannot be modified" (session stays broken) #1960's thought-pane leaks.
  2. PR body precision nit: the Notes line "a bleed inside signed text is not truncated, though later post-bleed calls are still suppressed" overstates (1) — suppression holds when the bleed pattern reaches unsigned visible text; a bleed entirely inside signed text is undetectable. I can't edit the PR body (title/body are author-owned), so flagging here for the author/owner.

Hot-file note (non-blocking): open PRs #7181 (tag-echo mangled names, touches tag-echo-filter.ts) and #7192 (google array prefix) sit in adjacent territory. This PR only imports ACP_NAME_ALT without modifying it — no textual conflict; if #7181 reworks the name set, a semantic re-check at merge time is warranted.


结构化八问

  1. 是什么问题:长 Google 会话里模型偶尔"说超自己的回合",在同一条响应里吐出伪造的 \nuser\n 回合(假用户请求 + 客户端信封)和真实 functionCall;tag-echo 过滤只剥了标签,假文本和调用被放行,客户端把幻觉调用当真实工具调用执行了。
  2. 什么原因导致的:两层——根因是模型被上百个 render tag 条件化、错过 Gemini 原生 turn stop(模型侧,本仓不可修);仓内根因是循环/适配器没有回合边界判定:流里所有 functionCall 都被当作当前回合结算,而 tag-echo 过滤只看标签字节、看不到回合边界。
  3. 影响范围与严重程度:仅 Google/v1internal 车道,需长会话(100+ 带 tag 消息)+ thinking 模型,概率触发;严重度高——触发即执行用户未授权的命令/工具,核心承诺失灵,故 p1。
  4. 怎么修复的:在适配器流解析层加 code-fence 感知的"turn-bleed"断路器——代码块外,user 行后跟 render tag 或客户端用户回合信封即判定回合结束:可见文本在 \nuser 前截断、其后所有 part(含 functionCall)丢弃、取消上游、以 STOP 结算;歧义短前缀跨 SSE 块边界挂起;截断前已缓冲的调用照常结算;工具调用参数永不触碰。
  5. 是否有回归问题:无。逐项审过:非 bleed 流量字节级不变(仅新增 ≤1 块延迟的挂起,下块或 flush 即释放);签名文本(KDD release v0.1.3 #10)仍逐字转发、只同步 fence 状态;四条终局路径均先释放挂起尾巴;bleed 路径的 flushFilter 双调用幂等(实现已核);bleed 的 done 不带 suppressCompletion → 循环合成 finish 帧(STOP+usage),与正常路径"重放上游 finish 帧故抑制"语义一致;[BUG] In some cases, with Codex's auto model + DeepSeek, it will suddenly terminate automatically. #732 退化重试在 google wire 上不可能触发。唯一有意行为变更 = 修复本身(旧:假文本放行+调用执行 → 新:截断+抑制+STOP)。
  6. 是否破坏缓存:不影响——请求侧字节、前缀缓存、增量游标、去重键均未触碰,改动只作用于当前响应的流解析。
  7. 是否改了设计方向:没有——两种压缩模式、wire 形状、kernel 均不动;是适配器层新增的回合边界判定,与既有 tag-echo 过滤的通道划分(可见无签名文本通道)一致。
  8. 是否引入了新的配置:无。

判定:可合并 — mergeable=true、CI 30/30 绿、base 最新、diff 干净、测试有牙且双向。等 owner 合并。

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[google/v1internal] tag-echo strips <acp> tags in hallucinated \nuser\n<acp ...> turn bleed but forwards fake user prompt and trailing functionCalls

2 participants