The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 09:30 UTC
fix(usage): combine duplicated skipped-record warnings (#23411)
Alex committed
Oct 31, 2026 at 02:01 UTC
b010581aea0a1071f6797780c120f78596c8e975
5 files changed
+185
-38
apps/mobile/src/features/usage/UsageRouteScreen.tsx
+4
-16
@@ -27,6 +27,7 @@ import {
27
formatUsageContractMismatch,
28
formatUsd,
29
makeWindow,
30
+ usageSourceMessages,
31
} from "@t3tools/shared/usageFormat";
32
import { useRef, useState, type ReactNode } from "react";
33
import { Platform, Pressable, RefreshControl, View } from "react-native";
@@ -119,22 +120,9 @@ function UsageScreen(props: {
120
void refresh();
121
void limits.refreshAfterEnable();
122
};
122
- const sourceMessages = [
123
- ...new Set(
124
- selectedEnvironments.flatMap(
125
- (environment) =>
126
- environment.summary?.sources.flatMap((source) =>
127
- source.message &&
128
- !source.action &&
129
- (source.status === "partial" ||
130
- source.status === "failed" ||
131
- source.fingerprint.provider === "cursor")
132
- ? [source.message]
133
- : [],
134
- ) ?? [],
135
- ),
136
- ),
137
- ];
123
+ const sourceMessages = usageSourceMessages(
124
+ selectedEnvironments.flatMap((environment) => environment.summary?.sources ?? []),
125
+ );
126
const canReadDiagnostics = selectedEnvironments.some(
127
(environment) => environment.canReadDiagnostics,
128
);
apps/server/src/usage/UsageService.ts
+2
-4
@@ -28,6 +28,7 @@ import * as Semaphore from "effect/Semaphore";
28
import { HttpClient, HttpClientResponse } from "effect/http";
29
30
import { writeFileStringAtomically } from "@t3tools/shared/atomicWrite";
31
+import { formatMalformedUsageWarning } from "@t3tools/shared/usageFormat";
32
import * as ServerConfig from "../config.ts";
33
import * as ServerSettings from "../serverSettings.ts";
34
import { BUILT_IN_USAGE_DRIVERS, type BuiltInUsageReadersEnv } from "../provider/builtInDrivers.ts";
@@ -855,10 +856,7 @@ export const make = Effect.gen(function* () {
856
distinctSessions: sessionIds.size,
857
message:
858
malformedRecords > 0
858
- ? [
859
- sourceMessage,
860
- `${malformedRecords} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`,
861
- ]
859
+ ? [sourceMessage, formatMalformedUsageWarning(malformedRecords)]
860
.filter(Boolean)
861
.join(" ")
862
: sourceMessage,
apps/web/src/components/usage/UsagePage.tsx
+8
-16
@@ -56,6 +56,7 @@ import {
56
formatUsageContractMismatch,
57
formatUsd,
58
makeWindow,
59
+ usageSourceMessages,
60
} from "@t3tools/shared/usageFormat";
61
import { Button, InlineButton } from "../ui/button";
62
import { ProviderInstanceIcon } from "../chat/ProviderInstanceIcon";
@@ -650,23 +651,14 @@ function UsageOverview({
651
const chartLoadingProviders = new Set(
652
activeProviders.filter((provider) => loadingProviders.has(provider)),
653
);
653
- const sourceMessages = [
654
- ...new Set(
655
- selectedEnvironments.flatMap(
656
- (environment) =>
657
- environment.summary?.sources.flatMap((source) =>
658
- source.message &&
659
- !source.action &&
660
- !hiddenProviders.has(source.fingerprint.provider) &&
661
- (source.status === "partial" ||
662
- source.status === "failed" ||
663
- source.fingerprint.provider === "cursor")
664
- ? [source.message]
665
- : [],
666
- ) ?? [],
667
- ),
654
+ const sourceMessages = usageSourceMessages(
655
+ selectedEnvironments.flatMap(
656
+ (environment) =>
657
+ environment.summary?.sources.filter(
658
+ (source) => !hiddenProviders.has(source.fingerprint.provider),
659
+ ) ?? [],
660
),
669
- ];
661
+ );
662
const breakdownPeriods: readonly (DailyTotals | HourlyTotals)[] = (
663
shownHourly ? merged.hourly : merged.daily
664
).toReversed();
packages/shared/src/usageFormat.test.ts
+126
-1
@@ -1,7 +1,132 @@
1
// @effect-diagnostics globalDate:off -- A fixed instant keeps calendar-window assertions deterministic.
2
import { describe, expect, it, vi } from "vite-plus/test";
3
+import type { UsageSource } from "@t3tools/contracts";
4
4
-import { formatHourShort, makeWindow } from "./usageFormat.ts";
5
+import { formatHourShort, makeWindow, usageSourceMessages } from "./usageFormat.ts";
6
+
7
+function malformedSource(count: number, overrides: Partial<UsageSource> = {}): UsageSource {
8
+ return {
9
+ fingerprint: {
10
+ hostId: "demo-host",
11
+ provider: "codex",
12
+ resolvedHomePath: "/demo/codex",
13
+ volumeId: "demo-volume",
14
+ },
15
+ status: "partial",
16
+ scannedFiles: 1,
17
+ skippedFiles: 0,
18
+ malformedRecords: count,
19
+ distinctSessions: 1,
20
+ message: `${count} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`,
21
+ ...overrides,
22
+ };
23
+}
24
+
25
+describe("usage source messages", () => {
26
+ it("combines different skipped-record counts into one warning", () => {
27
+ const codex = malformedSource(1);
28
+ const claude = malformedSource(11, {
29
+ fingerprint: { ...codex.fingerprint, provider: "claude", resolvedHomePath: "/demo/claude" },
30
+ });
31
+
32
+ expect(usageSourceMessages([codex, claude])).toEqual([
33
+ "12 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
34
+ ]);
35
+ });
36
+
37
+ it.each([
38
+ { first: 1, second: 1, total: 2 },
39
+ { first: 1, second: 11, total: 12 },
40
+ ])(
41
+ "combines hosts reporting $first and $second skipped records once per source",
42
+ ({ first, second, total }) => {
43
+ const source = malformedSource(first);
44
+ const otherHost = malformedSource(second, {
45
+ fingerprint: { ...source.fingerprint, hostId: "other-demo-host" },
46
+ });
47
+
48
+ expect(usageSourceMessages([source, source, otherHost, otherHost])).toEqual([
49
+ `${total} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`,
50
+ ]);
51
+ expect(usageSourceMessages([source, source])).toEqual([
52
+ `${first} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`,
53
+ ]);
54
+ expect(usageSourceMessages([otherHost, otherHost])).toEqual([
55
+ `${second} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`,
56
+ ]);
57
+ },
58
+ );
59
+
60
+ it("keeps different machines with the same hostname and home path distinct", () => {
61
+ const source = malformedSource(1);
62
+ const otherMachine = malformedSource(11, {
63
+ fingerprint: { ...source.fingerprint, volumeId: "other-demo-volume" },
64
+ });
65
+
66
+ expect(usageSourceMessages([source, otherMachine])).toEqual([
67
+ "12 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
68
+ ]);
69
+ });
70
+
71
+ it("uses the largest count when connected environments report the same source", () => {
72
+ const source = malformedSource(1);
73
+
74
+ expect(usageSourceMessages([source, malformedSource(11), source])).toEqual([
75
+ "11 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
76
+ ]);
77
+ });
78
+
79
+ it("retains other diagnostics beside the combined warning", () => {
80
+ const source = malformedSource(1);
81
+ const readFailure = "2 transcript path(s) could not be read; usage may be incomplete.";
82
+ const cursor = malformedSource(0, {
83
+ fingerprint: { ...source.fingerprint, provider: "cursor" },
84
+ status: "ok",
85
+ message: "Cursor history is limited to the last 30 days.",
86
+ });
87
+
88
+ expect(
89
+ usageSourceMessages([
90
+ { ...source, message: `${readFailure} ${source.message}` },
91
+ { ...source, malformedRecords: 0, message: readFailure },
92
+ cursor,
93
+ ]),
94
+ ).toEqual([
95
+ readFailure,
96
+ cursor.message,
97
+ "1 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
98
+ ]);
99
+ });
100
+
101
+ it("includes cached diagnostics when a transcript directory is missing", () => {
102
+ const source = malformedSource(1);
103
+ const missing = malformedSource(2, {
104
+ fingerprint: { ...source.fingerprint, hostId: "other-demo-host" },
105
+ status: "missing",
106
+ scannedFiles: 0,
107
+ skippedFiles: 1,
108
+ distinctSessions: 0,
109
+ message:
110
+ "No transcript directory on this environment. 2 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
111
+ });
112
+
113
+ expect(usageSourceMessages([source, missing, missing])).toEqual([
114
+ "No transcript directory on this environment.",
115
+ "3 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
116
+ ]);
117
+ });
118
+
119
+ it("omits empty missing sources, healthy sources, and diagnostics handled by an action", () => {
120
+ expect(
121
+ usageSourceMessages([
122
+ malformedSource(0, { status: "missing" }),
123
+ malformedSource(1, { status: "ok" }),
124
+ malformedSource(1, { action: "enableCursorKeychain" }),
125
+ ]),
126
+ ).toEqual([]);
127
+ expect(usageSourceMessages([])).toEqual([]);
128
+ });
129
+});
130
131
describe("hourly usage formatting", () => {
132
it("keeps requested zones separate when formatting repeated calls", () => {
packages/shared/src/usageFormat.ts
+45
-1
@@ -1,5 +1,10 @@
1
// @effect-diagnostics globalDate:off -- Usage windows are calendar days in the viewer's zone, derived from wall-clock "now" via Intl.
2
-import { UsageDay, type UsageResolution, type UsageSummaryInput } from "@t3tools/contracts";
2
+import {
3
+ UsageDay,
4
+ type UsageResolution,
5
+ type UsageSource,
6
+ type UsageSummaryInput,
7
+} from "@t3tools/contracts";
8
9
import type { UsageContractMismatch } from "./usageMerge.ts";
10
@@ -12,6 +17,45 @@ const CURRENCY = new Intl.NumberFormat("en-US", {
17
18
const INTEGER = new Intl.NumberFormat("en-US");
19
20
+export function formatMalformedUsageWarning(count: number): string {
21
+ return `${count} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`;
22
+}
23
+
24
+export function usageSourceMessages(sources: readonly UsageSource[]): readonly string[] {
25
+ const messages = new Set<string>();
26
+ const malformedBySource = new Map<string, number>();
27
+ for (const source of sources) {
28
+ if (
29
+ source.action ||
30
+ (source.status !== "partial" &&
31
+ source.status !== "failed" &&
32
+ source.fingerprint.provider !== "cursor" &&
33
+ !(source.status === "missing" && source.malformedRecords > 0))
34
+ ) {
35
+ continue;
36
+ }
37
+ let message = source.message;
38
+ if (source.malformedRecords > 0) {
39
+ const { hostId, provider, resolvedHomePath, volumeId } = source.fingerprint;
40
+ const key = JSON.stringify([hostId, provider, resolvedHomePath, volumeId]);
41
+ malformedBySource.set(
42
+ key,
43
+ Math.max(malformedBySource.get(key) ?? 0, source.malformedRecords),
44
+ );
45
+ // Servers also append this warning to message; retain any preceding read failure.
46
+ const warning = formatMalformedUsageWarning(source.malformedRecords);
47
+ if (message?.endsWith(warning)) message = message.slice(0, -warning.length).trim();
48
+ }
49
+ if (message) messages.add(message);
50
+ }
51
+ const malformedRecords = [...malformedBySource.values()].reduce(
52
+ (total, count) => total + count,
53
+ 0,
54
+ );
55
+ if (malformedRecords > 0) messages.add(formatMalformedUsageWarning(malformedRecords));
56
+ return [...messages];
57
+}
58
+
59
export function formatUsd(value: number): string {
60
return CURRENCY.format(value);
61
}