Skip to content

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

Runtime: keep lastActivityAt fresh while a turn produces work

Seyamalam957d7f8194ce
diff --git a/server/runtime/runtime.test.ts b/server/runtime/runtime.test.ts
index 0d094f157b..15b6dc2d2b 100644
--- a/server/runtime/runtime.test.ts
+++ b/server/runtime/runtime.test.ts
@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
import { addHeldSecretScrubber, HELD_SECRET_MASK } from "../../shared/redact.ts";
import { checkInvariants } from "./invariants.ts";
-import { Runtime, type BeginRequest } from "./runtime.ts";
+import { PROGRESS_WRITE_MS, Runtime, type BeginRequest } from "./runtime.ts";
import { openRuntimeStore, type RuntimeStore } from "./store.ts";
import { CUT_TWICE, INTERRUPTED_OUTWARD, loopStop, nextGuard, outwardDetail } from "./tree.ts";
import type { Assignment, Clock, EnqueueRequest, Limits, RunInput, RunResult, TurnRunner } from "./types.ts";
@@ -345,6 +345,32 @@ describe("Runtime: one tree, one lane", () => {
invariants(w);
});
+ it("re-stamps a running node's lastActivityAt on real activity, on a coarse cadence and without event rows", () => {
+ const w = world(() => "hold");
+ const root = w.runtime.begin(beginReq());
+ const a = ok(w.runtime.enqueue(toBot("a", root.id)));
+ w.runtime.tick();
+ const dispatched = w.store.get(a.id)!.lastActivityAt;
+ const rootDispatched = w.store.get(root.id)!.lastActivityAt;
+ const eventsBefore = w.store.events(a.id).length;
+ // Inside the cadence the stall windows still move, but the row keeps its dispatch stamp.
+ w.clock.advance(PROGRESS_WRITE_MS - 1);
+ w.runtime.progress(a.id);
+ expect(w.store.get(a.id)!.lastActivityAt).toBe(dispatched);
+ // Past it, one progress call durably re-stamps the node and its root.
+ w.clock.advance(1);
+ w.runtime.progress(a.id);
+ const stamped = w.store.get(a.id)!.lastActivityAt;
+ expect(stamped).toBe(w.clock.t);
+ expect(stamped).toBeGreaterThan(rootDispatched);
+ expect(w.store.get(root.id)!.lastActivityAt).toBe(stamped);
+ // The next event inside the new window leaves the row alone, and none of this wrote event rows.
+ w.runtime.progress(a.id);
+ expect(w.store.get(a.id)!.lastActivityAt).toBe(stamped);
+ expect(w.store.events(a.id).length).toBe(eventsBefore);
+ invariants(w);
+ });
+
it("reconciles a restart from stored data: rerun once, cut after handing out work, re-park, fail a rerun cut again", async () => {
const store = openRuntimeStore(":memory:");
const first = world(() => "hold", {}, store);
diff --git a/server/runtime/runtime.ts b/server/runtime/runtime.ts
index dc1db18733..276cfb16a7 100644
--- a/server/runtime/runtime.ts
+++ b/server/runtime/runtime.ts
@@ -39,6 +39,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;
+/** progress() runs once per runtime event; lastActivityAt catches up at this cadence, not per event. */
+export const PROGRESS_WRITE_MS = 10_000;
const defaultClock: Clock = { now: () => Date.now(), monotonic: () => performance.now() };
@@ -533,16 +535,28 @@ export class Runtime {
this.store.update(n, { kind: "held", at: this.clock.now(), detail: clip(why, DETAIL_MAX) });
}
- /** The host saw activity on this running turn: its stall window restarts. */
+ /** The host saw activity on this running turn: its stall window restarts, and lastActivityAt
+ * catches up on the PROGRESS_WRITE_MS cadence so the stored row reads as live, not frozen at dispatch. */
progress(id: string): void {
+ const now = this.clock.now();
const turn = this.turns.get(id);
+ const n = this.store.get(id);
if (turn) {
turn.activeAt = this.clock.monotonic();
- this.rootActivity.set(turn.rootId, this.clock.now());
- return;
+ this.rootActivity.set(turn.rootId, now);
+ } else if (n?.status === "running") {
+ this.rootActivity.set(n.rootId, now);
}
- const n = this.store.get(id);
- if (n?.status === "running") this.rootActivity.set(n.rootId, this.clock.now());
+ if (!n || n.status !== "running" || now - n.lastActivityAt < PROGRESS_WRITE_MS) return;
+ n.lastActivityAt = now;
+ const root = n.rootId === n.id ? undefined : this.store.get(n.rootId);
+ this.store.transaction(() => {
+ this.store.update(n);
+ if (root && !isTerminal(root)) {
+ root.lastActivityAt = now;
+ this.store.update(root);
+ }
+ });
}
/** A parked question is answered, by the person unless `by` says otherwise; the next turn carries it. */
diff --git a/server/runtime/types.ts b/server/runtime/types.ts
index d16088065b..e7f195b256 100644
--- a/server/runtime/types.ts
+++ b/server/runtime/types.ts
@@ -109,7 +109,8 @@ export interface Assignment {
restart?: "rerun" | "cut" | "teammates";
createdAt: number;
startedAt?: number;
- /** Last durable progress: a dispatch, a settlement, a child's settlement. */
+ /** Last durable progress: a dispatch, a settlement, a child's settlement — and, while a turn
+ * runs, activity the host reports through progress(), written on a coarse cadence. */
lastActivityAt: number;
finishedAt?: number;
/** What the assignee last said or how it ended (at most 12 000 characters). */