Path: redb-route/tests/redb.Route.Tests.Llm/TestHelpers/TenantScopedTool.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/tests/redb.Route.Tests.Llm/TestHelpers/TenantScopedTool.cs ---- using System.Collections.Concurrent; using redb.Route.Configuration; namespace redb.Route.Tests.Llm.TestHelpers; /// /// Descriptor for a per-tenant tool: its address is built from the caller's tenant header, which is /// the one thing may legitimately do with the /// parent exchange. Exists to prove the tool cache keys on the resolved address — two tenants with /// the same input must never share an entry. /// public sealed class TenantScopedToolDescriptor : ILlmToolDescriptor { /// Header carrying the tenant id ( would propagate this in a real host). public const string TenantHeader = "x-tenant-id"; /// Tool name exposed to the model. public const string ToolName = "lookup"; private const string Schema = """{"type":"object","properties":{"q":{"type":"string"}},"required":["q"]}"""; /// public LlmToolCapability Capability { get; } = new() { Name = ToolName, Description = "Look up a fact for the calling tenant.", InputSchema = Schema, Safety = new LlmToolSafety { SideEffect = ToolSideEffect.ReadOnly, Caching = ToolCachingPolicy.Persist } }; /// public string BuildEndpointUri(string inputJson, IExchange parentExchange) => $"direct:tool-lookup.{ResolveTenant(parentExchange)}"; /// Tenant id of , or none when the header is absent. public static string ResolveTenant(IExchange exchange) => exchange.In.Headers.TryGetValue(TenantHeader, out var tenant) && tenant is not null ? tenant.ToString()! : "none"; } /// /// Mounts one direct:tool-lookup.{tenant} endpoint per tenant and counts dispatches, so a test /// can assert both "the right tenant was called" and "the other tenant's cached answer was not /// served". Each endpoint answers with its own tenant id and its own call counter — a cross-tenant /// leak would be visible in the payload, not only in the counters. /// public sealed class TenantScopedToolRoutes : RouteBuilder { private const string Schema = """{"type":"object","properties":{"q":{"type":"string"}},"required":["q"]}"""; private readonly ConcurrentDictionary _counts = new(StringComparer.Ordinal); /// Dispatches per tenant id. public IReadOnlyDictionary Counts => _counts; /// Dispatches for one tenant id (0 when never called). public int CountFor(string tenant) => _counts.TryGetValue(tenant, out var n) ? n : 0; /// protected override void Configure() { Mount("a"); Mount("b"); Mount("none"); } private void Mount(string tenant) { From($"direct:tool-lookup.{tenant}") .Process(e => { var call = _counts.AddOrUpdate(tenant, 1, (_, n) => n + 1); e.Out ??= e.In.Clone(); e.Out.Body = $$"""{"tenant":"{{tenant}}","call":{{call}}}"""; e.Out.Headers["Content-Type"] = "application/json"; }); } }