omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] fix(apple-health): coerce null numeric metrics and parse ISO startDate in workout tools ($50 proposed)

Open Dimank92 opened this issue 1 month ago • 2 comments

Summary

In backend/utils/retrieval/tools/apple_health_tools.py, HealthKit retrieval tools (get_apple_health_steps_tool, get_apple_health_sleep_tool, get_apple_health_heart_rate_tool, get_apple_health_workouts_tool, get_apple_health_summary_tool) retrieve synced Apple Health metrics from Firestore (integration.get('health_data', {})).

When HealthKit sync records empty/null metrics or ISO 8601 timestamps, the tools crash with unhandled TypeErrors inside their execution blocks:

  1. TypeError: unsupported format string passed to NoneType.__format__ (in steps total/avg, sleep total/daily, workouts duration/calories, summary steps/energy)
  2. TypeError: unsupported operand type(s) for /: 'NoneType' and 'int' (in sleep average per night)
  3. TypeError: unsupported operand type(s) for /: 'str' and 'int' (in workout timestamp calculation when startDate is an ISO 8601 string)

Runtime Crashes & Reproduction

  1. Workouts startDate is ISO string or durationMinutes is None:
# workout = {'type': 'Running', 'startDate': '2026-10-21T08:00:00Z', 'durationMinutes': None, 'caloriesBurned': None}
start_ms = workout.get('startDate', 0)
start_dt = datetime.fromtimestamp(start_ms / 1000, tz=timezone.utc)
# -> TypeError: unsupported operand type(s) for /: 'str' and 'int'
duration = workout.get('durationMinutes', 0)
f"Duration: {duration:.0f} minutes\n"
# -> TypeError: unsupported format string passed to NoneType.__format__

Note: _workout_timestamp() already supports ISO strings via to_datetime(value), but get_apple_health_workouts_tool() line 340 assumes start_ms is numeric and divides by 1000. When an ISO string is received, start_ms / 1000 raises TypeError, silencing the date display completely.

  1. Steps total, average, or daily steps is None:
# steps_data = {'total': None, 'average_per_day': None, 'daily': [{'date': '2026-10-21', 'steps': None}]}
total_steps = steps_data.get('total', 0)
f"Total Steps: {total_steps:,}\n"
# -> TypeError: unsupported format string passed to NoneType.__format__
  1. Sleep total_sleep_hours or daily sleepHours is None:
# sleep_data = {'total_sleep_hours': None, 'daily': [{'date': '2026-10-21', 'sleepHours': None}]}
total_sleep_hours / max(len(daily_sleep), 1)
# -> TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

Fix

  • Add defensive coercion helpers _safe_float(val, default=0.0) and _safe_int(val, default=0) across all apple health retrieval tools.
  • In get_apple_health_workouts_tool, normalize startDate via to_datetime(workout.get('startDate')) (consistent with _workout_timestamp and _format_health_date), falling back to timestamp division if numeric.
  • Add comprehensive hermetic unit tests in backend/tests/unit/test_apple_health_null_coercion.py verifying null metric handling, missing keys, and ISO startDate workouts.

Bounty Proposal

  • Proposed tier: $50 (Targeted bug fix + unit test coverage)
  • Settlement: Base Mainnet USDC / Stripe / Singapore Card

Dimank92 avatar Oct 21 '26 19:10 Dimank92

Fix submitted via #23103 with comprehensive hermetic unit tests (7/7 passing).

  • Coerces null metrics across all Apple Health retrieval tools (_safe_float, _safe_int).
  • Normalizes startDate in get_apple_health_workouts_tool for both numeric ms timestamps and ISO 8601 strings.

Payout address (Base USDC): 0xb95e2AD8E9D9e9BF902c7531577Cd0E1ceB203D4

Booshdag-io avatar Oct 21 '26 19:10 Booshdag-io

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