OpenIslet

scripts/wetlab_contract.py

One measurement contract for Python ingestion and generated MCP metadata.

script Release v0.8.0 gen-eb955f8266035aba 913 KB · 68 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/wetlab_contract.py · SHA-256: 6bb3955530f8f05d…

            'readiness_tier': str(s11_eval['status']),
            'overall_status': str(s11_eval['status']),
            'hard_stop_precedence_winner': str(s11_eval['status']),
            'execution_supported': False,
            'holds': list(s11_eval.get('reasons', [s11_eval['status']])) + holds,
            'invalidated_stale_keys': sorted(invalidated_stale_keys),
            'retained_reset_audit_log': retained_reset_audit_log,
            **_t6b_meta,
        }

    if early_device_or_route_hold is not None:
        return {
            'readiness_tier': early_device_or_route_hold[0],
            'overall_status': early_device_or_route_hold[0],
            'hard_stop_precedence_winner': None,
            'execution_supported': False,
            'holds': [early_device_or_route_hold[1]] + [h for h in holds if h != f'{early_device_or_route_hold[0]}:{early_device_or_route_hold[1]}'],
            'invalidated_stale_keys': sorted(invalidated_stale_keys),
            'retained_reset_audit_log': retained_reset_audit_log,
            **_t6b_meta,
        }

    # PRECEDENCE 4: Malformed batch or non-AtomicObservation holds (only when no S9/Tier 6B/S11 hard stop latched):
    if malformed_batch_holds:
        return {
            'readiness_tier': malformed_batch_holds[0],
            'overall_status': malformed_batch_holds[0],
            'hard_stop_precedence_winner': None,
            'execution_supported': False,
            'holds': malformed_batch_holds + holds,
            'invalidated_stale_keys': sorted(invalidated_stale_keys),
            'retained_reset_audit_log': retained_reset_audit_log,
            **_t6b_meta,
        }

    if 'gate0s_shedding_ddpcr_assay' in dev_store and 'gate0s_shedding_viable_outgrowth_assay' in dev_store:
        if dev_store['gate0s_shedding_ddpcr_assay'].sample_unit_id == dev_store['gate0s_shedding_viable_outgrowth_assay'].sample_unit_id:
            holds.append('HOLD_GATE0S_DUAL_SHEDDING_REQUIRES_INDEPENDENT_SPECIMEN_UNITS')

    # PRECEDENCE 5: Mandatory core S9 safety observations, Rule S11 holds, and mandatory sorted-lot Tier 6B per-fraction thinning
    # (matching evaluate_stage0_product_contract hold ordering identically across all 4 entry paths):
    missing_s9_lot = [
        eid for eid in S9_CORE_LOT_ENDPOINT_IDS
        if eid not in lot_store or lot_store[eid].kind != 'CONTINUOUS_STUDENT_T'
        or atom_status_by_store_key.get(('LOT_SCOPE', eid)) != 'PASS'
    ]
    if missing_s9_lot and 'HOLD_MISSING_CORE_S9_MOLECULAR_OBSERVATIONS' not in holds:
        holds.append('HOLD_MISSING_CORE_S9_MOLECULAR_OBSERVATIONS')
    if s11_eval.get('gate_pass') is not True:
        _s11_h = str(s11_eval['status'])
        if _s11_h not in holds:
            holds.append(_s11_h)
    if tier6b_eval.get('gate_pass') is not True:
        _t6b_h = str(tier6b_eval['status'])
        if _t6b_h not in holds:
            holds.append(_t6b_h)

    missing_mcb = [mk for mk in ENDPOINT_REGISTRY_SPEC['mcb_mandatory_endpoint_ids'] if mk not in mcb_store]
    if holds:
        return {
            'readiness_tier': 'HOLD_FAILED_STAGE0_GATE',
            'overall_status': holds[0],
            'hard_stop_precedence_winner': None,
            'execution_supported': False,
            'holds': holds + missing_mcb + missing_s9_lot,
            'invalidated_stale_keys': sorted(invalidated_stale_keys),
            'retained_reset_audit_log': retained_reset_audit_log,
            **_t6b_meta,
        }
    if missing_mcb or missing_s9_lot or (not lot_store and tier6b_eval.get('gate_pass') is not True) or (specimen_lifecycle_stage != 'AUTHORIZED_TEST_ARTICLE_PRE_IMPLANT' and any(tk not in dev_store for tk in sister_terminal_ids)):
        unresolved_stale_keys = {k for k in invalidated_stale_keys if k not in lot_store and k not in dev_store}
        tier = (
            'HOLD_LOT_OR_DEVICE_SCOPED_EVIDENCE_MISSING_AFTER_PROVENANCE_CHANGE'
            if unresolved_stale_keys
            else 'PROVISIONAL_PARTIAL_LOT_SCREENING_ONLY'
        )
        return {
            'readiness_tier': tier,
            'overall_status': tier,
            'hard_stop_precedence_winner': None,
            'execution_supported': False,
            'holds': missing_mcb or ['incomplete_lot_or_device_panel'],
            'invalidated_stale_keys': sorted(invalidated_stale_keys),
            'transferred_mcb_count': len(mcb_store),
            'retained_reset_audit_log': retained_reset_audit_log,
            **_t6b_meta,
        }
    if is_staged_research_comparator:
        return {
            'readiness_tier': 'STAGE0_RESEARCH_COMPARATOR_PENDING_ALLELE_QC',
            'overall_status': 'RESEARCH_COMPARATOR_STAGE0_PANEL_CLEARED_PENDING_STATE4_PROMOTION',
            'hard_stop_precedence_winner': None,
            'holds': [f'HOLD_NON_EXECUTABLE_STAGE0_RESEARCH_COMPARATOR:{pvid_canon}'],
            'execution_supported': False,
            'transferred_mcb_count': len(mcb_store),
            'retained_reset_audit_log': retained_reset_audit_log,
            **_t6b_meta,
        }
    if is_non_exec_product and not is_z_prod:
        return {
            'readiness_tier': 'RESEARCH_COMPARATOR_UNSUPPORTED_VARIANT_HOLD',
            'overall_status': 'RESEARCH_COMPARATOR_UNSUPPORTED_VARIANT_HOLD',
            'hard_stop_precedence_winner': None,
            'holds': [f'non_executable_product:{pvid_canon}'],
            'execution_supported': False,
            'transferred_mcb_count': len(mcb_store),
            'retained_reset_audit_log': retained_reset_audit_log,
            **_t6b_meta,
        }
    if specimen_lifecycle_stage == 'AUTHORIZED_TEST_ARTICLE_PRE_IMPLANT':
        return {
            'readiness_tier': 'AUTHORIZED_TEST_ARTICLE_ASSEMBLED_PENDING_LONGITUDINAL_READOUT',
            'overall_status': 'AUTHORIZED_TEST_ARTICLE_ASSEMBLED_PENDING_LONGITUDINAL_READOUT',
            'hard_stop_precedence_winner': None,
            'holds': [],
            'execution_supported': not is_non_exec_product,
            'transferred_mcb_count': len(mcb_store),
            'retained_reset_audit_log': retained_reset_audit_log,
            **_t6b_meta,
        }
    if is_z_prod:
        return {
            'readiness_tier': 'WILDCARD_COUPON_QUALIFIED_NON_EXECUTABLE',
            'overall_status': 'WILDCARD_COUPON_QUALIFIED_NON_EXECUTABLE',
            'hard_stop_precedence_winner': None,
            'holds': [],
            'execution_supported': False,
            'wildcard_coupon_qualified': True,
            'transferred_mcb_count': len(mcb_store),
            'retained_reset_audit_log': retained_reset_audit_log,
            **_t6b_meta,
        }
    final_tier = (
        'READY_WITH_RETAINED_PRIOR_S9_INVESTIGATION_HISTORY'
        if retained_reset_audit_log
        else 'READY_FOR_ENCAPSULATION_COMPLETE_STAGE0_EVIDENCE'
    )
    return {
        'readiness_tier': final_tier,
        'overall_status': final_tier,
        'hard_stop_precedence_winner': None,
        'holds': [],
        'execution_supported': True,
        'transferred_mcb_count': len(mcb_store),
        'retained_reset_audit_log': retained_reset_audit_log,
        **_t6b_meta,
    }


