Skip to content

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

fix(engines): keep replacement prestarts after stale failures

Afloat39159177b82f
diff --git a/server/engine-prestart.test.ts b/server/engine-prestart.test.ts
index b7d72e8664..8318c7244b 100644
--- a/server/engine-prestart.test.ts
+++ b/server/engine-prestart.test.ts
@@ -188,4 +188,35 @@ describe("starting a new conversation's engine from the bot's last launch", () =
expect(credentials.has(NEXT)).toBe(false);
expect(prestart.waiting()).toEqual([]);
});
+
+ it.each(["refused", "rejected", "started"] as const)("keeps a replacement reservation when an obsolete start %s", async (outcome) => {
+ const pending: Array<{ resolve: (up: boolean) => void; reject: (error: Error) => void }> = [];
+ const drop = vi.fn();
+ const { prestart, credentials } = host({
+ adapter: () => ({ prestart: {
+ start: () => new Promise<boolean>((resolve, reject) => pending.push({ resolve, reject })),
+ held: () => true, ready: () => false, drop,
+ } }),
+ });
+ prestart.note("claude", firstTurn(), true);
+ prestart.threadMade("bot", NEXT);
+ prestart.drop(NEXT);
+ prestart.threadMade("bot", NEXT);
+ const replacement = credentials.get(NEXT)!;
+ expect(pending).toHaveLength(2);
+ expect(drop).toHaveBeenCalledExactlyOnceWith(NEXT);
+
+ if (outcome === "rejected") pending[0]!.reject(new Error("old launch failed"));
+ else pending[0]!.resolve(outcome === "started");
+ await vi.advanceTimersByTimeAsync(0);
+ expect(prestart.waiting()).toEqual([NEXT]);
+ expect(credentials.get(NEXT)).toBe(replacement);
+ expect(drop).toHaveBeenCalledOnce();
+
+ pending[1]!.resolve(false);
+ await vi.advanceTimersByTimeAsync(0);
+ expect(prestart.waiting()).toEqual([]);
+ expect(credentials.has(NEXT)).toBe(false);
+ expect(drop).toHaveBeenCalledTimes(2);
+ });
});
diff --git a/server/engine-prestart.ts b/server/engine-prestart.ts
index ced18c566c..d88dcf929c 100644
--- a/server/engine-prestart.ts
+++ b/server/engine-prestart.ts
@@ -129,9 +129,11 @@ export function createEnginePrestart(deps: EnginePrestartDeps) {
const launch = { ...(JSON.parse(input) as SendTurnInput), text: "" };
const timer = setTimeout(() => release(threadId, !deps.busy(botId, threadId)), (deps.idleMs ?? PRESTART_IDLE_MS) + RELEASE_AFTER_MS);
timer.unref?.();
- reserved.set(threadId, { botId, instanceId: from.instanceId, timer });
- void adapter.start(launch).then((up) => { if (!up && !deps.busy(botId, threadId)) release(threadId, true); },
- () => release(threadId, !deps.busy(botId, threadId)));
+ const reservation = { botId, instanceId: from.instanceId, timer };
+ reserved.set(threadId, reservation);
+ const releaseOwn = (retire: boolean) => { if (reserved.get(threadId) === reservation) release(threadId, retire); };
+ void adapter.start(launch).then((up) => { if (!up && !deps.busy(botId, threadId)) releaseOwn(true); },
+ () => releaseOwn(!deps.busy(botId, threadId)));
},
/** The conversation's bearers were retired (a Stop, a delete): an engine started ahead with them goes too. */