@diffci.com/diffci 0.3.18 → 0.3.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-client/src/client/cli.js +4 -2
- package/dist-client/src/client/verify.js +24 -3
- package/dist-client/src/client/working-tree.js +12 -0
- package/node_modules/@diffci.com/core/README.md +23 -10
- package/node_modules/@diffci.com/core/dist/git/git-diff.js +70 -3
- package/node_modules/@diffci.com/core/dist/git/git-diff.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/git/types.d.ts +6 -3
- package/node_modules/@diffci.com/core/dist/planner/check-narrowing.js +1 -1
- package/node_modules/@diffci.com/core/dist/planner/check-narrowing.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/planner/script-reads.js +1 -1
- package/node_modules/@diffci.com/core/dist/planner/script-reads.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/planner/type-program.js +1 -1
- package/node_modules/@diffci.com/core/dist/planner/type-program.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/file-references.d.ts +4 -0
- package/node_modules/@diffci.com/core/dist/repo/file-references.js +9 -2
- package/node_modules/@diffci.com/core/dist/repo/file-references.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/impact.d.ts +1 -0
- package/node_modules/@diffci.com/core/dist/repo/impact.js +38 -10
- package/node_modules/@diffci.com/core/dist/repo/impact.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/lockfiles.d.ts +9 -0
- package/node_modules/@diffci.com/core/dist/repo/lockfiles.js +11 -0
- package/node_modules/@diffci.com/core/dist/repo/lockfiles.js.map +1 -0
- package/node_modules/@diffci.com/core/dist/repo/yarn-lockfile.d.ts +24 -0
- package/node_modules/@diffci.com/core/dist/repo/yarn-lockfile.js +352 -0
- package/node_modules/@diffci.com/core/dist/repo/yarn-lockfile.js.map +1 -0
- package/node_modules/@diffci.com/core/src/git/git-diff.ts +59 -3
- package/node_modules/@diffci.com/core/src/git/types.ts +6 -3
- package/node_modules/@diffci.com/core/src/planner/check-narrowing.ts +1 -1
- package/node_modules/@diffci.com/core/src/planner/script-reads.ts +1 -1
- package/node_modules/@diffci.com/core/src/planner/type-program.ts +1 -1
- package/node_modules/@diffci.com/core/src/repo/file-references.ts +13 -2
- package/node_modules/@diffci.com/core/src/repo/impact.ts +35 -10
- package/node_modules/@diffci.com/core/src/repo/lockfiles.ts +10 -0
- package/node_modules/@diffci.com/core/src/repo/yarn-lockfile.ts +318 -0
- package/package.json +2 -2
|
@@ -251,6 +251,7 @@ export class ImpactAnalyzer {
|
|
|
251
251
|
private layout: RepositoryLayout = UNKNOWN_REPOSITORY_LAYOUT;
|
|
252
252
|
private repositoryFiles: ReadonlySet<string> | undefined;
|
|
253
253
|
private contentCleared: ReadonlySet<string> = new Set();
|
|
254
|
+
private dependencyLockfile: string | undefined;
|
|
254
255
|
private readRepositoryFile: ((path: string) => string | undefined) | undefined;
|
|
255
256
|
private sortedRepositoryFiles: string[] = [];
|
|
256
257
|
private compiledProjectCache: CompiledProject[] | undefined;
|
|
@@ -290,6 +291,7 @@ export class ImpactAnalyzer {
|
|
|
290
291
|
});
|
|
291
292
|
const referenceResolved = new Set(delta.analysis.referenceResolvedPaths ?? []);
|
|
292
293
|
this.contentCleared = referenceResolved;
|
|
294
|
+
this.dependencyLockfile = delta.analysis.dependencyLockfile;
|
|
293
295
|
const changedImpacts: ChangedImpact[] = delta.files.map((file) => ({ file, category: classifyChangedFile(file, this.isTestFile, this.layout, options.repositoryFiles, referenceResolved), reasons: changedFileReasons(file) }));
|
|
294
296
|
const evidence: ImpactEvidence[] = [];
|
|
295
297
|
const riskSignals: ImpactRiskSignal[] = [];
|
|
@@ -499,20 +501,33 @@ export class ImpactAnalyzer {
|
|
|
499
501
|
const packages = delta.analysis.changedDependencies;
|
|
500
502
|
if (!packages || packages.length === 0) return;
|
|
501
503
|
const { graph } = graphResult;
|
|
504
|
+
const yarnCandidates = delta.analysis.dependencyLockfile === "yarn.lock";
|
|
505
|
+
const changedFile = delta.analysis.dependencyLockfile ?? "package.json";
|
|
506
|
+
const dependencyChange = (name: string) => yarnCandidates
|
|
507
|
+
? `Dependency ${name} may be affected by the Yarn lockfile change` : `Dependency ${name} changed`;
|
|
502
508
|
const fail = (message: string) => {
|
|
503
|
-
riskSignals.push({ level: "critical", reason: "DEPENDENCY_MANIFEST", message, paths: [
|
|
509
|
+
riskSignals.push({ level: "critical", reason: "DEPENDENCY_MANIFEST", message, paths: [changedFile] });
|
|
504
510
|
if (!fallbackReasons.includes(message)) fallbackReasons.push(message);
|
|
505
511
|
};
|
|
506
512
|
// Typecheck and build tasks still see a dependency change (planner globalRiskTriggers).
|
|
507
|
-
riskSignals.push({ level: "info", reason: "DEPENDENCY_MANIFEST", message:
|
|
513
|
+
riskSignals.push({ level: "info", reason: "DEPENDENCY_MANIFEST", message: yarnCandidates
|
|
514
|
+
? `Dependencies potentially affected by Yarn lockfile change: ${packages.join(", ")}`
|
|
515
|
+
: `Dependencies changed: ${packages.join(", ")}`, paths: [changedFile] });
|
|
508
516
|
if (!this.repositoryFiles || !this.readRepositoryFile) {
|
|
509
|
-
fail(
|
|
517
|
+
fail(`${yarnCandidates ? "Potential dependency impact" : "Dependency change"} (${packages.join(", ")}) cannot be traced without repository contents; full validation required`);
|
|
510
518
|
return;
|
|
511
519
|
}
|
|
512
520
|
const packageRoot = (specifier: string) => {
|
|
513
521
|
const parts = specifier.split("/");
|
|
514
522
|
return specifier.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0]!;
|
|
515
523
|
};
|
|
524
|
+
if (yarnCandidates) {
|
|
525
|
+
// Classic can hoist transitive packages that repository code imports without declaring them.
|
|
526
|
+
// All-root widening covers peers, but cannot attribute these additional importers to a root.
|
|
527
|
+
const undeclared = [...new Set(graphResult.references.filter((ref) => ref.resolution === "external-package")
|
|
528
|
+
.map((ref) => packageRoot(ref.specifier)).filter((name) => !packages.includes(name)))].sort();
|
|
529
|
+
if (undeclared.length) fail(`Yarn lockfile changed and repository imports undeclared dependencies (${undeclared.join(", ")}); full validation required`);
|
|
530
|
+
}
|
|
516
531
|
const scripts = profile.packageJson.scripts;
|
|
517
532
|
const closureBodies = [...testScriptClosure(scripts)].map((name) => scripts[name] ?? "");
|
|
518
533
|
for (const name of packages) {
|
|
@@ -520,30 +535,30 @@ export class ImpactAnalyzer {
|
|
|
520
535
|
.filter((ref) => ref.resolution === "external-package" && packageRoot(ref.specifier) === name)
|
|
521
536
|
.map((ref) => ref.importer))].sort();
|
|
522
537
|
if (importers.length === 0) {
|
|
523
|
-
fail(
|
|
538
|
+
fail(`${dependencyChange(name)} and no repository source imports it; full validation required`);
|
|
524
539
|
continue;
|
|
525
540
|
}
|
|
526
541
|
const quoted = new RegExp(`["'\`]${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?:/[^"'\`]*)?["'\`]`);
|
|
527
542
|
const bare = new RegExp(`(?:^|[\\s"'=])${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?=$|[\\s"'/@])`);
|
|
528
543
|
if (closureBodies.some((body) => bare.test(body))) {
|
|
529
|
-
fail(
|
|
544
|
+
fail(`${dependencyChange(name)} and the test script runs it; full validation required`);
|
|
530
545
|
continue;
|
|
531
546
|
}
|
|
532
547
|
const toolingReader = this.sortedRepositoryFiles.find((path) => isImplicitlyLoadedFile(path) && !isLockfile(path) && quoted.test(this.readRepositoryFile!(path) ?? ""));
|
|
533
548
|
if (toolingReader) {
|
|
534
|
-
fail(
|
|
549
|
+
fail(`${dependencyChange(name)} and ${toolingReader} loads it; full validation required`);
|
|
535
550
|
continue;
|
|
536
551
|
}
|
|
537
552
|
for (const importer of importers) {
|
|
538
|
-
evidence.push(makeEvidence("DEPENDENCY",
|
|
539
|
-
if (this.isTestFile(importer)) this.addAffectedTest(
|
|
553
|
+
evidence.push(makeEvidence("DEPENDENCY", changedFile, `${importer} imports ${yarnCandidates ? "potentially affected" : "changed"} dependency ${name}`, importer));
|
|
554
|
+
if (this.isTestFile(importer)) this.addAffectedTest(changedFile, importer, graph, affectedTests, "DEPENDENCY", evidence);
|
|
540
555
|
this.processSourceChange(importer, graph, profile, affectedSources, affectedEntryPoints, affectedTests, affectedScripts, evidence);
|
|
541
556
|
}
|
|
542
557
|
// Code outside the graph that loads the package reaches the suite only if something runs it.
|
|
543
558
|
for (const path of this.sortedRepositoryFiles) {
|
|
544
559
|
if (!isSourceFilePath(path) || hasNode(graph, path) || !quoted.test(this.readRepositoryFile(path) ?? "")) continue;
|
|
545
560
|
if (!this.resolveByReference(path, graph, profile, affectedSources, affectedEntryPoints, affectedTests, affectedScripts, evidence)) {
|
|
546
|
-
fail(
|
|
561
|
+
fail(`${dependencyChange(name)} and ${path}, which loads it, is outside the dependency graph; full validation required`);
|
|
547
562
|
break;
|
|
548
563
|
}
|
|
549
564
|
}
|
|
@@ -571,6 +586,15 @@ export class ImpactAnalyzer {
|
|
|
571
586
|
return;
|
|
572
587
|
}
|
|
573
588
|
const category = changedImpact.category;
|
|
589
|
+
if (changedPath === this.dependencyLockfile) {
|
|
590
|
+
// Dependency selection does not cover a test that reads the lockfile's bytes. Unlike general
|
|
591
|
+
// data, no reader is acceptable here because the package-manager effect was modeled already.
|
|
592
|
+
if (this.resolveByReference(changedPath, graph, profile, affectedSources, affectedEntryPoints, affectedTests, affectedScripts, evidence, { allowNoReaders: true, implicitReadersUnresolvable: true })) return;
|
|
593
|
+
const message = `Lockfile readers could not be resolved: ${changedPath}; full validation required`;
|
|
594
|
+
riskSignals.push({ level: "critical", reason: "LOCKFILE_GLOBAL", message, paths: [changedPath] });
|
|
595
|
+
if (!fallbackReasons.includes(message)) fallbackReasons.push(message);
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
574
598
|
if (category === "test-fixture") {
|
|
575
599
|
// Resolved per change PATH (a rename's old path resolves independently); an unresolvable side
|
|
576
600
|
// degrades to the unknown-file fallback below rather than being silently dropped.
|
|
@@ -699,7 +723,7 @@ export class ImpactAnalyzer {
|
|
|
699
723
|
affectedTests: Map<string, TestImpact>,
|
|
700
724
|
affectedScripts: Map<string, ImpactEvidence[]>,
|
|
701
725
|
evidence: ImpactEvidence[],
|
|
702
|
-
{ implicitReadersUnresolvable = false }: { implicitReadersUnresolvable?: boolean } = {},
|
|
726
|
+
{ implicitReadersUnresolvable = false, allowNoReaders = false }: { implicitReadersUnresolvable?: boolean; allowNoReaders?: boolean } = {},
|
|
703
727
|
): boolean {
|
|
704
728
|
if (!this.repositoryFiles || !this.readRepositoryFile) return false;
|
|
705
729
|
if (isImplicitlyLoadedFile(changedPath) && !(isWorkerConfig(changedPath) && this.workerConfigsOutsideSuite())) return false;
|
|
@@ -715,6 +739,7 @@ export class ImpactAnalyzer {
|
|
|
715
739
|
workflowFeedsTests: (content, names) => workflowFeedsTests(content, names, scripts),
|
|
716
740
|
contentCleared: this.contentCleared,
|
|
717
741
|
implicitReadersUnresolvable,
|
|
742
|
+
allowNoReaders,
|
|
718
743
|
testPhases: this.selectTestPhases,
|
|
719
744
|
workerConfigsOutsideSuite: this.workerConfigsOutsideSuite(),
|
|
720
745
|
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
/**
|
|
3
|
+
* The one lockfile-name pattern behind every lockfile signal (2026-10-30, issue #47).
|
|
4
|
+
*
|
|
5
|
+
* Four copies of this regex had drifted apart: the planner's three (`check-narrowing.ts`,
|
|
6
|
+
* `script-reads.ts`, `type-program.ts`) knew npm's shrinkwrap and Bun's text lockfile, while the
|
|
7
|
+
* git-diff signal in `git-diff.ts` did not, so a change to `bun.lock` or `npm-shrinkwrap.json`
|
|
8
|
+
* alone still forced the full suite. Sharing this constant keeps the signals in agreement.
|
|
9
|
+
*/
|
|
10
|
+
export const LOCKFILE = /^(?:package-lock\.json|npm-shrinkwrap\.json|pnpm-lock\.yaml|yarn\.lock|bun\.lockb?)$/;
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
/**
|
|
3
|
+
* Conservative dependency candidates for Yarn Classic v1 lockfile changes.
|
|
4
|
+
*
|
|
5
|
+
* Unlike npm's installed-path lockfile, Classic records request selectors and omits peer dependency
|
|
6
|
+
* relationships and the hoisted install layout. Traversing dependencies alone therefore cannot prove
|
|
7
|
+
* that another direct package is unaffected. A semantic change returns ALL old/new direct names,
|
|
8
|
+
* after validating exact manifest requests and attributing every changed selector to a root. The
|
|
9
|
+
* caller must still account for tooling, lockfile readers and undeclared imports of hoisted packages.
|
|
10
|
+
* This is a candidate set for conservative selection, not an exact list of changed installed trees.
|
|
11
|
+
*
|
|
12
|
+
* Only generated v1 registry entries with version/resolved/integrity and dependency mappings are
|
|
13
|
+
* modeled. Berry, workspaces/aliases/local or git requests, missing edges, ambiguous or malformed
|
|
14
|
+
* syntax and unfamiliar fields return undefined so the caller retains full validation. This small
|
|
15
|
+
* parser intentionally accepts less than Yarn's general parser; it never repairs merge conflicts.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
type DirectDependencies = Readonly<Record<string, string>>;
|
|
19
|
+
|
|
20
|
+
interface LockEntry {
|
|
21
|
+
version: string;
|
|
22
|
+
resolved: string;
|
|
23
|
+
integrity?: string;
|
|
24
|
+
dependencies: Map<string, string>;
|
|
25
|
+
optionalDependencies: Map<string, string>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface Token { value: string; end: number }
|
|
29
|
+
|
|
30
|
+
// Keep parsing bounded independently of the repository's Git output limit. Bigger lockfiles fall
|
|
31
|
+
// back; these are coverage limits, not a reason to interpret a prefix as a complete lockfile.
|
|
32
|
+
const MAX_CONTENT_LENGTH = 8 * 1024 * 1024;
|
|
33
|
+
const MAX_LINES = 100_000;
|
|
34
|
+
const MAX_LINE_LENGTH = 16_384;
|
|
35
|
+
const MAX_SELECTORS = 100_000;
|
|
36
|
+
const PACKAGE_NAME = /^(?:@[a-zA-Z0-9][a-zA-Z0-9._-]*\/)?[a-zA-Z0-9][a-zA-Z0-9._-]*$/;
|
|
37
|
+
const NUMERIC_PART = "(?:0|[1-9][0-9]*)";
|
|
38
|
+
const IDENTIFIERS = "[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*";
|
|
39
|
+
const EXACT_VERSION = new RegExp(`^${NUMERIC_PART}\\.${NUMERIC_PART}\\.${NUMERIC_PART}(?:-${IDENTIFIERS})?(?:\\+${IDENTIFIERS})?$`);
|
|
40
|
+
const RANGE_VERSION = new RegExp(`^v?(?:${NUMERIC_PART}|[xX*])(?:\\.(?:${NUMERIC_PART}|[xX*])){0,2}$`);
|
|
41
|
+
|
|
42
|
+
function isExactVersion(value: string): boolean {
|
|
43
|
+
if (!EXACT_VERSION.test(value)) return false;
|
|
44
|
+
const prerelease = /^[^-]+-([^+]+)/.exec(value)?.[1];
|
|
45
|
+
return !prerelease || prerelease.split(".").every((part) => !/^0[0-9]+$/.test(part));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isRangeVersion(value: string): boolean {
|
|
49
|
+
const version = value.replace(/^v/, "");
|
|
50
|
+
if (isExactVersion(version)) return true;
|
|
51
|
+
if (!RANGE_VERSION.test(value)) return false;
|
|
52
|
+
// Once a wildcard is used, a later numeric component is not a modeled range (e.g. 1.x.3).
|
|
53
|
+
const parts = version.split(".");
|
|
54
|
+
const wildcard = parts.findIndex((part) => /^[xX*]$/.test(part));
|
|
55
|
+
return wildcard === -1 || parts.slice(wildcard).every((part) => /^[xX*]$/.test(part));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A deliberately limited registry semver grammar; tags and protocol requests remain unsupported. */
|
|
59
|
+
function isRegistryRequest(value: string): boolean {
|
|
60
|
+
if (!value || value.length > 512 || value !== value.trim()) return false;
|
|
61
|
+
return value.split("||").every((alternative) => {
|
|
62
|
+
const range = alternative.trim();
|
|
63
|
+
if (!range) return false;
|
|
64
|
+
const hyphen = range.split(" - ");
|
|
65
|
+
if (hyphen.length > 1) return hyphen.length === 2 && hyphen.every(isRangeVersion);
|
|
66
|
+
// Yarn also accepts spaces after comparators. Normalizing them here only validates syntax;
|
|
67
|
+
// selector lookup below still uses the exact original request from the manifest or entry.
|
|
68
|
+
const comparators = range.replace(/([<>]=?|[=~^]) +/g, "$1").split(/ +/);
|
|
69
|
+
return comparators.every((part) => {
|
|
70
|
+
const match = /^(?:<=|>=|<|>|=|~|\^)?(.+)$/.exec(part);
|
|
71
|
+
return !!match && isRangeVersion(match[1]!);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function descriptor(name: string, request: string): string | undefined {
|
|
77
|
+
return PACKAGE_NAME.test(name) && isRegistryRequest(request) ? `${name}@${request}` : undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function validSelector(selector: string): boolean {
|
|
81
|
+
const separator = selector.indexOf("@", selector.startsWith("@") ? 1 : 0);
|
|
82
|
+
return separator > 0 && descriptor(selector.slice(0, separator), selector.slice(separator + 1)) === selector;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Read a JSON-quoted string or a Classic bare string, without interpreting comments as content. */
|
|
86
|
+
function tokenAt(text: string, start: number): Token | undefined {
|
|
87
|
+
if (text[start] === '"') {
|
|
88
|
+
let end = start + 1;
|
|
89
|
+
for (; end < text.length; end++) {
|
|
90
|
+
if (text[end] === "\\") { end++; continue; }
|
|
91
|
+
if (text[end] !== '"') continue;
|
|
92
|
+
try {
|
|
93
|
+
const value: unknown = JSON.parse(text.slice(start, end + 1));
|
|
94
|
+
if (typeof value !== "string" || /[\u0000-\u001f\u007f]/.test(value)) return undefined;
|
|
95
|
+
return { value, end: end + 1 };
|
|
96
|
+
} catch { return undefined; }
|
|
97
|
+
}
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
const match = /^[a-zA-Z/.-][^\s:,"\\\[\]{}]*/.exec(text.slice(start));
|
|
101
|
+
return match ? { value: match[0], end: start + match[0].length } : undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function selectorHeader(line: string): string[] | undefined {
|
|
105
|
+
const selectors: string[] = [];
|
|
106
|
+
const seen = new Set<string>();
|
|
107
|
+
let packageName: string | undefined;
|
|
108
|
+
let position = 0;
|
|
109
|
+
while (position < line.length) {
|
|
110
|
+
const token = tokenAt(line, position);
|
|
111
|
+
if (!token || !validSelector(token.value) || seen.has(token.value)) return undefined;
|
|
112
|
+
const separator = token.value.indexOf("@", token.value.startsWith("@") ? 1 : 0);
|
|
113
|
+
const name = token.value.slice(0, separator);
|
|
114
|
+
if (packageName !== undefined && packageName !== name) return undefined;
|
|
115
|
+
packageName = name;
|
|
116
|
+
selectors.push(token.value);
|
|
117
|
+
seen.add(token.value);
|
|
118
|
+
position = token.end;
|
|
119
|
+
while (line[position] === " ") position++;
|
|
120
|
+
if (line[position] === ":") return line.slice(position + 1).trim() === "" ? selectors : undefined;
|
|
121
|
+
if (line[position] !== ",") return undefined;
|
|
122
|
+
position++;
|
|
123
|
+
while (line[position] === " ") position++;
|
|
124
|
+
}
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function pair(line: string): [string, string] | undefined {
|
|
129
|
+
const key = tokenAt(line, 0);
|
|
130
|
+
if (!key || line[key.end] !== " ") return undefined;
|
|
131
|
+
let position = key.end;
|
|
132
|
+
while (line[position] === " ") position++;
|
|
133
|
+
const value = tokenAt(line, position);
|
|
134
|
+
if (!value || line.slice(value.end).trim() !== "") return undefined;
|
|
135
|
+
return [key.value, value.value];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function registryResolution(value: string, integrity: string | undefined): boolean {
|
|
139
|
+
if (!/^https?:\/\/[^\s\\]+$/.test(value)) return false;
|
|
140
|
+
try {
|
|
141
|
+
const url = new URL(value);
|
|
142
|
+
if (!/^https?:$/.test(url.protocol) || !url.hostname || url.username || url.password) return false;
|
|
143
|
+
// Registry URLs need not use a particular host or filename. Require a content pin, though:
|
|
144
|
+
// Classic's resolved fragment is SHA1; newer files can instead carry an integrity digest.
|
|
145
|
+
if (url.hash && !/^#[a-fA-F0-9]{40}$/.test(url.hash)) return false;
|
|
146
|
+
return !!url.hash || integrity !== undefined;
|
|
147
|
+
} catch { return false; }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function validIntegrity(value: string): boolean {
|
|
151
|
+
const sizes: Record<string, number> = { sha1: 20, sha256: 32, sha384: 48, sha512: 64 };
|
|
152
|
+
return value.split(" ").every((token) => {
|
|
153
|
+
const match = /^(sha(?:1|256|384|512))-([A-Za-z0-9+/]+={0,2})$/.exec(token);
|
|
154
|
+
if (!match) return false;
|
|
155
|
+
const digest = Buffer.from(match[2]!, "base64");
|
|
156
|
+
return digest.length === sizes[match[1]!] && digest.toString("base64").replace(/=+$/, "") === match[2]!.replace(/=+$/, "");
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function parse(content: string | undefined): Map<string, LockEntry> | undefined {
|
|
161
|
+
if (content === undefined || content.length > MAX_CONTENT_LENGTH) return undefined;
|
|
162
|
+
const text = content.replace(/^\uFEFF/, "").replace(/\r\n/g, "\n");
|
|
163
|
+
if (/[\u0000-\u0009\u000b-\u001f\u007f]/.test(text)) return undefined;
|
|
164
|
+
const lines = text.split("\n");
|
|
165
|
+
if (lines.length > MAX_LINES) return undefined;
|
|
166
|
+
const entries = new Map<string, LockEntry>();
|
|
167
|
+
let hasHeader = false;
|
|
168
|
+
let selectors: string[] = [];
|
|
169
|
+
let entry: LockEntry | undefined;
|
|
170
|
+
let fields = new Set<string>();
|
|
171
|
+
let section: "dependencies" | "optionalDependencies" | undefined;
|
|
172
|
+
|
|
173
|
+
const finish = (): boolean => {
|
|
174
|
+
if (!entry) return true;
|
|
175
|
+
if (!isExactVersion(entry.version) || !registryResolution(entry.resolved, entry.integrity)) return false;
|
|
176
|
+
for (const selector of selectors) {
|
|
177
|
+
if (entries.has(selector) || entries.size >= MAX_SELECTORS) return false;
|
|
178
|
+
entries.set(selector, entry);
|
|
179
|
+
}
|
|
180
|
+
return true;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
for (const rawLine of lines) {
|
|
184
|
+
if (rawLine.length > MAX_LINE_LENGTH) return undefined;
|
|
185
|
+
const line = rawLine.trimEnd();
|
|
186
|
+
if (!line) continue;
|
|
187
|
+
if (line.startsWith("#")) {
|
|
188
|
+
if (/^#\s*yarn lockfile\b/.test(line)) {
|
|
189
|
+
if (line !== "# yarn lockfile v1" || hasHeader || entry) return undefined;
|
|
190
|
+
hasHeader = true;
|
|
191
|
+
}
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if (!hasHeader) return undefined;
|
|
195
|
+
const indentation = line.length - line.trimStart().length;
|
|
196
|
+
if (indentation === 0) {
|
|
197
|
+
if (!finish()) return undefined;
|
|
198
|
+
const parsed = selectorHeader(line);
|
|
199
|
+
if (!parsed) return undefined;
|
|
200
|
+
selectors = parsed;
|
|
201
|
+
entry = { version: "", resolved: "", dependencies: new Map(), optionalDependencies: new Map() };
|
|
202
|
+
fields = new Set();
|
|
203
|
+
section = undefined;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (!entry) return undefined;
|
|
207
|
+
if (indentation === 2) {
|
|
208
|
+
const body = line.slice(2);
|
|
209
|
+
const key = tokenAt(body, 0);
|
|
210
|
+
if (!key || fields.has(key.value)) return undefined;
|
|
211
|
+
fields.add(key.value);
|
|
212
|
+
section = undefined;
|
|
213
|
+
if (key.value === "dependencies" || key.value === "optionalDependencies") {
|
|
214
|
+
if (body.slice(key.end) !== ":") return undefined;
|
|
215
|
+
section = key.value;
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
const scalar = pair(body);
|
|
219
|
+
if (!scalar) return undefined;
|
|
220
|
+
if (scalar[0] === "version" || scalar[0] === "resolved") entry[scalar[0]] = scalar[1];
|
|
221
|
+
else if (scalar[0] === "integrity") {
|
|
222
|
+
if (!validIntegrity(scalar[1])) return undefined;
|
|
223
|
+
entry.integrity = scalar[1].split(" ").sort().join(" ");
|
|
224
|
+
} else return undefined;
|
|
225
|
+
} else if (indentation === 4 && section) {
|
|
226
|
+
const dependency = pair(line.slice(4));
|
|
227
|
+
if (!dependency || !descriptor(...dependency)) return undefined;
|
|
228
|
+
const [name, request] = dependency;
|
|
229
|
+
if (entry.dependencies.has(name) || entry.optionalDependencies.has(name)) return undefined;
|
|
230
|
+
entry[section].set(name, request);
|
|
231
|
+
} else return undefined;
|
|
232
|
+
}
|
|
233
|
+
if (!hasHeader || !finish()) return undefined;
|
|
234
|
+
// Even an unchanged or unreachable entry with missing dependency data makes the model incomplete.
|
|
235
|
+
for (const entry of new Set(entries.values())) {
|
|
236
|
+
for (const [name, request] of [...entry.dependencies, ...entry.optionalDependencies]) {
|
|
237
|
+
if (!entries.has(`${name}@${request}`)) return undefined;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return entries;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function roots(entries: Map<string, LockEntry>, direct: DirectDependencies): string[] | undefined {
|
|
244
|
+
if (!direct || typeof direct !== "object" || Array.isArray(direct)) return undefined;
|
|
245
|
+
const prototype: unknown = Object.getPrototypeOf(direct);
|
|
246
|
+
if (prototype !== null && prototype !== Object.prototype) return undefined;
|
|
247
|
+
const result: string[] = [];
|
|
248
|
+
for (const [name, request] of Object.entries(direct)) {
|
|
249
|
+
if (typeof request !== "string") return undefined;
|
|
250
|
+
const selector = descriptor(name, request);
|
|
251
|
+
if (!selector || !entries.has(selector)) return undefined;
|
|
252
|
+
result.push(selector);
|
|
253
|
+
}
|
|
254
|
+
return result;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function reachable(entries: Map<string, LockEntry>, roots: string[]): Set<string> {
|
|
258
|
+
const seen = new Set<string>();
|
|
259
|
+
const expanded = new Set<LockEntry>();
|
|
260
|
+
const queue = [...roots];
|
|
261
|
+
while (queue.length) {
|
|
262
|
+
const selector = queue.pop()!;
|
|
263
|
+
if (seen.has(selector)) continue;
|
|
264
|
+
seen.add(selector);
|
|
265
|
+
const entry = entries.get(selector)!; // roots and all edges were checked before traversal
|
|
266
|
+
if (expanded.has(entry)) continue;
|
|
267
|
+
expanded.add(entry);
|
|
268
|
+
for (const [name, request] of [...entry.dependencies, ...entry.optionalDependencies]) {
|
|
269
|
+
const dependency = `${name}@${request}`;
|
|
270
|
+
if (!seen.has(dependency)) queue.push(dependency);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return seen;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function fingerprints(entries: Map<string, LockEntry>): Map<string, string> {
|
|
277
|
+
const byEntry = new Map<LockEntry, string>();
|
|
278
|
+
const bySelector = new Map<string, string>();
|
|
279
|
+
const sorted = (dependencies: Map<string, string>) => [...dependencies].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
280
|
+
for (const [selector, entry] of entries) {
|
|
281
|
+
let fingerprint = byEntry.get(entry);
|
|
282
|
+
if (fingerprint === undefined) {
|
|
283
|
+
fingerprint = JSON.stringify([entry.version, entry.resolved, entry.integrity ?? null, sorted(entry.dependencies), sorted(entry.optionalDependencies)]);
|
|
284
|
+
byEntry.set(entry, fingerprint);
|
|
285
|
+
}
|
|
286
|
+
bySelector.set(selector, fingerprint);
|
|
287
|
+
}
|
|
288
|
+
return bySelector;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Possible affected direct dependencies, or undefined when full validation must be retained.
|
|
293
|
+
* Exact requests from EACH manifest are required: a root name alone cannot identify its selector.
|
|
294
|
+
* An unchanged selector map returns []; the caller must not clear a manifest dependency edit unless
|
|
295
|
+
* its changed names are covered by the result. No semver resolution or package code is executed.
|
|
296
|
+
*/
|
|
297
|
+
export function yarnLockAffectedDependencies(
|
|
298
|
+
oldContent: string | undefined,
|
|
299
|
+
newContent: string | undefined,
|
|
300
|
+
beforeDirect: DirectDependencies,
|
|
301
|
+
afterDirect: DirectDependencies,
|
|
302
|
+
): string[] | undefined {
|
|
303
|
+
const before = parse(oldContent);
|
|
304
|
+
const after = parse(newContent);
|
|
305
|
+
if (!before || !after) return undefined;
|
|
306
|
+
const oldRoots = roots(before, beforeDirect);
|
|
307
|
+
const newRoots = roots(after, afterDirect);
|
|
308
|
+
if (!oldRoots || !newRoots) return undefined;
|
|
309
|
+
const oldFingerprints = fingerprints(before);
|
|
310
|
+
const newFingerprints = fingerprints(after);
|
|
311
|
+
const changed = [...new Set([...before.keys(), ...after.keys()])]
|
|
312
|
+
.filter((selector) => oldFingerprints.get(selector) !== newFingerprints.get(selector));
|
|
313
|
+
if (!changed.length) return [];
|
|
314
|
+
const oldReachable = reachable(before, oldRoots);
|
|
315
|
+
const newReachable = reachable(after, newRoots);
|
|
316
|
+
if (changed.some((selector) => !oldReachable.has(selector) && !newReachable.has(selector))) return undefined;
|
|
317
|
+
return [...new Set([...Object.keys(beforeDirect), ...Object.keys(afterDirect)])].sort();
|
|
318
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@diffci.com/diffci",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.19",
|
|
4
4
|
"description": "Fail-closed, change-aware verification for AI coding agents with conservative affected-test selection",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ci",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"test": "node scripts/run-tests.mjs"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@diffci.com/core": "git+https://github.com/DiffCI/core.git#
|
|
62
|
+
"@diffci.com/core": "git+https://github.com/DiffCI/core.git#0b82acbc69f650217c24c037527e4f8ad822ea91",
|
|
63
63
|
"yaml": "^2.9.0"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|