fix(managed): retain descriptor ownership through hash cancellation

Fixes #4381. Follow-up to the Linux lifecycle audit in #4227.

Keep managed source hashing on bounded reads through its existing FileHandle owner. Preserve exact opaque cancellation reasons before filesystem or cleanup interpretation, retain genuine failed closes, block unsafe admission, and recover the same provider after verified cleanup.

Validated with 484 related tests, source/docs checks, seven real Linux provider cancellation/recovery scenarios using an unchanged official Microsoft .NET assembly, and paired actual CLI/stdio MCP Evidence workflows. Normal completed-task timing, CPU, memory and token proxies remained comparable.

Adopts xu91102/rea commit ebeec580c839f2f72144282a44f4e11ea5759843 with independent Linux verification and cancellation-reason hardening.

Co-authored-by: hhang <[email protected]>
This commit is contained in:
Ysopwnableandhhang authored and GitHub committed 2026-10-16 18:49:10 +08:00
1 parent c884864876
commit fe2a398dd9
2 files changed
+212 -23

No files matched your search

+33 -23
View File
@@ -4,8 +4,8 @@ import type { FileHandle } from "node:fs/promises";
import { MANAGED_STATIC_PROVIDER } from "../application/InvestigationProviders.js";
import { classifyBinaryTargetFilesystemFailure } from "../application/BinaryTargetResolver.js";
import { withRegularFile } from "../application/RegularFileRead.js";
import {
withRegularFile,
RegularFileCleanupFailure,
type RegularFileReadOptions,
} from "../application/RegularFileRead.js";
@@ -87,18 +87,21 @@ export const hashManagedSource = async (
throw managedSnapshotCapacityError(operation);
const digest = createHash("sha256");
let byteLength = 0;
const stream = handle.createReadStream({
autoClose: false,
...(signal === undefined ? {} : { signal }),
});
for await (const chunk of stream) {
const chunk = Buffer.allocUnsafe(64 * 1024);
for (;;) {
signal?.throwIfAborted();
if (!Buffer.isBuffer(chunk))
throw new TypeError("Managed source stream returned non-buffer data");
if (chunk.length > MAX_MANAGED_SNAPSHOT_BYTES - byteLength)
const { bytesRead } = await handle.read(
chunk,
0,
Math.min(chunk.length, MAX_MANAGED_SNAPSHOT_BYTES - byteLength + 1),
null,
);
signal?.throwIfAborted();
if (bytesRead === 0) break;
if (bytesRead > MAX_MANAGED_SNAPSHOT_BYTES - byteLength)
throw managedSnapshotCapacityError(operation);
byteLength += chunk.length;
digest.update(chunk);
byteLength += bytesRead;
digest.update(chunk.subarray(0, bytesRead));
}
return { byteLength, sha256: digest.digest("hex") };
},
@@ -122,6 +125,9 @@ const normalizeManagedReadFailure = (
operation: string,
signal?: AbortSignal,
): unknown => {
// An exact caller reason is opaque, even if it resembles a cleanup record.
if (signal?.aborted === true && cause === signal.reason)
return normalizeReadFailure(cause, fallbackPath, operation, signal);
const cleanupError = managedCleanupError(
cause,
fallbackPath,
@@ -176,15 +182,19 @@ const normalizeReadFailure = (
path: string,
operation: string,
signal?: AbortSignal,
): unknown =>
cause instanceof RegularFileChangedError
? new EvidenceIntegrityError(
`Managed source changed while ${operation} was reading ${path}.`,
{ cause },
)
: cause instanceof AnalysisError
? cause
: (classifyBinaryTargetFilesystemFailure(path, cause) ??
(signal?.aborted === true
? new AnalysisCancelledError(operation, { cause })
: cause));
): unknown => {
if (signal?.aborted === true && cause === signal.reason)
return new AnalysisCancelledError(operation, { cause });
if (cause instanceof RegularFileChangedError)
return new EvidenceIntegrityError(
`Managed source changed while ${operation} was reading ${path}.`,
{ cause },
);
if (cause instanceof AnalysisError) return cause;
return (
classifyBinaryTargetFilesystemFailure(path, cause) ??
(signal?.aborted === true
? new AnalysisCancelledError(operation, { cause })
: cause)
);
};