OpenIslet

scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py

IIP-0043 / DEC-122 (CAND-034 / EXP-031 — Bucket 1 Closure, Stage S3-R2 Remediated)

script Release v0.8.0 gen-eb955f8266035aba 1008 KB · 76 sections

Use this file in any AI chat (paste-only or attachment-capable):

Download as text (.txt) Download section (.txt)

Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py · SHA-256: 20706180ed1ed6a2…

            for cache_obj in (
                _NATIVE_BASELINE_MMHG_CACHE,
                _NATIVE_BASELINE_META_CACHE,
                _MESH_REFINEMENT_SOLVE_CACHE,
            ):
                for ck in list(cache_obj.keys()):
                    if isinstance(ck, tuple) and len(ck) >= 1 and ck[0] in purge_strs:
                        cache_obj.pop(ck, None)

    with tempfile.TemporaryDirectory() as td_b2:
        td_p = Path(td_b2)
        copy_a = (td_p / "copy_A").resolve()
        copy_b = (td_p / "copy_B").resolve()
        copy_c = (td_p / "copy_C_symlink_escape").resolve()
        _populate_minimal_copy(copy_a)
        _populate_minimal_copy(copy_b)
        _populate_minimal_copy(copy_c)
        # Replace copy_C's Layer-4 script with a symlink pointing outside copy_C (into copy_A)
        (copy_c / "scripts" / "run_layer4_tissue_and_metabolic_twin.py").unlink()
        (copy_c / "scripts" / "run_layer4_tissue_and_metabolic_twin.py").symlink_to(
            copy_a / "scripts" / "run_layer4_tissue_and_metabolic_twin.py"
        )

        copy_a_ok = True
        copy_b_ok = True
        disjoint_ok = True
        parity_ok = True
        relpaths_union: set[str] = set()
        symlink_escape_rejected = False
        try:
            for rt, ocr_val, se_val, expected_mods in route_cases:
                res_a = dispatch_and_solve_nw08_measured_ocr(rt, ocr_val, se_val, openislet_root=copy_a)
                res_b = dispatch_and_solve_nw08_measured_ocr(rt, ocr_val, se_val, openislet_root=copy_b)
                orig_a = res_a.get("resolved_solver_module_origins") or {}
                orig_b = res_b.get("resolved_solver_module_origins") or {}
                rel_a = res_a.get("resolved_solver_module_relpaths") or {}
                rel_b = res_b.get("resolved_solver_module_relpaths") or {}

                if (
                    res_a.get("solver_isolation_mode") != "ISOLATED_CHILD_SUBPROCESS"
                    or res_a.get("all_solver_module_origins_within_repo_root") is not True
                    or not orig_a
                ):
                    copy_a_ok = False
                if (
                    res_b.get("solver_isolation_mode") != "ISOLATED_CHILD_SUBPROCESS"
                    or res_b.get("all_solver_module_origins_within_repo_root") is not True
                    or not orig_b
                ):
                    copy_b_ok = False

                for mod_name in expected_mods:
                    if mod_name not in orig_a:
                        copy_a_ok = False
                    if mod_name not in orig_b:
                        copy_b_ok = False

                for mod_name, path_str in orig_a.items():
                    mp_a = Path(path_str).resolve()
                    if not mp_a.is_relative_to(copy_a):
                        copy_a_ok = False
                    if mp_a.is_relative_to(copy_b) or mp_a.is_relative_to(resolved_src):
                        disjoint_ok = False
                for mod_name, path_str in orig_b.items():
                    mp_b = Path(path_str).resolve()
                    if not mp_b.is_relative_to(copy_b):
                        copy_b_ok = False
                    if mp_b.is_relative_to(copy_a) or mp_b.is_relative_to(resolved_src):
                        disjoint_ok = False
                for mod_name in set(orig_a.keys()) & set(orig_b.keys()):
                    if orig_a[mod_name] == orig_b[mod_name]:
                        disjoint_ok = False

                if rel_a != rel_b:
                    parity_ok = False
                relpaths_union.update(str(v) for v in rel_a.values())

                if (
                    res_a.get("adjudicated_verdict") != res_b.get("adjudicated_verdict")
                    or res_a.get("baseline_min_face_unrounded_mmhg") != res_b.get("baseline_min_face_unrounded_mmhg")
                    or res_a.get("face_min_mmhg_unrounded") != res_b.get("face_min_mmhg_unrounded")
                ):
                    parity_ok = False

            try:
                dispatch_and_solve_nw08_measured_ocr("R2_DZ40", 1.3164, 0.0, openislet_root=copy_c)
            except RuntimeError:
                symlink_escape_rejected = True
        finally:
            _purge_temp_caches((copy_a, copy_b, copy_c))

    ok_33 = bool(copy_a_ok and copy_b_ok and disjoint_ok and parity_ok and symlink_escape_rejected)
    return {
        "test": "r18_b2_cross_root_sequential_copy_a_then_copy_b_module_origin_isolation",
        "routes_tested": [rt for rt, _o, _s, _m in route_cases],
        "copy_a_all_origins_strictly_inside_copy_a": copy_a_ok,
        "copy_b_all_origins_strictly_inside_copy_b": copy_b_ok,
        "copy_a_and_copy_b_origins_disjoint_and_distinct_from_parent_root": disjoint_ok,
        "copy_a_and_copy_b_bit_for_bit_numerical_parity": parity_ok,
        "verified_module_relpaths_union": sorted(relpaths_union),
        "symlink_escape_outside_repo_root_rejected": symlink_escape_rejected,
        "passed": ok_33,
    }


