crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

fix: use timezone-aware memory timestamps

Open Ghraven opened this issue 6 months ago • 2 comments

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.py
  • git 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.

Review Change Stack

Ghraven avatar May 24 '26 02:05 Ghraven

📝 Walkthrough

Walkthrough

This PR migrates the memory module from naive UTC datetimes to timezone-aware UTC timestamps. All datetime creation and recency calculations use datetime.now(timezone.utc) with a helper to normalize legacy naive datetimes; tests and LanceDB storage touch/schema timestamps were updated accordingly.

Changes

Timezone-aware UTC timestamp migration

Layer / File(s) Summary
LanceDB storage layer updates
lib/crewai/src/crewai/memory/storage/lancedb_storage.py
LanceDB 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.py
Added _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.py
Composite-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

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped through timestamps, snug and neat,

replaced the naive with UTC complete.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 24 '26 02:05 coderabbitai[bot]

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.

Ghraven avatar May 24 '26 17:05 Ghraven