Skip to content

fix: hold remembered anchor snapshot against auxiliary <=2-message views (#1307) - #1308

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-09-25_fix-aux-snapshot-clobber
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-09-25_fix-aux-snapshot-clobber

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

rememberPluginMessages now holds the established anchor snapshot when an incoming view is ≤2 messages and strictly smaller than the snapshot already stored for the session, instead of overwriting it.

Why — #1307

Host auxiliary prompts (DSH auto-review / classifier / title-gen) share the main conversation's session key but send no tools and ≤2 messages with a normal output budget. So isSideRequest()'s max_tokens<=200 test misses them and they run the full pipeline. The pipeline unconditionally calls rememberPluginMessages, which overwrote the main session's anchor snapshot with this 1-message view. The plugin compress tool reads its refs from that snapshot, so every other ref dangled and compress failed deterministically:

Requested range(s) cannot be anchored (e.g. m00001..m00008) … Run acp_status, then call the compress tool again using only the refs it reports.

Author's prod log: 163/163 compress failures when the last-seen context was 1 message, 62/62 success otherwise.

Fix (write-point guard)

Guard the write point rather than classifying requests upstream: skip the overwrite when incoming.length <= 2 && previousLength > incoming.length. A genuine main turn always carries the full history, so this never fires for one. A matching info log line makes the skip observable (remembered snapshot kept (N msgs > M): …). This is the minimal fix validated by the author's own §3.5 A/B experiment (a guarded-only copy made compress succeed).

Behavior change (flagged per review policy)

  • Old: any prepared view was written; a ≤2-message auxiliary view clobbered the established snapshot.
  • New: such a view is held (snapshot preserved) and an info log is emitted.
  • Accepted cost: a native-compaction boundary that legitimately shrinks the live view to ≤2 is deferred by one turn and self-heals on the next normal request. All other paths are unchanged.

Regression tests (tests/side-request-isolation.test.ts)

  • Unit: foreign 1-msg / 2-msg views are held against a 40-msg snapshot (skip log emitted); larger views still update; a fresh session stores its first view.
  • E2E (anthropic wire): a main request establishes refs + a large snapshot → a tool-less single-message normal-budget request on the same session runs the full pipeline without shrinking the anchor snapshot.

Both tests fail without the guard (verified by disabling it) and pass with it.

Pre-flight

  • npm run typecheck: clean
  • npm test: 2561 tests, 0 fail, 10 skipped
  • npm run build: success
  • Red-state proof: with the guard disabled, both new tests fail; restored → green.

Out of scope (deferred — needs owner decision)

The root-cause fix would route these auxiliary requests to the existing side-passthrough branch (no refs / usage / nudge / orphan-GC / snapshot update). That is deliberately not done here because:

  1. It collides with detectUnannouncedHistoryRewrite 误判 1 条消息的侧边辅助请求(如 WebSearch),导致 Ref 映射表被洗空、编号雪崩耗尽(m99999)及 363 万 Token 溢出 #1075, whose locked-in behavior asserts that an auxiliary single-message request consumes exactly one ref (byRaw 24→25). Any size/shape-based passthrough for this shape breaks that test; the zero-overlap discriminator has the same collision.
  2. The orphan-GC sub-layer (verified real: 3 consecutive such requests can deactivate all active blocks and delete their decompress cache, silently) is ambiguous to gate by message count alone — existing proxy-orphan-gc.test.ts cases use 1–2-message visible sets to model genuine client-deletion orphaning.

Recommend a follow-up issue to design a unified auxiliary-request isolation policy across #1075 / #1307 / orphan-GC.

Fixes #1307

…ews (#1307)

Host auxiliary prompts (DSH auto-review / classifier / title-gen) share the
main session key but send no tools and <=2 messages with a NORMAL output
budget, so isSideRequest()'s budget<=200 test misses them and they run the
full pipeline. rememberPluginMessages then unconditionally overwrote the
established anchor snapshot with the tiny view; the plugin compress tool
reads its refs from that snapshot, so every other ref dangled and compress
failed deterministically with "Requested range(s) cannot be anchored".

Guard the write point: skip the overwrite when the incoming view is <=2
messages and strictly smaller than the established snapshot. A genuine main
turn always carries the full history, so this never fires for one. Accepted
cost: a native-compaction shrink to <=2 is deferred one turn and self-heals
on the next normal request.

Root-cause pipeline isolation (routing these requests to passthrough) is
deliberately out of scope here: it collides with #1075's locked-in behavior
(auxiliary single-message requests consuming exactly one ref) and the
orphan-GC sub-layer is ambiguous to gate by size alone. Tracked separately.
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-25_fix-aux-snapshot-clobber (7af491f)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1308

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1308.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 review complete — mergeable. I did not redo the work; I pulled 2026-09-25_fix-aux-snapshot-clobber (single commit 7af491fb, base = current master dc8bdea5 v0.1.149, no stale-base risk) and verified code + CI + local red-state.

① Original issue (#1307) — direction gate: pass

The bug report carries deterministic prod evidence (163/163 compress failures at ctx==1 vs 62/62 otherwise; §3.5 A/B where guarded-only copy makes compress succeed). This PR implements exactly the issue's recommended Fix A (guard at the write point where the unconditional remembered.set lives), and deliberately defers Fix B (pipeline-entry routing) because it directly contradicts #1075's locked-in assertion that such a request consumes exactly one ref (byRaw 24→25). That contradiction is real and owner-level — correctly parked in follow-up #1309. The fix addresses the producer of the bad state (the unconditional overwrite), not a symptom filter upstream — right layer per the 疏-not-堵 rule.

② Regression audit — existing behaviors on touched paths

Touched path Existing behavior Conclusion
rememberPluginMessages write (src/plugin.ts:265-289) any prepared view unconditionally overwrote the snapshot Intended change, disclosed old→new + accepted cost in the PR body (Deliverables#4 compliant). Sole writer is line 288; sole consumers are plugin.ts:735 (status panel) and plugin.ts:903 (tool-execution messages) — verified by grep, no other readers/writers exist
Plugin compress/decompress tool path read refs from whatever the last request wrote Fixed: ≤2-msg aux view can no longer dangle every other ref
Status panel showed the last written view (incl. clobbered 1-msg view) Improved: keeps showing the main view after an aux request
Proxy-mode preflight / decompress do NOT read remembered (confirmed: only the two plugin.ts consumers) Unchanged
Stale-session GC inside the fn pruned dead-session entries on every call Skipped on the early-return path; opportunistic, self-heals on next non-skipped call — negligible
src/server.ts:2189 called without logger Now passes existing log binding; its type (level: string, msg: string) => void matches the new optional param exactly — behavior-neutral

Edge cases checked: fresh session (previousLength=0) never fires → first small view stored (tested); genuine main turn always grows past the stored length → never fires; equal-size (prev=2, incoming=2) still overwrites — acceptable, both are already-shrunk states; incoming/previousLength use the same processed>0 ? processed : original rule as the consumer, so the comparison is apples-to-apples. Both compression modes considered: the call site runs "for ALL modes" but only plugin mode consumes the map, so proxy mode is untouched.

③ Code review

Guard condition, log line format, and the _rememberedForTest() test seam (follows the existing _resetPluginStateForTest pattern) all match the PR description. No as any/@ts-ignore; comment density justified. Nothing to fix in passing — I found no defects.

④ Test validity — teeth confirmed, red-state independently reproduced

Assertions are exact (snapshot lengths, log containment, forwarded-once count), not tautologies. I reproduced the red-state locally myself: disabling the guard → exactly the 2 new tests fail (unit + anthropic-wire e2e), 14 others still pass; restoring → 16/16 green. The e2e exercises the real pipeline path (full request → prepare → remember) on an established session, which is what pins the regression.

⑤ Diff cleanliness & CI

3 files, +106/−3, single commit, every line serves the fix/tests/seam. CI: all 11 checks green (test matrix ubuntu/windows × node 22/24, e2e codex, hermetic registry, pi lane, version-guard, build-artifact). Local: typecheck clean, target suite 16/16, build success.

Remaining known gaps are out of scope by design and tracked: the orphan-GC deactivation layer (3 consecutive aux requests can silently deactivate blocks) and the #1075/#1307 policy contradiction — both in #1309 pending owner decision. Merge is yours to make.

中文摘要:修复了辅助请求(auto-review/classifier/title-gen,≤2 条消息+正常预算)冲掉 plugin 会话锚点快照导致 compress 全败的问题——在写点加护栏(≤2 条且小于既有快照时跳过写入并打 info log),独立复现了红态证明(关护栏两个新测试必挂)、CI 全绿、无回归,可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

Superseded by #1312 — the owner's implementation fixes the same root cause in rememberPluginMessages with a cleaner diff (src/plugin.ts only, no server.ts touch) and a stronger dedicated e2e (tests/issue1307-side-snapshot.test.ts) that drives plugin-path compress to success. Closing this earlier draft to avoid a second open PR on the same function. See the #1307 thread.

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