@diffci.com/diffci 0.3.38 → 0.3.40

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.
Files changed (64) hide show
  1. package/README.md +3 -3
  2. package/dist-client/src/client/agent-setup.d.ts +1 -1
  3. package/dist-client/src/client/agent-setup.js +1 -1
  4. package/node_modules/@diffci.com/core/README.md +15 -4
  5. package/node_modules/@diffci.com/core/dist/cache/economics-context.d.ts +4 -1
  6. package/node_modules/@diffci.com/core/dist/cache/economics-context.js +7 -3
  7. package/node_modules/@diffci.com/core/dist/cache/economics-context.js.map +1 -1
  8. package/node_modules/@diffci.com/core/dist/cache/graph-cache.d.ts +1 -1
  9. package/node_modules/@diffci.com/core/dist/cache/graph-cache.js +5 -1
  10. package/node_modules/@diffci.com/core/dist/cache/graph-cache.js.map +1 -1
  11. package/node_modules/@diffci.com/core/dist/ci-inference/evidence.js +1 -1
  12. package/node_modules/@diffci.com/core/dist/ci-inference/evidence.js.map +1 -1
  13. package/node_modules/@diffci.com/core/dist/git/git-diff.d.ts +4 -0
  14. package/node_modules/@diffci.com/core/dist/git/git-diff.js +20 -7
  15. package/node_modules/@diffci.com/core/dist/git/git-diff.js.map +1 -1
  16. package/node_modules/@diffci.com/core/dist/repo/adapters/go.js +33 -7
  17. package/node_modules/@diffci.com/core/dist/repo/adapters/go.js.map +1 -1
  18. package/node_modules/@diffci.com/core/dist/repo/adapters/types.d.ts +8 -0
  19. package/node_modules/@diffci.com/core/dist/repo/adapters/types.js.map +1 -1
  20. package/node_modules/@diffci.com/core/dist/repo/analyzer.js +16 -1
  21. package/node_modules/@diffci.com/core/dist/repo/analyzer.js.map +1 -1
  22. package/node_modules/@diffci.com/core/dist/repo/export-reach.d.ts +16 -0
  23. package/node_modules/@diffci.com/core/dist/repo/export-reach.js +113 -0
  24. package/node_modules/@diffci.com/core/dist/repo/export-reach.js.map +1 -0
  25. package/node_modules/@diffci.com/core/dist/repo/file-references.js +2 -1
  26. package/node_modules/@diffci.com/core/dist/repo/file-references.js.map +1 -1
  27. package/node_modules/@diffci.com/core/dist/repo/graph.d.ts +5 -0
  28. package/node_modules/@diffci.com/core/dist/repo/graph.js +512 -41
  29. package/node_modules/@diffci.com/core/dist/repo/graph.js.map +1 -1
  30. package/node_modules/@diffci.com/core/dist/repo/impact-types.d.ts +1 -1
  31. package/node_modules/@diffci.com/core/dist/repo/impact.d.ts +51 -0
  32. package/node_modules/@diffci.com/core/dist/repo/impact.js +296 -9
  33. package/node_modules/@diffci.com/core/dist/repo/impact.js.map +1 -1
  34. package/node_modules/@diffci.com/core/dist/repo/lockfiles.d.ts +5 -0
  35. package/node_modules/@diffci.com/core/dist/repo/lockfiles.js +12 -0
  36. package/node_modules/@diffci.com/core/dist/repo/lockfiles.js.map +1 -1
  37. package/node_modules/@diffci.com/core/dist/repo/repo-config.d.ts +5 -0
  38. package/node_modules/@diffci.com/core/dist/repo/repo-config.js +3 -1
  39. package/node_modules/@diffci.com/core/dist/repo/repo-config.js.map +1 -1
  40. package/node_modules/@diffci.com/core/dist/repo/test-discovery.js +134 -3
  41. package/node_modules/@diffci.com/core/dist/repo/test-discovery.js.map +1 -1
  42. package/node_modules/@diffci.com/core/dist/repo/types.d.ts +35 -1
  43. package/node_modules/@diffci.com/core/dist/repo/workspace-sources.d.ts +35 -0
  44. package/node_modules/@diffci.com/core/dist/repo/workspace-sources.js +292 -0
  45. package/node_modules/@diffci.com/core/dist/repo/workspace-sources.js.map +1 -0
  46. package/node_modules/@diffci.com/core/package.json +1 -1
  47. package/node_modules/@diffci.com/core/src/cache/economics-context.ts +7 -3
  48. package/node_modules/@diffci.com/core/src/cache/graph-cache.ts +5 -1
  49. package/node_modules/@diffci.com/core/src/ci-inference/evidence.ts +1 -1
  50. package/node_modules/@diffci.com/core/src/git/git-diff.ts +22 -7
  51. package/node_modules/@diffci.com/core/src/repo/adapters/go.ts +32 -7
  52. package/node_modules/@diffci.com/core/src/repo/adapters/types.ts +5 -0
  53. package/node_modules/@diffci.com/core/src/repo/analyzer.ts +15 -1
  54. package/node_modules/@diffci.com/core/src/repo/export-reach.ts +92 -0
  55. package/node_modules/@diffci.com/core/src/repo/file-references.ts +2 -1
  56. package/node_modules/@diffci.com/core/src/repo/graph.ts +439 -37
  57. package/node_modules/@diffci.com/core/src/repo/impact-types.ts +2 -0
  58. package/node_modules/@diffci.com/core/src/repo/impact.ts +267 -9
  59. package/node_modules/@diffci.com/core/src/repo/lockfiles.ts +13 -0
  60. package/node_modules/@diffci.com/core/src/repo/repo-config.ts +8 -1
  61. package/node_modules/@diffci.com/core/src/repo/test-discovery.ts +107 -3
  62. package/node_modules/@diffci.com/core/src/repo/types.ts +32 -1
  63. package/node_modules/@diffci.com/core/src/repo/workspace-sources.ts +273 -0
  64. package/package.json +3 -3
