Skip to content

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

fix: preserve checked routine result destinations

Sebastian B74e26667d35c
diff --git a/server/routine-checks-engine.test.ts b/server/routine-checks-engine.test.ts
index 8eaec92af5..4793945444 100644
--- a/server/routine-checks-engine.test.ts
+++ b/server/routine-checks-engine.test.ts
@@ -5,13 +5,13 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { CHECK_BOOT_HOLD_MS, CHECK_RUNS_PER_DAY } from "./routine-check-pacing.ts";
import { CHECK_WAKE_MS } from "./routine-due.ts";
-import { RoutineManager, type Routine, type RoutineCheckResult, type RoutineChecks, type RoutineInput, type RoutineRun } from "./routines.ts";
+import { RoutineManager, type Routine, type RoutineCheckResult, type RoutineChecks, type RoutineInput, type RoutineManagerOptions, type RoutineRun } from "./routines.ts";
const MINUTE = 60_000;
const dirs: string[] = [];
afterEach(() => { for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); });
-function harness(options: { bot?: () => "ready" | "busy" | "missing"; turn?: () => Promise<void> } = {}) {
+function harness(options: { bot?: () => "ready" | "busy" | "missing"; turn?: () => Promise<void>; resolveResultsThread?: RoutineManagerOptions["resolveResultsThread"] } = {}) {
const dir = mkdtempSync(join(tmpdir(), "omb-checks-"));
dirs.push(dir);
let now = new Date(2026, 9, 22, 9, 0, 0).getTime();
@@ -41,6 +41,7 @@ function harness(options: { bot?: () => "ready" | "busy" | "missing"; turn?: ()
startTurn: async (botId, threadId, prompt) => { started.push({ botId, threadId, prompt }); await options.turn?.(); },
onRunFailed: (run, alert) => failed.push({ run, alert }),
onRunDeferred: (run) => deferred.push(run),
+ resolveResultsThread: options.resolveResultsThread,
checks,
});
let manager = make();
@@ -177,6 +178,30 @@ describe("routines with checks", () => {
expect(h.started[0]!.botId).toBe("bot-b");
});
+ it.each([
+ { edit: false, botId: "bot-a" },
+ { edit: true, botId: "bot-b" },
+ ])("reports a checked manual run to $botId's main chat (bot edit: $edit)", async ({ edit, botId }) => {
+ const h = harness({ resolveResultsThread: (routine) => ({ threadId: `${routine.botId}-main`, main: true }) });
+ const routine = h.watch({ botId: "bot-a", deliver: null });
+ let open!: () => void;
+ h.gate.wait = new Promise((resolve) => { open = resolve; });
+ h.answers.push({ run: { prompt: "Checked work", key: "batch-1" } });
+ if (edit) h.answers.push({ run: { prompt: "Current bot's work", key: "batch-2" } });
+ const manual = h.manager.runNow(routine.id)!;
+ expect(manual.resultsThreadId).toBe("bot-a-main");
+ expect(h.manager.listRoutines()[0]!.resultsThreadId).toBeUndefined();
+ await h.tick();
+ if (edit) h.manager.update(routine.id, { botId });
+ open();
+ await h.settle();
+ const [run] = h.manager.listRuns();
+ expect(run).toMatchObject({ id: manual.id, botId, status: "running", resultsThreadId: `${botId}-main`, checkKey: edit ? "batch-2" : "batch-1" });
+ expect(h.manager.listRoutines()[0]!.resultsThreadId).toBeUndefined();
+ h.complete(run!);
+ expect(h.settled.at(-1)).toMatchObject({ id: manual.id, status: "completed", resultsThreadId: `${botId}-main` });
+ });
+
it("a check that finds nothing starts no AI turn and leaves no run, only when it last checked", async () => {
const h = harness();
const routine = h.watch();
diff --git a/server/routine-run-definition.ts b/server/routine-run-definition.ts
index 044d53f369..e4b08f83be 100644
--- a/server/routine-run-definition.ts
+++ b/server/routine-run-definition.ts
@@ -1,5 +1,26 @@
import { cloneAttachments } from "./routine-attachments.ts";
-import type { Routine, RoutineRun } from "./routines.ts";
+import type { Routine, RoutineManagerOptions, RoutineRun } from "./routines.ts";
+
+export type ResultsThreadAllocation = { botId: string; threadId: string };
+
+export function resolveRoutineResultsThread(
+ routine: Routine,
+ resolve: RoutineManagerOptions["resolveResultsThread"],
+ allocations: ResultsThreadAllocation[],
+ now: number,
+ sourceThreadId = routine.sourceThreadId,
+): string | undefined {
+ if (routine.target !== "bot" || !resolve) return routine.resultsThreadId;
+ const destination = resolve({ ...routine, sourceThreadId }, false);
+ const own = destination?.main ? undefined : destination?.threadId;
+ if (own !== routine.resultsThreadId) {
+ if (own) allocations.push({ botId: routine.botId, threadId: own });
+ if (own) routine.resultsThreadId = own;
+ else delete routine.resultsThreadId;
+ routine.updatedAt = Math.max(now, routine.updatedAt + 1);
+ }
+ return destination?.threadId;
+}
export function routineRunDefinition(routine: Routine): Pick<RoutineRun,
"routineName" | "prompt" | "durationMinutes" | "timeoutMinutes" | "attachments" | "target" | "groupId" | "botId" | "runOn" | "access" | "deliver"> {
diff --git a/server/routines.ts b/server/routines.ts
index 6ab4cddfd7..77649c8075 100644
--- a/server/routines.ts
+++ b/server/routines.ts
@@ -31,7 +31,7 @@ import { cloneAttachments } from "./routine-attachments.ts";
import { loadInstalledPackage, loadRoutine, loadRun } from "./routine-load.ts";
import { sanitizeInput } from "./routine-input.ts";
import { checkHold, CHECK_BOOT_HOLD_MS, type CheckHold } from "./routine-check-pacing.ts";
-import { routineRunDefinition } from "./routine-run-definition.ts";
+import { resolveRoutineResultsThread, routineRunDefinition, type ResultsThreadAllocation } from "./routine-run-definition.ts";
export type { RoutineContextAttachment, RoutineIntervalWindow, RoutineRunStatus, RoutineRunTrigger };
export { intervalHasRestrictions } from "./routine-intervals.ts";
@@ -194,8 +194,6 @@ interface RoutineFile {
export type RoutineRequestOwner = Pick<RoutineRequestReceipt, "requestId" | "messageId" | "botId" | "threadId">;
-type ResultsThreadAllocation = { botId: string; threadId: string };
-
function routineRequestOwnerKey(owner: RoutineRequestOwner): string {
return JSON.stringify([owner.requestId, owner.messageId, owner.botId, owner.threadId]);
}
@@ -1230,7 +1228,10 @@ export class RoutineManager {
result.trouble || result.run ? {} : { checkedAt: this.now() });
if (result.run) {
run = queued ?? this.newRun(current, this.now(), false, allocations);
- if (queued) Object.assign(run, routineRunDefinition(current), { timeoutMinutes: current.timeoutMinutes, resultsThreadId: current.resultsThreadId });
+ if (queued) Object.assign(run, routineRunDefinition(current), {
+ timeoutMinutes: current.timeoutMinutes,
+ resultsThreadId: resolveRoutineResultsThread(current, this.options.resolveResultsThread, allocations, this.now(), run.sourceThreadId),
+ });
if (run.timeoutMinutes === undefined) delete run.timeoutMinutes;
Object.assign(run, { prompt: result.run.prompt, checkKey: result.run.key });
} else if (queued && !result.trouble) {
@@ -1427,18 +1428,7 @@ export class RoutineManager {
allocations: ResultsThreadAllocation[],
sourceThreadId = routine.sourceThreadId,
): RoutineRun {
- let resultsThreadId = routine.resultsThreadId;
- if (routine.target === "bot" && this.options.resolveResultsThread) {
- const destination = this.options.resolveResultsThread({ ...routine, sourceThreadId }, false);
- const own = destination?.main ? undefined : destination?.threadId;
- if (own !== routine.resultsThreadId) {
- if (own) allocations.push({ botId: routine.botId, threadId: own });
- if (own) routine.resultsThreadId = own;
- else delete routine.resultsThreadId;
- routine.updatedAt = Math.max(this.now(), routine.updatedAt + 1);
- }
- resultsThreadId = destination?.threadId;
- }
+ const resultsThreadId = resolveRoutineResultsThread(routine, this.options.resolveResultsThread, allocations, this.now(), sourceThreadId);
const run: RoutineRun = {
id: randomUUID(),
routineId: routine.id,