fix(process): harden filesystem snapshot identity and cancellation (#956)

* fix(process): bind snapshots to captured file identity

* fix(process): honor snapshot cancellation for empty files

* test(process): verify snapshot descriptor cleanup

* fix(process): preserve snapshot cancellation results

* fix(process): preserve non-cancellation capture failures

* test(process): verify descriptor cleanup with fstat

* fix(process): detect snapshot changes by ctime

* test(process): prove snapshot ctime invalidation
This commit is contained in:
morluto
2026-10-07 23:45:23 +08:00
committed by GitHub
parent d76f7d1508
commit 93aa9eb5c2
8 changed files with 377 additions and 13 deletions
@@ -0,0 +1,54 @@
import { randomUUID } from "node:crypto";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { expect, it } from "vitest";
import {
ProviderProcessSupervisor,
spawnOwnedProviderProcess,
} from "../../../src/process/ProviderProcess.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
it.skipIf(process.platform === "win32")(
"does not block when an observed regular file is replaced by a FIFO",
async () => {
const directory = await createTestTempDirectory("rea-snapshot-fifo-");
const probe = fileURLToPath(
new URL(
"../../fixtures/filesystemSnapshotFifoProbe.mjs",
import.meta.url,
),
);
const snapshotModule = fileURLToPath(
new URL(
"../../../dist/application/FilesystemSnapshot.js",
import.meta.url,
),
);
const launch = await spawnOwnedProviderProcess({
command: process.execPath,
arguments: [probe, snapshotModule, directory],
runId: `rea-snapshot-fifo-${randomUUID()}`,
cwd: tmpdir(),
hostEnvironment: process.env,
});
const supervisor = new ProviderProcessSupervisor({
...launch,
ownsProcessLifetime: true,
});
try {
const closed = await supervisor.waitForOutputClose(2_000);
expect(closed).toBe(true);
expect(supervisor.snapshot().stdout.text.trim()).toBe("rejected");
} finally {
const stopped = await supervisor.stop({
terminationGraceMs: 100,
killGraceMs: 500,
});
expect(stopped.status).not.toBe("incomplete");
}
},
);
@@ -1,5 +1,5 @@
import { execFile } from "node:child_process";
import { rm, symlink } from "node:fs/promises";
import { rm, symlink, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
@@ -8,11 +8,15 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { itWithCaptureCapability } from "./processCaptureCapability.js";
import { captureProcessScenario } from "../../../src/application/ProcessHarness.js";
import { snapshotRoots } from "../../../src/application/FilesystemSnapshot.js";
import { parseProcessScenario } from "../../../src/domain/processCapture.js";
const processFixture = fileURLToPath(
new URL("../../fixtures/processFidelity.mjs", import.meta.url),
);
const snapshotCancellationFixture = fileURLToPath(
new URL("../../fixtures/processSnapshotCancellation.mjs", import.meta.url),
);
const execFileAsync = promisify(execFile);
itWithCaptureCapability(
@@ -83,6 +87,114 @@ itWithCaptureCapability(
},
);
itWithCaptureCapability(
"classifies cancellation raised by the initial filesystem snapshot",
async () => {
const root = await createTestTempDirectory("rea-snapshot-initial-cancel-");
const path = join(root, "input");
await writeFile(path, "observed\n");
const controller = new AbortController();
const { signal } = controller;
const throwIfAborted = signal.throwIfAborted.bind(signal);
Object.defineProperty(signal, "throwIfAborted", {
value: () => {
controller.abort();
throwIfAborted();
},
});
const result = await captureProcessScenario(
parseProcessScenario({
executable: process.execPath,
working_directory: root,
filesystem_observation_paths: [path],
}),
signal,
);
expect(result.ok).toBe(false);
if (result.ok) throw new Error("expected initial snapshot cancellation");
expect(result.error).toMatchObject({
reason: "cancelled",
userCategory: "cancelled",
});
},
);
itWithCaptureCapability(
"classifies cancellation raised by the final filesystem snapshot",
async () => {
const root = await createTestTempDirectory("rea-snapshot-final-cancel-");
const controller = new AbortController();
let initialSnapshotCompleted = false;
const captureSnapshot: typeof snapshotRoots = async (scenario, signal) => {
if (initialSnapshotCompleted) controller.abort();
const snapshot = await snapshotRoots(scenario, signal);
initialSnapshotCompleted = true;
return snapshot;
};
const resultPromise = captureProcessScenario(
parseProcessScenario({
executable: process.execPath,
arguments: [snapshotCancellationFixture, root],
working_directory: dirname(snapshotCancellationFixture),
filesystem_observation_paths: [root],
settle_ms: 0,
}),
controller.signal,
process.platform,
process.env,
captureSnapshot,
);
const result = await resultPromise;
expect(result.ok).toBe(false);
if (result.ok) throw new Error("expected final snapshot cancellation");
expect(initialSnapshotCompleted).toBe(true);
expect(result.error).toMatchObject({
reason: "cancelled",
userCategory: "cancelled",
});
},
);
itWithCaptureCapability(
"preserves filesystem errors that are not caller cancellation",
async () => {
const root = await createTestTempDirectory("rea-snapshot-io-error-");
const obstruction = join(root, "not-a-directory");
await writeFile(obstruction, "file");
const controller = new AbortController();
const captureSnapshot: typeof snapshotRoots = async (scenario, signal) => {
try {
return await snapshotRoots(scenario, signal);
} catch (cause: unknown) {
controller.abort();
throw cause;
}
};
const result = await captureProcessScenario(
parseProcessScenario({
executable: process.execPath,
working_directory: root,
filesystem_observation_paths: [join(obstruction, "child")],
}),
controller.signal,
process.platform,
process.env,
captureSnapshot,
);
expect(result.ok).toBe(false);
if (result.ok) throw new Error("expected filesystem observation failure");
expect(result.error).toMatchObject({
reason: "capture_failed",
cause: { code: "ENOTDIR" },
});
},
);
itWithCaptureCapability(
"captures input, Unicode, and resize across PTY startup",
async () => {
+23
View File
@@ -0,0 +1,23 @@
import { execFileSync } from "node:child_process";
import { lstat, unlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { pathToFileURL } from "node:url";
const [snapshotModulePath, directory] = process.argv.slice(2);
if (snapshotModulePath === undefined || directory === undefined)
throw new Error(
"Filesystem snapshot FIFO probe requires a module path and directory",
);
const { hashFile } = await import(pathToFileURL(snapshotModulePath));
const path = join(directory, "observed-file");
await writeFile(path, "regular file\n");
const expected = await lstat(path);
await unlink(path);
execFileSync("mkfifo", [path]);
// This invokes the production lstat-to-open hashing seam with a known captured
// regular-file identity. The owner test bounds this child so an O_RDONLY FIFO
// regression cannot hang Vitest.
const digest = await hashFile(path, expected, 1_000);
if (digest !== null) throw new Error("Expected the FIFO to be rejected");
process.stdout.write("rejected\n");
+8
View File
@@ -0,0 +1,8 @@
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
const [root] = process.argv.slice(2);
if (root === undefined)
throw new Error("Snapshot cancellation fixture requires an observation root");
await writeFile(join(root, "created-after-start"), "x");