Add memory_guard parameter to Crew for write validation (#6043) - #6045
Add memory_guard parameter to Crew for write validation (#6043)#6045devin-ai-integration[bot] wants to merge 1 commit into
Conversation
Implements #6043: Add write guards for multi-agent crews to prevent cross-agent memory poisoning. - Add memory_guard field to Crew (Optional[Callable[[str], bool]]) - Integrate guard check into Memory base class save() - Add guard check to LongTermMemory.save() (bypasses super) - Propagate guard from Crew to all memory instances on creation - Log warnings when writes are blocked - Add 19 tests covering all memory types and Crew integration Co-Authored-By: João <joao@crewai.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
|
||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest |
|
This is excellent work — exactly the interface I was hoping for when I opened #6043. The For anyone looking for a production-ready guard function, from agent_memory_guard import scan_text
crew = Crew(
agents=[...],
tasks=[...],
memory=True,
memory_guard=lambda content: scan_text(content).risk_level != "critical"
)It runs heuristic + entropy + semantic similarity checks in ~59µs with zero dependencies — no ML model loading, no network calls. A few observations on the implementation:
|
|
@devin-ai-integration — Two items to address before this can pass CI:
|
|
@devin-ai-integration — friendly bump. This PR now has merge conflicts against main. Please rebase, remove the unused |
|
This PR is stale because it has been open for 45 days with no activity. |
Summary
Implements #6043 — adds a
memory_guardparameter toCrewthat intercepts all memory writes (short-term, long-term, entity, user) before persistence, allowing users to block content that fails validation (e.g. prompt injection payloads).How it works
Crew.memory_guardis anOptional[Callable[[str], bool]]. When set, every memory.save()call passes the content string through the guard; if it returnsFalse, the write is silently skipped with alogger.warning.Implementation details
Memorybase class: addedmemory_guardattribute + check insave()— coversShortTermMemory,EntityMemory, andUserMemory(all route throughsuper().save()).LongTermMemory.save(): added its own guard check since it bypassessuper().save()and writes directly to storage. The guard receivesf"{item.task} {item.agent} {item.expected_output}".Crew.create_crew_memory: propagatesself.memory_guardto each memory instance after construction.Link to Devin session: https://app.devin.ai/sessions/b14a03e828e3495bb580f40ba5b35225