Files
javis_bot/src/jarvis/memory/history.spec.md
javis-bot 6fc3ad3d57 feat(worker): answer traces stored from the question on and filled in as the answer is made
A trace is now kept the moment the request arrives (at the time it was
asked, ms -1), its steps are stored as the engine adds each one, and it
ends with the answer. Traces a killed run left unfinished are closed at
start. The 대화 screen reads a trace still being made as pending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:03:45 +09:00

109 lines
7.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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.
## Traces
How each answer was made, for the management app's 대화 screen (`app/src/admin.spec.md`,
"Conversations"). A trace is never a record: no read above returns it and it never reaches a prompt.
- One per `reply` the brain ran, kept from the moment it was asked so the screen can show it while
the answer is made: `start_trace(via, channel_id, user_id, question, ts)` (`via` `voice`, `chat` or
`task`; `channel_id` where it was asked, none for a task; `ts` when it was asked) stores it with no
answer and `ms` -1, `trace_steps(id, steps)` stores the steps so far, and
`finish_trace(id, answer, steps, ms)` the end (`answer` empty when the brain gave none; `ms` how
long it took). `close_unfinished_traces()`, which the host calls at start, ends with no answer the
traces a killed run left at -1. `steps` is a JSON list in the order they happened:
- `{"kind": "records", "recent": [...], "matches": [...], "notes": n}`: the history lines and the
number of personal notes the brain was given (the voice worker adds it first),
- `{"kind": "tools", "names": [...]}`: the tools the turn was offered after routing,
- `{"kind": "plan", "steps": [...]}`: the planner's steps, when it ran,
- `{"kind": "think", "text": "..."}`: the model's thinking in one round, when thinking is on
(at most 8,000 characters),
- `{"kind": "tool", "name", "args", "ok", "result"}`: a tool call and the start of its result or
error (at most 2,000 characters), or `"skipped": "not_allowed" | "duplicate"` with `ok` false
for a call the engine refused.
The engine writes them through `turn_state.trace` (`reply/turn_state.py`), which does nothing when
no trace list was set (v1, evals); the voice worker's list stores the steps as each is added.
- `traces_of(person)`: the person's traces, oldest first. `forget_person` deletes them too.
- `purge_traces_older_than(ts)`: the voice worker keeps traces 30 days (`TRACE_KEEP_DAYS`) and
removes older ones when it starts. `purge_older_than` removes old traces with the records.
## 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).