|
| 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-gate4-btd-scalar-volume-state-machine.mjs [--skip-branch-check] [--repo-root <path>]', |
| 48 | + '', |
| 49 | + 'Checks the V45 BTD scalar-volume 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 | + 'packages/btd/src/semantic-volume.ts', |
| 81 | + 'packages/btd/src/measuremint.ts', |
| 82 | + 'packages/btd/src/range.ts', |
| 83 | + 'packages/btd/src/source-to-shares.ts', |
| 84 | + 'package.json', |
| 85 | + 'scripts/check-v45-gate3-assetpack-lifecycle-state-machine.mjs', |
| 86 | + ]) { |
| 87 | + assertCheck(failures, exists(root, relativePath), `Missing required V45 Gate 4 file: ${relativePath}`); |
| 88 | + } |
| 89 | + |
| 90 | + const notes = read(root, 'BITCODE_SPEC_V45_NOTES.md'); |
| 91 | + const normalizedNotes = normalize(notes); |
| 92 | + const packageJson = read(root, 'package.json'); |
| 93 | + |
| 94 | + for (const phrase of [ |
| 95 | + 'V45 protocol atom 3: BTD scalar-volume state machine', |
| 96 | + 'BTD is first the weighted scalar knowledge-volume of a Need-Fit AssetPack', |
| 97 | + 'only its settled form carries rights', |
| 98 | + 'BTD is the non-fungible, proof-addressed scalar unit of technical knowledge volume', |
| 99 | + 'Deposit-time measurement may estimate BTD potential', |
| 100 | + 'final BTD size is only computed after a reviewed Need', |
| 101 | + 'selected Fit set, synthesized Need-Fit AssetPack', |
| 102 | + 'deterministic measurement weights', |
| 103 | + 'BTC remains the payment asset', |
| 104 | + 'BTD is not money', |
| 105 | + 'btd-not-applicable', |
| 106 | + 'btd-potential-measured', |
| 107 | + 'need-fit-measurements-admitted', |
| 108 | + 'measurement-weight-policy-locked', |
| 109 | + 'weighted-scalar-volume-computed', |
| 110 | + 'btd-quantized', |
| 111 | + 'measuremint-applied', |
| 112 | + 'btd-range-assigned', |
| 113 | + 'btd-quote-bound', |
| 114 | + 'btd-rights-pending', |
| 115 | + 'btd-rights-transferred', |
| 116 | + 'btd-source-to-shares-allocated', |
| 117 | + 'btd-repair-required', |
| 118 | + 'potential/range language only; no final BTD size', |
| 119 | + 'normalizedMeasurementVolume * measurementWeight', |
| 120 | + 'Need-relative normalized BTD volume', |
| 121 | + 'quoteable BTD token count or zero-cell receipt', |
| 122 | + 'source-safe mint receipt; no Reader rights yet', |
| 123 | + 'rights-bearing BTD and source unlock authority', |
| 124 | + 'Every final BTD measurement row must be Need-relative', |
| 125 | + 'Measurement weights are deterministic protocol policy', |
| 126 | + 'Scalar volume calculation uses fixed-point or integer arithmetic', |
| 127 | + 'Deposit-time measurements may inform depositor review', |
| 128 | + 'cannot mint, transfer, quote final BTD, unlock source, or allocate contributor shares', |
| 129 | + 'contiguous, non-overlapping AssetPack range', |
| 130 | + 'Zero-cell tail receipts remain valid measuremint evidence', |
| 131 | + 'Rights-bearing BTD exists only after BTC settlement finality and BTD rights transfer', |
| 132 | + 'Source-to-shares uses the settled Need-Fit AssetPack', |
| 133 | + 'must not be confused with scalar-volume calculation', |
| 134 | + 'Acceptance for this atom', |
| 135 | + 'BTD as Need-relative weighted scalar knowledge-volume', |
| 136 | + 'deposit-time BTD potential as non-final', |
| 137 | + 'deterministic fixed-point measurement weighting', |
| 138 | + 'measuremint/range conservation', |
| 139 | + 'BTC-before-rights law', |
| 140 | + ]) { |
| 141 | + assertCheck(failures, normalizedNotes.includes(phrase), `V45 Gate 4 notes must include phrase: ${phrase}`); |
| 142 | + } |
| 143 | + |
| 144 | + assertCheck( |
| 145 | + failures, |
| 146 | + packageJson.includes('"check:v45-gate4": "node scripts/check-v45-gate4-btd-scalar-volume-state-machine.mjs"'), |
| 147 | + 'package.json must expose check:v45-gate4.', |
| 148 | + ); |
| 149 | + |
| 150 | + if (failures.length > 0) { |
| 151 | + process.stderr.write('V45 Gate 4 BTD scalar-volume state machine check failed:\n'); |
| 152 | + for (const failure of failures) process.stderr.write(`- ${failure}\n`); |
| 153 | + process.exitCode = 1; |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + process.stdout.write('V45 Gate 4 BTD scalar-volume state machine check passed.\n'); |
| 158 | +} |
| 159 | + |
| 160 | +try { |
| 161 | + main(); |
| 162 | +} catch (error) { |
| 163 | + const detail = error instanceof Error ? error.message : String(error); |
| 164 | + process.stderr.write(`${detail}\n`); |
| 165 | + process.exitCode = 1; |
| 166 | +} |
0 commit comments