Auto-compact trigger and resume flow
Overview
Auto-compact fires inside the main turn loop when token usage approaches the context ceiling, forks a summary request, and replaces history with a compact boundary marker so the next turn resumes from the summary .
Steps
- Each turn iteration of the main loop projects the history window and tracks token usage via
autoCompactTracking, running snip and microcompact before evaluating whether a full compact is needed . - When threshold logic decides to compact (or the user invokes
/compact), the flow enterscompactConversation, which first counts pre-compact tokens and runs PreCompact hooks withtrigger: 'auto'. - A dedicated summary user message is built from
getCompactPrompt(customInstructions)and dispatched viastreamCompactSummary, which optionally forks an agent that reuses the main conversation's prompt cache prefix to keep the summary query cheap . - If the summary request itself returns
PROMPT_TOO_LONG, the loop truncates the oldest API-round groups withtruncateHeadForPTLRetryand retries up toMAX_PTL_RETRIESbefore giving up . - On success, the pre-compact read-file cache is snapshotted,
context.readFileStateand nested memory paths are cleared, and post-compact attachments (files, agent, plan mode, skills, deferred-tools delta, MCP instructions delta) are rebuilt so the next turn has fresh context . - The function returns a
CompactionResultwhoseboundaryMarkeris a SystemMessage plussummaryMessages— this is the compact boundary the next turn resumes from . - Back in the loop,
getMessagesAfterCompactBoundary(messages)is called at the top of each iteration, so subsequent turns read only messages after the boundary marker, effectively resuming from the summary . - For the
/compactcommand path,callincommands/compact/compact.tsfirst triestrySessionMemoryCompaction(session-memory route), and only falls through tocompactConversationwhen session memory is absent or empty . - The session-memory fast path checks
shouldUseSessionMemoryCompaction, waits for in-progress extraction, reads the stored session memory file, and returns its ownCompactionResultif content is present, otherwise yieldsnullto let the legacy path run . - Partial compaction (
/compactwith a pivot) usespartialCompactConversation, which slices messages around the pivot, strips stale compact boundaries in the'up_to'direction, and runs the same PTL-retry + attachment-rebuild logic .
State touched
Decisions
No design decisions are present in the whitelist for this flow .
Note: the whitelist contains zero decision: tokens, so the *why* behind threshold choice, cache-prefix sharing, and PTL-retry count cannot be cited here .