@@ -0,0 +1,273 @@
1
+ // SPDX-License-Identifier: MIT
2
+ /**
3
+ * Maps an import of a declared workspace package to that package's source files.
4
+ *
5
+ * Workspace packages commonly point `types`, `main` and `exports` at build output (2026-11-09, Sentry's
6
+ * JavaScript SDK: `@sentry/server-utils` exports `./build/types/index.d.ts`). Before a build those files
7
+ * do not exist and the import resolves nowhere; after `build:types` it resolves to a generated declaration
8
+ * file that no source change ever touches. Either way the edge into the package's `src/` is lost, and a
9
+ * selective plan would skip every downstream package.
10
+ *
11
+ * The mapping is exact or absent. A declaration entry maps to source only through a tsconfig in the
12
+ * package root that states both an output directory (`declarationDir` or `outDir`) containing the entry and
13
+ * a `rootDir`; `outDir/x.d.ts` becomes `rootDir/x.ts` (or `.tsx`, `.mts`, `.cts`, `.js`, `.jsx`) when that file
14
+ * exists. An entry that is itself a TypeScript source file is used as is. Anything else is unmapped, and the
15
+ * caller must fall back rather than guess.
16
+ */
17
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
18
+ import { join, posix } from "node:path";
19
+
20
+ import ts from "typescript";
21
+
22
+ export interface WorkspacePackage {
23
+ name: string;
24
+ /** Repository-relative directory; never the repository root. */
25
+ root: string;
26
+ }
27
+
28
+ /** One tsconfig's emit directories, repository-relative and inside the package. */
29
+ interface OutputDirectories {
30
+ declarationDir: string | undefined;
31
+ outDir: string | undefined;
32
+ rootDir: string;
33
+ }
34
+
35
+ const DECLARATION = /\.d\.([mc]?)ts$/;
36
+ const SOURCE = /\.([mc]?tsx?|[mc]?jsx?)$/;
37
+ const EMITTED = /\.([mc]?)jsx?$/;
38
+
39
+ function toPosix(path: string): string {
40
+ return path.replace(/\\/g, "/");
41
+ }
42
+
43
+ export class WorkspaceSources {
44
+ private readonly packages = new Map<string, WorkspacePackage>();
45
+ private readonly manifests = new Map<string, Record<string, unknown> | undefined>();
46
+ private readonly outputs = new Map<string, OutputDirectories[]>();
47
+
48
+ constructor(private readonly repoPath: string, workspaceRoots: Iterable<string>) {
49
+ for (const root of workspaceRoots) {
50
+ if (!root) continue;
51
+ const name = this.manifest(root)?.name;
52
+ if (typeof name === "string" && name && !this.packages.has(name)) this.packages.set(name, { name, root });
53
+ }
54
+ }
55
+
56
+ get size(): number {
57
+ return this.packages.size;
58
+ }
59
+
60
+ /** The workspace package whose directory holds a repository-relative path (the deepest one). */
61
+ packageContaining(path: string): WorkspacePackage | undefined {
62
+ let found: WorkspacePackage | undefined;
63
+ for (const pkg of this.packages.values()) {
64
+ if (path.startsWith(`${pkg.root}/`) && (!found || pkg.root.length > found.root.length)) found = pkg;
65
+ }
66
+ return found;
67
+ }
68
+
69
+ /** The workspace package a bare specifier names, and the export subpath (`.` or `./x`). */
70
+ packageFor(specifier: string): { pkg: WorkspacePackage; subpath: string } | undefined {
71
+ const parts = specifier.split("/");
72
+ const nameParts = specifier.startsWith("@") ? 2 : 1;
73
+ if (parts.length < nameParts || parts.slice(0, nameParts).some((part) => !part)) return undefined;
74
+ const pkg = this.packages.get(parts.slice(0, nameParts).join("/"));
75
+ if (!pkg) return undefined;
76
+ const rest = parts.slice(nameParts).join("/");
77
+ return { pkg, subpath: rest ? `./${rest}` : "." };
78
+ }
79
+
80
+ /** Repository-relative source file for an import of `subpath`, from the package manifest. */
81
+ sourceForSubpath(pkg: WorkspacePackage, subpath: string): string | undefined {
82
+ const manifest = this.manifest(pkg.root);
83
+ if (!manifest) return undefined;
84
+ const entry = entryTarget(manifest, subpath);
85
+ if (entry === undefined && subpath !== ".") {
86
+ // A deep import of a file path the manifest does not export, such as Sentry's
87
+ // `@sentry/node/build/types/types` in a type import: map that file itself.
88
+ const file = posix.normalize(posix.join(pkg.root, subpath));
89
+ if (!file.startsWith(`${pkg.root}/`)) return undefined;
90
+ for (const candidate of [file, `${file}.d.ts`, `${file}/index.d.ts`, `${file}.ts`, `${file}/index.ts`]) {
91
+ const source = this.toSource(pkg, candidate);
92
+ if (source) return source;
93
+ }
94
+ return undefined;
95
+ }
96
+ if (!entry || entry.startsWith("/") || /^[A-Za-z]:/.test(entry)) return undefined;
97
+ const target = posix.normalize(posix.join(pkg.root, entry));
98
+ if (target !== pkg.root && !target.startsWith(`${pkg.root}/`)) return undefined;
99
+ return this.toSource(pkg, target);
100
+ }
101
+
102
+ /** Repository-relative non-code file (a stylesheet, JSON, an image) an `exports` entry names for `subpath`,
103
+ * such as payloadcms/payload's `"./css/app.css": "./src/css/app.css"` in `@payloadcms/ui`. */
104
+ assetForSubpath(pkg: WorkspacePackage, subpath: string): string | undefined {
105
+ const manifest = this.manifest(pkg.root);
106
+ const node = manifest?.exports === undefined ? undefined : exportNode(manifest.exports, subpath);
107
+ if (!node) return undefined;
108
+ for (const leaf of exportLeaves(node.entry, node.star)) {
109
+ if (leaf.startsWith("/") || /^[A-Za-z]:/.test(leaf) || SOURCE.test(leaf) || DECLARATION.test(leaf)) continue;
110
+ const target = posix.normalize(posix.join(pkg.root, leaf));
111
+ if (target.startsWith(`${pkg.root}/`) && existsSync(join(this.repoPath, target))) return target;
112
+ }
113
+ return undefined;
114
+ }
115
+
116
+ /** Whether an import of `subpath` names anything: a manifest entry, or a file in the package. An import
117
+ * that names nothing fails whether the package is installed or not. */
118
+ names(pkg: WorkspacePackage, subpath: string): boolean {
119
+ const manifest = this.manifest(pkg.root);
120
+ if (!manifest) return true;
121
+ if (manifest.exports === undefined ? subpath === "." : exportNode(manifest.exports, subpath)) return true;
122
+ if (subpath === ".") return true;
123
+ const file = posix.normalize(posix.join(pkg.root, subpath));
124
+ return [file, `${file}.d.ts`, `${file}.ts`, `${file}.tsx`, `${file}.js`, `${file}/index.d.ts`, `${file}/index.ts`, `${file}/index.js`]
125
+ .some((candidate) => existsSync(join(this.repoPath, candidate)));
126
+ }
127
+
128
+ /** Repository-relative source file for a file inside the package that TypeScript resolved to. */
129
+ sourceForFile(pkg: WorkspacePackage, fileRel: string): string | undefined {
130
+ return this.toSource(pkg, fileRel);
131
+ }
132
+
133
+ private toSource(pkg: WorkspacePackage, targetRel: string): string | undefined {
134
+ const declaration = DECLARATION.exec(targetRel);
135
+ const script = declaration ? null : EMITTED.exec(targetRel);
136
+ const outputs = this.outputDirectories(pkg);
137
+ for (const { declarationDir, outDir, rootDir } of outputs) {
138
+ // Declarations live in declarationDir (or outDir); emitted JavaScript only in outDir.
139
+ const directory = declaration ? declarationDir : script ? outDir : undefined;
140
+ if (!directory || !targetRel.startsWith(`${directory}/`)) continue;
141
+ const flavor = (declaration ?? script)![1];
142
+ const stem = targetRel.slice(directory.length + 1).replace(declaration ? DECLARATION : EMITTED, "");
143
+ const extensions = flavor ? [`.${flavor}ts`, `.${flavor}js`] : [".ts", ".tsx", ".js", ".jsx"];
144
+ for (const extension of extensions) {
145
+ const candidate = posix.join(rootDir, stem + extension);
146
+ if (existsSync(join(this.repoPath, candidate))) return candidate;
147
+ }
148
+ }
149
+ if (declaration) return undefined;
150
+ // Build output is never source, even when it exists on disk.
151
+ if (outputs.some(({ declarationDir, outDir }) => [declarationDir, outDir].some((dir) => dir && targetRel.startsWith(`${dir}/`)))) return undefined;
152
+ if (!SOURCE.test(targetRel)) return undefined;
153
+ if (existsSync(join(this.repoPath, targetRel))) return targetRel;
154
+ // A `.js` specifier naming a TypeScript source beside it, as TypeScript resolves it (2026-11-10:
155
+ // payloadcms/payload exports `"types": "./src/exports/build-config.js"`, which exists only as `.ts`).
156
+ if (script) {
157
+ const stem = targetRel.slice(0, -script[0].length);
158
+ const flavor = script[1];
159
+ for (const extension of flavor ? [`.${flavor}ts`] : [".ts", ".tsx"]) {
160
+ if (existsSync(join(this.repoPath, stem + extension))) return stem + extension;
161
+ }
162
+ }
163
+ return undefined;
164
+ }
165
+
166
+ /** Output directories stated by tsconfig*.json files in the package root, with their rootDir. */
167
+ private outputDirectories(pkg: WorkspacePackage): OutputDirectories[] {
168
+ let found = this.outputs.get(pkg.root);
169
+ if (found) return found;
170
+ found = [];
171
+ const directory = join(this.repoPath, pkg.root);
172
+ let names: string[] = [];
173
+ try {
174
+ names = readdirSync(directory).filter((name) => /^tsconfig(\..+)?\.json$/.test(name)).sort();
175
+ } catch { /* an unreadable package states no outputs */ }
176
+ for (const name of names) {
177
+ const file = toPosix(join(directory, name));
178
+ const read = ts.readConfigFile(file, ts.sys.readFile);
179
+ if (read.error) continue;
180
+ const options = ts.parseJsonConfigFileContent(read.config, ts.sys, toPosix(directory), undefined, file).options;
181
+ const rootDir = this.relative(options.rootDir);
182
+ // Every directory must stay inside the package; a rootDir elsewhere would map onto another package's files.
183
+ const within = (path: string | undefined): path is string => path !== undefined && (path === pkg.root || path.startsWith(`${pkg.root}/`));
184
+ if (!within(rootDir)) continue;
185
+ const usable = (path: string | undefined) => (within(path) && path !== rootDir ? path : undefined);
186
+ const outDir = usable(this.relative(options.outDir));
187
+ const declarationDir = usable(this.relative(options.declarationDir ?? options.outDir));
188
+ if (outDir || declarationDir) found.push({ declarationDir, outDir, rootDir });
189
+ }
190
+ this.outputs.set(pkg.root, found);
191
+ return found;
192
+ }
193
+
194
+ private relative(path: string | undefined): string | undefined {
195
+ if (!path) return undefined;
196
+ const root = toPosix(this.repoPath).replace(/\/$/, "");
197
+ const value = posix.normalize(toPosix(path));
198
+ if (value.toLowerCase() === root.toLowerCase()) return "";
199
+ return value.toLowerCase().startsWith(`${root.toLowerCase()}/`) ? value.slice(root.length + 1) : undefined;
200
+ }
201
+
202
+ private manifest(root: string): Record<string, unknown> | undefined {
203
+ if (this.manifests.has(root)) return this.manifests.get(root);
204
+ let manifest: Record<string, unknown> | undefined;
205
+ try {
206
+ const parsed = JSON.parse(readFileSync(join(this.repoPath, root, "package.json"), "utf8"));
207
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) manifest = parsed;
208
+ } catch { /* an unreadable manifest names no package */ }
209
+ this.manifests.set(root, manifest);
210
+ return manifest;
211
+ }
212
+ }
213
+
214
+ /** The entry the manifest gives for `subpath`: the `types` target of its export (or a TypeScript source
215
+ * target when it has none), else the top-level `types`/`typings` for the package root. */
216
+ function entryTarget(manifest: Record<string, unknown>, subpath: string): string | undefined {
217
+ const exports = manifest.exports;
218
+ if (exports === undefined) {
219
+ if (subpath !== ".") return undefined;
220
+ const types = manifest.types ?? manifest.typings;
221
+ return typeof types === "string" ? types : undefined;
222
+ }
223
+ const node = exportNode(exports, subpath);
224
+ return node ? pickEntry(node.entry, node.star) : undefined;
225
+ }
226
+
227
+ /** The `exports` entry for `subpath` and the text its `*` stands for, or undefined when none matches. */
228
+ function exportNode(exports: unknown, subpath: string): { entry: unknown; star: string | undefined } | undefined {
229
+ const isMap = exports !== null && typeof exports === "object" && !Array.isArray(exports);
230
+ const keyed = isMap && Object.keys(exports as object).some((key) => key.startsWith("."));
231
+ if (!keyed) return subpath === "." ? { entry: exports, star: undefined } : undefined;
232
+ const map = exports as Record<string, unknown>;
233
+ if (subpath in map) return { entry: map[subpath], star: undefined };
234
+ // Subpath patterns: the longest matching prefix wins, as in Node.
235
+ let best: { key: string; star: string } | undefined;
236
+ for (const key of Object.keys(map)) {
237
+ const star = key.indexOf("*");
238
+ if (star < 0 || key.indexOf("*", star + 1) >= 0) continue;
239
+ const prefix = key.slice(0, star);
240
+ const suffix = key.slice(star + 1);
241
+ if (subpath.length < prefix.length + suffix.length || !subpath.startsWith(prefix) || !subpath.endsWith(suffix)) continue;
242
+ if (!best || prefix.length > best.key.indexOf("*")) best = { key, star: subpath.slice(prefix.length, subpath.length - suffix.length) };
243
+ }
244
+ return best ? { entry: map[best.key], star: best.star } : undefined;
245
+ }
246
+
247
+ /** Every string target of an `exports` entry, with `*` filled in. */
248
+ function exportLeaves(entry: unknown, star: string | undefined): string[] {
249
+ const leaves: string[] = [];
250
+ const walk = (node: unknown) => {
251
+ if (typeof node === "string") leaves.push(star === undefined ? node : node.split("*").join(star));
252
+ else if (Array.isArray(node)) for (const item of node) walk(item);
253
+ else if (node && typeof node === "object") for (const value of Object.values(node)) walk(value);
254
+ };
255
+ walk(entry);
256
+ return leaves;
257
+ }
258
+
259
+ function pickEntry(entry: unknown, star: string | undefined): string | undefined {
260
+ const fill = (value: string) => (star === undefined ? value : value.split("*").join(star));
261
+ const leaves: Array<{ value: string; types: boolean }> = [];
262
+ const walk = (node: unknown, types: boolean) => {
263
+ if (typeof node === "string") leaves.push({ value: fill(node), types });
264
+ else if (Array.isArray(node)) for (const item of node) walk(item, types);
265
+ else if (node && typeof node === "object") for (const [key, value] of Object.entries(node)) walk(value, types || key === "types");
266
+ };
267
+ walk(entry, false);
268
+ return leaves.find((leaf) => leaf.types)?.value
269
+ ?? leaves.find((leaf) => DECLARATION.test(leaf.value))?.value
270
+ ?? leaves.find((leaf) => /\.[mc]?tsx?$/.test(leaf.value))?.value
271
+ // JavaScript only: mapped back through a tsconfig outDir, or used as is when it is source.
272
+ ?? leaves.find((leaf) => EMITTED.test(leaf.value))?.value;
273
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@diffci.com/diffci",
3
- "version": "0.3.38",
4
- "description": "Fail-closed, change-aware verification for AI coding agents with conservative affected-test selection",
3
+ "version": "0.3.40",
4
+ "description": "Verification for AI coding agents: runs the tests and checks a change affects, with full fallback and a receipt",
5
5
  "keywords": [
6
6
  "ci",
7
7
  "cicd",
@@ -76,7 +76,7 @@
76
76
  "test": "node scripts/run-tests.mjs"
77
77
  },
78
78
  "dependencies": {
79
- "@diffci.com/core": "git+https://github.com/DiffCI/core.git#a50b0ef2d936fc82f0a82110532d1788f150a541",
79
+ "@diffci.com/core": "git+https://github.com/DiffCI/core.git#93f4e6ee55b19fdbb8b59423b8c7386c12840e50",
80
80
  "saxes": "6.0.0",
81
81
  "yaml": "^2.9.0"
82
82
  },