omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty Proposal] fix(chat): harden message persistence and memory citation deserialization resilience ( proposed)

Open naufalfx805-source opened this issue 2 months ago • 2 comments

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

  1. Chat Message Persistence & Desynchronization: In routers/chat.py (line 588) and utils/chat.py (line 544), process_message unpacks memories using raw dictionary comprehension [MessageConversation(**m) for m in ...]. If m in memories is an object instance (e.g. Conversation or SimpleNamespace), unpacking fails with TypeError: ... argument after ** must be a mapping. If m is a partial dictionary or legacy document lacking required structured fields or valid created_at, Pydantic raises an unhandled ValidationError.
    • In routers/chat.py, this occurs after chat_db.add_message has already written the message to Firestore. The exception bubbles to emit_done_frame, triggering persist_outcome = 'exhausted', which abandons the persisted message and mints a new in-memory Message with a different uuid.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.
  2. Model Decoupling Boundary Violation: In utils/chat.py (lines 515–522), process_message imported and called deserialize_conversation(m) simply to extract conversation IDs. Deserializing full Conversation models here violated the architectural decoupling rule (INV-CHAT-1 and Phase 4 model split) and caused unhandled ValidationError when handling lightweight citation cards.
  3. Namespace Collision & Endpoint Shadowing in routers/users.py: DELETE /v1/joan/{memory_id}/followup-question was defined as def delete_person_endpoint(...), identically shadowing DELETE /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_safe and MessageConversation.safe_build_many in models/chat.py to safely handle dicts, Conversation instances, and partial records without unhandled ValidationError or TypeError.
  • Replaced raw comprehension in routers/chat.py and utils/chat.py with MessageConversation.safe_build_many(memories).
  • Replaced unsafe deserialize_conversation comprehension in utils/chat.py with extract_memory_ids, avoiding model-split boundary violations.
  • Hardened extract_memory_ids in utils/conversation_helpers.py using getattr to safely handle objects without an id attribute.
  • Renamed shadowed Joan endpoint in routers/users.py to get_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

naufalfx805-source avatar Sep 30 '26 19:09 naufalfx805-source

This issue is linked to our feedback platform. For feedback and updates, please visit this link.

featurebase-for-github avatar Oct 01 '26 17:10 featurebase-for-github

Hi — I've opened #20509 (https://github.com/BasedHardware/omi/pull/20509).

Disclosed: authored with AI assistance (Z.ai).

monopolymummat-cmyk avatar Oct 03 '26 20:10 monopolymummat-cmyk