H.Session.Compaction (H v0.3.0)

Copy Markdown View Source

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

compaction_result()

@type compaction_result() :: %{
  summary: String.t(),
  kept_from_id: pos_integer(),
  instructions: String.t() | nil,
  usage: map()
}

preparation()

@type preparation() :: %{
  kept_from_id: pos_integer(),
  previous_summary: String.t() | nil,
  projected_messages: [map()],
  kept_entries: [map()],
  max_tokens: pos_integer()
}

Functions

auto_boundary(entries, request, cwd, limits, keep_chars, adapter, config \\ %{})

Returns a safe cut when the projected request exceeds its scoped token budget.

build_request(preparation, agent, opts \\ [])

@spec build_request(preparation(), map(), keyword()) :: map()

Builds the single provider-neutral request map for summarizing preparation.

compact(entries, agent, provider_mod, provider_config \\ %{}, opts \\ [])

@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.

entry_data(result)

@spec entry_data(compaction_result()) :: map()

Formats a compaction_result into plain JSON-compatible "compaction" entry data consumed by H.Context.

prepare(entries, opts \\ [])

@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)

provider_visible_entry?(entry)

@spec provider_visible_entry?(map()) :: boolean()

Returns true when entry is part of the provider-visible conversation (never aborted assistant turns, system entries, or !!/excluded entries).

serialize_messages(messages)

@spec serialize_messages([map()]) :: String.t()

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.

summarize(preparation, agent, provider_mod, provider_config \\ %{}, opts \\ [])

@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.

validate_args(raw_args)

@spec validate_args(term()) :: {:ok, map()} | {:error, String.t()}

Validates external compact command arguments.

validate_limits!(limits)

Validates the daemon's scoped token budgets before accepting work.