# `H.Session.Compaction`
[🔗](https://github.com/parlant/harness/blob/v0.3.0/lib/h/session/compaction.ex#L1)

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

# `compaction_result`

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

# `preparation`

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

# `auto_boundary`

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

# `build_request`

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

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

# `compact`

```elixir
@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`

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

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

# `prepare`

```elixir
@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?`

```elixir
@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`

```elixir
@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`

```elixir
@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`

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

Validates external `compact` command arguments.

# `validate_limits!`

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
