AstrBot-Desktop/src-tauri/src/networking
YokiiroBW 02ac2046d6 fix(chat): validate slot sends before writing history
send_connection_slot_message wrote the optimistic user row from the
runtime snapshot alone, then called send_slot_message, which can still
refuse the send for an empty message, a busy slot or a disconnected
slot. The rejected question stayed in history with no failure marker,
and because each attempt minted a fresh local- id, retrying appended a
second copy rather than replacing the first.

Add a read-only pre-flight, ensure_slot_can_send, and run it before any
database write. The conditions it checks are extracted into
slot_send_precondition, which send_slot_message now uses inside its own
critical section, so the two cannot drift. Mint the message id once so
the failure path can address the row it wrote.

A send that fails after AstrBot accepted it must keep its question:
cancelled and streamInterrupted both mean the message is already in the
remote conversation and a partial reply may already be stored. Those
two keep the row; every other code marks it failed. server_received
encodes that rule and is covered by a test that walks every
AstrBotClientError variant through astrbot_send_error, so a future
rename of an R2 error code cannot silently change the classification.

Marking rather than deleting keeps chat_sessions.last_message_id valid,
re-uses the idempotent upsert, and leaves unread counts alone since they
already ignore user rows. The queue sanctions either choice.

The R2 retry loop and its error mapping are untouched; all five R2
transport sentinels still pass.

Refs: F-18 (audit P2-3).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 00:20:09 +08:00
..
astrbot_client.rs fix(astrbot): scope stream event ids and fix the transport layer 2026-07-26 23:12:56 +08:00
commands.rs fix(chat): validate slot sends before writing history 2026-07-27 00:20:09 +08:00
gateway_client.rs fix(astrbot): scope stream event ids and fix the transport layer 2026-07-26 23:12:56 +08:00
manager.rs feat(p00): add Tauri desktop spike foundation 2026-07-18 20:34:47 +08:00
mod.rs fix(chat): validate slot sends before writing history 2026-07-27 00:20:09 +08:00
runtime.rs fix(chat): validate slot sends before writing history 2026-07-27 00:20:09 +08:00