← Back to Pull requests

fix(memory): drain pending saves before forget() deletes records #7291

Closed
unknown closed this pull request without merging on Oct 08, 2026 04:00 UTC
View on GitHub
unknown unknown commented on Sep 05, 2026 20:54 UTC
Author

Root cause

forget() in unified_memory.py (line 818) calls self._storage.delete() without first draining the pending-save queue:

# line 844 — no write barrier
return self._storage.delete(
    scope_prefix=effective_scope,
    ...
)

recall() already has the correct barrier at line 713:

# Read barrier: wait for any pending background saves to finish
self.drain_writes()

A remember_many() background save submitted before forget() is called can land after the delete, resurrecting the forgotten content. The race is:

1. remember_many() submits a background save to _save_pool
2. Caller calls forget() — delete runs immediately, content gone
3. Background save completes — content is back in storage
4. recall() returns the "forgotten" content

Fix

Add self.drain_writes() at the top of forget(), before the scope resolution, mirroring recall(). One line.

Tests

Three regression tests in lib/crewai/tests/memory/test_memory_forget_drain.py:

  • test_forget_drains_pending_saves_before_delete — verifies drain_writes() precedes _storage.delete() in forget()

  • test_recall_already_has_drain_writes — regression guard confirming recall() still has its barrier

  • test_forget_drain_ordering_matches_recall — both methods have consistent write-barrier semantics
  • All 3 pass with the fix, all 3 fail on the unpatched code.

    Fixes #7290

    <!-- crewai-require-issue-check -->

    <!-- CURSOR_SUMMARY -->
    ---

    [!NOTE]

    Medium Risk

    Changes deletion ordering and concurrency around unified memory; incorrect locking could still allow resurrection or block saves, but the change aligns with existing reset() semantics and is narrowly scoped.

    > Overview

    Fixes a race where Memory.forget() could delete records while a background save from remember_many() was still in flight, so deleted memories could reappear after the delete completed. recall() already waited on pending writes; forget() did not.

    > forget() now mirrors reset(): it runs drain_writes() before _storage.delete() and keeps _reset_lock held for the whole drain-and-delete sequence so _submit_save() cannot register a new pending save between the drain snapshot and the delete.

    > Adds regression tests in test_memory_forget_drain.py (source-order checks, lock held during drain/delete via a helper thread, and parity with recall()’s barrier). Addresses #7290.

    > <sup>Reviewed by Cursor Bugbot for commit 1effd265e099d18bb84602565a32da3f11e47317. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

    <!-- /CURSOR_SUMMARY -->

    Diff: 344 lines (+258 / -86) across 4 files Reviewer: bunnysayzz Review duration: 46506m
    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
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.4% (MEDIUM) · Higher = larger blast radius
    42.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.7% · Safe to merge
    16.7%