Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(runtime): retry a settlement the store refused instead of losing the result
koeriadc668ab6c9d3
diff --git a/server/runtime/runtime.test.ts b/server/runtime/runtime.test.tsindex 43a59978a3..de17a0b654 100644--- a/server/runtime/runtime.test.ts+++ b/server/runtime/runtime.test.ts@@ -1675,3 +1675,45 @@ describe("Runtime: a turn cut by the restart after a memory freeze", () => {store.close();});});++describe("Runtime: a settlement the store refused is retried, not lost", () => {+ it("keeps a completed result whose settle threw, settles it on the next tick, and never expires the tree meanwhile", async () => {+ const w = world(() => "hold", { turnStallMs: 1_000, stallMs: 10_000 });+ const root = w.runtime.begin(beginReq());+ const child = ok(w.runtime.enqueue(toBot("builder", root.id)));+ w.runtime.tick();+ expect(w.store.get(child.id)?.status).toBe("running");+ // One throw from the store's transaction stands in for a transient SQLite failure during settle.+ const real = w.store.transaction.bind(w.store);+ let refuse = true;+ (w.store as { transaction: (fn: () => unknown) => unknown }).transaction = fn => {+ if (refuse) { refuse = false; throw new Error("disk hiccup"); }+ return real(fn);+ };+ await w.runner.release(child.id, { kind: "completed", text: "all done" });+ expect(w.store.get(child.id)).toMatchObject({ status: "running" });+ expect(w.logs.some(l => l.includes("settling") && l.includes("will retry"))).toBe(true);+ // Longer than both windows: a pending settlement must not be read as a stall.+ w.clock.advance(60_000);+ w.runtime.tick();+ expect(w.store.get(child.id)).toMatchObject({ status: "completed", outcome: "done", result: "all done" });+ expect(w.store.events(child.id).some(e => e.kind === "stalled")).toBe(false);+ expect(checkInvariants(w.store, w.runner.live())).toEqual([]);+ });++ it("retries a refused end() the same way", async () => {+ const w = world(() => "hold", { turnStallMs: 1_000, stallMs: 10_000 });+ const root = w.runtime.begin(beginReq());+ w.runtime.tick();+ const real = w.store.transaction.bind(w.store);+ let refuse = true;+ (w.store as { transaction: (fn: () => unknown) => unknown }).transaction = fn => {+ if (refuse) { refuse = false; throw new Error("disk hiccup"); }+ return real(fn);+ };+ w.runtime.end(root.id, { ok: true, text: "Handed off" });+ expect(w.store.get(root.id)?.status).toBe("running");+ w.runtime.tick();+ expect(w.store.get(root.id)).toMatchObject({ status: "completed", outcome: "done", result: "Handed off" });+ });+});diff --git a/server/runtime/runtime.ts b/server/runtime/runtime.tsindex 17a5f3827a..f157e3f903 100644--- a/server/runtime/runtime.ts+++ b/server/runtime/runtime.ts@@ -85,6 +85,8 @@ export class Runtime {private readonly rootActivity = new Map<string, number>();private readonly heldFor = new Map<string, string>();private readonly reportDue = new Set<string>();+ /** Results whose SQLite settlement threw: retried each tick, deleted only once the transition commits. */+ private readonly pendingSettlements = new Map<string, { live: LiveTurn | undefined; result: RunResult }>();private batch: Map<string, Assignment> | null = null;private lastThrew = false;private readonly rerunGapMs: number;@@ -196,7 +198,9 @@ export class Runtime {try {this.withBatch(() => this.finishTurn(n, outcome, live));} catch (error) {- this.log(`end(${rootId}) failed: ${String(error)}`);+ // The same loss as turnEnded: keep the outcome and settle it on a later tick.+ this.pendingSettlements.set(rootId, { live, result: outcome });+ this.log(`end(${rootId}) failed, will retry: ${String(error)}`);}}@@ -314,6 +318,8 @@ export class Runtime {}private tickBody(): void {+ // First: settle any result the store refused earlier, before the windows below read it as stalled.+ this.retrySettlements();const now = this.clock.now();const mono = this.clock.monotonic();const live = this.store.all({ nonTerminal: true });@@ -349,7 +355,7 @@ export class Runtime {const anchor = Math.max(this.since(root.lastActivityAt), this.rootActivity.get(rootId) ?? 0);if (now - anchor <= this.limits.stallMs) continue;const tree = this.store.all({ rootId });- if (tree.some(n => n.status === "needs_input" || (n.status === "queued" && (n.continuation !== undefined || this.away(n))) || (n.status === "running" && this.away(n)))) continue;+ if (tree.some(n => n.status === "needs_input" || (n.status === "queued" && (n.continuation !== undefined || this.away(n))) || (n.status === "running" && this.away(n))) || tree.some(n => this.pendingSettlements.has(n.id))) continue;if (now - Math.max(anchor, ...tree.map(n => this.heldAway.get(n.id) ?? 0)) <= this.limits.stallMs) continue;this.store.update(root, { kind: "stalled", at: now, detail: `no progress for ${duration(now - anchor)}` });this.cancelTree(root, `no progress for ${duration(this.limits.stallMs)}`, "failed", "stalled");@@ -510,14 +516,33 @@ export class Runtime {/** Only the live handle's result settles the node: an ended, cancelled or stalled turn has no handle. */private turnEnded(id: string, live: LiveTurn, result: RunResult): void {+ if (this.turns.get(id) !== live) return;+ const n = this.store.get(id);+ if (!n || n.status !== "running") return;+ if (this.closing && result.kind === "failed") return;try {- if (this.turns.get(id) !== live) return;- const n = this.store.get(id);- if (!n || n.status !== "running") return;- if (this.closing && result.kind === "failed") return;this.withBatch(() => this.finishTurn(n, result, live));} catch (error) {- this.log(`settling ${id} failed: ${String(error)}`);+ // The completed result exists only here: keep it and settle from it on a later tick, instead of+ // leaving the node running with no live turn and nothing left to settle it.+ this.pendingSettlements.set(id, { live, result });+ this.log(`settling ${id} failed, will retry: ${String(error)}`);+ }+ }++ /** Settles a node from the result it ended with: the pending record is dropped only once the transition commits,+ * or the node is gone. While one is pending the node is exempt from the tree's inactivity expiry (tickBody). */+ private retrySettlements(): void {+ if (this.pendingSettlements.size === 0) return;+ for (const [id, pending] of this.pendingSettlements) {+ const n = this.store.get(id);+ if (!n || isTerminal(n)) { this.pendingSettlements.delete(id); continue; }+ try {+ this.withBatch(() => this.finishTurn(n, pending.result, pending.live));+ this.pendingSettlements.delete(id);+ } catch (error) {+ this.log(`settling ${id} failed again, still pending: ${String(error)}`);+ }}}