Files
crewAI/lib/crewai/tests/utilities/test_memory_knowledge_truncation.py
Devin AI 6ae74f0ad9 Add compact mode and context management features to address issue #3912
- Add 5 new Agent configuration fields:
  - compact_mode: Enable compact prompt mode to reduce context size
  - tools_prompt_strategy: Choose between 'full' or 'names_only' for tools
  - proactive_context_trimming: Enable proactive message trimming
  - memory_max_chars: Cap memory context length
  - knowledge_max_chars: Cap knowledge context length

- Implement compact prompt mode in utilities/prompts.py
  - Caps role to 100 chars, goal to 150 chars
  - Omits backstory entirely in compact mode

- Implement tools_prompt_strategy in Agent.create_agent_executor
  - 'names_only' uses get_tool_names for minimal tool descriptions
  - 'full' uses render_text_description_and_args (default)

- Implement memory/knowledge size bounds in Agent.execute_task
  - Truncates memory and knowledge contexts when limits are set

- Add trim_messages_structurally helper in agent_utils.py
  - Structural trimming without LLM calls
  - Keeps system messages and last N message pairs

- Integrate proactive trimming in CrewAgentExecutor._invoke_loop
  - Trims messages before each LLM call when enabled

- Update LangGraphAdapter and OpenAIAdapter to honor compact_mode
  - Compacts role/goal/backstory in system prompts

- Add comprehensive tests for all new features:
  - test_prompts_compact_mode.py
  - test_memory_knowledge_truncation.py
  - test_tools_prompt_strategy.py
  - test_proactive_context_trimming.py

All changes are opt-in with conservative defaults to maintain backward compatibility.

Fixes #3912

Co-Authored-By: João <joao@crewai.com>
2025-11-14 06:57:47 +00:00

115 lines
3.3 KiB
Python

"""Tests for memory and knowledge truncation."""
from unittest.mock import Mock, patch
import pytest
def test_truncate_text_helper():
"""Test basic text truncation helper logic."""
text = "A" * 1000
max_chars = 500
if len(text) > max_chars:
truncated = text[:max_chars] + "..."
assert len(truncated) == max_chars + 3
assert truncated.endswith("...")
assert truncated.startswith("A" * 100)
def test_memory_truncation_when_max_chars_set():
"""Test that memory is truncated when memory_max_chars is set."""
from crewai.agent import Agent
long_memory = "M" * 2000
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory",
memory_max_chars=1000,
)
if agent.memory_max_chars and len(long_memory) > agent.memory_max_chars:
truncated_memory = long_memory[:agent.memory_max_chars] + "..."
assert len(truncated_memory) == 1003
assert truncated_memory.endswith("...")
def test_memory_not_truncated_when_max_chars_none():
"""Test that memory is not truncated when memory_max_chars is None."""
from crewai.agent import Agent
long_memory = "M" * 2000
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory",
memory_max_chars=None,
)
result_memory = long_memory
if agent.memory_max_chars and len(long_memory) > agent.memory_max_chars:
result_memory = long_memory[:agent.memory_max_chars] + "..."
assert len(result_memory) == 2000
assert not result_memory.endswith("...")
def test_knowledge_truncation_when_max_chars_set():
"""Test that knowledge is truncated when knowledge_max_chars is set."""
from crewai.agent import Agent
long_knowledge = "K" * 3000
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory",
knowledge_max_chars=1500,
)
if agent.knowledge_max_chars and len(long_knowledge) > agent.knowledge_max_chars:
truncated_knowledge = long_knowledge[:agent.knowledge_max_chars] + "..."
assert len(truncated_knowledge) == 1503
assert truncated_knowledge.endswith("...")
def test_knowledge_not_truncated_when_max_chars_none():
"""Test that knowledge is not truncated when knowledge_max_chars is None."""
from crewai.agent import Agent
long_knowledge = "K" * 3000
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory",
knowledge_max_chars=None,
)
result_knowledge = long_knowledge
if agent.knowledge_max_chars and len(long_knowledge) > agent.knowledge_max_chars:
result_knowledge = long_knowledge[:agent.knowledge_max_chars] + "..."
assert len(result_knowledge) == 3000
assert not result_knowledge.endswith("...")
def test_agent_config_fields_exist():
"""Test that new configuration fields exist on Agent."""
from crewai.agent import Agent
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory",
memory_max_chars=1000,
knowledge_max_chars=2000,
)
assert hasattr(agent, "memory_max_chars")
assert hasattr(agent, "knowledge_max_chars")
assert agent.memory_max_chars == 1000
assert agent.knowledge_max_chars == 2000