Skip to content
49 changes: 48 additions & 1 deletion docs/concepts/memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,8 @@ The links are read where the code is:
such a hit's line, with the same note as a `memory.for_file` line; `memory.ask` notes
the memories whose code moved, changed or went.
- `namespace.validate` lists memories whose code changed, was not found again or is
gone, in the repositories the caller may read.
gone, in the repositories the caller may read, and the edits that bring a memory's
text up to a rename its link followed.

A memory with no link reads exactly as one whose code is current. `namespace.validate`
counts the served memories that name code (a backticked or qualified name, a call, or
Expand All @@ -603,6 +604,52 @@ linked, is not counted. How far each repository's links trail its
index is a gauge, and a health warning when it lasts
([monitoring](../operations/monitoring.md#what-to-alert-on)).

A `moved` link is at its code, and the memory's text may still name the old definition or
path. `namespace.validate` lists each memory whose text does under the `info` finding
`code_rename_edits`, with either the `edits` its text allows (replace `old` with `new` at
characters `start` to `end`) and the `content_sha256` they are of, or the one `reason` no
edit is offered. The finding's `totals` count the memories by `edits` and by reason. Loom
writes nothing: to apply an edit, read the sentence it changes, then call `memory.update`
on the key with the revised text and `if_match_sha256` set to the listed hash. That is a
revision by the caller, with its conflict check, and it is refused when the memory changed
after the report read it. The link pass then links the revised memory where the code is.

An edit is offered only for a name written as code (a whole backticked identifier, an
`A::b`, or a call written `a.b(` or `name()`) and for a whole repository path, in the
spelling the text uses: `Reader.get` becomes `Reader.fetch`, a full dotted name becomes the
full new one, and `self.get` keeps its receiver. The whole memory gets no edit for the
first of these reasons that applies:

| Reason | The memory |
| ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `not_text` | has a value that is not one string. |
| `link_not_settled` | has a link that is pending, `changed`, `unresolved` or `missing`, that its renamed class carried, or that is hashed under older fingerprint rules. |
| `incomplete` | names more code than the link caps keep. |
| `overloaded` | is linked to a definition the index numbers among several of one name. |
| `quoted_or_literal` | holds the old name or path in a fenced block, a URL, a quoted string or a backticked span that is more than one name. |
| `prose_occurrence` | writes the old name as a bare word of the prose. |
| `old_name_remains` | would still hold the old name or path where no edit reaches: a plain word, a part of a longer name, a longer name written as code that names no definition on the indexed trunk (`conn.read_rows()`), the dotted module the definition left (two segments or more), a path before a line number, or a path whose file still exists. |
| `names_several` | writes one name that two of its links were written against. |
| `receiver_form` | writes the name as `self.name`, `cls.name` or `this.name`, and its `files` do not name the defining file. |
| `old_name_still_defined` | spells the old name in a way that still names a definition on the indexed trunk. |
| `new_name_present` | already holds the new name or path. |
| `dated_claim` | is dated (`valid_to`, `invalid_at` or `temporal_anchor`), or the sentence to edit holds a commit hash, a version tag, a year, a month (`May` only capitalised after another word), or a pull request or issue number. |
| `about_naming` | would be edited in a sentence about the name or the place: it holds `name`, `names`, `named`, `naming`, a word that starts with `renam` or `alias`, `called`, `formerly`, `used to`, `replaced`, `moved from`, `deprecated`, `legacy`, `before` or `previously`. |
| `named_in_key_or_tags` | holds the old name or path in its `memory_key` or a tag. |
| `named_in_files` | lists the old path in `files`, which `memory.update` keeps. |
| `would_unlink` | would lose a link once revised: the new name is not one definition on the indexed trunk, or the revised text no longer names a definition or file it is linked to. |

A memory whose text names no old name or path is not listed: its bare name still holds
after a move, or the link is back where the text says. A sentence ends after `.`, `!` or
`?` and a space (not after a one-letter abbreviation such as `e.g.`), at a blank line and
before a list item; a line break alone does not end one. The words that mark a sentence as
being about the name are English, and `call` or `calls` is not one: it is read as calling
the code. The finding reads the 2,000 memories whose `moved` link was last checked (all
under `keys` when given), in the repositories the caller may read, and no high-sensitivity
memory; a link whose code also changed is listed under `code_links_stale`. Neither the
memory's key, tags, `files`, entities and concepts nor a line number written in its text
is edited.

## Time

| Column | Axis | Meaning |
Expand Down
38 changes: 38 additions & 0 deletions docs/use-cases/memory-linked-to-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ reported commit](#check-a-lesson-after-a-code-edit).
[`namespace.validate`](../reference/operations/namespace.md#namespacevalidate) lists the
memories whose code changed, was not found again or is gone, and the dashboard shows each
memory's links and their state.
- **The edit a rename allows.** After a rename or a move the link follows, the same report
lists the exact token edits that bring the memory's text up to date, or the reason it
offers none. [Bring a lesson's text up to a rename](#bring-a-lessons-text-up-to-a-rename)

![A memory's detail in the dashboard: its linked code, one definition current and one changed since a commit](../assets/screenshots/memory-linked-code.webp)

Expand Down Expand Up @@ -134,6 +137,41 @@ tool selection are not tested.
For removal, held-memory exceptions and model-backed revision, see the
[link policies](../concepts/memory.md#memories-linked-to-code).

## Bring a lesson's text up to a rename

When a definition is renamed or moved with nothing else changed, its link follows it and
reads `moved`. A lesson that wrote the old name, module or path still says it. Loom does
not rewrite it; `namespace.validate` lists what a rewrite would be:

1. Call `namespace.validate` on the memory namespace (with `keys` to check the lessons
one session wrote) and read the `code_rename_edits` finding. Each sample is one lesson
with either `edits` and a `content_sha256`, or a `reason`.
2. For a lesson with `edits`, replace `old` with `new` at characters `start` to `end` of
its text, applying the last edit first so the earlier positions hold. Read the edited
sentence: an edit is offered on the spelling and the index, and whether the claim
survives the swap is yours to judge.
3. Call `memory.update` on the key with the revised text and `if_match_sha256` set to the
sample's `content_sha256`. A lesson that changed since the report returns `409`; run
the report again.
4. After the next link pass the lesson is linked at the new name and reads `fresh`, and
the finding lists it no more.

A lesson with a `reason` gets no edit: the old name is a word of the prose, sits in a
quoted snippet, still names another definition, or the sentence is about the name itself
([every reason](../concepts/memory.md#memories-linked-to-code)). Check it against the code
and rewrite it by hand.

The finding's `totals` count a namespace's lessons by `edits` and by reason: how often a
mechanical edit is offered there, and what stops the rest. Applying the edits without a
person reading them is worth considering only for a namespace whose `totals` come from its
own lessons and where at least 100 listed edits were checked by hand against the lesson
text with none wrong.

The `test_validate_lists_the_edit_a_followed_rename_allows_and_an_update_applies_it`
[integration regression](https://github.com/ClickHouse/loom/blob/main/tests/integration/test_memory_code_links.py)
renames a function, reads the listed edit, applies it with `memory.update` and checks the
link at the new name.

## Compare a code-linked lesson across branches

When trunk and a maintained release branch diverge, a lesson can still apply to one of
Expand Down
1 change: 1 addition & 0 deletions sdk/python/loom/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -18183,6 +18183,7 @@ class NamespaceValidateFinding(TypedDict, total=False):
count: Required[int]
samples: NotRequired[list[dict[str, Any]]]
detail: Required[str]
totals: NotRequired[dict[str, int]]


class ObserveCrystallizeInput(TypedDict, total=False):
Expand Down
Loading
Loading