Skip to content

armgit / kiln

Public Imported from GitHub
0

Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.

fix(materials): retry transient Windows revision publication failures

Matt Kissingerae8c8df99174
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 22514102..5d3ce5d4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,9 @@ does not publish a package.
retain bounded topology facts when an explicit solid assertion fails.
- Document quantized skin transforms and consumer-owned LOD selection, and bring
demonstrated authoring and harness lessons into maintained workspace guidance.
+- Retry transient Windows permission failures when publishing material revisions,
+ using the existing bounded atomic-directory helper. Existing revisions remain
+ immutable and are accepted only after full material verification.
## 1.2.3 (2026-10-12)
diff --git a/src/material-library-node.ts b/src/material-library-node.ts
index 9f17c46c..ebeae550 100644
--- a/src/material-library-node.ts
+++ b/src/material-library-node.ts
@@ -1,9 +1,10 @@
/** Host-owned immutable material storage and byte-only evaluator delivery. No acquisition I/O. */
import { createHash, randomUUID } from 'node:crypto';
-import { lstat, mkdir, readFile, readdir, realpath, rename, rm, writeFile } from 'node:fs/promises';
+import { lstat, mkdir, readFile, readdir, realpath, rm, writeFile } from 'node:fs/promises';
import { dirname, join, relative, resolve, sep } from 'node:path';
import sharp from 'sharp';
import { DataTexture } from 'three';
+import { renameDirectoryAtomically } from './atomic-directory';
import {
assertMaterialJson,
canonicalMaterialJson,
@@ -590,7 +591,7 @@ export class FileMaterialLibrary {
flag: 'wx',
});
try {
- await rename(stage, dest);
+ await renameDirectoryAtomically(stage, dest);
} catch (error) {
const existing = await this.read(manifest.materialId, manifest.revisionId).catch(
() => undefined,
diff --git a/src/material-library-publication.test.ts b/src/material-library-publication.test.ts
new file mode 100644
index 00000000..9fd8f93c
--- /dev/null
+++ b/src/material-library-publication.test.ts
@@ -0,0 +1,200 @@
+import { afterEach, expect, spyOn, test } from 'bun:test';
+import { lstat, mkdtemp, readFile, readdir, rename, rm, writeFile } from 'node:fs/promises';
+import { tmpdir } from 'node:os';
+import { join, resolve, sep } from 'node:path';
+import * as atomicDirectory from './atomic-directory';
+import { canonicalMaterialJson } from './material-library';
+import { createMaterialRecordV1, FileMaterialLibrary } from './material-library-node';
+
+const publish = atomicDirectory.renameDirectoryAtomically;
+const roots: string[] = [];
+let publicationSpy:
+ | ReturnType<typeof spyOn<typeof atomicDirectory, 'renameDirectoryAtomically'>>
+ | undefined;
+afterEach(async () => {
+ publicationSpy?.mockRestore();
+ publicationSpy = undefined;
+ for (const root of roots.splice(0)) {
+ if (!resolve(root).startsWith(resolve(tmpdir()) + sep)) throw new Error('Invalid test root');
+ await rm(root, { recursive: true, force: true });
+ }
+});
+
+async function fixture() {
+ const root = await mkdtemp(join(tmpdir(), 'kiln-material-publication-'));
+ roots.push(root);
+ const record = await createMaterialRecordV1({
+ materialId: 'plaster',
+ name: 'Plaster',
+ tileable: true,
+ sources: [
+ {
+ id: 'authored',
+ kind: 'procedural',
+ provider: 'Kiln',
+ creator: 'Test author',
+ license: {
+ spdx: 'CC0-1.0',
+ url: 'https://creativecommons.org/publicdomain/zero/1.0/',
+ attribution: '',
+ },
+ originalFiles: [],
+ },
+ ],
+ maps: [
+ {
+ slot: 'baseColor',
+ sourceId: 'authored',
+ transforms: [],
+ procedural: {
+ schemaVersion: 2,
+ usage: 'albedo',
+ size: 8,
+ layers: [
+ {
+ op: 'noise',
+ colorA: 0xb9a89a,
+ colorB: 0xe5d8c9,
+ seed: 17,
+ scale: 4,
+ octaves: 2,
+ },
+ ],
+ },
+ },
+ ],
+ });
+ const parent = join(root, record.manifest.materialId);
+ const destination = join(parent, record.manifest.revisionId.slice(7));
+ return {
+ root,
+ record,
+ parent,
+ destination,
+ library: new FileMaterialLibrary(root),
+ };
+}
+
+test('material publication retries transient Windows denial with intact staged bytes', async () => {
+ const { record, parent, destination, library } = await fixture();
+ const denied = Object.assign(new Error('sharing violation'), {
+ code: 'EPERM',
+ });
+ let attempts = 0;
+ const delays: number[] = [];
+ publicationSpy = spyOn(atomicDirectory, 'renameDirectoryAtomically').mockImplementation(
+ (stage, dest) =>
+ publish(stage, dest, {
+ platform: 'win32',
+ renameOperation: async (from, to) => {
+ expect(await readFile(join(stage, 'manifest.json'), 'utf8')).toBe(
+ canonicalMaterialJson(record.manifest),
+ );
+ expect(new Uint8Array(await readFile(join(stage, 'baseColor.png')))).toEqual(
+ Uint8Array.from(record.files['baseColor.png']!),
+ );
+ if (++attempts < 3) throw denied;
+ await rename(from, to);
+ },
+ pause: async (milliseconds) => {
+ delays.push(milliseconds);
+ expect(await lstat(dest).catch(() => undefined)).toBeUndefined();
+ },
+ }),
+ );
+ expect(await library.import([record])).toEqual([record.manifest]);
+ // These assertions also fail if the publisher bypasses the fault-injected helper.
+ expect(attempts).toBe(3);
+ expect(delays).toEqual([20, 50]);
+ expect(await library.read(record.manifest.materialId, record.manifest.revisionId)).toEqual(
+ record,
+ );
+ expect(await readdir(parent)).toEqual([record.manifest.revisionId.slice(7)]);
+ expect(await readFile(join(destination, 'manifest.json'), 'utf8')).toBe(
+ canonicalMaterialJson(record.manifest),
+ );
+});
+
+test('persistent Windows publication denial rejects and removes only the staging directory', async () => {
+ const { record, parent, destination, library } = await fixture();
+ const denied = Object.assign(new Error('permission denied'), {
+ code: 'EPERM',
+ });
+ let attempts = 0;
+ let waited = 0;
+ publicationSpy = spyOn(atomicDirectory, 'renameDirectoryAtomically').mockImplementation(
+ (stage, dest) =>
+ publish(stage, dest, {
+ platform: 'win32',
+ renameOperation: async () => {
+ expect(new Uint8Array(await readFile(join(stage, 'baseColor.png')))).toEqual(
+ Uint8Array.from(record.files['baseColor.png']!),
+ );
+ attempts++;
+ throw denied;
+ },
+ pause: async (milliseconds) => {
+ waited += milliseconds;
+ },
+ }),
+ );
+ await expect(library.import([record])).rejects.toBe(denied);
+ expect(attempts).toBe(6);
+ expect(waited).toBe(770);
+ expect(await lstat(destination).catch(() => undefined)).toBeUndefined();
+ expect(await readdir(parent)).toEqual([]);
+});
+
+test('a corrupted existing material revision is rejected without replacing its files', async () => {
+ const { record, parent, destination, library } = await fixture();
+ await library.import([record]);
+ const mapPath = join(destination, 'baseColor.png');
+ await writeFile(mapPath, 'corrupted existing material');
+ const manifestBefore = await readFile(join(destination, 'manifest.json'));
+ const collision = Object.assign(new Error('destination exists'), {
+ code: 'EPERM',
+ });
+ let attempts = 0;
+ publicationSpy = spyOn(atomicDirectory, 'renameDirectoryAtomically').mockImplementation(
+ (stage, dest) =>
+ publish(stage, dest, {
+ platform: 'win32',
+ renameOperation: async () => {
+ attempts++;
+ throw collision;
+ },
+ pause: async () => {
+ throw new Error('Existing destination must not retry');
+ },
+ }),
+ );
+ await expect(library.import([record])).rejects.toBe(collision);
+ expect(attempts).toBe(1);
+ expect(await readFile(mapPath, 'utf8')).toBe('corrupted existing material');
+ expect(await readFile(join(destination, 'manifest.json'))).toEqual(manifestBefore);
+ expect(await readdir(parent)).toEqual([record.manifest.revisionId.slice(7)]);
+ expect((await readdir(destination)).sort()).toEqual(['baseColor.png', 'manifest.json']);
+});
+
+test('concurrent identical material imports publish one verified revision and clean both stages', async () => {
+ const { record, parent, library } = await fixture();
+ let arrivals = 0;
+ let release!: () => void;
+ const ready = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ publicationSpy = spyOn(atomicDirectory, 'renameDirectoryAtomically').mockImplementation(
+ async (stage, dest) => {
+ if (++arrivals === 2) release();
+ await ready;
+ await publish(stage, dest);
+ },
+ );
+ const results = await Promise.all([library.import([record]), library.import([record])]);
+ expect(arrivals).toBe(2);
+ expect(results).toEqual([[record.manifest], [record.manifest]]);
+ expect(await library.read(record.manifest.materialId, record.manifest.revisionId)).toEqual(
+ record,
+ );
+ expect(await readdir(parent)).toEqual([record.manifest.revisionId.slice(7)]);
+});