from pathlib import Path
from fractions import Fraction as F
from itertools import permutations, product
from math import lcm
import hashlib,json
ROOT=Path('/workspace/scratch/1b40da62dcbd')
paths={
 'strategy':ROOT/'upload/490d_Unification_Research_Strategy_v0_2_20260906.md',
 'file12':ROOT/'project_sources/16-File_12.Calendrical_Physics-20260907-192303-.md',
 'file46':ROOT/'project_sources/20-File_46.Harmonic_Expansion_Protocols-1-.md',
 'file51a':ROOT/'project_sources/39-File_51a.Rounded_Scaffold_Mod5_Architecture.md',
 'file63':ROOT/'project_sources/09-File_63.Scale_Neutral_480_483_490_Carrier-20260821-143254-.md',
 'file68':ROOT/'project_sources/08-File_68.Enochic_Seven_Week_Core-20260819-145634-.md',
 'C831':ROOT/'c732_c831/deliverables/490d_Chronological_Families_Integrated_Synthesis_C831.md'
}
sources={k:{'path':str(p),'sha256':hashlib.sha256(p.read_bytes()).hexdigest()} for k,p in paths.items()}
def excerpt(key,start,end):
 lines=paths[key].read_text().splitlines()
 return {'source':key,'lines':[start,end],'text':'\n'.join(lines[start-1:end])}
packet={'status':'Preparation only; no numbered research actions. Literal source packet, not canonical edits.', 'sources':sources,
 'source_excerpts':[excerpt('file12',263,264),excerpt('file46',84,133),excerpt('file46',148,245),excerpt('file46',363,382),excerpt('file51a',26,66),excerpt('file63',510,613),excerpt('file68',8697,8865)],
 'keys':{'E':[25,23],'P':[70,69],'J':[300,299]},
 'file46_literal_base_family':{'base':4830,'deep_anchor_BC':14006,'Enoch_pivot_BC':4836,'Christ_hinge_BC':6,'terminal_AD':65,'intermediate_Abraham_ledger_end_BC':2736,'Priestly_Solomon_ledger_end_BC':1756,'double_seed':9660,'Prophetic_Adam_head_BC':4206,'Priestly_Noah_Shem_hinge_BC':3506,'Jared_head_BC':4906,'Luke_Adam_head_BC':5256,'Luke_pre_head_BC':5326},
 'file46_literal_Adam_field':{'BC_heads':[14896,14466,14436,14006],'apparent_BC_head':14926,'declared_subdivision':[30,400,30,30,400,30],'outer_span':920,'outer_E_output':1000,'source_section':'6A.5'},
 'file63_literal_quarter_ladder':[{'m':m,'whole':str(F(40*m)),'phase':str(F(161*m,4))} for m in [3,10,12,30,300]],
 'file51a_literal_conversion_chains':[{'input_role':'civil count C, not Rounded width W_R','seed':10051,'E':10925,'EE':11875,'neighbor_WR':10050},{'input_role':'civil count C, not Rounded width W_R','seed':55016,'E':59800,'EE':65000,'E_then_J':60000,'neighbor_WR':55015}],
 'exclusions':['no new second decimal inversion','no new Gear','no primers','no remote extension','composition domains are arithmetic classifications, not source authorization','fractional outputs do not invalidate exact-phase comparisons']}
(ROOT/'c832_c931/prep/operator_inputs.json').write_text(json.dumps(packet,indent=2)+'\n')
ks={k:F(*x) for k,x in packet['keys'].items()}
def word_info(w):
 coeff=F(1);den=1;prefix=[]
 for k in w:
  coeff*=ks[k];den=lcm(den,coeff.denominator);prefix.append(str(coeff))
 return {'word_execution_order':list(w),'prefix_coefficients':prefix,'final_coefficient':str(coeff),'final_integer_domain':coeff.denominator,'all_stages_integer_domain':den,'all_stages_five_grid_domain':5*den}
diag={'status':'Preparatory exact calculations; root must select/execute numbered steps separately.',
 'two_key_words':[word_info(w) for w in product(ks,repeat=2)],
 'three_distinct_key_words':[word_info(w) for w in permutations(ks)],
 'quarter_ladder':[{**row,'outputs':{k:str(F(row['phase'])*v) for k,v in ks.items()},'quarter_grid':{k:(4*F(row['phase'])*v).denominator==1 for k,v in ks.items()}} for row in packet['file63_literal_quarter_ladder']],
 'Adam_field_about14006':[{'x_BC':x,'radius':x-14006,'E_radius':str((x-14006)*ks['E']),'E_BC_coordinate':str(14006+(x-14006)*ks['E'])} for x in [14926,14896,14466,14436,14006]],
 'anchor_commutator':{'a':14006,'b':4836,'difference_EaPb_minus_PbEa':str((ks['E']-1)*(ks['P']-1)*(14006-4836))},
 'pair_order_counterexample':{'seed':6877,'J_then_P':[str(6877*ks['J']),str(6877*ks['J']*ks['P'])],'P_then_J':[str(6877*ks['P']),str(6877*ks['P']*ks['J'])]}}
(ROOT/'c832_c931/prep/operator_diagnostics.json').write_text(json.dumps(diag,indent=2)+'\n')
print(json.dumps({'sources':len(sources),'two_key_words':len(diag['two_key_words']),'three_key_words':len(diag['three_distinct_key_words']),'quarter_ladder':diag['quarter_ladder'],'commutator':diag['anchor_commutator']},indent=2))
