# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-re-delegate-a-scoped-aaif-credential-whose-attenuation-proof-no-longer-covers-the-transitive-call-gra.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-re-delegate-a-scoped-aaif-credential-whose-attenuation-proof-no-longer-covers-the-transitive-call-gra.warrant.json
# Markdown SHA-256:   1b07b646392b0a6ac12432d2e39e9411b345ba98a18f3a7f792b4ad28095ea1d
# Code AST SHA-256:   52717b5bf88324ae025e7a5fd0013a58b79fb6c030f548b69ee76680d06e07f4
# Verified Classes:   ProofCoverageViolation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ProofCoverageViolation(RuntimeError):
    """Raised when a re-delegated AAIF credential's proof does not dominate
    the child's reachable edge set (Condition 3 failure)."""
    pass


def redelegate(parent_cred, child_scope, child_tool_manifest):
    # Condition 1: scope containment
    if not child_scope.issubset(parent_cred.scope):
        raise ProofCoverageViolation(
            f"child scope {child_scope} not contained in parent {parent_cred.scope}"
        )

    # Condition 2: fresh proof projection, never reuse parent proof
    removed = parent_cred.proof.removed_edges | (parent_cred.scope - child_scope)
    child_proof = parent_cred.proof.project(removed_edges=removed)

    # Condition 3: graph coverage over the child's reachable edges
    reachable = child_tool_manifest.reachable_edges()
    uncovered = reachable - child_scope - child_proof.removed_edges
    if uncovered:
        # The child can reach authority edges that are neither granted nor
        # explicitly denied by the accumulated removed-set. Refuse and escalate.
        raise ProofCoverageViolation(
            f"proof does not cover reachable edges {sorted(uncovered)}; "
            f"refusing re-delegation to avoid silent over-permission"
        )

    return parent_cred.issue_child(scope=child_scope, proof=child_proof)
