Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Check background status near wait deadline
WAI LOK HO15e59ce7c6be
diff --git a/server/cloud-computer-background.test.ts b/server/cloud-computer-background.test.tsindex 4c9cf50c64..6bbfa90cf8 100644--- a/server/cloud-computer-background.test.ts+++ b/server/cloud-computer-background.test.ts@@ -82,7 +82,54 @@ describe("background status deadlines", () => {expect(await pending).toMatchObject({ isError: true, content: [{ text: expect.stringContaining("call exec_status again") }] });});- it.each(["finished", "running", "late", "slow", "timeout", "not_found", "server_error", "malformed"] as const)("checks a short wait on a controlled clock: %s", async outcome => {+ it("does not start another read when a delayed sleep passes the deadline", async () => {+ vi.useFakeTimers();+ vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));+ const clock = {+ now: () => Date.now(),+ sleep: (ms: number) => new Promise<void>(resolve => setTimeout(resolve, ms + 1_000)),+ timeout: (ms: number) => {+ const controller = new AbortController();+ setTimeout(() => controller.abort(), ms);+ return controller.signal;+ },+ };+ const fetchStatus = vi.spyOn(globalThis, "fetch").mockResolvedValue(Response.json({ running: true, stdout: "partial", stderr: "" }));+ const pending = background.backgroundStatus({ box: { token: OWN_KEY } } as AppConfig, BOX,+ { process_id: background.processIdOn(BOX, 41), wait_seconds: 1 }, new AbortController().signal, clock);+ await vi.advanceTimersByTimeAsync(1_500);+ expect(fetchStatus).toHaveBeenCalledTimes(1);+ expect(json(await pending)).toMatchObject({ status: "running", observedAt: "2026-01-01T00:00:00.000Z", note: expect.stringContaining("may have changed") });+ });++ it("propagates cancellation during the final wait", async () => {+ vi.useFakeTimers();+ const clock = {+ now: () => Date.now(),+ sleep: (ms: number, signal: AbortSignal) => new Promise<void>((resolve, reject) => {+ const timer = setTimeout(resolve, ms);+ signal.addEventListener("abort", () => { clearTimeout(timer); reject(signal.reason); }, { once: true });+ }),+ timeout: (ms: number) => {+ const controller = new AbortController();+ setTimeout(() => controller.abort(), ms);+ return controller.signal;+ },+ };+ const fetchStatus = vi.spyOn(globalThis, "fetch").mockImplementation(async () => Response.json({ running: true, stdout: "", stderr: "" }));+ const controller = new AbortController();+ const pending = background.backgroundStatus({ box: { token: OWN_KEY } } as AppConfig, BOX,+ { process_id: background.processIdOn(BOX, 41), wait_seconds: 1 }, controller.signal, clock);+ const rejected = expect(pending).rejects.toMatchObject({ name: "AbortError" });+ await vi.advanceTimersByTimeAsync(0);+ await vi.advanceTimersToNextTimerAsync();+ expect(fetchStatus).toHaveBeenCalledTimes(2);+ controller.abort();+ await rejected;+ expect(fetchStatus).toHaveBeenCalledTimes(2);+ });++ it.each(["finished", "running", "late", "slow", "timeout", "last_timeout", "not_found", "server_error", "malformed"] as const)("checks a short wait on a controlled clock: %s", async outcome => {vi.useFakeTimers();vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));const clock = {@@ -99,7 +146,7 @@ describe("background status deadlines", () => {if (outcome === "slow") return new Promise<Response>(resolve => setTimeout(() => resolve(Response.json({ running: false, exitCode: 3, stdout: "final", stderr: "" })), 400));if (outcome === "not_found" || outcome === "server_error") return Response.json({ message: "unavailable" }, { status: outcome === "not_found" ? 404 : 503 });if (outcome === "malformed") return Response.json({});- if (outcome !== "timeout") return Response.json({ running: outcome === "running" || (outcome === "late" && Date.now() < Date.parse("2026-01-01T00:00:00.700Z")), exitCode: 3, stdout: "final", stderr: "" });+ if (outcome !== "timeout" && !(outcome === "last_timeout" && fetchStatus.mock.calls.length === 3)) return Response.json({ running: outcome === "running" || outcome === "last_timeout" || (outcome === "late" && Date.now() < Date.parse("2026-01-01T00:00:00.700Z")), exitCode: 3, stdout: "final", stderr: "" });return new Promise<Response>((_resolve, reject) => {init?.signal?.addEventListener("abort", () => reject(init.signal!.reason), { once: true });});@@ -108,11 +155,13 @@ describe("background status deadlines", () => {const pending = background.backgroundStatus({ box: { token: OWN_KEY } } as AppConfig, BOX,{ process_id: processId, wait_seconds: 1 }, new AbortController().signal, clock);await vi.advanceTimersByTimeAsync(1_000);- expect(fetchStatus).toHaveBeenCalledTimes(2);+ expect(fetchStatus).toHaveBeenCalledTimes(outcome === "running" || outcome === "late" || outcome === "last_timeout" ? 3 : 2);const raw = await pending;const result = json(raw);if (outcome === "timeout") {expect(result).toMatchObject({ status: "running", exitCode: null, stdout: "partial", observedAt: "2026-01-01T00:00:00.000Z", note: expect.stringContaining("may have changed") });+ } else if (outcome === "last_timeout") {+ expect(result).toMatchObject({ status: "running", exitCode: null, stdout: "final", observedAt: "2026-01-01T00:00:00.500Z", note: expect.stringContaining("may have changed") });} else if (outcome === "not_found" || outcome === "server_error" || outcome === "malformed") {expect(raw.isError).toBe(true);expect(result).toMatchObject({ error: expect.any(String), lastObserved: { processId, status: "running", observedAt: "2026-01-01T00:00:00.000Z", stdout: outcome === "server_error" ? "x".repeat(4_000) : "partial", stderr: "", note: expect.stringContaining("may have changed") } });@@ -120,7 +169,7 @@ describe("background status deadlines", () => {if (outcome === "not_found") expect(result.error).toContain("no background command");if (outcome === "malformed") expect(result.error).toContain("unreadable answer");} else {- expect(result).toMatchObject({ status: outcome === "running" || outcome === "late" ? "running" : "finished", exitCode: outcome === "running" || outcome === "late" ? null : 3, stdout: "final" });+ expect(result).toMatchObject({ status: outcome === "running" ? "running" : "finished", exitCode: outcome === "running" ? null : 3, stdout: "final" });expect(result.observedAt).toBeUndefined();}});diff --git a/server/cloud-computer-background.ts b/server/cloud-computer-background.tsindex 929b783901..a1dec23783 100644--- a/server/cloud-computer-background.ts+++ b/server/cloud-computer-background.ts@@ -32,7 +32,7 @@ const UNKNOWN_PROCESS = "This cloud computer has no background command with that+ "Look at the files the command writes to see how far it got.";const LOST = "The cloud computer restarted or lost track of this command, so its exit code may be unknown. Look at the files it writes to see how far it got.";const CUT = "Only the end of the output is shown. Write long output to a file and read the part you need with exec.";-const STALE = "The follow-up status read timed out. This is the last observed status; it may have changed. Call exec_status again.";+const STALE = "No newer status was confirmed before the wait ended. This is the last observed status; it may have changed. Call exec_status again.";type StatusClock = {now: () => number;@@ -169,8 +169,11 @@ export async function backgroundStatus(const until = clock.now() + waitMs;const lastReadWindow = Math.min(MAX_LAST_READ_WINDOW_MS, waitMs / 2);let last: { body: any; observedAt: number } | undefined;+ let finalRead = false;for (;;) {+ signal.throwIfAborted();assertBoatNotDeleting(boxId);+ if (last && clock.now() >= until) return describeStatus(args.process_id, last.body, last.observedAt);const deadline = waitMs ? clock.timeout(Math.max(1, until - clock.now())) : null;const readSignal = deadline ? AbortSignal.any([signal, deadline]) : signal;let response: Response, body: any;@@ -190,8 +193,12 @@ export async function backgroundStatus(const observedAt = clock.now();last = { body, observedAt };const remaining = until - observedAt;- if (!body.running || remaining <= lastReadWindow) return describeStatus(args.process_id, body);- await clock.sleep(Math.min(POLL_MS, remaining - lastReadWindow), signal);+ if (!body.running || remaining <= 0 || finalRead) return describeStatus(args.process_id, body);+ const inLastWindow = remaining <= lastReadWindow;+ finalRead = inLastWindow;+ await clock.sleep(inLastWindow ? Math.max(1, Math.floor(remaining / 2)) : Math.min(POLL_MS, remaining - lastReadWindow), signal);+ signal.throwIfAborted();+ if (clock.now() >= until) return describeStatus(args.process_id, body, observedAt);}}