peaks-loop-mut 0.0.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.
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/services/agent/ecc-cache-service.d.ts +98 -0
- package/dist/services/agent/ecc-cache-service.d.ts.map +1 -0
- package/dist/services/agent/ecc-cache-service.js +491 -0
- package/dist/services/mut/assert-scanner.d.ts +7 -0
- package/dist/services/mut/assert-scanner.d.ts.map +1 -0
- package/dist/services/mut/assert-scanner.js +116 -0
- package/dist/services/mut/index.d.ts +9 -0
- package/dist/services/mut/index.d.ts.map +1 -0
- package/dist/services/mut/index.js +7 -0
- package/dist/services/mut/mut-runner.d.ts +40 -0
- package/dist/services/mut/mut-runner.d.ts.map +1 -0
- package/dist/services/mut/mut-runner.js +29 -0
- package/dist/services/mut/production-stryker.d.ts +3 -0
- package/dist/services/mut/production-stryker.d.ts.map +1 -0
- package/dist/services/mut/production-stryker.js +75 -0
- package/dist/services/mut/report-builder.d.ts +12 -0
- package/dist/services/mut/report-builder.d.ts.map +1 -0
- package/dist/services/mut/report-builder.js +110 -0
- package/dist/services/mut/report-loader.d.ts +32 -0
- package/dist/services/mut/report-loader.d.ts.map +1 -0
- package/dist/services/mut/report-loader.js +78 -0
- package/dist/services/mut/thresholds.d.ts +22 -0
- package/dist/services/mut/thresholds.d.ts.map +1 -0
- package/dist/services/mut/thresholds.js +22 -0
- package/dist/services/mut/types.d.ts +337 -0
- package/dist/services/mut/types.d.ts.map +1 -0
- package/dist/services/mut/types.js +59 -0
- package/dist/shared/frontmatter.d.ts +17 -0
- package/dist/shared/frontmatter.d.ts.map +1 -0
- package/dist/shared/frontmatter.js +175 -0
- package/package.json +28 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per spec §4.2 验收审计 + §7 阶段二 — 5-pattern weak-assertion AST scan.
|
|
3
|
+
*
|
|
4
|
+
* Hard constraints:
|
|
5
|
+
* H6 (CLI裁决): every detection is regex/AST-based, not LLM-judged.
|
|
6
|
+
* KISS: regex matchers on common expect() patterns. Production slice
|
|
7
|
+
* would migrate to TypeScript Compiler API; v1 ships regex for speed.
|
|
8
|
+
*
|
|
9
|
+
* Weak patterns (5):
|
|
10
|
+
* - toBeDefined()
|
|
11
|
+
* - toBeTruthy()
|
|
12
|
+
* - toEqual(x) where arg === receiver (toEqual-self)
|
|
13
|
+
* - expect.anything()
|
|
14
|
+
* - toBe(x) where arg === receiver (toBe-self)
|
|
15
|
+
*/
|
|
16
|
+
import { readFile } from 'node:fs/promises';
|
|
17
|
+
import { join } from 'node:path';
|
|
18
|
+
const WEAK_PATTERNS = [
|
|
19
|
+
{ pattern: 'toBeDefined', regex: /\.toBeDefined\s*\(\s*\)/g },
|
|
20
|
+
{ pattern: 'toBeTruthy', regex: /\.toBeTruthy\s*\(\s*\)/g },
|
|
21
|
+
{ pattern: 'expect-anything', regex: /expect\.anything\s*\(\s*\)/g },
|
|
22
|
+
// Self-equality patterns require matched-pair parsing; handled below.
|
|
23
|
+
];
|
|
24
|
+
const TO_EQUAL_SELF = /\.toEqual\s*\(\s*([a-zA-Z_$][\w$]*)\s*\)/g;
|
|
25
|
+
const TO_BE_SELF = /\.toBe\s*\(\s*([a-zA-Z_$][\w$]*)\s*\)/g;
|
|
26
|
+
async function countAssertions(content) {
|
|
27
|
+
// Approximate: count expect(...).method( occurrences.
|
|
28
|
+
// Handle nested parens by finding expect( and matching to a balanced close.
|
|
29
|
+
let count = 0;
|
|
30
|
+
const re = /expect\s*\(/g;
|
|
31
|
+
for (const m of content.matchAll(re)) {
|
|
32
|
+
let depth = 1;
|
|
33
|
+
let i = (m.index ?? 0) + m[0].length;
|
|
34
|
+
while (i < content.length && depth > 0) {
|
|
35
|
+
const ch = content[i];
|
|
36
|
+
if (ch === '(')
|
|
37
|
+
depth++;
|
|
38
|
+
else if (ch === ')')
|
|
39
|
+
depth--;
|
|
40
|
+
i++;
|
|
41
|
+
}
|
|
42
|
+
// After balanced expect(...), check for .method( pattern.
|
|
43
|
+
const tail = content.slice(i);
|
|
44
|
+
if (/^\s*\.\w+\s*\(/.test(tail)) {
|
|
45
|
+
count++;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return count;
|
|
49
|
+
}
|
|
50
|
+
function lineOf(content, offset) {
|
|
51
|
+
return content.slice(0, offset).split('\n').length;
|
|
52
|
+
}
|
|
53
|
+
export async function scanAssertions(input) {
|
|
54
|
+
let total = 0;
|
|
55
|
+
let weak = 0;
|
|
56
|
+
const byPattern = new Map();
|
|
57
|
+
for (const file of input.testFiles) {
|
|
58
|
+
const fullPath = join(input.project, file);
|
|
59
|
+
const content = await readFile(fullPath, 'utf8');
|
|
60
|
+
total += await countAssertions(content);
|
|
61
|
+
for (const { pattern, regex } of WEAK_PATTERNS) {
|
|
62
|
+
const matches = [...content.matchAll(regex)];
|
|
63
|
+
if (matches.length > 0) {
|
|
64
|
+
weak += matches.length;
|
|
65
|
+
const list = byPattern.get(pattern) ?? [];
|
|
66
|
+
for (const m of matches) {
|
|
67
|
+
const idx = m.index ?? 0;
|
|
68
|
+
list.push({ file, line: lineOf(content, idx), code: m[0] });
|
|
69
|
+
}
|
|
70
|
+
byPattern.set(pattern, list);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// toEqual-self: needs receiver extraction.
|
|
74
|
+
const eqSelf = [...content.matchAll(TO_EQUAL_SELF)];
|
|
75
|
+
for (const m of eqSelf) {
|
|
76
|
+
const receiverMatch = content.slice(0, m.index ?? 0).match(/expect\s*\(\s*([a-zA-Z_$][\w$]*)\s*\)/g);
|
|
77
|
+
if (receiverMatch && receiverMatch.length > 0) {
|
|
78
|
+
const lastReceiver = receiverMatch[receiverMatch.length - 1];
|
|
79
|
+
const receiverName = lastReceiver?.match(/expect\s*\(\s*([a-zA-Z_$][\w$]*)\s*\)/)?.[1];
|
|
80
|
+
if (receiverName && receiverName === m[1]) {
|
|
81
|
+
weak += 1;
|
|
82
|
+
const idx = m.index ?? 0;
|
|
83
|
+
const list = byPattern.get('toEqual-self') ?? [];
|
|
84
|
+
list.push({ file, line: lineOf(content, idx), code: m[0] });
|
|
85
|
+
byPattern.set('toEqual-self', list);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// toBe-self: same pattern.
|
|
90
|
+
const beSelf = [...content.matchAll(TO_BE_SELF)];
|
|
91
|
+
for (const m of beSelf) {
|
|
92
|
+
const receiverMatch = content.slice(0, m.index ?? 0).match(/expect\s*\(\s*([a-zA-Z_$][\w$]*)\s*\)/g);
|
|
93
|
+
if (receiverMatch && receiverMatch.length > 0) {
|
|
94
|
+
const lastReceiver = receiverMatch[receiverMatch.length - 1];
|
|
95
|
+
const receiverName = lastReceiver?.match(/expect\s*\(\s*([a-zA-Z_$][\w$]*)\s*\)/)?.[1];
|
|
96
|
+
if (receiverName && receiverName === m[1]) {
|
|
97
|
+
weak += 1;
|
|
98
|
+
const idx = m.index ?? 0;
|
|
99
|
+
const list = byPattern.get('toBe-self') ?? [];
|
|
100
|
+
list.push({ file, line: lineOf(content, idx), code: m[0] });
|
|
101
|
+
byPattern.set('toBe-self', list);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const weakPatterns = [];
|
|
107
|
+
for (const [pattern, examples] of byPattern.entries()) {
|
|
108
|
+
weakPatterns.push({ pattern, count: examples.length, examples });
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
totalAssertions: total,
|
|
112
|
+
weakAssertions: weak,
|
|
113
|
+
weakRate: total === 0 ? 0 : weak / total,
|
|
114
|
+
weakPatterns,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { MutReportSchema } from './types.js';
|
|
2
|
+
export { scanAssertions, type ScanInput } from './assert-scanner.js';
|
|
3
|
+
export { runMutation, type RunMutationInput, type RunMutationOutput, type StrykerInvoker, type StrykerRawResult, } from './mut-runner.js';
|
|
4
|
+
export { createProductionStrykerInvoker } from './production-stryker.js';
|
|
5
|
+
export { buildMutReport, type BuildMutInput } from './report-builder.js';
|
|
6
|
+
export { loadMutReport, mutReportPath, MUT_REPORT_RELATIVE_PATH } from './report-loader.js';
|
|
7
|
+
export { DEFAULT_THRESHOLDS, evaluateThresholds, type Thresholds, type ThresholdEvaluation, type ThresholdViolation, type ThresholdViolationKind, } from './thresholds.js';
|
|
8
|
+
export type { MutReportJson, MutationReport, AssertionsReport, WeakPattern, WeakPatternCount, Followup, } from './types.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/mut/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,KAAK,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrE,OAAO,EACL,WAAW,EACX,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACnB,KAAK,gBAAgB,GACtB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,8BAA8B,EAAE,MAAM,yBAAyB,CAAC;AACzE,OAAO,EAAE,cAAc,EAAE,KAAK,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAC5F,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAClB,KAAK,UAAU,EACf,KAAK,mBAAmB,EACxB,KAAK,kBAAkB,EACvB,KAAK,sBAAsB,GAC5B,MAAM,iBAAiB,CAAC;AACzB,YAAY,EACV,aAAa,EAAE,cAAc,EAAE,gBAAgB,EAC/C,WAAW,EAAE,gBAAgB,EAAE,QAAQ,GACxC,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { MutReportSchema } from './types.js';
|
|
2
|
+
export { scanAssertions } from './assert-scanner.js';
|
|
3
|
+
export { runMutation, } from './mut-runner.js';
|
|
4
|
+
export { createProductionStrykerInvoker } from './production-stryker.js';
|
|
5
|
+
export { buildMutReport } from './report-builder.js';
|
|
6
|
+
export { loadMutReport, mutReportPath, MUT_REPORT_RELATIVE_PATH } from './report-loader.js';
|
|
7
|
+
export { DEFAULT_THRESHOLDS, evaluateThresholds, } from './thresholds.js';
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per spec §4.2 验收审计 — Stryker wrapper for TS mutation testing.
|
|
3
|
+
*
|
|
4
|
+
* Hard constraints:
|
|
5
|
+
* H6 (CLI裁决): invokeStryker is injected; production wires it to
|
|
6
|
+
* @stryker-mutator/core programmatic API.
|
|
7
|
+
* DRY: production implementation lives in this file; tests mock the
|
|
8
|
+
* invokeStryker injection point.
|
|
9
|
+
* KISS: keep the public surface narrow — input, invoker, output.
|
|
10
|
+
*/
|
|
11
|
+
import type { MutationReport } from './types.js';
|
|
12
|
+
export interface StrykerRawResult {
|
|
13
|
+
readonly mutantsTotal: number;
|
|
14
|
+
readonly mutantsKilled: number;
|
|
15
|
+
readonly mutantsSurvived: number;
|
|
16
|
+
readonly mutantsTimeout: number;
|
|
17
|
+
readonly perFile: ReadonlyArray<{
|
|
18
|
+
readonly file: string;
|
|
19
|
+
readonly killRate: number;
|
|
20
|
+
readonly survived: ReadonlyArray<{
|
|
21
|
+
line: number;
|
|
22
|
+
mutation: string;
|
|
23
|
+
survivedBecause: string;
|
|
24
|
+
}>;
|
|
25
|
+
}>;
|
|
26
|
+
}
|
|
27
|
+
export type StrykerInvoker = (opts: {
|
|
28
|
+
project: string;
|
|
29
|
+
testFiles: ReadonlyArray<string>;
|
|
30
|
+
}) => Promise<StrykerRawResult>;
|
|
31
|
+
export interface RunMutationInput {
|
|
32
|
+
readonly project: string;
|
|
33
|
+
readonly testFiles: ReadonlyArray<string>;
|
|
34
|
+
readonly invokeStryker: StrykerInvoker;
|
|
35
|
+
}
|
|
36
|
+
export interface RunMutationOutput {
|
|
37
|
+
readonly mutation: MutationReport;
|
|
38
|
+
}
|
|
39
|
+
export declare function runMutation(input: RunMutationInput): Promise<RunMutationOutput>;
|
|
40
|
+
//# sourceMappingURL=mut-runner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mut-runner.d.ts","sourceRoot":"","sources":["../../../src/services/mut/mut-runner.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC;QAC9B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;QAC1B,QAAQ,CAAC,QAAQ,EAAE,aAAa,CAAC;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,QAAQ,EAAE,MAAM,CAAC;YAAC,eAAe,EAAE,MAAM,CAAA;SAAE,CAAC,CAAC;KAC/F,CAAC,CAAC;CACJ;AAED,MAAM,MAAM,cAAc,GAAG,CAAC,IAAI,EAAE;IAClC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;CAClC,KAAK,OAAO,CAAC,gBAAgB,CAAC,CAAC;AAEhC,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAC1C,QAAQ,CAAC,aAAa,EAAE,cAAc,CAAC;CACxC;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,QAAQ,EAAE,cAAc,CAAC;CACnC;AAED,wBAAsB,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CA2BrF"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export async function runMutation(input) {
|
|
2
|
+
let raw;
|
|
3
|
+
try {
|
|
4
|
+
raw = await input.invokeStryker({
|
|
5
|
+
project: input.project,
|
|
6
|
+
testFiles: input.testFiles,
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
catch (err) {
|
|
10
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
11
|
+
throw new Error(`Stryker invocation failed: ${message}`);
|
|
12
|
+
}
|
|
13
|
+
const killRate = raw.mutantsTotal === 0 ? 0 : raw.mutantsKilled / raw.mutantsTotal;
|
|
14
|
+
return {
|
|
15
|
+
mutation: {
|
|
16
|
+
tool: 'stryker',
|
|
17
|
+
mutantsTotal: raw.mutantsTotal,
|
|
18
|
+
mutantsKilled: raw.mutantsKilled,
|
|
19
|
+
mutantsSurvived: raw.mutantsSurvived,
|
|
20
|
+
mutantsTimeout: raw.mutantsTimeout,
|
|
21
|
+
killRate,
|
|
22
|
+
byFile: raw.perFile.map((f) => ({
|
|
23
|
+
file: f.file,
|
|
24
|
+
killRate: f.killRate,
|
|
25
|
+
survived: f.survived,
|
|
26
|
+
})),
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"production-stryker.d.ts","sourceRoot":"","sources":["../../../src/services/mut/production-stryker.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,cAAc,EAAoB,MAAM,iBAAiB,CAAC;AAcxE,wBAAgB,8BAA8B,IAAI,cAAc,CAY/D"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Production Stryker invoker. Uses @stryker-mutator/core programmatic API.
|
|
3
|
+
* Note: this is a thin wrapper — actual Stryker invocation requires the
|
|
4
|
+
* Stryker config in `stryker.conf.js` to live at the project root.
|
|
5
|
+
*/
|
|
6
|
+
import { relative, isAbsolute } from 'node:path';
|
|
7
|
+
export function createProductionStrykerInvoker() {
|
|
8
|
+
return async ({ project, testFiles: _testFiles }) => {
|
|
9
|
+
// Lazy-load Stryker so unit tests don't need it installed.
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
11
|
+
const Stryker = (await import('@stryker-mutator/core')).Stryker;
|
|
12
|
+
// Mutation surface is owned by stryker.conf.js. testFiles is a runtime
|
|
13
|
+
// hint (consumed by the configured testRunner, not by Stryker's `mutate`
|
|
14
|
+
// field — `mutate` is source files to mutate, never test paths).
|
|
15
|
+
const stryker = new Stryker({});
|
|
16
|
+
const mutants = (await stryker.runMutationTest());
|
|
17
|
+
return normalize(mutants, project);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function normalize(mutants, project) {
|
|
21
|
+
let mutantsKilled = 0;
|
|
22
|
+
let mutantsSurvived = 0;
|
|
23
|
+
let mutantsTimeout = 0;
|
|
24
|
+
for (const m of mutants) {
|
|
25
|
+
if (m.status === 'Killed')
|
|
26
|
+
mutantsKilled++;
|
|
27
|
+
else if (m.status === 'Survived')
|
|
28
|
+
mutantsSurvived++;
|
|
29
|
+
else if (m.status === 'Timeout')
|
|
30
|
+
mutantsTimeout++;
|
|
31
|
+
}
|
|
32
|
+
// Bucket mutants by fileName to populate perFile. deriveFollowups iterates
|
|
33
|
+
// m.byFile to emit per-file followups (e.g. low_kill_rate) — leaving this
|
|
34
|
+
// empty (as v0 did) silently swallowed those followups in production.
|
|
35
|
+
const byFile = new Map();
|
|
36
|
+
for (const m of mutants) {
|
|
37
|
+
// Stryker 8 returns absolute paths in MutantResult.fileName. Strip the
|
|
38
|
+
// project root so byFile[].file is repo-relative (e.g. "src/services/loop/...").
|
|
39
|
+
// path.relative falls back to the absolute path when input is outside
|
|
40
|
+
// project, which is the safest failure mode.
|
|
41
|
+
const file = isAbsolute(m.fileName) && m.fileName.startsWith(project)
|
|
42
|
+
? relative(project, m.fileName)
|
|
43
|
+
: m.fileName;
|
|
44
|
+
let bucket = byFile.get(file);
|
|
45
|
+
if (!bucket) {
|
|
46
|
+
bucket = { killed: 0, survived: 0, survivedEntries: [] };
|
|
47
|
+
byFile.set(file, bucket);
|
|
48
|
+
}
|
|
49
|
+
if (m.status === 'Killed')
|
|
50
|
+
bucket.killed++;
|
|
51
|
+
else if (m.status === 'Survived') {
|
|
52
|
+
bucket.survived++;
|
|
53
|
+
bucket.survivedEntries.push({
|
|
54
|
+
line: m.location.start.line + 1, // Stryker is 0-based; reports are 1-based.
|
|
55
|
+
mutation: m.replacement,
|
|
56
|
+
survivedBecause: m.statusReason ?? '',
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const perFile = [...byFile.entries()]
|
|
61
|
+
.map(([file, b]) => {
|
|
62
|
+
const total = b.killed + b.survived;
|
|
63
|
+
const killRate = total === 0 ? 0 : b.killed / total;
|
|
64
|
+
return { file, killRate, survived: b.survivedEntries };
|
|
65
|
+
})
|
|
66
|
+
// Stable order for MUT.sig / reports.
|
|
67
|
+
.sort((a, b) => a.file.localeCompare(b.file));
|
|
68
|
+
return {
|
|
69
|
+
mutantsTotal: mutants.length,
|
|
70
|
+
mutantsKilled,
|
|
71
|
+
mutantsSurvived,
|
|
72
|
+
mutantsTimeout,
|
|
73
|
+
perFile,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type AssertionsReport, type MutationReport, type MutReportJson } from './types.js';
|
|
2
|
+
import { type Thresholds } from './thresholds.js';
|
|
3
|
+
export interface BuildMutInput {
|
|
4
|
+
readonly mutation: MutationReport;
|
|
5
|
+
readonly assertions: AssertionsReport;
|
|
6
|
+
readonly inputSig: string;
|
|
7
|
+
readonly out: string;
|
|
8
|
+
readonly thresholds?: Thresholds;
|
|
9
|
+
readonly now?: () => Date;
|
|
10
|
+
}
|
|
11
|
+
export declare function buildMutReport(input: BuildMutInput): Promise<MutReportJson>;
|
|
12
|
+
//# sourceMappingURL=report-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"report-builder.d.ts","sourceRoot":"","sources":["../../../src/services/mut/report-builder.ts"],"names":[],"mappings":"AAcA,OAAO,EAEL,KAAK,gBAAgB,EAErB,KAAK,cAAc,EACnB,KAAK,aAAa,EACnB,MAAM,YAAY,CAAC;AACpB,OAAO,EAGL,KAAK,UAAU,EAChB,MAAM,iBAAiB,CAAC;AAEzB,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,QAAQ,EAAE,cAAc,CAAC;IAClC,QAAQ,CAAC,UAAU,EAAE,gBAAgB,CAAC;IACtC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC;IACjC,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,IAAI,CAAC;CAC3B;AAuED,wBAAsB,cAAc,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAuCjF"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Combines MutationReport + AssertionsReport into MutReportJson, computes
|
|
3
|
+
* thresholds, derives followups, computes MUT.sig, writes file atomically.
|
|
4
|
+
*
|
|
5
|
+
* Hard constraints:
|
|
6
|
+
* H8 (audit trail hashable): MUT.sig is sha256 of normalized content;
|
|
7
|
+
* generatedAt + sha256 are excluded from the digest to make the
|
|
8
|
+
* signature deterministic across runs.
|
|
9
|
+
* H6 (CLI裁决): passed boolean + followups computed by CLI, not LLM.
|
|
10
|
+
* KISS: keep composition narrow — input, thresholds, output file path.
|
|
11
|
+
*/
|
|
12
|
+
import { createHash } from 'node:crypto';
|
|
13
|
+
import { mkdir, rename, unlink, writeFile } from 'node:fs/promises';
|
|
14
|
+
import { dirname } from 'node:path';
|
|
15
|
+
import { MutReportSchema, } from './types.js';
|
|
16
|
+
import { DEFAULT_THRESHOLDS, evaluateThresholds, } from './thresholds.js';
|
|
17
|
+
function deriveFollowups(m, a, t) {
|
|
18
|
+
const out = [];
|
|
19
|
+
if (m.killRate < t.mutationKillRateMin) {
|
|
20
|
+
for (const f of m.byFile) {
|
|
21
|
+
if (f.killRate < t.mutationKillRateMin) {
|
|
22
|
+
out.push({
|
|
23
|
+
file: f.file,
|
|
24
|
+
issue: 'low_kill_rate',
|
|
25
|
+
severity: 'soft',
|
|
26
|
+
suggestion: `Add tests for ${f.survived.length} survived mutants in ${f.file}`,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (a.weakRate > t.weakAssertionRateMax) {
|
|
32
|
+
for (const p of a.weakPatterns) {
|
|
33
|
+
if (p.count > 0) {
|
|
34
|
+
const exampleFile = p.examples[0]?.file ?? '<unknown>';
|
|
35
|
+
out.push({
|
|
36
|
+
file: exampleFile,
|
|
37
|
+
issue: 'high_weak_assertions',
|
|
38
|
+
severity: 'hard',
|
|
39
|
+
suggestion: `Replace ${p.count} weak assertions of type "${p.pattern}" with concrete value checks`,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Recursively sort object keys to produce a canonical JSON string.
|
|
48
|
+
* Arrays preserve order; only object keys are sorted.
|
|
49
|
+
*/
|
|
50
|
+
function canonicalize(value) {
|
|
51
|
+
if (value === null || typeof value !== 'object') {
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
if (Array.isArray(value)) {
|
|
55
|
+
return value.map((v) => canonicalize(v));
|
|
56
|
+
}
|
|
57
|
+
const obj = value;
|
|
58
|
+
const sorted = {};
|
|
59
|
+
for (const key of Object.keys(obj).sort()) {
|
|
60
|
+
sorted[key] = canonicalize(obj[key]);
|
|
61
|
+
}
|
|
62
|
+
return sorted;
|
|
63
|
+
}
|
|
64
|
+
function sha256OfCanonical(value) {
|
|
65
|
+
const canonical = JSON.stringify(canonicalize(value));
|
|
66
|
+
return createHash('sha256').update(canonical).digest('hex');
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Compute MUT.sig: sha256 of the report content excluding generatedAt and
|
|
70
|
+
* sha256 itself. Both fields are removed before canonicalization so the
|
|
71
|
+
* digest is stable across runs that differ only in timestamp.
|
|
72
|
+
*/
|
|
73
|
+
function computeMutSig(report) {
|
|
74
|
+
const { generatedAt: _g, ...rest } = report;
|
|
75
|
+
void _g;
|
|
76
|
+
return sha256OfCanonical(rest);
|
|
77
|
+
}
|
|
78
|
+
export async function buildMutReport(input) {
|
|
79
|
+
const thresholds = input.thresholds ?? DEFAULT_THRESHOLDS;
|
|
80
|
+
const now = input.now ?? (() => new Date());
|
|
81
|
+
const evalResult = evaluateThresholds(thresholds, input.mutation.killRate, input.assertions.weakRate);
|
|
82
|
+
const followups = deriveFollowups(input.mutation, input.assertions, thresholds);
|
|
83
|
+
const partial = {
|
|
84
|
+
version: '1.0',
|
|
85
|
+
generatedAt: now().toISOString(),
|
|
86
|
+
inputSig: input.inputSig,
|
|
87
|
+
mutation: input.mutation,
|
|
88
|
+
assertions: input.assertions,
|
|
89
|
+
thresholds: {
|
|
90
|
+
mutationKillRateMin: thresholds.mutationKillRateMin,
|
|
91
|
+
weakAssertionRateMax: thresholds.weakAssertionRateMax,
|
|
92
|
+
passed: evalResult.passed,
|
|
93
|
+
},
|
|
94
|
+
followups,
|
|
95
|
+
};
|
|
96
|
+
const sha256 = computeMutSig(partial);
|
|
97
|
+
const final = { ...partial, sha256 };
|
|
98
|
+
MutReportSchema.parse(final);
|
|
99
|
+
await mkdir(dirname(input.out), { recursive: true });
|
|
100
|
+
const tmp = `${input.out}.tmp`;
|
|
101
|
+
try {
|
|
102
|
+
await writeFile(tmp, JSON.stringify(final, null, 2), 'utf8');
|
|
103
|
+
await rename(tmp, input.out);
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
await unlink(tmp).catch(() => undefined);
|
|
107
|
+
throw err;
|
|
108
|
+
}
|
|
109
|
+
return final;
|
|
110
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type MutReportJson } from './types.js';
|
|
2
|
+
export declare const MUT_REPORT_RELATIVE_PATH = "mut/mut-report.json";
|
|
3
|
+
/**
|
|
4
|
+
* Build the canonical absolute path of a session's mut-report.json.
|
|
5
|
+
* Exported so the CLI surface in qa-commands can reuse the same
|
|
6
|
+
* constant for diagnostic messages.
|
|
7
|
+
*
|
|
8
|
+
* Uses POSIX separators intentionally: the one-axis envelope is
|
|
9
|
+
* documented as `.peaks/_runtime/<sid>/mut/mut-report.json` (forward
|
|
10
|
+
* slashes) and that path is what the hotfix 81f00ce audit-trail
|
|
11
|
+
* contract guarantees. `node:path` would produce backslashes on
|
|
12
|
+
* Windows, breaking the cross-platform invariant. POSIX join is
|
|
13
|
+
* correct here because the result is a logical path stored in JSON
|
|
14
|
+
* artifacts and CLI diagnostics — it never gets passed to OS
|
|
15
|
+
* syscalls directly (readFile / writeFile accept both forms).
|
|
16
|
+
*/
|
|
17
|
+
export declare function mutReportPath(sessionId: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* Read and validate a session's mut-report.json.
|
|
20
|
+
*
|
|
21
|
+
* Returns `null` for any of these cases:
|
|
22
|
+
* - file does not exist (ENOENT)
|
|
23
|
+
* - file exists but JSON is malformed
|
|
24
|
+
* - file exists and parses but does not satisfy `MutReportSchema`
|
|
25
|
+
*
|
|
26
|
+
* Never throws. Never logs to stdout (per "no console.log in src/").
|
|
27
|
+
* Schema-parse failures are reported via `process.stderr` so the
|
|
28
|
+
* qa-runner operator can see why their report is being treated as
|
|
29
|
+
* absent.
|
|
30
|
+
*/
|
|
31
|
+
export declare function loadMutReport(sessionId: string): Promise<MutReportJson | null>;
|
|
32
|
+
//# sourceMappingURL=report-loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"report-loader.d.ts","sourceRoot":"","sources":["../../../src/services/mut/report-loader.ts"],"names":[],"mappings":"AAoBA,OAAO,EAAmB,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AAEjE,eAAO,MAAM,wBAAwB,wBAAwB,CAAC;AAE9D;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAEvD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CA8BpF"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plan 2 — Task 8: peaks-qa consumes MUT.sig from peaks-mut.
|
|
3
|
+
*
|
|
4
|
+
* `loadMutReport` is the read-side counterpart of `buildMutReport`
|
|
5
|
+
* (which is the write-side). Both share the canonical path:
|
|
6
|
+
* `.peaks/_runtime/<sessionId>/mut/mut-report.json` (per hotfix 81f00ce
|
|
7
|
+
* one-axis envelope — no `.peaks/_runtime/<sessionId>/` fallback).
|
|
8
|
+
*
|
|
9
|
+
* Design contract (per spec lines 1292-1341):
|
|
10
|
+
* - Missing file → `null` (gate is skipped, NOT failed)
|
|
11
|
+
* - Schema-invalid → `null` (log via stderr; do not throw — qa is a
|
|
12
|
+
* pre-step, not a precondition)
|
|
13
|
+
* - Schema-valid → the parsed `MutReportJson`
|
|
14
|
+
*
|
|
15
|
+
* Returning `null` instead of throwing is what lets peaks-qa treat
|
|
16
|
+
* "peaks mut was not run" as a no-op. If we threw, the qa gate would
|
|
17
|
+
* crash on every session that hasn't run peaks-mut yet.
|
|
18
|
+
*/
|
|
19
|
+
import { readFile } from 'node:fs/promises';
|
|
20
|
+
import { posix as pathPosix } from 'node:path';
|
|
21
|
+
import { MutReportSchema } from './types.js';
|
|
22
|
+
export const MUT_REPORT_RELATIVE_PATH = 'mut/mut-report.json';
|
|
23
|
+
/**
|
|
24
|
+
* Build the canonical absolute path of a session's mut-report.json.
|
|
25
|
+
* Exported so the CLI surface in qa-commands can reuse the same
|
|
26
|
+
* constant for diagnostic messages.
|
|
27
|
+
*
|
|
28
|
+
* Uses POSIX separators intentionally: the one-axis envelope is
|
|
29
|
+
* documented as `.peaks/_runtime/<sid>/mut/mut-report.json` (forward
|
|
30
|
+
* slashes) and that path is what the hotfix 81f00ce audit-trail
|
|
31
|
+
* contract guarantees. `node:path` would produce backslashes on
|
|
32
|
+
* Windows, breaking the cross-platform invariant. POSIX join is
|
|
33
|
+
* correct here because the result is a logical path stored in JSON
|
|
34
|
+
* artifacts and CLI diagnostics — it never gets passed to OS
|
|
35
|
+
* syscalls directly (readFile / writeFile accept both forms).
|
|
36
|
+
*/
|
|
37
|
+
export function mutReportPath(sessionId) {
|
|
38
|
+
return pathPosix.join('.peaks', '_runtime', sessionId, MUT_REPORT_RELATIVE_PATH);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Read and validate a session's mut-report.json.
|
|
42
|
+
*
|
|
43
|
+
* Returns `null` for any of these cases:
|
|
44
|
+
* - file does not exist (ENOENT)
|
|
45
|
+
* - file exists but JSON is malformed
|
|
46
|
+
* - file exists and parses but does not satisfy `MutReportSchema`
|
|
47
|
+
*
|
|
48
|
+
* Never throws. Never logs to stdout (per "no console.log in src/").
|
|
49
|
+
* Schema-parse failures are reported via `process.stderr` so the
|
|
50
|
+
* qa-runner operator can see why their report is being treated as
|
|
51
|
+
* absent.
|
|
52
|
+
*/
|
|
53
|
+
export async function loadMutReport(sessionId) {
|
|
54
|
+
const path = mutReportPath(sessionId);
|
|
55
|
+
let raw;
|
|
56
|
+
try {
|
|
57
|
+
raw = await readFile(path, 'utf8');
|
|
58
|
+
}
|
|
59
|
+
catch { // TODO(g2): legacy silent catch — grace: 1 minor release (v2.14.0)
|
|
60
|
+
// ENOENT or permission denied — both treated as "not run".
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
let parsedJson;
|
|
64
|
+
try {
|
|
65
|
+
parsedJson = JSON.parse(raw);
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
69
|
+
process.stderr.write(`[peaks-qa] mut-report at ${path} is not valid JSON; treating as absent: ${message}\n`);
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
const parsed = MutReportSchema.safeParse(parsedJson);
|
|
73
|
+
if (!parsed.success) {
|
|
74
|
+
process.stderr.write(`[peaks-qa] mut-report at ${path} failed schema validation; treating as absent: ${parsed.error.message}\n`);
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
return parsed.data;
|
|
78
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per spec §4.2 验收审计 — Default thresholds + evaluator.
|
|
3
|
+
*
|
|
4
|
+
* Override via `peaks-mut.config.json` at project root.
|
|
5
|
+
*/
|
|
6
|
+
export interface Thresholds {
|
|
7
|
+
readonly mutationKillRateMin: number;
|
|
8
|
+
readonly weakAssertionRateMax: number;
|
|
9
|
+
}
|
|
10
|
+
export declare const DEFAULT_THRESHOLDS: Thresholds;
|
|
11
|
+
export type ThresholdViolationKind = 'mutationKillRateMin' | 'weakAssertionRateMax';
|
|
12
|
+
export interface ThresholdViolation {
|
|
13
|
+
readonly kind: ThresholdViolationKind;
|
|
14
|
+
readonly actual: number;
|
|
15
|
+
readonly threshold: number;
|
|
16
|
+
}
|
|
17
|
+
export interface ThresholdEvaluation {
|
|
18
|
+
readonly passed: boolean;
|
|
19
|
+
readonly violations: ReadonlyArray<ThresholdViolation>;
|
|
20
|
+
}
|
|
21
|
+
export declare function evaluateThresholds(t: Thresholds, actualKillRate: number, actualWeakRate: number): ThresholdEvaluation;
|
|
22
|
+
//# sourceMappingURL=thresholds.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thresholds.d.ts","sourceRoot":"","sources":["../../../src/services/mut/thresholds.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,mBAAmB,EAAE,MAAM,CAAC;IACrC,QAAQ,CAAC,oBAAoB,EAAE,MAAM,CAAC;CACvC;AAED,eAAO,MAAM,kBAAkB,EAAE,UAG/B,CAAC;AAEH,MAAM,MAAM,sBAAsB,GAAG,qBAAqB,GAAG,sBAAsB,CAAC;AAEpF,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,IAAI,EAAE,sBAAsB,CAAC;IACtC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC,kBAAkB,CAAC,CAAC;CACxD;AAED,wBAAgB,kBAAkB,CAChC,CAAC,EAAE,UAAU,EACb,cAAc,EAAE,MAAM,EACtB,cAAc,EAAE,MAAM,GACrB,mBAAmB,CAiBrB"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const DEFAULT_THRESHOLDS = Object.freeze({
|
|
2
|
+
mutationKillRateMin: 0.80,
|
|
3
|
+
weakAssertionRateMax: 0.05,
|
|
4
|
+
});
|
|
5
|
+
export function evaluateThresholds(t, actualKillRate, actualWeakRate) {
|
|
6
|
+
const violations = [];
|
|
7
|
+
if (actualKillRate < t.mutationKillRateMin) {
|
|
8
|
+
violations.push({
|
|
9
|
+
kind: 'mutationKillRateMin',
|
|
10
|
+
actual: actualKillRate,
|
|
11
|
+
threshold: t.mutationKillRateMin,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
if (actualWeakRate > t.weakAssertionRateMax) {
|
|
15
|
+
violations.push({
|
|
16
|
+
kind: 'weakAssertionRateMax',
|
|
17
|
+
actual: actualWeakRate,
|
|
18
|
+
threshold: t.weakAssertionRateMax,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return { passed: violations.length === 0, violations };
|
|
22
|
+
}
|