code-guardian-ai 0.0.0-stage → 0.3.1

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 (70) hide show
  1. package/.env.example +24 -0
  2. package/LICENSE +21 -0
  3. package/README.md +774 -2
  4. package/bin/cli.js +2 -0
  5. package/dist/build-manifest.json +136 -0
  6. package/dist/cli/commands/check.js +56 -0
  7. package/dist/cli/commands/doctor.js +55 -0
  8. package/dist/cli/commands/find.js +31 -0
  9. package/dist/cli/commands/init-files.js +13 -0
  10. package/dist/cli/commands/init.js +50 -0
  11. package/dist/cli/commands/optimize.js +53 -0
  12. package/dist/cli/commands/scan.js +47 -0
  13. package/dist/cli/index.js +62 -0
  14. package/dist/cli/output.js +4 -0
  15. package/dist/context.js +11 -0
  16. package/dist/core/ast/analysis.js +455 -0
  17. package/dist/core/ast/cached-info.js +46 -0
  18. package/dist/core/ast/discovery.js +485 -0
  19. package/dist/core/ast/extract-js.js +531 -0
  20. package/dist/core/ast/extract-py.js +298 -0
  21. package/dist/core/ast/graph.js +92 -0
  22. package/dist/core/ast/health.js +338 -0
  23. package/dist/core/ast/indexer.js +286 -0
  24. package/dist/core/ast/packages.js +343 -0
  25. package/dist/core/ast/parser.js +91 -0
  26. package/dist/core/ast/report.js +154 -0
  27. package/dist/core/ast/resolver.js +392 -0
  28. package/dist/core/ast/rules.js +37 -0
  29. package/dist/core/ast/scope-js.js +191 -0
  30. package/dist/core/ast/shared-index.js +131 -0
  31. package/dist/core/ast/tsconfig.js +301 -0
  32. package/dist/core/ast/types.js +1 -0
  33. package/dist/core/ast/util.js +156 -0
  34. package/dist/core/git/git.js +430 -0
  35. package/dist/core/llm/policy.js +35 -0
  36. package/dist/core/llm/provider.js +199 -0
  37. package/dist/core/prompt/compressor.js +118 -0
  38. package/dist/core/prompt/criteria.js +36 -0
  39. package/dist/core/prompt/find.js +47 -0
  40. package/dist/core/prompt/guidelines.js +22 -0
  41. package/dist/core/prompt/model-output.js +178 -0
  42. package/dist/core/prompt/refiner.js +194 -0
  43. package/dist/core/prompt/relevance.js +265 -0
  44. package/dist/core/prompt/untrusted.js +57 -0
  45. package/dist/core/security/masker.js +330 -0
  46. package/dist/core/security/outbound.js +40 -0
  47. package/dist/core/security/safe-file.js +396 -0
  48. package/dist/core/security/sandbox.js +181 -0
  49. package/dist/core/validation/baseline.js +121 -0
  50. package/dist/core/validation/delta.js +243 -0
  51. package/dist/core/validation/policy.js +123 -0
  52. package/dist/db/cache-key.js +221 -0
  53. package/dist/db/client.js +563 -0
  54. package/dist/db/schema-check.js +149 -0
  55. package/dist/db/schema.js +36 -0
  56. package/dist/db/windows-acl.js +150 -0
  57. package/dist/index.js +55 -0
  58. package/dist/mcp/server.js +20 -0
  59. package/dist/mcp/tools/find-reusable.js +22 -0
  60. package/dist/mcp/tools/get-context.js +39 -0
  61. package/dist/mcp/tools/optimize-prompt.js +31 -0
  62. package/dist/mcp/tools/result.js +31 -0
  63. package/dist/mcp/tools/validate-rules.js +28 -0
  64. package/dist/utils/config.js +117 -0
  65. package/dist/utils/errors.js +9 -0
  66. package/dist/utils/logger.js +19 -0
  67. package/dist/utils/pace.js +19 -0
  68. package/dist/utils/text.js +22 -0
  69. package/dist/utils/tokens.js +14 -0
  70. package/package.json +69 -4
