from pathlib import Path import datetime, hashlib, json, subprocess, zipfile root = Path(__file__).resolve().parents[2] out = Path(__file__).resolve().parent release = root / 'outputs/investmoz-parques-QA-20261016' review = release / 'para-revisao' sha = lambda p: hashlib.sha256(p.read_bytes()).hexdigest() m = json.loads((review / 'MANIFESTO-CANDIDATA.json').read_text('utf-8')) r = json.loads((review / 'RECIBO-INTEGRIDADE.json').read_text('utf-8')) candidate = 'parques-operacionais-QA-6aacf653a98e' assert m['candidate'] == r['candidate'] == candidate assert sha(review / 'RECIBO-INTEGRIDADE.json') == '022f92f9c8a2af5b4f8711d7cddd61f2a0dd079995350c49142b169e7176ad5e' for name, entry in r['documents'].items(): assert sha(review / name) == entry['sha256'], name archive = review / r['package']['name'] assert sha(archive) == r['package']['sha256'] == '559e6846f26b05cbdf8b5d7c5d388dd0d25bff27be3e504019e55f081ade7fd3' with zipfile.ZipFile(archive) as z: assert z.testzip() is None and len(z.namelist()) == 388 for item in m['sources']: assert hashlib.sha256(z.read('candidate/sources/' + item['path'])).hexdigest() == item['candidate_sha256'] for name, digest in m['build'].items(): assert hashlib.sha256(z.read('candidate/build/' + name)).hexdigest() == digest base = json.loads((release / 'ATIVO-ANTES.json').read_text('utf-8')) actual = {p.relative_to(root).as_posix(): sha(p) for folder in ['app','config','routes','database','resources','public/build'] for p in (root/folder).rglob('*') if p.is_file()} assert actual == base['files'], 'Runtime drift; reconcile before delivery' for name, digest in base['protected'].items(): assert sha(root/name) == digest, name for item in m['sources']: path = root / item['path'] assert (sha(path) if path.exists() else None) == item['before_sha256'], item['path'] assert not (out / 'AUTORIZACAO-TEMPO.json').exists() a = {'at': datetime.datetime.now(datetime.timezone.utc).isoformat(), 'candidate': candidate, 'decision_record': 'Mensagem expressa do responsável Tempo neste turno: pode implementar. Refere-se à candidata e proposta conferidas imediatamente antes.', 'code_hash': m['code_hash'], 'package_sha256': sha(archive), 'QA_receipt_sha256': sha(review/'RECIBO-INTEGRIDADE.json'), 'scope': {'application_artifacts':16, 'substitutions':11, 'new_sources':4, 'new_migration':1, 'compiled_files':162, 'migration':'2026_10_16_130000_add_investment_zone_editorial_draft'}, 'limits':['No real parks saved, classified or published; no fixtures imported', 'No grants, MFA changes, flags or other migrations', 'Preserve B, COUNTRY02, data cache, data and history', 'Fresh backup/restore/boot; authenticated installation only', 'Compatible partial rollback retains seven functional sources and data; full parks sheet remains open', 'No accepted functional tests repeated']} (out/'AUTORIZACAO-TEMPO.json').write_text(json.dumps(a,ensure_ascii=False,indent=2)+'\n','utf-8') (out/'PREFLIGHT-FICHEIROS.json').write_text(json.dumps({'at':a['at'],'candidate':candidate,'runtime_files_compared':len(actual),'protected_files':len(base['protected']), 'previous_source_hashes':11,'absences':5,'package_entries':388,'package_sha256':sha(archive),'passed':True},indent=2)+'\n','utf-8') old = root/'outputs/investmoz-referencias-economicas-ativacao-20261016' text = (old/'fresh-backup.php').read_text('utf-8') text = text.replace('outputs/investmoz-referencias-economicas-QA-20261016','outputs/investmoz-parques-QA-20261016').replace('MANIFESTO-ENTREGA.json','MANIFESTO-CANDIDATA.json') text = text.replace("$manifest['application']", "$manifest['sources']") text = text.replace('economic-references-before-','parks-before-').replace('economic_refs_before_','parks_before_') start = text.index(" $state=['ledger'") end = text.index(" $env=Dotenv", start) text = text[:start] + ''' $state=['ledger'=>(int)$source->query("SELECT count(*) FROM migrations WHERE migration='2026_10_16_130000_add_investment_zone_editorial_draft'")->fetchColumn(),'column'=>(int)$source->query("SELECT count(*) FROM information_schema.columns WHERE table_schema='public' AND table_name='investment_zones' AND column_name='editorial_draft'")->fetchColumn(),'economic_migration'=>(int)$source->query("SELECT count(*) FROM migrations WHERE migration='2026_10_16_120000_extend_economic_reference_hierarchy'")->fetchColumn()]; if($state!==['ledger'=>0,'column'=>0,'economic_migration'=>1])throw new RuntimeException('Unexpected parks schema state; reconcile before delivery'); file_put_contents(__DIR__.'/PREFLIGHT-ESQUEMA.json',json_encode(['at'=>gmdate('c'),'read_only'=>true,'schema'=>$state],JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR).PHP_EOL); ''' + text[end:] text = text.replace("'storage/app/private/ai-documents']", "'storage/app/private/ai-documents','storage/app/private/investment-zones']") (out/'fresh-backup.php').write_text(text,'utf-8') text = (old/'boot-restored-clone.php').read_text('utf-8') text = text.replace('referencias-economicas-QA-966202f15577',candidate).replace('economic_refs_before_','parks_before_').replace('economic-restore.test','parks-restore.test') text = text.replace("['/up','/login','/negocios']", "['/up','/login','/zonas','/api/v2/investment/zones']") (out/'boot-restored-clone.php').write_text(text,'utf-8') (out/'cache-snapshot.php').write_bytes((old/'cache-snapshot.php').read_bytes()) print(json.dumps({'candidate':candidate,'preflight':'passed','fresh_backup_scripts_prepared':True,'active_changes':0}))