Tiramemsu

How checkpoints work

Tiramemsu articles · 2 December 2026

Tiramemsu 0.13 can tag a moment in memory, diff two moments and revert to a tag (the release notes show how to use them). This article explains how they work inside a store that never deletes anything. Version control usually gets undo by rewriting a pointer or dropping rows. Tiramemsu can do neither, and that constraint shapes every part of the design.

The design adds no table, no index and no file format version. Tags, diff and revert are built from what the store already kept.

  1. What the store already knows
  2. A tag is a statement
  3. A diff reads only what changed
  4. Revert: retract, then restore
  5. What a revert leaves alone
  6. Previews that burn ids
  7. One implementation, two engines
  8. How it is tested

1. What the store already knows

Every statement in tiramemsu is a row with its own id (the eid), a subject, a predicate, an object and a valid time. It also carries two transaction numbers:

t_ret is set once and never cleared, and no row is ever deleted. On SQLite, triggers refuse anything else. So the memory as of transaction t is exact: it is every row with t_add ≤ t that was not retracted by t.

The store can also list rows by their events, through two indexes: rows asserted after a transaction (by t_add), and rows retracted after it (by t_ret). Constraint checks at commit and file conversion already used those two lists. Checkpoints are built from the same lists and from asOf.

2. A tag is a statement

A tag is not a pointer in a side table. It is a statement whose subject is a transaction:

tx.tag("before-sync")      # writes  (tx:1  sys:tag  "before-sync")  in transaction 2

Because tags are statements, they are history:

A tag table would have needed a file format bump and would have had no history.

Three rules keep tags trustworthy:

3. A diff reads only what changed

The obvious diff compares two full snapshots, so every call costs as much as the database. Tiramemsu uses a simpler fact: a row can differ between asOf(a) and asOf(b) only if it has an event between a and b. So the diff reads the rows with an event after a, through the two event indexes, and sorts each one into a list:

RowList
asserted after a, still live at badded
live at a, retracted by bremoved
asserted and retracted between themtransient (left out unless asked for)

In the code that is two loops over the event lists, bounded by b:

for r in store.added_since(a)? {
    if r.t_add > b { continue; }
    if r.t_ret.is_some_and(|t| t <= b) { transient.push(r) } else { added.push(r) }
}
for r in store.retracted_since(a)? {
    if r.t_add <= a && r.t_ret.is_some_and(|t| t <= b) { removed.push(r) }
}

The cost follows what happened after a, not the size of the memory. The usual question, “what changed since my checkpoint?”, reads exactly the change: a diff from a tag one transaction ago reads that transaction’s rows, even in a memory of ten million statements. A diff between two old moments also reads the events after the later one and skips them, so it costs more the further back it starts.

Three refinements sit on top:

A property test checks the contract: on random histories, applying every diff to the earlier statement set gives exactly the later one.

4. Revert: retract, then restore

A revert to r has to make the live memory equal in content to asOf(r). It must not delete or rewrite anything on the way. It runs as one transaction in four steps.

Step 1: diff from the target to now

It is the same diff as above. added is what has to go, and removed is what has to come back.

Step 2: retract everything in added, first

Each retraction is explicit, so it cascades to the layers that stand on the statement, as any retraction does. The retractions come before any restore because of unique predicates.

Take email, declared unique. Alice held "a@x" at the target, and someone else holds it now. Restoring Alice’s statement first would fail. Retracting first frees the value, and the later holder has to go anyway, because it was not there at the target.

Step 3: restore everything in removed, with new ids

The original row cannot come back. Its t_ret is set, and clearing it would change every past view and break the invariant the triggers enforce. So each statement is asserted again as a copy:

Layers raise one problem. A layer is a statement about a statement: (stmt 1 confidence 0.9). When statement 1 comes back as 16, its confidence has to come back about 16, not about the retracted 1.

The restore therefore keeps a map from old eid to new eid and rewrites subjects and objects through it. It processes statements in ascending eid order. That order is enough, because a statement is always younger than any statement it refers to: by the time the layer on 1 is restored, 1 already has its new eid. Graph memberships and derived-belief supports are statements too, so they come back the same way.

Step 4: the transaction records itself

It writes (tx:4 sys:revertedTo tx:1). If the caller passed tag_before, the transaction also tags the state just before the revert, so the revert can itself be reverted.

The result, row by row

This is the bad sync from the walk-through. The output is from a real run:

Transaction 4 is revert_to("before-sync", tag_before="bad-sync"). These are the rows that are live after it:

 eid  subject     predicate          object          t_add
   3  v:alice     v:prefers          "email"         1      untouched: live at both ends
   4  tx:1        sys:tag            "before-sync"   2      bookkeeping, left alone
  15  tx:3        sys:tag            "bad-sync"      4      tag_before
  16  v:alice     v:worksAt          v:acme          4      copy of 1
  17  stmt 16     sys:restoredFrom   stmt 1          4
  18  stmt 16     v:confidence       0.9             4      copy of 2, now about 16
  19  stmt 18     sys:restoredFrom   stmt 2          4
  20  tx:4        sys:revertedTo     tx:1            4

Statements 5 to 9, written by the sync, are retracted with t_ret = 4, and statements 1 and 2 keep t_ret = 3. asOf(1), asOf(2) and asOf(3) return exactly what they returned before the revert.

Seeing through a revert

Compared by statement id, the memory now differs from before-sync: 1 and 2 are removed, 16 and 18 are added. That is true, but it is not what a person means by “did the revert work?”.

The diff’s content mode answers that question. It compares (subject, predicate, object, valid time) and first follows each statement reference through the restoredFrom chain to its oldest original. Under that comparison 18 is about 1, just as 2 was, so both pairs cancel and the diff is empty. The chain also crosses several reverts, so reverting a revert still compares cleanly.

5. What a revert leaves alone

The first design excluded only tags from a revert. Without that, reverting to a moment before a tag was written would retract the tag you had just reverted to.

Then the property test added random reverts to random histories, and it failed with NotLive. It had found this sequence:

  1. A revert restores statement 1 as 16 and writes the link 17, (16 restoredFrom 1).
  2. A user writes a layer on that link: a note on 17.
  3. A second revert goes back to a moment before the first revert. The link 17 is not user data, so it is neither retracted nor restored, but the note on it is user data. The revert tries to restore the note, and its base is not live.

The fix was a rule rather than a special case. A revert ignores bookkeeping: anything that records history rather than belief. Bookkeeping is:

A note about history is history too. The check is memoized, so a deep chain of layers is walked once. Diff leaves bookkeeping out by default as well, so a diff after a revert does not list its own links as news.

6. Previews that burn ids

preview_revert runs the whole revert in a dry-run transaction, the mechanism behind speculative transactions, and rolls it back. It runs every check a real revert runs, including the schema, unique-predicate and constraint checks at commit. If any of them fails, the preview returns that failure in its report instead of raising it.

The preview allocated ids 10 to 14 and then rolled back. Those ids are never issued again, which is why the real revert’s copies are 16 and 18, not 10 and 12. That is deliberate. An id the preview showed you never names a different statement later, so a log of the preview stays accurate.

7. One implementation, two engines

Tiramemsu has two engines: SQL over SQLite, and a typed storage engine over redb. Checkpoints have one implementation for both:

Every checkpoint test runs on both engines and compares the results.

8. How it is tested

What is still open

The design decisions are recorded as D54–D60 in lat.md/checkpoints.md, and the code is in tm-core/src/checkpoint.rs.

Next: checkpoints for agent memory, a walk-through →