include/entropic/core/compaction.h

← API

include/entropic/core/compaction.h

entropic::CompactionManager

Manages automatic context compaction.

entropic::CompactionManager::CompactionManager(const CompactionConfig &config, TokenCounter &counter)

Construct a compaction manager.

Parameters
  • config — Compaction config (copied by value).
  • counter — Token counter instance (shared reference).
  • config — Compaction configuration.
  • counter — Token counter (shared reference).

CompactionResult entropic::CompactionManager::check_and_compact(std::vector< Message > &messages, bool force=false, const std::string &conversation_id="")

Check if compaction is needed and perform if so.

Check and perform compaction if over threshold.

Parameters
  • messages — Current message list (modified in place if compacted).
  • force — Bypass threshold check and compact immediately.
  • conversation_id — Conversation ID for snapshot (empty = skip).
  • messages — Message list (modified in place if compacted).
  • force — Bypass threshold.
Returns
  • Compaction result.
  • CompactionResult with before/after stats.

CompactionResult entropic::CompactionManager::compact_messages(const std::vector< Message > &messages)

Compact messages using the value-density strategy.

Compact messages using value-density strategy. Public entry point for the default compaction algorithm. Used by CompactorRegistry to wrap the built-in strategy as a compactor. Does NOT check thresholds — callers are responsible for that. Public wrapper around the private compact() method. Returns a fully-populated CompactionResult including the compacted message list. Does not check thresholds.

Parameters
  • messages — Messages to compact.
Returns
CompactionResult with compacted messages and metadata.

void entropic::CompactionManager::set_storage(const struct StorageInterface *storage)

Set storage interface for compaction snapshots.

Parameters
  • storage — Storage callbacks (nullable).

CompactionConfig entropic::CompactionManager::config

Compaction configuration.

TokenCounter& entropic::CompactionManager::counter

Shared token counter.

entropic::CompactionResult

Result of a compaction operation.

bool entropic::CompactionResult::compacted

Whether compaction occurred.

int entropic::CompactionResult::old_token_count

Token count before compaction.

int entropic::CompactionResult::new_token_count

Token count after compaction.

std::string entropic::CompactionResult::summary

Generated summary text.

int entropic::CompactionResult::preserved_messages

Messages kept after compaction.

int entropic::CompactionResult::messages_summarized

Messages stripped into summary.

std::vector<Message> entropic::CompactionResult::messages

The compacted message list (v1.9.9)

std::string entropic::CompactionResult::identity

Identity that triggered compaction.

std::string entropic::CompactionResult::compactor_source

"default", "global_custom", or identity name

bool entropic::CompactionResult::custom_compactor_used

true if a consumer-provided compactor ran

entropic::TokenCounter

Track token usage across conversation.

entropic::TokenCounter::TokenCounter(int max_tokens)

Construct a token counter.

Parameters
  • max_tokens — Maximum context window size.

int entropic::TokenCounter::count_message(const Message &msg) const

Count tokens in a single message.

gh#158 (v2.13.0): pure, and unsynchronized ON PURPOSE because there is nothing left to synchronize. The address-keyed memo this used to write from a const method is gone — see TokenCounter::clear_cache for both reasons it had to be.

Parameters
  • msg — Message to count.
Returns
  • Estimated token count.
  • Estimated token count (content + role overhead).

int entropic::TokenCounter::count_messages(const std::vector< Message > &messages) const

Count total tokens in a message list.

Parameters
  • messages — Messages to count.
Returns
Total estimated token count.

float entropic::TokenCounter::usage_percent(const std::vector< Message > &messages) const

Get usage as fraction of context window (0.0–1.0).

Get usage as fraction of context window.

Parameters
  • messages — Messages to measure.
Returns
  • Usage fraction.
  • Usage fraction (0.0 if max_tokens is 0).

void entropic::TokenCounter::clear_cache()

Clear the token count cache — retained as a no-op (gh#158).

Clear the token count cache — no-op since gh#158 (v2.13.0). v1.8.4–v2.12.x memoized per-message counts in an unordered_map keyed by the ADDRESS of the Message. Two defects, and the second is what removed it. (1) A Message address is not an identity: the vector holding it reallocates on append and the allocator reuses freed slots, so a hit could answer for a message that no longer exists — a silent wrong count, never a crash. (2) The map was written from count_message() const with NO lock, so two concurrent runs (gh#158) or one run plus an entropic_context_usage call would insert into it simultaneously — heap corruption, not a wrong number. The memo was never worth defending: count_text is one size() and one division, strictly cheaper than hashing a pointer and walking a bucket. So the cache is gone and counting is a pure function. The method stays because four call sites in CompactionManager and ContextManager invalidate around a compaction, and a no-op keeps that intent legible rather than deleting the calls and leaving a future reader to wonder whether invalidation was forgotten. Kept so the four invalidation call sites still read as "the counts these messages had are no longer valid". There is no cache to clear; counting is a pure function of the message content. See the header for why.

int entropic::TokenCounter::max_tokens

Maximum context window size.