Path: redb-route/tests/redb.Route.Tests.Llm/TestHelpers/LiveLlmHost.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/LiveLlmHost.cs
----
using Microsoft.Extensions.DependencyInjection;
using redb.Core;
using redb.Route.Components;
using redb.Route.Configuration;
using redb.Route.Http;
using redb.Route.Llm.Extensions;
using redb.Route.Llm.Engine.Governance;
using redb.Route.Llm.Engine.Observability;
using redb.Route.Llm.Engine.Storage;
namespace redb.Route.Tests.Llm.TestHelpers;
///
/// Minimal hand-rolled wiring for live DSL tests. Avoids the
/// RouteHostedService bootstrap (which requires a host) so the test
/// body stays close to a Camel-style example a human can read top-down:
/// build, register factory, declare routes, hit direct:, assert.
///
/// All public registrations match what services.AddRedbRoute().AddRedbRouteLlm()
/// would do in a real host. The single difference is that we drive
/// ourselves.
///
///
public sealed class LiveLlmHost : IAsyncDisposable
{
/// The shared route context.
public RouteContext Context { get; }
/// The producer template — used by the agent engine to dispatch tool routes.
public IProducerTemplate ProducerTemplate { get; }
/// The agent engine wired with the producer template.
public IAgentEngine Engine { get; }
/// The tool registry shared by every LLM endpoint and inline .Llm() step.
public IToolDescriptorRegistry ToolRegistry { get; }
private readonly SharedHttpServerManager? _httpManager;
private LiveLlmHost(RouteContext ctx, IProducerTemplate pt, IAgentEngine engine,
IToolDescriptorRegistry toolRegistry, SharedHttpServerManager? httpManager = null)
{
Context = ctx;
ProducerTemplate = pt;
Engine = engine;
ToolRegistry = toolRegistry;
_httpManager = httpManager;
}
/// Builds a host with the LLM component, agent engine and tool registry pre-wired.
/// Optional observer — defaults to . Tests pass a spy
/// when they need to assert on tool invocations the model performed.
/// Optional idempotency store — governance tests pass a spy to assert reservations.
/// Optional budget enforcer — defaults to the no-op one.
/// Optional approval gate — defaults to (today's shipped default).
/// Optional redaction filter — defaults to the no-op one.
/// Optional conversation store — pass to inspect persisted metadata.
/// Optional claims source. Absent means a claim-declaring tool cannot be built at all (fail closed).
/// Optional cross-run cache store, consulted for ToolCachingPolicy.Persist tools.
/// Optional price list. Absent means "this deployment cannot price a call".
///
/// Optional redb instance registered in the host's container. Needed by integration tests: the redb-backed
/// stores resolve their service from the exchange's DI scope, so without it they would look for a redb the
/// host does not have.
///
///
/// When true the engine is resolved from the container built by AddRedbRouteLlm() instead of being
/// hand-wired. That is the path every real host takes, and the only one that proves the package wires its
/// own governance seams.
///
///
/// Optional HTTP hosting options. With set, the host also carries an
/// HttpComponent, so a route can start at http:127.0.0.1:{port}/… — the only way to prove
/// that a caller identified by the transport reaches the engine's governance without a test hook.
///
/// Port for the HTTP entry point; required together with .
/// Engine options of the route context; tracing tests turn EnableTelemetry off.
public static LiveLlmHost Build(
IAgentObserver? observer = null,
IToolIdempotencyStore? idempotency = null,
IBudgetEnforcer? budget = null,
IApprovalGate? approval = null,
IRedactionFilter? redaction = null,
IConversationStore? conversation = null,
IToolClaimsSource? claimsSource = null,
IToolCacheStore? toolCache = null,
ICostCalculator? costCalculator = null,
IRedbService? redb = null,
bool engineFromDi = false,
HttpHostingOptions? httpHosting = null,
int? httpPort = null,
RouteEngineOptions? options = null)
{
// Closure trick: register IRouteContext as a factory that returns the
// context we are about to construct. This lets the inline `.Llm(...)`
// step resolve IRouteContext from exchange.ServiceProvider at runtime.
RouteContext ctx = null!;
var services = new ServiceCollection();
services.AddSingleton(_ => ctx);
if (redb is not null) services.AddSingleton(redb);
if (engineFromDi) services.AddRedbRouteLlm();
var sp = services.BuildServiceProvider();
ctx = new RouteContext(sp, contextId: "llm-test-ctx", options: options);
ctx.AddComponent(new LlmComponent());
// Optional HTTP entry point: the transport identifies the caller and puts the principal on the
// exchange, exactly as a real deployment does — no test-side hook touches the properties.
SharedHttpServerManager? httpManager = null;
if (httpPort is { } port)
{
httpManager = new SharedHttpServerManager(httpHosting);
ctx.AddComponent(new HttpComponent { ServerManager = httpManager });
}
// One registry must serve the route DSL and the endpoint that projects the tools. With the engine
// coming from DI, the LLM extension already registered one — using the host's own instance as well
// would give the DSL and the endpoint different registries, and the model would see no tools at all.
var toolRegistry = engineFromDi
? sp.GetRequiredService()
: new ToolDescriptorRegistry();
ctx.AddService(typeof(IToolDescriptorRegistry), toolRegistry);
// The claims source must be visible to the route context, not only to the engine: the DSL
// refuses to build a claim-declaring tool when no source is registered (fail closed).
if (claimsSource is not null)
ctx.AddService(typeof(IToolClaimsSource), claimsSource);
var pt = new ProducerTemplate(ctx);
ctx.AddService(typeof(IProducerTemplate), pt);
var engine = engineFromDi
? sp.GetRequiredService()
: new AgentEngine(
logger: null,
producerTemplate: pt,
observer: observer ?? new NoopAgentObserver(),
budget: budget ?? new NoopBudgetEnforcer(),
approval: approval ?? new AutoApproveGate(),
redaction: redaction ?? new NoopRedactionFilter(),
shadow: new NoopShadowRunner(),
conversation: conversation,
idempotency: idempotency,
approvalStore: null,
claimsSource: claimsSource,
toolCache: toolCache,
costCalculator: costCalculator);
// A real host gets its engine from the container; putting it in the context locator as well would
// mask whether the endpoint can find what the package registered — the very gap these tests exist for.
if (!engineFromDi) ctx.AddService(typeof(IAgentEngine), engine);
return new LiveLlmHost(ctx, pt, engine, toolRegistry, httpManager);
}
/// Registers a connection factory keyed by .
public LiveLlmHost AddFactory(string name, LlmConnectionFactory factory)
{
factory.Name = name;
Context.AddToRegistry(name, factory);
return this;
}
///
/// Declares routes inline and starts the context. The producer template is
/// started as part of this call so subsequent tool dispatches just work.
///
public async Task StartAsync(Action configure)
{
Context.AddRoutes(configure);
await Context.Start().ConfigureAwait(false);
((ProducerTemplate)ProducerTemplate).Start();
return this;
}
///
/// Mounts a (e.g. an )
/// and starts the context. Use this overload when the builder owns the route
/// so its descriptor lands in the registry via .AsLlmTool.
///
public async Task StartAsync(RouteBuilder routes, Action? extraInline = null)
{
Context.AddRoutes(routes);
if (extraInline is not null) Context.AddRoutes(extraInline);
await Context.Start().ConfigureAwait(false);
((ProducerTemplate)ProducerTemplate).Start();
return this;
}
/// Sends one message into and returns the resulting exchange.
/// Endpoint to send to.
/// Message body.
/// Optional inbound headers.
///
/// Optional hook running on the exchange before it enters the route. Used by tests that need state a
/// real transport would have produced — the caller's principal in the exchange properties, for
/// instance.
///
public async Task SendAsync(string endpointUri, string body,
IDictionary? headers = null, Action? configure = null)
{
var endpoint = Context.GetEndpoint(endpointUri);
var producer = endpoint.CreateProducer();
await producer.Start().ConfigureAwait(false);
var msg = new Message(body);
if (headers is not null)
foreach (var kv in headers) msg.Headers[kv.Key] = kv.Value;
// Construct the exchange with a per-call DI scope so inline .Llm()
// can resolve IRouteContext from exchange.ServiceProvider.
var scopeFactory = endpoint.ScopeFactory;
var ex = Exchange.Create(msg, scopeFactory);
configure?.Invoke(ex);
await producer.Process(ex).ConfigureAwait(false);
return ex;
}
/// Convenience cast for the mock sink so tests can read its received exchanges.
public MockEndpoint Mock(string uri) => (MockEndpoint)Context.GetEndpoint(uri);
///
public async ValueTask DisposeAsync()
{
await Context.DisposeAsync().ConfigureAwait(false);
if (ProducerTemplate is IDisposable d) d.Dispose();
if (_httpManager is not null) await _httpManager.DisposeAsync().ConfigureAwait(false);
}
}