Skip to content

fix: intercept codex's inline auto-compact prompt form (#3008) - #3056

Merged
ranxianglei merged 1 commit into
masterfrom
fix/codex-auto-compact-prompt-form
Oct 13, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
fix/codex-auto-compact-prompt-form

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

问题(#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 措辞)是错的:它会过线,只是不可辨形。

根因(源码级)

  • 分派链(session/turn.rs:1430):TokenBudget → 无模型调用;remote_compaction V2 → 空消息;两者皆不支持时走 local inline = 真实模型调用,请求 = 历史 + 模板 prompt,形态与普通 turn 无异。
  • drain_to_completed 逐项记录 output_item.done,summary_suffix = 最后一条 assistant 消息 —— 伪造回复只需恰好一条 assistant message item + response.completed。

修复

件 内容
hasCompactionPrompt() 从 input[] 尾部向前找最后一条 message:必须是 user 且以内置模板首行锚点开头(You are performing a CONTEXT CHECKPOINT COMPACTION.,与 #2372 前缀同款版本化管理)。中史引文不可能命中(只测最后一条 user);用户自定义 compact_prompt → 锚点失配 → 原样转发(文档化行为)
buildPromptForgeBody() 一条 assistant message item + 完整 SSE 事件序(created→item.added→delta→done→completed+[DONE],非流式给 JSON)——codex 把它记录为压缩输出,compacted payload = codex 自己的 SUMMARY_PREFIX + bili 折叠摘要
复用安全阀 与 trigger 形态同一道门:codexCompactGate(active block + <90% usage-grade 基线 + transform ok)+ 同一份 codexForgedSummaries 捕获账本
handle.ts forge 已备好时跳过 preflight(否则为一条本地响应白跑 19 轮总结)
文档 codexCompact 范围措辞全面更正(config reference EN/ZH + website + conflict remediation)——"never transits the proxy" 全部移除,#3018 的"explicit-only"表述同步修正

拦截矩阵(修复后)

形态 检测 默认行为
compaction_trigger(远端压缩) 最后 input item 类型 intercept(伪造,既有)
/responses/compact(端点) URL intercept(伪造,既有)
内联 auto-compact(本次) 最后 user 消息 = 模板锚点 intercept(伪造)
自定义 compact_prompt 锚点失配 原样转发(文档化)

测试

  • 单元:检测矩阵(锚点/中史引文/assistant 在后/自定义模板/空白容差)+ forge 形态(SSE 全序 + JSON)。
  • e2e:forge 命中(上游 0 命中、摘要进 codexForgedSummaries)/ 无块门禁放行 / pass 模式放行 / 非 codex UA 放行。
  • 红-绿证明:stash src 后 e2e 失败,恢复后绿。
  • 回归:codex-compact(-e2e)、codex-local-compaction、compaction-trigger-finality、native-compact-terminal、conflict-remediation、issue2612、issue3013、issue3004 全绿(55/55);tsc 干净;docs 生成器 + coverage 绿。

Closes #3008

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.
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: fix/codex-auto-compact-prompt-form (fc3d913)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3461-pr.3056.8065d1c

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr3056.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

独立审完这个 PR(没有重做):通读全部 13 个文件的 diff,核对了周边流程(handle.ts 分派、relay.ts 短路、prepare-responses.ts 压缩后重定基线、self-heal 计数),本地定向跑 8 个相关测试套件 69/69 绿,CI 在 head fc3d91382 上 14 条 lane 全绿(含全量 CI lane、E2E real codex、e2e-local-registry)。逐项结论:

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. 是否有回归问题:无(逐项审过):

  • trigger 形态在 handle.ts 提前返回(preflight 之前就走短路),endpoint 形态本就经 responsesCompact 跳过 preflight——本次新增的 preflight 跳过条件实际只作用于新的 prompt 形态,既有两条路径零变化;
  • 普通轮次不会误伤:需同时满足「最后一条 user 消息 = 锚点开头 + codex UA + intercept 模式 + 有活跃块 + <90%」,且模板后若有任何非 user 消息直接判否(中史引文被排除);
  • 非 codex UA 原样转发(e2e 钉住);
  • self-heal 计数:伪造请求只有在恰好是 nudge 到期轮时才会多计一次 idle,而压缩后的下一次请求触发重定基线会把计数清零,良性;
  • prompt 形态压缩后的下一次请求走已上线的 [bug] Codex local auto-compaction can destroy existing bili fold substrate instead of rebasing #2372 重定基线路径(codex 自己的 SUMMARY_PREFIX + 缺口判定),与 codex 自发起的本地压缩同一机制,不是新路径。

6. 是否破坏缓存:不破坏,反而消除了一类重缓存——被拦截的请求不再碰上游(事故场景省掉 720k 重缓存);其余流量线上字节不变,前缀缓存不受影响。伪造的 usage 只是本地记账;账本按精确文本去重,键稳定。

7. 是否改了设计方向:没有架构转向——在既有「检测形态 → 门禁 → 伪造」流水线上加第三种检测形态,复用同一道阀和同一个账本。一个需要明示的取舍:prompt 形态压缩后,codex 的历史里没有 fc_bili_ 标记项可回声,所以捕获的摘要会每一轮重新注入 developer 消息(endpoint 形态语义,代码注释已注明),与 trigger 形态(回声回来后停止注入)不同。这是有意设计——codex 的下一次自动压缩会把上一条 SUMMARY_PREFIX 消息也截掉,账本注入是唯一持久载体;代价是与累计折叠记忆成正比、整个会话期的每轮 token 开销,可接受但应知晓。

8. 是否引入新配置:没有新键。复用 BILI_CODEX_COMPACT / codexCompact(默认 intercept),但其语义范围扩大:现在同时管辖内联 prompt 形态的拦截(此前只管显式两形态)。这是既有旋钮的有意扩展,不是新增配置面。

行为变更清单(旧→新):

  • 内联 auto-compact 请求:原样转发(全量重缓存)→ 默认拦截、本地伪造响应(上游 0 命中);
  • codexCompact 语义:仅显式 trigger/endpoint 形态 → 增加内联 prompt 形态;自定义 compact_prompt → 原样转发(已文档化);
  • 文档措辞:「never transits the proxy」类表述全部移除(原陈述为假),config reference EN/ZH + website + 冲突修复文案同步更正,并由 conflict-remediation 测试钉住;
  • preflight 跳过:仅新增 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 问的旋钮语义扩展属有意设计,建议保留在发布说明里。

@ranxianglei
ranxianglei merged commit dafff54 into master Oct 13, 2026
35 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.384-master.dafff54 (rolling master tag).

npm install -g billion-context@0.0.384-master.dafff54  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

ranxianglei pushed a commit that referenced this pull request Oct 13, 2026
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.
LoveDoLove pushed a commit to LoveDoLove-Forked-Projects/billion-context that referenced this pull request Oct 14, 2026
…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.
@github-actions github-actions Bot mentioned this pull request Oct 14, 2026
@ranxianglei
ranxianglei deleted the fix/codex-auto-compact-prompt-form branch October 15, 2026 05:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bili 对 Codex 原生压缩的拦截分支在本会话没进入(貌似自己截断自己了)

1 participant