#!/usr/bin/env python3 """C327 exhaustive residual census of held source biography endpoints. python C327_Independent_Audit.py --sources DIR --out DIR Input: C322_DATA.json. No E execution, new endpoint/context, or C328 work. """ import argparse import hashlib import itertools import json from collections import Counter from fractions import Fraction as F from pathlib import Path def sign(x): return 'positive' if x > 0 else 'negative' if x < 0 else 'zero' def encode(value): if isinstance(value, F): return str(value) if isinstance(value, dict): return {str(k): encode(v) for k, v in value.items()} if isinstance(value, (list, tuple)): return [encode(v) for v in value] return value 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() raw = (args.sources / 'C322_DATA.json').read_bytes() source = json.loads(raw) phases = [F(-1, 4), F(1, 4)] roles = ['Noah birth', 'Shem birth', 'Flood start', 'Noah death', 'Shem death'] checks, annual, components, biographies = [], [], [], [] def check(name, actual, expected): ok = actual == expected checks.append({'name': name, 'actual': encode(actual), 'expected': encode(expected), 'pass': ok}) if not ok: raise AssertionError(name) for row in source['biographies']: gear, index = row['gear'], row['i'] label = f'G{gear}/i{index}' sp, mt = row['SP_records'], row['MT_records'] check(label + '/role menu', row['roles'], roles) biographies.append({'gear': gear, 'source_i': index, 'SP_records': sp, 'MT_records': mt}) exact = {} for comp in row['components']: p = F(comp['phase']) for tradition in ['SP', 'MT']: for pos, role in enumerate(roles): value = F(comp[tradition][pos]) exact[tradition, role, p] = value check(label + '/' + tradition + '/' + role + '/' + str(p), value - row[tradition + '_records'][role]['center'], p) for role in roles: check(label + '/same role translation/' + role, sp[role]['center'] - mt[role]['center'], 460) check(label + '/SP state/' + role, [sp[role][k] for k in ['tradition', 'gear', 'Cainan', 'Terah60', 'full430']], ['SP', gear, 0, 0, 1]) check(label + '/MT state/' + role, [mt[role][k] for k in ['tradition', 'gear', 'Cainan', 'Terah60', 'full430']], ['MT', gear, 1, 1, 1]) for a_role, c_role in itertools.product(roles, repeat=2): a, b, c = F(sp[a_role]['center']), F(mt[a_role]['center']), F(sp[c_role]['center']) d, r = a - c, b - c check(label + '/' + a_role + '/' + c_role + '/residual identity', r, d - 460) category = ('reversed_source_order' if d < 0 else 'same_source_role' if d == 0 else 'positive_source_span_below460' if d < 460 else 'source_span_equal460' if d == 460 else 'positive_residual_partition') candidate = {'gear': gear, 'source_i': index, 'anchor_role': a_role, 'terminal_role': c_role, 'records': {'A': sp[a_role], 'B': mt[a_role], 'C': sp[c_role]}, 'coordinates_ABC': [a, b, c], 'source_duration_d': d, 'context_leg': a - b, 'cross_context_residual_r': r, 'residual_sign': sign(r), 'source_order_category': category} annual.append(candidate) for pa, pb, pc in itertools.product(phases, repeat=3): av, bv, cv = exact['SP', a_role, pa], exact['MT', a_role, pb], exact['SP', c_role, pc] context, duration, residual = av - bv, av - cv, bv - cv offsets = [context - 460, duration - d, residual - r] preserved = all(x == 0 for x in offsets) components.append({'gear': gear, 'source_i': index, 'anchor_role': a_role, 'terminal_role': c_role, 'physical_phases_ABC': [pa, pb, pc], 'coordinates_ABC': [av, bv, cv], 'context_leg': context, 'source_duration': duration, 'residual': residual, 'annual_d': d, 'annual_r': r, 'residuals_context_source_residual': offsets, 'all_annual_lengths_preserved': preserved, 'residual_sign': sign(residual), 'partition_identity': residual == duration - context, 'equal_phase_criterion': preserved == (pa == pb == pc), 'annual_sign_preserved': sign(residual) == sign(r)}) positive = [r for r in annual if r['residual_sign'] == 'positive'] exact = [r for r in components if r['all_annual_lengths_preserved']] by_value = Counter(r['cross_context_residual_r'] for r in annual) distribution = [{'d': d, 'r': r, 'instances': count} for (d, r), count in sorted(Counter((row['source_duration_d'], row['cross_context_residual_r']) for row in annual).items())] counts = { 'annual_candidates': len(annual), 'annual_positive': len(positive), 'annual_negative': sum(r['residual_sign'] == 'negative' for r in annual), 'annual_zero': sum(r['residual_sign'] == 'zero' for r in annual), 'phase_candidates': len(components), 'exact_annual_partitions': len(exact), 'exact_positive_partitions': sum(r['residual_sign'] == 'positive' for r in exact), 'exact_negative_partitions': sum(r['residual_sign'] == 'negative' for r in exact), 'phase_positive': sum(r['residual_sign'] == 'positive' for r in components), 'phase_negative': sum(r['residual_sign'] == 'negative' for r in components), 'phase_zero': sum(r['residual_sign'] == 'zero' for r in components), 'individual_exact_legs_context_source_residual': [sum(row['residuals_context_source_residual'][k] == 0 for row in components) for k in range(3)], 'annual_source_order_categories': dict(Counter(r['source_order_category'] for r in annual)), 'positive_residual_multiplicities': {str(k): v for k, v in sorted(by_value.items()) if k > 0}, } for field, expected in [('annual_candidates', 75), ('annual_positive', 18), ('annual_negative', 57), ('annual_zero', 0), ('phase_candidates', 600), ('exact_annual_partitions', 150), ('exact_positive_partitions', 36), ('exact_negative_partitions', 114), ('phase_positive', 144), ('phase_negative', 456), ('phase_zero', 0)]: check('count/' + field, counts[field], expected) check('positive values and multiplicities', counts['positive_residual_multiplicities'], {'40': 6, '140': 6, '490': 3, '640': 3}) check('source order category counts', counts['annual_source_order_categories'], {'same_source_role': 15, 'positive_residual_partition': 18, 'positive_source_span_below460': 12, 'reversed_source_order': 30}) check('individual phase leg preservation counts', counts['individual_exact_legs_context_source_residual'], [300, 300, 300]) check('all600 exact arithmetic partition identities', all(r['partition_identity'] for r in components), True) check('all600 equality criteria', all(r['equal_phase_criterion'] for r in components), True) check('all600 signs stable', all(r['annual_sign_preserved'] for r in components), True) expected_pairs = {('Noah birth', 'Shem birth'), ('Noah birth', 'Flood start'), ('Noah birth', 'Noah death'), ('Noah birth', 'Shem death'), ('Shem birth', 'Shem death'), ('Flood start', 'Shem death')} check('six positive role families', set((r['anchor_role'], r['terminal_role']) for r in positive) == expected_pairs, True) check('closest nonpositive residual', max(r['cross_context_residual_r'] for r in annual if r['cross_context_residual_r'] <= 0), F(-10)) result = {'step': 'C327', 'source_sha256': hashlib.sha256(raw).hexdigest(), 'scope': 'All 3x5x5 same-Gear ordered role pairs and 8 independent held phase choices per pair.', 'source_biographies': biographies, 'counts': counts, 'duration_residual_distribution': distribution, 'annual_candidates': annual, 'positive_annual_candidates': positive, 'phase_candidates': components, 'checks': checks, 'check_count': len(checks), 'all_checks_passed': all(r['pass'] for r in checks), 'dependency': 'r=d-460 is forced by the admitted same-role context translation. Phase identity always holds; exact annual targets require equal physical phases.', 'claim_status': 'Finite source-qualified residual ledger, not new independent witnesses, a source-prescribed expansion, or archive-wide novelty.', 'prohibitions_observed': ['No E execution', 'No new endpoint or context', 'No fitted anchor', 'No C328 calculation']} args.out.mkdir(parents=True, exist_ok=True) target = args.out / 'C327_Independent_Audit_Results.json' target.write_text(json.dumps(encode(result), indent=2, sort_keys=True) + '\n') print(json.dumps({'result': str(target), 'checks': len(checks), 'counts': counts, 'all_checks_passed': result['all_checks_passed']}, sort_keys=True)) if __name__ == '__main__': main()