Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Usage bars: read every Claude instance's plan usage again (#5636)

Milind Sonif6271f62ad59
diff --git a/server/engine-limits.e2e.test.ts b/server/engine-limits.e2e.test.ts
index ee67b4a2ea..6f007401c2 100644
--- a/server/engine-limits.e2e.test.ts
+++ b/server/engine-limits.e2e.test.ts
@@ -26,8 +26,7 @@ it("a bot's turn reads its accounts' usage limits through its own capability, an
const { text } = await answer.json() as { text: string };
const accounts = JSON.parse(text).accounts as Array<{ engine: string; status: string; windows: unknown[]; why?: string }>;
const claude = accounts.find((account) => account.engine === "claudeAgent");
- // The Claude Code login is never read for usage, so Claude reports no limits; a refused turn still fails over.
- expect(claude, text).toMatchObject({ status: "unsupported", windows: [] });
+ expect(claude, text).toMatchObject({ status: "unknown", windows: [] });
expect(claude!.why, text).toBeTruthy();
writeFileSync(gate, "finish");
} finally {
diff --git a/server/limit-resets.test.ts b/server/limit-resets.test.ts
index 78a025d7fa..37556d0e1c 100644
--- a/server/limit-resets.test.ts
+++ b/server/limit-resets.test.ts
@@ -6,7 +6,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { codexLimitResetsAt } from "./drivers/codex-errors.ts";
import { engineLimitFromUsage, LimitResetClock, nearLimitNote, UNNAMED_LIMIT_MS } from "./limit-resets.ts";
import type { RuntimeEvent } from "../shared/runtime-events.ts";
-import { parseClaudeUsage, parseCodexUsage, parseOpenCodeUsage, type PlanProviderRow, type PlanUsageReport, type PlanWindows } from "./plan-usage.ts";
+import { parseClaudeUsage, parseCodexUsage, parseOpenCodeUsage, planAccountsFromInstances, type PlanProviderRow, type PlanUsageReport, type PlanWindows } from "./plan-usage.ts";
const NOW = Date.parse("2026-10-19T10:00:00Z");
const HOUR = 3_600_000;
@@ -34,8 +34,15 @@ const codex = (primary: [number, number], secondary: [number, number]) => row("c
}));
const report = (...providers: PlanProviderRow[]): PlanUsageReport => ({ fetchedAt: iso(NOW), providers });
-describe("a Claude account whose usage is not read", () => {
- it("stops a full reading from holding it, even one saved before an update stopped reading it, and keeps a refusal's hold", () => {
+describe("a Claude account's usage", () => {
+ it("reaches the app's limits for a Claude instance with no setting, beside the other engines", () => {
+ const accounts = planAccountsFromInstances({ claude: { driver: "claudeAgent" }, codex: { driver: "codex" } });
+ const instance = new LimitResetClock({ accounts: () => accounts, now: () => NOW });
+ instance.noteUsage(report(claude([38, NOW + 2 * HOUR], [12, NOW + 50 * HOUR]), codex([20, NOW + HOUR], [5, NOW + 90 * HOUR])));
+ expect(instance.report().engines.map((limit) => [limit.engine, limit.usedPercent])).toEqual([["claude", 38], ["codex", 20]]);
+ });
+
+ it("stops a full reading from holding it once its config sets usageMeter to false, and keeps a refusal's hold", () => {
const account = { id: "claude", name: "Claude", driver: "claude" as const, environment: {} };
let accounts: Array<typeof account & { noUsage?: true }> = [account];
const instance = new LimitResetClock({ accounts: () => accounts, now: () => NOW });
diff --git a/server/plan-usage.test.ts b/server/plan-usage.test.ts
index e19dc62dd1..66a9d1fec0 100644
--- a/server/plan-usage.test.ts
+++ b/server/plan-usage.test.ts
@@ -495,7 +495,7 @@ describe("plan usage fetcher", () => {
cursor: { driver: "cursorAgent", displayName: "Cursor" },
api: { driver: "grok", displayName: "Grok API" },
})).toEqual([
- { id: "claude", name: "Personal Claude", driver: "claude", environment: {}, configDir: "/tmp/claude", noUsage: true },
+ { id: "claude", name: "Personal Claude", driver: "claude", environment: {}, configDir: "/tmp/claude" },
{ id: "codex", name: "Codex", driver: "codex", environment: {} },
{ id: "grok", name: "Grok", driver: "grok", environment: { GROK_HOME: "/tmp/grok" } },
]);
@@ -615,22 +615,40 @@ describe("plan usage fetcher", () => {
expect(report.providers.map((row) => row.id)).toEqual(["codex"]);
});
- it("never reads a Claude Code login, on any Claude instance, and still reads the other engines", async () => {
+ it("reads every Claude instance's login with no setting, and none whose config sets usageMeter to false", async () => {
const accounts = planAccountsFromInstances({
claude: { driver: "claudeAgent" },
work: { driver: "claudeAgent", displayName: "Work", config: { configDir: "/tmp/claude-work" } },
+ quiet: { driver: "claudeAgent", displayName: "Quiet", config: { usageMeter: false } },
codex: { driver: "codex" },
});
// Still listed, so a refused turn's hold names the account and fails over.
expect(accounts).toEqual([
- { id: "claude", name: "Claude", driver: "claude", environment: {}, noUsage: true },
- { id: "work", name: "Work", driver: "claude", environment: {}, configDir: "/tmp/claude-work", noUsage: true },
+ { id: "claude", name: "Claude", driver: "claude", environment: {} },
+ { id: "work", name: "Work", driver: "claude", environment: {}, configDir: "/tmp/claude-work" },
+ { id: "quiet", name: "Quiet", driver: "claude", environment: {}, noUsage: true },
{ id: "codex", name: "Codex", driver: "codex", environment: {} },
]);
const read = vi.fn((_account: PlanAccount) => ({ token: SECRET, accountId: null, expired: false }));
- const fetchImpl = vi.fn<PlanFetch>(async () => jsonResponse({ rate_limit: { primary_window: { used_percent: 10 } } }));
+ const fetchImpl = vi.fn<PlanFetch>(async (url) => jsonResponse(url.includes("anthropic")
+ ? { five_hour: { utilization: 38, resets_at: "2026-10-19T15:40:00Z" } }
+ : { rate_limit: { primary_window: { used_percent: 10 } } }));
const report = await fetchPlanUsage(accounts, { fetch: fetchImpl, now: () => NOW, credentials: { read } });
- expect(read.mock.calls.map(([account]) => account.id)).toEqual(["codex"]);
- expect(report.providers.map((row) => row.id)).toEqual(["codex"]);
+ expect(read.mock.calls.map(([account]) => account.id)).toEqual(["claude", "work", "codex"]);
+ expect(report.providers.map((row) => [row.id, row.ok])).toEqual([["claude", true], ["work", true], ["codex", true]]);
+ expect(report.providers[0]!.fiveHour.usedPercent).toBe(38);
+ expect(JSON.stringify(report)).not.toContain(SECRET);
+ });
+
+ it("never serves a cached report across a usageMeter change", async () => {
+ const read = vi.fn((_account: PlanAccount) => ({ token: SECRET, accountId: null, expired: false }));
+ const fetchImpl = vi.fn<PlanFetch>(async () => jsonResponse({}));
+ const load = (config: Record<string, unknown>) => loadPlanUsage({
+ accounts: planAccountsFromInstances({ claude: { driver: "claudeAgent", config } }),
+ fetch: fetchImpl, now: NOW, credentials: { read },
+ });
+ expect((await load({})).providers.map((row) => row.id)).toEqual(["claude"]);
+ expect((await load({ usageMeter: false })).providers).toEqual([]);
+ expect((await load({})).providers.map((row) => row.id)).toEqual(["claude"]);
});
});
diff --git a/server/plan-usage.ts b/server/plan-usage.ts
index 41293d59d7..c7d04e56dd 100644
--- a/server/plan-usage.ts
+++ b/server/plan-usage.ts
@@ -72,7 +72,7 @@ export interface PlanAccount {
driver: PlanDriver;
environment: Record<string, string | undefined>;
configDir?: string;
- /** No usage reading: a ChatGPT plan signed in through OpenMausBot has no CLI login on disk, and a Claude Code login is never read, since Anthropic's credential policy covers that token for Claude Code alone (a refused turn still fails over). */
+ /** No usage reading: a ChatGPT plan signed in through OpenMausBot has no CLI login on disk, and a Claude instance whose config sets `usageMeter: false` keeps its login unread (a refused turn still fails over). */
noUsage?: true;
}
@@ -164,7 +164,7 @@ export function planAccountsFromInstances(instances: InstanceConfigMap): PlanAcc
environment: { ...entry.environment },
...(configDir ? { configDir } : {}),
// Reading ~/.codex/auth.json for a ChatGPT-plan provider would report (and hold it on) another account's limit.
- ...(config?.authMode === "chatgpt-plan" || driver === "claude" ? { noUsage: true as const } : {}),
+ ...(config?.authMode === "chatgpt-plan" || (driver === "claude" && config?.usageMeter === false) ? { noUsage: true as const } : {}),
});
}
return accounts;
diff --git a/src/components/ChatMoreMenu.test.ts b/src/components/ChatMoreMenu.test.ts
index e14b6fbe34..d5721c0f84 100644
--- a/src/components/ChatMoreMenu.test.ts
+++ b/src/components/ChatMoreMenu.test.ts
@@ -43,6 +43,7 @@ vi.mock("./SidebarPopoverMenu", async (importOriginal) => ({
}));
const { ChatMoreMenu, ChatHeaderMenu, ChatShareMenu } = await import("./ChatMoreMenu");
+const { rememberLimits } = await import("@/lib/limit-resets");
afterAll(() => vi.unstubAllGlobals());
const task = (threadId: string, title: string, at: number, extra: Partial<Task> = {}): Task => ({
@@ -168,6 +169,27 @@ describe("ChatMoreMenu, the round ⋯ for the bot panel", () => {
});
});
+describe("the phone header's ⋯", () => {
+ const modelRow = () => menuOf(createElement(ChatMoreMenu, { bot, phoneHeader: true })).items.find((item) => item.key === "model")!;
+ const trailing = () => renderToStaticMarkup(createElement("div", null, modelRow().trailing));
+ for (const advanced of [false, true]) {
+ it(`shows the model's usage bars on its Model row (${advanced ? "Advanced" : "Simple"})`, () => {
+ fixture.advanced = advanced;
+ rememberLimits({ checkedAt: 1, engines: [
+ { engine: "test", name: "Test", driver: "codex", window: "5-hour", usedPercent: 92, resetsAt: null, blocked: false },
+ { engine: "other", name: "Other", driver: "claude", window: "5-hour", usedPercent: 10, resetsAt: null, blocked: false },
+ ] });
+ expect(trailing()).toContain('data-usage-bars="near"');
+ expect(trailing()).toContain("Test: 92% used");
+ });
+ }
+
+ it("leaves the Model row bare for an engine that reports no usage", () => {
+ rememberLimits({ checkedAt: 1, engines: [{ engine: "other", name: "Other", driver: "claude", window: "5-hour", usedPercent: 10, resetsAt: null, blocked: false }] });
+ expect(trailing()).not.toContain("data-usage-bars");
+ });
+});
+
describe("ChatShareMenu, the bot panel's Share", () => {
it("copies or downloads the conversation, in those words", () => {
const menu = menuOf(createElement(ChatShareMenu, { bot, className: "" }));
diff --git a/src/components/ModelPicker.usage.test.ts b/src/components/ModelPicker.usage.test.ts
index 18fe0143a5..dc12165c8b 100644
--- a/src/components/ModelPicker.usage.test.ts
+++ b/src/components/ModelPicker.usage.test.ts
@@ -87,6 +87,21 @@ describe("the model button's usage bars", () => {
});
}
+ for (const advanced of [false, true]) {
+ it(`follow the bot's own engine when two engines report usage (${advanced ? "Advanced" : "Simple"})`, () => {
+ fixture.advanced = advanced;
+ const codex: EngineLimit = { ...usage(91), engine: "codex", name: "Codex", driver: "codex", windows: [{ window: "weekly", usedPercent: 91, resetsAt: RESET }] };
+ remember([usage(38), codex]);
+ flushSync(() => root.render(createElement(ModelPicker, { bot: { ...scout, modelSelection: { instanceId: "codex", model: "gpt-5" } }, threadId: scout.threadId })));
+ expect(chip().querySelector("[data-usage-bars]")?.getAttribute("data-usage-bars")).toBe("near");
+ expect(chip().textContent).toContain("Codex: 91% used");
+ expect(chip().textContent).not.toContain("Claude: 38% used");
+ render(true);
+ expect(chip().querySelector("[data-usage-bars]")?.getAttribute("data-usage-bars")).toBe("fine");
+ expect(chip().textContent).toContain("Claude: 38% used");
+ });
+ }
+
it("draw nothing for an engine that reports no usage", () => {
remember([{ ...usage(0), usedPercent: null, window: null, resetsAt: null, windows: [] }]);
render();