fix(ui): make chat policy-block message replacement atomic#1556
Conversation
Replace the non-atomic delete_message + add_message pattern in _chat_helpers.py with a single transactional ChatDatabase.upsert_message, so a crash or exception between the DELETE and INSERT can no longer leave a session without its policy-block / assistant message. Closes #987
SummaryClean, well-scoped fix that closes a real data-loss bug — approve. The chat policy-block path previously replaced a stored message with a separate I verified the core behavior by exercising Issues Found🟢 Minor — (Apply only if you want the consolidation — keeping them separate is a defensible call.) Strengths
VerdictApprove. No blocking issues — the one nit above is optional. Two notes for the merge, not blockers: I couldn't run |
The chat policy-block path in
_chat_helpers.pyreplaced a stored message with a separatedelete_messagethenadd_message. A crash or exception between the DELETE and the INSERT left the session with no message at all — silent data loss (#987). Now both call sites use a singleChatDatabase.upsert_message, which runs the DELETE and INSERT inside one transaction that rolls back as a unit, so the original message always survives a failed replacement.Closes #987.
Test plan
python -m pytest tests/unit/chat/ui/test_database.py -q— passes, including new upsert insert/replace, wrong-session-safety, metadata round-trip, and crash-rollback casespython -m black --check src/gaia/ui/database.py src/gaia/ui/_chat_helpers.py tests/unit/chat/ui/test_database.py