Skip to content

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

fix(coordination): a reply-none notice answers only the running assignment's requester

AdityaUmalee12af04df87a
diff --git a/server/coordinator.ts b/server/coordinator.ts
index e4cd98a81e..de2f6c2c84 100644
--- a/server/coordinator.ts
+++ b/server/coordinator.ts
@@ -153,6 +153,9 @@ export interface CoordinatorHost {
export interface Coordinator {
node(id: string): HandoffView | undefined;
+ /** The conversation the running assignment `id` may answer with a one-way notice: its requester, when it was handed
+ * with reply none outside rooms. */
+ answersTo(id: string): { botId: string; threadId: string } | undefined;
has(id: string): boolean;
all(): HandoffView[];
active(): HandoffView[];
diff --git a/server/host/work-thread.test.ts b/server/host/work-thread.test.ts
index b6485b0c71..9efc051acf 100644
--- a/server/host/work-thread.test.ts
+++ b/server/host/work-thread.test.ts
@@ -7,18 +7,20 @@ const by = (botId: string, name: string, threadId: string): TaskRecord["openedBy
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".
+// Bugfixer's scheduled run ("run-1") asked Liaison with reply none: Liaison's running assignment answers "run-1".
+// An earlier run ("run-0") once handed the same reused "liaison-work" thread work, so its opener is stale.
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"))],
+ bugfixer: [task("run-0"), 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-0"))],
};
+const run1 = { botId: "bugfixer", threadId: "run-1" };
const store = {
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, fromThreadId = "liaison-work") => workThreadFor(store,
- { botId: "bugfixer", botName: "Bugfixer", fromBotId: "liaison", fromThreadId, thread, notice, also });
+const fromLiaison = (thread: string, notice: boolean, also: (task: TaskRecord) => boolean = () => true,
+ answering: { botId: string; threadId: string } | undefined = run1) => workThreadFor(store,
+ { botId: "bugfixer", botName: "Bugfixer", fromBotId: "liaison", fromThreadId: "liaison-work", thread, notice, answering, also });
describe("workThreadFor", () => {
it("keeps a thread the sender opened", () => {
@@ -30,15 +32,18 @@ describe("workThreadFor", () => {
expect(() => fromLiaison("run-1", false)).toThrow(/handed you work: answer it there with reply "none"/);
});
- it("takes that notice only from the thread the work was handed to", () => {
- expect(() => fromLiaison("run-1", true, () => true, "liaison-chat")).toThrow(/is not a thread you opened on Bugfixer/);
+ it("takes that notice only for the running assignment's requester, not a stale opener of a reused thread", () => {
+ expect(() => fromLiaison("run-0", true)).toThrow(/is not a thread you opened on Bugfixer/);
+ expect(() => workThreadFor(store, { botId: "bugfixer", botName: "Bugfixer", fromBotId: "liaison", fromThreadId: "liaison-work", thread: "run-1",
+ notice: true, answering: undefined, also: () => true })).toThrow(/is not a thread you opened on Bugfixer/);
+ expect(() => fromLiaison("run-1", true, () => true, { botId: "someone", threadId: "run-1" })).toThrow(/is not a thread you opened on Bugfixer/);
});
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 }))
+ expect(() => workThreadFor(anyOther, { botId: "bugfixer", botName: "Bugfixer", fromBotId: "liaison", fromThreadId: "liaison-chat", thread: "someone-elses", notice: true, answering: run1, 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("archived-run", true, () => true, { botId: "bugfixer", threadId: "archived-run" })).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 ed41acf919..5ad8c0d255 100644
--- a/server/host/work-thread.ts
+++ b/server/host/work-thread.ts
@@ -2,20 +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, or of the teammate's thread that handed the sender's current
- * thread its work: a requester that asked with reply none hears back only there, so it takes a one-way notice (`notice`),
- * never new work, and only from the thread it handed work to.
+ * of a thread the sender opened on that teammate earlier, or `answering`: the requester of the sender's running assignment
+ * when it asked with reply none. Nothing returns from such work on its own, so it takes a one-way notice (`notice`) there,
+ * never new work.
* Throws when it is neither. `also` narrows which threads count. */
export function workThreadFor(store: Pick<Store, "workThread" | "taskByThread">, input: { botId: string; botName: string; fromBotId: string; fromThreadId: string;
- thread: string; notice: boolean; also: (task: TaskRecord) => boolean }): TaskRecord | undefined {
+ thread: string; notice: boolean; answering: { botId: string; threadId: string } | undefined; 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);
const usable = task !== undefined && task.archivedAt === undefined && also(task);
if (usable && task.openedBy?.botId === input.fromBotId && !task.openedBy.roomLane) return task;
- const handedBy = store.taskByThread(input.fromBotId, input.fromThreadId)?.openedBy;
- const handedWork = usable && handedBy?.botId === botId && handedBy.threadId === thread && !handedBy.roomLane;
+ const handedWork = usable && input.answering?.botId === botId && input.answering.threadId === thread;
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 2feeebfb6e..c0c17eb987 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -13941,6 +13941,7 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {
// 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, notice: mode === "notify",
+ answering: coordinator.answersTo(internalCapability.roomHandoffId ?? generation),
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 1aa0bb7a20..2a8e34d180 100644
--- a/server/runtime-coordinator.test.ts
+++ b/server/runtime-coordinator.test.ts
@@ -347,10 +347,12 @@ describe("RuntimeCoordinator: refusals in the legacy words", () => {
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" } });
+ 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.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 154cadfa94..d396f832ec 100644
--- a/server/runtime-coordinator.ts
+++ b/server/runtime-coordinator.ts
@@ -164,6 +164,13 @@ export class RuntimeCoordinator implements Coordinator {
return a && this.project(a);
}
+ answersTo(id: string): { botId: string; threadId: string } | undefined {
+ this.ready();
+ const a = this.store.get(id);
+ const { botId, threadId } = a?.requester ?? {};
+ return a && botId && threadId && answersSender(a, "notify", { botId, threadId }) ? { botId, threadId } : undefined;
+ }
+
has(id: string): boolean {
this.ready();
return this.store.get(id) !== undefined;