Skip to content

fix(action-items): coerce naive and ISO string timestamps in sort keys to prevent HTTP 500 (#22181) - #22218

Merged
aryanorastar merged 7 commits into
BasedHardware:mainfrom
hardik4love-lang:fix/action-items-timestamp-coercion-22181
Oct 29, 2026
Merged

aryanorastar merged 7 commits into
BasedHardware:mainfrom
hardik4love-lang:fix/action-items-timestamp-coercion-22181

Conversation

@hardik4love-lang

@hardik4love-lang hardik4love-lang commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

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, and get_scores deleted items filter) to timezone-aware UTC datetimes, preventing TypeError and AttributeError crashes that lead to HTTP 500 on action-item endpoints.

Verification

  • Passing in test_action_items_null_timestamps.py covering mixed string, naive datetime, and aware datetime sort and window filtering verification.
  • Formatted with Black 26.5.1.

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]

hardik4love-lang added 3 commits October 18, 2026 20:19
…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]
@hardik4love-lang

Copy link
Copy Markdown
Contributor Author

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 Git-on-my-level added the backend Backend Task (python) label Oct 20, 2026

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in action_item_codec.py), ISO string (with the same Z → +00:00 idiom 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):

  1. 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_read only normalizes values that have .timestamp(), so an ISO-string updated_at passes through untouched and still raises TypeError here → the same HTTP 500 class this PR's title says it prevents.
  2. get_scores deleted-items subtraction (~L1583): isinstance(created_at, datetime) and week_start <= created_at < day_end — a naive created_at passes 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 the updated_at sort in get_pending_apple_reminders_sync and to due_at/created_at in get_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.

@hardik4love-lang

Copy link
Copy Markdown
Contributor Author

Thanks @Git-on-my-level for the thorough review!

Addressed both remaining crash surfaces in commit \61e6a63002:

  1. Apple Reminders sync sort: Updated \get_pending_apple_reminders_sync\ to sort using _coerce_item_datetime(x.get('updated_at'), datetime.min.replace(tzinfo=timezone.utc)), safely normalizing ISO string and naive timestamps without raising TypeError.
  2. Scores deleted subtraction: Updated \get_scores\ to coerce both \due_at\ and \created_at\ with _coerce_item_datetime, allowing naive datetimes to compare properly against aware window boundaries and correctly accounting for deleted items.
  3. Unit Tests: Added regression tests in \ est_action_items_null_timestamps.py\ verifying Apple Reminders sorting and scores deleted-items window filtering with ISO strings and naive timestamps.

All three surfaces in #22181 are now completely covered and protected!

@hardik4love-lang

Copy link
Copy Markdown
Contributor Author

Hi @Git-on-my-level,

All requested crash spots from #22181 have been addressed and validated:

  1. \get_pending_apple_reminders_sync\ sort key wrapped with _coerce_item_datetime(x.get('updated_at'), ...).
  2. \get_scores\ timestamp arithmetic coerced with _coerce_item_datetime\ across both \due_at\ and \created_at\ bounds.
  3. Added Python @overload\ signatures for _coerce_item_datetime\ ensuring strict Pyright type safety without
    eportOptionalMemberAccess\ violations.
  4. Hermetic regression tests added in \ est_action_items_null_timestamps.py.
  5. PR metadata preflight ratchet exception included.

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 Git-on-my-level left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Git-on-my-level
Git-on-my-level dismissed their stale review October 21, 2026 12:39

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.

@Git-on-my-level Git-on-my-level added the positive-signal Automation verified a genuine fix/quality contribution label Oct 21, 2026
@aryanorastar
aryanorastar merged commit bc7ac70 into BasedHardware:main Oct 29, 2026
52 checks passed
@hardik4love-lang

Copy link
Copy Markdown
Contributor Author

Thank you @aryanorastar, @Git-on-my-level, and @kodjima33 for reviewing and merging!

Bounty Settlement Details

@kodjima33 @josancamon19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python) positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bounty proposal] fix(action-items): sort and score aggregation crashes with TypeError / AttributeError on ISO string or naive timestamps ( proposed)

3 participants