Skip to content

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

fix(runtime): only settle a pending settlement while the node still runs

koeria14b421cdd1c5
diff --git a/server/runtime/runtime.test.ts b/server/runtime/runtime.test.ts
index de17a0b654..d343605cfe 100644
--- a/server/runtime/runtime.test.ts
+++ b/server/runtime/runtime.test.ts
@@ -1701,6 +1701,29 @@ describe("Runtime: a settlement the store refused is retried, not lost", () => {
expect(checkInvariants(w.store, w.runner.live())).toEqual([]);
});
+
+ it("never replays a refused settlement onto a node the user cancelled 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();
+ 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)?.status).toBe("running");
+ // The user stops the tree while the settlement is still pending.
+ w.runtime.cancel(child.id);
+ w.runtime.tick();
+ expect(w.store.get(child.id)).toMatchObject({ status: "cancelled", outcome: "stopped" });
+ // The refused settlement must not overwrite the user's stop with the completed result.
+ expect(w.store.get(child.id)?.result).toBe("Stopped by user");
+ 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());
diff --git a/server/runtime/runtime.ts b/server/runtime/runtime.ts
index f157e3f903..a7acbafc0a 100644
--- a/server/runtime/runtime.ts
+++ b/server/runtime/runtime.ts
@@ -536,7 +536,13 @@ export class Runtime {
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; }
+ // 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") {
+ this.pendingSettlements.delete(id);
+ continue;
+ }
try {
this.withBatch(() => this.finishTurn(n, pending.result, pending.live));
this.pendingSettlements.delete(id);