Skip to content

Add memory_guard parameter to Crew for write validation (#6043) - #6045

Open
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1780605746-memory-guard-feature
Open

Add memory_guard parameter to Crew for write validation (#6043)#6045
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1780605746-memory-guard-feature

Conversation

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Summary

Implements #6043 — adds a memory_guard parameter to Crew that 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_guard is an Optional[Callable[[str], bool]]. When set, every memory .save() call passes the content string through the guard; if it returns False, the write is silently skipped with a logger.warning.

crew = Crew(
    agents=[researcher, writer],
    tasks=[...],
    memory=True,
    memory_guard=lambda content: "IGNORE ALL INSTRUCTIONS" not in content,
)

Implementation details

  • Memory base class: added memory_guard attribute + check in save() — covers ShortTermMemory, EntityMemory, and UserMemory (all route through super().save()).
  • LongTermMemory.save(): added its own guard check since it bypasses super().save() and writes directly to storage. The guard receives f"{item.task} {item.agent} {item.expected_output}".
  • Crew.create_crew_memory: propagates self.memory_guard to each memory instance after construction.
  • 19 new tests covering all memory types, guard propagation, keyword-based blocking, and content-string correctness.

Link to Devin session: https://app.devin.ai/sessions/b14a03e828e3495bb580f40ba5b35225

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-integration

Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring


from unittest.mock import MagicMock, patch

import pytest
@vgudur-dev

Copy link
Copy Markdown

This is excellent work — exactly the interface I was hoping for when I opened #6043. The Optional[Callable[[str], bool]] signature is clean and framework-agnostic, which means users can plug in any validator.

For anyone looking for a production-ready guard function, agent-memory-guard (the OWASP reference implementation) works directly with this interface:

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:

  1. Coverage across memory types — great that this covers ShortTermMemory, EntityMemory, UserMemory, and LongTermMemory separately. The LongTermMemory.save() bypass of super() is a common footgun, glad it's handled.
  2. Logging on blocklogger.warning is the right level. In production, teams will want to audit what got blocked without it being noisy.
  3. The unused pytest import flagged by code-quality is a trivial fix — happy to submit a follow-up if needed.
    Would love to see this merged. Happy to help with conflict resolution or documentation if the maintainers want a hand. cc @CrewAI team — this addresses a real security gap in multi-agent memory sharing (OWASP ASI-06).

@vgudur-dev

Copy link
Copy Markdown

@devin-ai-integration — Two items to address before this can pass CI:

  1. Remove unused import pytest in tests/memory/test_memory_guard.py (line 5). The code-quality bot already flagged this — just delete the line.
  2. Resolve the merge conflict — the branch has conflicts that need to be resolved against main.
    Once those are fixed, the lint + test checks should be able to run. Happy to help if needed.

@vgudur-dev

Copy link
Copy Markdown

@devin-ai-integration — friendly bump. This PR now has merge conflicts against main. Please rebase, remove the unused import pytest on line 5 of tests/memory/test_memory_guard.py, and force-push. Happy to help if Devin can't handle the rebase.

@github-actions

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open for 45 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant