fix(memory): handle timezone-aware created_at in compute_composite_score#6161
fix(memory): handle timezone-aware created_at in compute_composite_score#6161i-anubhav-anand wants to merge 1 commit into
Conversation
The recency age was computed as `datetime.utcnow() - record.created_at`. `utcnow()` returns a naive datetime, so when a MemoryRecord is created with a timezone-aware `created_at` (e.g. `datetime.now(timezone.utc)`, the modern idiom) the subtraction raises `TypeError: can't subtract offset-naive and offset-aware datetimes`. Use a timezone-aware now and treat a naive `created_at` as UTC before subtracting, so both the (naive) default factory and aware values work. This also drops the deprecated `datetime.utcnow()` call from the scoring path. Adds tests covering aware and naive `created_at`.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesTimezone-safe memory age calculation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
compute_composite_score(crewai.memory.types) computed the recency age as:datetime.utcnow()returns a naive datetime. When aMemoryRecordis created with a timezone-awarecreated_at— e.g.datetime.now(timezone.utc), the modern/recommended idiom — this subtraction raises:Reproduction
Fix
Use a timezone-aware
nowand treat a naivecreated_atas UTC before subtracting, so both the (naive) default-factory value and aware values work. This also removes the deprecateddatetime.utcnow()call from the scoring path (utcnow()is deprecated as of Python 3.12).Testing
tests/memory/test_types.pycovering aware and naivecreated_at. The aware case fails before this change (TypeError) and passes after.uv run pytest tests/memory/test_types.py→ 2 passed.ruff check/ruff format --checkclean on touched files.Summary by CodeRabbit
Bug Fixes
Tests