feat(app): call detection and the four response policies

- calls.rs: wake words anywhere in the utterance with near matches by
  edit distance on NFKD-normalised letters (Hangul splits into its
  letters), call modes (wakeWord, addressed, everything), designated
  people with both switches, and a voice stop (wake word + stop word).
- policy.rs: first, inOrder, latest and important as an event-driven
  state machine with the time passed in: ordering by end time, waiting
  for an earlier-ended utterance still being transcribed, queue limit,
  interruption, importance ranking only on a tie with a timeout, stop
  dropping the queue, and a reason for every missed request.
- settings: new call and response sections, validated with the key.
- conversation.spec.md documents the contract; registry and roadmap
  updated. Wiring into the Discord core and the brain comes next.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 22:05:50 +09:00
parent b88b28140f
commit 6fc3983a40
10 changed files with 1082 additions and 8 deletions

View File

@@ -0,0 +1,109 @@
# Conversation spec: call detection and response policies (v2 M4)
The part of the app that decides which finished utterances (and, later, chat messages) Jarvis answers
and in what order, and when an answer that is being spoken stops. It sits between the Discord voice
core (`discord_core.spec.md`), which reports what was heard and plays speech, and the voice worker's
brain (`src/voice_worker/voice_worker.spec.md`, `reply`), which writes the answer. The user-facing
design is `docs/v2/conversation.md`; this spec is the contract the code follows.
## Principles
- **Plain types, tested without Discord.** Call detection (`app/src/calls.rs`) is a pure function of
the settings, the speaker and the text. The response policy (`app/src/policy.rs`) is a state machine
driven by events with the time passed in, so every rule is tested with fake times.
- **Language-agnostic.** Nothing matches a language's words in code. Wake words and stop words are
settings; matching works on Unicode-normalised letters, so it behaves the same in any script.
- **No text in logs.** Logs carry speaker IDs, decisions, reasons, counts and timings, never what was
said.
- **Nothing is dropped silently.** Every request the policy does not answer, and every answer it stops,
is reported as missed with a reason (the dashboard's "놓친 요청" list, M5).
## Call detection
Settings (`settings.json`, section `call`; wake words stay in `voice.wakeWords`):
| Key | Default | Meaning |
|---|---|---|
| `voice.wakeWords` | `["자비스"]` | Wake words for voice (also the STT hint). |
| `call.chatWakeWords` | `null` | Wake words for chat; `null` uses `voice.wakeWords`. |
| `call.similarity` | `0.2` | How far a heard word may be from a wake word, as a share of the wake word's normalised letters (0-0.5). `0` needs an exact match. |
| `call.mode` | `"wakeWord"` | `wakeWord`: answer only when called by a wake word. `addressed`: also answer what is judged to be said to Jarvis. `everything`: answer every utterance. |
| `call.designated` | `[]` | Discord user IDs of the designated people. |
| `call.designatedOnly` | `false` | Answer only designated people. |
| `call.designatedNoWakeWord` | `false` | Answer designated people without a wake word. |
| `call.stopWords` | `["멈춰", "그만", "정지", "stop"]` | Said together with a wake word, they stop Jarvis in any policy. |
Normalisation: Unicode NFKD, lower case, and only letters and digits kept (spaces, punctuation and
combining marks dropped). NFKD splits Hangul syllables into their letters, so "자비즈" is one letter
away from "자비스" (of six) while "서비스" is two away.
A wake word is heard when some stretch of the normalised text is at most
`floor(similarity × letters of the wake word)` edits (insert, delete, substitute) from it. With the
default 0.2, "자비스" (six letters) allows one edit. A wake word anywhere in the utterance counts.
Decision for one utterance (`judge(speaker, text)`), in order:
1. Empty text (after normalisation): ignore.
2. `designatedOnly` and the speaker is not designated: ignore.
3. A wake word is heard and the rest of the text, with that stretch removed, starts with a stop word
and has at most 3 more letters ("멈춰", "멈춰 줘", "그만해"): **stop**.
4. A wake word is heard: **request**, `direct` (called by name).
5. `designatedNoWakeWord` and the speaker is designated: **request**, not direct.
6. `call.mode` `everything`: **request**, not direct. `addressed`: **ask** (the brain judges whether it
was said to Jarvis; a yes becomes a request, not direct). `wakeWord`: ignore.
The text sent to the brain is the utterance as heard; the brain treats the wake word as address.
## Response policy
Settings (section `response`):
| Key | Default | Meaning |
|---|---|---|
| `response.policy` | `"first"` | `first` (처음 대화만), `inOrder` (들어온 순서대로), `latest` (마지막 대화만), `important` (제일 중요한 대화만). |
| `response.queueMax` | `5` | `inOrder`: most waiting requests; past it the oldest is dropped. 1-20. |
| `response.collectMs` | `700` | `important`: how long candidates are collected before choosing. 0-3000. |
| `response.settleMs` | `800` | Longest wait for an earlier-ended utterance that is still being transcribed. 0-3000. |
| `response.priorities` | `{}` | `important`: user ID → priority (higher first, default 0). |
Terms:
- A **request** has an id, the speaker, the text, whether it was `direct`, and `ended_at`: when the
utterance ended (the final's arrival minus its `wait_ms`).
- An **answer** runs from the moment the policy starts it until it is done (its speech ended, failed or
was stopped). There is at most one answer at a time: Jarvis has one voice.
- An utterance is **open** from its `speech_start` until its final. An open utterance whose speaker
has stopped speaking (`speaking_end` sent) is **ending**: its final may still turn out to be a call.
Rules shared by every policy:
- **Ordering by end time.** Requests are compared by `ended_at`, not by when their final arrived, so a
long utterance is not pushed back by its transcription time.
- **Settling.** When a request could start now but some other utterance is ending and stopped earlier
than the request's `ended_at` (in `important`: any ending utterance, since it competes on score), the
policy waits for that final, at most `settleMs` from the first waiting request's arrival. A final that
is not a call ends the wait for it. `latest` does not settle: an earlier utterance would lose anyway.
- **Stop.** A stop decision stops the current answer and drops every waiting request, in every policy.
The stopped answer and the dropped requests are missed (`stopped`).
- An answer that is already done is never started again.
Per policy:
| Policy | No answer running | A new request while an answer runs |
|---|---|---|
| `first` | Answer the request that ended first (after settling). | Missed (`busy`). |
| `inOrder` | Answer the earliest-ended waiting request. | Queued by `ended_at`. Past `queueMax`, the earliest-ended waiting one is missed (`queue_full`). |
| `latest` | Answer the latest-ended waiting request; the others are missed (`replaced`). | The current answer stops (missed, `interrupted`) and the new request is answered. A request that ended before the current one is missed (`replaced`). |
| `important` | Collect for `collectMs` from the first request, then answer the best; the other candidates are missed (`lower_score`). | Kept as a candidate. If it beats the current answer, the current answer stops (missed, `interrupted`) and the best candidate is answered at once; otherwise, when the current answer ends, the best of the candidates collected meanwhile is answered and the rest are missed (`lower_score`). |
`important` ranks by (1) priority, (2) `direct`, then (3) the brain's importance ranking, asked for only
when two or more candidates tie on (1) and (2). While the ranking is pending nothing new starts; if it
does not come within 1.5 s, or fails, the earliest-ended of the tied candidates wins. A candidate
beats the running answer only on (1) or (2); the ranking is not asked for an interruption.
## Tests
`cargo test` covers call detection (exact and near wake words in Hangul and Latin, a different word
two letters away, wake word anywhere, stop with and without a wake word, designated combinations,
modes) and every policy rule above with fake times (settling, ordering by end time, busy, queue limit,
interruption, ranking tie, ranking timeout, stop dropping the queue).