← Back to Writing

Giving Coding Agents a Memory That Outlasts the Session

About this artifact

essayon-the-recordmaintained

automation · since 2026

A coding agent's notes answer questions about the work done where those notes live. In my setup, a decision made in another repository, or in a session run by a different agent tool, usually was not in them. I run Claude Code, Codex and Kimi across several repositories on one machine, and a question settled in one session could come up again in another with nothing to say it was settled.

I built a local memory store to fix that.

The problem#

Here is an invented example of the failure. On Monday, in the backend repository, an agent and I settle how a data fetch should fail: retry three times, then mark the source stale, because a silent zero had been mistaken for real data. The decision lands in that session's transcript and in a commit message in that repository.

On Tuesday, a different tool is working in the frontend repository on the page that shows the same data. It needs to know what a missing value means. Nothing it loads at startup mentions Monday. It guesses, or it asks me, and I answer from memory if I still have it.

Each tool keeps a transcript per session, so the record exists. It is spread across three tools' folders, far too long to read back, and a new session in another repository or another tool loads none of it. If a tool later removes its old transcripts, that record goes with them.

What a fix had to do#

The fix had to run on one machine with no server to look after, and what it showed a session at startup had to be small enough not to crowd out the work. No model could rewrite memory on its own authority, and every remembered statement had to point back to the session it came from. Secrets pasted into a session had to stay out of the searchable text.

What I built#

The store has three stages: capture, distill and recall.

Capture. A nightly job copies the transcripts from all three tools into a local archive, without a purge flag, so a file deleted at the source stays in the archive. It then reads each archived file from where it last stopped, replaces anything that matches a secret pattern, and stores one row per message in a single SQLite file. The raw archive copy is not redacted, so that folder stays sensitive.

Distill. A model reads the new messages in large chunks and proposes short statements: a fact, a decision, a piece of open work, or a preference. It only proposes. Plain code then compares each candidate with what is already stored and picks one of four outcomes: add it as new, record it as another source for an identical statement, replace an older statement on the same subject, or drop it as stale.

Recall. Sessions read the result two ways. A hook inserts a small prepared digest into the agent's context when a session starts, and a second hook adds a short update mid-session when another session has captured something new. A search tool answers the rest on demand. The transcript pipeline behind it, capture and distilling, runs as scheduled jobs.

Follow Monday's decision through it. That night the backend transcript is copied and stored. The model proposes "decision: retry failed fetches three times, then mark the source stale." No stored statement covers that subject, so the code adds it, linked to Monday's session. The digest has a fixed size, so the statement competes for a place in it. If it is selected, the next backend session sees it at startup. Tuesday's frontend session most likely does not, because the ordinary entries in a repository's digest come from sessions in that repository. It finds the decision by searching for "stale" or "retry", and the result carries the session it came from.

Part What I used Why
Database SQLite, one file, write-ahead logging No server. The nightly writer and the search tool share the file.
Search SQLite's built-in FTS5 full-text index Statements are short sentences, and keyword search over them needs no extension.
Pipeline Python, standard library only No third-party packages to keep patched.
Distilling A headless Claude Code call It uses the tool I already run. The model extracts and judges, and never writes to the memory database itself.
Search tool A small Node server speaking MCP MCP is how agent tools call outside tools.
Scheduling The operating system's job scheduler. My setup scripts default to a nightly run plus an hourly catch-up Already on the machine.
Session start A hook that prints the digest, with a 10 second timeout The model sees the digest before its first prompt.

I also have a prototype dashboard for browsing the store. The two screenshots show it filled with invented sample data, not with my stored memory.

A prototype memory dashboard filled with invented sample data: counts of facts, decisions, open work and preferences, the projects that hold the most memories, and the newest distilled statements.

The prototype's topic view filled with invented sample data: eight themes drawn as linked circles, with one theme's key ideas and representative statements listed beside the map.

How we got there#

Four choices and two failures account for most of the design.

Keyword search, with no embeddings. A vector index would match meaning, and I left it out. The statements are one sentence each, FTS5 ships inside SQLite, and a keyword match is easy to explain when a result looks wrong. The cost is real: a search has to share a word with the statement. Searching "which database did we choose" misses a statement that says "chose SQLite", because "choose" and "chose" are different words to the index.

The model proposes, code decides. The extraction prompt tells the model to emit candidates only, never to compare against stored statements, and never to update or delete. Identity is decided by a key computed in code: the first 16 words of the statement after lowercasing and dropping common words. One active statement is allowed per kind and key. Whatever the model gets wrong still has to pass through that code before the database changes, and it stays traceable to a session.

Order by when the session happened. A newer statement replaces an older one on the same subject, so "newer" had to be defined. Extraction time is the obvious clock, and it breaks during a backfill: months-old sessions get distilled this week, and a January decision would then replace one from October. The code compares the time of the source sessions and uses extraction time only as a fallback.

Reserved room for open work. The digest is capped at 4,096 bytes per file, and every statement competes for that space by a score that favors decisions and decays with age. Picking purely by score starved open work: at one point the best open item ranked 96th of 32,550 candidates and the global digest carried none. Up to five slots are now reserved for open work before the rest compete, as far as the size cap allows.

The store read its own output. For nine days in July the capture stage ingested the transcripts of its own extraction calls, and produced 21,264 junk sessions before anyone noticed. Ingest now has two guards against it and a check that runs after every ingest.

A tool changed how it wrote files. Capture resumed each file from a saved byte position. Codex rewrote some session files in place, the saved positions stopped lining up, and ingest failed from November 4 until a fix merged on November 17. The nightly job stops when ingest exits with an error, so those runs produced nothing new instead of distilling from a half-read archive.

What it does not prove#

The store records what agents said they decided. It does not check that they were right, and a statement in it is a model-extracted claim with a pointer to its source. I have no number for how often a recalled statement changes what a session does. A model also judges whether a later statement resolves a piece of open work, with a confidence threshold of 0.7 that I set by hand. Its precision is unmeasured, so a wrong judgment that passes the code's checks can close work that is still open.

If you work in one repository and commit your decisions, git history is the simpler option. The store is meant for work spread across repositories and tools, and the next question to answer is the unmeasured one: whether Tuesday's session, having found Monday's decision, does anything different with it. The institutional memory post covers the load-by-default principle the digest follows.

Reference#

These are defaults and constants in my scripts.

Setting Value
Extraction chunk About 25,000 tokens, estimated at 4 characters each
Digest size 4,096 bytes per file, up to 5 slots reserved for open work
Score half-lives 30 days for open work, 45 for facts, 180 for preferences, none for decisions
Open work expiry 45 days with no supporting session
Closure judge Confidence at least 0.7, and its quoted span must appear verbatim in the closing statement
Search results 20 at most

The decision rule for a candidate, as a sketch:

active = find_active(cand.kind, cand.key)
if active is None:             insert(cand)                   # new
elif same_text(active, cand):  add_provenance(active, cand)   # same statement, another source
elif is_newer(cand, active):   expire(active); insert(cand)   # replace
else:                          mark(cand, "stale")
# is_newer compares source-session time; extraction time is only the fallback

The session-start hook registration, as an illustration. The script path is a placeholder:

{ "hooks": { "SessionStart": [{
    "matcher": "startup|resume|clear",
    "hooks": [{ "type": "command", "timeout": 10,
                "command": "python ~/hooks/session_start_digest.py" }] }] } }

Sources, as of November 30, 2026: the store's prompts, resolver, digest generator, ingest, redaction, search and scheduling code, their tests, comments and git history, plus the hook settings I run. The Monday and Tuesday example is invented. Nothing here describes stored memory content.