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): keep missing values out of numeric rules
Afloat67a9d5c5d76b
diff --git a/server/skill-burger/numeric-rules.test.ts b/server/skill-burger/numeric-rules.test.tsnew file mode 100644index 0000000000..84a1f7e0a9--- /dev/null+++ b/server/skill-burger/numeric-rules.test.ts@@ -0,0 +1,114 @@+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } 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 { onCase, workflowSchema } from "../../shared/skill-burger/format.ts";+import { json, readBody } from "../harness/http.ts";+import type { RequestAuth } from "../request-auth.ts";+import { dispatchRoutes } from "../routes/table.ts";+import { createSkillBurgerRoutes } from "./routes.ts";+import type { RunEnvelope } from "./runner.ts";+import { createSkillBurgerRuns, RESUME_PATH, RUN_PATH } from "./runs.ts";++const roots: string[] = [];+const servers: Server[] = [];+afterEach(async () => {+ await Promise.all(servers.splice(0).map((server) => new Promise<void>((resolve, reject) => server.close((error) => error ? reject(error) : resolve()))));+ roots.splice(0).forEach((root) => rmSync(root, { recursive: true, force: true }));+});++const file = (id: string, when?: unknown) => ({ id, kind: "file", label: id, path: `${id}.txt`, content: "ran", ...(when ? { when } : {}) });+const condition = (op: string, value?: unknown) => ({ $step: "read", path: "count", op, value });++async function serve(value: unknown, tail: unknown[], check?: unknown) {+ const root = mkdtempSync(join(tmpdir(), "burger-numeric-rules-"));+ roots.push(root);+ writeFileSync(join(root, "source.json"), JSON.stringify({ count: value }));+ const workflow = workflowSchema.parse({+ version: 1, summary: "Check the reported count", args: {},+ steps: [{ id: "read", kind: "shell", label: "Read count", run: "node -e \"process.stdout.write(require('node:fs').readFileSync('source.json'))\"", ...(check ? { check } : {}) }, ...tail],+ });+ const routes = createSkillBurgerRoutes({ root, messages: () => [], owns: () => true, sees: () => true, botIds: () => ["lead"] });+ let runs = createSkillBurgerRuns({ root, dataDir: root });+ const turn = { botId: "lead", threadId: "count", cwd: root, mayEditBurgers: true, publish() {}, internal: async () => { throw new Error("unexpected external call"); } };+ const server = createServer(async (req, res) => {+ const url = new URL(req.url ?? "/", "http://localhost");+ const auth = { kind: "loopback", scopes: ["admin"] } as RequestAuth;+ if (!await dispatchRoutes([routes, runs.internalRoutes(turn)], { 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 <T>(path: string, body?: unknown) => {+ const response = await fetch(`${base}${path}`, body === undefined ? undefined : { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });+ const result = await response.json() as T;+ expect(response.ok, JSON.stringify(result)).toBe(true);+ return result;+ };+ const saved = await api<{ skill: { name: string } }>("/api/skill-burgers", { name: "check-the-count", botId: "lead", workflow });+ const read = await api<{ workflow: unknown }>(`/api/skill-burgers/${saved.skill.name}`);+ expect(workflowSchema.parse(read.workflow)).toEqual(workflow);+ return {+ root,+ ran: (id: string) => existsSync(join(root, `${id}.txt`)),+ run: () => api<RunEnvelope>(RUN_PATH, { name: saved.skill.name }),+ resume: (run: string) => {+ runs = createSkillBurgerRuns({ root, dataDir: root });+ return api<RunEnvelope>(RESUME_PATH, { run, response: "A numeric count is now available." });+ },+ };+}++it.each([null, undefined, false, "", " \t", [], [0], "Infinity", "-Infinity", "NaN"].map((value) => ({ value })))("does not run numeric branches for a count of $value", async ({ value }) => {+ const h = await serve(value, [file("below", condition("lt", 1)), file("above", condition("gt", -1)), file("complete")]);+ expect(await h.run()).toMatchObject({ ok: true, status: "ok" });+ expect(h.ran("below")).toBe(false);+ expect(h.ran("above")).toBe(false);+ expect(h.ran("complete")).toBe(true);+});++it.each([+ { value: 0, below: true }, { value: -2, below: true }, { value: 2.5, below: false }, { value: " 2.5 ", below: false },+])("keeps numeric comparison for $value", async ({ value, below }) => {+ const h = await serve(value, [file("below", condition("lt", 1)), file("above", condition("gt", 1)), file("complete")]);+ expect(await h.run()).toMatchObject({ ok: true, status: "ok" });+ expect(h.ran("below")).toBe(below);+ expect(h.ran("above")).toBe(!below);+});++it.each([null, "", [], "Infinity"].map((threshold) => ({ threshold })))("does not run a numeric branch with a nonnumeric threshold of $threshold", async ({ threshold }) => {+ const h = await serve(5, [file("below", condition("lt", threshold)), file("above", condition("gt", threshold)), file("complete")]);+ expect(await h.run()).toMatchObject({ ok: true, status: "ok" });+ expect(h.ran("below")).toBe(false);+ expect(h.ran("above")).toBe(false);+});++it("uses the otherwise case when a numeric decision has no reported count", async () => {+ const h = await serve(null, [+ { id: "choose", kind: "decide", label: "Choose a count path", cases: ["low", "unknown"], rules: [{ ...condition("lt", 1), case: "low" }], otherwise: "unknown" },+ file("low", onCase("choose", "low")), file("unknown", onCase("choose", "unknown")),+ ]);+ expect(await h.run()).toMatchObject({ ok: true, status: "ok", steps: expect.arrayContaining([{ id: "choose", label: "Choose a count path", state: "done", case: "unknown" }]) });+ expect(h.ran("low")).toBe(false);+ expect(h.ran("unknown")).toBe(true);+});++it("fails a numeric check on null and resumes from disk once the source contains zero", async () => {+ const h = await serve(null, [file("complete")], { says: "A numeric count was received and is below one", rule: { path: "count", op: "lt", value: 1 } });+ const stopped = await h.run();+ expect(stopped).toMatchObject({ ok: false, error: { type: "step_failed", step: "read", message: expect.stringContaining("its check didn't hold") } });+ expect(h.ran("complete")).toBe(false);+ writeFileSync(join(h.root, "source.json"), JSON.stringify({ count: 0 }));+ expect(await h.resume(stopped.run)).toMatchObject({ ok: true, status: "ok" });+ expect(readFileSync(join(h.root, "complete.txt"), "utf8")).toBe("ran");+});++it("leaves boolean equality and existence conditions intact", async () => {+ const h = await serve(false, [file("equals", condition("eq", false)), file("exists", condition("exists"))]);+ expect(await h.run()).toMatchObject({ ok: true, status: "ok" });+ expect(h.ran("equals")).toBe(true);+ expect(h.ran("exists")).toBe(true);+});diff --git a/server/skill-burger/runner.ts b/server/skill-burger/runner.tsindex d29e8f7825..48578d3e59 100644--- a/server/skill-burger/runner.ts+++ b/server/skill-burger/runner.ts@@ -134,13 +134,19 @@ export function resolve(node: unknown, args: Record<string, unknown>, results: Rconst asText = (value: unknown): string => (value === undefined || value === null ? "" : typeof value === "string" ? value : JSON.stringify(value));+function asNumber(value: unknown): number {+ if (typeof value !== "number" && (typeof value !== "string" || !value.trim())) return NaN;+ const number = Number(value);+ return Number.isFinite(number) ? number : NaN;+}+function compare(left: unknown, op: string, right: unknown): boolean {switch (op) {case "exists": return left !== undefined && left !== null && left !== "" && !(Array.isArray(left) && left.length === 0);case "eq": return asText(left) === asText(right);case "ne": return asText(left) !== asText(right);- case "gt": return Number(left) > Number(right);- case "lt": return Number(left) < Number(right);+ case "gt": return asNumber(left) > asNumber(right);+ case "lt": return asNumber(left) < asNumber(right);default: return false;}}diff --git a/shared/skill-burger/format.ts b/shared/skill-burger/format.tsindex e2ffb599cf..01c8853a28 100644--- a/shared/skill-burger/format.ts+++ b/shared/skill-burger/format.ts@@ -17,6 +17,7 @@ const ref = z.union([]);export type WorkflowRef = z.infer<typeof ref>;+/** Numeric gt/lt rules compare finite numbers or nonempty numeric strings. Other values do not match. */const condition = z.object({$step: z.string(), path: z.string().max(200).optional(), op: z.enum(["eq", "ne", "gt", "lt", "exists"]), value: z.unknown().optional(),}).strict();