Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Chief answers for the person: review fixes from #4394
Doğukaan Kılıçarslan916e2d21d6a1
diff --git a/server/host/teammate-prompts.test.ts b/server/host/teammate-prompts.test.tsindex 7b223d3a28..44cbbe0037 100644--- a/server/host/teammate-prompts.test.ts+++ b/server/host/teammate-prompts.test.ts@@ -2,7 +2,7 @@ import { describe, expect, it, vi } from "vitest";import type { BotRecord } from "../store.ts";import { LONG_WAIT_MS, waitedWords } from "./long-waits.ts";-import { createAsksActingPerson, createTeammatePrompts, type TeammatePromptDeps } from "./teammate-prompts.ts";+import { createAsksActingPerson, createAsksActingPerson, createTeammatePrompts, type TeammatePromptDeps } from "./teammate-prompts.ts";vi.mock("../peer-roster.ts", () => ({ canManage: () => true }));@@ -68,6 +68,18 @@ describe("what a Chief is told about its teammates' questions", () => {expect(questions(true)).toContain("Keep the old page?");});+ it("on a team, a question counts only for the person the Chief's turn acts for; a room's turn names them through its generation", () => {+ const asks = createAsksActingPerson({ team: true,+ requester: (threadId, generation) => (threadId === "room" ? (generation === "turn" ? "ada" : null) : threadId === "ada-chat" ? "ada" : null),+ askedPerson: (botId) => (botId === "mira" ? "ada" : botId === "fox" ? "bob" : null) });+ expect(asks("ada-chat")("t1", "mira")).toBe(true);+ expect(asks("ada-chat")("t1", "fox")).toBe(false);+ expect(asks("room", "turn")("t1", "mira")).toBe(true);+ expect(asks("room")("t1", "mira")).toBe(false);+ expect(asks("nobody")("t1", "stray")).toBe(false);+ expect(createAsksActingPerson({ team: false, requester: () => null, askedPerson: () => null })("nobody")("t1", "stray")).toBe(true);+ });+it("a teammate's words stay in the JSON list, never loose in the sentence that tells the Chief what to do", () => {const told = questions(true, NOW - 2 * LONG_WAIT_MS);expect(told.split("Keep the old page?")).toHaveLength(2);diff --git a/server/host/teammate-prompts.ts b/server/host/teammate-prompts.tsindex 27c4323b5b..37803bbaf1 100644--- a/server/host/teammate-prompts.ts+++ b/server/host/teammate-prompts.ts@@ -28,6 +28,20 @@ export interface TeammatePromptDeps {now?(): number;}+/** Security invariant: on a team a Chief answers a teammate's question only for the person it is for, and only in a turn that person drives.+ * A room's turn names its person only through its generation. */+export function createAsksActingPerson(deps: {+ team: boolean;+ requester(threadId: string, generation?: string): string | null;+ askedPerson(botId: string, threadId: string): string | null;+}) {+ return (chiefThreadId: string, generation?: string): ((threadId: string, botId: string) => boolean) => {+ if (!deps.team) return () => true;+ const person = deps.requester(chiefThreadId, generation);+ return (threadId, botId) => person !== null && deps.askedPerson(botId, threadId) === person;+ };+}+export function createTeammatePrompts(deps: TeammatePromptDeps) {const { store } = deps;diff --git a/server/index.ts b/server/index.tsindex 40f3d34b4f..5e6466cfb7 100644--- a/server/index.ts+++ b/server/index.ts@@ -296,7 +296,7 @@ import { createLiveApprovalMode } from "./host/live-approval.ts";import { CONSENT_RULE, escapeNotes, labelledLine, OUTSIDE, peerLineText, peerProvenanceNote, reportNote, requestWorkOrigin, type WorkOrigin } from "./peer-provenance.ts"; import { lineNote, PROVENANCE_PROMPT } from "./provenance-label.ts"; import { openRoomThread } from "./host/room-new-thread.ts"; import { createLineLabels } from "./host/line-labels.ts";import { decideRoomPost, emptyRoomPostBudget, type RoomPostAttempt, type RoomPostBudget } from "./room-post-budget.ts";import { messagePages, pageSize, slimMessage } from "./host/message-pages.ts";-import { createAsksActingPerson, createTeammatePrompts } from "./host/teammate-prompts.ts"; import { createMausBotOwnChat } from "./host/mausbot-own-chat.ts"; import { moveMemoryLayer } from "./memory-move.ts";+import { createAsksActingPerson, createAsksActingPerson, createTeammatePrompts } from "./host/teammate-prompts.ts"; import { createMausBotOwnChat } from "./host/mausbot-own-chat.ts"; import { moveMemoryLayer } from "./memory-move.ts";import { createQueuedReason } from "./host/queued-reason.ts";import { ReplayBuffer } from "./host/sse-replay.ts";import {@@ -3783,12 +3783,7 @@ const threadReadableFrom = (threadId: string) => (other: string): boolean => !prconst queuedDeps = { botName: (id: string) => store.bot(id)?.name, threadBusy, groupTurn: (id: string) => Boolean(activeGroupTurnForBot(id)), behindPerson: queuedBehindPerson,waitsForComputer: (node: HandoffView) => { const seat = parkedSeats.get(node.id); return Boolean(seat && !turnResources.free(seat)); },slots: (id: string) => ({ busy: store.tasks(id).filter((task) => threadBusy(id, task.threadId)).map(({ threadId, title }) => ({ threadId, title })), limit: maxConcurrentChildThreads(cfg, botThreadsHost(cloudPlanTier.starter())) }) }; const queuedStatus = createQueuedReason(queuedDeps), queuedNotes = createQueuedNotes({ ...queuedDeps, now: Date.now, language: (threadId) => threadLanguage(threadId), parent: (node) => node.parentId ? coordinator.node(node.parentId) : undefined, live: (id) => coordinator.node(id)?.status === "queued", post: (parent, node, line) => { const bot = store.bot(node.botId), title = store.taskByThread(node.botId, node.threadId)?.title; store.appendMessage(parent.threadId, { role: "bot", kind: "activity", ...(bot ? { from: { botId: bot.id, name: bot.name, color: bot.color } } : {}), tool: { name: line, ok: true }, ...(title ? { threadRef: { botId: node.botId, threadId: node.threadId, title } } : {}) }); } });-/** Security invariant: on a team a Chief answers a teammate's question only for the person it is for, and only in a turn that person drives. */-function asksActingPerson(chiefThreadId: string): (threadId: string, botId: string) => boolean {- if (!CLOUD_TEAM) return () => true;- const person = actingPeople.requester(chiefThreadId);- return (threadId, botId) => person !== null && teamOwners.routing.askedPerson(botId, threadId) === person;-}+const asksActingPerson = createAsksActingPerson({ team: Boolean(CLOUD_TEAM), requester: (threadId, generation) => actingPeople.requester(threadId, generation), askedPerson: (botId, threadId) => teamOwners.routing.askedPerson(botId, threadId) });const teammatePrompts = createTeammatePrompts({ store, asksActingPerson, outstandingDirect: (threadId) => coordinator.outstandingDirect(threadId), openPersonCard, queuedStatus, isUnattended, fullAccess: fullAccessForSource, startedOutside, guestDriven: cloudGuestDriven, confined: (threadId) => proactive.fence.confines(threadId), readableFrom: threadReadableFrom, waitingForPerson: (chief, teammates, readable) => questionRouting.waitingForPerson(chief, teammates, readable) });function queuedBehindPerson(node: HandoffView): { threadId: string; kind: PersonWaitKind; title: string } | undefined {@@ -13866,7 +13861,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);// A Chief answers, for the person, the question a teammate it manages waits on in a thread the Chief did not open.- const forPerson = EXTERNAL || room || fresh || call.answers || mode !== "work" || call.thread === "standing" || !teammatePrompts.actsForPerson(internalSender, internalCapability.threadId) || !canManage(internalSender, recipient) || !threadReadableFrom(internalCapability.threadId)(call.thread) || !asksActingPerson(internalCapability.threadId)(call.thread, recipient.id) ? undefined : await questionRouting.answerForPerson({ sender: internalSender, senderThreadId: address.threadId, target: recipient, threadId: call.thread, message: call.message });+ const forPerson = EXTERNAL || room || fresh || call.answers || mode !== "work" || call.thread === "standing" || !teammatePrompts.actsForPerson(internalSender, internalCapability.threadId) || !canManage(internalSender, recipient) || !threadReadableFrom(internalCapability.threadId)(call.thread) || !asksActingPerson(internalCapability.threadId, store.groupByThread(internalCapability.threadId) ? internalCapability.generation : undefined)(call.thread, recipient.id) ? undefined : await questionRouting.answerForPerson({ sender: internalSender, senderThreadId: address.threadId, target: recipient, threadId: call.thread, message: call.message });if (forPerson) { if (forPerson.outcome === "failed") refused.push(forPerson.detail); else answeredForPerson.add(forPerson); receipts.push(forPerson); continue; }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, reach: (task) => outsideLineage.mayReach(address.threadId, task.threadId) });diff --git a/server/teammate-follow-ups.e2e.test.ts b/server/teammate-follow-ups.e2e.test.tsindex aa033a29a5..1fc2b50a98 100644--- a/server/teammate-follow-ups.e2e.test.ts+++ b/server/teammate-follow-ups.e2e.test.ts@@ -343,6 +343,7 @@ it("P7 keeps Mira's question from a Clive turn a scheduled routine started: it iawait expect.poll(() => f.keys(f.clive.id), { timeout: 30_000 }).toEqual(["C-ROUTINE"]);expect(f.given("C-ROUTINE")).not.toContain("Teammates you manage wait on the person's answer");expect(f.replies("C-ROUTINE")).toEqual([`"${chat}" is not a thread you opened on Mira: use a threadId one of your coordinate_bots receipts gave, or one list_threads shows on them`]);+ expect(await asking(f, chat)).toBeDefined();expect((await asking(f, chat))?.answered).toBeUndefined();expect(f.children()).toEqual([]);}), hostTimeout(90_000));