fix(tool): stable cache key for @tool(cache_results) independent of tool_hooks (#4387) #4394
+123
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes issue #4387 where
@tool(cache_results=True)
does not work as expected whentool_hooks
is not set.Root Cause
The cache key computation was inconsistent between cache lookup and cache storage:
entrypoint_args
that start emptyentrypoint_args
after mutation witharguments.update()
Solution
_get_cache_key_from_resolved_args()
method for deterministic cache key generation_build_resolved_args()
helper to build canonical arguments before mutationsexecute()
andaexecute()
to compute cache key once from resolved args and reuse for both lookup and storageChanges Made
Function.py changes:
_get_cache_key_from_resolved_args()
methodFunctionCall._build_resolved_args()
helper methodexecute()
andaexecute()
methodsTest coverage:
test_tool_cache_without_hooks()
- verifies caching with no tool_hookstest_tool_cache_with_empty_hooks()
- verifies caching with empty tool_hookstest_tool_cache_kwargs_order_independence()
- verifies order independenceTesting
Impact
@tool(cache_results=True)
whentool_hooks
is not providedtool_hooks
is present