Files
ClawdDotNet/src/ClawdDotNet.App/Services/RunInsights.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

72 lines
2.8 KiB
C#

using ClawdDotNet.Core.Analytics;
using ClawdDotNet.Core.Audit;
using ClawdDotNet.Core.Staging;
using ClawdDotNet.Core.Tasks;
using ClawdDotNet.Core.Transcripts;
namespace ClawdDotNet.App.Services;
/// <summary>Everything known about a single run (#660).</summary>
public sealed record RunDetail(
string RunId,
RunReceipt? Receipt,
string? TaskTitle,
IReadOnlyList<AuditEntry> ToolCalls,
IReadOnlyList<StagedCall> Approvals,
RunTranscript? Transcript,
string? SystemPrompt,
string TranscriptState);
/// <summary>
/// Read side of the usage analysis and the prompt history (#660) for both user interfaces.
/// Owns no data: receipts and audit come from the audit repository, transcripts from the
/// transcript store, task titles from the board.
/// </summary>
public sealed class RunInsights(
IAuditRepository audit,
IRunTranscriptStore? transcripts,
TaskboardService? board,
StagingService? staging,
Func<string, int> retentionFor)
{
public UsageAnalytics Analytics { get; } = new(audit, board is null
? null
: async (id, ct) => (await board.GetAsync(id, ct))?.Title);
public IRunTranscriptStore? Transcripts => transcripts;
public async Task<RunDetail?> GetRunAsync(string runId, CancellationToken ct)
{
var receipt = await audit.GetReceiptForRunAsync(runId, ct);
var calls = await audit.ListForRunAsync(runId, ct);
var transcript = transcripts is null ? null : await transcripts.GetAsync(runId, ct);
if (receipt is null && calls.Count == 0 && transcript is null)
return null;
var agentId = receipt?.AgentId ?? transcript?.AgentId ?? calls.FirstOrDefault()?.AgentId ?? "";
var taskId = receipt?.TaskId ?? transcript?.TaskId;
return new RunDetail(
runId,
receipt,
taskId is not null && board is not null ? (await board.GetAsync(taskId, ct))?.Title : null,
// Only the completed calls; the "started" markers (#554) would double every line.
calls.Where(c => c.Status != AuditStatus.Started).ToList(),
staging is null ? [] : await staging.ListForRunAsync(runId, ct),
transcript,
transcript?.SystemPromptHash is { } hash && transcripts is not null
? await transcripts.GetSystemPromptAsync(hash, ct)
: null,
TranscriptState(transcript, agentId));
}
/// <summary>Why there is or is not a transcript - the UI says it instead of showing an empty box.</summary>
private string TranscriptState(RunTranscript? transcript, string agentId)
{
if (transcript is not null) return "recorded";
if (transcripts is null) return "unavailable";
return retentionFor(agentId) == PromptHistorySettings.Never ? "disabled" : "missing";
}
}