small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
albedo robot-docs compaction.md
18 kB · 289 lines
Markdown
at main

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.

Explicit HTTP compactions use durable receipts. Connection loss does not cancel accepted work. The receipt preserves a failed result after a strategy was applied; interrupted receipts require inspection before new work.

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 request's size with a percentage of known capacity. Without capacity it does not trigger automatically.

Every strategy sizes through Context.sizing (compaction.Sizing); none turns lengths into tokens itself. A request's size is the provider's count when its route has a count endpoint (Upstream.count), else the reported input and output of the request it continues plus an estimate of what came after, else an estimate of the whole request. measured owns that rule: what a reply's usage measured (how many inputs its request carried and their input chain, nothing when the reply reported no usage), and that a request continues it only while it starts with the same inputs. The loop keeps the value between steps and rebuilds it from the request ledger after a restart. Rows are priced by their source session and sequence from the reported usage of the calls that wrote and first sent them (requests.row_costs). Inputs without a known source use estimates, including synthesized inputs with the same content as a transcript row and one-to-many provider projections. The link lookup reads the requested rows once and then finds the other transcript rows that share their provider requests. size is the one module that estimates: a crude length measure, with clips priced by their route (size.Route.clips, see media), times a correction each response teaches for its route and model, priced high until a route has reported three times and held within one to four times the crude measure. Each observation's size says how much of it was counted, reported or estimated, with the correction the estimate used (see the context inspector).

Each strategy's observation names where it compacts as trigger_tokens, in the same measure as the observation size's tokens. For rolling, snapcompact and coding-lcm it is their share of the window rounded up to a whole token, the boundary triggered compares against; for persistent-agent it is foldTokens, or 85% of a smaller window. The full and summary context views both carry it, and it is null until a request has been observed. The context meter draws compaction at that point and counts the tokens left until it, and draws neither when the strategy reports none. Clients must not derive the point from the window themselves.

persistent-agent and coding-lcm size their tails, chunk checks, pressure relief and merge guards the same way. persistent-agent starts a fold only on the trusted part of a request's size: what providers measured, and estimates once their correction has settled from reported usage. A request sized only by a provisional estimate, such as the first after an upgrade, never folds, and its observation says so. A request that still overflows comes back as a ContextTooLong refusal, and loop forces one compaction and resends it. The refusal covers a body past the provider's byte limit as well as a prompt past the window: Google's The message size (N bytes) exceeds 30.000MB limit. (Claude behind Vertex and Antigravity), Gemini's The input token count exceeds the maximum number of tokens allowed N., Anthropic's request_too_large, and Anthropic's 400 invalid_request_error (prompt is too long: ...) read as ContextTooLong, since a session of screenshots reaches those limits long before its window. That compaction never replays the refused request to summarize it, since the summary would be refused the same.

Row sizing in extensions #

Use context.sizing.rows(rows) when each compaction.Row carries the origin of the occurrence it represents. Use compaction.size_inputs(context.sizing, inputs) for bare inputs, including summaries and notes created by the strategy. This replaces context.sizing.inputs(inputs).

A stored-fold provider returns compaction.Prior(folds, rest, rest_offset). Set rest_offset to Some(list.length(evicted)) if the provider removes evicted and returns the untouched suffix. Use None if the provider rewrites or reorders that history. Use compaction.no_prior(history) when it applies no folds. A strategy can get that suffix's sourced rows from compaction.prior_rows(context.sizing, prior).

The compaction.Project callback receives transcript.SourcedEntry values and returns compaction.Row values. Preserve a source when one projected input represents the source occurrence. Give a merged or split input None for its source when one occurrence cannot price it.

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 and the provider's declared image limits, with an upper bound of 80 frames. Each provider's ImageLimits carries a VisionProfile (geometry family, whether images ride inline, and a frame cap for a provider that states no image count); snapcompact reads only that, never a provider or model name. The limits are the ones the request is sent under, which runtime.upstream sets to no images when the model's catalog says it reads only text. Settings validation requires 0 < tailPercent < triggerPercent < 100 and 0 < archivePercent < 100.

Session transfer carries the saved archive. The destination renders its frames from the transferred text as needed. A rewind clears the saved archive in the same transaction that cuts the transcript, so replaying the same prompts cannot bring back frames of the old replies. snapcompact-memory initializes the archive tables, prunes old cached frames on startup, and removes a deleted session's archive. Its setup also initializes rolling storage for the text fallback.

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 run. Input names sort in chunk order. sdk/renderer finds the binary, sets the 30-second timeout and runs it through albedo_ports.

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. When the request may carry 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 shared library tables.

Rolling keeps a summary and fingerprints the user messages it covers, so the same cut can survive provider switches and transfers. A rewind clears the saved rolling summary in the transcript transaction. Replaying the same user messages can produce different answers, and a matching user fingerprint would otherwise bring back the old summary. See the rolling history follower.

