'use strict'; const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const crypto = require('node:crypto'); const assert = require('node:assert/strict'); const source = process.argv[2]; if (!source) throw new Error('Usage: node ledger-join.js SOURCE_DIRECTORY'); const inputs = {}; const read = name => { const bytes = fs.readFileSync(path.join(source, name)); inputs[name] = crypto.createHash('sha256').update(bytes).digest('hex'); return bytes.toString('utf8'); }; const moduleText = read('questions.js'); const paths = [ 'research/centered-discrepancy-estimate.md', 'research/global-factor-signs.md', 'research/history/staging/xchan-at29-prereg.md', 'research/history/staging/shadow-prereg.md', ]; const current = Object.fromEntries(paths.map(name => [name, read(name.replaceAll('/', '__'))])); const corrected = {...current, [paths[0]]: read('accepted-83.md'), [paths[1]]: read('accepted-153.md'), }; const registry = read('research__QUESTIONS.md'); const oldRegistry = read('accepted-80.md'); function render(notes) { const corpus = { ROOT: '/fixture', RESEARCH: '/fixture/research', bodyMarkdown: paths.filter(name => !name.includes('/history/')).map(name => '/fixture/' + name), historyMarkdown: paths.filter(name => name.includes('/history/')).map(name => '/fixture/' + name), rel: file => file.replace(/^\/fixture\//, ''), norm: text => String(text).replace(/\s+/g, ' ').trim(), read: file => { const key = file.replace(/^\/fixture\//, ''); if (!(key in notes)) throw new Error('Unexpected fixture read: ' + key); return notes[key]; }, }; const module = {exports: {}}; const fixtureRequire = name => { if (name === './corpus') return corpus; if (name === 'path') return path; if (name === 'fs') return { existsSync: () => false, readFileSync: () => { throw new Error('Unexpected direct filesystem read'); }, writeFileSync: () => { throw new Error('Fixture must not write an index'); }, }; if (name === 'child_process') return {execFileSync: () => ''}; throw new Error('Unexpected generator dependency: ' + name); }; vm.runInNewContext(moduleText, {module, require: fixtureRequire}, {timeout: 1000}); const collected = module.exports.collect().rows; for (const row of collected) assert.deepEqual(Array.from(row.problems || []), []); return { text: module.exports.renderQuestions().text, blocks: Object.fromEntries(collected.map(row => [row.block.id, { path: corpus.rel(row.file), status: row.block.status, verdict: row.block.verdict, }])), }; } function rows(text, ids) { const result = Object.fromEntries(ids.map(id => [id, []])); for (const [offset, line] of text.split('\n').entries()) { const byId = /^\|\s*`(Q-[^`]+)`\s*\|\s*([A-Z]+)\s*\|/.exec(line); const byTodo = /^\|\s*[^|]*\|\s*`(Q-[^`]+)`[^|]*\|\s*([A-Z]+)\s*\|/.exec(line); const match = byId || byTodo; if (match && Object.hasOwn(result, match[1])) { result[match[1]].push({layout: byId ? 'by-id' : 'by-todo', status: match[2], line: offset + 1, text: line}); } } for (const id of ids) { assert.equal(result[id].length, 2, 'Both registry layouts must be present: ' + id); assert.deepEqual(result[id].map(row => row.layout).sort(), ['by-id', 'by-todo']); } return result; } const fromCurrent = render(current); const fromCorrected = render(corrected); const ids = Object.keys(fromCurrent.blocks).sort(); const servedRows = rows(registry, ids); const oldRows = rows(oldRegistry, ids); const generatedRows = rows(fromCurrent.text, ids); const correctedRows = rows(fromCorrected.text, ids); const comparisons = ids.map(id => ({ id, source: fromCurrent.blocks[id], accepted_source_disposition: fromCorrected.blocks[id].status, served_registry_statuses: servedRows[id].map(row => row.status), reference_registry_80_statuses: oldRows[id].map(row => row.status), generated_from_current_statuses: generatedRows[id].map(row => row.status), after_two_source_restorations_statuses: correctedRows[id].map(row => row.status), stale_against_current_source: servedRows[id].some(row => row.status !== fromCurrent.blocks[id].status), stale_against_accepted_source: servedRows[id].some(row => row.status !== fromCorrected.blocks[id].status), source_needs_restoration: fromCurrent.blocks[id].status !== fromCorrected.blocks[id].status, old_registry_snapshot_status_diff: servedRows[id].some((row, i) => row.status !== oldRows[id][i].status), })); assert.equal(comparisons.length, 4); assert.equal(comparisons.filter(row => row.stale_against_current_source).length, 1); assert.equal(comparisons.filter(row => row.source_needs_restoration).length, 2); assert.equal(comparisons.filter(row => row.stale_against_accepted_source).length, 3); assert.equal(comparisons.filter(row => row.old_registry_snapshot_status_diff).length, 0); for (const row of comparisons) { assert.deepEqual(row.generated_from_current_statuses, [row.source.status, row.source.status]); } const altered = {...current, [paths[3]]: current[paths[3]].replace('status: ANSWERED', 'status: OPEN'), }; assert.equal(render(altered).blocks['Q-shadow-prereg'].status, 'OPEN'); console.log(JSON.stringify({ scope: 'Four explicit question/source pairs, not a corpus-wide count or a full regeneration', input_sha256: inputs, comparisons, counters: {current_source_drift: 1, source_restorations_needed: 2, accepted_source_vs_registry: 3, reference_registry_status_diff: 0}, controls: {four_sources_present: true, both_layouts_present: true, generation_follows_source_mutation: true, no_generated_file_written: true}, }));