test: verify CHRNX publication rollback

This commit is contained in:
chrnx committed 2026-08-13 11:59:11 -05:00
1 parent b7732a2179
commit b0e44423a1
2 files changed
+210 -19

No files matched your search

+23 -7
View File
@@ -183,6 +183,9 @@ export async function publishReviewEvidence( {
allowedParentDirectory,
capturePlan,
captureImage,
renameEntry = rename,
removeEntry = rm,
reportCleanupFailure = ( message ) => process.stderr.write( `${ message }\n` ),
} ) {
const { outputPath, parentPath, outputExists } = await assertSafeOutput(
outputDirectory,
@@ -203,29 +206,42 @@ export async function publishReviewEvidence( {
if ( await optionalLstat( backupPath ) ) {
throw new Error( 'A previous review backup must be resolved before publication.' );
}
await rename( outputPath, backupPath );
await renameEntry( outputPath, backupPath );
backupCreated = true;
}
try {
await rename( stagingPath, outputPath );
await renameEntry( stagingPath, outputPath );
published = true;
} catch ( error ) {
if ( backupCreated ) {
await rename( backupPath, outputPath );
await renameEntry( backupPath, outputPath );
backupCreated = false;
}
throw error;
}
if ( backupCreated ) {
await rm( backupPath, { recursive: true } );
backupCreated = false;
let cleanupError;
for ( let attempt = 0; attempt < 2; attempt++ ) {
try {
await removeEntry( backupPath, { recursive: true } );
backupCreated = false;
break;
} catch ( error ) {
cleanupError = error;
}
}
if ( backupCreated ) {
const detail =
cleanupError instanceof Error ? cleanupError.message : String( cleanupError );
reportCleanupFailure( `Published review backup remains at ${ backupPath}: ${ detail }` );
}
}
} finally {
if ( ! published ) {
await rm( stagingPath, { recursive: true, force: true } );
}
if ( backupCreated && ! await optionalLstat( outputPath ) ) {
await rename( backupPath, outputPath );
if ( backupCreated && ! ( await optionalLstat( outputPath ) ) ) {
await renameEntry( backupPath, outputPath );
}
}
}