Files
ClawdDotNet/tests/ClawdDotNet.Core.Tests/Infrastructure/InMemoryAuditRepository.cs
T
RichardandClaude Opus 5 5adf4c1ca6 Prompt History und Verbrauchsauswertung (#660), englische Hauptsprache (#661)
Prompt history: every run records its transcript (task, each model step with
tokens and cost, tool calls and results, engine follow-ups) - bounded in size,
scrubbed of recognisable secrets, system prompts stored once per text.
Retention in days per instance (default 14) and per agent (0 = never, -1 =
forever); an hourly janitor deletes what is past it, "never" also purges.

Usage analysis over run receipts: filter by period, agent, model, event, status,
cost and free text; sort by every column; totals over the whole filter; hourly
or daily series grouped by agent, model or event; markers for budget denials and
model changes; run detail with receipt, transcript, tool calls and approvals.

WebUI: Analysis > Token usage > Runs / Timeline, run detail, Prompt History with
retention settings. New code, comments and texts in English; texts come from
language packs (i18n/en.json, de.json) via modules/i18n.js. CLAUDE.md records
English as the main language.

Version 0.1.12.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-19 15:33:09 +02:00

100 lines
4.4 KiB
C#

using System.Collections.Concurrent;
using ClawdDotNet.Core.Audit;
namespace ClawdDotNet.Core.Tests.Infrastructure;
/// <summary>
/// In-Memory-Attrappe des Audit-Logs für Engine-Tests — hält Einträge und Belege in
/// Listen, damit ein Test prüfen kann, was die Engine stempelt, ohne eine Datei.
/// </summary>
internal sealed class InMemoryAuditRepository : IAuditRepository
{
public ConcurrentQueue<AuditEntry> Entries { get; } = new();
public ConcurrentQueue<RunReceipt> Receipts { get; } = new();
public Task AppendAsync(AuditEntry entry, CancellationToken ct)
{
Entries.Enqueue(entry);
return Task.CompletedTask;
}
public Task RecordReceiptAsync(RunReceipt receipt, CancellationToken ct)
{
Receipts.Enqueue(receipt);
return Task.CompletedTask;
}
public Task<IReadOnlyList<AuditEntry>> ListRecentAsync(int limit, CancellationToken ct)
=> Task.FromResult<IReadOnlyList<AuditEntry>>(Entries.Reverse().Take(limit).ToList());
public Task<IReadOnlyList<AuditEntry>> ListForRunAsync(string runId, CancellationToken ct)
=> Task.FromResult<IReadOnlyList<AuditEntry>>(Entries.Where(e => e.RunId == runId).ToList());
public Task<RunReceipt?> GetReceiptForRunAsync(string runId, CancellationToken ct)
=> Task.FromResult(Receipts.LastOrDefault(r => r.RunId == runId));
public Task<IReadOnlyList<RunReceipt>> ListReceiptsForTaskAsync(string taskId, CancellationToken ct)
=> Task.FromResult<IReadOnlyList<RunReceipt>>(Receipts.Where(r => r.TaskId == taskId).ToList());
public Task<IReadOnlyList<RunReceipt>> ListReceiptsAsync(DateTime fromUtc, DateTime toUtc, int limit, CancellationToken ct)
=> Task.FromResult<IReadOnlyList<RunReceipt>>(Receipts
.Where(r => r.OccurredAt >= fromUtc && r.OccurredAt < toUtc)
.OrderBy(r => r.OccurredAt).Take(limit).ToList());
public Task<int> CountAsync(CancellationToken ct) => Task.FromResult(Entries.Count);
/// <summary>Spiegelt die SQL-Regel: Started ohne späteren Abschluss derselben CallId.</summary>
public Task<IReadOnlyList<AuditEntry>> ListOpenCallsAsync(CancellationToken ct)
{
var alle = Entries.ToList();
var abgeschlossen = alle
.Where(e => e.Status != AuditStatus.Started && e.CallId.Length > 0)
.Select(e => e.CallId)
.ToHashSet();
var offen = alle
.Where(e => e.Status == AuditStatus.Started
&& e.CallId.Length > 0
&& !abgeschlossen.Contains(e.CallId))
.ToList();
return Task.FromResult<IReadOnlyList<AuditEntry>>(offen);
}
public Task<IReadOnlyList<AuditEntry>> ListForAgentAsync(
string agentId, DateTime? seit, int limit, CancellationToken ct)
=> Task.FromResult<IReadOnlyList<AuditEntry>>(
Entries.Where(e => e.AgentId == agentId)
.Where(e => seit is null || e.OccurredAt >= seit)
.Reverse()
.Take(limit)
.ToList());
public Task<IReadOnlyList<ToolUsageStat>> SummarizeToolUsageAsync(
string? agentId, DateTime? seit, CancellationToken ct)
=> Task.FromResult<IReadOnlyList<ToolUsageStat>>(
Entries.Where(e => agentId is null || e.AgentId == agentId)
.Where(e => seit is null || e.OccurredAt >= seit)
.GroupBy(e => (e.AgentId, e.Tool))
.Select(g =>
{
var abgeschlossen = g.Where(e => e.Status != AuditStatus.Started).ToList();
var begonnen = g.Count(e => e.Status == AuditStatus.Started);
return new ToolUsageStat
{
AgentId = g.Key.AgentId,
Tool = g.Key.Tool,
Calls = abgeschlossen.Count,
Failed = abgeschlossen.Count(e => e.Status == AuditStatus.Error),
Denied = abgeschlossen.Count(e => e.Status == AuditStatus.Denied),
Unfinished = Math.Max(0, begonnen - abgeschlossen.Count),
TotalDurationMs = abgeschlossen.Sum(e => e.DurationMs),
MaxDurationMs = g.Max(e => e.DurationMs)
};
})
.OrderByDescending(s => s.Calls)
.ToList());
}