Path: redb-route/redb.Route.Llm/Engine/Storage/IToolCacheStore.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm/Engine/Storage/IToolCacheStore.cs ---- using System.Collections.Concurrent; using redb.Route.Abstractions; namespace redb.Route.Llm.Engine.Storage; /// /// Caches deterministic / read-only tool outputs by a content-hash key so repeated calls can skip /// the underlying work: inside one run for ToolCachingPolicy.Memoize (served in process, /// never through this store) and across runs for ToolCachingPolicy.Persist. /// /// The agent loop consults this store for Persist tools, after approval and before the /// idempotency store: a hit means "this exact input already produced this output", which is cheaper /// than asking "was this tool_use already executed?", and it is reported to observers as a skipped /// invocation with SkipReason = "cache_hit". Only read-only tools may declare a caching /// policy — a cached entry suppresses the side effect it stands for, so the route build rejects /// Caching != None on a mutating tool. /// /// /// An output the redaction filter changed is never stored: persisting the raw body would keep /// redaction-bypassing content in the store, and persisting the redacted body would hand the model /// different data on a hit than on a miss. /// /// /// The optional exchange parameter on every method carries the route /// pipeline's current exchange; REDB-backed implementations resolve a /// per-exchange through /// IRouteContext.GetRedbService(name, exchange). In-memory /// implementations ignore it. /// Without an exchange a redb-backed implementation opens a redb scope of its own for the call. That scope joins an /// ambient (a , a /// route's .Transacted()) as every redb connection does, but not a transaction begun on the context's shared /// IRedbService (ExecuteAtomicAsync, BeginTransaction), which it does not see: pass the exchange, /// or use a TransactionScope, when the call must commit or roll back with that work. In-memory implementations /// take part in no transaction. /// /// /// A store does not report metrics. Hit/miss counters belong to the agent loop, which is the /// only layer that sees both this store and the run-scoped memo; a store that counted as well would /// count every persisted read twice. /// /// public interface IToolCacheStore { /// Returns the cached output for , or null on a miss. ValueTask GetAsync(string cacheKey, IExchange? exchange = null, CancellationToken ct = default); /// Stores the cached output and (optionally) sets a relative time-to-live. ValueTask SetAsync(string cacheKey, string outputJson, TimeSpan? ttl = null, IExchange? exchange = null, CancellationToken ct = default); /// Drops every entry — used by tests. ValueTask ClearAsync(IExchange? exchange = null, CancellationToken ct = default); } /// In-memory tool cache. TTL is honoured lazily — entries are dropped on read. public sealed class InMemoryToolCacheStore : IToolCacheStore { private readonly ConcurrentDictionary _entries = new(StringComparer.Ordinal); /// public ValueTask GetAsync(string cacheKey, IExchange? exchange = null, CancellationToken ct = default) { if (!_entries.TryGetValue(cacheKey, out var entry)) return ValueTask.FromResult(null); if (entry.ExpiresAtUtc is { } exp && exp <= DateTime.UtcNow) { _entries.TryRemove(cacheKey, out _); return ValueTask.FromResult(null); } return ValueTask.FromResult(entry.OutputJson); } /// public ValueTask SetAsync(string cacheKey, string outputJson, TimeSpan? ttl = null, IExchange? exchange = null, CancellationToken ct = default) { var expires = ttl is { } t && t > TimeSpan.Zero ? DateTime.UtcNow.Add(t) : (DateTime?)null; _entries[cacheKey] = new Entry(outputJson, expires); return ValueTask.CompletedTask; } /// public ValueTask ClearAsync(IExchange? exchange = null, CancellationToken ct = default) { _entries.Clear(); return ValueTask.CompletedTask; } private readonly record struct Entry(string OutputJson, DateTime? ExpiresAtUtc); }