feat(usage): read cursor, opencode, and antigravity history (#10409)
Co-authored-by: Yash Singh <[email protected]>
This commit is contained in:
1 parent
1c12706638
commit
e5a46d6c5d
43 files changed
+3053
-133
No files matched your search
@@ -5,13 +5,15 @@ import {
|
||||
collectLimitAccounts,
|
||||
collectLimitNotices,
|
||||
collectLimitPools,
|
||||
cursorUsageWindowDetails,
|
||||
displayLimitWindows,
|
||||
formatDuration,
|
||||
formatResetsIn,
|
||||
remainingPercent,
|
||||
type LimitAccount,
|
||||
type LimitPoolWindow,
|
||||
} from "@t3tools/shared/usageLimits";
|
||||
import { useId, useState } from "react";
|
||||
import { Fragment, type ReactNode, useId, useState } from "react";
|
||||
import { Pressable, ScrollView, View } from "react-native";
|
||||
import { Defs, Path, Pattern, Rect, Svg } from "react-native-svg";
|
||||
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
||||
@@ -70,11 +72,15 @@ function PoolWindowCard({
|
||||
color,
|
||||
now,
|
||||
environmentIds,
|
||||
label,
|
||||
description,
|
||||
}: {
|
||||
readonly pool: LimitPoolWindow;
|
||||
readonly color: string;
|
||||
readonly now: number;
|
||||
readonly environmentIds: readonly string[] | null;
|
||||
readonly label?: string;
|
||||
readonly description?: string;
|
||||
}) {
|
||||
const navigation = useNavigation();
|
||||
const nextRefill = pool.resets.find((reset) => reset.restoresPercent > 0);
|
||||
@@ -96,7 +102,7 @@ function PoolWindowCard({
|
||||
<View className="gap-3 rounded-[24px] border-continuous bg-card p-4">
|
||||
<View className="flex-row items-start justify-between gap-3">
|
||||
<View className="gap-1">
|
||||
<Text className="text-sm font-t3-medium text-foreground">{pool.label}</Text>
|
||||
<Text className="text-sm font-t3-medium text-foreground">{label ?? pool.label}</Text>
|
||||
<View className="flex-row items-baseline gap-1.5">
|
||||
<Text className="text-3xl font-t3-bold tabular-nums text-foreground">
|
||||
{pool.remainingPercent}%
|
||||
@@ -108,6 +114,7 @@ function PoolWindowCard({
|
||||
<Text className="text-xs text-foreground-tertiary">{PACE_LABEL[pool.pace]}</Text>
|
||||
) : null}
|
||||
</View>
|
||||
{description ? <Text className="text-xs text-foreground-muted">{description}</Text> : null}
|
||||
{nextRefill ? (
|
||||
<Text className="text-xs tabular-nums text-foreground-muted">
|
||||
↻ +{nextRefill.restoresPercent}%{" "}
|
||||
@@ -196,10 +203,12 @@ export function UsageLimitsSection({
|
||||
now,
|
||||
failedLabels,
|
||||
selectedEnvironmentIds,
|
||||
cursorPrompt,
|
||||
}: {
|
||||
readonly now: number;
|
||||
readonly failedLabels: readonly string[];
|
||||
readonly selectedEnvironmentIds: ReadonlySet<EnvironmentId> | null;
|
||||
readonly cursorPrompt?: ReactNode;
|
||||
}) {
|
||||
const presentations = useAtomValue(environmentPresentations.presentationsAtom);
|
||||
const selected =
|
||||
@@ -209,34 +218,54 @@ export function UsageLimitsSection({
|
||||
const pools = collectLimitPools(collectLimitAccounts(selected), now);
|
||||
const notices = collectLimitNotices(selected);
|
||||
const colors = useProviderColors();
|
||||
const cursorPromptAt =
|
||||
Math.max(
|
||||
pools.findIndex((pool) => pool.driver === "codex"),
|
||||
pools.findIndex((pool) => pool.driver === "claudeAgent"),
|
||||
) + 1;
|
||||
return (
|
||||
<View className="gap-6">
|
||||
{pools.length === 0 && notices.length === 0 && failedLabels.length === 0 ? (
|
||||
{pools.length === 0 && notices.length === 0 && failedLabels.length === 0 && !cursorPrompt ? (
|
||||
<Text className="py-12 text-center text-base text-foreground-muted">
|
||||
{selected.size === 0
|
||||
? "Select an environment to see limits."
|
||||
: "No provider on the selected environments reports subscription limits."}
|
||||
</Text>
|
||||
) : null}
|
||||
{pools.map((pool) => (
|
||||
<View key={pool.driver} className="gap-3">
|
||||
<View className="flex-row items-center gap-2 px-1">
|
||||
<ProviderIcon provider={pool.driver} size={18} />
|
||||
<Text className="text-base font-t3-medium text-foreground">
|
||||
{DRIVER_LABEL[pool.driver] ?? pool.driver}
|
||||
</Text>
|
||||
</View>
|
||||
{pool.windows.map((window) => (
|
||||
<PoolWindowCard
|
||||
key={`${window.kind}:${window.id}`}
|
||||
pool={window}
|
||||
color={pool.driver === "claudeAgent" ? colors.claude : colors.codex}
|
||||
now={now}
|
||||
environmentIds={selectedEnvironmentIds === null ? null : [...selectedEnvironmentIds]}
|
||||
/>
|
||||
))}
|
||||
</View>
|
||||
))}
|
||||
{pools.map((pool, index) => {
|
||||
const windows = displayLimitWindows(pool);
|
||||
return (
|
||||
<Fragment key={pool.driver}>
|
||||
{index === cursorPromptAt ? cursorPrompt : null}
|
||||
<View className="gap-3">
|
||||
<View className="flex-row items-center gap-2 px-1">
|
||||
<ProviderIcon provider={pool.driver} size={18} />
|
||||
<Text className="text-base font-t3-medium text-foreground">
|
||||
{DRIVER_LABEL[pool.driver] ?? pool.driver}
|
||||
</Text>
|
||||
</View>
|
||||
{windows.map((window) => {
|
||||
const details =
|
||||
pool.driver === "cursor" ? cursorUsageWindowDetails(window.id) : undefined;
|
||||
return (
|
||||
<PoolWindowCard
|
||||
key={`${window.kind}:${window.id}`}
|
||||
pool={window}
|
||||
color={pool.driver === "claudeAgent" ? colors.claude : colors.codex}
|
||||
now={now}
|
||||
environmentIds={
|
||||
selectedEnvironmentIds === null ? null : [...selectedEnvironmentIds]
|
||||
}
|
||||
label={details?.label}
|
||||
description={details?.description}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</View>
|
||||
</Fragment>
|
||||
);
|
||||
})}
|
||||
{cursorPromptAt === pools.length ? cursorPrompt : null}
|
||||
{notices.length > 0 || failedLabels.length > 0 ? (
|
||||
<View
|
||||
accessible
|
||||
|
||||
@@ -294,7 +294,7 @@ export function useRefreshLimits(
|
||||
const [failedEnvironments, setFailedEnvironments] = useState<
|
||||
readonly { environmentId: EnvironmentId; label: string }[]
|
||||
>([]);
|
||||
const refresh = async (automatic = false) => {
|
||||
const refresh = async (automatic = false, afterPending = false) => {
|
||||
const connected = [...presentations].filter(
|
||||
([environmentId, presentation]) =>
|
||||
presentation.connection.phase === "connected" &&
|
||||
@@ -307,6 +307,7 @@ export function useRefreshLimits(
|
||||
environmentId,
|
||||
() => refreshProviders({ environmentId, input: {} }),
|
||||
automatic,
|
||||
afterPending,
|
||||
);
|
||||
if (result === undefined) return;
|
||||
setFailedEnvironments((previous) => [
|
||||
@@ -354,5 +355,11 @@ export function useRefreshLimits(
|
||||
selectedEnvironmentIds === null || selectedEnvironmentIds.has(environmentId),
|
||||
)
|
||||
.map(({ label }) => label);
|
||||
return { now, refreshing, failedLabels, refresh: refreshManually };
|
||||
return {
|
||||
now,
|
||||
refreshing,
|
||||
failedLabels,
|
||||
refresh: refreshManually,
|
||||
refreshAfterEnable: () => refresh(false, true),
|
||||
};
|
||||
}
|
||||
@@ -25,9 +25,12 @@ import { useSafeAreaInsets } from "react-native-safe-area-context";
|
||||
|
||||
import { SegmentedControl } from "../../components/SegmentedControl";
|
||||
import { AppText as Text } from "../../components/AppText";
|
||||
import { ProviderIcon } from "../../components/ProviderIcon";
|
||||
import { cn } from "../../lib/cn";
|
||||
import { SettingsScreen } from "../settings/components/SettingsScreen";
|
||||
import { useUsage, type EnvironmentUsageStatus } from "../../state/usage";
|
||||
import { serverEnvironment } from "../../state/server";
|
||||
import { useAtomCommand } from "../../state/use-atom-command";
|
||||
import { SettingsSection } from "../settings/components/SettingsSection";
|
||||
import { UsageDailyChart } from "./UsageDailyChart";
|
||||
import { toggleUsageEnvironment } from "./usageEnvironmentSelection";
|
||||
@@ -59,6 +62,7 @@ const METRIC_OPTIONS = [
|
||||
] as const satisfies readonly { value: UsageChartMetric; label: string }[];
|
||||
|
||||
const CHART_HEIGHT = 180;
|
||||
const CURSOR_KEYCHAIN_COPY = "Requires access to your Cursor login in macOS Keychain.";
|
||||
|
||||
/**
|
||||
* Two tabs over one screen. Usage is the transcript-derived spend for a
|
||||
@@ -97,6 +101,29 @@ export function UsageRouteScreen() {
|
||||
);
|
||||
const isFocused = useIsFocused();
|
||||
const limits = useRefreshLimits(selectedEnvironmentIds, isFocused && tab === "limits");
|
||||
const cursorAccessEnvironments = selectedEnvironments.filter((environment) =>
|
||||
environment.summary?.sources.some((source) => source.action === "enableCursorKeychain"),
|
||||
);
|
||||
const refreshAfterCursorEnable = () => {
|
||||
void refresh();
|
||||
void limits.refreshAfterEnable();
|
||||
};
|
||||
const sourceMessages = [
|
||||
...new Set(
|
||||
selectedEnvironments.flatMap(
|
||||
(environment) =>
|
||||
environment.summary?.sources.flatMap((source) =>
|
||||
source.message &&
|
||||
!source.action &&
|
||||
(source.status === "partial" ||
|
||||
source.status === "failed" ||
|
||||
source.fingerprint.provider === "cursor")
|
||||
? [source.message]
|
||||
: [],
|
||||
) ?? [],
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
const days = useMemo(
|
||||
() => enumerateDays(window.sinceDay, window.untilDay),
|
||||
@@ -251,7 +278,6 @@ export function UsageRouteScreen() {
|
||||
}
|
||||
>
|
||||
<SegmentedControl options={TAB_OPTIONS} selected={tab} onSelect={setTab} role="tab" />
|
||||
|
||||
<Animated.View
|
||||
key={tab}
|
||||
entering={FadeIn.duration(160).reduceMotion(ReduceMotion.System)}
|
||||
@@ -262,6 +288,14 @@ export function UsageRouteScreen() {
|
||||
now={limits.now}
|
||||
failedLabels={limits.failedLabels}
|
||||
selectedEnvironmentIds={selectedEnvironmentIds}
|
||||
cursorPrompt={
|
||||
cursorAccessEnvironments.length > 0 ? (
|
||||
<CursorEnableLimits
|
||||
environments={cursorAccessEnvironments}
|
||||
onEnabled={refreshAfterCursorEnable}
|
||||
/>
|
||||
) : null
|
||||
}
|
||||
/>
|
||||
) : (
|
||||
<>
|
||||
@@ -301,6 +335,11 @@ export function UsageRouteScreen() {
|
||||
</Text>
|
||||
) : (
|
||||
<>
|
||||
{sourceMessages.map((message) => (
|
||||
<Text key={message} className="text-sm text-foreground-muted">
|
||||
{message}
|
||||
</Text>
|
||||
))}
|
||||
<ChartCard
|
||||
merged={merged}
|
||||
days={chartDays}
|
||||
@@ -311,7 +350,13 @@ export function UsageRouteScreen() {
|
||||
isPast24Hours={isPast24Hours}
|
||||
timeZone={window.timeZone}
|
||||
/>
|
||||
<ProviderSection merged={merged} metric={metric} />
|
||||
<ProviderSection
|
||||
merged={merged}
|
||||
metric={metric}
|
||||
cursorAccessEnvironments={cursorAccessEnvironments}
|
||||
showCursorEnvironment={selectedEnvironments.length > 1}
|
||||
onCursorEnabled={refreshAfterCursorEnable}
|
||||
/>
|
||||
<TotalsSection merged={merged} isPast24Hours={isPast24Hours} />
|
||||
<ModelsSection merged={merged} />
|
||||
</>
|
||||
@@ -324,6 +369,110 @@ export function UsageRouteScreen() {
|
||||
);
|
||||
}
|
||||
|
||||
function CursorEnableAction({
|
||||
environmentId,
|
||||
label,
|
||||
onEnabled,
|
||||
buttonText = "Enable",
|
||||
}: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly label: string;
|
||||
readonly onEnabled: () => void;
|
||||
readonly buttonText?: string;
|
||||
}) {
|
||||
const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
|
||||
label: "enable Cursor account usage",
|
||||
});
|
||||
const [pending, setPending] = useState(false);
|
||||
const enable = async () => {
|
||||
setPending(true);
|
||||
try {
|
||||
const result = await updateSettings({
|
||||
environmentId,
|
||||
input: { patch: { cursorKeychainUsageEnabled: true } },
|
||||
});
|
||||
if (result._tag === "Success") onEnabled();
|
||||
} finally {
|
||||
setPending(false);
|
||||
}
|
||||
};
|
||||
return (
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel={`Enable Cursor usage from ${label}`}
|
||||
accessibilityHint={CURSOR_KEYCHAIN_COPY}
|
||||
disabled={pending}
|
||||
onPress={() => void enable()}
|
||||
className="rounded-full bg-primary px-4 py-2"
|
||||
>
|
||||
<Text className="text-sm font-medium text-primary-foreground">{buttonText}</Text>
|
||||
</Pressable>
|
||||
);
|
||||
}
|
||||
|
||||
function CursorEnableRow({
|
||||
environmentId,
|
||||
label,
|
||||
showEnvironment,
|
||||
bordered,
|
||||
onEnabled,
|
||||
}: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly label: string;
|
||||
readonly showEnvironment: boolean;
|
||||
readonly bordered: boolean;
|
||||
readonly onEnabled: () => void;
|
||||
}) {
|
||||
const colors = useProviderColors();
|
||||
return (
|
||||
<View
|
||||
className={cn(
|
||||
"flex-row items-center justify-between gap-3 p-4",
|
||||
bordered && "border-t border-border-subtle",
|
||||
)}
|
||||
>
|
||||
<View className="min-w-0 flex-1 flex-row items-center gap-2">
|
||||
<View className="size-2.5 rounded-full" style={{ backgroundColor: colors.cursor }} />
|
||||
<Text className="shrink text-lg text-foreground">
|
||||
Cursor{showEnvironment ? ` · ${label}` : ""}
|
||||
</Text>
|
||||
</View>
|
||||
<CursorEnableAction environmentId={environmentId} label={label} onEnabled={onEnabled} />
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
function CursorEnableLimits({
|
||||
environments,
|
||||
onEnabled,
|
||||
}: {
|
||||
readonly environments: readonly EnvironmentUsageStatus[];
|
||||
readonly onEnabled: () => void;
|
||||
}) {
|
||||
return (
|
||||
<View className="gap-3">
|
||||
<View className="flex-row items-center gap-2 px-1">
|
||||
<ProviderIcon provider="cursor" size={18} />
|
||||
<Text className="text-base font-t3-medium text-foreground">Cursor</Text>
|
||||
</View>
|
||||
<View className="items-start gap-3 rounded-[24px] border-continuous bg-card p-4">
|
||||
<Text className="text-xs text-foreground-muted">{CURSOR_KEYCHAIN_COPY}</Text>
|
||||
<View className="flex-row flex-wrap gap-2">
|
||||
{environments.map((environment) => (
|
||||
<CursorEnableAction
|
||||
key={environment.environmentId}
|
||||
environmentId={environment.environmentId}
|
||||
label={environment.label}
|
||||
buttonText={environments.length > 1 ? `Enable on ${environment.label}` : "Enable"}
|
||||
onEnabled={onEnabled}
|
||||
/>
|
||||
))}
|
||||
</View>
|
||||
</View>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
/** Headline figure, the animated daily chart, and its legend, in one card. */
|
||||
function ChartCard(props: {
|
||||
readonly merged: MergedUsage;
|
||||
@@ -400,20 +549,53 @@ function ChartCard(props: {
|
||||
function ProviderSection(props: {
|
||||
readonly merged: MergedUsage;
|
||||
readonly metric: UsageChartMetric;
|
||||
readonly cursorAccessEnvironments: readonly EnvironmentUsageStatus[];
|
||||
readonly showCursorEnvironment: boolean;
|
||||
readonly onCursorEnabled: () => void;
|
||||
}) {
|
||||
const { merged, metric } = props;
|
||||
const colors = useProviderColors();
|
||||
if (merged.providers.length === 0) return null;
|
||||
if (merged.providers.length === 0 && props.cursorAccessEnvironments.length === 0) return null;
|
||||
|
||||
// Ranked by whatever the toggle is showing, so the rows always descend.
|
||||
// .sort() on a copy, not .toSorted(): Hermes doesn't ship the ES2023 method.
|
||||
const ordered = [...merged.providers].sort((a, b) =>
|
||||
metric === "cost" ? b.costUsd - a.costUsd : b.totalTokens - a.totalTokens,
|
||||
);
|
||||
const rows: Array<
|
||||
| { readonly kind: "usage"; readonly provider: (typeof ordered)[number] }
|
||||
| { readonly kind: "enable"; readonly environment: EnvironmentUsageStatus }
|
||||
> = ordered.map((provider) => ({ kind: "usage", provider }));
|
||||
const cursorInsertAt =
|
||||
Math.max(
|
||||
ordered.findIndex((provider) => provider.provider === "codex"),
|
||||
ordered.findIndex((provider) => provider.provider === "claude"),
|
||||
) + 1;
|
||||
rows.splice(
|
||||
cursorInsertAt,
|
||||
0,
|
||||
...props.cursorAccessEnvironments.map((environment) => ({
|
||||
kind: "enable" as const,
|
||||
environment,
|
||||
})),
|
||||
);
|
||||
|
||||
return (
|
||||
<SettingsSection title="Providers">
|
||||
{ordered.map((provider, index) => {
|
||||
{rows.map((row, index) => {
|
||||
if (row.kind === "enable") {
|
||||
return (
|
||||
<CursorEnableRow
|
||||
key={`enable:${row.environment.environmentId}`}
|
||||
environmentId={row.environment.environmentId}
|
||||
label={row.environment.label}
|
||||
showEnvironment={props.showCursorEnvironment}
|
||||
bordered={index > 0}
|
||||
onEnabled={props.onCursorEnabled}
|
||||
/>
|
||||
);
|
||||
}
|
||||
const provider = row.provider;
|
||||
const share = metric === "cost" ? provider.costShare : provider.tokenShare;
|
||||
return (
|
||||
<View
|
||||
|
||||
@@ -5,12 +5,22 @@ import { useAppearancePreferences } from "../settings/appearance/AppearancePrefe
|
||||
* Series and table order. The chart stacks providers from the bottom in this
|
||||
* order, so it also fixes which band sits on top of the bars.
|
||||
*/
|
||||
export const PROVIDER_ORDER: readonly UsageProviderKind[] = ["codex", "claude", "grok"];
|
||||
export const PROVIDER_ORDER: readonly UsageProviderKind[] = [
|
||||
"codex",
|
||||
"claude",
|
||||
"grok",
|
||||
"cursor",
|
||||
"opencode",
|
||||
"antigravity",
|
||||
];
|
||||
|
||||
export const PROVIDER_LABEL: Record<UsageProviderKind, string> = {
|
||||
claude: "Claude Code",
|
||||
codex: "Codex",
|
||||
grok: "Grok Build",
|
||||
cursor: "Cursor",
|
||||
opencode: "OpenCode",
|
||||
antigravity: "Antigravity",
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -23,5 +33,8 @@ export function useProviderColors(): Record<UsageProviderKind, string> {
|
||||
claude: "#d97757",
|
||||
codex: scheme === "dark" ? "#e6e6e6" : "#3c3c43",
|
||||
grok: scheme === "dark" ? "#a1a1aa" : "#52525b",
|
||||
cursor: "#8b8b8b",
|
||||
opencode: "#5b9bbd",
|
||||
antigravity: "#8c7bd1",
|
||||
};
|
||||
}
|
||||
@@ -27,6 +27,7 @@
|
||||
"@effect/platform-node": "catalog:",
|
||||
"@effect/platform-node-shared": "catalog:",
|
||||
"@ff-labs/fff-node": "0.9.4",
|
||||
"@napi-rs/keyring": "^1.3.0",
|
||||
"@opencode-ai/sdk": "^1.3.15",
|
||||
"diff": "8.0.3",
|
||||
"effect": "catalog:",
|
||||
|
||||
@@ -148,9 +148,15 @@ export const CursorDriver: ProviderDriver<CursorSettings, CursorDriverEnv> = {
|
||||
snapshot.auth.status === "authenticated"
|
||||
),
|
||||
(snapshot) =>
|
||||
readCursorUsageLimits(effectiveConfig, processEnv).pipe(
|
||||
Effect.map((usageLimits) => ({ ...snapshot, usageLimits })),
|
||||
),
|
||||
Effect.gen(function* () {
|
||||
const settings = yield* serverSettings.getSettings;
|
||||
const usageLimits = yield* readCursorUsageLimits(
|
||||
effectiveConfig,
|
||||
processEnv,
|
||||
settings.cursorKeychainUsageEnabled,
|
||||
);
|
||||
return { ...snapshot, usageLimits };
|
||||
}),
|
||||
),
|
||||
Effect.map(stampIdentity),
|
||||
Effect.provideService(HttpClient.HttpClient, httpClient),
|
||||
|
||||
@@ -991,21 +991,21 @@ describe("Cursor usage limits", () => {
|
||||
{
|
||||
id: "totalPercentUsed",
|
||||
kind: "monthly",
|
||||
label: "Monthly",
|
||||
label: "Overall",
|
||||
usedPercent: 72.4,
|
||||
resetsAt: "2026-09-20T03:53:06.000Z",
|
||||
},
|
||||
{
|
||||
id: "autoPercentUsed",
|
||||
kind: "monthly",
|
||||
label: "Monthly · Auto",
|
||||
label: "Cursor Models",
|
||||
usedPercent: 69.5,
|
||||
resetsAt: "2026-09-20T03:53:06.000Z",
|
||||
},
|
||||
{
|
||||
id: "apiPercentUsed",
|
||||
kind: "monthly",
|
||||
label: "Monthly · API",
|
||||
label: "Other Models",
|
||||
usedPercent: 100,
|
||||
resetsAt: "2026-09-20T03:53:06.000Z",
|
||||
},
|
||||
@@ -1019,10 +1019,10 @@ describe("Cursor usage limits", () => {
|
||||
);
|
||||
expect(
|
||||
cursorUsageResponseToLimits({ planUsage: { totalPercentUsed: 0 } }, checkedAt).windows,
|
||||
).toEqual([{ id: "totalPercentUsed", kind: "monthly", label: "Monthly", usedPercent: 0 }]);
|
||||
).toEqual([{ id: "totalPercentUsed", kind: "monthly", label: "Overall", usedPercent: 0 }]);
|
||||
expect(
|
||||
cursorUsageResponseToLimits({ planUsage: { totalPercentUsed: 150 } }, checkedAt).windows,
|
||||
).toEqual([{ id: "totalPercentUsed", kind: "monthly", label: "Monthly", usedPercent: 100 }]);
|
||||
).toEqual([{ id: "totalPercentUsed", kind: "monthly", label: "Overall", usedPercent: 100 }]);
|
||||
});
|
||||
|
||||
it("reads the instance's credentials and endpoint even when usage enabled is false", async () => {
|
||||
@@ -1079,6 +1079,10 @@ describe("Cursor usage limits", () => {
|
||||
AGENT_CLI_CREDENTIAL_STORE: platform === "linux" ? "memory" : "default",
|
||||
...(token ? { CURSOR_AUTH_TOKEN: token } : {}),
|
||||
},
|
||||
false,
|
||||
async () => {
|
||||
throw new Error("must not read Keychain before opt-in");
|
||||
},
|
||||
).pipe(
|
||||
Effect.provideService(HostProcessPlatform, platform),
|
||||
Effect.provideService(
|
||||
@@ -1108,6 +1112,71 @@ describe("Cursor usage limits", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("reads the default macOS Cursor login from Keychain for limits", async () => {
|
||||
const limits = await runNode(
|
||||
readCursorUsageLimits({ apiEndpoint: "" }, {}, true, async () => "keychain-token").pipe(
|
||||
Effect.provideService(HostProcessPlatform, "darwin"),
|
||||
Effect.provideService(
|
||||
FileSystem.FileSystem,
|
||||
FileSystem.makeNoop({
|
||||
readFileString: () => Effect.die("must not read a stale credential file"),
|
||||
}),
|
||||
),
|
||||
Effect.provideService(
|
||||
HttpClient.HttpClient,
|
||||
HttpClient.make((request) => {
|
||||
expect(request.headers.authorization).toBe("Bearer keychain-token");
|
||||
return Effect.succeed(
|
||||
HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({ planUsage: { totalPercentUsed: 42 } }),
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(limits.windows[0]?.usedPercent).toBe(42);
|
||||
});
|
||||
|
||||
it("reports a Keychain initialization failure without failing the provider refresh", async () => {
|
||||
const limits = await runNode(
|
||||
readCursorUsageLimits({ apiEndpoint: "" }, {}, true, async () => {
|
||||
throw new Error("Keychain initialization failed");
|
||||
}).pipe(
|
||||
Effect.provideService(HostProcessPlatform, "darwin"),
|
||||
Effect.provideService(
|
||||
HttpClient.HttpClient,
|
||||
HttpClient.make(() => Effect.die("must not request limits without a login")),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(limits.unavailable?.reason).toBe("probeFailed");
|
||||
});
|
||||
|
||||
it("does not read Keychain or send its token to a custom endpoint", async () => {
|
||||
for (const [apiEndpoint, environment] of [
|
||||
["http://localhost:3000", {}],
|
||||
["", { CURSOR_API_ENDPOINT: "http://localhost:3000" }],
|
||||
["https://cursor-proxy.example", {}],
|
||||
["", { CURSOR_API_ENDPOINT: "https://cursor-proxy.example" }],
|
||||
] as const) {
|
||||
const limits = await runNode(
|
||||
readCursorUsageLimits({ apiEndpoint }, environment, true, async () => {
|
||||
throw new Error("must not read Keychain for a custom endpoint");
|
||||
}).pipe(
|
||||
Effect.provideService(HostProcessPlatform, "darwin"),
|
||||
Effect.provideService(
|
||||
HttpClient.HttpClient,
|
||||
HttpClient.make(() => Effect.die("must not send a Keychain credential to a proxy")),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(limits.unavailable?.reason).toBe("unsupported");
|
||||
expect(limits.unavailable?.message).toContain("default Cursor endpoint");
|
||||
}
|
||||
});
|
||||
|
||||
it("reports failed requests without exposing credentials or response bodies", async () => {
|
||||
const limits = await runNode(
|
||||
readCursorUsageLimits({ apiEndpoint: "" }, { CURSOR_AUTH_TOKEN: "private-token" }).pipe(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import * as NodeOS from "node:os";
|
||||
import type { CursorSettings, ServerProviderUsageWindow } from "@t3tools/contracts";
|
||||
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
|
||||
import { CURSOR_USAGE_WINDOWS } from "@t3tools/shared/usageLimits";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
@@ -13,8 +14,10 @@ import {
|
||||
makeUnavailableUsageLimits,
|
||||
makeUsageLimits,
|
||||
} from "../providerUsageLimits.ts";
|
||||
import { readMacCursorAccessToken } from "../cursorCredentialStore.ts";
|
||||
|
||||
const CursorCredentials = Schema.Struct({ accessToken: Schema.optional(Schema.String) });
|
||||
const DEFAULT_CURSOR_API_ENDPOINT = "https://api2.cursor.sh";
|
||||
const decodeCredentials = Schema.decodeEffect(Schema.fromJsonString(CursorCredentials));
|
||||
const CursorUsageResponse = Schema.Struct({
|
||||
billingCycleEnd: Schema.optional(Schema.Union([Schema.String, Schema.Number])),
|
||||
@@ -39,15 +42,11 @@ export function cursorUsageResponseToLimits(
|
||||
: undefined;
|
||||
const windows: ServerProviderUsageWindow[] = [];
|
||||
if (response.planUsage) {
|
||||
for (const [key, label] of [
|
||||
["totalPercentUsed", "Monthly"],
|
||||
["autoPercentUsed", "Monthly · Auto"],
|
||||
["apiPercentUsed", "Monthly · API"],
|
||||
] as const) {
|
||||
const usedPercent = response.planUsage[key];
|
||||
for (const { id, label } of CURSOR_USAGE_WINDOWS) {
|
||||
const usedPercent = response.planUsage[id];
|
||||
if (usedPercent === undefined || !Number.isFinite(usedPercent)) continue;
|
||||
windows.push({
|
||||
id: key,
|
||||
id,
|
||||
kind: "monthly",
|
||||
label,
|
||||
usedPercent: clampPercent(usedPercent),
|
||||
@@ -63,30 +62,49 @@ export function cursorUsageResponseToLimits(
|
||||
export const readCursorUsageLimits = Effect.fn("readCursorUsageLimits")(function* (
|
||||
settings: Pick<CursorSettings, "apiEndpoint">,
|
||||
environment: NodeJS.ProcessEnv = process.env,
|
||||
allowKeychain = false,
|
||||
keychainToken: () => Promise<string | null> = readMacCursorAccessToken,
|
||||
) {
|
||||
const checkedAt = DateTime.formatIso(yield* DateTime.now);
|
||||
return yield* Effect.gen(function* () {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const platform = yield* HostProcessPlatform;
|
||||
const endpoint = (
|
||||
settings.apiEndpoint.trim() ||
|
||||
environment.CURSOR_API_ENDPOINT?.trim() ||
|
||||
DEFAULT_CURSOR_API_ENDPOINT
|
||||
).replace(/\/$/, "");
|
||||
let token = environment.CURSOR_AUTH_TOKEN?.trim();
|
||||
// An explicit API key can name a different account from the stored login.
|
||||
if (!token && environment.CURSOR_API_KEY?.trim()) {
|
||||
return makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" });
|
||||
}
|
||||
const credentialStore = environment.AGENT_CLI_CREDENTIAL_STORE;
|
||||
if (
|
||||
!token &&
|
||||
(credentialStore === "memory" || (platform === "darwin" && credentialStore !== "file"))
|
||||
) {
|
||||
// Cursor's default macOS login lives in the keychain; a leftover file may be another account.
|
||||
if (!token && credentialStore === "memory") {
|
||||
return makeUnavailableUsageLimits({
|
||||
checkedAt,
|
||||
reason: "unsupported",
|
||||
message: "Cursor usage requires a file-based login or CURSOR_AUTH_TOKEN.",
|
||||
message: "Cursor usage requires a CLI login or CURSOR_AUTH_TOKEN.",
|
||||
});
|
||||
}
|
||||
if (!token) {
|
||||
if (!token && platform === "darwin" && credentialStore !== "file") {
|
||||
if (!allowKeychain) {
|
||||
return makeUnavailableUsageLimits({
|
||||
checkedAt,
|
||||
reason: "unsupported",
|
||||
message: "Enable Cursor account usage in T3 Code to read its Keychain login.",
|
||||
});
|
||||
}
|
||||
if (endpoint !== DEFAULT_CURSOR_API_ENDPOINT) {
|
||||
return makeUnavailableUsageLimits({
|
||||
checkedAt,
|
||||
reason: "unsupported",
|
||||
message: "Cursor account usage requires the default Cursor endpoint when using Keychain.",
|
||||
});
|
||||
}
|
||||
token = (yield* Effect.tryPromise(keychainToken))?.trim();
|
||||
} else if (!token) {
|
||||
const home =
|
||||
(platform === "win32" ? environment.USERPROFILE : environment.HOME) || NodeOS.homedir();
|
||||
const directory =
|
||||
@@ -106,11 +124,6 @@ export const readCursorUsageLimits = Effect.fn("readCursorUsageLimits")(function
|
||||
}
|
||||
if (!token) return makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" });
|
||||
const client = yield* HttpClient.HttpClient;
|
||||
const endpoint = (
|
||||
settings.apiEndpoint.trim() ||
|
||||
environment.CURSOR_API_ENDPOINT?.trim() ||
|
||||
"https://api2.cursor.sh"
|
||||
).replace(/\/$/, "");
|
||||
const response = yield* client.execute(
|
||||
HttpClientRequest.post(`${endpoint}/aiserver.v1.DashboardService/GetCurrentPeriodUsage`).pipe(
|
||||
HttpClientRequest.bearerToken(token),
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import { assert, describe, it } from "@effect/vitest";
|
||||
|
||||
import { makeCachedCursorAccessTokenReader } from "./cursorCredentialStore.ts";
|
||||
|
||||
describe("Cursor Keychain reader", () => {
|
||||
it("shares concurrent reads and rechecks after the cache expires", async () => {
|
||||
let reads = 0;
|
||||
let time = 0;
|
||||
const read = makeCachedCursorAccessTokenReader(
|
||||
async () => {
|
||||
reads++;
|
||||
return `token-${reads}`;
|
||||
},
|
||||
() => time,
|
||||
);
|
||||
assert.deepStrictEqual(await Promise.all([read(), read()]), ["token-1", "token-1"]);
|
||||
assert.strictEqual(await read(), "token-1");
|
||||
assert.strictEqual(reads, 1);
|
||||
time = 5 * 60_000;
|
||||
assert.strictEqual(await read(), "token-2");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
const CACHE_MS = 5 * 60_000;
|
||||
|
||||
/** Share one Keychain request across usage history and limits in this server process. */
|
||||
export function makeCachedCursorAccessTokenReader(
|
||||
read: () => Promise<string | null>,
|
||||
now: () => number = Date.now,
|
||||
): () => Promise<string | null> {
|
||||
let cached: { token: string; until: number } | null = null;
|
||||
let pending: Promise<string | null> | null = null;
|
||||
return () => {
|
||||
if (cached && cached.until > now()) return Promise.resolve(cached.token);
|
||||
if (pending) return pending;
|
||||
pending = read()
|
||||
.then((token) => {
|
||||
cached = token ? { token, until: now() + CACHE_MS } : null;
|
||||
return token;
|
||||
})
|
||||
.finally(() => {
|
||||
pending = null;
|
||||
});
|
||||
return pending;
|
||||
};
|
||||
}
|
||||
|
||||
/** Read the Cursor CLI's default macOS credential without invoking the shared security binary. */
|
||||
export const readMacCursorAccessToken = makeCachedCursorAccessTokenReader(async () => {
|
||||
const { AsyncEntry } = await import("@napi-rs/keyring");
|
||||
return (await new AsyncEntry("cursor-access-token", "cursor-user").getPassword()) ?? null;
|
||||
});
|
||||
@@ -3,10 +3,11 @@
|
||||
import * as NodeFSP from "node:fs/promises";
|
||||
import * as NodeOS from "node:os";
|
||||
import * as NodePath from "node:path";
|
||||
import * as NodeSqlite from "node:sqlite";
|
||||
|
||||
import { assert, describe, it } from "@effect/vitest";
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
|
||||
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
|
||||
import { mergeUsage } from "@t3tools/shared/usageMerge";
|
||||
import {
|
||||
EnvironmentId,
|
||||
@@ -31,6 +32,7 @@ import * as ServerConfig from "../config.ts";
|
||||
import * as ServerSettings from "../serverSettings.ts";
|
||||
import * as UsageService from "./UsageService.ts";
|
||||
|
||||
const encodeUnknownJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
|
||||
const encodeUnknownJsonString = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
|
||||
|
||||
function claudeLine(id: number, outputTokens: number, model = "claude-fable-5"): string {
|
||||
@@ -82,9 +84,11 @@ const serviceLayers = (input: {
|
||||
/** Defaults to an unparsable document so every scan retries the fetch. */
|
||||
readonly ratesDocument?: unknown;
|
||||
readonly environment?: NodeJS.ProcessEnv;
|
||||
readonly platform?: NodeJS.Platform;
|
||||
}) =>
|
||||
ServerConfig.layerTest(process.cwd(), { prefix: input.prefix }).pipe(
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
Layer.provideMerge(Layer.succeed(HostProcessPlatform, input.platform ?? "linux")),
|
||||
Layer.provideMerge(ServerSettings.layerTest(input.settings)),
|
||||
Layer.provideMerge(
|
||||
Layer.succeed(
|
||||
@@ -101,7 +105,12 @@ const serviceLayers = (input: {
|
||||
),
|
||||
Layer.provideMerge(
|
||||
Layer.succeed(HostProcessEnvironment, {
|
||||
HOME: input.home,
|
||||
GROK_HOME: NodePath.join(input.home, "grok"),
|
||||
OPENCODE_DATA_DIR: NodePath.join(input.home, "opencode"),
|
||||
ANTIGRAVITY_DATA_DIR: NodePath.join(input.home, "antigravity"),
|
||||
XDG_CONFIG_HOME: NodePath.join(input.home, "config"),
|
||||
APPDATA: NodePath.join(input.home, "config"),
|
||||
...input.environment,
|
||||
}),
|
||||
),
|
||||
@@ -112,6 +121,193 @@ function totalOutputTokens(summary: { buckets: readonly { totals: { outputTokens
|
||||
}
|
||||
|
||||
describe("UsageService", () => {
|
||||
it.live("does not read the macOS Cursor Keychain before account usage is enabled", () =>
|
||||
Effect.gen(function* () {
|
||||
const { settings, home } = yield* setup;
|
||||
const service = yield* UsageService.make.pipe(
|
||||
Effect.provide(
|
||||
serviceLayers({
|
||||
prefix: "usage-service-cursor-keychain-disabled",
|
||||
home,
|
||||
settings,
|
||||
platform: "darwin",
|
||||
environment: {},
|
||||
}),
|
||||
),
|
||||
);
|
||||
const summary = yield* service.readSummary(WINDOW);
|
||||
const cursor = summary.sources.find((source) => source.fingerprint.provider === "cursor");
|
||||
assert.strictEqual(cursor?.status, "missing");
|
||||
assert.strictEqual(cursor?.action, "enableCursorKeychain");
|
||||
}).pipe(Effect.scoped),
|
||||
);
|
||||
|
||||
it.live("ignores stale Cursor file logins when the active credential store differs", () =>
|
||||
Effect.gen(function* () {
|
||||
const { settings, home } = yield* setup;
|
||||
for (const [index, testCase] of [
|
||||
{
|
||||
platform: "darwin" as const,
|
||||
environment: { AGENT_CLI_CREDENTIAL_STORE: "memory" },
|
||||
authPath: [".cursor", "auth.json"],
|
||||
},
|
||||
{
|
||||
platform: "linux" as const,
|
||||
environment: { AGENT_CLI_CREDENTIAL_STORE: "memory" },
|
||||
authPath: ["config", "cursor", "auth.json"],
|
||||
},
|
||||
{
|
||||
platform: "linux" as const,
|
||||
environment: { CURSOR_API_KEY: "different-account" },
|
||||
authPath: ["config", "cursor", "auth.json"],
|
||||
},
|
||||
].entries()) {
|
||||
const authPath = NodePath.join(home, ...testCase.authPath);
|
||||
yield* Effect.promise(async () => {
|
||||
await NodeFSP.mkdir(NodePath.dirname(authPath), { recursive: true });
|
||||
await NodeFSP.writeFile(
|
||||
authPath,
|
||||
encodeUnknownJsonString({ accessToken: "stale-token" }),
|
||||
);
|
||||
});
|
||||
const service = yield* UsageService.make.pipe(
|
||||
Effect.provide(
|
||||
serviceLayers({
|
||||
prefix: `usage-service-cursor-store-${index}`,
|
||||
home,
|
||||
settings,
|
||||
platform: testCase.platform,
|
||||
environment: testCase.environment,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const summary = yield* service.readSummary(WINDOW);
|
||||
const cursor = summary.sources.find((source) => source.fingerprint.provider === "cursor");
|
||||
assert.strictEqual(cursor?.status, "missing");
|
||||
assert.include(cursor?.message ?? "", "Cursor CLI login");
|
||||
assert.isFalse(summary.buckets.some((bucket) => bucket.provider === "cursor"));
|
||||
}
|
||||
}).pipe(Effect.scoped),
|
||||
);
|
||||
|
||||
it.live(
|
||||
"includes OpenCode history but does not substitute desktop usage for an unavailable Cursor account",
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
const { settings, home } = yield* setup;
|
||||
const root = NodePath.join(home, "opencode");
|
||||
const message = yield* encodeUnknownJson({
|
||||
id: "msg_1",
|
||||
sessionID: "session-1",
|
||||
role: "assistant",
|
||||
modelID: "example-model",
|
||||
time: { created: Date.parse("2026-08-01T10:00:00Z") },
|
||||
tokens: { input: 10, output: 5, reasoning: 2, cache: { read: 20, write: 3 } },
|
||||
});
|
||||
const bubble = yield* encodeUnknownJson({
|
||||
type: 2,
|
||||
createdAt: "2026-08-01T10:00:00Z",
|
||||
modelInfo: { modelName: "example-model" },
|
||||
tokenCount: { inputTokens: 100, outputTokens: 20 },
|
||||
});
|
||||
yield* Effect.promise(async () => {
|
||||
const directory = NodePath.join(root, "storage", "message", "session-1");
|
||||
await NodeFSP.mkdir(directory, { recursive: true });
|
||||
await NodeFSP.writeFile(NodePath.join(directory, "msg_1.json"), message);
|
||||
const desktop = NodePath.join(home, "config", "Cursor", "User", "globalStorage");
|
||||
await NodeFSP.mkdir(desktop, { recursive: true });
|
||||
const db = new NodeSqlite.DatabaseSync(NodePath.join(desktop, "state.vscdb"));
|
||||
try {
|
||||
db.exec("CREATE TABLE cursorDiskKV (key TEXT, value TEXT)");
|
||||
db.prepare("INSERT INTO cursorDiskKV VALUES (?, ?)").run(
|
||||
"bubbleId:session:assistant",
|
||||
bubble,
|
||||
);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
});
|
||||
const service = yield* UsageService.make.pipe(
|
||||
Effect.provide(serviceLayers({ prefix: "usage-service-opencode", home, settings })),
|
||||
);
|
||||
const summary = yield* service.readSummary(WINDOW);
|
||||
assert.strictEqual(summary.buckets[0]?.provider, "opencode");
|
||||
assert.isFalse(summary.buckets.some((bucket) => bucket.provider === "cursor"));
|
||||
assert.strictEqual(
|
||||
summary.sources.find((source) => source.fingerprint.provider === "cursor")?.status,
|
||||
"missing",
|
||||
);
|
||||
assert.strictEqual(
|
||||
summary.buckets[0]?.sourcePath,
|
||||
yield* Effect.promise(() => NodeFSP.realpath(root)),
|
||||
);
|
||||
assert.strictEqual(summary.buckets[0]?.totals.outputTokens, 7);
|
||||
assert.strictEqual(
|
||||
summary.sources.find((source) => source.fingerprint.provider === "opencode")
|
||||
?.distinctSessions,
|
||||
1,
|
||||
);
|
||||
assert.include(
|
||||
summary.sources.find((source) => source.fingerprint.provider === "cursor")?.message ?? "",
|
||||
"Cursor account history needs a Cursor CLI login",
|
||||
);
|
||||
}).pipe(Effect.scoped),
|
||||
);
|
||||
|
||||
it.live("counts aliased OpenCode and Antigravity directories once", () =>
|
||||
Effect.gen(function* () {
|
||||
const { settings, home } = yield* setup;
|
||||
const opencode = NodePath.join(home, "opencode-store");
|
||||
const opencodeAlias = NodePath.join(home, "opencode-alias");
|
||||
const conversations = NodePath.join(home, "antigravity-conversations");
|
||||
const antigravityA = NodePath.join(home, "antigravity-a");
|
||||
const antigravityB = NodePath.join(home, "antigravity-b");
|
||||
yield* Effect.promise(async () => {
|
||||
await NodeFSP.mkdir(opencode);
|
||||
await NodeFSP.symlink(opencode, opencodeAlias, "junction");
|
||||
await NodeFSP.mkdir(conversations);
|
||||
await NodeFSP.mkdir(antigravityA);
|
||||
await NodeFSP.mkdir(antigravityB);
|
||||
await NodeFSP.symlink(
|
||||
conversations,
|
||||
NodePath.join(antigravityA, "conversations"),
|
||||
"junction",
|
||||
);
|
||||
await NodeFSP.symlink(
|
||||
conversations,
|
||||
NodePath.join(antigravityB, "conversations"),
|
||||
"junction",
|
||||
);
|
||||
});
|
||||
const service = yield* UsageService.make.pipe(
|
||||
Effect.provide(
|
||||
serviceLayers({
|
||||
prefix: "usage-service-aliased-roots-test",
|
||||
home,
|
||||
settings,
|
||||
environment: {
|
||||
OPENCODE_DATA_DIR: `${opencode},${opencodeAlias}`,
|
||||
ANTIGRAVITY_DATA_DIR: `${antigravityA},${antigravityB}`,
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
const summary = yield* service.readSummary(WINDOW);
|
||||
const sourcesFor = (provider: "opencode" | "antigravity") =>
|
||||
summary.sources.filter((source) => source.fingerprint.provider === provider);
|
||||
assert.strictEqual(sourcesFor("opencode").length, 1);
|
||||
assert.strictEqual(sourcesFor("antigravity").length, 1);
|
||||
assert.strictEqual(
|
||||
sourcesFor("opencode")[0]?.fingerprint.resolvedHomePath,
|
||||
yield* Effect.promise(() => NodeFSP.realpath(opencode)),
|
||||
);
|
||||
assert.strictEqual(
|
||||
sourcesFor("antigravity")[0]?.fingerprint.resolvedHomePath,
|
||||
yield* Effect.promise(() => NodeFSP.realpath(conversations)),
|
||||
);
|
||||
}).pipe(Effect.scoped),
|
||||
);
|
||||
|
||||
it.live("reads configured and disabled accounts once across shared and aliased homes", () =>
|
||||
Effect.gen(function* () {
|
||||
const { transcript, settings, home } = yield* setup;
|
||||
@@ -258,9 +454,12 @@ describe("UsageService", () => {
|
||||
const service = yield* UsageService.make;
|
||||
const first = yield* service.readSummary(WINDOW);
|
||||
assert.strictEqual(totalOutputTokens(first), 7);
|
||||
const configuredProjects = yield* Effect.promise(() =>
|
||||
NodeFSP.realpath(NodePath.join(configured, "projects")),
|
||||
);
|
||||
assert.include(
|
||||
first.sources.map((source) => source.fingerprint.resolvedHomePath),
|
||||
NodePath.join(configured, "projects"),
|
||||
configuredProjects,
|
||||
);
|
||||
yield* settingsService.updateSettings({
|
||||
providerInstances: {
|
||||
@@ -275,9 +474,12 @@ describe("UsageService", () => {
|
||||
});
|
||||
const second = yield* service.readSummary(WINDOW);
|
||||
assert.strictEqual(totalOutputTokens(second), 8);
|
||||
const environmentProjects = yield* Effect.promise(() =>
|
||||
NodeFSP.realpath(NodePath.join(environmentHome, "projects")),
|
||||
);
|
||||
assert.include(
|
||||
second.sources.map((source) => source.fingerprint.resolvedHomePath),
|
||||
NodePath.join(environmentHome, "projects"),
|
||||
environmentProjects,
|
||||
);
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
@@ -505,6 +707,9 @@ describe("UsageService", () => {
|
||||
Effect.gen(function* () {
|
||||
const { transcript, settings, home } = yield* setup;
|
||||
yield* Effect.promise(() => NodeFSP.writeFile(transcript, claudeLine(1, 5, "example-model")));
|
||||
const transcriptDir = yield* Effect.promise(() =>
|
||||
NodeFSP.realpath(NodePath.join(home, "claude", "projects")),
|
||||
);
|
||||
|
||||
yield* Effect.gen(function* () {
|
||||
const settingsService = yield* ServerSettings.ServerSettingsService;
|
||||
@@ -519,7 +724,7 @@ describe("UsageService", () => {
|
||||
exists: (path) =>
|
||||
fileSystem.exists(path).pipe(
|
||||
Effect.tap(() => {
|
||||
if (path !== NodePath.join(home, "claude", "projects")) return Effect.void;
|
||||
if (path !== transcriptDir) return Effect.void;
|
||||
homeProbes += 1;
|
||||
return Deferred.succeed(
|
||||
homeProbes === 1 ? firstScanStarted : secondScanStarted,
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
/**
|
||||
* UsageService - scans provider transcripts and returns priced usage buckets.
|
||||
*
|
||||
* The scan reads the provider CLIs' own session files (Claude Code, Codex, and
|
||||
* Grok Build) rather than T3 Code's orchestration projections, so usage covers
|
||||
* turns driven outside T3 Code too. This is the approach `ccusage` takes.
|
||||
* The scan reads native session files and databases, including work driven
|
||||
* outside T3 Code. Cursor's local records provide only partial coverage.
|
||||
*
|
||||
* Transcripts are append-only, so parsed records are memoised per file by
|
||||
* JSONL transcripts are append-only, so parsed records are memoised per file by
|
||||
* `(size, mtime)`. A cold 30-day scan of ~1.4 GB lands around 2-3 seconds; warm
|
||||
* scans only reparse files that changed, and a file that merely grew resumes
|
||||
* from its cached parse position so only the appended bytes are read.
|
||||
* SQLite readers query live databases each scan so WAL writes remain visible.
|
||||
*
|
||||
* @module UsageService
|
||||
*/
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
CodexSettings,
|
||||
type ProviderInstanceConfig,
|
||||
USAGE_CONTRACT_VERSION,
|
||||
ProviderInstanceId,
|
||||
type ServerSettings as ServerSettingsValue,
|
||||
type UsageProviderKind,
|
||||
type UsageSource,
|
||||
@@ -27,10 +28,11 @@ import {
|
||||
type UsageSummaryInput,
|
||||
UsageReadError,
|
||||
} from "@t3tools/contracts";
|
||||
import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
|
||||
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
|
||||
import * as Cause from "effect/Cause";
|
||||
import * as Clock from "effect/Clock";
|
||||
import * as Context from "effect/Context";
|
||||
import * as Crypto from "effect/Crypto";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Deferred from "effect/Deferred";
|
||||
import * as Effect from "effect/Effect";
|
||||
@@ -46,7 +48,11 @@ import { ServerConfig } from "../config.ts";
|
||||
import { expandHomePath } from "../pathExpansion.ts";
|
||||
import * as ServerSettings from "../serverSettings.ts";
|
||||
import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts";
|
||||
import { resolveAntigravityInstanceDirectories } from "../provider/antigravityAuthSupport.ts";
|
||||
import { mergeProviderInstanceEnvironment } from "../provider/ProviderInstanceEnvironment.ts";
|
||||
import { readOpenCodeUsage } from "./opencodeUsageReader.ts";
|
||||
import { readAntigravityUsage } from "./antigravityUsageReader.ts";
|
||||
import { readCursorAccountUsage } from "./cursorUsageReader.ts";
|
||||
import { UsageAggregator } from "./usageAggregation.ts";
|
||||
import { createOverrideRateTable, parseRateTable, type RateTable } from "./usagePricing.ts";
|
||||
import {
|
||||
@@ -144,12 +150,14 @@ export const layerTest = Layer.succeed(
|
||||
);
|
||||
|
||||
export const make = Effect.gen(function* () {
|
||||
const crypto = yield* Crypto.Crypto;
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const config = yield* ServerConfig;
|
||||
const settingsService = yield* ServerSettings.ServerSettingsService;
|
||||
const httpClient = yield* HttpClient.HttpClient;
|
||||
const hostEnvironment = yield* HostProcessEnvironment;
|
||||
const platform = yield* HostProcessPlatform;
|
||||
|
||||
const fileCache: ScanCache = new Map();
|
||||
const sourceCache = new Map<string, typeof CachedSource.Type>();
|
||||
@@ -446,6 +454,10 @@ export const make = Effect.gen(function* () {
|
||||
readonly provider: UsageProviderKind;
|
||||
readonly dir: string;
|
||||
readonly volumeId: string;
|
||||
readonly hostId?: string;
|
||||
readonly status?: UsageSource["status"];
|
||||
readonly message?: string;
|
||||
readonly action?: UsageSource["action"];
|
||||
/** Parsed records per file, or `null` when the directory does not exist. */
|
||||
readonly files:
|
||||
| readonly { readonly path: string; readonly records: readonly UsageRecord[] }[]
|
||||
@@ -481,6 +493,171 @@ export const make = Effect.gen(function* () {
|
||||
}
|
||||
scanned.push({ provider, dir, volumeId, files: parsedFiles });
|
||||
}
|
||||
|
||||
const home = NodeOS.homedir();
|
||||
const envRoots = Effect.fnUntraced(function* (key: string, defaults: readonly string[]) {
|
||||
const roots = hostEnvironment[key]
|
||||
?.split(",")
|
||||
.map((value) => value.trim())
|
||||
.filter(Boolean);
|
||||
const canonical = new Set<string>();
|
||||
for (const root of roots?.length ? roots : defaults) {
|
||||
const resolved = path.resolve(expandHomePath(root));
|
||||
canonical.add(
|
||||
yield* fileSystem.realPath(resolved).pipe(Effect.orElseSucceed(() => resolved)),
|
||||
);
|
||||
}
|
||||
return [...canonical];
|
||||
});
|
||||
const dataHome = hostEnvironment["XDG_DATA_HOME"]?.trim();
|
||||
for (const dir of yield* envRoots("OPENCODE_DATA_DIR", [
|
||||
path.join(
|
||||
dataHome && path.isAbsolute(dataHome) ? dataHome : path.join(home, ".local", "share"),
|
||||
"opencode",
|
||||
),
|
||||
])) {
|
||||
const result = yield* Effect.promise(() => readOpenCodeUsage(dir, windowStartMs));
|
||||
scanned.push({
|
||||
provider: "opencode",
|
||||
dir,
|
||||
volumeId: yield* Effect.promise(() => readDirectoryVolumeId(dir)),
|
||||
files: result.missing && !result.error ? null : result.files,
|
||||
status: result.error ? "partial" : "ok",
|
||||
...(result.error ? { message: "Some OpenCode history could not be read." } : {}),
|
||||
});
|
||||
}
|
||||
const antigravityRoots = yield* envRoots("ANTIGRAVITY_DATA_DIR", [
|
||||
...["antigravity", "antigravity-cli", "antigravity-ide", "antigravity-backup"].map((name) =>
|
||||
path.join(home, ".gemini", name),
|
||||
),
|
||||
path.join(home, ".config", "antigravity"),
|
||||
]);
|
||||
for (const [instanceId, instance] of Object.entries(settings.providerInstances)) {
|
||||
if (instance.driver === "antigravity") {
|
||||
const directories = yield* resolveAntigravityInstanceDirectories(
|
||||
config.stateDir,
|
||||
ProviderInstanceId.make(instanceId),
|
||||
).pipe(
|
||||
Effect.provideService(Crypto.Crypto, crypto),
|
||||
Effect.provideService(Path.Path, path),
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new UsageReadError({
|
||||
reason: "scanFailed",
|
||||
detail: "Antigravity profile directory could not be resolved.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
antigravityRoots.push(path.join(directories.profile, "antigravity-acp"));
|
||||
}
|
||||
}
|
||||
const antigravityDirs = new Set<string>();
|
||||
for (const root of antigravityRoots) {
|
||||
const resolvedRoot = yield* fileSystem.realPath(root).pipe(Effect.orElseSucceed(() => root));
|
||||
const nested = path.join(resolvedRoot, "conversations");
|
||||
const dir = (yield* fileSystem
|
||||
.exists(nested)
|
||||
.pipe(Effect.catchCause(() => Effect.succeed(false))))
|
||||
? nested
|
||||
: resolvedRoot;
|
||||
antigravityDirs.add(yield* fileSystem.realPath(dir).pipe(Effect.orElseSucceed(() => dir)));
|
||||
}
|
||||
const antigravity = yield* Effect.promise(() =>
|
||||
readAntigravityUsage([...antigravityDirs], windowStartMs),
|
||||
);
|
||||
for (const dir of antigravityDirs) {
|
||||
const exists = yield* fileSystem
|
||||
.exists(dir)
|
||||
.pipe(Effect.catchCause(() => Effect.succeed(false)));
|
||||
const failed = antigravity.errors.some(
|
||||
(error) => error === dir || error.startsWith(`${dir}${path.sep}`),
|
||||
);
|
||||
scanned.push({
|
||||
provider: "antigravity",
|
||||
dir,
|
||||
volumeId: yield* Effect.promise(() => readDirectoryVolumeId(dir)),
|
||||
files: !exists && !failed ? null : antigravity.files.filter((file) => file.root === dir),
|
||||
status: failed ? "partial" : "ok",
|
||||
...(failed ? { message: "Some Antigravity history could not be read." } : {}),
|
||||
});
|
||||
}
|
||||
const cursorUserHome =
|
||||
(platform === "win32" ? hostEnvironment["USERPROFILE"] : hostEnvironment["HOME"]) || home;
|
||||
const configHome = hostEnvironment["XDG_CONFIG_HOME"]?.trim();
|
||||
const cursorHome =
|
||||
platform === "darwin"
|
||||
? path.join(cursorUserHome, "Library", "Application Support")
|
||||
: platform === "win32"
|
||||
? hostEnvironment["APPDATA"] || path.join(cursorUserHome, "AppData", "Roaming")
|
||||
: configHome && path.isAbsolute(configHome)
|
||||
? configHome
|
||||
: path.join(cursorUserHome, ".config");
|
||||
const cursorAuthPath =
|
||||
platform === "darwin"
|
||||
? path.join(cursorUserHome, ".cursor", "auth.json")
|
||||
: path.join(cursorHome, platform === "win32" ? "Cursor" : "cursor", "auth.json");
|
||||
const credentialStore = hostEnvironment["AGENT_CLI_CREDENTIAL_STORE"];
|
||||
const loginUnavailable =
|
||||
Boolean(hostEnvironment["CURSOR_AUTH_TOKEN"]?.trim()) ||
|
||||
Boolean(hostEnvironment["CURSOR_API_KEY"]?.trim()) ||
|
||||
credentialStore === "memory";
|
||||
if (
|
||||
platform === "darwin" &&
|
||||
credentialStore !== "file" &&
|
||||
!loginUnavailable &&
|
||||
!settings.cursorKeychainUsageEnabled
|
||||
) {
|
||||
scanned.push({
|
||||
provider: "cursor",
|
||||
dir: cursorAuthPath,
|
||||
volumeId: "",
|
||||
files: null,
|
||||
message: "Cursor account usage is off on this environment.",
|
||||
action: "enableCursorKeychain",
|
||||
});
|
||||
return scanned;
|
||||
}
|
||||
const cursorUntilMs = yield* Clock.currentTimeMillis;
|
||||
const account = loginUnavailable
|
||||
? {
|
||||
accountKey: null,
|
||||
records: [],
|
||||
missing: true,
|
||||
error: "Cursor account history needs a Cursor CLI login on this server.",
|
||||
}
|
||||
: yield* Effect.promise(() =>
|
||||
readCursorAccountUsage(
|
||||
platform === "darwin" && credentialStore !== "file"
|
||||
? { kind: "keychain" }
|
||||
: cursorAuthPath,
|
||||
windowStartMs,
|
||||
cursorUntilMs,
|
||||
),
|
||||
);
|
||||
if (account.accountKey !== null && account.error === null && !account.missing) {
|
||||
// The same account includes CLI and desktop history from every machine.
|
||||
// A stable remote fingerprint prevents connected environments counting it twice.
|
||||
const source = `cursor-account:${account.accountKey}`;
|
||||
scanned.push({
|
||||
provider: "cursor",
|
||||
dir: source,
|
||||
hostId: "cursor.com",
|
||||
volumeId: account.accountKey,
|
||||
files: [{ path: source, records: account.records }],
|
||||
status: "ok",
|
||||
});
|
||||
return scanned;
|
||||
}
|
||||
scanned.push({
|
||||
provider: "cursor",
|
||||
dir: cursorAuthPath,
|
||||
volumeId: yield* Effect.promise(() => readDirectoryVolumeId(cursorAuthPath)),
|
||||
// Never combine a local fallback with another server's account-wide history.
|
||||
files: null,
|
||||
message:
|
||||
account.error ?? "Cursor account history needs a Cursor CLI login saved on this server.",
|
||||
});
|
||||
return scanned;
|
||||
});
|
||||
|
||||
@@ -555,7 +732,16 @@ export const make = Effect.gen(function* () {
|
||||
|
||||
const sources: UsageSource[] = [];
|
||||
|
||||
for (const { provider, dir, volumeId, files } of scannedDirs) {
|
||||
for (const {
|
||||
provider,
|
||||
dir,
|
||||
volumeId,
|
||||
files,
|
||||
status,
|
||||
message,
|
||||
action,
|
||||
hostId: sourceHostId,
|
||||
} of scannedDirs) {
|
||||
const retainedFiles = [...(files ?? [])];
|
||||
const livePaths = new Set(retainedFiles.map((file) => file.path));
|
||||
// Cleanup may remove transcripts, but the usage we already saved still
|
||||
@@ -601,21 +787,23 @@ export const make = Effect.gen(function* () {
|
||||
}
|
||||
// Only sessions contributing in-window count; the mtime slack can
|
||||
// admit boundary files whose records fall outside the range.
|
||||
if (aggregator.add(usageRecord) && record.sessionId.length > 0) {
|
||||
if (aggregator.add(usageRecord, dir) && record.sessionId.length > 0) {
|
||||
sessionIds.add(record.sessionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sources.push({
|
||||
fingerprint: { hostId, provider, resolvedHomePath: dir, volumeId },
|
||||
fingerprint: { hostId: sourceHostId ?? hostId, provider, resolvedHomePath: dir, volumeId },
|
||||
// Clients exclude missing sources, so saved records remain an available source.
|
||||
status: files === null && scannedFiles === 0 ? "missing" : "ok",
|
||||
status: files === null && scannedFiles === 0 ? "missing" : (status ?? "ok"),
|
||||
scannedFiles,
|
||||
skippedFiles,
|
||||
malformedRecords: 0,
|
||||
distinctSessions: sessionIds.size,
|
||||
message: files === null ? "No transcript directory on this environment." : null,
|
||||
message:
|
||||
message ?? (files === null ? "No transcript directory on this environment." : null),
|
||||
...(action ? { action } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -650,6 +838,7 @@ export const make = Effect.gen(function* () {
|
||||
const scanKey = (
|
||||
input: UsageSummaryInput,
|
||||
priceOverrides: ServerSettingsValue["usagePriceOverrides"],
|
||||
cursorKeychainUsageEnabled: boolean,
|
||||
): string =>
|
||||
JSON.stringify([
|
||||
input.timeZone,
|
||||
@@ -659,11 +848,12 @@ export const make = Effect.gen(function* () {
|
||||
input.sinceTime ?? null,
|
||||
input.untilTime ?? null,
|
||||
priceOverrides,
|
||||
cursorKeychainUsageEnabled,
|
||||
]);
|
||||
|
||||
const readSummary = Effect.fn("UsageService.readSummary")(function* (input: UsageSummaryInput) {
|
||||
const settings = yield* readSettings;
|
||||
const key = scanKey(input, settings.usagePriceOverrides);
|
||||
const key = scanKey(input, settings.usagePriceOverrides, settings.cursorKeychainUsageEnabled);
|
||||
const deferred = yield* Effect.uninterruptible(
|
||||
Effect.gen(function* () {
|
||||
const existing = inflightScans.get(key);
|
||||
|
||||
@@ -0,0 +1,374 @@
|
||||
// node:sqlite reads live conversation databases while Node fs discovers them.
|
||||
// @effect-diagnostics nodeBuiltinImport:off
|
||||
import * as NodeFSP from "node:fs/promises";
|
||||
import * as NodePath from "node:path";
|
||||
import * as NodeSqlite from "node:sqlite";
|
||||
import * as NodeTimersPromises from "node:timers/promises";
|
||||
|
||||
import type { UsageRecord } from "./usageTranscripts.ts";
|
||||
|
||||
type FieldValue = number | bigint | Uint8Array;
|
||||
type Fields = Map<number, FieldValue[]>;
|
||||
|
||||
/** Antigravity stores usage metadata as protobuf, independently of conversation text. */
|
||||
function fields(bytes: Uint8Array): Fields {
|
||||
let offset = 0;
|
||||
const result: Fields = new Map();
|
||||
const varint = () => {
|
||||
let value = 0n;
|
||||
for (let shift = 0n; shift < 70n; shift += 7n) {
|
||||
const byte = bytes[offset++];
|
||||
if (byte === undefined || (shift === 63n && byte > 1)) {
|
||||
throw new Error("Invalid Antigravity protobuf varint");
|
||||
}
|
||||
value |= BigInt(byte & 127) << shift;
|
||||
if (byte < 128) {
|
||||
return value > BigInt(Number.MAX_SAFE_INTEGER) ? value : Number(value);
|
||||
}
|
||||
}
|
||||
throw new Error("Invalid Antigravity protobuf varint");
|
||||
};
|
||||
while (offset < bytes.length) {
|
||||
const tag = varint();
|
||||
if (typeof tag !== "number") throw new Error("Invalid protobuf field");
|
||||
const number = Math.floor(tag / 8);
|
||||
const wire = tag % 8;
|
||||
if (number === 0) throw new Error("Invalid protobuf field");
|
||||
let value: FieldValue;
|
||||
if (wire === 0) {
|
||||
value = varint();
|
||||
} else if (wire === 1 || wire === 5 || wire === 2) {
|
||||
const length = wire === 2 ? varint() : wire === 1 ? 8 : 4;
|
||||
if (typeof length !== "number") throw new Error("Invalid protobuf field length");
|
||||
if (length > bytes.length - offset) throw new Error("Truncated protobuf field");
|
||||
value = bytes.subarray(offset, offset + length);
|
||||
offset += length;
|
||||
if (wire !== 2) continue;
|
||||
} else {
|
||||
throw new Error("Unsupported protobuf wire type");
|
||||
}
|
||||
const entries = result.get(number) ?? [];
|
||||
entries.push(value);
|
||||
result.set(number, entries);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
const numberAt = (value: Fields, key: number) => {
|
||||
const entry = value.get(key)?.[0];
|
||||
return typeof entry === "number" ? entry : 0;
|
||||
};
|
||||
const bytesAt = (value: Fields, key: number) => {
|
||||
const entry = value.get(key)?.[0];
|
||||
return entry instanceof Uint8Array ? entry : undefined;
|
||||
};
|
||||
const nested = (value: Fields, key: number) => {
|
||||
const bytes = bytesAt(value, key);
|
||||
return bytes === undefined ? new Map<number, FieldValue[]>() : fields(bytes);
|
||||
};
|
||||
const textAt = (value: Fields, key: number) => {
|
||||
const bytes = bytesAt(value, key);
|
||||
return bytes === undefined ? "" : new TextDecoder("utf-8", { fatal: true }).decode(bytes).trim();
|
||||
};
|
||||
const timestamp = (value: Fields) => {
|
||||
const seconds = numberAt(value, 1);
|
||||
return seconds > 0 ? seconds * 1000 + Math.floor(numberAt(value, 2) / 1_000_000) : null;
|
||||
};
|
||||
|
||||
const MODEL_IDS: Record<number, string> = {
|
||||
246: "gemini-2.5-pro",
|
||||
312: "gemini-2.5-flash",
|
||||
313: "gemini-2.5-flash-thinking",
|
||||
329: "gemini-2.5-flash-thinking",
|
||||
330: "gemini-2.5-flash-lite",
|
||||
281: "claude-sonnet-4",
|
||||
282: "claude-sonnet-4",
|
||||
290: "claude-opus-4",
|
||||
291: "claude-opus-4",
|
||||
333: "claude-sonnet-4-5",
|
||||
334: "claude-sonnet-4-5",
|
||||
340: "claude-haiku-4-5",
|
||||
341: "claude-haiku-4-5",
|
||||
1026: "claude-opus-4-6",
|
||||
1035: "claude-sonnet-4-6",
|
||||
1016: "gemini-3.1-pro",
|
||||
1036: "gemini-3.1-pro",
|
||||
1037: "gemini-3.1-pro",
|
||||
1018: "gemini-3-flash-preview",
|
||||
1084: "gemini-3-flash-preview",
|
||||
1047: "gemini-3-flash-preview",
|
||||
};
|
||||
|
||||
function modelName(name: string, id: number): string {
|
||||
if (name) {
|
||||
const normalized = name
|
||||
.toLowerCase()
|
||||
.replace(/\s*\([^)]*\)\s*$/, "")
|
||||
.replaceAll(" ", "-");
|
||||
if (normalized.startsWith("claude-")) {
|
||||
return normalized
|
||||
.replace(/^claude-(4(?:\.\d+)?)-(sonnet|opus|haiku)/, "claude-$2-$1")
|
||||
.replaceAll(".", "-");
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
return MODEL_IDS[id] ?? (id > 0 ? `antigravity-model-${id}` : "");
|
||||
}
|
||||
|
||||
interface Metadata {
|
||||
model: string;
|
||||
timestampMs: number | null;
|
||||
usages: Fields[];
|
||||
}
|
||||
|
||||
function metadata(bytes: Uint8Array, step: boolean): Metadata {
|
||||
const root = fields(bytes);
|
||||
if (!step && bytesAt(root, 1) === undefined) {
|
||||
throw new Error("Missing Antigravity generation metadata");
|
||||
}
|
||||
const data = step ? root : nested(root, 1);
|
||||
const model = step ? nested(data, 24) : data;
|
||||
const usage = bytesAt(data, step ? 9 : 4);
|
||||
const usages = usage === undefined ? [] : [fields(usage)];
|
||||
for (const retry of data.get(step ? 28 : 17) ?? []) {
|
||||
if (!(retry instanceof Uint8Array)) throw new Error("Invalid retry metadata");
|
||||
const retryUsage = bytesAt(fields(retry), 2);
|
||||
if (retryUsage !== undefined) usages.push(fields(retryUsage));
|
||||
}
|
||||
return {
|
||||
model: modelName(
|
||||
textAt(model, step ? 12 : 19) || textAt(model, step ? 8 : 21),
|
||||
numberAt(model, step ? 1 : 3),
|
||||
),
|
||||
timestampMs: step
|
||||
? (timestamp(nested(data, 8)) ?? timestamp(nested(data, 1)))
|
||||
: timestamp(nested(nested(data, 9), 4)),
|
||||
usages,
|
||||
};
|
||||
}
|
||||
|
||||
function blob(value: unknown): Uint8Array {
|
||||
if (!(value instanceof Uint8Array)) throw new Error("Invalid Antigravity metadata blob");
|
||||
return value;
|
||||
}
|
||||
|
||||
interface UsageCandidate {
|
||||
record: UsageRecord;
|
||||
keys: readonly string[];
|
||||
timestampQuality: number;
|
||||
}
|
||||
|
||||
async function readDatabase(path: string, fallbackTimestamp: number): Promise<UsageCandidate[]> {
|
||||
const db = new NodeSqlite.DatabaseSync(path, { readOnly: true });
|
||||
try {
|
||||
db.exec("PRAGMA busy_timeout = 100; BEGIN");
|
||||
const tables = new Set(
|
||||
db
|
||||
.prepare("SELECT name FROM sqlite_master WHERE type = 'table'")
|
||||
.all()
|
||||
.map((row) => row.name),
|
||||
);
|
||||
if (!tables.has("gen_metadata") && !tables.has("steps")) {
|
||||
throw new Error("Missing Antigravity usage tables");
|
||||
}
|
||||
const readMetadata = async (query: string, column: string, step: boolean) => {
|
||||
const entries: Array<{ idx: number; entry: Metadata }> = [];
|
||||
for (const row of db.prepare(query).iterate()) {
|
||||
if (typeof row.idx !== "number") throw new Error("Invalid Antigravity metadata index");
|
||||
entries.push({ idx: row.idx, entry: metadata(blob(row[column]), step) });
|
||||
if (entries.length % 256 === 0) await NodeTimersPromises.setImmediate();
|
||||
}
|
||||
return entries;
|
||||
};
|
||||
const generations = tables.has("gen_metadata")
|
||||
? await readMetadata("SELECT idx, data FROM gen_metadata ORDER BY idx", "data", false)
|
||||
: [];
|
||||
let trajectoryTimestamp: number | null = null;
|
||||
if (tables.has("trajectory_metadata_blob")) {
|
||||
for (const row of db.prepare("SELECT data FROM trajectory_metadata_blob").iterate()) {
|
||||
trajectoryTimestamp ??= timestamp(nested(fields(blob(row.data)), 2));
|
||||
}
|
||||
}
|
||||
const steps = tables.has("steps")
|
||||
? await readMetadata(
|
||||
"SELECT idx, metadata FROM steps WHERE metadata IS NOT NULL ORDER BY idx",
|
||||
"metadata",
|
||||
true,
|
||||
)
|
||||
: [];
|
||||
const sessionId = NodePath.basename(path, ".db");
|
||||
const records: UsageCandidate[] = [];
|
||||
const generationModels = new Map(generations.map(({ idx, entry }) => [idx, entry.model]));
|
||||
for (const [source, entries] of [
|
||||
["step", steps],
|
||||
["generation", generations],
|
||||
] as const) {
|
||||
for (const [index, { idx, entry }] of entries.entries()) {
|
||||
for (const [usageIndex, usage] of entry.usages.entries()) {
|
||||
const outputTokens = Math.max(
|
||||
numberAt(usage, 3),
|
||||
numberAt(usage, 9) + numberAt(usage, 10),
|
||||
);
|
||||
const totals = {
|
||||
uncachedInputTokens: numberAt(usage, 2),
|
||||
cachedInputTokens: numberAt(usage, 5),
|
||||
cacheCreationTokens: numberAt(usage, 4),
|
||||
outputTokens,
|
||||
reasoningTokens: Math.min(outputTokens, numberAt(usage, 9)),
|
||||
};
|
||||
if (
|
||||
totals.uncachedInputTokens +
|
||||
totals.cachedInputTokens +
|
||||
totals.cacheCreationTokens +
|
||||
outputTokens ===
|
||||
0
|
||||
)
|
||||
continue;
|
||||
const keys = ([11, 12, 7] as const).flatMap((key) => {
|
||||
const id = textAt(usage, key);
|
||||
return id ? [`antigravity:${key}:${id}`] : [];
|
||||
});
|
||||
const record: UsageRecord = {
|
||||
provider: "antigravity",
|
||||
sessionId,
|
||||
timestampMs: entry.timestampMs ?? trajectoryTimestamp ?? fallbackTimestamp,
|
||||
model:
|
||||
MODEL_IDS[numberAt(usage, 1)] ||
|
||||
entry.model ||
|
||||
(source === "step" ? generationModels.get(idx) : "") ||
|
||||
modelName("", numberAt(usage, 1)) ||
|
||||
"antigravity-unknown",
|
||||
totals,
|
||||
reportedCostUsd: null,
|
||||
fast: false,
|
||||
dedupeKey: keys[0] ?? `antigravity:${sessionId}:${source}:${index}:${usageIndex}`,
|
||||
};
|
||||
records.push({
|
||||
record,
|
||||
keys,
|
||||
timestampQuality: entry.timestampMs !== null ? 2 : trajectoryTimestamp !== null ? 1 : 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return records;
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
/** Reads and merges aliases across every configured Antigravity store before date filtering. */
|
||||
export async function readAntigravityUsage(
|
||||
conversationsDirectories: string | readonly string[],
|
||||
sinceMs: number,
|
||||
) {
|
||||
const roots =
|
||||
typeof conversationsDirectories === "string"
|
||||
? [conversationsDirectories]
|
||||
: conversationsDirectories;
|
||||
const files: Array<{ root: string; path: string; records: UsageRecord[] }> = [];
|
||||
const errors: string[] = [];
|
||||
const identities = new Map<string, number>();
|
||||
const groups: Array<
|
||||
UsageCandidate & { parent: number; size: number; owner: number; fileIndex: number }
|
||||
> = [];
|
||||
const find = (index: number): number => {
|
||||
let root = index;
|
||||
while (groups[root]!.parent !== root) root = groups[root]!.parent;
|
||||
while (index !== root) {
|
||||
const parent = groups[index]!.parent;
|
||||
groups[index]!.parent = root;
|
||||
index = parent;
|
||||
}
|
||||
return root;
|
||||
};
|
||||
const merge = (left: number, right: number): number => {
|
||||
let a = find(left);
|
||||
let b = find(right);
|
||||
if (a === b) return a;
|
||||
if (groups[a]!.size < groups[b]!.size) [a, b] = [b, a];
|
||||
const target = groups[a]!;
|
||||
const source = groups[b]!;
|
||||
const first = target.owner < source.owner ? target : source;
|
||||
const bestTime =
|
||||
source.timestampQuality > target.timestampQuality ||
|
||||
(source.timestampQuality === target.timestampQuality &&
|
||||
source.record.timestampMs < target.record.timestampMs)
|
||||
? source
|
||||
: target;
|
||||
const x = target.record.totals;
|
||||
const y = source.record.totals;
|
||||
target.record = {
|
||||
...first.record,
|
||||
model:
|
||||
first.record.model === "antigravity-unknown"
|
||||
? first === target
|
||||
? source.record.model
|
||||
: target.record.model
|
||||
: first.record.model,
|
||||
timestampMs: bestTime.record.timestampMs,
|
||||
totals: {
|
||||
uncachedInputTokens: Math.max(x.uncachedInputTokens, y.uncachedInputTokens),
|
||||
cachedInputTokens: Math.max(x.cachedInputTokens, y.cachedInputTokens),
|
||||
cacheCreationTokens: Math.max(x.cacheCreationTokens, y.cacheCreationTokens),
|
||||
outputTokens: Math.max(x.outputTokens, y.outputTokens),
|
||||
reasoningTokens: Math.max(x.reasoningTokens, y.reasoningTokens),
|
||||
},
|
||||
};
|
||||
target.timestampQuality = bestTime.timestampQuality;
|
||||
target.owner = first.owner;
|
||||
target.fileIndex = first.fileIndex;
|
||||
target.size += source.size;
|
||||
source.parent = a;
|
||||
return a;
|
||||
};
|
||||
const append = (candidate: UsageCandidate, fileIndex: number) => {
|
||||
const index = groups.length;
|
||||
groups.push({ ...candidate, parent: index, size: 1, owner: index, fileIndex });
|
||||
for (const key of candidate.keys) {
|
||||
const existing = identities.get(key);
|
||||
if (existing !== undefined) merge(index, existing);
|
||||
identities.set(key, index);
|
||||
}
|
||||
};
|
||||
const visited = new Set<string>();
|
||||
const walk = async (directory: string, root: string): Promise<void> => {
|
||||
let entries;
|
||||
try {
|
||||
entries = await NodeFSP.readdir(directory, { withFileTypes: true });
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== "ENOENT") errors.push(directory);
|
||||
return;
|
||||
}
|
||||
entries.sort((a, b) => a.name.localeCompare(b.name));
|
||||
for (const entry of entries) {
|
||||
const path = NodePath.join(directory, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
await walk(path, root);
|
||||
} else if (entry.isFile() && entry.name.endsWith(".db")) {
|
||||
try {
|
||||
const canonical = await NodeFSP.realpath(path);
|
||||
if (visited.has(canonical)) continue;
|
||||
visited.add(canonical);
|
||||
const stat = await NodeFSP.stat(path);
|
||||
const candidates = await readDatabase(path, stat.mtimeMs);
|
||||
const fileIndex = files.length;
|
||||
files.push({ root, path, records: [] });
|
||||
for (const [index, candidate] of candidates.entries()) {
|
||||
append(candidate, fileIndex);
|
||||
if (index % 256 === 255) await NodeTimersPromises.setImmediate();
|
||||
}
|
||||
} catch {
|
||||
errors.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
for (const root of roots) await walk(root, root);
|
||||
for (const [index, group] of groups.entries()) {
|
||||
if (group.parent === index && group.record.timestampMs >= sinceMs) {
|
||||
files[group.fileIndex]!.records.push(group.record);
|
||||
}
|
||||
}
|
||||
return { files, errors };
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
// Node fs reads CLI credentials, and crypto hashes account IDs for deduplication.
|
||||
// @effect-diagnostics nodeBuiltinImport:off
|
||||
import * as NodeFSP from "node:fs/promises";
|
||||
import * as NodeCrypto from "node:crypto";
|
||||
import * as NodeTimersPromises from "node:timers/promises";
|
||||
|
||||
import type { UsageRecord } from "./usageTranscripts.ts";
|
||||
import { readMacCursorAccessToken } from "../provider/cursorCredentialStore.ts";
|
||||
|
||||
function object(value: unknown): Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
: {};
|
||||
}
|
||||
|
||||
function tokens(value: unknown): number {
|
||||
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.trunc(value) : 0;
|
||||
}
|
||||
|
||||
export interface CursorAccountUsageReadResult {
|
||||
readonly accountKey: string | null;
|
||||
readonly records: readonly UsageRecord[];
|
||||
readonly missing: boolean;
|
||||
readonly error: string | null;
|
||||
}
|
||||
|
||||
const accountHash = (value: string) => NodeCrypto.createHash("sha256").update(value).digest("hex");
|
||||
|
||||
function canonicalJson(value: unknown): string {
|
||||
if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
|
||||
if (value !== null && typeof value === "object") {
|
||||
return `{${Object.entries(value)
|
||||
.sort(([a], [b]) => a.localeCompare(b))
|
||||
.map(([key, entry]) => `${JSON.stringify(key)}:${canonicalJson(entry)}`)
|
||||
.join(",")}}`;
|
||||
}
|
||||
return JSON.stringify(value) ?? "null";
|
||||
}
|
||||
|
||||
/** Find the longest exact suffix/prefix overlap in linear time. */
|
||||
function boundaryOverlap(previous: readonly string[], current: readonly string[]): number {
|
||||
const sequence = [...current, "", ...previous];
|
||||
const lengths = Array.from({ length: sequence.length }, () => 0);
|
||||
for (let index = 1; index < sequence.length; index++) {
|
||||
let length = lengths[index - 1]!;
|
||||
while (length > 0 && sequence[index] !== sequence[length]) length = lengths[length - 1]!;
|
||||
if (sequence[index] === sequence[length]) length++;
|
||||
lengths[index] = length;
|
||||
}
|
||||
return lengths.at(-1) ?? 0;
|
||||
}
|
||||
|
||||
/** Dashboard usage includes headless agents and reports fresh input separately from cache reads. */
|
||||
export async function readCursorAccountUsage(
|
||||
credentialSource: string | { readonly kind: "keychain" },
|
||||
sinceMs: number,
|
||||
endDate: number,
|
||||
request: (url: string, init: RequestInit) => Promise<Response> = globalThis.fetch,
|
||||
keychainToken: () => Promise<string | null> = readMacCursorAccessToken,
|
||||
): Promise<CursorAccountUsageReadResult> {
|
||||
let accessToken: unknown;
|
||||
try {
|
||||
accessToken =
|
||||
typeof credentialSource === "string"
|
||||
? object(JSON.parse(await NodeFSP.readFile(credentialSource, "utf8"))).accessToken
|
||||
: await keychainToken();
|
||||
} catch (cause) {
|
||||
const missing = typeof credentialSource === "string" && object(cause).code === "ENOENT";
|
||||
return {
|
||||
accountKey: null,
|
||||
records: [],
|
||||
missing,
|
||||
error: missing
|
||||
? null
|
||||
: typeof credentialSource === "string"
|
||||
? "Cursor credentials could not be read."
|
||||
: "Cursor Keychain credentials could not be read.",
|
||||
};
|
||||
}
|
||||
if (typeof accessToken !== "string" || !accessToken) {
|
||||
return {
|
||||
accountKey: null,
|
||||
records: [],
|
||||
missing: true,
|
||||
error:
|
||||
typeof credentialSource === "string"
|
||||
? null
|
||||
: "Cursor account history needs a macOS Keychain CLI login on this server.",
|
||||
};
|
||||
}
|
||||
let accountKey: string | null = null;
|
||||
try {
|
||||
const payload = accessToken.split(".")[1];
|
||||
const subject = object(
|
||||
JSON.parse(Buffer.from(payload ?? "", "base64url").toString("utf8")),
|
||||
).sub;
|
||||
if (typeof subject !== "string" || !subject) throw new Error("Invalid authentication");
|
||||
const userId = subject.split("|").at(-1);
|
||||
if (!userId) throw new Error("Invalid authentication");
|
||||
accountKey = accountHash(subject);
|
||||
if (!Number.isFinite(sinceMs) || !Number.isFinite(endDate) || sinceMs < 0 || sinceMs > endDate)
|
||||
throw new Error("Invalid date window");
|
||||
const deadline = AbortSignal.timeout(60_000);
|
||||
const records: UsageRecord[] = [];
|
||||
const occurrences = new Map<string, number>();
|
||||
const pages: unknown[][] = [];
|
||||
let completed = false;
|
||||
const pageSize = 1000;
|
||||
let total: number | undefined;
|
||||
for (let page = 1; ; page++) {
|
||||
// A count can include overlapping page boundaries. Allow room to
|
||||
// reconcile them without imposing a fixed account-size limit.
|
||||
if (page > (total === undefined ? 1000 : Math.ceil(total / pageSize) * 2 + 1)) {
|
||||
throw new Error("Account usage page limit exceeded");
|
||||
}
|
||||
const response = await request("https://cursor.com/api/dashboard/get-filtered-usage-events", {
|
||||
method: "POST",
|
||||
redirect: "error",
|
||||
signal: AbortSignal.any([deadline, AbortSignal.timeout(10_000)]),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Origin: "https://cursor.com",
|
||||
Cookie: `WorkosCursorSessionToken=${encodeURIComponent(`${userId}::${accessToken}`)}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
page,
|
||||
pageSize,
|
||||
startDate: String(sinceMs),
|
||||
endDate: String(endDate),
|
||||
}),
|
||||
});
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
return {
|
||||
accountKey,
|
||||
records: [],
|
||||
missing: false,
|
||||
error: "Sign in to Cursor again to read account usage.",
|
||||
};
|
||||
}
|
||||
if (!response.ok) throw new Error("Account usage request failed");
|
||||
const parsed: unknown = await response.json();
|
||||
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
||||
throw new Error("Invalid account usage page");
|
||||
}
|
||||
const body = object(parsed);
|
||||
const keys = Object.keys(body);
|
||||
if ("error" in body || "message" in body || "code" in body)
|
||||
throw new Error("Account usage error response");
|
||||
const count = keys.length === 0 ? 0 : body.totalUsageEventsCount;
|
||||
const events =
|
||||
keys.length === 0 || (keys.length === 1 && keys[0] === "totalUsageEventsCount")
|
||||
? []
|
||||
: body.usageEventsDisplay;
|
||||
if (
|
||||
(count !== undefined &&
|
||||
(typeof count !== "number" ||
|
||||
!Number.isSafeInteger(count) ||
|
||||
count < 0 ||
|
||||
(total !== undefined && count !== total))) ||
|
||||
!Array.isArray(events) ||
|
||||
events.length > pageSize ||
|
||||
(count === undefined && !Array.isArray(body.usageEventsDisplay))
|
||||
) {
|
||||
throw new Error("Inconsistent account usage page");
|
||||
}
|
||||
if (typeof count === "number") total = count;
|
||||
pages.push(events);
|
||||
if (events.length < pageSize) {
|
||||
completed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!completed) throw new Error("Account usage page limit exceeded");
|
||||
const rawCount = pages.reduce((sum, page) => sum + page.length, 0);
|
||||
if (total !== undefined && rawCount < total) throw new Error("Incomplete account usage pages");
|
||||
let removalsRemaining = total === undefined ? 0 : rawCount - total;
|
||||
let previousKeys: string[] = [];
|
||||
for (const events of pages) {
|
||||
const eventKeys =
|
||||
removalsRemaining > 0 ? events.map((event) => accountHash(canonicalJson(event))) : [];
|
||||
const removalCount = Math.min(removalsRemaining, boundaryOverlap(previousKeys, eventKeys));
|
||||
removalsRemaining -= removalCount;
|
||||
previousKeys = eventKeys;
|
||||
for (const raw of events.slice(removalCount)) {
|
||||
const event = object(raw);
|
||||
const usage = object(event.tokenUsage);
|
||||
if (event.tokenUsage === undefined || event.tokenUsage === null) continue;
|
||||
for (const key of [
|
||||
"inputTokens",
|
||||
"outputTokens",
|
||||
"cacheReadTokens",
|
||||
"cacheWriteTokens",
|
||||
"totalCents",
|
||||
]) {
|
||||
const value = usage[key];
|
||||
if (
|
||||
value !== undefined &&
|
||||
(typeof value !== "number" || !Number.isFinite(value) || value < 0)
|
||||
) {
|
||||
throw new Error("Invalid account usage totals");
|
||||
}
|
||||
}
|
||||
const timestampMs =
|
||||
typeof event.timestamp === "string" && event.timestamp.trim() !== ""
|
||||
? Number(event.timestamp)
|
||||
: event.timestamp;
|
||||
if (
|
||||
typeof timestampMs !== "number" ||
|
||||
!Number.isFinite(timestampMs) ||
|
||||
typeof event.model !== "string" ||
|
||||
!event.model
|
||||
)
|
||||
throw new Error("Invalid account usage event");
|
||||
if (timestampMs < sinceMs || timestampMs > endDate) continue;
|
||||
const totals = {
|
||||
uncachedInputTokens: tokens(usage.inputTokens),
|
||||
cachedInputTokens: tokens(usage.cacheReadTokens),
|
||||
cacheCreationTokens: tokens(usage.cacheWriteTokens),
|
||||
outputTokens: tokens(usage.outputTokens),
|
||||
reasoningTokens: 0,
|
||||
};
|
||||
const reportedCostUsd =
|
||||
typeof usage.totalCents === "number" ? usage.totalCents / 100 : null;
|
||||
const sessionId = typeof event.conversationId === "string" ? event.conversationId : "";
|
||||
// No event ID is provided. Preserve identical billed rows with an occurrence index.
|
||||
const key = accountHash(
|
||||
JSON.stringify([timestampMs, event.model, sessionId, totals, reportedCostUsd]),
|
||||
);
|
||||
const occurrence = occurrences.get(key) ?? 0;
|
||||
occurrences.set(key, occurrence + 1);
|
||||
records.push({
|
||||
provider: "cursor",
|
||||
timestampMs,
|
||||
model: event.model,
|
||||
sessionId,
|
||||
totals,
|
||||
reportedCostUsd,
|
||||
fast: false,
|
||||
dedupeKey: `cursor-account:${accountKey}:${key}:${occurrence}`,
|
||||
});
|
||||
}
|
||||
await NodeTimersPromises.setImmediate();
|
||||
}
|
||||
if (removalsRemaining !== 0) throw new Error("Inconsistent account usage boundaries");
|
||||
return { accountKey, records, missing: false, error: null };
|
||||
} catch {
|
||||
return {
|
||||
accountKey,
|
||||
records: [],
|
||||
missing: false,
|
||||
error: "Cursor account usage could not be read.",
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
// node:sqlite reads live OpenCode databases; Node fs walks legacy JSON history.
|
||||
// @effect-diagnostics nodeBuiltinImport:off
|
||||
import * as NodeFSP from "node:fs/promises";
|
||||
import * as NodePath from "node:path";
|
||||
import * as NodeSqlite from "node:sqlite";
|
||||
import * as NodeTimersPromises from "node:timers/promises";
|
||||
|
||||
import { totalTokens, type UsageRecord } from "./usageTranscripts.ts";
|
||||
|
||||
function object(value: unknown): Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
: {};
|
||||
}
|
||||
|
||||
function tokens(value: unknown): number {
|
||||
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.trunc(value) : 0;
|
||||
}
|
||||
|
||||
function text(value: unknown): string {
|
||||
return typeof value === "string" ? value : "";
|
||||
}
|
||||
|
||||
/** OpenCode stores uncached input and reasoning separately from input/output. */
|
||||
function parseOpenCodeMessage(
|
||||
source: string,
|
||||
fallback: {
|
||||
readonly id?: string;
|
||||
readonly sessionId?: string;
|
||||
readonly timestampMs?: number;
|
||||
} = {},
|
||||
): UsageRecord | null {
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(source);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
const message = object(parsed);
|
||||
if (message.role !== undefined && message.role !== "assistant") return null;
|
||||
const usage = object(message.tokens);
|
||||
const cache = object(usage.cache);
|
||||
const modelReference = object(message.model);
|
||||
const model = text(modelReference.id) || text(modelReference.modelID) || text(message.modelID);
|
||||
const timestampMs = object(message.time).created ?? fallback.timestampMs;
|
||||
if (!model || typeof timestampMs !== "number" || !Number.isFinite(timestampMs)) return null;
|
||||
const reasoningTokens = tokens(usage.reasoning);
|
||||
const totals = {
|
||||
uncachedInputTokens: tokens(usage.input),
|
||||
cachedInputTokens: tokens(cache.read),
|
||||
cacheCreationTokens: tokens(cache.write),
|
||||
outputTokens: tokens(usage.output) + reasoningTokens,
|
||||
reasoningTokens,
|
||||
};
|
||||
if (totalTokens(totals) === 0) return null;
|
||||
const id = fallback.id || text(message.id);
|
||||
const cost = message.cost;
|
||||
return {
|
||||
provider: "opencode",
|
||||
timestampMs,
|
||||
model,
|
||||
sessionId: fallback.sessionId || text(message.sessionID),
|
||||
totals,
|
||||
// OpenCode writes zero for models without a known rate, including paid
|
||||
// subscription models. Let the shared price table estimate those records.
|
||||
reportedCostUsd: typeof cost === "number" && Number.isFinite(cost) && cost > 0 ? cost : null,
|
||||
fast: false,
|
||||
dedupeKey: id ? `opencode:${id}` : null,
|
||||
};
|
||||
}
|
||||
|
||||
export interface OpenCodeUsageReadResult {
|
||||
readonly files: readonly { readonly path: string; readonly records: readonly UsageRecord[] }[];
|
||||
readonly missing: boolean;
|
||||
readonly error: boolean;
|
||||
}
|
||||
|
||||
/** Reads current SQLite and pre-migration JSON stores without modifying either. */
|
||||
export async function readOpenCodeUsage(
|
||||
root: string,
|
||||
sinceMs: number,
|
||||
): Promise<OpenCodeUsageReadResult> {
|
||||
const files: { path: string; records: UsageRecord[] }[] = [];
|
||||
const seen = new Set<string>();
|
||||
let found = false;
|
||||
let error = false;
|
||||
const append = (records: UsageRecord[], record: UsageRecord | null) => {
|
||||
if (record === null || record.timestampMs < sinceMs) return;
|
||||
if (record.dedupeKey !== null) {
|
||||
if (seen.has(record.dedupeKey)) return;
|
||||
seen.add(record.dedupeKey);
|
||||
}
|
||||
records.push(record);
|
||||
};
|
||||
|
||||
let databases: string[] = [];
|
||||
try {
|
||||
databases = (await NodeFSP.readdir(root, { withFileTypes: true }))
|
||||
.filter((entry) => entry.isFile() && /^opencode(?:-[a-zA-Z0-9_-]+)?\.db$/.test(entry.name))
|
||||
.map((entry) => entry.name)
|
||||
.sort((a, b) => (a === "opencode.db" ? -1 : b === "opencode.db" ? 1 : a.localeCompare(b)));
|
||||
} catch (cause) {
|
||||
if (object(cause).code !== "ENOENT") error = true;
|
||||
}
|
||||
for (const name of databases) {
|
||||
found = true;
|
||||
const file = { path: NodePath.join(root, name), records: [] as UsageRecord[] };
|
||||
files.push(file);
|
||||
let database: NodeSqlite.DatabaseSync | undefined;
|
||||
try {
|
||||
database = new NodeSqlite.DatabaseSync(NodePath.join(root, name), { readOnly: true });
|
||||
// A busy live provider should fail this source promptly rather than
|
||||
// stalling the server while SQLite waits for its writer.
|
||||
database.exec("PRAGMA busy_timeout = 100");
|
||||
const tables = new Set(
|
||||
database
|
||||
.prepare("SELECT name FROM sqlite_master WHERE type = 'table'")
|
||||
.all()
|
||||
.map((row) => row.name),
|
||||
);
|
||||
if (!tables.has("message") && !tables.has("session_message")) error = true;
|
||||
for (const table of ["message", "session_message"] as const) {
|
||||
if (!tables.has(table)) continue;
|
||||
const columns = new Set(
|
||||
database
|
||||
.prepare(`PRAGMA table_info(${table})`)
|
||||
.all()
|
||||
.map((row) => row.name),
|
||||
);
|
||||
const timestamp = columns.has("time_created") ? "time_created" : "NULL";
|
||||
const predicates = table === "session_message" ? ["type = 'assistant'"] : [];
|
||||
if (timestamp !== "NULL") predicates.push("time_created >= ?");
|
||||
const where = predicates.length > 0 ? ` WHERE ${predicates.join(" AND ")}` : "";
|
||||
const statement = database.prepare(
|
||||
`SELECT id, session_id, data, ${timestamp} AS created FROM ${table}${where}`,
|
||||
);
|
||||
let count = 0;
|
||||
for (const row of statement.iterate(...(timestamp === "NULL" ? [] : [sinceMs]))) {
|
||||
append(
|
||||
file.records,
|
||||
parseOpenCodeMessage(text(row.data), {
|
||||
id: text(row.id),
|
||||
sessionId: text(row.session_id),
|
||||
...(typeof row.created === "number" ? { timestampMs: row.created } : {}),
|
||||
}),
|
||||
);
|
||||
if (++count % 256 === 0) await NodeTimersPromises.setImmediate();
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
error = true;
|
||||
} finally {
|
||||
database?.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Do not follow symlinks, including cycles. Database records win over their
|
||||
// old JSON copies when OpenCode has migrated a store in place.
|
||||
const directories = [NodePath.join(root, "storage", "message")];
|
||||
while (directories.length > 0) {
|
||||
const directory = directories.pop()!;
|
||||
try {
|
||||
for (const entry of await NodeFSP.readdir(directory, { withFileTypes: true })) {
|
||||
const path = NodePath.join(directory, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
directories.push(path);
|
||||
} else if (entry.isFile() && entry.name.endsWith(".json")) {
|
||||
found = true;
|
||||
const id = entry.name.slice(0, -5);
|
||||
if (seen.has(`opencode:${id}`)) continue;
|
||||
const file = { path, records: [] as UsageRecord[] };
|
||||
files.push(file);
|
||||
try {
|
||||
append(
|
||||
file.records,
|
||||
parseOpenCodeMessage(await NodeFSP.readFile(path, "utf8"), { id }),
|
||||
);
|
||||
} catch (cause) {
|
||||
if (object(cause).code !== "ENOENT") error = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (cause) {
|
||||
if (object(cause).code !== "ENOENT") error = true;
|
||||
}
|
||||
}
|
||||
return { files, missing: !found && !error, error };
|
||||
}
|
||||
@@ -112,7 +112,7 @@ export class UsageAggregator {
|
||||
* can derive per-window facts (distinct sessions, for one) from the records
|
||||
* that landed rather than everything the mtime prefilter happened to admit.
|
||||
*/
|
||||
add(record: UsageRecord): boolean {
|
||||
add(record: UsageRecord, sourcePath?: string): boolean {
|
||||
if (record.dedupeKey !== null) {
|
||||
if (this.#seen.has(record.dedupeKey)) {
|
||||
this.#duplicatesDropped += 1;
|
||||
@@ -146,7 +146,7 @@ export class UsageAggregator {
|
||||
this.#hourlyWindow.sinceTimeMs +
|
||||
Math.floor((record.timestampMs - this.#hourlyWindow.sinceTimeMs) / HOUR_MS) * HOUR_MS,
|
||||
).toISOString();
|
||||
const key = `${day}\u0000${hourStart}\u0000${record.provider}\u0000${record.model}`;
|
||||
const key = `${day}\u0000${hourStart}\u0000${record.provider}\u0000${record.model}\u0000${sourcePath ?? ""}`;
|
||||
let bucket = this.#buckets.get(key);
|
||||
if (bucket === undefined) {
|
||||
bucket = {
|
||||
@@ -180,12 +180,14 @@ export class UsageAggregator {
|
||||
finish(): AggregateResult {
|
||||
const buckets: UsageBucket[] = [];
|
||||
for (const [key, bucket] of this.#buckets) {
|
||||
const [day = "", hourStart = "", provider = "", model = ""] = key.split("\u0000");
|
||||
const [day = "", hourStart = "", provider = "", model = "", sourcePath = ""] =
|
||||
key.split("\u0000");
|
||||
buckets.push({
|
||||
day: day as UsageDay,
|
||||
...(hourStart === "" ? {} : { hourStart }),
|
||||
provider: provider as UsageBucket["provider"],
|
||||
model,
|
||||
...(sourcePath === "" ? {} : { sourcePath }),
|
||||
totals: bucket.totals,
|
||||
costUsd: bucket.costUsd,
|
||||
cacheSavingsUsd: bucket.cacheSavingsUsd,
|
||||
|
||||
@@ -4,13 +4,40 @@
|
||||
import * as NodeFSP from "node:fs/promises";
|
||||
import * as NodeOS from "node:os";
|
||||
import * as NodePath from "node:path";
|
||||
import * as NodeSqlite from "node:sqlite";
|
||||
|
||||
import { afterEach, assert, beforeEach, describe, it } from "@effect/vitest";
|
||||
|
||||
import { readTranscriptRecords } from "./usageTranscriptReader.ts";
|
||||
import { readOpenCodeUsage } from "./opencodeUsageReader.ts";
|
||||
import { readCursorAccountUsage } from "./cursorUsageReader.ts";
|
||||
import { readAntigravityUsage } from "./antigravityUsageReader.ts";
|
||||
|
||||
let dir: string;
|
||||
|
||||
function protoNumber(field: number, value: number): number[] {
|
||||
const varint = (number: number) => {
|
||||
const bytes: number[] = [];
|
||||
do {
|
||||
const byte = number % 128;
|
||||
number = Math.floor(number / 128);
|
||||
bytes.push(byte + (number > 0 ? 128 : 0));
|
||||
} while (number > 0);
|
||||
return bytes;
|
||||
};
|
||||
return [...varint(field * 8), ...varint(value)];
|
||||
}
|
||||
|
||||
function protoBytes(field: number, bytes: readonly number[]): number[] {
|
||||
const encoded = protoNumber(field, bytes.length);
|
||||
encoded[0] = encoded[0]! + 2;
|
||||
return [...encoded, ...bytes];
|
||||
}
|
||||
|
||||
function protoText(field: number, value: string): number[] {
|
||||
return protoBytes(field, [...Buffer.from(value)]);
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
dir = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "usage-reader-test-"));
|
||||
});
|
||||
@@ -208,3 +235,530 @@ describe("readTranscriptRecords resume", () => {
|
||||
assert.isNull(await readTranscriptRecords(NodePath.join(dir, "missing.jsonl"), "claude"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("SQLite usage readers", () => {
|
||||
it("reads Cursor account history with the default macOS Keychain login", async () => {
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`;
|
||||
let keychainReads = 0;
|
||||
const result = await readCursorAccountUsage(
|
||||
{ kind: "keychain" },
|
||||
0,
|
||||
1781000000000,
|
||||
async (_url, init) => {
|
||||
assert.include(new Headers(init.headers).get("cookie") ?? "", "demo%3A%3A");
|
||||
return Response.json({ totalUsageEventsCount: 0, usageEventsDisplay: [] });
|
||||
},
|
||||
async () => {
|
||||
keychainReads++;
|
||||
return accessToken;
|
||||
},
|
||||
);
|
||||
assert.strictEqual(keychainReads, 1);
|
||||
assert.isNull(result.error);
|
||||
assert.isFalse(result.missing);
|
||||
assert.isNotNull(result.accountKey);
|
||||
});
|
||||
|
||||
it("reads paginated Cursor account history including headless calls with separate cache tokens", async () => {
|
||||
const authPath = NodePath.join(dir, "auth.json");
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo", exp: 4102444800 })).toString("base64url")}.signature`;
|
||||
await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken }));
|
||||
const pages: number[] = [];
|
||||
const signals: AbortSignal[] = [];
|
||||
const request = async (url: string, init: RequestInit) => {
|
||||
assert.strictEqual(String(url), "https://cursor.com/api/dashboard/get-filtered-usage-events");
|
||||
assert.strictEqual(init?.redirect, "error");
|
||||
const headers = new Headers(init?.headers);
|
||||
assert.strictEqual(headers.get("origin"), "https://cursor.com");
|
||||
assert.include(headers.get("cookie") ?? "", "WorkosCursorSessionToken=demo%3A%3A");
|
||||
const body = JSON.parse(String(init?.body));
|
||||
pages.push(body.page);
|
||||
if (init.signal) signals.push(init.signal);
|
||||
return Response.json({
|
||||
totalUsageEventsCount: 1001,
|
||||
usageEventsDisplay: Array.from({ length: body.page === 1 ? 1000 : 1 }, (_, index) => ({
|
||||
timestamp: String(1780000000000 + ((body.page - 1) * 1000 + index) * 1000),
|
||||
model: "claude-sonnet-4-5",
|
||||
conversationId: `conversation-${body.page}`,
|
||||
isHeadless: body.page === 2,
|
||||
chargedCents: 0,
|
||||
tokenUsage: {
|
||||
inputTokens: 10,
|
||||
outputTokens: 5,
|
||||
cacheReadTokens: 30,
|
||||
cacheWriteTokens: 2,
|
||||
totalCents: 25,
|
||||
},
|
||||
})),
|
||||
});
|
||||
};
|
||||
const result = await readCursorAccountUsage(authPath, 0, 1781000000000, request);
|
||||
assert.isNull(result.error);
|
||||
assert.deepStrictEqual(pages, [1, 2]);
|
||||
assert.lengthOf(signals, 2);
|
||||
assert.notStrictEqual(signals[0], signals[1]);
|
||||
assert.strictEqual(result.records.length, 1001);
|
||||
assert.strictEqual(result.records.at(-1)?.sessionId, "conversation-2");
|
||||
assert.deepStrictEqual(result.records[0]?.totals, {
|
||||
uncachedInputTokens: 10,
|
||||
cachedInputTokens: 30,
|
||||
cacheCreationTokens: 2,
|
||||
outputTokens: 5,
|
||||
reasoningTokens: 0,
|
||||
});
|
||||
assert.strictEqual(result.records[0]?.reportedCostUsd, 0.25);
|
||||
assert.isFalse(result.accountKey?.includes("demo") ?? true);
|
||||
});
|
||||
|
||||
it("reads Cursor account history beyond 100 pages", async () => {
|
||||
const authPath = NodePath.join(dir, "auth.json");
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`;
|
||||
await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken }));
|
||||
const fullPage = Array.from({ length: 1000 }, () => ({ tokenUsage: null }));
|
||||
let requests = 0;
|
||||
const result = await readCursorAccountUsage(authPath, 0, 1781000000000, async () => {
|
||||
requests += 1;
|
||||
return Response.json({
|
||||
totalUsageEventsCount: 100_001,
|
||||
usageEventsDisplay: requests <= 100 ? fullPage : [{ tokenUsage: null }],
|
||||
});
|
||||
});
|
||||
assert.isNull(result.error);
|
||||
assert.strictEqual(requests, 101);
|
||||
assert.deepStrictEqual(result.records, []);
|
||||
});
|
||||
|
||||
it("accepts confirmed empty Cursor usage but rejects error envelopes", async () => {
|
||||
const authPath = NodePath.join(dir, "auth.json");
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`;
|
||||
await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken }));
|
||||
for (const body of [
|
||||
{},
|
||||
{ totalUsageEventsCount: 0 },
|
||||
{ totalUsageEventsCount: 0, usageEventsDisplay: [] },
|
||||
]) {
|
||||
const result = await readCursorAccountUsage(authPath, 0, 1781000000000, async () =>
|
||||
Response.json(body),
|
||||
);
|
||||
assert.isNull(result.error);
|
||||
assert.deepStrictEqual(result.records, []);
|
||||
assert.isFalse(result.missing);
|
||||
}
|
||||
for (const body of [
|
||||
{ error: "upstream error" },
|
||||
{ detail: "unknown error envelope" },
|
||||
{ totalUsageEventsCount: 0, error: "upstream error" },
|
||||
null,
|
||||
[],
|
||||
"invalid",
|
||||
0,
|
||||
]) {
|
||||
const result = await readCursorAccountUsage(authPath, 0, 1781000000000, async () =>
|
||||
Response.json(body),
|
||||
);
|
||||
assert.isNotNull(result.error);
|
||||
assert.deepStrictEqual(result.records, []);
|
||||
}
|
||||
});
|
||||
|
||||
it("requires a terminal Cursor page after a full page reaches the reported count", async () => {
|
||||
const authPath = NodePath.join(dir, "auth.json");
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`;
|
||||
await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken }));
|
||||
let requests = 0;
|
||||
const result = await readCursorAccountUsage(authPath, 0, 1781000000000, async () => {
|
||||
requests++;
|
||||
return Response.json(
|
||||
requests === 1
|
||||
? {
|
||||
totalUsageEventsCount: 1000,
|
||||
usageEventsDisplay: Array.from({ length: 1000 }, (_, index) => ({
|
||||
timestamp: String(1780000000000 + index),
|
||||
model: "gpt-5",
|
||||
tokenUsage: { inputTokens: 10, outputTokens: 5 },
|
||||
})),
|
||||
}
|
||||
: { totalUsageEventsCount: 1000 },
|
||||
);
|
||||
});
|
||||
assert.isNull(result.error);
|
||||
assert.strictEqual(result.records.length, 1000);
|
||||
assert.strictEqual(requests, 2);
|
||||
});
|
||||
|
||||
it("removes only count-proven Cursor boundary copies and preserves identical billed events", async () => {
|
||||
const authPath = NodePath.join(dir, "auth.json");
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`;
|
||||
await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken }));
|
||||
const event = (index: number) => ({
|
||||
timestamp: String(1780000000000 + index),
|
||||
model: "gpt-5",
|
||||
tokenUsage: { inputTokens: 10, outputTokens: 5, totalCents: 1 },
|
||||
});
|
||||
for (const total of [2000, 2001]) {
|
||||
let requests = 0;
|
||||
const result = await readCursorAccountUsage(authPath, 0, 1781000000000, async () => {
|
||||
requests++;
|
||||
return Response.json({
|
||||
totalUsageEventsCount: total,
|
||||
usageEventsDisplay:
|
||||
requests === 1
|
||||
? Array.from({ length: 1000 }, (_, index) => event(index))
|
||||
: requests === 2
|
||||
? Array.from({ length: 1000 }, (_, index) => event(999 + index))
|
||||
: [event(1999)],
|
||||
});
|
||||
});
|
||||
assert.isNull(result.error);
|
||||
assert.strictEqual(result.records.length, total);
|
||||
assert.strictEqual(requests, 3);
|
||||
assert.strictEqual(result.records.at(-1)?.timestampMs, 1780000001999);
|
||||
assert.strictEqual(
|
||||
result.records.filter((record) => record.timestampMs === 1780000000999).length,
|
||||
total === 2000 ? 1 : 2,
|
||||
);
|
||||
assert.strictEqual(new Set(result.records.map((record) => record.dedupeKey)).size, total);
|
||||
}
|
||||
let requests = 0;
|
||||
const inconsistent = await readCursorAccountUsage(authPath, 0, 1781000000000, async () => {
|
||||
requests++;
|
||||
return Response.json({
|
||||
totalUsageEventsCount: 1001,
|
||||
usageEventsDisplay:
|
||||
requests === 1
|
||||
? Array.from({ length: 1000 }, (_, index) => event(index))
|
||||
: [event(500), event(1000)],
|
||||
});
|
||||
});
|
||||
assert.isNotNull(inconsistent.error);
|
||||
assert.deepStrictEqual(inconsistent.records, []);
|
||||
});
|
||||
|
||||
it("does not present truncated Cursor account pages or authentication failures as complete history", async () => {
|
||||
const authPath = NodePath.join(dir, "auth.json");
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo", exp: 4102444800 })).toString("base64url")}.signature`;
|
||||
await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken }));
|
||||
const truncated = await readCursorAccountUsage(authPath, 0, 1781000000000, async () =>
|
||||
Response.json({ totalUsageEventsCount: 101, usageEventsDisplay: [] }),
|
||||
);
|
||||
assert.isNotNull(truncated.error);
|
||||
assert.deepStrictEqual(truncated.records, []);
|
||||
const denied = await readCursorAccountUsage(
|
||||
authPath,
|
||||
0,
|
||||
1781000000000,
|
||||
async () => new Response(accessToken, { status: 401 }),
|
||||
);
|
||||
assert.isNotNull(denied.error);
|
||||
assert.isFalse(denied.error?.includes(accessToken) ?? true);
|
||||
assert.deepStrictEqual(denied.records, []);
|
||||
let requested = false;
|
||||
const missing = await readCursorAccountUsage(
|
||||
NodePath.join(dir, "missing.json"),
|
||||
0,
|
||||
1781000000000,
|
||||
async () => {
|
||||
requested = true;
|
||||
return Response.json({});
|
||||
},
|
||||
);
|
||||
assert.isTrue(missing.missing);
|
||||
assert.isFalse(requested);
|
||||
});
|
||||
|
||||
it("counts migrated OpenCode messages once and sees subsequent WAL writes", async () => {
|
||||
const db = new NodeSqlite.DatabaseSync(NodePath.join(dir, "opencode.db"));
|
||||
try {
|
||||
db.exec(
|
||||
"PRAGMA journal_mode = WAL; PRAGMA wal_autocheckpoint = 0; CREATE TABLE message (id TEXT, session_id TEXT, data TEXT)",
|
||||
);
|
||||
const message = {
|
||||
id: "msg-1",
|
||||
sessionID: "session-1",
|
||||
role: "assistant",
|
||||
modelID: "claude-sonnet-4-5",
|
||||
time: { created: 1780000000000 },
|
||||
cost: 0.25,
|
||||
tokens: { input: 100, output: 20, reasoning: 5, cache: { read: 30, write: 10 } },
|
||||
};
|
||||
const insert = db.prepare("INSERT INTO message VALUES (?, ?, ?)");
|
||||
insert.run(message.id, message.sessionID, JSON.stringify(message));
|
||||
const legacy = NodePath.join(dir, "storage", "message", message.sessionID);
|
||||
await NodeFSP.mkdir(legacy, { recursive: true });
|
||||
await NodeFSP.writeFile(NodePath.join(legacy, "msg-1.json"), JSON.stringify(message));
|
||||
const first = await readOpenCodeUsage(dir, 0);
|
||||
assert.isFalse(first.error);
|
||||
const records = first.files.flatMap((file) => file.records);
|
||||
assert.strictEqual(records.length, 1);
|
||||
assert.deepStrictEqual(records[0]?.totals, {
|
||||
uncachedInputTokens: 100,
|
||||
cachedInputTokens: 30,
|
||||
cacheCreationTokens: 10,
|
||||
outputTokens: 25,
|
||||
reasoningTokens: 5,
|
||||
});
|
||||
assert.strictEqual(records[0]?.reportedCostUsd, 0.25);
|
||||
insert.run(
|
||||
"msg-2",
|
||||
message.sessionID,
|
||||
JSON.stringify({ ...message, id: "msg-2", time: { created: 1780000001000 } }),
|
||||
);
|
||||
const next = await readOpenCodeUsage(dir, 1780000001000);
|
||||
assert.isFalse(next.error);
|
||||
assert.deepStrictEqual(
|
||||
next.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
|
||||
["opencode:msg-2"],
|
||||
);
|
||||
assert.isAbove((await NodeFSP.stat(NodePath.join(dir, "opencode.db-wal"))).size, 0);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("deduplicates Antigravity generation and step usage while preserving retry model and token buckets", async () => {
|
||||
const db = new NodeSqlite.DatabaseSync(NodePath.join(dir, "session-1.db"));
|
||||
const stamp = protoNumber(1, 1780000000);
|
||||
const usage = [
|
||||
...protoNumber(2, 100),
|
||||
...protoNumber(3, 40),
|
||||
...protoNumber(4, 5),
|
||||
...protoNumber(5, 20),
|
||||
...protoNumber(9, 10),
|
||||
...protoText(11, "response-1"),
|
||||
];
|
||||
const retry = [
|
||||
...protoNumber(1, 1026),
|
||||
...protoNumber(2, 12),
|
||||
...protoNumber(3, 3),
|
||||
...protoText(11, "retry-1"),
|
||||
];
|
||||
const generation = protoBytes(1, [
|
||||
...protoBytes(4, usage),
|
||||
...protoText(19, "Gemini 3 Pro"),
|
||||
...protoBytes(9, protoBytes(4, stamp)),
|
||||
]);
|
||||
const step = [
|
||||
...protoBytes(9, usage),
|
||||
...protoBytes(8, stamp),
|
||||
...protoBytes(28, protoBytes(2, retry)),
|
||||
];
|
||||
try {
|
||||
db.exec(
|
||||
"CREATE TABLE gen_metadata (idx INTEGER, data BLOB); CREATE TABLE steps (idx INTEGER, metadata BLOB)",
|
||||
);
|
||||
db.prepare("INSERT INTO gen_metadata VALUES (?, ?)").run(0, new Uint8Array(generation));
|
||||
db.prepare("INSERT INTO steps VALUES (?, ?)").run(0, new Uint8Array(step));
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
const result = await readAntigravityUsage(dir, 0);
|
||||
assert.deepStrictEqual(result.errors, []);
|
||||
const records = result.files.flatMap((file) => file.records);
|
||||
assert.strictEqual(records.length, 2);
|
||||
const main = records.find((record) => record.model === "gemini-3-pro");
|
||||
assert.isDefined(main);
|
||||
assert.strictEqual(main?.timestampMs, 1780000000000);
|
||||
assert.strictEqual(main?.sessionId, "session-1");
|
||||
assert.deepStrictEqual(main?.totals, {
|
||||
uncachedInputTokens: 100,
|
||||
cachedInputTokens: 20,
|
||||
cacheCreationTokens: 5,
|
||||
outputTokens: 40,
|
||||
reasoningTokens: 10,
|
||||
});
|
||||
assert.strictEqual(
|
||||
records.find((record) => record.model === "claude-opus-4-6")?.totals.uncachedInputTokens,
|
||||
12,
|
||||
);
|
||||
assert.deepStrictEqual(
|
||||
(await readAntigravityUsage(dir, 1780000000001)).files.flatMap((file) => file.records),
|
||||
[],
|
||||
);
|
||||
});
|
||||
|
||||
it("uses the matching Antigravity generation model for each model-less step", async () => {
|
||||
const db = new NodeSqlite.DatabaseSync(NodePath.join(dir, "model-switch.db"));
|
||||
try {
|
||||
db.exec(
|
||||
"CREATE TABLE gen_metadata (idx INTEGER, data BLOB); CREATE TABLE steps (idx INTEGER, metadata BLOB)",
|
||||
);
|
||||
const generation = db.prepare("INSERT INTO gen_metadata VALUES (?, ?)");
|
||||
const step = db.prepare("INSERT INTO steps VALUES (?, ?)");
|
||||
for (const [idx, name] of ["Gemini 3 Pro", "Claude Opus 4.6"].entries()) {
|
||||
generation.run(idx, new Uint8Array(protoBytes(1, protoText(19, name))));
|
||||
step.run(idx, new Uint8Array(protoBytes(9, protoNumber(2, 10 + idx))));
|
||||
}
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
const result = await readAntigravityUsage(dir, 0);
|
||||
assert.deepStrictEqual(result.errors, []);
|
||||
assert.deepStrictEqual(
|
||||
result.files.flatMap((file) => file.records).map((record) => record.model),
|
||||
["gemini-3-pro", "claude-opus-4-6"],
|
||||
);
|
||||
});
|
||||
|
||||
it("merges Antigravity aliases that bridge previously separate step records", async () => {
|
||||
const db = new NodeSqlite.DatabaseSync(NodePath.join(dir, "bridge.db"));
|
||||
try {
|
||||
db.exec(
|
||||
"CREATE TABLE gen_metadata (idx INTEGER, data BLOB); CREATE TABLE steps (idx INTEGER, metadata BLOB)",
|
||||
);
|
||||
const step = db.prepare("INSERT INTO steps VALUES (?, ?)");
|
||||
step.run(
|
||||
0,
|
||||
new Uint8Array(protoBytes(9, [...protoNumber(2, 100), ...protoText(11, "response")])),
|
||||
);
|
||||
step.run(
|
||||
1,
|
||||
new Uint8Array(protoBytes(9, [...protoNumber(3, 40), ...protoText(12, "provider")])),
|
||||
);
|
||||
db.prepare("INSERT INTO gen_metadata VALUES (?, ?)").run(
|
||||
0,
|
||||
new Uint8Array(
|
||||
protoBytes(1, [
|
||||
...protoText(19, "Gemini 3 Pro"),
|
||||
...protoBytes(4, [
|
||||
...protoNumber(2, 50),
|
||||
...protoNumber(5, 20),
|
||||
...protoText(11, "response"),
|
||||
...protoText(12, "provider"),
|
||||
]),
|
||||
]),
|
||||
),
|
||||
);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
const result = await readAntigravityUsage(dir, 0);
|
||||
assert.deepStrictEqual(result.errors, []);
|
||||
const records = result.files.flatMap((file) => file.records);
|
||||
assert.strictEqual(records.length, 1);
|
||||
assert.deepStrictEqual(records[0]?.totals, {
|
||||
uncachedInputTokens: 100,
|
||||
cachedInputTokens: 20,
|
||||
cacheCreationTokens: 0,
|
||||
outputTokens: 40,
|
||||
reasoningTokens: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("merges Antigravity provider and message aliases across configured roots while keeping original ownership", async () => {
|
||||
const roots = [NodePath.join(dir, "first"), NodePath.join(dir, "second")];
|
||||
for (const [index, root] of roots.entries()) {
|
||||
await NodeFSP.mkdir(root);
|
||||
const db = new NodeSqlite.DatabaseSync(NodePath.join(root, `session-${index}.db`));
|
||||
try {
|
||||
db.exec("CREATE TABLE steps (idx INTEGER, metadata BLOB)");
|
||||
for (const identity of [7, 12]) {
|
||||
const usage = [
|
||||
...protoNumber(1, 246),
|
||||
...protoNumber(2, index === 0 ? 100 : 150),
|
||||
...protoText(11, `response-${index}-${identity}`),
|
||||
...protoText(identity, `shared-${identity}`),
|
||||
];
|
||||
db.prepare("INSERT INTO steps VALUES (?, ?)").run(
|
||||
identity,
|
||||
new Uint8Array(protoBytes(9, usage)),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
const result = await readAntigravityUsage(roots, 0);
|
||||
assert.deepStrictEqual(result.errors, []);
|
||||
assert.strictEqual(result.files.length, 2);
|
||||
assert.strictEqual(result.files[0]?.root, roots[0]);
|
||||
assert.strictEqual(result.files[0]?.records.length, 2);
|
||||
assert.strictEqual(result.files[1]?.records.length, 0);
|
||||
assert.deepStrictEqual(
|
||||
result.files[0]?.records.map((record) => record.totals.uncachedInputTokens),
|
||||
[150, 150],
|
||||
);
|
||||
assert.isTrue(result.files[0]?.records.every((record) => record.sessionId === "session-0"));
|
||||
});
|
||||
|
||||
it("upgrades Antigravity fallback timestamps before applying the date window", async () => {
|
||||
for (const fallback of ["mtime", "trajectory"]) {
|
||||
const path = NodePath.join(dir, `${fallback}.db`);
|
||||
const db = new NodeSqlite.DatabaseSync(path);
|
||||
try {
|
||||
db.exec(
|
||||
"CREATE TABLE gen_metadata (idx INTEGER, data BLOB); CREATE TABLE steps (idx INTEGER, metadata BLOB)",
|
||||
);
|
||||
if (fallback === "trajectory") {
|
||||
db.exec("CREATE TABLE trajectory_metadata_blob (data BLOB)");
|
||||
db.prepare("INSERT INTO trajectory_metadata_blob VALUES (?)").run(
|
||||
new Uint8Array(protoBytes(2, protoNumber(1, 1780000200))),
|
||||
);
|
||||
}
|
||||
for (const [index, seconds] of [1780000000, 1780000200].entries()) {
|
||||
const usage = [...protoNumber(2, 10), ...protoText(11, `${fallback}-${index}`)];
|
||||
db.prepare("INSERT INTO steps VALUES (?, ?)").run(
|
||||
index,
|
||||
new Uint8Array(protoBytes(9, usage)),
|
||||
);
|
||||
db.prepare("INSERT INTO gen_metadata VALUES (?, ?)").run(
|
||||
index,
|
||||
new Uint8Array(
|
||||
protoBytes(1, [
|
||||
...protoBytes(4, usage),
|
||||
...protoBytes(9, protoBytes(4, protoNumber(1, seconds))),
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
await NodeFSP.utimes(path, 1780000000, 1780000000);
|
||||
}
|
||||
const result = await readAntigravityUsage(dir, 1780000100000);
|
||||
assert.deepStrictEqual(result.errors, []);
|
||||
const records = result.files.flatMap((file) => file.records);
|
||||
assert.strictEqual(records.length, 2);
|
||||
assert.deepStrictEqual(
|
||||
records.map((record) => record.timestampMs),
|
||||
[1780000200000, 1780000200000],
|
||||
);
|
||||
});
|
||||
|
||||
it("reads Antigravity step-only stores and reports malformed databases", async () => {
|
||||
const db = new NodeSqlite.DatabaseSync(NodePath.join(dir, "steps.db"));
|
||||
try {
|
||||
db.exec("CREATE TABLE steps (idx INTEGER, metadata BLOB)");
|
||||
const usage = [...protoNumber(1, 246), ...protoNumber(2, 10), ...protoNumber(3, 5)];
|
||||
db.prepare("INSERT INTO steps VALUES (?, ?)").run(
|
||||
0,
|
||||
new Uint8Array([...protoBytes(9, usage), ...protoBytes(8, protoNumber(1, 1780000000))]),
|
||||
);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
await NodeFSP.writeFile(NodePath.join(dir, "broken.db"), "not a sqlite database");
|
||||
const result = await readAntigravityUsage(dir, 0);
|
||||
assert.strictEqual(result.errors.length, 1);
|
||||
assert.strictEqual(result.files.flatMap((file) => file.records)[0]?.model, "gemini-2.5-pro");
|
||||
assert.strictEqual(result.files.flatMap((file) => file.records)[0]?.totals.outputTokens, 5);
|
||||
});
|
||||
|
||||
it("ignores large values in unused Antigravity protobuf fields", async () => {
|
||||
const db = new NodeSqlite.DatabaseSync(NodePath.join(dir, "large-varint.db"));
|
||||
try {
|
||||
db.exec("CREATE TABLE steps (idx INTEGER, metadata BLOB)");
|
||||
const unusedField = [...protoNumber(99, 0).slice(0, -1), ...Array(9).fill(0xff), 0x01];
|
||||
const usage = [...protoNumber(1, 246), ...protoNumber(2, 10), ...unusedField];
|
||||
db.prepare("INSERT INTO steps VALUES (?, ?)").run(0, new Uint8Array(protoBytes(9, usage)));
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
const result = await readAntigravityUsage(dir, 0);
|
||||
assert.deepStrictEqual(result.errors, []);
|
||||
assert.strictEqual(
|
||||
result.files.flatMap((file) => file.records)[0]?.totals.uncachedInputTokens,
|
||||
10,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -314,7 +314,25 @@ function ProviderSettingsPanelContent(target: ProviderSettingsTarget) {
|
||||
hasServerConfig: environment.serverConfig !== null,
|
||||
}),
|
||||
)?.environmentId;
|
||||
const searchableCursorEnvironmentId = options.find(
|
||||
(environment) =>
|
||||
environment.serverConfig?.environment.platform.os === "darwin" &&
|
||||
isProviderSettingsEnvironmentAvailable({
|
||||
connectionPhase: environment.connection.phase,
|
||||
hasServerConfig: true,
|
||||
}),
|
||||
)?.environmentId;
|
||||
useEffect(() => {
|
||||
if (
|
||||
!target.scoped &&
|
||||
searchTargetId === searchableSetting("cursor-keychain-usage").id &&
|
||||
(!selectedEnvironmentCanRenderSettings ||
|
||||
selectedEnvironment?.serverConfig?.environment.platform.os !== "darwin") &&
|
||||
searchableCursorEnvironmentId !== undefined
|
||||
) {
|
||||
setSelectedEnvironmentId(searchableCursorEnvironmentId);
|
||||
return;
|
||||
}
|
||||
if (
|
||||
!target.scoped &&
|
||||
(searchTargetId === searchableSetting("provider-health-check-interval").id ||
|
||||
@@ -326,7 +344,9 @@ function ProviderSettingsPanelContent(target: ProviderSettingsTarget) {
|
||||
}
|
||||
}, [
|
||||
searchTargetId,
|
||||
searchableCursorEnvironmentId,
|
||||
searchableEnvironmentId,
|
||||
selectedEnvironment,
|
||||
selectedEnvironmentCanRenderSettings,
|
||||
target.scoped,
|
||||
]);
|
||||
@@ -1102,6 +1122,7 @@ export function EnvironmentProviderSettings({
|
||||
environmentId={environmentId}
|
||||
environmentLabel={environmentLabel}
|
||||
sources={settings.usageLimitSources}
|
||||
cursorKeychainUsageEnabled={settings.cursorKeychainUsageEnabled}
|
||||
readOnly={readOnly}
|
||||
/>
|
||||
|
||||
|
||||
@@ -116,7 +116,7 @@ export function SettingsSidebarNav({ pathname }: { pathname: string }) {
|
||||
const searchInputRef = useRef<HTMLInputElement>(null);
|
||||
const [query, setQuery] = useState("");
|
||||
const [activeResultIndex, setActiveResultIndex] = useState(0);
|
||||
const searchableItems = useAvailableSettingsSearchItems();
|
||||
const searchableItems = useAvailableSettingsSearchItems(scopeSearch);
|
||||
const results = useMemo(() => searchSettings(query, searchableItems), [query, searchableItems]);
|
||||
const isSearching = query.trim().length > 0;
|
||||
const hasResults = results.length > 0;
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import type { EnvironmentId, UnifiedSettings } from "@t3tools/contracts";
|
||||
import { useAtomValue } from "@effect/atom-react";
|
||||
import { PlusIcon } from "lucide-react";
|
||||
import { useState } from "react";
|
||||
|
||||
import { useUpdateEnvironmentSettings } from "../../hooks/useSettings";
|
||||
import { serverEnvironment } from "../../state/server";
|
||||
import { useAtomCommand } from "../../state/use-atom-command";
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogClose,
|
||||
@@ -13,6 +16,7 @@ import {
|
||||
AlertDialogTitle,
|
||||
} from "../ui/alert-dialog";
|
||||
import { Button } from "../ui/button";
|
||||
import { Switch } from "../ui/switch";
|
||||
import { AddUsageLimitSourceDialog } from "./AddUsageLimitSourceDialog";
|
||||
import { searchableSetting } from "./settingsSearch";
|
||||
import { SettingsRow, SettingsSection } from "./settingsLayout";
|
||||
@@ -22,17 +26,43 @@ export function UsageProviderSettings({
|
||||
environmentId,
|
||||
environmentLabel,
|
||||
sources,
|
||||
cursorKeychainUsageEnabled,
|
||||
readOnly,
|
||||
}: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly environmentLabel: string;
|
||||
readonly sources: UnifiedSettings["usageLimitSources"];
|
||||
readonly cursorKeychainUsageEnabled: boolean;
|
||||
readonly readOnly: boolean;
|
||||
}) {
|
||||
const updateSettings = useUpdateEnvironmentSettings(environmentId);
|
||||
const updateCursorSettings = useAtomCommand(serverEnvironment.updateSettings, {
|
||||
label: "update Cursor account usage",
|
||||
});
|
||||
const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const platform = useAtomValue(serverEnvironment.configValueAtom(environmentId))?.environment
|
||||
.platform;
|
||||
const [adding, setAdding] = useState(false);
|
||||
const [updatingCursor, setUpdatingCursor] = useState(false);
|
||||
const entries = Object.entries(sources);
|
||||
|
||||
const setCursorUsageEnabled = async (enabled: boolean) => {
|
||||
setUpdatingCursor(true);
|
||||
try {
|
||||
const result = await updateCursorSettings({
|
||||
environmentId,
|
||||
input: { patch: { cursorKeychainUsageEnabled: enabled } },
|
||||
});
|
||||
if (result._tag === "Success") {
|
||||
await refreshProviders({ environmentId, input: {} });
|
||||
}
|
||||
} finally {
|
||||
setUpdatingCursor(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<SettingsSection
|
||||
@@ -46,8 +76,23 @@ export function UsageProviderSettings({
|
||||
) : null
|
||||
}
|
||||
>
|
||||
{platform?.os === "darwin" ? (
|
||||
<SettingsRow
|
||||
id="cursor-keychain-usage"
|
||||
title="Cursor account usage"
|
||||
description="Read your existing Cursor CLI login from macOS Keychain to show account history and monthly limits. macOS may ask you to allow access."
|
||||
control={
|
||||
<Switch
|
||||
aria-label="Cursor account usage"
|
||||
checked={cursorKeychainUsageEnabled}
|
||||
disabled={readOnly || updatingCursor}
|
||||
onCheckedChange={(enabled) => void setCursorUsageEnabled(enabled)}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
) : null}
|
||||
{entries.length === 0 ? (
|
||||
<SettingsRow title="No usage providers configured." />
|
||||
<SettingsRow title="No hubs configured." />
|
||||
) : (
|
||||
entries.map(([id, source]) => {
|
||||
const label = source.label?.trim() || source.url;
|
||||
|
||||
@@ -154,6 +154,7 @@ describe("searchSettings", () => {
|
||||
hasCloudPublicConfig: false,
|
||||
hasEnvironment: false,
|
||||
hasProviderSettingsEnvironment: false,
|
||||
hasMacProviderSettingsEnvironment: false,
|
||||
canManageLocalBackend: false,
|
||||
isWslSettingsRowVisible: false,
|
||||
hasThreadAutoSettlement: false,
|
||||
@@ -165,6 +166,7 @@ describe("searchSettings", () => {
|
||||
"network-access",
|
||||
"publish-agent-activity",
|
||||
"provider-health-check-interval",
|
||||
"cursor-keychain-usage",
|
||||
"source-control-writer-model",
|
||||
"source-control-writing-style",
|
||||
"t3-connect",
|
||||
@@ -177,11 +179,31 @@ describe("searchSettings", () => {
|
||||
expect(available.map((item) => item.id).filter((id) => gatedIds.has(id))).toEqual([]);
|
||||
});
|
||||
|
||||
it("offers Cursor Keychain settings only when a macOS provider environment is available", () => {
|
||||
const availability = {
|
||||
hasCloudPublicConfig: false,
|
||||
hasEnvironment: true,
|
||||
hasProviderSettingsEnvironment: true,
|
||||
hasMacProviderSettingsEnvironment: false,
|
||||
canManageLocalBackend: false,
|
||||
isWslSettingsRowVisible: false,
|
||||
hasThreadAutoSettlement: false,
|
||||
};
|
||||
const itemIds = (macAvailable: boolean) =>
|
||||
filterAvailableSettingsSearchItems({
|
||||
...availability,
|
||||
hasMacProviderSettingsEnvironment: macAvailable,
|
||||
}).map((item) => item.id);
|
||||
expect(itemIds(false)).not.toContain("cursor-keychain-usage");
|
||||
expect(itemIds(true)).toContain("cursor-keychain-usage");
|
||||
});
|
||||
|
||||
it("keeps the local toggle searchable without offering hidden host publishing controls", () => {
|
||||
const availability = {
|
||||
hasCloudPublicConfig: true,
|
||||
hasEnvironment: true,
|
||||
hasProviderSettingsEnvironment: true,
|
||||
hasMacProviderSettingsEnvironment: false,
|
||||
canManageLocalBackend: false,
|
||||
isWslSettingsRowVisible: false,
|
||||
hasThreadAutoSettlement: false,
|
||||
@@ -204,6 +226,7 @@ describe("searchSettings", () => {
|
||||
hasCloudPublicConfig: false,
|
||||
hasEnvironment: false,
|
||||
hasProviderSettingsEnvironment: false,
|
||||
hasMacProviderSettingsEnvironment: false,
|
||||
canManageLocalBackend: false,
|
||||
isWslSettingsRowVisible: false,
|
||||
hasThreadAutoSettlement: true,
|
||||
@@ -330,6 +353,7 @@ describe("searchSettings", () => {
|
||||
hasCloudPublicConfig: false,
|
||||
hasEnvironment: true,
|
||||
hasProviderSettingsEnvironment: true,
|
||||
hasMacProviderSettingsEnvironment: false,
|
||||
canManageLocalBackend: false,
|
||||
isWslSettingsRowVisible: false,
|
||||
hasThreadAutoSettlement: true,
|
||||
@@ -424,6 +448,7 @@ describe("auto-settlement search availability", () => {
|
||||
hasCloudPublicConfig: false,
|
||||
hasEnvironment: true,
|
||||
hasProviderSettingsEnvironment: true,
|
||||
hasMacProviderSettingsEnvironment: false,
|
||||
canManageLocalBackend: false,
|
||||
isWslSettingsRowVisible: false,
|
||||
hasThreadAutoSettlement: availability.eligibleEnvironmentIds.length > 0,
|
||||
|
||||
@@ -55,6 +55,7 @@ export interface SettingsSearchItem {
|
||||
readonly cloudOnly?: boolean;
|
||||
readonly environmentOnly?: boolean;
|
||||
readonly providerSettingsOnly?: boolean;
|
||||
readonly macProviderSettingsOnly?: boolean;
|
||||
readonly localBackendManagementOnly?: boolean;
|
||||
readonly localEnvironmentOnly?: boolean;
|
||||
readonly wslAvailableOnly?: boolean;
|
||||
@@ -71,6 +72,7 @@ export interface SettingsSearchAvailability {
|
||||
readonly hasCloudPublicConfig: boolean;
|
||||
readonly hasEnvironment: boolean;
|
||||
readonly hasProviderSettingsEnvironment: boolean;
|
||||
readonly hasMacProviderSettingsEnvironment: boolean;
|
||||
readonly canManageLocalBackend: boolean;
|
||||
readonly isWslSettingsRowVisible: boolean;
|
||||
readonly hasThreadAutoSettlement: boolean;
|
||||
@@ -546,6 +548,14 @@ export const SETTINGS_SEARCH_ITEMS = [
|
||||
],
|
||||
providerSettingsOnly: true,
|
||||
},
|
||||
{
|
||||
id: "cursor-keychain-usage",
|
||||
title: "Cursor account usage",
|
||||
to: "/settings/providers",
|
||||
searchTerms: ["cursor macOS keychain usage tokens cost limits permission"],
|
||||
providerSettingsOnly: true,
|
||||
macProviderSettingsOnly: true,
|
||||
},
|
||||
{
|
||||
id: "provider-health-check-interval",
|
||||
title: "Health check interval",
|
||||
@@ -943,6 +953,7 @@ export function filterAvailableSettingsSearchItems(
|
||||
(!item.cloudOnly || availability.hasCloudPublicConfig) &&
|
||||
(!item.environmentOnly || availability.hasEnvironment) &&
|
||||
(!item.providerSettingsOnly || availability.hasProviderSettingsEnvironment) &&
|
||||
(!item.macProviderSettingsOnly || availability.hasMacProviderSettingsEnvironment) &&
|
||||
(!item.localBackendManagementOnly || availability.canManageLocalBackend) &&
|
||||
(!item.localEnvironmentOnly || !availability.localEnvironmentDisabled) &&
|
||||
(!item.wslAvailableOnly || availability.isWslSettingsRowVisible) &&
|
||||
|
||||
@@ -10,12 +10,13 @@ import { useEnvironmentQuery } from "~/state/query";
|
||||
import { usePrimarySessionState } from "~/environments/primary";
|
||||
import { isWslSettingsRowVisible } from "./ConnectionsSettings.logic";
|
||||
import { isProviderSettingsEnvironmentAvailable } from "./ProviderSettingsPanel.logic";
|
||||
import type { SettingsScopeSearch } from "./settingsScope";
|
||||
import {
|
||||
filterAvailableSettingsSearchItems,
|
||||
getThreadAutoSettlementSearchAvailability,
|
||||
} from "./settingsSearch";
|
||||
|
||||
export function useAvailableSettingsSearchItems() {
|
||||
export function useAvailableSettingsSearchItems(scopeSearch: SettingsScopeSearch = {}) {
|
||||
const { environments } = useEnvironments();
|
||||
const primarySessionState = usePrimarySessionState();
|
||||
const localEnvironmentDisabled = isLocalEnvironmentDisabled();
|
||||
@@ -41,6 +42,16 @@ export function useAvailableSettingsSearchItems() {
|
||||
hasServerConfig: environment.serverConfig !== null,
|
||||
}),
|
||||
),
|
||||
hasMacProviderSettingsEnvironment: environments.some(
|
||||
(environment) =>
|
||||
(scopeSearch.machine === undefined ||
|
||||
environment.environmentId === scopeSearch.machine) &&
|
||||
environment.serverConfig?.environment.platform.os === "darwin" &&
|
||||
isProviderSettingsEnvironmentAvailable({
|
||||
connectionPhase: environment.connection.phase,
|
||||
hasServerConfig: true,
|
||||
}),
|
||||
),
|
||||
canManageLocalBackend,
|
||||
isWslSettingsRowVisible: isWslSettingsRowVisible({
|
||||
state: desktopWsl.data,
|
||||
@@ -55,6 +66,7 @@ export function useAvailableSettingsSearchItems() {
|
||||
desktopWsl.error,
|
||||
environments,
|
||||
localEnvironmentDisabled,
|
||||
scopeSearch.machine,
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
remainingPercent,
|
||||
} from "@t3tools/shared/usageLimits";
|
||||
import { GaugeIcon, TrendingDownIcon, TrendingUpIcon } from "lucide-react";
|
||||
import { Fragment, useState } from "react";
|
||||
import { Fragment, type ReactNode, useState } from "react";
|
||||
|
||||
import { usePrimarySettings } from "../../hooks/useSettings";
|
||||
import { environmentPresentations } from "../../state/presentation";
|
||||
@@ -322,14 +322,16 @@ export function ResetCredits({
|
||||
export function UsageLimitsSection({
|
||||
selectedEnvironmentIds,
|
||||
now,
|
||||
cursorPrompt,
|
||||
}: {
|
||||
readonly selectedEnvironmentIds: ReadonlySet<EnvironmentId> | null;
|
||||
readonly now: number;
|
||||
readonly cursorPrompt?: ReactNode;
|
||||
}) {
|
||||
const presentations = useAtomValue(environmentPresentations.presentationsAtom);
|
||||
const selected =
|
||||
selectedEnvironmentIds === null
|
||||
? presentations
|
||||
: new Map([...presentations].filter(([id]) => selectedEnvironmentIds.has(id)));
|
||||
return <UsageLimitsPooled presentations={selected} now={now} />;
|
||||
return <UsageLimitsPooled presentations={selected} now={now} cursorPrompt={cursorPrompt} />;
|
||||
}
|
||||
@@ -2,7 +2,8 @@ import {
|
||||
collectLimitAccounts,
|
||||
collectLimitNotices,
|
||||
collectLimitPools,
|
||||
formatDuration,
|
||||
cursorUsageWindowDetails,
|
||||
displayLimitWindows,
|
||||
formatResetsIn,
|
||||
type LimitAccount,
|
||||
type LimitPool,
|
||||
@@ -11,7 +12,7 @@ import {
|
||||
remainingPercent,
|
||||
} from "@t3tools/shared/usageLimits";
|
||||
import { AlertTriangleIcon, TicketIcon } from "lucide-react";
|
||||
import { type ReactNode, useState } from "react";
|
||||
import { Fragment, type ReactNode, useState } from "react";
|
||||
|
||||
import { usePrimarySettings } from "../../hooks/useSettings";
|
||||
import { cn } from "../../lib/utils";
|
||||
@@ -224,6 +225,7 @@ function PoolSegment({
|
||||
color,
|
||||
now,
|
||||
index,
|
||||
showAccountName,
|
||||
}: {
|
||||
readonly account: LimitAccount;
|
||||
readonly window: LimitPoolMember["window"];
|
||||
@@ -232,6 +234,7 @@ function PoolSegment({
|
||||
readonly now: number;
|
||||
/** 1-based position in the bar, shown on the strip and its legend row to tie them together. */
|
||||
readonly index: number;
|
||||
readonly showAccountName: boolean;
|
||||
}) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const remaining = remainingPercent(window);
|
||||
@@ -274,7 +277,12 @@ function PoolSegment({
|
||||
{index}
|
||||
</span>
|
||||
<div className="relative hidden h-full min-w-0 items-center gap-1.5 px-2 text-xs @2xl/pool:flex">
|
||||
<AccountName account={account} className="min-w-0 truncate font-medium text-foreground" />
|
||||
{showAccountName ? (
|
||||
<AccountName
|
||||
account={account}
|
||||
className="min-w-0 truncate font-medium text-foreground"
|
||||
/>
|
||||
) : null}
|
||||
<span className="shrink-0 font-semibold text-foreground tabular-nums">{remaining}%</span>
|
||||
{/* Countdown and badge get their own plate: fill and hatching run under them otherwise. */}
|
||||
<span className="ms-auto flex shrink-0 items-center gap-1.5 rounded-sm bg-background/85 px-1.5 py-0.5 text-2xs text-foreground tabular-nums">
|
||||
@@ -463,6 +471,7 @@ function PoolBar({
|
||||
color={color}
|
||||
now={now}
|
||||
index={position + 1}
|
||||
showAccountName={pool.columns.length > 1}
|
||||
/>
|
||||
) : null,
|
||||
)}
|
||||
@@ -479,17 +488,21 @@ function PoolWindowCard({
|
||||
pool,
|
||||
color,
|
||||
now,
|
||||
label,
|
||||
description,
|
||||
}: {
|
||||
readonly pool: LimitPoolWindow;
|
||||
readonly color: string;
|
||||
readonly now: number;
|
||||
readonly label?: string | undefined;
|
||||
readonly description?: string | undefined;
|
||||
}) {
|
||||
// The soonest reset that hands anything back; an untouched account resets to no effect.
|
||||
const nextRefill = pool.resets.find((reset) => reset.restoresPercent > 0);
|
||||
return (
|
||||
<div className="grid items-center gap-x-6 gap-y-3 rounded-lg border border-border/60 p-4 md:grid-cols-[11rem_minmax(0,1fr)]">
|
||||
<div className="flex flex-col gap-1">
|
||||
<span className="text-sm font-medium text-foreground">{pool.label}</span>
|
||||
<span className="text-sm font-medium text-foreground">{label ?? pool.label}</span>
|
||||
<span className="flex items-baseline gap-2">
|
||||
<span className="text-3xl font-semibold text-foreground tabular-nums">
|
||||
{pool.remainingPercent}%
|
||||
@@ -497,14 +510,16 @@ function PoolWindowCard({
|
||||
<span className="text-sm text-muted-foreground">left</span>
|
||||
{pool.pace ? <PaceIcon pace={pool.pace} /> : null}
|
||||
</span>
|
||||
{nextRefill ? (
|
||||
<span className="text-xs text-muted-foreground tabular-nums">
|
||||
<span className="font-medium text-foreground">↻ +{nextRefill.restoresPercent}%</span>{" "}
|
||||
{nextRefill.at <= now ? "now" : `in ${formatDuration(nextRefill.at - now)}`}
|
||||
{nextRefill && pool.columns.length > 1 ? (
|
||||
<span className="text-xs font-medium text-foreground tabular-nums">
|
||||
↻ +{nextRefill.restoresPercent}%
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
<PoolBar pool={pool} color={color} now={now} />
|
||||
{description ? (
|
||||
<p className="text-xs text-muted-foreground md:col-span-2">{description}</p>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -512,6 +527,7 @@ function PoolWindowCard({
|
||||
function PoolSection({ pool, now }: { readonly pool: LimitPool; readonly now: number }) {
|
||||
const color = barColor(pool.driver);
|
||||
const label = getDriverOption(pool.driver)?.label ?? String(pool.driver);
|
||||
const windows = displayLimitWindows(pool);
|
||||
return (
|
||||
<section className="flex flex-col gap-3">
|
||||
<h2 className="flex items-center gap-2 text-sm font-medium text-foreground">
|
||||
@@ -524,9 +540,19 @@ function PoolSection({ pool, now }: { readonly pool: LimitPool; readonly now: nu
|
||||
/>
|
||||
{label}
|
||||
</h2>
|
||||
{pool.windows.map((window) => (
|
||||
<PoolWindowCard key={`${window.kind}:${window.id}`} pool={window} color={color} now={now} />
|
||||
))}
|
||||
{windows.map((window) => {
|
||||
const details = pool.driver === "cursor" ? cursorUsageWindowDetails(window.id) : undefined;
|
||||
return (
|
||||
<PoolWindowCard
|
||||
key={`${window.kind}:${window.id}`}
|
||||
pool={window}
|
||||
color={color}
|
||||
now={now}
|
||||
label={details?.label}
|
||||
description={details?.description}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -539,22 +565,33 @@ function PoolSection({ pool, now }: { readonly pool: LimitPool; readonly now: nu
|
||||
export function UsageLimitsPooled({
|
||||
presentations,
|
||||
now,
|
||||
cursorPrompt,
|
||||
}: {
|
||||
readonly presentations: Parameters<typeof collectLimitAccounts>[0];
|
||||
readonly now: number;
|
||||
readonly cursorPrompt?: ReactNode;
|
||||
}) {
|
||||
const pools = collectLimitPools(collectLimitAccounts(presentations), now);
|
||||
const notices = collectLimitNotices(presentations);
|
||||
const cursorPromptAt =
|
||||
Math.max(
|
||||
pools.findIndex((pool) => pool.driver === "codex"),
|
||||
pools.findIndex((pool) => pool.driver === "claudeAgent"),
|
||||
) + 1;
|
||||
return (
|
||||
<div className="flex flex-col gap-8">
|
||||
{pools.length === 0 && notices.length === 0 ? (
|
||||
{pools.length === 0 && notices.length === 0 && !cursorPrompt ? (
|
||||
<p className="text-sm text-muted-foreground">
|
||||
No provider on the selected environments reports subscription limits.
|
||||
</p>
|
||||
) : null}
|
||||
{pools.map((pool) => (
|
||||
<PoolSection key={pool.driver} pool={pool} now={now} />
|
||||
{pools.map((pool, index) => (
|
||||
<Fragment key={pool.driver}>
|
||||
{index === cursorPromptAt ? cursorPrompt : null}
|
||||
<PoolSection pool={pool} now={now} />
|
||||
</Fragment>
|
||||
))}
|
||||
{cursorPromptAt === pools.length ? cursorPrompt : null}
|
||||
<LimitNotices notices={notices} />
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { RefreshIcon } from "~/components/ui/refresh-icon";
|
||||
import { useAtomValue } from "@effect/atom-react";
|
||||
import {
|
||||
ProviderDriverKind,
|
||||
USAGE_CONTRACT_VERSION,
|
||||
type EnvironmentId,
|
||||
type UsageProviderKind,
|
||||
@@ -40,6 +41,7 @@ import {
|
||||
makeWindow,
|
||||
} from "@t3tools/shared/usageFormat";
|
||||
import { Button, InlineButton } from "../ui/button";
|
||||
import { ProviderInstanceIcon } from "../chat/ProviderInstanceIcon";
|
||||
import {
|
||||
Menu,
|
||||
MenuCheckboxItem,
|
||||
@@ -53,6 +55,7 @@ import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../
|
||||
import { SidebarInset } from "../ui/sidebar";
|
||||
import { Skeleton } from "../ui/skeleton";
|
||||
import { Toggle, ToggleGroup } from "../ui/toggle-group";
|
||||
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
|
||||
import {
|
||||
WorkspaceBreadcrumb,
|
||||
WorkspaceBreadcrumbItem,
|
||||
@@ -118,6 +121,25 @@ export function UsagePage() {
|
||||
selectedEnvironmentIds,
|
||||
);
|
||||
const presentations = useAtomValue(environmentPresentations.presentationsAtom);
|
||||
const cursorAccessEnvironments = selectedEnvironments.filter((environment) =>
|
||||
environment.summary?.sources.some((source) => source.action === "enableCursorKeychain"),
|
||||
);
|
||||
const sourceMessages = [
|
||||
...new Set(
|
||||
selectedEnvironments.flatMap(
|
||||
(environment) =>
|
||||
environment.summary?.sources.flatMap((source) =>
|
||||
source.message &&
|
||||
!source.action &&
|
||||
(source.status === "partial" ||
|
||||
source.status === "failed" ||
|
||||
source.fingerprint.provider === "cursor")
|
||||
? [source.message]
|
||||
: [],
|
||||
) ?? [],
|
||||
),
|
||||
),
|
||||
];
|
||||
const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders, {
|
||||
reportFailure: false,
|
||||
});
|
||||
@@ -147,6 +169,17 @@ export function UsagePage() {
|
||||
[breakdown, merged.models, metric],
|
||||
);
|
||||
const activeProviders = useMemo(() => providersWithUsage(merged.providers), [merged.providers]);
|
||||
const summaryRows: Array<
|
||||
| { readonly kind: "usage"; readonly provider: UsageProviderKind }
|
||||
| { readonly kind: "enable"; readonly environment: EnvironmentUsageStatus }
|
||||
> = activeProviders.map((provider) => ({ kind: "usage", provider }));
|
||||
const cursorInsertAt =
|
||||
Math.max(activeProviders.indexOf("codex"), activeProviders.indexOf("claude")) + 1;
|
||||
summaryRows.splice(
|
||||
cursorInsertAt,
|
||||
0,
|
||||
...cursorAccessEnvironments.map((environment) => ({ kind: "enable" as const, environment })),
|
||||
);
|
||||
const timeValueColumnWidth = `${60 / (activeProviders.length + 2)}%`;
|
||||
|
||||
const selectWindow = (days: number) => {
|
||||
@@ -165,7 +198,7 @@ export function UsagePage() {
|
||||
setPreferences(nextPreferences);
|
||||
saveUsagePagePreferences(nextPreferences);
|
||||
};
|
||||
const refreshLimits = async (automatic = false) => {
|
||||
const refreshLimits = async (automatic = false, afterPending = false) => {
|
||||
try {
|
||||
await Promise.all(
|
||||
Array.from(presentations, ([environmentId, presentation]) => {
|
||||
@@ -175,6 +208,7 @@ export function UsagePage() {
|
||||
environmentId,
|
||||
() => refreshProviders({ environmentId, input: {} }),
|
||||
automatic,
|
||||
afterPending,
|
||||
);
|
||||
}
|
||||
}),
|
||||
@@ -380,11 +414,30 @@ export function UsagePage() {
|
||||
: `Select an environment to see ${showingLimits ? "limits" : "usage"}.`}
|
||||
</p>
|
||||
) : showingLimits ? (
|
||||
<UsageLimitsSection selectedEnvironmentIds={selectedEnvironmentIds} now={limitsNow} />
|
||||
<UsageLimitsSection
|
||||
selectedEnvironmentIds={selectedEnvironmentIds}
|
||||
now={limitsNow}
|
||||
cursorPrompt={
|
||||
cursorAccessEnvironments.length > 0 ? (
|
||||
<CursorEnableLimits
|
||||
environments={cursorAccessEnvironments}
|
||||
onEnabled={() => {
|
||||
void refresh();
|
||||
void refreshLimits(false, true);
|
||||
}}
|
||||
/>
|
||||
) : null
|
||||
}
|
||||
/>
|
||||
) : isPending ? (
|
||||
<UsageSkeleton />
|
||||
) : (
|
||||
<>
|
||||
{sourceMessages.map((message) => (
|
||||
<p key={message} className="mb-4 text-sm text-muted-foreground">
|
||||
{message}
|
||||
</p>
|
||||
))}
|
||||
<section className="grid gap-6 lg:grid-cols-[minmax(0,18rem)_minmax(0,1fr)]">
|
||||
<div className="flex min-w-0 flex-col gap-5">
|
||||
<div className="flex flex-col gap-1">
|
||||
@@ -404,7 +457,22 @@ export function UsagePage() {
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{activeProviders.map((provider) => {
|
||||
{summaryRows.map((row) => {
|
||||
if (row.kind === "enable") {
|
||||
return (
|
||||
<CursorEnableRow
|
||||
key={`enable:${row.environment.environmentId}`}
|
||||
environmentId={row.environment.environmentId}
|
||||
label={row.environment.label}
|
||||
showEnvironment={selectedEnvironments.length > 1}
|
||||
onEnabled={() => {
|
||||
void refresh();
|
||||
void refreshLimits(false, true);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
const provider = row.provider;
|
||||
const totals = merged.providers.find((entry) => entry.provider === provider);
|
||||
const share =
|
||||
metric === "cost" ? (totals?.costShare ?? 0) : (totals?.tokenShare ?? 0);
|
||||
@@ -636,6 +704,137 @@ export function UsagePage() {
|
||||
);
|
||||
}
|
||||
|
||||
const CURSOR_KEYCHAIN_COPY = "Requires access to your Cursor login in macOS Keychain.";
|
||||
|
||||
function CursorEnableButton({
|
||||
environmentId,
|
||||
label,
|
||||
onEnabled,
|
||||
tooltip,
|
||||
buttonText = "Enable",
|
||||
}: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly label: string;
|
||||
readonly onEnabled: () => void;
|
||||
readonly tooltip: boolean;
|
||||
readonly buttonText?: string;
|
||||
}) {
|
||||
const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
|
||||
label: "enable Cursor account usage",
|
||||
});
|
||||
const [pending, setPending] = useState(false);
|
||||
const enable = async () => {
|
||||
setPending(true);
|
||||
try {
|
||||
const result = await updateSettings({
|
||||
environmentId,
|
||||
input: { patch: { cursorKeychainUsageEnabled: true } },
|
||||
});
|
||||
if (result._tag === "Success") onEnabled();
|
||||
} finally {
|
||||
setPending(false);
|
||||
}
|
||||
};
|
||||
const button = tooltip ? (
|
||||
<InlineButton
|
||||
disabled={pending}
|
||||
aria-busy={pending}
|
||||
aria-label={`Enable Cursor usage from ${label}`}
|
||||
onClick={() => void enable()}
|
||||
>
|
||||
{buttonText}
|
||||
</InlineButton>
|
||||
) : (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
disabled={pending}
|
||||
aria-busy={pending}
|
||||
aria-label={`Enable Cursor usage from ${label}`}
|
||||
onClick={() => void enable()}
|
||||
>
|
||||
{buttonText}
|
||||
</Button>
|
||||
);
|
||||
if (!tooltip) return button;
|
||||
return (
|
||||
<Tooltip>
|
||||
<TooltipTrigger render={button} />
|
||||
<TooltipPopup>{CURSOR_KEYCHAIN_COPY}</TooltipPopup>
|
||||
</Tooltip>
|
||||
);
|
||||
}
|
||||
|
||||
function CursorEnableRow({
|
||||
environmentId,
|
||||
label,
|
||||
showEnvironment,
|
||||
onEnabled,
|
||||
}: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly label: string;
|
||||
readonly showEnvironment: boolean;
|
||||
readonly onEnabled: () => void;
|
||||
}) {
|
||||
return (
|
||||
<div className="flex min-w-0 items-baseline justify-between gap-4 text-sm">
|
||||
<span className="flex min-w-0 items-center gap-2 text-sm text-foreground">
|
||||
<span
|
||||
aria-hidden
|
||||
className="size-2 shrink-0 rounded-full"
|
||||
style={{ backgroundColor: PROVIDER_PRESENTATION.cursor.color }}
|
||||
/>
|
||||
<ProviderMark provider="cursor" className="size-4" />
|
||||
<span className="truncate">Cursor{showEnvironment ? ` · ${label}` : ""}</span>
|
||||
</span>
|
||||
<CursorEnableButton
|
||||
environmentId={environmentId}
|
||||
label={label}
|
||||
onEnabled={onEnabled}
|
||||
tooltip
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function CursorEnableLimits({
|
||||
environments,
|
||||
onEnabled,
|
||||
}: {
|
||||
readonly environments: readonly EnvironmentUsageStatus[];
|
||||
readonly onEnabled: () => void;
|
||||
}) {
|
||||
return (
|
||||
<section className="flex flex-col gap-3">
|
||||
<h2 className="flex items-center gap-2 text-sm font-medium text-foreground">
|
||||
<ProviderInstanceIcon
|
||||
driverKind={ProviderDriverKind.make("cursor")}
|
||||
displayName="Cursor"
|
||||
indicatorBackground="var(--background)"
|
||||
className="size-5"
|
||||
iconClassName="size-4 text-foreground/80"
|
||||
/>
|
||||
Cursor
|
||||
</h2>
|
||||
<div className="flex flex-col items-start gap-3 rounded-lg border border-border/60 p-4">
|
||||
<p className="text-xs text-muted-foreground">{CURSOR_KEYCHAIN_COPY}</p>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{environments.map((environment) => (
|
||||
<CursorEnableButton
|
||||
key={environment.environmentId}
|
||||
environmentId={environment.environmentId}
|
||||
label={environment.label}
|
||||
buttonText={environments.length > 1 ? `Enable on ${environment.label}` : "Enable"}
|
||||
onEnabled={onEnabled}
|
||||
tooltip={false}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
/** Brand mark for the harness a row belongs to. */
|
||||
function ProviderMark({
|
||||
provider,
|
||||
|
||||
@@ -89,6 +89,9 @@ describe("buildPeriodColumns", () => {
|
||||
{ provider: "codex", value: 10 },
|
||||
{ provider: "claude", value: 20 },
|
||||
{ provider: "grok", value: 0 },
|
||||
{ provider: "cursor", value: 0 },
|
||||
{ provider: "opencode", value: 0 },
|
||||
{ provider: "antigravity", value: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import type { UsageProviderKind } from "@t3tools/contracts";
|
||||
|
||||
import { ClaudeAI, GrokIcon, type Icon, OpenAI } from "../Icons";
|
||||
import {
|
||||
AntigravityIcon,
|
||||
ClaudeAI,
|
||||
CursorIcon,
|
||||
GrokIcon,
|
||||
type Icon,
|
||||
OpenAI,
|
||||
OpenCodeIcon,
|
||||
} from "../Icons";
|
||||
|
||||
type UsageProviderPresentation = {
|
||||
readonly label: string;
|
||||
@@ -30,6 +38,9 @@ export const PROVIDER_PRESENTATION = {
|
||||
color: "color-mix(in oklab, var(--contrast-foreground) 72%, var(--background))",
|
||||
mark: GrokIcon,
|
||||
},
|
||||
cursor: { label: "Cursor", color: "#8b8b8b", mark: CursorIcon },
|
||||
opencode: { label: "OpenCode", color: "#5b9bbd", mark: OpenCodeIcon },
|
||||
antigravity: { label: "Antigravity", color: "#8c7bd1", mark: AntigravityIcon },
|
||||
} satisfies Record<UsageProviderKind, UsageProviderPresentation>;
|
||||
|
||||
/** Stable provider reading order across charts, summaries, tables, and hover rows. */
|
||||
|
||||
+17
-5
@@ -6,13 +6,25 @@ desktop when the terminal is not focused. Customize `usage.open` in
|
||||
|
||||
## Understand your usage
|
||||
|
||||
**Usage** combines Codex, Claude Code, and Grok Build session history from your connected
|
||||
**Usage** combines Codex, Claude Code, Grok Build, OpenCode, Antigravity, and Cursor history from your connected
|
||||
environments. It shows token use, cache savings, model breakdowns, and estimated API-equivalent
|
||||
cost. These estimates are not your subscription bill.
|
||||
|
||||
Totals depend on the history available on each server. Grok turns without a saved completed-turn
|
||||
record are missing from the totals.
|
||||
|
||||
OpenCode reads its SQLite database and older JSON history. Antigravity reads local conversation
|
||||
databases, including T3-managed profiles. Set `OPENCODE_DATA_DIR` or `ANTIGRAVITY_DATA_DIR` on the
|
||||
server to read a different data directory; comma-separated paths read multiple directories.
|
||||
|
||||
Cursor reads account usage from Cursor's dashboard API using the CLI login saved on the server.
|
||||
This includes headless T3 sessions and desktop usage across machines; the same account counts
|
||||
once across connected environments. Without an accessible CLI login, T3 shows a
|
||||
notice instead of incomplete local totals. T3 does not estimate missing tokens from conversation text.
|
||||
On macOS, choose **Enable Cursor usage** on Usage to allow T3 to read your existing CLI login
|
||||
from Keychain. You can turn it off in **Settings → Providers → Usage providers**. macOS may ask
|
||||
you to allow access on the server Mac.
|
||||
|
||||
Usage includes each configured account's history, including disabled accounts. Custom homes follow
|
||||
the account's home setting or its `CODEX_HOME`, `CLAUDE_CONFIG_DIR`, or `GROK_HOME` environment
|
||||
variable. Use absolute paths or `~/` paths in the account's environment settings; relative
|
||||
@@ -78,10 +90,10 @@ anything. The command is offered only for providers that appear under **Usage
|
||||
OpenCode Go reports its session, weekly, and monthly allowance when OpenCode runs locally in
|
||||
the environment. T3 cannot report limits for external OpenCode servers because their credentials
|
||||
belong to the remote server. Cursor reports
|
||||
its monthly allowance, including separate Auto and API usage, using a file-based CLI login or
|
||||
`CURSOR_AUTH_TOKEN`. Cursor's default macOS keychain login does not currently report limits.
|
||||
On macOS, use `AGENT_CLI_CREDENTIAL_STORE=file` when signing in and in the provider's environment
|
||||
to use a file-based login.
|
||||
its monthly allowance, including separate Auto and API usage, using the CLI login or
|
||||
`CURSOR_AUTH_TOKEN`. On macOS, this includes the default Keychain login after you enable Cursor
|
||||
usage. Keychain login is used for limits only with Cursor's default API endpoint. If you configure
|
||||
a custom Cursor endpoint, use an explicit token or file-based CLI login for limits.
|
||||
|
||||
Grok reports the remaining subscription allowance and reset time for its current billing period
|
||||
after signing in with `grok login`. Explicit `XAI_API_KEY` connections and custom authentication
|
||||
|
||||
@@ -933,6 +933,14 @@ export function createServerEnvironmentAtoms<R, E>(
|
||||
);
|
||||
}).pipe(Atom.withLabel(`environment-data:server:usage-prices:${environmentId}`)),
|
||||
);
|
||||
const usageScanSettingsAtom = Atom.family((environmentId: EnvironmentId) =>
|
||||
Atom.make((get) =>
|
||||
JSON.stringify([
|
||||
get(usagePricesAtom(environmentId)),
|
||||
get(settingsValueAtom(environmentId))?.cursorKeychainUsageEnabled ?? false,
|
||||
]),
|
||||
).pipe(Atom.withLabel(`environment-data:server:usage-scan-settings:${environmentId}`)),
|
||||
);
|
||||
const providersValueAtom = Atom.family((environmentId: EnvironmentId) =>
|
||||
Atom.make((get) => get(configValueAtom(environmentId))?.providers ?? null).pipe(
|
||||
Atom.withLabel(`environment-data:server:providers:${environmentId}`),
|
||||
@@ -1050,7 +1058,7 @@ export function createServerEnvironmentAtoms<R, E>(
|
||||
label: "environment-data:server:usage-summary",
|
||||
tag: WS_METHODS.serverGetUsageSummary,
|
||||
staleTimeMs: 60_000,
|
||||
refreshTrigger: ({ environmentId }) => usagePricesAtom(environmentId),
|
||||
refreshTrigger: ({ environmentId }) => usageScanSettingsAtom(environmentId),
|
||||
}),
|
||||
configProjection,
|
||||
welcome,
|
||||
|
||||
@@ -184,6 +184,19 @@ describe("manual usage refresh", () => {
|
||||
});
|
||||
|
||||
describe("limits refresh cooldown", () => {
|
||||
it("runs a fresh check after an in-flight check when settings change", async () => {
|
||||
const id = EnvironmentId.make("limits-after-enable");
|
||||
const oldCheck = Promise.withResolvers<string>();
|
||||
const first = refreshUsageLimits(id, () => oldCheck.promise, true);
|
||||
const newCheck = vi.fn(async () => "new limits");
|
||||
const afterEnable = refreshUsageLimits(id, newCheck, false, true);
|
||||
expect(newCheck).not.toHaveBeenCalled();
|
||||
oldCheck.resolve("old limits");
|
||||
expect(await first).toBe("old limits");
|
||||
expect(await afterEnable).toBe("new limits");
|
||||
expect(newCheck).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("joins manual calls and gates automatic refreshes after success or failure", async () => {
|
||||
const clock = vi.spyOn(Date, "now").mockReturnValue(1_000);
|
||||
try {
|
||||
|
||||
@@ -16,9 +16,18 @@ export async function refreshUsageLimits<A>(
|
||||
environmentId: EnvironmentId,
|
||||
refresh: () => Promise<A>,
|
||||
automatic = false,
|
||||
afterPending = false,
|
||||
): Promise<A | undefined> {
|
||||
const pending = limitsRefreshes.get(environmentId);
|
||||
if (pending !== undefined) {
|
||||
if (afterPending) {
|
||||
try {
|
||||
await pending;
|
||||
} catch {
|
||||
// The new check still needs to run if the earlier one failed.
|
||||
}
|
||||
return refreshUsageLimits(environmentId, refresh, false, true);
|
||||
}
|
||||
// Manual refresh waits for the current check; automatic refresh does not repeat it.
|
||||
return automatic ? undefined : ((await pending) as A);
|
||||
}
|
||||
|
||||
@@ -1277,6 +1277,10 @@ export const ServerSettings = Schema.Struct({
|
||||
usageLimitSources: Schema.Record(UsageLimitSourceId, UsageLimitSourceConfig).pipe(
|
||||
Schema.withDecodingDefault(Effect.succeed({})),
|
||||
),
|
||||
/** Allows this server to read the Cursor CLI's macOS Keychain login for account usage. */
|
||||
cursorKeychainUsageEnabled: Schema.Boolean.pipe(
|
||||
Schema.withDecodingDefault(Effect.succeed(false)),
|
||||
),
|
||||
/** Exact model IDs, applied to past and future usage on this environment. */
|
||||
usagePriceOverrides: Schema.Record(TrimmedNonEmptyString, UsageModelPriceOverride).pipe(
|
||||
Schema.withDecodingDefault(Effect.succeed({})),
|
||||
@@ -1550,6 +1554,7 @@ export const ServerSettingsPatch = Schema.Struct({
|
||||
usageLimitSources: Schema.optionalKey(
|
||||
Schema.Record(UsageLimitSourceId, Schema.NullOr(UsageLimitSourceConfig)),
|
||||
),
|
||||
cursorKeychainUsageEnabled: Schema.optionalKey(Schema.Boolean),
|
||||
/** Each entry replaces one model's rates; `null` restores automatic pricing. */
|
||||
usagePriceOverrides: Schema.optionalKey(
|
||||
Schema.Record(TrimmedNonEmptyString, Schema.NullOr(UsageModelPriceOverride)),
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
/**
|
||||
* Usage reporting contract.
|
||||
*
|
||||
* Each environment scans the provider CLIs' own on-disk session transcripts
|
||||
* (`~/.claude/projects/**\/*.jsonl`, `~/.codex/sessions/**\/*.jsonl`,
|
||||
* `~/.grok/sessions/**\/updates.jsonl`) rather than relying on T3 Code's own
|
||||
* orchestration projections, so usage stays complete even for turns that were
|
||||
* never driven through T3 Code. This mirrors the approach `ccusage` takes.
|
||||
* Each environment scans native session files and databases, including work
|
||||
* driven outside T3 Code. Source status describes gaps in local coverage.
|
||||
*
|
||||
* Environments return pre-aggregated `(day, hourStart?, provider, model)`
|
||||
* Environments return pre-aggregated `(day, hourStart?, provider, model, sourcePath?)`
|
||||
* buckets. Raw transcript records never cross the wire.
|
||||
*
|
||||
* @module usage
|
||||
@@ -21,18 +18,24 @@ import { NonNegativeInt, TrimmedNonEmptyString } from "./baseSchemas.ts";
|
||||
* client renders partial coverage when an environment reports an older version
|
||||
* rather than failing the whole page.
|
||||
*/
|
||||
export const USAGE_CONTRACT_VERSION = 5 as const;
|
||||
export const USAGE_CONTRACT_VERSION = 6 as const;
|
||||
|
||||
/**
|
||||
* Oldest {@link UsageSummary} version a current client will still merge.
|
||||
*
|
||||
* v5 only adds `grok` to {@link UsageProviderKind}; v4 Claude/Codex buckets
|
||||
* remain valid, so mixed-version environments keep those totals instead of
|
||||
* treating every older server as stale.
|
||||
* v5/v6 add providers and optional source attribution; v4 Claude/Codex
|
||||
* buckets remain valid in mixed-version environments.
|
||||
*/
|
||||
export const USAGE_MERGE_COMPATIBLE_SINCE = 4 as const;
|
||||
|
||||
export const UsageProviderKind = Schema.Literals(["claude", "codex", "grok"]);
|
||||
export const UsageProviderKind = Schema.Literals([
|
||||
"claude",
|
||||
"codex",
|
||||
"grok",
|
||||
"cursor",
|
||||
"opencode",
|
||||
"antigravity",
|
||||
]);
|
||||
export type UsageProviderKind = typeof UsageProviderKind.Type;
|
||||
|
||||
/**
|
||||
@@ -93,6 +96,8 @@ export const UsageBucket = Schema.Struct({
|
||||
hourStart: Schema.optional(TrimmedNonEmptyString),
|
||||
provider: UsageProviderKind,
|
||||
model: TrimmedNonEmptyString,
|
||||
/** Source directory, so overlapping multi-home environments merge once per source. */
|
||||
sourcePath: Schema.optional(TrimmedNonEmptyString),
|
||||
totals: UsageTokenTotals,
|
||||
costUsd: Schema.Number,
|
||||
/**
|
||||
@@ -151,6 +156,8 @@ export const UsageSource = Schema.Struct({
|
||||
*/
|
||||
distinctSessions: NonNegativeInt,
|
||||
message: Schema.NullOr(TrimmedNonEmptyString),
|
||||
/** An action the client can offer to make this source available. */
|
||||
action: Schema.optionalKey(Schema.Literal("enableCursorKeychain")),
|
||||
});
|
||||
export type UsageSource = typeof UsageSource.Type;
|
||||
|
||||
|
||||
@@ -5,10 +5,22 @@ import {
|
||||
enumerateHourStarts,
|
||||
formatDateTimeShort,
|
||||
formatHourShort,
|
||||
formatPercent,
|
||||
formatRelativeHourShort,
|
||||
makeWindow,
|
||||
} from "./usageFormat.ts";
|
||||
|
||||
describe("formatPercent", () => {
|
||||
it("distinguishes a small positive share from zero", () => {
|
||||
expect(formatPercent(0)).toBe("0.0%");
|
||||
expect(formatPercent(0.0004)).toBe("<0.1%");
|
||||
expect(formatPercent(0.0009)).toBe("<0.1%");
|
||||
expect(formatPercent(0.001)).toBe("0.1%");
|
||||
expect(formatPercent(0.023)).toBe("2.3%");
|
||||
expect(formatPercent(0.00004, 2)).toBe("<0.01%");
|
||||
});
|
||||
});
|
||||
|
||||
describe("hourly usage formatting", () => {
|
||||
it("keeps requested zones separate when formatting repeated calls", () => {
|
||||
const instant = "2026-08-11T12:37:00.000Z";
|
||||
|
||||
@@ -43,7 +43,10 @@ function trim(value: number): string {
|
||||
}
|
||||
|
||||
export function formatPercent(share: number, digits = 1): string {
|
||||
return `${(share * 100).toFixed(digits)}%`;
|
||||
const percent = share * 100;
|
||||
const smallest = 10 ** -digits;
|
||||
if (percent > 0 && percent < smallest) return `<${smallest.toFixed(digits)}%`;
|
||||
return `${percent.toFixed(digits)}%`;
|
||||
}
|
||||
|
||||
/** `2026-08-07` to `Aug 7`. */
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
collectLimitAccounts,
|
||||
collectLimitNotices,
|
||||
collectLimitPools,
|
||||
displayLimitWindows,
|
||||
elapsedShare,
|
||||
formatResetsIn,
|
||||
limitsNotice,
|
||||
@@ -687,6 +688,53 @@ describe("pooled account columns", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("Cursor limit presentation", () => {
|
||||
const cursorAccount: LimitAccount = {
|
||||
key: "cursor",
|
||||
driver: ProviderDriverKind.make("cursor"),
|
||||
displayName: "Cursor",
|
||||
email: undefined,
|
||||
plan: undefined,
|
||||
accentColor: undefined,
|
||||
environments: [],
|
||||
sourceLabel: "Cursor",
|
||||
redeem: null,
|
||||
limits: {
|
||||
checkedAt: "2026-09-03T11:00:00.000Z",
|
||||
windows: [
|
||||
{ id: "apiPercentUsed", kind: "monthly", label: "Other Models", usedPercent: 49 },
|
||||
{ id: "autoPercentUsed", kind: "monthly", label: "Cursor Models", usedPercent: 9 },
|
||||
{ id: "totalPercentUsed", kind: "monthly", label: "Overall", usedPercent: 15 },
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
it("hides the combined percentage and orders the two pools", () => {
|
||||
const [pool] = collectLimitPools([cursorAccount], now);
|
||||
const display = displayLimitWindows(pool!);
|
||||
expect(display.map((window) => window.id)).toEqual(["autoPercentUsed", "apiPercentUsed"]);
|
||||
});
|
||||
|
||||
it("keeps the combined percentage as a card if either allowance is missing", () => {
|
||||
const [pool] = collectLimitPools(
|
||||
[
|
||||
{
|
||||
...cursorAccount,
|
||||
limits: {
|
||||
...cursorAccount.limits,
|
||||
windows: cursorAccount.limits.windows.filter(
|
||||
(window) => window.id !== "apiPercentUsed",
|
||||
),
|
||||
},
|
||||
},
|
||||
],
|
||||
now,
|
||||
);
|
||||
const display = displayLimitWindows(pool!);
|
||||
expect(display.map((window) => window.id)).toEqual(["totalPercentUsed", "autoPercentUsed"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("collectLimitNotices", () => {
|
||||
const checkedAt = "2026-09-03T11:00:00.000Z";
|
||||
const claude = ProviderDriverKind.make("claudeAgent");
|
||||
|
||||
@@ -24,6 +24,33 @@ const MINUTE = 60_000;
|
||||
const HOUR = 60 * MINUTE;
|
||||
const DAY = 24 * HOUR;
|
||||
|
||||
export const CURSOR_USAGE_WINDOWS = [
|
||||
{
|
||||
id: "totalPercentUsed",
|
||||
label: "Overall",
|
||||
description: "Combined usage across both allowances, not a third quota.",
|
||||
},
|
||||
{
|
||||
id: "autoPercentUsed",
|
||||
label: "Cursor Models",
|
||||
description: "Grok and Composer use this first. Auto can use either pool.",
|
||||
},
|
||||
{
|
||||
id: "apiPercentUsed",
|
||||
label: "Other Models",
|
||||
description: "Claude, GPT, and Gemini use this pool. Grok and Composer fall back here.",
|
||||
},
|
||||
] as const;
|
||||
|
||||
export function cursorUsageWindowDetails(id: string) {
|
||||
return CURSOR_USAGE_WINDOWS.find((window) => window.id === id);
|
||||
}
|
||||
|
||||
function cursorUsageWindowRank(id: string): number {
|
||||
const rank = CURSOR_USAGE_WINDOWS.findIndex((window) => window.id === id);
|
||||
return rank < 0 ? CURSOR_USAGE_WINDOWS.length : rank;
|
||||
}
|
||||
|
||||
/**
|
||||
* Providers that belong on the Limits view: enabled, installed, and one whose
|
||||
* driver reports subscription usage at all. A driver with no notion of usage
|
||||
@@ -282,6 +309,17 @@ export interface LimitPool {
|
||||
readonly windows: readonly LimitPoolWindow[];
|
||||
}
|
||||
|
||||
/** Show Cursor's two usable pools instead of a combined percentage when both are available. */
|
||||
export function displayLimitWindows(pool: LimitPool) {
|
||||
if (pool.driver !== "cursor") return pool.windows;
|
||||
const hasAuto = pool.windows.some((window) => window.id === "autoPercentUsed");
|
||||
const hasApi = pool.windows.some((window) => window.id === "apiPercentUsed");
|
||||
const hasBothPools = hasAuto && hasApi;
|
||||
return pool.windows
|
||||
.filter((window) => !hasBothPools || window.id !== "totalPercentUsed")
|
||||
.sort((left, right) => cursorUsageWindowRank(left.id) - cursorUsageWindowRank(right.id));
|
||||
}
|
||||
|
||||
const WINDOW_KIND_ORDER: Record<ServerProviderUsageWindow["kind"], number> = {
|
||||
session: 0,
|
||||
weekly: 1,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
USAGE_CONTRACT_VERSION,
|
||||
USAGE_MERGE_COMPATIBLE_SINCE,
|
||||
type EnvironmentId,
|
||||
type UsageBucket,
|
||||
type UsageDay,
|
||||
@@ -74,6 +75,41 @@ function environment(id: string, usageSummary: UsageSummary): EnvironmentUsage {
|
||||
}
|
||||
|
||||
describe("mergeUsage", () => {
|
||||
it("counts a Cursor account once across servers while retaining each server's other providers", () => {
|
||||
const account = {
|
||||
provider: "cursor" as const,
|
||||
hostId: "cursor.com",
|
||||
homePath: "cursor-account:account-hash",
|
||||
volumeId: "account-hash",
|
||||
};
|
||||
const merged = mergeUsage(
|
||||
[
|
||||
environment(
|
||||
"mac",
|
||||
summary([bucket({ provider: "cursor", sourcePath: account.homePath })], [account]),
|
||||
),
|
||||
environment(
|
||||
"linux",
|
||||
summary(
|
||||
[
|
||||
bucket({ provider: "cursor", sourcePath: account.homePath }),
|
||||
bucket({ provider: "opencode", sourcePath: "/opencode" }),
|
||||
],
|
||||
[account, { provider: "opencode", hostId: "linux", homePath: "/opencode" }],
|
||||
),
|
||||
),
|
||||
],
|
||||
USAGE_CONTRACT_VERSION,
|
||||
);
|
||||
expect(
|
||||
merged.providers.map((provider) => [provider.provider, provider.costUsd]).sort(),
|
||||
).toEqual([
|
||||
["cursor", 10],
|
||||
["opencode", 10],
|
||||
]);
|
||||
expect(merged.duplicateSources).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("sums environments that read different transcript directories", () => {
|
||||
const merged = mergeUsage(
|
||||
[
|
||||
@@ -146,6 +182,31 @@ describe("mergeUsage", () => {
|
||||
).toEqual({ claude: 1, codex: 1 });
|
||||
});
|
||||
|
||||
it("counts overlapping provider roots once while keeping each environment's unique root", () => {
|
||||
const source = (homePath: string) => ({
|
||||
provider: "opencode" as const,
|
||||
hostId: "host",
|
||||
homePath,
|
||||
});
|
||||
const usage = (sourcePath: string, costUsd: number) =>
|
||||
bucket({ provider: "opencode", sourcePath, costUsd });
|
||||
const merged = mergeUsage(
|
||||
[
|
||||
environment(
|
||||
"env-a",
|
||||
summary([usage("/shared", 10), usage("/a", 2)], [source("/shared"), source("/a")]),
|
||||
),
|
||||
environment(
|
||||
"env-b",
|
||||
summary([usage("/shared", 10), usage("/b", 3)], [source("/shared"), source("/b")]),
|
||||
),
|
||||
],
|
||||
USAGE_CONTRACT_VERSION,
|
||||
);
|
||||
expect(merged.costUsd).toBe(15);
|
||||
expect(merged.sessions).toBe(3);
|
||||
});
|
||||
|
||||
it("uses the newest scan when environments share the same transcript directory", () => {
|
||||
const source = { provider: "claude" as const, hostId: "mac", homePath: "/home/theo/.claude" };
|
||||
const environments = [
|
||||
@@ -166,6 +227,104 @@ describe("mergeUsage", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("prefers a complete scan over a newer partial scan of the same directory", () => {
|
||||
const source = { provider: "claude" as const, hostId: "mac", homePath: "/home/theo/.claude" };
|
||||
const incomplete = summary([bucket({ costUsd: 4, records: 2 })], [source]);
|
||||
const partial = environment("new", {
|
||||
...incomplete,
|
||||
readAt: "2026-08-07T01:00:00.000Z",
|
||||
sources: incomplete.sources.map((entry) => ({ ...entry, status: "partial" as const })),
|
||||
});
|
||||
const complete = environment("old", summary([bucket()], [source]));
|
||||
|
||||
for (const ordered of [
|
||||
[partial, complete],
|
||||
[complete, partial],
|
||||
]) {
|
||||
const merged = mergeUsage(ordered, USAGE_CONTRACT_VERSION);
|
||||
expect(merged.costUsd).toBe(10);
|
||||
expect(merged.contributingEnvironments).toEqual(["old"]);
|
||||
expect(merged.duplicateSources).toEqual(["new: /home/theo/.claude"]);
|
||||
}
|
||||
expect(mergeUsage([partial], USAGE_CONTRACT_VERSION).costUsd).toBe(4);
|
||||
});
|
||||
|
||||
it("keeps new cells from a later partial scan without recounting older cells", () => {
|
||||
const source = { provider: "claude" as const, hostId: "mac", homePath: "/home/theo/.claude" };
|
||||
const complete = environment(
|
||||
"old",
|
||||
summary([bucket()], [source], USAGE_MERGE_COMPATIBLE_SINCE),
|
||||
);
|
||||
const partialSummary = summary(
|
||||
[
|
||||
bucket({ sourcePath: source.homePath, costUsd: 4, records: 2 }),
|
||||
bucket({
|
||||
day: "2026-08-08" as UsageDay,
|
||||
sourcePath: source.homePath,
|
||||
costUsd: 3,
|
||||
records: 1,
|
||||
}),
|
||||
],
|
||||
[{ ...source, distinctSessions: 2 }],
|
||||
);
|
||||
const partial = environment("new", {
|
||||
...partialSummary,
|
||||
readAt: "2026-08-08T01:00:00.000Z",
|
||||
sources: partialSummary.sources.map((entry) => ({ ...entry, status: "partial" as const })),
|
||||
});
|
||||
|
||||
for (const ordered of [
|
||||
[complete, partial],
|
||||
[partial, complete],
|
||||
]) {
|
||||
const merged = mergeUsage(ordered, USAGE_CONTRACT_VERSION);
|
||||
expect(merged.costUsd).toBe(13);
|
||||
expect(merged.records).toBe(6);
|
||||
expect(merged.sessions).toBe(2);
|
||||
expect(merged.daily.map(({ day, costUsd }) => [day, costUsd])).toEqual([
|
||||
["2026-08-07", 10],
|
||||
["2026-08-08", 3],
|
||||
]);
|
||||
expect(merged.contributingEnvironments).toEqual(
|
||||
ordered.map(({ environmentId }) => environmentId),
|
||||
);
|
||||
expect(merged.duplicateSources).toEqual(["new: /home/theo/.claude"]);
|
||||
}
|
||||
});
|
||||
|
||||
it("retains a complete cell when a larger partial cell may have skipped old records", () => {
|
||||
const source = { provider: "claude" as const, hostId: "mac", homePath: "/home/theo/.claude" };
|
||||
const complete = environment("old", summary([bucket()], [source]));
|
||||
const partialSummary = summary(
|
||||
[
|
||||
bucket({
|
||||
costUsd: 4,
|
||||
records: 6,
|
||||
totals: {
|
||||
uncachedInputTokens: 80,
|
||||
cachedInputTokens: 500,
|
||||
cacheCreationTokens: 10,
|
||||
outputTokens: 30,
|
||||
reasoningTokens: 0,
|
||||
},
|
||||
}),
|
||||
],
|
||||
[{ ...source, distinctSessions: 2 }],
|
||||
);
|
||||
const partial = environment("new", {
|
||||
...partialSummary,
|
||||
readAt: "2026-08-07T01:00:00.000Z",
|
||||
sources: partialSummary.sources.map((entry) => ({ ...entry, status: "partial" as const })),
|
||||
});
|
||||
|
||||
const merged = mergeUsage([complete, partial], USAGE_CONTRACT_VERSION);
|
||||
expect(merged.costUsd).toBe(10);
|
||||
expect(merged.totalTokens).toBe(1160);
|
||||
expect(merged.records).toBe(5);
|
||||
expect(merged.sessions).toBe(1);
|
||||
expect(merged.contributingEnvironments).toEqual(["old"]);
|
||||
});
|
||||
|
||||
it("excludes an environment reporting an older contract version", () => {
|
||||
const merged = mergeUsage(
|
||||
[
|
||||
@@ -178,7 +337,7 @@ describe("mergeUsage", () => {
|
||||
summary(
|
||||
[bucket()],
|
||||
[{ provider: "claude", hostId: "linux", homePath: "/b" }],
|
||||
USAGE_CONTRACT_VERSION - 2,
|
||||
USAGE_MERGE_COMPATIBLE_SINCE - 1,
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
type EnvironmentId,
|
||||
type UsageBucket,
|
||||
type UsageProviderKind,
|
||||
type UsageSource,
|
||||
type UsageSourceFingerprint,
|
||||
type UsageSummary,
|
||||
} from "@t3tools/contracts";
|
||||
@@ -113,20 +114,46 @@ function fingerprintKey(fingerprint: UsageSourceFingerprint): string {
|
||||
].join(" ");
|
||||
}
|
||||
|
||||
function bucketsForSource(summary: UsageSummary, source: UsageSource): readonly UsageBucket[] {
|
||||
const providerSources = summary.sources.filter(
|
||||
(entry) => entry.fingerprint.provider === source.fingerprint.provider,
|
||||
);
|
||||
return summary.buckets.filter(
|
||||
(bucket) =>
|
||||
bucket.provider === source.fingerprint.provider &&
|
||||
(bucket.sourcePath === source.fingerprint.resolvedHomePath ||
|
||||
(bucket.sourcePath === undefined && providerSources.length === 1)),
|
||||
);
|
||||
}
|
||||
|
||||
function bucketKey(bucket: UsageBucket): string {
|
||||
return JSON.stringify([bucket.day, bucket.hourStart ?? null, bucket.provider, bucket.model]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decides which environment owns each physical transcript directory.
|
||||
*
|
||||
* Several environments on one machine (worktree servers, for instance) resolve
|
||||
* the same provider home and would otherwise double count every token. The
|
||||
* most recently read summary claims a fingerprint; the rest have that provider's
|
||||
* buckets dropped. Environment ids break ties so the winner is stable when
|
||||
* summaries have the same read time.
|
||||
* Complete scans claim a fingerprint ahead of partial scans, then the most
|
||||
* recently read scan wins within each status. A newer partial scan can still
|
||||
* contribute cells absent from an older complete scan. Environment ids break
|
||||
* ties so the result is stable when summaries have the same read time.
|
||||
*/
|
||||
function claimSources(environments: readonly EnvironmentUsage[]): {
|
||||
readonly ownerByFingerprint: ReadonlyMap<string, EnvironmentId>;
|
||||
readonly supplementalBucketsByEnvironment: ReadonlyMap<EnvironmentId, ReadonlySet<UsageBucket>>;
|
||||
readonly sessionsByFingerprint: ReadonlyMap<string, number>;
|
||||
readonly duplicates: readonly string[];
|
||||
} {
|
||||
const ownerByFingerprint = new Map<string, EnvironmentId>();
|
||||
const ownerScanByFingerprint = new Map<
|
||||
string,
|
||||
{ environment: EnvironmentUsage; source: UsageSource }
|
||||
>();
|
||||
const seenBucketKeysByFingerprint = new Map<string, Set<string>>();
|
||||
const supplementalBucketsByEnvironment = new Map<EnvironmentId, Set<UsageBucket>>();
|
||||
const sessionsByFingerprint = new Map<string, number>();
|
||||
const duplicates: string[] = [];
|
||||
|
||||
const ordered = [...environments].sort(
|
||||
@@ -135,30 +162,82 @@ function claimSources(environments: readonly EnvironmentUsage[]): {
|
||||
a.environmentId.localeCompare(b.environmentId),
|
||||
);
|
||||
|
||||
for (const environment of ordered) {
|
||||
for (const source of environment.summary.sources) {
|
||||
if (source.status === "missing") continue;
|
||||
const key = fingerprintKey(source.fingerprint);
|
||||
if (ownerByFingerprint.has(key)) {
|
||||
duplicates.push(`${environment.label}: ${source.fingerprint.resolvedHomePath}`);
|
||||
continue;
|
||||
// A complete scan takes precedence over a newer partial scan of the same
|
||||
// directory. Partial history still contributes when no complete copy exists.
|
||||
for (const status of ["ok", "partial", "failed"] as const) {
|
||||
for (const environment of ordered) {
|
||||
for (const source of environment.summary.sources) {
|
||||
if (source.status !== status) continue;
|
||||
const key = fingerprintKey(source.fingerprint);
|
||||
if (ownerByFingerprint.has(key)) {
|
||||
duplicates.push(`${environment.label}: ${source.fingerprint.resolvedHomePath}`);
|
||||
continue;
|
||||
}
|
||||
ownerByFingerprint.set(key, environment.environmentId);
|
||||
ownerScanByFingerprint.set(key, { environment, source });
|
||||
sessionsByFingerprint.set(key, source.distinctSessions);
|
||||
}
|
||||
ownerByFingerprint.set(key, environment.environmentId);
|
||||
}
|
||||
}
|
||||
|
||||
return { ownerByFingerprint, duplicates };
|
||||
// A newer partial scan may contain usage recorded after an older complete
|
||||
// scan. Keep cells absent from the complete scan. Aggregated cells do not
|
||||
// reveal enough to reconcile overlapping records without double counting.
|
||||
for (const environment of ordered) {
|
||||
for (const source of environment.summary.sources) {
|
||||
if (source.status !== "partial") continue;
|
||||
const key = fingerprintKey(source.fingerprint);
|
||||
const owner = ownerScanByFingerprint.get(key);
|
||||
if (
|
||||
owner?.source.status !== "ok" ||
|
||||
Date.parse(environment.summary.readAt) <= Date.parse(owner.environment.summary.readAt)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
let seen = seenBucketKeysByFingerprint.get(key);
|
||||
if (seen === undefined) {
|
||||
seen = new Set(bucketsForSource(owner.environment.summary, owner.source).map(bucketKey));
|
||||
seenBucketKeysByFingerprint.set(key, seen);
|
||||
}
|
||||
const supplemental =
|
||||
supplementalBucketsByEnvironment.get(environment.environmentId) ?? new Set<UsageBucket>();
|
||||
let added = false;
|
||||
for (const bucket of bucketsForSource(environment.summary, source)) {
|
||||
const cell = bucketKey(bucket);
|
||||
if (seen.has(cell)) continue;
|
||||
seen.add(cell);
|
||||
supplemental.add(bucket);
|
||||
added = true;
|
||||
}
|
||||
if (!added) continue;
|
||||
supplementalBucketsByEnvironment.set(environment.environmentId, supplemental);
|
||||
sessionsByFingerprint.set(
|
||||
key,
|
||||
Math.max(sessionsByFingerprint.get(key) ?? 0, source.distinctSessions),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
ownerByFingerprint,
|
||||
supplementalBucketsByEnvironment,
|
||||
sessionsByFingerprint,
|
||||
duplicates,
|
||||
};
|
||||
}
|
||||
|
||||
/** Sources this environment owns after fingerprint claims, plus their buckets. */
|
||||
function ownedContribution(
|
||||
environment: EnvironmentUsage,
|
||||
ownerByFingerprint: ReadonlyMap<string, EnvironmentId>,
|
||||
supplementalBuckets: ReadonlySet<UsageBucket>,
|
||||
sessionsByFingerprint: ReadonlyMap<string, number>,
|
||||
): {
|
||||
readonly buckets: readonly UsageBucket[];
|
||||
readonly sessionsByProvider: ReadonlyMap<UsageProviderKind, number>;
|
||||
} {
|
||||
const ownedProviders = new Set<UsageProviderKind>();
|
||||
const ownedSources = new Set<string>();
|
||||
const sessionsByProvider = new Map<UsageProviderKind, number>();
|
||||
for (const source of environment.summary.sources) {
|
||||
if (source.status === "missing") continue;
|
||||
@@ -166,16 +245,24 @@ function ownedContribution(
|
||||
if (ownerByFingerprint.get(key) === environment.environmentId) {
|
||||
const provider = source.fingerprint.provider;
|
||||
ownedProviders.add(provider);
|
||||
ownedSources.add(`${provider}\u0000${source.fingerprint.resolvedHomePath}`);
|
||||
// Distinct within a directory. Summing per-bucket session counts instead
|
||||
// would count a session once per day and model it spans.
|
||||
sessionsByProvider.set(
|
||||
provider,
|
||||
(sessionsByProvider.get(provider) ?? 0) + source.distinctSessions,
|
||||
(sessionsByProvider.get(provider) ?? 0) +
|
||||
(sessionsByFingerprint.get(key) ?? source.distinctSessions),
|
||||
);
|
||||
}
|
||||
}
|
||||
return {
|
||||
buckets: environment.summary.buckets.filter((bucket) => ownedProviders.has(bucket.provider)),
|
||||
buckets: environment.summary.buckets.filter(
|
||||
(bucket) =>
|
||||
supplementalBuckets.has(bucket) ||
|
||||
(bucket.sourcePath === undefined
|
||||
? ownedProviders.has(bucket.provider)
|
||||
: ownedSources.has(`${bucket.provider}\u0000${bucket.sourcePath}`)),
|
||||
),
|
||||
sessionsByProvider,
|
||||
};
|
||||
}
|
||||
@@ -246,7 +333,12 @@ export function mergeUsage(
|
||||
}
|
||||
}
|
||||
|
||||
const { ownerByFingerprint, duplicates } = claimSources(current);
|
||||
const {
|
||||
ownerByFingerprint,
|
||||
supplementalBucketsByEnvironment,
|
||||
sessionsByFingerprint,
|
||||
duplicates,
|
||||
} = claimSources(current);
|
||||
|
||||
let costUsd = 0;
|
||||
let uncachedInputTokens = 0;
|
||||
@@ -295,7 +387,12 @@ export function mergeUsage(
|
||||
const contributingEnvironments: EnvironmentId[] = [];
|
||||
|
||||
for (const environment of current) {
|
||||
const { buckets, sessionsByProvider } = ownedContribution(environment, ownerByFingerprint);
|
||||
const { buckets, sessionsByProvider } = ownedContribution(
|
||||
environment,
|
||||
ownerByFingerprint,
|
||||
supplementalBucketsByEnvironment.get(environment.environmentId) ?? new Set(),
|
||||
sessionsByFingerprint,
|
||||
);
|
||||
if (buckets.length > 0) contributingEnvironments.push(environment.environmentId);
|
||||
|
||||
for (const [providerKind, providerSessions] of sessionsByProvider) {
|
||||
|
||||
Generated
+3
@@ -516,6 +516,9 @@ importers:
|
||||
'@ff-labs/fff-node':
|
||||
specifier: 0.9.4
|
||||
version: 0.9.4(patch_hash=c4e3cc2420ceb9dc650f9d189e9c24baf7e83f342998bacda5f459a4ce7927a8)
|
||||
'@napi-rs/keyring':
|
||||
specifier: ^1.3.0
|
||||
version: 1.3.0
|
||||
'@opencode-ai/sdk':
|
||||
specifier: ^1.3.15
|
||||
version: 1.15.13
|
||||
|
||||
@@ -49,6 +49,7 @@ describe("shouldBundleCliDependency", () => {
|
||||
"ffi-rs",
|
||||
"@yuuang/ffi-rs-win32-x64-msvc",
|
||||
"@ff-labs/fff-node",
|
||||
"@napi-rs/keyring",
|
||||
"@clerk/electron-passkeys",
|
||||
"node-addon-api",
|
||||
]) {
|
||||
@@ -82,7 +83,7 @@ describe("selectCliRuntimeExternalDependencies", () => {
|
||||
it("selects every external root declared by the server", () => {
|
||||
assert.deepStrictEqual(
|
||||
Object.keys(selectCliRuntimeExternalDependencies(serverPackageJson.dependencies)).sort(),
|
||||
["@ff-labs/fff-node", "node-pty"],
|
||||
["@ff-labs/fff-node", "@napi-rs/keyring", "node-pty"],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -30,6 +30,7 @@ export const CLI_RUNTIME_EXTERNAL_PREFIXES = [
|
||||
"ffi-rs",
|
||||
"@yuuang/",
|
||||
"@ff-labs/",
|
||||
"@napi-rs/keyring",
|
||||
"@clerk/electron-passkeys",
|
||||
"node-gyp-build",
|
||||
"node-addon-api",
|
||||
|
||||
Reference in new issue
Block a user