Repository navigation
fix: intercept codex's inline auto-compact prompt form (#3008) - #3056
Conversation
Codex's local inline auto-compact (compact.rs run_inline_auto_compact_task, observed rust-v0.162.1) transits the proxy as an ORDINARY turn: full history plus the SUMMARIZATION_PROMPT template as the last user message. No compaction_trigger item, no /responses/compact call — the only wire signature is the prompt text itself, so the existing interception never fired and the request re-cached the full unfolded history (720,706 tokens in the #3008 incident). If the summarization call returned no assistant content, codex fell back to a placeholder summary (template header + last conversation message). - hasCompactionPrompt(): scan input[] backwards; the last message item must be a user message opening with the built-in template anchor. A user-overridden compact_prompt falls out of detection (documented) and transits verbatim. Mid-history quotes cannot match (only the final user message is tested). - buildPromptForgeBody(): one assistant message item + response.completed — exactly what codex's drain_to_completed records item-by-item; its summary_suffix = this text, so the compacted payload becomes codex's own SUMMARY_PREFIX + bili's fold summaries. Forge reuses the same safety gate (active block + <90% usage-grade baseline + transform ok) and the same captured-summaries ledger as the trigger form. - handle.ts: skip preflight when a forge is about to be served locally. - Docs: codexCompact scope copy updated in the config reference (EN/ZH), website, and conflict remediation — 'auto-compaction never transits the proxy' was false and is now corrected everywhere (#3018 follow-up). Tests: detector matrix (anchor-last/quotes/assistant-last/overridden template), forge shapes (SSE + JSON), e2e forge (upstream untouched, summaries captured) + gate-fail/pass-mode/non-codex passthrough.
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.3461-pr.3056.8065d1cThis exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr3056.tgz
npm install -g packageInstall instructions are refreshed on each push. |
(ai: vllm-qwen/qwen3.8-27b) 独立审完这个 PR(没有重做):通读全部 13 个文件的 diff,核对了周边流程(handle.ts 分派、relay.ts 短路、prepare-responses.ts 压缩后重定基线、self-heal 计数),本地定向跑 8 个相关测试套件 69/69 绿,CI 在 head 1. 是什么问题:codex 的本地内联自动压缩过线时长得跟普通续写请求一模一样(完整历史 + 内置模板当最后一条 user 消息,没有 trigger、没有专用端点),bili 现有两个拦截点全都不认识它——事故里那笔请求把 720,706 token 全量重缓存了一遍;模型若没产出 assistant 内容,codex 还会回退成占位摘要,用户看到奇怪的压缩块。 2. 什么原因导致的:codex 的分派链在 TokenBudget 和远端压缩都不支持时落到「本地内联」分支,那是一次真实模型调用,线上形态与普通轮次无法区分——唯一签名是模板文本本身;而既有拦截只认「trigger item / compact 端点」两种形态。之前文档说的「客户端自动压缩不过代理」是错的(#3018 时期措辞),本 PR 一并更正。 3. 影响范围与严重程度:所有走 bili 代理的 codex 用户(默认配置);触发条件 = codex 自身达到自动压缩阈值 + bili 侧有活跃压缩块 + 用量低于 90% 安全线。严重度:性能浪费(全量重缓存,事故中 2m16s 慢请求即此)+ 摘要质量可能退化(占位回退);不丢数据、不产生协议错误。 4. 怎么修复的:按形态检测——从 input[] 尾部找最后一条 message,必须是 user 且以内置模板首行锚点开头(锚点版本化管理,与 #2372 前缀同款做法);命中后走与 trigger 形态同一道安全阀(活跃块 + <90% 证据级基线 + transform ok),伪造一条恰好含一个 assistant 消息的完整响应(codex 会把它逐项记为压缩输出并取最后一条 assistant 消息作摘要),伪造前把活跃块摘要捕获进既有的 codexForgedSummaries 账本。用户自定义 compact_prompt → 锚点失配 → 原样转发(文档化行为);未来 codex 改模板 → 检测失配 → 自动退回旧的转发行为(失败方向是安全的)。 5. 是否有回归问题:无(逐项审过):
6. 是否破坏缓存:不破坏,反而消除了一类重缓存——被拦截的请求不再碰上游(事故场景省掉 720k 重缓存);其余流量线上字节不变,前缀缓存不受影响。伪造的 usage 只是本地记账;账本按精确文本去重,键稳定。 7. 是否改了设计方向:没有架构转向——在既有「检测形态 → 门禁 → 伪造」流水线上加第三种检测形态,复用同一道阀和同一个账本。一个需要明示的取舍:prompt 形态压缩后,codex 的历史里没有 fc_bili_ 标记项可回声,所以捕获的摘要会每一轮重新注入 developer 消息(endpoint 形态语义,代码注释已注明),与 trigger 形态(回声回来后停止注入)不同。这是有意设计——codex 的下一次自动压缩会把上一条 SUMMARY_PREFIX 消息也截掉,账本注入是唯一持久载体;代价是与累计折叠记忆成正比、整个会话期的每轮 token 开销,可接受但应知晓。 8. 是否引入新配置:没有新键。复用 BILI_CODEX_COMPACT / codexCompact(默认 intercept),但其语义范围扩大:现在同时管辖内联 prompt 形态的拦截(此前只管显式两形态)。这是既有旋钮的有意扩展,不是新增配置面。 行为变更清单(旧→新):
验证记录:diff 独立通读 + 周边代码核对;本地定向跑 codex-compact、codex-compact-e2e、conflict-remediation、codex-local-compaction、compaction-trigger-finality、native-compact-terminal、issue2612、issue3004 共 69 用例全绿(注:PR 正文列的「issue3013」在仓库中没有对应测试文件,疑为正文笔误,不影响代码);CI 全 lane 绿。diff 整洁度:单 commit、13 个文件全部与本 PR 目的相关,无无关改动、无锁文件连带、未动 version。 结论:可合并。 无阻断项;第 7 问的每轮再注入持久开销与第 8 问的旋钮语义扩展属有意设计,建议保留在发布说明里。 |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.384-master.dafff54 # this exact build
npm install -g billion-context@master # always the newest merged build
|
The v4 extension-namespaces feature (session_ext + kv_store) is tracked in #3069; every provenance comment and the CHANGELOG line cited #3056, which is an unrelated, already-closed codex inline auto-compact PR. Correct the 10 references so grep-based archaeology lands on the right issue. No logic change; comments/CHANGELOG only.
…v4) (ranxianglei#3056) Two generic tables give host/plugin/UI features a durable home in the unified index without per-feature schema migrations: - session_ext (session_id, key) → value_json: per-session arbitrary state, swept by deleteSession (no pin semantics — checkpoint instead); - kv_store (namespace, key) → value_json: namespaced cross-session state (lane-level / per-agent / global), caller-managed lifecycle (deleteKv("lane:<id>") retires a lane namespace). Keys/namespaces: non-empty printable ≤256 chars (namespaced by convention: "r3:", "web:", "plugin:<name>:", "lane:<id>:"). Values: inline JSON ≤4 MiB (large payloads belong in the content CAS). LIKE metacharacters escaped in listKv key-prefix filters. Kernel state NEVER migrates here — kstate_json stays the kernel's and the tables are outside the LegacySessionLike view (zero digest parity churn). Available in shadow AND full (side-table substrate like checkpoints); legacy-only fails fast with the 501-able message. Store: set/get/list/delete for both tables; deleteSession sweeps session_ext inside its transaction. SessionStore: thin liveShadow() wrappers. Migration v3→v4 is two idempotent CREATE TABLE IF NOT EXISTS. Tests: round-trips/upsert/prefix-isolation, sweep-vs-kv survival, guards, v3→v4 in-place migration + idempotence, legacy 501 class. Schema version assertions 3→4.
问题(#3008)
codex 的本地内联 auto-compact(
compact.rs run_inline_auto_compact_task,实读 rust-v0.162.1 源码)以普通续写请求的形态过线:完整历史 + 最后一条 user 消息 = SUMMARIZATION_PROMPT 模板。没有compaction_trigger、没有/responses/compact——唯一的线上签名就是 prompt 文本本身。因此现有拦截(trigger/endpoint 两种形态)从未触发:之前文档"客户端自动压缩不过代理"(#2372 措辞)是错的:它会过线,只是不可辨形。
根因(源码级)
summary_suffix = 最后一条 assistant 消息—— 伪造回复只需恰好一条 assistant message item + response.completed。修复
hasCompactionPrompt()You are performing a CONTEXT CHECKPOINT COMPACTION.,与 #2372 前缀同款版本化管理)。中史引文不可能命中(只测最后一条 user);用户自定义compact_prompt→ 锚点失配 → 原样转发(文档化行为)buildPromptForgeBody()codexCompactGate(active block + <90% usage-grade 基线 + transform ok)+ 同一份codexForgedSummaries捕获账本handle.ts#3018的"explicit-only"表述同步修正拦截矩阵(修复后)
compaction_trigger(远端压缩)/responses/compact(端点)测试
codexForgedSummaries)/ 无块门禁放行 / pass 模式放行 / 非 codex UA 放行。Closes #3008