# lemma

lemma lets a memory into an agent's working set only if no contradiction follows from it. In its first bench run it leaked no contradictions, and lost on speed, replay and far-worded recall.

Prototype · d/rksci labs · Published 2026-11-30 · Tags: Autonomous Agents, Evaluation

## A memory that refuses contradictions

Each memory is two write-once objects: the text, and a short statement of the facts it asserts. The bench was one run on one machine against two in-process stand-ins, with no model called. Mem0, Zep and Letta are still to be run.

## What it is for

An agent's memory gathers updates. A store that returns the old fact beside its replacement leaves the model to sort them out, and a store that returns everything spends the context window on it. In lemma each memory is a statement that can be checked: the facts it asserts, the quote each fact rests on and the memories it supersedes, all named by SHA-256.

Figure: Source to lem build to body and statement to store; below, recall to fibers, ask, check and admit, which returns memories and since_last.

A question returns facts with derivations, and the kernel's `check` re-walks each one against freshly fetched blobs. Only `check` has to be trusted. Rules are function-free Horn clauses with guards in CEL and no floating point, so evaluation terminates and an answer is a pure function of the statements, the question and the date.

## How it works

The design names four things and treats nothing else as a concept.

A body (the text as raw bytes at its hash) and a statement (facts, what it rests on, what it supersedes, the body's hash). A change is a new blob that supersedes an old one; nothing stored is edited.

`ask` searches for answers and returns derivations; it may be wrong. `check` walks a derivation in time linear in its size and is the only part that must be correct.

The working set, 4,000 tokens by default. A blob enters only if `false` does not derive. Otherwise a supersession wins, then a strictly later date, and failing both neither side enters and the pair is reported.

Host bookkeeping that orders admission and caching by use in past answers. It is never inside a blob and never settles a conflict.

## One memory, as written

A memory is one Markdown file. `lem build` stores the text below the frontmatter as the body, raw bytes at its hash, and compiles the frontmatter into the statement. Below are the specification's worked example, as the corpus generator writes it, and the note the bench writes to supersede it (lines wrapped).

```markdown
---
name: blake.pref.editor
kind: note
says:
  - { s: "blake", p: "pref.editor", o: "helix",
      quote: "switched to Helix for everything",
      from: "2026-10-02" }
  - { s: "blake.pref.editor", p: "about", o: "term.tools.editor" }
  - { s: "blake.pref.editor", p: "modal", o: "true" }
---
Blake switched to Helix for everything in October,
after a month alongside Neovim.
```

```markdown
---
name: blake.pref.editor
kind: note
supersedes: [blake.pref.editor]
says:
  - { s: "blake", p: "pref.editor", o: "nano",
      quote: "went back to nano", from: "2026-11-20" }
  - { s: "blake.pref.editor", p: "about", o: "term.tools.editor" }
  - { s: "blake.pref.editor", p: "modal", o: "false" }
---
Blake went back to nano in November; the modal editors never stuck.
```

Each fact is a triple. The compiler refuses a note whose `quote` does not occur word for word in its body. The second note keeps the name and lists the first in `supersedes`, so the old blob still answers for the old date while the new one takes its place in the window.

## Plans, conflicts and orphans

The generator writes a corpus in fixed shares, mostly preferences, projects and editor notes, with a thin stream of updates, conflicts and plans. A plan rests on other notes. `blake.plan.dotfiles.migrate` rests on the first editor note through a rule that derives which config format its owner maintains. Once the second note lands, the `needs` of the plan's first step no longer derive, and the plan is reported stale at that step.

Two notes from the same date that contradict each other, with neither superseding the other, both stay out of the window and are reported. Splitting a term in two leaves behind the notes that fit neither half, and those are reported to their author.

Figure: A corpus band by share; below, one person's notes: a supersession, a stale plan, a held-out conflict and an orphan.

A plan is a statement too, and holds only while none of its premises has a successor. On a generated corpus of 1,000 memories, asking for a plan whose premise was superseded returned this in the answer's `text` field (digests cut, lines wrapped):

```text
$ lem --store out/.lemma recall \
    '{"plan":"p00001.plan.dotfiles"}' --at 2026-12-01
STALE at step `export`: plan p00001.plan.dotfiles
no longer holds at 2026-02-02 (needs of step
`export` do not derive). Premise
p00001.pref.editor@sha256:82bbf0e2… was
superseded by p00001.pref.editor@sha256:24a74cb9….
```

## What the bench measured

One run of `lem bench run` with one params file, on 30 November 2026, on a cloud VM with 4 cores of an Intel Xeon at 2.10 GHz and 15 GB of RAM. Corpora were 10³, 10⁴ and 10⁵ synthetic memories with a 10% stream of updates and conflicting claims, generated from seed 7 by the same code that wrote the checks. No model was called. The two baselines are in-process stand-ins: `flat-file` returns the whole memory, and `bow-topk` returns the top 10 by bag-of-words, a weak stand-in for a vector store.

Figure: Bars by corpus size. lemma a tick at zero: 0 of 159, 0 of 300, 0 of 300. bow-topk 0.730, 0.517, 0.333. flat-file 0.755, 0.667, 0.667.

lemma answered every leak question correctly (accuracy 1.000) and reported the unsettled conflicts it was asked about instead of returning either side: 6 at 10³ and 33 at each larger size. The baselines' accuracy on the same questions was 0.252, 0.370 and 0.417.

Figure: Three groups of bars on a log axis from 1 to a million. lemma 7 at each size; bow-topk 84, 130, 154; flat-file 7,941, 74,593, 741,068.

An answer from lemma held 7 tokens on average at every size. bow-topk returned 84 to 154, and flat-file, which returns the whole memory, 7,941 at 10³ and 741,068 at 10⁵.

The window stayed inside its 4,000-token budget, ending at 1,162, 3,992 and 3,998 tokens.

| Corpus | Plans | Stale | Flagged | False flags | Still holding |
| ------ | ----- | ----- | ------- | ----------- | ------------- |
| 10³    | 8     | 3     | 3       | 0           | 1 of 1        |
| 10⁴    | 90    | 34    | 34      | 0           | 11 of 11      |
| 10⁵    | 887   | 333   | 333     | 0           | 110 of 110    |

| Corpus | Blobs written | Seconds to correct | Orphans found |
| ------ | ------------- | ------------------ | ------------- |
| 10³    | 1 / 2 / 2     | 0.15 / 0.31 / 0.37 | 13 of 13      |
| 10⁴    | 1 / 2 / 2     | 0.37 / 1.13 / 1.64 | 114 of 114    |
| 10⁵    | 1 / 2 / 2     | 2.6 / 9.6 / 5.2    | 1057 of 1057  |

The baselines had to re-tag every record (1,000, 10,000 and 100,000) and answered under the stale label. lemma's answers were right on the next ask, but at 10⁵ the split took 9.6 s to get there.

## Where it lost

Figure: Bars on a log axis in ms. lemma 3.705, 17.215, 27.829; bow-topk 0.01, 0.089, 0.829; flat-file 0.008, 0.135, 1.49.

A typed write needs no model call and no money, and writes two objects. Its p50 still grew from 0.93 ms at 10³ to 4.8 ms at 10⁴ and 65 ms at 10⁵, which is 976, 196 and 13.6 writes a second, while both baselines wrote in under a millisecond at every size.

Replay asked one question 100 times in one long-lived engine. Byte-identical answers came back 100 of 100 times at 10³ but 1 of 100 at 10⁴ and 10⁵. The set of memories was the same on all 100 asks; heat, raised by the first ask, moved one memory's position, and asks 2 to 100 matched each other. Fresh engines with no carried-over heat agreed 10 of 10 at every size. As the design is built, equal inputs give equal bytes only if the engine's heat is counted as an input. The preregistered form, the same answers on two hosts, was not tested.

Heat was run against plain LRU on a replayed Zipf-mix query log of 1,000 queries. Answers were the same under both and the window hit rate was the same (0.861, 0.614, 0.451). Heat made more remote calls: 458 against 408, 1,188 against 1,102 and 1,683 against 1,588. Its cache hit rate was marginally higher (0.9956 against 0.9945 at 10⁵), over a different number of cache reads, so the two rates are not directly comparable. This run does not support the claim that heat beats LRU, and the heat parameters were not tuned.

| System            | Wording | Found, 10³ | Found, 10⁴ | Found, 10⁵ |
| ----------------- | ------- | ---------- | ---------- | ---------- |
| lemma, search on  | near    | 324        | 639        | 636        |
| lemma, search off | near    | 70         | 319        | 316        |
| bow-topk          | near    | 204        | 190        | 195        |
| lemma, search on  | far     | 0          | 0          | 0          |
| lemma, search off | far     | 0          | 0          | 0          |
| bow-topk          | far     | 197        | 182        | 189        |

On near wording lemma returned no stale facts where bow-topk returned 52 to 59 per 28 questions, but more than half of what lemma returned was off-topic: 397 of 735 at 10³ with search on. On far wording, which shares only the value with the stored text, lemma found nothing with search on or off. A body must hold at least half of a query's distinct terms to match, and a question such as "anyone into techno" shares one of three. The implementation sets that threshold, not the specification, and it was not tuned on this data.

## Row by row

| Row                | Result  | What decides it                                             |
| ------------------ | ------- | ----------------------------------------------------------- |
| contradiction-leak | win     | 0 leaks at every size; baselines 0.333 to 0.755             |
| plan-staleness     | win     | 333 of 333 flagged at 10⁵, step named; baselines do not try |
| working-context    | win     | 7 tokens per answer; flat-file up to 741,068                |
| reorganisation     | win     | 1 or 2 blobs; split took 9.6 s at 10⁵                       |
| write-cost         | mixed   | No model calls; 13.6 writes a second at 10⁵                 |
| fuzzy-recall       | mixed   | Ahead on near wording; 0 found on far wording               |
| replay             | loss    | Strictly identical 1 of 100 at 10⁴ and 10⁵                  |
| heat               | loss    | Same window hit rate as LRU, more remote calls              |
| recall latency     | loss    | p50 27.8 ms at 10⁵ against 0.829 and 1.49 ms                |
| knowledge-update   | not run | Needs LongMemEval and MEME data and models                  |

## What is not known

Mem0, Zep with Graphiti, and Letta were not run. Each calls a model on write and needs a container or server and an API key, and none was used; their rows are marked not run in the result file. The only comparison is with the two stand-ins, so none of the wins above says anything about those systems.

Corpus 10⁶ was not run. The knowledge-update row and the LongMemEval form of fuzzy recall need datasets and models that were not set up. Only one host was used, so the claim that `exact` values agree across machines is untested. The corpus is synthetic and its answer key comes from the same generator, so the accuracy figures show the mechanism working on this data and say nothing about real memories.

Earlier runs on the same branch found four bugs, all fixed and covered by tests before the recorded run. One of them let one side of an unsettled same-date conflict into the window, and accuracy was 0.96 at 10³ and 0.89 at the larger sizes until it was fixed.

Mem0, Zep and Letta on the same generated corpus, with the same extractor and answer model for every system.

lemma is a d/rksci labs prototype at 0.1.0. Every bench number here is from the result file `20261130T074156Z-634d95b5ed00.json` (schema `lemma-bench/1`, params hash `sha256:650112546a39`) and its write-up `bench/RESULT.md` in the lemma repository; `measured` values hold for the one VM, `exact` values are machine independent. The plan example was run on a 1,000-memory corpus from `lem gen --seed 7`. The four charts are drawn from the same file; the run plate shows shapes only.