Files
reverse-engineering-anything/scripts/lib/verifier-file-snapshot-owner.mts
T
morluto d01ee7b19a fix(verifiers): own setup terminal and snapshot lifetimes
Join identity-verified PTY family cleanup and native exit before workspace removal, retaining failed cleanup capabilities for retry. Replace duplicate kill-and-reject loops with shared process ownership and deadlines.

Capture configuration before setup using owned file handles and compare raw bytes. Preserve interactive cancellation, terminal restoration, redirected output, and selected inputs; remove the test-only forwarding export. Verified 122 focused cases, 9 README cases, build, static checks, docs, discovery, and duplication at 3.58%.
2026-10-24 17:54:18 +08:00

82 lines
2.5 KiB
TypeScript

import { lstat, open } from "node:fs/promises";
import { ArtifactResourceScope } from "../../src/artifacts/ArtifactResourceScope.js";
import { OwnedFileHandle } from "../../src/filesystem/OwnedFileHandle.js";
import type { PackageVerifierCommands } from "./package-command-owner.mjs";
import { withVerifierCleanup } from "./verifier-cleanup.mjs";
export type VerifierFileSnapshotEntry =
| {
readonly path: string;
readonly exists: true;
readonly content: Buffer;
}
| {
readonly path: string;
readonly exists: false;
readonly content: null;
};
/** Capture source values while the command owner retains every in-flight file. */
export async function captureVerifierFileSnapshot(
commands: Pick<PackageVerifierCommands, "adopt">,
paths: readonly string[],
): Promise<readonly VerifierFileSnapshotEntry[]> {
const resources = new ArtifactResourceScope();
commands.adopt(resources, "verifier file snapshots");
return withVerifierCleanup(
() =>
Promise.all(
paths.map((path) =>
resources.run(async (): Promise<VerifierFileSnapshotEntry> => {
try {
await lstat(path);
} catch (cause: unknown) {
if (
cause instanceof Error &&
"code" in cause &&
cause.code === "ENOENT"
)
return { path, exists: false, content: null };
throw cause;
}
const handle = await open(path, "r");
const owner = new OwnedFileHandle(handle);
resources.retain({
kind: "file-handle",
resource: path,
handle: owner,
});
return {
path,
exists: true,
content: await handle.readFile(),
};
}),
),
),
[() => resources.close()],
);
}
/** Compare the same selected paths against their completed baseline facts. */
export async function assertVerifierFilesUnchanged(
commands: Pick<PackageVerifierCommands, "adopt">,
snapshot: readonly VerifierFileSnapshotEntry[],
label: string,
): Promise<void> {
const current = await captureVerifierFileSnapshot(
commands,
snapshot.map(({ path }) => path),
);
for (const [index, entry] of snapshot.entries()) {
const observed = current[index]!;
if (
entry.exists
? !observed.exists || !entry.content.equals(observed.content)
: observed.exists
)
throw new Error(`${label} changed ${entry.path}`);
}
}