Skip to content

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

fix(plan-usage): a usage-meter switch never leaves a stale reading in play

heimixieb2b2759ef5430
diff --git a/server/limit-resets.test.ts b/server/limit-resets.test.ts
index 19efc4d125..73b22f5ff8 100644
--- a/server/limit-resets.test.ts
+++ b/server/limit-resets.test.ts
@@ -34,6 +34,24 @@ 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 meter is turned off", () => {
+ it("stops a full reading from holding it, even one fetched before the switch, 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 });
+ const full = report(claude([100, NOW + 2 * HOUR], [40, NOW + 50 * HOUR]));
+ instance.noteUsage(full);
+ expect(instance.blockedUntil("claude")).toBe(NOW + 2 * HOUR);
+ accounts = [{ ...account, noUsage: true }];
+ expect(instance.blockedUntil("claude")).toBeNull();
+ instance.noteUsage(full);
+ expect(instance.blockedUntil("claude")).toBeNull();
+ expect(instance.report().engines).toEqual([]);
+ instance.noteRefusal("claude", NOW + HOUR);
+ expect(instance.blockedUntil("claude")).toBe(NOW + HOUR);
+ });
+});
+
describe("engineLimitFromUsage", () => {
it("takes Claude's fullest window and its reset when nothing is at the limit", () => {
expect(engineLimitFromUsage(claude([92, NOW + 2 * HOUR], [40, NOW + 50 * HOUR]))).toEqual({
diff --git a/server/limit-resets.ts b/server/limit-resets.ts
index 56b0214283..3e4c2e1e83 100644
--- a/server/limit-resets.ts
+++ b/server/limit-resets.ts
@@ -125,7 +125,7 @@ export class LimitResetClock {
* window still at its limit; otherwise the usage's. It may have passed, which means the limit has reset. */
resetAt(engine: string): number | null {
const refused = this.guessed.has(engine) ? null : this.refusal(engine);
- const usage = this.usage.get(engine);
+ const usage = this.reading(engine);
if (refused === null) return usage?.resetsAt ?? null;
return usage?.blocked && usage.resetsAt !== null && usage.resetsAt > refused ? usage.resetsAt : refused;
}
@@ -134,7 +134,7 @@ export class LimitResetClock {
blockedUntil(engine: string): number | null {
const now = this.now();
const refused = this.refusal(engine);
- const usage = this.usage.get(engine);
+ const usage = this.reading(engine);
const until = Math.max(refused ?? 0, usage?.blocked && usage.resetsAt !== null ? usage.resetsAt : 0);
return until > now ? until : null;
}
@@ -144,7 +144,7 @@ export class LimitResetClock {
}
report(): EngineLimitsReport {
- const engines = new Map(this.usage);
+ const engines = new Map([...this.usage].filter(([engine]) => this.reading(engine)));
for (const engine of this.refusals.keys()) {
const until = this.blockedUntil(engine);
const known = engines.get(engine);
@@ -153,6 +153,13 @@ export class LimitResetClock {
return { checkedAt: this.checkedAt, engines: [...engines.values()] };
}
+ /** A usage reading counts only while its account's usage is read: one taken before its meter was turned off (or a
+ * report fetched before that) says nothing now. A refusal's hold is kept apart and still counts. */
+ private reading(engine: string): EngineLimit | undefined {
+ const usage = this.usage.get(engine);
+ return usage && !this.options.accounts().some((account) => account.id === engine && account.noUsage) ? usage : undefined;
+ }
+
/** Every runtime event, with the engine its turn runs on when known: a refusal holds the engine, and a turn that
* works on it afterwards ends the hold, since the account plainly has room again (another model, plan or account). */
noteRuntimeEvent(engine: string | undefined, event: RuntimeEvent): void {
diff --git a/server/plan-usage.test.ts b/server/plan-usage.test.ts
index 7980465452..2b92396473 100644
--- a/server/plan-usage.test.ts
+++ b/server/plan-usage.test.ts
@@ -630,4 +630,16 @@ describe("plan usage fetcher", () => {
expect(read.mock.calls.map(([account]) => account.id)).toEqual(["metered"]);
expect(report.providers.map((row) => row.id)).toEqual(["metered"]);
});
+
+ it("never serves a cached report across a usage-meter switch", async () => {
+ const read = vi.fn((_account: PlanAccount) => ({ token: SECRET, accountId: null, expired: false }));
+ const fetchImpl = vi.fn<PlanFetch>(async () => jsonResponse({}));
+ const load = (usageMeter: boolean) => loadPlanUsage({
+ accounts: planAccountsFromInstances({ claude: { driver: "claudeAgent", config: { usageMeter } } }),
+ fetch: fetchImpl, now: NOW, credentials: { read },
+ });
+ expect((await load(true)).providers.map((row) => row.id)).toEqual(["claude"]);
+ expect((await load(false)).providers).toEqual([]);
+ expect((await load(true)).providers.map((row) => row.id)).toEqual(["claude"]);
+ });
});
diff --git a/server/plan-usage.ts b/server/plan-usage.ts
index 3582577322..4f666b92a5 100644
--- a/server/plan-usage.ts
+++ b/server/plan-usage.ts
@@ -709,7 +709,7 @@ function planUsageCacheKey(accounts: PlanAccount[]): string {
.sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
.map(([name, value]) => `${name}=${value}`)
.join("\n");
- return [account.id, account.driver, account.name, account.configDir ?? "", environment].join("\u0000");
+ return [account.id, account.driver, account.name, account.configDir ?? "", environment, account.noUsage ? "no-usage" : "usage"].join("\u0000");
}).join("\n");
}