Skip to content

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

fix(automations): only bot runs count as hand-off waits; an older question still shows behind one

AdityaUmale34d6ad68afac
diff --git a/src/components/automations/parts.tsx b/src/components/automations/parts.tsx
index 9434f42a09..cfecd05da3 100644
--- a/src/components/automations/parts.tsx
+++ b/src/components/automations/parts.tsx
@@ -1,6 +1,6 @@
import { Check, TriangleAlert } from "lucide-react";
-import { AUTOMATION_FIX_LABEL, type Automation, type AutomationStatus } from "@/lib/automations";
+import { AUTOMATION_FIX_LABEL, runAwaitingAnswer, type Automation, type AutomationStatus } from "@/lib/automations";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import { useStore } from "@/state/store";
@@ -24,7 +24,7 @@ export function FixButton({ automation, status, actions, onEdit, className }: {
const { dispatch } = useStore();
if (status.state !== "needs-you" || !status.fix) return null;
const fix = status.fix;
- const waiting = fix === "answer" ? automation.runs.find((run) => run.status === "waiting") : undefined;
+ const waiting = fix === "answer" ? runAwaitingAnswer(automation.runs) : undefined;
const onClick = () => {
if (fix === "answer") actions.openRun(automation, waiting);
else if (fix === "sign-in") actions.openRun(automation, automation.runs.find((run) => run.status !== "queued"));
diff --git a/src/lib/automations.test.ts b/src/lib/automations.test.ts
index 9250249c0b..db57dda73a 100644
--- a/src/lib/automations.test.ts
+++ b/src/lib/automations.test.ts
@@ -4,7 +4,7 @@ import { setLocale } from "@/lib/i18n";
import type { Routine, RoutineRun } from "@/lib/routines";
import type { WebhookTrigger } from "@/lib/webhooks";
import {
- automationSentence, automationsFrom, automationStatus, doesPhrase, lastReceived, lastWhen, nextWhen, orderAutomations, plainWhen, runLine, sourcePhrase, statusLine,
+ automationSentence, automationsFrom, automationStatus, doesPhrase, lastReceived, lastWhen, nextWhen, orderAutomations, plainWhen, runAwaitingAnswer, runLine, sourcePhrase, statusLine,
webhookCalls, type Automation,
} from "./automations";
import type { WebhookAttempt } from "@/lib/webhooks";
@@ -136,6 +136,18 @@ describe("status: Working, Paused, Needs you", () => {
expect(runLine(run("waiting", at(0, 9), { attention: "Approve the reply?" }), NOW)).toMatchObject({ mark: "!", text: "It's waiting for your answer." });
});
+ it("still asks the person when an older run waits for an answer behind a newer run that waits on teammates", () => {
+ const asking = run("waiting", at(-1, 9), { attention: "Approve the reply?" });
+ const runs = [run("waiting", at(0, 9), { attention: "Waiting for work it handed on to finish" }), asking];
+ expect(automationStatus(automation(runs))).toMatchObject({ state: "needs-you", fix: "answer" });
+ expect(runAwaitingAnswer(automation(runs).runs)?.id).toBe(asking.id);
+ expect(automationStatus(automation([run("waiting", at(0, 9), { attention: "Waiting for work it handed on to finish" }), run("completed", at(-1, 9))])).state).toBe("working");
+ });
+
+ it("treats a room goal's own wait as a question even when its words match the hand-off line", () => {
+ expect(automationStatus(automation([run("waiting", at(0, 9), { target: "room-goal", attention: "Waiting for work it handed on to finish" })])).state).toBe("needs-you");
+ });
+
it("ignores runs still waiting to start and runs a person stopped when judging the last run", () => {
expect(automationStatus(automation([run("queued", at(0, 11)), run("cancelled", at(0, 10)), run("completed", at(0, 9))])).state).toBe("working");
expect(automationStatus(automation([run("queued", at(0, 11)), run("cancelled", at(0, 10)), run("failed", at(0, 9)), run("failed", at(-1, 9))])).state).toBe("needs-you");
diff --git a/src/lib/automations.ts b/src/lib/automations.ts
index 6bacbaa090..66003bff6f 100644
--- a/src/lib/automations.ts
+++ b/src/lib/automations.ts
@@ -249,8 +249,14 @@ function ownerFix(run: RoutineRun): { reason: string; fix: AutomationFix } | nul
return null;
}
-/** A waiting run that waits only on work it handed to teammates: nothing is asked of the person. */
-const waitsOnTeammates = (run: Pick<RoutineRun, "attention">) => run.attention === ROUTINE_HANDOFF_WAIT;
+/** A bot's waiting run that waits only on work it handed to teammates: nothing is asked of the person. */
+const waitsOnTeammates = (run: Pick<RoutineRun, "target" | "attention">) => run.target === "bot" && run.attention === ROUTINE_HANDOFF_WAIT;
+
+/** The run whose question the person should answer: the newest run, looking past runs that only wait on teammates. */
+export function runAwaitingAnswer(runs: readonly RoutineRun[]): RoutineRun | undefined {
+ const run = runs.find((candidate) => candidate.status !== "cancelled" && candidate.status !== "queued" && !(candidate.status === "waiting" && waitsOnTeammates(candidate)));
+ return run?.status === "waiting" ? run : undefined;
+}
/** Done when a one-time automation's time has passed, Paused when switched off. Needs you when its last run waits on the
* person, its app needs reconnecting, failed for a reason only the person can fix, or failed twice running. A run that
@@ -262,7 +268,7 @@ export function automationStatus(automation: Pick<Automation, "enabled" | "runs"
if (automation.checkTrouble) return { state: "needs-you", reason: routineLineText(automation.checkTroubleCode, automation.checkTrouble), fix: SIGNED_OUT.test(automation.checkTrouble) ? "reconnect" : "run" };
const settled = automation.runs.filter((run) => run.status !== "cancelled" && run.status !== "queued");
const latest = settled[0];
- if (latest?.status === "waiting" && !waitsOnTeammates(latest)) return { state: "needs-you", reason: t("automations.reason.answer"), fix: "answer" };
+ if (runAwaitingAnswer(settled)) return { state: "needs-you", reason: t("automations.reason.answer"), fix: "answer" };
if (latest && isProblem(latest)) {
const owner = ownerFix(latest);
if (owner) return { state: "needs-you", ...owner };