Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Merge #5114: Hand-offs: the budget refusal says what renews it; a fan-out counts once
milind-soni4f538d5201ca
diff --cc server/coordinator.tsindex 9519b90620,0418b8a821..a49bbbc7de--- a/server/coordinator.ts+++ b/server/coordinator.ts@@@ -103,8 -104,11 +103,10 @@@ export interface HandoffOptionsmode?: Mode;reply?: Reply;thread?: "standing" | "new";- approvalGranted?: boolean;rework?: boolean;requestBatchKey?: string;+ /** One call handing the same work to several teammates: it counts once against the request budget. */+ fanOut?: string;doneWhen?: string;/** The target is a new thread of the requester's own bot (start_thread with report_back). */ownThread?: boolean;diff --cc server/index.tsindex 67d1f10b8a,a228ef97c4..6d2b68c717--- a/server/index.ts+++ b/server/index.ts@@@ -13735,10 -13754,24 +13735,10 @@@ const handleRequest = async (req: Incomtarget.botId === internalSender.id && !toSelf ? "Choose a teammate, not yourself": !addressed ? `No bot with id "${target.botId}" — call list_bots and copy the exact id from the result`: addressed.hidden ? `The bot with id "${target.botId}" is no longer available — call list_bots for the ones you can reach`- : roomHandoffProblem(target, address);+ : roomHandoffProblem(target, address) ?? personalReach.handOnProblem(internalSender, addressed, address.threadId, internalCapability.generation);if (eligibility) return refuse(403, eligibility);}- const requestBatchKey = destination ? randomUUID() : undefined, handOnFor = CLOUD_TEAM ? teamConnections.handOnFor(internalCapability.threadId, internalCapability.generation) : null;- let approvalGranted = false;- if (!toSelf && peerReviewRequired(internalSender, address.threadId)) {- const verdicts = await Promise.all(targets.map(target =>- requestPeerApproval(approvalBus, internalSender, store.bot(target.botId)!, call.message, call.consult ? "ask_bot" : "delegate_bot", address.threadId)));- requireActiveInternalCapability();- const blocked = verdicts.flatMap((verdict, index) => verdict === "allow" ? []- : [{ botId: targets[index]!.botId, ...peerApprovalFailure(verdict) }]);- if (blocked.length) return refuse(403,- blocked.every(failure => failure.approvalOutcome === "deny")- ? "Denied by user; no work sent."- : `${blocked.map(failure => failure.error).join("; ")}; no work sent.`,- { approvals: blocked });- approvalGranted = true;- }+ const requestBatchKey = destination ? randomUUID() : undefined, fanOut = targets.length > 1 ? randomUUID() : undefined, handOnFor = CLOUD_TEAM ? teamConnections.handOnFor(internalCapability.threadId, internalCapability.generation) : null;const generation = EXTERNAL ? randomUUID() : internalCapability.generation;if (EXTERNAL) {const opener = handoffOpener(internalCapability, address.threadId);@@@ -13787,10 -13820,10 +13787,10 @@@const routed = lane ? { botId: target.botId, threadId: lane.threadId } : target;outsideLineage.carry(address.threadId, routed.threadId);const { node, duplicate } = coordinator.enqueue(address, generation, internalCapability.roomHandoffId, routed, call.message, {- mode, reply, thread: lane || fresh ? "new" : "standing", rework: call.rework, ...(lane ? {} : { requestBatchKey }), ...(call.ownThread ? { ownThread: true } : {}),- mode, reply, thread: lane || fresh ? "new" : "standing", approvalGranted, rework: call.rework, ...(lane ? {} : { requestBatchKey }), ...(fanOut ? { fanOut } : {}), ...(call.ownThread ? { ownThread: true } : {}),++ mode, reply, thread: lane || fresh ? "new" : "standing", rework: call.rework, ...(lane ? {} : { requestBatchKey }), ...(fanOut ? { fanOut } : {}), ...(call.ownThread ? { ownThread: true } : {}),...(call.doneWhen ? { doneWhen: call.doneWhen } : {}),});- usageTrace.link(node.id, node.parentId, { threadId: internalCapability.threadId, turnId: liveTurnByThread.get(internalCapability.threadId) ?? "" }); if (!duplicate) actingPeople.handOn(internalCapability, node);+ usageTrace.link(node.id, node.parentId, { threadId: internalCapability.threadId, turnId: liveTurnByThread.get(internalCapability.threadId) ?? "" }); if (!duplicate) { actingPeople.handOn(internalCapability, node, lends); if (!lends && !room && !lane) handedInUnproven(routed.threadId); }if (duplicate && createdThread) store.deleteTask(target.botId, createdThread);else if (createdThread) {threadStarters.set(createdThread, opener);diff --cc server/runtime-coordinator.test.tsindex cb1085ae20,f77be3c038..8d27a3a83a--- a/server/runtime-coordinator.test.ts+++ b/server/runtime-coordinator.test.ts@@@ -91,10 -91,10 +91,10 @@@ const begun = (w: World, source: RoomAd};const send = (w: World, source: RoomAddress, generation: string, parentId: string | undefined, target: RoomAddress, text: string,- opts: { rework?: boolean; batchKey?: string; sourceText?: string } = {}) => {- opts: { approved?: boolean; rework?: boolean; batchKey?: string; fanOut?: string; sourceText?: string } = {}) => {++ opts: { rework?: boolean; batchKey?: string; fanOut?: string; sourceText?: string } = {}) => {if (!parentId) begun(w, source, generation, opts.sourceText);- return w.coordinator.enqueue(source, generation, parentId, target, text, { approvalGranted: opts.approved ?? false, rework: opts.rework ?? false,+ return w.coordinator.enqueue(source, generation, parentId, target, text, { rework: opts.rework ?? false,- requestBatchKey: opts.batchKey });+ requestBatchKey: opts.batchKey, ...(opts.fanOut ? { fanOut: opts.fanOut } : {}) });};const ended = (w: World, generation: string, ok = true) =>diff --cc server/runtime-coordinator.tsindex 614c411cda,7bb11547d2..f9a65ead91--- a/server/runtime-coordinator.ts+++ b/server/runtime-coordinator.ts@@@ -228,7 -228,7 +228,7 @@@ export class RuntimeCoordinator implemeenqueue(source: RoomAddress, generation: string, parentId: string | undefined, target: RoomAddress, text: string,options: HandoffOptions = {}): { node: HandoffView; duplicate: boolean } {this.ready();- const { rework = false, requestBatchKey, thread } = options;- const { approvalGranted = false, rework = false, requestBatchKey, fanOut, thread } = options;++ const { rework = false, requestBatchKey, fanOut, thread } = options;const mode = options.mode ?? "work";const parent = parentId ? this.store.get(parentId) : this.store.get(generation);if (!parent) throw new Error(parentId ? NOT_RUNNING : NO_TURN);@@@ -269,10 -269,12 +269,11 @@@...(options.doneWhen ? { doneWhen: options.doneWhen } : {}),levels: { own: level.level, ...(level.chief !== undefined ? { chief: level.chief } : {}) },...(level.driverKind !== undefined ? { driverKind: level.driverKind } : {}),- approved: approvalGranted,...(target.groupId && requestBatchKey ? { batchKey: requestBatchKey } : {}),+ ...(fanOut ? { fanOut } : {}),});if (!result.ok) {- if (result.outcome === "budget") throw new Error(BUDGET);+ if (result.outcome === "budget") throw new Error(budgetRefusal(this.runtime.limits.requests));throw new Error(RUNTIME_DEPTH.test(result.reason) ? DEPTH : result.reason);}return { node: view(result.assignment, parent), duplicate: result.duplicate };diff --cc server/runtime/runtime.test.tsindex 5daacac03e,c393973653..87d787ee22--- a/server/runtime/runtime.test.ts+++ b/server/runtime/runtime.test.ts@@@ -419,22 -397,17 +419,33 @@@ describe("Runtime: one tree, one lane"store.close();});+ it("records an expired question's reason on the node that asked it, never on the work under it", async () => {+ const w = world(() => "hold", { inputWaitMs: 5_000 });+ const root = w.runtime.begin(beginReq());+ const lead = ok(w.runtime.enqueue(toBot("lead", root.id)));+ w.runtime.tick();+ const helper = ok(w.runtime.enqueue({ ...toBot("helper", lead.id), requester: { kind: "bot_turn", botId: "lead", threadId: "lead-work" } }));+ w.runtime.tick();+ await w.runner.release(lead.id, { kind: "needs_input", question: "Which bucket?" });+ expect(w.store.get(lead.id)?.status).toBe("needs_input");+ w.clock.advance(5_001);+ w.runtime.tick();+ expect(w.store.get(lead.id)).toMatchObject({ status: "failed", outcome: "expired", outcomeDetail: "the person did not answer" });+ expect(w.store.get(helper.id)).toMatchObject({ status: "failed", outcome: "expired", outcomeDetail: "Stopped with the request it was for" });+ expect(w.runner.reports.filter(r => r.a.outcomeDetail === "the person did not answer").map(r => r.a.id)).toEqual([lead.id]);+ });++ it("charges the request budget once per fan-out, and still refuses the next call", () => {+ const w = world(() => "hold", { requests: 1 });+ const root = w.runtime.begin(beginReq());+ ok(w.runtime.enqueue({ ...toBot("builder", root.id), fanOut: "call-1" }));+ ok(w.runtime.enqueue({ ...toBot("tester", root.id), fanOut: "call-1" }));+ expect(w.store.get(root.id)?.budgets?.requests).toBe(1);+ expect(w.store.children(root.id).map(c => c.fanOut)).toEqual(["call-1", "call-1"]);+ expect(w.runtime.enqueue({ ...toBot("scout", root.id), fanOut: "call-2" })).toMatchObject({ ok: false, outcome: "budget" });+ expect(w.runtime.enqueue(toBot("scout", root.id))).toMatchObject({ ok: false, outcome: "budget" });+ });+it("cancels down the tree, aborts running turns and reports each stop", async () => {const w = world(() => "hold");const root = w.runtime.begin(beginReq());