fix: use timezone-aware memory timestamps#5918
Conversation
📝 WalkthroughWalkthroughThis PR migrates the memory module from naive UTC datetimes to timezone-aware UTC timestamps. All datetime creation and recency calculations use ChangesTimezone-aware UTC timestamp migration
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/crewai/src/crewai/memory/types.py`:
- Line 371: compute_composite_score is subtracting datetime.now(timezone.utc)
from MemoryRecord.created_at which can be naive (datetime.utcnow) causing a
TypeError; update the score computation to normalize record timestamps to
timezone-aware UTC before subtraction (e.g., detect if record.created_at or
record.last_accessed is tzinfo is None and convert to UTC-aware via
replace(tzinfo=timezone.utc) or astimezone(timezone.utc)), and apply the same
normalization wherever created_at/last_accessed are used so all datetime
arithmetic in compute_composite_score and related functions operates on
consistent UTC-aware datetimes.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: b764b9ac-76be-48d8-b702-e3a4a84c8b81
📒 Files selected for processing (5)
lib/crewai/src/crewai/memory/encoding_flow.pylib/crewai/src/crewai/memory/storage/lancedb_storage.pylib/crewai/src/crewai/memory/types.pylib/crewai/src/crewai/memory/unified_memory.pylib/crewai/tests/memory/test_unified_memory.py
|
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. |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/crewai/src/crewai/memory/types.py (1)
352-356: 💤 Low valueConsider adding a docstring to document the UTC assumption.
The
_as_utc()helper correctly normalizes naive and tz-aware datetimes to UTC, preventing TypeErrors in datetime arithmetic. The implementation assumes naive datetimes are in UTC, which is valid given the codebase context (legacy timestamps useddatetime.utcnow()).📝 Suggested docstring
def _as_utc(dt: datetime) -> datetime: + """Normalize a datetime to timezone-aware UTC. + + Naive datetimes are assumed to be in UTC and have tzinfo added. + Timezone-aware datetimes are converted to UTC. + + Args: + dt: A naive or timezone-aware datetime. + + Returns: + A timezone-aware datetime in UTC. + """ if dt.tzinfo is None: return dt.replace(tzinfo=timezone.utc) return dt.astimezone(timezone.utc)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/crewai/src/crewai/memory/types.py` around lines 352 - 356, Add a short docstring to the helper function _as_utc() explaining that it normalizes naive and tz-aware datetimes to UTC and that naive datetimes are assumed to already be UTC (matching legacy usage like datetime.utcnow()); include expected input types and returned datetime timezone behavior so callers know this normalization occurs and no timezone conversion is attempted for naive datetimes.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@lib/crewai/src/crewai/memory/types.py`:
- Around line 352-356: Add a short docstring to the helper function _as_utc()
explaining that it normalizes naive and tz-aware datetimes to UTC and that naive
datetimes are assumed to already be UTC (matching legacy usage like
datetime.utcnow()); include expected input types and returned datetime timezone
behavior so callers know this normalization occurs and no timezone conversion is
attempted for naive datetimes.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: e3767868-2f60-452e-bbbb-3d1925b58863
📒 Files selected for processing (2)
lib/crewai/src/crewai/memory/types.pylib/crewai/tests/memory/test_unified_memory.py
Summary
datetime.utcnow()calls in the unified memory runtime with timezone-aware UTC timestampsBefore / 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 --checkSummary by CodeRabbit
Bug Fixes
Tests