Expand description
Making a long conversation fit.
Every turn sends the whole history, so a session that runs long enough stops being able to send anything at all. Compaction replaces the middle of the transcript with a summary and keeps the ends: the task at the top, so the agent still knows what it was asked, and the most recent turns verbatim, because that is where the work actually is.
§The constraint that decides the design
A tool_result is only valid if its tool_use is still in the conversation
— the next request 400s otherwise, and that is the whole run gone. So the
cut cannot land anywhere convenient; it has to land somewhere legal. The
transcript alternates user and assistant, and tool results arrive in the user
message immediately after the assistant turn that asked for them, so the only
safe place to resume is at an assistant message. Cutting there drops each
tool_use together with the results answering it.
The logic here is deliberately pure and provider-free. Getting the boundary wrong produces a 400 from a real API twenty turns into a real session, which is the worst possible place to discover it.
Enums§
- Summary
Verdict - What the validator said about a summary.
Constants§
- CARRIED_
HEADER - Marks the block holding tool state carried across a compaction.
- SUMMARY_
INSTRUCTION - The prompt handed to the summariser.
- SUMMARY_
SYSTEM - What the summariser is told it is.
- SUPERSEDED_
MARKER - Starts every evicted result, so a second pass can tell it has already been here — and so the model can tell a stale result from a short one.
- THINNED_
RESULT_ CHARS - How much of a tool result survives thinning.
- TRUNCATION_
MARKER - Appended to a result whose middle was removed, so the model can tell the difference between a short file and a shortened one.
- VALIDATE_
SYSTEM - What the summary validator is told it is. Like the summariser, a separate persona: it reads two texts, it does not act on either.
Functions§
- cut_
point - The first index at or after
targetwhere the transcript can be cut. - evict_
superseded_ results - Replace tool results that a later call has superseded.
- orphaned_
tool_ results - Every
tool_resultwhosetool_useis missing — the error that 400s. - orphaned_
tool_ uses - Every
tool_useid in the transcript that has no matchingtool_result. - parse_
omissions - Read a verdict out of the validator’s reply.
Nonemeans it said nothing usable — the caller treats that as no verdict, not as a failure, because a validator that cannot run must not be able to veto a compaction the run may need to survive. - rebuild
- Rebuild the transcript around
summary. - render_
for_ summary - Flatten messages into plain text for the summariser.
- retry_
instruction - The summariser’s second attempt: the same instruction, plus what the first attempt lost. Naming the omissions is the whole intervention — the summariser cannot see its own gaps, and a bare “try again” would sample the same blind spot.
- thin_
old_ results - Shorten old tool results, leaving the tool calls that produced them.
- validate_
instruction - Build the validator’s one user message.
- worth_
compacting - Whether compacting would actually remove anything worth the round trip.