Skip to content

Two independent compactors fight each other: host-side kernel compaction spawns a fresh anonymous session inside bili, which then EMERGENCY-folds every turn and forwards a payload still above the host trigger — a self-sustaining loop #5488

Open

Description

@tjp72

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)

  1. 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.

  2. bili's prefix-affinity sees 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..."
    
  3. 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]"
    
  4. 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
    
  5. 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)
    
  6. 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

  1. Client with its own auto-compaction, session budget set by the user (e.g. 300 K), routed through bili.
  2. 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.
  3. Keep the conversation going until the host compacts once.
  4. 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

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 default

logs

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

  1. ranxianglei commented on Nov 1, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    (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)验证了环的每一段都真实存在:

    1. 首见大质量 EMERGENCY 在零增长下触发 —— 成立且是设计使然。 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 一次」反复喂进来。
    2. 换头即分叉 —— 成立且是设计使然。 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"> 塞成新第一条 ⇒ 头变了 ⇒ 必分叉。
    3. bili 完全不认识这个宿主的 compact 信封 —— 确认。 conversation_history_summary / compact-summary / data-role="compact" 这些标记在 src/ 和 kernel/ 里 grep 全空。bili 对 CodeBuddy/WorkBuddy 的 compact 产物零识别。
    4. 「仍超窗就转发一次」是环的自持点 —— 确认。 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 步。

    关键对照(这是定性的核心): 代码库其实已经解决过「一个会话两个压缩器」——

    也就是说「一个会话恰好一个压缩器」这条原则对 dsh/codex 都在执行,唯独 CodeBuddy/WorkBuddy 走 /bili/ 前缀代理模式是个没被覆盖的洞。所以根因不是某一处判错,而是这条车道缺单属主契约。

    标注为推断、尚未实测的两点:① bili 折不到 152K 以下,我判断是非可折地板高(system prompt 58K + protected/recent 区),但没在你环境里复现钉死;② 若 CodeBuddy 其实能发稳定的 session-id 头而 bili 没用上,那第 1 层(prefix-affinity 分叉)整个消失——这点值得先查它的真实请求头。


    结构化八问(决策请求)

    1. 是什么问题:宿主自带 auto-compaction 时,它每 compact 一次就把 bili 的基线清零,bili 每次都当「全新大会话」紧急折叠一轮又折不到宿主触发线以下,只能转发回去逼宿主再 compact——两个压缩器互相踩、永不收敛,缓存命中从 99% 崩到 1%、净省量为负、每轮卡 380s。
    2. 什么原因导致的:根因是一个会话有两个互不知情的上下文属主(不同窗口:宿主 300K 档 vs bili 解析出 200K→有效 152K)。表面链是三层叠加——L1 身份:换头被 prefix-affinity 判成「另一段对话」而分叉丢基线;L2 决策:新会话零增长即触发 first-sight-mass EMERGENCY;L3 收敛:折不到墙下 + 「转发一次」把已知超触发线的载荷喂回宿主。三层各自都是「按设计工作」,组合起来才成环——所以单层打补丁堵不住。
    3. 影响范围与严重程度:能用但体验毁灭级(p1,符合 owner 2026-10-19「缓存命中崩坏导致成本爆炸」档)。触发面:任何「宿主自带 auto-compaction + 经 bili 代理 + 客户端无稳定 session-id」的组合,首次宿主 compact 后每个回合都中招。你实测 netSaved -67K、cacheHitPct 1%、每轮 380–415s、25 分钟 19 次折叠。按选项的影响面见下表。
    4. 怎么修复的(推荐方向):把代码库已有的「单属主」原则补到这条车道——治本走 B(宿主拥有 compact 时确立单属主契约,bili 要么像 dsh 那样拒收宿主信封、要么退位让宿主独占),止血走 A(识别 compact 信封、把这一跳当「同一对话延续」而非「新会话」,带上衰减基线),收敛护栏并入 C(别把已知超宿主触发线的载荷静默转发回去)。三者都动承重面,需你拍板后再动手。
    5. 是否有回归风险(按选项):
    6. 是否破坏缓存:现状正是缓存崩坏的元凶(每轮折叠失效前缀缓存,cacheHitPct 1%)。三个方案都是改善而非破坏缓存;A/B 落地后该会话应回到接近健康会话的命中率。
    7. 是否改了设计方向:A 不改方向,只补识别;B 是明确的方向补全——把「一个会话一个压缩器」从 dsh/codex 的特例提升为对所有「宿主自带 compact」车道的通用契约;C 不改方向,只收紧转发条件。
    8. 是否引入新配置:A/C 不需要;B 可能要(若选「配置声明宿主是否自管 compact」这条路,就是新配置面,须按 Configuration Surface Discipline 先报后做)。若 B 靠流量层信封检测实现则可避免新配置。

    选项对比

    止血力度 治本否 改动面 回归风险 新配置
    A 认信封当延续 止住每轮 fork+紧急折叠风暴 否(地板仍高于宿主线时还打) 身份层 中 无
    B 单属主契约 根治(环结构性消失) 是 架构/可能 wire 高 视实现
    C 收紧转发一次 断自持反馈 部分 转发路径 中高 无

    请你拍板两件事

    • 方向:只做 A 止血?还是 A 止血 + C 护栏一起上?B 作为后续治本立项单独推进?我的推荐是先 A(+C) 止血、B 立为治本 issue 并行设计——但 A 也动身份层,想听你对「带衰减基线跨整段改写」这个语义是否接受的判断。
    • 一个事实缺口:能否给我一条 CodeBuddy 打到 bili 的真实请求头样本(脱敏即可)?我要确认它到底发不发稳定 session-id——若发而 bili 没用上,L1 那层换个更便宜的修法就消失了,能省掉 A 里最险的身份层改动。

    标签: bug · codebuddy · workbuddy · scope/wiring · p1 · needs-decision —— 缺陷类;双客户端标签对应 WorkBuddy 桌面 + CodeBuddy CLI kernel;承重缺陷落在 nudge/请求管道/会话绑定故 scope/wiring;缓存命中崩坏到 1% 且净省为负、每轮 380s,达 p1「体验毁灭」档;needs-decision 因需你先定方向。

    (本轮只做了核对与定性,未改任何代码。)

  2. ranxianglei commented on Nov 1, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    (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 前后各一个请求,重点看两点:

    1. 它到底发不发上面列表里任意一个(尤其通用的 x-session-id / session-id)?
    2. 若发,这个值在宿主 compact 那一跳变不变?
    • 若「发了且稳定不变」→ 那 L1(prefix-affinity 分叉丢基线)本来就该被这个 id 接住却没接住,说明是 bili 的取头逻辑漏了它的具体形态(比如大小写/位置),修法是加一行识别,比方案 A 动身份层安全得多;
    • 若「压根不发」或「compact 时换了值」→ 那只能靠方案 A(认信封当延续)或 B(单属主契约),我上一条的方向判断不变。

    这条只影响选 A 还是走更便宜的加头修法,不影响根因定性(两个压缩器互踩)。等你样本或 owner 定方向我再动手。

  3. tjp72 commented on Nov 2, 2026

    @tjp72
    Author

    evidence-5488.md这是你要的详细信息

  4. ranxianglei commented on Nov 2, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    (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 折叠风暴消失。
    • 回归测试钉住「识别 + 上下两条排序边界」。
    • rebase 到当前 master(fad9805b);定向套件绿(proxy-session-id / responses-instructions-drift / plugin-protocol,54 pass);全量交 CI。

    行为变更(按交付规则显式标注)

    • CodeBuddy/WorkBuddy 代理模式用户:旧 = 每次宿主 compact 都分叉新匿名会话(丢基线、EMERGENCY 折叠、cacheHitPct~1);新 = 同一会话 id 贯穿,基线/折叠状态跨 compact 保留。这正是本次要修的点。
    • 其他所有客户端:零影响(现有客户端都不发这个头;且它排在专属头之后,即便某客户端两者都发,也是专属头赢)。

    即时止血(不等发版)

    受影响用户现在就能:~/.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。

    结构化八问

    1. 是什么问题:宿主自带 auto-compaction 时,每 compact 一次就清零 bili 基线,bili 当「全新大会话」紧急折叠一轮又折不到宿主触发线以下,只能转发回去逼宿主再 compact——两个压缩器互踩、永不收敛(缓存命中 99%→1%、净省量为负、每轮 ~380s)。
    2. 什么原因:根因是一个会话有两个互不知情的上下文属主;本客户端的具体触发链落在 L1 身份层——bili 取头列表漏了 CodeBuddy/WorkBuddy 实际发送的稳定 x-conversation-id,导致换头被 prefix-affinity 判成新对话而分叉丢基线,L2/L3(零增长 EMERGENCY、折不到墙下还转发一次)随之被反复点燃。证据把根因从「三层都得动」收窄到「L1 一处识别」。
    3. 影响范围与严重程度:p1(缓存命中崩坏 + 成本倒挂 + 每轮数百秒,符合 owner 2026-10-19 档)。触发面 = 「宿主自带 compact + 经 bili 代理 + 该稳定 id 未被识别」;本修复覆盖 CodeBuddy/WorkBuddy。
    4. 怎么修的:在既有会话身份识别列表里补上该客户端实际使用的稳定头,排进低信任通用档(不越级)。这是「疏」不是「堵」——让正确的身份信号被接住,不是在流量层改写/过滤载荷。
    5. 回归风险:低。只新增一个候选头且排在专属头之后;现有客户端不发此头、行为零变化;唯一语义变化是 CodeBuddy/WorkBuddy 从「每次 compact 分叉」变「稳定单会话」,即修复目标本身。未触碰 wire 形状 / 持久化格式 / version / kernel。
    6. 是否破坏缓存:改善而非破坏——修前每轮折叠失效前缀缓存(cacheHitPct 1%),修后会话稳定,应回到接近健康会话的命中率。
    7. 是否改设计方向:不改。沿用既有「按稳定 id 归一会话」机制,只补一条识别,没有引入新身份模型或架构转向。
    8. 是否新配置:代码路径不需要新配置(走既有识别列表);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,说一声我再动手——目前不建议预立。

  5. added a commit that references this issue on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions