Skip to main content

Module compact

Module compact 

Source
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] ref

Every 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 limit chars, head and tail kept.
clean_judgement
What survives of a model-written judgement, and why not the rest.
compile
Compile messages into a handoff of at most options.budget_chars characters.
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 limit chars, 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 messages holds, 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 lone ESC 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.