Skip to content

fix: archive blocks across omp native compaction instead of silently deactivating them (#395) - #421

Merged
ranxianglei merged 3 commits into
masterfrom
2026-08-31_omp-native-compact-archive
Sep 3, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
2026-08-31_omp-native-compact-archive

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

Fixes #395: after an in-session omp native compaction (/compact or auto-threshold), bili's per-sid registry silently reuses stale state, syncBlocks deactivates every block whose raw ids left the shortened history, and decompress becomes permanently unreachable — the ledger break #19 shelved.

This wires the omp session_compact hook bili was ignoring and degrades the now-unreachable blocks into an auditable pre-compaction archive instead of silently dropping them.

Mechanism (from the issue, confirmed against source)

  1. omp re-sends a shortened history (its summary + retained tail) and does not rotate the session id on in-session compaction.
  2. bili's per-sid registry reuses the same key with stale state.
  3. syncBlocks deactivates blocks whose effectiveMessageIds no longer intersect the visible history — silently. Retained-tail tags point at dead blocks → unsolvable.
  4. byRaw/byRef are additive (never removed) → stale mappings persist (the 幽灵 ref 永不回收:编号单调膨胀、逼近 99999 时 throw 并静默停摆该请求压缩;orphan-gc 失活块同样泄漏映射 #390 leak).

Changes

  • src/agent/pi.ts (omp reuses this factory): subscribe to session_compact, fire-and-forget POST /__bili/plugin/compact {conversationId}. A failed notification never breaks the agent's compaction.
  • src/plugin.ts + src/server.ts: new POST /__bili/plugin/compact → handlePluginCompact marks a compaction boundary on the session for that conversation.
  • src/session.ts: markCompactionBoundary / applyCompactionArchive / preCompactionArchiveOf. After the processTurn that follows the boundary, blocks that were active before but inactive now are recorded in session.metadata.preCompactionArchive (with a reason + timestamp); byRaw/byRef are pruned to the live raw ids. nextIndex is left alone so a freed ref slot is never re-allocated onto a retained tail's live tag. Distinct from the existing Responses/codex nativeCompactionBoundary (which rebases by resetting state).
  • src/server.ts prepareAnthropic + prepareOpenai: capture active block ids before processTurn, call applyCompactionArchive after it (before orphan-gc). The Responses path is untouched (it has its own rebase).
  • src/loop/core.ts acp_status: lists archived blocks with reason — "content was replaced by the client's native compaction summary … decompress is unavailable".
  • src/decompress-shared.ts: decompress on an archived block returns an explicit error + reason instead of silent failure or wrong-block content.

Acceptance criteria

Pre-flight

  • npm run typecheck — clean
  • npm test — 873 pass (868 + 5 new in tests/compaction-archive.test.ts)
  • npm run build — success
  • E2E (tests/e2e/e2e-codex.test.ts) — not run: the sandbox has no codex binary (spawn codex ENOENT). The changes are additive to the request pipeline (no behavior change unless a compaction boundary is marked), so regression risk is low; please run the E2E on a host with codex before merge.

Note: the recordAnchoredHistoryRewrite (omp token-accounting) and provider-id-rotation options from the issue are out of scope here — this is the minimal-to-complete bili-side fix.

…deactivating them (#395)

omp's in-session native compaction re-sends a shortened history (its summary +
retained tail) without rotating the session id, so bili's per-sid registry
reuses stale state and syncBlocks silently deactivates every block whose raw
ids left the history — making decompress permanently unreachable and decoupling
acp_status from the real visible context (the ledger break #19 shelved).

Wire the omp session_compact hook bili was ignoring:
- agent plugin (pi/omp) POSTs /__bili/plugin/compact on session_compact
- proxy marks a compaction boundary; the next processTurn degrades the
  active->inactive blocks into an auditable pre-compaction archive
- acp_status lists archived blocks with reason; decompress returns an explicit
  error instead of silent failure or wrong-block content
- prune byRaw/byRef to live raw ids at the boundary (stops the #390 additive
  leak); nextIndex is left alone so a freed ref slot is never re-allocated onto
  a retained tail's live tag
@github-actions

github-actions Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-08-31_omp-native-compact-archive (4fa7fcc)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-421

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

Option B — Download artifact

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

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · vllm-qwen/qwen3.8-27b

[bot] 🏷 PR review — verified, LGTM with 2 minor nits + 1 merge gate.

Pre-flight re-run (independent):

  • npm run typecheck — clean
  • npm test — 873/873 pass (868 pre-existing + 5 new in tests/compaction-archive.test.ts)
  • npm run build — success
  • E2E — cannot run here (no codex binary, same as the PR note). ⚠️ Per AGENTS.md, tests/e2e/e2e-codex.test.ts (at least the 4-phase core) must be run on a host with codex before merge — this touches server.ts's request pipeline. This is the only remaining merge gate.

Design verified against source:

  • conversationId mapping is sound: omp stamps prompt_cache_key = <omp session id> (stampPromptCacheKey in src/agent/pi.ts) → server.ts records recordPluginSession(pck, session.id) (server.ts:1103) → conversations map is keyed by the omp session id → the new POST sends sessionIdOf(ctx) (same id) → handlePluginCompact resolves the same session object prepareAnthropic/prepareOpenai use. No identity gap.
  • session_compact event name matches the omp source cited in omp 原生压缩后 bili 注册表跨代失配:syncBlocks 静默失活全部块、decompress 永久不可达(无视 omp session_compact 钩子;#19 搁置项的完整机制) #395 (shared-events.ts:63-87, 220-238). Subscribing on the shared pi/omp factory is harmless for pi (unknown/absent event = no-op; unknown conversation id = 404).
  • One-shot boundary: pending flag consumed by the next processTurn; a no-op compaction (full history re-sent) consumes it with an empty archive — no harm.
  • Persistence/audit: preCompactionArchive + compactionBoundary live in session.metadata with markDirty → survives restart; archivedBlocks records the generation per acceptance criterion 2.
  • Ordering vs orphan-gc is correct: applyCompactionArchive runs before reapOrphanBlocks; orphan-gc skips already-inactive blocks, so archived blocks keep their auditable reason instead of being silently GC'd.
  • Ref-map pruning (byRaw/byRef → live raw ids) stops the 幽灵 ref 永不回收:编号单调膨胀、逼近 99999 时 throw 并静默停摆该请求压缩;orphan-gc 失活块同样泄漏映射 #390 additive leak; nextIndex untouched so freed ref slots can't re-allocate onto retained-tail tags. Responses path untouched.
  • Route placement: behind the same loopback/trusted-origin gate as sibling /__bili/plugin/* routes, same readBody/413/400 error convention.
  • Code rules: no as any/@ts-ignore, no console.error in server modules, no version bump, branch/commit naming compliant.

Minor nits (non-blocking):

  1. Fire-and-forget race: if the shortened request reaches the proxy before the session_compact POST is processed, the blocks are silently deactivated (old behavior) and the boundary is consumed on the next request with an empty archive. Worst case = status quo, no corruption — inherent to the deliberate "a failed notification never breaks compaction" design. Acceptable; noted for the record.
  2. PLUGIN.md gap: the protocol doc listed register/tool/status/manifest but not the new endpoint. Fixed in 1b258ee (pushed to the branch) — adds section 5 documenting POST /__bili/plugin/compact (semantics, fire-and-forget contract, error codes).
  3. No direct unit test for the HTTP handler's 400/404/200 paths (session-level behavior is covered by the 5 integration tests). Optional.

Out-of-scope items (recordAnchoredHistoryRewrite token accounting, provider-id rotation) correctly deferred per the issue's 三档 scoping.

Verdict: LGTM. Merge is gated only on the E2E run on a codex host. (Reminder: PR merges are human-only — I won't merge.)

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · vllm-qwen/qwen3.8-27b

[bot] 🏷 Addendum (minor, pre-existing — not from this PR): package-lock.json on master still says "version": "0.1.65" while package.json is 0.1.67 — the v0.1.66/v0.1.67 release commits didn't sync the lockfile. Harmless for npm ci reproducibility of deps (only the version fields + a couple of stale entries differ), but per the release convention the lockfile should be synced when it drifts — worth folding into the next release commit (release v{VERSION} may include package-lock.json). No action needed for this PR.

…rip list + handlePluginCompact); add compact endpoint test
@ranxianglei

Copy link
Copy Markdown
Owner Author

Re-review: verified against current master (v0.1.74), cleared the E2E merge gate, resolved ework's nit #3 (endpoint test) — pushed as 4fa7fcc to this branch.

The 8-31 review was against a v0.1.67-era base. Since then #440/#444/#475 landed (all touching server.ts), so I re-verified the PR on top of current master.

What I pushed (4fa7fcc)

Pre-flight (on 4fa7fcc = master + PR)

  • typecheck clean, build success
  • full suite: 950 tests / 948 pass / 2 fail — the known permanent pair in plugin-agent.test.ts (reproduces on clean master)
  • new tests 5/5 (compaction-archive) + 9/9 (launcher-plugin-mode)
  • CI on the pushed head: 6/6 green
  • E2E: PASS — the remaining merge gate is cleared. Real codex 0.147.0 through this branch's dist against a live Responses upstream, 4-phase core (warmup → load growth → ACP compress → purity) in 396s, zero failures. Forge phase skipped (E2E_FORGE unset; it covers the codex-native-compact interception path, which this PR does not touch).

Additional facts verified (beyond the 8-31 review)

  • omp really emits the event: 3 extensionRunner.emit({ type: "session_compact", ... }) sites in the shipped omp binary. This grep is the actual proof — on(event: string) in the structural ExtensionAPI type means the compiler cannot catch a typoed event name.
  • liveRawIds semantics hold: kernel message ids are content-hash (h_ + sha256(role|contentType|toolCallId|toolName|text) + per-request cluster counter, acp-kernel/wire message-id), stable across turns — so new Set(msgs.map(m => m.id)) matches the byRaw keyspace and retained-tail ids survive the compaction. Known edge (pre-existing kernel behavior, not introduced here): dropping one of two identical messages shifts the cluster suffix of the other → over-archival (conservative, no corruption).
  • Persistence: metadata (compactionBoundary + preCompactionArchive) round-trips through persist.ts:338/373 → boundary and archive survive eviction/restart.
  • Ordering vs orphan-gc: reapOrphanBlocks skips already-inactive blocks, so archived blocks keep their auditable reason instead of being silently GC'd.

Minor notes (non-blocking, for the record)

  1. handlePluginCompact mutates via peekSession without withSessionLock, while handlePluginTool locks. Safe in practice — the consumer stretch in prepareAnthropic/prepareOpenai (activeBefore capture → processTurn → applyCompactionArchive) is fully synchronous, so no interleave is possible — but the inconsistency could bite a future refactor that inserts an await there.
  2. preCompactionArchive grows unboundedly across /compact cycles (~100 B/entry). Fine for realistic session lifetimes; a long-lived heavy-compacting session accumulates entries forever.
  3. Archived blocks keep their blockContents cache although decompress returns the explicit FAILED message before reading content — orphan-gc deletes content for reaped blocks; the archive path could do the same.

Verdict: LGTM, merge gate cleared. (Merge itself stays human-only per AGENTS.md.)

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] 🏷 Re-verified 4fa7fcc independently — all claims check out, nothing outstanding on my side.

Merge state: branch merges master@0647309 (v0.1.74). Master has since advanced to 09ef208 (#396 windows-overlay-review-fixes), which touches only src/launcher.ts + tests/launcher.test.ts — zero overlap with this PR's files, and a simulated merge into current master (git merge-tree --write-tree) is conflict-free. GitHub's "mergeable" is correct; no need to re-merge master.

Pre-flight on 4fa7fcc (local):

  • typecheck clean, build success
  • full suite: 950 / 949 pass / 1 fail — the single failure is environmental, not PR-related: tests/launcher.test.ts:715 "resolveClientCommand: codex/claude resolve to themselves" fails because codex-cli 0.147.0 is installed in my sandbox, so resolution returns an absolute path instead of the bare name. It reproduces identically on clean master (09ef208) in this environment; CI runners have no codex → green there (consistent with your CI 6/6).
  • plugin-agent.test.ts solo: 27/27 pass here — the permanent pair you saw didn't reproduce in my environment (machine/timing-dependent, consistent with your "reproduces on clean master" observation on your host).
  • new endpoint test present at tests/launcher-plugin-mode.test.ts:422+ (validation 400/400/404 + full boundary lifecycle through a real proxy) and passing — nit Use kernel renderTags:"text-only" + forbid tag echoing in prompt #3 resolved ✓

E2E gate: couldn't re-run independently here — the sandbox now has the codex binary but no reachable Responses upstream. I accept your first-hand E2E PASS (real codex 0.147.0, 4-phase core, zero failures) as the gate clearance.

Your three minor notes: agree with all, non-blocking as characterized. One optional suggestion on (a): the lock-free path's safety rests entirely on the consumer stretch staying synchronous, so a one-line invariant note near applyCompactionArchive ("keep synchronous, or take withSessionLock") would guard a future refactor that inserts an await — purely optional.

One pre-existing item (report-only, same pattern flagged earlier): after the master merge, package-lock.json is still stale — lock says 0.1.71 vs package.json 0.1.74 (the v0.1.72–v0.1.74 release commits didn't sync the lockfile). Belongs in the next release commit, not this PR.

Verdict: LGTM stands, now verified against current master. Ready for human merge per AGENTS.md whenever you click.

@ranxianglei
ranxianglei merged commit 1000c12 into master Sep 3, 2026
6 checks passed
antclt pushed a commit to antclt/billion-context that referenced this pull request Sep 25, 2026
Picks up ranxianglei#422/ranxianglei#421: syncBlocks no longer crashes processTurn on
id-less host-shaped messages (0.0.90 regression from the ranxianglei#233
subid-coverage pass; every earlier release silently digested that
shape). No bili-side changes required — both lanes normalize ids on
their own paths; this keeps the pinned floor at the fixed kernel.
antclt pushed a commit to antclt/billion-context that referenced this pull request Oct 6, 2026
…anglei#2149)

Add the solver lane of ranxianglei#2149 as a model-specification (route 4) proof:

- proof/spec/*.smt2: byte-level contract of outbound-body assembly
  (String theory over bytes; impure points modeled at the interface as
  free functions/free inputs per owner stance) plus nine obligations:
  T1 growth stability (f(H+[m]) vs f(H) share a prefix of depth D past
  the merge-anchor's stable content; two scenarios: anchor flip and
  stationary) proven UNSAT in refutation form, T2 ref non-reuse under
  the persisted monotone cursor (UNSAT), and mutation twins that MUST
  be found (stable-region byte rewrite below D; post-archive cursor
  reset reissuing the dead high number 5 = the documented ranxianglei#421
  residual, model hui=3 creset=4 n3=4 n4=5).
- tests/cache-theorem.test.ts: z3 driver (PATH CLI or python module;
  solver subtests skip individually when absent so the main suite stays
  green) with per-check timing, plus grounding subtests pinning each
  spec axiom to the real code it abstracts (esc algebra vs JSON string
  encoder, mktag vs indexToRef, allocation rules vs assignRefs/
  allocateFreeRef).
- .github/workflows/ci-theorem.yml: dedicated ubuntu-only theorem lane,
  pinned z3-solver==4.9.1.0, paths-filtered to the assembly surface.

Pure test/proof surface: zero production code touched, kernel/**
untouched, no config surface added, no behavior changes.
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