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:
@@ -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<V: Voice> Conversation<V> {
|
||||
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<String>),
|
||||
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]
|
||||
|
||||
Reference in New Issue
Block a user