import fs from 'node:fs';import path from 'node:path';import crypto from 'node:crypto'; const read=p=>JSON.parse(fs.readFileSync(p,'utf8').replace(/^\uFEFF/,''));const hash=p=>crypto.createHash('sha256').update(fs.readFileSync(p)).digest('hex'); const state=read('C:/laragon/data/sgf-tempo/run/modular-regression-current.json'),tag=process.argv[2];if(state.port!==55439||!/^[a-f0-9]{12}$/.test(tag??''))throw Error('Owned QA tag required'); const root=path.join(state.root,'private/ledger-'+tag),owned=read(path.join(root,'settings.json'));if(owned.tag!==tag||owned.cluster!=='sgf_modular_regression_'+state.id)throw Error('Owned cluster mismatch'); const cases=read(path.join(root,'ledger-cases.json')),race=read(path.join(root,'ledger-race.json')),http=read(path.join(root,'ledger-http.json')),ui=read(path.join(root,'ledger-ui.json')),uiState=read(path.join(root,'ledger-ui-state.json')),post=read(path.join(root,'ledger-post.json')),integrity=read(path.join(root,'ledger-integrity.json')); if([cases,race,http,ui,uiState,post,integrity].some(p=>!p.passed)||cases.cases.some(c=>!c.passed)||post.journals!==1||post.lines!==2||post.posting_events!==1||post.debit_minor!==6000||post.credit_minor!==6000||post.default_flag!==false||!post.financial_fingerprint_equal)throw Error('Ledger proof incomplete'); const previous='docs/proofs/sgf-payable-report-20261102.json',prior=read(previous);for(const p of prior.evidence)if(hash(path.join(state.root,p.file))!==p.sha256)throw Error('Previous proof changed'); const spec='docs/GENERAL_LEDGER_FIRST_SYNTHETIC_SCOPE.md',currentSpec=fs.readFileSync(spec,'utf8'),approved=currentSpec.replace(/\r?\n\*\*Autorização recebida[^\r\n]*\r?\n/,''); const expected=prior.accounting_next.sha256;let approvedBytes; for(const candidate of [approved,approved.replaceAll('\r\n','\n'),approved.replace(/\r?\n/g,'\r\n')])if(crypto.createHash('sha256').update(candidate).digest('hex')===expected)approvedBytes=candidate; if(!approvedBytes)throw Error('Approved scope text changed beyond its authorization note'); const archive=path.join(root,'approved-ledger-scope.md');if(!fs.existsSync(archive))fs.writeFileSync(archive,approvedBytes);if(hash(archive)!==expected)throw Error('Approved scope archive differs'); const teamRoot=path.join(state.root,'private/ledger-5905dd7a5ecf'),team=read(path.join(teamRoot,'ledger-demo.json')),teamHttp=read(path.join(teamRoot,'ledger-demo-http.json')),teamState=read(path.join(teamRoot,'ledger-demo-state.json')),teamCookie=read(path.join(teamRoot,'ledger-demo-cookie.json')); if(!teamHttp.passed||!teamState.passed||!teamCookie.passed||teamState.journals!==0||teamState.proposals!==0||teamState.qa_grants!==6||!teamCookie.shared_xsrf_cookie_not_replaced)throw Error('Independent demo proof incomplete'); const files=['packages/general-ledger/bootstrap.php','packages/general-ledger/src/LedgerSchema.php','packages/general-ledger/src/PayableJournal.php','packages/general-ledger/README.md','app/Modules/Accounting/GeneralLedger.php','app/Modules/Accounting/GeneralLedgerController.php','app/Providers/AppServiceProvider.php','app/Shared/Context/ModuleNavigation.php','config/general_ledger.php','routes/web.php','resources/views/accounting/general-ledger.blade.php','resources/js/AccountingMenuWorkspace.jsx','scripts/ledger-run.mjs','scripts/ledger-proof.php','scripts/ledger-cases.php','scripts/ledger-race.php','scripts/ledger-ui-check.mjs','scripts/ledger-http.mjs','scripts/ledger-http-entry.php','scripts/ledger-http-router.php','scripts/ledger-demo.mjs','scripts/ledger-demo-check.mjs','scripts/ledger-demo-cookie-check.mjs','scripts/payable-history-sgf-proof.php','scripts/summarize-ledger.mjs','docs/integration/sgf-flow-plan.json','scripts/inspect-module-flow-plan.mjs']; const evidence=['ledger-fixture.json','ledger-cases.json','ledger-race.json','ledger-http.json','ledger-http-read-mode.json','ledger-ui.json','ledger-ui-state.json','ledger-post.json','ledger-integrity.json','approved-ledger-scope.md'].map(file=>({file:'private/ledger-'+tag+'/'+file,sha256:hash(path.join(root,file))})); const result={increment:'COMMON-LEDGER-PAYABLE-SIM-v1',date:'2026-11-02',authorization:{answer:'Autorizar o incremento sintético proposto',approved_scope_sha256:expected,scope_archive_in_evidence:true,real_access:false,acceptance_or_checkpoint:false},synthetic_only:true, tests:cases,concurrency:{passed:race.passed,waiting:race.waiting,both_completed_after_release:true,same_journal_id:race.same_journal_id,journals:race.journals,lines:race.lines,posting_events:race.posting_events,replay_without_duplicate:race.replay_without_duplicate},http,ui,posted_proposal_ui:uiState,preservation:post,integrity, sources:files.map(file=>({file,sha256:hash(file)})),evidence,team_demo:{url:team.url,isolated_from_regression:true,checks:teamHttp.checks,initial_state:teamState,cookie_isolation:teamCookie,evidence:['ledger-demo.json','ledger-demo-http.json','ledger-demo-state.json','ledger-demo-cookie.json'].map(file=>({file:'private/ledger-5905dd7a5ecf/'+file,sha256:hash(path.join(teamRoot,file))}))},flow_inventory:{file:'docs/proofs/module-flow-ledger-update-20261102.json',sha256:hash('docs/proofs/module-flow-ledger-update-20261102.json'),static_only:true},previous_proof:{file:previous,sha256:hash(previous),all_recorded_private_artifacts_preserved:true}, private_build:{manifest_sha256:hash(path.join(owned.sgf.storage,'public/build/manifest.json')),installed_public_manifest_not_replaced:true}, limits:['General module/navigation check did not complete and was interrupted; not counted as passing. Three targeted static permission/render checks, private production build and actual module/context HTTP refusals passed.','Only SGF QA adapter and one recognition scenario. Native SGC adapter, real PGC mapping, fiscal recognition, bank/settlement accounting, correction/reversal, operational periods/closing and deployment remain outside delivery.','Stock and budget tables are absent in this new minimal QA composition; those engines were not exercised. No new stock/budget binding or write is performed by the ledger.','The QA application role owns its isolated database. Production role/schema hardening and sustained volume/concurrency are not validated.','Configuration writes have no application API in this increment; future configuration/source writers must use the documented company serialization boundary.'], distinct_new_qa_rights:['general_ledger.view','general_ledger.prepare','general_ledger.validate','general_ledger.post'],qa_grants:6,real_grants:0,operational_migrations:false,default_enabled:false,six_pending_associations_untouched:true,blocked_cache_not_cleared:true,demos_preserved:true,native_purchasing_activation:false,publication:false,checkpoint:false,visual_review_pending:true,deployment_capacity_validated:false}; fs.writeFileSync('docs/proofs/common-ledger-payable-20261102.json',JSON.stringify(result,null,2)+'\n');console.log(JSON.stringify({cases:cases.cases.length,assertions:cases.assertions,http_checks:http.checks,journals:1,lines:2,debit_minor:6000,credit_minor:6000,default_enabled:false,approved_scope_archived:true}));