Skip to content

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

Runtime: a refused settlement is retried on five ticks at most, and never onto a turn dispatched since

milind-soni827c728bfff1
diff --git a/server/runtime/runtime.test.ts b/server/runtime/runtime.test.ts
index d343605cfe..48ccc8ca17 100644
--- a/server/runtime/runtime.test.ts
+++ b/server/runtime/runtime.test.ts
@@ -1739,4 +1739,27 @@ describe("Runtime: a settlement the store refused is retried, not lost", () => {
w.runtime.tick();
expect(w.store.get(root.id)).toMatchObject({ status: "completed", outcome: "done", result: "Handed off" });
});
+
+ it("gives up a settlement the store keeps refusing after a few ticks, so the tree's own expiry ends it", async () => {
+ const w = world(() => "hold", { turnStallMs: 10_000_000, stallMs: 10_000 });
+ const root = w.runtime.begin(beginReq());
+ const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
+ 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) throw new Error("disk full");
+ return real(fn);
+ };
+ await w.runner.release(child.id, { kind: "completed", text: "all done" });
+ // The refusing store fails the tick's other writes too; each tick still retries the settlement first.
+ for (let at = 0; at < 5; at += 1) { try { w.runtime.tick(); } catch { /* the same refusal */ } }
+ expect(w.logs.some(l => l.includes(`settling ${child.id}`) && l.includes("giving up after 5 retries"))).toBe(true);
+ refuse = false;
+ w.clock.advance(60_000);
+ w.runtime.tick();
+ expect(w.store.events(root.id).some(e => e.kind === "stalled")).toBe(true);
+ expect(w.store.get(child.id)?.status).not.toBe("running");
+ expect(w.store.get(child.id)?.result).not.toBe("all done");
+ });
});
diff --git a/server/runtime/runtime.ts b/server/runtime/runtime.ts
index a7acbafc0a..2a8f2f74e1 100644
--- a/server/runtime/runtime.ts
+++ b/server/runtime/runtime.ts
@@ -55,6 +55,8 @@ const BRIEF_MAX = 12_000;
const RESULT_MAX = 12_000;
const STOPPED_WITH_REQUEST = "Stopped with the request it was for";
const DETAIL_MAX = 400;
+/** Ticks a settlement the store refused is retried on; past them it is dropped, so the tree's own expiry ends it. */
+const SETTLE_RETRIES = 5;
/** progress() runs once per runtime event; lastActivityAt catches up at this cadence, not per event. */
export const PROGRESS_WRITE_MS = 10_000;
@@ -86,7 +88,7 @@ export class Runtime {
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 readonly pendingSettlements = new Map<string, { live: LiveTurn | undefined; result: RunResult; executions: number; attempts: number }>();
private batch: Map<string, Assignment> | null = null;
private lastThrew = false;
private readonly rerunGapMs: number;
@@ -199,7 +201,7 @@ export class Runtime {
this.withBatch(() => this.finishTurn(n, outcome, live));
} catch (error) {
// The same loss as turnEnded: keep the outcome and settle it on a later tick.
- this.pendingSettlements.set(rootId, { live, result: outcome });
+ this.pendingSettlements.set(rootId, { live, result: outcome, executions: n.executions, attempts: 0 });
this.log(`end(${rootId}) failed, will retry: ${String(error)}`);
}
}
@@ -516,17 +518,19 @@ 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;
+ let executions: number | undefined;
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;
+ executions = n.executions;
this.withBatch(() => this.finishTurn(n, result, live));
} catch (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)}`);
+ if (executions !== undefined) this.pendingSettlements.set(id, { live, result, executions, attempts: 0 });
+ this.log(`settling ${id} failed${executions !== undefined ? ", will retry" : ""}: ${String(error)}`);
}
}
@@ -539,7 +543,8 @@ export class Runtime {
// Only a still-running node may take the stored result: a node that left
// running some other way (needs_input, cancelled tree) must not have a
// stale settlement replayed onto it.
- if (!n || isTerminal(n) || n.status !== "running") {
+ // A node dispatched again since is running another turn: the stored result is not that turn's.
+ if (!n || isTerminal(n) || n.status !== "running" || n.executions !== pending.executions) {
this.pendingSettlements.delete(id);
continue;
}
@@ -547,7 +552,10 @@ export class Runtime {
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)}`);
+ pending.attempts += 1;
+ const giveUp = pending.attempts >= SETTLE_RETRIES;
+ if (giveUp) this.pendingSettlements.delete(id);
+ this.log(`settling ${id} failed again, ${giveUp ? `giving up after ${SETTLE_RETRIES} retries` : "still pending"}: ${String(error)}`);
}
}
}