def evaluate_coupled_catalase_endotoxin_gate_iip0025(
    *,
    m_total_cat_mg: Any,
    eu_per_mg_mean: Any,
    eu_per_mg_se: Any,
    total_implant_eu_mean: Any,
    total_implant_eu_se: Any,
    replicates_n: Any = 6,
) -> dict[str, Any]:
    """
    Fail-closed unrounded mass-coupled human P04040 catalase endotoxin validator enforcing BOTH
    U95_t(EU/mg) <= min(0.10, 0.50 / m_total_CAT_mg) (rejecting 0.05505 at 9.0830769 mg where
    exact cap is 0.0550474255) AND canonical total implant endotoxin U95_t <= 0.50 EU (IIP-0025; DEC-104).
    """
    if not _is_strict_int_iip0024(replicates_n) or replicates_n < 6:
        return {'gate_pass': False, 'status': 'HOLD_STAGE0_INDEPENDENT_LOT_REPLICATES_BELOW_6_STUDENT_T_MINIMUM'}
    for v_num in (m_total_cat_mg, eu_per_mg_mean, eu_per_mg_se, total_implant_eu_mean, total_implant_eu_se):
        if not _is_strict_finite_number_iip0024(v_num):
            return {'gate_pass': False, 'status': 'HOLD_NONFINITE_CATALASE_ENDOTOXIN_INPUT'}
    m_mg = float(m_total_cat_mg)
    c_mean, c_se = float(eu_per_mg_mean), float(eu_per_mg_se)
    tot_mean, tot_se = float(total_implant_eu_mean), float(total_implant_eu_se)
    if m_mg <= 0.0 or c_mean < 0.0 or c_se <= 0.0 or tot_mean < 0.0 or tot_se <= 0.0:
        return {'gate_pass': False, 'status': 'HOLD_INVALID_CATALASE_MASS_OR_SE_BOUNDS'}
    t95 = student_t_ppf_one_sided_95(float(replicates_n - 1))
    u95_eu_per_mg = c_mean + t95 * c_se
    u95_tot_eu = tot_mean + t95 * tot_se
    exact_cap = min(0.10, 0.50 / m_mg)
    expected_tot = m_mg * c_mean
    if u95_eu_per_mg > exact_cap:
        return {
            'gate_pass': False,
            'status': 'HOLD_P05_MASS_COUPLED_CATALASE_ENDOTOXIN_CAP_EXCEEDED',
            'exact_unrounded_cap_eu_per_mg': exact_cap,
            'u95_eu_per_mg': u95_eu_per_mg,
        }
    if u95_tot_eu > 0.50:
        return {
            'gate_pass': False,
            'status': 'HOLD_P05_TOTAL_IMPLANT_CATALASE_ENDOTOXIN_GT_0P50EU',
            'u95_total_implant_eu': u95_tot_eu,
        }
    if abs(tot_mean - expected_tot) > max(0.015, 0.05 * expected_tot):
        return {
            'gate_pass': False,
            'status': 'HOLD_P05_COUPLED_TOTAL_ENDOTOXIN_MASS_PRODUCT_INCONSISTENT',
            'expected_total_eu': expected_tot,
            'reported_total_eu': tot_mean,
        }
    return {
        'gate_pass': True,
        'status': 'CLEARED_COUPLED_UNROUNDED_CATALASE_ENDOTOXIN_GATE',
        'exact_unrounded_cap_eu_per_mg': exact_cap,
        'u95_eu_per_mg': u95_eu_per_mg,
        'u95_total_implant_eu': u95_tot_eu,
    }


