Path: redb-route/redb.Route.Llm/Engine/Storage/IEvalRunStore.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/IEvalRunStore.cs
----
using System.Collections.Concurrent;
using redb.Route.Abstractions;
using redb.Route.Llm.Engine.Governance;
namespace redb.Route.Llm.Engine.Storage;
///
/// Persists the outcome of a recorded evaluation run. Used by the eval harness
/// to track regression scores between agent / prompt / model versions.
///
/// 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.
///
///
public interface IEvalRunStore
{
/// Persists a completed evaluation run.
Task SaveAsync(EvalRunRecord record, IExchange? exchange = null, CancellationToken ct = default);
///
/// Persists many evaluation runs at once. Bulk-friendly stores commit the
/// whole set in a single transaction; the default falls back to a loop.
///
async Task SaveManyAsync(IEnumerable records, IExchange? exchange = null, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(records);
foreach (var record in records)
{
ct.ThrowIfCancellationRequested();
await SaveAsync(record, exchange, ct).ConfigureAwait(false);
}
}
/// Loads a single run by its identifier.
Task GetAsync(string runId, IExchange? exchange = null, CancellationToken ct = default);
///
/// Lists runs filtered by scenario name. Returns newest first.
///
Task> ListAsync(string? scenario = null, int take = 50, IExchange? exchange = null, CancellationToken ct = default);
}
/// An evaluation run snapshot.
public sealed class EvalRunRecord
{
/// Identifier assigned by the eval harness.
public required string RunId { get; init; }
/// Scenario name (e.g. "summarize-pr", "sql-rag").
public required string Scenario { get; init; }
/// Agent / prompt / model fingerprint that produced the run.
public required string AgentFingerprint { get; init; }
/// Aggregate score in [0.0, 1.0]; null when not scored.
public double? Score { get; init; }
/// Total usage observed across the run.
public AgentUsage TotalUsage { get; init; } = AgentUsage.Zero;
/// Per-iteration metrics — empty when the harness did not record per-iteration data.
public IReadOnlyList Iterations { get; init; } = [];
/// Custom scoring dimensions (e.g. "accuracy" = 0.92).
public IReadOnlyDictionary? Dimensions { get; init; }
/// Fixture identifiers / version hashes consumed by the run.
public IReadOnlyDictionary? Fixtures { get; init; }
/// Wall-clock timestamp the record was created.
public DateTime CreatedAtUtc { get; init; } = DateTime.UtcNow;
}
/// Per-iteration metric inside an .
public sealed class EvalIterationRecord
{
/// 1-based iteration index within the run.
public int Iteration { get; init; }
/// Input tokens consumed by this iteration.
public int InputTokens { get; init; }
/// Output tokens produced by this iteration.
public int OutputTokens { get; init; }
/// Wall-clock duration of the iteration in milliseconds.
public int DurationMs { get; init; }
/// Per-iteration score in [0.0, 1.0]; null when not scored.
public double? Score { get; init; }
/// Optional free-form note (judge rationale, failure tag, etc.).
public string? Notes { get; init; }
}
/// In-memory eval run store — used by unit tests; production swaps in the redb implementation.
public sealed class InMemoryEvalRunStore : IEvalRunStore
{
private readonly ConcurrentDictionary _records = new(StringComparer.Ordinal);
///
public Task SaveAsync(EvalRunRecord record, IExchange? exchange = null, CancellationToken ct = default)
{
_records[record.RunId] = record;
return Task.CompletedTask;
}
///
public Task GetAsync(string runId, IExchange? exchange = null, CancellationToken ct = default) =>
Task.FromResult(_records.TryGetValue(runId, out var rec) ? rec : null);
///
public Task> ListAsync(string? scenario = null, int take = 50, IExchange? exchange = null, CancellationToken ct = default)
{
IEnumerable q = _records.Values;
if (scenario is not null)
q = q.Where(r => string.Equals(r.Scenario, scenario, StringComparison.Ordinal));
var result = q.OrderByDescending(r => r.CreatedAtUtc).Take(take).ToArray();
return Task.FromResult>(result);
}
}