Files
crewAI/lib/crewai/tests/utilities/test_tools_prompt_strategy.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

96 lines
2.9 KiB
Python

"""Tests for tools_prompt_strategy configuration."""
from unittest.mock import Mock
import pytest
from crewai.utilities.agent_utils import get_tool_names, render_text_description_and_args
def test_get_tool_names_returns_comma_separated_names():
"""Test that get_tool_names returns comma-separated tool names."""
tool1 = Mock()
tool1.name = "search_tool"
tool2 = Mock()
tool2.name = "calculator_tool"
tool3 = Mock()
tool3.name = "file_reader_tool"
tools = [tool1, tool2, tool3]
result = get_tool_names(tools)
assert result == "search_tool, calculator_tool, file_reader_tool"
assert "description" not in result.lower()
def test_render_text_description_includes_descriptions():
"""Test that render_text_description_and_args includes full descriptions."""
tool1 = Mock()
tool1.description = "This is a search tool that searches the web for information"
tool2 = Mock()
tool2.description = "This is a calculator tool that performs mathematical operations"
tools = [tool1, tool2]
result = render_text_description_and_args(tools)
assert "search tool" in result
assert "calculator tool" in result
assert "searches the web" in result
assert "mathematical operations" in result
def test_names_only_strategy_is_shorter_than_full():
"""Test that names_only strategy produces shorter output than full descriptions."""
tool1 = Mock()
tool1.name = "search_tool"
tool1.description = "This is a very long description " * 10
tool2 = Mock()
tool2.name = "calculator_tool"
tool2.description = "This is another very long description " * 10
tools = [tool1, tool2]
names_only = get_tool_names(tools)
full_description = render_text_description_and_args(tools)
assert len(names_only) < len(full_description)
assert len(names_only) < 100
assert len(full_description) > 200
def test_agent_tools_prompt_strategy_config():
"""Test that Agent has tools_prompt_strategy configuration field."""
from crewai.agent import Agent
agent_full = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory",
tools_prompt_strategy="full",
)
agent_names = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory",
tools_prompt_strategy="names_only",
)
assert hasattr(agent_full, "tools_prompt_strategy")
assert hasattr(agent_names, "tools_prompt_strategy")
assert agent_full.tools_prompt_strategy == "full"
assert agent_names.tools_prompt_strategy == "names_only"
def test_tools_prompt_strategy_default_is_full():
"""Test that tools_prompt_strategy defaults to 'full'."""
from crewai.agent import Agent
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory",
)
assert agent.tools_prompt_strategy == "full"