Expand description
Compaction: compile a conversation into a handoff, deterministically.
This is the compute half of compaction. Nothing here calls a model, nothing here
paraphrases: every line is either quoted from the transcript or read out of it
(files_modified, exit_code, an error name, a path in a call’s arguments), so the same
input twice produces the same bytes and a fact that is not in the transcript cannot appear
in the handoff.
The shape is three windows — head, tail, middle — and this module only ever sees the middle: the turns that no longer fit in the request. What it returns replaces them:
## Historical Task Snapshot the most recent user ask, one line
## Original Request the first user ask
## Constraints & Preferences the user's own words, quoted
## Blocked calls that failed and were never retried successfully
## Errors & Fixes a failure that a later call fixed
## Error Names Seen *Error/*Exception/errno names, first seen
## Files Touched path — read/written/patched (failures say so)
## Commands Run command — outcome; failures keep their place first
## Open Threads TODO/next-step language, quoted from assistant turns
## Session Log (oldest first) one line per message, each with a [#N] refEvery section is capped and the whole document has a hard character budget. What gives way,
in order: the ledgers shorten alternately (three entries each, then one), the least
load-bearing sections drop whole, the verbatim block elides, and only then are log lines
given up — the log is the index that makes every other line recoverable, so it is not what a
tight budget spends first. Each removal says how much it removed, because “anything omitted is
one [#N] search away” has to stay true.
Untrusted text is treated as untrusted: tool output can contain ## A Heading, an escape
sequence, or a model turn marker, and a path can contain a newline. clean neutralises all
of it and safe_path refuses anything that could break a bullet, so a tool result cannot
open a section of its own.
Modules§
- verify
- The compaction harness (22d): measure the handoff instead of trusting it.
Structs§
- Facts
- The ledgers a compile would render, before any character budget touches them.
- Limits
- Per-item character caps. Every truncation is marked and stays recoverable by ref.
- Options
- What to compile, besides the transcript itself.
Constants§
- BLOCKED_
HEADING - COMMANDS_
HEADING - CONSTRAINTS_
HEADING - EARLIER_
HEADING - ERROR_
NAMES_ HEADING - FILES_
HEADING - FIXES_
HEADING - GOAL_
HEADING - JUDGEMENT_
BULLET_ CHARS - Longest a single judgement bullet may be, and how many may reach the handoff.
- JUDGEMENT_
HEADING - JUDGEMENT_
MAX_ BULLETS - LOG_
HEADING - LOG_
MARKER - MEMORY_
HEADING - MEMORY_
NOTE - NO_
USER_ TASK - OWN_
HEADINGS - Every heading this module owns, so a previous handoff can be parsed back into sections and nothing outside this list is ever mistaken for one.
- TASK_
HEADING - THREADS_
HEADING - VERBATIM_
HEADING - VERSION
- Bumped when the rendered text changes shape: a handoff written by another version is recompiled rather than reused.
Functions§
- call_
summary - A tool call’s name and target exactly as the renderer writes them into a log line.
- clean
- One safe single-line excerpt of at most
limitchars, head and tail kept. - clean_
judgement - What survives of a model-written judgement, and why not the rest.
- compile
- Compile
messagesinto a handoff of at mostoptions.budget_charscharacters. - compile_
digest - Render the input the judgement pass reads: the exact user asks plus the assistant lines that already carry decision language — a few thousand characters instead of a transcript.
- elide
- At most
limitchars, cut on word boundaries: a mid-word cut leaves a fragment (“meo”) that looks like a separate fact and cannot be grounded against the source row. - error_
names - Distinct
*Error/*Exception/errno names, first-seen order, at most eight. - facts
- The facts
messagesholds, uncapped. - has_
error_ text - True when a sample looks like a failure. Literal-first scanning, because this runs on every tool result and the common case is a clean one.
- is_
scaffolding - User-role rows that are framework scaffolding, not something the user typed. They stay in the Session Log (they are real rows) but quoting them verbatim makes the handoff read like the user asked for them.
- iter_
refs - Every
[#N]ref in a handoff. - replace_
markers - Replace model and host turn markers with
[marker]: a handoff that carries</s>or<|im_start|>invites whatever reads it to treat the text as a turn boundary. - safe_
path - A path safe to render as one handoff bullet, or
None. - split_
sections - Split a handoff into its preamble and its
##sections. - strip_
ansi - Drop ANSI escape sequences:
ESC [ ... final,ESC ] ... BEL|ESC \, and a loneESC x. - strip_
think - Drop
<think …>…</think>spans (or a run to end of text), in linear time. - verbatim_
users - The user’s most recent real messages, verbatim, inside a bounded budget.