Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix: end a Codex limit hold once fresh usage shows the window that refused is gone (#4281)
Aditya Umalefb9948191efa
diff --git a/server/limit-resets.test.ts b/server/limit-resets.test.tsindex 6ebd48cdd0..00427efeab 100644--- a/server/limit-resets.test.ts+++ b/server/limit-resets.test.ts@@ -307,6 +307,57 @@ describe("LimitResetClock and the turns that follow a refusal", () => {expect(instance.resetAt("claude"), "a past time is no reset time").toBeNull();});+ it("ends a Codex hold once fresh usage shows room and no window resetting at the hold's time, as after a plan upgrade", () => {+ const codexAccounts = [{ id: "codex", name: "Codex", driver: "codex" as const, environment: {} }];+ const dir = mkdtempSync(join(tmpdir(), "omb-limit-holds-"));+ try {+ const file = join(dir, "limit-holds.json");+ const boot = () => new LimitResetClock({ accounts: () => codexAccounts, now: () => NOW, file });+ const first = boot();+ first.noteRefusal("codex", NOW + 23 * HOUR);+ first.noteUsage(report(codex([99, NOW + 2 * HOUR], [99, NOW + 23 * HOUR + 30_000])));+ expect(first.blockedUntil("codex"), "usage still showing the window that refused keeps the hold").toBe(NOW + 23 * HOUR);+ const restarted = boot();+ expect(restarted.blockedUntil("codex"), "the hold outlives a restart").toBe(NOW + 23 * HOUR);+ restarted.noteUsage(report(codex([3, NOW + 4 * HOUR], [8, NOW + 5 * 24 * HOUR])));+ expect(restarted.blockedUntil("codex")).toBeNull();+ expect(restarted.limit("codex")).toEqual(expect.objectContaining({ blocked: false, usedPercent: 8 }));+ expect(boot().blockedUntil("codex"), "and stays gone after the next restart").toBeNull();+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ });++ it("keeps a Codex hold when the usage was read before the refusal, as a cached read can be", () => {+ const { instance } = clock();+ instance.noteRefusal("codex", NOW + 23 * HOUR);+ const room = codex([3, NOW + 4 * HOUR], [8, NOW + 5 * 24 * HOUR]);+ instance.noteUsage({ fetchedAt: iso(NOW - 30_000), providers: [room] });+ expect(instance.blockedUntil("codex"), "a read from before the refusal").toBe(NOW + 23 * HOUR);+ instance.noteUsage({ fetchedAt: iso(NOW), providers: [room] });+ expect(instance.blockedUntil("codex"), "a read from after it").toBeNull();+ });++ it("counts a Codex hold from its latest refusal, so a read between two refusals with the same reset does not end it", () => {+ let now = NOW;+ const instance = new LimitResetClock({ accounts: () => [{ id: "codex", name: "Codex", driver: "codex" as const, environment: {} }], now: () => now });+ instance.noteRefusal("codex", NOW + 23 * HOUR);+ now = NOW + 60_000;+ instance.noteRefusal("codex", NOW + 23 * HOUR);+ const room = codex([3, NOW + 4 * HOUR], [8, NOW + 5 * 24 * HOUR]);+ instance.noteUsage({ fetchedAt: iso(NOW + 30_000), providers: [room] });+ expect(instance.blockedUntil("codex"), "a read from before the second refusal").toBe(NOW + 23 * HOUR);+ instance.noteUsage({ fetchedAt: iso(NOW + 60_000), providers: [room] });+ expect(instance.blockedUntil("codex"), "a read from after it").toBeNull();+ });++ it("keeps a Claude hold whatever its usage shows, since Claude refuses on limits those windows do not show", () => {+ const { instance } = clock();+ instance.noteRefusal("claude", NOW + 23 * HOUR);+ instance.noteUsage(report(claude([3, NOW + 4 * HOUR], [8, NOW + 5 * 24 * HOUR])));+ expect(instance.blockedUntil("claude")).toBe(NOW + 23 * HOUR);+ });+it("never holds the engine for one model's own limit", () => {const { instance } = clock();instance.noteRuntimeEvent("claude", { ...refused("t1", NOW + 50 * HOUR), limit: "model" } as RuntimeEvent);diff --git a/server/limit-resets.ts b/server/limit-resets.tsindex bf3ecc1600..58820daeec 100644--- a/server/limit-resets.ts+++ b/server/limit-resets.ts@@ -38,6 +38,18 @@ export function engineLimitFromUsage(row: PlanProviderRow): EngineLimit | null {return { ...base, window: fullest.window, usedPercent: fullest.used, resetsAt: fullest.resetsAt, blocked: false };}+/** How far apart a refusal's reset and its window's own reset can be: a refusal names its minute, usage the second. */+const SAME_RESET_MS = 5 * 60_000;++/** A Codex refusal's hold that fresh usage shows is over: no window at its limit and none resetting at the hold's time,+ * so the window that refused is gone (a plan upgrade, a reset Codex moved). Every new turn goes to another AI while the+ * engine is held, so no turn could end the hold. Claude keeps its holds: it also refuses on limits these windows do not show. */+function codexHoldGone(row: PlanProviderRow, limit: EngineLimit, held: number | undefined, guessed: boolean): boolean {+ if (held === undefined || guessed || limit.driver !== "codex" || limit.blocked || limit.usedPercent === null) return false;+ const windows = [windowLine("5-hour", row.fiveHour), windowLine("weekly", row.weekly)];+ return windows.every((line) => line === null || (line.resetsAt !== null && Math.abs(line.resetsAt - held) > SAME_RESET_MS));+}+const PRODUCT_NAMES: Record<string, string> = { claudeAgent: "Claude", codex: "Codex", grokAgent: "Grok" };/** "Claude", "Codex", or the name the person gave that engine. */@@ -90,6 +102,8 @@ export class LimitResetClock {private readonly refusals = new Map<string, number>();/** Engines held by a refusal that named no reset time: the hold is a guess, never shown as a reset. */private readonly guessed = new Set<string>();+ /** When each hold began: a usage read from before it (a cached one) says nothing about whether it is over. */+ private readonly heldAt = new Map<string, number>();private readonly turnEngines = new Map<string, string>();private checkedAt = 0;private loading: Promise<void> | null = null;@@ -163,7 +177,9 @@ export class LimitResetClock {/** A turn on this engine was refused by its usage limit; `resetsAt` is when the engine said it resets. */noteRefusal(engine: string | undefined, resetsAt: number | undefined): void {- if (!engine || resetsAt === undefined || !Number.isFinite(resetsAt) || resetsAt <= this.now() || (this.refusals.get(engine) === resetsAt && !this.guessed.has(engine))) return;+ if (!engine || resetsAt === undefined || !Number.isFinite(resetsAt) || resetsAt <= this.now()) return;+ // Without this refresh, a usage read between two refusals would end the hold.+ if (this.refusals.get(engine) === resetsAt && !this.guessed.has(engine)) { this.heldAt.set(engine, this.now()); return; }this.hold(engine, resetsAt, false);}@@ -171,6 +187,7 @@ export class LimitResetClock {if (guessed) this.guessed.add(engine);else this.guessed.delete(engine);this.refusals.set(engine, until);+ this.heldAt.set(engine, this.now());this.saveHolds();this.options.onChange?.();if (this.soon) clearTimeout(this.soon);@@ -180,10 +197,18 @@ export class LimitResetClock {noteUsage(report: PlanUsageReport): void {const next = new Map<string, EngineLimit>();+ const fetchedAt = Date.parse(report.fetchedAt);+ let released = false;for (const row of report.providers) {const limit = engineLimitFromUsage(row);- if (limit) next.set(limit.engine, limit);+ if (!limit) continue;+ next.set(limit.engine, limit);+ if (fetchedAt >= (this.heldAt.get(limit.engine) ?? 0) && codexHoldGone(row, limit, this.refusals.get(limit.engine), this.guessed.has(limit.engine))) {+ this.refusals.delete(limit.engine);+ released = true;+ }}+ if (released) this.saveHolds();this.usage = next;this.checkedAt = this.now();this.options.onChange?.();