Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Stop means stop for the CLI and routines: report teammates the Stop ended
milind-soni0f4685d33723
diff --git a/docs/headless-cli.md b/docs/headless-cli.mdindex 37bc8e3fcf..e2f69b968f 100644--- a/docs/headless-cli.md+++ b/docs/headless-cli.md@@ -135,11 +135,13 @@ and `wait` keep asking for the same job until their time runs out; a refusal(an expired token) ends them with exit 5 at once.Stopping a job (`interrupt --tree`, `--on-input stop`, `--on-timeout stop`)-presses Stop in its conversation, then in each teammate's own conversation-that still holds part of it: a turn still running, waiting on its own-teammates, parked on an answer, or about to run again. Work that never-started is dropped by the first Stop, and its teammate's conversation is left-alone.+presses Stop in its conversation, which ends everything it handed out:+teammates mid-turn, what they handed on, and work that never started. If a+teammate's own conversation still holds part of it after that (a turn still+running, waiting on its own teammates, parked on an answer, or about to run+again), Stop is pressed there too. `stopped` (`teammates` for `interrupt+--tree`) lists each teammate task that was at work and ended; work that never+started is dropped without being listed.The waiting is done by the server: each request is held until the jobchanges, so a long wait costs one request at a time, not a poll loop. Thediff --git a/server/headless/run.test.ts b/server/headless/run.test.tsindex 91642279c0..531ed8196f 100644--- a/server/headless/run.test.ts+++ b/server/headless/run.test.ts@@ -175,6 +175,27 @@ describe("wait", () => {expect(result.stopped.map((node: Rec) => node.botName)).toEqual(["Priya", "Marcus", "Kenji"]);});+ it("counts a teammate the conversation's Stop ended with it as stopped, and presses Stop again only where work is left", async () => {+ const needs = waited("needs-user", "needs-input", job("needs-input", "v1", { needsInput: [{ botName: "Priya", question: "Which tier?" }] }));+ const before = [+ { id: "root", botId: "lead", threadId: "t1", status: "waiting" },+ { id: "a1", botId: "priya", botName: "Priya", threadId: "p1", status: "running" },+ { id: "a2", botId: "lena", botName: "Lena", threadId: "l1", status: "queued" },+ { id: "a3", botId: "kenji", botName: "Kenji", threadId: "k1", status: "cancelled" },+ ];+ const after = [{ ...before[0], status: "cancelled" }, { ...before[1], status: "cancelled" }, { ...before[2], status: "cancelled" }, before[3],+ { id: "a4", botId: "marcus", botName: "Marcus", threadId: "m1", status: "running" }];+ let reads = 0;+ const stopped = harness([needs], { jobRead: () => job("needs-input", "v1", { nodes: reads++ === 0 ? before : after }) });+ const { code, result } = await stopped.run(["wait", "--bot", "lead", "--on-input", "stop", "--json"]);+ expect(code).toBe(3);+ // Priya was at work and ended with the Stop; Lena never started and Kenji had ended before it.+ expect(result.stopped.map((node: Rec) => node.botName)).toEqual(["Priya", "Marcus"]);+ expect(stopped.requests.filter((request) => request.method === "POST")).toEqual([+ { path: "/api/bots/marcus/interrupt", method: "POST", body: { threadId: "m1" } },+ ]);+ });+it("times out (4) and leaves the work running unless --on-timeout stop; --strict fails a run whose teammate failed", async () => {const running = waited("timed-out", "timed-out", job("running", "v1"));const left = harness([running], { delayMs: 20 });diff --git a/server/headless/run.ts b/server/headless/run.tsindex 98e41dbe7f..a948725633 100644--- a/server/headless/run.ts+++ b/server/headless/run.ts@@ -12,23 +12,33 @@ const enc = encodeURIComponent;const STOPPABLE = new Set(["running", "waiting", "needs_input", "resume"]);-export async function stopTeammates(client: HeadlessClient, threadId: string, nodes: readonly Rec[]): Promise<Rec[]> {+/** The teammate tasks a Stop ended: each one still at work after the conversation's Stop is stopped in its own+ * conversation, and one that was at work before it (`working`, node ids) and is cancelled now ended with it. */+export async function stopTeammates(client: HeadlessClient, threadId: string, nodes: readonly Rec[], working: ReadonlySet<string> = new Set()): Promise<Rec[]> {const stopped: Rec[] = [];const done = new Set<string>();for (const node of nodes) {- if (!STOPPABLE.has(node.status) || node.threadId === threadId || done.has(node.threadId)) continue;+ if (node.threadId === threadId || done.has(node.threadId)) continue;+ const record = { id: node.id, botId: node.botId, botName: node.botName, threadId: node.threadId };+ if (node.status === "cancelled" && working.has(node.id)) {+ done.add(node.threadId);+ stopped.push(record);+ continue;+ }+ if (!STOPPABLE.has(node.status)) continue;done.add(node.threadId);const route = node.groupId ? `/api/groups/${enc(node.groupId)}/interrupt` : `/api/bots/${enc(node.botId)}/interrupt`;try {await client.json(route, { method: "POST", body: JSON.stringify({ threadId: node.threadId }) });- stopped.push({ id: node.id, botId: node.botId, botName: node.botName, threadId: node.threadId });+ stopped.push(record);} catch {}}return stopped;}-/** The job is read before the Stop (for its since, which the Stop moves past) and again after it. */+/** The job is read before the Stop (for its since, which the Stop moves past, and the teammates then at work) and+ * again after it. The Stop itself ends the teammates mid-turn; any still at work after it are stopped one by one. */export async function stopConversation(client: HeadlessClient, target: Target, tree: boolean): Promise<{ result: Rec; stopped: Rec[] }> {let threadId = target.taskId;if (tree && !threadId) {@@ -38,8 +48,9 @@ export async function stopConversation(client: HeadlessClient, target: Target, tconst before = tree && threadId ? await readJob(client, threadId) : undefined;const result = await client.tool("interrupt_conversation", { target_type: target.type, target_id: target.id, ...(threadId ? { task_id: threadId } : {}) });if (!before) return { result, stopped: [] };+ const working = new Set<string>((before.nodes ?? []).filter((node: Rec) => STOPPABLE.has(node.status)).map((node: Rec) => node.id));const after = await readJob(client, result.taskId, { since: String(before.since) });- return { result, stopped: await stopTeammates(client, result.taskId, after.nodes ?? []) };+ return { result, stopped: await stopTeammates(client, result.taskId, after.nodes ?? [], working) };}/** From the job's own state: MCP's `status` keeps older words (a lead whose work failed is "settled" there). */diff --git a/server/routine-delegation.e2e.test.ts b/server/routine-delegation.e2e.test.tsindex 6d099ceb73..ea3815d803 100644--- a/server/routine-delegation.e2e.test.ts+++ b/server/routine-delegation.e2e.test.ts@@ -277,7 +277,7 @@ describe("routine delegation through the isolated harness", () => {evidence.push({ providerReloadKeptRuntimeHandoff: true, runId: run.id, transcript: await messages(run.threadId) });}, 60_000);- it("does not revive a cancelled routine when its running peer replies during later user work", async () => {+ it("does not revive a cancelled routine: its running peer stops with it, and later user work never resumes it", async () => {const run = await start();await delegate(run.threadId);finish(run.threadId);@@ -287,20 +287,26 @@ describe("routine delegation through the isolated harness", () => {await expect.poll(() => peerWork(run.threadId)?.status, { timeout: 15_000 }).toBe("running");await api("POST", `/api/routine-runs/${run.id}/cancel`);await expect.poll(() => rows().find((node) => node.id === work.parentId)?.status, { timeout: 10_000 }).toBe("cancelled");+ // Stop means stop: the peer mid-turn ends with the cancelled run, its engine turn included.+ await expect.poll(() => peerWork(run.threadId)?.status, { timeout: 10_000 }).toBe("cancelled");+ await expect.poll(async () => Boolean((await botState(peer.id)).busy), { timeout: 15_000 }).toBe(false);unlinkSync(file(run.threadId, "gate"));unlinkSync(file(run.threadId, "json"));await api("POST", `/api/bots/${source.id}/messages`, { threadId: run.threadId, text: "A new independent request after cancellation." });await dump(run.threadId);+ // Past the moment the peer would have answered, during the later user work.finish(work.threadId);- await expect.poll(() => peerWork(run.threadId)?.status, { timeout: 15_000 }).toBe("completed");finish(run.threadId);await control(["wait", "--bot", source.id, "--task", run.threadId]);await new Promise((resolve) => setTimeout(resolve, 2_000));+ expect(peerWork(run.threadId)?.status).toBe("cancelled");const transcript = await messages(run.threadId);+ expect(transcript.some((message) => message.tool?.name === "Routine peer replied")).toBe(false);+ expect(transcript.some((message) => message.text?.includes(REPORT_FROM_PEER))).toBe(false);expect(transcript.some((message) => message.text?.includes(SETTLED))).toBe(false);expect(transcript.some((message) => message.tool?.name?.startsWith("Resumed with"))).toBe(false);expect((await runState(run.id)).status).toBe("cancelled");expect(Boolean((await botState(source.id)).busy)).toBe(false);- evidence.push({ cancelledPeerDidNotResumeNewUserTurn: true, transcript });+ evidence.push({ cancelledPeerStoppedWithRun: true, transcript });}, 45_000);});