Files
rea/tests/boundary/filesystem/referenceSourceFailureDetails.test.ts
T
679658b287 fix(reference): report entries beyond the path limit as I/O failures (#2716)
* fix(reference): report entries beyond the path limit as I/O failures

Reference-source traversal joins each child onto the root path. A valid
tree whose nested entries exceed the platform's absolute path limit
(PATH_MAX 1024 on macOS) makes lstat fail with ENAMETOOLONG. The
reader's filesystem error allow-list lacked that code, so
filesystemFailureDetail returned undefined and the native rejection
escaped both readReferenceSource and importReferenceSource instead of
becoming the existing failed `io` entry.

Add ENAMETOOLONG to the allow-list, matching the JavaScript application
reader's list. Readable siblings stay in the graph and the unreachable
entry is recorded with its OS message.

Fixes #2652

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(reference): assert the imported graph keeps readable and failed entries

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(process): report no remaining time after a provider deadline fires

Node timers can fire while performance.now() is still a fraction of a
millisecond short of the monotonic deadline. ProviderStartupDeadline then
reported interruption "timeout" and an aborted signal while remainingMs()
returned ceil(0.x) = 1, so polling loops guarded by remainingMs() > 0
(Hopper and Ghidra startup) could run past an elapsed deadline, and the
real-timer test "aborts a bounded wait when its real monotonic deadline
expires" failed intermittently in CI (expected 1 to be 0).

Once the deadline's own timer has classified a timeout, report zero
remaining time. A deterministic regression test fires the timer with the
monotonic clock 0.4 ms short of the deadline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(reference): preserve path limit entry uncertainty

* fix(reference): preserve unknown entry failures and identity

* fix(reference): type unknown-kind exclusions and keep them out of symlink counts

The metadata-failure path offers "unknown" to exclusion predicates, but
the predicate type only admitted concrete kinds, which broke the build.
Directory-only secret and ignore rules now also match an unknown-kind
entry (it may be a directory), and the summary counts unknown entries
separately instead of as symlinks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
Co-authored-by: Codex <codex@users.noreply.github.com>
2026-10-13 17:33:09 -07:00

221 lines
8.2 KiB
TypeScript

import { execFile } from "node:child_process";
import { chmod, mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import { describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import {
importReferenceSource,
readReferenceSource,
} from "../../support/referenceSourceResourceScope.js";
const importTree = (root: string) =>
importReferenceSource({
root,
caller: "native-entry-failure-detail-test",
policy: { secretPatterns: [] },
});
const permissionChecksUnavailable =
process.platform === "win32" || process.getuid?.() === 0;
describe("reference import native entry failure details", () => {
it.skipIf(permissionChecksUnavailable)(
"classifies a root behind a blocked parent as an I/O failure",
async () => {
const directory = await createTestTempDirectory(
"rea-reference-root-permission-",
);
const parent = join(directory, "blocked-parent");
const root = join(parent, "source");
await mkdir(root, { recursive: true });
await chmod(parent, 0);
try {
const read = await readReferenceSource(root);
expect(read).toMatchObject({
ok: false,
error: {
tag: "reference-source-reader",
code: "io",
message: expect.stringContaining("EACCES"),
},
});
} finally {
await chmod(parent, 0o700);
}
},
);
it.skipIf(permissionChecksUnavailable)(
"retains the actual operating-system reason for an unreadable file",
async () => {
const root = await createTestTempDirectory("rea-import-native-detail-");
const path = join(root, "blocked.ts");
await writeFile(path, "export const value = 1;\n");
await chmod(path, 0);
try {
const read = await readReferenceSource(root);
if (!read.ok) throw new Error(read.error.message);
const failure = read.value.entries.find(
(entry) => entry.status === "failed" && entry.path === "blocked.ts",
);
if (failure?.status !== "failed")
throw new Error(
"Owned permission fixture did not produce a read failure",
);
expect(failure.message).toContain("EACCES");
const imported = await importTree(root);
if (!imported.ok) throw new Error(imported.error.message);
const entry = imported.value.entries.find(
(item) => item.path === failure.path,
);
expect(entry?.limitations.join(" ")).toContain(failure.message);
} finally {
await chmod(path, 0o600);
}
},
);
it.skipIf(process.platform === "win32")(
"retains the unsupported file-type reason without suggesting another OS",
async () => {
const root = await createTestTempDirectory("rea-import-native-fifo-");
await promisify(execFile)("mkfifo", [join(root, "events.pipe")], {
timeout: 2_000,
});
const imported = await importTree(root);
if (!imported.ok) throw new Error(imported.error.message);
const entry = imported.value.entries.find(
(item) => item.path === "events.pipe",
);
expect(entry?.limitations.join(" ")).toContain(
"Entry is not a regular file",
);
expect(entry?.limitations.join(" ")).not.toContain("supported system");
},
);
it.skipIf(process.platform === "win32")(
"excludes secret-patterned paths before failure projection",
async () => {
const root = await createTestTempDirectory("rea-import-native-secret-");
await promisify(execFile)("mkfifo", [join(root, "secret-events.pipe")], {
timeout: 2_000,
});
const imported = await importReferenceSource({
root,
caller: "native-entry-secret-detail-test",
policy: { secretPatterns: ["*secret*"] },
});
if (!imported.ok) throw new Error(imported.error.message);
expect(
imported.value.entries.find(
(item) => item.path === "secret-events.pipe",
),
).toBeUndefined();
expect(imported.value.exclusions).toContainEqual({
path: "secret-events.pipe",
reason: "configured-secret",
pattern: "*secret*",
});
},
);
it("preserves an ordinary readable source-file control", async () => {
const root = await createTestTempDirectory("rea-import-native-readable-");
await writeFile(join(root, "main.ts"), "export const value = 1;\n");
const imported = await importTree(root);
if (!imported.ok) throw new Error(imported.error.message);
expect(
imported.value.entries.find(({ path }) => path === "main.ts"),
).toMatchObject({ kind: "file", content_state: "hashed", limitations: [] });
});
});
describe("reference import paths beyond the platform limit", () => {
it.skipIf(process.platform === "win32")(
"reports an entry beyond the absolute path limit as an I/O failure",
async () => {
const root = await createTestTempDirectory("rea-import-deep-path-");
await writeFile(join(root, "kept.txt"), "k");
// Relative creation lets the tree exceed PATH_MAX (1024 on macOS, 4096
// on Linux) while each 200-byte component stays below NAME_MAX.
const deepTree = (action: "create" | "remove") =>
promisify(execFile)(process.execPath, [
"-e",
`const fs = require("node:fs");
const names = Array.from({ length: 24 }, (_, i) => "d" + String(i).padStart(2, "0") + "x".repeat(197));
process.chdir(${JSON.stringify(root)});
if (${JSON.stringify(action)} === "create") {
for (const name of names) { fs.mkdirSync(name); process.chdir(name); }
fs.writeFileSync("deep.txt", "d");
} else {
for (const name of names) process.chdir(name);
fs.rmSync("deep.txt");
for (const name of [...names].reverse()) { process.chdir(".."); fs.rmdirSync(name); }
}`,
]);
await deepTree("create");
try {
const read = await readReferenceSource(root);
if (!read.ok) throw new Error(read.error.message);
expect(read.value.entries).toContainEqual(
expect.objectContaining({ path: "kept.txt", status: "read" }),
);
const failed = read.value.entries.find(
(entry) => entry.status === "failed",
);
expect(failed).toMatchObject({
status: "failed",
code: "io",
message: expect.stringContaining("ENAMETOOLONG"),
});
const filteredRead = await readReferenceSource(root, {
shouldExclude: (path, kind) =>
path === failed?.path && kind === "directory",
});
if (!filteredRead.ok) throw new Error(filteredRead.error.message);
expect(
filteredRead.value.entries.find(
(entry) => entry.path === failed?.path,
),
).toMatchObject({ status: "failed", kind: "unknown", code: "io" });
const imported = await importTree(root);
if (!imported.ok) throw new Error(imported.error.message);
expect(imported.value.entries).toContainEqual(
expect.objectContaining({
path: "kept.txt",
content_state: "hashed",
}),
);
const deep = imported.value.entries.find(
(entry) => entry.path === failed?.path,
);
expect(deep?.limitations.join(" ")).toContain(
failed?.status === "failed" ? failed.message : "missing failure",
);
// A directory-only secret rule still applies when the kind is unknown.
const secretPattern = `${failed?.path ?? "missing"}/`;
const secret = await importReferenceSource({
root,
caller: "native-entry-failure-detail-test",
policy: { secretPatterns: [secretPattern] },
});
if (!secret.ok) throw new Error(secret.error.message);
expect(
secret.value.entries.some(({ path }) => path === failed?.path),
).toBe(false);
expect(secret.value.exclusions).toContainEqual({
path: failed?.path,
reason: "configured-secret",
pattern: secretPattern,
});
} finally {
await deepTree("remove");
}
},
);
});