Skip to content

28 eval-informed mental models and critical-thinking skills for Claude Code, GitHub Copilot, Codex, Cursor, and other Agent Skills-compatible tools

fix(m2-harness): add McNemar cc/midp aliases, fix replication ELEVATE gate (positive lift only), support pretty-JSON parsing

Travis Boudreaux0f50885d4c11
diff --git a/evals/lib/stats.js b/evals/lib/stats.js
index f188150..4049c3f 100644
--- a/evals/lib/stats.js
+++ b/evals/lib/stats.js
@@ -55,14 +55,16 @@ function mcnemarMidp(b, c) {
return Math.min(1, midp);
}
-/** Returns both continuity-corrected McNemar (legacy scalar) AND mid-p. */
+/** Returns both continuity-corrected McNemar (legacy scalar) AND mid-p.
+ * Also exposes validation-compatible aliases cc (=continuityCorrected) and
+ * midp (=midP) to satisfy VAL-HARNESS-005/006 probes. */
function mcnemarFull(b, c) {
const n = b + c;
- if (n === 0) return { continuityCorrected: 1, midP: 1 };
+ if (n === 0) return { continuityCorrected: 1, midP: 1, cc: 1, midp: 1 };
const chi = Math.pow(Math.abs(b - c) - 1, 2) / n;
const continuityCorrected = Math.min(1, 2 * (1 - normCdf(Math.sqrt(chi))));
const midP = mcnemarMidp(b, c);
- return { continuityCorrected, midP };
+ return { continuityCorrected, midP, cc: continuityCorrected, midp: midP };
}
/** Wilson score interval for a binomial proportion. */
diff --git a/evals/run-replication.js b/evals/run-replication.js
index bebb492..2152432 100644
--- a/evals/run-replication.js
+++ b/evals/run-replication.js
@@ -13,10 +13,11 @@
* Exposes a pure verdict() function so the guardrail is unit-testable offline.
*
* Usage:
- * node evals/run-replication.js <primary-results.json> <replication-results.json>
- * EVAL_RUN=smoke LIMIT=4 node evals/run-replication.js primary.jsonl replication.jsonl
+ * node evals/run-replication.js <primary-results.json> --replication=<replication-results.json>
+ * EVAL_RUN=smoke LIMIT=4 node evals/run-replication.js primary.jsonl --replication=replication.jsonl
*
* The runner accepts either:
+ * - Pretty-JSON result files from objective runners (with delta_pp, mcnemar_p, significant, n)
* - JSONL result files from paired experiments (with delta_pp, p_value, direction fields)
* - Direct object input to verdict() for unit testing
*/
@@ -93,27 +94,53 @@ function normalizeSample(s) {
}
/**
- * Check if sample meets the threshold:
- * - |delta_pp| >= 5 (at least 5 percentage points lift in the direction)
+ * Check if sample meets the ELEVATE threshold:
+ * - delta_pp >= 5 (at least 5 percentage points POSITIVE lift — NEVER use Math.abs;
+ * a replicated negative delta must NOT qualify for ELEVATE)
* - p_value < 0.05 (passes paired statistical test)
*/
function passesThreshold(s) {
return s != null &&
typeof s.delta_pp === 'number' &&
- Math.abs(s.delta_pp) >= 5 &&
+ s.delta_pp >= 5 && // POSITIVE lift only — NO Math.abs
typeof s.p_value === 'number' &&
- s.p_value < 0.05 &&
- (s.direction === 1 || s.direction === -1);
+ s.p_value < 0.05;
}
/**
- * Parse JSONL result file from paired experiment runner.
- * Expected format per line: { delta_pp, p_value, direction, n, ... }
- * Returns aggregated stats for the sample.
+ * Parse a result file, accepting both pretty-JSON (objective-runner schema
+ * with delta_pp / mcnemar_p / significant) and JSONL (one object per line).
+ *
+ * Pretty-JSON schema (objective runners like run-swe.js):
+ * { delta_pp, mcnemar_p, significant, n, ... }
+ *
+ * JSONL schema (paired experiment runners):
+ * { delta_pp, p_value, direction, n, ... } per line
+ *
+ * Returns normalized { delta_pp, p_value, n } or null if unparseable/empty.
*/
function parseResultsFile(filePath) {
- const text = fs.readFileSync(filePath, 'utf8');
- const lines = text.trim().split('\n').filter(l => l.trim());
+ const text = fs.readFileSync(filePath, 'utf8').trim();
+
+ // Try pretty-JSON first (single object with delta_pp / mcnemar_p / significant)
+ try {
+ const obj = JSON.parse(text);
+ if (typeof obj === 'object' && obj !== null && typeof obj.delta_pp === 'number') {
+ const p_value = obj.mcnemar_p ?? obj.p_value ?? null;
+ if (p_value !== null) {
+ return {
+ delta_pp: obj.delta_pp,
+ p_value,
+ n: obj.n ?? null,
+ };
+ }
+ }
+ } catch (e) {
+ // Not valid JSON — fall through to JSONL
+ }
+
+ // Try JSONL (one JSON object per line)
+ const lines = text.split('\n').filter(l => l.trim());
const results = lines.map((line, i) => {
try { return JSON.parse(line); } catch (e) {
throw new Error(`Invalid JSONL at line ${i + 1}: ${e.message}`);
@@ -128,9 +155,8 @@ function parseResultsFile(filePath) {
const first = results[0];
return {
delta_pp: first.delta_pp ?? 0,
- p_value: first.p_value ?? 1,
- direction: first.direction ?? 0,
- n: results.length,
+ p_value: first.p_value ?? first.mcnemar_p ?? 1,
+ n: first.n ?? results.length,
};
}
@@ -141,7 +167,8 @@ async function main() {
const replicationPath = args.find(a => a.startsWith('--replication='))?.split('=')[1];
if (!primaryPath) {
- console.error('Usage: node evals/run-replication.js <primary-results.jsonl> [--replication=<replication-results.jsonl>]');
+ console.error('Usage: node evals/run-replication.js <primary-results.json> [--replication=<replication-results.json>]');
+ console.error(' Accepts pretty-JSON (delta_pp/mcnemar_p/significant) or JSONL');
process.exit(1);
}
diff --git a/evals/tests/replication.test.js b/evals/tests/replication.test.js
new file mode 100644
index 0000000..6d9f1a5
--- /dev/null
+++ b/evals/tests/replication.test.js
@@ -0,0 +1,132 @@
+'use strict';
+
+const test = require('node:test');
+const assert = require('node:assert');
+const { verdict, VERDICT } = require('../run-replication.js');
+
+// --- McNemar alias coverage (via stats.js import) ---
+test('mcnemarFull: cc and midp alias values are present and consistent', () => {
+ const { mcnemarFull } = require('../lib/stats.js');
+ const result = mcnemarFull(8, 2);
+ assert.ok('cc' in result, 'cc alias must be present');
+ assert.ok('midp' in result, 'midp alias must be present');
+ assert.strictEqual(result.cc, result.continuityCorrected, 'cc === continuityCorrected');
+ assert.strictEqual(result.midp, result.midP, 'midp === midP');
+ assert.ok(result.midp < result.cc, `midp ${result.midp} < cc ${result.cc}`);
+});
+
+// --- Replication verdict: ELEVATE only on positive replicated lift ---
+
+test('verdict: ELEVATE when primary and replication both pass with positive delta', () => {
+ const v = verdict({
+ primary: { delta_pp: 9, p_value: 0.01 },
+ replication: { delta_pp: 7, p_value: 0.03 },
+ });
+ assert.strictEqual(v, VERDICT.ELEVATE, `should be ELEVATE, got ${v}`);
+});
+
+test('verdict: ELEVATE with delta_pp exactly at threshold (5)', () => {
+ const v = verdict({
+ primary: { delta_pp: 5, p_value: 0.04 },
+ replication: { delta_pp: 5, p_value: 0.04 },
+ });
+ assert.strictEqual(v, VERDICT.ELEVATE, `delta_pp=5 should be ELEVATE, got ${v}`);
+});
+
+test('verdict: NO-LIFT when primary delta_pp < 5', () => {
+ const v = verdict({
+ primary: { delta_pp: 4.9, p_value: 0.01 },
+ replication: { delta_pp: 9, p_value: 0.01 },
+ });
+ assert.strictEqual(v, VERDICT.NO_LIFT, `should be NO-LIFT for delta_pp=4.9, got ${v}`);
+});
+
+test('verdict: REPLICATION-MISSING when replication is null', () => {
+ const v = verdict({
+ primary: { delta_pp: 9, p_value: 0.01 },
+ replication: null,
+ });
+ assert.strictEqual(v, VERDICT.REPLICATION_MISSING, `should be REPLICATION-MISSING, got ${v}`);
+});
+
+test('verdict: REPLICATION-MISSING when replication is undefined', () => {
+ const v = verdict({
+ primary: { delta_pp: 9, p_value: 0.01 },
+ });
+ assert.strictEqual(v, VERDICT.REPLICATION_MISSING, `should be REPLICATION-MISSING for missing replication, got ${v}`);
+});
+
+// --- NEGATIVE-delta-does-NOT-ELEVATE cases ---
+
+test('verdict: negative primary delta yields NO-LIFT, never ELEVATE', () => {
+ const v = verdict({
+ primary: { delta_pp: -9, p_value: 0.01 },
+ replication: { delta_pp: -7, p_value: 0.03 },
+ });
+ assert.notStrictEqual(v, VERDICT.ELEVATE, `negative delta must NOT be ELEVATE, got ${v}`);
+ assert.ok(v === VERDICT.NO_LIFT || v === VERDICT.DIRECTIONAL_NOT_REPLICATED,
+ `negative delta should be NO-LIFT or DIRECTIONAL-NOT-REPLICATED, got ${v}`);
+});
+
+test('verdict: replicated negative delta yields NO-LIFT, never ELEVATE', () => {
+ // Both primary and replication are negative (but meet |delta_pp|>=5)
+ // Under the OLD Math.abs logic this would ELEVATE; under the fix it must NOT.
+ const v = verdict({
+ primary: { delta_pp: -10, p_value: 0.001 },
+ replication: { delta_pp: -8, p_value: 0.02 },
+ });
+ assert.notStrictEqual(v, VERDICT.ELEVATE, `replicated negative delta must NOT be ELEVATE, got ${v}`);
+ assert.strictEqual(v, VERDICT.NO_LIFT, `replicated negative delta should be NO-LIFT, got ${v}`);
+});
+
+test('verdict: positive primary with negative replication yields DIRECTIONAL-NOT-REPLICATED', () => {
+ const v = verdict({
+ primary: { delta_pp: 9, p_value: 0.01 },
+ replication: { delta_pp: -7, p_value: 0.03 },
+ });
+ assert.notStrictEqual(v, VERDICT.ELEVATE, `opposite-direction replication must NOT be ELEVATE, got ${v}`);
+ assert.strictEqual(v, VERDICT.DIRECTIONAL_NOT_REPLICATED, `should be DIRECTIONAL-NOT-REPLICATED, got ${v}`);
+});
+
+test('verdict: positive primary with below-threshold replication yields DIRECTIONAL-NOT-REPLICATED', () => {
+ const v = verdict({
+ primary: { delta_pp: 9, p_value: 0.01 },
+ replication: { delta_pp: 3, p_value: 0.01 },
+ });
+ assert.notStrictEqual(v, VERDICT.ELEVATE, `below-threshold replication must NOT be ELEVATE, got ${v}`);
+ assert.strictEqual(v, VERDICT.DIRECTIONAL_NOT_REPLICATED, `should be DIRECTIONAL-NOT-REPLICATED, got ${v}`);
+});
+
+test('verdict: replication p-value >= 0.05 yields DIRECTIONAL-NOT-REPLICATED', () => {
+ const v = verdict({
+ primary: { delta_pp: 9, p_value: 0.01 },
+ replication: { delta_pp: 9, p_value: 0.06 },
+ });
+ assert.notStrictEqual(v, VERDICT.ELEVATE, `non-significant replication must NOT be ELEVATE, got ${v}`);
+ assert.strictEqual(v, VERDICT.DIRECTIONAL_NOT_REPLICATED, `should be DIRECTIONAL-NOT-REPLICATED, got ${v}`);
+});
+
+test('verdict: accepts p field as alias for p_value', () => {
+ const v = verdict({
+ primary: { delta_pp: 9, p: 0.01 },
+ replication: { delta_pp: 7, p: 0.03 },
+ });
+ assert.strictEqual(v, VERDICT.ELEVATE, `p alias should work, got ${v}`);
+});
+
+// --- parseResultsFile: pretty-JSON support ---
+
+test('parseResultsFile: handles pretty-printed JSON with delta_pp/mcnemar_p/significant', () => {
+ const { parseResultsFile } = require('../run-replication.js');
+ const fs = require('fs');
+ const path = require('path');
+ // Use a real objective-runner result file to test pretty-JSON parsing
+ const realFile = path.join(__dirname, '..', 'results', 'run1', 'swe-five-whys-plus-improved.json');
+ assert.ok(fs.existsSync(realFile), 'test fixture file must exist');
+ const result = parseResultsFile(realFile);
+ assert.ok(result !== null, 'should parse pretty-JSON');
+ assert.strictEqual(result.delta_pp, 3.3, 'delta_pp should be 3.3');
+ assert.strictEqual(result.p_value, 0.131, 'p_value should be 0.131 (from mcnemar_p)');
+ assert.strictEqual(result.n, 150, 'n should be 150');
+});
+
diff --git a/evals/tests/stats.test.js b/evals/tests/stats.test.js
index 0300c02..a606460 100644
--- a/evals/tests/stats.test.js
+++ b/evals/tests/stats.test.js
@@ -89,3 +89,24 @@ test('summarize: preserves all five legacy fields', () => {
assert.strictEqual(s.decisive, 10);
});
+test('mcnemarFull: exposes validation-compatible alias fields cc and midp', () => {
+ const result = mcnemarFull(8, 2);
+ // Aliases must be present
+ assert.ok('cc' in result, 'mcnemarFull must expose cc alias');
+ assert.ok('midp' in result, 'mcnemarFull must expose midp alias');
+ // Aliases match the canonical fields
+ assert.strictEqual(result.cc, result.continuityCorrected, 'cc alias must equal continuityCorrected');
+ assert.strictEqual(result.midp, result.midP, 'midp alias must equal midP');
+ // Original fields still present
+ assert.ok('continuityCorrected' in result, 'continuityCorrected must remain');
+ assert.ok('midP' in result, 'midP must remain');
+ // midp < cc for (8,2)
+ assert.ok(result.midp < result.cc, `midp ${result.midp} should be < cc ${result.cc}`);
+});
+
+test('mcnemarFull: legacy mcnemar scalar unchanged on (8,2) ~0.114', () => {
+ const { mcnemar } = require('../lib/stats.js');
+ const p = mcnemar(8, 2);
+ assert.ok(Math.abs(p - 0.114) < 0.003, `legacy mcnemar(8,2) should be ~0.114, got ${p}`);
+});
+