Skip to content

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

fix(skill-burger): cancel execution when its caller disconnects

Afloat466d027cb468
diff --git a/server/skill-burger/run-disconnect.test.ts b/server/skill-burger/run-disconnect.test.ts
new file mode 100644
index 0000000000..43afbed7de
--- /dev/null
+++ b/server/skill-burger/run-disconnect.test.ts
@@ -0,0 +1,114 @@
+import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
+import { createServer, request, type ClientRequest } from "node:http";
+import type { AddressInfo } from "node:net";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { expect, it } from "vitest";
+
+import { json, readBody } from "../harness/http.ts";
+import type { RequestAuth } from "../request-auth.ts";
+import { dispatchRoutes } from "../routes/table.ts";
+import { createSkillBurgerRuns, RESUME_PATH, RUN_PATH } from "./runs.ts";
+import type { RunEnvelope } from "./runner.ts";
+import { saveWorkflowSkill } from "./store.ts";
+
+function latch() {
+ let done!: () => void;
+ const promise = new Promise<void>((resolve) => { done = resolve; });
+ return { promise, release: () => done() };
+}
+
+function bounded<T>(promise: Promise<T>): Promise<T> {
+ let timer: ReturnType<typeof setTimeout>;
+ return Promise.race([promise, new Promise<T>((_resolve, reject) => { timer = setTimeout(() => reject(new Error("request did not finish")), 3_000); })]).finally(() => clearTimeout(timer));
+}
+
+async function exercise(mode: string, disconnect: boolean) {
+ const root = mkdtempSync(join(tmpdir(), "burger-disconnect-"));
+ const entered = latch();
+ const release = latch();
+ const closed = latch();
+ const completed = latch();
+ const errors: unknown[] = [];
+ const path = mode === "resume" ? RESUME_PATH : RUN_PATH;
+ const calling = { botId: "reporter", threadId: "weekly", generation: "turn-1" };
+ const saved = saveWorkflowSkill({ name: "disconnect-report", origin: calling.botId, botIds: [], root, workflow: {
+ version: 1, summary: "Make a report", args: {}, steps: [
+ ...(mode === "resume" ? [{ id: "review", kind: "ai", deterministic: false, label: "Review request", prompt: "Review the request." }] : []),
+ { id: "draft", kind: "app", app: "gmail", tool: "gmail.create_draft", label: "Create draft", args: { to: "[email protected]", subject: "Report", body: "Ready" } },
+ { id: "receipt", kind: "file", label: "Write receipt", path: "receipt.txt", content: { $step: "draft", path: "id" } },
+ ],
+ } });
+ if ("error" in saved) throw new Error(saved.error);
+ let runs!: ReturnType<typeof createSkillBurgerRuns>;
+ const server = createServer(async (req, res) => {
+ const url = new URL(req.url ?? "/", "http://localhost");
+ const target = url.pathname === path;
+ if (target) res.once("close", closed.release);
+ try {
+ if (url.pathname === "/api/internal/connectors/mcp") {
+ await readBody(req);
+ entered.release();
+ await release.promise;
+ if (!res.destroyed) json(res, 200, { result: { content: [{ type: "text", text: JSON.stringify({ successful: true, data: { id: "draft-current" } }) }] } });
+ return;
+ }
+ await dispatchRoutes([runs.routesFor(calling, "Bearer fixture-capability")], { req, res, url, path: url.pathname, method: req.method ?? "GET", auth: {} as RequestAuth, json, readBody });
+ } catch (error) {
+ errors.push(error);
+ if (!res.destroyed) json(res, 500, { error: String(error) });
+ } finally {
+ if (target) completed.release();
+ }
+ });
+ let client: ClientRequest | undefined;
+ try {
+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
+ const port = (server.address() as AddressInfo).port;
+ const base = `http://127.0.0.1:${port}`;
+ runs = createSkillBurgerRuns({ dataDir: root, root, host: {
+ port, cwd: () => root, mint: () => "fixture-capability", browser: async () => null, publish: () => {}, mayEditBurgers: () => true,
+ } });
+ let body: Record<string, unknown> = { name: saved.name };
+ if (mode === "resume") {
+ const paused = await (await fetch(`${base}${RUN_PATH}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) })).json() as RunEnvelope;
+ expect(paused.requiresInput).toMatchObject({ type: "ai_step" });
+ body = { run: paused.run, response: "ready" };
+ }
+ const response = new Promise<RunEnvelope>((resolve, reject) => {
+ client = request(`${base}${path}`, { method: "POST", headers: { "content-type": "application/json" } }, (res) => {
+ let text = "";
+ res.setEncoding("utf8");
+ res.on("data", (chunk) => { text += chunk; });
+ res.on("end", () => { try { resolve(JSON.parse(text)); } catch (error) { reject(error); } });
+ });
+ client.on("error", reject);
+ client.end(JSON.stringify(body));
+ });
+ void response.catch(() => {});
+ await bounded(entered.promise);
+ if (disconnect) {
+ client!.destroy();
+ await bounded(closed.promise);
+ release.release();
+ await response.catch(() => {});
+ } else {
+ release.release();
+ expect(await bounded(response)).toMatchObject({ ok: true, status: "ok" });
+ }
+ await bounded(completed.promise);
+ expect(errors).toEqual([]);
+ expect(existsSync(join(root, "receipt.txt"))).toBe(!disconnect);
+ if (!disconnect) expect(readFileSync(join(root, "receipt.txt"), "utf8")).toBe("draft-current");
+ } finally {
+ release.release();
+ client?.destroy();
+ await bounded(completed.promise).catch(() => {});
+ server.closeAllConnections();
+ await new Promise<void>((resolve) => server.close(() => resolve()));
+ rmSync(root, { recursive: true, force: true });
+ }
+}
+
+it.each(["run", "resume"])("stops later actions when the %s caller disconnects", async (mode) => { await exercise(mode, true); });
+it.each(["run", "resume"])("completes a normal %s request without treating its response close as cancellation", async (mode) => { await exercise(mode, false); });
diff --git a/server/skill-burger/runs.ts b/server/skill-burger/runs.ts
index cac20a0603..62ce52a12a 100644
--- a/server/skill-burger/runs.ts
+++ b/server/skill-burger/runs.ts
@@ -79,10 +79,11 @@ export function createSkillBurgerRuns(deps: { dataDir: string; root?: string; ho
const states = createRunStates(join(deps.dataDir, "skill-burger-runs"));
const history = createRunHistory(join(deps.dataDir, "skill-burger-runs"));
const open = new Map<string, Set<string>>();
- const runDeps = (turn: BurgerTurn): RunDeps => ({
+ const runDeps = (turn: BurgerTurn, signal: AbortSignal): RunDeps => ({
exec: deps.executors?.(turn) ?? createExecutors({ botId: turn.botId, cwd: turn.cwd, internal: turn.internal, ...(turn.connected ? { connected: turn.connected } : {}) }),
events: stepEvents(turn, open),
save: (state) => states.save(state),
+ signal,
});
const finished = (turn: BurgerTurn, name: string | undefined, envelope: RunEnvelope) => {
if (envelope.status !== "needs_input") open.delete(envelope.run);
@@ -92,13 +93,16 @@ export function createSkillBurgerRuns(deps: { dataDir: string; root?: string; ho
const internalRoutes = (turn: BurgerTurn): RouteHandler => async ({ req, res, path, method, json, readBody }) => {
if (method !== "POST" || ![RUN_PATH, RESUME_PATH, EDIT_PATH, UNDO_PATH].includes(path)) return PASS;
+ const controller = new AbortController();
+ res.once("close", () => { if (!res.writableEnded) controller.abort(); });
+ if (res.destroyed) controller.abort();
const body = await readBody(req);
if (path === RUN_PATH) {
const parsed = runBody.safeParse(body);
if (!parsed.success) return json(res, 400, { error: "run_workflow needs the Skill Burger's name." });
const found = findWorkflowSkill(turn.botId, parsed.data.name, deps.root);
if (!found) return json(res, 404, { error: `No Skill Burger named "${parsed.data.name}" here. Check the names in your skills index.` });
- const envelope = await startRun({ name: parsed.data.name, botId: turn.botId, threadId: turn.threadId, sha256: found.sha256, workflow: found.workflow, ...(parsed.data.args ? { args: parsed.data.args } : {}) }, runDeps(turn));
+ const envelope = await startRun({ name: parsed.data.name, botId: turn.botId, threadId: turn.threadId, sha256: found.sha256, workflow: found.workflow, ...(parsed.data.args ? { args: parsed.data.args } : {}) }, runDeps(turn, controller.signal));
return json(res, 200, finished(turn, parsed.data.name, envelope));
}
if (path === RESUME_PATH) {
@@ -106,7 +110,7 @@ export function createSkillBurgerRuns(deps: { dataDir: string; root?: string; ho
if (!parsed.success) return json(res, 400, { error: "Resume needs the run's token and your response." });
const state = states.take(parsed.data.run, turn.botId);
if (!state) return json(res, 404, { error: "That run can't be resumed: it finished, was resumed already, or is more than a day old." });
- return json(res, 200, finished(turn, state.name, await resumeRun({ ...state, threadId: turn.threadId }, parsed.data.response, runDeps(turn))));
+ return json(res, 200, finished(turn, state.name, await resumeRun({ ...state, threadId: turn.threadId }, parsed.data.response, runDeps(turn, controller.signal))));
}
if (path === UNDO_PATH) {
const parsed = undoBody.safeParse(body);