Skip to content

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

fix: reset keep-warm windows when their target changes

Afloatd6d0cd933384
diff --git a/server/cache-keepwarm-target.test.ts b/server/cache-keepwarm-target.test.ts
new file mode 100644
index 0000000000..b710992b36
--- /dev/null
+++ b/server/cache-keepwarm-target.test.ts
@@ -0,0 +1,74 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import { createCacheKeepWarm, PING_AFTER_MS, type CacheKeepWarm, type KeepWarmPingResult, type KeepWarmTarget } from "./cache-keepwarm.ts";
+
+const target: KeepWarmTarget = { threadId: "thread", botId: "bot", instanceId: "claude-one" };
+const turn = { ok: true, contextTokens: 200_000 };
+const warm: KeepWarmPingResult = { ok: true, input: 4, cacheRead: 100_000, cacheWrite: 20, output: 2, costUsd: 0.01, model: "claude-opus-5-5" };
+const instances: CacheKeepWarm[] = [];
+
+function fixture() {
+ vi.useFakeTimers();
+ const holds: Array<{ target: KeepWarmTarget; until: number | null }> = [];
+ const booked: KeepWarmTarget[] = [];
+ let finish!: (result: KeepWarmPingResult) => void;
+ const keepWarm = createCacheKeepWarm({
+ now: () => Date.now(),
+ after: (ms, fn) => { const timer = setTimeout(fn, ms); return { cancel: () => clearTimeout(timer) }; },
+ windowMs: () => 3 * 60 * 60_000,
+ ineligible: () => null,
+ busy: () => false,
+ overBudget: () => false,
+ ping: () => new Promise((resolve) => { finish = resolve; }),
+ hold: (heldTarget, until) => { holds.push({ target: heldTarget, until }); },
+ book: (bookedTarget) => { booked.push(bookedTarget); },
+ changed: () => {},
+ });
+ instances.push(keepWarm);
+ return { keepWarm, holds, booked, settle: async () => { finish(warm); await Promise.resolve(); } };
+}
+
+afterEach(() => {
+ for (const instance of instances.splice(0)) instance.dispose();
+ vi.useRealTimers();
+});
+
+describe.each([
+ ["engine", { ...target, instanceId: "claude-two" }],
+ ["room speaker", { ...target, botId: "another-bot" }],
+] as const)("keep-warm after a change of %s", (_kind, replacement) => {
+ it("releases the previous target's held session before holding its replacement", () => {
+ const h = fixture();
+ h.keepWarm.noteTurnCompleted(target, turn);
+ h.keepWarm.noteTurnStarted(target.threadId);
+ h.keepWarm.noteTurnCompleted(replacement, turn);
+ expect(h.holds).toEqual([
+ { target, until: expect.any(Number) },
+ { target, until: null },
+ { target: replacement, until: expect.any(Number) },
+ ]);
+ });
+
+ it("books a late ping to the target that ran it without updating the replacement", async () => {
+ const h = fixture();
+ h.keepWarm.noteTurnCompleted(target, turn);
+ await vi.advanceTimersByTimeAsync(PING_AFTER_MS);
+ h.keepWarm.noteTurnStarted(target.threadId);
+ h.keepWarm.noteTurnCompleted(replacement, turn);
+ await h.settle();
+ expect(h.booked).toEqual([target]);
+ expect(h.keepWarm.status(target.threadId)?.last).toBeUndefined();
+ expect(h.keepWarm.status(target.threadId)?.nextPingAt).toBe(Date.now() + PING_AFTER_MS);
+ });
+
+ it("does not show the previous target's ping as the replacement's last ping", async () => {
+ const h = fixture();
+ h.keepWarm.noteTurnCompleted(target, turn);
+ await vi.advanceTimersByTimeAsync(PING_AFTER_MS);
+ await h.settle();
+ expect(h.keepWarm.status(target.threadId)?.last).toBeDefined();
+ h.keepWarm.noteTurnStarted(target.threadId);
+ h.keepWarm.noteTurnCompleted(replacement, turn);
+ expect(h.keepWarm.status(target.threadId)?.last).toBeUndefined();
+ });
+});
diff --git a/server/cache-keepwarm.ts b/server/cache-keepwarm.ts
index 4699a2b5a8..49381e85d9 100644
--- a/server/cache-keepwarm.ts
+++ b/server/cache-keepwarm.ts
@@ -158,7 +158,7 @@ export function createCacheKeepWarm(deps: KeepWarmDeps) {
noteTurnCompleted(target: KeepWarmTarget, turn: { ok: boolean; stopReason?: string | null; contextTokens?: number }): void {
if (disposed) return;
- const existing = windows.get(target.threadId);
+ let existing = windows.get(target.threadId);
const hadStopped = stoppedWhy.delete(target.threadId);
const windowMs = deps.windowMs(target.botId);
const quiet = (w: Window | undefined) => {
@@ -169,6 +169,11 @@ export function createCacheKeepWarm(deps: KeepWarmDeps) {
if (windowMs === null || !turn.ok) return quiet(existing);
if ((turn.contextTokens ?? 0) < MIN_CONTEXT_TOKENS) return quiet(existing);
if (deps.ineligible(target)) return quiet(existing);
+ if (existing && (existing.target.botId !== target.botId || existing.target.instanceId !== target.instanceId)) {
+ end(existing, null);
+ lastPing.delete(target.threadId);
+ existing = undefined;
+ }
const now = deps.now();
const w: Window = existing ?? { target, deadline: 0, lastRequestAt: now, pending: null, nextPingAt: null, inflight: null, paused: false };
w.target = target;