fix(usage): identify client version mismatches (#8208)

This commit is contained in:
Tristan Knight authored and GitHub committed 2026-09-26 14:58:29 -05:00
1 parent d6802b4acf
commit 3dae78f33b
5 files changed
+92 -25

No files matched your search

@@ -15,6 +15,7 @@ import {
formatHourShort,
formatPercent,
formatTokens,
formatUsageContractMismatch,
formatUsd,
makeWindow,
} from "@t3tools/shared/usageFormat";
@@ -753,7 +754,12 @@ function usageEnvironmentStatus(environment: EnvironmentUsageStatus): string {
environment.summary &&
!isCompatibleUsageContractVersion(environment.summary.contractVersion, USAGE_CONTRACT_VERSION)
) {
return "Older server · excluded from usage totals";
return formatUsageContractMismatch(environment.label, {
direction:
environment.summary.contractVersion < USAGE_CONTRACT_VERSION
? "serverBehind"
: "clientBehind",
});
}
if (!environment.isConnected)
return environment.summary ? "Disconnected · showing saved usage" : "Waiting for connection…";
+19 -13
View File
@@ -20,6 +20,7 @@ import {
isModelCostUnknown,
type DailyTotals,
type HourlyTotals,
type MergedUsage,
} from "@t3tools/shared/usageMerge";
import { isElectron } from "../../env";
@@ -40,6 +41,7 @@ import {
formatHourShort,
formatPercent,
formatTokens,
formatUsageContractMismatch,
formatUsd,
makeWindow,
} from "@t3tools/shared/usageFormat";
@@ -314,7 +316,7 @@ export function UsagePage() {
showUsageStatus={!showingLimits}
isPartial={isPartial}
duplicateSources={merged.duplicateSources}
staleEnvironments={merged.staleEnvironments}
contractMismatches={merged.contractMismatches}
/>
</WorkspaceBreadcrumbItem>
</WorkspaceBreadcrumb>
@@ -899,17 +901,21 @@ function Metric({ label, value }: { readonly label: string; readonly value: stri
function UsageCoverageNotice({
environments,
duplicateSources,
staleEnvironments,
contractMismatches,
}: {
readonly environments: readonly EnvironmentUsageStatus[];
readonly duplicateSources: readonly string[];
readonly staleEnvironments: readonly string[];
readonly contractMismatches: MergedUsage["contractMismatches"];
}) {
const failed = environments.filter((environment) => environment.error !== null);
const stale = environments.filter((environment) =>
staleEnvironments.includes(environment.environmentId),
const mismatchByEnvironment = new Map(
contractMismatches.map((mismatch) => [mismatch.environmentId, mismatch]),
);
if (failed.length === 0 && stale.length === 0 && duplicateSources.length === 0) {
const incompatible = environments.flatMap((environment) => {
const mismatch = mismatchByEnvironment.get(environment.environmentId);
return mismatch === undefined ? [] : [{ environment, mismatch }];
});
if (failed.length === 0 && incompatible.length === 0 && duplicateSources.length === 0) {
return null;
}
@@ -918,9 +924,9 @@ function UsageCoverageNotice({
{failed.map((environment) => (
<span key={environment.label}>{environment.label} could not report usage.</span>
))}
{stale.map((environment) => (
<span key={environment.label}>
{environment.label} runs an older server version and is excluded from totals.
{incompatible.map(({ environment, mismatch }) => (
<span key={environment.environmentId}>
{formatUsageContractMismatch(environment.label, mismatch)}
</span>
))}
{duplicateSources.length > 0 ? (
@@ -942,7 +948,7 @@ function UsageEnvironmentFilter({
showUsageStatus,
isPartial,
duplicateSources,
staleEnvironments,
contractMismatches,
}: {
readonly environments: readonly EnvironmentUsageStatus[];
readonly selectedEnvironments: readonly EnvironmentUsageStatus[];
@@ -951,7 +957,7 @@ function UsageEnvironmentFilter({
readonly showUsageStatus: boolean;
readonly isPartial: boolean;
readonly duplicateSources: readonly string[];
readonly staleEnvironments: readonly string[];
readonly contractMismatches: MergedUsage["contractMismatches"];
}) {
const [modelPricesOpen, setModelPricesOpen] = useState(false);
const allSelected = selectedEnvironmentIds === null;
@@ -966,7 +972,7 @@ function UsageEnvironmentFilter({
).length;
const hasIssue =
selectedEnvironments.some((environment) => environment.error !== null) ||
staleEnvironments.length > 0;
contractMismatches.length > 0;
return (
<>
@@ -1066,7 +1072,7 @@ function UsageEnvironmentFilter({
<UsageCoverageNotice
environments={selectedEnvironments}
duplicateSources={duplicateSources}
staleEnvironments={staleEnvironments}
contractMismatches={contractMismatches}
/>
) : null}
<MenuSeparator />
+11
View File
@@ -6,6 +6,8 @@
*/
import { UsageDay, type UsageResolution, type UsageSummaryInput } from "@t3tools/contracts";
import type { UsageContractMismatch } from "./usageMerge.ts";
const CURRENCY = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
@@ -49,6 +51,15 @@ export function formatPercent(share: number, digits = 1): string {
return `${percent.toFixed(digits)}%`;
}
export function formatUsageContractMismatch(
environmentLabel: string,
mismatch: Pick<UsageContractMismatch, "direction">,
): string {
return mismatch.direction === "serverBehind"
? `${environmentLabel} runs an older server version and is excluded from totals.`
: `This client is older than the server on ${environmentLabel}; its usage is excluded from totals.`;
}
/** `2026-08-07` to `Aug 7`. */
export function formatDayShort(day: string): string {
const [year, month, dayOfMonth] = day.split("-").map((part) => Number(part));
+34 -3
View File
@@ -329,7 +329,7 @@ describe("mergeUsage", () => {
expect(merged.contributingEnvironments).toEqual(["old"]);
});
it("excludes an environment reporting an older contract version", () => {
it("identifies an environment reporting an older contract version", () => {
const merged = mergeUsage(
[
environment(
@@ -349,7 +349,38 @@ describe("mergeUsage", () => {
);
expect(merged.costUsd).toBe(10);
expect(merged.staleEnvironments).toEqual(["env-b"]);
expect(merged.contractMismatches).toEqual([
{
environmentId: "env-b",
direction: "serverBehind",
contractVersion: USAGE_MERGE_COMPATIBLE_SINCE - 1,
},
]);
});
it("identifies an environment reporting a newer contract version", () => {
const merged = mergeUsage(
[
environment(
"env-a",
summary(
[bucket()],
[{ provider: "claude", hostId: "mac", homePath: "/a" }],
USAGE_CONTRACT_VERSION + 1,
),
),
],
USAGE_CONTRACT_VERSION,
);
expect(merged.costUsd).toBe(0);
expect(merged.contractMismatches).toEqual([
{
environmentId: "env-a",
direction: "clientBehind",
contractVersion: USAGE_CONTRACT_VERSION + 1,
},
]);
});
it("keeps the previous compatible contract version so additive provider expansions still merge", () => {
@@ -375,7 +406,7 @@ describe("mergeUsage", () => {
);
expect(merged.costUsd).toBe(14);
expect(merged.staleEnvironments).toEqual([]);
expect(merged.contractMismatches).toEqual([]);
});
it("keeps known usage when newer providers and bucket variants cannot be decoded", () => {
+21 -8
View File
@@ -76,6 +76,12 @@ export interface CostQuality {
readonly cacheSavingsUsd: number;
}
export interface UsageContractMismatch {
readonly environmentId: EnvironmentId;
readonly direction: "serverBehind" | "clientBehind";
readonly contractVersion: number;
}
export interface MergedUsage {
readonly costUsd: number;
readonly uncachedInputTokens: number;
@@ -94,7 +100,7 @@ export interface MergedUsage {
/** Environments whose data was dropped as a duplicate of another's. */
readonly duplicateSources: readonly string[];
readonly contributingEnvironments: readonly EnvironmentId[];
readonly staleEnvironments: readonly EnvironmentId[];
readonly contractMismatches: readonly UsageContractMismatch[];
}
/**
@@ -303,15 +309,15 @@ const EMPTY_MERGED: MergedUsage = {
},
duplicateSources: [],
contributingEnvironments: [],
staleEnvironments: [],
contractMismatches: [],
};
/**
* Merges every connected environment's summary.
*
* `expectedContractVersion` guards against an environment running older server
* code: rather than blocking the page, incompatible data is excluded and its
* id is reported so the UI can say coverage is partial. Versions in
* `expectedContractVersion` guards against incompatible server code: rather
* than blocking the page, its data is excluded and the mismatch direction is
* reported so the UI can identify which side needs updating. Versions in
* [{@link USAGE_MERGE_COMPATIBLE_SINCE}, expected] still merge, so an additive
* provider expansion does not drop Claude/Codex totals from older servers.
*/
@@ -322,14 +328,21 @@ export function mergeUsage(
if (environments.length === 0) return EMPTY_MERGED;
const current: EnvironmentUsage[] = [];
const staleEnvironments: EnvironmentId[] = [];
const contractMismatches: UsageContractMismatch[] = [];
for (const environment of environments) {
if (
isCompatibleUsageContractVersion(environment.summary.contractVersion, expectedContractVersion)
) {
current.push(environment);
} else {
staleEnvironments.push(environment.environmentId);
contractMismatches.push({
environmentId: environment.environmentId,
direction:
environment.summary.contractVersion < expectedContractVersion
? "serverBehind"
: "clientBehind",
contractVersion: environment.summary.contractVersion,
});
}
}
@@ -544,6 +557,6 @@ export function mergeUsage(
},
duplicateSources: duplicates,
contributingEnvironments,
staleEnvironments,
contractMismatches,
};
}