Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172import { lstat, rmdir, unlink,} from 'node:fs/promises';
import { caughtValueText, } from '@monochromatic-dev/module-caught-value/ts';
import { entryMatches, } from './entry-compare.ts';import { filesystemPath, } from './ignored-paths.ts';import type { InstalledWorktreePath, StagedWorktreeSnapshot,} from './model.ts';
/** Reports current relation to exact post-creation filesystem identity. @param path - current destination path @param installed - durable post-creation identity @returns absent, exact identity match, or changed identity @example ```ts await installedIdentityState({ path: '/wt/cache', installed }); ``` */async function installedIdentityState({ path, installed,}: Readonly<{ path: string; installed: InstalledWorktreePath;}>,): Promise<'absent' | 'changed' | 'match'> { try { /** Current no-follow destination identity. */ const stats = await lstat( path, { bigint: true, }, ); return (stats.dev .toString() === installed.device) && (stats.ino .toString() === installed.inode) ? 'match' : 'changed'; } catch (error: unknown) { if (Error.isError(error,) && ('code' in error) && (error.code === 'ENOENT')) return 'absent'; throw error; }}
/** Removes unchanged entries created by failed installation. A proven directory is removed only when empty; a proven file or symbolic link only while it still equals the private stage.
@param snapshot - validated stage retained for ownership comparison @param destinationRoot - partially populated worktree @param created - paths created by current installation @returns paths retained because ownership could not be proven @example ```ts await rollbackCreated({ snapshot, destinationRoot: '/wt', created }); ``` */export async function rollbackCreated({ snapshot, destinationRoot, created,}: Readonly<{ snapshot: StagedWorktreeSnapshot; destinationRoot: string; created: readonly InstalledWorktreePath[];}>,): Promise<readonly string[]> { /** Selected expected entries indexed for exact comparison. */ const selectedByPath = new Map(snapshot.entries .map(function indexEntry(entry,) { return [ entry.relativePath, entry, ] as const; },),); /** Paths retained after conservative rollback. */ const retained: string[] = []; for (const installed of created.toReversed()) { /** Native installed destination path. */ const destinationPath = filesystemPath({ root: destinationRoot, repositoryPath: installed.relativePath, },); /** Selected expected entry, absent for scaffolding directory. */ const expected = selectedByPath.get(installed.relativePath,); try { /** Current relation to proven post-creation identity. */ // oxlint-disable-next-line no-await-in-loop -- exact identity gates every destructive rollback candidate const initialIdentity = await installedIdentityState({ path: destinationPath, installed, },); if (initialIdentity === 'absent') continue; if (initialIdentity === 'changed') { retained.push(`${installed.relativePath}: filesystem identity changed`,); continue; } // A proven directory still carries its private installation mode until modes are applied, // and rmdir removes it only when it is empty, so its identity is the whole ownership proof. if ((expected !== undefined) && (expected.kind !== 'directory')) { /* oxlint-disable no-await-in-loop -- ownership proof precedes each destructive rollback step */ /** Whether installed selected entry still equals private snapshot. */ const unchanged = await entryMatches({ expectedRoot: snapshot.stageRoot, actualRoot: destinationRoot, entry: expected, },); /* oxlint-enable no-await-in-loop */ if (!unchanged) { retained.push(installed.relativePath,); continue; } } /** Identity recheck narrowing replacement race before removal. */ // oxlint-disable-next-line no-await-in-loop -- replacement check must immediately precede destructive rollback const finalIdentity = await installedIdentityState({ path: destinationPath, installed, },); if (finalIdentity !== 'match') { if (finalIdentity === 'changed') retained.push(`${installed.relativePath}: filesystem identity changed`,); continue; } // oxlint-disable-next-line no-await-in-loop -- child-first rollback requires sequential path removal await ((expected?.kind === 'directory') || (!installed.selected) ? rmdir(destinationPath,) : unlink(destinationPath,)); } catch (error: unknown) { retained.push(`${installed.relativePath}: ${caughtValueText(error,)}`,); } } return retained;}