Skip to content

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

fix(coordinator): don't count a system stop as the person's stop

AdityaUmale7e1e7d95d6ef
diff --git a/server/runtime-coordinator.test.ts b/server/runtime-coordinator.test.ts
index 290fb71026..b6ac310ae2 100644
--- a/server/runtime-coordinator.test.ts
+++ b/server/runtime-coordinator.test.ts
@@ -954,6 +954,56 @@ describe("RuntimeCoordinator: a stop the person made is remembered", () => {
});
});
+// From a bug report: the chief of staff handed a long job to a teammate, then sent it a one-way follow-up, which queued
+// behind the running job. The system then cut the teammate's running step (a waiting message went first), which ended
+// the job, and the teammate's own thread stopping with it (the host's abort handler) was remembered as the person's stop:
+// handing the job out again was refused until the person asked again, though the person never stopped anything.
+describe("RuntimeCoordinator: a stop the system made is never remembered as the person's", () => {
+ const tick = () => sleep(2);
+ /** Like the host: a teammate's coordinated turn that is aborted stops its own thread, with no reason of its own. */
+ const stopsItsThread = (w: World) => {
+ for (const run of w.host.runs) run.signal.addEventListener("abort", () => w.coordinator.stopAwaitingDirect(run.node.threadId), { once: true });
+ };
+ const handedOut = async () => {
+ const w = world(hold);
+ const { node: work } = send(w, chief, "gen-1", undefined, builder, "Plan the pricing feature");
+ w.coordinator.tick();
+ await tick();
+ const { node: followUp } = w.coordinator.enqueue(chief, "gen-1", undefined, builder, "Also cover the trial page", { mode: "notify", reply: "none" });
+ w.coordinator.tick();
+ stopsItsThread(w);
+ return { w, work, followUp };
+ };
+
+ it("keeps a one-way follow-up queued behind the running work, without touching that work", async () => {
+ const { w, work, followUp } = await handedOut();
+ expect(w.coordinator.node(work.id)?.status).toBe("running");
+ expect(w.coordinator.node(followUp.id)?.status).toBe("queued");
+ expect(w.host.runs.filter(r => r.node.botId === "builder")).toHaveLength(1);
+ expect(w.host.runs[0]!.signal.aborted).toBe(false);
+ });
+
+ it.each([
+ ["the teammate's step cut so a waiting message goes first", (w: World) => w.coordinator.stopAwaitingDirect("builder-work", "Stopped after 9 min so a waiting message could go first")],
+ ["the teammate's thread cut by a routine restart", (w: World) => w.coordinator.cancelDirect("builder-work", "The routine restarted")],
+ ])("lets the work be handed out again after %s", async (_, stop) => {
+ const { w, work, followUp } = await handedOut();
+ stop(w);
+ expect(w.coordinator.node(work.id)?.status).toBe("cancelled");
+ expect(w.coordinator.node(followUp.id)?.status).toBe("cancelled");
+ await tick();
+ expect(send(w, chief, "gen-1", undefined, builder, "Plan the pricing feature", { rework: true }).node).toMatchObject({ botId: "builder", status: "queued" });
+ });
+
+ it("still remembers the person's own stop of that work, with the same follow-up queued", async () => {
+ const { w, work } = await handedOut();
+ w.coordinator.stopAwaitingDirect("builder-work");
+ expect(w.coordinator.node(work.id)?.status).toBe("cancelled");
+ await tick();
+ expect(() => send(w, chief, "gen-1", undefined, builder, "Plan the pricing feature", { rework: true })).toThrow("The person stopped this teammate's work on this request.");
+ });
+});
+
describe("RuntimeCoordinator: read models", () => {
it("awaitingTeammateThreads answers, for every thread at once, what outstandingDirect and the parked roots answer per thread", async () => {
const w = world(hold);
diff --git a/server/runtime-coordinator.ts b/server/runtime-coordinator.ts
index e30c31f0d6..446a220bdd 100644
--- a/server/runtime-coordinator.ts
+++ b/server/runtime-coordinator.ts
@@ -150,6 +150,8 @@ export class RuntimeCoordinator implements Coordinator {
private readonly log: (line: string) => void;
/** The person's stops, remembered: which bots' work on which request they stopped, and when. */
private readonly stops: Array<{ at: number; request: string; bots: Set<string> }> = [];
+ /** The cause of the stop running now, if any (stopFor). */
+ private stopCause: string | undefined;
private opened = false;
private prunedAt: number | undefined;
@@ -381,7 +383,7 @@ export class RuntimeCoordinator implements Coordinator {
/** Nothing can answer what this parked turn waits on any more. */
releaseParked(id: string, reason: string): void {
this.ready();
- if (this.store.get(id)?.status === "needs_input") this.runtime.cancel(id, reason, "stopped");
+ if (this.store.get(id)?.status === "needs_input") this.stopFor(reason, () => this.runtime.cancel(id, reason, "stopped"));
}
parkedFor(botId: string): HandoffView[] {
@@ -414,27 +416,40 @@ export class RuntimeCoordinator implements Coordinator {
return { id: batch[0]!.id, botIds: batch.map(c => c.assignee.botId) };
}
- cancelRoom(groupId: string, threadId?: string, reason = "Stopped by user", status: "failed" | "cancelled" = "cancelled"): void {
+ cancelRoom(groupId: string, threadId?: string, reason = STOPPED_BY_USER, status: "failed" | "cancelled" = "cancelled"): void {
this.ready();
- for (const n of this.live()) {
- if (n.assignee.destination.groupId !== groupId || (threadId && n.assignee.destination.threadId !== threadId)) continue;
- this.runtime.cancel(n.id, reason, status === "failed" ? "failed" : "stopped");
- }
+ this.stopFor(reason, () => {
+ for (const n of this.live()) {
+ if (n.assignee.destination.groupId !== groupId || (threadId && n.assignee.destination.threadId !== threadId)) continue;
+ this.runtime.cancel(n.id, reason, status === "failed" ? "failed" : "stopped");
+ }
+ });
}
- cancelDirect(threadId: string, reason = "Stopped by user"): void {
+ cancelDirect(threadId: string, reason = STOPPED_BY_USER): void {
this.ready();
- for (const n of this.live()) if (this.direct(n, threadId)) this.runtime.cancel(n.id, reason, "stopped");
+ this.stopFor(reason, () => { for (const n of this.live()) if (this.direct(n, threadId)) this.runtime.cancel(n.id, reason, "stopped"); });
}
- stopAwaitingDirect(threadId: string, reason = STOPPED_BY_USER): void {
+ /** With no reason given, the stop is the person's, unless it is part of a stop already under way: see stopFor. */
+ stopAwaitingDirect(threadId: string, reason?: string): void {
this.ready();
- const stopping = reason === STOPPED_BY_USER ? this.live().filter(n => n.assignee.destination.threadId === threadId) : [];
+ const cause = reason ?? this.stopCause ?? STOPPED_BY_USER;
+ const stopping = cause === STOPPED_BY_USER ? this.live().filter(n => n.assignee.destination.threadId === threadId) : [];
const trees = stopping.map(n => ({ root: this.store.get(n.rootId), work: this.subtree(n).filter(a => !isTerminal(a)) }));
- this.runtime.stopRequester({ threadId }, reason);
+ this.stopFor(cause, () => this.runtime.stopRequester({ threadId }, cause));
for (const { root, work } of trees) if (root) this.rememberStop(root, work);
}
+ /** Runs a stop with its cause. A teammate's turn it ends stops that teammate's thread as well (the host's abort handler
+ * calls stopAwaitingDirect with no reason), and that is still this stop: a cut the system made (a waiting line going
+ * first, a restart, a deleted conversation) is never remembered as the person's stop. */
+ private stopFor(cause: string, stop: () => void): void {
+ const outer = this.stopCause;
+ this.stopCause = cause;
+ try { stop(); } finally { this.stopCause = outer; }
+ }
+
/** The person's stop, remembered (scoped to that request): the bots whose work on it the stop ended don't take the same
* request from another bot again, unless the person asked again after the stop. In memory only, for a day. */
private rememberStop(root: Assignment, work: readonly Assignment[]): void {