Skip to content

Enhance memory reset functionality and JSON crew handling - #6195

Merged
joaomdmoura merged 4 commits into
mainfrom
joaomdmoura/reset-memory-json
Jun 17, 2026
Merged

Enhance memory reset functionality and JSON crew handling#6195
joaomdmoura merged 4 commits into
mainfrom
joaomdmoura/reset-memory-json

Conversation

@joaomdmoura

@joaomdmoura joaomdmoura commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator
  • Added reset_all method to the Memory class to reset the entire memory store, ignoring root_scope.
  • Updated the Crew class to utilize reset_all when resetting memory.
  • Enhanced the _reset_flow_memory function to check for Memory instances and call reset_all accordingly.
  • Introduced helper functions to load JSON crew configurations and handle project declarations, improving the reset command's flexibility.
  • Added tests to validate the new JSON crew memory reset behavior and ensure proper handling of declared flow projects.

Note

Medium Risk
Changes persistent memory wipe semantics and concurrent save behavior during reset; incorrect locking could cause data loss or stuck saves, though coverage targets the race.

Overview
Improves unified memory reset so CLI and crew/flow paths clear the whole backing store (not only root_scope), and coordinates resets with background saves. Bumps the optional litellm extra to >=1.84.0,<1.85 (lockfile updated).

Memory: Adds reset_all() (full storage wipe via scope_prefix=None). reset() and reset_all() now take an _reset_lock, call drain_writes() first, and _submit_save runs under the same lock so new saves cannot slip in mid-reset. Crew and flow reset paths call reset_all() for Memory instances.

CLI reset-memories: Discovers JSON-first crews from crew.jsonc (via load_crew) and appends them to the crew list; skips that path when pyproject.toml declares type = "flow". Invalid JSON crews log a skip message without blocking classic crews.

Tests cover full reset for crews/flows, reset/save ordering, JSON crew scenarios, and reset_all after embedding dimension mismatch.

Reviewed by Cursor Bugbot for commit 65f357e. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved reliability of unified memory resets by coordinating with background save activity to prevent race conditions.
    • Unified memory resets now follow the full-reset path for applicable flow and crew scenarios.
    • JSON crew handling during memory reset is more robust: invalid JSON won’t block classic resets, and flow-typed projects skip JSON loading appropriately.
  • Tests

    • Expanded CLI and unified memory test coverage, including concurrency blocking and reset-all behavior.
  • Chores

    • Updated the optional litellm version range.

- Added `reset_all` method to the `Memory` class to reset the entire memory store, ignoring `root_scope`.
- Updated the `Crew` class to utilize `reset_all` when resetting memory.
- Enhanced the `_reset_flow_memory` function to check for `Memory` instances and call `reset_all` accordingly.
- Introduced helper functions to load JSON crew configurations and handle project declarations, improving the reset command's flexibility.
- Added tests to validate the new JSON crew memory reset behavior and ensure proper handling of declared flow projects.
@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d9f57ea8-68ee-40fa-9c62-33d4a78eacbf

📥 Commits

Reviewing files that changed from the base of the PR and between d2e5775 and 65f357e.

