How checkpoints work
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.
- What the store already knows
- A tag is a statement
- A diff reads only what changed
- Revert: retract, then restore
- What a revert leaves alone
- Previews that burn ids
- One implementation, two engines
- 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_add, the transaction that asserted it;t_ret, the transaction that retracted it, or nothing while it is live.
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:
asOf(t)shows the tags that existed att.- Untagging is an ordinary retraction, and the name can be used again afterwards. Asking an old view for a tag gives the transaction the tag named at that time.
- A tag can name an earlier transaction than the one that writes it. A revert needs that, to name the state just before itself.
A tag table would have needed a file format bump and would have had no history.
Three rules keep tags trustworthy:
sys:tagis engine-owned. Only the tag operation writes it, and a user assert fails withReservedNamespace, so a tag cannot be forged.- Names are unique among live tags (
TagExists). - A name may not be
nowor look like a number. Every operation that takes a moment accepts a transaction number,nowor a tag name, and these rules keep that unambiguous (InvalidTag).
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:
| Row | List |
|---|---|
asserted after a, still live at b | added |
live at a, retracted by b | removed |
| asserted and retracted between them | transient (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:
- Corrections come as pairs. When a supersede link added in the range joins an added statement to a removed one, the diff reports the pair as
superseded, so a reader sees “5 replaced 1” rather than an unrelated add and remove. - A reversed range (from a later moment to an earlier one) swaps
addedandremoved. - A diff is a read. It honours a result limit and says whether it is
complete.
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:
- the copy gets a fresh eid, with the original subject, predicate, object and valid time;
- a link
(copy sys:restoredFrom original)says where it came from.
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 1 asserted Alice’s job at Acme (1), a confidence on it (2) and her email preference (3).
- Transaction 2 tagged transaction 1 as
before-sync. - Transaction 3, the sync, superseded the job with Globex and added two wrong facts.
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:
- A revert restores statement 1 as 16 and writes the link 17,
(16 restoredFrom 1). - A user writes a layer on that link: a note on 17.
- 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:
- statements about transactions (tags and transaction metadata, such as who wrote a transaction and why);
restoredFromlinks;- transitively, any statement that stands on one of those.
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:
- Reading. The diff is a single function over a small read trait: rows added since a transaction, rows retracted since one, a statement by id, and a pattern scan. redb implements the trait directly, and SQLite through an adapter. Nothing is written twice.
- Writing. The revert uses the shared transaction handle and one new primitive in each engine. That primitive writes a statement as the engine rather than as the user, so it may use the reserved predicates. It may also point a
restoredFromlink at a retracted statement, the same exceptionsupersedesalready had.
Every checkpoint test runs on both engines and compares the results.
8. How it is tested
- 19 scenario tests in the facade’s suite, each run on SQLite and on redb. They cover tags (including forged tags), diffs (including transient statements and reversed ranges) and reverts. The revert scenarios include layers and graph memberships coming back, an unchanged history, undoing a revert, the unique conflict above, and a preview that matches the real revert.
- Two property tests, run for 800 cases each, mix random asserts, layers, supersedes, retractions and reverts:
- every diff replays exactly;
- after a revert, the live memory equals the target in content, ignoring bookkeeping.
- Every package has its own test: the JSON bridge round trip, Node, Python, a
tm revert --dry-runrun that checks the file’s last transaction is unchanged, and an MCP session in which an agent checkpoints, writes and rolls back. The MCP tests also check thatrevertis hidden by default and refused in read-only mode.
What is still open
- Erasure. The specification says a revert must not bring back content that has been legally erased, and must list it as
erased. Until erasure exists there is nothing to test, so that requirement is marked partial. - Cost of large reverts. Each restored statement adds a link row, so a revert of a large import writes about twice as many rows as it restores. The preview shows the counts before anything is written.
- Branches. They are deliberately not here. A tag names a point on the file’s one line of history. A second line of work will be a second file, merged back by the planned memory merge, which combines files by statement identity and reports disagreements as conflicts instead of picking a winner.
The design decisions are recorded as D54–D60 in lat.md/checkpoints.md, and the code is in tm-core/src/checkpoint.rs.