Compaction
Conversation compaction shrinks older turns into a structured <compaction-summary> message when a long session approaches the model context window, while preserving recent turns verbatim and keeping critical state deterministic.
Triggers
Section titled “Triggers”Compaction runs either automatically or on demand:
- Automatic: Controlled by
[compaction]inconfig.toml(enabled = trueby default). After a turn finishes,oxchecks latest-turn context usage against the effective thresholdmin(window * auto_compact_pct / 100, window - reserve_tokens)(defaults:auto_compact_pct = 85,reserve_tokens = 16384). - Manual: Run
/compact [focus]in the TUI at any time. Optionalfocustext is appended to the summarizer input as user focus instructions.
See Configuration for the full [compaction] schema and Commands for slash command usage.
Safe Cut Point
Section titled “Safe Cut Point”Before summarizing, ox walks backward through the conversation to find a split index (to_summarize, to_keep):
- Each message is estimated at
chars / 4tokens plus256tokens per attachment. oxaccumulates at leastkeep_recent_tokens(default20000) of recent messages to keep verbatim.- The cut always snaps to a human user-turn boundary (
index > 0), soToolCall/ToolResultpairs are never split across the compaction boundary.
If the entire conversation fits within keep_recent_tokens or no earlier user-turn boundary exists, compaction stops without modifying history.
Deterministic Preservation
Section titled “Deterministic Preservation”Critical session state is reconstructed deterministically alongside the model-generated summary:
- System prompt (
Request.system): Refreshed from disk on compaction and sent outside the compacted message history, keeping system prompt sections,AGENTS.md, project memory index, andalwaysskills current. - Active on-demand skills (
<active-skills>):oxcollects skill names fromload_skilltool calls,<skill name="...">tags, and any prior<active-skills>block, reloads their bodies from disk (skippingalwaysskills), and embeds them within a16,000-character budget (MAX_PRESERVED_SKILLS_CHARS) using fair water-filling allocation. Small skills stay intact while oversized skills are truncated with\n[truncated], and skills whose allocated budget falls below64characters are recorded asload_skill("<name>")pointers. After compaction,loaded_skillsis reset soon_filesandon_toolsskills re-fire when matching activity recurs. - Tool outputs in the summarizer transcript:
ToolResultbodies passed to the built-in summarizer share a64,000-character budget (MAX_COMPACTION_TOOL_OUTPUTS_CHARS) via fair water-filling allocation, keeping small outputs intact and truncating oversized outputs with\n[truncated]. - File tracking (
<modified-files>and<read-files>): Extracted directly from tool calls in the compacted prefix (write,edit,multi_edit,patch,apply_patchfor modified files, andread,view,read_filefor read files) merged with any prior summary lists. Paths present in<modified-files>are omitted from<read-files>.
Incremental Summarization and Customization
Section titled “Incremental Summarization and Customization”Compaction inserts a single user message wrapped in <compaction-summary>...</compaction-summary> ahead of the kept turns. When a later compaction sees a leading <compaction-summary>, it switches to an incremental merge prompt so the summary updates across cycles while dropping obsolete details.
The built-in summarizer resolves its system prompt in precedence order (highest to lowest):
<cwd>/.ox/prompts/compact.md[compaction].prompt_file(relative paths resolve against<cwd>)[compaction].prompt- Built-in initial or incremental template
Custom compaction prompts support Template Placeholders.
Additional customization hooks into the same pipeline:
- Runtime focus:
/compact <focus>appends user focus instructions to the summarizer transcript. - Hooks:
PreCompactcommand hooks can supply extra compaction guidance. See Hooks. - Plugin override: A plugin implementing
compaction/compactruns before the built-in summarizer. If the plugin returns an error or an empty summary,oxlogs a warning and falls back to the built-in summarizer automatically. See Plugin Protocol.
Non-Destructive Session History
Section titled “Non-Destructive Session History”Compaction appends a boundary record to the SQLite compactions table while leaving all original rows in messages intact.
Session resume loads the latest compaction summary followed by messages after that boundary, and /rewind and /fork continue to operate cleanly across compaction boundaries. See Sessions for the SQLite schema and rewind semantics.