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;
/// 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]

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
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

View File

@@ -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));

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
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).

View File

@@ -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,