fix(io): consolidate managed snapshots and retain cleanup ownership
Reuse managed snapshot admission and hashing in sequential path comparisons, retaining caller paths and completed inspection Evidence on later failure. Preserve exact failed-close owners alongside typed read and cancellation failures, and retry them through caller-owned resource scopes. Require canonical Hopper lease identities, remove signed offset coercion, and repair stale setup cleanup expectations that failed CI.
This commit is contained in:
@@ -19,6 +19,7 @@ import {
|
||||
readSkillFile,
|
||||
skillDestinations,
|
||||
} from "../../../src/application/SetupSkill.js";
|
||||
import { RegularFileCleanupFailure } from "../../../src/application/RegularFileRead.js";
|
||||
import { systemSetupHost } from "../../../src/application/SetupHost.js";
|
||||
import {
|
||||
runUninstall,
|
||||
@@ -67,7 +68,7 @@ const failFirstCloses = (path: string) => {
|
||||
};
|
||||
|
||||
describe("skill regular-file cleanup ownership", () => {
|
||||
it("keeps setup skill reads owned until setup host disposal", async () => {
|
||||
it("retains a failed setup skill read until setup host disposal", async () => {
|
||||
const home = await createTestTempDirectory("rea-setup-skill-close-");
|
||||
const [destination] = skillDestinations(home, []);
|
||||
if (destination === undefined) throw new Error("Shared skill root missing");
|
||||
@@ -81,7 +82,18 @@ describe("skill regular-file cleanup ownership", () => {
|
||||
);
|
||||
|
||||
try {
|
||||
expect(await host.skillNeedsInstall([])).toBe(true);
|
||||
const failure = await host.skillNeedsInstall([]).then(
|
||||
() => undefined,
|
||||
(cause: unknown) => cause,
|
||||
);
|
||||
expect(failure).toBeInstanceOf(RegularFileCleanupFailure);
|
||||
expect(failure).toMatchObject({
|
||||
path: manifest,
|
||||
outcome: { kind: "completed" },
|
||||
cleanupCause: expect.objectContaining({
|
||||
message: "test holds the skill handle open",
|
||||
}),
|
||||
});
|
||||
expect(close.attempts.length).toBeGreaterThan(0);
|
||||
expect(new Set(close.attempts).size).toBe(1);
|
||||
const attemptsBeforeClose = close.attempts.length;
|
||||
@@ -147,7 +159,7 @@ describe("skill regular-file cleanup ownership", () => {
|
||||
});
|
||||
|
||||
describe("skill cleanup diagnostics", () => {
|
||||
it("preserves a doctor report and cleanup diagnostic when host close still fails", async () => {
|
||||
it("preserves read and host cleanup failures when doctor identity cannot complete", async () => {
|
||||
const home = await createTestTempDirectory("rea-doctor-skill-close-");
|
||||
const [destination] = skillDestinations(home, []);
|
||||
if (destination === undefined) throw new Error("Shared skill root missing");
|
||||
@@ -172,15 +184,23 @@ describe("skill cleanup diagnostics", () => {
|
||||
});
|
||||
|
||||
try {
|
||||
const report = await runDoctorOwned(undefined, host, { skill: true });
|
||||
|
||||
expect(report.healthy).toBe(false);
|
||||
expect(report.identity?.skill.installed_version).toBeNull();
|
||||
expect(report.checks).toContainEqual(
|
||||
const failure = await runDoctorOwned(undefined, host, {
|
||||
skill: true,
|
||||
}).then(
|
||||
() => undefined,
|
||||
(cause: unknown) => cause,
|
||||
);
|
||||
expect(failure).toBeInstanceOf(AggregateError);
|
||||
if (!(failure instanceof AggregateError))
|
||||
throw new Error("Doctor cleanup should preserve both failures");
|
||||
expect(failure.errors[0]).toBeInstanceOf(RegularFileCleanupFailure);
|
||||
expect(failure.errors[0]).toMatchObject({
|
||||
path: manifest,
|
||||
outcome: { kind: "completed" },
|
||||
});
|
||||
expect(failure.errors[1]).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "doctor-resource-cleanup",
|
||||
ok: false,
|
||||
detail: expect.stringContaining(manifest),
|
||||
message: expect.stringContaining(manifest),
|
||||
}),
|
||||
);
|
||||
expect(close.attempts.length).toBeGreaterThan(0);
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { truncate, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
import { ArtifactResourceScope } from "../../../../src/artifacts/ArtifactResourceScope.js";
|
||||
import { OwnedFileHandle } from "../../../../src/filesystem/OwnedFileHandle.js";
|
||||
|
||||
import {
|
||||
compareManagedMembersEvidenceValidated,
|
||||
@@ -19,6 +21,11 @@ import { compareManagedMembersInputSchema } from "../../../../src/domain/managed
|
||||
import { inspectManagedMembersBytes } from "../../../../src/dotnet/ManagedMemberInspector.js";
|
||||
import { managedPeFixtureTarget } from "../../../../src/dotnet/ManagedPe.fixture.js";
|
||||
import { buildManagedPeFixture } from "../../../../src/dotnet/ManagedPe.fixture.js";
|
||||
import {
|
||||
MAX_MANAGED_SNAPSHOT_BYTES,
|
||||
readManagedSnapshot,
|
||||
} from "../../../../src/dotnet/ManagedSnapshot.js";
|
||||
import * as boundedFileBytes from "../../../../src/process/BoundedFileBytes.js";
|
||||
|
||||
it.each([
|
||||
["accessibility", 6, 0x0011],
|
||||
@@ -184,6 +191,14 @@ describe("managed member comparison path workflow", () => {
|
||||
confidence: "inferred",
|
||||
authority: "analyst-inference",
|
||||
});
|
||||
expect(result.value.parameters).toMatchObject({
|
||||
left_path: leftPath,
|
||||
right_path: rightPath,
|
||||
});
|
||||
expect(result.value.locations).toEqual([
|
||||
{ kind: "artifact-path", path: leftPath },
|
||||
{ kind: "artifact-path", path: rightPath },
|
||||
]);
|
||||
expect(
|
||||
managedMemberComparisonResultSchema.parse(result.value.normalized_result)
|
||||
.matching.exact_il_signature,
|
||||
@@ -205,7 +220,179 @@ describe("managed member comparison path workflow", () => {
|
||||
details: { path: expect.stringContaining("missing.dll") },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed comparison post-admission read failures", () => {
|
||||
it.each([
|
||||
["left", "disappearance", "ENOENT"],
|
||||
["left", "permission denial", "EACCES"],
|
||||
["right", "disappearance", "ENOENT"],
|
||||
["right", "permission denial", "EACCES"],
|
||||
])(
|
||||
"classifies a %s-side %s during the post-resolution read",
|
||||
async (side, _name, code) => {
|
||||
const directory = await createTestTempDirectory(
|
||||
"rea-managed-read-failure-",
|
||||
);
|
||||
const leftPath = join(directory, "left.dll");
|
||||
const rightPath = join(directory, "right.dll");
|
||||
await writeFile(leftPath, buildManagedPeFixture());
|
||||
await writeFile(rightPath, buildManagedPeFixture());
|
||||
|
||||
const result = await compareManagedMemberPaths(
|
||||
{ leftPath, rightPath },
|
||||
{
|
||||
resolveTarget: parseBinaryTarget,
|
||||
readSnapshot: async (path, operation, options) => {
|
||||
if (path === (side === "left" ? leftPath : rightPath)) {
|
||||
const failure = Object.assign(new Error(`read failed: ${code}`), {
|
||||
code,
|
||||
});
|
||||
throw failure;
|
||||
}
|
||||
return readManagedSnapshot(path, operation, options);
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error._tag).toBe("BinaryTargetError");
|
||||
expect(projectAnalysisError(result.error)).toMatchObject({
|
||||
details: { path: side === "left" ? leftPath : rightPath },
|
||||
});
|
||||
if (side === "right")
|
||||
expect(result.error.partialObservation).toMatchObject({
|
||||
operation: "inspect_managed_members",
|
||||
subject: {
|
||||
local_path: leftPath,
|
||||
digest: { sha256: expect.stringMatching(/^[0-9a-f]{64}$/u) },
|
||||
},
|
||||
});
|
||||
else expect(result.error.partialObservation).toBeUndefined();
|
||||
if (code === "EACCES")
|
||||
expect(projectAnalysisError(result.error)).toMatchObject({
|
||||
code: "access_denied",
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("managed comparison snapshot admission and cleanup", () => {
|
||||
it("rejects a sparse oversized snapshot through the default reader before reading it", async () => {
|
||||
const directory = await createTestTempDirectory(
|
||||
"rea-managed-oversized-snapshot-",
|
||||
);
|
||||
const leftPath = join(directory, "oversized.dll");
|
||||
const rightPath = join(directory, "right.dll");
|
||||
const fixture = buildManagedPeFixture();
|
||||
await writeFile(leftPath, Buffer.alloc(0));
|
||||
await truncate(leftPath, MAX_MANAGED_SNAPSHOT_BYTES + 1);
|
||||
await writeFile(rightPath, fixture);
|
||||
const template = managedPeFixtureTarget(fixture);
|
||||
const read = vi.spyOn(boundedFileBytes, "readBoundedFileBytes");
|
||||
try {
|
||||
const result = await compareManagedMemberPaths(
|
||||
{ leftPath, rightPath },
|
||||
{
|
||||
resolveTarget: async (path) => ({
|
||||
ok: true,
|
||||
value: { ...template, path },
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error).toMatchObject({
|
||||
_tag: "AnalysisResourceConstraintError",
|
||||
operation: "compare_managed_members",
|
||||
reportedLimits: {
|
||||
max_snapshot_bytes: MAX_MANAGED_SNAPSHOT_BYTES,
|
||||
},
|
||||
});
|
||||
expect(read).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
read.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it("retries a transient snapshot close failure through the same resource owner", async () => {
|
||||
const directory = await createTestTempDirectory(
|
||||
"rea-managed-compare-close-",
|
||||
);
|
||||
const leftPath = join(directory, "left.dll");
|
||||
const rightPath = join(directory, "right.dll");
|
||||
await writeFile(leftPath, buildManagedPeFixture());
|
||||
await writeFile(rightPath, buildManagedPeFixture());
|
||||
const resources = new ArtifactResourceScope();
|
||||
const originalClose = OwnedFileHandle.prototype.close;
|
||||
const owners: OwnedFileHandle[] = [];
|
||||
let attempts = 0;
|
||||
const close = vi
|
||||
.spyOn(OwnedFileHandle.prototype, "close")
|
||||
.mockImplementation(async function (this: OwnedFileHandle) {
|
||||
owners.push(this);
|
||||
attempts += 1;
|
||||
if (attempts === 1) throw new Error("injected transient close failure");
|
||||
await originalClose.call(this);
|
||||
});
|
||||
try {
|
||||
const result = await compareManagedMemberPaths(
|
||||
{ leftPath, rightPath },
|
||||
{},
|
||||
resources,
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
expect(owners[0]).toBe(owners[1]);
|
||||
expect(attempts).toBe(3);
|
||||
} finally {
|
||||
close.mockRestore();
|
||||
await resources.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("projects persistent snapshot cleanup failure and retains its exact owner", async () => {
|
||||
const directory = await createTestTempDirectory(
|
||||
"rea-managed-compare-cleanup-",
|
||||
);
|
||||
const leftPath = join(directory, "left.dll");
|
||||
const rightPath = join(directory, "right.dll");
|
||||
await writeFile(leftPath, buildManagedPeFixture());
|
||||
await writeFile(rightPath, buildManagedPeFixture());
|
||||
const resources = new ArtifactResourceScope();
|
||||
const cleanupCause = new Error("injected persistent close failure");
|
||||
const owners: OwnedFileHandle[] = [];
|
||||
const close = vi
|
||||
.spyOn(OwnedFileHandle.prototype, "close")
|
||||
.mockImplementation(async function (this: OwnedFileHandle) {
|
||||
owners.push(this);
|
||||
throw cleanupCause;
|
||||
});
|
||||
let result: Awaited<ReturnType<typeof compareManagedMemberPaths>>;
|
||||
try {
|
||||
result = await compareManagedMemberPaths(
|
||||
{ leftPath, rightPath },
|
||||
{},
|
||||
resources,
|
||||
);
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(projectAnalysisError(result.error)).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: { cleanup: "incomplete", resources: [leftPath] },
|
||||
});
|
||||
expect(owners[0]).toBe(owners[1]);
|
||||
expect(owners[0]?.handle.fd).toBeGreaterThanOrEqual(0);
|
||||
} finally {
|
||||
close.mockRestore();
|
||||
await resources.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed comparison target identity", () => {
|
||||
it("rejects a file that changes after target identity is resolved", async () => {
|
||||
const directory = await createTestTempDirectory(
|
||||
"rea-managed-compare-race-",
|
||||
@@ -227,7 +414,6 @@ describe("managed member comparison path workflow", () => {
|
||||
await writeFile(path, replacement);
|
||||
return target;
|
||||
},
|
||||
readBytes: (path) => readFile(path),
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { truncate, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
|
||||
@@ -15,6 +15,180 @@ import {
|
||||
} from "../../../../src/domain/managed/managedArtifact.js";
|
||||
import { ManagedStaticProvider } from "../../../../src/dotnet/ManagedStaticProvider.js";
|
||||
import { buildManagedPeFixture } from "../../../../src/dotnet/ManagedPe.fixture.js";
|
||||
import {
|
||||
MAX_MANAGED_SNAPSHOT_BYTES,
|
||||
readManagedSnapshot,
|
||||
} from "../../../../src/dotnet/ManagedSnapshot.js";
|
||||
import { OwnedFileHandle } from "../../../../src/filesystem/OwnedFileHandle.js";
|
||||
import { ArtifactResourceScope } from "../../../../src/artifacts/ArtifactResourceScope.js";
|
||||
import { RegularFileCleanupFailure } from "../../../../src/application/RegularFileRead.js";
|
||||
import { projectAnalysisError } from "../../../../src/domain/analysisErrorProjection.js";
|
||||
import * as boundedFileBytes from "../../../../src/process/BoundedFileBytes.js";
|
||||
|
||||
describe("managed static snapshot capacity failure", () => {
|
||||
it("preserves a typed capacity failure when closing the admitted file also fails", async () => {
|
||||
const directory = await createTestTempDirectory(
|
||||
"rea-managed-capacity-cleanup-",
|
||||
);
|
||||
const path = join(directory, "sparse.exe");
|
||||
await writeFile(path, Buffer.alloc(0));
|
||||
await truncate(path, MAX_MANAGED_SNAPSHOT_BYTES + 1);
|
||||
const resources = new ArtifactResourceScope();
|
||||
const originalClose = OwnedFileHandle.prototype.close;
|
||||
let permitClose = false;
|
||||
const owners: OwnedFileHandle[] = [];
|
||||
const close = vi
|
||||
.spyOn(OwnedFileHandle.prototype, "close")
|
||||
.mockImplementation(async function (this: OwnedFileHandle) {
|
||||
owners.push(this);
|
||||
if (!permitClose) throw new Error("fixture handle close failure");
|
||||
await originalClose.call(this);
|
||||
});
|
||||
try {
|
||||
const failure = await readManagedSnapshot(
|
||||
path,
|
||||
"inspect_managed_artifact",
|
||||
{
|
||||
resources,
|
||||
},
|
||||
).catch((cause: unknown) => cause);
|
||||
|
||||
expect(failure).toMatchObject({
|
||||
cleanupIncomplete: true,
|
||||
cleanupResources: [path],
|
||||
diagnostics: { primary_error: { code: "resource_constraint" } },
|
||||
});
|
||||
expect(owners.length).toBeGreaterThan(0);
|
||||
expect(owners.every((owner) => owner === owners[0])).toBe(true);
|
||||
|
||||
permitClose = true;
|
||||
await resources.close();
|
||||
} finally {
|
||||
permitClose = true;
|
||||
await resources.close();
|
||||
close.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed static snapshot cancellation", () => {
|
||||
it("preserves cancellation when snapshot cleanup also fails", async () => {
|
||||
const directory = await createTestTempDirectory(
|
||||
"rea-managed-cancel-cleanup-",
|
||||
);
|
||||
const path = join(directory, "fixture.exe");
|
||||
await writeFile(path, buildManagedPeFixture());
|
||||
const parsed = await parseBinaryTarget(path);
|
||||
if (!parsed.ok) throw parsed.error;
|
||||
const client = new ManagedStaticProvider().createClient(parsed.value);
|
||||
const controller = new AbortController();
|
||||
const originalRead = boundedFileBytes.readBoundedFileBytes;
|
||||
const read = vi
|
||||
.spyOn(boundedFileBytes, "readBoundedFileBytes")
|
||||
.mockImplementation(async (...arguments_) => {
|
||||
controller.abort(new Error("fixture cancellation"));
|
||||
return originalRead(...arguments_);
|
||||
});
|
||||
const originalClose = OwnedFileHandle.prototype.close;
|
||||
let permitClose = false;
|
||||
const owners: OwnedFileHandle[] = [];
|
||||
const close = vi
|
||||
.spyOn(OwnedFileHandle.prototype, "close")
|
||||
.mockImplementation(async function (this: OwnedFileHandle) {
|
||||
owners.push(this);
|
||||
if (!permitClose) throw new Error("fixture handle close failure");
|
||||
await originalClose.call(this);
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await client.execute(
|
||||
"inspect_managed_artifact",
|
||||
{},
|
||||
{ signal: controller.signal },
|
||||
);
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(projectAnalysisError(result.error)).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: {
|
||||
diagnostics: {
|
||||
primary_error: { code: "cancelled" },
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(result.error.cause).toBeInstanceOf(AggregateError);
|
||||
if (!(result.error.cause instanceof AggregateError)) return;
|
||||
const cleanupError = result.error.cause.errors[1];
|
||||
expect(cleanupError).toBeInstanceOf(Error);
|
||||
if (!(cleanupError instanceof Error)) return;
|
||||
expect(cleanupError.cause).toBeInstanceOf(RegularFileCleanupFailure);
|
||||
if (!(cleanupError.cause instanceof RegularFileCleanupFailure)) return;
|
||||
expect(cleanupError.cause.owner).toBe(owners[0]);
|
||||
expect(owners[0]?.handle.fd).toBeGreaterThanOrEqual(0);
|
||||
|
||||
permitClose = true;
|
||||
expect(await client.close()).toMatchObject({ ok: true });
|
||||
expect(owners.every((owner) => owner === owners[0])).toBe(true);
|
||||
} finally {
|
||||
permitClose = true;
|
||||
await client.close();
|
||||
close.mockRestore();
|
||||
read.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed static file ownership", () => {
|
||||
it("retains the same failed file owner, blocks reads, and retries it on client close", async () => {
|
||||
const directory = await createTestTempDirectory("rea-managed-close-");
|
||||
const bytes = buildManagedPeFixture();
|
||||
const path = join(directory, "fixture.exe");
|
||||
await writeFile(path, bytes);
|
||||
const parsed = await parseBinaryTarget(path);
|
||||
if (!parsed.ok) throw parsed.error;
|
||||
const client = new ManagedStaticProvider().createClient(parsed.value);
|
||||
const originalClose = OwnedFileHandle.prototype.close;
|
||||
const owners: OwnedFileHandle[] = [];
|
||||
let permitClose = false;
|
||||
const close = vi
|
||||
.spyOn(OwnedFileHandle.prototype, "close")
|
||||
.mockImplementation(async function (this: OwnedFileHandle) {
|
||||
owners.push(this);
|
||||
if (!permitClose) throw new Error("fixture handle close failure");
|
||||
await originalClose.call(this);
|
||||
});
|
||||
try {
|
||||
const failed = await client.execute("inspect_managed_artifact", {});
|
||||
expect(failed).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
cleanupIncomplete: true,
|
||||
cleanupResources: [path],
|
||||
},
|
||||
});
|
||||
|
||||
const blocked = await client.execute("inspect_managed_artifact", {});
|
||||
expect(blocked).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
cleanupIncomplete: true,
|
||||
cleanupResources: [path],
|
||||
},
|
||||
});
|
||||
expect(owners).toHaveLength(3);
|
||||
expect(owners.every((owner) => owner === owners[0])).toBe(true);
|
||||
|
||||
permitClose = true;
|
||||
expect(await client.close()).toMatchObject({ ok: true });
|
||||
expect(owners).toHaveLength(4);
|
||||
expect(owners.every((owner) => owner === owners[0])).toBe(true);
|
||||
} finally {
|
||||
permitClose = true;
|
||||
await client.close();
|
||||
close.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed static provider path boundary", () => {
|
||||
it("executes as a read-only auxiliary provider with digest, cancellation, and format boundaries", async () => {
|
||||
|
||||
@@ -219,7 +219,11 @@ describe("managed one-shot cleanup outcomes", () => {
|
||||
);
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error.cause).toBe(primaryError);
|
||||
expect(result.error.cause).toBeInstanceOf(AggregateError);
|
||||
expect(result.error.cause).toMatchObject({
|
||||
cause: primaryError,
|
||||
errors: [primaryError, cleanupError],
|
||||
});
|
||||
expect(projectAnalysisError(result.error)).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: {
|
||||
|
||||
Reference in New Issue
Block a user