From 9e75df266ba3e3c9813fe35324da6fb7a1e9fad2 Mon Sep 17 00:00:00 2001 From: javis-bot Date: Thu, 12 Nov 2026 01:39:48 +0900 Subject: [PATCH] fix: a delete question counts as asked only once the answer reached the person MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- app/src/conversation.rs | 40 +++++++---- app/src/conversation.spec.md | 3 +- app/src/discord.rs | 5 ++ app/src/discord_core.spec.md | 2 + app/src/worker_client.rs | 6 ++ app/tests/worker_client.rs | 6 ++ src/voice_worker/server.py | 69 +++++++++++++++---- src/voice_worker/voice_worker.spec.md | 13 ++-- tests/test_voice_worker_brain.py | 97 ++++++++++++++++++++++++++- 9 files changed, 209 insertions(+), 32 deletions(-) diff --git a/app/src/conversation.rs b/app/src/conversation.rs index fcb3b88..5ab090a 100644 --- a/app/src/conversation.rs +++ b/app/src/conversation.rs @@ -32,8 +32,11 @@ pub trait Voice { 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`. fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool; - /// Post `text` in `channel` as a reply to `message` (never pinging anyone). - fn send_reply(&self, channel: u64, message: u64, text: &str); + /// Post the chat answer `id` in `channel` as a reply to `message` (never pinging anyone), then + /// 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 { @@ -55,15 +58,20 @@ impl Voice for DiscordCore { fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool { 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 text = text.to_owned(); + let (id, text) = (id.to_owned(), text.to_owned()); 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}"); } + 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`). @@ -178,7 +186,9 @@ impl Conversation { 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); + self.voice.send_reply(id, channel, message, text); + } else { + self.voice.reply_not_posted(id); } } } @@ -364,7 +374,8 @@ mod tests { Addressed(String, u64), Rank(String, Vec), ChatAnswer(String, u64, String), - SendReply(u64, u64, String), + SendReply(String, u64, u64, String), + NotPosted(String), } #[derive(Clone, Default)] @@ -396,8 +407,11 @@ mod tests { self.did.borrow_mut().push(Did::ChatAnswer(id.into(), user, text.into())); true } - fn send_reply(&self, channel: u64, message: u64, text: &str) { - self.did.borrow_mut().push(Did::SendReply(channel, message, text.into())); + fn send_reply(&self, id: &str, channel: u64, message: u64, text: &str) { + 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 { @@ -578,7 +592,7 @@ mod tests { let Did::ChatAnswer(id, user, text) = f.take().remove(0) else { panic!() }; assert_eq!((user, text.as_str()), (A, "자비스 내일 일정 알려 줘")); 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. c.on_event(&CoreEvent::ReplyText { id, text: "x".into(), failed: false }, t); assert!(f.take().is_empty()); @@ -623,7 +637,7 @@ mod tests { assert!(matches!(did[0], Did::Answer(..)) && matches!(did[1], Did::ChatAnswer(..)), "{did:?}"); let Did::ChatAnswer(id, ..) = &did[1] else { panic!() }; 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"); } @@ -632,8 +646,8 @@ mod tests { 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"); + c.on_event(&CoreEvent::ReplyText { id: id.clone(), text: "안녕하세요.".into(), failed: false }, t + CHAT_ANSWER_KEPT); + assert_eq!(f.take(), vec![Did::NotPosted(id)], "too late: the conversation has moved on"); } #[test] diff --git a/app/src/conversation.spec.md b/app/src/conversation.spec.md index 6d4dd6f..4801eaf 100644 --- a/app/src/conversation.spec.md +++ b/app/src/conversation.spec.md @@ -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 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 - 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 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 diff --git a/app/src/discord.rs b/app/src/discord.rs index b6eec43..7f4e622 100644 --- a/app/src/discord.rs +++ b/app/src/discord.rs @@ -416,6 +416,11 @@ impl DiscordCore { 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. pub fn cancel(&self, id: &str) { with_playback(&self.inner, |p| p.cancel(id)); diff --git a/app/src/discord_core.spec.md b/app/src/discord_core.spec.md index ff45766..fd685ba 100644 --- a/app/src/discord_core.spec.md +++ b/app/src/discord_core.spec.md @@ -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 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`. + `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. - **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). diff --git a/app/src/worker_client.rs b/app/src/worker_client.rs index bc9e07c..79e4e19 100644 --- a/app/src/worker_client.rs +++ b/app/src/worker_client.rs @@ -325,6 +325,12 @@ impl WorkerClient { 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) { 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, diff --git a/app/tests/worker_client.rs b/app/tests/worker_client.rs index 4f59d62..24d04a2 100644 --- a/app/tests/worker_client.rs +++ b/app/tests/worker_client.rs @@ -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())); 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 }); + // 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(); } diff --git a/src/voice_worker/server.py b/src/voice_worker/server.py index a6f2aa6..0facf60 100644 --- a/src/voice_worker/server.py +++ b/src/voice_worker/server.py @@ -41,7 +41,8 @@ def is_loopback(host: str) -> bool: class VoiceWorker: 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, - 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: raise ValueError("a connection token is required") self.token = token @@ -59,14 +60,15 @@ class VoiceWorker: self.brain_lanes = {"voice": asyncio.Lock(), "chat": asyncio.Lock()} self.brain_people: dict[str, asyncio.Lock] = {} 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 # earlier connection (or an earlier worker) never completes a newer action. 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), # 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 - # the person's previous answer was given, and after an answer they never got, so a - # confirmation asked in that answer lapses. + # replies have not reached them yet (spoken, or posted by the app). A turn number is skipped + # for a request that arrived before the person's previous answer reached them, and after an + # answer that never did, so a confirmation asked in that answer lapses. self.reply_turns: dict[str, int] = {} self.pending_deletes: dict[str, dict] = {} self.replies_unanswered: dict[str, int] = {} @@ -174,6 +176,9 @@ class Session: # Spoken replies whose speech was cancelled while the brain was still answering. self.cancelled_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.closed = False @@ -211,6 +216,8 @@ class Session: for fut in self.chat_waiting.values(): if not fut.done(): fut.set_result({"ok": False, "error": "the app disconnected"}) + for rid in list(self.deliveries): + self._delivered(rid, False) ticker.cancel() writer.cancel() if w.scheduler: @@ -309,7 +316,7 @@ class Session: "chat_delete": self._on_chat_delete, "reply": self._on_reply, "addressed": self._on_addressed, "rank": self._on_rank, "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: self.error("bad_message", f"unknown type: {kind!r}") return @@ -350,6 +357,7 @@ class Session: tts = self.worker.tts if tts is None: self.error("tts_unavailable", "no TTS engine is loaded", id=rid) + self._delivered(rid, False) return req = self.tts_requests.get(rid) if req is None: @@ -553,10 +561,38 @@ class Session: finally: self.thinking_replies.discard(rid) self.cancelled_replies.discard(rid) - waiting = self.worker.replies_unanswered - waiting[user] -= 1 - if not waiting[user]: - del waiting[user] + if not self._handed_over.pop(rid, False): # else _delivered counts it + self._reached(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: w = self.worker @@ -588,9 +624,17 @@ class Session: answer = None answer = (answer or "").strip() self.send({"type": "reply_text", "id": rid, "text": answer, "failed": not answer}) - if not answer or (speak and rid in self.cancelled_replies): - # They never got this answer, so nothing it asked can be confirmed next. - w.reply_turns[user] = max(w.reply_turns.get(user, 0), turn + 1) + # The answer reaches them when spoken (tts_done) or when the app reports the post + # (reply_delivered); an unspoken voice answer has nothing more to wait for. + 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: return 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.send({"type": "tts_done", "id": req.id, "chunks": req.chunks, "cancelled": req.cancelled, "failed": req.failed}) + self._delivered(req.id, not (req.cancelled or req.failed)) def _tts_error(self, req: TtsRequest, message: str) -> None: self.error("tts_failed", message, id=req.id) diff --git a/src/voice_worker/voice_worker.spec.md b/src/voice_worker/voice_worker.spec.md index bfa3c8f..8e2fef4 100644 --- a/src/voice_worker/voice_worker.spec.md +++ b/src/voice_worker/voice_worker.spec.md @@ -200,6 +200,7 @@ which carry Discord's own time. -> {"type": "chat_delete", "channel": "...", "message_id": "..."} -> {"type": "reply", "id": "r1", "user": "", "text": "...", "via": "voice", "speak": true} <- {"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": "", "text": "..."} <- {"type": "addressed_result", "id": "q1", "addressed": true, "failed": false} -> {"type": "rank", "id": "k1", "candidates": [{"id": "7", "user": "", "text": "..."}]} @@ -262,10 +263,14 @@ which carry Discord's own time. newer action. 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 - per person, so a delete is confirmed only in that person's next reply. A reply sent before the - person's previous answer was given cannot answer it, and an answer they never got (failed, or - stopped before it was spoken) asked them nothing: in both cases a turn number is skipped, so a - delete asked in that previous answer lapses. + per person, so a delete is confirmed only in that person's next reply. An answer reaches the + person when its speech ends with `tts_done` neither failed nor cancelled (`speak: true`), when the + app reports it posted with `reply_delivered` `ok: true` (`via: "chat"`), or at `reply_text` for an + 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 `{"type": "memory_config", ...}`: `chat` (true), `others` (true), `off` (false), `since_days` (null: whole history), `recent_minutes` (30), `recent_records` (20), `matches` (5), diff --git a/tests/test_voice_worker_brain.py b/tests/test_voice_worker_brain.py index 531e894..9d3794c 100644 --- a/tests/test_voice_worker_brain.py +++ b/tests/test_voice_worker_brain.py @@ -49,8 +49,8 @@ def store(tmp_path): s.close() -async def with_worker(body, store=None, brain=None, tts=None): - worker = VoiceWorker(TOKEN, LevelTranscriber(), AmplitudeVad, tts, history=store, brain=brain) +async def with_worker(body, store=None, brain=None, tts=None, **kw): + worker = VoiceWorker(TOKEN, LevelTranscriber(), AmplitudeVad, tts, history=store, brain=brain, **kw) server = await worker.serve("127.0.0.1", 0) port = server.sockets[0].getsockname()[1] 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) 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 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 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 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 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) 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) @@ -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)) assert not any(isinstance(m, dict) and m.get("type") == "chat_action" for m in seen) 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)]