feat(tools): tool result cache — avoid redundant executions (#1822)#2027
Merged
feat(tools): tool result cache — avoid redundant executions (#1822)#2027
Conversation
…ssion
Implement per-session in-memory cache for deterministic tool results to
eliminate redundant executions (e.g., re-reading same file, repeated web scrape).
Architecture:
- CachingExecutor as ToolResultCache in ToolOrchestrator
- Cache key: {tool_name}:{args_hash} using existing canonicalization
- TTL configurable (default 5 min), reset on /clear
- Non-deterministic tools (bash, write, memory_save, scheduler, memory_search,
mcp_*) excluded via static deny list
- Cache hits emit proper ToolStart/ToolOutput events for TUI visibility
- Metrics tracking: hits, misses, entries count
Integration:
- Config: [tools.result_cache] { enabled = true, ttl_secs = 300 }
- Slash command: /cache-stats shows cache status and hit rate
- Repeat detection: cache hits pushed into recent_tool_calls window
- Pre-built result pattern: cache lookup before tier dispatch, store after
All P0/P1 fixes from architecture + critic phases integrated:
- P0: Pre-built result pattern for parallel dispatch safety
- P1: TTL=0 semantics fixed (Option<Duration>), MCP tools non-cacheable,
repeat detection coupling, /cache-stats MVP, TUI events preserved
Test coverage: 5 new cache_stats tests, 4 config tests, 2 session config
asserts. Total: 5964 tests pass (delta +9).
Closes #1822
abc76ef to
1b1641a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Implement per-session in-memory cache for deterministic tool results to eliminate redundant executions within a single agent session. This addresses the "Speculative Tool Calls" paper (arxiv.org/abs/2512.15834) with session-scoped caching.
Key Features
/cache-statscommand shows cache status, hit rate, entry count[tools.result_cache] { enabled = true, ttl_secs = 300 }Implementation Details
New Module:
crates/zeph-tools/src/cache.rsToolResultCachestruct with HashMap<CacheKey, CacheEntry>CacheKey = {tool_name, args_hash}(reuses existing canonicalization)CacheEntry = {output: ToolOutput, inserted_at: Instant}Option<Duration>(None = never expire)Integration Points:
ToolOrchestratorfield for per-session cache ownershipnative.rs: Pre-built result pattern (lookup before tier dispatch, store after join_all)tool_orchestrator.rs:/cache-statsslash commandmetrics.rs: tool_cache_hits, tool_cache_misses, tool_cache_entries fieldsbuilder.rs+session_config.rs: Config threadingArchitecture Decisions (from review):
get(), not background timer)Option<Duration>semantics: None = never expire, Some(n) = expire after n secondsTest Coverage
Compliance
✅ All P0 blocking fixes from architecture critique:
✅ All P1 important fixes:
/cache-statscommand (MVP observability)Performance
Deferred (P2/P3)
Issue: Closes #1822
Branch: feat/issue-1822/tool-result-cache
Test count: 5964 (before: 5955, delta: +9)