feat(memory): v2 conversation history store and its context in the reply prompt

- src/jarvis/memory/history.py keeps voice transcripts, Jarvis's answers,
  registered chat channel messages (with edits and deletes) and voice channel
  presence in one SQLite file. Every read is scoped to the asker: own records
  always, chat channels and others' words (only while the asker was in the
  same voice channel) behind settings, plus an optional since and off.
- Search is language-agnostic: Unicode words, prefixes keeping 60% of the
  word, rarer words weigh more, newer wins ties. 100k records search in
  24-61 ms here. context() returns the recent window and the matches cut to a
  token budget, matches at most half so chatter cannot push out the record
  the question is about.
- run_reply_engine(history=...) puts that context into the system prompt
  under "Information shared in previous conversations", fenced as data.
  evals/test_history_context_recall.py: with the block qwen2.5:7b answers the
  stored day in Korean and English (3 of 3 runs), without it the day is unknown.
- Spec history.spec.md, registry entry in CLAUDE.md, reply.spec.md,
  docs/llm_contexts.md, docs/v2/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:45:33 +09:00
parent 6b23a8adc7
commit 54a465948a
11 changed files with 774 additions and 2 deletions

View File

@@ -0,0 +1,75 @@
# Conversation history store spec (v2 M3)
The history store keeps everything said where Jarvis listens: voice transcripts, Jarvis's answers,
and the messages of the registered chat channels. Before each answer the brain asks it for the
records that matter to this question and to this person. Product rules live in `docs/v2/memory.md`
and `docs/v2/privacy.md`; this file is the contract of `src/jarvis/memory/history.py`.
## Principles
- **Only what the asker may see.** Every read takes the asker's user ID and returns only records in
the asker's scope (below). There is no read that ignores scope, except the per-person data view
and delete, which the management app calls for the person shown.
- **Local and quiet.** One SQLite file in the data folder. Logs carry counts and timings, never
record text or names.
- **Language-agnostic search.** Matching uses Unicode word characters and prefixes only, no word
lists, so it works the same for Korean, English or any other language.
- **Whole history is searched, little is sent.** The full history is a search index; only the best
matches and the recent window go to the model, under a size budget.
## Records
| Kind | Where | Author | Extra |
|---|---|---|---|
| `voice` | a voice channel | the speaker (Discord user ID and display name) | |
| `reply` | the voice channel Jarvis answered in (spoken answer) | Jarvis | `to_id`: the user Jarvis answered |
| `chat` | a registered chat channel | the message author (may be Jarvis's own bot user) | `message_id`; `to_id` when it is Jarvis's answer to a user; edits replace the text, deletes remove the record |
Times are Unix seconds (UTC). Text is stored as given; empty or whitespace-only text is not stored.
Presence is stored as intervals: user, voice channel, joined at, left at (open while the user is in
the channel). The app reports joins and leaves for everyone in the channel Jarvis is in, including
the people already there when Jarvis joins (joined at = the moment Jarvis saw them). When Jarvis
leaves a channel, every open interval in it is closed at that moment, because nothing said after
that is heard.
## Scope of an asker
A record is in asker A's scope when any of these holds:
1. **Own record** (always): A is the author, or it is Jarvis's answer to A (`to_id` is A).
2. **Chat channels** (setting `chat`, default on): it is a `chat` record.
3. **Others in the same voice channel** (setting `others`, default on): it is a `voice` or `reply`
record, and A had a presence interval in that voice channel covering its time.
Setting `since` (default none, meaning the whole history) drops records older than it. Setting
`off` (scope "끔" in the app) returns nothing.
## Reads
- `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.
- `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.
## Writes and maintenance
- `add_voice`, `add_reply`, `add_chat`, `edit_chat`, `delete_chat`.
- `presence_join`, `presence_leave`, `presence_close_channel`.
- `records_of(person)` and `forget_person(person)`: the person's own records (authored by them or
replies to them), for the data screen's per-person view and delete. Deleting also removes their
presence intervals.
- `purge_older_than(ts)`: retention. Removes records and closed presence intervals older than `ts`.
The default retention is forever (never called).