Repository navigation
fix(agents): set from_cache on the native tool finished event - #9504
Vidit Ostwal (Vidit-Ostwal) merged 1 commit into
Conversation
Strix Security ReviewWarning This pull request has 34 commits after the last Strix review ( ✅ No open security findings. PR summary: This PR passes the cache-hit flag through to the tool-usage finished event on both native tool execution paths, so a result replayed from the tool cache is reported as cached rather than as a live run, and adds a test asserting the event, the returned result and the untouched tool agree on a cache hit. Review summaryNo security issues found. The change only threads an already-computed Ruled out (5)
Review 1 on this PR · Reviewed by Strix |
Closes crewAIInc#9486 Both default-executor native tool paths already read the cache and return from_cache in their result, but neither passed it to ToolUsageFinishedEvent, so every call looked like it ran. A replayed result showed up as a live run in the crewai.tool.from_cache span attribute and in the CLI run view. Pass from_cache=from_cache at both emit sites: AgentExecutor's _execute_single_native_tool_call and the shared execute_single_native_tool_call used by StepExecutor. The event field already defaults to False, so nothing changes for callers that don't care. Tests: parametrized over both backends, the new test seeds the cache, runs the call, and asserts the emitted event reports from_cache=True and the tool never ran. It fails on main for both backends and passes with the change; the rest of tests/agents, test_tool_usage.py, test_tool_failure.py and test_events.py are unchanged (the failures they already have here are environment-only).
03eb03d to
70ecec5
Compare
Vidit Ostwal (Vidit-Ostwal)
left a comment
There was a problem hiding this comment.
Thanks for this, it's a clean, minimal fix. 👍
- Both emit sites from #9486 now pass
from_cache(agent_executor.pyandagent_utils.py), and the value is set before the event on every path. - I checked that
test_finished_event_reports_a_cache_hit[agent|shared]fails onmainand passes on this branch, along with a separate repro that runs a live call followed by a cached call.
Related issue
Fixes #9486
Summary
from_cacheis computed and returned on both default-executor native tool paths, but neither passed it toToolUsageFinishedEvent, so the field stayed at itsFalsedefault and every call looked like it ran. Thecrewai.tool.from_cachespan attribute and the CLI run view therefore reported a replayed result as a live run.The fix passes
from_cache=from_cacheat both emit sites:AgentExecutor._execute_single_native_tool_call(agents/agent_executor.py)execute_single_native_tool_call(utilities/agent_utils.py), the oneStepExecutorusesThe event field already exists and defaults to
False, so callers that don't look at it see no change. The result payload (NativeToolCallResult.from_cache/ the dict key) was already correct and is untouched.Verification
New test in
tests/agents/test_native_tool_hook_contract.py, parametrized over both backends (agentandshared), using the existingCacheHandlerharness in that file: seed the cache, run the call, collectToolUsageFinishedEvent, then assert the event reportsfrom_cache is True, the tool never ran, andoutputmatches the returned result.Numbers, on
mainvs. with the patch (same machine, same env):test_native_tool_hook_contract.py -k finished_eventtest_native_tool_hook_contract.py(whole file)tests/agents+test_tool_usage.py+test_tool_failure.py+test_events.pyThe 49 failures are identical before and after (compared the sorted failure lists) and come from this sandbox's dependency mix, not from the change.
Additional context
Should be a backport candidate if 1.15.x still ships the default executor path, since the span attribute is what shows up in traces.
None otherwise.