Skip to content

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

Merge pull request #4924 from Coke1120/fix/4913-background-handoff (follow-up commits)

milind-soni35fc90515bbb
diff --cc server/cloud-computer-background.test.ts
index aeec252c8e,6bbfa90cf8..f1af38383d
--- a/server/cloud-computer-background.test.ts
+++ b/server/cloud-computer-background.test.ts
@@@ -78,11 -78,58 +78,58 @@@ describe("background status deadlines"
}));
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_000);
- expect(await pending).toMatchObject({ isError: true, content: [{ text: expect.stringContaining("call exec_status again") }] });
+ await vi.advanceTimersByTimeAsync(1_500);
+ expect(json(await pending)).toMatchObject({ status: "finished", exitCode: 0, stdout: "done" });
});
- it.each(["finished", "running", "timeout"] 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 = {
diff --cc server/cloud-computer-background.ts
index 95a1022f92,a1dec23783..43c0ce6a3b
--- a/server/cloud-computer-background.ts
+++ b/server/cloud-computer-background.ts
@@@ -154,10 -167,14 +167,14 @@@ 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;
+ 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 deadline = waitMs && last ? clock.timeout(Math.max(1, until - clock.now())) : null;
const readSignal = deadline ? AbortSignal.any([signal, deadline]) : signal;
let response: Response, body: any;
try {