# 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`](../src/albedo/harness/compaction.gleam). The [context inspector](context.md) 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`](../src/albedo/harness/compaction.gleam) 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`](../src/albedo/harness/compaction.gleam) 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`](../src/albedo/harness/compaction.gleam) labels the measurement, and [the loop](../src/albedo/harness/loop.gleam) 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`](../src/albedo/harness/extensions/commands/extension.gleam). [`snapcompact`](../src/albedo/harness/extensions/snapcompact/extension.gleam) 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](../src/albedo/harness/extensions/snapcompact/albedo_snapcompact.erl) retains subprocess invocation, temporary-file cleanup, hashing, and argument formatting. The other strategies have their own state and configuration in [`rolling`](../src/albedo/harness/extensions/rolling/extension.gleam), [`coding-lcm`](../src/albedo/harness/extensions/lcm/extension.gleam), and [`persistent-agent`](../src/albedo/harness/extensions/persistent_agent/extension.gleam). Their implementation, rather than snapcompact's defaults, determines their behavior. ## Summary failures and progress [`reply_text`](../src/albedo/harness/compaction.gleam) 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](../src/albedo/harness/extensions/notes/extension.gleam) 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`](../src/albedo/daemon/images.gleam) 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. ## Related pages - [All topics](README.md) - [Change guidance](contributing.md) - [Context inspector](context.md) - [Python kernel state](kernel-state.md)