omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] fix(apple-health-tools): guard string numeric formatting, handle None health_data, and sanitize lists ($50 proposed)

Open Dimank92 opened this issue 1 month ago • 2 comments

Summary

In backend/utils/retrieval/tools/apple_health_tools.py, health data synced from iOS devices or Firestore documents exhibits multiple input-type and formatting failure modes:

  1. health_data: None crashes: integration.get('health_data', {}) returns None if the Firestore document explicitly contains "health_data": None (or corrupted non-dict payload). Subsequently, health_data.get('steps', {}) raises an unhandled AttributeError: 'NoneType' object has no attribute 'get'. This causes tools to catch generic errors and report "Failed to retrieve step data." rather than the informative "No step data found. Make sure Apple Health is synced from your iPhone.".
  2. String numeric formatting errors (TypeError / ValueError):
    • total_steps:, and avg_per_day:,.0f crash with ValueError: Cannot specify ',' with 's' if values are stored as string numbers. If values are None, they crash with TypeError: unsupported format string passed to NoneType.__format__.
    • total_sleep_hours:.1f crashes with ValueError: Unknown format code 'f' for object of type 'str' if stored as a string, and total_sleep_hours / max(...) raises TypeError: unsupported operand type(s) for /: 'str' and 'int'.
    • Heart rate values (avg_hr:.0f, min_hr:.0f, max_hr:.0f) and workout metrics (duration:.0f, calories:.0f, distance:.2f) crash on string formats.
    • Workout timestamps: start_ms / 1000 raises TypeError when startDate is a string timestamp.
  3. Malformed / non-dict list entries: daily steps, daily sleep, and workouts containing None or non-dict items cause sorting keys (lambda x: x.get('date', ''), _workout_timestamp) to crash with AttributeError.
  4. get_apple_health_data helper: Does not guard against health_data being None or non-dict before indexing data_type.

Proposed Changes

  1. Safe Numeric Coercion (_safe_float, _safe_int): Add helpers to safely coerce numeric values (ints, floats, and numeric strings) while gracefully rejecting booleans, non-numeric strings, NaN, inf, and None.
  2. Robust health_data Extraction: Ensure health_data is guaranteed to be a dictionary across all tool endpoints and get_apple_health_data.
  3. List Sanitization: Filter daily entries and workouts to dictionary items only before sorting or accessing keys.
  4. Safe Formatting: Ensure all f-string numeric formatters (:,, :.1f, :.0f, :.2f) use coerced numeric types with safe fallbacks.
  5. Hermetic Unit Tests: Add backend/tests/unit/test_apple_health_coercion_and_guards.py verifying:
    • _safe_float and _safe_int with ints, floats, strings, negative values, booleans, NaNs, and text.
    • get_apple_health_data with None health data.
    • String total and daily steps formatting.
    • String total_sleep_hours and nightly average calculation.
    • String heart rate metrics formatting.
    • String workout metrics and timestamp parsing.
    • Comprehensive summary formatting with mixed types and malformed list items.

Bounty Proposal

Proposed bounty: $50 USDC Payout address: 0xC345C07229307d4f1c7935aD2cAe48e390215808 (USDC Polygon/Base/Arbitrum)

Dimank92 avatar Oct 22 '26 04:10 Dimank92

PR created with comprehensive hermetic unit tests: https://github.com/BasedHardware/omi/pull/23212

Bounty proposal: $50 USDC Payout address: 0xC345C07229307d4f1c7935aD2cAe48e390215808 (USDC Polygon/Base/Arbitrum)

Dimank92 avatar Oct 22 '26 04:10 Dimank92

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