Plugin

Context Engine

IN THIS DOCUMENT

Crimson marks the heading currently in view; every entry is one of the document's own headings.

The context engine is the dylib session’s middle-turn archiver: when the conversation index fills up, it packs the middle turns into one compact CCR entry and replaces them with a single marker, so the model sees a short history (head + marker + tail) instead of a growing list of archived turns.

The archive is in-place but recoverable: the middle turns are removed from Aphrodite’s own conversation index and stored (BLAKE3-keyed) in the inline/CCR store, where aphrodite_retrieve can restore them.

The engine never touches Hermes’ message store - Hermes holds the raw conversation; the engine compacts Aphrodite’s archived-turn index only.

Activation

The plugin manifest declares the capability:

# plugin.yaml
provides_context_engine: true

The plugin registers the engine instance only when explicitly requested:

APHRODITE_CONTEXT_ENGINE=1

Without the flag the plugin registers hooks and tools only and runs the hook + proxy path, and the dylib session never consults the engine.

If registration fails (for example the host does not expose the ContextEngine base class), the plugin logs a warning and continues on the hook + proxy path - registration is best-effort, never fatal.

Hermes selects the registered engine by name; the plugin registers under the name aphrodite.

What the engine does

The engine runs inside the dylib session, triggered by the pre_llm_call context assembler each turn.

All gates run before any mutation; when any gate fails the engine is a no-op and the session is byte-identical to an engine-less run.

GateCondition for archiving
Engine enabledcontext_engine_enabled is true ([compression] context_engine, env APHRODITE_CONTEXT_ENGINE)
Thresholdengine_threshold_pct is neither 0 (never) nor >= 100 (off escape hatch)
Minimum turnsconv_index length is at least engine_min_msgs (default 8)
Fill ratiototal * 100 / CONV_INDEX_CAP (CONV_INDEX_CAP = 50) is at or above engine_threshold_pct
Middle sizetotal - engine_protect_first - engine_protect_last is at least 3

When all gates pass:

  1. The middle slice - keeping exactly engine_protect_first turns at the start and engine_protect_last at the end, oldest first - is packed into one compact JSON payload with one entry per turn ({"turn", "hash", "summary", "size"}).
  2. The payload is hashed with BLAKE3 (headroom_core::ccr::compute_key), stored in the inline/CCR store, and summarized as engine offload: N middle turns archived (keep H/T).
  3. The middle turns are removed from the conversation index and replaced by a single marker entry, so the model sees head + marker + tail.
  4. The engine returns the marker plus the rendered engine_offload prompt (template vars {hash} and {tail}), which the assembler injects as the [engine-offload: ...] section - it is never dropped under budget pressure, because the archive already mutated the index.

The marker is a normal CCR entry of type context: its size is the retrieval cost, and aphrodite_retrieve returns the packed JSON of the archived turns.

Engine configuration

Settings resolve env > TOML > compiled default in the dylib session. The standalone HTTP proxy never reads them.

SettingEnv VarTOML keyCompiled defaultShipped example
Engine enabledAPHRODITE_CONTEXT_ENGINE[compression] context_enginetruetrue
Threshold percentAPHRODITE_ENGINE_THRESHOLD_PCTengine_threshold_pct4530
Protect first NAPHRODITE_ENGINE_PROTECT_FIRSTengine_protect_first22
Protect last NAPHRODITE_ENGINE_PROTECT_LASTengine_protect_last55
Min messagesAPHRODITE_ENGINE_MIN_MSGSengine_min_msgs88

Threshold semantics: 0 = never archive, >= 100 = engine off (escape hatch), anything between = the fill percentage of the conversation index (total * 100 / 50) at or above which archiving triggers.

Environment overrides TOML, which overrides the default.

See also