omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty Proposal] Validate inputs and handle exceptions in database daily summaries

Open Ariyachan opened this issue 2 months ago • 1 comments

Problem

  1. Uncaught KeyError and TypeError on partial counters in upsert_desktop_daily_usage (backend/database/daily_summaries.py:46-77):

    • upsert_desktop_daily_usage loops over DESKTOP_DAILY_USAGE_COUNTER_FIELDS and accesses counters[field] directly. If a caller submits a partial dictionary (e.g. reporting a subset of metrics or omitting fields), this immediately crashes with KeyError: field.
    • If any counter value is None or non-integer, max(previous_value, counters[field]) raises an unhandled TypeError: '>' not supported between instances of 'NoneType' and 'int'.
    • Passing empty or whitespace-only strings for uid, date, or client_device_id causes Firestore to throw ValueError: A document id must not be empty or builds an invalid composite document ID __.
  2. Uncaught NotFound during concurrent visibility update and deletion (backend/database/daily_summaries.py:212-253):

    • In set_daily_summary_visibility, summary_ref.update({'visibility': visibility}) raises google.api_core.exceptions.NotFound if the summary document was concurrently pruned or deleted, causing the patch endpoint to crash with an unhandled 500 error.
    • In delete_daily_summary, summary_ref.delete() does not catch NotFound.
  3. Missing input guards across daily summaries database methods (backend/database/daily_summaries.py:97-268):

    • create_daily_summary raises bare KeyError if summary_data lacks 'id', crashes on empty/whitespace uid, and uses destructive set() without merge=True.
    • get_daily_summary, get_daily_summary_by_date, and get_summaries_count lack validation for empty/whitespace IDs.

Proposed Solution

  • Add input validation and whitespace stripping across upsert_desktop_daily_usage, get_desktop_daily_usage, create_daily_summary, get_daily_summary, get_daily_summary_by_date, get_daily_summaries, update_daily_summary, delete_daily_summary, set_daily_summary_visibility, and get_summaries_count.
  • Safely extract counter fields using counters.get(field, 0) with integer type guards in upsert_desktop_daily_usage, preventing KeyError and TypeError.
  • Use set(..., merge=True) in create_daily_summary to prevent destructive loss of concurrent or existing fields.
  • Wrap summary_ref.update(...) in try...except NotFound in set_daily_summary_visibility to return False safely on missing documents.
  • Wrap summary_ref.delete(...) in try...except NotFound in delete_daily_summary.
  • Add 48 comprehensive unit tests in backend/tests/unit/test_database_daily_summaries_resilience.py.

Ariyachan avatar Sep 26 '26 08:09 Ariyachan

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 11:10 undivisible