Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(skill-burger): include executable content in workflow revisions
Afloatcfd935c7ed7b
diff --git a/server/skill-burger/store.ts b/server/skill-burger/store.tsindex 6daf362dbb..8ab657e08b 100644--- a/server/skill-burger/store.ts+++ b/server/skill-burger/store.ts@@ -2,6 +2,7 @@// on for every bot: whoever saves one, every chat and every bot can use it. They are listed to every bot whether or not// the library is switched on. Undo, rename and remove act on the exact copy they saw and refuse one that changed since.// Burgers once kept as one bot's own skills move into the library once (migrateBotBurgers).+import { createHash } from "node:crypto";import { mkdirSync, readFileSync, writeFileSync } from "node:fs";import { join } from "node:path";@@ -43,18 +44,20 @@ function botFolder(botId: string, name: string): string | null {return entry && isBurgerSource(entry.source) ? skillDirectory(botId, name, entry) : null;}-function readAt(place: BurgerPlace, name: string, root: string): { workflow: Workflow; sha256: string; botId?: string } | null {+const revision = (instructionSha256: string, workflow: Workflow) => createHash("sha256").update(instructionSha256).update("\0").update(JSON.stringify(workflow)).digest("hex");++function readAt(place: BurgerPlace, name: string, root: string): { workflow: Workflow; sha256: string; instructionSha256: string; botId?: string } | null {if (!isSkillName(name)) return null;if ("everyChat" in place) {const entry = readSkillLibraryIndex(root)[name];if (!entry?.tags?.includes(WORKFLOW_TAG)) return null;const workflow = parsed(join(librarySkillDirectory(root, name), WORKFLOW_FILE));- return workflow ? { workflow, sha256: entry.sha256, botId: burgerBotOf(entry.source) } : null;+ return workflow ? { workflow, sha256: revision(entry.sha256, workflow), instructionSha256: entry.sha256, botId: burgerBotOf(entry.source) } : null;}const folder = botFolder(place.botId, name);const workflow = parsed(folder && join(folder, WORKFLOW_FILE));const entry = readManifest(place.botId)[name];- return workflow && entry ? { workflow, sha256: entry.sha256, botId: place.botId } : null;+ return workflow && entry ? { workflow, sha256: revision(entry.sha256, workflow), instructionSha256: entry.sha256, botId: place.botId } : null;}const listed = (name: string, workflow: Workflow, sha256: string, savedAt: string, botId: string | undefined): SavedWorkflow => ({@@ -130,7 +133,7 @@ function write(name: string, workflow: Workflow, origin: string | undefined, roofunction erase(name: string, sha256: string, root: string): { removed: true } | { stale: true } | { error: string } {const found = readAt(GLOBAL, name, root);if (!found || found.sha256 !== sha256) return { stale: true };- const result = retireLibrarySkill(name, sha256, root);+ const result = retireLibrarySkill(name, found.instructionSha256, root);return "retired" in result ? { removed: true } : result;}diff --git a/server/skill-burger/workflow-revisions.test.ts b/server/skill-burger/workflow-revisions.test.tsnew file mode 100644index 0000000000..591c4dee84--- /dev/null+++ b/server/skill-burger/workflow-revisions.test.ts@@ -0,0 +1,120 @@+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync } from "node:fs";+import { createServer, type Server } from "node:http";+import type { AddressInfo } from "node:net";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { afterEach, expect, it } from "vitest";++import { workflowSchema, type Workflow } from "../../shared/skill-burger/format.ts";+import type { SavedWorkflow } from "../../shared/skill-burger/saved.ts";+import { json, readBody } from "../harness/http.ts";+import type { RequestAuth } from "../request-auth.ts";+import { dispatchRoutes } from "../routes/table.ts";+import { readSkillLibraryIndex } from "../skill-library.ts";+import { createSkillBurgerRoutes } from "./routes.ts";+import { createSkillBurgerRuns, type BurgerTurn } from "./runs.ts";+import { saveWorkflowSkill } from "./store.ts";++const servers: Server[] = [];+const roots: string[] = [];+afterEach(async () => {+ await Promise.all(servers.splice(0).map((server) => new Promise<void>((done) => server.close(() => done()))));+ roots.splice(0).forEach((root) => rmSync(root, { recursive: true, force: true }));+});++async function fixture() {+ const root = mkdtempSync(join(tmpdir(), "burger-revisions-"));+ roots.push(root);+ const cwd = join(root, "work");+ mkdirSync(cwd);+ const workflow = workflowSchema.parse({ version: 1, summary: "Invoice report", args: {}, steps: [+ { id: "s1", kind: "app", app: "gmail", tool: "GMAIL_FETCH_EMAILS", label: "Find invoices", args: { query: "invoice" } },+ { id: "s2", kind: "file", label: "Write report", path: "report.txt", content: "Invoice summary" },+ ] });+ const saved = saveWorkflowSkill({ workflow, name: "invoice-report", origin: "lead", botIds: ["lead"], root });+ if ("error" in saved) throw new Error(saved.error);+ let fetches = 0;+ const turn: BurgerTurn = {+ botId: "lead", threadId: "t", cwd, mayEditBurgers: true, publish() {},+ async internal(kind, path, body) {+ expect(kind).toBe("connectors");+ expect(path).toBe("/api/internal/connectors/mcp");+ expect(body).toMatchObject({ params: { arguments: { tools: [{ tool_slug: "GMAIL_FETCH_EMAILS", arguments: { query: "invoice" } }] } } });+ fetches += 1;+ return { ok: true, body: { result: { content: [{ type: "text", text: JSON.stringify({ data: { results: [+ { tool_slug: "GMAIL_FETCH_EMAILS", response: { successful: true, data: { messages: [{ id: "invoice-1" }] } } },+ ] } }) }] } } };+ },+ };+ const runs = createSkillBurgerRuns({ dataDir: root, root });+ const routes = [runs.internalRoutes(turn), createSkillBurgerRoutes({+ root, messages: () => [], botIds: () => ["lead"], owns: () => true, sees: () => true,+ })];+ const server = createServer(async (req, res) => {+ const url = new URL(req.url ?? "/", "http://localhost");+ const auth = { kind: "loopback", scopes: ["admin"] } as unknown as RequestAuth;+ if (!await dispatchRoutes(routes, { req, res, url, path: url.pathname, method: req.method ?? "GET", auth, json, readBody })) json(res, 404, {});+ });+ servers.push(server);+ await new Promise<void>((ready) => server.listen(0, "127.0.0.1", ready));+ const base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;+ const api = async (method: string, path: string, body?: unknown) => {+ const response = await fetch(`${base}${path}`, { method, ...(body === undefined ? {} : { headers: { "content-type": "application/json" }, body: JSON.stringify(body) }) });+ return { status: response.status, body: await response.json() as { workflow?: Workflow; text?: string; skill?: SavedWorkflow; undo?: string; error?: unknown } };+ };+ const edit = (path: string) => api("POST", "/api/internal/skill-burgers/edit", {+ name: saved.name, action: "set_when", step: "s2", when: { $step: "s1", path, op: "exists" },+ });+ const run = () => api("POST", "/api/internal/skill-burgers/run", { name: saved.name });+ return { api, edit, run, saved, instructionSha256: readSkillLibraryIndex(root)[saved.name]!.sha256, report: join(cwd, "report.txt"), fetches: () => fetches };+}++it("refuses an older undo after another condition edit, then still allows undo of the latest edit", async () => {+ const h = await fixture();+ const before = await h.api("GET", "/api/skill-burgers/invoice-report");+ const first = await h.edit("messages.0");+ const second = await h.edit("messages.1");+ expect([first.status, second.status]).toEqual([200, 200]);+ expect((await h.api("GET", "/api/skill-burgers/invoice-report")).body.text).toBe(before.body.text);+ const stale = await h.api("POST", "/api/internal/skill-burgers/undo", { undo: first.body.undo });+ expect((await h.run()).body).toMatchObject({ ok: true, status: "ok" });+ expect(h.fetches()).toBe(1);+ expect(existsSync(h.report)).toBe(false);+ expect(stale.status).toBe(409);+ expect((await h.api("GET", "/api/skill-burgers/invoice-report")).body.workflow!.steps[1]!.when).toEqual({ $step: "s1", path: "messages.1", op: "exists" });+ expect(second.body.skill!.sha256).not.toBe(first.body.skill!.sha256);++ expect((await h.api("POST", "/api/internal/skill-burgers/undo", { undo: second.body.undo })).status).toBe(200);+ expect((await h.run()).body).toMatchObject({ ok: true, status: "ok" });+ expect(readFileSync(h.report, "utf8")).toBe("Invoice summary");+});++it.each(["remove", "rename"])("refuses a stale %s after an executable-only edit, while accepting the current revision", async (action) => {+ const h = await fixture();+ const changed = await h.edit("messages.1");+ expect(changed.status).toBe(200);+ const mutate = (sha256: string) => action === "remove"+ ? h.api("DELETE", `/api/skill-burgers/invoice-report?sha256=${sha256}`)+ : h.api("PATCH", "/api/skill-burgers/invoice-report", { rename: "renamed-report", sha256 });+ const stale = await mutate(h.saved.sha256);+ expect((await h.run()).body).toMatchObject({ ok: true, status: "ok" });+ expect(h.fetches()).toBe(1);+ expect(existsSync(h.report)).toBe(false);+ expect(stale.status).toBe(409);+ const current = await mutate(changed.body.skill!.sha256);+ expect(current.status).toBe(200);+ expect((await h.api("GET", "/api/skill-burgers/invoice-report")).status).toBe(404);+ if (action === "rename") {+ expect((await h.api("GET", "/api/skill-burgers/renamed-report")).body.workflow!.steps[1]!.when).toEqual({ $step: "s1", path: "messages.1", op: "exists" });+ }+});++it("requires a refreshed token for a client holding only the old instructions hash", async () => {+ const h = await fixture();+ const old = await h.api("DELETE", `/api/skill-burgers/invoice-report?sha256=${h.instructionSha256}`);+ expect(old.status).toBe(409);+ expect((await h.run()).body).toMatchObject({ ok: true, status: "ok" });+ expect(readFileSync(h.report, "utf8")).toBe("Invoice summary");+ const current = await h.api("DELETE", `/api/skill-burgers/invoice-report?sha256=${h.saved.sha256}`);+ expect(current.status).toBe(200);+});diff --git a/shared/skill-burger/saved.ts b/shared/skill-burger/saved.tsindex 7a7d719dbe..47d6ccb906 100644--- a/shared/skill-burger/saved.ts+++ b/shared/skill-burger/saved.ts@@ -15,6 +15,7 @@ export interface SavedWorkflowLayer {export interface SavedWorkflow {name: string;summary: string;+ /** Revision of the instructions and executable workflow, used to reject stale edits. */sha256: string;savedAt: string;layers: SavedWorkflowLayer[];