[Bounty proposal] fix(database): add stream fault tolerance and client injection to trends ($50 proposed)
Problem Description
The trends data access module (backend/database/trends.py) queries public trends and persists topic aggregations across user memories. However, it currently has several error resilience, input sanitization, and testability shortcomings:
- Unprotected Root Stream Collection:
get_trends_datacallstrends_ref.stream(retry=Retry())outside of anytry/exceptblock and hardcodes the globaldbclient without dependency injection. If the initial stream encounters a network timeout or Firestore unavailability, the/v1/trendsendpoint crashes with an unhandled exception instead of safely returning[]. - Missing Category ID Crash: When constructing
category_topics_ref = trends_ref.document(category_data['id']).collection('topics'), if a Firestore document payload lacks the'id'field, it raises an unhandledKeyError: 'id'. It should fall back tocategory.id. - Malformed Memory ID Len Calculation: In topic sorting,
len(e.get('memory_ids') or [])throwsTypeErrorifmemory_idsis stored as a non-iterable (e.g. an integer or boolean). - Unsanitized Inputs in save_trends:
save_trendsdoes not sanitizememory_idortopicsstrings, allowing empty/blank strings to create invalid documents or duplicate array unions. - Missing Dependency Injection: Neither
get_trends_datanorsave_trendsacceptsfirestore_client=None. - Zero Unit Test Coverage:
backend/database/trends.pycurrently has zero dedicated unit tests.
Proposed Solution
- Root Stream Error Boundary: Wrap collection streaming in a
try/exceptboundary returning[]on error. - Client Dependency Injection: Add
firestore_client: Optional[Any] = Noneparameter acrossget_trends_dataandsave_trends. - ID Fallback & Safe Len: Fall back to document
idwhen missing from dictionary payloads, and guardmemory_idslength checking withisinstance(..., list). - Input Sanitization: Strip
memory_idandtopics, skipping blank items and empty payloads. - Unit Tests: Add 8 comprehensive unit tests in
backend/tests/unit/test_trends.pycovering category filtering, sorting, ID fallbacks, stream failures, and save operations.
Proposed Bounty: $50 PayPal: https://paypal.me/Dragonire9 ([email protected])
@josancamon19 @kodjima33 ā is the complete trends repair in this issue still wanted as a paid contribution, and available to take on?
The full scope Iām referring to is: return an empty result safely when the root stream fails; fall back to the document ID; handle malformed memory ID lists without losing valid categories; normalize memory/topic inputs and skip blanks; allow an injected Firestore client in both read and save paths; and cover all eight regressions for filtering, sorting, ID fallbacks, stream failures, and saves while preserving existing API behavior.
Could you confirm the current target revision, complete acceptance criteria and assignment process, the approved amount and ISO currency (the posted $50 is only proposed), and the payment process available to a Mainland China based individual using this GitHub identity, including fees and payment timing? Once those and the current implementation are confirmed, I can propose a fixed price and delivery date for the complete fix.
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! š