Skip to content

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

fix(skill-burger): apply input renames without chaining

Afloat742edb655e6e
diff --git a/server/skill-burger/input-renames.test.ts b/server/skill-burger/input-renames.test.ts
new file mode 100644
index 0000000000..a351fa5165
--- /dev/null
+++ b/server/skill-burger/input-renames.test.ts
@@ -0,0 +1,100 @@
+import { 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 type { Workflow } from "../../shared/skill-burger/format.ts";
+import type { RunMessage } from "../../shared/skill-burger/layers.ts";
+import { json, readBody } from "../harness/http.ts";
+import type { RequestAuth } from "../request-auth.ts";
+import { dispatchRoutes } from "../routes/table.ts";
+import { createExecutors } from "./executors.ts";
+import { createSkillBurgerRoutes } from "./routes.ts";
+import { createRunStates } from "./run-state.ts";
+import { resumeRun, startRun, type RunDeps } from "./runner.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 call = (id: string, name: string, input: unknown): RunMessage => ({
+ id, role: "bot", kind: "activity", tool: { name, ok: true, input: JSON.stringify(input) },
+});
+const messages: RunMessage[] = [
+ { id: "ask", role: "user", kind: "text", text: "Compare https://alpha.example and https://beta.example, then write the summary." },
+ call("left", "agent_browser_open", { url: "https://alpha.example" }),
+ call("right", "agent_browser_open", { url: "https://beta.example" }),
+ call("look", "agent_browser_snapshot", {}),
+ call("record", "Bash", { command: 'printf "%s\\n%s\\n" "https://alpha.example" "https://beta.example" > selected.txt' }),
+ call("summary", "Write", { file_path: "summary.txt", content: "The comparison considers both sites and identifies their strongest features. ".repeat(5) }),
+];
+
+async function fixture() {
+ const root = mkdtempSync(join(tmpdir(), "burger-input-renames-"));
+ roots.push(root);
+ const routes = [createSkillBurgerRoutes({ root, messages: () => messages, owns: (bot, thread) => bot === "lead" && thread === "compare", sees: () => true, botIds: () => ["lead"] })];
+ 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 = (path: string, body?: unknown) => fetch(`${base}${path}`, body === undefined ? undefined : { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
+ return { root, api };
+}
+
+it.skipIf(process.platform === "win32").each([
+ { name: "swapped names", left: "url_2", right: "url" },
+ { name: "a name reused while its old owner moves", left: "url_2", right: "destination" },
+ { name: "two fresh names", left: "left", right: "right" },
+])("saves and replays distinct inputs with $name", async ({ left, right }) => {
+ const { root, api } = await fixture();
+ const draft = await (await api("/api/skill-burger-draft", { botId: "lead", threadId: "compare" })).json() as { draft: { inputs: Array<{ name: string }> } };
+ expect(draft.draft.inputs.map((input: { name: string }) => input.name)).toEqual(["url", "url_2"]);
+ const saved = await api("/api/skill-burgers", { botId: "lead", threadId: "compare", name: "compare-sites", inputs: {
+ url: { name: left, value: "https://saved-alpha.example" },
+ url_2: { name: right, value: "https://saved-beta.example" },
+ } });
+ expect(saved.status).toBe(201);
+ const savedBody = await saved.json() as { skill: { sha256: string } };
+ const read = await (await api("/api/skill-burgers/compare-sites")).json() as { workflow: Workflow };
+ const opened: string[] = [];
+ const stateDir = join(root, "states");
+ const deps: RunDeps = {
+ exec: createExecutors({ botId: "lead", cwd: root, async internal(kind, path, body) {
+ expect(kind).toBe("browser");
+ expect(path).toBe("/api/internal/browser/mcp");
+ const frame = body as { params: { name: string; arguments: { url: string } } };
+ expect(frame.params.name).toBe("agent_browser_open");
+ opened.push(frame.params.arguments.url);
+ return { ok: true, body: { result: { content: [{ type: "text", text: "Opened the fixture page" }] } } };
+ } }),
+ events: { started() {}, ended() {} }, save: (state) => createRunStates(stateDir).save(state),
+ };
+ const expected = ["https://current-alpha.example", "https://current-beta.example"];
+ const result = await startRun({ name: "compare-sites", botId: "lead", threadId: "replay", sha256: savedBody.skill.sha256, workflow: read.workflow, args: { [left]: expected[0], [right]: expected[1] } }, deps);
+ expect(result).toMatchObject({ ok: true, status: "needs_input", requiresInput: { type: "ai_step" } });
+ expect(readFileSync(join(root, "selected.txt"), "utf8")).toBe(`${expected.join("\n")}\n`);
+ expect(opened).toEqual(expected);
+ expect(result.requiresInput!.prompt).toContain(`Compare ${expected[0]} and ${expected[1]}`);
+ expect(read.workflow.args).toEqual({ [left]: { default: "https://saved-alpha.example", label: "url" }, [right]: { default: "https://saved-beta.example", label: "url" } });
+ const state = createRunStates(stateDir).take(result.run, "lead");
+ expect(state).not.toBeNull();
+ expect(await resumeRun(state!, "Both current sites were compared.", deps)).toMatchObject({ ok: true, status: "ok" });
+ expect(readFileSync(join(root, "summary.txt"), "utf8")).toBe("Both current sites were compared.");
+});
+
+it("rejects two inputs with the same final name without saving a workflow", async () => {
+ const { api } = await fixture();
+ const saved = await api("/api/skill-burgers", { botId: "lead", threadId: "compare", name: "compare-sites", inputs: { url: { name: "target" }, url_2: { name: "target" } } });
+ expect(saved.status).toBe(422);
+ expect(await saved.json()).toMatchObject({ error: "There is already an input named target." });
+ expect((await api("/api/skill-burgers/compare-sites")).status).toBe(404);
+});
diff --git a/shared/skill-burger/draft-view.ts b/shared/skill-burger/draft-view.ts
index 4eb9b05108..7ef24a9291 100644
--- a/shared/skill-burger/draft-view.ts
+++ b/shared/skill-burger/draft-view.ts
@@ -118,21 +118,21 @@ export function dependents(steps: ReadonlyArray<{ id: string; needs: readonly st
return [...out];
}
-/** Renames an arg in references, prompt placeholders and the OMB_ARG_ variables in commands. */
-function renameArg(steps: WorkflowStep[], from: string, to: string): WorkflowStep[] {
- const env = new RegExp(`OMB_ARG_${from.toUpperCase()}(?![A-Z0-9_])`, "g");
+/** Renames args in references, prompt placeholders and the OMB_ARG_ variables in commands. */
+function renameArgs(steps: WorkflowStep[], names: ReadonlyMap<string, string>): WorkflowStep[] {
+ const prompt = (text: string) => text.replace(/\{([a-z][a-z0-9_]{0,39})\}/g, (token, from: string) => names.has(from) ? `{${names.get(from)}}` : token);
const walk = (node: unknown): unknown => {
if (Array.isArray(node)) return node.map(walk);
if (!node || typeof node !== "object") return node;
const record = node as Record<string, unknown>;
- if (record.$arg === from && Object.keys(record).length === 1) return { $arg: to };
+ if (typeof record.$arg === "string" && names.has(record.$arg) && Object.keys(record).length === 1) return { $arg: names.get(record.$arg) };
return Object.fromEntries(Object.entries(record).map(([key, value]) => [key, walk(value)]));
};
return steps.map((step) => {
const renamed = walk(step) as WorkflowStep;
- if (renamed.kind === "shell") renamed.run = renamed.run.replace(env, `OMB_ARG_${to.toUpperCase()}`);
- if ((renamed.kind === "ai" || renamed.kind === "decide") && renamed.prompt) renamed.prompt = renamed.prompt.replaceAll(`{${from}}`, `{${to}}`);
- if (renamed.check && typeof renamed.check === "object") renamed.check.says = renamed.check.says.replaceAll(`{${from}}`, `{${to}}`);
+ if (renamed.kind === "shell") renamed.run = renamed.run.replace(/OMB_ARG_([A-Z][A-Z0-9_]*)/g, (token, from: string) => names.has(from.toLowerCase()) ? `OMB_ARG_${names.get(from.toLowerCase())!.toUpperCase()}` : token);
+ if ((renamed.kind === "ai" || renamed.kind === "decide") && renamed.prompt) renamed.prompt = prompt(renamed.prompt);
+ if (renamed.check && typeof renamed.check === "object") renamed.check.says = prompt(renamed.check.says);
return renamed;
});
}
@@ -148,14 +148,22 @@ export function applyChoices(workflow: Workflow, choices: DraftChoices | undefin
const missing = needsOf(step).find((id) => !kept.has(id) && workflow.steps.some((one) => one.id === id));
if (missing) return { error: `"${step.label}" uses what "${workflow.steps.find((one) => one.id === missing)!.label}" returns; leave both out or keep both.` };
}
- const args: Workflow["args"] = {};
- const used = (name: string) => JSON.stringify(steps).includes(`"$arg":"${name}"`) || JSON.stringify(steps).includes(`{${name}}`) || steps.some((step) => step.kind === "shell" && new RegExp(`OMB_ARG_${name.toUpperCase()}(?![A-Z0-9_])`).test(step.run));
- for (const [name, arg] of Object.entries(workflow.args)) {
+ const names = new Map<string, string>();
+ const taken = new Set<string>();
+ for (const name of Object.keys(workflow.args)) {
const choice: { name?: string; value?: string } | undefined = choices.inputs?.[name];
const to = choice?.name?.trim().toLowerCase().replace(/[^a-z0-9_]+/g, "_") || name;
if (!ARG_NAME.test(to)) return { error: `"${choice?.name}" can't be an input's name: use letters, numbers and _ only, starting with a letter.` };
- if (to !== name && (to in args || (to in workflow.args && !(choices.inputs?.[to]?.name)))) return { error: `There is already an input named ${to}.` };
- if (to !== name) steps = renameArg(steps, name, to);
+ if (taken.has(to)) return { error: `There is already an input named ${to}.` };
+ names.set(name, to);
+ taken.add(to);
+ }
+ steps = renameArgs(steps, names);
+ const args: Workflow["args"] = {};
+ const used = (name: string) => JSON.stringify(steps).includes(`"$arg":"${name}"`) || JSON.stringify(steps).includes(`{${name}}`) || steps.some((step) => step.kind === "shell" && new RegExp(`OMB_ARG_${name.toUpperCase()}(?![A-Z0-9_])`).test(step.run));
+ for (const [name, arg] of Object.entries(workflow.args)) {
+ const to = names.get(name)!;
+ const choice = choices.inputs?.[name];
if (!used(to)) continue;
args[to] = { ...arg, ...(choice?.value !== undefined ? { default: choice.value } : {}) };
}