Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { describe, expect, it, vi } from "vitest";
import { createCoordinator, type HandoffView, type HostTurnResult } from "../coordinator.ts";
import { handoffReply } from "../runtime-coordinator.ts";
import { privateHandBack } from "./acting-person.ts";
import type { Notification } from "../notify.ts";
import type { RequestAuth } from "../request-auth.ts";
import type { BotRecord, Message } from "../store.ts";
import { askToolName, CARD_SETTLE_MS, createAskCards, type AskCardsDeps } from "./ask-cards.ts";
const AUTH = { kind: "session", scopes: ["client"], session: { id: "s", email: "[email protected]" } } as unknown as RequestAuth;
const QUESTION = "What wording should the new footer text use?";
function world(options: { inboxThread?: string; live?: string[]; privateThreads?: string[] } = {}) {
const threads = new Map<string, Message[]>();
let ids = 0;
const bots = [
{ id: "scout", name: "Scout", color: "orange", tasks: [{ threadId: "inbox", title: "Inbox" }] },
{ id: "ledger", name: "Ledger", color: "teal", tasks: [{ threadId: "work", title: "Pricing page: fix" }] },
] as unknown as BotRecord[];
const rows = (threadId: string) => threads.get(threadId) ?? [];
const store = {
bot: (id: string) => bots.find((bot) => bot.id === id),
messagesFor: rows,
activePath: rows,
appendMessage: (threadId: string, message: Omit<Message, "id" | "at">) => {
const full = { id: `m${++ids}`, at: 1_000 + ids, ...message } as Message;
threads.set(threadId, [...rows(threadId), full]);
return full;
},
patchMessage: (threadId: string, id: string, patch: Partial<Message>) => {
const list = rows(threadId);
const at = list.findIndex((message) => message.id === id);
list[at] = { ...list[at]!, ...patch };
return list[at]!;
},
taskByThread: (botId: string, threadId: string) => (bots.find((bot) => bot.id === botId)?.tasks ?? []).find((task) => task.threadId === threadId),
patchTask: () => null,
group: () => undefined,
groupTaskByThread: () => undefined,
patchGroup: () => null,
groupByThread: (threadId: string) => (threadId === "room" ? { id: "room" } : undefined),
};
const nodes = new Map<string, HandoffView>();
const answered: Array<{ id: string; text: string }> = [];
const notified: Notification[] = [];
const answers: Array<{ threadId: string; requestId: string; message: string }> = [];
const live = new Set(options.live ?? []);
let refuse: { status: number; error: string } | null = null;
let afterAnswer: (() => void) | null = null;
let clock = 0;
const deps: AskCardsDeps = {
store: store as unknown as AskCardsDeps["store"],
node: (id) => nodes.get(id),
runningIn: (threadId) => [...nodes.values()].filter((node) => node.threadId === threadId && node.status === "running"),
delegatorOf: (threadId) => (threadId === "work" ? { threadId: "inbox", botId: "scout" } : undefined),
notify: (notification) => { if (notification) notified.push(notification); },
copiedInto: (inboxThreadId) => inboxThreadId === options.inboxThread,
keptPrivate: (threadId) => (options.privateThreads ?? []).includes(threadId),
live: (_threadId, requestId) => live.has(requestId),
answerCard: vi.fn(async (input) => {
if (refuse) return { ok: false as const, ...refuse };
answers.push({ threadId: input.threadId, requestId: input.requestId, message: input.message });
live.delete(input.requestId);
afterAnswer?.();
return { ok: true as const };
}),
answeredIn: (threadId, requestId) => (threadId === "inbox" && requestId.startsWith("req") ? "work" : threadId),
now: () => clock,
answerNode: (id, text) => {
const parked = nodes.get(id);
if (!parked || parked.status !== "waiting") return false;
answered.push({ id, text });
nodes.set(id, { ...parked, status: "queued", question: undefined, answer: text } as HandoffView);
return true;
},
};
const cards = createAskCards(deps);
const node = (over: Partial<HandoffView> = {}): HandoffView => {
const made = { id: "as_1", rootId: "root", parentId: "root", threadId: "work", botId: "ledger", key: "ledger", text: "Fix it", createdAt: 1_000,
startedAt: 1_000, status: "waiting", result: "", reported: false, executions: 1, lastProgressAt: 1_000, kind: "work",
restarts: 0, mode: "work", reply: "resume", ...over } as HandoffView;
nodes.set(made.id, made);
return made;
};
nodes.set("root", { id: "root", rootId: "root", threadId: "inbox", botId: "scout" } as HandoffView);
const say = (threadId: string, text: string) => store.appendMessage(threadId, { role: "bot", kind: "text", text });
return { cards, store, rows, notified, answers, answered, node, say, deps, open: (requestId: string) => live.add(requestId),
refuse: (value: typeof refuse) => { refuse = value; }, afterAnswer: (value: typeof afterAnswer) => { afterAnswer = value; },
/** Moves the clock past every card shown so far, as when the person reads a card before typing. */
settle: () => { clock = Math.max(clock, ...[...threads.values()].flat().map((row) => row.at)) + CARD_SETTLE_MS; } };
}
describe("a teammate's NEEDS INPUT question", () => {
it("becomes a question card in its conversation, its line keeps the words before it, and the requester's chat gets one chip", () => {
const w = world();
w.say("work", `The footer text still uses the old template.\nNEEDS INPUT: ${QUESTION}`);
w.cards.park(w.node(), QUESTION);
const [line, card] = w.rows("work");
expect(line).toMatchObject({ kind: "text", text: "The footer text still uses the old template." });
expect(card).toMatchObject({ role: "bot", kind: "options", text: QUESTION,
card: { subtitle: QUESTION, options: [], requestId: "needs-input-as_1-1", requestType: "question" } });
expect(w.rows("inbox").map((row) => row.tool?.name)).toEqual([`Ledger needs your input: ${QUESTION}`]);
expect(w.notified.map((notification) => [notification.kind, notification.threadId])).toEqual([["question", "work"]]);
});
it("keeps the localized chip's question identical to its English fallback, without the default answer", () => {
const w = world();
w.cards.park(w.node(), "CSV or Excel?\nDefault: CSV");
expect(w.rows("inbox")[0]?.tool).toMatchObject({
name: "Ledger needs your input: CSV or Excel?",
nameCode: "chat.notice.teammateNeedsInput",
nameParams: { name: "Ledger", question: "CSV or Excel?" },
});
});
it("turns a reply that is only the question into the card itself", () => {
const w = world();
w.say("work", `NEEDS INPUT: ${QUESTION}`);
w.cards.park(w.node(), QUESTION);
expect(w.rows("work")).toHaveLength(1);
expect(w.rows("work")[0]).toMatchObject({ kind: "options", text: QUESTION, card: { requestId: "needs-input-as_1-1" } });
});
it("leaves the person's inbox to the card's copy: no chip there", () => {
const w = world({ inboxThread: "inbox" });
w.say("work", `Checked.\nNEEDS INPUT: ${QUESTION}`);
w.cards.park(w.node(), QUESTION);
expect(w.rows("inbox")).toEqual([]);
expect(w.notified.map((notification) => notification.kind)).toEqual(["question"]);
});
it("shows it once across a restart, and still once when the restart came before the first showing", () => {
const w = world();
w.say("work", `Checked.\nNEEDS INPUT: ${QUESTION}`);
const parked = w.node();
w.cards.park(parked, QUESTION);
w.cards.park(parked, QUESTION, true);
expect(w.rows("work").filter((row) => row.card)).toHaveLength(1);
expect(w.rows("inbox")).toHaveLength(1);
expect(w.notified).toHaveLength(1);
const fresh = world();
fresh.say("work", `Checked.\nNEEDS INPUT: ${QUESTION}`);
fresh.cards.park(fresh.node(), QUESTION, true);
expect(fresh.rows("work").filter((row) => row.card)).toHaveLength(1);
expect(fresh.notified).toHaveLength(1);
});
it("asks again with a new card after an answer, on the node's next execution", () => {
const w = world();
w.say("work", `NEEDS INPUT: ${QUESTION}`);
w.cards.park(w.node(), QUESTION);
w.cards.answeredInWords("work", "Short and friendly");
w.say("work", "NEEDS INPUT: With a logo?");
w.cards.park(w.node({ executions: 2 }), "With a logo?");
expect(w.rows("work").map((row) => row.card?.requestId)).toEqual(["needs-input-as_1-1", "needs-input-as_1-2"]);
});
it("asks a room member's question with the same card, in place of the member's own line, and answers the waiting work from it", () => {
const room = world();
room.store.appendMessage("work", { role: "bot", kind: "text", text: "My part is done.", from: { botId: "scout", name: "Scout", color: "orange" } });
room.store.appendMessage("work", { role: "bot", kind: "text", text: `Checked.\nNEEDS INPUT: ${QUESTION}`, from: { botId: "ledger", name: "Ledger", color: "teal" } });
const member = room.node({ groupId: "room", question: QUESTION });
room.cards.park(member, QUESTION);
const [other, line, card] = room.rows("work");
expect(other!.text).toBe("My part is done.");
expect(line).toMatchObject({ text: "Checked.", from: { botId: "ledger" } });
expect(card).toMatchObject({ kind: "options", from: { botId: "ledger" }, card: { subtitle: QUESTION, requestId: "needs-input-as_1-1" } });
expect(room.notified.map((notification) => notification.kind)).toEqual(["question"]);
expect(room.cards.answerParked("work", "needs-input-as_1-1", "Short and friendly")).toBe(true);
expect(room.answered).toEqual([{ id: "as_1", text: "Short and friendly" }]);
expect(room.rows("work").find((row) => row.card)!.card).toMatchObject({ answered: "answer", answeredText: "Short and friendly" });
expect(room.cards.answerParked("work", "needs-input-as_1-1", "once more")).toBe(false);
});
it("keeps a parked card's chip, and leaves telling the person to the card itself", () => {
const card = world({ inboxThread: "inbox" });
card.cards.park(card.node({ cards: ["m9"] }), "Waiting on the person: connect Gmail");
expect(card.rows("inbox").map((row) => row.tool?.name)).toEqual(["Ledger needs your input: Waiting on the person: connect Gmail"]);
expect(card.notified).toEqual([]);
});
});
describe("at Full access a teammate's question goes to its requester", () => {
it("shows who decides, in place of the line, with no chip and no notification, and then what was picked and by whom", () => {
const w = world({ inboxThread: "inbox" });
w.say("work", `Checked.\nNEEDS INPUT: CSV or Excel?\nDefault: CSV`);
const node = w.node({ decide: { default: "CSV" }, question: "CSV or Excel?\nDefault: CSV" });
w.cards.park(node, "CSV or Excel?\nDefault: CSV");
const card = () => w.rows("work").find((row) => row.card)!.card!;
expect(w.rows("work")[0]!.text).toBe("Checked.");
expect(card()).toMatchObject({ subtitle: "CSV or Excel?", options: [], answered: "answer", answeredBy: { kind: "bot", name: "Scout" } });
expect(card().answeredText).toBeUndefined();
expect(w.rows("inbox")).toEqual([]);
expect(w.notified).toEqual([]);
w.cards.park(node, "CSV or Excel?\nDefault: CSV", true);
expect(w.rows("work").filter((row) => row.card)).toHaveLength(1);
w.cards.decided({ ...node, status: "queued", answer: "CSV", answeredBy: { kind: "default" } }, "Ledger");
expect(card()).toMatchObject({ answered: "answer", answeredText: "CSV", answeredBy: { kind: "bot", name: "Ledger" } });
w.cards.closed({ ...node, status: "completed", outcome: "done" });
expect(card(), "a decided question stays decided").toMatchObject({ answered: "answer", answeredText: "CSV" });
});
it("closes a question its requester was still deciding when the work ends without it", () => {
const w = world();
w.say("work", "NEEDS INPUT: CSV or Excel?");
const node = w.node({ decide: {} });
w.cards.park(node, "CSV or Excel?");
w.cards.closed({ ...node, status: "cancelled", outcome: "stopped" });
const card = w.rows("work").find((row) => row.card)!.card!;
expect(card).toMatchObject({ answered: "unavailable", dismissed: true, stopped: true });
expect(card.answeredBy).toBeUndefined();
});
it("offers the default a person's question names as a one-tap choice", () => {
const w = world();
w.say("work", "NEEDS INPUT: Which invoice?\nDefault: the October one");
w.cards.park(w.node(), "Which invoice?\nDefault: the October one");
expect(w.rows("work")[0]!.card).toMatchObject({ subtitle: "Which invoice?", options: ["the October one"] });
expect(w.notified[0]!.body).toBe("Which invoice?");
});
});
describe("nothing stale waits on the person", () => {
const ask = (w: ReturnType<typeof world>, threadId: string, requestId: string, at?: { from?: Message["from"] }) =>
w.store.appendMessage(threadId, { role: "bot", kind: "options", text: "Should I?", ...(at?.from ? { from: at.from } : {}),
card: { title: "Your bot has a question", subtitle: "Should I?", options: ["Yes"], requestId, requestType: "question" } });
it("a parked card's chip is not posted again after a restart, even when other lines came after it", () => {
const w = world();
const node = w.node({ cards: ["m9"] });
w.cards.park(node, "Waiting on the person: sign in to notion.so");
w.say("inbox", "Scout: something else");
w.cards.park(node, "Waiting on the person: sign in to notion.so", true);
expect(w.rows("inbox").filter((row) => row.tool?.name.includes("needs your input"))).toHaveLength(1);
expect(w.notified).toEqual([]);
});
it("a newer question closes the bot's older one that no turn waits on any more, but not one a turn still waits on", () => {
const w = world({ live: ["req-2", "req-3"] });
ask(w, "work", "req-1");
ask(w, "work", "req-2");
ask(w, "work", "req-3");
w.cards.askedAgain("work", "req-3", "ledger");
expect(w.rows("work").map((row) => row.card!.answered ?? null)).toEqual(["unavailable", null, null]);
});
it("a bot's own reply that ends on NEEDS INPUT becomes the question card, once", () => {
const w = world();
w.store.appendMessage("chat", { role: "bot", kind: "text", text: "I have two drafts.\nNEEDS INPUT: Which one do you like?", turnId: "t1" });
w.cards.ownQuestion("ledger", "chat", "t1");
w.cards.ownQuestion("ledger", "chat", "t1");
expect(w.rows("chat").map((row) => [row.kind, row.text])).toEqual([["text", "I have two drafts."], ["options", "Which one do you like?"]]);
expect(w.rows("chat")[1]!.card).toMatchObject({ subtitle: "Which one do you like?", requestType: "question", options: [] });
});
});
describe("a handed-on turn owns its question", () => {
it.each([0, 20, 11_995, 12_001])("keeps one answerable card after a reply-none requester finishes, with %i leading characters", async (prefixLength) => {
const prefix = prefixLength ? `${"x".repeat(prefixLength)}\n` : "";
const w = world();
let markFirstSettled!: () => void;
let markCompleted!: () => void;
const firstSettled = new Promise<void>((resolve) => { markFirstSettled = resolve; });
const completed = new Promise<void>((resolve) => { markCompleted = resolve; });
const parents: Array<HandoffView["status"] | undefined> = [];
const assigners: Array<string | undefined> = [];
const turns: string[] = [];
const coordinator = createCoordinator({ dataDir: ":memory:", now: () => 1_000, host: {
validate: () => undefined, busy: () => false, chiefOf: () => false,
levelOf: () => ({ level: "ask" }), names: () => ({ assignee: "Ledger", requester: "Scout" }),
changed: () => {},
report: (node) => { if (node.parentId && node.status === "completed") { markFirstSettled(); markCompleted(); } },
park: (node, question) => { w.cards.park(node, question); markFirstSettled(); },
run: async (node, _resumed, _signal, composed) => {
turns.push(composed.turn);
if (composed.turn === "answered") return { ok: true, text: "Used the chosen wording." };
parents.push(node.parentId ? coordinator.node(node.parentId)?.status : undefined);
assigners.push(coordinator.assignerOf(node.threadId)?.id);
const text = `${prefix}NEEDS INPUT: ${QUESTION}`;
w.store.appendMessage(node.threadId, { role: "bot", kind: "text", text, turnId: "handed-turn" });
if (!coordinator.assignerOf(node.threadId)) w.cards.ownQuestion(node.botId, node.threadId, "handed-turn");
return { ok: true, ...handoffReply(text) };
},
} });
w.deps.node = (id) => coordinator.node(id);
w.deps.runningIn = (threadId) => coordinator.runningIn(threadId);
w.deps.answerNode = (id, text) => coordinator.answerNode(id, text);
try {
const source = { botId: "scout", threadId: "inbox" };
const root = coordinator.beginTurn({ id: "requester-turn", address: source, brief: "Prepare a footer", level: "ask" });
const child = coordinator.enqueue(source, root.id, undefined, { botId: "ledger", threadId: "work" }, "Prepare a footer", { reply: "none" }).node;
coordinator.endTurn(root.id, { ok: true, text: "The teammate has the work." });
coordinator.tick();
await firstSettled;
expect(parents).toEqual(["completed"]);
expect(assigners).toEqual([undefined]);
expect(coordinator.node(child.id)?.status).toBe("waiting");
const requestId = `needs-input-${child.id}-1`;
expect(w.rows("work").filter((row) => row.card).map((row) => row.card!.requestId)).toEqual([requestId]);
expect(w.rows("work").find((row) => row.card)?.text).toBe(QUESTION);
expect(w.rows("work").find((row) => row.card)?.card?.subtitle).toBe(QUESTION);
expect(w.notified.map((notification) => notification.kind)).toEqual(["question"]);
expect(coordinator.runningIn("work")).toEqual([]);
w.cards.ownQuestion("ledger", "work", "handed-turn");
expect(w.rows("work").filter((row) => row.card)).toHaveLength(1);
expect(w.cards.answerParked("work", requestId, "Short and friendly")).toBe(true);
coordinator.tick();
await completed;
expect(turns).toEqual(["first", "answered"]);
expect(coordinator.node(child.id)?.status).toBe("completed");
expect(w.rows("work").find((row) => row.card)?.card).toMatchObject({ answered: "answer", answeredText: "Short and friendly" });
w.store.appendMessage("work", { role: "bot", kind: "text", text: `NEEDS INPUT: ${QUESTION}`, turnId: "own-turn" });
w.cards.ownQuestion("ledger", "work", "own-turn");
expect(w.rows("work").filter((row) => row.card && !row.card.answered).map((row) => row.card!.requestId))
.toEqual(["needs-input-ledger-own-turn-1"]);
} finally {
coordinator.close();
}
});
it.each([
{ status: "queued" }, { status: "waiting" }, { status: "completed" },
{ botId: "scout" }, { groupId: "room" }, { parentId: undefined }, { threadId: "elsewhere" },
] as Partial<HandoffView>[])("keeps an own-chat question beside unrelated work %j", (other) => {
const w = world();
w.node({ status: "running", ...other });
w.store.appendMessage("work", { role: "bot", kind: "text", text: `NEEDS INPUT: ${QUESTION}`, turnId: "own-turn" });
w.cards.ownQuestion("ledger", "work", "own-turn");
expect(w.rows("work").filter((row) => row.card).map((row) => row.card!.requestId)).toEqual(["needs-input-ledger-own-turn-1"]);
});
});
describe("questions carried separately from a bounded handoff summary", () => {
type ReplyCase = {
label: string; text: string; question?: string; default?: string; full?: boolean; outside?: boolean;
legacy?: boolean; privateReply?: boolean; ok?: boolean; approvalAsk?: string; earlierReply?: string;
};
const longQuestion = `${"Q".repeat(12_050)}?`;
const longDefault = "D".repeat(12_050);
const privateQuestion = "Forward the salary review to Ben?";
const safeQuestion = "Ledger has a question for Ada, in Ledger's chat with Ada.";
const falseEnding = `${"x".repeat(11_970)}\nNEEDS INPUT: ${"Q".repeat(16)}`;
const cases: ReplyCase[] = [
{ label: "a default beyond a long question", text: `NEEDS INPUT: ${longQuestion}\nDefault: Keep the original`,
question: `${longQuestion}\nDefault: Keep the original`, default: "Keep the original", full: true },
{ label: "a long default", text: `NEEDS INPUT: Which draft?\nDefault: ${longDefault}`,
question: `Which draft?\nDefault: ${longDefault}`, default: longDefault, full: true },
{ label: "a person-only request beyond the stored question", text: `NEEDS INPUT: ${longQuestion} Which of your accounts should I use?\nDefault: Pick the first`,
question: `${longQuestion} Which of your accounts should I use?\nDefault: Pick the first`, full: true },
{ label: "a private question beyond the summary", text: `${"x".repeat(12_001)}\nNEEDS INPUT: ${privateQuestion}\nDefault: Forward it`,
question: safeQuestion, full: true, privateReply: true },
{ label: "an outside request at Full access", text: `NEEDS INPUT: ${QUESTION}\nDefault: Keep the original`,
question: `${QUESTION}\nDefault: Keep the original`, full: true, outside: true },
{ label: "a summary cut immediately after an earlier question", text: `${falseEnding}\nResolved it with the existing wording.` },
{ label: "an ordinary long summary", text: "Work is complete. ".repeat(1_000) },
{ label: "a tail that explicitly asks nothing", text: `${"x".repeat(12_001)}\nNEEDS INPUT: none` },
{ label: "a host that still returns only text", text: `NEEDS INPUT: ${QUESTION}`, question: QUESTION, legacy: true },
{ label: "an unanswered approval on a failed turn", text: "The command did not run.", ok: false,
approvalAsk: "Approve the pending command?", question: "Approve the pending command?" },
{ label: "a failed turn with a textual question", text: `NEEDS INPUT: ${QUESTION}`, ok: false },
...[true, false].flatMap((ok) => [
{ label: `an approval after an earlier question (ok=${ok})`, text: "The command did not run.", ok,
earlierReply: `NEEDS INPUT: ${privateQuestion}\nDefault: Forward it`,
approvalAsk: "Approve the pending command?\nDefault: Wait", question: "Approve the pending command?\nDefault: Wait" },
{ label: `a private approval after an earlier question (ok=${ok})`, text: "The command did not run.", ok, privateReply: true,
earlierReply: `NEEDS INPUT: ${privateQuestion}\nDefault: Forward it`,
approvalAsk: "Approve the pending command?\nDefault: Wait", question: safeQuestion },
]),
];
it.each(cases)("preserves routing for $label", async (fixture) => {
const w = world({ privateThreads: fixture.privateReply ? ["work"] : [] });
let markSettled!: () => void;
const settled = new Promise<void>((resolve) => { markSettled = resolve; });
const parked: Array<{ node: HandoffView; question: string }> = [];
let sent!: HostTurnResult;
const coordinator = createCoordinator({ dataDir: ":memory:", now: () => 1_000, host: {
validate: () => undefined, busy: () => false, chiefOf: () => false,
levelOf: () => ({ level: fixture.full ? "full" : "ask" }), names: () => ({ assignee: "Ledger", requester: "Scout" }),
changed: () => {}, report: (node) => { if (node.parentId) markSettled(); },
park: (node, question) => { parked.push({ node, question }); w.cards.park(node, question); markSettled(); },
run: async (node) => {
const outcome: HostTurnResult = { ok: fixture.ok ?? true, ...(fixture.legacy ? { text: fixture.text } : handoffReply(fixture.text)),
...(fixture.approvalAsk ? { approvalAsk: fixture.approvalAsk } : {}) };
sent = fixture.privateReply ? privateHandBack(outcome, { child: "ada", parent: null, bot: "Ledger", person: "Ada" }) : outcome;
if (fixture.earlierReply) w.store.appendMessage(node.threadId, { role: "bot", kind: "text", text: fixture.earlierReply, turnId: "handed-turn" });
w.store.appendMessage(node.threadId, { role: "bot", kind: "text", text: fixture.text, turnId: "handed-turn" });
if (sent.ok && !coordinator.assignerOf(node.threadId)) w.cards.ownQuestion(node.botId, node.threadId, "handed-turn");
return sent;
},
} });
w.deps.node = (id) => coordinator.node(id);
w.deps.runningIn = (threadId) => coordinator.runningIn(threadId);
w.deps.answerNode = (id, text) => coordinator.answerNode(id, text);
try {
const source = { botId: "scout", threadId: "inbox" };
const root = coordinator.beginTurn({ id: "requester-turn", address: source, brief: "Prepare a footer", level: fixture.full ? "full" : "ask",
origin: fixture.outside ? { kind: "outside" } : { kind: "user", request: "Prepare a footer" } });
const child = coordinator.enqueue(source, root.id, undefined, { botId: "ledger", threadId: "work" }, "Prepare a footer", { reply: "none" }).node;
coordinator.endTurn(root.id, { ok: true, text: "The teammate has the work." });
coordinator.tick();
await settled;
const cards = w.rows("work").filter((row) => row.card);
expect(sent.text.length).toBeLessThanOrEqual(12_000);
if (!fixture.privateReply) expect(sent.text).toBe(fixture.text.slice(0, 12_000));
if (fixture.question === undefined) {
expect(parked).toEqual([]);
expect(cards).toEqual([]);
expect(coordinator.node(child.id)?.status).toBe(fixture.ok === false ? "failed" : "completed");
} else {
expect(parked).toHaveLength(1);
expect(cards).toHaveLength(1);
expect(parked[0]!.question).toBe(fixture.question.slice(0, 12_000));
expect(parked[0]!.node.decide).toEqual(fixture.default ? { default: fixture.default.slice(0, 12_000) } : undefined);
expect(coordinator.node(child.id)?.status).toBe(fixture.default ? "queued" : "waiting");
if (fixture.default) expect(coordinator.node(child.id)?.answer).toBe(fixture.default.slice(0, 12_000));
}
if (fixture.earlierReply) {
expect(w.rows("work").slice(0, 2).map((row) => ({ kind: row.kind, text: row.text, card: row.card }))).toEqual([
{ kind: "text", text: fixture.earlierReply, card: undefined },
{ kind: "text", text: fixture.text, card: undefined },
]);
if (!fixture.privateReply) expect(cards[0]).toMatchObject({
text: "Approve the pending command?", card: { subtitle: "Approve the pending command?", options: ["Wait"] },
});
}
if (fixture.privateReply) {
expect(sent.question).toBe(fixture.approvalAsk ? null : safeQuestion);
expect(sent.privateQuestion).toBe(true);
expect(JSON.stringify(sent)).not.toContain("salary");
expect(cards[0]?.text).toBe(fixture.approvalAsk ? safeQuestion : privateQuestion);
expect(cards[0]?.card?.subtitle).toBe(fixture.approvalAsk ? safeQuestion : privateQuestion);
expect(cards[0]?.card?.options).toEqual(fixture.approvalAsk ? [] : ["Forward it"]);
expect(JSON.stringify(coordinator.node(child.id))).not.toContain("salary");
expect(JSON.stringify(w.rows("inbox"))).not.toContain("salary");
expect(JSON.stringify(w.notified)).not.toContain("salary");
expect(w.cards.answerParked("work", cards[0]!.card!.requestId!, "Keep it private")).toBe(true);
expect(coordinator.node(child.id)?.answer).toBe("Keep it private");
}
} finally {
coordinator.close();
}
});
it.each([
{ label: "an earlier execution", startedAt: 2_000 },
{ label: "a shared conversation", shared: true },
{ label: "a room", groupId: "room" },
] as Array<{ label: string; startedAt?: number; shared?: boolean; groupId?: string }>)("keeps the supplied card question for $label", (fixture) => {
const threadId = fixture.groupId ? "room" : "work";
const w = world({ privateThreads: fixture.shared ? [] : [threadId] });
w.store.appendMessage(threadId, { role: "bot", kind: "text", text: `NEEDS INPUT: ${QUESTION}`,
...(fixture.groupId ? { from: { botId: "ledger", name: "Ledger", color: "teal" } } : {}) });
const node = w.node({ threadId, ...(fixture.startedAt ? { startedAt: fixture.startedAt } : {}),
...(fixture.groupId ? { groupId: fixture.groupId } : {}) });
w.cards.park(node, safeQuestion);
expect(w.rows(threadId).find((row) => row.card)?.card?.subtitle).toBe(safeQuestion);
});
});
describe("how an open question card settles", () => {
const parked = () => {
const w = world();
w.say("work", `Checked.\nNEEDS INPUT: ${QUESTION}`);
const node = w.node();
w.cards.park(node, QUESTION);
const card = () => w.rows("work").find((row) => row.card)!.card!;
return { w, node, card };
};
it("takes the person's own words as the answer", () => {
const { w, card } = parked();
w.cards.answeredInWords("work", "Short and friendly");
expect(card()).toMatchObject({ answered: "answer", answeredText: "Short and friendly" });
});
it("says Stop ended it, or that the wait ran out, and leaves an answered or finished one alone", () => {
const stopped = parked();
stopped.w.cards.closed({ ...stopped.node, status: "cancelled", outcome: "stopped" });
expect(stopped.card()).toMatchObject({ answered: "unavailable", dismissed: true, stopped: true });
const expired = parked();
expired.w.cards.closed({ ...expired.node, status: "failed", outcome: "expired" });
expect(expired.card()).toMatchObject({ expired: true });
expect(expired.card().answered).toBeUndefined();
const failed = parked();
failed.w.cards.closed({ ...failed.node, status: "failed", outcome: "refused" });
expect(failed.card()).toMatchObject({ answered: "unavailable", dismissed: true });
expect(failed.card().stopped).toBeUndefined();
const done = parked();
done.w.cards.answeredInWords("work", "Yes");
done.w.cards.closed({ ...done.node, status: "cancelled", outcome: "stopped" });
expect(done.card()).toMatchObject({ answered: "answer", answeredText: "Yes" });
const finished = parked();
finished.w.cards.closed({ ...finished.node, status: "completed", outcome: "done" });
expect(finished.card(), "finished work no longer waits on anyone").toMatchObject({ answered: "unavailable", dismissed: true });
});
it("closes a question the work asked with its question tool once that work ends, unless a turn still waits on it", () => {
const w = world({ live: ["req-live"] });
const node = w.node();
w.store.appendMessage("work", { role: "bot", kind: "options", text: "Should I?", card: { title: "Your bot has a question", subtitle: "Should I?", options: ["Yes"], requestId: "req-1", requestType: "question" } });
w.store.appendMessage("work", { role: "bot", kind: "options", text: "Really?", card: { title: "Your bot has a question", subtitle: "Really?", options: ["Yes"], requestId: "req-live", requestType: "question" } });
w.cards.closed({ ...node, status: "completed", outcome: "done" });
expect(w.rows("work").map((row) => [row.card!.requestId, row.card!.answered ?? null])).toEqual([["req-1", "unavailable"], ["req-live", null]]);
});
});
describe("a typed line answers a question card", () => {
const question = (w: ReturnType<typeof world>, threadId: string, card: Partial<NonNullable<Message["card"]>> = {}) =>
w.store.appendMessage(threadId, { role: "bot", kind: "options", text: "Should I?", card: { title: "Your bot has a question", subtitle: "Should I?",
options: ["Yes, only the footer text", "No"], requestId: "req-1", requestType: "question", ...card } });
it("answers the card the bot's turn waits on in its conversation, and keeps the person's line under it", async () => {
const w = world({ live: ["req-1"] });
const asked = question(w, "work");
const result = await w.cards.typed(AUTH, "work", "Just the footer text, please", undefined, { sendId: "send-1" });
expect(w.answers).toEqual([{ threadId: "work", requestId: "req-1", message: "Just the footer text, please" }]);
expect(result).toMatchObject({ status: 202, message: { role: "user", text: "Just the footer text, please", replyToId: asked.id, sendId: "send-1" } });
const again = await w.cards.typed(AUTH, "work", "Just the footer text, please", undefined, { sendId: "send-1" });
expect(again?.message?.id).toBe(result?.message?.id);
expect(w.answers).toHaveLength(1);
});
it("files the words under a structured question's own text", async () => {
const w = world({ live: ["req-1"] });
question(w, "work", { questionRequest: { version: 1, questions: [{ question: "Should I?", options: [{ label: "Yes" }, { label: "No" }] }] } });
await w.cards.typed(AUTH, "work", "Not until tomorrow", undefined, {});
expect(w.answers[0]!.message).toBe("The user answered your questions.\n\nQ: Should I?\nA: Not until tomorrow");
});
it("answers the original through the copy a reply in the inbox is to, whether or not its turn still waits", async () => {
const w = world();
const copy = question(w, "inbox");
const result = await w.cards.typed(AUTH, "inbox", "Yes, only the footer text", copy, { sendId: "send-2" });
expect(w.answers).toEqual([{ threadId: "inbox", requestId: "req-1", message: "Yes, only the footer text" }]);
expect(result?.message).toMatchObject({ role: "user", replyToId: copy.id, sendId: "send-2" });
});
it("leaves everything else to the usual send: no live card, a reply to another line, Stop, a room, a card that takes only its options", async () => {
const idle = world();
const asked = question(idle, "work");
expect(await idle.cards.typed(AUTH, "work", "Yes", undefined, {})).toBeNull();
expect(await idle.cards.typed(AUTH, "work", "Yes", asked, {})).toBeNull();
const busy = world({ live: ["req-1"] });
question(busy, "work");
const other = busy.say("work", "I checked.");
expect(await busy.cards.typed(AUTH, "work", "Thanks", other, {})).toBeNull();
expect(await busy.cards.typed(AUTH, "work", "stop", undefined, {})).toBeNull();
expect(await busy.cards.typed(AUTH, "room", "Yes", undefined, {})).toBeNull();
const fixed = world({ live: ["req-1"] });
question(fixed, "work", { fixedOptions: true });
expect(await fixed.cards.typed(AUTH, "work", "Yes", undefined, {})).toBeNull();
expect(idle.answers).toEqual([]);
expect(busy.answers).toEqual([]);
expect(fixed.answers).toEqual([]);
});
it("answers the card its turn waits on in the bot's own chat with exactly the person's words", async () => {
const w = world({ live: ["req-1"] });
const asked = question(w, "chat");
w.settle();
const result = await w.cards.typed(AUTH, "chat", "What time is it?", undefined, { sendId: "send-3" });
expect(w.answers).toEqual([{ threadId: "chat", requestId: "req-1", message: "What time is it?" }]);
expect(result).toMatchObject({ status: 202, message: { role: "user", text: "What time is it?", replyToId: asked.id, sendId: "send-3" } });
});
it("files a structured question's answer under its text, as an answer typed on the card would be", async () => {
const w = world({ live: ["req-1"] });
question(w, "chat", { questionRequest: { version: 1, questions: [{ question: "Should I?", options: [{ label: "Yes" }, { label: "No" }] }] } });
w.settle();
await w.cards.typed(AUTH, "chat", "Not yet", undefined, {});
expect(w.answers).toEqual([{ threadId: "chat", requestId: "req-1", message: "The user answered your questions.\n\nQ: Should I?\nA: Not yet" }]);
});
it("never touches a card in the bot's own chat that only takes a tap: the line goes on to the usual send", async () => {
const several = { questionRequest: { version: 1 as const, questions: [{ question: "A?", options: [{ label: "x" }] }, { question: "B?", options: [{ label: "y" }] }] } };
for (const card of [{ fixedOptions: true }, several]) {
const w = world({ live: ["req-1"] });
const asked = question(w, "chat", card);
w.settle();
expect(await w.cards.typed(AUTH, "chat", "Something else first", undefined, {})).toBeNull();
expect(await w.cards.typed(AUTH, "chat", "Something else first", asked, {})).toBeNull();
expect(w.deps.answerCard).not.toHaveBeenCalled();
expect(w.rows("chat")).toEqual([asked]);
}
});
it("leaves a line to the usual send when its card opened as the person typed, unless the line replies to that card", async () => {
const w = world({ live: ["req-1"] });
const asked = question(w, "chat");
expect(await w.cards.typed(AUTH, "chat", "And the header too", undefined, { sendId: "send-8" })).toBeNull();
expect(w.answers).toEqual([]);
const replied = await w.cards.typed(AUTH, "chat", "The footer only", asked, { sendId: "send-10" });
expect(replied).toMatchObject({ status: 202, message: { text: "The footer only", replyToId: asked.id } });
expect(w.answers).toEqual([{ threadId: "chat", requestId: "req-1", message: "The footer only" }]);
});
it("gives a send tried again the line it wrote, and touches no card it finds open then", async () => {
const w = world({ live: ["req-1"] });
question(w, "chat");
w.settle();
const first = await w.cards.typed(AUTH, "chat", "What time is it?", undefined, { sendId: "send-4" });
question(w, "chat", { requestId: "req-2" });
w.open("req-2");
w.settle();
const again = await w.cards.typed(AUTH, "chat", "What time is it?", undefined, { sendId: "send-4" });
expect(again).toEqual({ status: 202, message: first!.message });
expect(w.answers.map((answer) => answer.requestId)).toEqual(["req-1"]);
expect(await w.cards.typed(AUTH, "chat", "Something else", undefined, { sendId: "send-4" }), "other words go on to be refused").toBeNull();
expect(w.answers).toHaveLength(1);
expect(w.rows("chat").filter((row) => row.role === "user")).toHaveLength(1);
});
it("answers once when the same send arrives twice at the same time", async () => {
const w = world({ live: ["req-1"] });
question(w, "chat");
w.settle();
const [one, two] = await Promise.all([1, 2].map(() => w.cards.typed(AUTH, "chat", "What time is it?", undefined, { sendId: "send-5" })));
expect(two?.message?.id).toBe(one?.message?.id);
expect(w.answers).toHaveLength(1);
});
it("answers one card when the same send arrives twice at once and a new card opens before its line is written", async () => {
const w = world({ live: ["req-1"] });
question(w, "chat");
w.settle();
w.afterAnswer(() => {
w.afterAnswer(null);
question(w, "chat", { requestId: "req-2" });
w.open("req-2");
w.settle();
});
const tries = await Promise.all([1, 2].map(() => w.cards.typed(AUTH, "chat", "What time is it?", undefined, { sendId: "send-6" })));
expect(tries[1]?.message?.id).toBe(tries[0]?.message?.id);
expect(w.answers.map((answer) => answer.requestId)).toEqual(["req-1"]);
});
it("leaves a line in the bot's own chat to the usual send while two question cards wait there", async () => {
for (const second of [{}, { fixedOptions: true }]) {
const w = world({ live: ["req-1", "req-2"] });
const first = question(w, "chat");
question(w, "chat", { requestId: "req-2", ...second });
w.settle();
expect(await w.cards.typed(AUTH, "chat", "What time is it?", undefined, {})).toBeNull();
expect(await w.cards.typed(AUTH, "chat", "What time is it?", first, {})).toBeNull();
expect(w.answers).toEqual([]);
}
});
it("leaves a line in the bot's own chat to the usual send when no turn waits on its card, it is Stop, it replies to another line or carries a file", async () => {
const idle = world();
question(idle, "chat");
idle.settle();
expect(await idle.cards.typed(AUTH, "chat", "Take the second one", undefined, {})).toBeNull();
const busy = world({ live: ["req-1"] });
question(busy, "chat");
const other = busy.say("chat", "I checked.");
busy.settle();
expect(await busy.cards.typed(AUTH, "chat", "stop", undefined, {})).toBeNull();
expect(await busy.cards.typed(AUTH, "chat", "Thanks", other, {})).toBeNull();
expect(await busy.cards.typed(AUTH, "chat", 'Look <attached-image path="a.png" />', undefined, {})).toBeNull();
expect(idle.answers).toEqual([]);
expect(busy.answers).toEqual([]);
});
it("passes on why an answer was refused, and writes no line", async () => {
const w = world({ live: ["req-1"] });
question(w, "work");
w.refuse({ status: 403, error: "This question is Ben's to answer." });
expect(await w.cards.typed(AUTH, "work", "Yes", undefined, {})).toEqual({ status: 403, error: "This question is Ben's to answer." });
expect(w.rows("work").filter((row) => row.role === "user")).toEqual([]);
});
it("leaves a line in the bot's own chat to the usual send when its card could not take it as the answer", async () => {
for (const refusal of [{ status: 403, error: "This question is Ben's to answer." }, { status: 409, error: "This card was already answered." }]) {
const w = world({ live: ["req-1"] });
const asked = question(w, "chat");
w.settle();
w.refuse(refusal);
expect(await w.cards.typed(AUTH, "chat", "What time is it?", undefined, { sendId: "send-9" })).toBeNull();
expect(w.deps.answerCard).toHaveBeenCalledWith(expect.objectContaining({ requestId: "req-1", behavior: "answer" }));
expect(w.rows("chat")).toEqual([asked]);
}
});
});
describe("a reminder of work parked on the person", () => {
const closesAt = 2 * 24 * 60 * 60_000;
it("names the question and when it closes, and adds no second row", () => {
const w = world();
const node = w.node({ status: "waiting", question: "Which region?\nDefault: eu-west" });
w.cards.remind(node, closesAt);
expect(w.notified).toHaveLength(1);
expect(w.notified[0]).toMatchObject({ kind: "question", botId: "ledger", threadId: "work", body: expect.stringContaining("Which region?") });
expect(w.notified[0]!.body).toContain("2 days");
expect(w.notified[0]!.body).not.toContain("eu-west");
expect(w.rows("work")).toEqual([]);
});
it("does not read a card's internal wait description to the person", () => {
const w = world();
w.cards.remind(w.node({ status: "waiting", question: "Waiting on the person: provide API key", cards: ["m1"] }), closesAt);
expect(w.notified[0]!.body).not.toContain("Waiting on the person");
expect(w.notified[0]!.body).toContain("2 days");
});
});
describe("the chip in the conversation that handed the work on", () => {
const asker = { id: "ledger", name: "Ledger", color: "teal" } as BotRecord;
it("says the work waits on the person, unless that conversation is the person's inbox, which has the card", () => {
const desk = world();
desk.cards.showWaitingOnPersonChip("work", "req-1", asker, "answer");
expect(desk.rows("inbox").map((row) => row.tool?.name)).toEqual(["Waiting on your answer in @Ledger"]);
const team = world({ inboxThread: "inbox" });
team.cards.showWaitingOnPersonChip("work", "req-1", asker, "answer");
expect(team.rows("inbox")).toEqual([]);
});
it("links to a sign-in, password or app to connect left open in the work, once, and says when it no longer waits", () => {
const desk = world();
desk.cards.showWaitingOnCardChip("work", { kind: "sign-in", messageId: "c1", label: "example.com" }, asker);
desk.cards.showWaitingOnCardChip("work", { kind: "sign-in", messageId: "c1", label: "example.com" }, asker);
desk.cards.showWaitingOnCardChip("work", { kind: "connect", messageId: "c2", label: "Gmail" }, asker);
expect(desk.rows("inbox")).toMatchObject([
{ kind: "activity", from: { botId: "ledger", name: "Ledger" }, threadRef: { botId: "ledger", threadId: "work", title: "Pricing page: fix" },
tool: { name: "Waiting on you to sign in to example.com in @Ledger", nameCode: "chat.notice.waitingSignInElsewhere", nameParams: { name: "Ledger", label: "example.com" } } },
{ tool: { name: "Waiting on you to connect Gmail in @Ledger", nameCode: "chat.notice.waitingConnectElsewhere" } },
]);
desk.cards.settleWaitingOnCardChip("work", "c1");
const [settled, open] = desk.rows("inbox");
expect(settled?.tool).toMatchObject({ name: "@Ledger no longer waits on you", ok: true });
expect(settled?.tool?.nameCode).toBeUndefined();
expect(settled?.threadRef).toMatchObject({ threadId: "work" });
expect(open?.tool?.name).toBe("Waiting on you to connect Gmail in @Ledger");
});
it("says the same line waits again when the card waits again, without a second line", () => {
const desk = world();
desk.cards.reopenWaitingOnCardChip("work", "c1");
expect(desk.rows("inbox")).toEqual([]);
desk.cards.showWaitingOnCardChip("work", { kind: "sign-in", messageId: "c1", label: "example.com" }, asker);
desk.cards.settleWaitingOnCardChip("work", "c1");
desk.cards.reopenWaitingOnCardChip("work", "c1");
const rows = desk.rows("inbox");
expect(rows).toHaveLength(1);
expect(rows[0]!.tool).toMatchObject({ name: "Waiting on you to sign in to example.com in @Ledger", nameCode: "chat.notice.waitingSignInElsewhere" });
expect(rows[0]!.tool?.ok).toBeUndefined();
desk.cards.settleWaitingOnCardChip("work", "c1");
expect(desk.rows("inbox")[0]!.tool).toMatchObject({ name: "@Ledger no longer waits on you", ok: true });
});
it("keeps a private conversation's card, site and title out of a conversation that belongs to someone else", () => {
const team = world({ privateThreads: ["work"] });
team.cards.showWaitingOnCardChip("work", { kind: "sign-in", messageId: "c1", label: "example.com" }, asker);
expect(team.rows("inbox")).toEqual([]);
});
it("leaves a settled chip settled when its conversation has since become private to someone else", () => {
const privateThreads: string[] = [];
const team = world({ privateThreads });
team.cards.showWaitingOnCardChip("work", { kind: "sign-in", messageId: "c1", label: "example.com" }, asker);
team.cards.settleWaitingOnCardChip("work", "c1");
privateThreads.push("work");
team.cards.reopenWaitingOnCardChip("work", "c1");
expect(team.rows("inbox")[0]!.tool).toMatchObject({ name: "@Ledger no longer waits on you", ok: true });
});
it("leaves a card's chip out of the person's inbox, which has the card, and out of work nobody handed on", () => {
const team = world({ inboxThread: "inbox" });
team.cards.showWaitingOnCardChip("work", { kind: "password", messageId: "c1", label: "API key" }, asker);
expect(team.rows("inbox")).toEqual([]);
const desk = world();
desk.cards.showWaitingOnCardChip("inbox", { kind: "password", messageId: "c1", label: "API key" }, asker);
desk.cards.showWaitingOnCardChip("work", { kind: "question", messageId: "c2", label: "Which?" }, asker);
expect(desk.rows("inbox")).toEqual([]);
});
});
describe("askToolName", () => {
const bot = (toolScope?: BotRecord["toolScope"]) => ({ modelSelection: { instanceId: "claude", model: "m" }, toolScope }) as BotRecord;
const claude = () => ({ kind: "mcp" as const, server: "ogb", name: "ask_user" });
it("names the engine's question tool as its model calls it, unless the bot's tool selection leaves it out", () => {
expect(askToolName(bot(), claude)).toBe("mcp__ogb__ask_user");
expect(askToolName(bot(), () => ({ kind: "native", name: "AskQuestion" }))).toBe("AskQuestion");
expect(askToolName(bot({ deny: ["mcp:ogb:ask_user"] }), claude)).toBeUndefined();
expect(askToolName(bot({ allow: ["mcp:agents:*"] }), claude)).toBeUndefined();
expect(askToolName(bot(), () => undefined)).toBeUndefined();
expect(askToolName(null, claude)).toBeUndefined();
});
});