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:
@@ -21,7 +21,9 @@ written, send, edit its own messages, react, delete and pin. Product rules: `doc
|
||||
data; one word YES or NO). Any other answer, an error, a refusal, a question, another topic or a
|
||||
different message means no and cancels the request. A message longer than 600 characters is not
|
||||
judged and means no (cutting it could drop a later "don't"). A request not confirmed in the very
|
||||
next turn lapses. Eval: `evals/test_chat_delete_consent.py`.
|
||||
next turn lapses. The backend numbers the turns so that only a message sent after the person got
|
||||
the question is their next turn (voice worker: `voice_worker.spec.md`, "History and brain"). Eval:
|
||||
`evals/test_chat_delete_consent.py`.
|
||||
- **Raw data.** The tool returns plain text for the reply model; the only LLM call inside is the
|
||||
delete consent check.
|
||||
|
||||
|
||||
@@ -62,10 +62,14 @@ class VoiceWorker:
|
||||
# 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 a later one) and the
|
||||
# deletes they were asked to confirm (chat_channels.spec.md).
|
||||
# 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.
|
||||
self.reply_turns: dict[str, int] = {}
|
||||
self.pending_deletes: dict[str, dict] = {}
|
||||
self.replies_unanswered: dict[str, int] = {}
|
||||
self.scheduler: SttScheduler | None = None
|
||||
self.tts: TtsService | None = None
|
||||
self._ids = itertools.count(1)
|
||||
@@ -495,7 +499,10 @@ class Session:
|
||||
return
|
||||
if speak:
|
||||
self.thinking_replies.add(rid)
|
||||
asyncio.get_running_loop().create_task(self._reply(rid, user, text, via, speak))
|
||||
waiting = self.worker.replies_unanswered
|
||||
early = waiting.get(user, 0) > 0 # sent before their previous answer: not a reply to it
|
||||
waiting[user] = waiting.get(user, 0) + 1
|
||||
asyncio.get_running_loop().create_task(self._reply(rid, user, text, via, speak, early))
|
||||
|
||||
def _on_addressed(self, msg: dict) -> None:
|
||||
got = self._strings(msg, "id", "user", "text")
|
||||
@@ -540,14 +547,18 @@ class Session:
|
||||
self.send({"type": "rank_result", "id": qid, "order": order or [], "failed": order is None})
|
||||
asyncio.get_running_loop().create_task(rank())
|
||||
|
||||
async def _reply(self, rid: str, user: str, text: str, via: str, speak: bool) -> None:
|
||||
async def _reply(self, rid: str, user: str, text: str, via: str, speak: bool, early: bool = False) -> None:
|
||||
try:
|
||||
await self._reply_inner(rid, user, text, via, speak)
|
||||
await self._reply_inner(rid, user, text, via, speak, early)
|
||||
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]
|
||||
|
||||
async def _reply_inner(self, rid: str, user: str, text: str, via: str, speak: bool) -> None:
|
||||
async def _reply_inner(self, rid: str, user: str, text: str, via: str, speak: bool, early: bool) -> None:
|
||||
w = self.worker
|
||||
channel = self.voice_channel
|
||||
# Person first, then lane: a reply waiting for its asker's other answer holds no lane.
|
||||
@@ -563,7 +574,10 @@ class Session:
|
||||
else:
|
||||
from jarvis.memory.history import HistoryContext
|
||||
history = HistoryContext()
|
||||
turn = w.reply_turns[user] = w.reply_turns.get(user, 0) + 1
|
||||
turn = w.reply_turns[user] = w.reply_turns.get(user, 0) + (2 if early else 1)
|
||||
if early:
|
||||
debug_log(f"brain reply {rid} arrived before the previous answer: a turn is skipped",
|
||||
"voice_worker")
|
||||
from .chat_bridge import ChatBridge
|
||||
chat = ChatBridge(self, asyncio.get_running_loop(), turn, w.pending_deletes.setdefault(user, {})) \
|
||||
if self.chat_channels else None
|
||||
@@ -574,6 +588,9 @@ 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)
|
||||
if not answer:
|
||||
return
|
||||
if via == "voice" and channel and w.history is not None:
|
||||
|
||||
@@ -262,7 +262,10 @@ 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 later reply.
|
||||
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.
|
||||
- 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),
|
||||
|
||||
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user