Skip to content

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

Teammates: only a one-shot hand-off thread closes and is archived on its result

milind-sonie3a47f73e97f
diff --git a/packages/phone-protocol/src/task-provenance.ts b/packages/phone-protocol/src/task-provenance.ts
index f08c97af82..ae9f12842f 100644
--- a/packages/phone-protocol/src/task-provenance.ts
+++ b/packages/phone-protocol/src/task-provenance.ts
@@ -7,6 +7,9 @@ export interface TaskOpenedBy {
* durable conversation between two bots; "work" closes itself once its
* result has been reported. */
kind?: "pair" | "work";
+ /** coordinate_bots opened it with thread "new" for one hand-off: it closes, and leaves the sidebar, once its result is
+ * back (server/host/finished-work.ts). start_thread never sets it, and a post_to_thread into the thread takes it off. */
+ oneShot?: true;
/** The burst this thread is one part of: it is listed under that burst's card. */
burstId?: string;
/** The opener's own conversation this "work" thread was opened from.
diff --git a/server/coordinator.ts b/server/coordinator.ts
index 4b8c36b819..3571e80fcd 100644
--- a/server/coordinator.ts
+++ b/server/coordinator.ts
@@ -28,6 +28,8 @@ export type CoordinateBotsCall = {
files?: readonly PassedFile[];
/** start_thread's report_back: the one target is the sender's own new thread, run on this model. */
ownThread?: { modelSelection?: ModelSelection; approvalMode?: NewThreadLevel };
+ /** coordinate_bots thread "new": the thread it opens is this one hand-off's, so it closes once the result is back. */
+ oneShot?: true;
/** post_to_thread: one message into this existing thread; wake starts a run there, else it waits for the next one. */
post?: { deliveryId: string; wake: boolean };
};
diff --git a/server/direct-coordination.e2e.test.ts b/server/direct-coordination.e2e.test.ts
index 7abcf7575f..54ded22d5a 100644
--- a/server/direct-coordination.e2e.test.ts
+++ b/server/direct-coordination.e2e.test.ts
@@ -388,6 +388,7 @@ it("closes a thread \"new\" opened once its result is back, and opens it again f
const [child] = leadNodes();
const closed = await leadTask(child.threadId);
expect(closed.openedBy).not.toHaveProperty("kind");
+ expect(closed.openedBy.oneShot).toBe(true);
expect(closed.closedBy).toMatchObject({ botId: f.chief.id, name: "Clive" });
expect(closed.archivedAt).toBe(closed.closedBy.at);
expect((await f.messages(child.threadId)).map((message: any) => message.text).filter(Boolean).join("\n")).toContain("FRESH_RESULT verified");
diff --git a/server/host/finished-work.test.ts b/server/host/finished-work.test.ts
index bef69eb2fb..9afa2745b7 100644
--- a/server/host/finished-work.test.ts
+++ b/server/host/finished-work.test.ts
@@ -1,11 +1,13 @@
import { describe, expect, it } from "vitest";
import type { TaskRecord } from "../store.ts";
-import { archivesOnResult, closesOnResult, finishHandedThread, reopenHandedThread } from "./finished-work.ts";
+import { archivedOnResult, archivesOnResult, closesOnResult, finishHandedThread, keepHandedThread, reopenHandedThread } from "./finished-work.ts";
type Task = Parameters<typeof closesOnResult>[0];
const opened = (more: Partial<NonNullable<TaskRecord["openedBy"]>> = {}): NonNullable<TaskRecord["openedBy"]> =>
({ botId: "chief", name: "Clive", threadId: "chief-chat", at: 0, ...more });
+/** A thread coordinate_bots opened with thread "new" for one hand-off. */
+const oneShot = (more: Partial<NonNullable<TaskRecord["openedBy"]>> = {}) => opened({ oneShot: true, ...more });
const closes = (task: Task, opener = "chief", owner = "lead") => closesOnResult(task, opener, owner);
describe("closesOnResult", () => {
@@ -13,14 +15,18 @@ describe("closesOnResult", () => {
expect(closes({ openedBy: opened({ kind: "work" }) })).toBe(true);
});
- it("closes a thread a teammate opened with thread \"new\"", () => {
- expect(closes({ openedBy: opened() })).toBe(true);
+ it("closes a thread a teammate opened with thread \"new\" for one hand-off", () => {
+ expect(closes({ openedBy: oneShot() })).toBe(true);
+ });
+
+ it("keeps a thread start_thread opened on a teammate, or one a post_to_thread reached", () => {
+ expect(closes({ openedBy: opened() })).toBe(false);
});
it("keeps a thread \"new\" opened that was pinned, filed in a folder or written in by a person", () => {
- expect(closes({ openedBy: opened(), pinned: true })).toBe(false);
- expect(closes({ openedBy: opened(), projectId: "folder-1" })).toBe(false);
- expect(closes({ openedBy: opened(), askedAt: 1 })).toBe(false);
+ expect(closes({ openedBy: oneShot(), pinned: true })).toBe(false);
+ expect(closes({ openedBy: oneShot(), projectId: "folder-1" })).toBe(false);
+ expect(closes({ openedBy: oneShot(), askedAt: 1 })).toBe(false);
});
it("closes the standing work thread whether or not it was kept", () => {
@@ -28,33 +34,34 @@ describe("closesOnResult", () => {
});
it("keeps the pair conversation, a burst's part, a room lane and a thread a bot opened on itself", () => {
- expect(closes({ openedBy: opened({ kind: "pair" }) })).toBe(false);
- expect(closes({ openedBy: opened({ burstId: "burst-1" }) })).toBe(false);
- expect(closes({ openedBy: opened({ roomLane: true }) })).toBe(false);
- expect(closes({ openedBy: opened({ botId: "lead" }) }, "lead", "lead")).toBe(false);
+ expect(closes({ openedBy: oneShot({ kind: "pair" }) })).toBe(false);
+ expect(closes({ openedBy: oneShot({ burstId: "burst-1" }) })).toBe(false);
+ expect(closes({ openedBy: oneShot({ roomLane: true }) })).toBe(false);
+ expect(closes({ openedBy: oneShot({ botId: "lead" }) }, "lead", "lead")).toBe(false);
});
it("leaves a thread another bot opened, one already closed, and one nobody opened", () => {
- expect(closes({ openedBy: opened({ botId: "other" }) })).toBe(false);
+ expect(closes({ openedBy: oneShot({ botId: "other" }) })).toBe(false);
expect(closes({ openedBy: opened({ kind: "work", botId: "other" }) })).toBe(false);
- expect(closes({ openedBy: opened(), closedBy: { botId: "chief", name: "Clive", at: 1 } })).toBe(false);
+ expect(closes({ openedBy: oneShot(), closedBy: { botId: "chief", name: "Clive", at: 1 } })).toBe(false);
expect(closes({})).toBe(false);
expect(closes(undefined)).toBe(false);
});
});
describe("archivesOnResult", () => {
- it("archives a thread \"new\" opened, but not the standing work thread", () => {
- expect(archivesOnResult({ openedBy: opened() }, false)).toBe(true);
+ it("archives a one-shot thread, but not the standing work thread or a thread start_thread opened", () => {
+ expect(archivesOnResult({ openedBy: oneShot() }, false)).toBe(true);
expect(archivesOnResult({ openedBy: opened({ kind: "work" }) }, false)).toBe(false);
+ expect(archivesOnResult({ openedBy: opened() }, false)).toBe(false);
});
it("leaves an unread thread in the sidebar", () => {
- expect(archivesOnResult({ openedBy: opened(), unread: true }, false)).toBe(false);
+ expect(archivesOnResult({ openedBy: oneShot(), unread: true }, false)).toBe(false);
});
it("leaves a thread whose wait is still armed in the sidebar", () => {
- expect(archivesOnResult({ openedBy: opened() }, true)).toBe(false);
+ expect(archivesOnResult({ openedBy: oneShot() }, true)).toBe(false);
});
});
@@ -71,15 +78,15 @@ describe("finishHandedThread", () => {
};
};
- it("closes and archives a thread \"new\" opened at the same moment", () => {
- const store = fakeStore({ openedBy: opened() });
+ it("closes and archives a one-shot thread at the same moment", () => {
+ const store = fakeStore({ openedBy: oneShot() });
finishHandedThread(store, "lead", "lead-thread", "chief", () => false, 42);
expect(store.record.closedBy).toEqual({ botId: "chief", name: "Clive", at: 42 });
expect(store.record.archivedAt).toBe(42);
});
it("only closes the standing work thread and an unread thread", () => {
- for (const task of [{ openedBy: opened({ kind: "work" }) }, { openedBy: opened(), unread: true }]) {
+ for (const task of [{ openedBy: opened({ kind: "work" }) }, { openedBy: oneShot(), unread: true }]) {
const store = fakeStore(task);
finishHandedThread(store, "lead", "lead-thread", "chief", () => false, 42);
expect(store.calls).toEqual(["closed"]);
@@ -87,7 +94,7 @@ describe("finishHandedThread", () => {
});
it("neither closes nor archives a thread that stays open", () => {
- for (const task of [{ openedBy: opened(), pinned: true }, { openedBy: opened(), askedAt: 1 }, { openedBy: opened({ kind: "pair" as const }) }]) {
+ for (const task of [{ openedBy: oneShot(), pinned: true }, { openedBy: oneShot(), askedAt: 1 }, { openedBy: oneShot({ kind: "pair" as const }) }, { openedBy: opened() }]) {
const store = fakeStore(task);
finishHandedThread(store, "lead", "lead-thread", "chief", () => false, 42);
expect(store.calls).toEqual([]);
@@ -96,11 +103,35 @@ describe("finishHandedThread", () => {
});
describe("reopenHandedThread", () => {
- it("unarchives an archived thread and leaves an open one alone", () => {
+ const closedBy = { botId: "chief", name: "Clive", at: 5 };
+ it("takes a one-shot thread out of the archive its result put it in", () => {
const patches: unknown[] = [];
const store = { patchTask: (_botId: string, _threadId: string, patch: unknown) => { patches.push(patch); return null; } };
- reopenHandedThread(store, "lead", { threadId: "t", archivedAt: 5 });
- reopenHandedThread(store, "lead", { threadId: "t" });
+ reopenHandedThread(store, "lead", { threadId: "t", openedBy: oneShot(), closedBy, archivedAt: 5 });
+ reopenHandedThread(store, "lead", { threadId: "t", openedBy: oneShot() });
expect(patches).toEqual([{ archivedAt: undefined }]);
});
+
+ it("leaves a thread the person archived in the archive", () => {
+ expect(archivedOnResult({ openedBy: oneShot(), closedBy, archivedAt: 5 })).toBe(true);
+ expect(archivedOnResult({ openedBy: oneShot(), closedBy, archivedAt: 8 })).toBe(false);
+ expect(archivedOnResult({ openedBy: oneShot(), archivedAt: 5 })).toBe(false);
+ expect(archivedOnResult({ openedBy: oneShot(), closedBy: { ...closedBy, botId: "lead" }, archivedAt: 5 })).toBe(false);
+ expect(archivedOnResult({ openedBy: opened(), closedBy, archivedAt: 5 })).toBe(false);
+ });
+});
+
+describe("keepHandedThread", () => {
+ it("takes the one-shot mark off a thread a post_to_thread reached, and leaves other threads alone", () => {
+ const tasks: Record<string, Partial<TaskRecord>> = { shot: { openedBy: oneShot() }, plain: { openedBy: opened() } };
+ const written: unknown[] = [];
+ const store = {
+ taskByThread: (_botId: string, threadId: string) => tasks[threadId] as TaskRecord | undefined,
+ setTaskOpenedBy: (_botId: string, threadId: string, openedBy: TaskRecord["openedBy"]) => { written.push([threadId, openedBy]); return null; },
+ };
+ keepHandedThread(store, "lead", "shot");
+ keepHandedThread(store, "lead", "plain");
+ keepHandedThread(store, "lead", "gone");
+ expect(written).toEqual([["shot", opened()]]);
+ });
});
diff --git a/server/host/finished-work.ts b/server/host/finished-work.ts
index ec5eb76c99..5feffd3ea6 100644
--- a/server/host/finished-work.ts
+++ b/server/host/finished-work.ts
@@ -1,25 +1,34 @@
-// Work one bot hands a teammate is finished once its result is reported back: the thread it ran in closes, which folds
-// it out of the sidebar, and the next turn in it opens it again.
+// Work one bot hands a teammate is finished once its result is reported back. The standing work thread closes, which folds
+// it out of the sidebar. A thread coordinate_bots opened with thread "new" for that one hand-off (`openedBy.oneShot`) also
+// closes, and leaves the sidebar for the archive. The next turn in either opens it again. Every other thread stays as it is.
import type { Store, TaskRecord } from "../store.ts";
type FinishedTask = Pick<TaskRecord, "openedBy" | "closedBy" | "pinned" | "projectId" | "askedAt">;
/** Whether `task`, a thread of `ownerBotId` that `openerBotId` handed work to, closes now that the result is reported.
- * The standing work thread always does. A thread opened with coordinate_bots thread "new" does too, unless it was kept:
- * pinned, filed in a folder, or written in by a person. The pair conversation, a burst's part and a thread a bot opened on itself never do. */
+ * The standing work thread always does. A one-shot thread does too, unless it was kept: pinned, filed in a folder, or
+ * written in by a person. A thread start_thread opened, one a post_to_thread reached, the pair conversation, a burst's
+ * part, a room lane and a thread a bot opened on itself never do. */
export function closesOnResult(task: FinishedTask | undefined, openerBotId: string, ownerBotId: string): boolean {
const openedBy = task?.openedBy;
if (!task || !openedBy || task.closedBy || openedBy.botId !== openerBotId) return false;
if (openedBy.kind === "work") return true;
- return openedBy.kind === undefined && ownerBotId !== openerBotId && !openedBy.burstId && !openedBy.roomLane
+ return openedBy.oneShot === true && openedBy.kind === undefined && ownerBotId !== openerBotId && !openedBy.burstId && !openedBy.roomLane
&& task.pinned !== true && task.projectId === undefined && task.askedAt === undefined;
}
-/** Whether a thread that just closed on its result is also archived: one opened with thread "new" is, unless it is
- * unread or a wait it set is still armed, since that wait's message lands there later. The standing work thread is not,
- * so the opener's next work goes on in it. */
+/** Whether a thread that just closed on its result is also archived: a one-shot thread is, unless it is unread or a wait
+ * it set is still armed, since that wait's message lands there later. The standing work thread is not, so the opener's
+ * next work goes on in it. */
export function archivesOnResult(task: Pick<TaskRecord, "openedBy" | "unread">, waitArmed: boolean): boolean {
- return task.openedBy !== undefined && task.openedBy.kind === undefined && task.unread !== true && !waitArmed;
+ return task.openedBy?.oneShot === true && task.openedBy.kind === undefined && task.unread !== true && !waitArmed;
+}
+
+/** Whether a one-shot thread is in the archive because its result came back (`finishHandedThread` stamps the close and the
+ * archive at the same moment), not because the person archived it. Only such a thread may be taken out of the archive. */
+export function archivedOnResult(task: Pick<TaskRecord, "openedBy" | "closedBy" | "archivedAt">): boolean {
+ return task.openedBy?.oneShot === true && task.archivedAt !== undefined
+ && task.closedBy?.botId === task.openedBy.botId && task.closedBy.at === task.archivedAt;
}
type FinishStore = Pick<Store, "taskByThread" | "bot" | "setTaskClosedBy" | "patchTask">;
@@ -33,7 +42,16 @@ export function finishHandedThread(store: FinishStore, ownerBotId: string, threa
if (archivesOnResult(task, waitArmed(threadId))) store.patchTask(ownerBotId, threadId, { archivedAt: now });
}
-/** A thread the opener sends to again leaves the archive, so the person sees the work it runs. */
-export function reopenHandedThread(store: Pick<Store, "patchTask">, ownerBotId: string, task: Pick<TaskRecord, "threadId" | "archivedAt">): void {
- if (task.archivedAt !== undefined) store.patchTask(ownerBotId, task.threadId, { archivedAt: undefined });
+/** A one-shot thread its opener sends to again leaves the archive its result put it in, so the person sees the work it
+ * runs. A thread the person archived stays there. */
+export function reopenHandedThread(store: Pick<Store, "patchTask">, ownerBotId: string, task: Pick<TaskRecord, "threadId" | "openedBy" | "closedBy" | "archivedAt">): void {
+ if (archivedOnResult(task)) store.patchTask(ownerBotId, task.threadId, { archivedAt: undefined });
+}
+
+/** A post_to_thread reached this thread: bots go on writing in it, so it is no longer one hand-off's and stays open after a result. */
+export function keepHandedThread(store: Pick<Store, "taskByThread" | "setTaskOpenedBy">, ownerBotId: string, threadId: string): void {
+ const openedBy = store.taskByThread(ownerBotId, threadId)?.openedBy;
+ if (!openedBy?.oneShot) return;
+ const { oneShot: _oneShot, ...kept } = openedBy;
+ store.setTaskOpenedBy(ownerBotId, threadId, kept);
}
diff --git a/server/host/thread-posts.test.ts b/server/host/thread-posts.test.ts
index 911b0ce5ce..bafd38a99c 100644
--- a/server/host/thread-posts.test.ts
+++ b/server/host/thread-posts.test.ts
@@ -99,6 +99,16 @@ describe("thread posts", () => {
expect(calls).toHaveLength(3);
});
+ it("keeps the thread a delivered post reached, and not one a refused post named", async () => {
+ const { deps: d } = deps("sender-kept");
+ const kept: string[] = [];
+ d.kept = (botId, threadId) => { kept.push(`${botId}/${threadId}`); };
+ expect((await postToThread({ toThreadId: "t-1", message: "hi", deliveryId: "kept-1" }, d)).status).toBe(200);
+ d.coordinateBots = async () => ({ status: 403, body: { error: "not reachable" } });
+ expect((await postToThread({ toThreadId: "t-2", message: "hi", deliveryId: "kept-2" }, d)).status).toBe(403);
+ expect(kept).toEqual(["bot-b/t-1"]);
+ });
+
it("refuses a call made from a room", async () => {
const { calls, deps: d } = deps("sender-room", ["from-run"]);
expect((await postToThread({ toThreadId: "t-1", message: "hi" }, d)).status).toBe(409);
diff --git a/server/host/thread-posts.ts b/server/host/thread-posts.ts
index a07c260cc5..bbd78c2ee9 100644
--- a/server/host/thread-posts.ts
+++ b/server/host/thread-posts.ts
@@ -80,7 +80,9 @@ const pending = new Map<string, string>();
/** POST /api/internal/post-to-thread: checks the call and sends it through coordinate_bots as a post, so its reach rules hold. */
export async function postToThread(body: unknown, deps: { senderId: string; fromThreadId: string; waitsOn: (threadId: string) => boolean;
- store: Pick<Store, "botByThread" | "groupByThread" | "messagesFor">; coordinateBots: (call: CoordinateBotsCall) => Promise<Reply> }): Promise<Reply> {
+ store: Pick<Store, "botByThread" | "groupByThread" | "messagesFor">; coordinateBots: (call: CoordinateBotsCall) => Promise<Reply>;
+ /** A delivered post makes the thread one bots go on writing in, so it no longer closes on a hand-off's result. */
+ kept?: (botId: string, threadId: string) => void }): Promise<Reply> {
const refuse = (status: number, error: string): Reply => ({ status, body: { error } });
const parsed = PostBody.safeParse(body);
if (!parsed.success) return refuse(400, `Give thread_id (from list_threads) and a message of 1-${THREAD_POST_MAX_CHARS} characters; wake is true or false.`);
@@ -110,6 +112,7 @@ export async function postToThread(body: unknown, deps: { senderId: string; from
return refuse(result.status === 200 ? 409 : result.status, String(receipt?.detail ?? result.body.error ?? "Not delivered."));
}
deliveries.set(key, threadId);
+ deps.kept?.(owner.id, threadId);
if (deliveries.size > 1_000) deliveries.delete(deliveries.keys().next().value!);
return { status: 200, body: { deliveryId, threadId, botName: owner.name, woke: wake,
message: wake ? `Delivered; that thread runs it ${receipt.outcome === "injected" ? "in its running turn" : "next"}. Nothing comes back to you.` : "Posted; nothing started there. That thread reads it on its next run." } };
diff --git a/server/host/work-thread.test.ts b/server/host/work-thread.test.ts
index 109017bf0b..502ab99f2a 100644
--- a/server/host/work-thread.test.ts
+++ b/server/host/work-thread.test.ts
@@ -28,10 +28,16 @@ describe("workThreadFor", () => {
expect(fromLiaison("opened-by-liaison", false)?.threadId).toBe("opened-by-liaison");
});
- it("keeps a thread the sender opened after it was archived, so a follow-up or rework there reopens it", () => {
- const archived = { ...store, taskByThread: () => task("finished-new", { botId: "liaison", name: "Liaison", threadId: "liaison-chat", at: 0 }, { archivedAt: 9, closedBy: { botId: "liaison", name: "Liaison", at: 9 } }) };
- expect(workThreadFor(archived, { botId: "bugfixer", botName: "Bugfixer", fromBotId: "liaison", fromThreadId: "liaison-chat", thread: "finished-new",
- notice: false, answering: undefined, also: () => true, reach: () => true })?.threadId).toBe("finished-new");
+ it("keeps a one-shot thread the sender opened after its result archived it, so a follow-up or rework there reopens it", () => {
+ const archived = (more: Partial<TaskRecord>, oneShot = true) => workThreadFor({ ...store, taskByThread: () => task("finished-new",
+ { botId: "liaison", name: "Liaison", threadId: "liaison-chat", at: 0, ...(oneShot ? { oneShot: true as const } : {}) }, more) },
+ { botId: "bugfixer", botName: "Bugfixer", fromBotId: "liaison", fromThreadId: "liaison-chat", thread: "finished-new",
+ notice: false, answering: undefined, also: () => true, reach: () => true })?.threadId;
+ expect(archived({ archivedAt: 9, closedBy: { botId: "liaison", name: "Liaison", at: 9 } })).toBe("finished-new");
+ // The person archived it (another moment than the close), or it was never one hand-off's: it stays in the archive.
+ expect(() => archived({ archivedAt: 12, closedBy: { botId: "liaison", name: "Liaison", at: 9 } })).toThrow(/is not a thread you opened/);
+ expect(() => archived({ archivedAt: 9 })).toThrow(/is not a thread you opened/);
+ expect(() => archived({ archivedAt: 9, closedBy: { botId: "liaison", name: "Liaison", at: 9 } }, false)).toThrow(/is not a thread you opened/);
});
it("lets a one-way notice answer the thread that handed the sender work, and refuses new work there", () => {
diff --git a/server/host/work-thread.ts b/server/host/work-thread.ts
index 46a7c06a8d..5377927d23 100644
--- a/server/host/work-thread.ts
+++ b/server/host/work-thread.ts
@@ -1,9 +1,10 @@
import type { Store, TaskRecord } from "../store.ts";
+import { archivedOnResult } from "./finished-work.ts";
import { OUTSIDE_STAYS_HERE } from "./outside-lineage.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, archived or not, or `answering`: the requester of the sender's running assignment
+ * of a thread the sender opened on that teammate earlier (archived only by its own result), 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. */
@@ -20,11 +21,12 @@ export function workThreadFor(store: Pick<Store, "workThread" | "taskByThread">,
if (task && !reach(task)) throw new Error(OUTSIDE_STAYS_HERE);
const allowed = task !== undefined && also(task);
const usable = allowed && task.archivedAt === undefined;
+ const reusable = usable || (allowed && archivedOnResult(task));
if (input.any) {
if (usable && !task.closedBy) return task;
throw new Error(`"${thread}" is not an open thread of ${input.botName}: use a threadId list_threads shows on them`);
}
- if (allowed && task.openedBy?.botId === input.fromBotId && !task.openedBy.roomLane) return task;
+ if (reusable && task.openedBy?.botId === input.fromBotId && !task.openedBy.roomLane) return task;
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`);
diff --git a/server/index.ts b/server/index.ts
index 88216f32e7..4501d7641f 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -26,7 +26,7 @@ import { botMemoryFiles, createLendingMemory } from "./lending-memory.ts";
import { SharedComputerControl } from "./shared-computer-control.ts";
import { STATUS_REPLY_TIMEOUT_MS } from "./runtime/status-reply.ts"; import { createCoordinator, type ComposedTurn, type CoordinateBotsCall, type Coordinator, type HandoffOpener, type HandoffOptions, type HandoffView, type HostTurnResult } from "./coordinator.ts";
import { queuedBehindPersonText, turnWaitedOnPerson, UnansweredApprovals } from "./waiting-on-person.ts";
-import { createPersonWaits } from "./host/person-waits.ts"; import { askToolName, createAskCards } from "./host/ask-cards.ts"; import { createQuestionRouting } from "./host/question-routing.ts"; import { workThreadFor } from "./host/work-thread.ts"; import { finishHandedThread, reopenHandedThread } from "./host/finished-work.ts"; import { leaveThreadPost, postToThread, threadWaitsOn, unreadThreadPosts } from "./host/thread-posts.ts"; import { anyCardLive, closedByStop, resumableCards as cardsWaitedOn } from "./host/resumable-cards.ts"; import { createMcpChat } from "./host/mcp-chat.ts"; import { isCardOwnerLive, isMcpSignInCard } from "./host/mcp-sign-in-cards.ts"; import { createConnectCardSettle } from "./host/connect-card-settle.ts";
+import { createPersonWaits } from "./host/person-waits.ts"; import { askToolName, createAskCards } from "./host/ask-cards.ts"; import { createQuestionRouting } from "./host/question-routing.ts"; import { workThreadFor } from "./host/work-thread.ts"; import { finishHandedThread, keepHandedThread, reopenHandedThread } from "./host/finished-work.ts"; import { leaveThreadPost, postToThread, threadWaitsOn, unreadThreadPosts } from "./host/thread-posts.ts"; import { anyCardLive, closedByStop, resumableCards as cardsWaitedOn } from "./host/resumable-cards.ts"; import { createMcpChat } from "./host/mcp-chat.ts"; import { isCardOwnerLive, isMcpSignInCard } from "./host/mcp-sign-in-cards.ts"; import { createConnectCardSettle } from "./host/connect-card-settle.ts";
import { assertRequestTarget, guardedRequestPath, requestConflict, requestNeedsInput } from "./guarded-requests.ts";
import { botAvatarUrlFromStoredPath } from "../shared/bot-avatar.ts";
import { BOT_PROFILE_LIMITS } from "../shared/bot-profile.ts";
@@ -13569,7 +13569,7 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {
if (!room) {
const opened = { botId: internalSender.id, name: internalSender.name, threadId: address.threadId, at: Date.now(), ...(lends ? { forOwner: true as const } : {}) }, startedFor = (created: { threadId: string }) => threadStarters.set(created.threadId, opener), level = call.ownThread ? call.ownThread.approvalMode : EXTERNAL || mode === "consult" ? undefined : ownThreads.levelFor(recipient, undefined, address);
const task = continued ?? (fresh
- ? store.createTask(target.botId, call.title ?? (call.message.replace(/\s+/g, " ").trim().slice(0, 60) || `From ${internalSender.name}`), false, call.projectId, opened, level, call.ownThread?.modelSelection, folder.cwd, startedFor)
+ ? store.createTask(target.botId, call.title ?? (call.message.replace(/\s+/g, " ").trim().slice(0, 60) || `From ${internalSender.name}`), false, call.projectId, call.oneShot ? { ...opened, oneShot: true as const } : opened, level, call.ownThread?.modelSelection, folder.cwd, startedFor)
: store.createTask(target.botId, `@${internalSender.name} · work`, false, undefined, { ...opened, kind: "work" }, level, undefined, folder.cwd, startedFor));
if (!task) throw new Error("The recipient no longer exists");
target.threadId = task.threadId;
@@ -13700,9 +13700,9 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {
if (!parsed.success) return json(res, 400, { error: coordinateTooLong(parsed.error.issues) ?? `Provide 1-4 distinct botIds and a message of 1-${coordinateMessageMax} characters; thread is standing, new or the id of a thread you opened on that teammate, title one line of at most 80 characters, reply is resume or none, consult is a boolean, done_when at most 1000 characters, files at most 5 paths.` });
const { groupId, botIds, message, reason, rework, thread, title, reply, consult, inMyFolder, done_when: doneWhen, answers, files: paths } = parsed.data;
const callRun = activeRoutineRunForThread(internalCapability.threadId) ?? undefined, result = await handoffFiles.coordinate({ paths, answers, senderId: internalSender.id, recipients: botIds.length, saveOne: async (path) => saveTurnFile(internalCapability, internalSender.id, await attachmentVmForTurn(internalCapability), path, undefined, true),
- send: (files) => coordinateBots({ groupId, botIds, message, reason: reason || undefined, rework, thread, title: title || undefined, reply, consult, inMyFolder, doneWhen: doneWhen || undefined, ...(answers ? { answers } : {}), ...(files ? { files } : {}) }) }); refusedHandoffs.settled(internalCapability.threadId, internalSender, botIds, result.status, callRun);
+ send: (files) => coordinateBots({ groupId, botIds, message, reason: reason || undefined, rework, thread, title: title || undefined, reply, consult, inMyFolder, doneWhen: doneWhen || undefined, ...(answers ? { answers } : {}), ...(files ? { files } : {}), ...(thread === "new" ? { oneShot: true as const } : {}) }) }); refusedHandoffs.settled(internalCapability.threadId, internalSender, botIds, result.status, callRun);
return json(res, result.status, result.body);
- } if (method === "POST" && path === "/api/internal/post-to-thread") { const posted = await postToThread(await readInternalBody(), { senderId: internalSender.id, fromThreadId: internalCapability.threadId, store, coordinateBots, waitsOn: (threadId) => threadWaitsOn(coordinator, internalCapability.roomHandoffId ?? internalCapability.generation, threadId) }); return json(res, posted.status, posted.body); }
+ } if (method === "POST" && path === "/api/internal/post-to-thread") { const posted = await postToThread(await readInternalBody(), { senderId: internalSender.id, fromThreadId: internalCapability.threadId, store, coordinateBots, kept: (botId, threadId) => keepHandedThread(store, botId, threadId), waitsOn: (threadId) => threadWaitsOn(coordinator, internalCapability.roomHandoffId ?? internalCapability.generation, threadId) }); return json(res, posted.status, posted.body); }
if (method === "GET" && path === "/api/internal/room-targets") {
const source = store.groupByThread(internalCapability.threadId);
if (source && (source.dm || !source.memberIds.includes(internalSender.id))) {