refactor: simplify pass (trekexecute)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-10-24 22:22:18 +02:00
commit af90e82794
Signed by: ktg
SSH key fingerprint: SHA256:JakMjO6FTBBzN0Bhfj9saOoEjaFxlSdYuZQQpM/lF9Q
12 changed files with 303 additions and 186 deletions

View file

@ -9,7 +9,7 @@ import { strict as assert } from 'node:assert';
import { readFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { illegalBriefLowering, severityRank as rank } from '../../lib/review/coordinator-contract.mjs';
import { dedupByTriplet, illegalBriefLowering, severityRank as rank } from '../../lib/review/coordinator-contract.mjs';
import { ENGINES } from '../../lib/review/engine-findings.mjs';
import { RULE_CATALOGUE } from '../../lib/review/rule-catalogue.mjs';
@ -719,6 +719,90 @@ test('[ORD] sibling: workflow — the title classes are placed in alphabetical o
}
});
// ---- Parity: the workflow's inlined dedup groups as coordinator-contract Pass 1 does ----
//
// The script cannot import, so its groupTriplet is a hand-kept copy of
// lib/review/coordinator-contract.mjs's. Both run on the ORD fixtures of this
// file and of tests/lib/coordinator-contract.test.mjs, plus the WF floor
// fixture below (the one where two titles from two sources share a row), in
// every arrival order within a payload, and must give the same rows in the
// same order. Each finding
// carries its workflow source as `reviewer` (what the contract groups and
// orders by) and, from /code-review, `engine` last (where the workflow puts
// it), so both read the same objects. A row is compared by its survivor and its
// group's severity.
/** Every order of `xs`. */
function permutations(xs) {
if (xs.length <= 1) return [xs];
return xs.flatMap((x, i) => permutations([...xs.slice(0, i), ...xs.slice(i + 1)]).map((rest) => [x, ...rest]));
}
const SRC_F = 'reviewer:brief-conformance';
const SRC_C = 'reviewer:code-correctness';
const SRC_E = 'engine:claude-code-review';
const from = (src, f) => ({ ...f, reviewer: src, ...(src === SRC_E ? { engine: 'code-review' } : {}) });
const rowSig = (r) => JSON.stringify([r.title ?? null, r.severity, r.engine ?? null, r.contradicts_brief ?? null, r.recommended_action ?? null]);
function parityFixtures() {
const out = [];
const add = (name, { f = [], c = [], e = [] }) => out.push({
name, f: f.map((x) => from(SRC_F, x)), c: c.map((x) => from(SRC_C, x)), e: e.map((x) => from(SRC_E, x)),
});
// tests/lib/coordinator-contract.test.mjs
const slug = { file: 'bin/slug.mjs', line: 46, detail: 'd' };
for (const [rule_key, a, b, cb] of [['MISSING_TEST', 'MAJOR', 'MAJOR', 'MAJOR'], ['NOT_IN_THE_CATALOGUE', 'MINOR', 'MAJOR', 'MINOR']]) {
add(`cA ${a}, fB ${b}, cB ${cb} (${rule_key})`, {
f: [{ ...slug, rule_key, severity: b, title: 'B' }],
c: [{ ...slug, rule_key, severity: a, title: 'A' }, { ...slug, rule_key, severity: cb, title: 'B' }],
});
}
const off = { ...slug, rule_key: 'NOT_IN_THE_CATALOGUE' };
add('cA, fB, cB and an untitled correctness BLOCKER', {
f: [{ ...off, severity: 'MAJOR', title: 'B' }],
c: [{ ...off, severity: 'BLOCKER', title: 'A', recommended_action: '' }, { ...off, severity: 'MINOR', title: 'B' }, { ...off, severity: 'BLOCKER' }],
});
const inj = { file: 'x.mjs', line: 3, rule_key: 'SECURITY_INJECTION', severity: 'BLOCKER' };
add('one reviewer\'s two same-title BLOCKERs', {
c: [{ ...inj, title: 'Injection', recommended_action: '' }, { ...inj, title: 'injection ', recommended_action: 'Quote the argument.' }],
});
const t = { file: 'x.mjs', line: 3, rule_key: 'CODE_REVIEW_DEFECT', severity: 'MAJOR', title: 'T' };
add('two /code-review MAJORs titled T, one marked', { e: [{ ...t, contradicts_brief: MARK }, { ...t }] });
// this file
const K = 'CODE_REVIEW_DEFECT';
add('conformance B with /code-review A and B', {
f: [F(K, 'MINOR', { title: 'B' })], e: [F(K, 'MINOR', { title: 'A' }), F(K, 'MAJOR', { title: 'B' })],
});
add('a conformance R next to /code-review E1 and E2 (WF)', {
f: [F(K, 'MAJOR', { title: 'R' })], e: [F(K, 'MINOR', { title: 'E1' }), F(K, 'MINOR', { title: 'E2' })],
});
const { title: _t, ...untitled } = F(K, 'BLOCKER');
add('conformance B with /code-review A, B and an untitled BLOCKER', {
f: [F(K, 'MAJOR', { title: 'B' })], e: [F(K, 'BLOCKER', { title: 'A' }), F(K, 'MINOR', { title: 'B' }), untitled],
});
return out;
}
test('parity: the workflow dedup and coordinator-contract dedupByTriplet give the same rows on the ORD fixtures, in every arrival order within a payload', async () => {
let runs = 0;
for (const fx of parityFixtures()) {
for (const f of permutations(fx.f)) for (const c of permutations(fx.c)) for (const e of permutations(fx.e)) {
const name = `${fx.name}, order ${[...f, ...c, ...e].map((x) => `${x.reviewer}/${x.title ?? 'U'}`).join(', ')}`;
const s = stubs({
reviewers: [{ reviewer: 'brief-conformance', findings: structuredClone(f) }, { reviewer: 'code-correctness', findings: structuredClone(c) }],
coordinator: { verdict: 'WARN', findings: [] },
});
await exec({ ...BASE, enginePayloads: BOTH_ENGINES(structuredClone(e)) }, s);
const prompt = s.calls.coordinatorPrompt;
assert.ok(typeof prompt === 'string', `${name}: the coordinator ran`);
const merged = JSON.parse(prompt.slice(prompt.indexOf(PROMPT_JSON_MARK) + PROMPT_JSON_MARK.length));
assert.deepEqual(merged.map(rowSig), dedupByTriplet(structuredClone([...f, ...c, ...e])).map(rowSig), name);
runs++;
}
}
assert.equal(runs, 24, 'control: every fixture ran in each of its arrival orders');
});
// ---- WF: the floor and the put-back match each returned finding to its own row ----
//
// A row is a merged finding; its key is its triplet plus its normalized title.