// Reproduce a narrow documentation correction with the project's own parser. // This checks document custody and generated fields, not the numerical claims. const fs = require('node:fs'); const path = require('node:path'); const crypto = require('node:crypto'); const assert = require('node:assert/strict'); const { spawnSync } = require('node:child_process'); const sha = s => crypto.createHash('sha256').update(s).digest('hex'); const lf = s => s.replace(/\r\n/g, '\n'); const norm = s => s.replace(/\s+/g, ' ').trim(); const inputText = fs.readFileSync(path.join(__dirname, 'registry-735-inputs.json'), 'utf8'); assert.equal(sha(inputText), '8ec8ca3bc5f4799e5257c0d3597d39ead6a1f10f4523688c6bfd679050151f3f'); const input = JSON.parse(inputText); assert.equal(sha(input.generator), 'e5890d255bfeabc026d60c2c34db066752b1e2d1b57a5cb3d16607ed9ea786b6'); assert.equal(sha(lf(input.registry)), 'e2ddcfc55f89be535041515a26fbc5068db832a7db548d35765b75f5360ff60e'); for (const note of Object.values(input.notes)) assert.equal(sha(lf(note.content)), note.lf_sha256); // Extract the actual primary functions. The adapter only supplies archived // public file contents; it does not run the generator's filesystem or network code. const parserBegin = input.generator.indexOf('function parseBlock(file) {'); const parserEnd = input.generator.indexOf('\nfunction todoItems()', parserBegin); assert(parserBegin >= 0 && parserEnd > parserBegin); const summary = input.generator.match(/const summarise = id => \{[\s\S]*?\n \};/)[0]; const constants = ['STATUSES', 'BLOCK_RE'].map(name => input.generator.match(new RegExp('^const ' + name + ' .+$', 'm'))[0]).join('\n'); const byId = new Map(); const C = { read: file => input.notes[file].content }; const { parseBlock, summarise } = new Function('C', 'path', 'byId', constants + '\n' + input.generator.slice(parserBegin, parserEnd) + '\n' + summary + '\nreturn {parseBlock, summarise};')(C, path, byId); function load(id) { const selected = input.selected.find(row => row.id === id); const entries = selected.notes.map(note => { assert.equal(input.notes[note.path].lf_sha256, note.sha256); const parsed = parseBlock(note.path); assert(parsed.block); assert.equal(parsed.block.id, id); assert.deepEqual(parsed.problems, []); return parsed; }); entries.sort((a, b) => a.file < b.file ? -1 : a.file > b.file ? 1 : 0); byId.set(id, entries); } function cells(line) { const out = []; let current = ''; for (let i = 0; i < line.length; i++) { if (line[i] === '\\' && line[i + 1] === '|') { current += '|'; i++; } else if (line[i] === '|') { out.push(current.trim()); current = ''; } else current += line[i]; } out.push(current.trim()); return out.slice(1, -1); } const questionLines = text => text.split('\n').filter(line => /^\|\s*`Q-[A-Za-z0-9-]+`\s*\|/.test(line)); const allRows = questionLines(input.registry); assert.equal(allRows.length, 554); const before = input.selected.map(row => { load(row.id); const actual = summarise(row.id); const indexed = allRows.find(line => cells(line)[0] === '`' + row.id + '`'); assert(indexed); const c = cells(indexed); const status_ok = norm(actual.status) === norm(c[1]); const verdict_ok = norm(actual.verdict) === norm(c[3]); assert.equal(status_ok, row.status_ok); assert.equal(verdict_ok, row.verdict_ok); return { row: row.row, id: row.id, status_ok, verdict_ok }; }); assert.equal(before.length, 15); assert.deepEqual(before.filter(row => !row.status_ok || !row.verdict_ok).map(row => row.row), [15]); const sourcePath = 'research/history/staging/attack-roughpair-error.md'; const originalSource = input.notes[sourcePath].content; const parsedOriginal = parseBlock(sourcePath).block; assert.equal(parsedOriginal.status, 'PARTIAL'); assert(!input.new_verdict.includes('|')); const oldLine = 'verdict: ' + parsedOriginal.verdict; assert.equal(originalSource.split(oldLine).length - 1, 1); const revisedSource = originalSource.replace(oldLine, 'verdict: ' + input.new_verdict); assert.equal(revisedSource.replace('verdict: ' + input.new_verdict, oldLine), originalSource); assert.equal(revisedSource.split('-->\n')[1], originalSource.split('-->\n')[1]); input.notes[sourcePath].content = revisedSource; // Keep the original hash in the input custody record; only the parser reads // revised content here. Load directly rather than asserting original custody twice. const revisedParsed = parseBlock(sourcePath); assert.deepEqual(revisedParsed.problems, []); for (const key of Object.keys(parsedOriginal)) { if (key !== 'verdict') assert.deepEqual(revisedParsed.block[key], parsedOriginal[key]); } byId.set('Q-roughpair-error', [revisedParsed]); const proposed = summarise('Q-roughpair-error'); assert.equal(proposed.status, 'PARTIAL'); assert.equal(norm(proposed.verdict), norm(input.new_verdict)); let changedLines = 0; const revisedRegistry = input.registry.split('\n').map(line => { if (!line.startsWith('|') || !line.includes('`Q-roughpair-error`')) return line; const c = cells(line); const isFullTable = c[0] === '`Q-roughpair-error`'; assert.equal(c[isFullTable ? 1 : 2], 'PARTIAL'); assert.equal(norm(c[3]), norm(parsedOriginal.verdict)); assert.equal(line.split(c[3]).length - 1, 1); changedLines++; return line.replace(c[3], proposed.verdict.replace(/\|/g, '\\|')); }).join('\n'); assert.equal(changedLines, 2); const revisedRows = questionLines(revisedRegistry); assert.equal(revisedRows.length, 554); assert.equal(allRows.filter(line => ['OPEN', 'PARTIAL'].includes(cells(line)[1])).length, 97); assert.equal(revisedRows.filter(line => ['OPEN', 'PARTIAL'].includes(cells(line)[1])).length, 97); assert.equal(allRows.filter((line, i) => line === revisedRows[i]).length, 553); const oldLines = input.registry.split('\n'); const newLines = revisedRegistry.split('\n'); assert.equal(oldLines.length, newLines.length); assert.equal(oldLines.filter((line, i) => line !== newLines[i]).length, 2); for (const line of revisedRows.filter(line => cells(line)[0] === '`Q-roughpair-error`')) { assert.equal(norm(cells(line)[3]), norm(proposed.verdict)); } // Negative control: the old source still generates the old verdict and cannot // justify the two revised rows. Merely editing the index fails this gate. input.notes[sourcePath].content = originalSource; byId.set('Q-roughpair-error', [parseBlock(sourcePath)]); assert.equal(norm(summarise('Q-roughpair-error').verdict), norm(parsedOriginal.verdict)); assert.notEqual(norm(summarise('Q-roughpair-error').verdict), norm(proposed.verdict)); const files = [ { path: sourcePath, old: originalSource, revised: revisedSource, output: 'registry-735-roughpair-error.md' }, { path: 'research/QUESTIONS.md', old: input.registry, revised: revisedRegistry, output: 'registry-735-QUESTIONS.md' } ]; for (const file of files) fs.writeFileSync(path.join(__dirname, file.output), file.revised); const patchResult = spawnSync('python3', ['-c', 'import json,sys,difflib\nfiles=json.load(sys.stdin)\nfor f in files:\n sys.stdout.write("".join(difflib.unified_diff(f["old"].splitlines(True), f["revised"].splitlines(True), fromfile="a/"+f["path"], tofile="b/"+f["path"])))\n'], { input: JSON.stringify(files), encoding: 'utf8' }); assert.equal(patchResult.status, 0, patchResult.stderr); fs.writeFileSync(path.join(__dirname, 'registry-735.patch'), patchResult.stdout); const result = { job_id: 735, input_sha256: sha(inputText), primary_generator_sha256: sha(input.generator), original_registry_sha256: sha(input.registry), fresh_source_notes: 22, sweep_14_28: before, mechanical_stale_rows: [15], existing_owner: 'pending audit 220; own 305 duplicate', changed_source_verdict_lines: 1, changed_index_entries: 2, other_full_table_rows_byte_unchanged: 553, source_status_preserved: 'PARTIAL', all_other_ledger_fields_and_body_byte_unchanged: true, full_registry_rows: 554, full_open_partial_before: 97, full_open_partial_after: 97, primary_parser_revised_verdict_matches_both_index_rows: true, old_source_negative_control_rejects_revised_index: true, revised_source_sha256: sha(revisedSource), revised_registry_sha256: sha(revisedRegistry), patch_sha256: sha(patchResult.stdout), null_note_sha256: sha(input.null_note.content ?? input.null_note), scope: 'Documentary correction only. No new numerical run, accepted mathematics, asymptotic law, status closure, full registry generation, or integration.' }; process.stdout.write(JSON.stringify(result, null, 2) + '\n');