Skip to content

Context Compaction (/compact & /auto-compact) ​

Long coding sessions accumulate large tool outputs, file reads, test logs, and multi-turn exploration. Kompile manages context window pressure through model-aware automatic compaction (/auto-compact), on-demand manual compaction (/compact), explicit reviewed scope retirement (/compact reviewed), and post-compaction working-set reload so your memories, skills, and project rules survive every summary boundary.


How compaction works (CompactionService) ​

Kompile's compaction budget is model-aware: on every turn, the chat loop refreshes maxTokens from the active model's real context window (dynamic provider catalog, static catalog, or local model probe), so compaction behaves appropriately on both a 4K local GGUF and a 200K+ frontier model.

When context crosses the configured threshold:

  1. Estimate active conversation tokens against the model's real context window minus output/compaction reserve.
  2. Prune and summarize older turns and tool outputs into a compact checkpoint summary while preserving system instructions, user intent, and a recent tail window intact (proportional to the model, capped at 40,000 tokens).
  3. Re-deliver the session's working set (CompactionWorkingSet) — memory files the session read/recalled/saved and expanded <skill> bodies — so the model never forgets project rules or loaded skills right after a summary.

TIP

Check live context pressure anytime: Run /status context to inspect the live context ledger, or /auto-compact status to see your active threshold, window budget, and output reserve.


Manual compaction (/compact) ​

Run /compact whenever a task phase finishes (for example, after finishing a deep investigation before starting edits) or before context gets crowded:

text
/compact                                                   # Summarize older history using default focus
/compact keep the auth refactor plan and failing test IDs  # Steer what the summary preserves

Execution safety ​

  • Runs on a maintenance turn thread: /compact dispatches through the session's turn lifecycle with a live in-place CompactionProgressIndicator so it stays cancellable and never deadlocks the terminal input row.
  • Never interrupts mid-tool calls: If a turn or tool call is actively running, /compact waits until the turn finishes so provider function-call / tool-result linkage cannot be broken.
  • Portable history accounting: On completion, Kompile logs the before/after portable token estimate and preserved turn count (for server-mode sessions that don't run local compaction, use /clear instead).

Reviewed scope retirement (/compact reviewed) ​

For direct client-owned sessions that support structured tool replay and expose fetch_result, /compact reviewed provides a two-step explicit retirement flow instead of a one-shot automatic summary:

text
/compact reviewed prepare            # Generates a digest-bound retirement candidate + fetch_result handle
/compact reviewed <approval-json>    # Applies the reviewed retirement strictly against that exact digest

Safeguards enforced by AgenticChatLoop.reviewedCompact:

  • Refuses to run while owned background processes/tasks are still active.
  • Requires fetch_result to be exposed so retired exact tool outputs remain recoverable by reference handle.
  • Binds the approval to the exact conversation snapshot digest (if any turn or setting changes between prepare and apply, Kompile rejects the stale plan and asks you to prepare again).

Automatic compaction (/auto-compact) ​

Auto-compaction is enabled by default (triggerRatio = 0.85, default output reserve 8,192 tokens) in standard local chat mode. Inspect or tune it live with /auto-compact (changes persist to your loaded or global chat-config.json):

text
/auto-compact                      # Show current auto-compaction status & effective token budgets
/auto-compact status               # Same as bare /auto-compact
/auto-compact on                   # Enable automatic compaction
/auto-compact off                  # Disable automatic compaction
/auto-compact threshold 80%        # Trigger between 50% and 95% (accepts 80%, 80, or 0.8)
/auto-compact context 128000       # Override effective context window (or 'auto' for model default)
/auto-compact output 16384         # Override max output reserve (or 'auto')
/auto-compact reserve 12000        # Override explicit compaction reserve tokens (or 'auto')

Provider-managed compaction (Claude Code, OpenCode, Antigravity agy) ​

When you run a provider-owned CLI route (such as Claude Code native mode, OpenCode, or Antigravity agy), that vendor CLI owns its own compaction trigger. Running /auto-compact status tells you when the session is provider-managed (for example, Auto-compaction: Claude Code managed) so you know Kompile's local threshold knobs do not override claude -p.

Even when the vendor CLI runs compaction itself, Kompile still protects your rules and loaded knowledge via post-compaction working-set reload below.


How Kompile prevents post-compaction amnesia ​

Ordinary compaction summarizes away tool results — which normally erases memory files read earlier in the session and expanded skill definitions. Kompile closes that gap in three places:

  1. Working-set reload (CompactionWorkingSet): Tracks up to 32 memory files the session read, recalled, or saved plus expanded <skill> blocks. When compaction fires, Kompile re-reads those memory files fresh from disk (so any edits made during the session win) and injects a <kompile_compaction_reload> block into the compacted checkpoint.
  2. Native CLI compaction reload (NativeCompactionReload):
    • Claude Code: Uses Kompile's post-compaction hook (ClaudeNativeContextReloadPolicy).
    • OpenCode & Antigravity (agy): Tracks outbound skills and Kompile memory tool calls, mirrors the reload block to KOMPILE_COMPACTION_RELOAD_FILE (for OpenCode's experimental.session.compacting plugin hook), and prepends the reload block on the next prompt after provider compaction is detected (enabled by default; disable with -Dkompile.chat.compactionReload=false).
  3. Compaction-proof durability (progress, memory, todowrite):
    • progress reports (/progress-reports) live in a per-transcript JSONL log outside the prompt window, so milestone and subagent summaries survive compaction intact.
    • memory files live on disk and stay indexed in MEMORY.md on every turn.
    • Transcripts on disk are append-only ledgers — compaction only trims what is sent to the LLM, never erasing your saved conversation history or /policy-enforce audit trail.

Target-aware compaction on cross-vendor /resume ​

When you resume a long conversation on a different vendor or a model with a smaller context window (CrossAgentResumeCompactor), Kompile automatically performs target-aware compaction so the resumed transcript fits cleanly inside the destination model's budget without losing key findings or file state. See Import & Resume.


Released under the Apache-2.0 License.