Compaction #
Compaction changes the conversation projected into a model request. The durable
transcript remains the source for text history, but tool-output image payloads
can be elided. The strategy interface and request preparation live in
compaction.gleam. The
context inspector reports the prepared request and its compaction
observation.
Request preparation #
A Strategy supplies project and compact. Projection reads saved strategy
state without writing it or calling a model.
prepare projects the history,
applies notes layers, and compacts when the strategy says the projection is due
or the request forces compaction. It compacts at most once per request, then
projects again. Errors propagate to the caller.
triggered triggers a forced
compaction regardless of capacity. Otherwise it compares the estimated size with
a percentage of known capacity. Without capacity it does not trigger
automatically. Estimates and provider-reported usage are different measurements.
scale uses the reported count to adjust an estimate, bounded between one and
four times the estimate.
When the previous successful request's measurement remains compatible, the
trigger uses its reported input and output tokens plus estimates of newly
appended inputs. Otherwise it uses the local estimate.
reported_observation labels the
measurement, and the loop checks whether it
can be reused. This combined count is still an approximation of the next
request.
Strategies #
The /compact command can retain the active strategy or switch to
snapcompact, rolling, coding-lcm, or persistent-agent. The command
describes an idle session and preserves the transcript. See
commands/extension.gleam.
snapcompact
can represent earlier conversation as rendered frames or serialized text, with a
verbatim tail. Its memory extension exposes transcript.grep and
transcript.read to retrieve transcript rows. Its defaults are an 80% trigger,
10% tail, and 20% archive budget. When maxFrames is unset, the frame budget
depends on capacity, image limits, and provider caps, with an upper bound of 80
frames. Settings validation requires 0 < tailPercent < triggerPercent < 100
and 0 < archivePercent < 100.
Snapcompact's Gleam extension owns archive normalization and pagination. It expands tabs to four spaces and folds adjacent CR/LF runs into full-block cells. Escape removal consumes whole Unicode scalars; malformed legacy bytes become replacement characters. The scanner retains limited CSI and OSC handling, including OSC termination by ESC followed by any character, rather than acting as a terminal emulator. Frames count scalars, including combining marks and joiners, independently. Nonpositive advance, width, or row count, or a frame with no cells, returns no pages. The native renderer retains subprocess invocation, temporary-file cleanup, hashing, and argument formatting.
The other strategies have their own state and configuration in
rolling,
coding-lcm, and
persistent-agent.
Their implementation, rather than snapcompact's defaults, determines their
behavior.
Summary failures and progress #
reply_text rejects an empty
summarizer reply and detects replies that stopped before finishing. Compaction
exposes progress stages and updates through ProgressUpdate. Consumers must
handle failure as well as progress. A compacted request is a projection, not a
replacement for the durable transcript.
The notes layer can rewrite notes when newly evicted history extends beyond its saved cut. A summarizer failure keeps the previous notes and adds a failure observation. Configuration, storage reads, or saving rewritten notes can still fail preparation after the strategy commits.
Image retention #
images.elide_evicted can replace evicted
tool-output images with a marker while retaining their text. Unreferenced
payloads are then released. User uploads are excluded from that cleanup. The
same module implements age-based tool-image expiry. A transcript row or a saved
summary does not guarantee continued access to an image payload.