Skip to content

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

fix(runtime): let non-waiting work go back to an ancestor (#6539)

CI fixturec47a9d772cf7
diff --git a/server/runtime-coordinator.test.ts b/server/runtime-coordinator.test.ts
index b5914980a3..292cfa84a0 100644
--- a/server/runtime-coordinator.test.ts
+++ b/server/runtime-coordinator.test.ts
@@ -393,20 +393,22 @@ describe("RuntimeCoordinator: refusals in the legacy words", () => {
await w.host.release(node.id, { ok: true, text: "done" });
});
- it("lets work handed on with reply none answer its requester's conversation with a one-way notice, and nothing more", async () => {
+ it("lets work handed on with reply none answer its requester's conversation with a one-way notice, and reach an ancestor only as a notice", async () => {
const w = world(hold);
begun(w, chief, "gen-1");
const { node } = w.coordinator.enqueue(chief, "gen-1", undefined, builder, "Ask the person, then tell me", { mode: "notify", reply: "none" });
w.coordinator.tick();
expect(() => send(w, builder, "gen-1", node.id, chief, "They said yes")).toThrow(/back to an ancestor/);
- expect(() => w.coordinator.enqueue(builder, "gen-1", node.id, { ...chief, threadId: "chief-other" }, "They said yes", { mode: "notify", reply: "none" })).toThrow(/back to an ancestor/);
+ expect(w.coordinator.enqueue(builder, "gen-1", node.id, { ...chief, threadId: "chief-other" }, "For your other chat", { mode: "notify", reply: "none" }))
+ .toMatchObject({ duplicate: false, node: { botId: "chief", threadId: "chief-other", status: "queued" } });
const answer = w.coordinator.enqueue(builder, "gen-1", node.id, chief, "They said yes", { mode: "notify", reply: "none" });
expect(answer).toMatchObject({ duplicate: false, node: { botId: "chief", threadId: "chief-chat", status: "queued" } });
expect(w.coordinator.answersTo(node.id)).toEqual({ botId: "chief", threadId: "chief-chat" });
await w.host.release(node.id, { ok: true, text: "done" });
const awaited = send(w, chief, "gen-2", undefined, builder, "Build it");
w.coordinator.tick();
- expect(() => w.coordinator.enqueue(builder, "gen-2", awaited.node.id, chief, "Done", { mode: "notify", reply: "none" })).toThrow(/back to an ancestor/);
+ expect(w.coordinator.enqueue(builder, "gen-2", awaited.node.id, chief, "Done", { mode: "notify", reply: "none" }))
+ .toMatchObject({ duplicate: false, node: { botId: "chief", threadId: "chief-chat", status: "queued" } });
expect(w.coordinator.answersTo(awaited.node.id)).toBeUndefined();
await w.host.release(awaited.node.id, { ok: true, text: "done" });
});
diff --git a/server/runtime-coordinator.ts b/server/runtime-coordinator.ts
index 41fdeb5602..f0cf3fa305 100644
--- a/server/runtime-coordinator.ts
+++ b/server/runtime-coordinator.ts
@@ -296,7 +296,10 @@ export class RuntimeCoordinator implements Coordinator {
const assignee: Assignee = { botId: target.botId, destination: { kind: target.groupId ? "room_thread" : thread === "standing" ? "standing_thread" : "work_thread",
threadId: target.threadId, ...(target.groupId ? { groupId: target.groupId } : {}) } };
const own = ownThread(lineage, assignee, options.ownThread);
- if (!answersSender(parent, mode, target) && (own ? lineage.slice(0, -1) : lineage).some(n => ancestorOf(address(n), target))) throw new Error(ANCESTOR);
+ const wanted: Reply = mode === "notify" ? "none" : options.reply ?? "resume";
+ const reply: Reply = wanted === "none" ? "none" : root.reply === "poll" ? "poll" : wanted;
+ // Same rule as the runtime's admit: only a reply that would wait on the ancestor's completion closes a wait cycle.
+ if (!answersSender(parent, mode, target) && reply === "resume" && (own ? lineage.slice(0, -1) : lineage).some(n => ancestorOf(address(n), target))) throw new Error(ANCESTOR);
if (this.stoppedFor(root, target.botId)) throw new Error(STOPPED_AGAIN);
const siblings = this.store.children(parent.id);
const existing = siblings.findLast(c => c.assignee.botId === target.botId && c.assignee.destination.groupId === target.groupId && c.brief === brief && c.mode === mode
@@ -310,8 +313,6 @@ export class RuntimeCoordinator implements Coordinator {
// A bot's own threads wait on it by design and can never ask it back (the ancestor rule above), so no two bots can cross.
const waiting = own || mode === "notify" || target.groupId !== undefined ? undefined : this.waitingOn(source.botId, target.botId);
if (waiting) throw new Error(crossed(this.host.names(this.project(waiting.request), this.project(waiting.parent)).requester, waiting.request));
- const wanted: Reply = mode === "notify" ? "none" : options.reply ?? "resume";
- const reply: Reply = wanted === "none" ? "none" : root.reply === "poll" ? "poll" : wanted;
const requester: Requester = { kind: "bot_turn", botId: parent.assignee.botId, threadId: parent.assignee.destination.threadId,
chief: this.host.chiefOf(address(parent)), ...(parent.id === root.id && root.requester.runId !== undefined ? { runId: root.requester.runId } : {}) };
const level = this.host.levelOf(target, this.project(parent));
diff --git a/server/runtime/runtime.test.ts b/server/runtime/runtime.test.ts
index 48ccc8ca17..39bd726dad 100644
--- a/server/runtime/runtime.test.ts
+++ b/server/runtime/runtime.test.ts
@@ -201,6 +201,32 @@ describe("Runtime: one tree, one lane", () => {
expect(w.store.all()).toHaveLength(4);
});
+ it("lets work come back to an ancestor only when its reply cannot wait on that ancestor", async () => {
+ const w = world(() => "hold");
+ const root = w.runtime.begin(beginReq());
+ const child = ok(w.runtime.enqueue(toBot("builder", root.id)));
+ w.runtime.tick();
+ expect(w.store.get(child.id)?.status).toBe("running");
+ const builder = { kind: "bot_turn" as const, botId: "builder", threadId: "builder-work" };
+ const ask = ok(w.runtime.enqueue({ ...toBot("chief", child.id), requester: builder, reply: "poll" }));
+ expect(ask).toMatchObject({ parentId: child.id, reply: "poll", assignee: { botId: "chief" } });
+ const notice = ok(w.runtime.enqueue({ ...toBot("chief", child.id, "A one-way notice"), requester: builder, mode: "notify" }));
+ expect(notice).toMatchObject({ parentId: child.id, reply: "none" });
+ const refused = "Cannot assign work back to an ancestor; results return automatically";
+ expect(w.runtime.enqueue({ ...toBot("chief", child.id, "Wait on me"), requester: builder }))
+ .toMatchObject({ ok: false, outcome: "refused", reason: refused });
+ expect(w.runtime.enqueue({ ...toBot("chief", child.id, "Wait on me too"), requester: builder, reply: "resume" }))
+ .toMatchObject({ ok: false, outcome: "refused", reason: refused });
+ invariants(w);
+ await w.runner.release(child.id, { kind: "completed", text: "builder done" });
+ w.runtime.tick();
+ expect(w.store.get(ask.id)?.status).toBe("running");
+ await w.runner.release(ask.id, { kind: "completed", text: "chief answered" });
+ expect(w.store.get(ask.id)).toMatchObject({ status: "completed", outcome: "done" });
+ expect(w.store.inbox({ botId: "builder", threadId: "builder-work" }).map(i => i.assignmentId)).toEqual([ask.id]);
+ invariants(w);
+ });
+
it("renews the root's budgets on progress, and never refuses the owed resume turn", async () => {
const w = world(undefined, { requests: 2, executions: 2 });
const root = w.runtime.begin(beginReq());
diff --git a/server/runtime/runtime.ts b/server/runtime/runtime.ts
index 2a8f2f74e1..c06a727ac2 100644
--- a/server/runtime/runtime.ts
+++ b/server/runtime/runtime.ts
@@ -229,12 +229,15 @@ export class Runtime {
if (parent && parent.status !== "running") return refuse("The requesting turn is not running");
const brief = clip(req.brief, BRIEF_MAX);
const mode = req.mode ?? "work";
+ const reply = req.reply ?? (mode === "notify" ? "none" : "resume");
const lineage = parent ? path(this.store, parent) : [];
const root = lineage[0];
const target = req.assignee;
let sameCall = false;
const ancestors = ownThread(lineage, target, req.ownThread) ? lineage.slice(0, -1) : lineage;
- if (!answersSender(parent, mode, { botId: target.botId, ...target.destination }) && ancestors.some(a => a.assignee.botId === target.botId &&
+ // Work that would wait on an ancestor's completion closes the wait cycle the delegation already opened; a reply
+ // that cannot wait on it (none, poll) cannot, so an ancestor may take it.
+ if (!answersSender(parent, mode, { botId: target.botId, ...target.destination }) && reply === "resume" && ancestors.some(a => a.assignee.botId === target.botId &&
(!a.assignee.destination.groupId || !target.destination.groupId || a.assignee.destination.groupId === target.destination.groupId))) {
return refuse("Cannot assign work back to an ancestor; results return automatically");
}
@@ -260,7 +263,7 @@ export class Runtime {
const node: Assignment = {
id: randomUUID(), rootId: parent ? parent.rootId : "", ...(parent ? { parentId: parent.id } : {}), depth: parent ? parent.depth + 1 : 0,
origin: root ? root.origin : req.origin, requester: req.requester, assignee: target, mode,
- reply: req.reply ?? (mode === "notify" ? "none" : "resume"), brief, ...(req.doneWhen ? { doneWhen: req.doneWhen } : {}),
+ reply, brief, ...(req.doneWhen ? { doneWhen: req.doneWhen } : {}),
status: "queued", level, levelSource: source, executions: 0, restarts: 0, createdAt: now, lastActivityAt: now, result: "",
rework: Boolean(req.rework), ...(req.approved !== undefined ? { approved: req.approved } : {}),
...(req.batchKey !== undefined ? { batchKey: req.batchKey } : {}), ...(req.fanOut !== undefined ? { fanOut: req.fanOut } : {}), version: 1,