# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-honor-a-federated-trust-anchor-after-policy-harmonization-converges.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-honor-a-federated-trust-anchor-after-policy-harmonization-converges.warrant.json
# Markdown SHA-256:   93be023611811effbcbd2a0c2e86579e882b011022ba4dfd866b102565029739
# Code AST SHA-256:   2a497879b088b4fbde9c40151a5c5447b5ec690c7faa439e095d903cd82b5d95
# Verified Classes:   UnauthorizedAnchorWideningError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class UnauthorizedAnchorWideningError(RuntimeError):
    """Raised when a converged policy set exceeds an anchor's original signed scope."""
    pass


def verify_converged_policy_against_original_anchors(
    converged_predicates: list[dict],
    original_anchors: dict[str, dict],
    active_anchors: dict[str, dict],
    origin_signed_amendments: list[dict],
) -> None:
    """
    Validate that every converged predicate is entailed by the ORIGINAL signed
    scope of each anchor on the path, honoring only origin-signed amendments.

    Raises UnauthorizedAnchorWideningError if any predicate exceeds original scope
    without a legitimate origin-signed amendment.
    """
    amendment_scopes = {
        (a["anchor_id"], a["predicate"]): a
        for a in origin_signed_amendments
        if a.get("signed_by") == a.get("origin_org")
    }

    for pred in converged_predicates:
        anchor_id = pred["anchor_id"]
        original = original_anchors.get(anchor_id)
        if original is None:
            raise UnauthorizedAnchorWideningError(
                f"converged predicate references unknown anchor {anchor_id!r}; "
                f"cannot validate against original signed scope"
            )

        if _entailed_by(pred, original["scope"]):
            continue

        amendment = amendment_scopes.get((anchor_id, pred["predicate"]))
        if amendment is not None and amendment["timestamp"] < pred["harmonization_start"]:
            continue

        active = active_anchors.get(anchor_id, {})
        raise UnauthorizedAnchorWideningError(
            f"anchor {anchor_id!r} original scope {original['scope']!r} does not "
            f"entail converged predicate {pred['predicate']!r}; "
            f"active anchor scope is {active.get('scope')!r} "
            f"(possible policy-induced widening via re-signing). "
            f"Refusing to honor anchor; halt federation path."
        )


def _entailed_by(predicate: dict, scope: dict) -> bool:
    """Minimal entailment check: predicate capability and classification must be within scope."""
    cap_ok = predicate["capability"] in scope.get("capabilities", [])
    class_ok = predicate["classification"] in scope.get("classifications", [])
    return cap_ok and class_ok
