feat: live task token usage and stats-tab fixes

Capture per-session token usage from pi-coding-agent's getSessionStats()
after each promptWithFallback in the executor and merger paths, so
task.tokenUsage populates live during runs and reflects final totals on
done tasks. Previously the executor never read session usage and only
the heartbeat path bumped agent token totals, leaving task.tokenUsage
undefined even after completion.

Stats panel and done-card timing also now reflect live state: the modal
overlays the SSE-updated task prop on top of the one-shot fullDetail
snapshot, in-progress workflow steps contribute live elapsed to the
Workflow runtime metric, and the done card uses Timed duration (matching
the stats tab) with workflow runtime as fallback. Time indicator labels
coarsened to <1m / Nm / Nh / Nd.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsxdsm
2026-04-27 04:51:29 -07:00
parent 30df38efda
commit d30edfda5d
8 changed files with 334 additions and 52 deletions

View File

@@ -13,6 +13,7 @@ import { getFreshBatchData } from "../hooks/useBatchBadgeFetch";
import { useTaskDiffStats } from "../hooks/useTaskDiffStats";
import { isTaskStuck } from "../utils/taskStuck";
import { getUnifiedTaskProgress } from "../utils/taskProgress";
import { getTimedDurationMs } from "../utils/taskTiming";
import type { ToastType } from "../hooks/useToast";
import { useConfirm } from "../hooks/useConfirm";
@@ -154,12 +155,8 @@ function formatElapsedDuration(elapsedMs: number): string {
if (elapsedMs < 60_000) return "<1m";
const elapsedSeconds = elapsedMs / 1000;
const elapsedMinutes = Math.floor(elapsedSeconds / 60);
if (elapsedMinutes < 60) {
const remSeconds = Math.round(elapsedSeconds % 60);
return remSeconds > 0 ? `${elapsedMinutes}m ${remSeconds}s` : `${elapsedMinutes}m`;
}
const elapsedMinutes = Math.floor(elapsedMs / 60_000);
if (elapsedMinutes < 60) return `${elapsedMinutes}m`;
const elapsedHours = Math.floor(elapsedMinutes / 60);
if (elapsedHours < 24) return `${elapsedHours}h`;
@@ -700,6 +697,18 @@ function TaskCardComponent({
}
if (task.column === "in-progress") {
const timedDurationMs = getTimedDurationMs(task.log);
if (timedDurationMs != null) {
const elapsedLabel = formatElapsedDuration(timedDurationMs);
if (elapsedLabel) {
return {
label: elapsedLabel,
title: `Timed duration ${elapsedLabel}`,
ariaLabel: `Timed duration ${elapsedLabel}`,
};
}
}
const startMs = getInProgressTimeIndicatorStartMs(task);
if (startMs == null) {
return null;
@@ -717,14 +726,29 @@ function TaskCardComponent({
};
}
// Done cards report agent execution time (sum of workflow step durations),
// matching the Workflow runtime metric in the stats tab. Fall back to
// wallclock processing duration when no workflow timing data is available.
// Done cards report the same "Timed duration" metric shown in the stats tab
// (sum of [timing]-tagged log events). Fall back to workflow step runtime,
// then to wallclock processing duration when no instrumentation exists.
const completionMs = getDoneCompletionMs(task);
if (completionMs == null) {
return null;
}
const timedDurationMs = getTimedDurationMs(task.log);
if (timedDurationMs != null) {
const elapsedLabel = formatElapsedDuration(timedDurationMs);
if (!elapsedLabel) {
return null;
}
const completedAt = new Date(completionMs).toLocaleString();
return {
label: elapsedLabel,
title: `Timed duration ${elapsedLabel}. Completed ${completedAt}`,
ariaLabel: `Timed duration ${elapsedLabel}. Completed ${completedAt}`,
};
}
const workflowRuntimeMs = getDoneWorkflowRuntimeMs(task);
if (workflowRuntimeMs != null) {
const elapsedLabel = formatElapsedDuration(workflowRuntimeMs);
@@ -756,7 +780,7 @@ function TaskCardComponent({
title: `Processing took ${elapsedLabel}. Completed ${completedAt}`,
ariaLabel: `Completed processing duration ${elapsedLabel}. Completed ${completedAt}`,
};
}, [task.column, task.columnMovedAt, task.updatedAt, task.createdAt, task.workflowStepResults, timeIndicatorNowMs]);
}, [task.column, task.columnMovedAt, task.updatedAt, task.createdAt, task.workflowStepResults, task.log, timeIndicatorNowMs]);
useEffect(() => {
if (!hasGitHubBadge || !isInViewport) {