Skip to content

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

Teammates on other servers: rework to one of them stops after 4 rounds per request

milind-soni50b5b46a899b
diff --git a/server/host/remote-teammate-handoff.test.ts b/server/host/remote-teammate-handoff.test.ts
index 68408b504d..a432cd8d56 100644
--- a/server/host/remote-teammate-handoff.test.ts
+++ b/server/host/remote-teammate-handoff.test.ts
@@ -6,7 +6,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { createCoordinator, type Coordinator, type CoordinateBotsCall, type CoordinatorHost, type HandoffView } from "../coordinator.ts";
import type { RemoteResult, RemoteTeammate } from "../remote-teammates.ts";
import { removeTempDir } from "../testing/cleanup.ts";
-import { createRemoteHandoff, remoteBotId, type RemoteHandoffContext, type RemoteHandoffDeps } from "./remote-teammate-handoff.ts";
+import { createRemoteHandoff, REMOTE_REWORK_MAX, remoteBotId, type RemoteHandoffContext, type RemoteHandoffDeps } from "./remote-teammate-handoff.ts";
const ADA: RemoteTeammate = { id: "6f1c1a52-9d55-4b8e-9d0e-6c1f0b0f2a11", name: "Ada", base: "https://bots.example.test", botId: "ada-bot", key: "bak_testkeytestkeytestkey", createdAt: 1 };
const REMOTE = remoteBotId(ADA.id);
@@ -357,3 +357,22 @@ describe("remote teammate hand-off: the teammate's turn", () => {
expect(log).toHaveBeenCalledWith(expect.stringContaining("ignoring unreadable"));
});
});
+
+describe("remote teammate hand-off: rework", () => {
+ it("stops rework to a teammate on another server after the limit under one request, and counts each request apart", async () => {
+ let clock = 1_000;
+ const s = setup({ now: () => clock });
+ const rework = (round: number) => s.remote.coordinate(call([REMOTE], { rework: true, message: `Fix round ${round}` }), s.context, local);
+ for (let round = 1; round <= REMOTE_REWORK_MAX; round++) expect((await rework(round)).status).toBe(200);
+ const refused = await rework(REMOTE_REWORK_MAX + 1);
+ expect(refused.status).not.toBe(200);
+ expect((refused.body.receipts as Array<{ outcome: string; detail: string }>)[0]).toMatchObject({ outcome: "failed", detail: expect.stringContaining(`past the limit of ${REMOTE_REWORK_MAX}`) });
+ expect(s.coordinator.children("gen-1")).toHaveLength(REMOTE_REWORK_MAX);
+
+ s.coordinator.beginTurn({ id: "gen-2", address: { botId: "chief-id", threadId: "chief-chat" }, brief: "Next", level: "full", origin: { kind: "user", request: "Next" } });
+ expect((await s.remote.coordinate(call([REMOTE], { rework: true, message: "Fix it" }), { ...s.context, generation: "gen-2" }, local)).status).toBe(200);
+
+ clock += 6 * 60 * 60_000 + 1;
+ expect((await rework(REMOTE_REWORK_MAX + 2)).status).toBe(200);
+ });
+});
diff --git a/server/host/remote-teammate-handoff.ts b/server/host/remote-teammate-handoff.ts
index 638343343b..2081247fd5 100644
--- a/server/host/remote-teammate-handoff.ts
+++ b/server/host/remote-teammate-handoff.ts
@@ -33,6 +33,10 @@ const REPLY_CHARS = 20_000;
const KEEP_MS = 30 * 24 * 60 * 60_000;
/** The work thread of a hand-off with `thread: "new"` starts with this, after the teammate's address: it never goes on in an earlier conversation. */
const NEW_THREAD = "new:";
+/** Rework sent to one teammate on another server under one request. Each round is a turn there on its owner's key and
+ * nothing there sees the loop, so a worker here and a checker there that never agree stop after this many. */
+export const REMOTE_REWORK_MAX = 4;
+const REWORK_KEPT_MS = 6 * 60 * 60_000;
const conversationSchema = z.object({ teammate: z.string(), thread: z.string(), at: z.number() });
const workSchema = z.object({
@@ -78,6 +82,7 @@ export function createRemoteHandoff(deps: RemoteHandoffDeps) {
const send = deps.send ?? sendToRemote, read = deps.read ?? readRemote;
const wait = deps.wait ?? ((ms: number, signal: AbortSignal) => delay(ms, undefined, { signal }));
const now = deps.now ?? Date.now;
+ const reworkSent = new Map<string, { count: number; at: number }>();
const teammateOf = (botId: string): RemoteTeammate | undefined => (isRemoteBotId(botId) ? deps.teammates.get(botId.slice(REMOTE_PREFIX.length)) : undefined);
const nameOf = (botId: string) => teammateOf(botId)?.name ?? "Teammate";
@@ -135,6 +140,12 @@ export function createRemoteHandoff(deps: RemoteHandoffDeps) {
const receipt = (outcome: PeerDeliveryReceipt["outcome"], detail: string, more: Partial<PeerDeliveryReceipt> = {}): PeerDeliveryReceipt => ({ botId, botName: teammate.name, outcome, detail: clip(detail, 400), ...more });
const problem = unsupported(call, context);
if (problem) { refused.push(problem); receipts.push(receipt("failed", problem)); continue; }
+ const reworkKey = call.rework ? `${coordinator.node(context.parentId ?? context.generation)?.rootId ?? context.address.threadId}\u0000${teammate.id}` : undefined;
+ for (const [key, entry] of reworkSent) if (now() - entry.at > REWORK_KEPT_MS) reworkSent.delete(key);
+ if (reworkKey && (reworkSent.get(reworkKey)?.count ?? 0) >= REMOTE_REWORK_MAX) {
+ const stop = `coordinate_bots was not run: this would be rework number ${REMOTE_REWORK_MAX + 1} sent to ${teammate.name} on another server for the same request, past the limit of ${REMOTE_REWORK_MAX}, so nothing was sent. Stop here: give the person the latest result and say what is still open, with ${teammate.name}'s last reply.`;
+ refused.push(stop); receipts.push(receipt("failed", stop)); continue;
+ }
const threadId = `${botId}:${call.thread === "new" ? `${NEW_THREAD}${context.generation}:${receipts.length}` : context.address.threadId}`;
try {
const { node, duplicate } = coordinator.enqueue(context.address, context.generation, context.parentId, { botId, threadId }, call.message, {
@@ -144,6 +155,7 @@ export function createRemoteHandoff(deps: RemoteHandoffDeps) {
receipts.push(receipt(duplicate && node.status !== "queued" ? "injected" : "queued",
duplicate ? `already sent to ${teammate.name} on another server: ${returns}` : `sent to ${teammate.name} on another server; ${returns}.${files}`, { requestId: node.id }));
if (!duplicate) context.handed(node.id);
+ if (reworkKey && !duplicate) reworkSent.set(reworkKey, { count: (reworkSent.get(reworkKey)?.count ?? 0) + 1, at: now() });
} catch (error) {
const said = error instanceof Error ? error.message : String(error);
refused.push(said);