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
@@ -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 () => {