coding-lcm and persistent-agent share the LCM summary graph. The lcm-memory extension follows forks and rewinds for that graph through a FollowPlugin, whichever strategy is active. A fork copies the nodes and pending memories whose whole source span it keeps. A rewind drops the nodes and pending memories that reach past the cut, and the complete children of a dropped node become the frontier again. See graph.follow. The graph's tables belong to lcm-memory, which initializes them and removes the graph when a session is deleted. coding-lcm and persistent-agent declare its owner, so the graph's functions run under its storage handle whichever strategy calls them. persistent-agent initializes and cleans up its own failed-merge records.

lcm.grep walks summary pages in node order within the session and matches Unicode text in Gleam. It looks up the first frontier node covering each returned source row. Pages with several source hits share a scan of the frontier. See graph search.

persistent-agent consolidates memories while they take more than memoryTokens and stops once they fit it. Six tenths of memoryTokens only sizes how much one pass tries to free; consolidation does not chase it. A memory's size is a size value: the provider's count of the request with the memories in it, past the request without them, when the route has a count endpoint; else the output tokens, thinking included, that the call named by the memory's written_by reported; else the size module's labelled estimate. Whether to consolidate is decided on trusted sizes only: measured ones, and estimates once their correction has settled from reported usage. Memories written before sizes were linked, priced with a provisional correction, never start a consolidation by themselves. A pass sizes the whole frontier, the batches it takes, and once the memories it leaves. consolidation.plan picks the batches oldest first: four consecutive memories of one level merge into one a level up, and adjacent pairs merge only when no such group is left. It counts each batch as saving half of itself and stops once that covers the excess, so older life grows lossier and the newest memories keep their detail. A compaction makes one pass of merges side by side, at most four, and leaves the rest to the next fold; another pass runs only to fall back to smaller batches after refusals. A memory a merge wrote during this compaction is not merged again, alone or in a batch, until the next compaction, so a memory climbs at most one level per compaction. The trailing level-0 memories, grouped in fours from the oldest of them, end in the newest group of written memories, whole or still forming. A batch touching it is merged only when that group is the whole frontier and the compaction has not merged anything yet, so recent memories stay as the agent wrote them even when that leaves the memories over memoryTokens until a later compaction.

Each merge prompt asks for a length relative to the memories it joins, never a number: about half as long, or a third when half would pass half the summarizer's output limit. When half would still leave the memories over memoryTokens, even counting half of every older memory still free to merge, the pass's merges share by size what memoryTokens leaves beside the memories they do not merge, asking for a third or a quarter, never less. It also asks for a cue to each detail it leaves out. The joined memories stay in the graph as the merge's children, so lcm.grep, lcm.describe and lcm.expand still reach them and their rows. The length is prompt guidance; nothing truncates the reply. A merge cut off at its output limit, or one whose size is not a tenth below the memories it replaces, is discarded. The round then falls back from groups of four to pairs, a refused pair ends it, and no fold asks for that batch again for a day.

Everything an LCM request shows before its verbatim tail follows from the rows the graph covers. When the tail continues a turn whose user message a fold took, that message stays verbatim before the tail until the next fold, even after a newer message arrives. A new message therefore only appends, so the next request keeps the provider's cache and reuses its reported count. See uncovered_of.

Summary failures and progress #

reply_text rejects an empty summarizer reply and detects replies that stopped before finishing. A long reply cut at its output limit is kept as a Summary with cut_off set, and its text ends with a closing note for the reader. Code decides on the flag, never on the note. A summary combined from parts is cut_off when any part was, even when the reply that combined them finished. persistent-agent discards a consolidation merge marked cut_off, because the merge lost its end and would replace complete memories. LCM graph nodes and pending memories store the flag in a cut_off column, and a forced persistent-agent fold re-forms any cut-off leaf from its rows. Nodes written before that column were marked once, when it was added, by whether their text ends with the note.

A Summary also names the provider request whose reply is its text, in written_by: the provider_requests row the loop recorded for the call, or the call that joined a summary's parts. Text kept verbatim has none. Graph nodes and pending memories store it in a written_by column, so what a memory cost to write is read from that call's reported usage rather than from its text. Nodes written before the column stay unlinked, and nothing guesses a link for them. A transfer shifts the column with the requests it moves.

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. The evicted history is what the strategy's kept tail does not match, counting a newest input the projection rewrote as kept, and moved forward to a user message. A strategy that kept nothing, or kept no user message, evicted the whole history, so a compaction never leaves the notes silently unwritten. 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. A fit row scaled from a picture no row of the session shows any more becomes its note. Unreferenced payloads, scaled copies included, are then released. User uploads are excluded from that cleanup. The same module implements age-based tool-image expiry, and runs the payload reads and inserts that sdk/images exposes (core table operations). A transcript row or a saved summary does not guarantee continued access to an image payload.