[Bounty Proposal] fix(chat): harden message persistence and memory citation deserialization resilience ( proposed)
Summary
Fixes a critical message persistence and desynchronization failure in chat delivery (backend/routers/chat.py, backend/utils/chat.py), decouples models properly without unsafe deserialization, and fixes a function name collision in backend/routers/users.py.
Problem & Impact
- Chat Message Persistence & Desynchronization:
In
routers/chat.py(line 588) andutils/chat.py(line 544),process_messageunpacksmemoriesusing raw dictionary comprehension[MessageConversation(**m) for m in ...]. Ifminmemoriesis an object instance (e.g.ConversationorSimpleNamespace), unpacking fails withTypeError: ... argument after ** must be a mapping. Ifmis a partial dictionary or legacy document lacking requiredstructuredfields or validcreated_at, Pydantic raises an unhandledValidationError.- In
routers/chat.py, this occurs afterchat_db.add_messagehas already written the message to Firestore. The exception bubbles toemit_done_frame, triggeringpersist_outcome = 'exhausted', which abandons the persisted message and mints a new in-memoryMessagewith a differentuuid.uuid4(). As a result, the client receives an unregistered message ID that does not match the database document, corrupting feedback ratings, citations, and conversation continuity.
- In
- Model Decoupling Boundary Violation:
In
utils/chat.py(lines 515–522),process_messageimported and calleddeserialize_conversation(m)simply to extract conversation IDs. Deserializing fullConversationmodels here violated the architectural decoupling rule (INV-CHAT-1and Phase 4 model split) and caused unhandledValidationErrorwhen handling lightweight citation cards. - Namespace Collision & Endpoint Shadowing in
routers/users.py:DELETE /v1/joan/{memory_id}/followup-questionwas defined asdef delete_person_endpoint(...), identically shadowingDELETE /v1/users/people/{person_id}(delete_person_endpoint). This overwrote the person deletion endpoint in the module namespace, corrupted OpenAPI operation IDs, and had a typo in error detail (progres).
Fix
- Added
MessageConversation.from_memory_safeandMessageConversation.safe_build_manyinmodels/chat.pyto safely handle dicts,Conversationinstances, and partial records without unhandledValidationErrororTypeError. - Replaced raw comprehension in
routers/chat.pyandutils/chat.pywithMessageConversation.safe_build_many(memories). - Replaced unsafe
deserialize_conversationcomprehension inutils/chat.pywithextract_memory_ids, avoiding model-split boundary violations. - Hardened
extract_memory_idsinutils/conversation_helpers.pyusinggetattrto safely handle objects without anidattribute. - Renamed shadowed Joan endpoint in
routers/users.pytoget_joan_followup_question_endpoint, corrected typo in error detail, and provided backwards-compatible fallback for legacy callers.
PR
https://github.com/BasedHardware/omi/pull/20017
Tests
- 14/14 unit tests passing in
tests/unit/test_chat_memory_resilience.py. - 49/49 passing in
tests/unit/test_conversation_model_split.py. - 1/1 passing in
tests/unit/test_models_chat_import_purity.py. - 66/66 passing in
tests/unit/test_lock_bypass_fixes.py.
Proposing US$150 (critical message delivery integrity, citation resilience, and namespace collision on production chat paths).
Payout EVM Address (Base Mainnet): 0x496e32ff3851cdb9146223a811e3d4f9218dd954
This issue is linked to our feedback platform. For feedback and updates, please visit this link.
Hi — I've opened #20509 (https://github.com/BasedHardware/omi/pull/20509).
Disclosed: authored with AI assistance (Z.ai).