Files
SGF/scripts/ledger-run.mjs
T
2026-11-04 09:37:20 +02:00

22 lines
4.5 KiB
JavaScript

import fs from 'node:fs';import path from 'node:path';import crypto from 'node:crypto';import {spawnSync} from 'node:child_process';
const read=p=>JSON.parse(fs.readFileSync(p,'utf8'));
const state=read('C:/laragon/data/sgf-tempo/run/modular-regression-current.json');if(state.port!==55439||!/^[a-f0-9]{12}$/.test(state.id))throw Error('Owned isolated cluster required');
const resume=process.argv[2]==='new'?undefined:process.argv[2];if(resume&&!/^[a-f0-9]{12}$/.test(resume))throw Error('Invalid owned tag');
const tag=resume??crypto.randomBytes(6).toString('hex'),root=path.join(state.root,'private/ledger-'+tag),settings=path.join(root,'settings.json');
if(!resume){
const prior=path.join(state.root,'private/payable-history-03e16d9fb37d/source.json'),bundle=read(prior),admin=fs.readFileSync(path.join(state.root,'private/admin-password.txt'),'utf8').trim();
const pg=input=>{const r=spawnSync('C:/Program Files/PostgreSQL/17/bin/psql.exe',['-h','127.0.0.1','-p','55439','-U','sgf_regression_owner','-d','postgres','-Atq','-v','ON_ERROR_STOP=1'],{input,encoding:'utf8',windowsHide:true,env:{...process.env,PGPASSWORD:admin}});if(r.status!==0)throw Error('QA provisioning failed');return r.stdout.trim();};
if(pg("SELECT current_setting('cluster_name')")!=='sgf_modular_regression_'+state.id)throw Error('Wrong effective cluster');
const role='payable_history_sgf_'+tag,database='payable_sgf_'+tag,password=crypto.randomBytes(32).toString('hex');pg(`CREATE ROLE ${role} LOGIN PASSWORD '${password}' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS; CREATE DATABASE ${database} OWNER ${role}; REVOKE ALL ON DATABASE ${database} FROM PUBLIC;`);
const storage=path.join(root,'storage');for(const dir of ['framework/cache/data','framework/views','framework/sessions','logs','cache','public'])fs.mkdirSync(path.join(storage,dir),{recursive:true});
fs.copyFileSync(prior,path.join(root,'source.json'));fs.writeFileSync(settings,JSON.stringify({tag,root,cluster:'sgf_modular_regression_'+state.id,source_instance:bundle.source.instance,app_key:'base64:'+crypto.randomBytes(32).toString('base64'),sgf:{root:process.cwd(),database,role,password,storage}},null,2));
}
const owned=read(settings);if(owned.tag!==tag||owned.cluster!=='sgf_modular_regression_'+state.id)throw Error('Owned settings mismatch');
const phases=process.argv[3]?.split(',')??(resume?[]:['sgf','ledger-setup']);
for(const phase of phases){
const output={'ledger-cost-industrial-correction-setup':'cost-fixture.json','ledger-cost-industrial-correction-ready':'industrial-correction-fixture.json','ledger-cost-industrial-correction-cases':'industrial-correction-cases.json','ledger-cost-industrial-setup':'cost-fixture.json','ledger-cost-industrial-ready':'industrial-fixture.json','ledger-cost-industrial-workflow':'industrial-workflow.json','ledger-cost-industrial-cases':'industrial-cases.json','ledger-cost-industrial-race':'industrial-race.json','ledger-cost-composition-cases':'composition-cases.json','ledger-cost-composition-workflow':'composition-workflow.json','ledger-cost-composition-baseline':'composition-demo-baseline.json','ledger-cost-composition-preservation':'composition-demo-preservation.json',sgf:'fixture.json','ledger-cost-composition-setup':'cost-fixture.json','ledger-cost-composition-ready':'ledger-composition-fixture.json','ledger-cost-correction-setup':'cost-fixture.json','ledger-cost-correction-ready':'cost-correction-fixture.json','ledger-cost-setup':'cost-fixture.json','ledger-sales-setup':'sales-fixture.json','ledger-setup':'ledger-fixture.json','ledger-cycle-setup':'ledger-fixture.json','ledger-flow-setup':'ledger-fixture.json','ledger-setup-recover':'ledger-fixture.json','ledger-cases':'ledger-cases.json','ledger-race':'ledger-race.json','ledger-post':'ledger-post.json'}[phase];if(!output)throw Error('Unknown phase');if(fs.existsSync(path.join(root,output)))throw Error('Completed proof preserved');
const at=Date.now(),r=spawnSync('C:/laragon/tools/sgf-tempo/php-8.4.27/php.exe',['-d','memory_limit=512M','scripts/payable-history-sgf-proof.php',settings,phase],{encoding:'utf8',windowsHide:true,maxBuffer:1024*1024});fs.writeFileSync(path.join(root,phase+'-'+at+'.log'),(r.stdout??'')+(r.stderr??''));
if(r.status!==0||!fs.existsSync(path.join(root,output)))throw Error('Owned ledger phase failed; private diagnostics: '+root);process.stdout.write(r.stdout);
}
fs.writeFileSync(path.join(state.root,'ledger-latest.json'),JSON.stringify({tag,root}));console.log(JSON.stringify({tag,root,new_owned_synthetic_database_only:true}));