From c2964a93be48a3edfef5c9278b5b8eeff4c8eef4 Mon Sep 17 00:00:00 2001 From: Jake Ruesink Date: Wed, 2 Dec 2026 04:18:57 -0600 Subject: [PATCH 1/2] gallery-sync: batch installs by dependency closure, dependencies first registry_add installs each item with its dependencies, at most four items and eight written files per call (identical files don't count). At v0.1.17 the pairings depend on font-* items, so the old batching, which counted only each item's own files, sent three pairings and a font in one call: nine writes, refused with add_too_large before any write landed. Installs now go in dependency order (fonts before the pairings that load them), and a batch counts every file its items' closures bring that no earlier batch wrote. Dry runs list the calls. v0.1.17's 56 installs fit 14 calls of at most four new files. Co-Authored-By: Claude Opus 5.5 --- scripts/gallery-sync.mjs | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/scripts/gallery-sync.mjs b/scripts/gallery-sync.mjs index 3ffa782..6a2fd8c 100644 --- a/scripts/gallery-sync.mjs +++ b/scripts/gallery-sync.mjs @@ -292,15 +292,39 @@ async function sync() { // Install every block, the type pairings the canvases load and the font items the type specimens link; the draft // keeps its own tokens. Font items are text stylesheets only: their WOFF2 binaries come from the platform's pack. const installs = catalog.items.filter(({ name, usage }) => (usage && name !== "tokens") || /^(pairing|font)-/.test(name)); + // registry_add installs each item with its dependencies: at most four items and eight written files per call, and + // files already identical on the draft don't count. So dependencies go first (fonts before the pairings that load + // them), and a batch counts every file its items bring that no earlier batch wrote. + const byName = new Map(catalog.items.map((item) => [item.name, item])); + const closure = (name, seen = new Set()) => { + if (seen.has(name) || !byName.has(name)) return seen; + seen.add(name); + for (const dependency of byName.get(name).dependencies ?? []) closure(dependency, seen); + return seen; + }; + const installing = new Set(installs.map(({ name }) => name)); + const ordered = []; + const placed = new Set(); + const place = (name) => { + if (placed.has(name)) return; + placed.add(name); + for (const dependency of byName.get(name).dependencies ?? []) if (installing.has(dependency)) place(dependency); + ordered.push(name); + }; + for (const { name } of installs) place(name); const batches = []; - for (const { name, files } of installs) { - const bytes = files.reduce((sum, file) => sum + file.bytes, 0); + const written = new Set(); + for (const name of ordered) { + const fresh = [...closure(name)].flatMap((item) => byName.get(item).files).filter(({ target }) => !written.has(target)); const last = batches.at(-1); - if (last && last.items.length < 4 && last.bytes + bytes <= 100 * 1024) { + const merged = last && new Map([...last.files, ...fresh.map((file) => [file.target, file])]); + if (last && last.items.length < 4 && merged.size <= 8 && [...merged.values()].reduce((sum, file) => sum + file.bytes, 0) <= 100 * 1024) { last.items.push(name); - last.bytes += bytes; - } else batches.push({ items: [name], bytes }); + last.files = merged; + } else batches.push({ items: [name], files: new Map(fresh.map((file) => [file.target, file])) }); + for (const { target } of fresh) written.add(target); } + console.log(`registry: ${installs.length} items in ${batches.length} registry_add calls, at most ${Math.max(...batches.map(({ files }) => files.size))} new files each.`); // Plan before writing anything. Draft writes check the file count and refuse a file or an install over // 128 KiB, but not the total: the build checks that, so a sync over the budget would write every file // and leave a draft that won't build, and an oversized file or item would fail after earlier writes @@ -374,6 +398,7 @@ async function sync() { if (dryRun) { for (const { path, expectedRevision } of writes) console.log(` ${expectedRevision ? "update" : "create"} ${path}`); for (const { path } of plan.retire) console.log(` delete ${path} (an untouched copy of a committed version)`); + for (const { items, files } of batches) console.log(` registry_add ${items.join(", ")} (${files.size} files not yet written this run)`); return; } await phase("write", async () => { From afd2a37c0ac9fc1bba522ceb21ef0ab577d1cd1a Mon Sep 17 00:00:00 2001 From: Jake Ruesink Date: Wed, 2 Dec 2026 04:30:02 -0600 Subject: [PATCH 2/2] gallery-sync: refuse a registry_add call that can't fit, before any write One item's dependency closure can't be split across calls, so the batch it starts may exceed the platform's per-call limits; merged batches are already capped. The plan now checks every planned call against the eight-file and per-call byte limits, beside the gallery files, and refuses before writing anything. The four-item and eight-file limits are named constants. Co-Authored-By: Claude Opus 5.5 --- scripts/gallery-sync.mjs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/scripts/gallery-sync.mjs b/scripts/gallery-sync.mjs index 6a2fd8c..fee054a 100644 --- a/scripts/gallery-sync.mjs +++ b/scripts/gallery-sync.mjs @@ -211,6 +211,9 @@ const bytes = (text) => Buffer.byteLength(text); // The platform refuses a file, and a registry_add or edit_site call, over 128 KiB of source. const maxFileBytes = 128 * 1024; const maxBatchBytes = 120 * 1024; +// registry_add takes at most four items and writes at most eight files per call. +const maxAddItems = 4; +const maxAddWrites = 8; // The draft's file list and source budget. A partial list would hide files from the plan, so it stops the sync. async function draftListing(siteId) { @@ -318,7 +321,7 @@ async function sync() { const fresh = [...closure(name)].flatMap((item) => byName.get(item).files).filter(({ target }) => !written.has(target)); const last = batches.at(-1); const merged = last && new Map([...last.files, ...fresh.map((file) => [file.target, file])]); - if (last && last.items.length < 4 && merged.size <= 8 && [...merged.values()].reduce((sum, file) => sum + file.bytes, 0) <= 100 * 1024) { + if (last && last.items.length < maxAddItems && merged.size <= maxAddWrites && [...merged.values()].reduce((sum, file) => sum + file.bytes, 0) <= 100 * 1024) { last.items.push(name); last.files = merged; } else batches.push({ items: [name], files: new Map(fresh.map((file) => [file.target, file])) }); @@ -356,17 +359,18 @@ async function sync() { `plan: read ${present.length} managed files${leaving.length ? ` and ${leaving.length} retired` : ""} (${current.reread} needed a second read); source ${listing.budget.bytes} of ${listing.budget.limitBytes} bytes now, ${projected} after this sync; ${files} of ${listing.budget.limitFiles} files${leaving.length ? ` (${peak} before the retired go)` : ""}.`, ); const edited = leaving.filter((path) => !released(path).has(current.get(path).content)); - // registry_add installs a whole item in one call; edit_site writes gallery files one by one at most. + // Each registry_add call writes its items' new closure files, and one item's closure can't be split across + // calls; edit_site writes gallery files one by one at most. const oversized = [ - ...installs.map(({ name, files }) => [`the ${name} item`, files.reduce((sum, file) => sum + file.bytes, 0)]), - ...paths.map((path) => [path, next.get(path)]), + ...batches.map(({ items, files }) => [`registry_add ${items.join(", ")}`, [...files.values()].reduce((sum, file) => sum + file.bytes, 0), files.size]), + ...paths.map((path) => [path, next.get(path), 1]), ] - .filter(([, size]) => size > maxFileBytes) - .map(([what, size]) => `${what} (${size} bytes)`); + .filter(([, size, count]) => size > maxFileBytes || count > maxAddWrites) + .map(([what, size, count]) => `${what} (${count > 1 ? `${count} files, ` : ""}${size} bytes)`); const problems = [ projected > listing.budget.limitBytes && `it needs ${projected - listing.budget.limitBytes} bytes more than the draft's ${listing.budget.limitBytes}-byte source budget`, peak > listing.budget.limitFiles && `it needs ${peak} files${leaving.length ? " before the retired ones go" : ""} and the draft allows ${listing.budget.limitFiles}`, - oversized.length && `the platform refuses a file or an install over ${maxFileBytes} bytes, so ${oversized.join(", ")} would fail after earlier writes landed`, + oversized.length && `the platform refuses a file over ${maxFileBytes} bytes and a registry_add call over ${maxAddWrites} files or ${maxFileBytes} bytes, so ${oversized.join(", ")} would fail after earlier writes landed`, edited.length && `${edited.join(", ")} changed on the draft after a release synced it, so the sync won't remove it; merge or delete it by hand`, ].filter(Boolean); if (problems.length) throw new Error(`This sync can't land: ${problems.join("; ")}. No registry or gallery files were written.`);