@aryam/fixmap-core 0.8.8 → 0.9.0

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 (71) hide show
  1. package/README.md +20 -2
  2. package/dist/browser.d.ts +10 -2
  3. package/dist/browser.js +7 -1
  4. package/dist/browser.js.map +1 -1
  5. package/dist/cli-quote.d.ts +3 -0
  6. package/dist/cli-quote.js +9 -0
  7. package/dist/cli-quote.js.map +1 -0
  8. package/dist/compare.js +45 -12
  9. package/dist/compare.js.map +1 -1
  10. package/dist/context.d.ts +37 -0
  11. package/dist/context.js +214 -0
  12. package/dist/context.js.map +1 -0
  13. package/dist/exclude.d.ts +2 -0
  14. package/dist/exclude.js +8 -3
  15. package/dist/exclude.js.map +1 -1
  16. package/dist/explain.js +16 -17
  17. package/dist/explain.js.map +1 -1
  18. package/dist/graph.d.ts +18 -0
  19. package/dist/graph.js +65 -0
  20. package/dist/graph.js.map +1 -0
  21. package/dist/grounding.d.ts +3 -1
  22. package/dist/grounding.js +21 -17
  23. package/dist/grounding.js.map +1 -1
  24. package/dist/impact.d.ts +8 -0
  25. package/dist/impact.js +131 -0
  26. package/dist/impact.js.map +1 -0
  27. package/dist/import-graph.d.ts +2 -0
  28. package/dist/import-graph.js +84 -10
  29. package/dist/import-graph.js.map +1 -1
  30. package/dist/index.d.ts +11 -3
  31. package/dist/index.js +9 -2
  32. package/dist/index.js.map +1 -1
  33. package/dist/languages.d.ts +1 -1
  34. package/dist/languages.js +60 -12
  35. package/dist/languages.js.map +1 -1
  36. package/dist/markdown.d.ts +2 -0
  37. package/dist/markdown.js +8 -0
  38. package/dist/markdown.js.map +1 -0
  39. package/dist/paths.d.ts +8 -0
  40. package/dist/paths.js +38 -8
  41. package/dist/paths.js.map +1 -1
  42. package/dist/plan.d.ts +17 -1
  43. package/dist/plan.js +68 -16
  44. package/dist/plan.js.map +1 -1
  45. package/dist/rank.d.ts +9 -0
  46. package/dist/rank.js +167 -89
  47. package/dist/rank.js.map +1 -1
  48. package/dist/repo-scan.d.ts +3 -1
  49. package/dist/repo-scan.js +511 -106
  50. package/dist/repo-scan.js.map +1 -1
  51. package/dist/report.d.ts +9 -1
  52. package/dist/report.js +98 -54
  53. package/dist/report.js.map +1 -1
  54. package/dist/retrieval.d.ts +6 -0
  55. package/dist/retrieval.js +94 -0
  56. package/dist/retrieval.js.map +1 -0
  57. package/dist/signals.d.ts +5 -0
  58. package/dist/signals.js +130 -38
  59. package/dist/signals.js.map +1 -1
  60. package/dist/test-gates.js +15 -3
  61. package/dist/test-gates.js.map +1 -1
  62. package/dist/text.d.ts +2 -0
  63. package/dist/text.js +4 -0
  64. package/dist/text.js.map +1 -1
  65. package/dist/types.d.ts +45 -2
  66. package/dist/validate.d.ts +1 -1
  67. package/dist/validate.js +174 -16
  68. package/dist/validate.js.map +1 -1
  69. package/dist/verify.js +48 -5
  70. package/dist/verify.js.map +1 -1
  71. package/package.json +2 -2
package/dist/repo-scan.js CHANGED
@@ -1,35 +1,25 @@
1
1
  import { execFile } from "node:child_process";
2
- import { createHash } from "node:crypto";
3
- import { mkdir, readdir, readFile, realpath, stat, unlink, writeFile } from "node:fs/promises";
2
+ import { createHash, randomUUID } from "node:crypto";
3
+ import { mkdir, readdir, readFile, realpath, rename, stat, unlink, writeFile } from "node:fs/promises";
4
4
  import { homedir } from "node:os";
