fix: voice answers no longer wait behind chat answers; late chat answers are dropped

- The voice worker writes voice and chat replies in separate lanes (one
  of each at a time), so a chat answer being written never holds up a
  voice answer. Tests: a voice reply finishes while a chat reply is still
  running; replies of one kind stay one at a time.
- A chat answer arriving 5 minutes or more after it was asked is not
  posted, checked when it arrives.
- Specs: the lanes, and that Strata needs --parallel 2 for both lanes'
  LLM calls to run at once until the app starts Strata itself (M5).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 22:42:30 +09:00
parent a70ccda37e
commit 2f69557e1b
5 changed files with 85 additions and 10 deletions

View File

@@ -173,9 +173,11 @@ impl<V: Voice> Conversation<V> {
CoreEvent::SpeechStart(h) => self.policy.utterance_open(h.speaker_id),
CoreEvent::Chat(m) => self.on_chat(m, now),
CoreEvent::ReplyText { id, text, failed } => {
if let Some((channel, message, _)) = self.chat_answers.remove(id) {
debug_log!("conversation", "chat answer {id} chars={} failed={failed}", text.chars().count());
if !failed && !text.trim().is_empty() {
if let Some((channel, message, asked)) = self.chat_answers.remove(id) {
let late = now >= asked + CHAT_ANSWER_KEPT;
debug_log!("conversation", "chat answer {id} chars={} failed={failed} late={late}",
text.chars().count());
if !failed && !late && !text.trim().is_empty() {
self.voice.send_reply(channel, message, text);
}
}
@@ -625,6 +627,15 @@ mod tests {
assert_eq!(c.missed().count(), 0, "a chat message never takes the voice turn");
}
#[test]
fn a_chat_answer_after_its_keep_time_is_not_posted() {
let (mut c, f, t) = chat_conv(CallSettings::default());
c.on_event(&chat(CH_WAKE, 5, A, false, "자비스 안녕"), t);
let Did::ChatAnswer(id, ..) = f.take().remove(0) else { panic!() };
c.on_event(&CoreEvent::ReplyText { id, text: "안녕하세요.".into(), failed: false }, t + CHAT_ANSWER_KEPT);
assert!(f.take().is_empty(), "too late: the conversation has moved on");
}
#[test]
fn end_times_come_from_the_finals_wait() {
let (mut c, f, t) = conv(PolicyKind::InOrder, CallMode::WakeWord);

View File

@@ -132,10 +132,13 @@ never takes the voice turn and never goes through the response policy.
- Only a request is answered: a stop in chat ("자비스 멈춰") is not a question and gets no answer.
- The brain answers through `reply` with `via: "chat"` and `speak: false`; the answer is posted in the
same channel as a Discord reply to the message (so the history records it as an answer to that
person), pinging nobody. A failed or empty answer posts nothing. Answers waited for over 5 minutes
are forgotten.
- Known limit: the worker writes one reply at a time, so a voice answer waits while a chat answer is
being written (the LLM's parallel requests for "answer everything" channels are M5/M6 settings).
person), pinging nobody. A failed or empty answer posts nothing. An answer that arrives 5 minutes or
more after it was asked is not posted.
- The worker writes voice and chat answers in separate lanes (`voice_worker.spec.md`), so a voice
answer never waits behind a chat answer. For the two to run at the same time the LLM must take two
requests at once (`docs/v2/conversation.md`, "LLM 동시 요청 수"): until the app starts Strata itself
(M5), Strata is started with `--parallel 2` when some channel answers everything; with
`--parallel 1` the two lanes' LLM calls take turns at Strata.
## The conversation loop

View File

@@ -52,7 +52,9 @@ class VoiceWorker:
self.tick_s = tick_s
self.history = history # jarvis.memory.history.HistoryStore or None
self.brain = brain
self.brain_lock = asyncio.Lock() # one reply at a time
# One reply at a time per lane: voice answers never wait behind a chat answer (conversation.md:
# with an "answer everything" chat channel the LLM takes two or more requests at once).
self.brain_lanes = {"voice": asyncio.Lock(), "chat": asyncio.Lock()}
self.chat_timeout_s = chat_timeout_s
# chat_action ids are unique across connections and worker runs, so a late answer from an
# earlier connection (or an earlier worker) never completes a newer action.
@@ -545,7 +547,7 @@ class Session:
async def _reply_inner(self, rid: str, user: str, text: str, via: str, speak: bool) -> None:
w = self.worker
channel = self.voice_channel
async with w.brain_lock:
async with w.brain_lanes[via]:
if self.closed:
return
now = time.time()

View File

@@ -218,7 +218,9 @@ which carry Discord's own time.
or when it was said, or who heard it. A final said outside a voice channel is not stored.
- Chat messages are stored only as given; the app sends only registered channels.
- `reply` asks the brain to answer `user`'s `text`. The worker builds the history context for that
user (their scope only) and runs the reply engine on a worker thread, one reply at a time.
user (their scope only) and runs the reply engine on a worker thread, one voice reply and one chat
reply at a time: the two kinds have separate lanes, so a voice answer never waits behind a chat
answer (`app/src/conversation.spec.md`, chat calls).
`reply_text` carries the answer (`failed: true` with empty text when the brain gave none). A voice
answer (`via: "voice"`) is stored as a `reply` record to the user in the current channel; a chat
answer is stored when the app reports the sent message as `chat_message` with `to`. With

View File

@@ -646,3 +646,60 @@ def test_a_ranking_keeps_left_out_requests_in_order_and_fails_without_numbers(mo
assert judges.rank_requests("u", "m", ["a", "b", "c"]) == [2, 0, 1]
monkeypatch.setattr(judges, "call_llm_direct", lambda *a, **k: "none of them")
assert judges.rank_requests("u", "m", ["a", "b"]) is None
@pytest.mark.unit
def test_a_voice_answer_does_not_wait_behind_a_chat_answer(store):
import threading
release = threading.Event()
running = []
def answer(user, text):
running.append(text)
if text == "chat question":
release.wait(10)
return f"answer for {text}"
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="reply", id="c1", user="100", text="chat question", via="chat", speak=False)
await asyncio.sleep(0.1)
await send(ws, type="reply", id="v1", user="200", text="voice question", via="voice", speak=False)
seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "v1")
release.set()
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "c1")
return seen
seen = asyncio.run(with_worker(body, store=store, brain=FakeBrain(answer)))
assert seen[-1]["text"] == "answer for voice question"
assert not any(isinstance(m, dict) and m.get("id") == "c1" for m in seen), "the chat answer was still running"
@pytest.mark.unit
@pytest.mark.parametrize("via", ["voice", "chat"])
def test_answers_of_one_kind_are_written_one_at_a_time(store, via):
import threading
active, peak = [0], [0]
lock = threading.Lock()
def answer(user, text):
with lock:
active[0] += 1
peak[0] = max(peak[0], active[0])
import time as _t
_t.sleep(0.05)
with lock:
active[0] -= 1
return "ok"
async def body(url):
async with connect(url) as ws:
await hello(ws)
for i in range(3):
await send(ws, type="reply", id=f"r{i}", user="100", text="q", via=via, speak=False)
for i in range(3):
await recv_until(ws, lambda m, i=i: isinstance(m, dict) and m.get("id") == f"r{i}")
asyncio.run(with_worker(body, store=store, brain=FakeBrain(answer)))
assert peak[0] == 1