Dashboard › opencode › Distillation
c373b5d2-1844-4934-a007-339b23f77059["lore_tm_v1_sp6pcORh7vFMPVEZ-K1KkQgFhfdVzqukL0Byyk5w84A","lore_tm_v1_vx7wSnqZLeA13dkSJIpSGRaqf3KzhCVYEE0oDFqL51U","lore_tm_v1_sz0CVt0uFP7TMfoF5hQDUcYZnASxSG29CW4IEG_OMsE","lore_tm_v1_qxyufv_dGEpCZvk0u_n2x1GO9-KHtiqHOWhuXvyPHQw"]
Date: Sep 16, 2026
packages/core/src/session/compaction.ts configures automatic compaction defaults: DEFAULT_BUFFER = 20_000, DEFAULT_KEEP_TOKENS = 8_000, TOOL_OUTPUT_MAX_CHARS = 2_000, and SUMMARY_OUTPUT_TOKENS = 4_096; config documents can override compaction.auto, compaction.buffer, and compaction.keep.tokens.packages/core/src/session/compaction.ts summary output must preserve the ordered Markdown sections ## Objective, ## Important Details, ## Work State (### Completed, ### Active, ### Blocked), ## Next Move, and ## Relevant Files; instructions require terse bullets and exact preservation of paths, symbols, commands, errors, URLs, and identifiers.SessionCompaction.buildPrompt() supports initial summaries and updates. Update prompts combine <prior-summary> with the newer <conversation>, treat the newer conversation as authoritative on conflicts, carry forward unfinished objectives/constraints/directives/parallel workstreams, move finished work to Completed, and refresh Objective and Next Move.packages/core/src/session/compaction.ts labels user, assistant, assistant-reasoning, tool-call, tool-result, tool-error, system, synthetic, and shell content distinctly. Tool outputs and shell output are truncated after 2_000 characters with \n[truncated]; attachments serialize as [Attached <mime>: <name>] or equivalent URI/mime forms.SessionCompaction.compactIfNeeded() triggers only when automatic compaction is enabled, the model has a positive context limit, and estimated tokens for system, messages, and tools exceed context - Math.max(output, config.buffer). It delegates overflow handling to SessionCompaction.compactAfterOverflow().SessionCompaction.compactAfterOverflow() retains a recent tail up to the configured token target, summarizes the older head, and can incorporate the previous compaction’s summary and recent context. It publishes SessionEvent.Compaction.Started before streaming and SessionEvent.Compaction.Ended only after a nonempty successful summary; provider errors, LLM.Error, empty output, impossible prompt sizing, or absent compactable history return false.input.model and input.request.http, disable tools with tools: [], and cap generation at Math.min(configured output || 4_096, 4_096) tokens. beforeStream runs immediately before the LLM stream; eventCommit can fence the started event, while commit is attached to the ended event.packages/core/src/session/context-epoch.ts defines SessionContextEpoch.initialize() and SessionContextEpoch.prepare(), both requiring a SessionExecutionClaim.Claim. Prepared context consists of exact fields baseline: string and baselineSeq: number.SessionContextEpoch.prepare() concurrently obtains current SystemContext, the stored SessionContextEpochTable row, and SessionHistory.latestCompaction(). With no stored epoch it calls SystemContext.initialize(), inserts the generated baseline/snapshot, and anchors baseline_seq to EventV2.latestSequence().Schema.decodeUnknownEffect(SystemContext.Snapshot); decode failures become ContextSnapshotDecodeError({ sessionID, details: String(error) }). A compaction newer than stored.baseline_seq selects SystemContext.replace(); otherwise preparation uses SystemContext.reconcile().packages/core/src/session/context-epoch.ts: Unchanged and ReplacementBlocked retain the stored baseline and sequence; ReplacementReady replaces baseline/snapshot and uses the compaction sequence or current EventV2.latestSequence(); an ordinary update publishes SessionEvent.ContextUpdated and advances the stored snapshot through the event commit callback.insert, replace, and advance database mutations are fenced through SessionExecutionClaim.transaction(). replace and advance die with exact error "Context Epoch not found" if no epoch row is updated; SessionContextEpoch.reset() deletes the session’s epoch row.