Repository navigation
fix(state): rebuild compression state by replaying the session log after import - #319
Merged
Merged
Conversation
…ter import pi's importFromJsonl copies only the .jsonl transcript; the <sessionFile>.acp.json sidecar never travels with an imported session, so compression state silently resets and the adapter re-compresses from scratch (full re-cache cost, context bloat back to original size). Last-resort fallback in runtime.stateFor: when the sidecar and parent-session inheritance both miss and the log contains successful compress calls, replay them in order — each call runs processTurn on the message prefix (which assigns messageRefs, mirroring the live pipeline) followed by applyCompression — then persist the recreated sidecar. Errored, no-op and unparseable calls are skipped (batches are atomic); the replay is idempotent once the sidecar exists. Closes #299
Contributor
📦 Built Extension ArtifactBranch: Option A — Install from npm PR tag (recommended)pi install npm:billion-context-pi@pr-319Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pi-pr319.tgz
pi install ./packageThis comment is automatically updated on each push. |
This was referenced Sep 7, 2026
Merged
HangxiangMa
pushed a commit
to HangxiangMa/billion-context-pi
that referenced
this pull request
Sep 21, 2026
…99-log-replay-rebuild fix(state): rebuild compression state by replaying the session log after import
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #299
Problem
pi's
importFromJsonlcopies only the.jsonltranscript. The<sessionFile>.acp.jsonsidecar — which holds the entire compression state — never travels with an imported session, so the state silently resets to empty and the adapter re-compresses from scratch: every LLM call resends the full raw history (one-time full re-cache cost + context bloat back to original size; very long sessions can approach the model window before re-compression kicks in).Parent-session inheritance (
SessionStateStore.load→tryLoadParentState) covers clones, but an imported session has no parent sidecar next to it either.Solution — adapter-side self-help (works regardless of host support)
New
src/state-rebuild.tswired intoruntime.stateFor: last-resort fallback that rebuilds the state by replaying the session log itself.Trigger: sidecar missing (or empty) and parent inheritance missed and the log contains ≥1 non-error compress toolResult.
Replay per successful call, in order:
entriesToCoreMessages(prefix)— messages the model saw when it made the call (up to and including the assistant toolCall entry, toolResult comes after — same as the live path)core.processTurn(...)— assigns refs intostate.messageRefs; applyCompression resolvesstartRef/endRefagainst that map, so the live pipeline order (context event → processTurn → compress) is mirrored exactlycore.applyCompression(...)with the recorded ranges/summaries (sanitizeSummaryapplied, like handleCompress)Then the recreated state is persisted via
store.save— subsequent loads take the normal path; the replay is idempotent.Properties:
state-rebuilt(blocks/callsApplied/callsSkipped)Tests (
tests/state-rebuild.test.ts)Full suite: 592 pass / 0 fail.
Relationship to the host-side fix
ranxianglei/pi#1 proposes the host-side fix (import copies
${basename}.*sidecars + export preservesparentSession).parentSessionexport loss is a genuine pi defect (pi defines and consumes the field but drops it on export); the sidecar copy is a contract gap — this adapter-side replay makes bcp self-sufficient either way. The two fixes are complementary, not competing.