Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
refactor(limits): make the dispatch claims with a factory
laijsg13c922cc77535
diff --git a/server/host/dispatch-claims.test.ts b/server/host/dispatch-claims.test.tsindex 2c2bb8ef08..7799d3ef75 100644--- a/server/host/dispatch-claims.test.ts+++ b/server/host/dispatch-claims.test.ts@@ -1,5 +1,5 @@import { describe, expect, it } from "vitest";-import { DispatchClaims } from "./dispatch-claims.ts";+import { createDispatchClaims } from "./dispatch-claims.ts";type Claim = { id: string; phase: "setup" | "dispatching" };@@ -10,13 +10,13 @@ const settledYet = async (promise: Promise<void>): Promise<boolean> => {return done;};-describe("DispatchClaims", () => {+describe("createDispatchClaims", () => {it("resolves at once when the thread holds no claim", async () => {- expect(await settledYet(new DispatchClaims<Claim>().released("t1"))).toBe(true);+ expect(await settledYet(createDispatchClaims<Claim>().released("t1"))).toBe(true);});it("waits through a phase change of the same claim and resolves when it is cleared", async () => {- const claims = new DispatchClaims<Claim>();+ const claims = createDispatchClaims<Claim>();claims.set("t1", { id: "c1", phase: "setup" });const released = claims.released("t1");claims.set("t1", { id: "c1", phase: "dispatching" });@@ -28,7 +28,7 @@ describe("DispatchClaims", () => {});it("resolves when another turn's claim replaces it, and then waits on that one only for a new caller", async () => {- const claims = new DispatchClaims<Claim>();+ const claims = createDispatchClaims<Claim>();claims.set("t1", { id: "c1", phase: "dispatching" });const first = claims.released("t1");claims.set("t1", { id: "c2", phase: "setup" });diff --git a/server/host/dispatch-claims.ts b/server/host/dispatch-claims.tsindex e19b7011ed..6098f5b2e5 100644--- a/server/host/dispatch-claims.ts+++ b/server/host/dispatch-claims.ts@@ -1,8 +1,15 @@/** The direct turns being set up or sent to their engine, by thread. A thread holding a claim reads busy. */-export class DispatchClaims<Claim extends { id: string }> extends Map<string, Claim> {+export type DispatchClaims<Claim extends { id: string }> = Map<string, Claim> & {+ released(threadId: string): Promise<void>;+};++export function createDispatchClaims<Claim extends { id: string }>(): DispatchClaims<Claim> {+ return new ClaimsByThread<Claim>();+}++class ClaimsByThread<Claim extends { id: string }> extends Map<string, Claim> {private readonly waiting = new Map<string, Array<() => void>>();- /** Resolves once the claim the thread holds now is gone: cleared, or replaced by another turn's. */released(threadId: string): Promise<void> {const claim = this.get(threadId);if (!claim) return Promise.resolve();diff --git a/server/index.ts b/server/index.tsindex 936668f5b7..b3926a2ff9 100644--- a/server/index.ts+++ b/server/index.ts@@ -333,7 +333,7 @@ import { Handoffs, handedStateUsable, isContextMessage, recordHanded, renderUnseimport { extractReplayImages, extractTurnImages, nativeClips, turnMedia, videoFramesWithin } from "./turn-images.ts";import { callTitleExcerpt } from "./thread-title.ts";import { generateThreadTitle } from "./thread-title-generate.ts";-import { engineCommandCpuMs, engineProcessExited, engineProcessGone, ENGINE_EXIT_STALL_MS, stallLimitMs, stallNotice, TurnWatchdog } from "./turn-watchdog.ts"; import { createIdleThreads } from "./host/idle-threads.ts"; import { DispatchClaims } from "./host/dispatch-claims.ts";+import { engineCommandCpuMs, engineProcessExited, engineProcessGone, ENGINE_EXIT_STALL_MS, stallLimitMs, stallNotice, TurnWatchdog } from "./turn-watchdog.ts"; import { createIdleThreads } from "./host/idle-threads.ts"; import { createDispatchClaims } from "./host/dispatch-claims.ts";import { TurnResources, type TurnOwner } from "./turn-resources.ts";import { createPhoneHold } from "./phone-screen/hold.ts"; import { createPhoneGate } from "./routes/phone-gate.ts";import { endHoldUnlessOn, usbPhoneOffered, usbPhoneTakeoverEnabled } from "./phone-screen/flag.ts"; import { dataPrompts } from "./data-prompt.ts";@@ -1882,7 +1882,7 @@ async function cloudComputerHolders(botId: string): Promise<string[]> {function boatNotConfigured(bot: { name: string }, source: PlaceSource): PlaceUnavailableError {return placeUnavailable("cloud", { state: CLOUD_HOME ? "cc-unavailable" : "cc-needs-key", params: { bot: bot.name }, source }, Boolean(CLOUD_HOME));}-const directTurnDispatchClaims = new DispatchClaims<DirectTurnDispatchClaim>();+const directTurnDispatchClaims = createDispatchClaims<DirectTurnDispatchClaim>();const directTurnGenerationByThread = new Map<string, string>();// Latest direct request per thread, kept past Stop until completion or failure cleanup; unknown continuations deliberately lose proof.const directRequestOwners = new Map<string, {