Skip to content

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

fix(skill-burger): preserve command working directories on replay

Afloatd65b8b7a35b3
diff --git a/server/skill-burger/executors.ts b/server/skill-burger/executors.ts
index 8118d15a7a..87e46058fa 100644
--- a/server/skill-burger/executors.ts
+++ b/server/skill-burger/executors.ts
@@ -61,7 +61,7 @@ function runInBox(context: ExecutorContext, input: Parameters<StepExecutors["she
let child: ReturnType<typeof spawnCli>;
try {
child = spawnCli(shell.command, shell.args(input.command), {
- cwd: input.cwd && isAbsolute(input.cwd) ? input.cwd : context.cwd,
+ cwd: input.cwd ? resolvePath(context.cwd, input.cwd) : context.cwd,
env: { PATH: commandPath(), HOME: process.env.HOME, LANG: process.env.LANG, ...input.env },
stdio: ["pipe", "pipe", "pipe"],
box: { botId: context.botId },
diff --git a/server/skill-burger/working-directory.test.ts b/server/skill-burger/working-directory.test.ts
new file mode 100644
index 0000000000..c67a6e4ef9
--- /dev/null
+++ b/server/skill-burger/working-directory.test.ts
@@ -0,0 +1,47 @@
+import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { expect, it } from "vitest";
+
+import { draftWorkflow } from "../../shared/skill-burger/draft.ts";
+import type { RunMessage } from "../../shared/skill-burger/layers.ts";
+import { createExecutors } from "./executors.ts";
+import { startRun } from "./runner.ts";
+
+const oldFolder = "/Users/ana/omb/task-workspaces/ae3a6bd7-afb4-4ad1-909b-5a7b6d887ac9/a48d8ea2-161f-4ca1-a80c-0591f3082283";
+const shell = (id: string, command: string, cwd?: string): RunMessage => ({
+ id, role: "bot", kind: "activity",
+ tool: { name: "shell", ok: true, input: JSON.stringify({ command, ...(cwd ? { cwd } : {}) }) },
+});
+const recorded = (cwd: string): RunMessage[] => [
+ { id: "ask", role: "user", kind: "text", text: "Prepare the report" },
+ shell("mkdir", "mkdir -p project"),
+ shell("write", "printf report > report.txt", cwd),
+];
+
+it.each([
+ [`${oldFolder}/project`, "./project"],
+ [`C:${oldFolder.replaceAll("/", "\\")}\\project`, ".\\project"],
+])("preserves the subfolder in a recorded conversation cwd %s", (cwd, relative) => {
+ expect(draftWorkflow(recorded(cwd!))!.workflow.steps[1]).toMatchObject({ kind: "shell", cwd: relative });
+});
+
+it.skipIf(process.platform === "win32").each(["relative", "relative-dot", "conversation", "absolute"])("replays a %s working directory in the intended folder", async (kind) => {
+ const cwd = mkdtempSync(join(tmpdir(), "burger-cwd-"));
+ try {
+ const recordedCwd = kind === "relative" ? "project"
+ : kind === "relative-dot" ? "./project"
+ : kind === "conversation" ? `${oldFolder}/project`
+ : join(cwd, "project");
+ const workflow = draftWorkflow(recorded(recordedCwd))!.workflow;
+ const result = await startRun({ name: "report", botId: "b", threadId: "t", sha256: "test", workflow }, {
+ exec: createExecutors({ botId: "b", cwd, internal: async () => ({ ok: false, body: {} }) }),
+ events: { started() {}, ended() {} }, save() {},
+ });
+ expect(result).toMatchObject({ ok: true, status: "ok" });
+ expect(readFileSync(join(cwd, "project/report.txt"), "utf8")).toBe("report");
+ expect(existsSync(join(cwd, "report.txt"))).toBe(false);
+ } finally {
+ rmSync(cwd, { recursive: true, force: true });
+ }
+});
diff --git a/shared/skill-burger/draft.ts b/shared/skill-burger/draft.ts
index 9e624dcb1b..ca60e7560f 100644
--- a/shared/skill-burger/draft.ts
+++ b/shared/skill-burger/draft.ts
@@ -332,7 +332,7 @@ class Drafter {
example: redactSecretsInText(heredoc[3]!).slice(0, EXAMPLE_MAX),
});
} else if (layer.kind === "shell") {
- const cwd = typeof layer.args.cwd === "string" && !new RegExp(CONVERSATION_FOLDER.source).test(layer.args.cwd) ? layer.args.cwd : undefined;
+ const cwd = typeof layer.args.cwd === "string" ? layer.args.cwd.replace(CONVERSATION_FOLDER, ".") : undefined;
this.steps.push({ id, kind: "shell", label: label.replace(CONVERSATION_FOLDER, "."), run: this.shell(command.replace(CONVERSATION_FOLDER, ".")), ...(cwd ? { cwd } : {}) });
} else if (layer.kind === "file") {
const content = String(layer.args.content ?? "");