Repository navigation
fix: detect silent client history rewrites; archive+prune at the boundary, normalize reversed ref ranges (#1001) - #1009
Conversation
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-1009Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr1009.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 PR #1009 verified — code matches description, local pre-flight reproduces, CI fully green. This is my own PR ([PR]-mirror rule: verify, don't redo). Pulled Diff hygiene — single linear commit over current master, 5 files (+336/−5), every line on-topic: no unrelated files, no whitespace churn, no lock-file drift. Fresh base, no stale-base risk. Code review findings:
Local pre-flight (rebased head, reproduced): typecheck ✓ · CI: all 8 check runs on No new problems found while verifying; the issue-3 verdict (not a regression; salvage merge would be an acp-kernel change) and the 结论:修复了客户端静默改写历史导致的孤儿 block 泄漏与非单调 ref 区间问题(边界处归档+剪枝、区间归一化),本地复跑与 CI 全绿,diff 干净,可以合并。 Merge stays yours: #1009 |
…dary, normalize reversed ref ranges (#1001)
599f19f to
550ce45
Compare
|
Review + handoff note (2026-09-20) Reviewed the three fixes on behalf of #1001 — logic verified sound:
Housekeeping done on this branch: rebased onto current master (was behind #1005/#1007/#1011 — server.ts hot-file overlap resolved, no semantic conflict). Full pre-flight green: typecheck ✅, 1972 pass / 0 fail, build ✅. Gap: the issue's 问题 3 (line-form compress rejected — Scope note: |
…actionable no-valid-ranges hint + regression guard - bump acp-kernel 0.0.78 -> 0.0.79: kernel #330 coalesces the documented line form split across sibling array elements (refs header + summary as separate strings) which previously died kind=no-valid-ranges dropped=2 - stream.ts: the no-valid-ranges failure hint now describes BOTH accepted content shapes (object {startId,endId,summary} and line-form string, including the split-element form) instead of object-only - tests: guard the pinned kernel actually coalesces through the bili applyRanges funnel (issue log: m01588-m01712 line form rejected while the object form of the same content succeeded)
|
Follow-up commit ab06840 — closes the remaining 问题3 (line-form compress regression):
Pre-flight on the pushed head: typecheck ✅ · full suite 1975 tests, 0 fail (2 skipped, pre-existing) · build ✅. With this, all three problems in #1001 are covered: P1 unannounced-rewrite detection + boundary archive/prune, P2 reversed-range normalization, P3 line-form coalesce. |
What changed
Root cause of #1001: opencode silently native-compacted its own history mid-session after the model switch (567 msgs / 613,345 tok vs ~2200 raws in the A-era; giant
h_*blobs are client compaction summaries; ref counter continued past the A-era high-water mark m02196 → same session state, not a reset). bili has no announcement channel for this on the chat/completions path, so the A-era blocks were orphaned — the kernel renders orphaned active blocks at the head of the rebuilt context (prune: earliest surviving effective id ?? 0) alongside the full new content → 613k to model B. The 234s preflight then had to regenerate summaries for content the client had already discarded; that cost is inherent, but the handling of the rewrite was wrong.Three fixes, one test file:
src/session.ts):detectUnannouncedHistoryRewrite(session, knownRefsBefore, liveRawIds)— timing-invariant ratio test: capture the known-ref snapshot BEFOREprocessTurn(post-turn every incoming id has a fresh ref and the ratio reads 1.0), then flag when ≥20 refs were known pre-turn, the session carries compression history, and <50% of incoming messages carry pre-turn refs. Append-only turns stay near 1.0; a rewrite collapses it. Wired into bothprepareAnthropicandprepareOpenai(src/server.ts): detection marks the existingcompactionBoundary, so the rewrite takes the SAME auditable path as an announced/compact—applyCompactionArchivearchives the orphaned blocks (reason "content replaced by client native compaction summary") and prunesbyRaw/byRefto live ids (stops the additive dead-ref leak and the theoretical ref re-issue window).src/stream.ts+src/preflight.ts): after a rewrite, ref numbers are no longer monotonic with position, so position-derived spans come back numerically reversed (the issue's m02243–m00334 et al.). The kernel resolves boundaries BY POSITION and swaps silently — nownormalizeRangeOrder()normalizes up front in both preflight (with an explicit warn log) and theapplyRangesfunnel that guards every compress caller, so specs/logs/stored ranges stay honest.stream.tsloggedstate has ? ref(s) mappedbecause it read.sizeon a plain object (messageRefs.byRefis{}-shaped, not a Map) — always printed "?". Now counts keys. (This line in the issue logs was NOT evidence of lost fold state; refs had continued correctly.)Issue 3 verdict (no code change)
The line-form "regression" is not one: the kernel parser is byte-identical between 0.0.73 and 0.0.74 (bili 0.1.116→0.1.117 only bumped the pin), and the failing calls split header+body into TWO array elements — each element is parsed as its own range, so the first fails "line entry: missing summary after the refs header line" and the second "no mNNNNN/bN refs in header" →
kind=no-valid-ranges dropped=2, exactly matching the logs. Correct line form is ONE string per range:m01588–m01712 topic\n\n<summary>. A salvage-merge (treat[headerOnly, body]as one range) would be an acp-kernel change — cross-repo, manual per AGENTS.md scope note.Side question from the issue
resolveLearnedLimitdid exist session-wide in 0.1.117 (src/weak-overflow.ts:94); it is already REMOVED on master by #987 (commit 721c4a5, weak-overflow.ts deleted) — nothing left to do here.Tests
New
tests/fix-1001-history-rewrite.test.ts(5 tests):normalizeRangeOrder(swaps reversed mNNNNN pairs, leaves bN/mixed untouched);byRawpruned to exactly the live ids, no duplicate ref numbers, rebuilt payload carries the summary and fits, and an append-only turn 3 continues cleanly.Pre-flight (rebased head, origin/master 23df1ef)
npm run typecheck✓npm test✓ 1970 pass / 0 fail / 2 skipped (gated e2e)npm run build✓Closes #1001