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:
@@ -457,3 +457,46 @@ def test_a_chat_action_the_app_never_answers_fails(store):
|
||||
|
||||
asyncio.run(run())
|
||||
assert results and results[0]["ok"] is False and results[0]["error"]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_a_late_answer_from_an_earlier_connection_is_not_taken_for_a_new_action(store):
|
||||
results = []
|
||||
brain = FakeBrain(lambda u, t: results.append(brain.chat.act("send", "c1", text="x")) or "done")
|
||||
worker = VoiceWorker(TOKEN, LevelTranscriber(), AmplitudeVad, None, history=store, brain=brain,
|
||||
chat_timeout_s=0.5)
|
||||
|
||||
async def ask(url):
|
||||
async with connect(url) as ws:
|
||||
await hello(ws)
|
||||
await ws.send(json.dumps(CHANNELS))
|
||||
await send(ws, type="reply", id="r1", user="100", text="q", via="chat", speak=False)
|
||||
seen = await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "chat_action")
|
||||
return ws, seen[-1]["id"]
|
||||
|
||||
async def run():
|
||||
server = await worker.serve("127.0.0.1", 0)
|
||||
url = f"ws://127.0.0.1:{server.sockets[0].getsockname()[1]}"
|
||||
try:
|
||||
async with connect(url) as ws: # first connection: asked, never answered, then gone
|
||||
await hello(ws)
|
||||
await ws.send(json.dumps(CHANNELS))
|
||||
await send(ws, type="reply", id="r1", user="100", text="q", via="chat", speak=False)
|
||||
old = (await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "chat_action"))[-1]["id"]
|
||||
async with connect(url) as ws: # second connection: the old answer arrives late
|
||||
await hello(ws)
|
||||
await ws.send(json.dumps(CHANNELS))
|
||||
await send(ws, type="reply", id="r2", user="100", text="q", via="chat", speak=False)
|
||||
new = (await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "chat_action"))[-1]["id"]
|
||||
await send(ws, type="chat_action_result", id=old, ok=True, message_ids=["stale"])
|
||||
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r2")
|
||||
return old, new
|
||||
finally:
|
||||
server.close()
|
||||
await server.wait_closed()
|
||||
await worker.stop()
|
||||
|
||||
old, new = asyncio.run(asyncio.wait_for(run(), 15))
|
||||
assert old != new
|
||||
assert all(r.get("message_ids") != ["stale"] for r in results), results
|
||||
assert results[-1]["ok"] is False
|
||||
|
||||
Reference in New Issue
Block a user