# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-delegated-capability-whose-audit-envelope-cannot-survive-a-policy-boundary-crossing.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-delegated-capability-whose-audit-envelope-cannot-survive-a-policy-boundary-crossing.warrant.json
# Markdown SHA-256:   0e06a87246a88d38e0c6962afc0be43d631112559f9814c52d80914de4d4b641
# Code AST SHA-256:   7613830e5757cb7b90d253bae41e028b267ef6a47af9a668abc23722ab80f8e6
# Verified Classes:   EnvelopeFidelityError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class EnvelopeFidelityError(RuntimeError):
    """Raised when a delegated capability's audit envelope cannot carry the
    claims/signatures the delegator's policy bound requires across the
    trust boundary. Must be raised BEFORE any side effect."""
    pass


def accept_delegation(warrant: dict, delegate_outbound_schema: dict,
                      delegate_redaction: set[str]) -> dict:
    """
    warrant: {
        "cap": str,
        "required_claims": list[str],   # claims the delegator's policy bound reads
        "required_sigs": list[str],     # signatures needed to anchor claims
        "envelope_schema_version": str,
    }
    delegate_outbound_schema: {"version": str, "fields": set[str]}
    delegate_redaction: set of field names the delegate's audit layer strips
    """
    if delegate_outbound_schema["version"] != warrant["envelope_schema_version"]:
        raise EnvelopeFidelityError(
            f"schema version drift: warrant wants "
            f"{warrant['envelope_schema_version']}, delegate emits "
            f"{delegate_outbound_schema['version']}"
        )

    survivable = delegate_outbound_schema["fields"] - delegate_redaction
    needed = set(warrant["required_claims"]) | set(warrant["required_sigs"])
    missing = needed - survivable

    if missing:
        # Refuse at acceptance; do NOT execute and return a lossy envelope.
        raise EnvelopeFidelityError(
            f"capability {warrant['cap']!r} refused: envelope cannot carry "
            f"{sorted(missing)} across trust boundary; policy bound on "
            f"{warrant['required_claims']} would be unverifiable"
        )

    return {"accepted": True, "cap": warrant["cap"],
            "anchored_claims": sorted(needed)}
