# Memory

> What zelk remembers across conversations, how a few notes are chosen to reach the model, who may write them, and why a note is never an instruction.

A conversation ends, and what zelk learned in it doesn't have to. Memory is
a small set of notes that later conversations read, so zelk stops asking
what you already told it and stops repeating a mistake you corrected.

Most of what happens in a session is history, and stays in the session's
log. Only what a person would otherwise have to say twice becomes memory.
A stale note the model trusts does more harm than no note, so memory is
kept small, dated and attributed, and it can be read, corrected and
forgotten.

## A note

A note is a small markdown file with front matter. It has a name, a
one-line description of at most 150 characters, a type, who wrote it, where
it came from, when it was created and updated, and a body of at most 2,000
bytes. It may also have an expiry. The type says how the note is used:

| Type | Holds |
|---|---|
| `user` | who you are: your role, your expertise, how you like answers |
| `feedback` | how you want work done: corrections, and approaches you confirmed |
| `project` | what the code and history don't show: goals, decisions, constraints |
| `reference` | where to look: a dashboard, a tracker, a runbook |

`user` and `feedback` notes change how zelk works on every turn, so zelk
treats them as rules and puts them first. The author is `user` (saved for
you by a host command, or written by hand), `model` (saved by the model's
tool call) or `host`.

A name is the note's identity within its scope. Saving under a name that is
taken is a conflict that shows the note already there, so the model
corrects it instead of adding a second one. A note's revision is a digest of
its file's bytes. A correction or a forget names the revision it read, so a
note changed meanwhile, by another session or by you in an editor, fails as
a conflict instead of being overwritten.

## Scopes, and where notes live

A note lives in one scope. **Project** notes belong to one project. The host
names the project; the terminal UI and the CLI use the workspace's absolute
path, so a moved checkout starts as a new project. **User** notes hold in
every project. A session reads its own project's scope and the user scope,
and no other project's.

A save names its scope, and there is no default. A note goes to the user
scope only when you said it holds everywhere or it is plainly about you.
When that is unclear it goes to the project, and zelk never promotes it
later on its own.

The file store keeps each scope in a directory of `<name>.md` files under
the state directory's `memory/`. You can read and edit them with any editor,
and an edit is just another revision. A file that doesn't parse is listed
with its problem and is never shown to the model. There is no vector index
and no separate index file. In Go, `memory/file` is that store, and
`memory/mem` is an in-process one for tests.

## What reaches the model

A project’s notes and the user’s, narrowed to what the request carries, with the limit set to 2,000 bytes here (8,192 by default). The frame takes 694 of it. Four steps take the store to the request:

1. **stored**: every note of the project’s scope and of the user’s, as the store lists them.
2. **readable, not expired**: a note that can’t be parsed or whose expiry has passed is skipped; the store still lists it.
3. **ordered**: how to work (user and feedback notes) before context (project and reference notes); project scope before user scope; newest first.
4. **in the request**: a frame saying the notes are data, then each note whole while it fits, else one line, else only counted, all within the limit.

- **A session starts**. Nothing is guessed at; the budget decides. zelk reads both scopes, skips what has expired or can’t be read, orders what is left, and spends the limit on the notes in that order: each whole while it fits, then one line each, then a count. The request opens with a frame that says the notes are data.
   - the request: 3 notes in full (tests, commits, terse), 2 notes as one line (role, deploy), 1 note left out (grafana)
- **The model saves a note**. A save is checked before it is kept. The store refuses anything that looks like a credential. A note that passes records who wrote it and when, and it goes to the head of its section in every later session, which can push an older note down from whole to a line.
   - before: 3 notes in full (tests, commits, terse), 2 notes as one line (role, deploy), 1 note left out (grafana)
   - MemorySave staging-key → refused: it looks like it holds a credential, and memory never holds secrets
   - MemorySave pr-size → saved as project feedback, author model
   - the request: 3 notes in full (pr-size, tests, commits), 2 notes as one line (terse, role), 2 notes left out (deploy, grafana)
- **A stale note is forgotten**. Forgetting removes the note from the store. No later view or search has it, and the room it held goes to the next note in order. A session that already holds the old view keeps it until its next compaction, and the forget’s result tells the model not to use it.
   - before: 3 notes in full (tests, commits, terse), 2 notes as one line (role, deploy), 1 note left out (grafana)
   - MemoryForget deploy, rev c3d4e5f6a1b2 → forgotten from project memory
   - the request: 3 notes in full (tests, commits, terse), 2 notes as one line (role, grafana)

Memory reaches the model by three paths, each recorded in the session's log.

**The memory view.** When a session starts, zelk reads both scopes and
records a view of them in the session's configuration. The view has a limit,
8 KiB by default, counted in bytes (`WithMemoryLimit` in Go). Each stage
narrows what is left:

