# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-federated-trust-anchor-has-silently-re-scoped-its-policy-surface-mid-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-federated-trust-anchor-has-silently-re-scoped-its-policy-surface-mid-handoff.warrant.json
# Markdown SHA-256:   3121f5a528014ef364421219ed99a0c0cac6d8cbddd3bd3304a7fd3a284f8c86
# Code AST SHA-256:   069d15ed3634addda36a6d7de54c6606b04bdf74408a02da4b8f30982d5359ec
# Verified Classes:   PolicyRevisionDriftError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class PolicyRevisionDriftError(RuntimeError):
    """Raised when a callee's enforced policy revision diverges from the
    revision negotiated at handoff time, without an intervening renegotiation."""
    pass


def dispatch_delegated_task(handoff, callee_envelope, renegotiated=False):
    negotiated_rev = handoff.policy_revision          # content-addressed hash
    enforced_rev = callee_envelope.policy_revision     # hash callee actually applied

    if negotiated_rev != enforced_rev:
        if renegotiated:
            # Caller explicitly accepted the new surface; record the transition.
            handoff.record_revision_transition(negotiated_rev, enforced_rev)
        else:
            raise PolicyRevisionDriftError(
                f"callee enforced policy revision {enforced_rev!r} but caller "
                f"negotiated {negotiated_rev!r} with no renegotiation event; "
                f"refusing to consume potentially narrowed evidence set"
            )

    if callee_envelope.partial_evidence and not handoff.evidence_gap_allowed:
        raise PolicyRevisionDriftError(
            "callee returned partial evidence under a policy surface that "
            "forbids silent truncation; treat as narrowing and fail closed"
        )

    return callee_envelope.result
