Compaction in Prime Agent is a pure-function module that summarizes old conversation history when context tokens approach the window limit; SessionManager handles all I/O and reloads the session after the compaction logic completes. The compaction system estimates context tokens from actual usage data and previous messages, determines valid cut points (user/assistant/custom/execution/summary messages—never tool results), and produces a CompactionResult carrying the summary and tracking metadata without session identifiers. An active goal (a long-running autonomous objective) is persisted independently across the compaction boundary in agent-session.ts, preventing daemon sessions from silently dropping the goal when context is trimmed. After compaction completes and session context is restored, agent-session.ts automatically requeues any continuation prompt that was interrupted by compaction, resuming in-flight work. When a post-compaction continuation fails to start (e.g., due to a context error or resource exhaustion), agent-session.ts propagates a rejection to all registered idle waiters rather than leaving them hanging indefinitely.
The compaction module at packages/coding-agent/src/core/compaction/compaction.ts contains pure functions only; the SessionManager is responsible for all I/O, and the session is reloaded after compaction completes.[1] The COMPACT_SKILL_NAME constant is "compact", identifying the compaction skill by name.[1]
shouldCompact triggers compaction when contextTokens > contextWindow - settings.reserveTokens, and returns false if compaction is disabled or contextWindow <= 0.[1]
estimateContextTokens anchors its estimate on the last assistant message's actual usage data and adds a chars/4 estimate only for messages after that anchor point; without any usage data, it estimates all messages with chars/4.[1] getAssistantUsage skips messages with a stopReason of "aborted" or "error" because those messages do not carry valid usage data.[1]
Valid compaction cut points are user, assistant, custom, bashExecution, branchSummary, and compactionSummary messages; toolResult messages are never valid cut points because they must immediately follow their tool call.[1] getMessageFromEntryForCompaction deliberately excludes compaction entries (returning undefined for them), unlike getMessageFromEntry which converts them to summary messages — this prevents prior compaction summaries from being re-summarized.[1]
The CompactionResult interface carries summary, firstKeptEntryId, tokensBefore, and an optional details field for extension-specific data; uuid and parentUuid are not included — the SessionManager adds those fields when persisting the result.[1] File operations accumulated in a CompactionDetails from a previous pi-generated compaction entry are carried forward into the next compaction's file-op tracking; hook-generated compaction entries are excluded from this carry-forward (checked via !prevCompaction.fromHook).[1]
agent-session-compaction-continuation.test.ts is the canonical regression guard for compaction continuity, covering both generic continuation-state and goal-specific state persistence across the compaction boundary.
Sources