@@ -0,0 +1,485 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { performance } from 'node:perf_hooks';
4
+ import { compareText } from '../../utils/text.js';
5
+ import { createPacer } from '../../utils/pace.js';
6
+ const LANGUAGE_BY_EXT = {
7
+ '.ts': 'typescript',
8
+ '.mts': 'typescript',
9
+ '.cts': 'typescript',
10
+ '.tsx': 'tsx',
11
+ '.js': 'javascript',
12
+ '.mjs': 'javascript',
13
+ '.cjs': 'javascript',
14
+ '.jsx': 'javascript',
15
+ '.py': 'python',
16
+ };
17
+ export const GENERATED = /\.d\.[cm]?ts$|\.min\.[cm]?js$/;
18
+ export const languageOf = (file) => LANGUAGE_BY_EXT[path.extname(file).toLowerCase()];
19
+ // Files that can import code but are not analysed here: an unseen importer means "nobody imports this" cannot be certain.
20
+ export const FOREIGN_SOURCE = new Set(['.vue', '.svelte', '.astro', '.mdx']);
21
+ // ---- Memory model --------------------------------------------------------------------------------------------------------------
22
+ //
23
+ // An entry-count limit alone says nothing about memory: a million entries with 255-character names hold several times what a million
24
+ // short names do. Discovery therefore keeps a running ESTIMATE of what it holds and stops, reporting 'memory-limit', before the
25
+ // estimate would pass the budget. The estimate is computed from names and counts only (never from the heap), so the same directory
26
+ // content gives the same estimate on every run. The constants below were measured on V8 (64-bit, no pointer compression) and rounded
27
+ // UP; names are charged at two bytes per UTF-16 unit, the worst case for a string.
28
+ // listing item {name, kind} object + string header + array slot ITEM_FIXED_BYTES + 2 * name.length (measured <= 66 + 2n)
29
+ // path string a retained relative path (pending directories, result lists) STRING_FIXED_BYTES + 2 * length (measured <= 40 + 2n)
30
+ // candidate {rel, lang, size, mtimeMs} + its path + allowance for the CANDIDATE_FIXED_BYTES + 2 * length (measured <= 82 + 2n)
31
+ // final sort of `eligible`
32
+ // sorting while a listing buffer is sorted or merged, the original array, the runs and the merge output are alive together
33
+ // (three pointer arrays) plus garbage not yet collected; SORT_BYTES_PER_ENTRY is reserved for EVERY buffered entry
34
+ // at all times, so a sort is always affordable when it is needed.
35
+ // fixed directory read batches (1024 Dirents), the result object, the pacer: FIXED_OVERHEAD_BYTES.
36
+ // What is charged: the listing buffer of the directory being read (and, once read, its entries until the directory is processed),
37
+ // the sort allowance for them, the directories still to visit, `eligible` and every other retained result list.
38
+ // What is not: the memory of the rest of the process, and the allocations of one file system call.
39
+ export const ITEM_FIXED_BYTES = 80;
40
+ export const STRING_FIXED_BYTES = 40;
41
+ export const CANDIDATE_FIXED_BYTES = 128;
42
+ export const SORT_BYTES_PER_ENTRY = 48;
43
+ export const FIXED_OVERHEAD_BYTES = 1024 * 1024;
44
+ export const DEFAULT_DISCOVERY_MEMORY_BYTES = 128 * 1024 * 1024;
45
+ export const MAX_DISCOVERY_MEMORY_BYTES = 256 * 1024 * 1024;
46
+ export const estimateItemBytes = (name) => ITEM_FIXED_BYTES + 2 * name.length;
47
+ export const estimatePathBytes = (rel) => STRING_FIXED_BYTES + 2 * rel.length;
48
+ export const estimateCandidateBytes = (rel) => CANDIDATE_FIXED_BYTES + 2 * rel.length;
49
+ class MemoryMeter {
50
+ limit;
51
+ // Directories still to visit and every retained result.
52
+ retained = FIXED_OVERHEAD_BYTES;
53
+ // Entries of the directory being processed, after it was listed.
54
+ listing = 0;
55
+ peak = FIXED_OVERHEAD_BYTES;
56
+ constructor(limit) {
57
+ this.limit = Number.isFinite(limit) ? Math.min(Math.max(limit, 0), MAX_DISCOVERY_MEMORY_BYTES) : DEFAULT_DISCOVERY_MEMORY_BYTES;
58
+ }
59
+ get total() {
60
+ return this.retained + this.listing;
61
+ }
62
+ fits(extra) {
63
+ return this.total + extra <= this.limit;
64
+ }
65
+ observe(extra = 0) {
66
+ const now = this.total + extra;
67
+ if (now > this.peak)
68
+ this.peak = now;
69
+ }
70
+ }
71
+ // What is kept of a directory entry while it is listed: the name and its kind, nothing else. A fs.Dirent also carries the
72
+ // parent path and other fields, which for a directory of hundreds of thousands of files is memory that is never used.
73
+ const FILE = 0;
74
+ const DIRECTORY = 1;
75
+ const SYMLINK = 2;
76
+ // A socket, FIFO or device: known to be neither a file nor a directory, and never indexed.
77
+ const OTHER = 3;
78
+ // The listing gave no type (some file systems and network mounts report DT_UNKNOWN). The entry may be a file or a directory, so
79
+ // it is classified with lstat, which never follows a symbolic link.
80
+ const UNKNOWN = 4;
81
+ const kindOf = (e) => e.isSymbolicLink()
82
+ ? SYMLINK
83
+ : e.isDirectory()
84
+ ? DIRECTORY
85
+ : e.isFile()
86
+ ? FILE
87
+ : e.isSocket() || e.isFIFO() || e.isCharacterDevice() || e.isBlockDevice()
88
+ ? OTHER
89
+ : UNKNOWN;
90
+ const kindOfStats = (s) => (s.isSymbolicLink() ? SYMLINK : s.isDirectory() ? DIRECTORY : s.isFile() ? FILE : OTHER);
91
+ const byName = (a, b) => compareText(a.name, b.name);
92
+ // Sorting is done in bounded slices so that a directory with hundreds of thousands of entries never holds the event loop
93
+ // for one long Array.prototype.sort: each chunk is sorted on its own, then the sorted runs are merged with a checkpoint
94
+ // every few thousand steps. Names are unique inside a directory, so the order is total and identical to a plain sort.
95
+ const SORT_CHUNK = 4096;
96
+ const MERGE_STEP = 2048;
97
+ async function mergeRuns(a, b, checkpoint) {
98
+ const out = new Array(a.length + b.length);
99
+ let i = 0;
100
+ let j = 0;
101
+ let k = 0;
102
+ while (i < a.length || j < b.length) {
103
+ out[k++] = j >= b.length || (i < a.length && byName(a[i], b[j]) <= 0) ? a[i++] : b[j++];
104
+ if ((k & (MERGE_STEP - 1)) === 0)
105
+ await checkpoint();
106
+ }
107
+ return out;
108
+ }
109
+ export async function sortEntries(entries, checkpoint) {
110
+ if (entries.length <= SORT_CHUNK)
111
+ return entries.sort(byName);
112
+ let runs = [];
113
+ for (let i = 0; i < entries.length; i += SORT_CHUNK) {
114
+ runs.push(entries.slice(i, i + SORT_CHUNK).sort(byName));
115
+ await checkpoint();
116
+ }
117
+ while (runs.length > 1) {
118
+ const next = [];
119
+ for (let i = 0; i < runs.length; i += 2)
120
+ next.push(i + 1 < runs.length ? await mergeRuns(runs[i], runs[i + 1], checkpoint) : runs[i]);
121
+ runs = next;
122
+ }
123
+ return runs[0] ?? [];
124
+ }
125
+ // ---- Opening a directory without trusting the path -----------------------------------------------------------------------------
126
+ //
127
+ // A directory that was listed as a directory can be replaced by a symbolic link before it is opened, which would make the walk list
128
+ // (and descend into) a directory outside the workspace. Checking with lstat and then opening by path leaves a gap between the two
129
+ // calls, so the checks are layered:
130
+ // 1. lstat the path: it must be a directory and not a link. Its device and inode are remembered.
131
+ // 2. Where the platform has O_NOFOLLOW (Linux, macOS), open the directory descriptor with O_NOFOLLOW | O_DIRECTORY: the kernel
132
+ // refuses a link in the final component atomically. fstat the descriptor: it must be the object seen in step 1.
133
+ // 3. On Linux the listing is read through /proc/self/fd/<fd>, that is, from the verified object itself, whatever happens to the
134
+ // path afterwards. Its real path (readlink) must be inside the workspace.
135
+ // 4. After the listing, the path is checked again: still a directory, not a link, same object, still inside the workspace. If not,
136
+ // the whole listing is discarded and the directory is reported as replaced.
137
+ // Node has no openat or fdopendir, so on macOS and Windows the listing itself is still opened by path; there the checks in 1 and 4
138
+ // bracket it and a replacement that is undone within the window is not detectable. File contents are never read through this path:
139
+ // readInside refuses links and paths outside the workspace on its own.
140
+ class DirectoryReplaced extends Error {
141
+ constructor(message) {
142
+ super(message);
143
+ this.name = 'DirectoryReplaced';
144
+ }
145
+ }
146
+ const NOFOLLOW = typeof fs.constants.O_NOFOLLOW === 'number' && process.platform !== 'win32';
147
+ const OPEN_DIRECTORY = fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW ?? 0) | (fs.constants.O_DIRECTORY ?? 0);
148
+ const sameObject = (a, b) => a.isDirectory() === b.isDirectory() && a.dev === b.dev && a.ino === b.ino;
149
+ const insideRoot = (real, rootReal) => real === rootReal || real.startsWith(rootReal.endsWith(path.sep) ? rootReal : rootReal + path.sep);
150
+ const codeOf = (err) => err?.code;
151
+ async function openVerifiedDirectory(rootReal, abs) {
152
+ const before = fs.lstatSync(abs);
153
+ if (before.isSymbolicLink() || !before.isDirectory())
154
+ throw new DirectoryReplaced('no longer a directory');
155
+ let fd;
156
+ let listPath = abs;
157
+ let real;
158
+ try {
159
+ if (NOFOLLOW) {
160
+ try {
161
+ fd = fs.openSync(abs, OPEN_DIRECTORY);
162
+ }
163
+ catch (err) {
164
+ // ELOOP: the final component is now a link. ENOTDIR: it is no longer a directory.
165
+ if (codeOf(err) === 'ELOOP' || codeOf(err) === 'ENOTDIR')
166
+ throw new DirectoryReplaced('replaced while being opened');
167
+ throw err;
168
+ }
169
+ if (!sameObject(before, fs.fstatSync(fd)))
170
+ throw new DirectoryReplaced('a different directory was opened');
171
+ const viaFd = `/proc/self/fd/${fd}`;
172
+ if (process.platform === 'linux' && fs.existsSync(viaFd)) {
173
+ listPath = viaFd;
174
+ real = fs.readlinkSync(viaFd);
175
+ }
176
+ else {
177
+ real = fs.realpathSync(abs);
178
+ }
179
+ }
180
+ else {
181
+ real = fs.realpathSync(abs);
182
+ }
183
+ if (!insideRoot(real, rootReal))
184
+ throw new DirectoryReplaced('resolves outside the workspace');
185
+ const dir = await fs.promises.opendir(listPath, { bufferSize: 1024 });
186
+ const opened = fd;
187
+ return {
188
+ dir,
189
+ close: () => {
190
+ if (opened !== undefined) {
191
+ try {
192
+ fs.closeSync(opened);
193
+ }
194
+ catch {
195
+ // already closed
196
+ }
197
+ }
198
+ },
199
+ verify: () => {
200
+ const after = fs.lstatSync(abs);
201
+ if (after.isSymbolicLink() || !after.isDirectory() || !sameObject(before, after))
202
+ throw new DirectoryReplaced('replaced while being listed');
203
+ if (!insideRoot(fs.realpathSync(abs), rootReal))
204
+ throw new DirectoryReplaced('resolves outside the workspace');
205
+ },
206
+ };
207
+ }
208
+ catch (err) {
209
+ if (fd !== undefined) {
210
+ try {
211
+ fs.closeSync(fd);
212
+ }
213
+ catch {
214
+ // already closed
215
+ }
216
+ }
217
+ throw err;
218
+ }
219
+ }
220
+ // Thrown by the checkpoint while a directory is being listed or sorted, when maxMs has run out.
221
+ const OUT_OF_TIME = Symbol('discovery-out-of-time');
222
+ // Thrown while a directory is being listed when what has to be held would pass the memory budget.
223
+ const OUT_OF_MEMORY = Symbol('discovery-out-of-memory');
224
+ // Lists one directory without a single blocking readdir call: entries arrive in batches from the thread pool, and every
225
+ // batch passes a checkpoint (cancellation, deadline, event-loop yield).
226
+ //
227
+ // Which names come first in name order cannot be known before the whole directory has been read, so every entry is read
228
+ // (and counted in `stats.listed`); what is bounded is how many are kept. At most `keep` entries are retained, the first
229
+ // `keep` in name order, which is all the entry limit can ever reach. Once `keep` names have been collected, the largest of
230
+ // them is a cutoff: an entry that sorts after it can never be selected and is dropped on arrival instead of buffered. The
231
+ // buffer is sorted and cut back to `keep` whenever it grows by half of `keep` (at least 8192), so its size stays within
232
+ // `1.5 * keep + 8192` entries however large the directory is, and in the usual case stays close to `keep`.
233
+ // The selection is exactly the first `keep` names in name order, whatever order the file system lists them in.
234
+ //
235
+ // The buffer is also bounded in BYTES (see "Memory model"). Before an entry is buffered, the estimate of the buffer, its sort
236
+ // allowance and the rest of the walk must fit the budget. If it does not, the buffer is cut back early when that frees at least
237
+ // MIN_RECLAIM entries (so a sort is never repeated for a handful of entries). If that is not possible either, the directory cannot
238
+ // be selected from within the budget: OUT_OF_MEMORY is thrown and nothing from this directory is used. Nothing is dropped silently:
239
+ // the caller reports 'memory-limit'.
240
+ async function listDirectory(rootReal, abs, keep, checkpoint, stats, meter) {
241
+ const trimAt = keep + Math.max(keep >> 1, 8192);
242
+ const minReclaim = Math.max(8192, keep >> 3);
243
+ let buffer = [];
244
+ let bufferBytes = 0;
245
+ let cutoff;
246
+ const sizeOf = (items) => {
247
+ let total = 0;
248
+ for (const item of items)
249
+ total += estimateItemBytes(item.name);
250
+ return total;
251
+ };
252
+ const trim = async () => {
253
+ buffer = await sortEntries(buffer, checkpoint);
254
+ if (buffer.length > keep)
255
+ buffer.length = keep;
256
+ bufferBytes = sizeOf(buffer);
257
+ if (buffer.length === keep)
258
+ cutoff = buffer[keep - 1].name;
259
+ };
260
+ // Bytes held if one more entry of `cost` is buffered: the buffer, the item, and the sort allowance for all of them.
261
+ const needed = (cost) => bufferBytes + cost + SORT_BYTES_PER_ENTRY * (buffer.length + 1);
262
+ const opened = await openVerifiedDirectory(rootReal, abs);
263
+ let seen = 0;
264
+ try {
265
+ for await (const entry of opened.dir) {
266
+ stats.listed++;
267
+ if ((++seen & 255) === 0)
268
+ await checkpoint();
269
+ if (cutoff !== undefined && compareText(entry.name, cutoff) > 0)
270
+ continue;
271
+ const cost = estimateItemBytes(entry.name);
272
+ if (!meter.fits(needed(cost))) {
273
+ const before = buffer.length;
274
+ if (before > keep)
275
+ await trim();
276
+ // The cutoff may have just been set or lowered: an entry beyond it is not wanted after all.
277
+ if (cutoff !== undefined && compareText(entry.name, cutoff) > 0)
278
+ continue;
279
+ if (before - buffer.length < minReclaim || !meter.fits(needed(cost)))
280
+ throw OUT_OF_MEMORY;
281
+ }
282
+ buffer.push({ name: entry.name, kind: kindOf(entry) });
283
+ bufferBytes += cost;
284
+ meter.observe(bufferBytes + SORT_BYTES_PER_ENTRY * buffer.length);
285
+ if (buffer.length > stats.peakListingBuffer)
286
+ stats.peakListingBuffer = buffer.length;
287
+ if (buffer.length >= trimAt)
288
+ await trim();
289
+ }
290
+ // Everything listed is only trusted if the path still leads to the directory that was opened.
291
+ opened.verify();
292
+ }
293
+ finally {
294
+ opened.close();
295
+ }
296
+ buffer = await sortEntries(buffer, checkpoint);
297
+ if (buffer.length > keep)
298
+ buffer.length = keep;
299
+ return { items: buffer, bytes: sizeOf(buffer) };
300
+ }
301
+ // Directories are visited in sorted order, so which entries are seen before a limit is hit does not depend on the
302
+ // order the operating system lists them. The entry limit is therefore deterministic; the time limit cannot be.
303
+ // The deadline is enforced while a directory is listed, sorted and processed entry by entry, not only between
304
+ // directories, and the walk hands the event loop back regularly and throws the signal's reason when cancelled.
305
+ export async function walk(env, limits) {
306
+ const result = { eligible: [], oversized: [], unreadableDirs: [], configs: [], manifests: [], legacy: [], foreign: new Set(), unclassified: [], replacedDirs: [], vanished: [], listed: 0, entries: 0, peakListingBuffer: 0, memory: { limitBytes: 0, peakEstimatedBytes: 0 } };
307
+ const meter = new MemoryMeter(limits.maxMemoryBytes ?? DEFAULT_DISCOVERY_MEMORY_BYTES);
308
+ result.memory.limitBytes = meter.limit;
309
+ // Every retained string is charged when it is stored, so the budget covers the result lists as well as the pending directories.
310
+ const retainPath = (list, rel) => {
311
+ list.push(rel);
312
+ meter.retained += estimatePathBytes(rel);
313
+ meter.observe();
314
+ };
315
+ const pacer = createPacer({ signal: limits.signal });
316
+ const deadline = performance.now() + limits.maxMs;
317
+ const timeUp = () => performance.now() > deadline;
318
+ const checkpoint = async () => {
319
+ await pacer.tick();
320
+ if (timeUp())
321
+ throw OUT_OF_TIME;
322
+ };
323
+ const pending = [''];
324
+ // Every path below is built from the real path of the root, so a root that is itself a link is resolved once, up front.
325
+ let rootReal = env.root;
326
+ try {
327
+ rootReal = fs.realpathSync(env.root);
328
+ }
329
+ catch {
330
+ // an unreadable root is reported as an unreadable directory below
331
+ }
332
+ scan: while (pending.length > 0) {
333
+ await pacer.tick();
334
+ if (timeUp()) {
335
+ result.stopped = 'time-limit';
336
+ break;
337
+ }
338
+ const dir = pending.pop();
339
+ if (dir)
340
+ meter.retained -= estimatePathBytes(dir);
341
+ let entries;
342
+ try {
343
+ // One more than the budget left, so that exceeding the entry limit is still detected after trimming.
344
+ const listed = await listDirectory(rootReal, path.join(rootReal, dir), limits.maxEntries - result.entries + 1, checkpoint, result, meter);
345
+ entries = listed.items;
346
+ meter.listing = listed.bytes;
347
+ meter.observe();
348
+ }
349
+ catch (err) {
350
+ if (err === OUT_OF_TIME) {
351
+ result.stopped = 'time-limit';
352
+ break;
353
+ }
354
+ // The directory could not be selected from within the budget. Nothing of it is used, and the scan says it stopped.
355
+ if (err === OUT_OF_MEMORY) {
356
+ result.stopped = 'memory-limit';
357
+ break;
358
+ }
359
+ // Cancellation is never reported as an unreadable directory.
360
+ if (limits.signal?.aborted)
361
+ throw err;
362
+ if (err instanceof DirectoryReplaced)
363
+ retainPath(result.replacedDirs, dir || '.');
364
+ // A subdirectory that no longer exists was removed during the scan: nothing is hidden by that. A root that cannot be read is a gap.
365
+ else if (codeOf(err) === 'ENOENT' && dir !== '')
366
+ retainPath(result.vanished, dir);
367
+ else
368
+ retainPath(result.unreadableDirs, dir || '.');
369
+ continue;
370
+ }
371
+ const subdirs = [];
372
+ let sinceTick = 0;
373
+ for (const entry of entries) {
374
+ if (timeUp()) {
375
+ result.stopped = 'time-limit';
376
+ break scan;
377
+ }
378
+ // The entry that would exceed the limit proves there is more to see, but it is not processed and not counted.
379
+ if (result.entries >= limits.maxEntries) {
380
+ result.stopped = 'entry-limit';
381
+ break scan;
382
+ }
383
+ const rel = dir ? `${dir}/${entry.name}` : entry.name;
384
+ // Room for the most one entry can retain (a candidate, plus its path in `legacy`) before it is looked at. Like the entry limit,
385
+ // the entry that does not fit is not processed and not counted; everything before it, in sorted order, is.
386
+ if (!meter.fits(estimateCandidateBytes(rel) + estimatePathBytes(rel))) {
387
+ result.stopped = 'memory-limit';
388
+ break scan;
389
+ }
390
+ result.entries++;
391
+ if ((++sinceTick & 31) === 0)
392
+ await pacer.tick();
393
+ if (entry.kind === SYMLINK)
394
+ continue;
395
+ let kind = entry.kind;
396
+ if (kind === UNKNOWN) {
397
+ try {
398
+ kind = kindOfStats(fs.lstatSync(path.join(rootReal, rel)));
399
+ }
400
+ catch (err) {
401
+ // ENOENT: it was removed after it was listed. Any other error (permissions, I/O): neither the listing nor lstat could say what
402
+ // this is, so it cannot be ruled out as a source file or a directory.
403
+ if (codeOf(err) === 'ENOENT')
404
+ retainPath(result.vanished, rel);
405
+ else
406
+ retainPath(result.unclassified, rel);
407
+ continue;
408
+ }
409
+ if (kind === SYMLINK)
410
+ continue;
411
+ }
412
+ if (kind === DIRECTORY) {
413
+ if (!env.ignores(`${rel}/`)) {
414
+ subdirs.push(rel);
415
+ meter.retained += estimatePathBytes(rel);
416
+ meter.observe();
417
+ }
418
+ continue;
419
+ }
420
+ if (kind !== FILE || env.ignores(rel))
421
+ continue;
422
+ if (env.isTsConfig(entry.name)) {
423
+ retainPath(result.configs, rel);
424
+ continue;
425
+ }
426
+ if (entry.name === 'package.json' || entry.name === 'pyproject.toml') {
427
+ retainPath(result.manifests, rel);
428
+ continue;
429
+ }
430
+ // setup.cfg / setup.py can declare console entry points that are not read; setup.py is still indexed as source.
431
+ if (entry.name === 'setup.cfg' || entry.name === 'setup.py') {
432
+ retainPath(result.legacy, rel);
433
+ if (entry.name === 'setup.cfg')
434
+ continue;
435
+ }
436
+ const foreignExt = path.extname(entry.name).toLowerCase();
437
+ if (FOREIGN_SOURCE.has(foreignExt))
438
+ result.foreign.add(foreignExt);
439
+ const lang = languageOf(entry.name);
440
+ if (!lang || GENERATED.test(entry.name) || env.isSensitive(rel))
441
+ continue;
442
+ let stat;
443
+ try {
444
+ // lstat, not stat: a file swapped for a symbolic link after it was listed is not followed.
445
+ stat = fs.lstatSync(path.join(rootReal, rel));
446
+ }
447
+ catch (err) {
448
+ // A source file that cannot be examined is never dropped silently: ENOENT means it is gone (recorded, no gap); anything else
449
+ // (permissions, I/O error) means it may be a source file the scan could not see, and the scan is reported as partial.
450
+ if (codeOf(err) === 'ENOENT')
451
+ retainPath(result.vanished, rel);
452
+ else
453
+ retainPath(result.unclassified, rel);
454
+ continue;
455
+ }
456
+ // Listed as a file, no longer one: replaced by a link or a directory since. It is not followed, and the scan says it did not see it.
457
+ if (!stat.isFile()) {
458
+ retainPath(result.unclassified, rel);
459
+ continue;
460
+ }
461
+ if (stat.size === 0)
462
+ continue;
463
+ if (stat.size > env.maxBytes) {
464
+ retainPath(result.oversized, rel);
465
+ continue;
466
+ }
467
+ result.eligible.push({ rel, lang, size: stat.size, mtimeMs: Math.floor(stat.mtimeMs) });
468
+ meter.retained += estimateCandidateBytes(rel);
469
+ meter.observe();
470
+ }
471
+ // The directory is done: its entries are no longer held.
472
+ meter.listing = 0;
473
+ // Reversed so that popping from the end visits subdirectories in sorted order.
474
+ for (let i = subdirs.length - 1; i >= 0; i--)
475
+ pending.push(subdirs[i]);
476
+ }
477
+ result.memory.peakEstimatedBytes = meter.peak;
478
+ result.eligible.sort((a, b) => compareText(a.rel, b.rel));
479
+ result.oversized.sort(compareText);
480
+ result.unreadableDirs.sort(compareText);
481
+ result.unclassified.sort(compareText);
482
+ result.replacedDirs.sort(compareText);
483
+ result.vanished.sort(compareText);
484
+ return result;
485
+ }