Skip to content

fix: detect silent client history rewrites; archive+prune at the boundary, normalize reversed ref ranges (#1001) - #1009

Merged
ranxianglei merged 2 commits into
masterfrom
wt-1001-ework-daemon
Sep 19, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
wt-1001-ework-daemon

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

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:

  1. Unannounced-rewrite detection (src/session.ts): detectUnannouncedHistoryRewrite(session, knownRefsBefore, liveRawIds) — timing-invariant ratio test: capture the known-ref snapshot BEFORE processTurn (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 both prepareAnthropic and prepareOpenai (src/server.ts): detection marks the existing compactionBoundary, so the rewrite takes the SAME auditable path as an announced /compact — applyCompactionArchive archives the orphaned blocks (reason "content replaced by client native compaction summary") and prunes byRaw/byRef to live ids (stops the additive dead-ref leak and the theoretical ref re-issue window).
  2. Reversed range normalization (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 — now normalizeRangeOrder() normalizes up front in both preflight (with an explicit warn log) and the applyRanges funnel that guards every compress caller, so specs/logs/stored ranges stay honest.
  3. Observability bug: stream.ts logged state has ? ref(s) mapped because it read .size on a plain object (messageRefs.byRef is {}-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

resolveLearnedLimit did 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):

  • unit: detector positive (8/24 known → detected) + negatives (append-only, fresh session, no blocks);
  • unit: normalizeRangeOrder (swaps reversed mNNNNN pairs, leaves bN/mixed untouched);
  • e2e openai-wire through the real proxy: turn 1 grows past the window → preflight creates a real block → the client rewrites history (last 8 kept + 16 fresh) → asserts boundary marked+consumed, block archived with reason, byRaw pruned 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

@github-actions

github-actions Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: wt-1001-ework-daemon (ab06840)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1009

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1009.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 #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 wt-1001-ework-daemon @ 599f19f, reviewed the full diff against current master (23df1ef), ran the pre-flight locally, and polled CI to completion.

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:

  • Detector (src/session.ts) — timing invariant holds: knownRefsBefore is snapshotted before processTurn in both prepareAnthropic and prepareOpenai, so the ratio can't read 1.0 post-turn. Gates (≥20 known refs, blocks.length > 0, ratio < 0.5) correctly exclude fresh sessions and first replays. False-positive edge analyzed: an append-only turn adding more new messages than the entire prior history could trip the <0.5 ratio, but then nothing left the context → applyCompactionArchive archives zero blocks and the prune keeps every (live) ref, so worst case is one warn log + metadata bookkeeping. Harmless.
  • Archive+prune wiring — markCompactionBoundary sets pending: true; the existing applyCompactionArchive consumes it, archives only active-before/inactive-now blocks with the content replaced by client native compaction summary reason, prunes byRaw/byRef to live ids, and leaves nextIndex untouched (confirmed in session.ts:454-455 comment + code) → no ref-slot re-allocation onto retained tails. Consistent with the kernel id-never-reused contract.
  • Normalization (src/stream.ts) — verified the kernel's CompressRange is a plain mutable {startRef, endRef} object, so the in-place swap in normalizeRangeOrder is safe. Regex is m-ref-only, so bN/mixed endpoints are untouched (correct — no cross-namespace ordering). Applied at both sites: the applyRanges funnel (guards every compress caller) and the preflight position-span loop with its own warn log.
  • Observability fix — .size on the plain-object messageRefs.byRef was indeed always undefined → "?"; Object.keys(...).length is the right fix.
  • Both compression modes — detection lives in the shared prepare* paths that run for every request regardless of pluginMode; the opencode-native-plugin case (the 模型切换后压缩块跨模型失配,新模型收到 61.3 万 token 全量上下文 #1001 repro) goes through prepareOpenai + the preflight span loop, so both modes are covered.

Local pre-flight (rebased head, reproduced): typecheck ✓ · npm test 1970 pass / 0 fail / 2 skipped (exactly matches the PR's claim) · build ✓. All 5 regression tests present as described in tests/fix-1001-history-rewrite.test.ts (detector positive/negatives, normalizeRangeOrder unit, openai-wire e2e through the real proxy), 34 assertions.

CI: all 8 check runs on 599f19f completed successful — test matrix ubuntu/windows × Node 22/24, codex CODEX_HOME overlay, real codex vs deterministic fake upstream, build-artifact, version-guard.

No new problems found while verifying; the issue-3 verdict (not a regression; salvage merge would be an acp-kernel change) and the resolveLearnedLimit side answer stand as written in the PR body.

结论:修复了客户端静默改写历史导致的孤儿 block 泄漏与非单调 ref 区间问题(边界处归档+剪枝、区间归一化),本地复跑与 CI 全绿,diff 干净,可以合并。 Merge stays yours: #1009

@ranxianglei

Copy link
Copy Markdown
Owner Author

Review + handoff note (2026-09-20)

Reviewed the three fixes on behalf of #1001 — logic verified sound:

  1. Detection timing invariant — knownRefsBefore is captured pre-processTurn in both prepareAnthropic (server.ts:1978) and prepareOpenai (server.ts:2140); post-turn the ratio would always read 1.0. Correct.
  2. Boundary wiring — markCompactionBoundary → applyCompactionArchive consumes it with activeBefore captured at server.ts:1995/2147 (pre-turn), and nextIndex is left alone, so the id-never-reused contract (AGENTS.md §2) is respected. Correct.
  3. normalizeRangeOrder inside the applyRanges funnel covers every compress caller (stream.ts:30, stream-openai.ts:25, stream-responses.ts:39, loop/core.ts:161, compress-loop-responses.ts:92, preflight.ts:970) + the preflight span emit. The byRef .size observability fix confirmed (plain object, always printed ?).

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 — kind=no-valid-ranges, dropped=2) is NOT covered by this PR. Root cause is kernel-side and now fixed in acp-kernel #330: models emit the documented line form split across sibling array elements (["m01588–m01712 topic", "## TASK …"]); the header-only half fails "missing summary" and the summary half fails "no refs in header", so the whole call dies. #330 adds coalesceLineEntries() (refless string following a header-only string is adopted as its summary body). Once #330 merges and ships as 0.0.79, this branch will bump the pin so #1001 is fully closed end-to-end.

Scope note: prepareResponses is not wired for silent-rewrite detection — codex announces its compactions through its own channel (reconcileNativeCompactionBoundary, server.ts:2290), so this matches the issue's chat/completions scope; flag here if a Responses client is ever seen rewriting silently.

…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)
@ranxianglei

Copy link
Copy Markdown
Owner Author

Follow-up commit ab06840 — closes the remaining 问题3 (line-form compress regression):

  • acp-kernel pin 0.0.78 → 0.0.79 (kernel preflight fail-fast × soft-protection livelock: over-window payload with nothing foldable outside the protected zone 502s forever #330): the documented line form was being split across sibling array elements (["m01588–m01712 topic", "## TASK …"]), each half invalid → whole call died kind=no-valid-ranges dropped=2 while the object form of the same content succeeded. Kernel now coalesces a bare refs-header string with the summary-only string that follows it.
  • src/stream.ts hint: the failure message now describes BOTH accepted content shapes (object {startId,endId,summary} and line-form string, incl. the split-element form) — previously object-only, which is exactly why the model flailed between formats.
  • New regression guard in tests/fix-1001-history-rewrite.test.ts: split-element line form must produce a block through the bili applyRanges funnel (guards the pinned kernel actually carries the coalesce).

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. Fixes #1001.

@ranxianglei
ranxianglei merged commit 3917d8d into master Sep 19, 2026
8 checks passed
@ranxianglei
ranxianglei deleted the wt-1001-ework-daemon 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.

模型切换后压缩块跨模型失配,新模型收到 61.3 万 token 全量上下文

1 participant