# 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. `render_frames` writes every missing chunk to one scratch directory and renders them in one [`sdk/renderer`](../src/albedo/harness/sdk/renderer.gleam) run. Input names sort in chunk order. `sdk/renderer` finds the binary and sets the 30-second timeout. The port that runs it is [`albedo_render`](../src/albedo/harness/albedo_render.erl), which stays Erlang because Gleam has no port library. The other strategies have their own state and configuration in [`rolling`](../src/albedo/harness/extensions/rolling/strategy.gleam), [`coding-lcm`](../src/albedo/harness/extensions/lcm/core.gleam), and [`persistent-agent`](../src/albedo/harness/extensions/persistent_agent/extension.gleam). Their implementation, rather than snapcompact's defaults, determines their behavior. When the model reads no images, snapcompact runs rolling's strategy as its text fallback, and that strategy keeps its summary in rolling's table under rolling's storage handle. See [public features own their tables](extensions.md#public-features-own-their-tables). `coding-lcm` and `persistent-agent` share the LCM summary graph. The [`lcm-memory`](../src/albedo/harness/extensions/lcm/memory.gleam) extension follows forks and rewinds for that graph through a [`FollowPlugin`](plugins.md#daemon-contributions), whichever strategy is active. A fork copies the nodes whose whole source span it keeps. A rewind drops the nodes that reach past the cut, and their complete children become the frontier again. See [`graph.follow`](../src/albedo/harness/extensions/lcm/graph.gleam). The graph's tables belong to `lcm-memory`. `coding-lcm` and `persistent-agent` declare its owner, so the graph's functions run under its storage handle whichever strategy calls them. ## 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. Payload reads and inserts live in [`sdk/images`](../src/albedo/harness/sdk/images.gleam). 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) - [Extensions](extensions.md) - [Python kernel state](kernel-state.md) - [Plugins](plugins.md)