Files
SGF/scripts/summarize-ledger-cost-correction.mjs
2026-11-04 09:37:20 +02:00

13 lines
8.2 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').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-direct-cost-correction-20261103.json';if(state.port!==55439||fs.existsSync(output))throw Error('Fresh owned correction consolidation required');
const names={actual_source_setup:'ledger-e2ec7e0e3f38/cost-correction-fixture.json',actual_complete_workflow:'ledger-e2ec7e0e3f38/cost-correction-workflow.json',defaults:'ledger-e2ec7e0e3f38/cost-correction-defaults.json',regression:'ledger-c4c506a3c295/cost-correction-cases.json',security:'ledger-c4c506a3c295/cost-correction-security.json',actual_http:'ledger-c4c506a3c295/cost-correction-http.json',http_final:'ledger-c4c506a3c295/cost-correction-http-final.json',concurrency:'ledger-eee1a330c1bb/cost-correction-race.json',three_interface_postcheck:'ledger-eee1a330c1bb/cost-correction-ui-postcheck.json',cost_supplier_corrections:'ledger-ac404fba21fd/cost-correction-coexistence.json',native_supplier_cycle:'ledger-native-0912930aea5b/native-cycle-cases.json',old_cost_mode:'ledger-6d0ea9e319e0/cost-correction-legacy-mode.json',old8054_preservation:'ledger-6a73790314ce/cost-demo-after-correction.json',team_readiness_and_forms:'ledger-f3e0a05eb2ad/cost-correction-demo-http.json',team_preservation:'ledger-f3e0a05eb2ad/cost-correction-demo-preservation.json'};
const tests={},evidence=[];for(const [name,file]of Object.entries(names)){const p=path.join(state.root,'private',file),v=read(p);if(!v.passed)throw Error('Unsuccessful correction evidence '+name);tests[name]=v;evidence.push({file:'private/'+file,sha256:sha(p)});}
if(tests.regression.checks!==30||tests.security.checks!==8||tests.actual_http.checks!==58||tests.concurrency.workers!==6||tests.team_readiness_and_forms.checks!==23||tests.actual_complete_workflow.gross_debit_minor!==84500||tests.actual_complete_workflow.cost_balance_minor!==29500||tests.cost_supplier_corrections.gross_minor!==102500||tests.native_supplier_cycle.checks!==23||tests.defaults.default_without_environment!==false)throw Error('Expected authorised coverage missing');
const priorFiles=['docs/proofs/common-ledger-direct-cost-20261103.json','docs/proofs/common-ledger-reports-20261103.json','docs/proofs/common-ledger-sales-receivable-20261103.json','docs/proofs/common-finance-bundle-20261103.json','docs/proofs/common-ledger-complete-cycle-20261102.json'];let preserved=0;for(const file of priorFiles){const old=read(file);for(const f of old.private_evidence??old.evidence??[]){const p=path.resolve(state.root,f.file);if(!p.startsWith(path.resolve(state.root)+path.sep)||sha(p)!==f.sha256)throw Error('Previous valid artifact changed '+f.file);preserved++;}}
const approvedFile='private/ledger-cost-correction-approved-scope.md',approved=sha(path.join(state.root,approvedFile));if(approved!==read(priorFiles[0]).next_scope.sha256)throw Error('Approved scope differs from presented proposal');evidence.push({file:approvedFile,sha256:approved});
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 package changed');for(const f of m.files)if(sha(path.join(root,f.file))!==f.sha256)throw Error('Frozen runtime file changed');packages.push({files:n,manifest_sha256:pin,all_hashes_preserved:true,not_replaced:true});}
const sources=['AGENTS.md','config/cost_ledger.php','app/Modules/Accounting/GeneralLedger.php','app/Modules/Accounting/CostLedgerSource.php','app/Modules/Accounting/GeneralLedgerController.php','app/Modules/Accounting/CostLedgerController.php','app/Modules/Workshop/DirectCosts.php','app/Shared/Testing/TestingDatabaseGuard.php','packages/general-ledger/bootstrap.php','packages/general-ledger/README.md',...['LedgerSchema','CycleSchema','CostSchema','CostCorrectionSchema','PayableJournal','LedgerCycle','LedgerReports'].map(n=>'packages/general-ledger/src/'+n+'.php'),'packages/general-ledger/ui/cycle.php','resources/views/accounting/general-ledger.blade.php','resources/views/accounting/cost-ledger.blade.php','resources/views/accounting/cost-correction.blade.php','resources/views/accounting/cost-correction-demo-entry.blade.php','routes/web.php','scripts/payable-history-sgf-proof.php','scripts/ledger-run.mjs','scripts/ledger-cost-proof.php','scripts/ledger-cost-http-entry.php','scripts/ledger-cost-http-router.php','scripts/ledger-cost-demo.mjs',...fs.readdirSync('scripts').filter(n=>n.startsWith('ledger-cost-correction-')).map(n=>'scripts/'+n),'scripts/archive-ledger-cost-correction-scope.mjs','scripts/summarize-ledger-cost-correction.mjs','docs/GENERAL_LEDGER_DIRECT_COST_CORRECTION_SCOPE.md','docs/GENERAL_LEDGER_DIRECT_COST_CORRECTION_DELIVERY.md','README.md','docs/PRODUCT_DEMO.md','docs/SGF_MODULE_FLOW_PREPARATION.md'];let php=0,js=0;for(const file of sources){if(file.endsWith('.blade.php')||(!file.endsWith('.php')&&!file.endsWith('.mjs')))continue;const isPhp=file.endsWith('.php'),r=spawnSync(isPhp?'C:/laragon/tools/sgf-tempo/php-8.4.27/php.exe':process.execPath,[isPhp?'-l':'--check',file],{encoding:'utf8',windowsHide:true});if(r.status!==0)throw Error('Syntax failed '+file);isPhp?php++:js++;}
for(const cwd of [process.cwd(),path.resolve('../compras')])if(spawnSync('git',['diff','--check'],{cwd,encoding:'utf8',windowsHide:true}).status!==0)throw Error('Diff check failed');const demo=read(path.join(state.root,'private/ledger-f3e0a05eb2ad/cost-demo.json')),r=await fetch(demo.url,{signal:AbortSignal.timeout(5000)});if(!r.ok||!(await r.text()).includes('data-cost-demo="'+demo.tag+'"'))throw Error('Team demo unavailable');
const result={increment:'COMMON-LEDGER-DIRECT-COST-CORRECTION-SIM-v1',consolidated_at_utc:new Date().toISOString(),authorization:{answer:'Autorizar a correcção sintética proposta',scope_archive_matches_presented_proposal:true,approved_scope_sha256:approved},functional_code:true,synthetic_only:true,status:'implemented_and_tested_with_explicit_limits',tests,evidence,previous_proofs:priorFiles.map(file=>({file,sha256:sha(file)})),previous_private_artifacts_verified:preserved,frozen_native_packages:packages,sources:sources.map(file=>({file,sha256:sha(file)})),quality:{syntax:{php_files:php,javascript_files:js},git_diff_check_both:true,sgf_phpstan_pint:'not_installed_not_run',native_application_code_unchanged_prior_static_quality_not_repeated:true,new_dependencies:false},result:{original_minor:27500,replacement_minor:29500,delta_minor:2000,journals:3,lines:6,gross_each_side_minor:84500,current_cost_minor:29500,atomic_group:true,isolated_parts_public_and_sql_refused:true,same_and_other_key_single_result:true},rights:{distinct_ledger_rights:10,ledger_grants_per_standard_new_fixture:12,existing_correction_rights_only:true,real_or_public_grants:0},team_demo:demo,preservation:{old8054_source_outbox_rights_exact:true,old8054_correction_flag_off_and_schema_absent:true,six_pending_records_untouched:true,blocked_cache_not_cleared:true,existing_demos_not_reset:true,original_journal_calculations_decisions_outbox_preserved:true,previous_valid_proofs_preserved:true,unrelated_work_preserved:true,destination_unchanged:true},limits:{visual_review_pending:true,actual_browser:false,deployment_capacity_validated:false,new_large_volume_benchmarks:false,new_indexes:false,all_six_modules_operational_composition:false,additional_implicit_revaluation_chains:false,closed_period_correction_or_reopening:false,stock_valuation_or_posting:false,payroll_or_industrial_cost_posting:false,real_tax_chart_or_financial_operations:false,destination_migrations_or_grants:false,publishing:false,purchasing_activation:false,checkpoint:false,acceptance_presumed:false}};
fs.writeFileSync(output,JSON.stringify(result,null,2)+'\n');console.log(JSON.stringify({output,regression:30,security:8,http:58,workers:6,team:23,native_supplier:23,previous_artifacts_verified:preserved,syntax:result.quality.syntax,demo:demo.url}));