# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-scoped-forget-call-whose-audit-trail-would-itself-re-identify-the-redacted-span.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-scoped-forget-call-whose-audit-trail-would-itself-re-identify-the-redacted-span.warrant.json
# Markdown SHA-256:   13b1a0f1bf2716bdd6795df7550c5495d7da241bd7277796cce1725e14cf3a19
# Code AST SHA-256:   a2a07745d00ab662bde5c9d3c6048f64356035cd123f41dd8fb9062ad54434f6
# Verified Classes:   ScopedForgetAuditLeakError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ScopedForgetAuditLeakError(RuntimeError):
    """Raised when a scoped forget's audit record would re-identify the redacted span
    for an audit reader whose scope exceeds the forget's retention scope."""
    pass


DISCLOSURE_THRESHOLD_BITS = 4.0  # policy-defined; below this, record is non-minimal


def _joint_entropy_bits(record: dict) -> float:
    # Placeholder for the real estimator: joint entropy of the record's
    # identifying fields (reason, field-list, interval) under the audit
    # reader's candidate distribution.
    raise NotImplementedError


def _surviving_pointers_into_span(span_id: str, store) -> list:
    # Scan distilled/semantic entries for provenance pointers into span_id.
    return [e for e in store.iter_surviving() if span_id in e.provenance_refs]


def scoped_forget(subject: str, span_id: str, policy, store, ledger) -> None:
    if not policy.audit_scope_strictly_exceeds(policy.retention_scope):
        # Symmetric scopes: audit is not a disclosure channel.
        store.tombstone(span_id)
        ledger.append({"op": "forget", "span": span_id, "scope": policy.retention_scope})
        return

    proposed = build_minimized_audit_record(span_id, policy)

    if _joint_entropy_bits(proposed) < DISCLOSURE_THRESHOLD_BITS:
        raise ScopedForgetAuditLeakError(
            f"forget of span {span_id} under scope {policy.retention_scope} "
            f"would emit an audit record ({proposed}) whose joint entropy "
            f"({_joint_entropy_bits(proposed):.2f} bits) falls below the "
            f"disclosure threshold ({DISCLOSURE_THRESHOLD_BITS} bits) for "
            f"audit scope {policy.audit_scope}. Refusing; escalate to "
            f"authority empowered to relax retention or audit constraint."
        )

    leaked = _surviving_pointers_into_span(span_id, store)
    if leaked:
        raise ScopedForgetAuditLeakError(
            f"forget of span {span_id} incomplete: {len(leaked)} surviving "
            f"entries carry provenance pointers into the tombstoned span, "
            f"re-identifying it independently of the audit record. "
            f"Refusing ledger append; scrub distillation output first."
        )

    store.tombstone(span_id)
    ledger.append({**proposed, "minimized": True})
