#!/usr/bin/env node // Job #622: does the display rule pick the NEWEST note? // // The mirror this was written against has ONE commit, so `git log` gives every // note the same date and the rule cannot be exercised through fileLastDates(). // That is exactly why the choice is a pure function: pickNewest(group, dates, rel) // takes the date map as an argument, so the rule is checked here against an // injected map on a REAL group from the corpus. // // Run from the repository root: node pick-newest-check.js // No compute, no network, instant. 'use strict'; const path = require('path'); const Q = require(path.join(process.cwd(), 'research/qc/questions.js')); const { rows } = Q.collect(); const byId = new Map(); for (const r of rows) { if (!r.block || !r.block.id) continue; if (!byId.has(r.block.id)) byId.set(r.block.id, []); byId.get(r.block.id).push(r); } const rel = Q.rel ? Q.rel : f => path.relative(process.cwd(), f).split(path.sep).join('/'); let fails = 0, total = 0; const ok = (cond, label) => { total++; console.log(` ${cond ? 'ok ' : 'FAIL'} ${label}`); if (!cond) fails++; }; const names = g => g.map(x => path.basename(x.file)).join(' , '); const pick = (g, map) => path.basename(Q.pickNewest(g, new Map(Object.entries(map)), rel).file); console.log('pickNewest: group = the notes sharing one id, dates = Map(relpath -> date)\n'); // 1. The case that motivated the rule (job #609). Q-rho-maximal-law has two // notes that AGREE on PARTIAL and DISAGREE on the verdict; path order put // rho-maximal-law.md last, so the row showed the 2026-08-19 text and hid the // 2026-08-28 note that had moved past it. console.log('1. the real group, both notes dated'); const rho = byId.get('Q-rho-maximal-law'); console.log(' group (path order):', names(rho)); ok(rho.length === 2, 'the group has two notes'); ok(rho[rho.length - 1].file.endsWith('rho-maximal-law.md'), 'path order ends on rho-maximal-law.md (what the old rule displayed)'); const dated = {}; rho.forEach((x, i) => { dated[rel(x.file)] = i === 0 ? '2026-08-28' : '2026-08-19'; }); ok(pick(rho, dated) === 'rho-exact-z31-01.md', 'with 2026-08-28 > 2026-08-19 the rule picks rho-exact-z31-01.md (the NEWEST, not the path-order last)'); // 2. The other direction, so the check is not one-way. const flipped = {}; rho.forEach((x, i) => { flipped[rel(x.file)] = i === 0 ? '2026-08-19' : '2026-08-28'; }); ok(pick(rho, flipped) === 'rho-maximal-law.md', 'flipping the dates flips the choice'); // 3. Equal dates: the old rule survives as the tiebreak, and the result is // deterministic (the last in path order within the newest-dated set). const same = {}; rho.forEach(x => { same[rel(x.file)] = '2026-08-19'; }); ok(pick(rho, same) === 'rho-maximal-law.md', 'equal dates keep the last-in-path-order choice'); ok(pick(rho, same) === pick(rho, same), 'and it is deterministic'); // 4. No git at all (an empty map): byte-for-byte the behaviour this replaced. ok(pick(rho, {}) === 'rho-maximal-law.md', 'an empty date map is the old path-order rule'); // 5. A file git does not know counts as NEW, which is the parity gate's // convention in the same file ('an untracked note is new'). Here that is the // newest-dated set, so the undated note wins and the tiebreak is path order. const partial = { [rel(rho[0].file)]: '2026-08-28' }; ok(pick(rho, partial) === 'rho-maximal-law.md', 'a single undated note is treated as newest (rho-maximal-law.md, the undated one)'); // 6. Three notes, newest in the MIDDLE of path order: proves the rule is not a // path-order trick. Uses a real 3-note group from the corpus. const tri = ['Q-quadpoint-transplant', 'Q-records-placement', 'Q-varE-limit'].filter(id => (byId.get(id) || []).length >= 3); console.log('\n2. a real 3+ note group'); const g3 = byId.get(tri[0]); console.log(' group (path order):', names(g3)); const mid = {}; g3.forEach((x, i) => { mid[rel(x.file)] = i === Math.floor(g3.length / 2) ? '2026-09-01' : '2026-08-01'; }); const pickMid = pick(g3, mid); ok(pickMid === path.basename(g3[Math.floor(g3.length / 2)].file), `the newest date wins wherever it sits in path order (picked ${pickMid})`); console.log(`\n${fails ? fails + ' FAILED' : 'all checks pass'} (${total} assertions)`); process.exit(fails ? 1 : 0);