Skip to content

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

fix(routines): keep a long reply's closing question in the run output

AdityaUmale410f8c4ac760
diff --git a/server/automation-reports.test.ts b/server/automation-reports.test.ts
index 46d1d1a700..8b8d8dd18c 100644
--- a/server/automation-reports.test.ts
+++ b/server/automation-reports.test.ts
@@ -3,6 +3,7 @@ import { beforeEach, describe, expect, it } from "vitest";
import { ROUTINE_HANDOFF_WAIT, type RoutineRunReport } from "../shared/routines.ts";
import "./server-line-packs.ts";
+import { clipRunOutput } from "./routine-lines.ts";
import { automationContextText, createAutomationReports, newestAutomationLines, readRunReply, recoveredText, routineRunNeedsReview } from "./automation-reports.ts";
import { DATA_DIR } from "./config.ts";
import type { RoutineRun } from "./routines.ts";
@@ -42,6 +43,7 @@ describe("routineRunNeedsReview", () => {
expect(routineRunNeedsReview({ status: "waiting", attention: "Approve: Bash" })).toBe(true);
expect(routineRunNeedsReview({ status: "waiting", attention: ROUTINE_HANDOFF_WAIT })).toBe(false);
expect(routineRunNeedsReview({ status: "running" })).toBe(false);
+ expect(routineRunNeedsReview({ status: "completed", output: clipRunOutput(`${"Long report. ".repeat(300)}\nNEEDS INPUT: Send it?`, 2_000) })).toBe(true);
expect(routineRunNeedsReview({ status: "cancelled" })).toBe(false);
});
});
diff --git a/server/routine-lines.test.ts b/server/routine-lines.test.ts
index 1a417835c1..ba94c5151f 100644
--- a/server/routine-lines.test.ts
+++ b/server/routine-lines.test.ts
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import englishCatalog from "../src/locales/en.json" with { type: "json" };
-import { codedLine, fittedLine, ROUTINE_LINE, routineLine } from "./routine-lines.ts";
+import { clipRunOutput, codedLine, fittedLine, ROUTINE_LINE, routineLine } from "./routine-lines.ts";
it("the catalog's routine lines are exactly the ones the server stores", () => {
const catalog = Object.fromEntries(Object.entries(englishCatalog).filter(([key]) => key.startsWith("routines.server.")));
@@ -48,3 +48,30 @@ describe("a long line", () => {
});
});
});
+
+describe("clipRunOutput", () => {
+ const long = "Checked everything. ".repeat(200);
+
+ it("leaves a short reply alone and cuts a long one with no closing question", () => {
+ expect(clipRunOutput("Short.\nNEEDS INPUT: Go?", 2_000)).toBe("Short.\nNEEDS INPUT: Go?");
+ expect(clipRunOutput(long, 2_000)).toBe(long.slice(0, 2_000));
+ });
+
+ it("keeps a closing question, and its default line, whole at the end", () => {
+ const clipped = clipRunOutput(`${long}\nNEEDS INPUT: Send it?`, 2_000);
+ expect(clipped.length).toBeLessThanOrEqual(2_000);
+ expect(clipped.endsWith("\n…\nNEEDS INPUT: Send it?")).toBe(true);
+ expect(clipRunOutput(`${long}\nNEEDS INPUT: Send it?\n\ndefault: wait`, 2_000).endsWith("NEEDS INPUT: Send it?\n\ndefault: wait")).toBe(true);
+ });
+
+ it("does not move a question from the middle to the end", () => {
+ const text = `NEEDS INPUT: old?\n${long}`;
+ expect(clipRunOutput(text, 2_000)).toBe(text.slice(0, 2_000));
+ expect(clipRunOutput(`${long}\nNEEDS INPUT: Go?\nMore text after it.`, 2_000)).toBe(`${long}\nNEEDS INPUT: Go?\nMore text after it.`.slice(0, 2_000));
+ });
+
+ it("cuts plainly when the question alone is too long to keep", () => {
+ const text = `${long}\nNEEDS INPUT: ${"why ".repeat(400)}?`;
+ expect(clipRunOutput(text, 2_000)).toBe(text.slice(0, 2_000));
+ });
+});
diff --git a/server/routine-lines.ts b/server/routine-lines.ts
index a08e0a3dbe..557298c1c3 100644
--- a/server/routine-lines.ts
+++ b/server/routine-lines.ts
@@ -87,3 +87,18 @@ export function applyCheckResult(routine: RoutineTroubleCode & { checkTrouble?:
result.trouble ? { checkTrouble: result.trouble.slice(0, 300), ...(result.troubleCode ? { checkTroubleCode: result.troubleCode } : {}) } : {},
result.trouble || result.run ? {} : { checkedAt: now });
}
+
+const CLOSING_QUESTION = /^\s*needs input:/i;
+
+/** A run's reply cut to max characters for its stored output. A reply that ends on a NEEDS INPUT question (with at most
+ * a default: line after it) keeps that question whole at the end, so a long reply still reads as waiting on the person. */
+export function clipRunOutput(text: string, max: number): string {
+ if (text.length <= max) return text;
+ const lines = text.split(/\r?\n/);
+ const at = lines.findLastIndex((line) => CLOSING_QUESTION.test(line));
+ const after = at < 0 ? [] : lines.slice(at + 1).filter((line) => line.trim());
+ const closing = at >= 0 && (after.length === 0 || (after.length === 1 && /^default:/i.test(after[0]!.trim())));
+ const tail = closing ? lines.slice(at).join("\n").trim() : "";
+ if (!tail || tail.length > max / 2) return text.slice(0, max);
+ return `${text.slice(0, max - tail.length - 3).trimEnd()}\n…\n${tail}`;
+}
diff --git a/server/routines.test.ts b/server/routines.test.ts
index 4efda7bc6c..93506b01ad 100644
--- a/server/routines.test.ts
+++ b/server/routines.test.ts
@@ -2950,6 +2950,22 @@ describe("RoutineManager", () => {
});
});
+ it("keeps a long reply's closing question in the stored output", async () => {
+ const h = harness();
+ const routine = h.manager.create({ name: "Inbox", prompt: "Check the inbox", botId: "maus-3", schedule: { type: "once", at: new Date(2026, 7, 17, 8, 1).getTime() } });
+ h.setNow(routine.nextRunAt!);
+ await h.manager.tick();
+ const base = { eventId: "event-long", provider: "fake", threadId: "thread-1", createdAt: new Date(h.manager.listRuns()[0]!.startedAt!).toISOString() };
+ h.manager.handleRuntimeEvent({ ...base, type: "item.completed", itemType: "assistant_text", text: `${"Found a lot. ".repeat(300)}\nNEEDS INPUT: Send the draft?` });
+ h.manager.handleRuntimeEvent({ ...base, type: "turn.completed", ok: true });
+
+ const run = h.manager.listRuns()[0]!;
+ expect(run.status).toBe("completed");
+ expect(run.output!.length).toBeLessThanOrEqual(2_000);
+ expect(run.output).toMatch(/^Found a lot\./);
+ expect(run.output).toMatch(/\nNEEDS INPUT: Send the draft\?$/);
+ });
+
it("ignores intermediate provider completions until the room goal reports its outcome", async () => {
const h = harness();
const routine = h.manager.create({
diff --git a/server/routines.ts b/server/routines.ts
index 4805328778..4593fb20cd 100644
--- a/server/routines.ts
+++ b/server/routines.ts
@@ -34,7 +34,7 @@ import { loadInstalledPackage, loadRequestReceipts, loadRoutine, loadRun } from
import { applyHolds, holdCauses, type AppliedHolds } from "./routine-holds.ts";
import { cancelUnfinishedRuns, interruptRunTurns } from "./routine-stops.ts";
import { sanitizeInput } from "./routine-input.ts";
-import { applyCheckResult, routineLine, runLineFields, type RoutineLineCodes, type RoutineTroubleCode } from "./routine-lines.ts";
+import { applyCheckResult, clipRunOutput, routineLine, runLineFields, type RoutineLineCodes, type RoutineTroubleCode } from "./routine-lines.ts";
import { checkHold, CHECK_BOOT_HOLD_MS, type CheckHold } from "./routine-check-pacing.ts";
import { resolveRoutineResultsThread, routineRunDefinition, type ResultsThreadAllocation } from "./routine-run-definition.ts";
@@ -1253,7 +1253,7 @@ export class RoutineManager {
run.status = "running";
run.attention = undefined;
} else if (event.type === "item.completed" && event.itemType === "assistant_text") {
- run.output = redactSecretsInText(event.text).trim().slice(0, 2_000);
+ run.output = clipRunOutput(redactSecretsInText(event.text).trim(), 2_000);
} else if (event.type === "runtime.error") {
Object.assign(run, runLineFields("error", event.message, event.text));
this.failureFacts.set(run.id, engineFacts(event));