fix(chat): delete needs the person's own yes; chat action ids never repeat

- chatChannels delete with confirm=true now runs only in the person's very
  next turn and only when their own message of that turn agrees, checked by a
  small fail-closed YES/NO call on the router model chain (the message fenced
  as data). A refusal, question, other topic or error cancels the request;
  an unconfirmed request lapses after one turn. The reply model's confirm
  flag alone can no longer delete.
- chat_action ids carry a per-run nonce and the session, so a late answer
  meant for an earlier connection (or worker run) never completes a newer
  action.
- Tests: agree, refuse, other topic, lapse, classifier failure, late answer
  across reconnects. Eval test_chat_delete_consent: 12 of 12 on qwen2.5:7b,
  3 runs. Spec and llm_contexts updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 14:08:35 +09:00
parent fe4e296361
commit 0182823592
8 changed files with 200 additions and 18 deletions

View File

@@ -7,6 +7,7 @@ import hmac
import ipaddress
import itertools
import json
import secrets
import time
from dataclasses import dataclass
from typing import Callable
@@ -53,6 +54,9 @@ class VoiceWorker:
self.brain = brain
self.brain_lock = asyncio.Lock() # one reply at a time
self.chat_timeout_s = chat_timeout_s
# 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).
self.reply_turns: dict[str, int] = {}
@@ -431,7 +435,7 @@ class Session:
async def chat_action(self, action: str, channel: str, args: dict) -> dict:
"""Ask the app to do a chat action and wait for its answer (or a timeout)."""
aid = f"a{next(self._chat_ids)}"
aid = f"{self.worker.chat_action_prefix}-{self.sid}-{next(self._chat_ids)}"
fut = asyncio.get_running_loop().create_future()
self.chat_waiting[aid] = fut
self.send({"type": "chat_action", "id": aid, "action": action, "channel": channel,