Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
perf(store): skip superseded saves during deferred flushes
Afloat5e2e84eb6d89
diff --git a/server/registry-store-narrow.test.ts b/server/registry-store-narrow.test.tsindex 012db255ba..2a3360149d 100644--- a/server/registry-store-narrow.test.ts+++ b/server/registry-store-narrow.test.ts@@ -49,6 +49,20 @@ describe("bots.json saves that name their bot serialize only that bot", () => {expect(onDisk()).toBe(JSON.stringify(persistedBots(bots)));});+ it("serializes the registry once when a whole pass and a deferred thread save are both waiting", () => {+ const registry = new JsonRegistry();+ const bots = Array.from({ length: 100 }, (_, index) => bot(`bot-${index}`));+ const save = () => registry.writeBots(bots, true);+ save();+ bots[0]!.name = "Renamed";+ registry.upsertThread(owner(bots[0]!.id), undefined, "now", save);+ bots[1]!.tasks![0]!.title = "Deferred change";+ registry.upsertThread(owner(bots[1]!.id), bots[1]!.threadId, "later", save);++ expect(serialized(bots, flushLaterSaves)).toHaveLength(bots.length);+ expect(onDisk()).toBe(JSON.stringify(persistedBots(bots)));+ });+it("writes a change made in place to a bot no save named with the whole pass that follows a narrowed save", () => {const registry = new JsonRegistry();const bots = [bot("a"), bot("b")];diff --git a/server/save-batch.test.ts b/server/save-batch.test.tsindex e6ea8c90ab..05d82bf42e 100644--- a/server/save-batch.test.ts+++ b/server/save-batch.test.ts@@ -85,6 +85,21 @@ describe("saveLater", () => {expect(writes).toEqual([]);});+ it("preserves a newer write queued while the current flush is in progress", () => {+ const writes: string[] = [];+ saveLater("a", () => {+ writes.push("a");+ saveLater("b", () => writes.push("b-current"));+ });+ saveLater("b", () => writes.push("b-stale"));+ flushLaterSaves();+ expect(writes).toEqual(["a"]);+ expect(waitingSaves()).toBe(1);+ flushLaterSaves();+ expect(writes).toEqual(["a", "b-current"]);+ expect(waitingSaves()).toBe(0);+ });+it("keeps a failed write waiting and tries it again", async () => {const error = vi.spyOn(console, "error").mockImplementation(() => {});let fail = true;diff --git a/server/save-batch.ts b/server/save-batch.tsindex 0a379f7a39..f040730d8d 100644--- a/server/save-batch.ts+++ b/server/save-batch.ts@@ -91,6 +91,7 @@ export function flushLaterSaves(): void {const started = performance.now();let failed = false;for (const [owner, write] of Array.from(later)) { // a snapshot: a failed write is put back and must not be met again here+ if (later.get(owner) !== write) continue;later.delete(owner);try {write();@@ -103,5 +104,5 @@ export function flushLaterSaves(): void {lastLaterWriteMs = performance.now() - started;laterFailures = failed ? laterFailures + 1 : 0;// After SAVE_LATER_RETRIES failures in a row the write waits for the file's next save or the exit, instead of a timer.- if (later.size && laterFailures <= SAVE_LATER_RETRIES) laterTimer = setTimeout(flushLaterSaves, SAVE_LATER_MAX_MS);+ if (later.size && laterFailures <= SAVE_LATER_RETRIES) laterTimer ??= setTimeout(flushLaterSaves, SAVE_LATER_MAX_MS);}