fix: use timezone-aware memory timestamps
Summary
- replace remaining naive
datetime.utcnow()calls in the unified memory runtime with timezone-aware UTC timestamps - update the matching memory tests to construct timezone-aware UTC datetimes
Before / after
Before, memory records and LanceDB rows could be created or scored with naive UTC datetimes from datetime.utcnow().
After, these code paths use datetime.now(timezone.utc), preserving explicit UTC offsets in serialized timestamps.
Verification
python -m py_compile lib/crewai/src/crewai/memory/unified_memory.py lib/crewai/src/crewai/memory/types.py lib/crewai/src/crewai/memory/encoding_flow.py lib/crewai/src/crewai/memory/storage/lancedb_storage.py lib/crewai/tests/memory/test_unified_memory.pygit diff --check
Summary by CodeRabbit
-
Bug Fixes
- Standardized timestamps to use timezone-aware UTC across memory operations, improving accuracy and consistency of time-based behavior and scoring; maintains compatibility with legacy naive timestamps.
-
Tests
- Updated memory test suite to reflect timezone-aware timestamp handling and added a test ensuring legacy naive datetimes remain supported.
This PR migrates the memory module from naive UTC datetimes to timezone-aware UTC timestamps. All datetime creation and recency calculations use Timezone-aware UTC timestamp migration 🎯 3 (Moderate) | ⏱️ ~20 minutes 🐰 I hopped through timestamps, snug and neat, replaced the naive with UTC complete. ✏️ Tip: You can configure your own custom pre-merge checks in the settings.📝 Walkthrough
Walkthrough
datetime.now(timezone.utc) with a helper to normalize legacy naive datetimes; tests and LanceDB storage touch/schema timestamps were updated accordingly.Changes
Layer / File(s)
Summary
LanceDB storage layer updates
lib/crewai/src/crewai/memory/storage/lancedb_storage.pyLanceDB storage backend now imports
timezone and uses datetime.now(timezone.utc) for schema placeholder timestamps, parsing fallbacks when values are missing, and touch_records write operations.
Memory scoring and API updates
lib/crewai/src/crewai/memory/types.py, lib/crewai/src/crewai/memory/unified_memory.py, lib/crewai/src/crewai/memory/encoding_flow.pyAdded
_as_utc helper and updated compute_composite_score to compute ages against datetime.now(timezone.utc); Memory.update() and the encoding flow now write last_accessed using timezone-aware UTC.
Test suite alignment
lib/crewai/tests/memory/test_unified_memory.pyComposite-scoring tests updated to construct timezone-aware
created_at values and a new test ensures legacy naive created_at=datetime.utcnow() records are still accepted.Estimated code review effort
Poem
Old memories score, both new and shy,
now timezone-aware beneath the sky.
A tiny hop to keep time spry.🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name
Status
Explanation
Description Check
✅ Passed
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
✅ Passed
The title 'fix: use timezone-aware memory timestamps' directly and clearly describes the main change: converting naive UTC datetimes to timezone-aware UTC timestamps across memory system files.
Docstring Coverage
✅ Passed
Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check
✅ Passed
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
✅ Passed
Check skipped because no linked issues were found for this pull request.
✨ 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 @coderabbitai help to get the list of available commands and usage tips.
Thanks for catching this. I pushed a small follow-up that normalizes legacy naive datetimes to UTC before the recency calculation, and added a regression test for that path.