include/entropic/core/context_manager.h

← API

include/entropic/core/context_manager.h

entropic::ContextManager

Handles context management for the agentic loop.

entropic::ContextManager::ContextManager(CompactionManager &compaction, EngineCallbacks &callbacks, ContextManagerHooks hooks={})

Construct a context manager.

Parameters
  • compaction — Compaction manager (shared reference).
  • callbacks — Engine callbacks (shared reference).
  • hooks — Engine-level hooks.
  • compaction — Compaction manager reference.
  • callbacks — Engine callbacks reference.

void entropic::ContextManager::refresh_context_limit(LoopContext &ctx, int context_length)

Refresh context limit based on tier config.

Refresh context limit from tier config.

Parameters
  • ctx — Loop context.
  • context_length — New context length (0 = no change).
  • ctx — Loop context (unused, kept for interface consistency).
  • context_length — New max tokens (0 = no change).

std::pair< int, int > entropic::ContextManager::prune_tool_results(LoopContext &ctx, int keep_recent)

Replace old tool results with stubs.

Parameters
  • ctx — Loop context.
  • keep_recent — Number of recent results to keep.
Returns
(pruned_count, freed_chars).

void entropic::ContextManager::prune_old_tool_results(LoopContext &ctx)

Auto-prune tool results older than TTL iterations.

Issue #6 (v2.1.3): pre-2.1.3 this fired on every iteration regardless of context fill. A 25-minute session against a 32K-token context window observed 78 prune events even though peak fill was 29% — dropping evidence that wasn't crowding anything out and forcing the tier to re-issue duplicate tool calls for data it already had (16 Duplicate tool call warnings in the affected session). Now gated on context fill: below warning_threshold_percent (the same threshold inject_context_warning uses) prune is a no-op. At/above the threshold, prune by TTL as before. Companion fix to issue #5 (validator runs against post-prune context) — fixing this issue eliminates the bug class entirely whenever context has headroom.

Parameters
  • ctx — Loop context.

void entropic::ContextManager::inject_context_warning(LoopContext &ctx)

Inject context usage warning if over threshold.

Parameters
  • ctx — Loop context.

void entropic::ContextManager::check_compaction(LoopContext &ctx, bool force=false)

Check and perform compaction if needed.

Parameters
  • ctx — Loop context.
  • force — Bypass threshold check.

bool entropic::ContextManager::fire_pre_compact_hook(LoopContext &ctx, bool force)

Fire ON_PRE_COMPACT; report whether compaction was cancelled.

Extracted from check_compaction to keep it knots-clean.

Parameters
  • ctx — Loop context.
  • force — Whether this is a forced compaction.
Returns
true if a hook cancelled compaction.

void entropic::ContextManager::fire_post_compact_hooks(LoopContext &ctx, int old_count, int new_count)

Fire post-compaction callbacks + ON_POST_COMPACT hook.

Extracted from check_compaction to keep it knots-clean.

Parameters
  • ctx — Loop context.
  • old_count — Token count before compaction.
  • new_count — Token count after compaction.

void entropic::ContextManager::set_hooks(const HookInterface &hooks)

Set the hook dispatch interface.

Parameters
  • hooks — Hook dispatch interface.

entropic::ContextManagerHooks

Engine-level hooks called during context management.

std::function<void(LoopContext&)> entropic::ContextManagerHooks::after_compaction

Post-compaction hook.