fix(claude-do): Run reporting: token accounting + populate empty result

BUNDLE — both fixes live in the Worker run-recording / persistence layer (where a TaskRun is written after an agent finishes), NOT in ExternalMcpService.cs. Keep this disjoint from the MCP-surface bundle so the two can run in parallel without worktree conflicts. The DTO fields (tokensIn, tokensOut, resultMarkdown) already exist and are surfaced by list_runs/get_run — the bug is at write time.

1.

ClaudeDo-Task: 49a6060a-5044-4f1b-8665-5cfc064b8a82
This commit is contained in:
Mika Kuns
2026-06-01 16:01:11 +02:00
parent 5170914a7a
commit 4c6e6594dc
2 changed files with 64 additions and 1 deletions

View File

@@ -44,6 +44,14 @@ public sealed class StreamAnalyzer
_structuredOutputJson = structuredProp.ToString();
if (root.TryGetProperty("session_id", out var sessionProp))
_sessionId = sessionProp.GetString();
// Authoritative token totals live on the result event.
if (root.TryGetProperty("usage", out var resultUsage))
{
if (resultUsage.TryGetProperty("input_tokens", out var inp))
_tokensIn = inp.GetInt32();
if (resultUsage.TryGetProperty("output_tokens", out var outp))
_tokensOut = outp.GetInt32();
}
break;
case "assistant":
@@ -66,7 +74,7 @@ public sealed class StreamAnalyzer
public StreamResult GetResult() => new()
{
ResultMarkdown = _resultMarkdown,
ResultMarkdown = FallbackResult(),
StructuredOutputJson = _structuredOutputJson,
SessionId = _sessionId,
TurnCount = _turnCount,
@@ -75,6 +83,20 @@ public sealed class StreamAnalyzer
ApiRetryCount = _apiRetryCount,
};
private string? FallbackResult()
{
if (!string.IsNullOrEmpty(_resultMarkdown)) return _resultMarkdown;
if (_structuredOutputJson is null) return _resultMarkdown;
try
{
using var doc = JsonDocument.Parse(_structuredOutputJson);
if (doc.RootElement.TryGetProperty("summary", out var s))
return s.GetString();
}
catch { }
return _structuredOutputJson;
}
private void TryAccumulateUsage(JsonElement root)
{
if (!root.TryGetProperty("event", out var eventProp)) return;