fix(voice_worker): a yes sent before the delete question was answered does not delete

A delete is confirmed only in the person's very next turn, but turn numbers
were given when a reply started. A "응, 지워" sent while the delete question
was still being written waited for the person's lock and then counted as the
next turn, so the delete went through.

- The worker counts each person's unanswered replies. A reply that arrived
  before their previous answer was given skips a turn number, so a delete
  asked in that answer lapses.
- After an answer the person never got (failed, or stopped before it was
  spoken) a turn number is skipped as well: it asked them nothing.
- Tests with the real chatChannels tool and an agreeing consent check: a yes
  sent before the answer does not delete, a yes after a failed answer does
  not delete, a yes after the answer deletes.
- Specs: voice_worker.spec.md (turn numbering), chat_channels.spec.md (the
  next turn is a message sent after the question was received).

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

View File

@@ -734,3 +734,110 @@ def test_one_persons_voice_and_chat_answers_are_written_one_at_a_time(store):
started, order = asyncio.run(with_worker(body, store=store, brain=FakeBrain(answer)))
assert started == ["chat question"], "the voice question started before the same person's chat answer"
assert order == ["c1", "v1"]
DELETABLE = {"type": "chat_channels", "bot": "900", "channels": [
{"id": "c1", "name": "일정", "description": "약속", "read": True, "send": True, "edit": True,
"react": True, "delete": True, "pin": False}]}
def delete_tool_brain(release, question="지울까요?"):
"""A brain whose reply runs the real chatChannels tool: "지워" asks to delete message m1 (and waits
for ``release`` before answering), "응, 지워" confirms. The consent check always agrees."""
from unittest.mock import patch
from jarvis.reply import turn_state
from jarvis.tools.builtin.chat_channels import ChatChannelsTool
class Cfg:
ollama_base_url = "http://x"
ollama_chat_model = "m"
results = []
def answer(user, text):
turn_state.reset()
turn_state.set_chat(brain.chat)
args = {"action": "delete", "channel": "c1", "message_id": "m1", "confirm": text == "응, 지워"}
try:
with patch("jarvis.tools.builtin.chat_channels.call_llm_direct", return_value="YES"):
res = ChatChannelsTool().execute(db=None, cfg=Cfg(), tool_args=args, system_prompt="",
original_prompt=text, redacted_text=text, max_retries=0,
user_print=lambda m: None)
finally:
turn_state.reset()
results.append((text, res.success))
if text == "지워":
release.wait(10)
return question if text == "지워" else "done"
brain = FakeBrain(answer)
return brain, results
@pytest.mark.unit
def test_a_yes_sent_before_the_delete_question_was_answered_does_not_delete(store):
import threading
release = threading.Event()
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="chat", speak=False)
await asyncio.sleep(0.2)
# Typed before the question reached them: it cannot be their answer to it.
await send(ws, type="reply", id="r2", user="100", text="응, 지워", via="voice", speak=False)
await asyncio.sleep(0.1)
release.set()
return await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r2")
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)]
@pytest.mark.unit
def test_a_yes_after_the_delete_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="chat", speak=False)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r1")
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))
assert (action["action"], action["channel"], action["message_id"]) == ("delete", "c1", "m1")
assert results == [("지워", True), ("응, 지워", True)]
@pytest.mark.unit
def test_a_yes_after_a_delete_question_they_never_got_does_not_delete(store):
import threading
release = threading.Event()
release.set()
brain, results = delete_tool_brain(release, question="") # the answer failed: nothing was asked
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="chat", speak=False)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r1")
await send(ws, type="reply", id="r2", user="100", text="응, 지워", via="voice", speak=False)
return await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r2")
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)]