Path: redb-route/tests/redb.Route.Tests.Llm/Governance/InMemoryStoresInTransactionWarningTests.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/Governance/InMemoryStoresInTransactionWarningTests.cs ---- using System.Transactions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using redb.Route.Llm.Engine.Storage; using redb.Route.Processors; using redb.Route.Tests.Llm.TestHelpers; namespace redb.Route.Tests.Llm.Governance; /// /// The in-memory conversation and tool idempotency stores do not take part in an ambient transaction, so a run /// inside one warns, once per route and connection factory: after a rollback the transcript keeps the rolled-back /// run's tool calls. The run itself goes on. No warning outside a transaction, with the redb stores, or with a /// host's own store. /// [Trait("Category", "Governance")] public sealed class InMemoryStoresInTransactionWarningTests { private sealed class ListLogger : ILogger { private readonly List<(LogLevel Level, string Message)> _entries = new(); public IReadOnlyList Warnings { get { lock (_entries) return _entries.Where(e => e.Level == LogLevel.Warning).Select(e => e.Message).ToArray(); } } public IDisposable? BeginScope(TState state) where TState : notnull => null; public bool IsEnabled(LogLevel logLevel) => true; public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { lock (_entries) _entries.Add((logLevel, formatter(state, exception))); } } /// A host's own conversation store; it happens to keep its rows in memory, which is its own business. private sealed class HostConversationStore : IConversationStore { private readonly InMemoryConversationStore _inner = new(); public Task AppendAsync(string conversationId, string? parentId, LlmMessage message, ConversationMessageMeta meta, IExchange? exchange = null, CancellationToken ct = default) => _inner.AppendAsync(conversationId, parentId, message, meta, exchange, ct); public Task> LoadPathAsync(string conversationId, string? leafId = null, IExchange? exchange = null, CancellationToken ct = default) => _inner.LoadPathAsync(conversationId, leafId, exchange, ct); public Task> LoadTreeAsync(string conversationId, IExchange? exchange = null, CancellationToken ct = default) => _inner.LoadTreeAsync(conversationId, exchange, ct); } private static AgentEngine Engine(ILogger logger, IConversationStore? conversation, IToolIdempotencyStore? idempotency) => new( logger: logger, producerTemplate: null, observer: null, budget: null, approval: null, redaction: null, shadow: null, conversation: conversation, idempotency: idempotency, approvalStore: null); private static AgentRequest Request(string route, bool withTools = false) { var provider = new FakeProvider().EnqueueText("done"); return new AgentRequest { Factory = new LlmConnectionFactory { Name = "support", Provider = "fake", ModelId = provider.ModelId, PrebuiltProvider = provider }, Exchange = new Exchange(new Message("hello")) { RouteId = route }, UserContent = [new LlmTextBlock("hello")], ConversationId = "conv-1", Tools = withTools ? [new TenantScopedToolDescriptor()] : [] }; } private static async Task RunInTransactionAsync(AgentEngine engine, AgentRequest request) { using var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled); var response = await engine.RunAsync(request); response.Content.OfType().Single().Text.Should().Be("done", "the run goes on as before"); tx.Complete(); } [Fact] public async Task InMemoryStores_InsideATransaction_WarnOncePerRouteAndFactory() { var logger = new ListLogger(); var engine = Engine(logger, new InMemoryConversationStore(), new InMemoryToolIdempotencyStore(new InMemoryIdempotentRepository())); await RunInTransactionAsync(engine, Request("orders", withTools: true)); await RunInTransactionAsync(engine, Request("orders", withTools: true)); logger.Warnings.Should().ContainSingle(); var warning = logger.Warnings[0]; warning.Should().Contain("llm:support").And.Contain("'orders'") .And.Contain("InMemoryConversationStore and InMemoryToolIdempotencyStore") .And.Contain("do not take part in the transaction") .And.Contain("may not call the tool again") .And.Contain("AddRedbLlmStorage()").And.Contain(".Transacted()"); await RunInTransactionAsync(engine, Request("billing")); logger.Warnings.Should().HaveCount(2, "another route is another step, warned once on its own"); logger.Warnings[1].Should().Contain("'billing'").And.Contain("InMemoryConversationStore keep state") .And.NotContain("InMemoryToolIdempotencyStore", "a run without tools never touches the idempotency store"); } [Fact] public async Task InMemoryStores_OutsideATransaction_DoNotWarn() { var logger = new ListLogger(); var engine = Engine(logger, new InMemoryConversationStore(), new InMemoryToolIdempotencyStore(new InMemoryIdempotentRepository())); await engine.RunAsync(Request("orders", withTools: true)); logger.Warnings.Should().BeEmpty(); } [Fact] public async Task AHostsOwnStore_InsideATransaction_IsNotWarnedAbout() { var logger = new ListLogger(); var engine = Engine(logger, new HostConversationStore(), idempotency: null); await RunInTransactionAsync(engine, Request("orders", withTools: true)); logger.Warnings.Should().BeEmpty(); } [Fact] public void AddRedbLlmStorage_ReplacesBothInMemoryStores() { // The warning looks at the store instances the engine holds; with AddRedbLlmStorage() neither of them is an // in-memory one, so a transacted run is not warned about. A run against the redb stores is covered by the // storage suites, which need a database. var services = new ServiceCollection(); services.AddSingleton(new RouteContext()); services.AddRedbRouteLlm(); services.AddRedbLlmStorage(); using var sp = services.BuildServiceProvider(); sp.GetRequiredService().Should().NotBeOfType(); sp.GetRequiredService().Should().NotBeOfType(); } }