# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-deletion-attestation-whose-scope-is-a-strict-superset-of-the-retention-scope-it-was-configured-with.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-deletion-attestation-whose-scope-is-a-strict-superset-of-the-retention-scope-it-was-configured-with.warrant.json
# Markdown SHA-256:   01f0c00890f5d5894b7b15e081a6ebf8a3d2bd54166a9757b7138a5d98cc9c03
# Code AST SHA-256:   8c27c7d6dfabafe588018c31869ecb04c2e5bb51d3a9e86fe76e07f207ca1653
# Verified Classes:   ScopeEscalationError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ScopeEscalationError(RuntimeError):
    """Raised when a forget/attest request targets a scope not contained
    in the instance's configured retention scope. Binding attestations
    over supersets are unsound and must be refused, never narrowed."""
    pass


def forget_and_attest(instance, requested_scope, store, binding=True):
    R = instance.configured_retention_scope
    if not is_subset(requested_scope, R):
        if binding:
            # Refuse: the instance cannot enumerate the complement of R,
            # so any attestation over a superset would be a claim about
            # data it never held. Do not delete, do not narrow, do not
            # return a vacuous ok.
            raise ScopeEscalationError(
                f"requested scope {requested_scope!r} is not contained in "
                f"retention scope {R!r}; route superset attestation to the "
                f"coordinator that owns the full shard map"
            )
        # Advisory path: narrowing is permitted only if the narrowed
        # scope is stated explicitly in the response.
        effective = intersect(requested_scope, R)
        deleted = store.delete_prefix(effective)
        return {"ok": True, "attested_scope": effective, "deleted": deleted}

    deleted = store.delete_prefix(requested_scope)
    remaining = store.count(requested_scope)
    if remaining != 0:
        raise ScopeEscalationError(
            f"post-delete residue {remaining} in {requested_scope!r}; "
            f"attestation withheld"
        )
    return {"ok": True, "attested_scope": requested_scope, "deleted": deleted}
