Path: redb-route/redb.Route.Llm/Storage/Redb/RedbApprovalStore.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/Storage/Redb/RedbApprovalStore.cs ---- using redb.Core; using redb.Core.Exceptions; using redb.Core.Models.Entities; using redb.Route.Abstractions; using redb.Route.Llm.Engine.Governance; using redb.Route.Llm.Engine.Storage; using redb.Route.Llm.Storage.Redb.Schemas; using redb.Route.RedbCore; using redb.Route.RedbCore.Extensions; namespace redb.Route.Llm.Storage.Redb; /// /// REDB-backed . One /// row per decision; the approval id lives in _objects.value_string /// (partial index on PostgreSQL/SQLite; MSSQL cannot index NVARCHAR(MAX)) and, /// normalized, in _objects._value_unique — one approval id resolves to /// exactly one recorded decision, first wins. /// /// The store does not own an instance — each call /// resolves one through IRouteContext.GetRedbService(name, exchange), /// which honours the per-exchange scope cache. The redb name is read from /// exchange.Properties[LlmKeys.RedbName] (set by the LLM endpoint URI), /// falling back to the constructor-supplied default name and then to the host's /// default unnamed instance. /// /// public sealed class RedbApprovalStore : IApprovalStore { private readonly IRouteContext _context; private readonly string? _defaultRedbName; /// Creates the store. Scheme is synced by the host's redb.InitializeAsync(). public RedbApprovalStore(IRouteContext context, string? defaultRedbName = null) { _context = context ?? throw new ArgumentNullException(nameof(context)); _defaultRedbName = defaultRedbName; } /// /// The service a call works on: the exchange's with one; without one a scope of its own, handed back in /// for the caller to dispose, so concurrent calls do not share the context's single /// instance and its one connection. /// private IRedbService Resolve(IExchange? exchange, out RedbScope? owned) { var name = _defaultRedbName; if (exchange is not null && exchange.Properties.TryGetValue(LlmKeys.RedbName, out var raw) && raw is string s && s.Length > 0) name = s; if (exchange is not null) { owned = null; return _context.GetRedbService(name ?? string.Empty, exchange); } owned = _context.CreateRedbScope(name); return owned.Service; } /// public async Task RecordAsync(ApprovalRequest request, ApprovalDecision decision, IExchange? exchange = null, CancellationToken ct = default) { var id = decision.ApprovalId ?? Guid.NewGuid().ToString("N"); var row = new RedbObject { value_string = id, // A caller-supplied approval id must resolve to ONE decision: the key rides // in _value_unique, and a duplicate record of the same id is dropped — // first wins (a self-generated GUID never collides). ValueUnique = RedbUniqueKey.Normalize(id), Props = new ApprovalProps { ConversationId = request.ConversationId, ToolName = request.Tool.Name, ToolUseId = request.ToolUseId, Approved = decision.Approved, Reason = decision.Reason, ApprovedBy = decision.ApprovalId, DecidedAtUtc = DateTimeOffset.UtcNow, InputJson = request.InputJson } }; var redb = Resolve(exchange, out var owned); await using var ownedScope = owned; try { await redb.SaveAsync(row).ConfigureAwait(false); } catch (RedbUniqueViolationException) { // The decision for this approval id is already recorded — first wins. } } /// public async Task FindAsync(string approvalId, IExchange? exchange = null, CancellationToken ct = default) { var redb = Resolve(exchange, out var owned); await using var ownedScope = owned; var hit = await redb.Query() .WhereRedb(x => x.ValueUnique == RedbUniqueKey.Normalize(approvalId)) .FirstOrDefaultAsync() .ConfigureAwait(false); if (hit is null) return null; return new ApprovalRecord { ApprovalId = hit.value_string ?? approvalId, ConversationId = hit.Props.ConversationId, ToolName = hit.Props.ToolName, InputJson = hit.Props.InputJson, Approved = hit.Props.Approved, Reason = hit.Props.Reason, CreatedAtUtc = hit.Props.DecidedAtUtc.UtcDateTime }; } }