Behaviour and default implementation for context compaction in Planck.Agent.
Behaviour
Use use Planck.Agent.Hooks.Compactor to implement a custom compaction strategy
in a sidecar. The only required callback is compact/2; compact_timeout/0 has a
default of 120000 ms.
defmodule MySidecar.Compactors.Builder do
use Planck.Agent.Hooks.Compactor
@impl true
def compact(_model, messages) do
summary = Message.new({:custom, :summary}, [{:text, summarise(messages)}])
kept = Enum.take(messages, -5)
{:compact, summary, kept}
end
@impl true
def compact_timeout, do: 60_000
endDispatch
Planck.Agent stores the resolved compactor module atom in state and calls
compact/4 before every LLM turn:
Hooks.Compactor.compact(state.compactor, state.model, messages, state.sidecar_node)module: nil— uses the built-in local LLM-based compactor.moduleset,sidecar_node: nil— callsmodule.compact/2in-process.moduleset,sidecar_nodeset — calls the module on the remote node via RPC; falls back to the local LLM-based compactor on:badrpc.
Summary
Callbacks
Compact the message list.
RPC call timeout in milliseconds when this compactor is invoked remotely.
Functions
Dispatch compaction for the given module, model, and messages.
Default RPC timeout used when a compactor module omits compact_timeout/0.
Callbacks
@callback compact(model :: Planck.AI.Model.t(), messages :: [Planck.Agent.Message.t()]) :: {:compact, summary :: Planck.Agent.Message.t(), kept :: [Planck.Agent.Message.t()]} | :skip
Compact the message list.
Return {:compact, summary_msg, kept} to replace older messages with a summary,
or :skip to leave the list unchanged.
@callback compact_timeout() :: pos_integer()
RPC call timeout in milliseconds when this compactor is invoked remotely.
Defaults to 120000 ms.
Functions
@spec compact( module() | nil, Planck.AI.Model.t(), [Planck.Agent.Message.t()], atom() | nil ) :: :skip | {:compact, Planck.Agent.Message.t(), [Planck.Agent.Message.t()]}
Dispatch compaction for the given module, model, and messages.
nil— runs the built-in local LLM-based compactor.- module set,
sidecar_node: nil— callsmodule.compact/2in-process. - module set,
sidecar_nodeset — calls via RPC with local fallback on failure.
Returns :skip or {:compact, summary_msg, kept}.
@spec default_compact_timeout() :: pos_integer()
Default RPC timeout used when a compactor module omits compact_timeout/0.