omi
omi copied to clipboard
[Bounty Proposal] Validate inputs and handle exceptions in database daily summaries
Problem
-
Uncaught
KeyErrorandTypeErroron partial counters inupsert_desktop_daily_usage(backend/database/daily_summaries.py:46-77):upsert_desktop_daily_usageloops overDESKTOP_DAILY_USAGE_COUNTER_FIELDSand accessescounters[field]directly. If a caller submits a partial dictionary (e.g. reporting a subset of metrics or omitting fields), this immediately crashes withKeyError: field.- If any counter value is
Noneor non-integer,max(previous_value, counters[field])raises an unhandledTypeError: '>' not supported between instances of 'NoneType' and 'int'. - Passing empty or whitespace-only strings for
uid,date, orclient_device_idcauses Firestore to throwValueError: A document id must not be emptyor builds an invalid composite document ID__.
-
Uncaught
NotFoundduring concurrent visibility update and deletion (backend/database/daily_summaries.py:212-253):- In
set_daily_summary_visibility,summary_ref.update({'visibility': visibility})raisesgoogle.api_core.exceptions.NotFoundif 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 catchNotFound.
- In
-
Missing input guards across daily summaries database methods (
backend/database/daily_summaries.py:97-268):create_daily_summaryraises bareKeyErrorifsummary_datalacks'id', crashes on empty/whitespaceuid, and uses destructiveset()withoutmerge=True.get_daily_summary,get_daily_summary_by_date, andget_summaries_countlack 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, andget_summaries_count. - Safely extract counter fields using
counters.get(field, 0)with integer type guards inupsert_desktop_daily_usage, preventingKeyErrorandTypeError. - Use
set(..., merge=True)increate_daily_summaryto prevent destructive loss of concurrent or existing fields. - Wrap
summary_ref.update(...)intry...except NotFoundinset_daily_summary_visibilityto returnFalsesafely on missing documents. - Wrap
summary_ref.delete(...)intry...except NotFoundindelete_daily_summary. - Add 48 comprehensive unit tests in
backend/tests/unit/test_database_daily_summaries_resilience.py.
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! 🙏