Path: redb-route/tests/redb.Route.Tests.Llm/LlmScheduledOwnershipTests.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/LlmScheduledOwnershipTests.cs ---- using redb.Route.Abstractions; using redb.Route.Tests.Llm.TestHelpers; namespace redb.Route.Tests.Llm; /// /// Statistics ownership for the scheduled agent (From("llm://...?schedule=...")): /// the route pipeline is wrapped by the core's StatisticsProcessor, so the consumer must not /// self-record what the core already counts. It records only what the core cannot see — /// a tick that failed before the exchange reached the pipeline. /// public sealed class LlmScheduledOwnershipTests { [Fact] public async Task ScheduledTick_IsCountedByTheCore_NotByTheConsumer() { var fake = new FakeProvider().EnqueueText("tick!"); await using var host = LiveLlmHost.Build(); host.AddFactory("fake", new LlmConnectionFactory { Name = "fake", Provider = "fake", ModelId = fake.ModelId, PrebuiltProvider = fake }); const string uri = "llm://fake?schedule=200ms&initialBodyRef=go"; await host.StartAsync(r => r.From(uri).To("mock:llm-ticks")); var sink = host.Mock("mock:llm-ticks"); var deadline = DateTime.UtcNow.AddSeconds(10); while (sink.ReceivedCount == 0 && DateTime.UtcNow < deadline) await Task.Delay(50); sink.ReceivedCount.Should().BeGreaterThan(0, "at least one tick must reach the route"); var ep = (IEndpointStatistics)host.Context.GetEndpoint(uri); // The pipeline leg is the core's: MessagesIn per tick from StatisticsProcessor. ep.MessagesIn.Should().BeGreaterThan(0, "MessagesIn is written by StatisticsProcessor around From()"); // Nothing produces THROUGH the scheduled endpoint - self-recording a MessagesOut per // tick doubled the picture (core wrote its own success number on the same endpoint). ep.MessagesOut.Should().Be(0, "MessagesOut is a producer counter, a tick is not one"); ep.Errors.Should().Be(0, "successful ticks must bring no errors"); } [Fact] public async Task A_tick_whose_model_call_failed_is_recorded_with_its_text() { var fake = new FakeProvider { ThrowOnCall = new InvalidOperationException("model refused by the test") }; await using var host = LiveLlmHost.Build(); host.AddFactory("fake", new LlmConnectionFactory { Name = "fake", Provider = "fake", ModelId = fake.ModelId, PrebuiltProvider = fake }); const string uri = "llm://fake?schedule=200ms&initialBodyRef=go"; await host.StartAsync(r => r.From(uri).To("mock:llm-failed-ticks")); var ep = (IEndpointStatistics)host.Context.GetEndpoint(uri); var deadline = DateTime.UtcNow.AddSeconds(10); while (ep.Errors == 0 && DateTime.UtcNow < deadline) await Task.Delay(50); ep.Errors.Should().BeGreaterThan(0, "a tick that failed before the pipeline is the consumer's to count"); ep.LastErrorMessage.Should().Contain("model refused by the test"); } }