Skip to content

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

fix(routines): a run stopped by its own limit keeps its code and the report it delivered

David Neyrae0293d60a1bd
diff --git a/docs/routine-schedules.md b/docs/routine-schedules.md
index 71648eb657..adc5edc7b2 100644
--- a/docs/routine-schedules.md
+++ b/docs/routine-schedules.md
@@ -246,7 +246,8 @@ use an appropriate event/webhook workflow instead of a fake weekly schedule.
lifts.
- A run stops when it reaches its limit, ends as failed with the limit named
("Stopped: this run ran longer than the 3-hour limit"), and the schedule
- carries on. A routine may set its own limit, 5 minutes to 24 hours: that one
+ carries on. A run that had already delivered its report keeps it: its row
+ says the work was partly done. A routine may set its own limit, 5 minutes to 24 hours: that one
counts the time since the run started, less the time its approvals and
questions were open. Without one, the default applies to every run, whether
the schedule, an event or **Run now** started it: 3 hours of work for a
diff --git a/server/routine-bounds.test.ts b/server/routine-bounds.test.ts
index 1f66b8c538..3192242187 100644
--- a/server/routine-bounds.test.ts
+++ b/server/routine-bounds.test.ts
@@ -107,7 +107,7 @@ describe("the run limit", () => {
const clock = new RunClock();
const own = run({ timeoutMinutes: 10 });
expect(runOverLimit(own, clock, 10 * MINUTE - 1)).toBeNull();
- expect(runOverLimit(own, clock, 10 * MINUTE)).toEqual({ text: "Stopped after reaching the 10-minute run limit", codes: { errorCode: undefined, errorParams: undefined } });
+ expect(runOverLimit(own, clock, 10 * MINUTE)).toEqual({ text: "Stopped after reaching the 10-minute run limit", codes: { errorCode: "routines.server.overOwnLimit", errorParams: { minutes: 10 } } });
const long = run({ timeoutMinutes: 1_440 });
expect(runOverLimit(long, clock, 24 * HOUR - 1)).toBeNull();
expect(runOverLimit(long, clock, 24 * HOUR)?.text).toBe("Stopped after reaching the 1440-minute run limit");
diff --git a/server/routine-bounds.ts b/server/routine-bounds.ts
index 5b714ff210..a28d62ecc2 100644
--- a/server/routine-bounds.ts
+++ b/server/routine-bounds.ts
@@ -4,7 +4,7 @@
// scheduler pass at a time, each step capped, so the hours a computer slept, or a clock that jumped, are in neither.
import { ROUTINE_GOAL_RUN_LIMIT_MINUTES, ROUTINE_RUN_LIMIT_MINUTES } from "../shared/routines.ts";
import { DUE_CLOCK_MS } from "./due-timer.ts";
-import { codedLine, fittedLine, type RoutineLine } from "./routine-lines.ts";
+import { codedLine, type RoutineLine } from "./routine-lines.ts";
import { ROUTINE_BUSY_WAIT_MS } from "./routine-recovery.ts";
import { activeMs } from "./routine-waits.ts";
import type { RoutineManagerOptions, RoutineRun } from "./routines.ts";
@@ -78,7 +78,7 @@ export function runLimitAt(run: Started, now: number): number | null {
export function runOverLimit(run: RoutineRun, clock: RunClock, now: number): RoutineLine | null {
if (!started(run)) return null;
const worked = run.status === "running" ? clock.count(run, "workedMs", now) : run.workedMs ?? 0;
- if (run.timeoutMinutes != null) return activeMs(run, now) >= run.timeoutMinutes * 60_000 ? fittedLine("error", `Stopped after reaching the ${run.timeoutMinutes}-minute run limit`, undefined) : null;
+ if (run.timeoutMinutes != null) return activeMs(run, now) >= run.timeoutMinutes * 60_000 ? codedLine("error", "routines.server.overOwnLimit", { minutes: run.timeoutMinutes }) : null;
return run.status === "running" && worked >= defaultLimitMs(run) ? codedLine("error", "routines.server.overLimit", { hours: defaultLimitMs(run) / 3_600_000 }) : null;
}
diff --git a/server/routine-lines.ts b/server/routine-lines.ts
index d2e8054890..a8600bdfa7 100644
--- a/server/routine-lines.ts
+++ b/server/routine-lines.ts
@@ -22,6 +22,7 @@ export const ROUTINE_LINE = {
"routines.server.backupSignedOut": "Starting again on the backup AI: this bot's AI is signed out",
"routines.server.busyTooLong": "Did not run: the bot was busy for more than {minutes} minutes",
"routines.server.overLimit": "Stopped: this run ran longer than the {hours}-hour limit",
+ "routines.server.overOwnLimit": "Stopped after reaching the {minutes}-minute run limit",
"routines.server.stopPending": "Its turn has not confirmed that it stopped. The next run waits for that, up to {minutes} minutes.",
"routines.server.stopUnconfirmed": "Its turn never confirmed that it stopped, so the next run started without that.",
"routines.server.skippedOne": "Skipped because {why}",
diff --git a/server/routines.test.ts b/server/routines.test.ts
index 354894a4d3..bac262059a 100644
--- a/server/routines.test.ts
+++ b/server/routines.test.ts
@@ -2397,6 +2397,31 @@ describe("RoutineManager", () => {
]);
});
+ it("keeps a run that already delivered its report as partly done when its limit stops it", async () => {
+ const h = harness();
+ const routine = h.manager.create({
+ name: "Bounded check",
+ prompt: "Check the queue",
+ botId: "maus-timeout",
+ schedule: { type: "daily", time: "23:59", weekdays: [1] },
+ timeoutMinutes: 5,
+ });
+ const run = h.manager.runNow(routine.id)!;
+ await h.manager.tick();
+ const startedAt = h.manager.listRuns().find((candidate) => candidate.id === run.id)?.startedAt;
+ h.manager.handleRuntimeEvent({ eventId: "reply", provider: "fake", threadId: "thread-1", createdAt: new Date().toISOString(), type: "item.completed", itemType: "assistant_text", itemId: "reply", text: "The invoice is issued and validated." } as Parameters<typeof h.manager.handleRuntimeEvent>[0]);
+
+ h.setNow(startedAt! + 5 * 60_000);
+ await h.manager.tick();
+
+ expect(h.manager.listRuns().find((candidate) => candidate.id === run.id)).toMatchObject({
+ status: "failed",
+ partial: true,
+ output: "The invoice is issued and validated.",
+ error: "Stopped after reaching the 5-minute run limit",
+ });
+ });
+
it("keeps legacy duration metadata without imposing a timeout", async () => {
const h = harness();
const routine = h.manager.create({
diff --git a/server/routines.ts b/server/routines.ts
index a15942ca38..3d50fe7658 100644
--- a/server/routines.ts
+++ b/server/routines.ts
@@ -1373,7 +1373,7 @@ export class RoutineManager {
private async stopRun(run: RoutineRun, detail: string, status: "stopped" | "limit-reached") {
if (run.target === "room-goal") run.goalStatus = status;
- this.failRun(run, detail);
+ this.failRun(Object.assign(run, { partial: Boolean(run.output) || undefined }), detail);
if (!run.threadId) return;
if (run.target === "room-goal") await this.options.interruptGoal?.(run.groupId, run.threadId, { status, detail }).catch(() => {});
else await this.options.interruptTurn?.(run.botId, run.threadId).catch(() => {});
diff --git a/src/locales/en.json b/src/locales/en.json
index be57fbd767..001f66578e 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -10166,6 +10166,7 @@
"routines.server.backupSignedOut": "Starting again on the backup AI: this bot's AI is signed out",
"routines.server.busyTooLong": "Did not run: the bot was busy for more than {minutes} minutes",
"routines.server.overLimit": "Stopped: this run ran longer than the {hours}-hour limit",
+ "routines.server.overOwnLimit": "Stopped after reaching the {minutes}-minute run limit",
"routines.server.stopPending": "Its turn has not confirmed that it stopped. The next run waits for that, up to {minutes} minutes.",
"routines.server.stopUnconfirmed": "Its turn never confirmed that it stopped, so the next run started without that.",
"routines.server.skippedOne": "Skipped because {why}",
diff --git a/src/locales/pt-br.json b/src/locales/pt-br.json
index 92c3b1eb45..be4c87e78f 100644
--- a/src/locales/pt-br.json
+++ b/src/locales/pt-br.json
@@ -10077,6 +10077,7 @@
"routines.server.backupSignedOut": "Começando novamente na IA reserva: a IA deste bot está sem login",
"routines.server.busyTooLong": "Não rodou: o bot ficou ocupado por mais de {minutes} minutos",
"routines.server.overLimit": "Interrompida: esta execução passou do limite de {hours} horas",
+ "routines.server.overOwnLimit": "Interrompida: esta execução passou do limite de {minutes} minutos",
"routines.server.stopPending": "O turno ainda não confirmou que parou. A próxima execução espera por isso, por até {minutes} minutos.",
"routines.server.stopUnconfirmed": "O turno nunca confirmou que parou, então a próxima execução começou sem essa confirmação.",
"routines.server.skippedOne": "Pulada porque {why}",