def evaluate_dual_assay_gate0s_shedding_iip0025(
    *,
    ddpcr_k: Any,
    ddpcr_n: Any,
    ddpcr_eta_l95: Any = 1.0,
    ddpcr_specimen_unit_id: str,
    viable_k: Any,
    viable_n: Any,
    viable_eta_l95: Any = 1.0,
    viable_f_clonogenic: Any = 1.0,
    viable_specimen_unit_id: str,
    day90_recovery_mean: Any = None,
    day90_recovery_se: Any = None,
    day90_replicates_n: Any = 6,
    **forbidden_kwargs: Any,
) -> dict[str, Any]:
    """
    Fail-closed Day-90 Gate 0S dual-assay shedding & separated en-bloc recovery validator:
    forbids caller cap overrides (fixed 1e-6), requires distinct specimen units, strict integer
    observed k == 0 with exact independent sensitivity U95 < 1e-6 (N_eff >= 2,995,731), and keeps
    Student-t L95 >= 0.99 physical retrieval distinct from cellular shedding (IIP-0025; DEC-104).
    """
    if forbidden_kwargs:
        return {'gate_pass': False, 'status': 'HOLD_CALLER_CAP_OR_PARAMETER_OVERRIDE_FORBIDDEN'}
    if (
        not isinstance(ddpcr_specimen_unit_id, str)
        or not isinstance(viable_specimen_unit_id, str)
        or not ddpcr_specimen_unit_id.strip()
        or not viable_specimen_unit_id.strip()
        or ddpcr_specimen_unit_id.strip() == viable_specimen_unit_id.strip()
    ):
        return {'gate_pass': False, 'status': 'HOLD_GATE0S_DUAL_SHEDDING_REQUIRES_INDEPENDENT_SPECIMEN_UNITS'}
    dd_res = evaluate_rare_event_count_iip0024(
        gate_id='GATE0S_SHEDDING_DDPCR',
        k=ddpcr_k,
        n=ddpcr_n,
        max_u95_fraction=1e-6,
        require_observed_zero=True,
        eta_spike_recovery_l95=ddpcr_eta_l95,
    )
    if dd_res['status'] != 'PASS':
        return {'gate_pass': False, 'status': dd_res['status'], 'failed_assay': 'ddPCR', 'detail': dd_res}
    vi_res = evaluate_rare_event_count_iip0024(