Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(watchdog): don't count computer sleep as a turn's silence
AdityaUmalee39d9b0d70aa
diff --git a/server/turn-watchdog.test.ts b/server/turn-watchdog.test.tsindex 71a0e54597..d454c6e410 100644--- a/server/turn-watchdog.test.ts+++ b/server/turn-watchdog.test.ts@@ -1,7 +1,7 @@import { readFileSync } from "node:fs";import vm from "node:vm";import ts from "typescript";-import { describe, expect, it } from "vitest";+import { describe, expect, it, vi } from "vitest";import type { RuntimeEvent } from "./contracts.ts";import { EngineMemory } from "./engine-memory.ts";@@ -186,6 +186,45 @@ describe("TurnWatchdog", () => {expect(limits).toEqual([STALL]);});+ it("does not count the time the computer slept as a turn's silence", () => {+ vi.useFakeTimers({ now: 0 });+ const stalls: WatchedTurn[] = [];+ const dog = new TurnWatchdog({ stallMs: STALL, toolStallMs: TOOL_STALL, checkMs: 1_000, onStall: (turn) => stalls.push(turn) });+ try {+ dog.start();+ dog.watch("t1", "bot1");+ vi.advanceTimersByTime(STALL / 2);+ // The Mac sleeps for an hour: the process is suspended, so no sweep runs while the wall clock keeps going.+ vi.setSystemTime(Date.now() + 60 * 60_000);+ vi.advanceTimersByTime(1_000);+ expect(stalls).toHaveLength(0);+ vi.advanceTimersByTime(STALL / 2 - 2_000);+ expect(stalls).toHaveLength(0);+ vi.advanceTimersByTime(STALL);+ expect(stalls).toEqual([expect.objectContaining({ threadId: "t1" })]);+ } finally {+ dog.stop();+ vi.useRealTimers();+ }+ });++ it("still counts silence across sweeps that are only a little late", () => {+ vi.useFakeTimers({ now: 0 });+ const stalls: WatchedTurn[] = [];+ const dog = new TurnWatchdog({ stallMs: STALL, toolStallMs: TOOL_STALL, checkMs: 1_000, onStall: (turn) => stalls.push(turn) });+ try {+ dog.start();+ dog.watch("t1", "bot1");+ vi.advanceTimersByTime(STALL - 3_000);+ vi.setSystemTime(Date.now() + 2_000);+ vi.advanceTimersByTime(1_000);+ expect(stalls).toHaveLength(1);+ } finally {+ dog.stop();+ vi.useRealTimers();+ }+ });+it("says a bot is answering while one of its turns runs unparked, and not once it waits or ends", () => {const { dog } = rig();expect(dog.answering("bot1")).toBe(false);diff --git a/server/turn-watchdog.ts b/server/turn-watchdog.tsindex 48664f40e8..8261af30c2 100644--- a/server/turn-watchdog.ts+++ b/server/turn-watchdog.ts@@ -18,6 +18,8 @@ export function stallLimitMs(raw: string | undefined, fallback: number): numberreturn Number.isFinite(ms) && ms > 0 ? ms : fallback;}+const SUSPENDED_SWEEPS = 3;+const parked = (turn: WatchedTurn): boolean => turn.waitingOnHuman || turn.waitingOnComputer;export interface TurnWatchdogOptions {@@ -45,7 +47,15 @@ export class TurnWatchdog {start(): void {if (this.timer) return;- this.timer = setInterval(() => this.sweep(), this.opts.checkMs);+ // Real time, not now(): a sleeping computer suspends the process while the wall clock runs on, so the timer fires+ // that much late, and that time was not the turns' silence.+ let lastTickAt = Date.now();+ this.timer = setInterval(() => {+ const at = Date.now();+ const gap = at - lastTickAt;+ lastTickAt = at;+ this.sweep(gap > SUSPENDED_SWEEPS * this.opts.checkMs ? gap : 0);+ }, this.opts.checkMs);this.timer.unref?.();}@@ -110,9 +120,11 @@ export class TurnWatchdog {return false;}- sweep(): void {+ /** sleptMs: time the process was suspended since the last sweep; it is forgiven, not counted as silence. */+ sweep(sleptMs = 0): void {const at = this.now();for (const turn of this.turns.values()) {+ if (sleptMs) turn.lastEventAt = Math.min(at, turn.lastEventAt + sleptMs);if (parked(turn)) continue;const silentMs = turn.runningTools.size > 0 ? Math.max(this.opts.stallMs, this.opts.toolStallMs) : this.opts.stallMs;if (at - turn.lastEventAt < silentMs) continue;