# 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, 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). 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 - `add_voice`, `add_reply`, `add_chat`, `edit_chat`, `delete_chat`. - `presence_join`, `presence_leave`, `presence_close_channel`, and `close_open_presence(ts)`, which the host calls at start: a run that was killed never closed its intervals, and nobody is heard until the app reports who is there again. - `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).