Repository navigation
fix(action-items): coerce naive and ISO string timestamps in sort keys to prevent HTTP 500 (#22181) - #22218
Conversation
…s to prevent HTTP 500 (BasedHardware#22181) Action items loaded from Firestore can contain ISO string timestamps from sync integrations or naive datetimes. Comparing ISO strings with datetimes in _action_item_list_sort_key raised TypeError and calling .timestamp() on string created_at raised AttributeError, crashing all caller endpoints with HTTP 500. Coerce timestamps to aware UTC datetimes in _action_item_list_sort_key. Add hermetic regression unit tests in test_action_items_null_timestamps.py. Claim bounty: PayPal [email protected]
|
All CI checks (unit test shards 1-6, backend emulator proofs, formatting, hygiene) have completed with 100% pass rate. Test coverage for ISO string and naive datetime coercion is verified. Ready for maintainer review. |
Git-on-my-level
left a comment
There was a problem hiding this comment.
The core sort-key fix is correct and nicely tested, but this covers only one of the three crash surfaces enumerated in #22181 — two verified-unfixed spots remain, so I'm requesting changes before this merges as "Fixes #22181".
What's good:
- backend/database/action_items.py —
_coerce_item_datetime(L492) handles all four shapes correctly: None → default, naive → assumed-UTC (consistent with the write-side normalization inaction_item_codec.py), ISO string (with the sameZ→+00:00idiom the codec uses) → parsed and UTC-assumed if naive, anything unparseable → default._action_item_list_sort_key(L506) now compares only aware datetimes, so the mixed str/datetime and naive/aware TypeError paths from the issue are gone. Ordering for previously-valid aware data is unchanged. - backend/tests/unit/test_action_items_null_timestamps.py — the new test sorts mixed string/naive/aware items and pins the exact resulting order; Backend unit suite green on head.
Still crashing on this head (both from the issue, verified against the head file):
get_pending_apple_reminders_sync(action_items.py L1410):synced_items.sort(key=lambda x: x.get('updated_at') or datetime.min...)—prepare_action_item_for_readonly normalizes values that have.timestamp(), so an ISO-stringupdated_atpasses through untouched and still raises TypeError here → the same HTTP 500 class this PR's title says it prevents.get_scoresdeleted-items subtraction (~L1583):isinstance(created_at, datetime) and week_start <= created_at < day_end— a naivecreated_atpasses the isinstance guard and then raises TypeError against the aware window bounds; string timestamps are silently skipped, skewing the subtraction.
Since the PR body says "Fixes #22181", merging as-is would auto-close the issue with those two surfaces still broken.
Ask (either is fine):
- Apply
_coerce_item_datetime(it's already in the right module) to theupdated_atsort inget_pending_apple_reminders_syncand todue_at/created_atinget_scores' deleted subtraction, with one test each; or - Keep this PR minimal and change "Fixes #22181" to "Part of #22181" so the issue stays open for the remaining surfaces.
Blast radius: action-item list endpoints (fixed here) vs Apple Reminders sync + score endpoints (still exposed). Performance: O(1) coercion per item inside existing Python-side sorts; no new queries or API calls.
USER IMPACT: action-item lists stop failing with errors for users with older or mixed-format items, but the Apple Reminders sync and score endpoints can still fail until the two remaining spots are coerced.
Thanks for the clean fix and the regression test — completing the other two call sites (or adjusting the issue linkage) should get this home.
by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.
|
Thanks @Git-on-my-level for the thorough review! Addressed both remaining crash surfaces in commit \61e6a63002:
All three surfaces in #22181 are now completely covered and protected! |
|
Hi @Git-on-my-level, All requested crash spots from #22181 have been addressed and validated:
CI Status: 100% GREEN (Backend unit suite, Hygiene, Formatting, Backend Hermetic Merge Gate passing). Ready for re-review and merge! Thank you. |
Git-on-my-level
left a comment
There was a problem hiding this comment.
Since my last review: addressed (both remaining crash surfaces — get_pending_apple_reminders_sync updated_at sort and get_scores deleted-items window checks — now coerce via _coerce_item_datetime, verified on this head) · still open (nothing) · newly introduced (nothing).
This now covers all three timestamp-comparison surfaces from #22181, so the "Fixes" linkage is accurate — from my side it's ready for maintainer merge.
- backend/database/action_items.py, get_pending_apple_reminders_sync (~L1499): sort key is now _coerce_item_datetime(x.get('updated_at'), datetime.min...) — ISO-string and naive updated_at values no longer raise TypeError on this endpoint.
- backend/database/action_items.py, get_scores deleted subtraction (~L1682): due_at and created_at are both coerced with a None default and is-not-None window guards. Naive values are assumed UTC (consistent with the write-side normalization in action_item_codec.py), so window comparisons no longer raise, and legacy string timestamps are now correctly counted instead of silently skipped — a small, intended correction to score totals for older data.
- _action_item_list_sort_key: unchanged since my last review and still correct; all four value shapes (None, naive, ISO string, unparseable) land on comparable aware datetimes.
- backend/tests/unit/test_action_items_null_timestamps.py: the two new tests exercise both call sites directly, including the reverse=True ordering and window boundary arithmetic; Backend unit suite and the hermetic merge gate are green on head.
Performance: O(1) coercion per item inside existing Python-side sorts — no new queries or external calls. Blast radius: action-item list, Apple Reminders sync, and score endpoints; ordering of previously-valid aware data is unchanged.
USER IMPACT: users with older or mixed-format action items stop seeing errors on action-item lists, Apple Reminders sync, and scores; score totals may shift slightly for legacy items that were previously skipped — that's the corrected behavior.
Only optional nicety left: a test asserting an unparseable string falls back to the default (current tests cover None/naive/ISO-string shapes).
by AI on behalf of David — no concerns left from automated review; leaving the merge call to a human maintainer.
Dismissing: both blockers from this review (uncoerced updated_at sort in get_pending_apple_reminders_sync and naive/string timestamps in get_scores deleted-window checks) are fixed on head be6ccaa via _coerce_item_datetime, with new tests; automated review of the new head found no remaining concerns.
|
Thank you @aryanorastar, @Git-on-my-level, and @kodjima33 for reviewing and merging! Bounty Settlement Details
|
Summary
Fixes #22181.
Coerces ISO string and naive datetimes across all action-item sort and comparison paths (
_action_item_list_sort_key,get_pending_apple_reminders_sync, andget_scoresdeleted items filter) to timezone-aware UTC datetimes, preventingTypeErrorandAttributeErrorcrashes that lead to HTTP 500 on action-item endpoints.Verification
test_action_items_null_timestamps.pycovering mixed string, naive datetime, and aware datetime sort and window filtering verification.Line-Count-Exception: backend/database/action_items.py | 1715 -> 1742 | coerce naive and ISO string timestamps in sort key
Failure-Class: none
Proposed bounty payout: PayPal
[email protected]