checkchange 0.4.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.
- package/LICENSE +21 -0
- package/README.md +150 -0
- package/dist/attribution.d.ts +19 -0
- package/dist/attribution.js +175 -0
- package/dist/auto-coverage.d.ts +8 -0
- package/dist/auto-coverage.js +93 -0
- package/dist/cache.d.ts +70 -0
- package/dist/cache.js +458 -0
- package/dist/cli.d.ts +51 -0
- package/dist/cli.js +810 -0
- package/dist/complexity-providers/pythonASTComplexityProvider.d.ts +9 -0
- package/dist/complexity-providers/pythonASTComplexityProvider.js +148 -0
- package/dist/complexity-providers/pythonDescriptorProvider.d.ts +17 -0
- package/dist/complexity-providers/pythonDescriptorProvider.js +119 -0
- package/dist/complexity-providers.d.ts +6 -0
- package/dist/complexity-providers.js +1 -0
- package/dist/complexity.d.ts +14 -0
- package/dist/complexity.js +75 -0
- package/dist/coverage-providers/lcovProvider.d.ts +12 -0
- package/dist/coverage-providers/lcovProvider.js +126 -0
- package/dist/coverage.d.ts +28 -0
- package/dist/coverage.js +451 -0
- package/dist/crap.d.ts +31 -0
- package/dist/crap.js +84 -0
- package/dist/crapCalc.d.ts +5 -0
- package/dist/crapCalc.js +11 -0
- package/dist/delta.d.ts +100 -0
- package/dist/delta.js +214 -0
- package/dist/evidence.d.ts +201 -0
- package/dist/evidence.js +906 -0
- package/dist/execute.d.ts +59 -0
- package/dist/execute.js +92 -0
- package/dist/formatters/github.d.ts +5 -0
- package/dist/formatters/github.js +27 -0
- package/dist/formatters/index.d.ts +47 -0
- package/dist/formatters/index.js +14 -0
- package/dist/formatters/junit.d.ts +5 -0
- package/dist/formatters/junit.js +32 -0
- package/dist/formatters/sarif.d.ts +5 -0
- package/dist/formatters/sarif.js +35 -0
- package/dist/fs-safety.d.ts +10 -0
- package/dist/fs-safety.js +47 -0
- package/dist/git.d.ts +50 -0
- package/dist/git.js +137 -0
- package/dist/help.d.ts +40 -0
- package/dist/help.js +274 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/providers/config.d.ts +41 -0
- package/dist/providers/config.js +172 -0
- package/dist/providers/genericCommand.d.ts +3 -0
- package/dist/providers/genericCommand.js +89 -0
- package/dist/providers/index.d.ts +7 -0
- package/dist/providers/index.js +4 -0
- package/dist/providers/prepare.d.ts +124 -0
- package/dist/providers/prepare.js +459 -0
- package/dist/providers/runner-detection.d.ts +30 -0
- package/dist/providers/runner-detection.js +207 -0
- package/dist/rules.d.ts +27 -0
- package/dist/rules.js +54 -0
- package/package.json +52 -0
package/dist/cache.js
ADDED
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
// Incremental analysis cache (Experiment C, task C-3).
|
|
2
|
+
// Deterministic two-tier key cache: per-file complexity entries, per-run
|
|
3
|
+
// coverage entries, whole-run shard records. Never stale: ANY uncertainty
|
|
4
|
+
// (missing file, parse failure, key mismatch, oversized entry, guard failure,
|
|
5
|
+
// expired TTL) is a miss and the caller recomputes. Failures fall back to
|
|
6
|
+
// fresh computation; cache IO never throws and never logs. (mem:44542 §8)
|
|
7
|
+
import * as path from 'node:path';
|
|
8
|
+
import * as fsSync from 'node:fs';
|
|
9
|
+
import { createHash } from 'node:crypto';
|
|
10
|
+
import { access, constants, mkdir, readFile, readdir, rename, rm, stat, unlink, utimes, writeFile, realpath, chmod } from 'node:fs/promises';
|
|
11
|
+
import { openWithinRoot } from './fs-safety.js';
|
|
12
|
+
import { findAllTypeScriptFilesUnderSourceRoots, parseFileMethods } from '@barney-media/crap-typescript-core';
|
|
13
|
+
import { getGitTrackedCodeFiles, complexityProvenance } from './complexity.js';
|
|
14
|
+
import { readCoverage, coverageProvenance } from './coverage.js';
|
|
15
|
+
import { registerProvider, providerRegistry, getProvider } from './evidence.js';
|
|
16
|
+
export const CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 7 days, mtime-based (no clock in key)
|
|
17
|
+
export const MAX_CACHE_TOTAL_BYTES = 500 * 1024 * 1024; // 500MB total, LRU-pruned on write-through
|
|
18
|
+
export const MAX_CACHE_ENTRY_BYTES = 10 * 1024 * 1024; // 10MB per entry file; larger reads are misses
|
|
19
|
+
const TMP_STALE_MS = 24 * 60 * 60 * 1000; // tmp.<pid> leftovers older than this are swept
|
|
20
|
+
function sha256Hex(content) {
|
|
21
|
+
return createHash('sha256').update(content).digest('hex');
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Containment guard: candidate must resolve (realpath) inside root. Handles
|
|
25
|
+
* symlinked roots (macOS /tmp -> /private/tmp); realpath failure falls back to a
|
|
26
|
+
* lexical check, which is also what makes the check usable BEFORE the path
|
|
27
|
+
* exists (a not-yet-created cache `entries/` dir still passes).
|
|
28
|
+
*
|
|
29
|
+
* Best-effort, not a proof: the realpath arm and the caller's subsequent
|
|
30
|
+
* mkdir/chmod/write are separate operations, so a swap between them is not
|
|
31
|
+
* covered here. Callers must still validate every path they are about to use.
|
|
32
|
+
*/
|
|
33
|
+
async function isWithin(candidate, root) {
|
|
34
|
+
try {
|
|
35
|
+
const realCandidate = await realpath(candidate);
|
|
36
|
+
const realRoot = await realpath(root);
|
|
37
|
+
const rel = path.relative(realRoot, realCandidate);
|
|
38
|
+
return !rel.startsWith('..') && !path.isAbsolute(rel);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
const rel = path.relative(root, candidate);
|
|
42
|
+
return !rel.startsWith('..') && !path.isAbsolute(rel);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Cache root resolution and the allowed-root rule:
|
|
47
|
+
* - Default: realpath(cwd)/.checkchange/cache — always inside the analyzed repo.
|
|
48
|
+
* - CHECKCHANGE_CACHE_DIR override: must be an ABSOLUTE path whose realpath
|
|
49
|
+
* resolves. It MAY live outside cwd (operator explicitly opts in, e.g. a CI
|
|
50
|
+
* shared cache dir); "escaping the allowed root" therefore means an ENTRY
|
|
51
|
+
* escaping the resolved root — every cache path is isWithin-checked against
|
|
52
|
+
* the resolved root on read and write, so symlinked or swapped entries that
|
|
53
|
+
* resolve outside it are rejected as misses ('guard'). Relative overrides and
|
|
54
|
+
* unresolvable paths are rejected outright (null => caching disabled).
|
|
55
|
+
*/
|
|
56
|
+
export function resolveCacheRoot(cwd, env = process.env) {
|
|
57
|
+
const override = env.CHECKCHANGE_CACHE_DIR;
|
|
58
|
+
try {
|
|
59
|
+
if (override !== undefined && override !== '') {
|
|
60
|
+
if (!path.isAbsolute(override))
|
|
61
|
+
return null;
|
|
62
|
+
return fsSync.realpathSync(override);
|
|
63
|
+
}
|
|
64
|
+
return path.join(fsSync.realpathSync(cwd), '.checkchange', 'cache');
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// ---- Key derivation (deterministic; fixed property order in all templates) ----
|
|
71
|
+
/** Tier (a): per-file complexity entry key = sha256(fileContent) + provider version, plus optional changed-file scope. */
|
|
72
|
+
export function complexityFileKey(fileContent, providerVersion, changedFilesHash) {
|
|
73
|
+
const base = `${sha256Hex(fileContent)}:${providerVersion}`;
|
|
74
|
+
return changedFilesHash === undefined ? base : `${base}:changed:${changedFilesHash}`;
|
|
75
|
+
}
|
|
76
|
+
/** Tier (b): per-run coverage entry key = coverage artifact hash + ':' + provider version. */
|
|
77
|
+
export function coverageEntryKey(coverageHash, providerVersion) {
|
|
78
|
+
return `${coverageHash}:${providerVersion}`;
|
|
79
|
+
}
|
|
80
|
+
/** Whole-run shard: sha256 over canonical JSON, sliced to 12 hex chars for the filename. */
|
|
81
|
+
export function runShardKey(inputs) {
|
|
82
|
+
const canonical = JSON.stringify({
|
|
83
|
+
base: inputs.base,
|
|
84
|
+
coverageHash: inputs.coverageHash,
|
|
85
|
+
engine: inputs.engine,
|
|
86
|
+
fileKeys: [...inputs.fileKeys].sort(),
|
|
87
|
+
intervalsSha256: inputs.intervalsSha256,
|
|
88
|
+
threshold: String(inputs.threshold),
|
|
89
|
+
});
|
|
90
|
+
return sha256Hex(canonical).slice(0, 12);
|
|
91
|
+
}
|
|
92
|
+
function describeKey(root, key) {
|
|
93
|
+
if (key.kind === 'file') {
|
|
94
|
+
const keyString = complexityFileKey(key.fileContent, key.providerVersion, key.changedFilesHash);
|
|
95
|
+
return { filePath: path.join(root, 'entries', `${sha256Hex(keyString)}.json`), keyString };
|
|
96
|
+
}
|
|
97
|
+
if (key.kind === 'coverage') {
|
|
98
|
+
const keyString = coverageEntryKey(key.coverageHash, key.providerVersion);
|
|
99
|
+
return { filePath: path.join(root, 'entries', `${sha256Hex(keyString)}.json`), keyString };
|
|
100
|
+
}
|
|
101
|
+
const shard = runShardKey(key);
|
|
102
|
+
return { filePath: path.join(root, 'runs', `${shard}.json`), keyString: `run:${shard}` };
|
|
103
|
+
}
|
|
104
|
+
// ---- Storage ----
|
|
105
|
+
async function bestEffortUnlink(p) {
|
|
106
|
+
try {
|
|
107
|
+
await unlink(p);
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async function readEntry(filePath, keyString, root) {
|
|
115
|
+
let handle;
|
|
116
|
+
try {
|
|
117
|
+
handle = await openWithinRoot(root, filePath);
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
if (error.code === 'ENOENT' || error.code === 'ENOTDIR') {
|
|
121
|
+
return { ok: false, reason: 'absent' };
|
|
122
|
+
}
|
|
123
|
+
if (error instanceof Error && error.message.startsWith('openWithinRoot: candidate')) {
|
|
124
|
+
return { ok: false, reason: 'guard' };
|
|
125
|
+
}
|
|
126
|
+
return { ok: false, reason: 'corrupt' };
|
|
127
|
+
}
|
|
128
|
+
let raw;
|
|
129
|
+
try {
|
|
130
|
+
const st = await handle.stat();
|
|
131
|
+
if (st.size > MAX_CACHE_ENTRY_BYTES) {
|
|
132
|
+
await bestEffortUnlink(filePath);
|
|
133
|
+
return { ok: false, reason: 'oversized' };
|
|
134
|
+
}
|
|
135
|
+
if (Date.now() - st.mtimeMs > CACHE_TTL_MS) {
|
|
136
|
+
await bestEffortUnlink(filePath);
|
|
137
|
+
return { ok: false, reason: 'expired' };
|
|
138
|
+
}
|
|
139
|
+
raw = await handle.readFile('utf8');
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
return { ok: false, reason: 'corrupt' };
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
await handle.close().catch(() => undefined);
|
|
146
|
+
}
|
|
147
|
+
let parsed = null;
|
|
148
|
+
try {
|
|
149
|
+
const p = JSON.parse(raw);
|
|
150
|
+
if (typeof p === 'object' && p !== null && 'key' in p && 'value' in p) {
|
|
151
|
+
const rec = p;
|
|
152
|
+
if (typeof rec.key === 'string')
|
|
153
|
+
parsed = { key: rec.key, value: rec.value };
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
// fall through to corrupt
|
|
158
|
+
}
|
|
159
|
+
if (parsed === null || parsed.key !== keyString) {
|
|
160
|
+
await bestEffortUnlink(filePath);
|
|
161
|
+
return { ok: false, reason: 'corrupt' };
|
|
162
|
+
}
|
|
163
|
+
return { ok: true, value: parsed.value };
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Atomic write: tmp.<pid> (0600) + rename; containment-checked; never throws.
|
|
167
|
+
*
|
|
168
|
+
* Order matters: containment of the parent dir is settled BEFORE the first
|
|
169
|
+
* mutating op, so a `filePath` whose parent resolves outside `root` returns
|
|
170
|
+
* false having created nothing (no mkdir, no chmod, no tmp file). Both rename
|
|
171
|
+
* endpoints are re-checked after the write because either can be swapped
|
|
172
|
+
* between the precheck and the rename. The guard is best-effort — see isWithin.
|
|
173
|
+
*
|
|
174
|
+
* `flag: 'wx'` is the other half of that ordering: O_EXCL fails the tmp create
|
|
175
|
+
* with EEXIST when anything already holds that name, so a symlink planted at
|
|
176
|
+
* tmp is never written THROUGH — the link target is untouched. The catch arm
|
|
177
|
+
* unlinks the name (unlink never follows a symlink), so a later write retries
|
|
178
|
+
* on a clean slot. A stale tmp left by a crashed run costs one skipped write.
|
|
179
|
+
*/
|
|
180
|
+
async function writeEntry(filePath, root, payload) {
|
|
181
|
+
const tmp = `${filePath}.tmp.${process.pid}`;
|
|
182
|
+
const dir = path.dirname(filePath);
|
|
183
|
+
try {
|
|
184
|
+
if (!(await isWithin(dir, root)))
|
|
185
|
+
return false;
|
|
186
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
187
|
+
// Strict perms regardless of umask: mkdir mode is masked by umask, chmod is not.
|
|
188
|
+
await chmod(dir, 0o700);
|
|
189
|
+
await chmod(root, 0o700); // cache root itself — root passed explicitly, no dirname guessing
|
|
190
|
+
await writeFile(tmp, JSON.stringify(payload), { mode: 0o600, flag: 'wx' });
|
|
191
|
+
await chmod(tmp, 0o600);
|
|
192
|
+
if (!(await isWithin(tmp, root)) || !(await isWithin(filePath, root))) {
|
|
193
|
+
await bestEffortUnlink(tmp);
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
await rename(tmp, filePath);
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
await bestEffortUnlink(tmp);
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** Bump atime AND mtime on hit: LRU ordering + TTL refresh. Best effort. */
|
|
205
|
+
async function bumpTimes(filePath) {
|
|
206
|
+
const now = new Date();
|
|
207
|
+
try {
|
|
208
|
+
await utimes(filePath, now, now);
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
// best effort
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/** TTL sweep + stale-tmp sweep + 500MB LRU prune. Runs on write-through; never throws. */
|
|
215
|
+
async function prune(root) {
|
|
216
|
+
const files = [];
|
|
217
|
+
const now = Date.now();
|
|
218
|
+
for (const dir of [path.join(root, 'entries'), path.join(root, 'runs')]) {
|
|
219
|
+
let names;
|
|
220
|
+
try {
|
|
221
|
+
names = await readdir(dir);
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
for (const name of names) {
|
|
227
|
+
const p = path.join(dir, name);
|
|
228
|
+
try {
|
|
229
|
+
if (!(await isWithin(p, root)))
|
|
230
|
+
continue; // defense in depth: skip paths escaping root
|
|
231
|
+
const st = await stat(p);
|
|
232
|
+
if (!st.isFile())
|
|
233
|
+
continue;
|
|
234
|
+
if (name.includes('.tmp.') && now - st.mtimeMs > TMP_STALE_MS) {
|
|
235
|
+
await bestEffortUnlink(p);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
if (now - st.mtimeMs > CACHE_TTL_MS) {
|
|
239
|
+
await bestEffortUnlink(p);
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
files.push({ p, atimeMs: st.atimeMs, size: st.size });
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
let total = 0;
|
|
250
|
+
for (const f of files)
|
|
251
|
+
total += f.size;
|
|
252
|
+
if (total <= MAX_CACHE_TOTAL_BYTES)
|
|
253
|
+
return;
|
|
254
|
+
files.sort((a, b) => a.atimeMs - b.atimeMs); // least-recently-accessed first
|
|
255
|
+
for (const f of files) {
|
|
256
|
+
if (total <= MAX_CACHE_TOTAL_BYTES)
|
|
257
|
+
break;
|
|
258
|
+
if (await bestEffortUnlink(f.p))
|
|
259
|
+
total -= f.size;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// ---- Public API ----
|
|
263
|
+
/**
|
|
264
|
+
* Lookup `key` in the cache; on miss (or any uncertainty) run `compute`,
|
|
265
|
+
* write through, and return the computed value with `hit: false`.
|
|
266
|
+
* Value must be JSON-serializable (the cache stores `{key, value}` as JSON).
|
|
267
|
+
* Never throws on cache IO; `compute` errors propagate to the caller.
|
|
268
|
+
*/
|
|
269
|
+
export async function getOrCompute(cwd, key, compute) {
|
|
270
|
+
const root = resolveCacheRoot(cwd);
|
|
271
|
+
if (root === null) {
|
|
272
|
+
const value = await compute();
|
|
273
|
+
return { hit: false, value, reason: 'guard' };
|
|
274
|
+
}
|
|
275
|
+
const { filePath, keyString } = describeKey(root, key);
|
|
276
|
+
const read = await readEntry(filePath, keyString, root);
|
|
277
|
+
if (read.ok) {
|
|
278
|
+
await bumpTimes(filePath);
|
|
279
|
+
return { hit: true, value: read.value };
|
|
280
|
+
}
|
|
281
|
+
const value = await compute();
|
|
282
|
+
const wrote = await writeEntry(filePath, root, { key: keyString, value });
|
|
283
|
+
if (wrote)
|
|
284
|
+
await prune(root);
|
|
285
|
+
return { hit: false, value, reason: read.reason };
|
|
286
|
+
}
|
|
287
|
+
/** Wipe the resolved cache dir. Returns false when the root is unusable. */
|
|
288
|
+
export async function clear(cwd, env = process.env) {
|
|
289
|
+
const root = resolveCacheRoot(cwd, env);
|
|
290
|
+
if (root === null)
|
|
291
|
+
return false;
|
|
292
|
+
try {
|
|
293
|
+
await rm(root, { recursive: true, force: true });
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
return false;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
const cacheWarnings = [];
|
|
301
|
+
export function getCacheWarnings() {
|
|
302
|
+
return cacheWarnings;
|
|
303
|
+
}
|
|
304
|
+
export function clearCacheWarnings() {
|
|
305
|
+
cacheWarnings.length = 0;
|
|
306
|
+
}
|
|
307
|
+
async function cachedTsCollectComplexity(cwd, _trace, changedFiles) {
|
|
308
|
+
const sourceRoots = await findAllTypeScriptFilesUnderSourceRoots(cwd);
|
|
309
|
+
const fileSet = new Set([...sourceRoots, ...getGitTrackedCodeFiles(cwd)]);
|
|
310
|
+
const changedFilesHash = changedFiles
|
|
311
|
+
? sha256Hex(JSON.stringify([...changedFiles].sort()))
|
|
312
|
+
: undefined;
|
|
313
|
+
const results = [];
|
|
314
|
+
for (const filePath of fileSet) {
|
|
315
|
+
const rel = path.relative(cwd, filePath).replace(/\\/g, '/');
|
|
316
|
+
if (changedFiles && !changedFiles.has(rel))
|
|
317
|
+
continue;
|
|
318
|
+
try {
|
|
319
|
+
const content = await readFile(filePath, 'utf8');
|
|
320
|
+
const key = {
|
|
321
|
+
kind: 'file',
|
|
322
|
+
fileContent: content,
|
|
323
|
+
providerVersion: complexityProvenance.version,
|
|
324
|
+
...(changedFilesHash === undefined ? {} : { changedFilesHash }),
|
|
325
|
+
};
|
|
326
|
+
const lookup = await getOrCompute(cwd, key, async () => {
|
|
327
|
+
const methods = await parseFileMethods(filePath);
|
|
328
|
+
return methods.map((d) => ({
|
|
329
|
+
file: rel,
|
|
330
|
+
method: d.containerName ? `${d.containerName}.${d.functionName}` : d.functionName,
|
|
331
|
+
lineStart: d.startLine,
|
|
332
|
+
lineEnd: d.endLine,
|
|
333
|
+
cc: d.complexity,
|
|
334
|
+
}));
|
|
335
|
+
});
|
|
336
|
+
results.push(...lookup.value);
|
|
337
|
+
}
|
|
338
|
+
catch (err) {
|
|
339
|
+
cacheWarnings.push(`Warning: failed to parse ${filePath}, skipping: ${err instanceof Error ? err.message : String(err)}`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return results;
|
|
343
|
+
}
|
|
344
|
+
async function cachedReadCoveragePath(coveragePath, cwd) {
|
|
345
|
+
try {
|
|
346
|
+
const content = await readFile(coveragePath);
|
|
347
|
+
const coverageHash = createHash('sha256').update(content).digest('hex');
|
|
348
|
+
const key = { kind: 'coverage', coverageHash, providerVersion: coverageProvenance.version };
|
|
349
|
+
const lookup = await getOrCompute(cwd, key, async () => {
|
|
350
|
+
const result = await readCoverage(cwd, coveragePath);
|
|
351
|
+
const value = {
|
|
352
|
+
available: result.available,
|
|
353
|
+
coverageEntries: result.coverageMap ? [...result.coverageMap.entries()] : null,
|
|
354
|
+
error: result.error,
|
|
355
|
+
};
|
|
356
|
+
if (result.reason !== undefined)
|
|
357
|
+
value.reason = result.reason;
|
|
358
|
+
if (result.contentSha256 !== undefined)
|
|
359
|
+
value.contentSha256 = result.contentSha256;
|
|
360
|
+
return value;
|
|
361
|
+
});
|
|
362
|
+
const out = {
|
|
363
|
+
available: lookup.value.available,
|
|
364
|
+
coverageMap: lookup.value.coverageEntries ? new Map(lookup.value.coverageEntries) : null,
|
|
365
|
+
error: lookup.value.error,
|
|
366
|
+
};
|
|
367
|
+
if (lookup.value.reason !== undefined)
|
|
368
|
+
out.reason = lookup.value.reason;
|
|
369
|
+
if (lookup.value.contentSha256 !== undefined)
|
|
370
|
+
out.contentSha256 = lookup.value.contentSha256;
|
|
371
|
+
return out;
|
|
372
|
+
}
|
|
373
|
+
catch (err) {
|
|
374
|
+
// ponytail: never throw out of the cached path — degrade to the error shape
|
|
375
|
+
// readCoverageFile uses; the cache simply misses.
|
|
376
|
+
return { available: false, coverageMap: null, error: true, reason: err instanceof Error ? err.message : String(err) };
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
// ponytail: extracted candidate-search loop from cachedReadCoverage to reduce CRAP.
|
|
380
|
+
// Iterates coverageCandidates; returns first accessible, non-error result.
|
|
381
|
+
async function findCoverageCandidate(candidates, cwd) {
|
|
382
|
+
for (const candidate of candidates) {
|
|
383
|
+
const filePath = path.join(cwd, candidate);
|
|
384
|
+
try {
|
|
385
|
+
await access(filePath, constants.R_OK);
|
|
386
|
+
const result = await cachedReadCoveragePath(filePath, cwd);
|
|
387
|
+
if (!result.error)
|
|
388
|
+
return result;
|
|
389
|
+
cacheWarnings.push(`Coverage conversion failed for ${filePath}: ${result.reason}`);
|
|
390
|
+
}
|
|
391
|
+
catch {
|
|
392
|
+
// not accessible, continue
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
async function cachedReadCoverage(cwd, coverageFile) {
|
|
398
|
+
if (coverageFile !== undefined && coverageFile !== null && coverageFile !== '') {
|
|
399
|
+
const coveragePath = path.isAbsolute(coverageFile) ? coverageFile : path.resolve(cwd, coverageFile);
|
|
400
|
+
try {
|
|
401
|
+
await access(coveragePath, constants.R_OK);
|
|
402
|
+
}
|
|
403
|
+
catch {
|
|
404
|
+
return { available: true, coverageMap: null, error: true, reason: 'missing' };
|
|
405
|
+
}
|
|
406
|
+
return cachedReadCoveragePath(coveragePath, cwd);
|
|
407
|
+
}
|
|
408
|
+
// Config-driven: collect coverageFiles from all providers in registry
|
|
409
|
+
const registry = providerRegistry();
|
|
410
|
+
const coverageCandidates = registry
|
|
411
|
+
? [...new Set([...registry.values()].flatMap((r) => r.coverageFiles))]
|
|
412
|
+
: ['.coverage', 'coverage.xml', 'coverage.json', 'coverage/coverage-final.json']; // builtin defaults
|
|
413
|
+
const found = await findCoverageCandidate(coverageCandidates, cwd);
|
|
414
|
+
return found ?? { available: false, coverageMap: null, error: false };
|
|
415
|
+
}
|
|
416
|
+
export function registerCachedProviders() {
|
|
417
|
+
cacheWarnings.length = 0; // reset per registration — in-process re-invocations must not accumulate stale warnings
|
|
418
|
+
const registry = providerRegistry();
|
|
419
|
+
if (!registry)
|
|
420
|
+
return; // no config loaded yet
|
|
421
|
+
// Collect unique providers to avoid double-registration
|
|
422
|
+
const seen = new Set();
|
|
423
|
+
for (const [ext, resolved] of registry) {
|
|
424
|
+
const key = resolved.language;
|
|
425
|
+
// ponytail: snapshot original BEFORE registerProvider overwrites same ext
|
|
426
|
+
const orig = getProvider(ext);
|
|
427
|
+
if (seen.has(key)) {
|
|
428
|
+
// Same language, different ext — register same cached factory
|
|
429
|
+
registerProvider(ext, seen.has(`cached:${key}`) ? {
|
|
430
|
+
collectComplexity: cachedTsCollectComplexity,
|
|
431
|
+
readCoverage: cachedReadCoverage,
|
|
432
|
+
} : {
|
|
433
|
+
// Use snapshot: resolved is ResolvedProvider (no collectComplexity method)
|
|
434
|
+
collectComplexity: (cwd, trace, changedFiles) => orig ? orig.collectComplexity(cwd, trace, changedFiles) : Promise.resolve([]),
|
|
435
|
+
readCoverage: cachedReadCoverage,
|
|
436
|
+
});
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
seen.add(key);
|
|
440
|
+
if (resolved.language === 'typescript') {
|
|
441
|
+
registerProvider(ext, {
|
|
442
|
+
collectComplexity: cachedTsCollectComplexity,
|
|
443
|
+
readCoverage: cachedReadCoverage,
|
|
444
|
+
});
|
|
445
|
+
seen.add(`cached:${key}`);
|
|
446
|
+
}
|
|
447
|
+
else {
|
|
448
|
+
// Non-TS: cache coverage only; complexity passes through unchanged
|
|
449
|
+
registerProvider(ext, {
|
|
450
|
+
// Lazy-load original provider's collectComplexity to avoid circular deps
|
|
451
|
+
collectComplexity: async (cwd, trace, changedFiles) => {
|
|
452
|
+
return orig ? orig.collectComplexity(cwd, trace, changedFiles) : [];
|
|
453
|
+
},
|
|
454
|
+
readCoverage: cachedReadCoverage,
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { type FormatType } from './formatters/index.js';
|
|
3
|
+
export interface CheckArgs {
|
|
4
|
+
base: string | null;
|
|
5
|
+
json: boolean;
|
|
6
|
+
cache?: boolean;
|
|
7
|
+
autoCoverage?: boolean;
|
|
8
|
+
crapThreshold: number;
|
|
9
|
+
coverageFile: string | undefined;
|
|
10
|
+
format?: FormatType;
|
|
11
|
+
verbose: boolean;
|
|
12
|
+
providerConfig?: string;
|
|
13
|
+
allowExternalConfig: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare function parseCliArgs(argv?: string[]): CheckArgs;
|
|
16
|
+
interface EvidenceOutputShape {
|
|
17
|
+
analysisStatus: string;
|
|
18
|
+
gate: string | null;
|
|
19
|
+
completeness: string;
|
|
20
|
+
changedFunctions: Array<Record<string, unknown>>;
|
|
21
|
+
ruleResults: Array<{
|
|
22
|
+
ruleId: string;
|
|
23
|
+
result: string;
|
|
24
|
+
}>;
|
|
25
|
+
capabilities: Record<string, string>;
|
|
26
|
+
coverageErrorReason?: string;
|
|
27
|
+
diagnostics?: {
|
|
28
|
+
quality?: {
|
|
29
|
+
complexity?: string;
|
|
30
|
+
coverage?: string;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Auto-detect base reference. If base is non-null returns it directly,
|
|
36
|
+
* otherwise tries detectDefaultBase. Exits on failure.
|
|
37
|
+
*/
|
|
38
|
+
export declare function detectBase(base: string | null, verbose: boolean): Promise<string>;
|
|
39
|
+
/**
|
|
40
|
+
* Emit check output: verbose diagnostics, formatter, JSON/summary, and
|
|
41
|
+
* FAILED status handling + exit code. Preserves byte-identical output
|
|
42
|
+
* from pre-dispatcher CLI.
|
|
43
|
+
*/
|
|
44
|
+
export declare function formatOutput(output: EvidenceOutputShape, opts: CheckArgs, resolvedBase: string, cacheEnabled: boolean): void;
|
|
45
|
+
/**
|
|
46
|
+
* Main CLI function: subcommand dispatcher. First argument selects the
|
|
47
|
+
* subcommand; a non-subcommand first argument (e.g. legacy `--base HEAD check`)
|
|
48
|
+
* defaults to `check`, preserving pre-dispatcher behavior.
|
|
49
|
+
*/
|
|
50
|
+
export declare function main(): Promise<void>;
|
|
51
|
+
export {};
|