#!/usr/bin/env python3 """C332 death-partition sensitivity in exactly the twenty frozen C319 controls. python C332_Independent_Audit.py --sources DIR --out DIR Inputs: C319_DATA.json, C319_REPORT.md, C286_DATA.json. No index2 extension, new endpoint/control/phase, or C333 research. """ import argparse import hashlib import itertools import json from fractions import Fraction as F from pathlib import Path def encode(x): if isinstance(x, F): return str(x) if isinstance(x, dict): return {str(k): encode(v) for k, v in x.items()} if isinstance(x, (list, tuple)): return [encode(v) for v in x] return x def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--sources', type=Path, required=True) parser.add_argument('--out', type=Path, required=True) args = parser.parse_args() names = ['C319_DATA.json', 'C319_REPORT.md', 'C286_DATA.json'] raw = {name: (args.sources / name).read_bytes() for name in names} controls = json.loads(raw['C319_DATA.json']) source = json.loads(raw['C286_DATA.json']) register, source_pairs = source['target_register'], source['source_pairs'] rows, phase_rows, checks = [], [], [] def check(name, actual, expected): passed = actual == expected checks.append({'name': name, 'actual': encode(actual), 'expected': encode(expected), 'pass': passed}) if not passed: raise AssertionError(name) check('C319 twenty controls', len(controls['rows']), 20) check('fixed five-index/four-control domain', sorted((r['a'], r['b'], r['i']) for r in controls['rows']), sorted(itertools.product([0, 2], [0, 2], range(3, 8)))) for old in sorted(controls['rows'], key=lambda r: (r['a'], r['b'], r['i'])): a, b, i = old['a'], old['b'], old['i'] label = f'a{a}/b{b}/i{i}' s, q, r, n = [F(old[k]) for k in ['S', 'q', 'R', 'n']] noah_hits = [x for x in register if x['tradition'] == 'SP' and x['role'] == 'Noah birth' and x['Cainan'] == 0 and x['Terah60'] == 0 and x['full430'] == 1 and x['center'] == n] check(label + '/one actual SP Noah', len(noah_hits), 1) noah = noah_hits[0] gear, rail = noah['gear'], noah['rail'] death_hits = [x for x in register if x['tradition'] == 'SP' and x['role'] == 'Noah death' and x['gear'] == gear and x.get('rail') == rail and x['Cainan'] == 0 and x['Terah60'] == 0 and x['full430'] == 1] mt_hits = [x for x in register if x['tradition'] == 'MT' and x['role'] == 'Noah birth' and x['gear'] == gear and x['Cainan'] == 1 and x['Terah60'] == 1 and x['full430'] == 1] check(label + '/one same-rail death', len(death_hits), 1) check(label + '/one actual same-Gear MT Noah', len(mt_hits), 1) death, mt = death_hits[0], mt_hits[0] d, m = F(death['center']), F(mt['center']) boundary = 'Flood start' if rail == 'primary' else 'Flood close' required_gear = gear + b // 2 flood_hits = [x for x in register if x['tradition'] == 'MT' and x['role'] == boundary and x['gear'] == required_gear and x['Cainan'] == 1 and x['Terah60'] == 1 and x['full430'] == 1 and x['center'] == r] cumulative_hits = [x for x in source_pairs if x['Shem_Cainan'] == 1 and x['Flood_Cainan'] == 0 and x['Shem_index'] == i + a and x['Flood_index'] == i and x['S'] == s and x['F'] == q] cumulative_ok, regular_ok = bool(cumulative_hits), bool(flood_hits) joint = cumulative_ok and regular_ok check(label + '/S membership preserved', cumulative_ok, old['cumulative_admitted']) check(label + '/R membership preserved', regular_ok, old['regular_admitted']) check(label + '/joint membership preserved', joint, old['joint_admitted']) check(label + '/at most one typed S and R record', max(len(cumulative_hits), len(flood_hits)) <= 1, True) check(label + '/sourceNoah950', n - d, F(950)) check(label + '/death remainder formula', d - r, F(110 - b)) check(label + '/actual MT birth residual', m - d, F(490 if rail == 'primary' else 491)) check(label + '/actual context leg', n - m, F(460 if rail == 'primary' else 459)) sides = [q - s, n - r] widths = [[r - s, n - s], [r - q, n - q]] two17 = r - s == 8993 and n - q == 8993 all_k = all(value.denominator == 1 and value.numerator % 529 == 0 for value in sides + widths[0] + widths[1]) check(label + '/held two short spans', sides, [F(x) for x in old['sides']]) check(label + '/held width matrix', widths, [[F(x) for x in line] for line in old['crosswidths']]) check(label + '/two17K control', two17, old['two17K']) check(label + '/allK control', all_k, old['allK']) p108, p490 = n - d == 950 and d - r == 108, m - d == 490 check(label + '/108 equivalent to b2', p108, b == 2) check(label + '/490 equivalent to primary rail', p490, rail == 'primary') entry = {'a': a, 'b': b, 'i': i, 'SP_gear': gear, 'SP_rail': rail, 'formal_coordinates': {'S': s, 'q': q, 'R': r, 'N': n, 'M': m, 'D': d}, 'records': {'N': noah, 'M': mt, 'D': death}, 'R_required_Gear': required_gear, 'R_required_boundary': boundary, 'R_actual_records': flood_hits, 'S_required_index': i + a, 'S_actual_pair_records': cumulative_hits, 'regular_admitted': regular_ok, 'cumulative_admitted': cumulative_ok, 'joint_admitted': joint, 'Noah_life': n - d, 'N_minus_M': n - m, 'M_minus_D': m - d, 'D_minus_R': d - r, 'N_minus_R': n - r, 'death950_plus108': p108, 'Noah490': p490, 'both_new_criteria': p108 and p490, 'two17K': two17, 'allK': all_k, 'both_plus_two17K': p108 and p490 and two17, 'source_status': 'joint admitted' if joint else 'retained formal control; missing S and/or R source membership'} rows.append(entry) for original in old['components']: sc, qc, rc, nc = [F(x) for x in original] phase = nc - n check(label + '/held phase identity/' + str(phase), [sc - s, qc - q, rc - r, nc - n], [phase] * 4) check(label + '/held quarter phase/' + str(phase), phase in [F(-1, 4), F(1, 4)], True) mc, dc = m + phase, d + phase phase_rows.append({'a': a, 'b': b, 'i': i, 'phase': phase, 'inherited_components_S_q_R_N': original, 'appended_M': mc, 'appended_D': dc, 'Noah_life': nc - dc, 'M_minus_D': mc - dc, 'D_minus_R': dc - rc, 'death950_plus108': nc - dc == 950 and dc - rc == 108, 'Noah490': mc - dc == 490, 'joint_admitted': joint, 'same_criteria_as_annual': (nc - dc == 950 and dc - rc == 108) == p108 and (mc - dc == 490) == p490}) predicates = ['death950_plus108', 'Noah490', 'both_new_criteria', 'two17K', 'allK', 'both_plus_two17K'] counts = {'formal': {key: sum(r[key] for r in rows) for key in predicates}, 'joint_admitted': {key: sum(r['joint_admitted'] and r[key] for r in rows) for key in predicates}, 'formal_rows': len(rows), 'joint_admitted_rows': sum(r['joint_admitted'] for r in rows), 'phase_rows': len(phase_rows), 'joint_admitted_phase_rows': sum(r['joint_admitted'] for r in phase_rows)} check('formal counts', counts['formal'], {'death950_plus108': 10, 'Noah490': 12, 'both_new_criteria': 6, 'two17K': 10, 'allK': 5, 'both_plus_two17K': 3}) check('joint-admitted counts', counts['joint_admitted'], {'death950_plus108': 6, 'Noah490': 9, 'both_new_criteria': 4, 'two17K': 8, 'allK': 3, 'both_plus_two17K': 2}) check('fourteen joint-admitted rows', counts['joint_admitted_rows'], 14) check('forty held phase components', len(phase_rows), 40) check('twentyeight admitted components', counts['joint_admitted_phase_rows'], 28) check('all40 preserve annual criteria', all(r['same_criteria_as_annual'] for r in phase_rows), True) def keys(selected): return [(r['a'], r['b'], r['i']) for r in selected] both = [r for r in rows if r['joint_admitted'] and r['both_new_criteria']] counterexamples = [r for r in both if not r['allK']] two17_survivors = [r for r in rows if r['joint_admitted'] and r['both_plus_two17K']] allk_without490 = [r for r in rows if r['joint_admitted'] and r['allK'] and not r['Noah490']] check('both criteria admitted survivors', keys(both), [(0, 2, 3), (0, 2, 5), (2, 2, 3), (2, 2, 5)]) check('source-valid counterexamples to paired22 selection', keys(counterexamples), [(0, 2, 3), (0, 2, 5)]) check('adding existing two17K yields only22 primaries', keys(two17_survivors), [(2, 2, 3), (2, 2, 5)]) check('retained allK companion failing490', keys(allk_without490), [(2, 2, 4)]) groups = [] for a, b in itertools.product([0, 2], repeat=2): group = [r for r in rows if r['a'] == a and r['b'] == b] groups.append({'a': a, 'b': b, 'joint_indices': [r['i'] for r in group if r['joint_admitted']], 'admitted_108_indices': [r['i'] for r in group if r['joint_admitted'] and r['death950_plus108']], 'admitted_490_indices': [r['i'] for r in group if r['joint_admitted'] and r['Noah490']], 'admitted_both_indices': [r['i'] for r in group if r['joint_admitted'] and r['both_new_criteria']], 'admitted_both_plus_two17K_indices': [r['i'] for r in group if r['joint_admitted'] and r['both_plus_two17K']]}) result = {'step': 'C332', 'source_sha256': {name: hashlib.sha256(value).hexdigest() for name, value in raw.items()}, 'rows': rows, 'phase_rows': phase_rows, 'groups': groups, 'counts': counts, 'both_criteria_counterexamples_to_allK': counterexamples, 'both_plus_two17K_survivors': two17_survivors, 'allK_without490': allk_without490, 'checks': checks, 'check_count': len(checks), 'all_checks_passed': all(r['pass'] for r in checks), 'dependency': '950+108 is exactly the right2K criterion b=2. Noah490 restricts primary rail. Existing two17K adds a=b, conditionally giving22.', 'claim_status': 'Finite conditional selection and counterexamples, not an independent source-prescribed selector.', 'prohibitions_observed': ['No C287 index2 extension', 'No new endpoint/control/phase', 'No missing-source repair', 'No C333 new research']} args.out.mkdir(parents=True, exist_ok=True) path = args.out / 'C332_Independent_Audit_Results.json' path.write_text(json.dumps(encode(result), indent=2, sort_keys=True) + '\n') print(json.dumps({'result': str(path), 'checks': len(checks), 'counts': counts, 'all_checks_passed': result['all_checks_passed']}, sort_keys=True)) if __name__ == '__main__': main()