Files
fusion/packages/dashboard/app/utils/taskTokenCost.ts
gsxdsm dc50425e98 docs: correct 104 future-dated FNXC timestamps across 61 files (#2680)
## What

The FNXC convention exists so a reader can place a note against the
change that motivated it. A stamp dated *after* the edit landed defeats
exactly that.

This is program-wide drift, not one author's slip — I contributed to it
in my own commits this week, which is how I noticed it.

## Measured, on this tree

**104 stamps across 61 files** dated later than the day they were
written, from one day ahead to **2026-10-19 (81 days)**:

| count | date | count | date | count | date |
|---|---|---|---|---|---|
| 50 | 2026-07-31 | 6 | 2026-08-05 | 3 | 2026-08-13 |
| 17 | 2026-08-01 | 1 | 2026-08-07 | 1 | 2026-08-19 |
| 7 | 2026-08-02 | 1 | 2026-08-12 | 2 | 2026-08-26 |
| 11 | 2026-08-03 | | | 3 | 2026-10-19 |

An earlier number I circulated was ~70. That came from a narrower
pathspec and was wrong; **104** is the measurement.

## How

Each stamp is rewritten to the date of the commit that introduced **that
line**, via per-line `git blame` — deliberately *not* stamped uniformly
with today's date. A uniform stamp swaps a wrong date for a different
wrong date and flattens the ordering that makes these comments
navigable; blame preserves it. Times of day are untouched, and a blame
date in the future is clamped rather than trusted.

## Why the verification is listed

A docs sweep across 61 files is precisely where a stray edit hides, so
the safety claims are mechanical rather than asserted:

- every changed line begins with a comment marker — **no code touched**;
- **no test asserts an FNXC date later than today**, so no `toContain`
assertion on embedded source text can be silently invalidated (several
such assertions do exist);
- CSS files, which carry several of those assertions, are outside the
pathspec.

## Verified

lint clean · merge gate green (487 + 158 + 10 + 71) · `census --strict`
exit 0 · tsc clean for core, engine, and dashboard
(`tsconfig.app.json`).

**No behavior change.** Comment text only.

## Not done here

A guard preventing recurrence. A check that rejects an FNXC stamp dated
after the commit would stop this returning, but it needs a decision
about where it runs (lint rule vs. gate) and it is a behavior change to
CI — it does not belong riding inside the sweep it would police.
2026-07-30 02:38:58 -07:00

142 lines
5.7 KiB
TypeScript

import type { TaskDetail, TaskTokenUsagePerModel } from "@fusion/core";
import { costFor, type CostResult, type ModelPricingOverrides } from "../../../core/src/model-pricing";
export interface TokenCostRow {
key: string;
label: string;
modelProvider?: string;
modelId?: string;
inputTokens: number;
outputTokens: number;
cachedTokens: number;
cacheWriteTokens: number;
totalTokens: number;
cost: CostResult;
}
export function formatCount(n: number): string {
return Number.isFinite(n) ? Math.round(n).toLocaleString() : "0";
}
export function formatCost(usd: number | null, unavailable: boolean): string {
if (unavailable || usd === null || !Number.isFinite(usd)) {
return "—";
}
return `$${usd.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
}
export function toTokenBucketKey(bucket: Pick<TaskTokenUsagePerModel, "modelProvider" | "modelId">): string {
return `${bucket.modelProvider ?? ""}:${bucket.modelId ?? ""}`;
}
export function toTokenCostRow(
bucket: Pick<TaskTokenUsagePerModel, "modelProvider" | "modelId" | "inputTokens" | "outputTokens" | "cachedTokens" | "cacheWriteTokens" | "totalTokens">,
unknownLabel: string,
now: number,
pricingOverrides?: ModelPricingOverrides,
): TokenCostRow {
const modelId = bucket.modelId?.trim() || undefined;
const modelProvider = bucket.modelProvider?.trim() || undefined;
const label = modelId ?? unknownLabel;
return {
key: toTokenBucketKey({ modelProvider, modelId }),
label,
modelProvider,
modelId,
inputTokens: bucket.inputTokens,
outputTokens: bucket.outputTokens,
cachedTokens: bucket.cachedTokens,
cacheWriteTokens: bucket.cacheWriteTokens,
totalTokens: bucket.totalTokens,
cost: costFor(
{
inputTokens: bucket.inputTokens,
outputTokens: bucket.outputTokens,
cachedTokens: bucket.cachedTokens,
cacheWriteTokens: bucket.cacheWriteTokens,
},
{ provider: modelProvider, model: modelId },
now,
pricingOverrides,
),
};
}
/**
* FNXC:TaskDetailCost 2026-07-11-12:00:
* Task cost is a read-time derivation shared by the done Summary tab, always-available Cost tab, and optional card badge. Keep the costFor/pricing-overrides path centralized here so unpriced or zero-usage states keep the guess-free “—” sentinel everywhere and derived USD is never persisted.
*
* FNXC:TaskDetailSummaryTokenCost 2026-06-27-00:00:
* FNXC:TaskCost 2026-07-20-10:00: tokenUsage is a full-task additive ledger;
* triage and graph Plan Review buckets intentionally have no role filter here.
* Done-task Summary shows durable token usage broken down by model with derived USD cost. Use already-loaded task.tokenUsage.perModel buckets plus costFor and global pricing overrides threaded from TaskDetailModal; do not fetch or persist cost here. Unpriced models render “—” instead of $0 and make the task total unavailable so estimates are never understated.
*/
export function buildTokenCostRows(task: TaskDetail, unknownLabel: string, pricingOverrides?: ModelPricingOverrides): TokenCostRow[] {
const tokenUsage = task.tokenUsage;
if (!tokenUsage) return [];
const buckets = tokenUsage.perModel?.length
? tokenUsage.perModel
: [
{
modelProvider: tokenUsage.modelProvider,
modelId: tokenUsage.modelId,
inputTokens: tokenUsage.inputTokens,
outputTokens: tokenUsage.outputTokens,
cachedTokens: tokenUsage.cachedTokens,
cacheWriteTokens: tokenUsage.cacheWriteTokens,
totalTokens: tokenUsage.totalTokens,
},
];
const merged = new Map<string, Pick<TaskTokenUsagePerModel, "modelProvider" | "modelId" | "inputTokens" | "outputTokens" | "cachedTokens" | "cacheWriteTokens" | "totalTokens">>();
buckets.forEach((bucket) => {
const modelProvider = bucket.modelProvider?.trim() || undefined;
const modelId = bucket.modelId?.trim() || undefined;
const key = toTokenBucketKey({ modelProvider, modelId });
const current = merged.get(key);
if (!current) {
merged.set(key, { ...bucket, modelProvider, modelId });
return;
}
current.inputTokens += bucket.inputTokens;
current.outputTokens += bucket.outputTokens;
current.cachedTokens += bucket.cachedTokens;
current.cacheWriteTokens += bucket.cacheWriteTokens;
current.totalTokens += bucket.totalTokens;
});
const now = Date.now();
return Array.from(merged.values()).map((bucket) => toTokenCostRow(bucket, unknownLabel, now, pricingOverrides));
}
export function totalCostForRows(rows: TokenCostRow[]): { usd: number | null; unavailable: boolean } {
let usd = 0;
let unavailable = false;
let hasPositiveUsage = false;
rows.forEach((row) => {
if (row.totalTokens <= 0) return;
hasPositiveUsage = true;
if (row.cost.unavailable || row.cost.usd === null || !Number.isFinite(row.cost.usd)) {
unavailable = true;
return;
}
usd += row.cost.usd;
});
return { usd: unavailable || !hasPositiveUsage ? null : usd, unavailable: unavailable || !hasPositiveUsage };
}
export function hasTaskCost(task: TaskDetail): boolean {
const tokenUsage = task.tokenUsage;
if (!tokenUsage) return false;
const totalTokens = tokenUsage.totalTokens
?? ((tokenUsage.inputTokens ?? 0) + (tokenUsage.outputTokens ?? 0) + (tokenUsage.cachedTokens ?? 0) + (tokenUsage.cacheWriteTokens ?? 0));
if (totalTokens > 0) return true;
return (tokenUsage.perModel ?? []).some((bucket) => bucket.totalTokens > 0);
}
export function taskTotalCost(task: TaskDetail, pricingOverrides?: ModelPricingOverrides): CostResult {
const total = totalCostForRows(buildTokenCostRows(task, "(unknown)", pricingOverrides));
return { usd: total.usd, unavailable: total.unavailable, stale: false };
}