Skip to content

An agent skill for creating detailed, interactive isometric illustrations of real-world machines and complex systems. Generates polished SVGs with optional WebGL shaders and 3D effects.

fix(publish): keep the existing figure when a --force publish fails

Rudy Celekli21f0abfb1153
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8c9e657..d8236c1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@ All notable changes to Anatomy are recorded here. The format follows [Keep a Cha
### Fixed
+- `publish.mjs --force` keeps the figure it was replacing when the new publish fails (a `--pages` file that does not exist, files that reach outside the folder, a failed build), and a missing `--pages` file now says `page X not found` instead of a bare `ENOENT`.
- `capture.mjs` waits for the page to load before it touches it, even with `--wait 0`, and gives up with an error when Chrome leaves a request unanswered for 60 s instead of hanging.
- `drive.mjs` `until` times a condition inside the page, so one that is already true passes however short the wait, even on a slow machine; a promise that never settles still times out.
- Start headless Chrome on Windows from an elevated terminal: `capture.mjs`, `drive.mjs` and `turn-check.mjs` pass `--do-not-de-elevate` there. Chrome otherwise relaunches itself without the elevation, the process the script started exits with code 0, and the check stops with `Chrome exited before startup: 0` while the relaunched browser stays running.
diff --git a/scripts/publish.mjs b/scripts/publish.mjs
index 956ab2c..495f922 100644
--- a/scripts/publish.mjs
+++ b/scripts/publish.mjs
@@ -1,5 +1,5 @@
import { spawnSync } from "node:child_process";
-import { cpSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
+import { cpSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSync, renameSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
import { fileURLToPath } from "node:url";
import { fingerprintOfSkill } from "./fingerprint.mjs";
@@ -91,6 +91,7 @@ function detectPages(source, files, given) {
const html = files.filter((file) => !file.includes("/") && file.endsWith(".html"));
if (given) {
const named = given.filter(Boolean);
+ for (const page of named) if (!files.includes(page)) throw new Error(`page ${page} not found in ${source}; build the figure first`);
const pages = {};
for (const page of named) pages[themeOf(source, page) === "light" ? "light" : themeOf(source, page) === "dark" ? "dark" : page.replace(/\.html$/, "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 24)] = page;
return pages;
@@ -145,88 +146,98 @@ export function publish(options) {
const dest = join(repo, "figures", author.toLowerCase(), options.id);
if (existsSync(dest)) {
if (!options.force) throw new Error(`figures/${author.toLowerCase()}/${options.id} already exists; pick another --id or pass --force to replace it`);
- rmSync(dest, { recursive: true, force: true });
}
+ const replaced = existsSync(dest) ? `${dest}.replaced` : null;
+ if (replaced) {
+ rmSync(replaced, { recursive: true, force: true });
+ renameSync(dest, replaced);
+ }
+ try {
+ const all = listFiles(source);
+ const typed = all.filter((file) => SOURCE_TYPES.includes(extname(file).toLowerCase()));
+ const pages = detectPages(source, typed, options.pages);
+ const lightPage = pages.light ?? null;
+ for (const page of Object.values(pages)) if (page && !typed.includes(page)) throw new Error(`page ${page} not found in ${source}; build the figure first`);
+ const outputs = new Set(Object.values(pages).filter(Boolean));
+ const files = typed.filter((file) => !outputs.has(file) && !/-verify(?:-light)?\.html$/.test(file));
+ const skipped = all.filter((file) => !files.includes(file) && !outputs.has(file));
- const all = listFiles(source);
- const typed = all.filter((file) => SOURCE_TYPES.includes(extname(file).toLowerCase()));
- const pages = detectPages(source, typed, options.pages);
- const lightPage = pages.light ?? null;
- for (const page of Object.values(pages)) if (page && !typed.includes(page)) throw new Error(`page ${page} not found in ${source}; build the figure first`);
- const outputs = new Set(Object.values(pages).filter(Boolean));
- const files = typed.filter((file) => !outputs.has(file) && !/-verify(?:-light)?\.html$/.test(file));
- const skipped = all.filter((file) => !files.includes(file) && !outputs.has(file));
+ const skillLink = join(repo, ".skill");
+ const kit = fingerprintOfSkill(SKILL);
+ const outside = [];
+ for (const file of files) {
+ const from = join(source, file);
+ const to = join(dest, "src", file);
+ mkdirSync(dirname(to), { recursive: true });
+ if (/\.m?js$/.test(file)) {
+ const result = rewriteSkillPaths(readFileSync(from, "utf8"), { fromDir: dirname(from), toDir: dirname(to), source, skill: SKILL, skillLink });
+ outside.push(...result.outside.map((path) => `${file}: ${path}`));
+ writeFileSync(to, result.text);
+ } else cpSync(from, to);
+ }
+ if (outside.length) {
+ rmSync(dest, { recursive: true, force: true });
+ throw new Error(`these files reach outside the figure folder and the skill; move what they need into the folder:\n ${outside.join("\n ")}`);
+ }
- const skillLink = join(repo, ".skill");
- const kit = fingerprintOfSkill(SKILL);
- const outside = [];
- for (const file of files) {
- const from = join(source, file);
- const to = join(dest, "src", file);
- mkdirSync(dirname(to), { recursive: true });
- if (/\.m?js$/.test(file)) {
- const result = rewriteSkillPaths(readFileSync(from, "utf8"), { fromDir: dirname(from), toDir: dirname(to), source, skill: SKILL, skillLink });
- outside.push(...result.outside.map((path) => `${file}: ${path}`));
- writeFileSync(to, result.text);
- } else cpSync(from, to);
- }
- if (outside.length) {
- rmSync(dest, { recursive: true, force: true });
- throw new Error(`these files reach outside the figure folder and the skill; move what they need into the folder:\n ${outside.join("\n ")}`);
- }
+ let linked = "existing";
+ if (!existsSync(skillLink)) {
+ symlinkSync(SKILL, skillLink, process.platform === "win32" ? "junction" : "dir");
+ linked = "created";
+ } else if (fingerprintOfSkill(skillLink) !== kit) {
+ const where = lstatSync(skillLink).isSymbolicLink() ? readlinkSync(skillLink) : skillLink;
+ throw new Error(`${repo}/.skill (${where}) is a different kit version than this skill; remove it and run again`);
+ }
- let linked = "existing";
- if (!existsSync(skillLink)) {
- symlinkSync(SKILL, skillLink, process.platform === "win32" ? "junction" : "dir");
- linked = "created";
- } else if (fingerprintOfSkill(skillLink) !== kit) {
- const where = lstatSync(skillLink).isSymbolicLink() ? readlinkSync(skillLink) : skillLink;
- throw new Error(`${repo}/.skill (${where}) is a different kit version than this skill; remove it and run again`);
- }
+ const build = [];
+ if (pages.light) build.push([`src/${options.build}`, "--light"]);
+ if (Object.keys(pages).some((name) => name !== "light")) build.push([`src/${options.build}`]);
+ for (const command of build) {
+ log(`building ${command.includes("--light") ? "the light page" : "the page"} (heavy figures take a minute or two)…`);
+ run(command, dest);
+ }
+ let preview = null;
+ if (!options.noPreview) {
+ log("taking a preview…");
+ const shots = join(repo, "shots", `${author.toLowerCase()}-${options.id}`);
+ mkdirSync(shots, { recursive: true });
+ preview = join(shots, "preview.png");
+ run([join(SKILL, "scripts", "capture.mjs"), join(dest, "src", lightPage ?? Object.values(pages)[0]), "--reduced", "--width", "1200", "--out", preview], dest);
+ }
+ const measured = options.noSession || (options.minutes && options.tokens) ? null : fromThisSession({ prompt: options.prompt });
+ const minutes = options.minutes ?? measured?.minutes;
+ const tokens = options.tokens ?? measured?.tokens;
+ if (measured) log(`time and tokens from this session: ${describe(measured)}`);
+ const pageText = Object.values(pages).filter(Boolean).map((page) => readFileSync(join(dest, "src", page), "utf8")).join("\n");
+ for (const page of outputs) rmSync(join(dest, "src", page), { force: true });
+ const figure = {
+ id: options.id,
+ title: options.title.trim(),
+ author,
+ prompt: options.prompt.trim(),
+ kind: options.kind ?? detectKind(pageText),
+ agent: options.agent,
+ ...(options.model ? { model: options.model } : {}),
+ ...(minutes ? { minutes } : {}),
+ ...(tokens ? { tokens } : {}),
+ skill: { kit },
+ build,
+ pages: Object.fromEntries(Object.entries(pages).filter(([, page]) => page).map(([key, page]) => [key, `src/${page}`])),
+ preview: lightPage ? "light" : Object.keys(pages)[0],
+ ...(options.audit ? { audit: true } : {}),
+ license: options.license,
+ ...(options.tags.length ? { tags: options.tags } : {}),
+ created: options.date ?? new Date().toISOString().slice(0, 10),
+ };
+ writeFileSync(join(dest, "figure.json"), JSON.stringify(figure, null, 2) + "\n");
- const build = [];
- if (pages.light) build.push([`src/${options.build}`, "--light"]);
- if (Object.keys(pages).some((name) => name !== "light")) build.push([`src/${options.build}`]);
- for (const command of build) {
- log(`building ${command.includes("--light") ? "the light page" : "the page"} (heavy figures take a minute or two)…`);
- run(command, dest);
- }
- let preview = null;
- if (!options.noPreview) {
- log("taking a preview…");
- const shots = join(repo, "shots", `${author.toLowerCase()}-${options.id}`);
- mkdirSync(shots, { recursive: true });
- preview = join(shots, "preview.png");
- run([join(SKILL, "scripts", "capture.mjs"), join(dest, "src", lightPage ?? Object.values(pages)[0]), "--reduced", "--width", "1200", "--out", preview], dest);
+ if (replaced) rmSync(replaced, { recursive: true, force: true });
+ return { dest, figure, skipped, linked, preview, measured };
+ } catch (error) {
+ rmSync(dest, { recursive: true, force: true });
+ if (replaced) renameSync(replaced, dest);
+ throw error;
}
- const measured = options.noSession || (options.minutes && options.tokens) ? null : fromThisSession({ prompt: options.prompt });
- const minutes = options.minutes ?? measured?.minutes;
- const tokens = options.tokens ?? measured?.tokens;
- if (measured) log(`time and tokens from this session: ${describe(measured)}`);
- const pageText = Object.values(pages).filter(Boolean).map((page) => readFileSync(join(dest, "src", page), "utf8")).join("\n");
- for (const page of outputs) rmSync(join(dest, "src", page), { force: true });
- const figure = {
- id: options.id,
- title: options.title.trim(),
- author,
- prompt: options.prompt.trim(),
- kind: options.kind ?? detectKind(pageText),
- agent: options.agent,
- ...(options.model ? { model: options.model } : {}),
- ...(minutes ? { minutes } : {}),
- ...(tokens ? { tokens } : {}),
- skill: { kit },
- build,
- pages: Object.fromEntries(Object.entries(pages).filter(([, page]) => page).map(([key, page]) => [key, `src/${page}`])),
- preview: lightPage ? "light" : Object.keys(pages)[0],
- ...(options.audit ? { audit: true } : {}),
- license: options.license,
- ...(options.tags.length ? { tags: options.tags } : {}),
- created: options.date ?? new Date().toISOString().slice(0, 10),
- };
- writeFileSync(join(dest, "figure.json"), JSON.stringify(figure, null, 2) + "\n");
-
- return { dest, figure, skipped, linked, preview, measured };
}
export function submitPlan({ login, canPush, upstream, author, id, title }) {
diff --git a/scripts/publish.test.mjs b/scripts/publish.test.mjs
index 3499a8a..9bc4f20 100644
--- a/scripts/publish.test.mjs
+++ b/scripts/publish.test.mjs
@@ -1,6 +1,6 @@
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
-import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import test from "node:test";
@@ -78,3 +78,18 @@ test("--submit: people who can push get a branch; everyone else a fork made for
assert.equal(guest.steps.at(-1).pr.head, "Octo-Cat:figure/token-bucket");
assert.ok(guest.steps.some((s) => s.git?.[0] === "add" && s.git.at(-1) === "figures/octo-cat/token-bucket"), "only the figure's folder is added");
});
+
+test("a failed --force publish leaves the existing figure as it was", () => {
+ const repo = gallery();
+ const common = { source: join(SKILL, "examples", "dial-indicator"), into: repo, id: "dial-indicator", title: "Dial indicator", prompt: "/anatomy a dial indicator", author: "octo-cat", agent: "Codex", license: "MIT", build: "build.mjs", tags: [], date: "2026-10-12", noPreview: true };
+ try {
+ const { dest } = publish(common);
+ const before = readFileSync(join(dest, "figure.json"), "utf8");
+ assert.throws(() => publish({ ...common, force: true, pages: ["missing.html"] }), /page missing\.html not found/);
+ assert.equal(readFileSync(join(dest, "figure.json"), "utf8"), before);
+ assert.ok(existsSync(join(dest, "src", "build.mjs")));
+ assert.deepEqual(readdirSync(join(repo, "figures", "octo-cat")), ["dial-indicator"]);
+ } finally {
+ rmSync(repo, { recursive: true, force: true });
+ }
+});