Skip to content

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

fix(cli): validate forced export destinations before starting jobs

Afloat568f19d51e06
diff --git a/server/headless/export.ts b/server/headless/export.ts
index 3ac710c090..4c2083b025 100644
--- a/server/headless/export.ts
+++ b/server/headless/export.ts
@@ -56,14 +56,14 @@ function unused(dir: string, name: string, taken: Set<string>): string {
export function exportFolder(dir: string, force: boolean): string {
const out = resolve(dir);
- if (!existsSync(out) || force) return out;
+ if (!existsSync(out)) return out;
let entries: string[];
try {
entries = readdirSync(out);
} catch (error) {
throw usageError(`${out} cannot be written into (${(error as NodeJS.ErrnoException).code ?? "error"})`, "choose a folder");
}
- if (entries.length) throw usageError(`${out} already has files in it`, "choose an empty folder, or pass --force to write into it");
+ if (entries.length && !force) throw usageError(`${out} already has files in it`, "choose an empty folder, or pass --force to write into it");
return out;
}
diff --git a/server/headless/run-export.test.ts b/server/headless/run-export.test.ts
index 95a17d2506..9824c14252 100644
--- a/server/headless/run-export.test.ts
+++ b/server/headless/run-export.test.ts
@@ -1,6 +1,6 @@
import "../testing/scrub-ambient-omb-env.ts";
-import { mkdtempSync, readFileSync } from "node:fs";
+import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { createServer, type Server } from "node:http";
import type { AddressInfo } from "node:net";
import { tmpdir } from "node:os";
@@ -87,6 +87,22 @@ it("keeps a completed run successful when its export is written", async () => {
expect(sends).toBe(1);
});
+it.each([false, true])("refuses a file as the export folder before sending a job (force: %s)", async (force) => {
+ refuseExport = false;
+ const out = join(dir, `report-${sequence++}.json`);
+ writeFileSync(out, "earlier report");
+ const stdout: string[] = [];
+ const code = await runHeadless(["run", "--bot", "lead", "--task", "t1", "Write the report", "--export", out, ...(force ? ["--force"] : []), "--json"], {
+ env: { OPENMAUSBOT_URL: origin },
+ io: { out: (text) => stdout.push(text), err: () => undefined, readStdin: async () => "" },
+ });
+ expect(code).toBe(2);
+ expect(JSON.parse(stdout.join("\n"))).toMatchObject({ ok: false, exitCode: 2, error: expect.stringContaining("ENOTDIR"), hint: "choose a folder" });
+ expect(sends).toBe(0);
+ expect(exports).toBe(0);
+ expect(readFileSync(out, "utf8")).toBe("earlier report");
+});
+
it.each([["needs-input", 3], ["failed", 1]] as const)("preserves the %s job's exit code when export also fails", async (jobState, expectedCode) => {
state = jobState;
const { code, result } = await run();