OpenIslet

scripts/product_contract.py

Versioned exact-product identifiers shared by the ranking and scenario planes.

script Release v0.8.0 gen-eb955f8266035aba 52 KB · 4 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/product_contract.py · SHA-256: cdea1adb1914ce0e…

        if _rec_sha != _dy2_exp["full_record_sha256"]:
            raise ValueError(f"Invalid or mutated Design Y-FullStack v2 product record {pvid} (full_record_sha256 mismatch; IIP-0034; DEC-113)")
        if (
            not isinstance(_dy2_bind, dict)
            or _dy2_bind.get("proposal_id") != _dy2_exp["proposal_id"]
            or _dy2_bind.get("decision_id") != _dy2_exp["decision_id"]
            or _dy2_bind.get("fasta_path") != str(DESIGN_Y_V2_FASTA_PATH)
            or _dy2_bind.get("fasta_sha256") != DESIGN_Y_V2_FASTA_SHA256
            or _dy2_bind.get("fasta_record_id") != _dy2_exp["fasta_record_id"]
            or _dy2_bind.get("donor_sha256") != _dy2_exp["donor_sha256"]
            or type(_dy2_bind.get("donor_length_bp")) is not int
            or _dy2_bind.get("donor_length_bp") != _dy2_exp["donor_length_bp"]
            or type(_dy2_bind.get("tu_count")) is not int
            or _dy2_bind.get("tu_count") != _dy2_exp["tu_count"]
            or type(_dy2_bind.get("pigk_ectopic_gpi_count")) is not int
            or _dy2_bind.get("pigk_ectopic_gpi_count") != _dy2_exp["pigk_ectopic_gpi_count"]
            or type(_dy2_bind.get("encoded_2a_motifs_6frame")) is not int
            or _dy2_bind.get("encoded_2a_motifs_6frame") != _dy2_exp["encoded_2a_motifs_6frame"]
            or _dy2_bind.get("role") != _dy2_exp["role"]
            or _dy2_bind.get("stage0_promotion_gates") != _dy2_exp["stage0_promotion_gates"]
        ):
            raise ValueError(f"Invalid or missing staged_design_y_fullstack_v2_donor_binding for {pvid} (IIP-0034; DEC-113)")
    elif "staged_design_y_fullstack_v2_donor_binding" in product:
        raise ValueError(f"Non-Design-Y-v2 product {pvid} must not carry staged_design_y_fullstack_v2_donor_binding (IIP-0034; DEC-113)")
    if pvid in STAGED_D1B_AUTO_V2_PRODUCT_IDS:
        _d1b_exp = D1B_AUTO_V2_DONOR_BINDINGS[pvid]
        _d1b_alias = str(_d1b_exp["expected_alias"])
        if _d1b_alias not in aliases:
            raise ValueError(f"Staged Autologous D1b-Auto-C v2 comparator {pvid} must carry pinned donor alias {_d1b_alias!r} (IIP-0035; DEC-114)")
        for _a in aliases:
            if _a in STAGED_D1B_AUTO_V2_PRODUCT_IDS or _a.startswith("ISLET1_D1_") or (_a in STAGED_D1B_AUTO_V2_DONOR_ALIASES and _a != _d1b_alias) or (_a.startswith("D1AUTO_V2_") and _a != _d1b_alias):
                raise ValueError(f"Staged Autologous D1b-Auto-C v2 comparator {pvid} claims reserved identifier/alias {_a!r} (IIP-0035; DEC-114)")
        if product["execution_supported"] is not False:
            raise ValueError(f"Staged Autologous D1b-Auto-C v2 comparator {pvid} must keep execution_supported=False (IIP-0035; DEC-114)")
        if product.get("lifecycle_state") != "STAGE0_RESEARCH_COMPARATOR_PENDING_ALLELE_QC":
            raise ValueError(f"Staged Autologous D1b-Auto-C v2 comparator {pvid} must stay at STAGE0_RESEARCH_COMPARATOR_PENDING_ALLELE_QC (IIP-0035; DEC-114)")
        if product.get("physical_route_id") != "ROUTE_3P_MODULAR_PLANAR_ENVELOPE":
            raise ValueError(f"Staged Autologous D1b-Auto-C v2 comparator {pvid} must declare physical_route_id=ROUTE_3P_MODULAR_PLANAR_ENVELOPE (IIP-0035; DEC-114)")
        _d1b_rec_sha = hashlib.sha256(json.dumps(product, sort_keys=True, ensure_ascii=False, separators=(",", ":")).encode("utf-8")).hexdigest()
        if _d1b_rec_sha != _d1b_exp["full_record_sha256"]:
            raise ValueError(f"Invalid or mutated Autologous D1b-Auto-C v2 product record {pvid} (full_record_sha256 mismatch; IIP-0035; DEC-114)")
        _d1b_bind = product.get("staged_d1b_auto_v2_donor_binding")
        if (
            not isinstance(_d1b_bind, dict)
            or _d1b_bind.get("proposal_id") != _d1b_exp["proposal_id"]
            or _d1b_bind.get("decision_id") != _d1b_exp["decision_id"]
            or _d1b_bind.get("fasta_path") != str(D1B_AUTO_V2_FASTA_PATH)
            or _d1b_bind.get("fasta_sha256") != D1B_AUTO_V2_FASTA_SHA256
            or _d1b_bind.get("fasta_record_id") != _d1b_exp["fasta_record_id"]
            or _d1b_bind.get("donor_sha256") != _d1b_exp["donor_sha256"]
            or type(_d1b_bind.get("donor_length_bp")) is not int
            or _d1b_bind.get("donor_length_bp") != _d1b_exp["donor_length_bp"]
            or type(_d1b_bind.get("nominal_aav6_packaged_bp")) is not int
            or _d1b_bind.get("nominal_aav6_packaged_bp") != _d1b_exp["nominal_aav6_packaged_bp"]
            or type(_d1b_bind.get("tu_count")) is not int
            or _d1b_bind.get("tu_count") != _d1b_exp["tu_count"]
            or type(_d1b_bind.get("pigk_ectopic_gpi_count")) is not int
            or _d1b_bind.get("pigk_ectopic_gpi_count") != _d1b_exp["pigk_ectopic_gpi_count"]
            or type(_d1b_bind.get("encoded_2a_motifs_6frame")) is not int
            or _d1b_bind.get("encoded_2a_motifs_6frame") != _d1b_exp["encoded_2a_motifs_6frame"]
            or _d1b_bind.get("role") != _d1b_exp["role"]
            or _d1b_bind.get("stage0_promotion_gates") != _d1b_exp["stage0_promotion_gates"]
        ):
            raise ValueError(f"Invalid or missing staged_d1b_auto_v2_donor_binding for {pvid} (IIP-0035; DEC-114)")
    else:
        if pvid.startswith("ISLET1_D1_"):
            raise ValueError(f"Unpinned product {pvid!r} may not use reserved ISLET1_D1_ prefix (IIP-0035; DEC-114)")
        if "staged_d1b_auto_v2_donor_binding" in product:
            raise ValueError(f"Non-D1b-Auto-v2 product {pvid} must not carry staged_d1b_auto_v2_donor_binding (IIP-0035; DEC-114)")
        for _a in aliases:
            if _a in STAGED_D1B_AUTO_V2_PRODUCT_IDS or _a in STAGED_D1B_AUTO_V2_DONOR_ALIASES or _a.startswith("ISLET1_D1_") or _a.startswith("D1AUTO_V2_"):
                raise ValueError(f"Non-D1b-Auto-v2 product {pvid} must not claim reserved D1b identifier/alias {_a!r} (IIP-0035; DEC-114)")
    if product["physical_qualification_status"] != "PREREGISTERED_PENDING":
        raise ValueError("This release contains no physically qualified exact product")
    if fam == "B" and product["execution_supported"]:
        raise ValueError("Scenario B research comparators must keep execution_supported=False pending State-4 physical qualification")
    if "locus1_donor_variants_metadata" in product:
        l1_meta = product["locus1_donor_variants_metadata"]
        if not isinstance(l1_meta, dict) or not l1_meta:
            raise ValueError("locus1_donor_variants_metadata must be a non-empty dictionary")
        expected_variants = {
            "canonical_arm1b_3974bp": (
                3974,
                4264,
                "f74728767987c18bdcb8c9d49773ee8dbe10e6006e57aa622638e4fb07062b0b",
            ),
            "embodiment_a_star_4143bp": (
                4143,
                4433,
                "0995f8e35791d219dcc905df4bd50145c9b2a43ddb84efdb502b41dc1f2ae6a8",
            ),
        }
        for v_key, (exp_len, exp_pkg_len, exp_sha) in expected_variants.items():
            if v_key not in l1_meta or not isinstance(l1_meta[v_key], dict):
                raise ValueError(f"Missing required locus1 donor variant metadata: {v_key}")
            v_rec = l1_meta[v_key]
            if (
                type(v_rec.get("donor_length_bp")) is not int
                or v_rec.get("donor_length_bp") != exp_len
                or type(v_rec.get("packaged_aav6_length_bp")) is not int
                or v_rec.get("packaged_aav6_length_bp") != exp_pkg_len
            ):
                raise ValueError(f"Invalid donor/packaged length in {v_key}")
            if "l1_linker_aa" in v_rec and type(v_rec["l1_linker_aa"]) is not int:
                raise ValueError(f"Invalid l1_linker_aa type in {v_key}")
            sha_val = v_rec.get("donor_sha256")
            if not isinstance(sha_val, str) or not re.fullmatch(r"[0-9a-f]{64}", sha_val) or sha_val != exp_sha:
                raise ValueError(f"Invalid or mismatched donor_sha256 in {v_key}: {sha_val}")
    return product


def get_product_definition(
    product_id: str,
    root: Path | str | None = None,
    *,
    require_staged_fastas: bool = True,
) -> dict:
    if not isinstance(product_id, str) or not product_id or product_id != product_id.strip():
        raise ValueError(f"Unknown product variant: {product_id!r}")
    for product in load_product_contracts(root, require_staged_fastas=require_staged_fastas)["products"]:
        if product_id == product["product_variant_id"] or product_id in product.get("aliases", []):
            return deepcopy(product)
    raise ValueError(f"Unknown product variant: {product_id}")


def validate_product_definition_dict(pdef: dict, root: Path | str | None = None) -> dict:
    if not isinstance(pdef, dict):
        raise ValueError("Product definition must be a dictionary")
    validate_product_definition(pdef)
    canonical = get_product_definition(pdef["product_variant_id"], root=root)
    if not _strict_json_equal(pdef, canonical):
        raise ValueError(
            f"Supplied product_def for {pdef['product_variant_id']!r} diverges from authoritative registry definition"
        )
    return deepcopy(canonical)