Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.
HTML agent-skillsai-agentsarchitecture-diagramclaude-codeclaude-skillscodexcoding-agentsdeepseek-harnessdeveloper-toolsdiagramsdiagrams-as-codedsh-pluginflowchartllmmermaidopencodesequence-diagramsoftware-architecturesystem-designvisualization
fix: defer SMB cleanup until handles close
sunsunsun5377c6ba5052
diff --git a/archify.zip b/archify.zipindex 17ff9ca5..e3736e21 100644Binary files a/archify.zip and b/archify.zip differdiff --git a/archify/renderers/shared/atomic-output.mjs b/archify/renderers/shared/atomic-output.mjsindex 8a0fd130..bfad9581 100644--- a/archify/renderers/shared/atomic-output.mjs+++ b/archify/renderers/shared/atomic-output.mjs@@ -366,6 +366,7 @@ export function captureRegularFileBinding(filePath, {filePath: resolvedPath,subject,expectedLinks,+ deferredCleanupDirectories: new Set(),identity,mode,sha256: inspected.content.sha256,@@ -436,7 +437,12 @@ export function releaseRegularFileBinding(binding) {captured.subject,captured.filePath,);- return failure || relation('released', `${captured.subject}-binding-released`, {+ if (failure) return failure;+ for (const directory of captured.deferredCleanupDirectories) {+ const cleanup = removeEmptyQuarantine(directory, { retry: true });+ if (cleanup) return cleanup;+ }+ return relation('released', `${captured.subject}-binding-released`, {filePath: captured.filePath,});}@@ -461,9 +467,10 @@ function createRemovalQuarantine(parentPath) {return relation('unknown', 'removal-quarantine-name-exhausted', { parentPath });}-function removeEmptyQuarantine(directory) {+function removeEmptyQuarantine(directory, { retry = false } = {}) {+ const attempts = retry ? removalCleanupAttempts : 1;let failure;- for (let attempt = 0; attempt < removalCleanupAttempts; attempt += 1) {+ for (let attempt = 0; attempt < attempts; attempt += 1) {try {fs.rmdirSync(directory);return null;@@ -472,7 +479,7 @@ function removeEmptyQuarantine(directory) {// SMB can acknowledge unlink before a following directory removal sees// the deleted entry disappear. Retrying rmdir is claimant-safe: a real// entry keeps the directory non-empty and is never removed recursively.- if (error?.code !== 'ENOTEMPTY' || attempt === removalCleanupAttempts - 1) break;+ if (error?.code !== 'ENOTEMPTY' || attempt === attempts - 1) break;Atomics.wait(removalCleanupSignal,0,@@ -486,6 +493,15 @@ function removeEmptyQuarantine(directory) {});}+function deferEmptyQuarantineCleanup(binding, cleanup) {+ if (cleanup?.reason?.code !== 'removal-quarantine-cleanup-failed'+ || cleanup.reason.systemCode !== 'ENOTEMPTY') return false;+ const captured = regularFileBindings.get(binding);+ if (!captured) return false;+ captured.deferredCleanupDirectories.add(cleanup.reason.recoveryDirectory);+ return true;+}+function removalRecovery(subject, code, filePath, quarantineDirectory, quarantineFile, error) {return {status: 'recovery-required',@@ -521,6 +537,10 @@ export function quarantineRemoveRegularFileBinding(binding, filePath, {if (quarantine.status !== 'created') return quarantine;const quarantineFile = path.join(quarantine.directory, path.basename(resolvedPath));const removeEmpty = () => removeEmptyQuarantine(quarantine.directory);+ const removeEmptyAfterFileRemoval = () => {+ const cleanup = removeEmpty();+ return cleanup && deferEmptyQuarantineCleanup(binding, cleanup) ? null : cleanup;+ };const beforeMove = verifyRegularFileBinding(binding, {filePath: resolvedPath,@@ -557,7 +577,7 @@ export function quarantineRemoveRegularFileBinding(binding, filePath, {error,);}- const cleanup = removeEmpty();+ const cleanup = removeEmptyAfterFileRemoval();return cleanup || relation('removed', `${subject}-removed`, { filePath: resolvedPath });}@@ -638,7 +658,7 @@ export function quarantineRemoveRegularFileBinding(binding, filePath, {try {restored = fs.lstatSync(resolvedPath, { bigint: true });} catch (error) {- const cleanup = removeEmpty();+ const cleanup = removeEmptyAfterFileRemoval();return cleanup || filesystemFailure(`${subject}-replacement-final-verification-failed`,error,@@ -647,12 +667,12 @@ export function quarantineRemoveRegularFileBinding(binding, filePath, {}if (restored.dev !== displaced.dev || restored.ino !== displaced.ino|| restored.nlink !== displaced.nlink) {- const cleanup = removeEmpty();+ const cleanup = removeEmptyAfterFileRemoval();return cleanup || relation('different', `${subject}-replacement-final-identity-changed`, {filePath: resolvedPath,});}- const cleanup = removeEmpty();+ const cleanup = removeEmptyAfterFileRemoval();return cleanup || relation('preserved', `${subject}-replacement-restored`, {filePath: resolvedPath,priorState: moved.reason,diff --git a/archify/test/renderer-atomic-write.test.mjs b/archify/test/renderer-atomic-write.test.mjsindex b37be316..39776ee9 100644--- a/archify/test/renderer-atomic-write.test.mjs+++ b/archify/test/renderer-atomic-write.test.mjs@@ -1285,40 +1285,83 @@ test('quarantine removal deletes only the public entry still owned by its bindin}});-test('quarantine removal retries transient remote ENOTEMPTY after deleting the owned entry', (t) => {+test('quarantine removal defers remote ENOTEMPTY until its binding handle closes', (t) => {const root = workspace(t, 'archify-quarantine-remove-remote-delay-');const target = path.join(root, 'public.lock');fs.writeFileSync(target, 'owned\n');const captured = captureRegularFileBinding(target, { subject: 'test-owned-entry' });assert.equal(captured.status, 'captured');+ const closeSync = fs.closeSync.bind(fs);const rmdirSync = fs.rmdirSync.bind(fs);+ let bindingClosed = false;let delayedAttempts = 0;+ let postCloseDelays = 0;+ t.mock.method(fs, 'closeSync', (descriptor) => {+ bindingClosed = true;+ return closeSync(descriptor);+ });t.mock.method(fs, 'rmdirSync', (directory) => {- if (path.basename(String(directory)).startsWith('.archify-remove-')- && delayedAttempts < 2) {- delayedAttempts += 1;- throw Object.assign(new Error('injected remote deletion visibility delay'), {- code: 'ENOTEMPTY',- });+ if (path.basename(String(directory)).startsWith('.archify-remove-')) {+ if (!bindingClosed) delayedAttempts += 1;+ else if (postCloseDelays < 1) postCloseDelays += 1;+ else return rmdirSync(directory);+ throw Object.assign(new Error('injected remote deletion visibility delay'), { code: 'ENOTEMPTY' });}return rmdirSync(directory);});+ let released = false;try {const removed = quarantineRemoveRegularFileBinding(captured.binding, target, {subject: 'test-owned-entry',});assert.equal(removed.status, 'removed');- assert.equal(delayedAttempts, 2);+ assert.equal(delayedAttempts, 1);assert.equal(fs.existsSync(target), false);+ assert.equal(+ fs.readdirSync(root).some((entry) => entry.startsWith('.archify-remove-')),+ true,+ );+ const release = releaseRegularFileBinding(captured.binding);+ released = true;+ assert.equal(release.status, 'released');+ assert.equal(postCloseDelays, 1);assert.deepEqual(fs.readdirSync(root).filter((entry) => entry.startsWith('.archify-remove-')),[],);} finally {- releaseRegularFileBinding(captured.binding);+ if (!released) releaseRegularFileBinding(captured.binding);}});+test('deferred quarantine cleanup preserves a real claimant and fails closed', (t) => {+ const root = workspace(t, 'archify-quarantine-remove-deferred-claimant-');+ const target = path.join(root, 'public.lock');+ fs.writeFileSync(target, 'owned\n');+ const captured = captureRegularFileBinding(target, { subject: 'test-owned-entry' });+ assert.equal(captured.status, 'captured');+ const rmdirSync = fs.rmdirSync.bind(fs);+ let claimant;+ t.mock.method(fs, 'rmdirSync', (directory) => {+ if (!claimant && path.basename(String(directory)).startsWith('.archify-remove-')) {+ claimant = path.join(String(directory), 'claimant');+ fs.writeFileSync(claimant, 'external claimant\n', { flag: 'wx' });+ }+ return rmdirSync(directory);+ });++ const removed = quarantineRemoveRegularFileBinding(captured.binding, target, {+ subject: 'test-owned-entry',+ });+ assert.equal(removed.status, 'removed');+ assert.ok(claimant);+ const release = releaseRegularFileBinding(captured.binding);+ assert.equal(release.status, 'unknown');+ assert.equal(release.reason.code, 'removal-quarantine-cleanup-failed');+ assert.equal(release.reason.systemCode, 'ENOTEMPTY');+ assert.equal(fs.readFileSync(claimant, 'utf8'), 'external claimant\n');+});+test('owned-file cleanup preserves a successor swapped at the unlink boundary', (t) => {const root = workspace(t, 'archify-owned-cleanup-successor-');const target = path.join(root, 'candidate.html');