# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-delegated-scope-when-an-aaif-credential-is-re-attested-across-vendors-mid-task.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-lose-delegated-scope-when-an-aaif-credential-is-re-attested-across-vendors-mid-task.warrant.json
# Markdown SHA-256:   d2d18327e46ec39e5cc3412c5c57cc9ebade06c0220922a98b282da23d5c9248
# Code AST SHA-256:   02bae6d8c36e4ed7bddf971e8e1406ce1959d51f44eabe2649a00ee2bdf8479a
# Verified Classes:   DelegatedScopeDriftError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class DelegatedScopeDriftError(RuntimeError):
    """Raised when a cross-vendor re-issuance silently truncates delegated scope
    or rebinds the subject nonce mid-task."""
    pass


def verify_handoff_continuity(
    delegated_claims: dict,
    presented_claims: dict,
    delegated_subject_nonce: str,
) -> None:
    """Enforce the delegated-credential continuity invariant at a handoff boundary.

    delegated_claims: claim set the orchestrator issued to this instance.
    presented_claims: claim set on the credential returned by the peer authority.
    delegated_subject_nonce: instance nonce bound to the delegated credential.
    """
    delegated_scopes = set(delegated_claims.get("scopes", []))
    presented_scopes = set(presented_claims.get("scopes", []))
    presented_nonce = presented_claims.get("subject_nonce")

    missing = delegated_scopes - presented_scopes
    if missing:
        raise DelegatedScopeDriftError(
            f"scope truncation on re-issuance: delegated scopes {sorted(missing)} "
            f"absent from presented credential issued by "
            f"{presented_claims.get('issuer')!r}; refusing to resume task state"
        )

    if presented_nonce != delegated_subject_nonce:
        raise DelegatedScopeDriftError(
            f"subject rebinding detected: delegated nonce "
            f"{delegated_subject_nonce!r} != presented nonce {presented_nonce!r}; "
            f"local task ledger is keyed to the old binding and would replay "
            f"non-idempotent tool calls"
        )

    if presented_claims.get("delegation_depth", 0) < delegated_claims.get("delegation_depth", 0):
        raise DelegatedScopeDriftError(
            f"delegation depth reset: delegated depth "
            f"{delegated_claims.get('delegation_depth')} truncated to "
            f"{presented_claims.get('delegation_depth')}; downstream delegation "
            f"will fail as depth-exhausted"
        )


def resume_after_handoff(
    delegated_claims: dict,
    presented_claims: dict,
    delegated_subject_nonce: str,
    ledger: dict,
) -> None:
    verify_handoff_continuity(delegated_claims, presented_claims, delegated_subject_nonce)
    # Only reached when scope set, subject nonce, and depth are all preserved.
    ledger["subject_nonce"] = presented_claims["subject_nonce"]
    ledger["resumed"] = True
