Repository navigation
Description
Summary
When bili runs in proxy/native mode under a host that has its own auto-compaction (WorkBuddy / CodeBuddy kernel), the two compactors form a feedback loop and neither converges. Over a 25-minute window this produced 19 bili folds, 3 completed kernel compactions, a 380–415 s stall per turn, and a negative net token saving.
Observed sequence (one cycle, timings from bili.log)
-
Host kernel decides its context is full and rewrites history, injecting
<conversation_history_summary>/<system-reminder data-role="compact-summary">as the new first message. -
bili's
prefix-affinitysees a first message it does not recognize and declares a new anonymous session:new anonymous session pfa-XXXX (depth=1600, tail=XXXXXXXX; lineage=truncated of pfa-YYYY; ua="<client>") first="<system-reminder data-role=\"compact-summary\"> This session is being continued from a previous conversation..." -
Because the session is new, bili has no usage baseline.
estimateCoreMessagesUpper(char-count-as-token-upper-bound) reports a huge number, so the first-sight path fires even with zero growth:nudge INJECT T1: usage=1567% (2381924/152000), growth=0/22500, pendingT1=614985/50000 model=<alias> reason="EMERGENCY T1: max effective pending 614985, usage 1567% [first-sight mass]" -
bili then runs a preflight compaction over the whole payload and folds 18 ranges, 380 s:
context 1863274 tokens reached preflight target 152000 (model window 152000; usage-baseline=none local-est=530731) preflight compressed 18 range(s), ~1154370 tokens saved (1863274 -> 321775) in 380961ms -
The folded payload is still 321 K, which is above the host's own trigger, so bili gives up and forwards anyway:
payload still over window 152000 by local estimate (~361733 tok, 18 range(s) folded) with no upstream evidence — forwarding once for the upstream to arbitrate size before failing fast (#3146/#4185) -
The host receives 321 K, exceeds its budget, and compacts again → back to step 1.
Result: 4 sessions chained by lineage=truncated (pfa-23c6... → pfa-2711... → pfa-14bd... → pfa-d20e...), each inheriting the previous one's folded output.
Why this is not self-correcting
- Every host compaction resets bili's baseline, so bili always starts again from "first sight" and always over-estimates.
- bili's fold cannot get under the host trigger because the host trigger is lower than the payload bili is willing to forward (bili forwards once rather than fail-fast).
- The fold itself invalidates the provider prefix cache, so each cycle re-pays the full prefix.
Measured cost (one session)
netSaved: -67127 grossSaved: 312200 repayCost: 329268 summaryCost: 50059 cacheHitPct: 1
A comparable healthy session on the same machine, with the same client and no kernel compaction in the window, reports cacheHitPct: 99.
Reproduction
- Client with its own auto-compaction, session budget set by the user (e.g. 300 K), routed through bili.
- Because the model alias is not in bili's registry (see the alias issue), bili resolves the window to 200 000 and the effective wall to 152 000.
- Keep the conversation going until the host compacts once.
- From that point every turn repeats steps 1–6 of the loop.
Reproduction is deterministic in my environment — 3 consecutive turns, 3 full folds, each 380–415 s.
Related, not duplicate
- [Bug] Excessive preflight compression time on ultra-large contexts; client mid-disconnect causes repeated request forwar | [Bug] 超大上下文 preflight 压缩耗时过长,客户端中途断开导致请求反复无法转发 #3978 / [resilience] Preflight compress/timeout loop on sustained mass fold-coverage loss has no graceful degrade #2438 — preflight cost and lack of graceful degradation on huge contexts. Those are about one compaction being slow; this is about the compaction never converging because an external actor keeps resetting the baseline.
- Inflated context estimate makes folding block before forwarding (session appears to hang); /__bili/upstreams is 404 without CORS #5301 — inflated estimate causes fold-before-forward. Same symptom family, but that report is a single-session estimate bug; this one requires a second compactor in the loop.
preflightand[headroom]disagree on the effective window, and preflight folding silently creates no block #5172 — preflight target vs headroom effective window mismatch. That is a within-bili inconsistency; here the mismatch is between two processes, which is why neither side can fix it alone.- bili resolves the context window from its own registry and ignores the client's user-selected tier (300K / 600K / 1M) #4787 — window resolution ignores the client's tier. Fixing bili resolves the context window from its own registry and ignores the client's user-selected tier (300K / 600K / 1M) #4787's A2 would make bili's target match the host's, which would mask this loop but not remove it (the baseline reset on host compaction is independent).
Search performed before filing: two compactors feedback → 0, host-side compaction interop → 0, compaction fight loop → 2 (#1468 closed, #809 unrelated), lineage truncated → 11 (none describe the loop).
What I think the fix looks like (for discussion, not a demand)
- Treat a recognized host compaction envelope (
<conversation_history_summary>,<system-reminder data-role="compact-summary">) as a lineage continuation, not a new anonymous session — carry the baseline (or at least a decayed baseline) across the hop. - When the post-fold payload is still above the resolved window, do not silently forward; the current "forward once" behavior guarantees the host will compact again.
- Optionally: a guard that suppresses a second EMERGENCY fold for the same payload until new user turns arrive.
bili-version
0.1.209 (also reproduced by inspection on 0.1.210 — the relevant code paths are unchanged)
mode
Proxy (CODEBUDDY_BASE_URL / ANTHROPIC_BASE_URL style attach, /bili/ path prefix)
client
WorkBuddy 5.9.2 (CodeBuddy CLI kernel 2.165.0), Windows desktop
os
Windows 11 (win32), Node 22.x
upstream
Provider: copilot.tencent.com (OpenAI-compatible /v2), model alias fast-model (host-side tier alias, maxOutputTokens=48000). Client-declared context window: 300000. bili-resolved window: 200000 → effective 152000.
config
native:
attachUrl: http://127.0.0.1:<port>
attachPolicy: global-fallback
compress:
streamSummary: true
# everything else defaultlogs
12:57:10.400 [prefix-affinity] new anonymous session pfa-XXXX (depth=1600, tail=XXXXXXXX; lineage=truncated of pfa-YYYY; ua="<client>")
first="<system-reminder data-role=\"compact-summary\"> This session is being continued from a previous conversation that ran out of context..."
12:57:10.798 nudge INJECT T1: usage=1567% (2381924/152000), growth=0/22500, pendingT1=614985/50000 model=fast-model, reason="EMERGENCY T1: max effective pending 614985, usage 1567% [first-sight mass]"
12:57:10.921 context 1863274 tokens reached preflight target 152000 (model window 152000, model=fast-model; usage-baseline=none local-est=530731); preflight compressing before forward
12:57:10.9xx [preflight] payload ~2425865 tok vs window 152000 (~16.0x) — raising summarization budget 16 -> 32, rounds 16 -> 32
13:03:31.883 preflight compressed 18 range(s), ~1154370 tokens saved (1863274 -> 321775) in 380961ms
13:03:31.894 payload still over window 152000 by local estimate (~361733 tok, 18 range(s) folded) with no upstream evidence — forwarding once for the upstream to arbitrate size before failing fast (#3146/#4185)
13:03:31.897 slow-session: 1601 msgs / inbound=6.6MiB took local=381909ms — per-turn O(messages) synchronous scans are starving the event loop (#4528)
Session report for the same session:
context 339K / 200K (169.6%) | system prompt 58K | blocks 15 active/20 total
compressions(folds) 19 | input 730K | cached 137K | output 7.5K | cache hit 18.8%
cache economics: rebuild after fold 592K
And the chained sibling session:
netSaved: -67127 grossSaved: 312200 repayCost: 329268 summaryCost: 50059 cacheHitPct: 1
Activity
ranxianglei commentedon Nov 1, 2026
(ai: qwen/qwen3.8-27b)
查重: 不重复。 检索了 open+closed 的 issue/PR(关键词 first-sight mass / EMERGENCY T1 / forward-once / lineage=truncated / host compaction interop)。你列的相关项机制各不同:#3978/#2438 是「单次压缩慢/无优雅降级」,#5301 是「单会话估计虚高导致 fold-before-forward」,#5172 是「bili 内部 preflight-target vs headroom 不一致」,#4787 是「窗口解析忽略客户端档位」。没有任何一个描述的是两个压缩器互相重置基线、永不收敛的反馈环。本 issue 是这个环本身。
我核对过的代码(不是照抄你的描述)
逐条对着 master(
5594c13)验证了环的每一段都真实存在:kernel/src/compress.ts:1870的firstSightMassReady要求baseline === 0 && usage >= minContextLimitPct && max(pending) >= nudgeGrowthTokens。一个全新会话(baseline=0)+ 巨大就绪质量 + 高 usage,即使growth=0也会立刻 arm EMERGENCY T1。这是 fix: compress over unpruned view + acp-kernel 0.0.38 (billion-context-pi#195) #194 的 first-sight-mass bypass,本意是给「无状态全量灌入/恢复态」用的——它没料到会被「宿主中途 compact 一次」反复喂进来。src/prefix-affinity.ts:40-48:匹配是 HEAD-ANCHORED ONLY,历史被截断/改写头就不重挂原链,而是分叉成新匿名会话(lineage 仅作归因,Design: prefix-affinity tail-window reattach adopts sessions on mid-chain evidence — ownership proof needs a redesign, not a bigger threshold #1115)。宿主把<system-reminder data-role="compact-summary">塞成新第一条 ⇒ 头变了 ⇒ 必分叉。conversation_history_summary/compact-summary/data-role="compact"这些标记在src/和kernel/里 grep 全空。bili 对 CodeBuddy/WorkBuddy 的 compact 产物零识别。src/server.ts:4073([Bug] 新会话(无 usage 基线)preflight 把字符数当 token——折叠其实成功(2263796→1111212)却 fail-fast 502,上游实际只计费 540828 #3146/413 Deadlock: When an authenticated session exceeds the reservation window with zero collapsibles, the #3146 forwarding | 413 死锁:带身份会话超预留窗口且零可折叠时,#3146 转发门因 anonymous 条件不触发,会话从首条消息起永久不可用 #4185):折叠后仍超窗且无上游证据时,转发一次让上游裁决尺寸再 fail-fast。这里转发的 321K 同时高于 bili 墙(152K)和宿主触发线 ⇒ 宿主必然再 compact ⇒ 回到第 1 步。关键对照(这是定性的核心): 代码库其实已经解决过「一个会话两个压缩器」——
src/server/dsh-compaction-guard.ts在流量层本地拒绝宿主原生 compact 调用(marker-decisive),因为落地的宿主 checkpoint 会永久 shadow 掉重放区、摧毁 bili 的压缩基底;也就是说「一个会话恰好一个压缩器」这条原则对 dsh/codex 都在执行,唯独 CodeBuddy/WorkBuddy 走
/bili/前缀代理模式是个没被覆盖的洞。所以根因不是某一处判错,而是这条车道缺单属主契约。结构化八问(决策请求)
选项对比
请你拍板两件事
标签: bug · codebuddy · workbuddy · scope/wiring · p1 · needs-decision —— 缺陷类;双客户端标签对应 WorkBuddy 桌面 + CodeBuddy CLI kernel;承重缺陷落在 nudge/请求管道/会话绑定故 scope/wiring;缓存命中崩坏到 1% 且净省为负、每轮 380s,达 p1「体验毁灭」档;needs-decision 因需你先定方向。
(本轮只做了核对与定性,未改任何代码。)
ranxianglei commentedon Nov 1, 2026
(ai: qwen/qwen3.8-27b)
补一条——把我上一条问的「请求头样本」具体化,方便你抓包时直接对号。bili 认会话身份的入口在
src/session-id.ts:82,按顺序取第一个非空:头(headers,小写匹配):
x-bili-plugin-conversation(需伴x-bili-plugin标记才生效) →x-claude-code-session-id→x-grok-session-id/x-grok-conv-id→x-mavis-session-id→x-opencode-session-id→x-session-affinity/x-acp-session/x-session-id/x-opencode-session/session-id/session_id。体(body):Responses 线的
session_id/metadata.session_id,以及 OpenAI/Anthropic/Responses 共用的prompt_cache_key。你日志里是
pfa-…匿名会话 = 上面这些一个都没命中,所以落到 prefix-affinity、换头即分叉。抓一条 CodeBuddy compact 前后各一个请求,重点看两点:x-session-id/session-id)?这条只影响选 A 还是走更便宜的加头修法,不影响根因定性(两个压缩器互踩)。等你样本或 owner 定方向我再动手。
tjp72 commentedon Nov 2, 2026
evidence-5488.md这是你要的详细信息
ranxianglei commentedon Nov 2, 2026
(ai: qwen/qwen3.8-27b)
证据到位,方向收敛——已按我上一条(floor 4)承诺的「加一行识别」路径修完并开了 PR #5526(Closes #5488)。
你的 evidence-5488.md 正好落在我预判的第一分支:CodeBuddy/WorkBuddy 确实发稳定会话 id(在
x-conversation-id,全量 67/67、lite 11/11;baggage: codebuddy.session_id=同值),且这个 id 跨宿主 compact 那一跳不变(compact-summary 只改第一条消息的内容,从不轮换 id)。所以 L1(prefix-affinity 换头即分叉、丢基线)本来就该被这个 id 接住,只是 bili 的取头列表漏了它。这就是我说过「比方案 A 动身份层安全得多」的那条路——现在走了。做了什么(PR #5526)
src/session-id.ts:把x-conversation-id加进conversationHeaderSource候选列表,排在客户端专属头(claude/grok/mavis/opencode/acp)之后、通用头(x-session-id/session-id)之前——这名字太泛,绝不能盖过专属信号。稳定 id 一命中,宿主 compact 后该会话就不再算「全新」,firstSightMassReady(要求baseline===0)从宿主 compact 这条路够不到 ⇒ 每轮匿名分叉 + 零增长 EMERGENCY 折叠风暴消失。fad9805b);定向套件绿(proxy-session-id / responses-instructions-drift / plugin-protocol,54 pass);全量交 CI。行为变更(按交付规则显式标注)
即时止血(不等发版)
受影响用户现在就能:
~/.config/billion-context/billion-context.json里设"sessionHeader": "x-conversation-id"(你实验 5 已验证有效)。注意它在配置加载时绑定、不支持热更,改了要重启 bili。诚实缺口
你的探针是小 payload,没有端到端复现「长会话不再触发 380s EMERGENCY preflight」。逻辑上稳定 id ⇒ 非 fresh ⇒ first-sight-mass 够不到(
kernel/src/compress.ts的baseline===0门),但建议合并前用真实 CodeBuddy/WorkBuddy 长会话跑一遍打补丁构建确认。关于 A/B/C
对本客户端,A/B/C 之争已被证据消解:最便宜的「认头」修法就是正解,B(单属主契约)对 CodeBuddy/WorkBuddy 不再需要。B 泛化(给「压根不发稳定 id 的宿主」立契约)目前没有任何实证案例——CodeBuddy/WorkBuddy 都发稳定 id。我不为假想宿主预立项;将来真冒出这种宿主,再带来源标记单独立 issue 推进 B。
结构化八问
x-conversation-id,导致换头被 prefix-affinity 判成新对话而分叉丢基线,L2/L3(零增长 EMERGENCY、折不到墙下还转发一次)随之被反复点燃。证据把根因从「三层都得动」收窄到「L1 一处识别」。sessionHeader作为手动逃生门早已存在,本次只是文档化它对这类宿主的用法。标签维持:bug · codebuddy · workbuddy · scope/wiring · p1 · needs-decision —— 状态从「待你定方向」转为「PR #5526 已开、待人审」;needs-decision 保留是因为 session-identity 属 §7.4 人审级,且是否要为假想无 id 宿主另立 B 由你定。
收工判定:本 issue(#5488)报告的环已被 PR #5526 的识别修复解决(CI 绿 + 人审合并即生效);建议合并前补一次真实长会话 e2e 确认 380s 风暴消失。若你想让我把「无稳定 id 宿主的单属主契约(B)」立为后续 issue,说一声我再动手——目前不建议预立。
docs(#5488): list x-conversation-id in the session-identity header wa…