Skip to content

fix: require min incoming size + history overlap for unannounced-rewrite detection (#1075) - #1076

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-21_rewrite-min-incoming-guard
Sep 21, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-21_rewrite-min-incoming-guard

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #1075

Problem

Since #1001, detectUnannouncedHistoryRewrite (src/session.ts) had no minimum incoming-size gate (only incomingTotal > 0). Clients like Claude Code mechanically stamp ALL HTTP requests from a terminal with the global x-claude-code-session-id, so standalone auxiliary requests carrying a single brand-new message (WebSearch query refinement, title generation) — incomingTotal=1, knownIncoming=0, ratio 0 < 0.5 — tripped the "client silently rewrote its whole history" detector.

Each hit then ran markCompactionBoundary + applyCompactionArchive, wiping byRaw/byRef down to the single live id. On the next full-history replay (6,301 messages), every message looked brand-new and took a fresh ref from the cursor → +N refs per side-request. In search-heavy long sessions the 5-digit pool (m00001–m99999) exhausts within hours → ref capacity exhausted → prepareAnthropic fails open and forwards the uncompressed 11.9 MiB / 3.63M-token history → upstream 400 prompt too long → 529 overloaded_error retry loop.

Fix (src/session.ts, detection only)

Two conservative gates added to detectUnannouncedHistoryRewrite:

  • REWRITE_MIN_INCOMING_TOTAL = 10 (issue proposal): a genuine rewrite of a mature conversation never arrives as a near-empty payload.
  • knownIncoming > 0: a genuine rewrite always shares at least one message id with the prior history (clients keep a tail through compaction); a side request shares none.

Rationale for conservativism: the failure modes are asymmetric. Missing a genuine rewrite leaves stale map entries until session end (cheap, self-heals). A false positive wipes the whole ref map (fatal — this incident).

Call sites (src/server.ts, anthropic + openai wires) are unchanged; fixing the detector covers both wires. Issue option 2 (changing applyCompactionArchive pruning) was NOT adopted: once false positives are gone the inflation path disappears entirely, and changing prune semantics touches load-bearing persistence/ref-map behavior with no remaining benefit.

Evidence

New tests/fix-1075-side-request.test.ts:

  • Unit: 1-msg / 9-msg / 12-msg-zero-overlap payloads → NOT detected; 模型切换后压缩块跨模型失配,新模型收到 61.3 万 token 全量上下文 #1001-shape rewrite (8 known + 16 new) and floor-size shrinkage (1 known + 11 new) → still detected.
  • E2E (openai wire, real proxy + preflight block): turn 1 builds 24 refs + active block; turn 2 = 1-message side request; turn 3 = full-history replay. Asserts no compaction boundary marked, ref maps survive (25 entries, max ref m00025), originals keep their refs, max ref m00026 (pre-fix: maps pruned to 1, max ref m00050), block survives, outbound payload still carries the preflight summary.

Verified pre-fix: stashing the fix makes 4 of the new tests fail exactly as the incident describes; post-fix all green.

  • typecheck ✅ · full suite 2160 pass / 0 fail / 2 skipped (gated e2e) ✅ · build ✅

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到。这是 PR #1075 的跟踪 issue,我不会重做工作——正在拉取分支核对代码、跑 typecheck + 全量测试 + build、查 CI 状态,完成后回复结论。

@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-21_rewrite-min-incoming-guard (babc437)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1076

Each push to this PR publishes a new version under the pr-1076 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1076.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

[bot] 🏷 PR #1076 核对完成(分支 2026-09-21_rewrite-min-incoming-guard,单 commit babc437)。结论:代码正确、本地全绿、回归测试确实复现原事故,可以合并。

Diff 审查

本地验证(在 rebase 后的 head 上实际运行)

  • typecheck ✅ · build ✅
  • 全量测试:2162 tests → 2160 pass / 0 fail / 2 skipped(gated e2e),与 PR 描述数字一致。
  • 回归有效性:只回退 src/session.ts 的改动、保留新测试 → 恰好 4/6 失败(3 个侧边请求单元测试 + openai-wire e2e,失败形态与事故描述吻合);恢复修复后全绿。测试不是摆设。
  • E2E codex 套件未跑:本环境上游(127.0.0.1:8199)不可达。但本 PR 不触碰 AGENTS.md 要求必跑 e2e 的文件清单(server.ts/src/loop//adapters/preflight),且自带一个走真实 proxy + preflight block 的 openai-wire e2e 测试,已通过。

CI / 冲突

流程备注:此变更影响 compaction boundary 标记时机(进而影响 ref-map prune),按 §7.4 gate 属于人审合并范畴——我不代合并。CI 绿后请 owner 自行 merge。

一句话中文摘要:在 unannounced-rewrite 检测器上加了"入站 ≥10 条且与旧史至少重叠 1 条"两道保守门,消除 Claude Code 侧边辅助请求(WebSearch/标题生成)被误判为整史重写导致的 ref map 洗空与编号雪崩;已实测回退修复时新测试精确复现事故、修复后全量 2160 通过,代码可合并。

@ranxianglei
ranxianglei merged commit 06891be into master Sep 21, 2026
8 checks passed
@ranxianglei
ranxianglei deleted the 2026-09-21_rewrite-min-incoming-guard branch October 15, 2026 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant