fix(memory): drain pending saves before forget() deletes records #7291
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 344 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.350
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #7291"
general
weight: 0.35
bunnysayzz
Root cause
forget()inunified_memory.py(line 818) callsself._storage.delete()without first draining the pending-save queue:recall()already has the correct barrier at line 713:A
remember_many()background save submitted beforeforget()is called can land after the delete, resurrecting the forgotten content. The race is:1.
remember_many()submits a background save to_save_pool2. Caller calls
forget()— delete runs immediately, content gone3. Background save completes — content is back in storage
4.
recall()returns the "forgotten" contentFix
Add
self.drain_writes()at the top offorget(), before the scope resolution, mirroringrecall(). One line.Tests
Three regression tests in
lib/crewai/tests/memory/test_memory_forget_drain.py:test_forget_drains_pending_saves_before_delete— verifiesdrain_writes()precedes_storage.delete()inforget()test_recall_already_has_drain_writes— regression guard confirmingrecall()still has its barriertest_forget_drain_ordering_matches_recall— both methods have consistent write-barrier semanticsAll 3 pass with the fix, all 3 fail on the unpatched code.
Fixes #7290
<!-- crewai-require-issue-check -->
<!-- CURSOR_SUMMARY -->
---
<!-- /CURSOR_SUMMARY -->