mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 09:34:20 +00:00
Fixes #4379. Observe cancellation after file metadata, verified descriptor closure and recovered scope cleanup. Retain completed physical read outcomes and acquired-file digests under cancellation; preserve original failures and exact cleanup owners when close fails. Bind cancellation markers to their verified owner so raw readers preserve nested callback outcomes and exact caller reasons. Reuse one ADB partial-result mapper for file, package and bugreport acquisition. Validated with 570 related tests, source/docs checks, real Linux filesystem cancellation/cleanup/nesting probes and actual public CLI/MCP Evidence workflows.
173 lines
5.4 KiB
TypeScript
173 lines
5.4 KiB
TypeScript
import type { Stats } from "node:fs";
|
|
import { lstat, stat, type FileHandle } from "node:fs/promises";
|
|
|
|
import {
|
|
sameRegularFileState,
|
|
RegularFileChangedError,
|
|
} from "../filesystem/RegularFile.js";
|
|
import {
|
|
RegularFileCleanupFailure,
|
|
RegularFileCancelled,
|
|
withOwnedRegularFile,
|
|
type RegularFileOperationOutcome,
|
|
} from "../filesystem/OwnedRegularFile.js";
|
|
import { readBoundedFileBytes } from "../process/BoundedFileBytes.js";
|
|
import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
|
|
import type { AnalysisCleanupObservation } from "../domain/analysisErrorBase.js";
|
|
|
|
/** Filesystem admission policy for one selected regular input. */
|
|
export interface RegularFileReadOptions {
|
|
readonly signal?: AbortSignal | undefined;
|
|
readonly symlinks?: "follow" | "reject";
|
|
readonly resources?: ArtifactResourceScope;
|
|
}
|
|
|
|
export class InvalidUtf8FileError extends TypeError {
|
|
readonly _tag = "InvalidUtf8FileError";
|
|
constructor(
|
|
readonly path: string,
|
|
cause: unknown,
|
|
) {
|
|
super(`File is not valid UTF-8: ${path}`, { cause });
|
|
this.name = this._tag;
|
|
}
|
|
}
|
|
|
|
export { RegularFileCleanupFailure } from "../filesystem/OwnedRegularFile.js";
|
|
|
|
export type RegularFileReadOutcome<Value> = RegularFileOperationOutcome<Value>;
|
|
|
|
/** Retry a failed close through its retained owner and report any remaining cleanup. */
|
|
export const retryRegularFileCleanup = async <Value>(
|
|
failure: RegularFileCleanupFailure<Value>,
|
|
resources: ArtifactResourceScope,
|
|
): Promise<{
|
|
readonly outcome: RegularFileReadOutcome<Value>;
|
|
readonly cleanup?: AnalysisCleanupObservation;
|
|
}> => {
|
|
const released = await resources.release({
|
|
kind: "file-handle",
|
|
handle: failure.owner,
|
|
resource: failure.path,
|
|
});
|
|
if (released.kind === "released") return { outcome: failure.outcome };
|
|
return {
|
|
outcome: failure.outcome,
|
|
cleanup: {
|
|
reason: `${failureMessage(failure.cleanupCause)}; retry: ${failureMessage(released.cause)}`,
|
|
resources: [failure.path],
|
|
},
|
|
};
|
|
};
|
|
|
|
const failureMessage = (cause: unknown): string =>
|
|
cause instanceof Error ? cause.message : String(cause);
|
|
|
|
/** Admit one regular-file handle, retain it through the consumer, and always close it. */
|
|
export const withRegularFile = async <Value>(
|
|
path: string,
|
|
read: (handle: FileHandle, stats: Stats) => Promise<Value>,
|
|
options: RegularFileReadOptions = {},
|
|
): Promise<Value> => {
|
|
const { signal, resources } = options;
|
|
let admitted: FileHandle | undefined;
|
|
const operation = async () => {
|
|
try {
|
|
return await withOwnedRegularFile(
|
|
path,
|
|
{
|
|
...(signal === undefined ? {} : { signal }),
|
|
symlinks: options.symlinks ?? "follow",
|
|
},
|
|
async (handle) => {
|
|
admitted = handle;
|
|
const stats = await handle.stat();
|
|
signal?.throwIfAborted();
|
|
const value = await read(handle, stats);
|
|
signal?.throwIfAborted();
|
|
const [opened, currentPath] = await Promise.all([
|
|
handle.stat(),
|
|
options.symlinks === "reject" ? lstat(path) : stat(path),
|
|
]);
|
|
if (
|
|
!sameRegularFileState(stats, opened) ||
|
|
!sameRegularFileState(stats, currentPath)
|
|
)
|
|
throw new RegularFileChangedError(path);
|
|
return value;
|
|
},
|
|
);
|
|
} catch (cause: unknown) {
|
|
if (signal?.aborted === true && cause === signal.reason) throw cause;
|
|
if (
|
|
cause instanceof RegularFileCancelled &&
|
|
cause.owner.handle === admitted
|
|
)
|
|
throw cause.reason;
|
|
if (
|
|
!(cause instanceof RegularFileCleanupFailure) ||
|
|
resources === undefined
|
|
)
|
|
throw cause;
|
|
const retry = await retryRegularFileCleanup(cause, resources);
|
|
if (retry.cleanup !== undefined) throw cause;
|
|
if (retry.outcome.kind === "failed") throw retry.outcome.cause;
|
|
return retry.outcome.value;
|
|
}
|
|
};
|
|
const value = await (resources === undefined
|
|
? operation()
|
|
: resources.run(operation));
|
|
// A retained close may have recovered after the descriptor owner's outcome
|
|
// was captured. Include that retry and the scope's finalization in cancellation.
|
|
signal?.throwIfAborted();
|
|
return value;
|
|
};
|
|
|
|
/** Admit a regular file without waiting for a pipe, then read its verified handle. */
|
|
export const readRegularFile = (
|
|
path: string,
|
|
options: RegularFileReadOptions = {},
|
|
): Promise<Buffer> => readRegularFileContent(path, (bytes) => bytes, options);
|
|
|
|
/** Read valid UTF-8 without replacing source bytes or stripping an initial BOM. */
|
|
export const readRegularFileText = (
|
|
path: string,
|
|
options: RegularFileReadOptions = {},
|
|
): Promise<string> =>
|
|
readRegularFileContent(
|
|
path,
|
|
(bytes) => {
|
|
try {
|
|
return new TextDecoder("utf-8", {
|
|
fatal: true,
|
|
ignoreBOM: true,
|
|
}).decode(bytes);
|
|
} catch (cause: unknown) {
|
|
if (!(cause instanceof TypeError)) throw cause;
|
|
throw new InvalidUtf8FileError(path, cause);
|
|
}
|
|
},
|
|
options,
|
|
);
|
|
|
|
const readRegularFileContent = <Value>(
|
|
path: string,
|
|
decode: (bytes: Buffer) => Value,
|
|
options: RegularFileReadOptions,
|
|
): Promise<Value> =>
|
|
withRegularFile(
|
|
path,
|
|
async (handle, stats) => {
|
|
const bytes = await readBoundedFileBytes(
|
|
handle,
|
|
stats.size,
|
|
options.signal,
|
|
);
|
|
if (bytes === undefined || bytes.length !== stats.size)
|
|
throw new RegularFileChangedError(path);
|
|
return decode(bytes);
|
|
},
|
|
options,
|
|
);
|