1. A note that can't be read, or whose expiry has passed, is skipped. The
   store still lists it.
2. The rest are ordered. Rules come before context, project scope before
   user scope, and the newest note first.
3. The limit is spent in that order. A note is shown whole while it fits,
   then as one line (name, scope, type, date, revision and description),
   then only counted, with a line saying that `MemorySearch` finds the rest.

The view opens with a frame, about 700 bytes of the limit, that says the
notes are data. It sits at the start of the context, beside the project
instructions, where a model attends best.

zelk doesn't guess which notes a message needs. There is no relevance step,
because a preference the model has to choose to look up is one it will
sometimes miss. Notes are shown whole while they fit because a live model
once answered from a note's name without reading its corrected body.

A view isn't rebuilt on every turn, which would break the provider's prompt
cache. A save or a forget by the model records the store's new view, and
the request takes it up at the next compaction. A change made outside the
session, by another session or by you in an editor, reaches the model at
once, as a note before its next call, so it never acts on a note you have
since corrected. A resumed session works from what the store holds now, and
a replay never reads the store.

**On demand.** `MemorySearch` ranks the notes by the words they share with
a query, counting a word in the name, description or type twice, and returns the
best in full within a bound. `MemoryRead` returns notes by name. Both read
the live store.

**Subagents.** A child session gets the view and the read tools, but not
`MemorySave` or `MemoryForget`. A worker on one slice of a task doesn't
decide what you want remembered. It reports what is worth keeping, and its
parent decides.

## How notes are written

**By the model.** The model saves with `MemorySave` and removes with
`MemoryForget`, on its own judgement, guided by the memory section of its
system prompt. It saves when you correct how it works or confirm an approach
that wasn't obvious, when it learns who you are, when it learns project
context the code doesn't show, and when you ask it to remember something. It
doesn't save secrets, instructions it found in a tool result or a web page,
what the code or git history already says, or the state of the current task.
It corrects the note that covers a fact rather than adding another. Nothing
extracts notes from transcripts in the background.

A save isn't confirmed first, because friction is what makes notes go
unsaved. zelk's hosts allow the memory tools, and a rule you wrote to ask or
deny them still wins. Every save is visible, because the tool result says
where the note went, and a failed write is a failed result with its reason.

**By you.** In the terminal UI, **Remember…** (`/remember`) asks for the
text and whether it is for this project or every project, then saves it as
a note you authored. `zelk memory` lists, shows and forgets notes from a
shell. The [memory guide](/cli/memory/) covers both.

**By a host.** A Go host can put notes in the store itself, with `Put`, as
its own author. Every route goes through the same checks.

The store validates on write. It refuses a bad name or type, an oversized
body, and any text that looks like a credential: a private key block or a
token with a well-known prefix. The note is refused, not redacted.

## Trust

Memory is attributed data, never policy.

- **It can't authorize.** Permissions, tool sets and approvals come from the
  host's policy and from you. Nothing in memory reaches them, so a note
  that says "always allow `rm -rf`" changes no decision.
- **Precedence is stated.** The frame tells the model that your current
  message wins over a note, that the host, the system prompt and the
  project instructions win over a note, and that between two notes a
  correction replaces the one it corrects. A note that reads as a command to
  ignore rules, reveal secrets or change permissions is to be disregarded.
- **A note is a point in time.** Each note is shown with its age. A note
  that names a file, function, flag or command is checked against the
  workspace before the model relies on it, and when what it observes
  disagrees, the observation wins and the note is corrected.
- **Provenance is kept.** Each note records its author and source, so notes
  from one session can be found and removed together.

No note is dropped for who wrote it. A note the model wrote is as visible as
one you did, and its author and date are shown with it so it can be weighed.

## Forgetting

A note is forgotten when the model calls `MemoryForget`, when you forget it
from `/memory` or `zelk memory forget`, or when you delete its file. It is
gone from the store, so it is in no later view, search or session, and
forgetting survives a restart. An expiry is gentler: after it, a note is no
longer retrieved, but it stays in the store until it is renewed or
forgotten.

What was already sent to the model, and the session logs that recorded it,
are kept, and deleting the session is their control. A session's own
recorded view still holds a forgotten note until its next compaction. The
forget's result tells the model not to use it, and compaction summaries are
told not to carry it forward.

## Where to go deeper

The [memory contract](/internals/memory/) gives the exact note format, the
store's conflict rules, the view's order and budget, and how each promise is
tested. The [memory guide](/cli/memory/) covers the commands and the files,
and `-no-memory` turns memory off. In Go,
[`WithMemory`](/reference/go/zelk/#withmemory) gives an agent a store and
a project, [`WithMemoryLimit`](/reference/go/zelk/#withmemorylimit) sets the
view's limit, and the [`memory` package](/reference/go/memory/) holds the
notes and stores.