5
- import { dirname, extname, join, relative, resolve, sep } from "node:path";
5
+ import { dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
6
6
  import { promisify } from "node:util";
7
- import { ALWAYS_IGNORED_DIRS, GENERATED_DIRS, isGeneratedPath } from "./paths.js";
7
+ import { ALWAYS_IGNORED_DIRS, GENERATED_DIRS, SOURCE_FILE_EXTENSIONS, isGeneratedPath } from "./paths.js";
8
8
  import { DIAGNOSTIC_SPEC_LIMIT, truncateForDiagnostic } from "./text.js";
9
- const WALK_IGNORED_DIRS = new Set([...ALWAYS_IGNORED_DIRS, ...GENERATED_DIRS]);
10
- const SOURCE_EXTENSIONS = new Set([
11
- ".cjs",
12
- ".cs",
13
- ".css",
14
- ".cts",
15
- ".go",
16
- ".java",
17
- ".js",
18
- ".json",
19
- ".jsx",
20
- ".md",
21
- ".mjs",
22
- ".mts",
23
- ".php",
24
- ".py",
25
- ".rb",
26
- ".rs",
27
- ".svelte",
28
- ".ts",
29
- ".tsx",
30
- ".vue",
31
- ".yaml",
32
- ".yml"
9
+ // Vendored source is intentionally scannable and receives a ranking penalty. Git mode has
10
+ // always kept tracked vendor/ files; walk mode must not silently use a smaller corpus.
11
+ const WALK_IGNORED_DIRS = new Set([
12
+ ...ALWAYS_IGNORED_DIRS,
13
+ ...[...GENERATED_DIRS].filter((directory) => directory !== "vendor")
14
+ ]);
15
+ const SOURCE_EXTENSIONS = SOURCE_FILE_EXTENSIONS;
16
+ const CONVENTIONAL_DOCUMENT_NAMES = new Set([
17
+ "authors", "changelog", "code_of_conduct", "contributing", "license", "notice", "readme", "security"
18
+ ]);
19
+ const CONVENTIONAL_CONFIG_NAMES = new Set([
20
+ ".dockerignore", ".editorconfig", ".gitattributes", ".gitignore", ".npmignore",
21
+ "codeowners", "dockerfile", "gemfile", "jenkinsfile", "makefile", "procfile", "rakefile", "vagrantfile",
22
+ "pom.xml", "build.gradle", "settings.gradle"
33
23
  ]);
34
24
  /**
35
25
  * A single-file component is mostly template and style. Sampling the whole file let markup
@@ -43,6 +33,9 @@ const TEST_PATTERNS = [
43
33
  /\.spec\./,
44
34
  /(^|\/|\\)__tests__(\/|\\)/,
45
35
  /(^|\/|\\)tests?(\/|\\)/,
36
+ /(^|\/|\\)spec(\/|\\)/,
37
+ /_spec\.rb$/i,
38
+ /(?:Test\.java|Tests?\.cs|Test\.php)$/i,
46
39
  /_test\.go$/,
47
40
  /(^|\/|\\)(?:test_[^/\\]+|[^/\\]+_test)\.py$/
48
41
  ];
@@ -50,8 +43,15 @@ const MAX_TEXT_SAMPLE_BYTES = 64_000;
50
43
  const MAX_DIFF_TEXT_CHARS = 200_000;
51
44
  const MAX_SCANNED_FILES = 25_000;
52
45
  const GIT_MAX_BUFFER = 10 * 1024 * 1024;
46
+ const GIT_HISTORY_MAX_BUFFER = 24 * 1024 * 1024;
47
+ const MAX_HISTORY_COMMITS = 1_000;
48
+ const MAX_HISTORY_FILES_PER_COMMIT = 30;
53
49
  const exec = promisify(execFile);
54
- const SCAN_CACHE_VERSION = 1;
50
+ const SCAN_CACHE_VERSION = 4;
51
+ const SCAN_CACHE_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
52
+ const SCAN_CACHE_MAX_FUTURE_SKEW_MS = 5 * 60 * 1000;
53
+ const SCAN_CACHE_FILE = /^[a-f0-9]{24}-[a-f0-9]{24}\.json$/;
54
+ const SCAN_CACHE_TEMP_FILE = /^[a-f0-9]{24}-[a-f0-9]{24}\.json\.\d+-[0-9a-f-]+\.tmp$/i;
55
55
  export async function scanRepo(input) {
56
56
  const repoRoot = resolve(input.repoRoot);
57
57
  if (!(await isDirectory(repoRoot))) {
@@ -70,44 +70,77 @@ export async function scanRepo(input) {
70
70
  };
71
71
  }
72
72
  const diagnostics = [];
73
- const cacheLocation = input.useCache ? await buildScanCacheLocation(repoRoot) : undefined;
73
+ const internalPaths = await resolveInternalPaths(repoRoot, input.internalExclude ?? []);
74
+ const cacheRoot = configuredScanCacheRoot();
75
+ const internalCacheRoot = sameFilesystemPath(cacheRoot, repoRoot) || containedPath(repoRoot, cacheRoot) !== undefined
76
+ ? cacheRoot
77
+ : undefined;
78
+ const cacheDecision = input.useCache === true
79
+ ? await buildScanCacheLocation(repoRoot, cacheRoot, internalPaths, input.includeHistory === true)
80
+ : undefined;
81
+ const cacheLocation = cacheDecision?.location;
82
+ if (input.useCache === false) {
83
+ diagnostics.push({
84
+ code: "cache-bypass",
85
+ severity: "info",
86
+ message: "Repository scan caching was bypassed by --no-cache; this report used a fresh scan."
87
+ });
88
+ }
89
+ else if (input.useCache === true && cacheDecision?.skipReason) {
90
+ diagnostics.push({
91
+ code: "cache-skip",
92
+ severity: "info",
93
+ message: cacheDecision.skipReason
94
+ });
95
+ }
74
96
  const cached = cacheLocation ? await readScanCache(cacheLocation) : undefined;
75
97
  let files;
76
98
  let trackedFiles;
77
99
  let packageScripts;
78
100
  let packageManager;
101
+ let history;
79
102
  if (cached) {
80
103
  files = cached.files;
81
104
  trackedFiles = cached.trackedFiles;
82
105
  packageScripts = cached.packageScripts;
83
106
  packageManager = cached.packageManager;
107
+ history = cached.history ?? undefined;
84
108
  diagnostics.push(...cached.diagnostics, {
85
109
  code: "cache-hit",
86
110
  severity: "info",
87
- message: `Reused the repository scan for the exact current git state (${files.length.toLocaleString()} files). Pass --no-cache to rescan.`
111
+ message: `Reused the repository scan for the exact current git state (${files.length.toLocaleString()} files, ${describeCacheAge(cached.createdAt)}). Pass --no-cache to rescan.`
88
112
  });
89
113
  }
90
114
  else {
91
- files = await listFiles(repoRoot, diagnostics);
92
- trackedFiles = await listTrackedPaths(repoRoot);
115
+ files = await listFiles(repoRoot, diagnostics, internalCacheRoot, internalPaths);
116
+ trackedFiles = await listTrackedPaths(repoRoot, internalPaths);
93
117
  packageScripts = await readPackageScripts(repoRoot, files, diagnostics);
94
- packageManager = detectPackageManager(files);
118
+ packageManager = detectPackageManager(files, diagnostics);
119
+ history = input.includeHistory === true
120
+ ? await readRepositoryHistory(repoRoot, new Set(files.map((file) => file.path)), diagnostics)
121
+ : undefined;
95
122
  if (cacheLocation) {
96
123
  await writeScanCache(cacheLocation, {
97
124
  version: SCAN_CACHE_VERSION,
98
125
  stateKey: cacheLocation.stateKey,
126
+ createdAt: new Date().toISOString(),
99
127
  files,
100
128
  trackedFiles,
101
129
  packageScripts,
102
130
  packageManager,
103
- diagnostics: [...diagnostics]
131
+ diagnostics: [...diagnostics],
132
+ history: history ?? null
104
133
  });
105
134
  }
106
135
  }
107
136
  const diffSpec = resolveDiffSpec(input);
108
137
  const diff = input.workingTree
109
- ? await readWorkingTree(repoRoot, input.includeUntracked === true, diagnostics)
110
- : await readDiff(repoRoot, diffSpec, diagnostics);
138
+ ? await readWorkingTree(repoRoot, input.includeUntracked === true, diagnostics, internalPaths)
139
+ : await readDiff(repoRoot, diffSpec, diagnostics, internalPaths);
140
+ const orderedDiagnostics = [
141
+ ...diagnostics.filter((entry) => !entry.code.startsWith("impact-history-")),
142
+ ...diagnostics.filter((entry) => entry.code.startsWith("impact-history-"))
143
+ ];
111
144
  return {
112
145
  root: repoRoot,
113
146
  files,
@@ -116,24 +149,70 @@ export async function scanRepo(input) {
116
149
  changedFiles: diff.changedFiles,
117
150
  diffText: diff.diffText,
118
151
  packageManager,
119
- diagnostics
152
+ diagnostics: orderedDiagnostics,
153
+ ...(history ? { history } : {})
120
154
  };
121
155
  }
122
- async function buildScanCacheLocation(root) {
156
+ function configuredScanCacheRoot() {
157
+ return resolve(process.env.FIXMAP_CACHE_DIR ?? join(process.env.LOCALAPPDATA ?? process.env.XDG_CACHE_HOME ?? join(homedir(), ".cache"), "fixmap", "scans"));
158
+ }
159
+ function containedPath(root, candidate) {
160
+ const distance = relative(root, candidate);
161
+ return distance === "" || distance === ".." || distance.startsWith(`..${sep}`) || isAbsolute(distance)
162
+ ? undefined
163
+ : normalizePath(distance);
164
+ }
165
+ async function resolveInternalPaths(root, paths) {
166
+ const requested = paths.flatMap((path) => {
167
+ const relativePath = containedPath(root, resolve(path));
168
+ return relativePath ? [relativePath] : [];
169
+ });
170
+ if (requested.length === 0 || process.platform !== "win32")
171
+ return new Set(requested);
172
+ // Git pathspecs remain case-sensitive even on a case-insensitive Windows worktree. A
173
+ // caller can open PLAN.JSON when the directory entry is plan.json, so recover Git's
174
+ // canonical spelling before using exact exclusions in status and diff commands.
175
+ try {
176
+ const { stdout } = await exec("git", ["ls-files", "--cached", "--others", "--exclude-standard", "-z"], { cwd: root, maxBuffer: GIT_MAX_BUFFER });
177
+ const repositoryPaths = stdout.split("\0").filter(Boolean).map(normalizePath);
178
+ return new Set(requested.map((path) => repositoryPaths.find((candidate) => sameFilesystemPath(candidate, path)) ?? path));
179
+ }
180
+ catch {
181
+ return new Set(requested);
182
+ }
183
+ }
184
+ function hasInternalPath(paths, path) {
185
+ return [...paths].some((candidate) => sameFilesystemPath(candidate, path));
186
+ }
187
+ function gitPathspec(internalPaths) {
188
+ return ["--", ".", ...[...internalPaths]
189
+ .sort((a, b) => a.localeCompare(b))
190
+ .map((path) => `:(exclude,literal)${path}`)];
191
+ }
192
+ async function buildScanCacheLocation(root, cacheRoot, internalPaths, includeHistory) {
193
+ if (sameFilesystemPath(cacheRoot, root) || containedPath(root, cacheRoot) !== undefined) {
194
+ return {
195
+ skipReason: "Repository scan caching was skipped because FIXMAP_CACHE_DIR is inside the scanned repository. " +
196
+ "Move the cache outside the repository to enable exact-state reuse."
197
+ };
198
+ }
123
199
  try {
124
200
  const [{ stdout: head }, { stdout: status }] = await Promise.all([
125
201
  exec("git", ["rev-parse", "HEAD"], { cwd: root, maxBuffer: GIT_MAX_BUFFER }),
126
- exec("git", ["status", "--porcelain=v1", "-z", "--untracked-files=all", "--", "."], {
202
+ exec("git", ["status", "--porcelain=v1", "-z", "--untracked-files=all", ...gitPathspec(internalPaths)], {
127
203
  cwd: root,
128
204
  maxBuffer: GIT_MAX_BUFFER
129
205
  })
130
206
  ]);
131
207
  // Untracked files are scanner inputs but are absent from `git diff`. Do not cache that
132
208
  // state rather than keying it on names alone and serving stale contents after an edit.
133
- if (status.split("\0").some((entry) => entry.startsWith("?? ")))
134
- return undefined;
209
+ if (status.split("\0").some((entry) => entry.startsWith("?? "))) {
210
+ return {
211
+ skipReason: "Repository scan caching was skipped because untracked files are scanner inputs and can change without a stable git diff."
212
+ };
213
+ }
135
214
  const dirtyDiff = status.length > 0
136
- ? (await exec("git", ["diff", "--binary", "--no-ext-diff", "HEAD", "--", "."], {
215
+ ? (await exec("git", ["diff", "--binary", "--no-ext-diff", "HEAD", ...gitPathspec(internalPaths)], {
137
216
  cwd: root,
138
217
  maxBuffer: GIT_MAX_BUFFER
139
218
  })).stdout
@@ -143,28 +222,41 @@ async function buildScanCacheLocation(root) {
143
222
  resolve(root),
144
223
  head.trim(),
145
224
  status,
146
- dirtyDiff
225
+ dirtyDiff,
226
+ includeHistory ? "history" : "no-history",
227
+ ...[...internalPaths].sort((a, b) => a.localeCompare(b))
147
228
  ].join("\0"));
148
- const cacheRoot = process.env.FIXMAP_CACHE_DIR ?? join(process.env.LOCALAPPDATA ?? process.env.XDG_CACHE_HOME ?? join(homedir(), ".cache"), "fixmap", "scans");
149
- return {
150
- path: join(cacheRoot, `${hashText(resolve(root))}-${stateKey}.json`),
151
- stateKey
152
- };
229
+ return { location: {
230
+ path: join(cacheRoot, `${hashText(resolve(root))}-${stateKey}.json`),
231
+ stateKey
232
+ } };
153
233
  }
154
234
  catch {
155
235
  // Non-git directories deliberately do not cache: they have no cheap exact invalidation key.
156
- return undefined;
236
+ return {
237
+ skipReason: "Repository scan caching was skipped because this directory has no exact git state to key safely."
238
+ };
157
239
  }
158
240
  }
159
241
  async function readScanCache(location) {
160
242
  try {
161
243
  const cached = JSON.parse(await readFile(location.path, "utf8"));
244
+ const createdAt = typeof cached.createdAt === "string" ? Date.parse(cached.createdAt) : Number.NaN;
162
245
  if (cached.version !== SCAN_CACHE_VERSION ||
163
246
  cached.stateKey !== location.stateKey ||
247
+ typeof cached.createdAt !== "string" ||
248
+ !Number.isFinite(createdAt) ||
249
+ Date.now() - createdAt > SCAN_CACHE_MAX_AGE_MS ||
250
+ createdAt - Date.now() > SCAN_CACHE_MAX_FUTURE_SKEW_MS ||
164
251
  !Array.isArray(cached.files) ||
252
+ !cached.files.every(isCachedRepoFile) ||
165
253
  !Array.isArray(cached.trackedFiles) ||
254
+ !cached.trackedFiles.every(isCachedRelativePath) ||
166
255
  !Array.isArray(cached.packageScripts) ||
256
+ !cached.packageScripts.every(isCachedPackageScript) ||
167
257
  !Array.isArray(cached.diagnostics) ||
258
+ !cached.diagnostics.every(isCachedDiagnostic) ||
259
+ !(cached.history === null || isCachedHistory(cached.history)) ||
168
260
  !["npm", "pnpm", "yarn", "bun"].includes(cached.packageManager ?? ""))
169
261
  return undefined;
170
262
  return cached;
@@ -173,38 +265,135 @@ async function readScanCache(location) {
173
265
  return undefined;
174
266
  }
175
267
  }
268
+ function isCachedHistory(candidate) {
269
+ if (!isRecord(candidate) || !Array.isArray(candidate.commits) ||
270
+ typeof candidate.inspectedCommits !== "number" || !Number.isSafeInteger(candidate.inspectedCommits) || candidate.inspectedCommits < 0 ||
271
+ typeof candidate.skippedLargeCommits !== "number" || !Number.isSafeInteger(candidate.skippedLargeCommits) || candidate.skippedLargeCommits < 0 ||
272
+ typeof candidate.shallow !== "boolean" || typeof candidate.truncated !== "boolean") {
273
+ return false;
274
+ }
275
+ return candidate.commits.every((commit) => {
276
+ if (!isRecord(commit) || typeof commit.hash !== "string" || !/^[a-f0-9]{40}$/i.test(commit.hash) ||
277
+ typeof commit.committedAt !== "number" || !Number.isSafeInteger(commit.committedAt) || commit.committedAt < 0 ||
278
+ !Array.isArray(commit.files))
279
+ return false;
280
+ return commit.files.every(isCachedRelativePath);
281
+ });
282
+ }
283
+ function isCachedRepoFile(candidate) {
284
+ if (!isRecord(candidate))
285
+ return false;
286
+ const validSkipReason = candidate.textSampleSkipReason === "too-large" ||
287
+ candidate.textSampleSkipReason === "not-text" || candidate.textSampleSkipReason === "unreadable";
288
+ return isCachedRelativePath(candidate.path) &&
289
+ typeof candidate.extension === "string" &&
290
+ typeof candidate.sizeBytes === "number" && Number.isFinite(candidate.sizeBytes) && candidate.sizeBytes >= 0 &&
291
+ typeof candidate.isTest === "boolean" &&
292
+ typeof candidate.isSource === "boolean" &&
293
+ (candidate.kind === "code" || candidate.kind === "config" || candidate.kind === "documentation" || candidate.kind === "other") &&
294
+ typeof candidate.textSample === "string" &&
295
+ typeof candidate.textSampleComplete === "boolean" &&
296
+ ((candidate.textSampleComplete && candidate.textSampleSkipReason === undefined) ||
297
+ (!candidate.textSampleComplete && validSkipReason));
298
+ }
299
+ function isCachedPackageScript(candidate) {
300
+ if (!isRecord(candidate))
301
+ return false;
302
+ return typeof candidate.name === "string" && candidate.name.trim().length > 0 &&
303
+ typeof candidate.command === "string" &&
304
+ (candidate.packageDir === "" || isCachedRelativePath(candidate.packageDir)) &&
305
+ (candidate.packageName === undefined ||
306
+ (typeof candidate.packageName === "string" && candidate.packageName.trim().length > 0));
307
+ }
308
+ function isCachedDiagnostic(candidate) {
309
+ if (!isRecord(candidate))
310
+ return false;
311
+ return typeof candidate.code === "string" && candidate.code.trim().length > 0 &&
312
+ typeof candidate.message === "string" && candidate.message.trim().length > 0 &&
313
+ (candidate.severity === "info" || candidate.severity === "warning" || candidate.severity === "error") &&
314
+ (candidate.paths === undefined ||
315
+ (Array.isArray(candidate.paths) && candidate.paths.every(isCachedRelativePath)));
316
+ }
317
+ function isRecord(candidate) {
318
+ return typeof candidate === "object" && candidate !== null && !Array.isArray(candidate);
319
+ }
320
+ function isCachedRelativePath(candidate) {
321
+ if (typeof candidate !== "string" || candidate.trim().length === 0 || candidate.includes("\0") ||
322
+ isAbsolute(candidate) || /^[\\/]/.test(candidate) || /^[A-Za-z]:/.test(candidate)) {
323
+ return false;
324
+ }
325
+ // Cache files may move between Windows and POSIX. Normalize both separators regardless
326
+ // of the current host so `..\secret`, UNC paths, and drive-relative paths cannot become
327
+ // trusted repository paths merely because they were read on Linux.
328
+ const segments = candidate.replace(/\\/g, "/").split("/");
329
+ return segments.every((segment) => segment.length > 0 && segment !== "." && segment !== "..");
330
+ }
176
331
  async function writeScanCache(location, cached) {
332
+ const temporaryPath = `${location.path}.${process.pid}-${randomUUID()}.tmp`;
177
333
  try {
178
334
  await mkdir(dirname(location.path), { recursive: true });
179
- await writeFile(location.path, `${JSON.stringify(cached)}\n`, { encoding: "utf8", flag: "wx" });
335
+ await pruneExpiredScanCache(dirname(location.path));
336
+ // Write beside the destination and rename only after the JSON is complete. Readers never
337
+ // observe a truncated cache file, and a corrupt/expired exact-key entry heals immediately.
338
+ await writeFile(temporaryPath, `${JSON.stringify(cached)}\n`, { encoding: "utf8", flag: "wx" });
339
+ await rename(temporaryPath, location.path);
180
340
  }
181
- catch (error) {
182
- const code = error.code;
183
- if (code !== "EEXIST") {
184
- // Cache writes are an optimization. A read-only cache directory must not fail a plan.
185
- return;
186
- }
187
- // An interrupted prior write can leave an invalid exact-key file. Replace only that
188
- // FixMap-owned cache entry; never touch a repository path.
189
- const existing = await readScanCache(location);
190
- if (existing)
191
- return;
341
+ catch {
342
+ // Cache writes are an optimization. Read-only directories and concurrent writers must not
343
+ // fail a report. Remove only this process's uniquely named temporary file.
192
344
  try {
193
- await unlink(location.path);
194
- await writeFile(location.path, `${JSON.stringify(cached)}\n`, { encoding: "utf8", flag: "wx" });
195
- }
196
- catch {
197
- // Another process may have won the replacement race; the current scan is still valid.
345
+ await unlink(temporaryPath);
198
346
  }
347
+ catch { /* The rename may already have consumed it. */ }
199
348
  }
200
349
  }
