Skip to content

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

fix(routines): keep a person's own turn when pausing a routine; settle only connect cards from the last week

AdityaUmale847da67f4f9e
diff --git a/server/host/connect-card-settle.test.ts b/server/host/connect-card-settle.test.ts
index 497be2ce07..ca81983eef 100644
--- a/server/host/connect-card-settle.test.ts
+++ b/server/host/connect-card-settle.test.ts
@@ -36,6 +36,14 @@ describe("reconnecting an app settles older open connect cards for it", () => {
expect(h.resumed).toEqual([["old-run", "key-a"]]);
});
+ it("leaves a card older than a week even when the same conversation has a newer one", () => {
+ const h = harness({ t: [{ ...card("old", {}), at: NOW - 8 * 24 * 60 * 60_000 }, card("new", {})] });
+ expect(settleOpenConnectCards(h.deps, "instagram")).toBe(1);
+ expect(h.status("t", "old")).toBe("required");
+ expect(h.status("t", "new")).toBe("connected");
+ expect(h.resumed).toEqual([["t", "key-new"]]);
+ });
+
it("only settles cards for that same account", () => {
const h = harness({ t: [card("work", { alias: "Work" }), card("plain", {}), card("home", { alias: "home" })] });
expect(settleOpenConnectCards(h.deps, "instagram", " work ")).toBe(1);
diff --git a/server/host/connect-card-settle.ts b/server/host/connect-card-settle.ts
index 09e59f8f37..3da94850b4 100644
--- a/server/host/connect-card-settle.ts
+++ b/server/host/connect-card-settle.ts
@@ -18,11 +18,12 @@ export function settleOpenConnectCards(deps: ConnectCardSettleDeps, slug: string
const app = slug.toLowerCase();
const account = accountKey(alias);
let settled = 0;
- for (const threadId of deps.threadsWithCards(deps.now() - NEEDS_YOU_WINDOW_MS)) {
+ const since = deps.now() - NEEDS_YOU_WINDOW_MS;
+ for (const threadId of deps.threadsWithCards(since)) {
const resumeKeys = new Set<string>();
for (const message of deps.messagesFor(threadId)) {
const card = message.kind === "connector" ? message.connector : undefined;
- if (!card || card.source === "mcp" || card.slug.toLowerCase() !== app || accountKey(card.alias) !== account) continue;
+ if (!card || message.at < since || card.source === "mcp" || card.slug.toLowerCase() !== app || accountKey(card.alias) !== account) continue;
if (card.status === "connected" || card.dismissed || card.superseded || card.stopped || card.resumed) continue;
deps.patchMessage(threadId, message.id, { connector: { ...card, status: "connected", error: undefined } });
resumeKeys.add(card.resumeKey);
diff --git a/server/index.ts b/server/index.ts
index 39e020639f..f8a9a11ba8 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -8776,7 +8776,7 @@ routines = new RoutineManager({
? process.env.OMB_DESKTOP_LAUNCH_ID : undefined,
skipStartupRuns: (startedFromSleep = Boolean(CLOUD_HOME) && wokeFromSleep(DATA_DIR)), // a Cloud home waking from a sleep it was stopped for
hasPendingDelegations: runtimeWorkPending, refusedHandoff: refusedHandoffs.take, handedOffWorkLive: (threadId) => coordinator.outstandingFor(threadId).length > 0,
- routineTurn: (threadId) => directRequestOwners.get(threadId)?.automation !== undefined,
+ routineTurn: (threadId) => directRequestOwners.get(threadId)?.automation !== undefined, personTurn: (threadId) => directRequestOwners.get(threadId)?.origin?.kind === "user",
botState: unattendedDispatchState, pausedText: (botId) => starterCaps.pausedText(botId, threadLanguage(store.bot(botId)?.threadId ?? "")),
goalState: (groupId, coordinatorBotId) => {
if (!groupId) return unattendedDispatchState(coordinatorBotId);
diff --git a/server/routine-stops.ts b/server/routine-stops.ts
index 676775b9e1..e249106215 100644
--- a/server/routine-stops.ts
+++ b/server/routine-stops.ts
@@ -20,10 +20,10 @@ export function cancelUnfinishedRuns(runs: readonly RoutineRun[], routineId: str
return { cancelled, waited };
}
-/** Ends the turns of runs that stopped while their bot or room worked or waited, so their open cards stop and nothing shows them waiting. */
-export function interruptRunTurns(runs: readonly RoutineRun[], options: Pick<RoutineManagerOptions, "interruptTurn" | "interruptGoal">) {
+/** Ends the turns of runs that stopped while their bot or room worked or waited, so their open cards stop and nothing shows them waiting. A turn the person started in that conversation is theirs and keeps going. */
+export function interruptRunTurns(runs: readonly RoutineRun[], options: Pick<RoutineManagerOptions, "interruptTurn" | "interruptGoal" | "personTurn">) {
for (const run of runs) {
if (run.threadId && run.target === "room-goal") void options.interruptGoal?.(run.groupId, run.threadId).catch(() => {});
- else if (run.threadId) void options.interruptTurn?.(run.botId, run.threadId).catch(() => {});
+ else if (run.threadId && !options.personTurn?.(run.threadId)) void options.interruptTurn?.(run.botId, run.threadId).catch(() => {});
}
}
diff --git a/server/routines.test.ts b/server/routines.test.ts
index 2b822abe2a..eb67abbb79 100644
--- a/server/routines.test.ts
+++ b/server/routines.test.ts
@@ -450,6 +450,18 @@ describe("a run left waiting for the person", () => {
await vi.waitFor(() => expect(h.interruptedTurns).toEqual([{ botId: "maus-1", threadId: "thread-1" }]));
});
+ it("leaves a turn the person started in the waiting run's conversation running when its routine is paused", async () => {
+ const h = harness(monday);
+ const routine = h.manager.create(daily);
+ h.setNow(routine.nextRunAt!); await h.manager.tick();
+ h.setNow(routine.nextRunAt! + minutes(2)); parkOnPerson(h, "thread-1");
+ h.options.personTurn = (threadId) => threadId === "thread-1";
+ h.manager.update(routine.id, { enabled: false });
+ expect(h.manager.listRuns()[0]).toMatchObject({ status: "cancelled", error: "The routine was paused while this run waited for you" });
+ await Promise.resolve();
+ expect(h.interruptedTurns).toEqual([]);
+ });
+
it("leaves a run that is still working alone when its routine is paused", async () => {
const h = harness(monday);
const routine = h.manager.create(daily);
diff --git a/server/routines.ts b/server/routines.ts
index 5aa46d45ac..c88feca723 100644
--- a/server/routines.ts
+++ b/server/routines.ts
@@ -265,7 +265,7 @@ export interface RoutineManagerOptions {
onRunCut?: (run: RoutineRun) => void;
hasPendingDelegations?: (threadId: string) => boolean; /** Why a run that ended well in this thread must say a hand-off was refused, once; null when none was. */ refusedHandoff?: (threadId: string) => string | null;
/** The turn in this thread is a routine's own, not one a person started there while its run waits on teammates. */
- routineTurn?: (threadId: string) => boolean;
+ routineTurn?: (threadId: string) => boolean; /** The thread's latest turn is one a person started there by typing (false when not known, e.g. after a restart). */ personTurn?: (threadId: string) => boolean;
/** When an engine's usage limit resets (epoch ms), or null while that is not known. */
limitResetAt?: (engine: string) => number | null;
/** The bot has a backup engine to continue on when its own engine's usage limit refuses a run, or it is signed out. */