Context Garbage Collection System

JAATO implements a pluggable context garbage collection system that prevents context window overflow during long-running agentic sessions. Four strategy plugins share a common GCPlugin protocol. The most advanced strategy, gc_budget, uses the InstructionBudget for policy-aware removal across a five-tier priority system and supports continuous collection mode for predictable context trimming.

GC System Infography
Click to open full-size image in a new tab

Four GC Strategies

StrategyApproachContent DiscriminationBest For
gc_truncateRemove oldest turns entirelyNone — all turns equalSimple sessions, minimal overhead
gc_summarizeCompress old turns into summaryNone — all turns equalLong sessions needing context continuity
gc_hybridGenerational: truncate ancient, summarize middle, preserve recentAge-based tiersBalanced approach
gc_budgetPolicy-aware: remove by GC policy tierPolicy-based tiers (5 levels)Enterprise, long-running agents

GC Policy Tiers

The InstructionBudget assigns a GCPolicy to every tracked instruction source. These policies determine removal priority during budget-aware garbage collection:

PolicyIndicatorRemoval BehaviorExamples
LOCKED🔒Never removedSystem instructions, user's original request, core tool schemas
PRESERVABLEOnly under extreme pressureClarification Q&A, turn summaries, GC summaries
PARTIALContainer with mixed childrenCONVERSATION source, PLUGIN source
EPHEMERALFirst candidates for removalEnrichment data, discoverable tool schemas, verbose output

Five-Phase Removal (gc_budget)

The gc_budget plugin executes removal in strict priority order, stopping as soon as enough tokens are freed:

  1. Phase 1a: ENRICHMENT — Bulk clear entire enrichment source (regenerated every turn)
  2. Phase 1b: EPHEMERAL — Remove other ephemeral entries, oldest first
  3. Phase 2: PARTIAL turns — Remove old conversation turns, respecting preserve_recent_turns and pinned indices
  4. Phase 3: PRESERVABLE — Only if usage exceeds pressure_percent; never in continuous mode
  5. LOCKED — Never removed under any circumstances

Continuous vs Threshold Mode

AspectThreshold Mode (default)Continuous Mode
TriggerUsage ≥ threshold_percent (80%)Usage > target_percent (60%) after every turn
Enablepressure_percent > 0pressure_percent = 0 or None
Collection sizeLarge, infrequentSmall, every turn
PRESERVABLETouched under extreme pressureNever touched
PatternSawtooth (80% → 60% → 80%)Gentle ripple around target

Session Integration

GC integrates at three points during each turn: a pre-send check before the message is sent, streaming threshold monitoring during token generation, and post-turn collection if the threshold was crossed. After collection, the session synchronizes the InstructionBudget using the GCRemovalItem list returned by the plugin, then emits a budget update event for the UI.

Coherence Preservation

  • preserve_recent_turns — Last N turns are never removed (default: 5)
  • pinned_turn_indices — Specific turns that are never removed
  • LOCKED policy — User's original request and system instructions are never collected
  • Summary chain — GC summaries are marked PRESERVABLE and bridge removed history to current context
  • Turn boundaries — GC operates on complete turns, never splitting a turn partially
Back to Enterprise Overview