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

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