Skip to content

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

fix(routines): mark a chat To review only when an automation run needs the person

AdityaUmaled7aee75be790
diff --git a/server/automation-reports.e2e.test.ts b/server/automation-reports.e2e.test.ts
index 8f94c29dc0..791dceda12 100644
--- a/server/automation-reports.e2e.test.ts
+++ b/server/automation-reports.e2e.test.ts
@@ -127,7 +127,7 @@ it("reports each run into the main chat as the bot's own line, writes nothing fo
expect(each.resultsThreadId).toBe(s.main);
expect(saved.tasks.find((task: any) => task.threadId === each.threadId)).toMatchObject({ routineRunId: each.id, unread: false });
}
- expect(saved.tasks.find((task: any) => task.threadId === s.main).unread).toBe(true);
+ expect(saved.tasks.find((task: any) => task.threadId === s.main).unread, "a report that needs nothing from the person leaves the chat out of To review").toBe(false);
const done = s.notified().filter((notification) => notification.kind === "done");
expect(done).toEqual([expect.objectContaining({ threadId: s.main, body: "Inbox watch: Acme replied: they need the invoice by Friday." })]);
for (const key of ["first", "second", "third"]) expect(s.toldOnlyReplyLeaves(key)).toBe(true);
diff --git a/server/automation-reports.test.ts b/server/automation-reports.test.ts
index 1636db3e3d..46d1d1a700 100644
--- a/server/automation-reports.test.ts
+++ b/server/automation-reports.test.ts
@@ -1,9 +1,9 @@
import { rmSync } from "node:fs";
import { beforeEach, describe, expect, it } from "vitest";
-import type { RoutineRunReport } from "../shared/routines.ts";
+import { ROUTINE_HANDOFF_WAIT, type RoutineRunReport } from "../shared/routines.ts";
import "./server-line-packs.ts";
-import { automationContextText, createAutomationReports, newestAutomationLines, readRunReply, recoveredText } from "./automation-reports.ts";
+import { automationContextText, createAutomationReports, newestAutomationLines, readRunReply, recoveredText, routineRunNeedsReview } from "./automation-reports.ts";
import { DATA_DIR } from "./config.ts";
import type { RoutineRun } from "./routines.ts";
import { Store, type BotRecord, type Message } from "./store.ts";
@@ -32,6 +32,20 @@ describe("readRunReply", () => {
});
});
+describe("routineRunNeedsReview", () => {
+ it("is true only when the person is needed: a failed or missed run, a wait on them, or a closing question", () => {
+ expect(routineRunNeedsReview({ status: "completed", output: "Acme replied about the order." })).toBe(false);
+ expect(routineRunNeedsReview({ status: "completed", output: "I drafted a reply.\nNEEDS INPUT: Send it?" })).toBe(true);
+ expect(routineRunNeedsReview({ status: "completed", output: "Short." }, "A longer reply.\nNEEDS INPUT: Send it?")).toBe(true);
+ expect(routineRunNeedsReview({ status: "failed" })).toBe(true);
+ expect(routineRunNeedsReview({ status: "missed" })).toBe(true);
+ 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: "cancelled" })).toBe(false);
+ });
+});
+
describe("what the chat's own session reads", () => {
it("names and dates each line as the automation's own", () => {
const line = { routineId: "r1", runId: "run", name: "Inbox brief", trigger: "schedule" as const, executionThreadId: "x", scheduledFor: 0 };
@@ -113,7 +127,7 @@ describe("automation reports", () => {
const run = f.finished("Acme replied about the order.");
f.store.patchTask(f.bot.id, f.main, { unread: false });
f.reports.runSettled(run, `turn-${run.id}`);
- expect(f.store.taskByThread(f.bot.id, f.main)?.unread, "the chat it reports to shows unread").toBe(true);
+ expect(f.store.taskByThread(f.bot.id, f.main)?.unread, "a report that needs nothing from the person leaves the chat out of To review").toBe(false);
expect(f.lines()).toEqual([expect.objectContaining({
role: "bot", kind: "text", text: "Acme replied about the order.",
automation: { routineId: "routine-Inbox brief", runId: run.id, name: "Inbox brief", trigger: "schedule", executionThreadId: run.threadId, scheduledFor: run.scheduledFor },
@@ -139,7 +153,9 @@ describe("automation reports", () => {
it("asks the person in the chat, as a normal line they can answer, when the run needs them", () => {
const f = fixture();
const run = f.finished("I drafted a reply to Acme.\nNEEDS INPUT: Should I send it?");
+ f.store.patchTask(f.bot.id, f.main, { unread: false });
f.reports.runSettled(run, `turn-${run.id}`);
+ expect(f.store.taskByThread(f.bot.id, f.main)?.unread, "a question puts the chat under To review").toBe(true);
expect(f.lines()).toEqual([expect.objectContaining({ kind: "text", text: "I drafted a reply to Acme.\nShould I send it?", automation: expect.objectContaining({ needsYou: true }) })]);
expect(f.notified).toEqual([{ kind: "question", threadId: f.main, detail: "Inbox brief: Should I send it?" }]);
});
@@ -210,7 +226,9 @@ describe("automation reports", () => {
it("says once in the chat that a run failed, and stays out of chats a run does not report to", () => {
const f = fixture();
const failed = f.finished(null, "Inbox brief", { status: "failed", error: "Signed out" });
+ f.store.patchTask(f.bot.id, f.main, { unread: false });
f.reports.runFailed(failed, "This bot's engine is signed out. Sign in again in Settings → Model providers.");
+ expect(f.store.taskByThread(f.bot.id, f.main)?.unread, "a failed run puts the chat under To review").toBe(true);
expect(f.lines()).toEqual([expect.objectContaining({
text: "This run didn't finish: This bot's engine is signed out. Sign in again in Settings → Model providers.",
automation: expect.objectContaining({ failed: true }),
@@ -247,7 +265,9 @@ describe("automation reports", () => {
f.reports.runFailed(failed, "it ran past its 25-minute limit.");
const finishedAt = Date.UTC(2026, 9, 26, 2, 40);
const ok = f.finished("Checked 14 emails.\nNOTHING NEW", "Inbox brief", { finishedAt });
+ f.store.patchTask(f.bot.id, f.main, { unread: false });
f.reports.runSettled(ok, `turn-${ok.id}`);
+ expect(f.store.taskByThread(f.bot.id, f.main)?.unread, "a recovered line needs nothing from the person").toBe(false);
for (let i = 0; i < 3; i++) {
const later = f.finished("NOTHING NEW");
f.reports.runSettled(later, `turn-${later.id}`);
diff --git a/server/automation-reports.ts b/server/automation-reports.ts
index 38981f76ee..890c8fceba 100644
--- a/server/automation-reports.ts
+++ b/server/automation-reports.ts
@@ -6,6 +6,7 @@
// that delivers to the brief posts no line: its answer goes to the brief, which reads it as a small JSON result.
import { liveCardKind } from "../shared/live-approval.ts";
import type { AutomationLine } from "../shared/routine-run.ts";
+import { ROUTINE_HANDOFF_WAIT } from "../shared/routines.ts";
import { signInWaits } from "../shared/sign-in-request.ts";
import { intlLocale } from "./language.ts";
import { needsInputQuestion } from "./runtime-coordinator.ts";
@@ -30,6 +31,13 @@ export function readRunReply(text: string, attached = false): RunReply {
return { quiet: false, text: [...lines.slice(0, at), lines[at]!.replace(NEEDS_INPUT, ""), ...lines.slice(at + 1)].join("\n").trim(), question };
}
+/** A run puts its chat under To review only when the person is needed: it failed or was missed, waits on them (not on teammates), or ended on a question. */
+export function routineRunNeedsReview(run: Pick<RoutineRun, "status" | "attention" | "output">, reply?: string): boolean {
+ if (run.status === "failed" || run.status === "missed") return true;
+ if (run.status === "waiting") return run.attention !== ROUTINE_HANDOFF_WAIT;
+ return run.status === "completed" && needsInputQuestion(reply ?? run.output ?? "") !== undefined;
+}
+
function inHostZone(at: number, locale: string, options: Intl.DateTimeFormatOptions): string {
const format = (timeZone: string) => new Intl.DateTimeFormat(locale, { ...options, timeZone }).format(at);
try { return format(hostTimeZone()); } catch { return format("UTC"); }
@@ -204,7 +212,7 @@ export function createAutomationReports(deps: AutomationReportDeps) {
});
deps.routines()?.noteReport(run.id, { state: "posted", threadId: target.threadId, messageId: posted.id });
deps.wrote(target.bot, target.threadId);
- store.patchTask(target.bot.id, target.threadId, { unread: true });
+ if (line.kind === "failure" || reply.question !== undefined || routineRunNeedsReview(run)) store.patchTask(target.bot.id, target.threadId, { unread: true });
if (line.kind === "reply" && !recovered) deps.notify(reply.question ? "question" : "done", target.bot, target.threadId, `${run.routineName}: ${reply.question ?? reply.text}`);
return true;
};
diff --git a/server/index.ts b/server/index.ts
index 6f5d35afc4..3c84dc532d 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -458,7 +458,7 @@ import { createScreenFrameSource, rememberTurnFrame, type ScreenCapture } from "
import { screenFrameHash, screenSurfaceForTool, screenTouchingTool, settledFrameIsNews } from "./screen-frame-gate.ts";
import { asSchedule, RoutineRequestService } from "./routine-requests.ts"; import { ownerSpokeOnly } from "./cloud-routine-level.ts"; import { routineRunCard, routineRunFallbackText } from "./routine-run-card.ts";
import { activeRoutinesContext } from "./active-routines-context.ts";
-import { createRefusedHandoffs } from "./host/refused-handoffs.ts"; import { automationContextText, createAutomationReports, newestAutomationLines } from "./automation-reports.ts"; import { createAutomationDestinations } from "./automation-destinations.ts"; import { createTeamRoutines } from "./host/team-routines.ts"; import { botOwner } from "../shared/bot-owner.ts"; import { forgetMember } from "./bot-owners.ts"; import { createProactive } from "./host/proactive.ts"; import { createFirstHourHooks, startFirstHour, welcomeDonePatch } from "./host/first-hour-host.ts"; import { greetingMessage } from "./host/first-hour.ts"; import { READ_AND_DRAFT_PARTS, READ_AND_DRAFT_REFUSAL, READ_AND_DRAFT_TOOLS, recordsAnswer } from "./read-and-draft.ts"; import { createPresenceRoutes } from "./routes/presence.ts"; import { needsYouNow } from "./today.ts"; import { createTeamPresenceHost } from "./host/team-presence-host.ts";
+import { createRefusedHandoffs } from "./host/refused-handoffs.ts"; import { automationContextText, createAutomationReports, newestAutomationLines, routineRunNeedsReview } from "./automation-reports.ts"; import { createAutomationDestinations } from "./automation-destinations.ts"; import { createTeamRoutines } from "./host/team-routines.ts"; import { botOwner } from "../shared/bot-owner.ts"; import { forgetMember } from "./bot-owners.ts"; import { createProactive } from "./host/proactive.ts"; import { createFirstHourHooks, startFirstHour, welcomeDonePatch } from "./host/first-hour-host.ts"; import { greetingMessage } from "./host/first-hour.ts"; import { READ_AND_DRAFT_PARTS, READ_AND_DRAFT_REFUSAL, READ_AND_DRAFT_TOOLS, recordsAnswer } from "./read-and-draft.ts"; import { createPresenceRoutes } from "./routes/presence.ts"; import { needsYouNow } from "./today.ts"; import { createTeamPresenceHost } from "./host/team-presence-host.ts";
import { engineLabel, LimitResetClock, nearLimitNote } from "./limit-resets.ts"; import { loadPlanUsage, planAccountsFromInstances } from "./plan-usage.ts"; import { LimitFailovers, limitBackupAllowed } from "./limit-failover.ts"; import { createLimitResetRoutes } from "./routes/limit-resets.ts";
import { parseTriggerRequest, triggerTarget } from "./trigger-tools.ts"; import { WebhookRequestError, WebhookRequestService } from "./webhook-requests.ts"; import { createAppTriggerHost } from "./app-trigger-host.ts";
import { appliesNow, directApply, UNDO_STALE, type DirectApply } from "./direct-apply.ts";
@@ -6809,7 +6809,7 @@ bus.subscribe((event: RuntimeEvent) => {
settlingTurns.add(event.threadId);
liveTurnByThread.delete(event.threadId);
const routineReportThread = routineRun ? routineSourceThread(routineRun) : null;
- if (!routineReportThread && (!internal || reviewAnswer)) store.patchTask(bot.id, event.threadId, { unread: true });
+ if (!routineReportThread && (!internal || reviewAnswer) && (!routineRun || routineRunNeedsReview(routineRun, reply) || personWaits.open(event.threadId))) store.patchTask(bot.id, event.threadId, { unread: true });
if (routineRun && routineReportThread !== event.threadId) automationReports.runSettled(routineRun, completedTurnId);
else if ((!routineRun || routineRun.status === "completed") && (!internal || reviewAnswer) && !computerSelectionTurns.get(event.threadId)?.selected && doneNotice.atTurnEnd({ botId: bot.id, threadId: event.threadId, review: reviewAnswer, ok: event.ok, routine: Boolean(routineRun) }) === "notify" && !personWaits.atTurnEnd(event.threadId, reply) && !unaskedBotThread(settledTask, store.activePath(event.threadId))) {
notify(buildNotification("done", bot, event.threadId, routineRun ? reply || routineRun.output || routineRun.routineName : reply, { avatarUrl: bot.avatarUrl }), routineRun ? { routine: true } : reviewAnswer ? { summary: true } : undefined);
@@ -8665,7 +8665,7 @@ function syncRoutineRunToSource(run: RoutineRun): string | null {
if (execution?.routineRunId === run.id) {
store.patchTask(run.botId, execution.threadId, {
routineRunId: undefined,
- ...(["waiting", "completed", "failed", "missed"].includes(run.status) ? { unread: true } : {}),
+ ...(routineRunNeedsReview(run) ? { unread: true } : {}),
});
}
return null;
@@ -8696,7 +8696,7 @@ function syncRoutineRunToSource(run: RoutineRun): string | null {
}
}
- if (statusChanged && ["waiting", "completed", "failed", "missed"].includes(run.status)) {
+ if (statusChanged && (source.group ? ["waiting", "completed", "failed", "missed"].includes(run.status) : routineRunNeedsReview(run))) {
if (source.group) store.markGroupThreadUnread(source.group.id, sourceThreadId, true);
else store.patchTask(source.bot.id, sourceThreadId, { unread: true });
}
diff --git a/server/routine-results.e2e.test.ts b/server/routine-results.e2e.test.ts
index 1e7408c591..c3fbdb847b 100644
--- a/server/routine-results.e2e.test.ts
+++ b/server/routine-results.e2e.test.ts
@@ -65,7 +65,7 @@ it("reports every run into the bot's main thread while fresh executions, approva
expect(savedBot.threadId).toBe(originalThread);
expect(savedBot.tasks.filter((task: any) => !task.routineRunId)).toHaveLength(1);
expect(savedBot.tasks.some((task: any) => task.title.endsWith("· Results"))).toBe(false);
- expect(savedBot.tasks.find((task: any) => task.threadId === destination).unread).toBe(true);
+ expect(savedBot.tasks.find((task: any) => task.threadId === destination).unread, "plain reports need nothing from the person").toBe(false);
for (const run of completed) expect(savedBot.tasks.find((task: any) => task.threadId === run.threadId))
.toMatchObject({ routineRunId: run.id, unread: false, autoApprove: false, approvalMode: "ask" });