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:
@@ -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, {
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user