Skip to content

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

fix(routines): a scheduler pass writes its queued-run wait marks once, not once per run (#4260)

CI fixture2d156d9097b7
diff --git a/server/routines.ts b/server/routines.ts
index 87d09b8038..71ca0062a3 100644
--- a/server/routines.ts
+++ b/server/routines.ts
@@ -4,7 +4,7 @@ import { dirname, join } from "node:path";
import { DATA_DIR } from "./config.ts";
import type { RuntimeEvent } from "./contracts.ts";
-import { OwnedFile } from "./atomic.ts"; import { saveSoon } from "./save-batch.ts"; import { RunProgressSave } from "./routine-progress-save.ts";
+import { OwnedFile } from "./atomic.ts"; import { beginSaves, endSaves, saveSoon } from "./save-batch.ts"; import { RunProgressSave } from "./routine-progress-save.ts";
import { redactSecretsInText } from "./redact.ts";
import { activeRunsOf, nextDueOf, schedulerDueOf, wakeHoldOf } from "./routine-due.ts";
import { startDueTimer, type DueTimer } from "./due-timer.ts";
@@ -1015,6 +1015,9 @@ export class RoutineManager {
async tick(): Promise<void> {
if (this.closing || this.ticking) return;
this.ticking = true;
+ // A queued run's wait crosses its next minute each pass it is seen so; those marks ride the pass's next whole write, together, not one per run (server/save-batch.ts).
+ let batched = false;
+ const openSaves = () => { if (!batched) { beginSaves(); batched = true; } }, closeSaves = () => { if (batched) { endSaves(); batched = false; } };
try {
const now = this.now(); this.gate.sweep((id) => this.runs.some((run) => run.id === id && run.status === "queued"));
if (this.unreadable) {
@@ -1040,8 +1043,8 @@ export class RoutineManager {
}
const dueRoutines = this.routines.filter(isDue);
const scheduledRuns: RoutineRun[] = [], countedSkips: RoutineRun[] = []; let lateStarts = 0;
- const allocations: ResultsThreadAllocation[] = [];
- const checks: Array<{ id: string; hold?: CheckHold }> = [];
+ const allocations: ResultsThreadAllocation[] = [], checks: Array<{ id: string; hold?: CheckHold }> = [];
+ openSaves();
if (dueRoutines.length > 0) {
this.commitMutation(() => {
for (const routine of dueRoutines) {
@@ -1086,7 +1089,7 @@ export class RoutineManager {
// Oldest first, by id: a rollback across awaits can replace the run objects, and the array with them.
let indexed: RoutineRun[] | null = null, byId = new Map<string, RoutineRun>();
for (const id of this.runs.map((run) => run.id)) {
- if (this.closing) return;
+ if (this.closing) return; openSaves();
if (indexed !== this.runs) [indexed, byId] = [this.runs, new Map(this.runs.map((run) => [run.id, run]))];
const run = byId.get(id);
if (!run || run.status !== "queued" || (run.retryAt ?? 0) > now) continue;
@@ -1097,9 +1100,9 @@ export class RoutineManager {
if (definition?.schedule.type === "interval") {
const latest = latestIntervalOccurrence(definition.schedule, now);
if (latest !== null && latest > run.scheduledFor) {
+ // A stamp the app is shown is on disk when it is shown (reporting persists cards): these stamps save at once, not with the pass's waiting marks.
run.scheduledFor = latest;
- this.saveLater();
- this.emitRun(run);
+ this.save(); this.emitRun(run);
}
const requiresCurrentDayOccurrence =
definition.schedule.weekdays !== undefined || definition.schedule.window !== undefined;
@@ -1124,14 +1127,11 @@ export class RoutineManager {
if (state === "busy") {
if (run.deferredAt == null) {
run.deferredAt = now;
- this.saveLater();
- this.emitRun(run);
+ this.save(); this.emitRun(run);
}
if (run.deferredNoticeAt == null && !run.checkKey && now - run.deferredAt >= ROUTINE_DEFERRAL_NOTICE_MS) {
run.deferredNoticeAt = now;
- this.saveLater();
- this.emitRun(run);
- this.options.onRunDeferred?.(cloneRun(run));
+ this.save(); this.emitRun(run); this.options.onRunDeferred?.(cloneRun(run));
}
const busy = busyWait(run, this.clock, triggerSource === "schedule" && !held && run.checkKey === undefined, now);
if (busy.missed) this.failRun(Object.assign(run, busy.missed.codes), busy.missed.text, "missed");
@@ -1141,7 +1141,7 @@ export class RoutineManager {
if (state === "missing") { this.failRun(run, this.missingTargetMessage(run.target)); continue; }
const paused = run.target === "room-goal" ? null : this.options.pausedText?.(run.botId, run);
if (!this.ledger.ready(run)) continue; if (this.ledger.startedElsewhere(run)) { const line = codedLine("error", "routines.server.duplicate"); Object.assign(run, line.codes); void this.cancelRun(run.id, line.text); continue; }
- if (paused) { if (triggerSource !== "manual" && this.options.held?.(run.botId)) void this.cancelRun(run.id, paused); else this.failRun(run, paused); continue; } const startedBy = run.byPerson ? "manual" : triggerSource, capped = this.options.capText?.(run.botId, startedBy); if (capped) { if (startedBy === "manual") this.failRun(run, capped); else void this.cancelRun(run.id, capped); continue; } const gated = triggerSource === "manual" ? false : this.gate.hold(run); if (gated === true) { if (this.options.preparing && run.attention !== this.options.preparing) { run.attention = this.options.preparing; this.emitRun(run); } continue; } if (gated) { const again = wakeRetry(run, gated, definition, this.now()); if (again === true) { this.saveLater(); this.emitRun(run); } else this.failRun(run, again); continue; }
+ if (paused) { if (triggerSource !== "manual" && this.options.held?.(run.botId)) void this.cancelRun(run.id, paused); else this.failRun(run, paused); continue; } const startedBy = run.byPerson ? "manual" : triggerSource, capped = this.options.capText?.(run.botId, startedBy); if (capped) { if (startedBy === "manual") this.failRun(run, capped); else void this.cancelRun(run.id, capped); continue; } const gated = triggerSource === "manual" ? false : this.gate.hold(run); if (gated === true) { if (this.options.preparing && run.attention !== this.options.preparing) { run.attention = this.options.preparing; this.emitRun(run); } continue; } if (gated) { const again = wakeRetry(run, gated, definition, this.now()); if (again === true) { this.save(); this.emitRun(run); } else this.failRun(run, again); continue; }
const startedAt = this.now();
const suffix = ` · ${new Date(startedAt).toLocaleString("en-US", { month: "short", day: "numeric", hour: "numeric", minute: "2-digit" })}`;
const title = `${run.routineName.slice(0, 80 - suffix.length).trimEnd()}${suffix}`;
@@ -1161,7 +1161,7 @@ export class RoutineManager {
}
Object.assign(run, { threadId: task.threadId, startedAt, status: "running" } satisfies Partial<RoutineRun>); this.clock.from(run, "workedMs", startedAt); delete run.workedMs; this.clock.stop(run, "busyMs", true);
try { this.save(); } catch (error) { Object.assign(run, { status: "queued", startedAt: undefined } satisfies Partial<RoutineRun>); throw error; }
- this.emitRun(run);
+ this.emitRun(run); closeSaves(); // the handoff yields: the start's write carried the pass's waiting marks, and later ones land at the pass's end
try {
const prompt = run.prompt ?? this.routines.find((r) => r.id === run.routineId)?.prompt;
if (!prompt) {
@@ -1197,7 +1197,7 @@ export class RoutineManager {
}
}
} finally {
- this.ticking = false; this.passedAt = this.now();
+ closeSaves(); this.ticking = false; this.passedAt = this.now();
}
}
diff --git a/server/save-batch.ts b/server/save-batch.ts
index f040730d8d..e5c480f24f 100644
--- a/server/save-batch.ts
+++ b/server/save-batch.ts
@@ -8,15 +8,27 @@ let depth = 0;
const pending = new Map<object, () => void>();
export function batchSaves<T>(run: () => T): T {
- depth += 1;
+ beginSaves();
try {
return run();
} finally {
- depth -= 1;
- if (depth === 0) writePending();
+ endSaves();
}
}
+/** The same pass, opened by hand, for one that yields between stretches of synchronous work: open before a stretch,
+ * close before each yield, so the stretch's saveSoon writes land once, at its end, not once per change. */
+export function beginSaves(): void {
+ depth += 1;
+}
+
+/** Closes one stretch (or the whole pass): what its saves asked for writes now. */
+export function endSaves(): void {
+ if (depth === 0) return;
+ depth -= 1;
+ if (depth === 0) writePending();
+}
+
/** Writes now, or once at the end of the running batch; `owner` keeps one pending write per file. */
export function saveSoon(owner: object, write: () => void): void {
if (depth === 0) write();
diff --git a/server/server-writes.test.ts b/server/server-writes.test.ts
index 4bb775a278..ce3eb7b2b5 100644
--- a/server/server-writes.test.ts
+++ b/server/server-writes.test.ts
@@ -139,6 +139,36 @@ describe("routines.json saves", () => {
manager.close();
});
+ it("write a busy scheduler pass's waiting marks once per pass, not once per run", async () => {
+ const file = join(dir, "routines.json");
+ let now = Date.now();
+ const options = {
+ file,
+ now: () => now,
+ botState: () => "busy" as const,
+ createTask: () => ({ threadId: "thread-1" }),
+ startTurn: async () => {},
+ };
+ const manager = new RoutineManager(options);
+ const schedule = { type: "interval" as const, everyMinutes: 5, anchorAt: now };
+ const first = manager.create({ name: "First", prompt: "One.", botId: "bot-1", schedule });
+ const second = manager.create({ name: "Second", prompt: "Two.", botId: "bot-1", schedule });
+ now = Math.max(first.nextRunAt!, second.nextRunAt!) + 30_000;
+ await manager.tick();
+ const restart = new RoutineManager({ ...options, startTurn: async () => {} }).listRuns();
+ expect(restart).toHaveLength(2);
+ expect(restart.every((run) => run.status === "queued" && run.deferredAt != null)).toBe(true);
+ vi.mocked(fs.renameSync).mockClear();
+ // A minute later (the routines' next occurrence is minutes away), each queued run's wait crosses its next minute: the pass writes both marks together, once.
+ now += 60_000;
+ await manager.tick();
+ expect(writesOf("routines.json")).toBe(1);
+ const waited = new RoutineManager({ ...options, startTurn: async () => {} }).listRuns();
+ expect(waited).toHaveLength(2);
+ expect(waited.every((run) => run.status === "queued" && (run.busyMs ?? 0) >= 60_000 && run.deferredAt != null)).toBe(true);
+ manager.close();
+ });
+
it("save a long run's progress once per window, so a crash loses at most that window", async () => {
const { manager, routine, restart, onDisk, deliver } = setup();
manager.runNow(routine.id);