Compaction
Compaction replaces the conversation with a summary when it grows too large.
The agent keeps working from that summary. Implementation:
crates/core/src/compaction.rs, with the triggers in crates/core/src/llm.rs
and the response handlers in crates/core/src/handlers.rs.
When it runs
Compaction starts in one of four ways:
- Turn boundary. A turn ends with the prompt at or above the threshold
(100,000 tokens by default; the experiment-only
compact_thresholdlaunch key changes it, with a floor of 8,000). - Hard cap. A request is about to go out at twice the threshold in the
middle of a turn (at the threshold itself when
compact_thresholdis set). The request is not sent. A pending user message is replayed after the summary lands. - Operator.
/compact [focus]compacts now, with an optional focus for the summary. - Agent. The model calls the
compacttool (below).
The summary comes from the compaction model (compact_model, which defaults to
the main model). A verification pass then has the main model revise it into a
handoff briefing. If the compaction model cannot be reached, the main model
writes the summary instead. The local stub, which keeps only the last request
and the file list, is used only when no model can be reached. Below the hard
cap, a failed compaction keeps the transcript and is retried at the next turn
boundary.
Each applied compaction prints [INFO] Compaction applied: messages_before=<n>
on stderr, whichever trigger or rung produced it. The eval runner counts these
lines. The transcript and summary from before the compaction are saved as
artifacts/compaction-<n>.jsonl and .txt in the session directory.
Plan state and abandoned tool processes are harness state, not transcript, so
they survive compaction. The context packet carries them into the next
request. By default the plan otherwise lives in the latest update_plan_state
result; compaction removes that result, so the next packet carries the plan
until the agent updates it again.
The compact tool
{
"type": "object",
"properties": {
"focus": {
"type": "string",
"description": "Optional: what the summary should prioritize"
}
}
}
compact is an in-process builtin (__COMPACT__). Its execution policy is
InProcess, and it never runs a shell command.
The tool does not compact on the spot. Its call is part of an assistant turn
whose other calls may still be running, and their results must pair with that
turn. So the tool records the request and answers its call at once. The rest of
the batch then runs. When the last result is in and the harness is about to
send the results to the model, it compacts instead. The whole turn goes into
the summary, including the compact call and its result, so no tool_use is
left without its tool_result. After the summary is applied, the same turn
continues. The next request carries the summary and then a harness note saying
that the compaction the agent asked for has finished. If user input cut the
tool loop short, that input is replayed instead of the note, as the hard cap
replays it.
The tool is refused, with an error result and nothing recorded, when:
- compaction is disabled (
compact.enabled, which no launch setting turns off today); - a compaction is already requested in this turn or running;
- the prompt is below 8,000 tokens (
MIN_SELF_COMPACT_TOKENS). A freshly compacted prompt is just under 8,000 tokens, so compacting a smaller prompt would not make room.
If the requested compaction fails below the hard cap, the transcript is kept.
The turn continues with its results and a note giving the reason. An interrupt
drops a requested compaction that has not started yet. An agent-requested
compaction also prints [INFO] Compaction requested by the agent on stderr.
Idle resume note
A provider’s prompt cache expires while the agent is idle. The first request after a long idle period rebuilds the cache whatever it contains. That makes it a good moment to point the agent back at its goals, and the cheapest moment to compact.
The note is added on the first wake after PROMPT_CACHE_TTL: one hour since
the last main-model request. A wake is any input that starts a turn, such as a
user or bridge message, a judge reprompt or the idle nudge. The hour is
deliberately long. Anthropic’s ephemeral cache lasts five minutes and its
extended option one hour. OpenAI’s in-memory cache keeps a prefix for five to
ten minutes, up to an hour. Extended 24-hour retention is not covered.
What the note says:
- With a recorded plan: the objective, open tasks, blockers and next step,
and a request to check them against what is left and update the plan with
update_plan_stateif it is stale. When the wake is the harness’s own idle nudge, this text replaces the nudge rather than joining it. - Without a plan: a user or bridge message gets no goals note, because the message is the goal. The idle nudge stays unchanged.
- With a large context: a closing line says the agent can call
compactnow at no extra cache cost. This line is added in both cases above. A large context means at least half of the threshold, and never below 8,000 tokens. It needs thecompacttool and compaction enabled.
The note is a suggestion. Nothing on this path compacts. It is given at most once per idle period, because each request the agent sends starts a new period.
The note is stored in the transcript with the role note
(specs/transcript.cddl) and logged where it was added. Requests carry it as
user content at that place, and a resumed session sends it at the same place.
Code that looks for what the user said matches the role user, so a note is
never taken for the user’s words. The continuation after an agent-requested
compaction uses the same role.