Skip to content

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

fix(config): retain the rollback copy after a clock correction

Afloat64a33b084cf5
diff --git a/server/config-backup-clock.test.ts b/server/config-backup-clock.test.ts
new file mode 100644
index 0000000000..cea5b32bec
--- /dev/null
+++ b/server/config-backup-clock.test.ts
@@ -0,0 +1,61 @@
+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, describe, expect, it } from "vitest";
+import { CONFIG_BACKUP_KEEP, listConfigBackups, restoreConfigBackup, writeConfigBackup } from "./config-backup.ts";
+
+const dirs: string[] = [];
+afterEach(() => { for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); });
+function fixture() {
+ const dir = mkdtempSync(join(tmpdir(), "omb-backup-clock-"));
+ dirs.push(dir);
+ const path = join(dir, "config.json");
+ const names = Array.from({ length: CONFIG_BACKUP_KEEP }, (_, i) => writeConfigBackup(path, JSON.stringify({ saved: i }), new Date(Date.UTC(2099, 0, 1, 0, 0, i)))!);
+ return { dir, path, names };
+}
+
+describe("config rollback copies after a clock correction", () => {
+ it("keeps the just-written copy even when its timestamp sorts before every old copy", () => {
+ const { dir, path, names } = fixture();
+ const text = JSON.stringify({ apiKey: "fixture-current-key", settings: "current" });
+ const name = writeConfigBackup(path, text, new Date("2026-11-17T00:00:00Z"));
+ expect(name).toBeTruthy();
+ expect(existsSync(join(dir, name!))).toBe(true);
+ expect(readFileSync(join(dir, name!), "utf8")).toBe(text);
+ expect(listConfigBackups(dir)).toHaveLength(CONFIG_BACKUP_KEEP);
+ expect(existsSync(join(dir, names[0]!))).toBe(false);
+ expect(existsSync(join(dir, names.at(-1)!))).toBe(true);
+ });
+
+ it("returns a real undo copy when restoring among future-dated backups", () => {
+ const { dir, path, names } = fixture();
+ const current = JSON.stringify({ apiKey: "fixture-current-key", settings: "before restore" });
+ writeFileSync(path, current);
+ const restored = restoreConfigBackup(dir, names.at(-1)!);
+ expect(restored.ok).toBe(true);
+ if (!restored.ok || !restored.kept) throw new Error("expected the replaced settings to be kept");
+ expect(readFileSync(path, "utf8")).toBe(JSON.stringify({ saved: CONFIG_BACKUP_KEEP - 1 }));
+ expect(existsSync(restored.kept)).toBe(true);
+ expect(readFileSync(restored.kept, "utf8")).toBe(current);
+ expect(listConfigBackups(dir)).toHaveLength(CONFIG_BACKUP_KEEP);
+ });
+
+ it("keeps each current rollback copy and the retention bound across repeated backward saves", () => {
+ const { dir, path } = fixture();
+ for (let day = 20; day >= 17; day--) {
+ const text = JSON.stringify({ revision: day });
+ const name = writeConfigBackup(path, text, new Date(Date.UTC(2026, 10, day)));
+ expect(existsSync(join(dir, name!))).toBe(true);
+ expect(readFileSync(join(dir, name!), "utf8")).toBe(text);
+ expect(listConfigBackups(dir)).toHaveLength(CONFIG_BACKUP_KEEP);
+ }
+ });
+
+ it("still removes the oldest existing copy when the clock advances", () => {
+ const { dir, path, names } = fixture();
+ const name = writeConfigBackup(path, "latest", new Date("2100-01-01T00:00:00Z"));
+ expect(readFileSync(join(dir, name!), "utf8")).toBe("latest");
+ expect(existsSync(join(dir, names[0]!))).toBe(false);
+ expect(listConfigBackups(dir)).toHaveLength(CONFIG_BACKUP_KEEP);
+ });
+});
diff --git a/server/config-backup.ts b/server/config-backup.ts
index 39ac2724c6..9b25bcb0d4 100644
--- a/server/config-backup.ts
+++ b/server/config-backup.ts
@@ -104,7 +104,7 @@ export function writeConfigBackup(path: string, text: string, now: Date = new Da
} catch {
return null;
}
- pruneConfigBackups(dir);
+ pruneConfigBackups(dir, name);
return name;
}
return null;
@@ -138,9 +138,12 @@ export function listConfigBackups(dir: string): ConfigBackup[] {
});
}
-function pruneConfigBackups(dir: string): void {
+function pruneConfigBackups(dir: string, justWritten: string): void {
const copies = listConfigBackups(dir);
- for (const copy of copies.slice(0, Math.max(0, copies.length - CONFIG_BACKUP_KEEP))) {
+ // A corrected clock may put this save before every existing timestamp. The
+ // copy that makes the imminent overwrite reversible must survive retention.
+ const removable = copies.filter((copy) => copy.name !== justWritten);
+ for (const copy of removable.slice(0, Math.max(0, copies.length - CONFIG_BACKUP_KEEP))) {
try {
unlinkSync(copy.path);
} catch {