fix(evidence): retain source and stream ownership

This commit is contained in:
morluto committed 2026-10-27 05:36:09 +08:00
1 parent 4803c4f9c2
commit b6e58c22d4
7 files changed
+287 -42

No files matched your search

@@ -123,11 +123,10 @@ export const compareManagedMemberPaths = async (
const selectedPath = input[inputField];
let snapshot: Awaited<ReturnType<typeof readManagedSnapshot>>;
try {
snapshot = await readSnapshot(
path,
operation,
resources === undefined ? {} : { resources },
);
snapshot = await readSnapshot(path, operation, {
expectedSha256: target.sha256,
...(resources === undefined ? {} : { resources }),
});
} catch (cause: unknown) {
const failure = classifyBinaryTargetFilesystemFailure(path, cause);
if (failure !== undefined) throw failure;
+32 -18
View File
@@ -1,4 +1,5 @@
import { createHash } from "node:crypto";
import { once } from "node:events";
import { writeFileSync } from "node:fs";
import { buffer } from "node:stream/consumers";
import { createReader } from "./inventory/reader.js";
@@ -618,24 +619,37 @@ it("rejects forged entries and changed carrier identity, then closes idempotentl
}
});
it("settles an active member stream when closed", async () => {
const path = await writeCarrier(
"close",
nodeSeaElf({ main: Buffer.alloc(1024 * 1024, 65) }),
);
const reader = new NodeSeaArtifactReader(path);
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entry = entries[0];
if (entry === undefined) throw new Error("Missing main script");
const stream = await reader.open(entry);
await reader.close();
expect(stream.destroyed).toBe(true);
} finally {
await reader.close();
}
});
it.each([8, 1024 * 1024])(
"retires an unread %i-byte member through the shared reader factory",
async (size) => {
const path = await writeCarrier(
"close",
nodeSeaElf({ main: Buffer.alloc(size, 65) }),
);
const reader = createReader(path, "node-sea", {});
if (reader === undefined) throw new Error("Missing Node SEA reader");
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entry = entries[0];
if (entry === undefined) throw new Error("Missing main script");
const stream = await reader.open(entry);
const produced = size === 8 ? once(stream, "finish") : undefined;
await once(stream, "readable");
if (produced !== undefined) await produced;
expect(stream.destroyed).toBe(false);
await reader.close();
expect(stream.destroyed).toBe(true);
expect(stream.closed).toBe(true);
await expect(buffer(stream)).rejects.toMatchObject({
reason: "unavailable",
});
await reader.close();
} finally {
await reader.close();
}
},
);
it("honors cancellation before admitting a carrier", async () => {
const path = await writeCarrier("cancel", nodeSeaElf());
+3 -1
View File
@@ -138,12 +138,14 @@ export class NodeSeaArtifactReader implements ArtifactReader {
const output = this.#memberStream(stored, signal);
output.on("error", () => undefined);
this.#streams.add(output);
// A finished producer can leave source bytes buffered for its consumer.
// Keep the readable side owned until it closes as well.
output.once("close", () => this.#streams.delete(output));
void this.#scope
.run(
() =>
new Promise<void>((resolve) => {
pipeline(source, output, () => {
this.#streams.delete(output);
resolve();
});
}),
+53 -18
View File
@@ -40,6 +40,10 @@ interface ManagedSnapshot extends ManagedSourceObservation {
readonly bytes: Buffer;
}
interface ManagedSnapshotReadOptions extends RegularFileReadOptions {
readonly expectedSha256?: string;
}
/** Admit one observed source digest before any parser or cache consumes its bytes. */
export const verifyManagedSourceIdentity = (
target: { readonly path: string; readonly sha256: string },
@@ -77,7 +81,7 @@ const managedSnapshotCapacityError = (
export const readManagedSnapshot = async (
path: string,
operation: string,
options: RegularFileReadOptions = {},
options: ManagedSnapshotReadOptions = {},
): Promise<ManagedSnapshot> => {
return readManagedFile(
path,
@@ -106,7 +110,7 @@ export const readManagedSnapshot = async (
export const hashManagedSource = async (
path: string,
operation: string,
options: RegularFileReadOptions = {},
options: ManagedSnapshotReadOptions = {},
): Promise<ManagedSourceObservation> =>
readManagedFile(
path,
@@ -142,24 +146,42 @@ const readManagedFile = <Value extends ManagedSourceObservation>(
path: string,
operation: string,
read: (handle: FileHandle, metadata: Stats) => Promise<Value>,
options: RegularFileReadOptions,
options: ManagedSnapshotReadOptions,
): Promise<Value> =>
withRegularFileCompletion(path, read, options)
.then(({ value, cancellation }) => {
if (cancellation !== undefined)
throw new AnalysisCancelledError(operation, {
cause: cancellation.reason,
partialObservation: {
kind: "file-snapshot-hash",
path,
bytes: value.byteLength,
sha256: value.sha256,
},
if (cancellation !== undefined) {
const identity =
options.expectedSha256 === undefined
? undefined
: verifyManagedSourceIdentity(
{ path, sha256: options.expectedSha256 },
value,
operation,
);
const failure =
identity !== undefined && !identity.ok
? identity.error
: new AnalysisCancelledError(operation, {
cause: cancellation.reason,
});
throw failure.retainPartialObservation({
kind: "file-snapshot-hash",
path,
bytes: value.byteLength,
sha256: value.sha256,
});
}
return value;
})
.catch((cause: unknown) => {
throw normalizeManagedReadFailure(cause, path, operation, options.signal);
throw normalizeManagedReadFailure(
cause,
path,
operation,
options.signal,
options.expectedSha256,
);
});
/** Keep cleanup ownership and the original typed read failure at this boundary. */
@@ -168,6 +190,7 @@ const normalizeManagedReadFailure = (
fallbackPath: string,
operation: string,
signal?: AbortSignal,
expectedSha256?: string,
): unknown => {
// An exact caller reason is opaque, even if it resembles a cleanup record.
if (signal?.aborted === true && cause === signal.reason)
@@ -177,6 +200,7 @@ const normalizeManagedReadFailure = (
fallbackPath,
operation,
signal,
expectedSha256,
);
if (cleanupError !== undefined) return cleanupError;
return normalizeReadFailure(cause, fallbackPath, operation, signal);
@@ -188,6 +212,7 @@ export const managedCleanupError = (
fallbackPath: string,
operation: string,
signal?: AbortSignal,
expectedSha256?: string,
): AnalysisError | undefined => {
if (cause instanceof RegularFileCleanupFailure) {
const cancellation = regularFileCancellation(cause);
@@ -198,6 +223,14 @@ export const managedCleanupError = (
{ cause, operation },
);
bindRegularFileCleanup(cause, cleanup);
const identity =
cause.outcome.kind === "completed" && expectedSha256 !== undefined
? verifyManagedSourceIdentity(
{ path: cause.path, sha256: expectedSha256 },
cause.outcome.value as ManagedSourceObservation,
operation,
)
: undefined;
const failure =
cause.outcome.kind === "failed"
? normalizeReadFailure(
@@ -206,11 +239,13 @@ export const managedCleanupError = (
operation,
signal,
)
: cancellation === undefined
? undefined
: new AnalysisCancelledError(operation, {
cause: cancellation.reason,
});
: identity !== undefined && !identity.ok
? identity.error
: cancellation === undefined
? undefined
: new AnalysisCancelledError(operation, {
cause: cancellation.reason,
});
// This boundary produces only these snapshot facts. The descriptor owner
// confirms completion independently of a failed final close.
if (cause.outcome.kind === "completed") {
+2
View File
@@ -106,10 +106,12 @@ class ManagedStaticClient implements AnalysisClient {
const observed =
snapshot === undefined
? await readManagedSnapshot(this.target.path, operation, {
expectedSha256: this.target.sha256,
signal: options?.signal,
resources: this.#resources,
})
: await hashManagedSource(this.target.path, operation, {
expectedSha256: this.target.sha256,
signal: options?.signal,
resources: this.#resources,
});
@@ -31,8 +31,63 @@ import {
readManagedSnapshot,
} from "../../../../src/dotnet/ManagedSnapshot.js";
import * as boundedFileBytes from "../../../../src/process/BoundedFileBytes.js";
import { primaryAnalysisError } from "../../../../src/domain/analysisErrorBase.js";
import { buildManagedMemberCapacityFixture } from "../../../fixtures/managedMemberCapacity.js";
it("preserves a selected comparison source digest contradiction through failed close and retry", async () => {
const root = await createTestTempDirectory(
"rea-managed-comparison-identity-close-",
);
const leftPath = join(root, "left.dll");
const rightPath = join(root, "right.dll");
const bytes = buildManagedPeFixture();
await Promise.all([writeFile(leftPath, bytes), writeFile(rightPath, bytes)]);
const resources = new ArtifactResourceScope();
let blocked = false;
const nativeClose = OwnedFileHandle.prototype.close;
const close = vi
.spyOn(OwnedFileHandle.prototype, "close")
.mockImplementation(async function (this: OwnedFileHandle) {
if (blocked) throw new Error("comparison descriptor close denied");
await nativeClose.call(this);
});
try {
const result = await compareManagedMemberPaths(
{ leftPath, rightPath },
{
readSnapshot: async (path, operation, options) => {
const changed = Buffer.from(bytes);
changed[changed.length - 1] =
changed.readUInt8(changed.length - 1) ^ 1;
await writeFile(path, changed);
blocked = true;
return readManagedSnapshot(path, operation, options);
},
},
resources,
);
if (result.ok)
throw new Error("Expected changed selected comparison source");
expect(primaryAnalysisError(result.error)._tag).toBe(
"AnalysisArtifactChangedError",
);
expect(result.error.partialObservation).toMatchObject({
kind: "file-snapshot-hash",
path: leftPath,
bytes: bytes.length,
});
expect(result.error.cleanupIncomplete).toBe(true);
await expect(resources.close()).rejects.toBeDefined();
blocked = false;
await resources.close();
expect(result.error.cleanupIncomplete).toBe(false);
} finally {
blocked = false;
await resources.close();
close.mockRestore();
}
});
it("keeps missing members unknown when the opposite inspection retains only types", async () => {
const directory = await createTestTempDirectory(
"rea-managed-type-recovery-comparison-",
@@ -24,6 +24,144 @@ import { ArtifactResourceScope } from "../../../../src/artifacts/ArtifactResourc
import { RegularFileCleanupFailure } from "../../../../src/filesystem/OwnedRegularFile.js";
import { projectAnalysisError } from "../../../../src/domain/analysisErrorProjection.js";
import * as boundedFileBytes from "../../../../src/process/BoundedFileBytes.js";
import { primaryAnalysisError } from "../../../../src/domain/analysisErrorBase.js";
it.each(
[false, true].flatMap((warm) =>
[false, true].flatMap((changed) =>
[false, true].map((failedClose) => ({ warm, changed, failedClose })),
),
),
)(
"preserves selected managed identity through finalization (cached=$warm, changed=$changed, failedClose=$failedClose)",
async ({ warm, changed, failedClose }) => {
const root = await createTestTempDirectory(
"rea-managed-selected-identity-",
);
const path = join(root, "fixture.dll");
const bytes = buildManagedPeFixture();
await writeFile(path, bytes);
const target = await parseBinaryTarget(path);
if (!target.ok) throw target.error;
const client = new ManagedStaticProvider().createClient(target.value);
if (warm)
expect((await client.execute("inspect_managed_artifact", {})).ok).toBe(
true,
);
const observed = Buffer.from(bytes);
if (changed) {
observed[observed.length - 1] =
observed.readUInt8(observed.length - 1) ^ 1;
await writeFile(path, observed);
}
const controller = new AbortController();
const reason = { source: "selected managed finalization" };
let blocked = failedClose;
const owners = new Set<OwnedFileHandle>();
const nativeClose = OwnedFileHandle.prototype.close;
const close = vi
.spyOn(OwnedFileHandle.prototype, "close")
.mockImplementation(async function (this: OwnedFileHandle) {
owners.add(this);
controller.abort(reason);
if (blocked) throw new Error("managed source close denied");
await nativeClose.call(this);
});
try {
const result = await client.execute(
"inspect_managed_artifact",
{},
{ signal: controller.signal },
);
if (result.ok) throw new Error("Expected source finalization failure");
const primary = primaryAnalysisError(result.error);
expect(primary._tag).toBe(
changed ? "AnalysisArtifactChangedError" : "AnalysisCancelledError",
);
if (changed)
expect(primary.diagnostics).toMatchObject({
expected_sha256: target.value.sha256,
observed_sha256: createHash("sha256").update(observed).digest("hex"),
});
else expect(primary.cause).toBe(reason);
const receipt = {
kind: "file-snapshot-hash",
path,
sha256: createHash("sha256").update(observed).digest("hex"),
bytes: observed.length,
};
expect(result.error.partialObservation).toEqual(receipt);
expect(result.error.cleanupIncomplete).toBe(failedClose);
if (failedClose) expect((await client.close()).ok).toBe(false);
blocked = false;
expect((await client.close()).ok).toBe(true);
expect(result.error.cleanupIncomplete).toBe(false);
expect([...owners].every((owner) => owner.handle.fd === -1)).toBe(true);
expect(result.error.partialObservation).toEqual(receipt);
} finally {
blocked = false;
await client.close();
close.mockRestore();
}
},
);
it.each([false, true])(
"preserves physical managed read failure before close cancellation (failedClose=%s)",
async (failedClose) => {
const root = await createTestTempDirectory(
"rea-managed-physical-finalization-",
);
const path = join(root, "fixture.dll");
await writeFile(path, buildManagedPeFixture());
const target = await parseBinaryTarget(path);
if (!target.ok) throw target.error;
const client = new ManagedStaticProvider().createClient(target.value);
const controller = new AbortController();
const reason = { source: "after failed managed read" };
const physical = Object.assign(new Error("physical managed read denied"), {
code: "EIO",
});
const originalRead = boundedFileBytes.readBoundedFileBytes;
const read = vi
.spyOn(boundedFileBytes, "readBoundedFileBytes")
.mockImplementation((handle, ...args) => {
vi.spyOn(handle, "read").mockRejectedValueOnce(physical);
return originalRead(handle, ...args);
});
const nativeClose = OwnedFileHandle.prototype.close;
let blocked = failedClose;
const close = vi
.spyOn(OwnedFileHandle.prototype, "close")
.mockImplementation(async function (this: OwnedFileHandle) {
controller.abort(reason);
if (blocked) throw new Error("managed descriptor close denied");
await nativeClose.call(this);
});
try {
const result = await client.execute(
"inspect_managed_artifact",
{},
{ signal: controller.signal },
);
if (result.ok) throw new Error("Expected physical read failure");
expect(primaryAnalysisError(result.error)).toMatchObject({
_tag: "BinaryTargetError",
cause: physical,
});
expect(result.error.partialObservation).toBeUndefined();
expect(result.error.cleanupIncomplete).toBe(failedClose);
blocked = false;
expect((await client.close()).ok).toBe(true);
expect(result.error.cleanupIncomplete).toBe(false);
} finally {
blocked = false;
await client.close();
close.mockRestore();
read.mockRestore();
}
},
);
it("classifies a source changed during managed snapshot acquisition as artifact_changed", async () => {
const directory = await createTestTempDirectory("rea-managed-source-change-");