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

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

View File

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

View File

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