Repository navigation
Conversation
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.
fix(state): preserve compression state across native compaction (compress-only guard)
Problem
Two compounding failures around native opencode compaction:
filterCompacted) parents the successful summary to the newer user message instead of the compaction marker. The parent mismatches, so all messages are retained — nothing is compacted away.resetOnCompactionfired on ANY successful compaction summary event and wiped all compression blocks. Combined with (1), the raw, uncompressed history was sent on the next request.Incident: a 1,352,175-token request against a 1,048,576-token cap →
ContextOverflowError→ retry re-compacts → wipe again → raw history again → compaction-retry deadlock.Fix
Design: compress-only by construction. The event-triggered wipe is gone; the summary event is a log-only hint; the sole state-reduction mechanism is a per-block reconciliation at transform entry that fires only when a block's target messages are actually absent from the outbound payload.
lib/state/state.ts:46-62— the compaction event handler keeps thelastCompactiontimestamp update but no longer resets state. It setslastCompactionAnomalyand logsNative compaction summary detected — state preserved(withtimestampandsummaryMessageId). The summary id comes from a newfindLastCompactionSummary(messages);findLastCompactionTimestampdelegates to it, so TUI and session-init callers are untouched.lib/state/utils.ts:336-350—resetOnCompactionis deleted (deletion prevents reuse). Nolib/reference remains.lib/state/types.ts:111— optionallastCompactionAnomaly?: { detectedAt: number; summaryMessageId: string }onSessionState, next tolastCompaction. Not added toPersistedSessionState— no state-format change, no migration.lib/messages/sync.ts:126-184— new exportedreconcileCompressionBlocks(state, logger, presentMessageIds: ReadonlySet<string>): number. WalksblocksById; drops a block iff any id in itseffectiveMessageIdsis absent from the outbound message list (all-or-nothing, so ranges straddling a compaction boundary drop; empty/missingeffectiveMessageIdskeep the block). On drop it fully cleansblocksById,activeBlockIds,activeByAnchorMessageId(only when mapped to the dropped id), and filters the id out of everybyMessageIdentry'sallBlockIds/activeBlockIds. Returns the drop count.compressMessageIdabsence is not a drop criterion — the existing deactivate behavior insyncCompressionBlocksis untouched, so reconciliation is a new function, not a sync tweak.lib/hooks.ts:128-139(V1 transform) — reconcile immediately aftercheckSessionand beforesyncCompressionBlocks, against the post-shape-filter outbound ids; dirty-save only whendropped > 0(zero drops = zero new writes, matching the current write cadence). Reconciling before sync means sync rebuilds the active maps from survivors only.lib/v2/index.ts:205-218(V2, separate call site) — reconcile only whenkind === "context", againstview.messagesids, beforesyncCompressionBlocks. Never on the"compaction"hook: its message list is the compaction input, not the outbound payload — dropping blocks there could destroy state for a summary that never lands, recreating the bug class.Monotone safety: a kept block whose targets are gone is a no-op; a wiped block whose targets are present is the incident. Real compactions self-heal block-by-block (fully-covered blocks drop, surviving ranges stay valid); orphaned summaries change nothing.
Evidence
Unit tests: main suite 132/132 pass, logger workspace 8/8 pass, typecheck clean.
Isolated runtime proof (real host opencode driving the patched plugin against a scripted OpenAI-compatible mock; unpatched
mastercontrol over the identical scenario): the anomaly WARN is logged and state is preserved; wire differential at the orphan moment: −3 messages / −12,033 B — the patched build sent the substituted fold block while the control sent the folded range raw.Incident-scale end-to-end proof (same harness, incident-sized scenario): the fold covered 223 messages. At the orphan moment the patched request was 87 msgs / 255,865 est tokens — under the 260,096 usable-token limit with the block substituted: no second compaction, no
ContextOverflowError, exit 0. The unpatched control at the identical moment sent 309 msgs / 455,643 est tokens raw and triggered a second compaction — the deadlock loop. Differential: −222 msgs / −199,778 est tokens.Incident replay: the real 1089-message incident export replayed through the reconciliation path: blocks 30 → 30, 0 drops.
Relation to existing work
Test plan
tests/block-reconcile.test.ts(8 cases): identity (0 drops); all-missing drop; straddling drop; cleanup ofblocksById/activeBlockIds/activeByAnchorMessageId/byMessageId; idempotent repeat returns 0; empty/missingeffectiveMessageIdskept; fold-in-flight (block added, then identity reconcile) survives; emptyblocksById(fork) no-op.tests/message-ids.test.ts: the reset assertion flipped to preservation — aliases intact,lastCompactionadvanced, one anomaly recorded, no re-record on a second event.No changes to
tests/v2-messages.test.ts; PR #682 semantics remain untouched.