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').replace(/^\uFEFF/,'')),sha=p=>crypto.createHash('sha256').update(fs.readFileSync(p)).digest('hex'),state=read('C:/laragon/data/sgf-tempo/run/modular-regression-current.json'),output='docs/proofs/common-ledger-composition-20261103.json';if(state.port!==55439||fs.existsSync(output))throw Error('Fresh isolated consolidation required'); const tests={},evidence=[],names={regression:'ledger-2d5c71abf625/composition-cases.json',combined_workflow:'ledger-2d5c71abf625/composition-workflow.json',open_http:'ledger-2d5c71abf625/composition-http.json',closed_http:'ledger-01aab3ebad2d/composition-http.json',team_http_forms:'ledger-fe42e1169ce6/composition-demo-http.json',team_preservation:'ledger-fe42e1169ce6/composition-demo-preservation.json'};for(const [name,file]of Object.entries(names)){const p=path.join(state.root,'private',file),v=read(p);if(!v.passed)throw Error('Invalid final proof '+name);tests[name]=v;evidence.push({file:'private/'+file,sha256:sha(p)});} if(tests.regression.checks!==37||tests.combined_workflow.combined_checks!==31||tests.open_http.checks!==36||tests.closed_http.checks!==35||tests.team_http_forms.checks!==34||tests.combined_workflow.gross_debit_minor!==330500||tests.combined_workflow.lines!==17||!tests.team_preservation.all_business_source_outbox_rights_book_hashes_unchanged)throw Error('Expected composition proofs missing'); const priorFiles=['docs/proofs/common-ledger-direct-cost-correction-20261103.json','docs/proofs/common-ledger-direct-cost-20261103.json','docs/proofs/common-ledger-sales-receivable-20261103.json','docs/proofs/common-ledger-reports-20261103.json','docs/proofs/common-ledger-complete-cycle-20261102.json','docs/proofs/common-finance-bundle-20261103.json'];const prior=[];let checked=0;for(const file of priorFiles){const v=read(file);for(const e of v.private_evidence??v.evidence??[]){const p=path.resolve(state.root,e.file);if(!p.startsWith(path.resolve(state.root)+path.sep)||sha(p)!==e.sha256)throw Error('Prior private artifact changed '+e.file);checked++;}prior.push({file,sha256:sha(file),valid_private_artifacts_reused:true});} const packages=[];for(const [tag,pin,n] of [['760a92655dc1','6a507ea7d708a12a8e526e4c610696d8db84079d18aede470237b65505630e7e',25],['b06aa4315783','cbc4ddbea15f5e396c92bc2e664b973138551928980f334bcf3eb0cf097cacb1',30]]){const root=path.join(state.root,'private/finance-bundle-'+tag),m=read(path.join(root,'manifest.json'));if(sha(path.join(root,'manifest.json'))!==pin||m.files.length!==n)throw Error('Frozen bundle changed');for(const f of m.files)if(sha(path.join(root,f.file))!==f.sha256)throw Error('Frozen runtime changed');packages.push({files:n,manifest_sha256:pin,all_bytes_preserved:true});} const files=['app/Modules/Accounting/LedgerComposition.php','app/Modules/Accounting/LedgerCompositionController.php','app/Modules/Accounting/GeneralLedger.php','packages/general-ledger/src/PayableJournal.php','packages/general-ledger/src/SalesSchema.php','config/general_ledger.php','routes/web.php','scripts/payable-history-sgf-proof.php','scripts/ledger-cost-proof.php','scripts/ledger-cost-http-entry.php','scripts/ledger-cost-demo.mjs','scripts/ledger-run.mjs',...fs.readdirSync('scripts').filter(f=>f.startsWith('ledger-composition-')).map(f=>'scripts/'+f),'scripts/summarize-ledger-composition.mjs','resources/views/accounting/ledger-composition.blade.php','resources/views/accounting/ledger-composition-demo-entry.blade.php','resources/views/accounting/general-ledger.blade.php','docs/GENERAL_LEDGER_COMPOSITION_DELIVERY.md','docs/GENERAL_LEDGER_INDUSTRIAL_SCOPE.md','README.md','docs/PRODUCT_DEMO.md','docs/SGF_MODULE_FLOW_PREPARATION.md'];let php=0,js=0;for(const f of files){if(f.endsWith('.blade.php')||(!f.endsWith('.php')&&!f.endsWith('.mjs')))continue;const isPhp=f.endsWith('.php'),r=spawnSync(isPhp?'C:/laragon/tools/sgf-tempo/php-8.4.27/php.exe':process.execPath,[isPhp?'-l':'--check',f],{windowsHide:true,encoding:'utf8'});if(r.status!==0)throw Error('Syntax failed '+f);isPhp?php++:js++;}for(const cwd of [process.cwd(),path.resolve('../compras')])if(spawnSync('git',['diff','--check'],{cwd,windowsHide:true,encoding:'utf8'}).status!==0)throw Error('Diff check failed'); const team=read(path.join(state.root,'private/ledger-fe42e1169ce6/cost-demo.json'));for(const [url,text] of [[team.url,'data-cost-demo="fe42e1169ce6"'],['http://127.0.0.1:8055/qa/cost-demo','data-cost-demo="f3e0a05eb2ad"'],['http://127.0.0.1:8054/qa/cost-demo','data-cost-demo="6a73790314ce"']]){const r=await fetch(url,{signal:AbortSignal.timeout(5000)});if(!r.ok||!(await r.text()).includes(text))throw Error('Expected independent demo unavailable '+url);} const result={increment:'COMMON-LEDGER-COMPOSITION-SIM-v1',consolidated_at_utc:new Date().toISOString(),functional_code:true,synthetic_only:true,status:'implemented_and_tested_with_limits',authorization:'Existing approved Sales, Supplier and Direct Cost Correction effects composed; no new effect type or real grant.',tests,targeted_checks:173,private_evidence:evidence,reused_manifests:prior,prior_private_artifacts_verified:checked,frozen_bundles:packages,quality:{syntax:{php,js},diff_check_both_repositories:true,business_suites_unrelated_not_repeated:true},source_files:files.map(file=>({file,sha256:sha(file)})),team_demo:team,observed_result:{journals:8,lines:17,debit_minor:330500,credit_minor:330500,client_balance_minor:40000,cost_balance_minor:29500,supplier_balance_minor:6000,closed_periods:1},preservation:{six_pending_records_untouched:true,blocked_cache_untouched:true,old_demonstrations_not_reset:true,source_outbox_rights_preserved:true,private_credentials_not_versioned:true,unrelated_work_preserved:true,destination_unchanged:true},limits:{visual_review_pending:true,actual_browser:false,new_large_volume_measurements:false,deployment_capacity_validated:false,all_six_modules_operationally_complete:false,purchasing_activation:false,stock_valuation:false,payroll_or_industrial_posting:false,real_or_public_grants:false,published:false,checkpoint:false,acceptance_presumed:false},next_proposed_scope:{file:'docs/GENERAL_LEDGER_INDUSTRIAL_SCOPE.md',sha256:sha('docs/GENERAL_LEDGER_INDUSTRIAL_SCOPE.md'),authorization_required:true,not_implemented:true}}; fs.writeFileSync(output,JSON.stringify(result,null,2)+'\n');console.log(JSON.stringify({output,checks:173,prior_artifacts_verified:checked,syntax:result.quality.syntax,demo:team.url}));