def run_nw08_live_dispatch_solver_tests(openislet_root: Path) -> Dict[str, Any]:
    """
    B7 / R03-B2 live verification tests observing actual 3D FVM execution through
    `dispatch_and_solve_nw08_measured_ocr(...)` across R3P, R2, and R1 routes, including
    boundary-pressure override consistency, native unrounded float64 baselines, and
    concurrent in-process state isolation!
    """
    from concurrent.futures import ThreadPoolExecutor

    tests: List[Dict[str, Any]] = []
    _known_sig = {
        "ocr_uncertainty_model": NW08_OCR_UNCERTAINTY_MODEL_KNOWN_SIGMA,
        "ocr_uncertainty_provenance": "live_dispatch_calibrated_known_sigma_reference_v1",
    }

    # 1. R3P_30 triggered by measured OCR + SE -> actually solves 3D FVM and FAILS < 15.0 mmHg
    d_r3p30 = dispatch_and_solve_nw08_measured_ocr("R3P_30", 1.25, 0.04, openislet_root=openislet_root, **_known_sig)
    ok_1 = bool(
        d_r3p30["trigger_dispatched"] is True
        and d_r3p30["solver_executed"] is True
        and d_r3p30["baseline_source"] == "native_3d_fvm_unrounded_float64"
        and abs(d_r3p30["baseline_min_face_unrounded_mmhg"] - 15.2152) > 1e-6
        and d_r3p30["solver_p_bed_mmhg_verified"] == 30.0
        and isinstance(d_r3p30["face_min_mmhg_unrounded"], float)
        and d_r3p30["face_min_mmhg_unrounded"] < 15.0
        and d_r3p30["passes_15mmhg_floor_unrounded"] is False
        and d_r3p30["adjudicated_verdict"] == "FAIL"
    )
    tests.append({
        "test": "live_dispatch_r3p_bed30_executes_3d_fvm_and_fails_below_15mmhg",
        "solver_executed": d_r3p30["solver_executed"],
        "baseline_source": d_r3p30["baseline_source"],
        "solver_p_bed_mmhg_verified": d_r3p30["solver_p_bed_mmhg_verified"],
        "solved_at_vascular_ocr_u95": d_r3p30["solved_at_vascular_ocr_u95"],
        "face_min_mmhg_unrounded": d_r3p30["face_min_mmhg_unrounded"],
        "adjudicated_verdict": d_r3p30["adjudicated_verdict"],
        "passed": ok_1,
    })

    # 2. R3P_35_VPIO120 triggered by linear screen, but live 3D FVM resolves PASS >= 15.0 mmHg (reversal!)
    d_vpio = dispatch_and_solve_nw08_measured_ocr("R3P_35_VPIO120", 1.28, 0.022, openislet_root=openislet_root, **_known_sig)
    ok_2 = bool(
        d_vpio["trigger_dispatched"] is True
        and d_vpio["solver_executed"] is True
        and d_vpio["baseline_source"] == "native_3d_fvm_unrounded_float64"
        and d_vpio["solver_p_bed_mmhg_verified"] == 35.0
        and isinstance(d_vpio["face_min_mmhg_unrounded"], float)
        and d_vpio["face_min_mmhg_unrounded"] >= 15.0
        and d_vpio["passes_15mmhg_floor_unrounded"] is True
        and d_vpio["adjudicated_verdict"] == "PASS"
    )
    tests.append({
        "test": "live_dispatch_r3p_vpio120_linear_triggers_3d_fvm_resolves_pass",
        "solver_executed": d_vpio["solver_executed"],
        "solved_at_vascular_ocr_u95": d_vpio["solved_at_vascular_ocr_u95"],
        "face_min_mmhg_unrounded": d_vpio["face_min_mmhg_unrounded"],
        "adjudicated_verdict": d_vpio["adjudicated_verdict"],
        "passed": ok_2,
    })

    # 3. R2_DZ40 forced live 3D FVM solve at elevated OCR + SE -> PASS > 23 mmHg
    d_r2 = dispatch_and_solve_nw08_measured_ocr("R2_DZ40", 1.25, 0.04, openislet_root=openislet_root, always_solve_3d=True, **_known_sig)
    ok_3 = bool(
        d_r2["solver_executed"] is True
        and d_r2["baseline_source"] == "native_3d_fvm_unrounded_float64"
        and d_r2["solver_p_bed_mmhg_verified"] == 40.0
        and isinstance(d_r2["face_min_mmhg_unrounded"], float)
        and d_r2["face_min_mmhg_unrounded"] > 23.0
        and d_r2["adjudicated_verdict"] == "PASS"
    )
    tests.append({
        "test": "live_dispatch_r2_designz_executes_3d_fvm_and_passes",
        "solver_executed": d_r2["solver_executed"],
        "solved_at_vascular_ocr_u95": d_r2["solved_at_vascular_ocr_u95"],
        "face_min_mmhg_unrounded": d_r2["face_min_mmhg_unrounded"],
        "adjudicated_verdict": d_r2["adjudicated_verdict"],
        "passed": ok_3,
    })

    # 4. R1_350_stress triggered by measured OCR + SE -> executes live Route-1 3D FVM and FAILS < 15.0 mmHg
    d_r1 = dispatch_and_solve_nw08_measured_ocr("R1_350_stress", 1.26, 0.034, openislet_root=openislet_root, **_known_sig)
    ok_4 = bool(
        d_r1["trigger_dispatched"] is True
        and d_r1["solver_executed"] is True
        and d_r1["baseline_source"] == "native_3d_fvm_unrounded_float64"
        and d_r1["solver_p_bed_mmhg_verified"] == 35.0
        and isinstance(d_r1["face_min_mmhg_unrounded"], float)
        and d_r1["face_min_mmhg_unrounded"] < 15.0
        and d_r1["adjudicated_verdict"] == "FAIL"
    )
    tests.append({
        "test": "live_dispatch_r1_350_stress_executes_3d_fvm_and_fails_below_15mmhg",
        "solver_executed": d_r1["solver_executed"],
        "solved_at_vascular_ocr_u95": d_r1["solved_at_vascular_ocr_u95"],
        "face_min_mmhg_unrounded": d_r1["face_min_mmhg_unrounded"],
        "adjudicated_verdict": d_r1["adjudicated_verdict"],
        "passed": ok_4,
    })

    # 5. R03-B2: Lowered boundary pressure override on R3P_35 (p_bed_mmhg=30.0) carries through to BOTH
    #    native baseline screen AND live 3D FVM solver, matching R3P_30 to < 1e-12 mmHg and flipping PASS -> FAIL!
    d_r3p35_nom = dispatch_and_solve_nw08_measured_ocr("R3P_35", 1.25, 0.04, openislet_root=openislet_root, always_solve_3d=True, **_known_sig)
    d_r3p35_ov30 = dispatch_and_solve_nw08_measured_ocr("R3P_35", 1.25, 0.04, openislet_root=openislet_root, p_bed_mmhg=30.0, **_known_sig)
    ok_5 = bool(
        d_r3p35_nom["adjudicated_verdict"] == "PASS"
        and d_r3p35_nom["face_min_mmhg_unrounded"] > 19.5
        and d_r3p35_ov30["p_bed_override_applied"] is True
        and d_r3p35_ov30["effective_p_bed_mmhg"] == 30.0
        and d_r3p35_ov30["solver_p_bed_mmhg_verified"] == 30.0
        and d_r3p35_ov30["baseline_source"] == "native_3d_fvm_unrounded_float64"
        and d_r3p35_ov30["trigger_dispatched"] is True
        and d_r3p35_ov30["solver_executed"] is True
        and d_r3p35_ov30["adjudicated_verdict"] == "FAIL"
        and abs(d_r3p35_ov30["baseline_min_face_unrounded_mmhg"] - d_r3p30["baseline_min_face_unrounded_mmhg"]) < 1e-12
        and abs(d_r3p35_ov30["face_min_mmhg_unrounded"] - d_r3p30["face_min_mmhg_unrounded"]) < 1e-12
        and (d_r3p35_nom["face_min_mmhg_unrounded"] - d_r3p35_ov30["face_min_mmhg_unrounded"]) > 4.5
    )
    tests.append({
        "test": "r03_b2_r3p35_lowered_p_bed_30mmhg_override_matches_r3p30_and_flips_to_fail",
        "nominal_35mmhg_face_min_unrounded": d_r3p35_nom["face_min_mmhg_unrounded"],
        "override_30mmhg_face_min_unrounded": d_r3p35_ov30["face_min_mmhg_unrounded"],
        "solver_p_bed_mmhg_verified": d_r3p35_ov30["solver_p_bed_mmhg_verified"],
        "max_abs_diff_vs_r3p30_mmhg": abs(d_r3p35_ov30["face_min_mmhg_unrounded"] - d_r3p30["face_min_mmhg_unrounded"]),
        "adjudicated_verdict": d_r3p35_ov30["adjudicated_verdict"],
        "passed": ok_5,
    })

    # 6. R03-B2: Lowered boundary pressure override on R2_DZ40 (p_bed_mmhg=30.0) drops face_min_mmhg < 15.0 (FAIL)
    #    AND passing a stale 40-mmHg baseline with a 30-mmHg override raises ValueError fail-closed!
    d_r2_ov30 = dispatch_and_solve_nw08_measured_ocr("R2_DZ40", 1.25, 0.04, openislet_root=openislet_root, p_bed_mmhg=30.0, **_known_sig)
    stale_base_rejected = False
    try:
        dispatch_and_solve_nw08_measured_ocr(
            "R2_DZ40", 1.25, 0.04, openislet_root=openislet_root, p_bed_mmhg=30.0, baseline_min_face_mmhg=24.0468, **_known_sig
        )
    except ValueError:
        stale_base_rejected = True
    ok_6 = bool(
        d_r2_ov30["p_bed_override_applied"] is True
        and d_r2_ov30["solver_p_bed_mmhg_verified"] == 30.0
        and d_r2_ov30["trigger_dispatched"] is True
        and d_r2_ov30["solver_executed"] is True
        and d_r2_ov30["face_min_mmhg_unrounded"] < 15.0
        and d_r2_ov30["adjudicated_verdict"] == "FAIL"
        and (d_r2["face_min_mmhg_unrounded"] - d_r2_ov30["face_min_mmhg_unrounded"]) > 9.0