Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(rooms): a room turn waits for a running tool instead of stopping at its limit (#4023)
Aditya Umale6fe142640a30
diff --git a/docs/self-hosting.md b/docs/self-hosting.mdindex b2201cedb7..6f10ba32df 100644--- a/docs/self-hosting.md+++ b/docs/self-hosting.md@@ -1249,9 +1249,11 @@ is not a positive finite number (empty, `65m`, `0`, a negative number,`Infinity`, `1e309`) leaves the default. Set them in the server's environmentand restart it; the restart stops every running turn.-A turn in a room also has a work ceiling of its own, `rooms.turnTimeoutMinutes`-in `config.json` (default 5 minutes, 1-1440, which one conversation can-replace). It stops a member's turn even while its tool runs, and these two+A turn in a room also has a limit of its own, `rooms.turnTimeoutMinutes` in+`config.json` (default 5 minutes, 1-1440, which one conversation can replace).+It stops a member's turn that shows no progress for that long. While one of the+turn's tools runs, the turn may show nothing for up to 60 minutes (the longest+an MCP call can be set to take) or the room's limit if that is longer. These twosettings do not change it.## Updatingdiff --git a/server/index.ts b/server/index.tsindex 2223c123fa..b0b367d273 100644--- a/server/index.ts+++ b/server/index.ts@@ -81,7 +81,7 @@ import {import { fitsOnOneLine, parseBotProfilePatch } from "./bot-profile.ts";import { routeDirectTurn } from "./turn-route.ts"; import { QuickModelFallback } from "./quick-model-fallback.ts"; import { COMPUTER_JOURNAL_PATH, createSavedSteps, personRequest, savedStepsEnabled } from "./saved-steps/saved-steps.ts";import { groupTurnCwd } from "./room-cwd.ts";-import { ROOM_TURN_PROGRESS, RoomTurnDeadline, RoomTurnStallRegistry, effectiveRoomTurnTimeoutMinutes, parseConversationTurnTimeout, roomTurnTimeoutMessage } from "./room-turn-timeout.ts";+import { RoomTurnDeadline, RoomTurnStallRegistry, effectiveRoomTurnTimeoutMinutes, parseConversationTurnTimeout, roomTurnTimeoutMessage } from "./room-turn-timeout.ts";import { roomTurnEnd, type RoomClaimEnd } from "./room-turn-end.ts";import { applyHoldDirective, classifyHoldDirective, heldMemberLine, rememberHeldMessage, stopWordPlacement } from "./room-holds.ts";import { createPersonStop } from "./host/person-stop.ts";@@ -10416,7 +10416,7 @@ async function runClaimedGroupMemberTurn(if (shouldIgnoreProviderEvent(e)) return;if (e.threadId !== threadId) return;if (providerTurnId && e.turnId && e.turnId !== providerTurnId) return;- if (ROOM_TURN_PROGRESS.has(e.type)) deadline.progress();+ deadline.observe(e);if (e.type === "item.completed" && e.itemType === "assistant_text") turnTexts.push(e.text);else if (e.type === "turn.completed") {turnCompletedOk = e.ok;@@ -10426,9 +10426,6 @@ async function runClaimedGroupMemberTurn(}finish(end.outcome);}- // Waiting on a person or the computer is not idling: hold the limit while a card is open or the computer is busy.- else if (e.type === "request.opened" || e.type === "turn.wait_started") deadline.setWaiting(true);- else if (e.type === "request.resolved" || e.type === "turn.wait_ended") deadline.setWaiting(false);});deadline.start();// Swapped synchronously with the dispatch below, so no stall can fall between them.diff --git a/server/room-turn-timeout.test.ts b/server/room-turn-timeout.test.tsindex 9f92c55174..c18f2bb83d 100644--- a/server/room-turn-timeout.test.ts+++ b/server/room-turn-timeout.test.ts@@ -1,6 +1,8 @@import { afterEach, describe, expect, it, vi } from "vitest";+import type { RuntimeEvent } from "./contracts.ts";import {+ ROOM_TOOL_QUIET_LIMIT_MS,ROOM_TURN_PROGRESS,RoomTurnDeadline,RoomTurnStallRegistry,@@ -12,6 +14,12 @@ import {afterEach(() => vi.useRealTimers());+const base = { eventId: "e1", provider: "claudeAgent", threadId: "t1", createdAt: "2026-01-01T00:00:00.000Z" } as const;+const toolStarted = (itemId: string): RuntimeEvent => ({ ...base, type: "item.started", itemType: "tool", itemId });+const toolEnded = (itemId: string): RuntimeEvent => ({ ...base, type: "item.completed", itemType: "tool", itemId, ok: true });+const cardOpened: RuntimeEvent = { ...base, type: "request.opened", requestId: "r", requestType: "permission", tool: "Bash", summary: "ls" };+const cardResolved: RuntimeEvent = { ...base, type: "request.resolved", requestId: "r", behavior: "allow", source: "user" };+describe("room turn timeout", () => {it("converts configured minutes to milliseconds", () => {expect(roomTurnTimeoutMs(20)).toBe(20 * 60_000);@@ -177,6 +185,71 @@ describe("room turn timeout", () => {deadline.stop();});+ it("keeps a turn going while a tool runs past the limit, and gives it the full limit once the tool ends", async () => {+ vi.useFakeTimers();+ const onTimeout = vi.fn();+ const deadline = new RoomTurnDeadline(5, onTimeout);+ deadline.start();++ await vi.advanceTimersByTimeAsync(60_000);+ deadline.observe(toolStarted("run"));+ await vi.advanceTimersByTimeAsync(14 * 60_000);+ expect(onTimeout).not.toHaveBeenCalled();++ deadline.observe(toolEnded("run"));+ await vi.advanceTimersByTimeAsync(5 * 60_000 - 1);+ expect(onTimeout).not.toHaveBeenCalled();+ await vi.advanceTimersByTimeAsync(1);+ expect(onTimeout).toHaveBeenCalledOnce();+ });++ it("stops a turn whose tool shows nothing for the tool limit, as when its end never arrives", async () => {+ vi.useFakeTimers();+ const onTimeout = vi.fn();+ const deadline = new RoomTurnDeadline(5, onTimeout);+ deadline.start();++ deadline.observe(toolStarted("lost"));+ await vi.advanceTimersByTimeAsync(ROOM_TOOL_QUIET_LIMIT_MS - 1);+ expect(onTimeout).not.toHaveBeenCalled();+ await vi.advanceTimersByTimeAsync(1);+ expect(onTimeout).toHaveBeenCalledOnce();+ });++ it("stops a turn with no tool running at the limit, whatever tools ended before", async () => {+ vi.useFakeTimers();+ const onTimeout = vi.fn();+ const deadline = new RoomTurnDeadline(5, onTimeout);+ deadline.start();++ deadline.observe(toolStarted("a"));+ deadline.observe(toolEnded("a"));+ deadline.observe(toolEnded("never-started"));+ await vi.advanceTimersByTimeAsync(5 * 60_000 - 1);+ expect(onTimeout).not.toHaveBeenCalled();+ await vi.advanceTimersByTimeAsync(1);+ expect(onTimeout).toHaveBeenCalledOnce();+ });++ it("keeps the tool limit until the last running tool ends, across a card that opened and closed", async () => {+ vi.useFakeTimers();+ const onTimeout = vi.fn();+ const deadline = new RoomTurnDeadline(5, onTimeout);+ deadline.start();++ deadline.observe(toolStarted("a"));+ deadline.observe(toolStarted("b"));+ deadline.observe(cardOpened);+ deadline.observe(toolEnded("a"));+ deadline.observe(cardResolved);+ await vi.advanceTimersByTimeAsync(30 * 60_000);+ expect(onTimeout).not.toHaveBeenCalled();++ deadline.observe(toolEnded("b"));+ await vi.advanceTimersByTimeAsync(5 * 60_000);+ expect(onTimeout).toHaveBeenCalledOnce();+ });+it("does not fire while held even past the budget, and stop() cancels a held clock", async () => {vi.useFakeTimers();const onTimeout = vi.fn();diff --git a/server/room-turn-timeout.ts b/server/room-turn-timeout.tsindex f2f842c9ee..c62b4a52ad 100644--- a/server/room-turn-timeout.ts+++ b/server/room-turn-timeout.ts@@ -1,4 +1,5 @@import {+ MAX_MCP_CALL_TIMEOUT_MINUTES,MAX_ROOM_TURN_TIMEOUT_MINUTES,MIN_ROOM_TURN_TIMEOUT_MINUTES,} from "./config.ts";@@ -47,13 +48,18 @@ export function effectiveRoomTurnTimeoutMinutes(return globalMinutes;}-/** Stops a turn that shows no progress for the limit; the clock holds while the turn waits on a person or the computer, and stray ends clamp the count at zero. */+/** How long a turn may show nothing while one of its tools runs: as long as an MCP call may be set to take. */+export const ROOM_TOOL_QUIET_LIMIT_MS = MAX_MCP_CALL_TIMEOUT_MINUTES * 60_000;++/** Stops a turn that shows no progress for the limit, or for ROOM_TOOL_QUIET_LIMIT_MS while a tool runs; the clock+ * holds while the turn waits on a person or the computer, and stray ends clamp the count at zero. */export class RoomTurnDeadline {private limitMs: number;private progressAt = 0;private heldAt = 0;private timer: ReturnType<typeof setTimeout> | null = null;private openWaits = 0;+ private runningTools = new Set<string>();private stopped = false;private fired = false;private fire: () => void;@@ -69,9 +75,20 @@ export class RoomTurnDeadline {this.arm();}+ observe(e: RuntimeEvent): void {+ if (ROOM_TURN_PROGRESS.has(e.type)) this.progress();+ if (e.type === "request.opened" || e.type === "turn.wait_started") this.setWaiting(true);+ else if (e.type === "request.resolved" || e.type === "turn.wait_ended") this.setWaiting(false);+ else if ((e.type === "item.started" || e.type === "item.completed") && e.itemType === "tool" && e.itemId) {+ if (e.type === "item.started") this.runningTools.add(e.itemId);+ else this.runningTools.delete(e.itemId);+ this.rearm();+ }+ }+progress(): void {// A delayed event loop can deliver progress after the limit, before the timer ran: it must not revive an idle turn.- if (this.timer !== null && this.idleMs() >= this.limitMs) {+ if (this.timer !== null && this.idleMs() >= this.quietLimitMs()) {this.expired();return;}@@ -107,18 +124,29 @@ export class RoomTurnDeadline {return Date.now() - this.progressAt;}+ private quietLimitMs(): number {+ return this.runningTools.size > 0 ? Math.max(this.limitMs, ROOM_TOOL_QUIET_LIMIT_MS) : this.limitMs;+ }+private hold(): void {this.heldAt = Date.now();if (this.timer === null) return;clearTimeout(this.timer);this.timer = null;// A delayed event loop can deliver the wait after the deadline: a wait must not revive an idle turn.- if (this.idleMs() >= this.limitMs) this.expired();+ if (this.idleMs() >= this.quietLimitMs()) this.expired();+ }++ private rearm(): void {+ if (this.timer === null) return;+ clearTimeout(this.timer);+ this.timer = null;+ this.arm();}private arm(): void {if (this.stopped || this.fired || this.openWaits > 0) return;- const leftMs = this.limitMs - this.idleMs();+ const leftMs = this.quietLimitMs() - this.idleMs();if (leftMs <= 0) {this.expired();return;