Skip to content

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

Limit holds: stop polling once the last hold ran out; keep an OpenCode Go hold while its month is full

AdityaUmale2d6d6a9f9aa6
diff --git a/server/limit-resets.test.ts b/server/limit-resets.test.ts
index 27695f8c9f..b3105bed21 100644
--- a/server/limit-resets.test.ts
+++ b/server/limit-resets.test.ts
@@ -456,6 +456,39 @@ describe("LimitResetClock and the turns that follow a refusal", () => {
instance.stop();
});
+ it("stops re-reading usage once the last hold ran out, a guessed one included", async () => {
+ vi.useFakeTimers({ now: NOW });
+ const loadUsage = vi.fn(async () => ({ ...report(claude([40, NOW + 3 * HOUR], [8, NOW + 5 * 24 * HOUR])), fetchedAt: iso(Date.now()) }));
+ const instance = new LimitResetClock({ accounts: () => accounts, loadUsage, waiting: () => false });
+ instance.start();
+ instance.noteLimit("claude", undefined);
+ await vi.advanceTimersByTimeAsync(6 * 60_000);
+ expect(loadUsage, "a guessed hold is polled too").toHaveBeenCalled();
+ // Nothing reads the hold meanwhile, so only the poll itself can notice it ran out.
+ await vi.advanceTimersByTimeAsync(2 * HOUR);
+ loadUsage.mockClear();
+ await vi.advanceTimersByTimeAsync(30 * 60_000);
+ expect(loadUsage).not.toHaveBeenCalled();
+ expect(instance.blockedUntil("claude"), "the guessed hold ran out").toBeNull();
+ instance.stop();
+ vi.useRealTimers();
+ });
+
+ it("keeps an OpenCode Go hold while its month is full, even with room in its rolling and weekly windows", () => {
+ const goAccounts = [{ id: "opencodeGo", name: "OpenCode", driver: "opencode" as const, environment: {} }];
+ const instance = new LimitResetClock({ accounts: () => goAccounts, now: () => NOW });
+ const go = (monthly: number) => row("opencodeGo", "opencode", parseOpenCodeUsage({ usage: {
+ rolling: { percent: 3, resetsAt: iso(NOW + 4 * HOUR) },
+ weekly: { percent: 8, resetsAt: iso(NOW + 5 * 24 * HOUR) },
+ monthly: { percent: monthly, resetsAt: iso(NOW + 20 * 24 * HOUR) },
+ } }));
+ instance.noteRefusal("opencodeGo", NOW + 23 * HOUR);
+ instance.noteUsage(report(go(100)));
+ expect(instance.blockedUntil("opencodeGo"), "a full month keeps it held").not.toBeNull();
+ instance.noteUsage(report(go(40)));
+ expect(instance.blockedUntil("opencodeGo")).toBeNull();
+ });
+
it("ends a hold at once on Try again now, saved across a restart, and a new refusal holds it again", () => {
const dir = mkdtempSync(join(tmpdir(), "limit-release-"));
try {
diff --git a/server/limit-resets.ts b/server/limit-resets.ts
index d8d263bdc0..61bd9114af 100644
--- a/server/limit-resets.ts
+++ b/server/limit-resets.ts
@@ -52,6 +52,8 @@ const SAME_RESET_MS = 5 * 60_000;
* not show, that refusal holds it again. A guessed hold (no reset named) has no time to compare and runs out in an hour. */
function holdGone(row: PlanProviderRow, limit: EngineLimit, held: number | undefined, guessed: boolean): boolean {
if (held === undefined || guessed || limit.blocked || limit.usedPercent === null || limit.usedPercent >= 100) return false;
+ // OpenCode Go also refuses on its month, which its row carries as an extra line: a full month keeps the hold.
+ if (row.driver === "opencode" && row.extra.some((line) => line.label === "Monthly" && line.usedPercent >= 100)) 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));
}
@@ -259,7 +261,7 @@ export class LimitResetClock {
start(): void {
if (this.timer) return;
// While an engine is held its usage is read every few minutes too, so a limit that reset early ends the hold.
- this.timer = setInterval(() => { if (this.refusals.size || this.options.waiting?.()) void this.ensureFresh(LIMIT_POLL_MS - 1_000); }, 60_000);
+ this.timer = setInterval(() => { if (this.holding() || this.options.waiting?.()) void this.ensureFresh(LIMIT_POLL_MS - 1_000); }, 60_000);
this.timer.unref?.();
}
@@ -276,6 +278,11 @@ export class LimitResetClock {
return loadPlanUsage({ accounts, refresh: force });
}
+ /** Whether any hold is still live; reading each one drops a hold that ran out, so polling stops with the last. */
+ private holding(): boolean {
+ return [...this.refusals.keys()].some((engine) => this.refusal(engine) !== null);
+ }
+
private refusal(engine: string): number | null {
const at = this.refusals.get(engine);
if (at === undefined) return null;