|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { execFileSync } from 'node:child_process'; |
| 4 | +import { existsSync, readFileSync } from 'node:fs'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | +const defaultRepoRoot = path.resolve(__dirname, '..'); |
| 11 | + |
| 12 | +function read(root, relativePath) { |
| 13 | + return readFileSync(path.join(root, relativePath), 'utf8'); |
| 14 | +} |
| 15 | + |
| 16 | +function exists(root, relativePath) { |
| 17 | + return existsSync(path.join(root, relativePath)); |
| 18 | +} |
| 19 | + |
| 20 | +function git(root, args) { |
| 21 | + return execFileSync('git', args, { cwd: root, encoding: 'utf8' }).trim(); |
| 22 | +} |
| 23 | + |
| 24 | +function assertCheck(failures, condition, message) { |
| 25 | + if (!condition) failures.push(message); |
| 26 | +} |
| 27 | + |
| 28 | +function normalize(content) { |
| 29 | + return content.replace(/\s+/gu, ' ').trim(); |
| 30 | +} |
| 31 | + |
| 32 | +function parseArgs(argv) { |
| 33 | + const args = { repoRoot: defaultRepoRoot, skipBranchCheck: false }; |
| 34 | + for (let index = 0; index < argv.length; index += 1) { |
| 35 | + const arg = argv[index]; |
| 36 | + if (arg === '--repo-root') args.repoRoot = path.resolve(argv[++index]); |
| 37 | + else if (arg === '--skip-branch-check') args.skipBranchCheck = true; |
| 38 | + else if (arg === '--help' || arg === '-h') args.help = true; |
| 39 | + else throw new Error(`Unknown argument ${arg}`); |
| 40 | + } |
| 41 | + return args; |
| 42 | +} |
| 43 | + |
| 44 | +function printHelp() { |
| 45 | + process.stdout.write( |
| 46 | + [ |
| 47 | + 'Usage: node scripts/check-v45-gate3-assetpack-lifecycle-state-machine.mjs [--skip-branch-check] [--repo-root <path>]', |
| 48 | + '', |
| 49 | + 'Checks the V45 AssetPack lifecycle state machine specification atom.', |
| 50 | + ].join('\n'), |
| 51 | + ); |
| 52 | + process.stdout.write('\n'); |
| 53 | +} |
| 54 | + |
| 55 | +function main() { |
| 56 | + const args = parseArgs(process.argv.slice(2)); |
| 57 | + if (args.help) { |
| 58 | + printHelp(); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + const root = args.repoRoot; |
| 63 | + const failures = []; |
| 64 | + const pointer = read(root, 'BITCODE_SPEC.txt').trim(); |
| 65 | + |
| 66 | + assertCheck(failures, pointer === 'V44', `BITCODE_SPEC.txt must remain V44 during V45 atom work. Observed ${pointer || 'empty'}.`); |
| 67 | + |
| 68 | + if (!args.skipBranchCheck) { |
| 69 | + const branch = git(root, ['branch', '--show-current']); |
| 70 | + assertCheck( |
| 71 | + failures, |
| 72 | + branch === 'version/v45' || /^v45\/gate-\d+-[a-z0-9][a-z0-9-]*$/u.test(branch), |
| 73 | + `V45 work must occur on version/v45 or v45/gate-N-* branches. Observed ${branch || 'detached HEAD'}.`, |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + for (const relativePath of [ |
| 78 | + 'BITCODE_SPEC_V45_NOTES.md', |
| 79 | + 'BITCODE_SPEC_V44.md', |
| 80 | + 'package.json', |
| 81 | + 'scripts/check-v45-gate2-knowledge-commoditization-law.mjs', |
| 82 | + ]) { |
| 83 | + assertCheck(failures, exists(root, relativePath), `Missing required V45 Gate 3 file: ${relativePath}`); |
| 84 | + } |
| 85 | + |
| 86 | + const notes = read(root, 'BITCODE_SPEC_V45_NOTES.md'); |
| 87 | + const normalizedNotes = normalize(notes); |
| 88 | + const packageJson = read(root, 'package.json'); |
| 89 | + |
| 90 | + for (const phrase of [ |
| 91 | + 'V45 protocol atom 2: AssetPack lifecycle state machine', |
| 92 | + 'V43/V44 correctly establish AssetPacks in and AssetPacks out', |
| 93 | + 'Every AssetPack-like object must occupy exactly one lifecycle state at every protocol boundary', |
| 94 | + 'source safe only, source-bearing but withheld, quoteable, settled, deliverable, compensable, or in repair', |
| 95 | + 'No interface, API, pipeline, proof artifact, or operator tool may collapse lifecycle states', |
| 96 | + 'deposit option look like settled BTD', |
| 97 | + 'preview look like source disclosure', |
| 98 | + 'quote look like payment finality', |
| 99 | + 'admitted Depository AssetPack look like a delivered Need-Fit AssetPack', |
| 100 | + 'deposit-option-synthesized', |
| 101 | + 'deposit-option-reviewed', |
| 102 | + 'depository-assetpack-admitted', |
| 103 | + 'fit-candidates-recalled', |
| 104 | + 'fit-set-selected', |
| 105 | + 'need-fit-assetpack-synthesized', |
| 106 | + 'need-fit-assetpack-quoted', |
| 107 | + 'settlement-observed', |
| 108 | + 'btd-settled-rights-transferred', |
| 109 | + 'source-unlocked-delivery', |
| 110 | + 'compensated-and-reconciled', |
| 111 | + 'repair-required', |
| 112 | + 'Deposit-time BTD-relevant measurements may show potential', |
| 113 | + 'no final BTD size', |
| 114 | + 'Admitted supply may carry BTD potential, not final BTD', |
| 115 | + 'Candidate BTD contribution is only provisional until selected against the reviewed Need', |
| 116 | + 'Need-relative BTD scalar volume may be computed for quote', |
| 117 | + 'rights-bearing BTD is not minted or transferred', |
| 118 | + 'Quoteable BTD volume exists because a reviewed Need and selected Fit set exist', |
| 119 | + 'BTC payment is observed but not final', |
| 120 | + 'Rights-bearing BTD is minted/mined/assigned for the Need-Fit AssetPack', |
| 121 | + 'A deposit option may transition to `depository-assetpack-admitted` only after depositor approval', |
| 122 | + 'the search may recall many candidates above threshold', |
| 123 | + 'selected Fit set may contain one or many admitted Depository AssetPacks', |
| 124 | + 'The Need-Fit AssetPack is synthesized from the reviewed Need plus the selected Fit set', |
| 125 | + 'may not see source before BTC settlement finality and BTD rights transfer', |
| 126 | + 'BTD final scalar knowledge-volume is Need-relative', |
| 127 | + 'becomes rights-bearing only after settlement', |
| 128 | + 'BTC payment observation, BTC finality, BTD rights transfer, source unlock, repository delivery, source-to-shares compensation, and reconciliation are distinct states', |
| 129 | + 'moves the object to `repair-required`', |
| 130 | + 'Acceptance for this atom', |
| 131 | + 'Need-relative BTD calculation condition', |
| 132 | + 'one-to-many Finding Fits input set', |
| 133 | + 'BTC settlement-before-source law', |
| 134 | + ]) { |
| 135 | + assertCheck(failures, normalizedNotes.includes(phrase), `V45 Gate 3 notes must include phrase: ${phrase}`); |
| 136 | + } |
| 137 | + |
| 138 | + assertCheck( |
| 139 | + failures, |
| 140 | + packageJson.includes('"check:v45-gate3": "node scripts/check-v45-gate3-assetpack-lifecycle-state-machine.mjs"'), |
| 141 | + 'package.json must expose check:v45-gate3.', |
| 142 | + ); |
| 143 | + |
| 144 | + if (failures.length > 0) { |
| 145 | + process.stderr.write('V45 Gate 3 AssetPack lifecycle state machine check failed:\n'); |
| 146 | + for (const failure of failures) process.stderr.write(`- ${failure}\n`); |
| 147 | + process.exitCode = 1; |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + process.stdout.write('V45 Gate 3 AssetPack lifecycle state machine check passed.\n'); |
| 152 | +} |
| 153 | + |
| 154 | +try { |
| 155 | + main(); |
| 156 | +} catch (error) { |
| 157 | + const detail = error instanceof Error ? error.message : String(error); |
| 158 | + process.stderr.write(`${detail}\n`); |
| 159 | + process.exitCode = 1; |
| 160 | +} |
0 commit comments