@srobroek/pi-code-quality 0.23.0 → 0.24.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/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## quality [0.24.1](https://github.com/srobroek/harness-plugins/compare/quality--v0.24.0...quality--v0.24.1) (2026-12-05)
2
+
3
+ ### Bug Fixes
4
+
5
+ * **quality:** report format and file type guards, LCOV branch identity, JUnit suites ([#361](https://github.com/srobroek/harness-plugins/issues/361)) ([d1328b9](https://github.com/srobroek/harness-plugins/commit/d1328b9b9b328f07e8bc4045c9f86f61186ea756))
6
+
7
+ ## quality [0.24.0](https://github.com/srobroek/harness-plugins/compare/quality--v0.23.0...quality--v0.24.0) (2026-12-05)
8
+
9
+ ### Features
10
+
11
+ * **beads:** fold the beads skills into one beads-guide skill ([#306](https://github.com/srobroek/harness-plugins/issues/306)) ([dac13cb](https://github.com/srobroek/harness-plugins/commit/dac13cb58841044c37e63d22aa40a1b7cdc70f54))
12
+
1
13
  ## quality [0.23.0](https://github.com/srobroek/harness-plugins/compare/quality--v0.22.0...quality--v0.23.0) (2026-12-02)
2
14
 
3
15
  ### Features
@@ -7,7 +7,7 @@ import { join as join2 } from "path";
7
7
  import { Type } from "typebox";
8
8
 
9
9
  // plugins/quality/shared/audit-util.ts
10
- import { readFileSync, realpathSync, statSync } from "fs";
10
+ import { closeSync, constants, fstatSync, openSync, readFileSync, realpathSync } from "fs";
11
11
  import { tmpdir } from "os";
12
12
  import { isAbsolute, join, relative, resolve, sep } from "path";
13
13
  var TEXT_CAP = 4000;
@@ -78,9 +78,17 @@ function readReportFile(sessionDirectory, path, roots = reportRoots(sessionDirec
78
78
  }
79
79
  if (!roots.some((root) => inside(root, real)))
80
80
  throw new PathError(`${path} is outside the session and allowed report directories`);
81
- if (statSync(real).size > MAX_REPORT_BYTES)
82
- throw new Error(`${path} is larger than ${MAX_REPORT_BYTES / 1024 / 1024} MB`);
83
- return readFileSync(real, "utf8");
81
+ const fd = openSync(real, constants.O_RDONLY | constants.O_NONBLOCK);
82
+ try {
83
+ const info = fstatSync(fd);
84
+ if (!info.isFile())
85
+ throw new Error(`${path} is not a regular file`);
86
+ if (info.size > MAX_REPORT_BYTES)
87
+ throw new Error(`${path} is larger than ${MAX_REPORT_BYTES / 1024 / 1024} MB`);
88
+ return readFileSync(fd, "utf8");
89
+ } finally {
90
+ closeSync(fd);
91
+ }
84
92
  }
85
93
 
86
94
  // node_modules/fast-xml-parser/src/util.js
@@ -3766,7 +3774,7 @@ function detectCoverageFormat(text) {
3766
3774
  return "gocover";
3767
3775
  if (head.startsWith("<"))
3768
3776
  return "cobertura";
3769
- if (/^SF:/m.test(head))
3777
+ if (/^\s*SF:/m.test(head))
3770
3778
  return "lcov";
3771
3779
  return;
3772
3780
  }
@@ -3774,20 +3782,11 @@ function parseLcov(text, root) {
3774
3782
  const files = new Map;
3775
3783
  let current;
3776
3784
  let hasBranches = false;
3777
- const perLine = new Map;
3778
- const flush = () => {
3779
- for (const [no, c] of perLine)
3780
- if (current !== undefined)
3781
- addBranches(current, Number(no), c.covered, c.total);
3782
- perLine.clear();
3783
- };
3784
3785
  for (const raw of text.split(/\r?\n/)) {
3785
3786
  const line = raw.trim();
3786
3787
  if (line.startsWith("SF:")) {
3787
- flush();
3788
3788
  current = file(files, normalizePath(line.slice(3), root));
3789
3789
  } else if (line === "end_of_record") {
3790
- flush();
3791
3790
  current = undefined;
3792
3791
  } else if (current !== undefined && line.startsWith("DA:")) {
3793
3792
  const [no, hits] = line.slice(3).split(",");
@@ -3795,16 +3794,22 @@ function parseLcov(text, root) {
3795
3794
  if (Number.isInteger(n))
3796
3795
  mark(current.lines, n, Number(hits) > 0);
3797
3796
  } else if (current !== undefined && line.startsWith("BRDA:")) {
3798
- const [no, , , taken] = line.slice(5).split(",");
3797
+ const [no, block, branch, taken] = line.slice(5).split(",");
3799
3798
  hasBranches = true;
3800
- const c = perLine.get(no) ?? { covered: 0, total: 0 };
3799
+ current.branchIds ??= new Map;
3800
+ mark(current.branchIds, `${no},${block},${branch}`, taken !== "-" && Number(taken) > 0);
3801
+ }
3802
+ }
3803
+ for (const entry of files.values()) {
3804
+ for (const [id, hit] of entry.branchIds ?? []) {
3805
+ const n = Number(id.slice(0, id.indexOf(",")));
3806
+ const c = entry.branches.get(n) ?? { covered: 0, total: 0 };
3801
3807
  c.total++;
3802
- if (taken !== "-" && Number(taken) > 0)
3808
+ if (hit)
3803
3809
  c.covered++;
3804
- perLine.set(no, c);
3810
+ entry.branches.set(n, c);
3805
3811
  }
3806
3812
  }
3807
- flush();
3808
3813
  return { format: "lcov", files, hasBranches };
3809
3814
  }
3810
3815
  var xml = new XMLParser({
@@ -3869,6 +3874,12 @@ function parseCoverage(text, format, root) {
3869
3874
  const kind = format ?? detectCoverageFormat(text);
3870
3875
  if (kind === undefined)
3871
3876
  throw new Error("cannot detect the coverage format; pass format: lcov, cobertura or gocover");
3877
+ if (text.trim() !== "") {
3878
+ if (kind === "lcov" && !/^\s*SF:/m.test(text))
3879
+ throw new Error("not an LCOV report: no SF: record");
3880
+ if (kind === "gocover" && !text.trimStart().startsWith("mode:"))
3881
+ throw new Error("not a Go cover profile: no mode: header");
3882
+ }
3872
3883
  if (kind === "lcov")
3873
3884
  return parseLcov(text, root);
3874
3885
  if (kind === "cobertura")
@@ -3910,7 +3921,14 @@ function compareCoverage(baseline, candidate) {
3910
3921
  const lines = baseLines.filter((n) => cand.lines.get(n) !== true).sort((a, b) => a - b);
3911
3922
  const branches = [];
3912
3923
  let lostBranchCount = 0;
3913
- if (bothBranches) {
3924
+ if (bothBranches && base.branchIds !== undefined && cand.branchIds !== undefined) {
3925
+ for (const [id, hit] of [...base.branchIds].sort()) {
3926
+ if (!hit || cand.branchIds.get(id) === true)
3927
+ continue;
3928
+ branches.push(id.replaceAll(",", ":"));
3929
+ lostBranchCount++;
3930
+ }
3931
+ } else if (bothBranches) {
3914
3932
  for (const [n, count] of [...base.branches].sort((x, y) => x[0] - y[0])) {
3915
3933
  const now = cand.branches.get(n);
3916
3934
  if (count.covered > (now?.covered ?? 0)) {
@@ -4030,10 +4048,10 @@ function nestedStderrLines(node) {
4030
4048
  }
4031
4049
  return lines;
4032
4050
  }
4033
- function collect(node, out) {
4051
+ function collect(node, out, suites = []) {
4034
4052
  if (Array.isArray(node)) {
4035
4053
  for (const item of node)
4036
- collect(item, out);
4054
+ collect(item, out, suites);
4037
4055
  return;
4038
4056
  }
4039
4057
  if (!isNode(node))
@@ -4045,8 +4063,13 @@ function collect(node, out) {
4045
4063
  out.stderrLines += stderrLines(value);
4046
4064
  continue;
4047
4065
  }
4066
+ if (key === "testsuite") {
4067
+ for (const item of Array.isArray(value) ? value : [value])
4068
+ collect(item, out, [...suites, isNode(item) ? String(item["@_name"] ?? "") : ""]);
4069
+ continue;
4070
+ }
4048
4071
  if (key !== "testcase") {
4049
- collect(value, out);
4072
+ collect(value, out, suites);
4050
4073
  continue;
4051
4074
  }
4052
4075
  for (const raw of value) {
@@ -4058,6 +4081,7 @@ function collect(node, out) {
4058
4081
  const time = Number.parseFloat(String(tc["@_time"] ?? "0"));
4059
4082
  const status = caseStatus(tc);
4060
4083
  out.stderrLines += nestedStderrLines(tc);
4084
+ out.paths.push(suites.filter(Boolean).join(" > "));
4061
4085
  out.cases.push({
4062
4086
  id: file ? `${file} :: ${label}` : label,
4063
4087
  status,
@@ -4068,16 +4092,23 @@ function collect(node, out) {
4068
4092
  }
4069
4093
  }
4070
4094
  }
4071
- function parseJunitReport(xml) {
4095
+ function parseJunitReport(xml, stable = false) {
4072
4096
  const valid = XMLValidator.validate(xml);
4073
4097
  if (valid !== true)
4074
4098
  throw new Error(`invalid XML: ${valid.err.msg} (line ${valid.err.line})`);
4075
4099
  const doc = parser.parse(xml);
4076
4100
  if (!("testsuites" in doc) && !("testsuite" in doc))
4077
4101
  throw new Error("not a JUnit report: no testsuite element");
4078
- const out = { cases: [], stderrLines: 0 };
4102
+ const out = { cases: [], paths: [], stderrLines: 0 };
4079
4103
  collect(doc, out);
4080
- return out;
4104
+ if (stable) {
4105
+ return { cases: out.cases.map((c, i) => out.paths[i] ? { ...c, id: `${out.paths[i]} :: ${c.id}` } : c), stderrLines: out.stderrLines };
4106
+ }
4107
+ const suitesOf = new Map;
4108
+ for (const [i, c] of out.cases.entries())
4109
+ suitesOf.set(c.id, (suitesOf.get(c.id) ?? new Set).add(out.paths[i]));
4110
+ const cases = out.cases.map((c, i) => (suitesOf.get(c.id)?.size ?? 0) > 1 ? { ...c, id: `${out.paths[i]} :: ${c.id}` } : c);
4111
+ return { cases, stderrLines: out.stderrLines };
4081
4112
  }
4082
4113
 
4083
4114
  // plugins/quality/shared/mutation.ts
@@ -4180,6 +4211,9 @@ function parseMutation(text, format) {
4180
4211
  const kind = format ?? detectMutationFormat(data);
4181
4212
  if (kind === undefined || !isObj(data))
4182
4213
  throw new Error("cannot detect the mutation format; pass format: mutation-testing, cargo-mutants or gremlins");
4214
+ const shape = { "mutation-testing": isObj(data.files), "cargo-mutants": Array.isArray(data.outcomes), gremlins: Array.isArray(data.files) };
4215
+ if (!shape[kind])
4216
+ throw new Error(`not a ${kind} report: the required top-level ${kind === "cargo-mutants" ? "outcomes array" : kind === "gremlins" ? "files array" : "files object"} is missing`);
4183
4217
  if (kind === "mutation-testing")
4184
4218
  return parseMutationTesting(data);
4185
4219
  if (kind === "cargo-mutants")
@@ -4477,7 +4511,7 @@ function summarizeReports(root, raw, temporaryDirectory = opencodeTempDir()) {
4477
4511
  try {
4478
4512
  const text = readReportFile(root, path, roots);
4479
4513
  record.fileBytes = Buffer.byteLength(text);
4480
- const parsed = parseJunitReport(text);
4514
+ const parsed = parseJunitReport(text, true);
4481
4515
  record.cases = parsed.cases;
4482
4516
  record.stderrLines = parsed.stderrLines;
4483
4517
  record.report = "used";
@@ -4493,6 +4527,15 @@ function summarizeReports(root, raw, temporaryDirectory = opencodeTempDir()) {
4493
4527
  }
4494
4528
  var COVERAGE_FORMATS = ["lcov", "cobertura", "gocover"];
4495
4529
  var MUTATION_FORMATS = ["mutation-testing", "cargo-mutants", "gremlins"];
4530
+ function side(name, parse) {
4531
+ try {
4532
+ return parse();
4533
+ } catch (error) {
4534
+ if (error instanceof PathError)
4535
+ throw error;
4536
+ throw new Error(`${name}: ${error instanceof Error ? error.message : String(error)}`);
4537
+ }
4538
+ }
4496
4539
  function compareReports(root, input, temporaryDirectory = opencodeTempDir()) {
4497
4540
  const fail = (message) => ({ content: `${TEST_COMPARE_TOOL}: ${message}`, metadata: { error: message } });
4498
4541
  if (input.kind !== "coverage" && input.kind !== "mutation")
@@ -4508,11 +4551,11 @@ function compareReports(root, input, temporaryDirectory = opencodeTempDir()) {
4508
4551
  const candText = readReportFile(root, input.candidate, roots);
4509
4552
  if (input.kind === "coverage") {
4510
4553
  const format = input.format;
4511
- const result = compareCoverage(parseCoverage(baseText, format, root), parseCoverage(candText, format, root));
4554
+ const result = compareCoverage(side("baseline", () => parseCoverage(baseText, format, root)), side("candidate", () => parseCoverage(candText, format, root)));
4512
4555
  return { content: formatCoverage(result), metadata: { ...result } };
4513
4556
  }
4514
4557
  const format = input.format;
4515
- const result = compareMutation(parseMutation(baseText, format), parseMutation(candText, format));
4558
+ const result = compareMutation(side("baseline", () => parseMutation(baseText, format)), side("candidate", () => parseMutation(candText, format)));
4516
4559
  const cap = (list) => list.slice(0, 200);
4517
4560
  return {
4518
4561
  content: formatMutation(result),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@srobroek/pi-code-quality",
3
- "version": "0.23.0",
3
+ "version": "0.24.1",
4
4
  "description": "Code design and review guidance, a code-quality policy, testing standards and a test audit, performance profiling, and throwaway logic prototypes.",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -27,7 +27,7 @@ MUST Scale planning and verification by uncertainty, consequences, and reversibi
27
27
 
28
28
  MUST Keep tests in step with the code, following the `quality-testing` skill (and `lang-<language>-testing` for idioms and tools): one behavior per test through the public interface, expected values worked out apart from the code, and a distinct risk per test, so search the existing tests and parametrize instead of copying. A refactor leaves tests unchanged; removed behavior takes its tests and fixtures in the same change. Never add a test that only checks removed code is gone, mirrors the implementation, re-checks what the compiler proves or exists for coverage, and never weaken an assertion or update a snapshot just to pass.
29
29
 
30
- MUST Stub a dependency only when its contract exists and the implementation does not. With no contract, write the contract first; never invent the interface inside the stub. Never stub across a recorded decision (no "temporary" different library, engine or algorithm). Every stub is tracked work: no stub without a tracked swap task. In repositories that track work with bd, see the `beads-ledger` skill's `references/contracts-and-stubs.md` for the beads and edges. Design rules for writing a stub are in `quality-code-design`'s `contracts.md`.
30
+ MUST Stub a dependency only when its contract exists and the implementation does not. With no contract, write the contract first; never invent the interface inside the stub. Never stub across a recorded decision (no "temporary" different library, engine or algorithm). Every stub is tracked work: no stub without a tracked swap task. In repositories that track work with bd, see the `beads-guide` skill's `references/contracts-and-stubs.md` for the beads and edges. Design rules for writing a stub are in `quality-code-design`'s `contracts.md`.
31
31
 
32
32
  MUST A swap replaces the stub with the real implementation, removes every `STUB(<task-id>)` marker, and proves that the real implementation passes the same conformance suite and that the consumer's tests pass against it. A swap that needs more than wiring exposes a contract gap: add a fixture that captures it to the contract in the same change. Before a release, no swap task is open, no `STUB(` marker remains in the tree (a CI grep, not a test), and every contract's staleness check passes.
33
33