Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Report a free OpenCode model's own reset time, not the Go meter's
Bugfixer 275ae171625f9
diff --git a/server/host/limit-takeover.test.ts b/server/host/limit-takeover.test.tsindex ad61a4e609..5d8c949e25 100644--- a/server/host/limit-takeover.test.ts+++ b/server/host/limit-takeover.test.ts@@ -68,11 +68,15 @@ describe("which AI carries on while one is limited", () => {const clock = new LimitResetClock({ accounts: () => [{ id: "opencode", name: "OpenCode", driver: "opencode", environment: {} }], now: () => NOW });clock.noteUsage({ fetchedAt: new Date(NOW).toISOString(), providers: [{ id: "opencode", name: "OpenCode", driver: "opencode", ok: true, error: null,...parseOpenCodeUsage({ usage: { rolling: { percent: 100, resetsAt: new Date(NOW + HOUR).toISOString() }, weekly: { percent: 50, resetsAt: new Date(NOW + 48 * HOUR).toISOString() } } }) }] });- const { instance } = takeover({ engines: () => [openCode, CLAUDE, CODEX], blockedUntil: (id, model) => clock.blockedUntil(id, model), resetAt: (id) => clock.resetAt(id) });+ const { instance } = takeover({ engines: () => [openCode, CLAUDE, CODEX], blockedUntil: (id, model) => clock.blockedUntil(id, model), resetAt: (id, model) => clock.resetAt(id, model) });expect(instance.heldAtStart(KAI, on("opencode", "opencode/big-pickle")), "a free model has its own allowance").toBeNull();expect(instance.heldAtStart(KAI, on("opencode", "opencode-go/kimi-k2"))).toEqual({ scope: "engine", resetsAt: NOW + HOUR });const chosen = takeover({ engines: () => [openCode, CLAUDE, CODEX], blockedUntil: (id, model) => clock.blockedUntil(id, model), chosen: () => [on("opencode", "opencode/big-pickle")] }).instance;expect(chosen.backups("kai", on("claude", "claude-opus-5-5"))[0], "a free model the person chose still carries on for another AI").toEqual(on("opencode", "opencode/big-pickle"));+ clock.noteRefusal("opencode", NOW + HOUR / 2);+ const free = instance.heldAtStart(KAI, on("opencode", "opencode/big-pickle"));+ expect(free?.resetsAt, "a refused free model waits for the refusal's reset, not the Go meter's later one").toBe(clock.blockedUntil("opencode", "opencode/big-pickle"));+ expect(free?.resetsAt).toBeLessThan(NOW + HOUR);});it("never picks an AI that bills each use by itself, only as a backup the person chose", () => {diff --git a/server/host/limit-takeover.ts b/server/host/limit-takeover.tsindex e63c1328b5..46d6c223a1 100644--- a/server/host/limit-takeover.ts+++ b/server/host/limit-takeover.ts@@ -53,7 +53,7 @@ export interface LimitTakeoverDeps {/** Until when this engine refuses work, or null. */blockedUntil(instanceId: string, model?: string): number | null;/** When this engine's limit resets, when that is known. */- resetAt?(instanceId: string): number | null;+ resetAt?(instanceId: string, model?: string): number | null;/** The backups the person chose for this bot, or for every bot. */chosen(botId: string): ModelSelection[];/** The backups a signed-out AI's turns go to, as for a turn its AI never started: the bot's own list, or the@@ -259,7 +259,7 @@ export function createLimitTakeover(deps: LimitTakeoverDeps) {if (!scope) return null;const carries = (deps.enabled?.() ?? true) && backups(bot.id, selection, scope, approvalModeFor(bot)).length > 0;if (!carries && !(recovering && scope !== "signed-out")) return null;- const resetsAt = scope === "engine" ? deps.resetAt?.(selection.instanceId) ?? undefined : model?.resetsAt;+ const resetsAt = scope === "engine" ? deps.resetAt?.(selection.instanceId, selection.model) ?? undefined : model?.resetsAt;return { scope, ...(resetsAt && resetsAt > now() ? { resetsAt } : {}) };},/** Every runtime event of a bot's thread, with the model its turn ran on: a model's own limit holds that model, adiff --git a/server/index.ts b/server/index.tsindex b28a9e58c0..07c4d808fb 100644--- a/server/index.ts+++ b/server/index.ts@@ -8776,7 +8776,7 @@ async function stopBotForEmergencyApprovalDowngrade(botId: string): Promise<voidconst engineName = (engine: string) => engineLabel(engine, registry.get(engine) ?? undefined);const limitClock = new LimitResetClock({ file: join(DATA_DIR, "limit-holds.json"), accounts: () => planAccountsFromInstances(instanceConfigs(cfg)), onChange: () => routines?.retimeLimitResets(),- waiting: () => Boolean(routines?.listRoutines().some((routine) => routine.enabled && routine.schedule.type === "limit_reset")) }); const limitTakeover = createLimitTakeover({ file: join(DATA_DIR, "limit-notices.json"), holdsFile: join(DATA_DIR, "limit-takeover-holds.json"), configs: () => instanceConfigs(cfg), context: selectionContext, blockedUntil: (engine, model) => limitClock.blockedUntil(engine, model), resetAt: (engine) => limitClock.resetAt(engine), enabled: () => quotaSwitchEnabled(), where: CLOUD_HOME ? "on the Cloud" : "on this computer", signedOutNow: (id) => registry.knownSnapshot(id)?.authenticated === false, signedOutNoticed: () => registry.forgetSnapshots(), botsOn: (id) => store.bots.filter((bot) => !bot.hidden && bot.modelSelection.instanceId === id), engineName, engines: () => selectableInstances(registry.instances().map((engine) => ({ instanceId: engine.instanceId, driverKind: engine.driverKind, displayName: engine.displayName, enabled: engine.enabled, access: registry.access(engine.instanceId), models: engine.models, snapshot: registry.knownSnapshot(engine.instanceId) ?? { state: "unavailable" as const, reason: "not read yet" }, capabilities: engine.adapter.capabilities }))), chosen: (botId) => [...(store.bot(botId)?.fallback ?? []), ...(cfg.automaticRecovery?.backup ? [cfg.automaticRecovery.backup] : [])], signedOutChosen: (botId) => { const bot = store.bot(botId); return bot ? startupBackups(bot, cfg.automaticRecovery) : []; }, tell: (about, text) => notify(buildNoticeNotification(about, store.bot(about.id)?.threadId ?? "", text)), record: (failover) => { limitFailovers.record(failover); perf.noteFailover(failover.threadId, failover.why === "signed-out" ? "signed-out" : "limit"); } }); registry.onSignedIn((instanceId) => limitTakeover.signedIn(instanceId));+ waiting: () => Boolean(routines?.listRoutines().some((routine) => routine.enabled && routine.schedule.type === "limit_reset")) }); const limitTakeover = createLimitTakeover({ file: join(DATA_DIR, "limit-notices.json"), holdsFile: join(DATA_DIR, "limit-takeover-holds.json"), configs: () => instanceConfigs(cfg), context: selectionContext, blockedUntil: (engine, model) => limitClock.blockedUntil(engine, model), resetAt: (engine, model) => limitClock.resetAt(engine, model), enabled: () => quotaSwitchEnabled(), where: CLOUD_HOME ? "on the Cloud" : "on this computer", signedOutNow: (id) => registry.knownSnapshot(id)?.authenticated === false, signedOutNoticed: () => registry.forgetSnapshots(), botsOn: (id) => store.bots.filter((bot) => !bot.hidden && bot.modelSelection.instanceId === id), engineName, engines: () => selectableInstances(registry.instances().map((engine) => ({ instanceId: engine.instanceId, driverKind: engine.driverKind, displayName: engine.displayName, enabled: engine.enabled, access: registry.access(engine.instanceId), models: engine.models, snapshot: registry.knownSnapshot(engine.instanceId) ?? { state: "unavailable" as const, reason: "not read yet" }, capabilities: engine.adapter.capabilities }))), chosen: (botId) => [...(store.bot(botId)?.fallback ?? []), ...(cfg.automaticRecovery?.backup ? [cfg.automaticRecovery.backup] : [])], signedOutChosen: (botId) => { const bot = store.bot(botId); return bot ? startupBackups(bot, cfg.automaticRecovery) : []; }, tell: (about, text) => notify(buildNoticeNotification(about, store.bot(about.id)?.threadId ?? "", text)), record: (failover) => { limitFailovers.record(failover); perf.noteFailover(failover.threadId, failover.why === "signed-out" ? "signed-out" : "limit"); } }); registry.onSignedIn((instanceId) => limitTakeover.signedIn(instanceId));const limitFailovers = new LimitFailovers({ file: join(DATA_DIR, "limit-failovers.json"), blockedUntil: (engine, model) => limitClock.blockedUntil(engine, model), signedOut: (engine) => limitTakeover.stillSignedOut(engine), engineName,threadModel: (botId, threadId) => store.projectBotForTask(botId, threadId)?.modelSelection ?? null,note: (threadId, line, text) => store.appendMessage(threadId, { role: "bot", kind: "activity", tool: { name: `recovery: ${line}`, ok: true, ...(text ? { nameCode: text.key, nameParams: text.params } : {}) } }),diff --git a/server/limit-resets.test.ts b/server/limit-resets.test.tsindex f7dcec962d..1d56642667 100644--- a/server/limit-resets.test.ts+++ b/server/limit-resets.test.ts@@ -529,6 +529,8 @@ describe("LimitResetClock and the turns that follow a refusal", () => {expect(instance.blockedUntil("opencodeGo", "opencode/big-pickle")).toBeNull();instance.noteRefusal("opencodeGo", NOW + 2 * HOUR);expect(instance.blockedUntil("opencodeGo", "opencode/big-pickle"), "a refusal of the whole account still holds every model").not.toBeNull();+ expect(instance.resetAt("opencodeGo", "opencode/big-pickle"), "a free model resets with the refusal, not the Go meter").toBeLessThan(NOW + 4 * HOUR);+ expect(instance.resetAt("opencodeGo", "opencode-go/kimi-k2")).toBe(NOW + 4 * HOUR);});it("still holds every model of an engine other than OpenCode at a full window", () => {diff --git a/server/limit-resets.ts b/server/limit-resets.tsindex 3c552b5186..608a71c278 100644--- a/server/limit-resets.ts+++ b/server/limit-resets.ts@@ -131,9 +131,9 @@ export class LimitResetClock {/** The reset a limit-reset automation for this engine waits for: a refusal's own time unless usage shows a later* window still at its limit; otherwise the usage's. It may have passed, which means the limit has reset. */- resetAt(engine: string): number | null {+ resetAt(engine: string, model?: string): number | null {const refused = this.guessed.has(engine) ? null : this.refusal(engine);- const usage = this.reading(engine);+ const usage = this.meterFor(engine, model);if (refused === null) return usage?.resetsAt ?? null;return usage?.blocked && usage.resetsAt !== null && usage.resetsAt > refused ? usage.resetsAt : refused;}@@ -143,12 +143,18 @@ export class LimitResetClock {blockedUntil(engine: string, model?: string): number | null {const now = this.now();const refused = this.refusal(engine);- const usage = this.reading(engine);- const meterHolds = usage?.driver !== "opencode" || !model || (typedOpenCodeModel(model) ?? model).startsWith(OPENCODE_GO_MODEL_PREFIX);- const until = Math.max(refused ?? 0, meterHolds && usage?.blocked && usage.resetsAt !== null ? usage.resetsAt : 0);+ const usage = this.meterFor(engine, model);+ const until = Math.max(refused ?? 0, usage?.blocked && usage.resetsAt !== null ? usage.resetsAt : 0);return until > now ? until : null;}+ /** The engine's usage reading when its meter covers `model` (every model when none is named): OpenCode's meter is+ * the Go subscription's and never covers Zen's free models. */+ private meterFor(engine: string, model?: string): EngineLimit | undefined {+ const usage = this.reading(engine);+ return usage?.driver !== "opencode" || !model || (typedOpenCodeModel(model) ?? model).startsWith(OPENCODE_GO_MODEL_PREFIX) ? usage : undefined;+ }+limit(engine: string): EngineLimit | undefined {return this.report().engines.find((candidate) => candidate.engine === engine);}