Manual /compact policy, safe provider-visible prefix projection, boundary
selection, and single-call summarization for H sessions.
Compaction is append-only: previous entries are never mutated or deleted.
prepare/2 selects a real committed kept_from_id boundary preserving
user/tool pairing and excluding !!/hidden entries, then reuses H.Context
to project the already provider-visible prefix (including any prior compaction
summary). summarize/5 runs one H.Provider call and validates the output
before returning "compaction" entry data for H.Context.
Summary
Functions
Returns a safe cut when the projected request exceeds its scoped token budget.
Builds the single provider-neutral request map for summarizing preparation.
Prepares and executes manual compaction in one call.
Formats a compaction_result into plain JSON-compatible "compaction" entry
data consumed by H.Context.
Prepares a manual compaction plan from ordered transcript entries without
mutating any entry.
Returns true when entry is part of the provider-visible conversation
(never aborted assistant turns, system entries, or !!/excluded entries).
Serializes projected provider-visible messages into plain text for summarization. Omits thinking blocks, provider metadata, and raw image binaries, and truncates long tool results at 2000 characters.
Runs the single provider_mod.stream_turn/3 summarization call for preparation
and rejects incomplete, truncated, tool-calling, or empty responses.
Validates external compact command arguments.
Validates the daemon's scoped token budgets before accepting work.
Types
@type compaction_result() :: %{ summary: String.t(), kept_from_id: pos_integer(), instructions: String.t() | nil, usage: map() }
@type preparation() :: %{ kept_from_id: pos_integer(), previous_summary: String.t() | nil, projected_messages: [map()], kept_entries: [map()], max_tokens: pos_integer() }
Functions
Returns a safe cut when the projected request exceeds its scoped token budget.
@spec build_request(preparation(), map(), keyword()) :: map()
Builds the single provider-neutral request map for summarizing preparation.
@spec compact([map()], map(), module(), map(), keyword()) :: {:ok, compaction_result()} | {:error, :already_compacted | :nothing_to_compact | :invalid_boundary | map()}
Prepares and executes manual compaction in one call.
@spec entry_data(compaction_result()) :: map()
Formats a compaction_result into plain JSON-compatible "compaction" entry
data consumed by H.Context.
@spec prepare([map()], keyword()) :: {:ok, preparation()} | {:error, :already_compacted | :nothing_to_compact | :invalid_boundary}
Prepares a manual compaction plan from ordered transcript entries without
mutating any entry.
Options:
:keep_recent_chars- character budget kept verbatim at the tail (default 20000):kept_from_id- explicit entry ID boundary (must be a visible"user"or"assistant"entry):max_tokens- output token cap for the summarization call (default 16384)
Returns true when entry is part of the provider-visible conversation
(never aborted assistant turns, system entries, or !!/excluded entries).
Serializes projected provider-visible messages into plain text for summarization. Omits thinking blocks, provider metadata, and raw image binaries, and truncates long tool results at 2000 characters.
@spec summarize(preparation(), map(), module(), map(), keyword()) :: {:ok, compaction_result()} | {:error, map()}
Runs the single provider_mod.stream_turn/3 summarization call for preparation
and rejects incomplete, truncated, tool-calling, or empty responses.
Validates external compact command arguments.
Validates the daemon's scoped token budgets before accepting work.