Skip to content

fix(state): rebuild compression state by replaying the session log after import - #319

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-07_issue299-log-replay-rebuild
Sep 7, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-07_issue299-log-replay-rebuild

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #299

Problem

pi's importFromJsonl copies only the .jsonl transcript. The <sessionFile>.acp.json sidecar — 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.ts wired into runtime.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:

  1. 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)
  2. core.processTurn(...) — assigns refs into state.messageRefs; applyCompression resolves startRef/endRef against that map, so the live pipeline order (context event → processTurn → compress) is mirrored exactly
  3. core.applyCompression(...) with the recorded ranges/summaries (sanitizeSummary applied, like handleCompress)

Then the recreated state is persisted via store.save — subsequent loads take the normal path; the replay is idempotent.

Properties:

  • Errored / no-op / unparseable calls skipped — applyCompression batches are atomic, a rejected call leaves state untouched
  • Deterministic — same log ⇒ same state; summaries come back exactly as stored in the transcript
  • No new config — pure fallback, zero behavior change when a sidecar exists
  • Logged as state-rebuilt (blocks / callsApplied / callsSkipped)

Tests (tests/state-rebuild.test.ts)

  1. Import scenario: sidecar absent → replay rebuilds 1 block (summary / effectiveMessageIds / active / compressedTokens), sidecar rewritten, duplicate compress of the same range rejected, second fire does not re-apply
  2. Errored compress call in history → no blocks created
  3. Two sequential successful calls → two blocks

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 preserves parentSession). parentSession export 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.

…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
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

📦 Built Extension Artifact

Branch: 2026-09-07_issue299-log-replay-rebuild (5d65384)

Option A — Install from npm PR tag (recommended)

pi install npm:billion-context-pi@pr-319

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pi-pr319.tgz
pi install ./package

This comment is automatically updated on each push.

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.

会话迁移(export/import)丢失 .acp.json 状态与 parentSession,导入后会话失去全部压缩块

1 participant