350
+ async function pruneExpiredScanCache(cacheRoot) {
351
+ try {
352
+ const entries = await readdir(cacheRoot, { withFileTypes: true });
353
+ const now = Date.now();
354
+ await Promise.all(entries
355
+ // A killed process can leave its uniquely named atomic-write file behind. It is not
356
+ // readable as a cache entry, but without pruning it the cache directory grows forever.
357
+ // The same seven-day horizon preserves any plausible active writer.
358
+ .filter((entry) => entry.isFile() && (SCAN_CACHE_FILE.test(entry.name) || SCAN_CACHE_TEMP_FILE.test(entry.name)))
359
+ .map(async (entry) => {
360
+ const path = join(cacheRoot, entry.name);
361
+ try {
362
+ const metadata = await stat(path);
363
+ if (now - metadata.mtimeMs > SCAN_CACHE_MAX_AGE_MS)
364
+ await unlink(path);
365
+ }
366
+ catch {
367
+ // Cache cleanup is best effort and must never block a report.
368
+ }
369
+ }));
370
+ }
371
+ catch {
372
+ // A read-only or concurrently removed cache directory is harmless.
373
+ }
374
+ }
375
+ function describeCacheAge(createdAt) {
376
+ const ageMs = Math.max(0, Date.now() - Date.parse(createdAt));
377
+ if (ageMs < 5_000)
378
+ return "scanned just now";
379
+ const minutes = Math.floor(ageMs / 60_000);
380
+ if (minutes < 1)
381
+ return "scanned less than a minute ago";
382
+ if (minutes < 60)
383
+ return `scanned ${minutes}m ago`;
384
+ const hours = Math.floor(minutes / 60);
385
+ if (hours < 24)
386
+ return `scanned ${hours}h ago`;
387
+ return `scanned ${Math.floor(hours / 24)}d ago`;
388
+ }
201
389
  function hashText(value) {
202
390
  return createHash("sha256").update(value).digest("hex").slice(0, 24);
203
391
  }
