Skip to content

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

Preserve output from failed status checks

WAI LOK HOde15ef077032
diff --git a/server/cloud-computer-background.test.ts b/server/cloud-computer-background.test.ts
index f32914c2d5..dee17791e8 100644
--- a/server/cloud-computer-background.test.ts
+++ b/server/cloud-computer-background.test.ts
@@ -82,7 +82,7 @@ describe("background status deadlines", () => {
expect(await pending).toMatchObject({ isError: true, content: [{ text: expect.stringContaining("call exec_status again") }] });
});
- it.each(["finished", "running", "timeout"] as const)("checks a short wait on a controlled clock: %s", async outcome => {
+ it.each(["finished", "running", "slow", "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 = {
@@ -95,7 +95,10 @@ describe("background status deadlines", () => {
},
};
const fetchStatus = vi.spyOn(globalThis, "fetch").mockImplementation(async (_url, init) => {
- if (fetchStatus.mock.calls.length === 1) return Response.json({ running: true, stdout: "partial", stderr: "" });
+ if (fetchStatus.mock.calls.length === 1) return Response.json({ running: true, stdout: outcome === "server_error" ? "x".repeat(4_001) : "partial", stderr: "" });
+ 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", exitCode: 3, stdout: "final", stderr: "" });
return new Promise<Response>((_resolve, reject) => {
init?.signal?.addEventListener("abort", () => reject(init.signal!.reason), { once: true });
@@ -104,18 +107,21 @@ describe("background status deadlines", () => {
const processId = background.processIdOn(BOX, 41);
const pending = background.backgroundStatus({ box: { token: OWN_KEY } } as AppConfig, BOX,
{ process_id: processId, wait_seconds: 1 }, new AbortController().signal, clock);
- await vi.advanceTimersByTimeAsync(0);
- expect(fetchStatus).toHaveBeenCalledTimes(1);
- await vi.advanceTimersByTimeAsync(749);
- expect(fetchStatus).toHaveBeenCalledTimes(1);
- await vi.advanceTimersByTimeAsync(1);
+ await vi.advanceTimersByTimeAsync(1_000);
expect(fetchStatus).toHaveBeenCalledTimes(2);
- if (outcome === "timeout") await vi.advanceTimersByTimeAsync(250);
- const result = json(await pending);
+ 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 === "not_found" || outcome === "server_error" || outcome === "malformed") {
+ expect(raw.isError).toBe(true);
+ expect(result).toMatchObject({ error: expect.any(String), lastObserved: { observedAt: "2026-01-01T00:00:00.000Z", stdout: outcome === "server_error" ? "x".repeat(4_000) : "partial", stderr: "", note: expect.stringContaining("may have changed") } });
+ expect(result.lastObserved.status).toBeUndefined();
+ if (outcome === "server_error") expect(result.lastObserved.outputCut).toContain("Only the end");
+ 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, exitCode: outcome === "finished" ? 3 : null, 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.ts
index f4d2d91636..af0a4d13ff 100644
--- a/server/cloud-computer-background.ts
+++ b/server/cloud-computer-background.ts
@@ -13,7 +13,7 @@ export const STATUS_WAIT_MAX_SECONDS = 60;
export const STDOUT_TAIL_CHARS = 4_000;
export const STDERR_TAIL_CHARS = 2_000;
const POLL_MS = 2_000;
-const LAST_READ_WINDOW_MS = 250;
+const MAX_LAST_READ_WINDOW_MS = 1_000;
/** Bounds what a forgotten background server can cost: Boat bills while a computer is on. */
export const BACKGROUND_KEEPS_AWAKE_MS = 2 * 60 * 60_000;
const MAX_TRACKED_PER_COMPUTER = 20;
@@ -143,6 +143,19 @@ function describeStatus(processId: string, body: any, observedAt?: number): Clou
}));
}
+function statusError(message: string, last?: { body: any; observedAt: number }): CloudComputerResult {
+ if (!last) return text(message, true);
+ const stdout = tail(last.body.stdout, STDOUT_TAIL_CHARS), stderr = tail(last.body.stderr, STDERR_TAIL_CHARS);
+ return text(JSON.stringify({
+ error: message,
+ lastObserved: {
+ observedAt: new Date(last.observedAt).toISOString(), stdout: stdout.text, stderr: stderr.text,
+ ...(stdout.cut || stderr.cut || last.body.stdoutTruncated === true || last.body.stderrTruncated === true ? { outputCut: CUT } : {}),
+ note: "These outputs were last observed at this time and may have changed.",
+ },
+ }), true);
+}
+
export async function backgroundStatus(
cfg: AppConfig,
boxId: string,
@@ -154,6 +167,7 @@ export async function backgroundStatus(
if (processId === null) return text(OTHER_COMPUTER, true);
const waitMs = Math.min(args.wait_seconds ?? 0, STATUS_WAIT_MAX_SECONDS) * 1_000;
const until = clock.now() + waitMs;
+ const lastReadWindow = Math.min(MAX_LAST_READ_WINDOW_MS, waitMs / 2);
let last: { body: any; observedAt: number } | undefined;
for (;;) {
assertBoatNotDeleting(boxId);
@@ -169,15 +183,15 @@ export async function backgroundStatus(
if (deadline?.aborted) return last ? describeStatus(args.process_id, last.body, last.observedAt) : text(STATUS_LATE, true);
throw error;
}
- if (response.status === 404) return text(UNKNOWN_PROCESS, true);
- if (!response.ok) return text(boatErrorMessage(response.status, "Reading the command's status", body, included(cfg)), true);
- if (typeof body?.running !== "boolean") return text("The cloud computer gave an unreadable answer about this command. Call exec_status again.", true);
+ if (response.status === 404) return statusError(UNKNOWN_PROCESS, last);
+ if (!response.ok) return statusError(boatErrorMessage(response.status, "Reading the command's status", body, included(cfg)), last);
+ if (typeof body?.running !== "boolean") return statusError("The cloud computer gave an unreadable answer about this command. Call exec_status again.", last);
if (!body.running) untrack(boxId, processId);
const observedAt = clock.now();
last = { body, observedAt };
const remaining = until - observedAt;
- if (!body.running || remaining <= LAST_READ_WINDOW_MS) return describeStatus(args.process_id, body);
- await clock.sleep(Math.min(POLL_MS, remaining - LAST_READ_WINDOW_MS), signal);
+ if (!body.running || remaining <= lastReadWindow) return describeStatus(args.process_id, body);
+ await clock.sleep(Math.min(POLL_MS, remaining - lastReadWindow), signal);
}
}