fix(reference)!: preserve exclusion rule provenance

Record the exact matching pattern and distinguish project, default, caller, and configured-sensitive exclusions without changing ignore ordering. Include that provenance in graph commitments and validate the contract through compiled CLI and MCP consumers.

BREAKING CHANGE: Pattern-based reference graph exclusions require pattern and distinguish project-ignored and default-ignored reasons. Re-import saved graphs that omit matching patterns.
This commit is contained in:
morluto
2026-10-11 04:15:26 +08:00
parent be44b5098e
commit 6585293c56
9 changed files with 318 additions and 32 deletions
+44 -13
View File
@@ -15,7 +15,10 @@ import {
type ReferenceSourceImportError,
type ReferenceSourceImportOptions,
} from "./ReferenceSourceImportTypes.js";
import { prepareReferenceSourceImport } from "./ReferenceSourceImportPolicy.js";
import {
prepareReferenceSourceImport,
type PreparedReferenceSourceImport,
} from "./ReferenceSourceImportPolicy.js";
const failure = (
code: ReferenceSourceImportError["code"],
@@ -115,9 +118,47 @@ const sortExclusions = (
[...exclusions].sort((left, right) => {
const byPath = compareUnicodeCodePoints(left.path, right.path);
if (byPath !== 0) return byPath;
return compareUnicodeCodePoints(left.reason, right.reason);
const byReason = compareUnicodeCodePoints(left.reason, right.reason);
if (byReason !== 0) return byReason;
return compareUnicodeCodePoints(
"pattern" in left ? left.pattern : "",
"pattern" in right ? right.pattern : "",
);
});
const createShouldExclude =
(
exclusions: HistoricalSourceGraphInput["exclusions"],
secrets: PreparedReferenceSourceImport["secrets"],
ignored: PreparedReferenceSourceImport["ignored"],
): ((path: string) => boolean) =>
(path) => {
const secretMatch = secrets.test(path);
if (secretMatch.ignored) {
if (!secretMatch.rule)
throw new Error(`Ignored secret path has no matching rule: ${path}`);
exclusions.push({
path,
reason: "configured-secret",
pattern: secretMatch.rule.pattern,
});
return true;
}
const match = ignored.test(path);
if (!match.ignored) return false;
if (!match.rule)
throw new Error(`Ignored path has no matching rule: ${path}`);
const reason = match.rule.mark;
if (
reason !== "project-ignored" &&
reason !== "default-ignored" &&
reason !== "caller-excluded"
)
throw new Error(`Ignored path has unknown rule origin: ${path}`);
exclusions.push({ path, reason, pattern: match.rule.pattern });
return true;
};
/**
* Import a reference source directory into a committed historical source graph.
*
@@ -135,17 +176,7 @@ export const importReferenceSource = async (
if (isAborted(options.signal)) return err(cancelled());
const exclusions: HistoricalSourceGraphInput["exclusions"] = [];
const shouldExclude = (path: string): boolean => {
if (secrets.ignores(path)) {
exclusions.push({ path, reason: "configured-secret" });
return true;
}
if (ignored.ignores(path)) {
exclusions.push({ path, reason: "caller-excluded" });
return true;
}
return false;
};
const shouldExclude = createShouldExclude(exclusions, secrets, ignored);
const [readResult, vcs] = await Promise.all([
readReferenceSource(root, {
+20 -4
View File
@@ -30,6 +30,16 @@ const failure = (
const isAborted = (signal?: AbortSignal): boolean => signal?.aborted === true;
const addMarkedPatterns = (
matcher: ReturnType<typeof ignore>,
patterns: readonly string[],
mark: "project-ignored" | "default-ignored" | "caller-excluded",
): void => {
for (const pattern of patterns)
for (const line of pattern.split(/\r?\n/u))
matcher.add({ pattern: line, mark });
};
const resolveRoot = async (
requestedRoot: string,
signal?: AbortSignal,
@@ -87,11 +97,17 @@ const buildIgnored = async (
present = false;
}
signal?.throwIfAborted();
if (present) ignored.add(await readRegularFileText(policyPath, { signal }));
ignored.add([...DEFAULT_REFERENCE_SOURCE_IGNORE_PATTERNS]);
if (present) {
const text = await readRegularFileText(policyPath, { signal });
addMarkedPatterns(ignored, [text], "project-ignored");
}
addMarkedPatterns(
ignored,
DEFAULT_REFERENCE_SOURCE_IGNORE_PATTERNS,
"default-ignored",
);
for (const path of excludePaths) {
ignored.add(path);
ignored.add(`${path}/`);
addMarkedPatterns(ignored, [path, `${path}/`], "caller-excluded");
}
return ignored;
};
+26 -2
View File
@@ -175,7 +175,11 @@ describe("historical source graph", () => {
},
(input: HistoricalSourceGraphInput) => {
input.exclusions = [
{ path: "private.env", reason: "configured-secret" },
{
path: "private.env",
reason: "configured-secret",
pattern: ".env",
},
];
input.inventory_state = "partial";
},
@@ -188,6 +192,20 @@ describe("historical source graph", () => {
}
});
it("commits the matched pattern for each exclusion", () => {
const graphWith = (pattern: string) =>
createHistoricalSourceGraph({
...graphInput(),
inventory_state: "partial",
exclusions: [
{ path: "private.env", reason: "configured-secret", pattern },
],
});
expect(graphWith(".env").root_sha256).not.toBe(
graphWith("*.env").root_sha256,
);
});
it("retains actual external targets and rejects placeholder targets", () => {
const input = { ...graphInput(), inventory_state: "partial" as const };
input.entries[2] = symlinkEntry({
@@ -300,7 +318,13 @@ describe("partial historical source graphs", () => {
{ entries: partial.entries },
{ parse_failures: partial.parse_failures },
{
exclusions: [{ path: "secret", reason: "configured-secret" as const }],
exclusions: [
{
path: "secret",
reason: "configured-secret" as const,
pattern: "secret",
},
],
},
{ limitations: ["Incomplete"] },
{
+20 -10
View File
@@ -127,15 +127,22 @@ const parseFailureSchema = z.strictObject({
reason: boundedTextSchema,
});
const exclusionSchema = z.strictObject({
path: relativePathSchema,
reason: z.enum([
"configured-secret",
"symlink-escape",
"unreadable",
"caller-excluded",
]),
});
const exclusionSchema = z.discriminatedUnion("reason", [
z.strictObject({
path: relativePathSchema,
reason: z.enum([
"configured-secret",
"project-ignored",
"default-ignored",
"caller-excluded",
]),
pattern: boundedTextSchema,
}),
z.strictObject({
path: relativePathSchema,
reason: z.enum(["symlink-escape", "unreadable"]),
}),
]);
const vcsSchema = z
.discriminatedUnion("kind", [
@@ -405,7 +412,10 @@ function checkGraphInvariants(
context,
);
checkSortedUnique(
graph.exclusions.map(({ path, reason }) => `${path}\u0000${reason}`),
graph.exclusions.map(
(exclusion) =>
`${exclusion.path}\u0000${exclusion.reason}\u0000${"pattern" in exclusion ? exclusion.pattern : ""}`,
),
"exclusions",
context,
);