Skip to content

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

fix: scope retained keep-warm pings to their target

Afloat40eaadb8004d
diff --git a/server/cache-keepwarm-target.test.ts b/server/cache-keepwarm-target.test.ts
index b710992b36..0d0ea95e9e 100644
--- a/server/cache-keepwarm-target.test.ts
+++ b/server/cache-keepwarm-target.test.ts
@@ -7,7 +7,7 @@ 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() {
+function fixture(windowMs = 3 * 60 * 60_000) {
vi.useFakeTimers();
const holds: Array<{ target: KeepWarmTarget; until: number | null }> = [];
const booked: KeepWarmTarget[] = [];
@@ -15,7 +15,7 @@ function fixture() {
const keepWarm = createCacheKeepWarm({
now: () => Date.now(),
after: (ms, fn) => { const timer = setTimeout(fn, ms); return { cancel: () => clearTimeout(timer) }; },
- windowMs: () => 3 * 60 * 60_000,
+ windowMs: () => windowMs,
ineligible: () => null,
busy: () => false,
overBudget: () => false,
@@ -71,4 +71,31 @@ describe.each([
h.keepWarm.noteTurnCompleted(replacement, turn);
expect(h.keepWarm.status(target.threadId)?.last).toBeUndefined();
});
+
+ it.each(["expires", "is canceled"])("drops the previous target's ping after its window %s", async (ending) => {
+ const h = fixture(PING_AFTER_MS + 1_000);
+ h.keepWarm.noteTurnCompleted(target, turn);
+ await vi.advanceTimersByTimeAsync(PING_AFTER_MS);
+ await h.settle();
+ expect(h.keepWarm.status(target.threadId)?.last).toBeDefined();
+ if (ending === "expires") await vi.advanceTimersByTimeAsync(1_000);
+ else h.keepWarm.cancelBot(target.botId);
+ expect(h.keepWarm.status(target.threadId)).toBeUndefined();
+ h.keepWarm.noteTurnCompleted(replacement, turn);
+ expect(h.keepWarm.status(target.threadId)).toMatchObject({ state: "warm" });
+ expect(h.keepWarm.status(target.threadId)?.last).toBeUndefined();
+ });
+});
+
+it("preserves the last ping when the same target starts a new window", async () => {
+ const h = fixture(PING_AFTER_MS + 1_000);
+ h.keepWarm.noteTurnCompleted(target, turn);
+ await vi.advanceTimersByTimeAsync(PING_AFTER_MS);
+ await h.settle();
+ const last = h.keepWarm.status(target.threadId)?.last;
+ expect(last).toBeDefined();
+ await vi.advanceTimersByTimeAsync(1_000);
+ expect(h.keepWarm.status(target.threadId)).toBeUndefined();
+ h.keepWarm.noteTurnCompleted({ ...target }, turn);
+ expect(h.keepWarm.status(target.threadId)?.last).toEqual(last);
});
diff --git a/server/cache-keepwarm.ts b/server/cache-keepwarm.ts
index 49381e85d9..8f3c7c9ea3 100644
--- a/server/cache-keepwarm.ts
+++ b/server/cache-keepwarm.ts
@@ -63,7 +63,7 @@ export function pingWasWarm(read: number, write: number): boolean {
export function createCacheKeepWarm(deps: KeepWarmDeps) {
const windows = new Map<string, Window>();
- const lastPing = new Map<string, NonNullable<KeepWarmStatus["last"]>>();
+ const lastPing = new Map<string, { target: KeepWarmTarget; ping: NonNullable<KeepWarmStatus["last"]> }>();
const stoppedWhy = new Map<string, string>();
let disposed = false;
@@ -128,7 +128,10 @@ export function createCacheKeepWarm(deps: KeepWarmDeps) {
return end(w, result.reason);
}
const warm = pingWasWarm(result.cacheRead, result.cacheWrite);
- lastPing.set(w.target.threadId, { at: now, read: result.cacheRead, write: result.cacheWrite, costUsd: result.costUsd });
+ lastPing.set(w.target.threadId, {
+ target: { ...w.target },
+ ping: { at: now, read: result.cacheRead, write: result.cacheWrite, costUsd: result.costUsd },
+ });
if (!warm) {
return end(w, `the ping read ${fmtTokens(result.cacheRead)} and wrote ${fmtTokens(result.cacheWrite)} tokens: the cache was already gone`);
}
@@ -169,9 +172,12 @@ 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);
+ const previousPing = lastPing.get(target.threadId);
+ if (previousPing && (previousPing.target.botId !== target.botId || previousPing.target.instanceId !== target.instanceId)) {
+ lastPing.delete(target.threadId);
+ }
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();
@@ -202,7 +208,7 @@ export function createCacheKeepWarm(deps: KeepWarmDeps) {
status(threadId: string): KeepWarmStatus | undefined {
const w = windows.get(threadId);
- const last = lastPing.get(threadId);
+ const last = lastPing.get(threadId)?.ping;
if (w) {
return {
state: "warm",