fix: a delete question counts as asked only once the answer reached the person

A person's reply stopped counting as "unanswered" as soon as the worker sent
reply_text, so a delete question that could not be spoken (no TTS, a failed
or stopped speech) or that the app could not post still let the next
"응, 지워" confirm the delete.

- Worker: an answer reaches the person on a tts_done neither failed nor
  cancelled (spoken answers), on the app's reply_delivered ok (chat
  answers), or at reply_text (unspoken voice answers). tts_unavailable, a
  failed or cancelled tts_done, reply_delivered ok=false, no report within
  30 s and the session ending all mean it never did: a turn number is
  skipped, so the delete asked in it lapses. A reply sent before the
  previous answer reached them is still not their next turn.
- App: a chat answer reports reply_delivered with whether Discord took the
  post; a late, failed or empty answer reports it was not posted.
- Tests: no TTS, a failed speech, a chat post the app reports failed, and
  no report at all do not delete; a spoken and a posted question do.
  Conversation and worker client tests for the report.
- Specs: voice_worker.spec.md (reply_delivered, when an answer reaches the
  person), discord_core.spec.md, conversation.spec.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-12 01:39:48 +09:00
parent c328bda699
commit 9e75df266b
9 changed files with 209 additions and 32 deletions

View File

@@ -32,8 +32,11 @@ pub trait Voice {
fn ask_rank(&self, id: &str, candidates: &[(String, u64, String)]) -> bool; fn ask_rank(&self, id: &str, candidates: &[(String, u64, String)]) -> bool;
/// Have the brain answer a chat message (not spoken); its text comes as `CoreEvent::ReplyText`. /// Have the brain answer a chat message (not spoken); its text comes as `CoreEvent::ReplyText`.
fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool; fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool;
/// Post `text` in `channel` as a reply to `message` (never pinging anyone). /// Post the chat answer `id` in `channel` as a reply to `message` (never pinging anyone), then
fn send_reply(&self, channel: u64, message: u64, text: &str); /// tell the brain whether it was posted.
fn send_reply(&self, id: &str, channel: u64, message: u64, text: &str);
/// Tell the brain the chat answer `id` was not posted (late, failed or empty).
fn reply_not_posted(&self, id: &str);
} }
impl Voice for DiscordCore { impl Voice for DiscordCore {
@@ -55,15 +58,20 @@ impl Voice for DiscordCore {
fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool { fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool {
DiscordCore::chat_answer(self, id, user, text) DiscordCore::chat_answer(self, id, user, text)
} }
fn send_reply(&self, channel: u64, message: u64, text: &str) { fn send_reply(&self, id: &str, channel: u64, message: u64, text: &str) {
let core = self.clone(); let core = self.clone();
let text = text.to_owned(); let (id, text) = (id.to_owned(), text.to_owned());
tokio::spawn(async move { tokio::spawn(async move {
if let Err(e) = core.send_chat_reply(channel, message, &text).await { let posted = core.send_chat_reply(channel, message, &text).await;
if let Err(e) = &posted {
crate::error_log!("conversation", "could not post the chat answer in {channel}: {e}"); crate::error_log!("conversation", "could not post the chat answer in {channel}: {e}");
} }
core.reply_delivered(&id, posted.is_ok());
}); });
} }
fn reply_not_posted(&self, id: &str) {
self.reply_delivered(id, false);
}
} }
/// How a registered chat channel is answered (`chatChannels[].answer`). /// How a registered chat channel is answered (`chatChannels[].answer`).
@@ -178,7 +186,9 @@ impl<V: Voice> Conversation<V> {
debug_log!("conversation", "chat answer {id} chars={} failed={failed} late={late}", debug_log!("conversation", "chat answer {id} chars={} failed={failed} late={late}",
text.chars().count()); text.chars().count());
if !failed && !late && !text.trim().is_empty() { if !failed && !late && !text.trim().is_empty() {
self.voice.send_reply(channel, message, text); self.voice.send_reply(id, channel, message, text);
} else {
self.voice.reply_not_posted(id);
} }
} }
} }
@@ -364,7 +374,8 @@ mod tests {
Addressed(String, u64), Addressed(String, u64),
Rank(String, Vec<String>), Rank(String, Vec<String>),
ChatAnswer(String, u64, String), ChatAnswer(String, u64, String),
SendReply(u64, u64, String), SendReply(String, u64, u64, String),
NotPosted(String),
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
@@ -396,8 +407,11 @@ mod tests {
self.did.borrow_mut().push(Did::ChatAnswer(id.into(), user, text.into())); self.did.borrow_mut().push(Did::ChatAnswer(id.into(), user, text.into()));
true true
} }
fn send_reply(&self, channel: u64, message: u64, text: &str) { fn send_reply(&self, id: &str, channel: u64, message: u64, text: &str) {
self.did.borrow_mut().push(Did::SendReply(channel, message, text.into())); self.did.borrow_mut().push(Did::SendReply(id.into(), channel, message, text.into()));
}
fn reply_not_posted(&self, id: &str) {
self.did.borrow_mut().push(Did::NotPosted(id.into()));
} }
} }
impl Fake { impl Fake {
@@ -578,7 +592,7 @@ mod tests {
let Did::ChatAnswer(id, user, text) = f.take().remove(0) else { panic!() }; let Did::ChatAnswer(id, user, text) = f.take().remove(0) else { panic!() };
assert_eq!((user, text.as_str()), (A, "자비스 내일 일정 알려 줘")); assert_eq!((user, text.as_str()), (A, "자비스 내일 일정 알려 줘"));
c.on_event(&CoreEvent::ReplyText { id: id.clone(), text: "내일은 비어 있어요.".into(), failed: false }, t); c.on_event(&CoreEvent::ReplyText { id: id.clone(), text: "내일은 비어 있어요.".into(), failed: false }, t);
assert_eq!(f.take(), vec![Did::SendReply(CH_WAKE, 5, "내일은 비어 있어요.".into())]); assert_eq!(f.take(), vec![Did::SendReply(id.clone(), CH_WAKE, 5, "내일은 비어 있어요.".into())]);
// A second reply_text for the same id sends nothing. // A second reply_text for the same id sends nothing.
c.on_event(&CoreEvent::ReplyText { id, text: "x".into(), failed: false }, t); c.on_event(&CoreEvent::ReplyText { id, text: "x".into(), failed: false }, t);
assert!(f.take().is_empty()); assert!(f.take().is_empty());
@@ -623,7 +637,7 @@ mod tests {
assert!(matches!(did[0], Did::Answer(..)) && matches!(did[1], Did::ChatAnswer(..)), "{did:?}"); assert!(matches!(did[0], Did::Answer(..)) && matches!(did[1], Did::ChatAnswer(..)), "{did:?}");
let Did::ChatAnswer(id, ..) = &did[1] else { panic!() }; let Did::ChatAnswer(id, ..) = &did[1] else { panic!() };
c.on_event(&CoreEvent::ReplyText { id: id.clone(), text: String::new(), failed: true }, t); c.on_event(&CoreEvent::ReplyText { id: id.clone(), text: String::new(), failed: true }, t);
assert!(f.take().is_empty()); assert_eq!(f.take(), vec![Did::NotPosted(id.clone())]);
assert_eq!(c.missed().count(), 0, "a chat message never takes the voice turn"); assert_eq!(c.missed().count(), 0, "a chat message never takes the voice turn");
} }
@@ -632,8 +646,8 @@ mod tests {
let (mut c, f, t) = chat_conv(CallSettings::default()); let (mut c, f, t) = chat_conv(CallSettings::default());
c.on_event(&chat(CH_WAKE, 5, A, false, "자비스 안녕"), t); c.on_event(&chat(CH_WAKE, 5, A, false, "자비스 안녕"), t);
let Did::ChatAnswer(id, ..) = f.take().remove(0) else { panic!() }; let Did::ChatAnswer(id, ..) = f.take().remove(0) else { panic!() };
c.on_event(&CoreEvent::ReplyText { id, text: "안녕하세요.".into(), failed: false }, t + CHAT_ANSWER_KEPT); c.on_event(&CoreEvent::ReplyText { id: id.clone(), text: "안녕하세요.".into(), failed: false }, t + CHAT_ANSWER_KEPT);
assert!(f.take().is_empty(), "too late: the conversation has moved on"); assert_eq!(f.take(), vec![Did::NotPosted(id)], "too late: the conversation has moved on");
} }
#[test] #[test]

View File

@@ -133,7 +133,8 @@ never takes the voice turn and never goes through the response policy.
- The brain answers through `reply` with `via: "chat"` and `speak: false`; the answer is posted in the - 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 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. An answer that arrives 5 minutes or 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. more after it was asked is not posted. The worker is told whether each answer was posted
(`reply_delivered`).
- The worker writes voice and chat answers in separate lanes (`voice_worker.spec.md`), so a voice - The worker writes voice and chat answers in separate lanes (`voice_worker.spec.md`), so a voice
answer never waits behind another person's chat answer; one person's answers are written one at a answer never waits behind another person's chat answer; one person's answers are written one at a
time, in the order they asked. For the two to run at the same time the LLM must take two time, in the order they asked. For the two to run at the same time the LLM must take two

View File

@@ -416,6 +416,11 @@ impl DiscordCore {
self.inner.worker.reply(id, user, text, "chat", false) self.inner.worker.reply(id, user, text, "chat", false)
} }
/// Tell the brain whether the chat answer `id` was posted.
pub fn reply_delivered(&self, id: &str, ok: bool) {
self.inner.worker.reply_delivered(id, ok);
}
/// Stop one request (an answer or a `say`); the rest of the queue goes on. /// Stop one request (an answer or a `say`); the rest of the queue goes on.
pub fn cancel(&self, id: &str) { pub fn cancel(&self, id: &str) {
with_playback(&self.inner, |p| p.cancel(id)); with_playback(&self.inner, |p| p.cancel(id));

View File

@@ -191,6 +191,8 @@ registered until it is removed (M5 setup).
- **Answer** (M4): `send_chat_reply(channel_id, message_id, text)` sends like Send, with the first - **Answer** (M4): `send_chat_reply(channel_id, message_id, text)` sends like Send, with the first
part as a Discord reply to the message; nobody is pinged, not even its author. `chat_answer(id, user, part as a Discord reply to the message; nobody is pinged, not even its author. `chat_answer(id, user,
text)` asks the brain for a chat answer (`reply` with `via: "chat"`); its text comes as `ReplyText`. text)` asks the brain for a chat answer (`reply` with `via: "chat"`); its text comes as `ReplyText`.
`reply_delivered(id, ok)` tells the worker whether that answer was posted (`reply_delivered`), so a
delete asked in an answer the person never saw cannot be confirmed (`voice_worker.spec.md`).
- A channel that is not registered, or has the needed switch off, is refused with an access error. - A channel that is not registered, or has the needed switch off, is refused with an access error.
- **Chat tool actions** (M3, `chat_channels.spec.md`): when the worker link is ready and after login, - **Chat tool actions** (M3, `chat_channels.spec.md`): when the worker link is ready and after login,
the core sends `chat_channels` (every registered channel with its switches, and the bot's user ID). the core sends `chat_channels` (every registered channel with its switches, and the bot's user ID).

View File

@@ -325,6 +325,12 @@ impl WorkerClient {
sent sent
} }
/// Whether the chat answer `id` was posted (voice_worker.spec.md "History and brain"): a
/// question in an answer the person never saw cannot be confirmed in their next message.
pub fn reply_delivered(&self, id: &str, ok: bool) {
self.send_json(json!({"type": "reply_delivered", "id": id, "ok": ok}));
}
fn end_failed(&self, id: &str) { fn end_failed(&self, id: &str) {
if self.inner.state.lock().unwrap().open_tts.remove(id) { if self.inner.state.lock().unwrap().open_tts.remove(id) {
let _ = self.inner.events.send(WorkerEvent::TtsDone(TtsDone { id: id.into(), chunks: 0, cancelled: false, let _ = self.inner.events.send(WorkerEvent::TtsDone(TtsDone { id: id.into(), chunks: 0, cancelled: false,

View File

@@ -374,5 +374,11 @@ async fn a_chat_reply_is_not_spoken_and_its_text_comes_back() {
w.send(Message::text(json!({"type": "reply_text", "id": "c1", "text": "", "failed": true}).to_string())); w.send(Message::text(json!({"type": "reply_text", "id": "c1", "text": "", "failed": true}).to_string()));
let e = next_matching(&mut rx, |e| !matches!(e, WorkerEvent::Ready(_))).await; let e = next_matching(&mut rx, |e| !matches!(e, WorkerEvent::Ready(_))).await;
assert_eq!(e, WorkerEvent::ReplyText { id: "c1".into(), text: String::new(), failed: true }); assert_eq!(e, WorkerEvent::ReplyText { id: "c1".into(), text: String::new(), failed: true });
// Whether the answer was posted goes back, so a question in it counts only once it was seen.
c.reply_delivered("c1", true);
c.reply_delivered("c2", false);
until(|| w.got().len() >= 4).await;
assert_eq!(w.got()[2..], [Got::Json(json!({"type": "reply_delivered", "id": "c1", "ok": true})),
Got::Json(json!({"type": "reply_delivered", "id": "c2", "ok": false}))]);
c.close(); c.close();
} }

View File

@@ -41,7 +41,8 @@ def is_loopback(host: str) -> bool:
class VoiceWorker: class VoiceWorker:
def __init__(self, token: str, transcriber: Transcriber | None, vad_factory: Callable[[], VadStream], def __init__(self, token: str, transcriber: Transcriber | None, vad_factory: Callable[[], VadStream],
tts_engine: TtsEngine | None = None, hello_timeout_s: float = 5.0, tick_s: float = 0.05, tts_engine: TtsEngine | None = None, hello_timeout_s: float = 5.0, tick_s: float = 0.05,
history=None, brain: Brain | None = None, chat_timeout_s: float = 15.0): history=None, brain: Brain | None = None, chat_timeout_s: float = 15.0,
delivery_timeout_s: float = 30.0):
if not token: if not token:
raise ValueError("a connection token is required") raise ValueError("a connection token is required")
self.token = token self.token = token
@@ -59,14 +60,15 @@ class VoiceWorker:
self.brain_lanes = {"voice": asyncio.Lock(), "chat": asyncio.Lock()} self.brain_lanes = {"voice": asyncio.Lock(), "chat": asyncio.Lock()}
self.brain_people: dict[str, asyncio.Lock] = {} self.brain_people: dict[str, asyncio.Lock] = {}
self.chat_timeout_s = chat_timeout_s self.chat_timeout_s = chat_timeout_s
self.delivery_timeout_s = delivery_timeout_s # a chat answer the app never reports: not posted
# chat_action ids are unique across connections and worker runs, so a late answer from an # 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. # earlier connection (or an earlier worker) never completes a newer action.
self.chat_action_prefix = secrets.token_hex(4) self.chat_action_prefix = secrets.token_hex(4)
# Per person: how many replies they had (a delete is confirmed only in the very next one), # Per person: how many replies they had (a delete is confirmed only in the very next one),
# the deletes they were asked to confirm (chat_channels.spec.md) and how many of their # the deletes they were asked to confirm (chat_channels.spec.md) and how many of their
# replies are not answered yet. A turn number is skipped for a request that arrived before # replies have not reached them yet (spoken, or posted by the app). A turn number is skipped
# the person's previous answer was given, and after an answer they never got, so a # for a request that arrived before the person's previous answer reached them, and after an
# confirmation asked in that answer lapses. # answer that never did, so a confirmation asked in that answer lapses.
self.reply_turns: dict[str, int] = {} self.reply_turns: dict[str, int] = {}
self.pending_deletes: dict[str, dict] = {} self.pending_deletes: dict[str, dict] = {}
self.replies_unanswered: dict[str, int] = {} self.replies_unanswered: dict[str, int] = {}
@@ -174,6 +176,9 @@ class Session:
# Spoken replies whose speech was cancelled while the brain was still answering. # Spoken replies whose speech was cancelled while the brain was still answering.
self.cancelled_replies: set[str] = set() self.cancelled_replies: set[str] = set()
self.thinking_replies: set[str] = set() self.thinking_replies: set[str] = set()
# Answers sent but not yet known to have reached the person: id -> (user, turn, timer).
self.deliveries: dict[str, tuple[str, int, asyncio.TimerHandle | None]] = {}
self._handed_over: dict[str, bool] = {} # replies whose end is counted by _delivered
self.out: asyncio.Queue = asyncio.Queue() self.out: asyncio.Queue = asyncio.Queue()
self.closed = False self.closed = False
@@ -211,6 +216,8 @@ class Session:
for fut in self.chat_waiting.values(): for fut in self.chat_waiting.values():
if not fut.done(): if not fut.done():
fut.set_result({"ok": False, "error": "the app disconnected"}) fut.set_result({"ok": False, "error": "the app disconnected"})
for rid in list(self.deliveries):
self._delivered(rid, False)
ticker.cancel() ticker.cancel()
writer.cancel() writer.cancel()
if w.scheduler: if w.scheduler:
@@ -309,7 +316,7 @@ class Session:
"chat_delete": self._on_chat_delete, "reply": self._on_reply, "chat_delete": self._on_chat_delete, "reply": self._on_reply,
"addressed": self._on_addressed, "rank": self._on_rank, "addressed": self._on_addressed, "rank": self._on_rank,
"chat_channels": self._on_chat_channels, "chat_action_result": self._on_chat_action_result, "chat_channels": self._on_chat_channels, "chat_action_result": self._on_chat_action_result,
"memory_config": self._on_memory_config}.get(kind) "memory_config": self._on_memory_config, "reply_delivered": self._on_reply_delivered}.get(kind)
if handler is None: if handler is None:
self.error("bad_message", f"unknown type: {kind!r}") self.error("bad_message", f"unknown type: {kind!r}")
return return
@@ -350,6 +357,7 @@ class Session:
tts = self.worker.tts tts = self.worker.tts
if tts is None: if tts is None:
self.error("tts_unavailable", "no TTS engine is loaded", id=rid) self.error("tts_unavailable", "no TTS engine is loaded", id=rid)
self._delivered(rid, False)
return return
req = self.tts_requests.get(rid) req = self.tts_requests.get(rid)
if req is None: if req is None:
@@ -553,10 +561,38 @@ class Session:
finally: finally:
self.thinking_replies.discard(rid) self.thinking_replies.discard(rid)
self.cancelled_replies.discard(rid) self.cancelled_replies.discard(rid)
waiting = self.worker.replies_unanswered if not self._handed_over.pop(rid, False): # else _delivered counts it
waiting[user] -= 1 self._reached(user)
if not waiting[user]:
del waiting[user] def _reached(self, user: str) -> None:
waiting = self.worker.replies_unanswered
waiting[user] -= 1
if not waiting[user]:
del waiting[user]
def _delivered(self, rid: str, ok: bool) -> None:
"""The answer ``rid`` reached its asker (spoken, or posted by the app), or never will."""
got = self.deliveries.pop(rid, None)
if got is None:
return
user, turn, timer = got
if timer is not None:
timer.cancel()
if not ok:
# They never got this answer, so nothing it asked can be confirmed next.
w = self.worker
w.reply_turns[user] = max(w.reply_turns.get(user, 0), turn + 1)
debug_log(f"brain reply {rid} did not reach the asker", "voice_worker")
self._reached(user)
def _on_reply_delivered(self, msg: dict) -> None:
rid, ok = msg.get("id"), msg.get("ok")
if not isinstance(rid, str) or not isinstance(ok, bool):
self.error("bad_message", "reply_delivered needs a string id and a boolean ok")
return
got = self.deliveries.get(rid)
if got is not None and got[2] is not None: # only chat answers wait for the app's report
self._delivered(rid, ok)
async def _reply_inner(self, rid: str, user: str, text: str, via: str, speak: bool, early: bool) -> None: async def _reply_inner(self, rid: str, user: str, text: str, via: str, speak: bool, early: bool) -> None:
w = self.worker w = self.worker
@@ -588,9 +624,17 @@ class Session:
answer = None answer = None
answer = (answer or "").strip() answer = (answer or "").strip()
self.send({"type": "reply_text", "id": rid, "text": answer, "failed": not answer}) self.send({"type": "reply_text", "id": rid, "text": answer, "failed": not answer})
if not answer or (speak and rid in self.cancelled_replies): # The answer reaches them when spoken (tts_done) or when the app reports the post
# They never got this answer, so nothing it asked can be confirmed next. # (reply_delivered); an unspoken voice answer has nothing more to wait for.
w.reply_turns[user] = max(w.reply_turns.get(user, 0), turn + 1) posted = via == "chat" and not speak
timer = asyncio.get_running_loop().call_later(w.delivery_timeout_s, self._delivered, rid, False) \
if posted else None
self.deliveries[rid] = (user, turn, timer)
self._handed_over[rid] = True
if self.closed or not answer or (speak and rid in self.cancelled_replies):
self._delivered(rid, False)
elif not speak and not posted:
self._delivered(rid, True)
if not answer: if not answer:
return return
if via == "voice" and channel and w.history is not None: if via == "voice" and channel and w.history is not None:
@@ -613,6 +657,7 @@ class Session:
self.tts_requests.pop(req.id, None) self.tts_requests.pop(req.id, None)
self.send({"type": "tts_done", "id": req.id, "chunks": req.chunks, "cancelled": req.cancelled, self.send({"type": "tts_done", "id": req.id, "chunks": req.chunks, "cancelled": req.cancelled,
"failed": req.failed}) "failed": req.failed})
self._delivered(req.id, not (req.cancelled or req.failed))
def _tts_error(self, req: TtsRequest, message: str) -> None: def _tts_error(self, req: TtsRequest, message: str) -> None:
self.error("tts_failed", message, id=req.id) self.error("tts_failed", message, id=req.id)

View File

@@ -200,6 +200,7 @@ which carry Discord's own time.
-> {"type": "chat_delete", "channel": "...", "message_id": "..."} -> {"type": "chat_delete", "channel": "...", "message_id": "..."}
-> {"type": "reply", "id": "r1", "user": "<id>", "text": "...", "via": "voice", "speak": true} -> {"type": "reply", "id": "r1", "user": "<id>", "text": "...", "via": "voice", "speak": true}
<- {"type": "reply_text", "id": "r1", "text": "...", "failed": false} <- {"type": "reply_text", "id": "r1", "text": "...", "failed": false}
-> {"type": "reply_delivered", "id": "c1", "ok": true} // a chat answer was posted (ok: false: it was not)
-> {"type": "addressed", "id": "q1", "user": "<id>", "text": "..."} -> {"type": "addressed", "id": "q1", "user": "<id>", "text": "..."}
<- {"type": "addressed_result", "id": "q1", "addressed": true, "failed": false} <- {"type": "addressed_result", "id": "q1", "addressed": true, "failed": false}
-> {"type": "rank", "id": "k1", "candidates": [{"id": "7", "user": "<id>", "text": "..."}]} -> {"type": "rank", "id": "k1", "candidates": [{"id": "7", "user": "<id>", "text": "..."}]}
@@ -262,10 +263,14 @@ which carry Discord's own time.
newer action. newer action.
With no registered channels the tool is not offered. An action waits up to 15 s for its result With no registered channels the tool is not offered. An action waits up to 15 s for its result
(then it fails), and a closed session fails every waiting action. Each reply counts as one turn (then it fails), and a closed session fails every waiting action. Each reply counts as one turn
per person, so a delete is confirmed only in that person's next reply. A reply sent before the per person, so a delete is confirmed only in that person's next reply. An answer reaches the
person's previous answer was given cannot answer it, and an answer they never got (failed, or person when its speech ends with `tts_done` neither failed nor cancelled (`speak: true`), when the
stopped before it was spoken) asked them nothing: in both cases a turn number is skipped, so a app reports it posted with `reply_delivered` `ok: true` (`via: "chat"`), or at `reply_text` for an
delete asked in that previous answer lapses. unspoken voice answer. It never reaches them when the brain gave none, its speech was stopped,
failed or had no TTS (`tts_unavailable`), the app reports `ok: false`, no report comes within 30 s,
or the session ends first. A reply sent before the person's previous answer reached them cannot
answer it, and an answer that never reached them asked them nothing: in both cases a turn number is
skipped, so a delete asked in that previous answer lapses.
- Memory settings, optional in `hello` as `"memory": {...}` and changeable with - Memory settings, optional in `hello` as `"memory": {...}` and changeable with
`{"type": "memory_config", ...}`: `chat` (true), `others` (true), `off` (false), `since_days` `{"type": "memory_config", ...}`: `chat` (true), `others` (true), `off` (false), `since_days`
(null: whole history), `recent_minutes` (30), `recent_records` (20), `matches` (5), (null: whole history), `recent_minutes` (30), `recent_records` (20), `matches` (5),

View File

@@ -49,8 +49,8 @@ def store(tmp_path):
s.close() s.close()
async def with_worker(body, store=None, brain=None, tts=None): async def with_worker(body, store=None, brain=None, tts=None, **kw):
worker = VoiceWorker(TOKEN, LevelTranscriber(), AmplitudeVad, tts, history=store, brain=brain) worker = VoiceWorker(TOKEN, LevelTranscriber(), AmplitudeVad, tts, history=store, brain=brain, **kw)
server = await worker.serve("127.0.0.1", 0) server = await worker.serve("127.0.0.1", 0)
port = server.sockets[0].getsockname()[1] port = server.sockets[0].getsockname()[1]
try: try:
@@ -407,6 +407,7 @@ def test_the_brain_gets_the_registered_channels_and_their_messages(store):
text="회의는 3시", ts=1_800_000_000.0) text="회의는 3시", ts=1_800_000_000.0)
await send(ws, type="reply", id="r1", user="100", text="q", via="chat", speak=False) await send(ws, type="reply", id="r1", user="100", text="q", via="chat", speak=False)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text") await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "reply_text")
await send(ws, type="reply_delivered", id="r1", ok=True)
await send(ws, type="reply", id="r2", user="100", text="q", via="chat", speak=False) await send(ws, type="reply", id="r2", user="100", text="q", via="chat", speak=False)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r2") await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r2")
@@ -811,6 +812,7 @@ def test_a_yes_after_the_delete_question_deletes(store):
await ws.send(json.dumps(DELETABLE)) await ws.send(json.dumps(DELETABLE))
await send(ws, type="reply", id="r1", user="100", text="지워", via="chat", speak=False) await send(ws, type="reply", id="r1", user="100", text="지워", via="chat", speak=False)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r1") await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r1")
await send(ws, type="reply_delivered", id="r1", ok=True) # the app posted the question
await send(ws, type="reply", id="r2", user="100", text="응, 지워", via="voice", speak=False) await send(ws, type="reply", id="r2", user="100", text="응, 지워", via="voice", speak=False)
seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "chat_action") seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "chat_action")
await send(ws, type="chat_action_result", id=seen[-1]["id"], ok=True) await send(ws, type="chat_action_result", id=seen[-1]["id"], ok=True)
@@ -841,3 +843,94 @@ def test_a_yes_after_a_delete_question_they_never_got_does_not_delete(store):
seen = asyncio.run(with_worker(body, store=store, brain=brain)) seen = asyncio.run(with_worker(body, store=store, brain=brain))
assert not any(isinstance(m, dict) and m.get("type") == "chat_action" for m in seen) assert not any(isinstance(m, dict) and m.get("type") == "chat_action" for m in seen)
assert results == [("지워", True), ("응, 지워", False)] assert results == [("지워", True), ("응, 지워", False)]
def run_delete_then_yes(store, ask, after, question="지울까요?", tts=None, **kw):
"""The person asks to delete (``ask``: the reply message fields), ``after(ws)`` plays what happened
to the question, then they say "응, 지워" by voice. The chat actions that went out."""
import threading
release = threading.Event()
release.set()
brain, results = delete_tool_brain(release, question=question)
async def body(url):
async with connect(url) as ws:
await hello(ws)
await ws.send(json.dumps(DELETABLE))
await send(ws, type="reply", id="r1", user="100", text="지워", **ask)
await after(ws)
await send(ws, type="reply", id="r2", user="100", text="응, 지워", via="voice", speak=False)
seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r2")
return [m for m in seen if isinstance(m, dict) and m.get("type") == "chat_action"]
actions = asyncio.run(with_worker(body, store=store, brain=brain, tts=tts, **kw))
return actions, results
def until(pred):
async def after(ws):
await recv_until(ws, pred)
return after
@pytest.mark.unit
def test_a_yes_after_a_question_that_could_not_be_spoken_does_not_delete(store):
# No TTS engine: the spoken question never reached them.
actions, results = run_delete_then_yes(
store, dict(via="voice", speak=True),
until(lambda m: isinstance(m, dict) and m.get("code") == "tts_unavailable"))
assert actions == [] and results == [("지워", True), ("응, 지워", False)]
@pytest.mark.unit
def test_a_yes_after_a_question_whose_speech_failed_does_not_delete(store):
actions, results = run_delete_then_yes(
store, dict(via="voice", speak=True),
until(lambda m: isinstance(m, dict) and m.get("type") == "tts_done" and m["failed"]),
question="FAIL 지울까요?", tts=FakeTts())
assert actions == [] and results == [("지워", True), ("응, 지워", False)]
@pytest.mark.unit
def test_a_yes_after_a_spoken_question_deletes(store):
import threading
release = threading.Event()
release.set()
brain, results = delete_tool_brain(release)
async def body(url):
async with connect(url) as ws:
await hello(ws)
await ws.send(json.dumps(DELETABLE))
await send(ws, type="reply", id="r1", user="100", text="지워", via="voice", speak=True)
done = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done")
assert not done[-1]["failed"] and not done[-1]["cancelled"]
await send(ws, type="reply", id="r2", user="100", text="응, 지워", via="voice", speak=False)
seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "chat_action")
await send(ws, type="chat_action_result", id=seen[-1]["id"], ok=True)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r2")
return seen[-1]
action = asyncio.run(with_worker(body, store=store, brain=brain, tts=FakeTts()))
assert action["action"] == "delete" and results == [("지워", True), ("응, 지워", True)]
@pytest.mark.unit
def test_a_yes_after_a_chat_question_the_app_could_not_post_does_not_delete(store):
async def not_posted(ws):
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r1")
await send(ws, type="reply_delivered", id="r1", ok=False)
actions, results = run_delete_then_yes(store, dict(via="chat", speak=False), not_posted)
assert actions == [] and results == [("지워", True), ("응, 지워", False)]
@pytest.mark.unit
def test_a_chat_question_the_app_never_reports_counts_as_not_posted(store):
async def no_report(ws):
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r1")
await asyncio.sleep(0.3)
actions, results = run_delete_then_yes(store, dict(via="chat", speak=False), no_report,
delivery_timeout_s=0.1)
assert actions == [] and results == [("지워", True), ("응, 지워", False)]