Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(routines): a last tool call that fails after the reply was delivered completes the run partly done, not failed
koeria1304c7d144d9
diff --git a/server/routines.test.ts b/server/routines.test.tsindex bf953002d1..2500a76c8e 100644--- a/server/routines.test.ts+++ b/server/routines.test.ts@@ -301,24 +301,25 @@ describe("bounded scheduled overlap and run health", () => {await h.manager.tick();});- it("keeps a run that failed only on a step, after its reply came back, failed but marked partly done", async () => {+ it("does not fail a run whose reply came back before its last tool call went wrong: it completes partly done", async () => {const h = harness(start);const routine = h.manager.create({ ...input(), enabled: false });const event = (threadId: string, patch: Record<string, unknown>) => h.manager.handleRuntimeEvent({ eventId: `e-${Math.random()}`, provider: "fake", threadId, createdAt: new Date().toISOString(), ...patch } as Parameters<typeof h.manager.handleRuntimeEvent>[0]);- const attempt = async (threadId: string, partial: boolean, { reply = true, cutOff = false } = {}) => {+ const attempt = async (threadId: string, partial: boolean, { reply = true, cutOff = false, stopReason } = {}) => {h.manager.runNow(routine.id); await h.manager.tick();if (reply) event(threadId, { type: "item.completed", itemType: "assistant_text", itemId: "reply", text: "Here is the summary." });event(threadId, { type: "runtime.error", message: "Stopped because the tool call “read_file” failed.", terminal: true, ...(partial ? { partial: true } : {}) });if (cutOff) h.manager.failThread(threadId, "Provider settings changed");- else event(threadId, { type: "turn.completed", ok: false, stopReason: partial ? "tool_error" : "refusal" });+ else event(threadId, { type: "turn.completed", ok: false, stopReason: stopReason ?? (partial ? "tool_error" : "refusal") });};await attempt("thread-1", true);await attempt("thread-2", false);await attempt("thread-3", true, { reply: false });await attempt("thread-4", true, { cutOff: true });- const [partly, failed, silent, cutOff] = h.manager.listRuns().sort((a, b) => a.threadId!.localeCompare(b.threadId!));- expect(partly).toMatchObject({ status: "failed", partial: true, output: "Here is the summary.", error: expect.stringContaining("read_file") });- expect([failed, silent, cutOff].map((run) => [run.status, run.partial])).toEqual([["failed", undefined], ["failed", undefined], ["queued", undefined]]);+ await attempt("thread-5", true, { stopReason: "refusal" });+ const [done, failed, silent, cutOff, limited] = h.manager.listRuns().sort((a, b) => a.threadId!.localeCompare(b.threadId!));+ expect(done).toMatchObject({ status: "completed", partial: true, output: "Here is the summary.", error: expect.stringContaining("read_file") });+ expect([failed, silent, cutOff, limited].map((run) => [run.status, run.partial])).toEqual([["failed", undefined], ["failed", undefined], ["queued", undefined], ["failed", true]]);});it("tells of a failure once per streak, and again when it changes or after a run completes", async () => {diff --git a/server/routines.ts b/server/routines.tsindex 406f060a66..34a7219154 100644--- a/server/routines.ts+++ b/server/routines.ts@@ -1289,7 +1289,18 @@ export class RoutineManager {if (event.cost != null) run.cost = (run.cost ?? 0) + event.cost;if (event.denials?.length) run.denials = [...new Set([...(run.denials ?? []), ...event.denials])];if (!event.ok) {- this.settleFailure(run, routineFailure(run.error, event.stopReason), { ...this.failureFacts.get(run.id), stopReason: event.stopReason });+ const facts = { ...this.failureFacts.get(run.id), stopReason: event.stopReason };+ // A run whose reply was already delivered (partial: the answer streamed first) is not failed by the last tool+ // call that went wrong after it: the output holds the finished deliverable, so the run completes with the+ // failing step kept in its error line. A run-limit stop or a network failure still fails the run (#7186).+ if (event.stopReason === "tool_error" && facts.partial && run.output) {+ Object.assign(run, { status: "completed", attention: undefined, finishedAt: this.now(), partial: true } satisfies Partial<RoutineRun>);+ this.saveLater();+ this.emitRun(run);+ queueMicrotask(() => this.tickInBackground());+ return cloneRun(run);+ }+ this.settleFailure(run, routineFailure(run.error, event.stopReason), facts);return cloneRun(run);}this.failureFacts.delete(run.id);diff --git a/src/lib/automations.test.ts b/src/lib/automations.test.tsindex a5bd40d5c0..cfd80b93bb 100644--- a/src/lib/automations.test.ts+++ b/src/lib/automations.test.ts@@ -254,6 +254,9 @@ describe("order and the grey line", () => {expect(statusLine(automation([partly]), NOW)).toBe("On · last ran today 9:02 AM ! · next: Monday 9:00 AM");expect(runLine(run("failed", at(0, 9, 2), { error: "Stopped because the tool call failed." }), NOW)).toMatchObject({ mark: "✗" });expect(statusLine(automation([run("failed", at(0, 9, 2))]), NOW)).toBe("On · last ran today 9:02 AM ✗ · next: Monday 9:00 AM");+ const completedPartly = run("completed", at(0, 9, 2), { error: "Stopped because the tool call failed.\nmore", output: "Here is the summary.", partial: true });+ expect(runLine(completedPartly, NOW)).toEqual({ when: "Today 9:02 AM", mark: "!", text: "Partly done: Stopped because the tool call failed." });+ expect(statusLine(automation([completedPartly]), NOW)).toBe("On · last ran today 9:02 AM ! · next: Monday 9:00 AM");});it("says a run that found nothing to tell found nothing new, whatever its last words were", () => {diff --git a/src/lib/automations.ts b/src/lib/automations.tsindex daedc06e0f..1eabeb4064 100644--- a/src/lib/automations.ts+++ b/src/lib/automations.ts@@ -334,7 +334,13 @@ export function runLine(run: RoutineRun, now = Date.now()): { when: string; markconst firstLine = (text: string | undefined) => (text ?? "").split("\n").map((line) => line.trim()).find(Boolean) ?? "";const short = (text: string) => (text.length > 110 ? `${text.slice(0, 109).trimEnd()}…` : text);const when = capital(lastWhen(runTime(run), now));- if (run.status === "completed") return { when, mark: "✓", text: run.report?.state === "quiet" ? t("automations.run.nothingNew") : short(firstLine(run.output)) || t("automations.run.done") };+ if (run.status === "completed") {+ if (partlyDone(run)) {+ const reason = short(firstLine(run.error && routineLineText(run.errorCode, run.error, run.errorParams)));+ return { when, mark: "!", text: t("automations.run.partial", { reason }) };+ }+ return { when, mark: "✓", text: run.report?.state === "quiet" ? t("automations.run.nothingNew") : short(firstLine(run.output)) || t("automations.run.done") };+ }if (run.status === "waiting") return waitsOnTeammates(run) ? { when, mark: "…", text: t("automations.run.waitingOnTeam") } : { when, mark: "!", text: t("automations.reason.answer") };if (run.status === "running" || run.status === "queued") return { when, mark: "…", text: t("automations.runningNow") };if (run.status === "cancelled") {diff --git a/src/lib/routine-display.test.ts b/src/lib/routine-display.test.tsindex c9ca313f2b..c1a884eceb 100644--- a/src/lib/routine-display.test.ts+++ b/src/lib/routine-display.test.ts@@ -77,4 +77,10 @@ describe("a run's status", () => {expect(routineRunTone(run({}))).toBe("text-danger");expect(routineRunLabel(run({ status: "completed", partial: true }))).toBe("Completed");});++ it("reads a completed run that kept its failing step partly done, in the waiting colour", () => {+ expect(routineRunLabel(run({ status: "completed", partial: true, error: "Stopped because the tool call “read_file” failed." }))).toBe("Partly done");+ expect(routineRunTone(run({ status: "completed", partial: true, error: "Stopped because the tool call “read_file” failed." }))).toBe("text-warning");+ expect(routineRunLabel(run({ status: "completed", partial: true }))).toBe("Completed");+ });});diff --git a/src/lib/routine-display.ts b/src/lib/routine-display.tsindex 7b93acdd17..b5f7cfcc55 100644--- a/src/lib/routine-display.ts+++ b/src/lib/routine-display.ts@@ -13,9 +13,11 @@ export function latestRoutineRun(routineId: string, runs: readonly RoutineRun[]).reduce<RoutineRun | undefined>((latest, run) => !latest || routineRunTime(run) > routineRunTime(latest) ? run : latest, undefined);}-/** A run that failed only because a step did, after its reply came back. */+/** A run that failed only because a step did, after its reply came back: either recorded as failed with the reply in+ * its output, or completed with the failing step kept in its error line (#7186). */export function partlyDone(run: RoutineRun): boolean {- return run.status === "failed" && run.partial === true && !run.goalStatus;+ return !run.goalStatus && run.partial === true &&+ (run.status === "failed" || (run.status === "completed" && !!run.error));}export function routineRunLabel(run: RoutineRun): string {