"""Post-benchmark diagnostic: supply only Welcome's true counter phase at the page break."""
import json
from pathlib import Path
from missing_inputs_108 import geometry, grade, model, skip_segment, B
from lp_core import pages, vals
D=Path(__file__).resolve().parent
records={r['name']:r for r in json.loads((D/'mechanism_coverage_results.json').read_text())['known_method_groups']}
ref={r['name']:r['values'] for r in json.loads((D/'increment_calibration_sources.json').read_text())['groups']}
results=json.loads((D/'missing_inputs_108.json').read_text())['cases']['welcome']
row=records['welcome']
def src(r):return '%'.join(pages[i] for i in r['parsed_page_indices'])
c=list(map(int,vals(src(row)))); st,en=geometry(src(row)); t,_=model([(src(r),ref[k]) for k,r in records.items() if k!='welcome'])
k=row['ordinal_key_values']; f=row['unenciphered_F_offsets']; cut=results['prefix_length']
s,p,sk=skip_segment(c,k,st,en,t,0,cut,(0,B))
true_phase=(cut-sum(i<cut for i in f))%len(k)
_,base,f0=skip_segment(c,k,st,en,t,cut,len(c),s)
_,alt,f1=skip_segment(c,k,st,en,t,cut,len(c),(true_phase,s[1]))
g0=grade(p+base,ref['welcome'],cut);g1=grade(p+alt,ref['welcome'],cut)
assert results['skip_locations_hidden']['plaintext_sha256']==g0['plaintext_sha256']
out=dict(status='Post-hoc oracle; not a held-out benchmark pass.',cut=cut,period=len(k),
         predicted_phase=s[0],true_phase=true_phase,prefix_errors=[i for i in g0['error_offsets'] if i<cut],
         continuation_length=len(c)-cut,original_correct=g0['continuation_correct'],oracle_correct=g1['continuation_correct'],
         actual_continuation_skips=[i for i in f if i>=cut],original_continuation_skips=f0,oracle_continuation_skips=f1,
         oracle_continuation_error_offsets=[i for i in g1['error_offsets'] if i>=cut])
(D/'welcome_phase_108.json').write_text(json.dumps(out,indent=2)+'\n')
print(out)
