Skip to content

fix(agents): exclude sanitized cache-read tool name from tool cache#6485

Open
Diwak4r wants to merge 1 commit into
crewAIInc:mainfrom
Diwak4r:fix/latent-defect
Open

fix(agents): exclude sanitized cache-read tool name from tool cache#6485
Diwak4r wants to merge 1 commit into
crewAIInc:mainfrom
Diwak4r:fix/latent-defect

Conversation

@Diwak4r

@Diwak4r Diwak4r commented Jul 8, 2026

Copy link
Copy Markdown

Summary

The guard in ToolsHandler.on_tool_use that prevents the built-in cache-read tool ("Hit Cache") from being added to the tool cache compared the raw default name "Hit Cache" against the sanitized runtime name "hit_cache". They never match, so the guard was always True and the cache tool's own output was written into the cache — polluting it with dead entries and risking stale values.

Fix

Compare against sanitize_tool_name(CacheTools().name) so the sanitized names align.

Test

Added test_cache_tool_result_is_not_cached to lib/crewai/tests/tools/test_tool_usage.py. FAILS before the fix, PASSES after. Full tests/tools/test_tool_usage.py = 24 passed + new test (5 pre-existing event-bus timing failures are unrelated to this change).

🤖 Generated with Claude Code
Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com

Copilot AI review requested due to automatic review settings July 8, 2026 18:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Jul 8, 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: 845e4fbb-42d5-471c-b617-f94a0966380a

📥 Commits

Reviewing files that changed from the base of the PR and between dfce3c0 and 573fc42.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/agents/tools_handler.py
  • lib/crewai/tests/tools/test_tool_usage.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • lib/crewai/src/crewai/agents/tools_handler.py
  • lib/crewai/tests/tools/test_tool_usage.py

📝 Walkthrough

Walkthrough

The on_tool_use method now sanitizes the CacheTools name before comparing it with the calling tool name. A regression test verifies that the cache tool’s own result is not stored.

Changes

Cache Tool Name Sanitization

Layer / File(s) Summary
Sanitized comparison in on_tool_use
lib/crewai/src/crewai/agents/tools_handler.py
Imports sanitize_tool_name and applies it to CacheTools().name before comparing with calling.tool_name to determine caching eligibility.
Regression test for cache tool exclusion
lib/crewai/tests/tools/test_tool_usage.py
Updates imports and adds test_cache_tool_result_is_not_cached to confirm the cache tool’s own output is not stored in the cache.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly reflects the main fix: excluding the sanitized cache-read tool name from the tool cache.
Description check ✅ Passed The description directly explains the bug, the fix, and the added test, matching the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@ErenAta16 ErenAta16 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.

Confirmed the name mismatch directly: CacheTools.name is the literal class attribute "Hit Cache", but the tool actually registers itself at runtime via sanitize_tool_name(self.name) (per cache_tools.py), which lowercases and underscores it to "hit_cache". So the guard's calling.tool_name != CacheTools().name was comparing the sanitized runtime name against the raw display name — never equal, so the guard's != was always True and the cache-read tool's own output got written into the cache every time. Fix aligns both sides to the sanitized form. Test fails on main and passes on the branch per the PR's own description, which lines up with what I traced through the source.

The exclusion guard compared the raw default name "Hit Cache" against the
sanitized runtime name "hit_cache", so it was always true and the cache
tool's own output was written into the cache. Compare with
sanitize_tool_name(CacheTools().name) instead.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Diwak4r
Diwak4r force-pushed the fix/latent-defect branch from dfce3c0 to 573fc42 Compare July 15, 2026 06:26

@ErenAta16 ErenAta16 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.

Verified this matches the actual bug: CacheTools.name is the raw class attribute "Hit Cache", while the tool registers itself at runtime via sanitize_tool_name(self.name) ("hit_cache"), so the old != comparison was always true and the cache-read tool's own output got written back into the cache on every call. The fix aligns both sides to the sanitized form, and the regression test fails on main / passes on the branch as described. LGTM.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants