|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * UnalignedLedgerV1 — machine residual ledger for Grok vs Codex HostParity (E1/E8). |
| 5 | + * Authority: scripts/fixtures/host-parity/unaligned-ledger.v1.json |
| 6 | + */ |
| 7 | + |
| 8 | +const fs = require('fs') |
| 9 | +const path = require('path') |
| 10 | + |
| 11 | +const SCHEMA_VERSION = 'UnalignedLedgerV1' |
| 12 | +const REQUIRED_IDS = Object.freeze([ |
| 13 | + 'U-A1', 'U-A2', 'U-A3', 'U-A4', |
| 14 | + 'U-B1', 'U-B2', 'U-B3', 'U-B4', |
| 15 | + 'U-C1', 'U-C2', 'U-C3' |
| 16 | +]) |
| 17 | + |
| 18 | +const DEFAULT_LEDGER_PATH = path.join(__dirname, '../fixtures/host-parity/unaligned-ledger.v1.json') |
| 19 | +const DEFAULT_SCHEMA_PATH = path.join(__dirname, '../fixtures/host-parity/unaligned-ledger.schema.json') |
| 20 | + |
| 21 | +/** |
| 22 | + * @param {string} [ledgerPath] |
| 23 | + * @returns {object} |
| 24 | + */ |
| 25 | +function loadUnalignedLedger(ledgerPath = DEFAULT_LEDGER_PATH) { |
| 26 | + const raw = fs.readFileSync(ledgerPath, 'utf8') |
| 27 | + const ledger = JSON.parse(raw) |
| 28 | + return validateUnalignedLedger(ledger) |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Lightweight structural validation (no ajv dependency). |
| 33 | + * @param {object} ledger |
| 34 | + * @returns {object} same ledger if valid |
| 35 | + */ |
| 36 | +function validateUnalignedLedger(ledger) { |
| 37 | + if (!ledger || typeof ledger !== 'object' || Array.isArray(ledger)) { |
| 38 | + throw new Error('UNALIGNED_LEDGER_INVALID: root must be object') |
| 39 | + } |
| 40 | + if (ledger.schemaVersion !== SCHEMA_VERSION) { |
| 41 | + throw new Error(`UNALIGNED_LEDGER_INVALID: schemaVersion must be ${SCHEMA_VERSION}`) |
| 42 | + } |
| 43 | + if (typeof ledger.updatedAt !== 'string' || !ledger.updatedAt) { |
| 44 | + throw new Error('UNALIGNED_LEDGER_INVALID: updatedAt required') |
| 45 | + } |
| 46 | + if (!Array.isArray(ledger.items) || ledger.items.length !== REQUIRED_IDS.length) { |
| 47 | + throw new Error(`UNALIGNED_LEDGER_INVALID: items must have exactly ${REQUIRED_IDS.length} entries`) |
| 48 | + } |
| 49 | + const seen = new Set() |
| 50 | + for (const item of ledger.items) { |
| 51 | + if (!item || typeof item !== 'object') { |
| 52 | + throw new Error('UNALIGNED_LEDGER_INVALID: item must be object') |
| 53 | + } |
| 54 | + if (!REQUIRED_IDS.includes(item.id)) { |
| 55 | + throw new Error(`UNALIGNED_LEDGER_INVALID: unknown id ${item.id}`) |
| 56 | + } |
| 57 | + if (seen.has(item.id)) { |
| 58 | + throw new Error(`UNALIGNED_LEDGER_INVALID: duplicate id ${item.id}`) |
| 59 | + } |
| 60 | + seen.add(item.id) |
| 61 | + if (!item.title || typeof item.title !== 'string') { |
| 62 | + throw new Error(`UNALIGNED_LEDGER_INVALID: ${item.id} title required`) |
| 63 | + } |
| 64 | + if (!['open', 'closed', 'wont-fix'].includes(item.status)) { |
| 65 | + throw new Error(`UNALIGNED_LEDGER_INVALID: ${item.id} bad status`) |
| 66 | + } |
| 67 | + if (!item.closePath || !item.owner) { |
| 68 | + throw new Error(`UNALIGNED_LEDGER_INVALID: ${item.id} closePath/owner required`) |
| 69 | + } |
| 70 | + if (!Array.isArray(item.evidenceRefs)) { |
| 71 | + throw new Error(`UNALIGNED_LEDGER_INVALID: ${item.id} evidenceRefs must be array`) |
| 72 | + } |
| 73 | + if (item.status === 'closed' && item.evidenceRefs.length < 1) { |
| 74 | + throw new Error(`UNALIGNED_LEDGER_CLOSE_WITHOUT_EVIDENCE: ${item.id}`) |
| 75 | + } |
| 76 | + for (const ref of item.evidenceRefs) { |
| 77 | + if (!ref || typeof ref.kind !== 'string' || typeof ref.ref !== 'string') { |
| 78 | + throw new Error(`UNALIGNED_LEDGER_INVALID: ${item.id} evidenceRefs entries need kind+ref`) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + for (const id of REQUIRED_IDS) { |
| 83 | + if (!seen.has(id)) { |
| 84 | + throw new Error(`UNALIGNED_LEDGER_INVALID: missing id ${id}`) |
| 85 | + } |
| 86 | + } |
| 87 | + return ledger |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Attempt to mark an item closed; fails without evidence. |
| 92 | + * @param {object} ledger |
| 93 | + * @param {string} id |
| 94 | + * @param {{ kind: string, ref: string, note?: string }[]} evidenceRefs |
| 95 | + * @param {string} [closedAt] |
| 96 | + * @returns {object} new ledger (does not write disk) |
| 97 | + */ |
| 98 | +function closeUnalignedItem(ledger, id, evidenceRefs, closedAt = new Date().toISOString()) { |
| 99 | + const next = JSON.parse(JSON.stringify(ledger)) |
| 100 | + const item = next.items.find((x) => x.id === id) |
| 101 | + if (!item) throw new Error(`UNALIGNED_LEDGER_UNKNOWN_ID: ${id}`) |
| 102 | + if (!Array.isArray(evidenceRefs) || evidenceRefs.length < 1) { |
| 103 | + throw new Error(`UNALIGNED_LEDGER_CLOSE_WITHOUT_EVIDENCE: ${id}`) |
| 104 | + } |
| 105 | + item.status = 'closed' |
| 106 | + item.evidenceRefs = evidenceRefs |
| 107 | + item.closedAt = closedAt |
| 108 | + next.updatedAt = closedAt |
| 109 | + return validateUnalignedLedger(next) |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Markdown projection for humans (requirement-side copy). |
| 114 | + * @param {object} ledger |
| 115 | + * @returns {string} |
| 116 | + */ |
| 117 | +function formatUnalignedLedgerMarkdown(ledger) { |
| 118 | + validateUnalignedLedger(ledger) |
| 119 | + const lines = [ |
| 120 | + '# UnalignedLedgerV1 (projection)', |
| 121 | + '', |
| 122 | + `> schemaVersion: ${ledger.schemaVersion} · updatedAt: ${ledger.updatedAt}`, |
| 123 | + '> **Authority**: `scripts/fixtures/host-parity/unaligned-ledger.v1.json`', |
| 124 | + '', |
| 125 | + '| ID | Title | Status | Close path | Owner |', |
| 126 | + '|----|-------|:------:|------------|-------|' |
| 127 | + ] |
| 128 | + for (const item of ledger.items) { |
| 129 | + lines.push(`| ${item.id} | ${item.title} | ${item.status} | ${item.closePath} | ${item.owner} |`) |
| 130 | + } |
| 131 | + lines.push('') |
| 132 | + return lines.join('\n') |
| 133 | +} |
| 134 | + |
| 135 | +module.exports = { |
| 136 | + SCHEMA_VERSION, |
| 137 | + REQUIRED_IDS, |
| 138 | + DEFAULT_LEDGER_PATH, |
| 139 | + DEFAULT_SCHEMA_PATH, |
| 140 | + loadUnalignedLedger, |
| 141 | + validateUnalignedLedger, |
| 142 | + closeUnalignedItem, |
| 143 | + formatUnalignedLedgerMarkdown |
| 144 | +} |
0 commit comments