Skip to content

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

fix(cli): validate job exports before replacing saved files

Afloat1e37a1227693
diff --git a/server/headless/export.test.ts b/server/headless/export.test.ts
index 02ca91cd90..41d01c0e83 100644
--- a/server/headless/export.test.ts
+++ b/server/headless/export.test.ts
@@ -208,4 +208,17 @@ describe("export", () => {
const missing = await run(["export", "--thread", LEAD, "--out", join(dir, "none"), "--root", "nope", "--json"]);
expect(missing).toMatchObject({ code: 2, result: { error: "no job with root nope in this conversation" } });
});
+
+ it("leaves an earlier export intact when --force is paired with an unknown job root", async () => {
+ const out = join(dir, "invalid-root");
+ expect((await run(["export", "--thread", LEAD, "--out", out, "--json"])).code).toBe(0);
+ const saved = [
+ "manifest.json", "job.json", "transcripts/conversation-lead-thr.json", "transcripts/conversation-lead-thr.md",
+ "files/conversation-lead-thr/report.md", "screens/conversation-lead-thr/001-s1.png",
+ ].map((file) => ({ file, bytes: readFileSync(join(out, file)) }));
+
+ const refused = await run(["export", "--thread", LEAD, "--out", out, "--force", "--root", "nope", "--json"]);
+ expect(refused).toMatchObject({ code: 2, result: { error: "no job with root nope in this conversation" } });
+ for (const { file, bytes } of saved) expect(readFileSync(join(out, file))).toEqual(bytes);
+ });
});
diff --git a/server/headless/export.ts b/server/headless/export.ts
index c12c4b006a..3ac710c090 100644
--- a/server/headless/export.ts
+++ b/server/headless/export.ts
@@ -131,13 +131,13 @@ const refusal = async (response: Response | null): Promise<string> =>
export async function exportJob(client: HeadlessClient, options: ExportOptions, output: Output): Promise<Rec> {
const out = exportFolder(options.out, options.force === true);
- if (options.force) removeEarlierExport(out);
const maxFiles = options.maxFiles ?? 200;
const maxScreens = options.maxScreens ?? 100;
const maxBytes = options.maxBytes ?? 500_000_000;
const job = await readJob(client, options.threadId, { since: String(options.since ?? 0), full: "1" });
const nodes: Rec[] = (job.nodes ?? []).filter((node: Rec) => !options.root || node.rootId === options.root);
if (options.root && !nodes.length) throw usageError(`no job with root ${options.root} in this conversation`, "openmausbot export TARGET without --root writes all of it");
+ if (options.force) removeEarlierExport(out);
for (const dir of ["transcripts", "files", "screens"]) mkdirSync(join(out, dir), { recursive: true });
const threads = new Map<string, string>([[options.threadId, "conversation"]]);