refactor(usage): drop the transcript aggregation path in favour of TokenTracker

This commit is contained in:
mika kuns
2026-08-24 13:49:49 +02:00
parent 44337d4b94
commit 40879705ac
6 changed files with 23 additions and 281 deletions
@@ -9,63 +9,13 @@ namespace ClaudeDo.Worker.Usage;
public sealed class TranscriptUsageReader : ITranscriptUsageReader
{
private readonly string _projectsRoot;
private readonly string _centralRoot;
private readonly string _sandboxRoot;
private readonly ConcurrentDictionary<string, FileCacheEntry> _cache = new();
// cfg is unused since the model/scope aggregation moved to TokenTracker, but the parameter
// stays so the DI registration and every existing call site keep working.
public TranscriptUsageReader(WorkerConfig cfg, string? projectsRoot = null)
{
_projectsRoot = projectsRoot ?? Paths.Expand("~/.claude/projects");
_centralRoot = NormalizePath(cfg.CentralWorktreeRoot);
_sandboxRoot = NormalizePath(cfg.SandboxRoot);
}
public Task<IReadOnlyList<UsageAggregateRow>> ReadAsync(
DateOnly start, DateOnly end, CancellationToken ct = default)
{
var seenKeys = new HashSet<string>();
var buckets = new Dictionary<(DateOnly Date, string Model, UsageScope Scope), Accumulator>();
if (Directory.Exists(_projectsRoot))
{
// A transcript last written before the window began cannot hold a record inside it, so
// it is skipped unread — that is what keeps a 7-day range off the full history (hundreds
// of MB). One day of slack absorbs local-vs-UTC skew between mtime and record stamps.
var mtimeCutoff = start.ToDateTime(TimeOnly.MinValue).AddDays(-1);
foreach (var file in new DirectoryInfo(_projectsRoot).EnumerateFiles("*.jsonl", SearchOption.AllDirectories))
{
ct.ThrowIfCancellationRequested();
if (file.LastWriteTime < mtimeCutoff) continue;
foreach (var record in GetOrReadFile(file))
{
if (record.Date < start || record.Date > end) continue;
if (!seenKeys.Add(record.DedupeKey)) continue;
var key = (record.Date, record.Model, record.Scope);
if (!buckets.TryGetValue(key, out var acc))
{
acc = new Accumulator();
buckets[key] = acc;
}
acc.Input += record.InputTokens;
acc.Output += record.OutputTokens;
acc.CacheRead += record.CacheReadTokens;
acc.CacheCreation += record.CacheCreationTokens;
acc.Messages++;
}
}
}
var rows = buckets
.Select(kv => new UsageAggregateRow(
kv.Key.Date, kv.Key.Model, kv.Key.Scope,
kv.Value.Input, kv.Value.Output, kv.Value.CacheRead, kv.Value.CacheCreation, kv.Value.Messages))
.OrderBy(r => r.Date).ThenBy(r => r.Model).ThenBy(r => r.Scope)
.ToList();
return Task.FromResult<IReadOnlyList<UsageAggregateRow>>(rows);
}
public Task<SessionUsageTotals?> ReadSessionTotalsAsync(string sessionId, CancellationToken ct = default)
@@ -130,7 +80,8 @@ public sealed class TranscriptUsageReader : ITranscriptUsageReader
if (!root.TryGetProperty("message", out var msg) || msg.ValueKind != JsonValueKind.Object) continue;
if (!msg.TryGetProperty("model", out var modelEl) || modelEl.ValueKind != JsonValueKind.String) continue;
var date = DateOnly.FromDateTime(ts.LocalDateTime);
// The timestamp and model are still parsed above: they gate out non-assistant
// lines and <synthetic> models, which is right for the session totals too.
var model = modelEl.GetString()!;
if (model == "<synthetic>") continue;
@@ -143,54 +94,26 @@ public sealed class TranscriptUsageReader : ITranscriptUsageReader
cacheCreation = GetLong(usage, "cache_creation_input_tokens");
}
var cwd = TryGetString(root, "cwd") ?? "";
var dedupeKey = TryGetString(root, "requestId")
?? TryGetString(msg, "id")
?? Guid.NewGuid().ToString();
records.Add(new UsageMessageRecord(
date, model, ResolveScope(cwd), input, output, cacheRead, cacheCreation, dedupeKey));
records.Add(new UsageMessageRecord(input, output, cacheRead, cacheCreation, dedupeKey));
}
}
return records;
}
private UsageScope ResolveScope(string cwd)
{
var norm = NormalizePath(cwd);
if (norm.Length == 0) return UsageScope.Other;
// Sibling-strategy worktrees are placed next to whatever repo they belong to
// (no single root path), but always under a literal ".claudedo-worktrees" segment.
if (norm.Split('\\').Any(seg => seg == ".claudedo-worktrees")) return UsageScope.ClaudeDo;
if (IsUnderRoot(norm, _centralRoot)) return UsageScope.ClaudeDo;
if (IsUnderRoot(norm, _sandboxRoot)) return UsageScope.ClaudeDo;
return UsageScope.Other;
}
private static bool IsUnderRoot(string normPath, string normRoot) =>
normRoot.Length > 0 && (normPath == normRoot || normPath.StartsWith(normRoot + "\\", StringComparison.Ordinal));
private static string? TryGetString(JsonElement obj, string prop) =>
obj.TryGetProperty(prop, out var el) && el.ValueKind == JsonValueKind.String ? el.GetString() : null;
private static long GetLong(JsonElement obj, string prop) =>
obj.TryGetProperty(prop, out var el) && el.TryGetInt64(out var v) ? v : 0;
private static string NormalizePath(string p) =>
(p ?? "").Replace('/', '\\').TrimEnd('\\').ToLowerInvariant();
private sealed class Accumulator
{
public long Input, Output, CacheRead, CacheCreation;
public int Messages;
}
private sealed record FileCacheEntry(long Length, DateTime LastWriteUtc, List<UsageMessageRecord> Records);
private sealed record UsageMessageRecord(
DateOnly Date, string Model, UsageScope Scope,
long InputTokens, long OutputTokens, long CacheReadTokens, long CacheCreationTokens,
string DedupeKey);
}