Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
perf(routines): sweep watched turn events in linear time
Afloat06d9c4006148
diff --git a/server/routine-run-watch.test.ts b/server/routine-run-watch.test.tsnew file mode 100644index 0000000000..8dd0e25397--- /dev/null+++ b/server/routine-run-watch.test.ts@@ -0,0 +1,46 @@+import { expect, it } from "vitest";++import { COUNT_STEP_CAP_MS } from "./routine-bounds.ts";+import { RunWatch, TURN_GONE_MS } from "./routine-run-watch.ts";+import type { RoutineRun } from "./routines.ts";++const run = (id: string, more: Partial<RoutineRun> = {}): RoutineRun => ({+ id, routineId: id, routineName: "Check", prompt: "Check", target: "bot", botId: "bot-1", runOn: "maus",+ scheduledFor: 1, createdAt: 1, startedAt: 1, threadId: `thread-${id}`, status: "running", manual: false, ...more,+});++it("keeps event sweeping proportional to the number of runs and conversations", () => {+ function reads(count: number): number {+ let looked = 0;+ const runs = Array.from({ length: count }, (_, i) => {+ const row = run(String(i));+ Object.defineProperty(row, "threadId", { get: () => { looked++; return `thread-${i}`; } });+ return row;+ });+ const watch = new RunWatch(() => false);+ for (let i = 0; i < count; i++) watch.seen(`thread-${i}`);+ expect(watch.lost(runs, 1)).toEqual([]);+ return looked;+ }+ // Doubling the rows and their event histories must not quadruple the scan work.+ expect(reads(200)).toBeLessThan(reads(100) * 3);+});++it("retains a live thread's events while forgetting ended runs and absent conversations", () => {+ const watch = new RunWatch(() => false);+ const working = run("working"), ended = run("ended", { status: "completed" });+ const runs = [ended, working];+ watch.seen(ended.threadId!);+ watch.seen("not-a-routine");+ expect(watch.lost(runs, 1)).toEqual([]);+ let now = 1;+ for (; now < TURN_GONE_MS - COUNT_STEP_CAP_MS; now += COUNT_STEP_CAP_MS) expect(watch.lost(runs, now)).toEqual([]);+ watch.seen(working.threadId!);+ expect(watch.lost(runs, now)).toEqual([]);+ for (let elapsed = COUNT_STEP_CAP_MS; elapsed < TURN_GONE_MS; elapsed += COUNT_STEP_CAP_MS) {+ expect(watch.lost(runs, now + elapsed)).toEqual([]);+ }+ expect(watch.lost(runs, now + TURN_GONE_MS)).toEqual([working]);+ expect(watch.lost([], now + TURN_GONE_MS)).toEqual([]);+ expect(watch.dueAt([], now)).toBeNull();+});diff --git a/server/routine-run-watch.ts b/server/routine-run-watch.tsindex 2cb2799a9e..e65198175b 100644--- a/server/routine-run-watch.ts+++ b/server/routine-run-watch.ts@@ -26,10 +26,11 @@ export class RunWatch {/** The runs whose turn has now been gone for TURN_GONE_MS. */lost<R extends RoutineRun>(runs: readonly R[], now: number): R[] {- const lost: R[] = [], watched = new Set<string>();+ const lost: R[] = [], watched = new Set<string>(), threads = new Set<string>();if (!this.live) return lost;for (const run of runs) {if (!started(run)) continue;+ threads.add(run.threadId!);const events = this.events.get(run.threadId!) ?? 0, before = this.gone.get(run.id);if (this.live(run) !== false) continue;watched.add(run.id);@@ -38,7 +39,7 @@ export class RunWatch {if (ms >= TURN_GONE_MS) lost.push(run);}for (const id of this.gone.keys()) if (!watched.has(id)) this.gone.delete(id);- for (const threadId of this.events.keys()) if (!runs.some((run) => run.threadId === threadId && started(run))) this.events.delete(threadId);+ for (const threadId of this.events.keys()) if (!threads.has(threadId)) this.events.delete(threadId);return lost;}