204
- async function listTrackedPaths(root) {
392
+ async function listTrackedPaths(root, internalPaths) {
205
393
  try {
206
394
  const { stdout } = await exec("git", ["ls-files", "--cached", "-z"], { cwd: root, maxBuffer: GIT_MAX_BUFFER });
207
- return stdout.split("\0").filter(Boolean).map(normalizePath);
395
+ return stdout.split("\0").filter(Boolean).map(normalizePath)
396
+ .filter((path) => !hasInternalPath(internalPaths, path));
208
397
  }
209
398
  catch {
210
399
  return [];
@@ -213,11 +402,12 @@ async function listTrackedPaths(root) {
213
402
  function resolveDiffSpec(input) {
214
403
  return input.diffSpec ?? (input.baseRef ? `${input.baseRef}...${input.headRef ?? "HEAD"}` : undefined);
215
404
  }
216
- async function listFiles(root, diagnostics) {
405
+ async function listFiles(root, diagnostics, internalCacheRoot, internalPaths) {
217
406
  const gitPaths = await listGitPaths(root);
407
+ const visiblePaths = gitPaths?.paths.filter((path) => !hasInternalPath(internalPaths, normalizePath(path)) && !isInternalCachePath(root, path, internalCacheRoot));
218
408
  const files = gitPaths
219
- ? await buildFilesFromPaths(root, gitPaths.paths, diagnostics, gitPaths.gitLinks)
220
- : (await walkFiles(root, root, diagnostics, { count: 0, limitReported: false }))
409
+ ? await buildFilesFromPaths(root, visiblePaths ?? [], diagnostics, gitPaths.gitLinks)
410
+ : (await walkFiles(root, root, diagnostics, { count: 0, limitReported: false }, internalCacheRoot, internalPaths))
221
411
  .sort((a, b) => a.path.localeCompare(b.path));
222
412
  // These are properties of the scanned files, not of git. Keeping them here makes an
223
413
  // extracted archive and a checkout report the same content limitations.
@@ -225,6 +415,20 @@ async function listFiles(root, diagnostics) {
225
415
  reportGeneratedDominance(diagnostics, files);
226
416
  return files;
227
417
  }
418
+ function isInternalCachePath(root, path, internalCacheRoot) {
419
+ if (!internalCacheRoot)
420
+ return false;
421
+ const relativeCacheRoot = containedPath(root, internalCacheRoot);
422
+ if (relativeCacheRoot) {
423
+ const candidate = process.platform === "win32" ? path.toLowerCase() : path;
424
+ const cachePath = process.platform === "win32" ? relativeCacheRoot.toLowerCase() : relativeCacheRoot;
425
+ return candidate === cachePath || candidate.startsWith(`${cachePath}/`);
426
+ }
427
+ // If someone points FIXMAP_CACHE_DIR at the repository root itself, do not hide the
428
+ // repository. Hide only filenames owned by FixMap's cache format.
429
+ return sameFilesystemPath(internalCacheRoot, root) && (SCAN_CACHE_FILE.test(path) ||
430
+ SCAN_CACHE_TEMP_FILE.test(path));
431
+ }
228
432
  async function listGitPaths(root) {
229
433
  try {
230
434
  const [{ stdout }, { stdout: staged }] = await Promise.all([
@@ -257,6 +461,10 @@ async function buildFilesFromPaths(root, paths, diagnostics, knownGitLinks = new
257
461
  // git listed first would have made that an alphabetical accident.
258
462
  const seenRealPaths = new Map();
259
463
  const linked = [];
464
+ // macOS temporary directories commonly enter through /var while realpath returns
465
+ // /private/var. Resolve the repository root once so that prefix alias does not make
466
+ // every ordinary file look like a symlink.
467
+ const realRoot = await resolveRealPath(root);
260
468
  for (const [index, rawPath] of paths.entries()) {
261
469
  if (results.length >= MAX_SCANNED_FILES) {
262
470
  // Git handed us the whole tracked list, so the remainder is known exactly.
@@ -290,8 +498,8 @@ async function buildFilesFromPaths(root, paths, diagnostics, knownGitLinks = new
290
498
  const seenFile = results[seenIndex];
291
499
  // Looking only at the leaf with lstat misses Windows junctions, where the linked
292
500
  // object is an ancestor directory. Comparing literal and resolved paths covers both.
293
- const seenIsAlias = !sameFilesystemPath(resolve(root, seenFile.path), scanned.realPath);
294
- const currentIsAlias = !sameFilesystemPath(resolve(root, relativePath), scanned.realPath);
501
+ const seenIsAlias = !sameFilesystemPath(resolve(realRoot, seenFile.path), scanned.realPath);
502
+ const currentIsAlias = !sameFilesystemPath(resolve(realRoot, relativePath), scanned.realPath);
295
503
  if (seenIsAlias && !currentIsAlias) {
296
504
  linked.push({ path: seenFile.path, target: relativePath });
297
505
  results[seenIndex] = scanned.file;
@@ -348,7 +556,7 @@ function reportUnreadContent(diagnostics, files) {
348
556
  const sample = affected.slice(0, 3).map((file) => file.path).join(", ");
349
557
  const prefix = `${affected.length.toLocaleString()} source file${affected.length === 1 ? "" : "s"}`;
350
558
  diagnostics.push({
351
- code: "content-unread",
559
+ code: reason === "not-text" ? "content-not-utf8" : "content-unreadable",
352
560
  severity: "warning",
353
561
  message: reason === "not-text"
354
562
  ? `${prefix} ${affected.length === 1 ? "is" : "are"} not UTF-8 text (for example UTF-16 or binary) and ` +
@@ -369,7 +577,7 @@ function reportUnreadContent(diagnostics, files) {
369
577
  .map((file) => `${file.path} (${Math.ceil(file.sizeBytes / 1000).toLocaleString()} kB)`)
370
578
  .join(", ");
371
579
  diagnostics.push({
372
- code: "content-unread",
580
+ code: "content-too-large",
373
581
  severity: "warning",
374
582
  message: `${unread.length.toLocaleString()} source file${unread.length === 1 ? "" : "s"} could not be read as text and ` +
375
583
  `rank${unread.length === 1 ? "s" : ""} on path alone — largest: ${sample}` +
@@ -428,7 +636,7 @@ function reportLinkedDuplicates(diagnostics, linked) {
428
636
  `${linked.length > 3 ? ", …" : ""}.`
429
637
  });
430
638
  }
431
- async function walkFiles(root, current, diagnostics, state) {
639
+ async function walkFiles(root, current, diagnostics, state, internalCacheRoot, internalPaths) {
432
640
  let entries;
433
641
  try {
434
642
  entries = await readdir(current, { withFileTypes: true });
@@ -449,14 +657,20 @@ async function walkFiles(root, current, diagnostics, state) {
449
657
  if (WALK_IGNORED_DIRS.has(entry.name)) {
450
658
  continue;
451
659
  }
452
- results.push(...await walkFiles(root, join(current, entry.name), diagnostics, state));
660
+ const directory = join(current, entry.name);
661
+ if (internalCacheRoot && sameFilesystemPath(directory, internalCacheRoot))
662
+ continue;
663
+ results.push(...await walkFiles(root, directory, diagnostics, state, internalCacheRoot, internalPaths));
453
664
  continue;
454
665
  }
455
666
  if (!entry.isFile()) {
456
667
  continue;
457
668
  }
458
669
  const absolutePath = join(current, entry.name);
459
- const scanned = await toRepoFile(absolutePath, normalizePath(relative(root, absolutePath)));
670
+ const relativePath = normalizePath(relative(root, absolutePath));
671
+ if (hasInternalPath(internalPaths, relativePath) || isInternalCachePath(root, relativePath, internalCacheRoot))
672
+ continue;
673
+ const scanned = await toRepoFile(absolutePath, relativePath);
460
674
  if (scanned.status === "ok") {
461
675
  results.push(scanned.file);
462
676
  state.count += 1;
@@ -475,8 +689,9 @@ async function toRepoFile(absolutePath, relativePath) {
475
689
  if (!fileStat.isFile()) {
476
690
  return { status: "not-a-file" };
477
691
  }
478
- const extension = extname(relativePath);
479
- const isSource = SOURCE_EXTENSIONS.has(extension);
692
+ const extension = extname(relativePath).toLowerCase();
693
+ const conventionalKind = classifyConventionalTextFile(relativePath);
694
+ const isSource = SOURCE_EXTENSIONS.has(extension) || conventionalKind !== undefined;
480
695
  const sample = isSource
481
696
  ? await readTextSample(absolutePath, fileStat.size)
482
697
  : { text: "", complete: true };
@@ -622,21 +837,21 @@ function decodeManifest(bytes) {
622
837
  }
623
838
  return { text: bytes.toString("utf8"), encoding: "utf8" };
624
839
  }
625
- async function readDiff(repoRoot, diffSpec, diagnostics) {
840
+ async function readDiff(repoRoot, diffSpec, diagnostics, internalPaths) {
626
841
  if (!diffSpec) {
627
842
  return { changedFiles: [], diffText: "" };
628
843
  }
629
844
  try {
630
845
  const [{ stdout: names }, { stdout: diffText }] = await Promise.all([
631
- exec("git", ["diff", "--relative", "--name-only", diffSpec], { cwd: repoRoot, maxBuffer: GIT_MAX_BUFFER }),
632
- exec("git", ["diff", "--relative", diffSpec], { cwd: repoRoot, maxBuffer: GIT_MAX_BUFFER })
846
+ exec("git", ["diff", "--relative", "--name-only", diffSpec, ...gitPathspec(internalPaths)], { cwd: repoRoot, maxBuffer: GIT_MAX_BUFFER }),
847
+ exec("git", ["diff", "--relative", diffSpec, ...gitPathspec(internalPaths)], { cwd: repoRoot, maxBuffer: GIT_MAX_BUFFER })
633
848
  ]);
634
849
  const tracked = names
635
850
  .split(/\r?\n/)
636
851
  .map((path) => path.trim())
637
852
  .filter(Boolean)
638
853
  .map(normalizePath);
639
- const untracked = diffSpec.includes("..") ? [] : await listUntrackedPaths(repoRoot);
854
+ const untracked = diffSpec.includes("..") ? [] : await listUntrackedPaths(repoRoot, internalPaths);
640
855
  const changedFiles = [...new Set([...tracked, ...untracked])].sort((a, b) => a.localeCompare(b));
641
856
  diagnostics.push({
642
857
  code: "diff-resolved",
@@ -646,10 +861,7 @@ async function readDiff(repoRoot, diffSpec, diagnostics) {
646
861
  : `Diff "${truncateForDiagnostic(diffSpec, DIAGNOSTIC_SPEC_LIMIT)}" resolved ${changedFiles.length} changed ${changedFiles.length === 1 ? "path" : "paths"}.`,
647
862
  paths: changedFiles.slice(0, 8)
648
863
  });
649
- return {
650
- changedFiles,
651
- diffText: diffText.slice(0, MAX_DIFF_TEXT_CHARS)
652
- };
864
+ return { changedFiles, diffText: sampleDiffText(diffText, diagnostics) };
653
865
  }
654
866
  catch (error) {
655
867
  const checkoutState = isMissingGit(error) ? undefined : await describeGitCheckout(repoRoot);
@@ -667,6 +879,65 @@ async function readDiff(repoRoot, diffSpec, diagnostics) {
667
879
  return { changedFiles: [], diffText: "" };
668
880
  }
669
881
  }
882
+ /**
883
+ * Large diffs used to be cut at an arbitrary UTF-16 offset. That could split a line and,
884
+ * because git orders files, erase every content signal from files later in the diff. For an
885
+ * oversized diff the ranker only needs added lines, so sample complete added lines from every
886
+ * file in round-robin order and say exactly what was omitted.
887
+ */
888
+ function sampleDiffText(diffText, diagnostics) {
889
+ if (diffText.length <= MAX_DIFF_TEXT_CHARS)
890
+ return diffText;
891
+ const groups = [];
892
+ let current;
893
+ for (const line of diffText.split(/\r?\n/)) {
894
+ if (line.startsWith("diff --git ")) {
895
+ current = [];
896
+ groups.push(current);
897
+ continue;
898
+ }
899
+ if (line.startsWith("+") && !line.startsWith("+++")) {
900
+ if (!current) {
901
+ current = [];
902
+ groups.push(current);
903
+ }
904
+ current.push(line);
905
+ }
906
+ }
907
+ const queues = groups.filter((group) => group.length > 0);
908
+ const selected = [];
909
+ let selectedChars = 0;
910
+ let selectedLines = 0;
911
+ const totalLines = queues.reduce((total, group) => total + group.length, 0);
912
+ let cursor = 0;
913
+ let madeProgress = true;
914
+ while (madeProgress && selectedChars < MAX_DIFF_TEXT_CHARS) {
915
+ madeProgress = false;
916
+ for (const group of queues) {
917
+ const line = group[cursor];
918
+ if (line === undefined)
919
+ continue;
920
+ madeProgress = true;
921
+ const separator = selected.length === 0 ? 0 : 1;
922
+ if (selectedChars + separator + line.length <= MAX_DIFF_TEXT_CHARS) {
923
+ selected.push(line);
924
+ selectedChars += separator + line.length;
925
+ selectedLines += 1;
926
+ }
927
+ }
928
+ cursor += 1;
929
+ }
930
+ diagnostics.push({
931
+ code: "diff-text-truncated",
932
+ severity: "warning",
933
+ message: `The git diff was ${diffText.length.toLocaleString()} characters, above FixMap's ` +
934
+ `${MAX_DIFF_TEXT_CHARS.toLocaleString()}-character signal budget. FixMap sampled ` +
935
+ `${selectedLines.toLocaleString()} of ${totalLines.toLocaleString()} complete added lines ` +
936
+ `across ${queues.length.toLocaleString()} changed ${queues.length === 1 ? "file" : "files"}; ` +
937
+ "changed-file paths remain complete, but omitted diff content could reduce ranking precision."
938
+ });
939
+ return selected.join("\n");
940
+ }
670
941
  /**
671
942
  * "Map what I am touching right now."
672
943
  *
@@ -675,18 +946,18 @@ async function readDiff(repoRoot, diffSpec, diagnostics) {
675
946
  * scratch notes ranking beside the edit. Staged and unstaged tracked changes are what
676
947
  * "what I am working on" means; untracked files are a separate, opt-in question.
677
948
  */
678
- async function readWorkingTree(repoRoot, includeUntracked, diagnostics) {
949
+ async function readWorkingTree(repoRoot, includeUntracked, diagnostics, internalPaths) {
679
950
  try {
680
951
  const [{ stdout: names }, { stdout: diffText }] = await Promise.all([
681
- exec("git", ["diff", "--relative", "--name-only", "HEAD"], { cwd: repoRoot, maxBuffer: GIT_MAX_BUFFER }),
682
- exec("git", ["diff", "--relative", "HEAD"], { cwd: repoRoot, maxBuffer: GIT_MAX_BUFFER })
952
+ exec("git", ["diff", "--relative", "--name-only", "HEAD", ...gitPathspec(internalPaths)], { cwd: repoRoot, maxBuffer: GIT_MAX_BUFFER }),
953
+ exec("git", ["diff", "--relative", "HEAD", ...gitPathspec(internalPaths)], { cwd: repoRoot, maxBuffer: GIT_MAX_BUFFER })
683
954
  ]);
684
955
  const tracked = names
685
956
  .split(/\r?\n/)
686
957
  .map((path) => path.trim())
687
958
  .filter(Boolean)
688
959
  .map(normalizePath);
689
- const untracked = includeUntracked ? await listUntrackedPaths(repoRoot) : [];
960
+ const untracked = includeUntracked ? await listUntrackedPaths(repoRoot, internalPaths) : [];
690
961
  const changedFiles = [...new Set([...tracked, ...untracked])].sort((a, b) => a.localeCompare(b));
691
962
  diagnostics.push({
692
963
  code: "working-tree-diff",
@@ -699,7 +970,7 @@ async function readWorkingTree(repoRoot, includeUntracked, diagnostics) {
699
970
  `against HEAD${includeUntracked ? ", including untracked files" : " (untracked files are not counted as changed, though they still rank; pass --include-untracked to count them)"}.`,
700
971
  paths: changedFiles.slice(0, 8)
701
972
  });
702
- return { changedFiles, diffText: diffText.slice(0, MAX_DIFF_TEXT_CHARS) };
973
+ return { changedFiles, diffText: sampleDiffText(diffText, diagnostics) };
703
974
  }
704
975
  catch (error) {
705
976
  const checkoutState = isMissingGit(error) ? undefined : await describeGitCheckout(repoRoot);
@@ -726,6 +997,98 @@ const NOT_A_GIT_CHECKOUT = "this directory is not a git checkout. Ranking still
726
997
  "--diff, --base/--head and --working-tree need a repository with history.";
727
998
  const NO_GIT_HISTORY = "this repository has no commits yet, so there is nothing to diff against. " +
728
999
  "Commit the initial work first, or run with --issue alone to rank from the task text.";
1000
+ async function readRepositoryHistory(root, repositoryPaths, diagnostics) {
1001
+ try {
1002
+ const [{ stdout: shallowText }, { stdout: countText }, { stdout: logText }] = await Promise.all([
1003
+ exec("git", ["rev-parse", "--is-shallow-repository"], { cwd: root, maxBuffer: GIT_MAX_BUFFER }),
1004
+ exec("git", ["rev-list", "--count", "--no-merges", "HEAD"], { cwd: root, maxBuffer: GIT_MAX_BUFFER }),
1005
+ exec("git", [
1006
+ "-c", "core.quotepath=false",
1007
+ "log",
1008
+ "--no-merges",
1009
+ "-n", String(MAX_HISTORY_COMMITS),
1010
+ "--format=%x1e%H%x1f%ct",
1011
+ "--name-only",
1012
+ "-z",
1013
+ "HEAD"
1014
+ ], { cwd: root, maxBuffer: GIT_HISTORY_MAX_BUFFER })
1015
+ ]);
1016
+ const parsed = parseHistoryLog(logText, repositoryPaths);
1017
+ const totalCommits = Number.parseInt(countText.trim(), 10);
1018
+ const shallow = shallowText.trim() === "true";
1019
+ const truncated = Number.isFinite(totalCommits) && totalCommits > parsed.inspectedCommits;
1020
+ const history = {
1021
+ commits: parsed.commits,
1022
+ inspectedCommits: parsed.inspectedCommits,
1023
+ skippedLargeCommits: parsed.skippedLargeCommits,
1024
+ shallow,
1025
+ truncated
1026
+ };
1027
+ if (shallow) {
1028
+ diagnostics.push({
1029
+ code: "impact-history-shallow",
1030
+ severity: "info",
1031
+ message: `Impact history is shallow (${parsed.inspectedCommits.toLocaleString()} visible non-merge ` +
1032
+ `${parsed.inspectedCommits === 1 ? "commit" : "commits"}). Import and test relationships remain available, ` +
1033
+ "but co-change evidence may be incomplete."
1034
+ });
1035
+ }
1036
+ if (truncated) {
1037
+ diagnostics.push({
1038
+ code: "impact-history-truncated",
1039
+ severity: "info",
1040
+ message: `Impact history inspected the newest ${parsed.inspectedCommits.toLocaleString()} of ${totalCommits.toLocaleString()} ` +
1041
+ `non-merge commits. Commits touching more than ${MAX_HISTORY_FILES_PER_COMMIT} files were excluded from co-change evidence.`
1042
+ });
1043
+ }
1044
+ return history;
1045
+ }
1046
+ catch (error) {
1047
+ const checkoutState = isMissingGit(error) ? undefined : await describeGitCheckout(root);
1048
+ diagnostics.push({
1049
+ code: "impact-history-unavailable",
1050
+ severity: "info",
1051
+ message: checkoutState === "not-repository"
1052
+ ? "Impact history is unavailable because this directory is not a Git checkout; import and test relationships are still reported."
1053
+ : checkoutState === "no-history"
1054
+ ? "Impact history is unavailable because this repository has no commits; import and test relationships are still reported."
1055
+ : `Impact history could not be read (${truncateForDiagnostic(gitErrorDetail(error), DIAGNOSTIC_SPEC_LIMIT * 2)}); import and test relationships are still reported.`
1056
+ });
1057
+ return undefined;
1058
+ }
1059
+ }
1060
+ function parseHistoryLog(logText, repositoryPaths) {
1061
+ const commits = [];
1062
+ let inspectedCommits = 0;
1063
+ let skippedLargeCommits = 0;
1064
+ for (const record of logText.split("\x1e")) {
1065
+ if (!record)
1066
+ continue;
1067
+ const fields = record.split("\0");
1068
+ const header = fields.shift()?.replace(/^\r?\n/, "") ?? "";
1069
+ const separator = header.indexOf("\x1f");
1070
+ if (separator === -1)
1071
+ continue;
1072
+ const hash = header.slice(0, separator).trim();
1073
+ const committedAt = Number.parseInt(header.slice(separator + 1).trim(), 10);
1074
+ if (!/^[a-f0-9]{40}$/i.test(hash) || !Number.isSafeInteger(committedAt) || committedAt < 0)
1075
+ continue;
1076
+ inspectedCommits += 1;
1077
+ const allFiles = [...new Set(fields
1078
+ .map((path) => path.replace(/^\r?\n/, ""))
1079
+ .filter(Boolean)
1080
+ .map(normalizePath))];
1081
+ if (allFiles.length > MAX_HISTORY_FILES_PER_COMMIT) {
1082
+ skippedLargeCommits += 1;
1083
+ continue;
1084
+ }
1085
+ const currentFiles = allFiles.filter((path) => repositoryPaths.has(path));
1086
+ if (currentFiles.length === 0)
1087
+ continue;
1088
+ commits.push({ hash, committedAt, files: currentFiles });
1089
+ }
1090
+ return { commits, inspectedCommits, skippedLargeCommits };
1091
+ }
729
1092
  /**
730
1093
  * `execFile` puts "Command failed: git ..." in `message` and git's own explanation in
731
1094
  * `stderr`, so matching on the message alone never saw the reason. Both are checked.
@@ -758,28 +1121,69 @@ function gitErrorDetail(error) {
758
1121
  function isMissingGit(error) {
759
1122
  return error?.code === "ENOENT";
760
1123
  }
761
- function detectPackageManager(files) {
762
- const paths = new Set(files.map((file) => file.path));
763
- if (paths.has("pnpm-lock.yaml") || paths.has("pnpm-workspace.yaml"))
764
- return "pnpm";
765
- if (paths.has("yarn.lock") || paths.has(".yarnrc.yml"))
766
- return "yarn";
767
- if (paths.has("bun.lock") || paths.has("bun.lockb"))
768
- return "bun";
1124
+ function detectPackageManager(files, diagnostics) {
1125
+ const rootManagers = packageManagersForPaths(files.map((file) => file.path).filter((path) => !path.includes("/")));
1126
+ if (rootManagers.length > 1) {
1127
+ diagnostics.push({
1128
+ code: "package-manager-conflict",
1129
+ severity: "warning",
1130
+ message: `Conflicting root package-manager files were found (${rootManagers.join(", ")}); using ${rootManagers[0]} deterministically. Remove the stale lockfile so routed commands are unambiguous.`
1131
+ });
1132
+ }
1133
+ if (rootManagers[0])
1134
+ return rootManagers[0];
1135
+ // Archives and workspace extracts do not always carry a root lockfile. When every nested
1136
+ // declaration agrees, use that manager rather than printing npm commands that cannot run.
1137
+ const nestedManagers = packageManagersForPaths(files.map((file) => file.path));
1138
+ if (nestedManagers.length === 1)
1139
+ return nestedManagers[0];
1140
+ if (nestedManagers.length > 1) {
1141
+ diagnostics.push({
1142
+ code: "package-manager-conflict",
1143
+ severity: "warning",
1144
+ message: `Nested package-manager files disagree (${nestedManagers.join(", ")}); defaulting to npm for root routes. Point --repo at one workspace to get an unambiguous command.`
1145
+ });
1146
+ }
769
1147
  return "npm";
770
1148
  }
1149
+ function packageManagersForPaths(paths) {
1150
+ const names = new Set(paths.map((path) => path.split("/").at(-1) ?? path));
1151
+ const managers = [];
1152
+ if (names.has("pnpm-lock.yaml") || names.has("pnpm-workspace.yaml"))
1153
+ managers.push("pnpm");
1154
+ if (names.has("yarn.lock") || names.has(".yarnrc.yml"))
1155
+ managers.push("yarn");
1156
+ if (names.has("bun.lock") || names.has("bun.lockb"))
1157
+ managers.push("bun");
1158
+ if (names.has("package-lock.json") || names.has("npm-shrinkwrap.json"))
1159
+ managers.push("npm");
1160
+ return managers;
1161
+ }
771
1162
  function classifyFile(path, extension) {
772
1163
  const lower = path.toLowerCase();
773
- if (extension === ".md" || lower.startsWith("docs/") || lower === "license")
1164
+ const conventionalKind = classifyConventionalTextFile(path);
1165
+ if (conventionalKind)
1166
+ return conventionalKind;
1167
+ if (extension === ".md" || lower.startsWith("docs/"))
774
1168
  return "documentation";
775
1169
  if (lower.startsWith(".github/") ||
776
1170
  [".json", ".yaml", ".yml"].includes(extension) ||
777
- /(^|\/)([^/]+\.)?(config|rc)\.[^/]+$/.test(lower))
1171
+ /(^|\/)(?:[^/]+\.config|\.[^/]*rc)\.[^/]+$/.test(lower))
778
1172
  return "config";
779
1173
  if (SOURCE_EXTENSIONS.has(extension))
780
1174
  return "code";
781
1175
  return "other";
782
1176
  }
1177
+ function classifyConventionalTextFile(path) {
1178
+ const name = path.replace(/\\/g, "/").split("/").at(-1)?.toLowerCase() ?? "";
1179
+ if (CONVENTIONAL_DOCUMENT_NAMES.has(name))
1180
+ return "documentation";
1181
+ if (CONVENTIONAL_CONFIG_NAMES.has(name))
1182
+ return "config";
1183
+ if (/\.(?:csproj|fsproj|vbproj)$/.test(name))
1184
+ return "config";
1185
+ return undefined;
1186
+ }
783
1187
  async function readTextSample(path, sizeBytes) {
784
1188
  if (sizeBytes > MAX_TEXT_SAMPLE_BYTES) {
785
1189
  return { text: "", complete: false, skipReason: "too-large" };
@@ -800,10 +1204,11 @@ async function readTextSample(path, sizeBytes) {
800
1204
  return { text: "", complete: false, skipReason: "unreadable" };
801
1205
  }
802
1206
  }
803
- async function listUntrackedPaths(repoRoot) {
1207
+ async function listUntrackedPaths(repoRoot, internalPaths = new Set()) {
804
1208
  try {
805
1209
  const { stdout } = await exec("git", ["ls-files", "--others", "--exclude-standard", "-z"], { cwd: repoRoot, maxBuffer: GIT_MAX_BUFFER });
806
- return stdout.split("\0").filter(Boolean).map(normalizePath);
1210
+ return stdout.split("\0").filter(Boolean).map(normalizePath)
1211
+ .filter((path) => !hasInternalPath(internalPaths, path));
807
1212
  }
808
1213
  catch {
809
1214
  return [];