Skip to content

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

fix: retain image usage when the daily count cannot be saved

Afloat8658112b630b
diff --git a/server/image-daily-cap-persistence.test.ts b/server/image-daily-cap-persistence.test.ts
new file mode 100644
index 0000000000..d49511d1c7
--- /dev/null
+++ b/server/image-daily-cap-persistence.test.ts
@@ -0,0 +1,75 @@
+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import { createImageDailyCap, IMAGE_DAY_FILE } from "./image-daily-cap.ts";
+
+const roots: string[] = [];
+const fixture = () => {
+ const root = mkdtempSync(join(tmpdir(), "omb-image-cap-persistence-"));
+ roots.push(root);
+ vi.spyOn(console, "warn").mockImplementation(() => undefined);
+ return join(root, "data");
+};
+const now = () => new Date(2026, 10, 5, 12);
+
+afterEach(() => {
+ vi.restoreAllMocks();
+ for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
+});
+
+describe("image limits when the count cannot be saved", () => {
+ it("still caps successful images while the data directory is unavailable", () => {
+ const cap = createImageDailyCap(fixture(), { cap: 2, now });
+ for (let made = 0; made < 2; made += 1) {
+ expect(cap.reserve()).toBe(true);
+ cap.settle(true);
+ }
+ expect(cap.reserve()).toBe(false);
+ });
+
+ it("persists earlier unsaved images when writing succeeds again", () => {
+ const dir = fixture();
+ const cap = createImageDailyCap(dir, { cap: 3, now });
+ expect(cap.reserve()).toBe(true);
+ cap.settle(true);
+ mkdirSync(dir);
+ expect(cap.reserve()).toBe(true);
+ cap.settle(true);
+ expect(JSON.parse(readFileSync(join(dir, IMAGE_DAY_FILE), "utf8")))
+ .toEqual({ day: "2026-11-05", count: 2 });
+ });
+
+ it("does not forget images already counted when the saved file disappears", () => {
+ const dir = fixture();
+ mkdirSync(dir);
+ const cap = createImageDailyCap(dir, { cap: 2, now });
+ cap.reserve();
+ cap.settle(true);
+ rmSync(join(dir, IMAGE_DAY_FILE));
+ expect(cap.reserve()).toBe(true);
+ cap.settle(true);
+ expect(cap.reserve()).toBe(false);
+ });
+
+ it("resets unsaved counts at local midnight", () => {
+ let day = 5;
+ const cap = createImageDailyCap(fixture(), { cap: 1, now: () => new Date(2026, 10, day, 12) });
+ cap.reserve();
+ cap.settle(true);
+ expect(cap.reserve()).toBe(false);
+ day = 6;
+ expect(cap.reserve()).toBe(true);
+ });
+
+ it("still observes a larger saved count from the same day", () => {
+ const dir = fixture();
+ mkdirSync(dir);
+ const cap = createImageDailyCap(dir, { cap: 3, now });
+ cap.reserve();
+ cap.settle(true);
+ writeFileSync(join(dir, IMAGE_DAY_FILE), JSON.stringify({ day: "2026-11-05", count: 3 }));
+ expect(cap.reserve()).toBe(false);
+ });
+});
diff --git a/server/image-daily-cap.ts b/server/image-daily-cap.ts
index f58bf1e3ea..fa268435bf 100644
--- a/server/image-daily-cap.ts
+++ b/server/image-daily-cap.ts
@@ -26,14 +26,17 @@ export function createImageDailyCap(dataDir: string, options: { cap?: number; no
const now = options.now ?? (() => new Date());
const file = join(dataDir, IMAGE_DAY_FILE);
let held = 0;
+ let countedDay = "";
+ let count = 0;
const madeToday = (): number => {
+ const today = localDay(now());
+ if (countedDay !== today) { countedDay = today; count = 0; }
try {
const saved = JSON.parse(readFileSync(file, "utf8")) as { day?: unknown; count?: unknown } | null;
- const count = saved?.count;
- return saved?.day === localDay(now()) && typeof count === "number" && Number.isInteger(count) && count > 0 ? count : 0;
- } catch {
- return 0;
- }
+ const savedCount = saved?.count;
+ if (saved?.day === today && typeof savedCount === "number" && Number.isInteger(savedCount) && savedCount > count) count = savedCount;
+ } catch {}
+ return count;
};
return {
cap,
@@ -45,8 +48,9 @@ export function createImageDailyCap(dataDir: string, options: { cap?: number; no
settle(made) {
held = Math.max(0, held - 1);
if (!made) return;
+ count = madeToday() + 1;
try {
- writeFileAtomic(file, `${JSON.stringify({ day: localDay(now()), count: madeToday() + 1 })}\n`, { mode: 0o600 });
+ writeFileAtomic(file, `${JSON.stringify({ day: countedDay, count })}\n`, { mode: 0o600 });
} catch (error) {
console.warn(`image generation: could not count today's image in ${file}: ${error instanceof Error ? error.message : String(error)}`);
}