@diffci.com/diffci 0.3.15 → 0.3.17
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/agent-selection.js +30 -0
- package/dist-client/src/client/cli.js +21 -2
- package/dist-client/src/client/gate-checks.js +120 -0
- package/dist-client/src/client/hook.js +94 -0
- package/dist-client/src/client/mcp.js +11 -3
- package/dist-client/src/client/observe.js +7 -2
- package/dist-client/src/client/verify.js +49 -8
- package/node_modules/@diffci.com/core/LICENSE +21 -661
- package/node_modules/@diffci.com/core/NOTICE +2 -2
- package/node_modules/@diffci.com/core/README.md +21 -3
- package/node_modules/@diffci.com/core/dist/analyze.js +1 -1
- package/node_modules/@diffci.com/core/dist/analyze.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/cache/graph-cache.js +1 -1
- package/node_modules/@diffci.com/core/dist/cache/graph-cache.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/ci-inference/evidence.js +1 -1
- package/node_modules/@diffci.com/core/dist/ci-inference/evidence.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/ci-inference/infer.js +1 -1
- package/node_modules/@diffci.com/core/dist/ci-inference/infer.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/ci-inference/jobs.js +1 -1
- package/node_modules/@diffci.com/core/dist/ci-inference/jobs.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/ci-inference/purpose.js +1 -1
- package/node_modules/@diffci.com/core/dist/ci-inference/purpose.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/ci-inference/resolve.js +1 -1
- package/node_modules/@diffci.com/core/dist/ci-inference/resolve.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/ci-inference/schema.js +1 -1
- package/node_modules/@diffci.com/core/dist/ci-inference/schema.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/cli.js +1 -1
- package/node_modules/@diffci.com/core/dist/cli.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/git/git-diff.d.ts +29 -4
- package/node_modules/@diffci.com/core/dist/git/git-diff.js +136 -22
- 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 +7 -0
- package/node_modules/@diffci.com/core/dist/index.js +1 -1
- package/node_modules/@diffci.com/core/dist/index.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/measurement.js +1 -1
- package/node_modules/@diffci.com/core/dist/measurement.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/planner/check-narrowing.d.ts +45 -0
- package/node_modules/@diffci.com/core/dist/planner/check-narrowing.js +252 -0
- package/node_modules/@diffci.com/core/dist/planner/check-narrowing.js.map +1 -0
- package/node_modules/@diffci.com/core/dist/planner/pipeline-steps.d.ts +60 -0
- package/node_modules/@diffci.com/core/dist/planner/pipeline-steps.js +241 -0
- package/node_modules/@diffci.com/core/dist/planner/pipeline-steps.js.map +1 -0
- package/node_modules/@diffci.com/core/dist/planner/script-reads.d.ts +31 -0
- package/node_modules/@diffci.com/core/dist/planner/script-reads.js +197 -0
- package/node_modules/@diffci.com/core/dist/planner/script-reads.js.map +1 -0
- package/node_modules/@diffci.com/core/dist/planner/type-program.d.ts +33 -0
- package/node_modules/@diffci.com/core/dist/planner/type-program.js +106 -0
- package/node_modules/@diffci.com/core/dist/planner/type-program.js.map +1 -0
- package/node_modules/@diffci.com/core/dist/repo/compiled-projects.js +1 -1
- package/node_modules/@diffci.com/core/dist/repo/compiled-projects.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/file-references.d.ts +32 -0
- package/node_modules/@diffci.com/core/dist/repo/file-references.js +180 -9
- package/node_modules/@diffci.com/core/dist/repo/file-references.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/impact-types.d.ts +4 -0
- package/node_modules/@diffci.com/core/dist/repo/impact.d.ts +19 -0
- package/node_modules/@diffci.com/core/dist/repo/impact.js +105 -3
- package/node_modules/@diffci.com/core/dist/repo/impact.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/npm-lockfile.d.ts +27 -0
- package/node_modules/@diffci.com/core/dist/repo/npm-lockfile.js +116 -0
- package/node_modules/@diffci.com/core/dist/repo/npm-lockfile.js.map +1 -0
- package/node_modules/@diffci.com/core/dist/repo/script-universe.js +1 -1
- package/node_modules/@diffci.com/core/dist/repo/script-universe.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/workflow-test-steps.js +1 -1
- package/node_modules/@diffci.com/core/dist/repo/workflow-test-steps.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/repo/workspaces.js +1 -1
- package/node_modules/@diffci.com/core/dist/repo/workspaces.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/research/baseline/matcher.js +1 -1
- package/node_modules/@diffci.com/core/dist/research/baseline/matcher.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/research/baseline/test-activity.js +1 -1
- package/node_modules/@diffci.com/core/dist/research/baseline/test-activity.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/research/baseline/workflow-parser.js +1 -1
- package/node_modules/@diffci.com/core/dist/research/baseline/workflow-parser.js.map +1 -1
- package/node_modules/@diffci.com/core/package.json +1 -1
- package/node_modules/@diffci.com/core/src/analyze.ts +1 -1
- package/node_modules/@diffci.com/core/src/cache/graph-cache.ts +1 -1
- package/node_modules/@diffci.com/core/src/ci-inference/causal.ts +1 -1
- package/node_modules/@diffci.com/core/src/ci-inference/evidence.ts +1 -1
- package/node_modules/@diffci.com/core/src/ci-inference/expression.ts +1 -1
- package/node_modules/@diffci.com/core/src/ci-inference/infer.ts +1 -1
- package/node_modules/@diffci.com/core/src/ci-inference/jobs.ts +1 -1
- package/node_modules/@diffci.com/core/src/ci-inference/matrix.ts +1 -1
- package/node_modules/@diffci.com/core/src/ci-inference/purpose.ts +1 -1
- package/node_modules/@diffci.com/core/src/ci-inference/reference-graph.ts +1 -1
- package/node_modules/@diffci.com/core/src/ci-inference/resolve.ts +1 -1
- package/node_modules/@diffci.com/core/src/ci-inference/schema.ts +1 -1
- package/node_modules/@diffci.com/core/src/cli.ts +1 -1
- package/node_modules/@diffci.com/core/src/git/git-diff.ts +127 -21
- package/node_modules/@diffci.com/core/src/git/types.ts +7 -0
- package/node_modules/@diffci.com/core/src/index.ts +1 -1
- package/node_modules/@diffci.com/core/src/measurement.ts +1 -1
- package/node_modules/@diffci.com/core/src/planner/check-narrowing.ts +269 -0
- package/node_modules/@diffci.com/core/src/planner/pipeline-steps.ts +243 -0
- package/node_modules/@diffci.com/core/src/planner/script-reads.ts +174 -0
- package/node_modules/@diffci.com/core/src/planner/type-program.ts +103 -0
- package/node_modules/@diffci.com/core/src/repo/compiled-projects.ts +1 -1
- package/node_modules/@diffci.com/core/src/repo/file-references.ts +170 -9
- package/node_modules/@diffci.com/core/src/repo/impact-types.ts +4 -0
- package/node_modules/@diffci.com/core/src/repo/impact.ts +115 -2
- package/node_modules/@diffci.com/core/src/repo/npm-lockfile.ts +109 -0
- package/node_modules/@diffci.com/core/src/repo/script-universe.ts +1 -1
- package/node_modules/@diffci.com/core/src/repo/workflow-test-steps.ts +1 -1
- package/node_modules/@diffci.com/core/src/repo/workspaces.ts +1 -1
- package/node_modules/@diffci.com/core/src/research/baseline/matcher.ts +1 -1
- package/node_modules/@diffci.com/core/src/research/baseline/path-baseline.ts +1 -1
- package/node_modules/@diffci.com/core/src/research/baseline/registry.ts +1 -1
- package/node_modules/@diffci.com/core/src/research/baseline/test-activity.ts +1 -1
- package/node_modules/@diffci.com/core/src/research/baseline/workflow-parser.ts +1 -1
- package/package.json +2 -2
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
} from "./types.js";
|
|
13
13
|
import { infrastructureRole, testScriptClosure } from "../repo/file-references.js";
|
|
14
14
|
import { workflowMayRunTests } from "../repo/workflow-test-steps.js";
|
|
15
|
+
import { npmLockAffectedDependencies } from "../repo/npm-lockfile.js";
|
|
15
16
|
|
|
16
17
|
export const EMPTY_TREE_SHA = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
|
|
17
18
|
export const ZERO_SHA = "0000000000000000000000000000000000000000";
|
|
@@ -274,37 +275,109 @@ function scriptRecord(value: unknown): Record<string, string> | undefined {
|
|
|
274
275
|
return Object.fromEntries(Object.entries(value).filter((entry): entry is [string, string] => typeof entry[1] === "string"));
|
|
275
276
|
}
|
|
276
277
|
|
|
278
|
+
const TEST_FILE_WORD = /^(?:\.\/)?[A-Za-z0-9._@+\-/]+\.(?:test|spec)\.[cm]?[jt]sx?$/;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* The test files a test-closure script edit adds to or removes from an explicit file list, or
|
|
282
|
+
* undefined when the edit is anything else (2026-10-28). One dogfood repository's `test` script lists ~60 test
|
|
283
|
+
* files by path; agents add one to it in most commits that add a test, and every such edit forced the
|
|
284
|
+
* full suite (5 of the first 42 replayed commits). Only an insertion or removal of whole words that are
|
|
285
|
+
* test-file paths, inside an `&&` phase that runs `--test`, qualifies: the remaining words, in order,
|
|
286
|
+
* must be identical, so no flag, runner or other argument changed.
|
|
287
|
+
*/
|
|
288
|
+
export function testListEdit(oldBody: string, newBody: string): { added: string[]; removed: string[] } | undefined {
|
|
289
|
+
const phases = (body: string) => body.split("&&").map((phase) => phase.trim().split(/\s+/).filter(Boolean));
|
|
290
|
+
const oldPhases = phases(oldBody);
|
|
291
|
+
const newPhases = phases(newBody);
|
|
292
|
+
if (oldPhases.length !== newPhases.length) return undefined;
|
|
293
|
+
const added: string[] = [];
|
|
294
|
+
const removed: string[] = [];
|
|
295
|
+
for (let i = 0; i < oldPhases.length; i++) {
|
|
296
|
+
const before = oldPhases[i]!;
|
|
297
|
+
const after = newPhases[i]!;
|
|
298
|
+
const beforeSet = new Set(before);
|
|
299
|
+
const afterSet = new Set(after);
|
|
300
|
+
const phaseAdded = after.filter((word) => !beforeSet.has(word));
|
|
301
|
+
const phaseRemoved = before.filter((word) => !afterSet.has(word));
|
|
302
|
+
if (phaseAdded.length === 0 && phaseRemoved.length === 0) {
|
|
303
|
+
if (before.join(" ") !== after.join(" ")) return undefined; // reordered or duplicated words
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
if (!before.includes("--test") || !after.includes("--test")) return undefined;
|
|
307
|
+
if (![...phaseAdded, ...phaseRemoved].every((word) => TEST_FILE_WORD.test(word))) return undefined;
|
|
308
|
+
const kept = (words: string[], dropped: string[]) => words.filter((word) => !dropped.includes(word)).join(" ");
|
|
309
|
+
if (kept(before, phaseRemoved) !== kept(after, phaseAdded)) return undefined;
|
|
310
|
+
added.push(...phaseAdded.map((word) => word.replace(/^\.\//, "")));
|
|
311
|
+
removed.push(...phaseRemoved.map((word) => word.replace(/^\.\//, "")));
|
|
312
|
+
}
|
|
313
|
+
return { added, removed };
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const DEPENDENCY_FIELDS = new Set(["dependencies", "devDependencies", "optionalDependencies"]);
|
|
317
|
+
|
|
318
|
+
export interface PackageJsonChange {
|
|
319
|
+
kind: "metadata" | "data" | "dependencies" | "behavioral";
|
|
320
|
+
/** Test files a test-list edit added to the test script; they are now part of the suite. */
|
|
321
|
+
testScriptAddedPaths: string[];
|
|
322
|
+
/** Dependency names whose entry changed, when `kind` is `dependencies`. */
|
|
323
|
+
changedDependencies: string[];
|
|
324
|
+
}
|
|
325
|
+
|
|
277
326
|
/**
|
|
278
327
|
* How a modified package.json changed:
|
|
279
328
|
* - `metadata`: only descriptive fields (hasRelevantPackageJsonChange is false);
|
|
280
|
-
* - `data`: only PACKAGE_JSON_DATA_FIELDS
|
|
281
|
-
*
|
|
282
|
-
* file, so it is resolved by reference rather than ignored;
|
|
283
|
-
* - `
|
|
329
|
+
* - `data`: only PACKAGE_JSON_DATA_FIELDS, scripts outside the test closure (`test`, its lifecycle
|
|
330
|
+
* hooks, install hooks, and every script they name) on either side, and test-list edits (see
|
|
331
|
+
* testListEdit) - tests may read the file, so it is resolved by reference rather than ignored;
|
|
332
|
+
* - `dependencies`: as `data`, plus changed entries in dependencies, devDependencies or
|
|
333
|
+
* optionalDependencies. Which tests that affects depends on the lockfile and on who imports each
|
|
334
|
+
* package, so the caller decides, and stays behavioral when it cannot;
|
|
335
|
+
* - `behavioral`: anything else, including peerDependencies, overrides, engines, type, exports,
|
|
336
|
+
* workspaces and unparseable JSON.
|
|
284
337
|
*/
|
|
285
|
-
export function
|
|
286
|
-
|
|
338
|
+
export function packageJsonChange(oldContent: string | undefined, newContent: string | undefined): PackageJsonChange {
|
|
339
|
+
const result = (kind: PackageJsonChange["kind"], testScriptAddedPaths: string[] = [], changedDependencies: string[] = []): PackageJsonChange =>
|
|
340
|
+
({ kind, testScriptAddedPaths, changedDependencies });
|
|
341
|
+
if (!hasRelevantPackageJsonChange(oldContent, newContent)) return result("metadata");
|
|
287
342
|
let oldJson: Record<string, unknown>;
|
|
288
343
|
let newJson: Record<string, unknown>;
|
|
289
344
|
try {
|
|
290
345
|
oldJson = JSON.parse(oldContent!);
|
|
291
346
|
newJson = JSON.parse(newContent!);
|
|
292
347
|
} catch {
|
|
293
|
-
return "behavioral";
|
|
348
|
+
return result("behavioral");
|
|
294
349
|
}
|
|
295
350
|
const oldScripts = scriptRecord(oldJson.scripts);
|
|
296
351
|
const newScripts = scriptRecord(newJson.scripts);
|
|
297
|
-
if (!oldScripts || !newScripts) return "behavioral";
|
|
352
|
+
if (!oldScripts || !newScripts) return result("behavioral");
|
|
298
353
|
const closure = new Set([...testScriptClosure(oldScripts), ...testScriptClosure(newScripts)]);
|
|
354
|
+
const added = new Set<string>();
|
|
355
|
+
const dependencies = new Set<string>();
|
|
299
356
|
for (const field of new Set([...Object.keys(oldJson), ...Object.keys(newJson)])) {
|
|
300
357
|
if (PACKAGE_JSON_METADATA_FIELDS.has(field) || PACKAGE_JSON_DATA_FIELDS.has(field)) continue;
|
|
301
358
|
if (JSON.stringify(oldJson[field]) === JSON.stringify(newJson[field])) continue;
|
|
302
|
-
if (field
|
|
359
|
+
if (DEPENDENCY_FIELDS.has(field)) {
|
|
360
|
+
const before = scriptRecord(oldJson[field]);
|
|
361
|
+
const after = scriptRecord(newJson[field]);
|
|
362
|
+
if (!before || !after) return result("behavioral");
|
|
363
|
+
for (const name of new Set([...Object.keys(before), ...Object.keys(after)])) if (before[name] !== after[name]) dependencies.add(name);
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (field !== "scripts") return result("behavioral");
|
|
303
367
|
for (const name of new Set([...Object.keys(oldScripts), ...Object.keys(newScripts)])) {
|
|
304
|
-
if (oldScripts[name]
|
|
368
|
+
if (oldScripts[name] === newScripts[name] || !closure.has(name)) continue;
|
|
369
|
+
const edit = oldScripts[name] !== undefined && newScripts[name] !== undefined ? testListEdit(oldScripts[name]!, newScripts[name]!) : undefined;
|
|
370
|
+
if (!edit) return result("behavioral");
|
|
371
|
+
for (const path of edit.added) added.add(path);
|
|
305
372
|
}
|
|
306
373
|
}
|
|
307
|
-
return "data";
|
|
374
|
+
return result(dependencies.size > 0 ? "dependencies" : "data", [...added].sort(), [...dependencies].sort());
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** packageJsonChange's kind, with a dependency change reported as behavioral. */
|
|
378
|
+
export function packageJsonChangeKind(oldContent: string | undefined, newContent: string | undefined): "metadata" | "data" | "behavioral" {
|
|
379
|
+
const { kind } = packageJsonChange(oldContent, newContent);
|
|
380
|
+
return kind === "dependencies" ? "behavioral" : kind;
|
|
308
381
|
}
|
|
309
382
|
|
|
310
383
|
/** Reads a file's content at a specific commit via `git show <sha>:<path>`, without touching the
|
|
@@ -488,15 +561,47 @@ export async function analyzeGitDelta(
|
|
|
488
561
|
// hasRelevantPackageJsonChange()'s own doc comment) and only ever narrows true -> false.
|
|
489
562
|
const narrowed = new Set<ChangedFile>();
|
|
490
563
|
const referenceResolvedPaths: string[] = [];
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
564
|
+
const changedPackageJson = filesWithBinary.find((f) => f.path === "package.json" && f.changeType === "modified");
|
|
565
|
+
const oldPackageJson = readFileAtSha(baseSha, "package.json", repoPath);
|
|
566
|
+
const newPackageJson = readFileAtSha(headSha, "package.json", repoPath);
|
|
567
|
+
const packageChange = changedPackageJson ? packageJsonChange(oldPackageJson, newPackageJson) : undefined;
|
|
568
|
+
if (analysis.dependencyManifestChanged && changedPackageJson && packageChange) {
|
|
569
|
+
analysis.dependencyManifestChanged = packageChange.kind === "behavioral" || packageChange.kind === "dependencies";
|
|
570
|
+
if (packageChange.kind === "metadata" || packageChange.kind === "data") narrowed.add(changedPackageJson);
|
|
571
|
+
if (packageChange.kind === "data") referenceResolvedPaths.push("package.json");
|
|
572
|
+
if (packageChange.kind === "data" && packageChange.testScriptAddedPaths.length > 0) analysis.testScriptAddedPaths = packageChange.testScriptAddedPaths;
|
|
573
|
+
}
|
|
574
|
+
// 2026-10-28: a dependency change is narrowed to the direct dependencies whose installed tree the
|
|
575
|
+
// npm lockfile shows changed (src/repo/npm-lockfile.ts); ImpactAnalyzer selects the tests that
|
|
576
|
+
// import them, or falls back. Both files must agree: a manifest dependency change without a
|
|
577
|
+
// matching lockfile change stays global.
|
|
578
|
+
const changedLockfile = filesWithBinary.find((f) => f.path === "package-lock.json" && f.changeType === "modified");
|
|
579
|
+
const dependencyEdit = packageChange?.kind === "dependencies" || (!changedPackageJson && analysis.lockfileChanged);
|
|
580
|
+
const onlyNpmLock = (analysis.signalPaths?.lockfile ?? []).every((p) => p === "package-lock.json");
|
|
581
|
+
if (dependencyEdit && changedLockfile && onlyNpmLock) {
|
|
582
|
+
const manifests = [oldPackageJson, newPackageJson].map((content) => { try { return JSON.parse(content ?? "") as Record<string, unknown>; } catch { return undefined; } });
|
|
583
|
+
const direct = new Set<string>();
|
|
584
|
+
let modelled = manifests.every((manifest) => manifest && manifest.workspaces === undefined);
|
|
585
|
+
for (const manifest of manifests) {
|
|
586
|
+
for (const field of ["dependencies", "devDependencies", "optionalDependencies"]) {
|
|
587
|
+
const record = scriptRecord(manifest?.[field]);
|
|
588
|
+
if (!record) { modelled = false; continue; }
|
|
589
|
+
for (const name of Object.keys(record)) direct.add(name);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
const affected = modelled
|
|
593
|
+
? npmLockAffectedDependencies(readFileAtSha(baseSha, "package-lock.json", repoPath), readFileAtSha(headSha, "package-lock.json", repoPath), [...direct])
|
|
594
|
+
: undefined;
|
|
595
|
+
if (affected && (packageChange?.changedDependencies ?? []).every((name) => affected.includes(name))) {
|
|
596
|
+
analysis.dependencyManifestChanged = false;
|
|
597
|
+
analysis.lockfileChanged = false;
|
|
598
|
+
analysis.changedDependencies = affected;
|
|
599
|
+
narrowed.add(changedLockfile);
|
|
600
|
+
if (changedPackageJson) {
|
|
601
|
+
narrowed.add(changedPackageJson);
|
|
602
|
+
referenceResolvedPaths.push("package.json");
|
|
603
|
+
if (packageChange!.testScriptAddedPaths.length > 0) analysis.testScriptAddedPaths = packageChange!.testScriptAddedPaths;
|
|
604
|
+
}
|
|
500
605
|
}
|
|
501
606
|
}
|
|
502
607
|
// 2026-10-21: a workflow that cannot run tests (deploy, publish, scan) cannot change a test
|
|
@@ -522,7 +627,8 @@ export async function analyzeGitDelta(
|
|
|
522
627
|
analysis.configChanged = remaining.configChanged;
|
|
523
628
|
analysis.workflowChanged = remaining.workflowChanged;
|
|
524
629
|
analysis.signalPaths = { ...analysis.signalPaths, config: remaining.signalPaths?.config, workflow: remaining.signalPaths?.workflow,
|
|
525
|
-
...(analysis.dependencyManifestChanged ? {} : { dependencyManifest: [] })
|
|
630
|
+
...(analysis.dependencyManifestChanged ? {} : { dependencyManifest: [] }),
|
|
631
|
+
...(analysis.lockfileChanged ? {} : { lockfile: [] }) };
|
|
526
632
|
}
|
|
527
633
|
if (referenceResolvedPaths.length > 0) analysis.referenceResolvedPaths = referenceResolvedPaths.sort();
|
|
528
634
|
|
|
@@ -44,6 +44,13 @@ export interface GitDeltaAnalysis {
|
|
|
44
44
|
* did not set a global flag above. Tests may still read them, so ImpactAnalyzer resolves their
|
|
45
45
|
* readers by reference and falls back to full validation when it cannot (2026-10-21). */
|
|
46
46
|
referenceResolvedPaths?: string[];
|
|
47
|
+
/** Test files a package.json test-list edit added to the test script (2026-10-28). They are part of
|
|
48
|
+
* the suite from this change on, so ImpactAnalyzer selects them. */
|
|
49
|
+
testScriptAddedPaths?: string[];
|
|
50
|
+
/** Direct dependencies whose installed tree changed, when a package.json/package-lock.json change was
|
|
51
|
+
* narrowed to them (src/repo/npm-lockfile.ts, 2026-10-28). ImpactAnalyzer selects the tests that
|
|
52
|
+
* import each one, and falls back when it cannot. Absent when no such narrowing happened. */
|
|
53
|
+
changedDependencies?: string[];
|
|
47
54
|
/** The changed paths behind each flag above, so a fallback reason can name its files (2026-10-21:
|
|
48
55
|
* "Configuration file(s) changed" was the most frequent reason in both telemetry cohorts and never
|
|
49
56
|
* said which file). Absent on analyses produced before this field existed. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// SPDX-License-Identifier:
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
2
|
export { analyzeGitDelta, EMPTY_TREE_SHA } from "./git/git-diff.js";
|
|
3
3
|
export { analyzeRepository } from "./repo/analyzer.js";
|
|
4
4
|
export { buildDependencyGraph, graphToJson } from "./repo/graph.js";
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
/**
|
|
3
|
+
* Narrowing gate checks to a change (2026-10-29, docs/design/pipeline-impact.md, phase 1 slice 2).
|
|
4
|
+
*
|
|
5
|
+
* A per-file linter or formatter (`eslint "src/**\/*.ts"`, `prettier --check .`) judges each file on its
|
|
6
|
+
* own, so after a change it only needs the changed files it would have covered. When none of them is
|
|
7
|
+
* covered, it can be skipped.
|
|
8
|
+
*
|
|
9
|
+
* THE RULES, each failing toward running more:
|
|
10
|
+
*
|
|
11
|
+
* - Only `eslint` and `prettier --check` leaves whose every flag is known are touched. Anything else
|
|
12
|
+
* keeps its decision.
|
|
13
|
+
* - A change to the tool's configuration, an ignore file, a file the configuration names by a relative
|
|
14
|
+
* path, `package.json` or a lockfile runs the whole check.
|
|
15
|
+
* - ESLint rules that read other files make lint whole-program: type-aware parsing (`project`,
|
|
16
|
+
* `projectService`, `*TypeChecked` configs), import-plugin rules, boundary plugins, and any nested
|
|
17
|
+
* ESLint configuration (only the root one is read). Whole-program lint runs as written whenever a code
|
|
18
|
+
* file changed and is skipped only when none did. Lint that is only type-aware is skipped when no
|
|
19
|
+
* changed file is in the TypeScript program or the lint target (type-program.ts, slice 4).
|
|
20
|
+
* - `tsc` runs as written unless no changed file is in the project it checks (slice 4).
|
|
21
|
+
* - A deleted code file can break an import in a file that remains: ESLint then runs as written.
|
|
22
|
+
* - `--max-warnings` with a narrowed file list could fail on "file ignored" warnings for files the full
|
|
23
|
+
* run never lints, so it is never narrowed; it can still be skipped.
|
|
24
|
+
* - Paths that need shell quoting, or more than MAX_FILES files, run the whole check.
|
|
25
|
+
*
|
|
26
|
+
* RESIDUAL RISK. A shareable config that enables cross-file rules without any of the names above (a
|
|
27
|
+
* company preset wrapping eslint-plugin-import) is treated as per-file. Required CI stays authoritative.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { posix } from "node:path";
|
|
31
|
+
import type { GateCheck } from "./pipeline-steps.js";
|
|
32
|
+
import { decideScriptCheck } from "./script-reads.js";
|
|
33
|
+
import { programMembership, tscProject, type RootTextCache } from "./type-program.js";
|
|
34
|
+
|
|
35
|
+
export interface ChangedPath {
|
|
36
|
+
path: string;
|
|
37
|
+
status: "added" | "modified" | "deleted";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface NarrowingInput {
|
|
41
|
+
scripts: Readonly<Record<string, string>>;
|
|
42
|
+
changes: readonly ChangedPath[];
|
|
43
|
+
/** Repository-relative read of the checked-out content; undefined when absent. */
|
|
44
|
+
read: (path: string) => string | undefined;
|
|
45
|
+
/** Every tracked path, for finding nested configuration. Without it nested configs are not looked for. */
|
|
46
|
+
files?: readonly string[];
|
|
47
|
+
/** As in GateDiscoveryOptions: whether `npm run x` also runs `prex`/`postx`. Default true. */
|
|
48
|
+
lifecycleHooks?: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const MAX_FILES = 100;
|
|
52
|
+
const MAX_COMMAND = 6000;
|
|
53
|
+
const SAFE_PATH = /^[\w./@+-]+$/;
|
|
54
|
+
const CODE = /\.(?:[cm]?[jt]sx?|vue|svelte|astro|json5?|d\.ts)$/;
|
|
55
|
+
const LOCKFILE = /^(?:package-lock\.json|npm-shrinkwrap\.json|pnpm-lock\.yaml|yarn\.lock|bun\.lockb?)$/;
|
|
56
|
+
|
|
57
|
+
const ESLINT_CONFIG = /^(?:eslint\.config\.[cm]?[jt]s|\.eslintrc(?:\.(?:js|cjs|json|ya?ml))?|\.eslintignore)$/;
|
|
58
|
+
const PRETTIER_CONFIG = /^(?:\.prettierrc(?:\.(?:json5?|ya?ml|toml|[cm]?[jt]s))?|prettier\.config\.[cm]?[jt]s|\.prettierignore|\.editorconfig)$/;
|
|
59
|
+
const ESLINT_EXTENSIONS = ["js", "mjs", "cjs", "jsx", "ts", "tsx", "mts", "cts"];
|
|
60
|
+
const PRETTIER_EXTENSIONS = [...ESLINT_EXTENSIONS, "json", "json5", "css", "scss", "less", "md", "mdx", "yaml", "yml", "html", "vue", "graphql", "gql", "hbs", "handlebars"];
|
|
61
|
+
/** Configuration text that turns on rules reading files other than the one being linted: type-aware
|
|
62
|
+
* parsing reads the TypeScript program; import and boundary rules read whatever is imported. */
|
|
63
|
+
const TYPE_AWARE = /\bproject(?:Service)?\s*:|TypeChecked|type-checked/;
|
|
64
|
+
const IMPORT_RULES = /\bimport(?:-x)?\/|eslint-plugin-import|import-x|plugin:import|airbnb|boundaries|no-cycle/;
|
|
65
|
+
|
|
66
|
+
interface Tool {
|
|
67
|
+
name: "eslint" | "prettier";
|
|
68
|
+
valueFlags: Set<string>;
|
|
69
|
+
booleanFlags: Set<string>;
|
|
70
|
+
configs: RegExp;
|
|
71
|
+
extensions: string[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const TOOLS: Record<"eslint" | "prettier", Tool> = {
|
|
75
|
+
eslint: {
|
|
76
|
+
name: "eslint",
|
|
77
|
+
valueFlags: new Set(["--cache-strategy", "--cache-location", "--max-warnings", "--ext", "-c", "--config", "-f", "--format", "--ignore-path", "-o", "--output-file"]),
|
|
78
|
+
booleanFlags: new Set(["--cache", "--quiet", "--fix", "--color", "--no-color", "--no-warn-ignored", "--no-error-on-unmatched-pattern", "--report-unused-disable-directives", "--pass-on-no-patterns"]),
|
|
79
|
+
configs: ESLINT_CONFIG,
|
|
80
|
+
extensions: ESLINT_EXTENSIONS,
|
|
81
|
+
},
|
|
82
|
+
prettier: {
|
|
83
|
+
name: "prettier",
|
|
84
|
+
valueFlags: new Set(["--config", "--ignore-path", "--log-level", "--cache-location", "--cache-strategy", "--end-of-line"]),
|
|
85
|
+
booleanFlags: new Set(["--check", "-c", "--list-different", "-l", "--cache", "--ignore-unknown", "-u", "--no-error-on-unmatched-pattern"]),
|
|
86
|
+
configs: PRETTIER_CONFIG,
|
|
87
|
+
extensions: PRETTIER_EXTENSIONS,
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
interface ParsedCommand {
|
|
92
|
+
tool: Tool;
|
|
93
|
+
/** Everything before the targets, as written. */
|
|
94
|
+
prefix: string[];
|
|
95
|
+
targets: string[];
|
|
96
|
+
values: Map<string, string>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function words(command: string): string[] | undefined {
|
|
100
|
+
const out: string[] = [];
|
|
101
|
+
for (const match of command.trim().matchAll(/"([^"]*)"|'([^']*)'|(\S+)/g)) {
|
|
102
|
+
if (match[3] !== undefined && /["']/.test(match[3])) return undefined;
|
|
103
|
+
out.push(match[0]);
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const unquote = (word: string): string => word.replace(/^(["'])(.*)\1$/, "$2");
|
|
109
|
+
|
|
110
|
+
function parseCommand(command: string): ParsedCommand | undefined {
|
|
111
|
+
const all = words(command);
|
|
112
|
+
if (!all) return undefined;
|
|
113
|
+
let index = 0;
|
|
114
|
+
if (all[index] === "npx") { index++; if (all[index] === "--no-install") index++; }
|
|
115
|
+
const name = all[index];
|
|
116
|
+
if (name !== "eslint" && name !== "prettier") return undefined;
|
|
117
|
+
const tool = TOOLS[name];
|
|
118
|
+
const prefix = all.slice(0, ++index);
|
|
119
|
+
const values = new Map<string, string>();
|
|
120
|
+
const targets: string[] = [];
|
|
121
|
+
for (; index < all.length; index++) {
|
|
122
|
+
const word = all[index]!;
|
|
123
|
+
if (word.startsWith("-")) {
|
|
124
|
+
if (targets.length) return undefined;
|
|
125
|
+
const [flag, inline] = word.includes("=") ? [word.slice(0, word.indexOf("=")), word.slice(word.indexOf("=") + 1)] : [word, undefined];
|
|
126
|
+
if (tool.booleanFlags.has(flag)) { prefix.push(word); continue; }
|
|
127
|
+
if (!tool.valueFlags.has(flag)) return undefined;
|
|
128
|
+
const value = inline ?? all[++index];
|
|
129
|
+
if (value === undefined) return undefined;
|
|
130
|
+
values.set(flag, unquote(value));
|
|
131
|
+
prefix.push(...(inline === undefined ? [word, value] : [word]));
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
targets.push(unquote(word));
|
|
135
|
+
}
|
|
136
|
+
if (targets.length === 0 || targets.some((target) => target.startsWith("!"))) return undefined;
|
|
137
|
+
if (tool.name === "prettier" && !["--check", "-c", "--list-different", "-l"].some((flag) => prefix.includes(flag))) return undefined;
|
|
138
|
+
return { tool, prefix, targets, values };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function expandBraces(pattern: string): string[] {
|
|
142
|
+
const match = /\{([^{}]*)\}/.exec(pattern);
|
|
143
|
+
if (!match) return [pattern];
|
|
144
|
+
return match[1]!.split(",").flatMap((alternative) => expandBraces(pattern.slice(0, match.index) + alternative + pattern.slice(match.index + match[0].length)));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function globMatches(pattern: string, path: string): boolean {
|
|
148
|
+
return expandBraces(pattern.replace(/^\.\//, "")).some((p) => {
|
|
149
|
+
const source = p.replace(/[.+^$()|[\]\\]/g, "\\$&")
|
|
150
|
+
.replace(/\*\*\//g, "\0A").replace(/\*\*/g, "\0B").replace(/\*/g, "[^/]*").replace(/\?/g, "[^/]")
|
|
151
|
+
.replace(/\0A/g, "(?:.*/)?").replace(/\0B/g, ".*");
|
|
152
|
+
return new RegExp(`^${source}$`).test(path);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function covers(target: string, path: string, extensions: readonly string[]): boolean {
|
|
157
|
+
if (/[*?{[]/.test(target)) return globMatches(target, path);
|
|
158
|
+
const normalized = posix.normalize(target).replace(/\/$/, "");
|
|
159
|
+
if (normalized === path) return true;
|
|
160
|
+
const inside = normalized === "." || path.startsWith(`${normalized}/`);
|
|
161
|
+
return inside && extensions.includes(posix.extname(path).slice(1));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Root configuration files of the tool that exist, and the paths each one names relatively. */
|
|
165
|
+
function configReferences(tool: Tool, input: NarrowingInput, explicit: string | undefined): { texts: string[]; referenced: string[] } {
|
|
166
|
+
const candidates = explicit ? [posix.normalize(explicit)] : tool.name === "eslint"
|
|
167
|
+
? ["eslint.config.js", "eslint.config.mjs", "eslint.config.cjs", "eslint.config.ts", "eslint.config.mts", "eslint.config.cts", ".eslintrc.js", ".eslintrc.cjs", ".eslintrc.json", ".eslintrc.yaml", ".eslintrc.yml", ".eslintrc"]
|
|
168
|
+
: [];
|
|
169
|
+
const texts: string[] = [];
|
|
170
|
+
const referenced: string[] = [];
|
|
171
|
+
for (const path of candidates) {
|
|
172
|
+
const text = input.read(path);
|
|
173
|
+
if (text === undefined) continue;
|
|
174
|
+
texts.push(text);
|
|
175
|
+
for (const match of text.matchAll(/["'`](\.{1,2}\/[^"'`\s]+)["'`]/g)) {
|
|
176
|
+
const ref = posix.normalize(posix.join(posix.dirname(path), match[1]!));
|
|
177
|
+
// A local plugin's entry file imports its siblings, so its whole directory counts.
|
|
178
|
+
referenced.push(ref, ...(posix.extname(ref) && posix.dirname(ref) !== "." ? [posix.dirname(ref)] : []));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (tool.name === "eslint") {
|
|
182
|
+
const manifest = input.read("package.json");
|
|
183
|
+
try {
|
|
184
|
+
const embedded = manifest === undefined ? undefined : (JSON.parse(manifest) as { eslintConfig?: unknown }).eslintConfig;
|
|
185
|
+
if (embedded !== undefined) texts.push(JSON.stringify(embedded));
|
|
186
|
+
} catch { /* package.json changes already run the whole check */ }
|
|
187
|
+
}
|
|
188
|
+
return { texts, referenced };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function hasHooks(check: GateCheck, input: NarrowingInput): boolean {
|
|
192
|
+
return check.runsScript && input.lifecycleHooks !== false
|
|
193
|
+
&& (input.scripts[`pre${check.script}`] !== undefined || input.scripts[`post${check.script}`] !== undefined);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** `tsc` checks a whole program: it runs as written unless no changed file is in that program. */
|
|
197
|
+
function decideTypecheck(check: GateCheck, input: NarrowingInput, cache: RootTextCache): GateCheck {
|
|
198
|
+
if (!input.files || hasHooks(check, input)) return check;
|
|
199
|
+
const project = tscProject(check.command);
|
|
200
|
+
if (!project) return check;
|
|
201
|
+
const membership = programMembership(project, input, cache);
|
|
202
|
+
return { ...check, decision: membership.inProgram ? "RUN" : "SKIP", reason: membership.reason };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function decide(check: GateCheck, input: NarrowingInput, cache: RootTextCache): GateCheck {
|
|
206
|
+
if (check.kind !== "lint" && check.kind !== "format") return check;
|
|
207
|
+
if (hasHooks(check, input)) return check;
|
|
208
|
+
const parsed = parseCommand(check.command);
|
|
209
|
+
if (!parsed) return check;
|
|
210
|
+
const { tool, prefix, targets, values } = parsed;
|
|
211
|
+
const run = (reason: string): GateCheck => ({ ...check, decision: "RUN", reason });
|
|
212
|
+
const skip = (reason: string): GateCheck => ({ ...check, decision: "SKIP", reason });
|
|
213
|
+
|
|
214
|
+
const explicitConfig = values.get("-c") ?? values.get("--config");
|
|
215
|
+
const ignorePath = values.get("--ignore-path");
|
|
216
|
+
const { texts, referenced } = configReferences(tool, input, explicitConfig);
|
|
217
|
+
const configChange = input.changes.find(({ path }) => {
|
|
218
|
+
const base = posix.basename(path);
|
|
219
|
+
return tool.configs.test(base) || LOCKFILE.test(base) || path === "package.json"
|
|
220
|
+
|| path === explicitConfig || path === ignorePath || (tool.name === "eslint" && base === ".gitignore")
|
|
221
|
+
|| referenced.some((ref) => path === ref || path.startsWith(`${ref}/`) || path.startsWith(`${ref}.`));
|
|
222
|
+
});
|
|
223
|
+
if (configChange) return run(`${configChange.path} changed: runs the whole ${tool.name} check`);
|
|
224
|
+
|
|
225
|
+
const extensions = tool.name === "eslint" && values.has("--ext")
|
|
226
|
+
? [...ESLINT_EXTENSIONS, ...values.get("--ext")!.split(",").map((ext) => ext.replace(/^\./, ""))]
|
|
227
|
+
: tool.extensions;
|
|
228
|
+
if (tool.name === "eslint") {
|
|
229
|
+
const nested = input.files?.find((path) => path.includes("/") && ESLINT_CONFIG.test(posix.basename(path)) && !posix.basename(path).endsWith("ignore"));
|
|
230
|
+
const importRules = texts.some((text) => IMPORT_RULES.test(text));
|
|
231
|
+
const typeAware = texts.some((text) => TYPE_AWARE.test(text));
|
|
232
|
+
if (!nested && texts.length > 0 && !importRules && typeAware && input.files) {
|
|
233
|
+
// Type-aware rules read the TypeScript program (slice 4): outside it, and outside the lint target,
|
|
234
|
+
// a change cannot alter the result. `project: "x.json"` names the program; otherwise the root one.
|
|
235
|
+
const named = [...new Set(texts.flatMap((text) => [...text.matchAll(/\bproject\s*:\s*["']([^"']+\.json)["']/g)].map((m) => posix.normalize(m[1]!))))];
|
|
236
|
+
const membership = named.length > 1 ? undefined : programMembership(named[0] ?? "tsconfig.json", input, cache);
|
|
237
|
+
const linted = input.changes.find(({ path, status }) => status !== "deleted" && targets.some((target) => covers(target, path, extensions)));
|
|
238
|
+
if (membership && !membership.inProgram && !linted) return skip(`type-aware ESLint rules, but ${membership.reason}`);
|
|
239
|
+
return run(`type-aware ESLint rules lint the whole target: ${membership?.reason ?? "several projects are named"}`);
|
|
240
|
+
}
|
|
241
|
+
const crossFile = nested !== undefined || texts.length === 0 || importRules || typeAware;
|
|
242
|
+
if (crossFile) {
|
|
243
|
+
const why = nested ? `nested ESLint config ${nested}` : texts.length === 0 ? "no root ESLint config found" : "type-aware or cross-file ESLint rules";
|
|
244
|
+
const code = input.changes.find(({ path }) => CODE.test(path));
|
|
245
|
+
return code ? run(`${why}: lints the whole target because ${code.path} changed`) : skip(`${why}, but no code file changed`);
|
|
246
|
+
}
|
|
247
|
+
const deleted = input.changes.find(({ path, status }) => status === "deleted" && CODE.test(path));
|
|
248
|
+
if (deleted) return run(`${deleted.path} was deleted: an import of it may break in a file that remains`);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const files = input.changes.filter(({ path, status }) => status !== "deleted" && targets.some((target) => covers(target, path, extensions))).map(({ path }) => path);
|
|
252
|
+
if (files.length === 0) return skip(`no changed file is in ${targets.join(" ")}`);
|
|
253
|
+
if (tool.name === "eslint" && values.has("--max-warnings")) return run("--max-warnings: a narrowed file list could warn about files the full run ignores");
|
|
254
|
+
if (files.length > MAX_FILES || files.some((path) => !SAFE_PATH.test(path))) return run(`${files.length} changed file(s) in the target; runs the whole check`);
|
|
255
|
+
const command = [...prefix, ...files].join(" ");
|
|
256
|
+
if (command.length > MAX_COMMAND) return run("the narrowed command would be too long; runs the whole check");
|
|
257
|
+
return { ...check, runsScript: false, command, decision: "NARROWED", reason: `${files.length} changed file(s) in ${targets.join(" ")}` };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Narrows or skips each lint and format check, and runs or skips each repository-script check
|
|
261
|
+
* (script-reads.ts), for this change; other checks are returned unchanged. */
|
|
262
|
+
export function narrowGateChecks(checks: readonly GateCheck[], input: NarrowingInput): GateCheck[] {
|
|
263
|
+
// A whole-script leaf's command is the script body, so the same parse applies; a RUN keeps running the
|
|
264
|
+
// script as written.
|
|
265
|
+
const cache: RootTextCache = new Map();
|
|
266
|
+
return checks.map((check) => check.kind === "script" ? decideScriptCheck(check, input)
|
|
267
|
+
: check.kind === "typecheck" ? decideTypecheck(check, input, cache)
|
|
268
|
+
: decide(check, input, cache));
|
|
269
|
+
}
|