# Memory and undo

Source: https://www.zerd.si/docs/memory-and-undo

zerd learns your preferences from your corrections without any setup, and `/undo` puts every file back the way it was before the run.

## Memory

In an interactive session, zerd learns from what you say and from runs that verified. You don't have to run a command or write a rule.

After each message you send, zerd asks a model in the background, so your reply isn't held up. The model reads your new message alongside the request and answer it follows. It writes one rule if you:

- redirected it ("no, not that", "I meant the header")
- confirmed an approach ("yes, exactly like that")
- stated a preference ("always use pnpm")

A new unrelated task, a question or a thank-you gets no rule.

When it learns something, you see:

```text
Learned: use pnpm, never npm, in this repo (saved to AGENTS.md; /forget removes it)
```

### Where lessons live

The rule is saved in your own `AGENTS.md` under `## Learned by zerd`. You can read it, edit it and commit it, so your team gets it too. The next session loads it as an ordinary instruction, newest first, capped at 30 per repo.

```mermaid
flowchart LR
    S1["/learn rule"] --> L
    S2["You reject an action<br/>and say why"] --> L
    S3["A correction: 'no, not that'<br/>or 'yes, like that'"] --> L
    S4["Past Claude Code sessions<br/>learn-import"] --> L
    L["Lessons<br/>max 30 per repo"] --> AG["Your AGENTS.md<br/>## Learned by zerd"]
    L --> PR["Next session's prompt"]
    V["A verified run:<br/>request words to files changed"] --> EH["Edit history"]
    EH --> SR["search ranks files that fixed<br/>similar requests higher"]
```

### Managing lessons

- `/learn <rule>` adds one by hand.
- `/forget N` removes one, from `AGENTS.md` too.
- Duplicates and notes under four words are dropped.
- Rejecting an action with a reason ("not like that, use the helper in `utils/`") becomes a lesson immediately.

Scripted runs (`zerd "..."`, `--json` and `--rpc`) never write lessons, so CI and benchmarks can't change your repo.

### Edit history

A run that ends `verified` records the words of your request and the files it changed. Later, `search` ranks files that fixed similar requests higher. A failed run adds nothing.

### Importing old sessions

```bash
zerd learn-import --repo .
```

This mines your old Claude Code sessions for rules. Each one is checked against what `AGENTS.md` already says, so only new ones are added.

## Undo

Before each request, zerd saves a snapshot of the working tree as a commit object under `refs/zerd/backup`. It uses no stash and changes no index.

- `/undo` restores what the last request changed and removes the files it created. Files that were untracked before are never deleted.
- `/rewind` goes back up to 20 requests.
