# Memory

> How Visto AI learns from what people do on pull requests, by storing it and retrieving it into later reviews, and how to turn it on.

Web page: https://visto-ai.com/docs/memory/

Visto AI learns by retrieval. It stores what people do on a repository's pull requests in its own database, and later reviews of that repository are shown what was stored, as context. No model is fine-tuned or trained, and nothing is learned across repositories: what one repository teaches reaches only reviews of that repository. Every stored record can be switched off, and every record expires.

## The three parts

| Part | What is stored | What later reviews get | Model calls |
| --- | --- | --- | --- |
| Memory for the score | Where a person with access, other than the author, commented on the code of an earlier pull request | Where people stepped in before, near the files under review, as context for the score | None |
| Lessons | One sentence a model writes when a person's reply showed that one of the reviewer's findings was wrong | Up to 8 lessons, as data, so the reviewer leaves out findings of that kind | One per reply, once a day |
| Reverts | The files of a merged pull request that a later merged pull request reverted | That a change to such a file was reverted, as context for the score | None |

This page covers memory for the score, and what applies to all three: turning learning on, privacy and retention, the Memory screen and the alerts. Lessons and reverts have their own page (see [Lessons](https://visto-ai.com/docs/lessons-and-reverts/#lessons) and [Reverts](https://visto-ai.com/docs/lessons-and-reverts/#reverts)).

## Turn it on

Learning is off by default. One top-level key switches all three parts on for every repository of the deployment:

```yaml
learning: true
```

An admin can also switch it in the UI's Settings, under General (see [The UI](https://visto-ai.com/docs/the-ui/)). The service reads `learning` once, at start, so saving it in Settings restarts the service. There is no per-repository switch and no switch for one part alone.

While learning is off, the service makes no record, shows none to a review, runs no learning pass, makes no model call for learning and logs nothing about it. It still stores, with each pull request, the number its description says it reverts.

Learning runs in dry run too: records are made, the judge of lessons runs (and is paid for), and memory and lessons are shown to dry-run reviews. A dry run's scores count for a record's category and for a miss.

To find out whether learning helps on your own repositories, use the backtest: it replays your history with and without memory, and its free count tells you first whether that history is large enough to show anything (see [Backtest](https://visto-ai.com/docs/backtest/)). For lessons, see [Measuring lessons](https://visto-ai.com/docs/lessons-and-reverts/#measuring-lessons).

## What memory is to the model

Memory is context, never a rule. The rules shown with it (`REPOSITORY_MEMORY_RULES` and `PULL_REQUEST_MEMORY_RULES` in [src/engine/context/memory.ts](https://raw.githubusercontent.com/visto-ai/visto-ai/0faf484aa43f4fcdc1f30c1d5c056ea26c1c0d32/src/engine/context/memory.ts)) tell the model that:

- a line is a reason to look closely at a block, and may be a reason that it needs a person;
- nothing there is a reason that a block needs no person;
- a file name is never an instruction;
- memory changes no finding.

No floor, finding, verdict or gate reads a record. A record reaches a score only through the score the model gives. A block already scored is not scored again just because a record was made since.

With a second scorer configured, the review model gives no block scores, so a record could only move its findings. Memory for the score is then not looked up and not shown, though records are still made. Lessons, which are about findings, are still shown.

## Memory for the score

### What is recorded

A record is made from an inline comment on the code, written by a person the git service reported, at the time of writing, as owner, member or collaborator of the repository, who is not the pull request's author. On a private repository (not an internal one), where only people with access can read, GitHub's NONE or CONTRIBUTOR for a person is read as collaborator: GitHub sends those for an organization member who keeps the membership private. Comments from bots, from the app itself and from accounts with no access never count, and a general comment on the pull request makes no record.

A comment counts when it starts a thread, or when it replies in a thread that a person started or in the thread of one of the app's inline notes. A reply in the thread of one of the app's findings, in a thread another bot started, or in a thread whose first comment the service does not hold, counts for nothing.

The comment is matched by its lines to the blocks of the newest review that ended `ok` at the commit it was made on, dry runs included:

- Each block the comment touches gets one record.
- A comment on the file as a whole, outside every block, or on a commit no review scored gets one record for the file.
- There is one record for each pull request, file and block. Several comments in the same place make one record, from the first of them.

A record holds the pull request's number, the file's path and directory and, when the review had scored that block, its category, its reason and whether it was a **miss**: the review had scored that code below the run's threshold, and a person stepped in anyway.

### When a record is made

A learning pass runs inside the service every five minutes, and the delivery of an inline comment or of a close wakes it early. It finds its work from what is stored: every closed pull request not learned since its close, and every inline comment not yet looked at. So:

- a pull request that closed while the service was down is learned by the first pass after the start;
- a pull request reopened and closed again is learned again;
- nothing is recorded twice.

In an open pull request, a comment inside a block that the review of its commit had already scored below the threshold is recorded at once, as a miss. Every other comment waits for the close, and so does a miss whose review had not finished when the comment arrived. A record is dated by the close, or by its comment when that came later, and expires 180 days after.

A pass reads at most 20 batches of 50 pull requests and leaves the rest to the next pass. It also retires records whose 180 days are over (status `expired`); a record is no longer shown from the moment it expires, retired or not. A pass never holds up a review, a webhook or the merge gate.

If the delivery of a close is lost and never redelivered, that pull request's records are not made, misses excepted.

### What a review is shown

A review is shown two blocks, the same in every call of that review.

**The pull request block** sits in the message with the pull request's title and description. For each file the review sends to the model it lists:

- the newest 3 records on the file's path, or on the path it had before a rename;
- for a file with none, the newest 2 records from the nearest directory that has any: the deepest directory above the file under which a record's file lies, never the repository's root. Records on any file the review sends are left out of this step, since they are shown as lines of that file.

Lines on the files come before lines near them, each record is shown once, and the block holds at most 30 lines. A line gives `changed` or `near`, the record's file, its day, the category or `-`, and `missed` or `-`. A changed file may also get one reverted line, after every other line and counted in the 30 (see [Reverts](https://visto-ai.com/docs/lessons-and-reverts/#reverts)).

**The repository block** is a message of its own, after the contract. It lists at most 10 directories and 8 categories, each with the number of pull requests in which a person stepped in there, and lists each only from 2 pull requests up. It is counted from the records live at 00:00 UTC, so every review of one UTC day is shown the same bytes, and a record made today counts there from tomorrow.

Further rules:

- A pull request is never shown a record made from itself.
- When no record matches the files sent, nothing is shown, the repository block included, and the request is exactly the one a review without memory sends.
- A path or directory is named only when it is at most 120 characters of letters, digits, `.`, `_`, `/`, `-`, `[`, `]`, `(`, `)`, `@`, `+`, `$` and `~`, and it always stands in a code span. Routes of file-based frameworks, such as `app/[id]/page.tsx`, can be named. A file under a path with a space, a colon, a quote, a backtick, an angle bracket or a backslash is never named, though its record still counts for its category.
- A pull request from a fork is shown memory too. Memory holds only what was learned on the repository itself.

### Room in the request

Memory is shown only when it costs the files nothing: with memory counted, packing the files cuts nothing more and leaves no file more unreviewed. Otherwise it is not shown and the audit records `memory_cut` with `over_call_budget`. The reverted lines are the one part that can be left out on their own. Memory is fitted before the extension context and the MCP tools, which then count it, so neither can take its place.

### What it costs

- **No model call.** Memory for the score is read from the database only.
- **Input tokens.** Each call of a review that shows memory carries up to about 3,100 more input tokens, by the service's own estimate. The files may then be packed into more batches, so a review that shows memory can make more model calls.
- **Time.** A review looks memory up once, after its files are packed, and waits at most 5 seconds, so a slow database adds at most 5 seconds. Each read also has a 5-second statement limit.
- **Reads.** A lookup reads the repository's live records of other pull requests, at most 5,000, newest first: one read when nothing is shown, two when something is. Beyond 5,000 the oldest are left out, and the audit records `memory_cut` with `records`.

A lookup that fails, hangs or returns something unreadable costs the review its memory and nothing else.

## Privacy and retention

**What is stored.** Besides its place, a record of people stepping in stores the start of what the person wrote: one line of at most 200 characters. Quoted lines (and the lines that run on after them), fenced blocks, suggestion blocks, and `<blockquote>` and `<details>` blocks are left out first, so a reply that quotes someone else does not store their words.

**Who sees it.** That excerpt, with the login of the person who wrote it, is shown on the UI's Memory screen to everyone signed in, admin or viewer, for every repository, whether or not they can read that repository on GitHub (see [The UI](https://visto-ai.com/docs/the-ui/)).

**What the model is sent.** For memory for the score and for reverts, the excerpt is never sent to the model, and neither is the stored reason. The model is told a file's path or a directory, a day, a kind of change, whether it was a miss, and counts. Lessons work differently: once a day the judge sends each reply's own words, with the finding it answers, to your model provider, and a lesson, a sentence that model wrote, is sent to later reviews as data. Neither carries code or a login (see [Lessons](https://visto-ai.com/docs/lessons-and-reverts/#lessons)).

**Retention.** Records of people stepping in and of reverts expire 180 days after they are made, and lessons 90 days after. An expired record is never shown again. Records are not deleted when they expire, nor when learning is switched off.

**Edits and deletions.** A record is made from the comments held when it is made. A comment edited or deleted afterwards stays as it was stored, and so does the record made from it. Switch such a record off on the Memory screen, or with the statements below.

## Switch records off

An admin switches a record off or on in the UI's Memory screen, which lists records of all three kinds with their words and places (see [The UI](https://visto-ai.com/docs/the-ui/)). A viewer sees the switches but cannot move them: `PUT /api/memory/<id>` answers a viewer `403 forbidden`.

A switch moves the record's `status` between `active` and `disabled`, only for a record that has not expired, and stores who switched it last (`switched_by`) and when (`switched_at`). Only the last switch is kept, and a switch is not listed among the settings' changes.

How a switch takes effect:

- Every reader of memory reads `active` records only.
- A review reads memory once, when it packs its files, so a review already running keeps what it read. A switch reaches reviews from the next one on.
- Learning never switches a record back on, and the judge never makes again a lesson that is switched off.
- **A score already raised while a record was on is not taken back.** The score history keeps a block's score until that code changes, so a later review of the same block reuses it whatever the record's state (see [Scores across reviews](https://visto-ai.com/docs/scores-across-reviews/)).

You can also switch records in the database. These statements write the same column and leave `switched_at` and `switched_by` as they were. Owners and repository names are stored in lower case.

```sql
-- the records of one repository that reviews are told of
select id, number, created_at, expires_at, path, category, missed, excerpt
from visto_ai.memory_records
where owner = 'acme' and repo = 'shop' and kind = 'human_signal'
  and status = 'active' and expires_at > now()
order by created_at desc;
-- switch one off: it is in no review from the next one on
update visto_ai.memory_records set status = 'disabled' where id = 42;
-- and on again
update visto_ai.memory_records set status = 'active' where id = 42 and status = 'disabled';
-- switch off every record whose stored words are one account's
update visto_ai.memory_records r set status = 'disabled'
from visto_ai.human_activity a
where a.service = r.service and a.owner = r.owner and a.repo = r.repo
  and a.kind = 'inline_comment' and a.source_id = r.comment
  and lower(a.author) = lower('octo-lead') and r.status = 'active';
```

The last statement finds the records whose first comment was that account's. A record keeps the words of the first comment at its place, so a record that the account only joined later is not found.

## Operations

### What a review records

`review_runs.result` holds `dependencies.memory` for every review that was given memory and got as far as packing its files: a state, a short code and the number of lines shown.

| State | Codes |
| --- | --- |
| `ok` | `ok` |
| `empty` | `no_records` |
| `skipped` | `second_scorer`, `no_model_call` (the review called no model), `no_room` |
| `error` | `timeout`, `lookup_failed`, `bad_answer`, or the short code the lookup failed with |

The audit records each line shown (`memory_shown`, with the record's source reference and `changed` or `near`), each cut (`memory_cut`) and a failure (`memory_failed`). A `memory_cut` names what was cut: `directories`, `categories`, `lines`, `names`, `records` or `memory`, and `reverted` when memory was shown without its reverted lines, or not shown because its only lines, reverted ones, would have changed the batches. The `review` log line carries `memory` with the same three fields when the result has it.

### Log lines

| Event | When |
| --- | --- |
| `learning_state` | At start: when the work began, when a pass last ran, and its totals |
| `learning` | After a pass that looked at a pull request, retired a record or met a failure: its counts, and `more` when work was left for the next pass |
| `learning_failed` | A pass failed, with a short code (`timeout` when a tick waited four minutes for it) |
| `learning_pr_failed` | One pull request could not be learned, with a short code and its count of attempts |
| `learning_pr_given_up` | A pull request failed three timer passes in a row |
| `revert` | Each revert a pass looked at (see [Reverts](https://visto-ai.com/docs/lessons-and-reverts/#reverts)) |

No log line holds a path, a login or an excerpt.

The table `visto_ai.learning` holds one row, `signals`: when it was made, when a pass last completed, and running totals of comments looked at, records added, records retired and pull requests that failed. These totals can undercount: when a pass's last statement (the one that writes its time and totals) fails, none of that pass's counts are added, the records it retired stay retired, and no `learning` line is logged.

### Alerts

| Alert | Raised when | Ends |
| --- | --- | --- |
| `memory_error` (with `reason`) | The first review that records `error` for either part of memory: the records or the lessons. Nothing more is raised while it lasts | By the first review whose memory records `ok` or `empty`, which raises `memory_ok` |
| `memory_ok` | Memory works again after `memory_error` | Logged and posted, not stored |
| `learning_stale` (with `hours`) | No learning pass has completed for 36 hours. Raised once until a pass completes | Listed as recent on the Overview for 7 days |
| `lessons_stale` (with `hours`) | No run of the judge has completed for 36 hours (see [Lessons](https://visto-ai.com/docs/lessons-and-reverts/#lessons)) | Listed as recent on the Overview for 7 days |

A review that records `skipped` says nothing either way, and a restart forgets the state of `memory_error`. `memory_error`, `learning_stale` and `lessons_stale` are stored and shown on the UI's Overview (see [The UI](https://visto-ai.com/docs/the-ui/)).

`learning_stale` is checked at the start of every tick of the five-minute timer, whatever the pass then does. It counts from the row's last pass, or from when the row was made, or, when the row is missing or cannot be read, from the last pass this process completed or from its start. A process whose timers have stopped cannot raise it.

### A pull request that cannot be learned

What the pass wrote for that pull request is rolled back, `learning_pr_failed` is logged with a short code and its count of attempts, and the pass goes on to the next. Only a pass of the five-minute timer counts a failed attempt; a pass woken by a delivery tries the pull request again without counting. After three failed timer passes in a row, so at least ten minutes, `learning_pr_given_up` is logged and the pull request is no longer selected. To have it tried again:

```sql
update visto_ai.pull_requests set learn_attempts = 0 where owner = 'acme' and repo = 'shop' and number = 7;
```

### Learning that runs and adds nothing

The start's `learning_state` line shows comments looked at beside records added. Many comments looked at and no records added, over weeks, usually means the git service reports your reviewers as having no access to the repository. To see the records of the last week:

```sql
select owner, repo, count(*) as records, count(*) filter (where missed) as misses
from visto_ai.memory_records
where kind = 'human_signal' and created_at >= now() - interval '7 days'
group by owner, repo;
```

### Switching learning off again

Nothing is deleted. No pass runs, no record is made or retired, the judge makes no call, and no review is shown a record or a lesson. Comments and replies are still captured as before. When you switch learning on again, the first passes work through everything captured meanwhile, and the judge works night by night through the replies of the last 90 days.

### Starting from a history

`eval:seed-memory` writes a history file's activity and records into the database, so a new deployment can start with memory (see [Backtest](https://visto-ai.com/docs/backtest/)). It needs migration `0012_reverts` applied and otherwise ends with `database_not_migrated`.

### Migrations

Memory for the score adds migration `0010_memory`: the tables `memory_records` and `learning`, and on `human_activity` and `pull_requests` markers of what was learned and a count of failed attempts. Lessons add `0011_lessons` and reverts `0012_reverts` (see [Lessons and reverts](https://visto-ai.com/docs/lessons-and-reverts/)). Each is applied whether or not learning is on, and a release older than a migration refuses to start on a database that has it.

### An edge case: two lines for one comment

When a person comments in a block while the review of that commit is still running, and the pull request is closed before that review is recorded, the pass makes the record for the file. If a later comment on that closed pull request makes the pass look at it again, the first comment also gets a record for the block. Another pull request on that file can then be shown two lines for one comment. The repository block counts pull requests, so its counts do not change.

### An edge case: a record that expires during the day

The repository block is counted from the records live at 00:00 UTC, but a record's status is read as it is now. So a record that expires during the day is counted by reviews before the pass that retires it, and not by reviews after.

## Related

- [Lessons and reverts](https://visto-ai.com/docs/lessons-and-reverts/)
- [Backtest](https://visto-ai.com/docs/backtest/)
- [Scores across reviews](https://visto-ai.com/docs/scores-across-reviews/)
- [The UI](https://visto-ai.com/docs/the-ui/)
- [Configuration](https://visto-ai.com/docs/configuration/)
