fix(memory): rank the whole history and keep relevant recent records

- Search scored only the newest 5,000 matching records, so an old record
  behind enough newer chatter was never found. Both passes (word rarity counts
  and the score) now run in SQLite over every record in scope and only the
  best rows come back.
- context() excluded the recent window from search and then cut that window
  for the budget, so a relevant recent record could vanish. Search now covers
  the recent window, matches keep their half of the budget, and a matched
  record is removed from the recent block so it appears once.
- Matching used SQLite's ASCII-only lower() against Python's lowercasing.
  Stored text gets a folded column (NFKC + casefold) and query words are
  folded the same way, so case is ignored in every script.
- Prompt label "Records related to this question" (matches may now be recent);
  eval re-run 3x on qwen2.5:7b, all pass. Spec, memory.md and roadmap updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 12:50:37 +09:00
parent 54a465948a
commit 7a288129de
6 changed files with 125 additions and 66 deletions

View File

@@ -50,19 +50,23 @@ Setting `since` (default none, meaning the whole history) drops records older th
- `recent(asker, now, scope, minutes=30, max_records=20)`: records in scope newer than `now - minutes`,
at most the newest `max_records`, oldest first.
- `search(asker, query, scope, limit=5, exclude=ids)`: the records in scope that best match the
query. Query words are runs of Unicode word characters of 2 or more characters. A record matches
a word when its text contains the word, or a prefix of it that keeps at least 60% of the word and at
least 2 characters (so an inflected form or an attached particle still finds the stem: "watching"
finds "watched", "영화를" finds "영화"). The newest 5,000 matching records are scored. A word scores
its matched length divided by its own length, weighted by how rare the word is in scope (inverse
document frequency over the candidates). Ties go to the newer record. Records in `exclude` (the
recent window) are skipped so nothing is sent twice.
query, over the whole history in scope (nothing is cut by age or count before scoring). Stored
text and query are folded the same way (Unicode NFKC, then case folding) so matching ignores case
in every script ("МОСКВА" finds "москва", "STRASSE" finds "Straße"). Query words are runs of
Unicode word characters of 2 or more characters. A record matches a word when its text contains
the word, or a prefix of it that keeps at least 60% of the word and at least 2 characters (so an
inflected form or an attached particle still finds the stem: "watching" finds "watched", "영화를"
finds "영화"). A word scores its matched length divided by its own length, weighted by how rare the
word is in scope (log(1 + matching records / records matching that word)). Ties go to the newer
record. Records in `exclude` are skipped.
- `context(asker, query, now, scope, ...)`: `recent` plus `search`, formatted one record per line as
`[YYYY-MM-DD HH:MM] name: text` (local time of the host), and cut to the size budget
(default about 4,000 tokens, estimated as UTF-8 bytes divided by 3). Search matches may use at most
half the budget (lowest score cut first), so a long recent window cannot crowd out the record the
question is about; the recent window gets the rest (oldest cut first). Returns the two blocks separately so the
prompt can label them.
(default about 4,000 tokens, estimated as UTF-8 bytes divided by 3). The search covers the recent
window too, so a recent record the question is about is protected like any other match. Search
matches may use at most half the budget (lowest score cut first), so a long recent window cannot
crowd out the records the question is about; the recent window gets the rest (oldest cut first).
A record that is a match is not repeated in the recent block. Returns the two blocks separately
so the prompt can label them.
## Writes and maintenance