Files
MeltMind/tools/prepare_g1_blind120_sample.py

49 lines
5.2 KiB
Python

"""Locked same-key sampling only; identities stay in local custodian directory."""
from pathlib import Path
from itertools import combinations
from collections import Counter
import hashlib,json,os,secrets
import numpy as np
ROOT=Path('artifacts/s2-g1-blind120-2026-11-14')
def save(path,x):path.write_text(json.dumps(x,indent=2,ensure_ascii=False)+'\n')
def main():
ROOT.mkdir(exist_ok=False);sealed=ROOT/'custodian';sealed.mkdir(mode=0o700);public=ROOT/'public';public.mkdir()
old=Path('artifacts/s2-g1-inventory-2026-11-14/custodian')
clusters=json.loads((old/'source_clusters.json').read_text());parts={r['case']:r for r in json.loads((old/'parts.json').read_text())}
rng=np.random.Generator(np.random.PCG64(np.random.SeedSequence([2026111413]).spawn(2)[0]))
selected=[];strata=[];carry=0
for si,(lo,hi,quota) in enumerate([(2,2,30),(3,5,45),(6,10**9,45)]):
groups=sorted((g for g in clusters if lo<=len(g['members'])<=hi),key=lambda g:g['source_key'])
order=rng.permutation(len(groups)).tolist();pools={g['source_key']:list(combinations(sorted(g['members']),2)) for g in groups}
frame_pairs=sum(len(v) for v in pools.values());counts=Counter();rows=[];target=quota+carry
while len(rows)<target:
progress=False
for gi in order:
if len(rows)==target:break
g=groups[gi];key=g['source_key']
if counts[key]>=3 or not pools[key]:continue
pair=pools[key].pop(int(rng.integers(len(pools[key]))));counts[key]+=1;progress=True
rows.append({'a':pair[0],'b':pair[1],'source_key':key,'stratum':si,'frame_pairs_within_key':len(g['members'])*(len(g['members'])-1)//2,'source_cluster_state':g['state'], 'endpoint_exposure':[parts[c]['exposure_state'] for c in pair]})
if not progress:break
carry=target-len(rows)
# Current locked frame has no key shortage inside any round; exact
# marginal pair inclusion = expected key draws / key pair count.
assert not carry and all(len(g['members'])*(len(g['members'])-1)//2>=min(3,(target+len(groups)-1)//len(groups)) for g in groups)
for row in rows:row['marginal_inclusion_probability']=len(rows)/len(groups)/row['frame_pairs_within_key']
selected.extend(rows);strata.append({'stratum':si,'key_size_min':lo,'key_size_max':None if hi==10**9 else hi,'keys':len(groups),'frame_pairs':frame_pairs,'quota':quota,'sampled':len(rows),'sampled_keys':len(counts),'carry_to_next':carry,'key_draw_count_histogram':dict(Counter(counts.values()))})
assert len(selected)==120 and len({(r['a'],r['b']) for r in selected})==120
secret=secrets.token_bytes(32)
for r in selected:
digest=hashlib.sha256(secret+b'pair\0'+r['a'].encode()+b'\0'+r['b'].encode()).hexdigest();r['anonymous_pair_id']=digest[:32]
swap=int(digest[32],16)%2;r['left'],r['right']=(r['b'],r['a']) if swap else (r['a'],r['b'])
r['confirmed_relation']=None;r['development_heldout_assignment']=None
selected.sort(key=lambda r:r['anonymous_pair_id']);unique=sorted({r[k] for r in selected for k in ['a','b']})
save(sealed/'sampling-and-mapping.json',{'seed_sequence':[2026111413],'sampling_spawn_key':[0],'split_spawn_key_reserved':[1],'bit_generator':'PCG64','anonymous_id_secret_hex':secret.hex(),'pairs':selected,'unique_cases':unique,'source_inventory_manifest_sha256':hashlib.sha256((old/'manifest.json').read_bytes()).hexdigest(),'ordering':'keys sorted, pairs lexicographic, one permuted key order per stratum reused each round, rng.integers selects uniformly from remaining sorted pairs','probability':'unconditional marginal within fixed stratum; q/(number_of_keys * pairs_within_key); not realized key count / pairs_within_key'})
save(sealed/'geometry-source-index.json',{'cases':[{'case':c,'existing_surface_caches':parts[c]['surface_caches']} for c in unique]})
for p in sealed.iterdir():p.chmod(0o600)
sealed_manifest=[{'file':p.name,'bytes':p.stat().st_size,'sha256':hashlib.sha256(p.read_bytes()).hexdigest()} for p in sorted(sealed.iterdir())]
summary={'status':'SAMPLED_AND_MAPPING_SEALED_NOT_EXTRACTED_OR_REVIEWED','sampling_ruling':'1794655144.867149','sampled_pairs':120,'sampled_source_keys':len({r['source_key'] for r in selected}),'unique_geometry_cases':len(unique),'strata':strata,'selected_key_exposure_counts':dict(Counter({r['source_key']:r['source_cluster_state'] for r in selected}.values())),'pair_exposure_counts':dict(Counter(r['source_cluster_state'] for r in selected)),'claim':'same-key relationship pool only; not certified natural positives, not independent unexposed or unbiased cross-key validation','unblinding':'Mold labels frozen and committed before Grok verifies seals and identity join','post_review_split':'child1 reserved, not consumed; dominant-class key stratification, rigid<template<variant ties, alternating heldout/development starting heldout; each key at most once per class','sealed_files':sealed_manifest,'code_sha256':hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),'no_geometry_read':True,'no_candidates_or_registration':True}
save(public/'sample-summary-and-seals.json',summary);print(json.dumps(summary,indent=2))
if __name__=='__main__':main()