Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(store): retry deferred thread-cache eviction during reads
Afloateb74c5518281
diff --git a/server/thread-cache-read-budget.test.ts b/server/thread-cache-read-budget.test.tsnew file mode 100644index 0000000000..15c4f878e2--- /dev/null+++ b/server/thread-cache-read-budget.test.ts@@ -0,0 +1,125 @@+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { rmSync } from "node:fs";+import { DATA_DIR } from "./config.ts";+import { closeMessageDb } from "./message-db.ts";+import { approxBytes, ThreadCache } from "./thread-cache.ts";+import { Store, type Message } from "./store.ts";++const START = 1_000_000;+const rows = (id: string) => ({ messages: [{ id, at: 1, role: "user", kind: "text", text: "x".repeat(10_000) } as Message], activeLeafId: id });++describe("read-only access after transcript cache protections expire", () => {+ beforeEach(() => { vi.useFakeTimers({ toFake: ["Date"] }); vi.setSystemTime(START); });+ afterEach(() => vi.useRealTimers());++ it("retries byte eviction after a burst of recent loads, keeping the requested transcript", () => {+ const cache = new ThreadCache(() => new Set(), 25_000);+ const states = ["a", "b", "c", "d"].map((id) => cache.load(id, rows(id)));+ expect(cache.size).toBe(4); // all are inside the minimum residency window+ vi.setSystemTime(START + 5_001);++ expect(cache.get("d")).toBe(states[3]);++ expect(cache.approxBytes).toBeLessThanOrEqual(25_000);+ expect(cache.size).toBe(2);+ expect(cache.get("a")).toBeUndefined();+ expect(cache.get("b")).toBeUndefined();+ expect(cache.get("c")).toBe(states[2]);+ });++ it("reclaims a formerly busy transcript after its turn ends without requiring another write", () => {+ const busy = new Set(["busy"]);+ const cache = new ThreadCache(() => busy, 15_000);+ const work = cache.load("busy", rows("busy"));+ const shown = cache.load("shown", rows("shown"));+ vi.setSystemTime(START + 5_001);+ expect(cache.get("shown")).toBe(shown);+ expect(cache.size).toBe(2); // the other transcript is still busy+ busy.clear();+ vi.setSystemTime(START + 10_002);++ expect(cache.get("shown")).toBe(shown);++ expect(cache.get("busy")).toBeUndefined();+ expect(cache.approxBytes).toBeLessThanOrEqual(15_000);+ expect(work.messages[0]!.text).toHaveLength(10_000); // eviction never mutates a caller's value+ });++ it("refreshes the requested entry before choosing the least recently used victim", () => {+ const cache = new ThreadCache(() => new Set(), 25_000);+ const oldest = cache.load("oldest", rows("oldest"));+ cache.load("middle", rows("middle"));+ const newest = cache.load("newest", rows("newest"));+ vi.setSystemTime(START + 5_001);++ expect(cache.get("oldest")).toBe(oldest);++ expect(cache.get("middle")).toBeUndefined();+ expect(cache.get("newest")).toBe(newest);+ expect(cache.approxBytes).toBe(approxBytes(oldest.messages[0]) + approxBytes(newest.messages[0]));+ });++ it("does not shorten recent-use protection or evict a busy/current transcript", () => {+ const busy = new Set(["busy"]);+ const cache = new ThreadCache(() => busy, 1_000);+ const work = cache.load("busy", rows("busy"));+ const current = cache.load("current", rows("current"));+ vi.setSystemTime(START + 4_999);+ expect(cache.get("current")).toBe(current);+ expect(cache.size).toBe(2);+ vi.setSystemTime(START + 10_000);+ expect(cache.get("current")).toBe(current);+ expect(cache.get("busy")).toBe(work);+ expect(cache.size).toBe(2);+ });++ it("bounds deferred-eviction scans while all over-budget transcripts are protected", () => {+ const busy = new Set(["busy"]);+ const busyIds = vi.fn(() => busy);+ const cache = new ThreadCache(busyIds, 1_000);+ cache.load("busy", rows("busy"));+ cache.load("current", rows("current"));+ busyIds.mockClear();+ vi.setSystemTime(START + 5_001);+ for (let read = 0; read < 100; read++) expect(cache.get("current")).toBeDefined();+ expect(busyIds).toHaveBeenCalledTimes(1);+ });+});++it("Store reloads an evicted transcript with the same messages and active leaf", () => {+ rmSync(DATA_DIR, { recursive: true, force: true });+ vi.useFakeTimers({ toFake: ["Date"] });+ vi.setSystemTime(START);+ try {+ const store = new Store(() => ({ instanceId: "claude", model: "claude-sonnet-5" }));+ const cache = new ThreadCache(() => new Set(), 25_000);+ (store as unknown as { threads: ThreadCache }).threads = cache;+ const bot = store.createBot({ name: "Cache fixture" }, { seedMessages: false });+ const second = store.createTask(bot.id, "Second", false)!;+ const third = store.createTask(bot.id, "Third", false)!;+ for (const thread of [bot.threadId, second.threadId, third.threadId]) {+ store.appendMessage(thread, { role: "user", kind: "text", text: "x".repeat(10_000) });+ }+ const secondBefore = store.messagesFor(second.threadId);+ const secondExpected = structuredClone(secondBefore);+ const secondLeaf = store.activeLeaf(second.threadId);+ const before = store.messagesFor(bot.threadId);+ const leaf = store.activeLeaf(bot.threadId);+ const expected = structuredClone(before);+ vi.setSystemTime(START + 5_001);+ store.messagesFor(third.threadId);+ expect(cache.approxBytes).toBeLessThanOrEqual(25_000);+ expect(cache.get(second.threadId)).toBeUndefined();+ // Read the evicted conversation through Store, which reloads messages.db.+ const reloaded = store.messagesFor(second.threadId);+ expect(reloaded).not.toBe(secondBefore);+ expect(reloaded).toEqual(secondExpected);+ expect(store.activeLeaf(second.threadId)).toBe(secondLeaf);+ expect(store.messagesFor(bot.threadId)).toEqual(expected);+ expect(store.activeLeaf(bot.threadId)).toBe(leaf);+ } finally {+ vi.useRealTimers();+ closeMessageDb();+ rmSync(DATA_DIR, { recursive: true, force: true });+ }+});diff --git a/server/thread-cache.ts b/server/thread-cache.tsindex 9d9e5a6e06..6852d89300 100644--- a/server/thread-cache.ts+++ b/server/thread-cache.ts@@ -29,6 +29,7 @@ interface Entry { state: ThreadState; usedAt: number; bytes: number }export class ThreadCache {private entries = new Map<string, Entry>();private sweptAt = Date.now();+ private nextBudgetRetryAt = 0;private bytes = 0;private readonly busyThreadIds: () => ReadonlySet<string>;private readonly maxBytes: number;@@ -47,6 +48,9 @@ export class ThreadCache {entry.usedAt = now;this.entries.delete(threadId);this.entries.set(threadId, entry);+ // A burst of loads or busy turns can defer every eviction. Reads must+ // retry once those protections expire, even when no transcript grows.+ if (now >= this.nextBudgetRetryAt) this.shrink(threadId);return entry.state;}@@ -96,6 +100,8 @@ export class ThreadCache {private shrink(keep: string): void {if (this.bytes <= this.maxBytes) return;const now = Date.now();+ // Keep repeated reads cheap while every remaining entry is protected.+ this.nextBudgetRetryAt = now + MIN_RESIDENCY_MS;const busy = this.busyThreadIds();for (const [threadId, entry] of this.entries) {if (this.bytes <= this.maxBytes) break;