⛔ Files ignored due to path filters (1)
  • uv.lock is excluded by !**/*.lock
📒 Files selected for processing (2)
  • lib/crewai/pyproject.toml
  • pyproject.toml
✅ Files skipped from review due to trivial changes (1)
  • pyproject.toml

📝 Walkthrough

Walkthrough

Memory.reset() now drains background writes before resetting, and a new Memory.reset_all() method resets the entire backing store ignoring root_scope. Both operations use _reset_lock to serialize with background save submission. Crew._get_memory_systems() and _reset_flow_memory call reset_all() for Memory instances. reset_memories_command gains _current_project_declares_flow() and _get_json_crew() to conditionally load JSON-first crews when the project is not declared a flow. Litellm dependency is updated, and OpenAI override comments clarified.

Changes

Memory Reset API and Project-Aware Reset Command

Layer / File(s) Summary
Memory.reset() drain + reset_all() with background save sync
lib/crewai/src/crewai/memory/unified_memory.py
_reset_lock (re-entrant) coordinates reset and save submission. reset() acquires the lock, drains pending saves, computes effective_scope respecting root_scope, and resets storage. New reset_all() drains and resets the entire store with scope_prefix=None (ignoring root_scope). _submit_save() acquires the same lock to serialize submissions with concurrent resets. __deepcopy__ treats _reset_lock specially.
Crew and reset_memories wiring for reset_all() with JSON crew
lib/crewai/src/crewai/crew.py, lib/crewai/src/crewai/utilities/reset_memories.py
Crew._get_memory_systems() detects Memory instances and calls reset_all(). reset_memories.py imports Memory and crew loaders. _reset_flow_memory calls reset_all() on Memory instances. _current_project_declares_flow() reads pyproject type. _get_json_crew() loads JSON crew only when project is not declared a flow. reset_memories_command appends loaded crew before applying reset flags.
Concurrency test: reset-save serialization
lib/crewai/tests/memory/test_unified_memory.py
Validates _reset_lock serialization: reset_all() blocks new _submit_save() calls until reset finishes using event-based storage blocking and thread ordering assertions.
Integration and edge-case tests
lib/crewai/tests/memory/test_dimension_mismatch.py, lib/crewai/tests/test_crew.py
Dimension-mismatch test verifies reset_all() rebuilds LanceDB with new embedder dimension. Crew test asserts reset_all() is called for unified memory reset.
Dependency and configuration updates
lib/crewai/pyproject.toml, pyproject.toml
Updates litellm from >=1.83.7,<1.84 to >=1.84.0,<1.85. Clarifies OpenAI override rationale in root pyproject comments.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant Crew
  participant Memory
  participant ResetLock
  participant Storage

  Client->>Crew: reset_memories(command_type="memory")
  Crew->>Memory: reset_all()
  Memory->>ResetLock: acquire()
  Memory->>Memory: drain_writes()
  Memory->>Storage: reset(scope_prefix=None)
  Memory->>ResetLock: release()

  par Concurrent background save
    Client->>Memory: _submit_save()
    Memory->>ResetLock: acquire() [waits for reset]
    Memory->>Storage: submit/sync
    Memory->>ResetLock: release()
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested reviewers

  • vinibrsl
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly summarizes the primary changes: enhancing memory reset functionality (new reset_all method, concurrency safety, unified semantics) and JSON crew handling (loading from crew.jsonc, project type detection).
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
  • Commit unit tests in branch joaomdmoura/reset-memory-json

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.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 6c185d2. Configure here.

Comment thread lib/crewai/src/crewai/utilities/reset_memories.py

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
lib/crewai/tests/cli/test_cli.py (1)

182-191: ⚡ Quick win

Add a focused test for the new Memory-specific reset_all() path.

Current flow reset tests exercise the generic .reset() path, but not the new isinstance(mem, Memory) -> reset_all() branch. A direct test would lock in the behavior introduced in this PR.

Example test shape
+from crewai.memory.unified_memory import Memory
...
+def test_reset_flow_unified_memory_uses_reset_all(runner):
+    mock_flow = mock.Mock()
+    mock_flow.name = "TestFlow"
+    mem = mock.Mock(spec=Memory)
+    mock_flow.memory = mem
+
+    with mock.patch("crewai.utilities.reset_memories.get_crews", return_value=[]), \
+         mock.patch("crewai.utilities.reset_memories.get_flows", return_value=[mock_flow]), \
+         mock.patch("crewai.utilities.reset_memories._get_json_crew", return_value=None):
+        runner.invoke(reset_memories, ["-m"])
+
+    mem.reset_all.assert_called_once()
+    mem.reset.assert_not_called()

Also applies to: 253-266

🤖 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/tests/cli/test_cli.py` around lines 182 - 191, The current tests
test_reset_flow_memory and test_reset_flow_all_memories only exercise the
generic .reset() path but do not directly test the new isinstance(mem, Memory)
-> reset_all() code branch. Add a focused test that specifically verifies the
Memory-specific reset_all() path is called when resetting memories. Create a new
test that mocks a Memory instance and asserts that reset_all() is invoked
instead of the generic reset() method. Apply the same test addition to the
similar code section referenced at lines 253-266 to ensure comprehensive
coverage of the new Memory-specific behavior.
🤖 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/unified_memory.py`:
- Around line 993-1004: The reset and reset_all methods are not atomic against
concurrent background saves because drain_writes() only waits for a snapshot of
pending futures at that moment, allowing new saves to be submitted between the
drain_writes() call and the self._storage.reset() call, which can repopulate the
storage after reset. To fix this, implement a synchronization mechanism that
prevents new writes from being submitted during the reset operation, such as
acquiring a lock before calling drain_writes() in both the reset method and
reset_all method, and releasing it only after self._storage.reset() completes,
ensuring no writes can arrive between draining and resetting.

In `@lib/crewai/src/crewai/utilities/reset_memories.py`:
- Around line 66-67: The load_crew function call in the crew reset operation can
raise exceptions due to invalid JSON/JSONC or schema validation issues, causing
the entire reset command to abort and preventing other crews/flows from being
processed. Wrap the load_crew(crew_path) call at line 66 in a try-except block
to catch exceptions gracefully, log the error appropriately, and continue
processing remaining crews instead of aborting. Apply the same error handling
pattern to the similar code block at lines 93-96 to ensure consistent behavior
throughout the reset operation.

---

Nitpick comments:
In `@lib/crewai/tests/cli/test_cli.py`:
- Around line 182-191: The current tests test_reset_flow_memory and
test_reset_flow_all_memories only exercise the generic .reset() path but do not
directly test the new isinstance(mem, Memory) -> reset_all() code branch. Add a
focused test that specifically verifies the Memory-specific reset_all() path is
called when resetting memories. Create a new test that mocks a Memory instance
and asserts that reset_all() is invoked instead of the generic reset() method.
Apply the same test addition to the similar code section referenced at lines
253-266 to ensure comprehensive coverage of the new Memory-specific behavior.
🪄 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: 096cebce-c64c-459b-8e80-8e3fc35e33bf

📥 Commits

Reviewing files that changed from the base of the PR and between ee4f853 and 6c185d2.

📒 Files selected for processing (4)
  • lib/crewai/src/crewai/crew.py
  • lib/crewai/src/crewai/memory/unified_memory.py
  • lib/crewai/src/crewai/utilities/reset_memories.py
  • lib/crewai/tests/cli/test_cli.py

Comment thread lib/crewai/src/crewai/memory/unified_memory.py Outdated
Comment thread lib/crewai/src/crewai/utilities/reset_memories.py Outdated
@github-actions github-actions Bot added size/L and removed size/S labels Jun 17, 2026
@joaomdmoura

Copy link
Copy Markdown
Collaborator Author

Addressed the CodeRabbit summary nitpick in d2e5775 as well: added test_reset_flow_unified_memory_uses_full_reset to verify the flow reset path calls Memory.reset_all() rather than the generic reset() branch. Also added coverage for JSON load failures, reset atomicity, and the old/new embedding dimension migration path.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
lib/crewai/tests/memory/test_unified_memory.py (1)

981-1002: ⚡ Quick win

Assert the save body cannot run before reset completes.

This test currently proves _submit_save() does not return during reset, but it would miss a regression where the future is submitted before taking _reset_lock and only returns later. Wait for the submitted save and assert "save" occurs after "reset-end".

Strengthen the concurrency assertion
     reset_thread.join(timeout=2)
     submit_thread.join(timeout=2)
+    mem.drain_writes()
 
     assert not reset_thread.is_alive()
     assert not submit_thread.is_alive()
     assert order.index("reset-end") < order.index("submit-returned")
+    assert order.index("reset-end") < order.index("save")
🤖 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/tests/memory/test_unified_memory.py` around lines 981 - 1002, The
test currently only verifies that the _submit_save() call returns after reset
completes, but does not verify that the actual save body executes after reset.
This misses a regression where the future is submitted before acquiring
_reset_lock. Add code to wait for the submitted save operation (the lambda that
appends "save") to complete execution, then add an assertion to verify that the
"save" entry appears after "reset-end" in the order list, similar to the
existing assertion for "submit-returned".
🤖 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/tests/memory/test_unified_memory.py`:
- Around line 981-1002: The test currently only verifies that the _submit_save()
call returns after reset completes, but does not verify that the actual save
body executes after reset. This misses a regression where the future is
submitted before acquiring _reset_lock. Add code to wait for the submitted save
operation (the lambda that appends "save") to complete execution, then add an
assertion to verify that the "save" entry appears after "reset-end" in the order
list, similar to the existing assertion for "submit-returned".

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ae6f7c7c-0f19-46db-b8c7-d8144105cb53

📥 Commits

Reviewing files that changed from the base of the PR and between 6c185d2 and d2e5775.

📒 Files selected for processing (6)
  • lib/crewai/src/crewai/memory/unified_memory.py
  • lib/crewai/src/crewai/utilities/reset_memories.py
  • lib/crewai/tests/cli/test_cli.py
  • lib/crewai/tests/memory/test_dimension_mismatch.py
  • lib/crewai/tests/memory/test_unified_memory.py
  • lib/crewai/tests/test_crew.py
✅ Files skipped from review due to trivial changes (1)
  • lib/crewai/tests/memory/test_dimension_mismatch.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/crewai/src/crewai/utilities/reset_memories.py

@joaomdmoura
joaomdmoura merged commit d3c37c4 into main Jun 17, 2026
57 checks passed
@joaomdmoura
joaomdmoura deleted the joaomdmoura/reset-memory-json branch June 17, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants