Skip to content

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

fix(coordination): let a bot answer the thread that asked it with reply none

AdityaUmalecd82d5278d02
diff --git a/server/direct-coordination.e2e.test.ts b/server/direct-coordination.e2e.test.ts
index 8279255539..a132c62bc9 100644
--- a/server/direct-coordination.e2e.test.ts
+++ b/server/direct-coordination.e2e.test.ts
@@ -93,6 +93,26 @@ it("sends cross-bot work onward without resuming either sender", () => fixture(a
expect((await f.messages(f.chief.activeTaskId)).some((message: any) => message.text?.includes("QA sent") || message.text?.includes("Release QA complete"))).toBe(false);
}), 60_000);
+it("lets a teammate answer a one-way ask in the thread that asked, with a notice and nothing more", () => fixture(async f => {
+ const asking = f.chief.activeTaskId;
+ f.plan[f.chief.id] = { steps: [{ arguments: {
+ bot_ids: [f.lead.id], message: `Ask the person which day suits, then tell me in thread ${asking}.`, thread: "new", reply: "none",
+ } }], reply: "Asked Engineering", byPrompt: [{ promptIncludes: "Tuesday suits", plan: { reply: "Got Tuesday from Engineering" } }] };
+ f.plan[f.lead.id] = { steps: [
+ { arguments: { bot_ids: [f.chief.id], message: "Tuesday suits, please plan for it", thread: asking }, expectError: true },
+ { arguments: { bot_ids: [f.chief.id], message: "Tuesday suits", thread: asking, reply: "none" } },
+ ], reply: "Told Clive" };
+ await f.start();
+ const sends = () => f.evidence().find((turn: any) => turn.botId === f.lead.id)?.evidence.filter((entry: any) => entry.step?.arguments?.thread === asking);
+ await expect.poll(() => sends()?.length, { timeout: 15_000 }).toBe(2);
+ const [work, notice] = sends().map((entry: any) => entry.response.result);
+ expect(JSON.stringify(work)).toContain("handed you work: answer it there with reply");
+ expect(notice.isError).toBeFalsy();
+ expect(JSON.parse(notice.content[0].text).receipts[0]).toMatchObject({ threadId: asking });
+ await expect.poll(async () => (await f.messages(asking)).some((message: any) => message.text === "Got Tuesday from Engineering"), { timeout: 15_000 }).toBe(true);
+ expect((await f.api("/api/bots")).bots.find((bot: any) => bot.id === f.chief.id).tasks.filter((task: any) => task.title?.includes("Tuesday"))).toEqual([]);
+}), 60_000);
+
it("marks a failed one-way send so restart recovery cannot duplicate its notice", () => fixture(async f => {
// The failed send reports an incident to Clive, the lead's Chief; answering it must not send the task again.
f.plan[f.chief.id] = { steps: [{ arguments: {
diff --git a/server/host/work-thread.test.ts b/server/host/work-thread.test.ts
new file mode 100644
index 0000000000..e55015ea72
--- /dev/null
+++ b/server/host/work-thread.test.ts
@@ -0,0 +1,41 @@
+import { describe, expect, it } from "vitest";
+
+import type { TaskRecord } from "../store.ts";
+import { workThreadFor } from "./work-thread.ts";
+
+const by = (botId: string, name: string, threadId: string): TaskRecord["openedBy"] => ({ botId, name, threadId, kind: "work", at: 0 });
+const task = (threadId: string, openedBy?: TaskRecord["openedBy"], more: Partial<TaskRecord> = {}): TaskRecord =>
+ ({ id: threadId, threadId, title: threadId, ...(openedBy ? { openedBy } : {}), ...more }) as TaskRecord;
+
+// Bugfixer's scheduled run ("run-1") asked Liaison with reply none: that handoff opened Liaison's "liaison-work".
+const tasks: Record<string, TaskRecord[]> = {
+ bugfixer: [task("run-1"), task("archived-run", undefined, { archivedAt: 1 }), task("opened-by-liaison", by("liaison", "Liaison", "liaison-chat"))],
+ liaison: [task("liaison-work", by("bugfixer", "Bugfixer", "run-1")),
+ task("archived-ask", by("bugfixer", "Bugfixer", "archived-run"))],
+};
+const store = {
+ tasks: (botId: string) => tasks[botId] ?? [],
+ taskByThread: (botId: string, threadId: string) => (tasks[botId] ?? []).find((t) => t.threadId === threadId),
+ workThread: () => undefined,
+};
+const fromLiaison = (thread: string, notice: boolean, also: (task: TaskRecord) => boolean = () => true) => workThreadFor(store,
+ { botId: "bugfixer", botName: "Bugfixer", fromBotId: "liaison", fromThreadId: "liaison-chat", thread, notice, also });
+
+describe("workThreadFor", () => {
+ it("keeps a thread the sender opened", () => {
+ expect(fromLiaison("opened-by-liaison", false)?.threadId).toBe("opened-by-liaison");
+ });
+
+ it("lets a one-way notice answer the thread that handed the sender work, and refuses new work there", () => {
+ expect(fromLiaison("run-1", true)?.threadId).toBe("run-1");
+ expect(() => fromLiaison("run-1", false)).toThrow(/handed you work: answer it there with reply "none"/);
+ });
+
+ it("refuses any other thread, an archived one, or one `also` rules out, even for a notice", () => {
+ const anyOther = { ...store, taskByThread: () => task("someone-elses") };
+ expect(() => workThreadFor(anyOther, { botId: "bugfixer", botName: "Bugfixer", fromBotId: "liaison", fromThreadId: "liaison-chat", thread: "someone-elses", notice: true, also: () => true }))
+ .toThrow(/is not a thread you opened on Bugfixer/);
+ expect(() => fromLiaison("archived-run", true)).toThrow(/is not a thread you opened/);
+ expect(() => fromLiaison("run-1", true, () => false)).toThrow(/is not a thread you opened/);
+ });
+});
diff --git a/server/host/work-thread.ts b/server/host/work-thread.ts
index a6578aa1e0..63c1999c4e 100644
--- a/server/host/work-thread.ts
+++ b/server/host/work-thread.ts
@@ -2,13 +2,19 @@ import type { Store, TaskRecord } from "../store.ts";
/** Where coordinate_bots sends to a teammate outside a room, by its `thread`: "standing" is the sender conversation's one
* work thread with that teammate (undefined until it is opened), "new" opens one (undefined), and any other value is the id
- * of a thread the sender opened on that teammate earlier. Throws when it is not one. `also` narrows which threads count. */
-export function workThreadFor(store: Pick<Store, "workThread" | "taskByThread">, input: { botId: string; botName: string; fromBotId: string; fromThreadId: string;
- thread: string; also: (task: TaskRecord) => boolean }): TaskRecord | undefined {
+ * of a thread the sender opened on that teammate earlier, or of the teammate's thread that handed the sender work: a
+ * requester that asked with reply none hears back only there, so it takes a one-way notice (`notice`), never new work.
+ * Throws when it is neither. `also` narrows which threads count. */
+export function workThreadFor(store: Pick<Store, "workThread" | "taskByThread" | "tasks">, input: { botId: string; botName: string; fromBotId: string; fromThreadId: string;
+ thread: string; notice: boolean; also: (task: TaskRecord) => boolean }): TaskRecord | undefined {
const { botId, thread, also } = input;
if (thread === "new") return undefined;
if (thread === "standing") return store.workThread(botId, input.fromBotId, input.fromThreadId, false, also);
const task = store.taskByThread(botId, thread);
- if (task?.openedBy?.botId === input.fromBotId && !task.openedBy.roomLane && task.archivedAt === undefined && also(task)) return task;
+ const usable = task !== undefined && task.archivedAt === undefined && also(task);
+ if (usable && task.openedBy?.botId === input.fromBotId && !task.openedBy.roomLane) return task;
+ const handedWork = usable && store.tasks(input.fromBotId).some((own) => own.openedBy?.botId === botId && own.openedBy.threadId === thread && !own.openedBy.roomLane);
+ if (handedWork && input.notice) return task;
+ if (handedWork) throw new Error(`"${thread}" handed you work: answer it there with reply "none", a one-way notice; it cannot be given new work`);
throw new Error(`"${thread}" is not a thread you opened on ${input.botName}: use a threadId one of your coordinate_bots receipts gave, or one list_threads shows on them`);
}
diff --git a/server/index.ts b/server/index.ts
index af0c068bcc..2feeebfb6e 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -13940,7 +13940,7 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {
if (!recipient) throw new Error("The recipient no longer exists");
// Work a person's request hands on opens in the receiving bot's chat with them when it may use their own apps.
const opener = (room ? null : handOnFor) ?? openerFrom(address.threadId);
- const continued = room || fresh ? undefined : workThreadFor(store, { botId: target.botId, botName: recipient.name, fromBotId: internalSender.id, fromThreadId: address.threadId, thread: call.thread,
+ const continued = room || fresh ? undefined : workThreadFor(store, { botId: target.botId, botName: recipient.name, fromBotId: internalSender.id, fromThreadId: address.threadId, thread: call.thread, notice: mode === "notify",
also: (task) => !CLOUD_TEAM || threadStarters.get(task.threadId) === opener });
const relayed = EXTERNAL || (!call.answers && (room || fresh || mode === "consult")) ? undefined : await questionRouting.relay({ sender: internalSender, senderThreadId: address.threadId,
senderNode: coordinator.node(internalCapability.roomHandoffId ?? generation), target: recipient, message: call.message, ...(call.answers ? { answers: call.answers } : {}), ...(continued ? { threadId: continued.threadId } : {}),
diff --git a/server/runtime-coordinator.test.ts b/server/runtime-coordinator.test.ts
index 60febd3e2c..1aa0bb7a20 100644
--- a/server/runtime-coordinator.test.ts
+++ b/server/runtime-coordinator.test.ts
@@ -338,6 +338,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 () => {
+ 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/);
+ 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" } });
+ 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/);
+ await w.host.release(awaited.node.id, { ok: true, text: "done" });
+ });
+
it("refuses a second assignment to a teammate that completed one, unless it is rework", async () => {
const w = world();
const { node } = send(w, chief, "gen-1", undefined, builder, "Build it");
diff --git a/server/runtime-coordinator.ts b/server/runtime-coordinator.ts
index d1f70c07b3..154cadfa94 100644
--- a/server/runtime-coordinator.ts
+++ b/server/runtime-coordinator.ts
@@ -7,7 +7,7 @@ import { Runtime } from "./runtime/runtime.ts";
import { importLegacyDelegations, importLegacyHandoffs, type LegacyThreadResolver } from "./runtime/import-legacy.ts";
import { openRuntimeStore, type RuntimeStore } from "./runtime/store.ts";
import { personOnly, requesterDecides, withDefault } from "./runtime/questions.ts";
-import { childrenSince, clip, isTerminal, path } from "./runtime/tree.ts";
+import { answersSender, childrenSince, clip, isTerminal, path } from "./runtime/tree.ts";
import type { Answerer, Assignment, EndResult, Origin, Owner, Reply, Requester, RunInput, RunResult, StatusView, TurnRunner } from "./runtime/types.ts";
import { sameRequest } from "./same-request.ts";
@@ -200,7 +200,7 @@ export class RuntimeCoordinator implements Coordinator {
const kind: HandoffView["kind"] = target.groupId !== undefined && target.groupId === source.groupId ? "assignment" : "work";
const lineage = path(this.store, parent);
const root = lineage[0]!;
- if (lineage.some(n => ancestorOf(address(n), target))) throw new Error(ANCESTOR);
+ if (!answersSender(parent, mode, target) && lineage.some(n => ancestorOf(address(n), target))) throw new Error(ANCESTOR);
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);
if (existing && !(rework && isTerminal(existing))) return { node: view(existing, parent), duplicate: true };
diff --git a/server/runtime/envelope.test.ts b/server/runtime/envelope.test.ts
index 3c299acb63..fcaeb8edaf 100644
--- a/server/runtime/envelope.test.ts
+++ b/server/runtime/envelope.test.ts
@@ -70,6 +70,14 @@ describe("composeEnvelope: assignment with person lineage", () => {
.toContain("Ownership is yours: results, failures and questions stay in this thread; @Chief is not waiting and will not be resumed.");
});
+ it("tells work handed on with reply none how to answer back into its requester's thread, outside a room only", () => {
+ const answerBack = 'If it asked to hear back, send @Chief one coordinate_bots notice with reply "none" and thread "th_1".';
+ expect(composeEnvelope(assignment({ reply: "none", mode: "notify" }), names)).toContain(answerBack);
+ expect(envelope).not.toContain("If it asked to hear back");
+ const inRoom = assignment({ reply: "none", mode: "notify", assignee: { botId: "scout", destination: { kind: "room_thread", threadId: "th_2", groupId: "g_1" } } });
+ expect(composeEnvelope(inRoom, names)).not.toContain("If it asked to hear back");
+ });
+
it("tells a bot's assignee to ask its requester by ending the turn, never with coordinate_bots", () => {
expect(envelope).toContain("Ask @Chief or anyone above it by ending your turn with the question, never coordinate_bots.");
expect(composeEnvelope(assignment({ requester: { kind: "person" } }), names)).not.toContain("never coordinate_bots");
diff --git a/server/runtime/envelope.ts b/server/runtime/envelope.ts
index 0ceb59feb5..c1d5ed63ed 100644
--- a/server/runtime/envelope.ts
+++ b/server/runtime/envelope.ts
@@ -84,7 +84,10 @@ function replyLine(a: Assignment, requester: string): string {
return `${requester} is resumed with your reply when your turn ends${close}.${ask}`;
}
if (a.reply === "poll") return `${requester} reads your reply from its inbox when it asks${close}.`;
- return `Ownership is yours: results, failures and questions stay in this thread; ${requester} is not waiting and will not be resumed.`;
+ const { botId, threadId } = a.requester;
+ const answerBack = a.requester.kind === "bot_turn" && botId && threadId && a.assignee.destination.groupId === undefined
+ ? ` If it asked to hear back, send ${requester} one coordinate_bots notice with reply "none" and thread "${threadId}".` : "";
+ return `Ownership is yours: results, failures and questions stay in this thread; ${requester} is not waiting and will not be resumed.${answerBack}`;
}
/** Harness-quoted, so the person's "yes" never travels as pasted text, and a bot's answer never passes for theirs. */
diff --git a/server/runtime/runtime.ts b/server/runtime/runtime.ts
index 8f8fe5a953..d82b417bf7 100644
--- a/server/runtime/runtime.ts
+++ b/server/runtime/runtime.ts
@@ -4,7 +4,7 @@ import { randomUUID } from "node:crypto";
import { composeEnvelope } from "./envelope.ts";
import { assignmentLevel } from "./level.ts";
import type { RuntimeStore } from "./store.ts";
-import { asking, awaited, childrenSince, clip, continuations, CUT_TWICE, duration, isTerminal, loopStop, nextGuard, outwardDetail, ownsConversation, path, restartNote, wakeOwner } from "./tree.ts";
+import { answersSender, asking, awaited, childrenSince, clip, continuations, CUT_TWICE, duration, isTerminal, loopStop, nextGuard, outwardDetail, ownsConversation, path, restartNote, wakeOwner } from "./tree.ts";
import { DEFAULT_LIMITS, type Answerer, type Assignment, type Assignee, type Clock, type EndResult, type EnqueueRequest, type EnqueueResult, type EnvelopeNames,
type EventKind, type InboxItem, type InboxKind, type Level, type Limits, type LiveEdge, type Origin, type Outcome, type Owner,
type Reply, type Requester, type RunInput, type RunResult, type StatusView, type TurnRunner } from "./types.ts";
@@ -191,7 +191,7 @@ export class Runtime {
const lineage = parent ? path(this.store, parent) : [];
const root = lineage[0];
const target = req.assignee;
- if (lineage.some(a => a.assignee.botId === target.botId &&
+ if (!answersSender(parent, mode, { botId: target.botId, ...target.destination }) && lineage.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");
}
diff --git a/server/runtime/tree.ts b/server/runtime/tree.ts
index 2618f23511..aa4e11b034 100644
--- a/server/runtime/tree.ts
+++ b/server/runtime/tree.ts
@@ -1,6 +1,6 @@
// "Since this turn began" is read from event ids (one global order), never timestamps: a clock can step back.
import type { RuntimeStore } from "./store.ts";
-import { terminal, type Assignment, type Owner, type TurnEvidence } from "./types.ts";
+import { terminal, type Assignment, type Mode, type Owner, type TurnEvidence } from "./types.ts";
export const CUT_TWICE = "Interrupted by another OpenMausBot restart after resuming once; not resumed again. Some of its steps may already have taken effect.";
@@ -74,6 +74,13 @@ export const awaited = (children: readonly Assignment[]): Assignment[] => childr
* the rest of the work moves on while the person acts on it. Another card park waits on its card. */
export const asking = (a: Assignment): boolean => a.status === "needs_input" && (!(a.cards && a.cards.length > 0) || a.level === "full");
+/** A one-way notice from the turn running `parent` back into the conversation that handed it that work with reply none.
+ * Nothing returns from such work on its own, so this is the only way its answer gets back: the ancestor rule lets it
+ * through, but never as new work and never into another conversation of that bot. */
+export const answersSender = (parent: Assignment | undefined, mode: Mode, target: { botId: string; threadId: string; groupId?: string | undefined }): boolean =>
+ mode === "notify" && parent?.reply === "none" && parent.requester.botId === target.botId && parent.requester.threadId === target.threadId
+ && parent.assignee.destination.groupId === undefined && target.groupId === undefined;
+
export function path(store: RuntimeStore, n: Assignment): Assignment[] {
const out: Assignment[] = [];
const seen = new Set<string>();