Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/mobile/src/features/usage/UsageRouteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,12 @@ function UsageCoverageNotice(props: {
props.merged.staleEnvironments.includes(environment.environmentId),
);
const duplicateSources = props.merged.duplicateSources;
const incompleteSources = props.merged.incompleteSources;
if (
failed.length === 0 &&
stale.length === 0 &&
duplicateSources.length === 0 &&
incompleteSources.length === 0 &&
!props.isPartial
) {
return null;
Expand All @@ -489,6 +491,15 @@ function UsageCoverageNotice(props: {
{environment.label} runs an older server version and is excluded from totals.
</Text>
))}
{incompleteSources.map((source) => (
<Text
key={`${source.environmentId}:${source.provider}`}
className="text-sm text-foreground-muted"
>
{source.environmentLabel}&apos;s {PROVIDER_LABEL[source.provider]} usage{" "}
{source.status === "failed" ? "could not be read." : "is incomplete."}
</Text>
))}
{duplicateSources.length > 0 ? (
<Text className="text-sm text-foreground-muted">
Counted once across environments sharing a transcript directory:{" "}
Expand Down
7 changes: 5 additions & 2 deletions apps/mobile/src/features/usage/usageProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ 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"];
export const PROVIDER_ORDER: readonly UsageProviderKind[] = ["codex", "claude", "mcode"];

export const PROVIDER_LABEL: Record<UsageProviderKind, string> = {
claude: "Claude Code",
codex: "Codex",
mcode: "MCode",
};

/**
* Claude's brand orange holds in both themes; Codex is neutral and must flip
* with the theme or its bars vanish against the matching background.
* with the theme or its bars vanish against the matching background. MCode's
* official sky blue remains legible in both themes.
*/
export function useProviderColors(): Record<UsageProviderKind, string> {
const { themeAppearance: scheme } = useAppearancePreferences();
return {
claude: "#d97757",
codex: scheme === "dark" ? "#e6e6e6" : "#3c3c43",
mcode: scheme === "dark" ? "#7DC6FF" : "#2563EB",
};
}
2 changes: 2 additions & 0 deletions apps/mobile/src/state/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export function useUsage(input: UsageSummaryInput): UsageView {
const windowKey = useMemo(
() =>
JSON.stringify({
contractVersion: input.contractVersion,
sinceDay: input.sinceDay,
untilDay: input.untilDay,
timeZone: input.timeZone,
Expand All @@ -86,6 +87,7 @@ export function useUsage(input: UsageSummaryInput): UsageView {
untilTime: input.untilTime,
}),
[
input.contractVersion,
input.sinceDay,
input.untilDay,
input.timeZone,
Expand Down
84 changes: 84 additions & 0 deletions apps/server/src/usage/UsageService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { describe, expect, it } from "@effect/vitest";

import {
chooseMcodeUsageStore,
classifyUsageSourceExistence,
negotiateUsageContractVersion,
resolveMcodeDataDir,
summarizeSourceReadFailures,
} from "./UsageService.ts";

describe("classifyUsageSourceExistence", () => {
it("keeps I/O failures distinct from missing sources", () => {
expect(classifyUsageSourceExistence(true)).toBe("present");
expect(classifyUsageSourceExistence(false)).toBe("missing");
expect(classifyUsageSourceExistence(null)).toBe("failed");
});
});

describe("resolveMcodeDataDir", () => {
it("prefers the current MCode override and accepts the legacy name", () => {
expect(resolveMcodeDataDir({ MINIMAX_DATA_DIR: "/custom/mcode" }, "/home/user/.minimax")).toBe(
"/custom/mcode",
);
expect(resolveMcodeDataDir({ MAVIS_DATA_DIR: "/legacy/mavis" }, "/home/user/.minimax")).toBe(
"/legacy/mavis",
);
});

it("uses the shared TUI and desktop directory by default", () => {
expect(resolveMcodeDataDir({}, "/home/user/.minimax")).toBe("/home/user/.minimax");
});
});

describe("chooseMcodeUsageStore", () => {
it("uses the alternate when the primary is only an empty compatibility stub", () => {
expect(chooseMcodeUsageStore("primary.sqlite", "absent", "alternate.sqlite", "ready")).toBe(
"alternate.sqlite",
);
});

it("keeps the canonical primary when both stores have accounting", () => {
expect(chooseMcodeUsageStore("primary.sqlite", "ready", "alternate.sqlite", "ready")).toBe(
"primary.sqlite",
);
});

it("keeps the primary fingerprint when its probe fails transiently", () => {
expect(chooseMcodeUsageStore("primary.sqlite", "failed", "alternate.sqlite", "ready")).toBe(
"primary.sqlite",
);
});

it("keeps the alternate path when it is the only store but its probe fails", () => {
expect(chooseMcodeUsageStore("primary.sqlite", "absent", "alternate.sqlite", "failed")).toBe(
"alternate.sqlite",
);
});
});

describe("negotiateUsageContractVersion", () => {
it("keeps v4 responses decodable for legacy clients", () => {
expect(negotiateUsageContractVersion(undefined)).toBe(4);
expect(negotiateUsageContractVersion(4)).toBe(4);
});

it("serves MCode only to compatible clients", () => {
expect(negotiateUsageContractVersion(5)).toBe(5);
expect(negotiateUsageContractVersion(6)).toBe(5);
});
});

describe("summarizeSourceReadFailures", () => {
it("distinguishes healthy, partial, and failed stores", () => {
expect(summarizeSourceReadFailures(1, 0)).toEqual({ status: "ok", message: null });
expect(summarizeSourceReadFailures(2, 1)).toEqual({
status: "partial",
message: "1 usage file could not be read.",
});
expect(summarizeSourceReadFailures(1, 1)).toEqual({
status: "failed",
message: "1 usage file could not be read.",
});
});
});
Loading
Loading