internal/compaction
github.com/ensera-ai/taisce/internal/compaction · 2 files · 333 lines · 5 tests · source
Package compaction decides how a subject's history rolls up on the time axis, and how a context is assembled from the roll-up under a budget. It has no I/O, no clock and no model: it takes what a store reports and says what to write next and what to hand back, so both rules can be read and tested without a database or a provider.
The roll-up
A history is a sequence of formed turns in log order. The newest turns stay verbatim. Behind them, every full run of Branch consecutive turns becomes one level-1 segment: a summary carrying the offsets it covers. Every full run of Branch consecutive level-1 segments becomes one level-2 segment over their union, and so on. A segment is written once and never rewritten: what makes it stale is erasure, which deletes it through its registrations, and the pass then finds the gap and writes a new one from what survived. Summarisation work is therefore amortised O(1) per turn: each turn is summarised once per level it climbs, and the number of levels is logarithmic.
The assembly
A context is the newest Verbatim turns as they were said, then the highest-level segments that cover everything older, newest first, each replacing the turns and lower segments under it. Reading a context touches O(log n) segments. It is assembled under a character budget and cut from the oldest end, because the newest is what the next turn is about; the cut is reported, never silent.
Why segments cover whole turns
A turn is the unit of the log and the unit of the write contract, and a tool call with its results is one group inside one turn. A segment that covers whole turns therefore cannot split a group, which is the atomicity G9 requires, and it needs no rule of its own to keep it.
Where it sits
Imports: none in this repository
Imported by: internal/formation, internal/infra/inference, internal/infra/pg
Files
| File | Lines | Declarations | What it is for |
|---|---|---|---|
| compaction.go | 273 | 15 | Carries the package documentation. |
| material.go | 60 | 7 | Material is what one segment is written from: either the turns it covers, as they were said, or the segments of the level below it. |
5 tests hold this package's behaviour; the list is named for what each one proves.