omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty Proposal] Guard inputs and catch NotFound in database short_term_memories and sync_bridges

Open Ariyachan opened this issue 2 months ago • 3 comments

Problem

  1. Race condition and uncaught NotFound in mark_consolidated (backend/database/short_term_memories.py:22-38):

    • mark_consolidated checks if not doc_ref.get().exists: return. However, if the document is pruned or deleted concurrently between the .get() check and doc_ref.update(...), Firestore raises google.api_core.exceptions.NotFound: 404 No document to update.
    • This unhandled exception bubbles up as an HTTP 500 error when resolving review queue conflicts (POST /v3/memories/review-queue/{id}/resolve).
    • Passing an empty or whitespace uid or short_term_id raises an unhandled ValueError: A document id must not be empty.
  2. Uncaught NotFound aborting cascade deletion in tombstone_source (backend/database/short_term_memories.py:41-78):

    • During cascade delete of a conversation, tombstone_source iterates through collection_ref.stream().
    • Calling doc.reference.update(update_payload) without catching NotFound causes the entire cascade deletion to abort if any shadow record was pruned or deleted concurrently.
    • Passing an empty or whitespace uid or source_id raises ValueError: A document id must not be empty.
  3. Missing input validation and unhandled exceptions in mark_sync_bridge_cleaned (backend/database/sync_bridges.py:8-22):

    • mark_sync_bridge_cleaned does not validate uid or source_id, raising ValueError on empty/whitespace input.
    • If the target conversation document is concurrently deleted or purged during transactional commit, an unhandled NotFound exception is raised rather than returning False.

Proposed Solution

  • Add input validation for uid, short_term_id, and source_id across mark_consolidated, tombstone_source, and mark_sync_bridge_cleaned.
  • Wrap doc_ref.update(...) in try...except NotFound in mark_consolidated to guarantee safe no-op on concurrent deletion.
  • Wrap doc.reference.update(...) in try...except NotFound in tombstone_source to allow cascade deletion to continue uninterrupted.
  • Add existence check and catch NotFound in mark_sync_bridge_cleaned to return False cleanly.
  • Add comprehensive unit test suite in backend/tests/unit/test_database_short_term_and_sync_bridges_resilience.py.

Ariyachan avatar Sep 26 '26 07:09 Ariyachan

🚀 Bounty Fix Submitted

I have completed the fix for this issue in Pull Request: PR #19110

  • Full implementation, tested, zero regressions.
  • Ready for review and merge.

💸 Contributor Bounty Payout Coordinates:

shruhicollections-beep avatar Sep 26 '26 08:09 shruhicollections-beep

Thanks for putting this together. We aren't doing bounties at the moment, so we're closing the bounty proposal issues for now — thanks for the fix! 🙏

undivisible avatar Oct 22 '26 12:10 undivisible

I have implemented the complete fix for this bounty proposal in PR #23889 (https://github.com/BasedHardware/omi/pull/23889).

Summary of Solution

  • Added input validation guards for uid, short_term_id, and source_id in backend/database/short_term_memories.py (mark_consolidated, tombstone_source) and backend/database/sync_bridges.py (mark_sync_bridge_cleaned).
  • Wrapped doc_ref.update(...) in try...except NotFound in mark_consolidated to gracefully handle concurrent deletion during review-queue conflict resolution without raising HTTP 500.
  • Wrapped doc.reference.update(...) in try...except NotFound in tombstone_source to ensure cascade deletion continues uninterrupted even if individual shadow records are deleted concurrently.
  • Checked document existence and caught NotFound inside transaction and run_transactional in mark_sync_bridge_cleaned, returning False cleanly.
  • Added 29 unit tests covering all edge cases in backend/tests/unit/test_database_short_term_and_sync_bridges_resilience.py, with 100% test pass rate across the test suite.

Unified Payout Wallet: 0xC345C07229307d4f1c7935aD2cAe48e390215808 (Polygon / Base / Arbitrum / Stellar)

Dimank92 avatar Oct 28 '26 12:10 Dimank92