Skip to content

fix(agents): set from_cache on the native tool finished event - #9504

Merged
Vidit Ostwal (Vidit-Ostwal) merged 1 commit into
crewAIInc:mainfrom
sunhouy:fix/native-tool-finished-from-cache
Nov 20, 2026
Merged

Vidit Ostwal (Vidit-Ostwal) merged 1 commit into
crewAIInc:mainfrom
sunhouy:fix/native-tool-finished-from-cache

Conversation

@sunhouy

Copy link
Copy Markdown
Contributor

Related issue

Fixes #9486

Summary

from_cache is computed and returned on both default-executor native tool paths, but neither passed it to ToolUsageFinishedEvent, so the field stayed at its False default and every call looked like it ran. The crewai.tool.from_cache span attribute and the CLI run view therefore reported a replayed result as a live run.

The fix passes from_cache=from_cache at both emit sites:

  • AgentExecutor._execute_single_native_tool_call (agents/agent_executor.py)
  • execute_single_native_tool_call (utilities/agent_utils.py), the one StepExecutor uses

The 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 (agent and shared), using the existing CacheHandler harness in that file: seed the cache, run the call, collect ToolUsageFinishedEvent, then assert the event reports from_cache is True, the tool never ran, and output matches the returned result.

  • Tests added or updated for the changed behavior
  • Relevant tests and quality checks pass locally

Numbers, on main vs. with the patch (same machine, same env):

before after
test_native_tool_hook_contract.py -k finished_event 2 failed 0 failed
test_native_tool_hook_contract.py (whole file) 18 passed, 2 failed 20 passed
tests/agents + test_tool_usage.py + test_tool_failure.py + test_events.py 49 failed, 776 passed 49 failed, 778 passed

The 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.

@strix-security

strix-security Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Strix Security Review

Warning

This pull request has 34 commits after the last Strix review (03eb03d). Strix has not reviewed these changes.
Automatic review on push is off for this repository. To review the latest changes, tag @strix-security in a comment, or turn on re-review on push.

✅ 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 summary

No security issues found. The change only threads an already-computed from_cache boolean into the existing ToolUsageFinishedEvent field at the two default-executor native tool emit sites, making crewai.tool.from_cache and the CLI run view accurately report a replayed result. The flag is assigned before both emit sites on every path, it stays False when a before-tool hook blocks the call, and its only consumers are a tracing span attribute and a CLI display, so it cannot weaken an authorization, quota or audit decision. The added test is test-only. The surrounding native tool paths were traced for tool-name resolution, hook dispatch and cache-key scoping; the only behavioural divergence found is a configuration-time metadata attribution issue that no attacker can trigger.

Ruled out (5)
  • Do tool hooks fail open when an operator's guard raises an unexpected exception, letting a call that should be blocked run?
    Fail-open on anything other than `HookAborted` is the deliberate, documented contract enforced and tested in the hook dispatcher, and the added lines sit downstream of hook dispatch without altering it.
  • Can two tools whose names sanitize to the same string make a model call execute a different tool than the one advertised in the schema?
    Both native paths execute `available_functions[func_name]`, keyed by the same de-duplicated alias the schema advertises, so a model-supplied name can never reach another tool's implementation and the alternative path fails closed with an unknown-tool failure.
  • Can a colliding tool name make a call inherit another tool's `result_as_answer`, `cache_function`, output formatter or failure policy?
    Only a hand-built list such as `[search, search, search_2]` triggers the crossover, all of those settings come from the developer's own tool list, and the callable that runs is still the advertised one, so the only party who can cause it already controls the agent's capabilities.
  • Can one agent read another agent's cached tool results through the crew-shared cache key?
    The cache key scheme and the cache-reading tool are untouched by this PR; the added line only reports that the existing read returned a hit, keyed by the same tool alias the call used.
  • Does adding a field to every finished tool event grow the per-run event record without bound?
    The finished event was already emitted and retained before this change, the added field is a single boolean, and no retention or eviction behaviour is altered.

Review 1 on this PR · 03eb03d · requested Nov 17 08:14 UTC when the PR was opened


Reviewed by Strix
Re-run review · Configure security review settings · Try a deeper review with @strix-security /deep

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).
@Vidit-Ostwal
Vidit Ostwal (Vidit-Ostwal) force-pushed the fix/native-tool-finished-from-cache branch from 03eb03d to 70ecec5 Compare November 20, 2026 09:17

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this, it's a clean, minimal fix. 👍

  • Both emit sites from #9486 now pass from_cache (agent_executor.py and agent_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 on main and passes on this branch, along with a separate repro that runs a live call followed by a cached call.

@Vidit-Ostwal
Vidit Ostwal (Vidit-Ostwal) merged commit 8c75a46 into crewAIInc:main Nov 20, 2026
54 checks passed
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.

[BUG] The default executor reports a cached native tool call as a live one

2 participants