# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-delegated-agent-credential-has-been-silently-orphaned-by-mid-chain-revocation.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-delegated-agent-credential-has-been-silently-orphaned-by-mid-chain-revocation.warrant.json
# Markdown SHA-256:   fdd9bebc3443bca705e782665494f14eacb77a86afe543098695659916da3b3a
# Code AST SHA-256:   a3d050b9ef93f8b28428a51a7903f72d6aceae3966af28f55cf5b2dcafa05ad3
# Verified Classes:   OrphanedCredentialError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class OrphanedCredentialError(RuntimeError):
    """Raised when a delegated credential is locally valid but its authority
    path to the root issuer cannot be confirmed unrevoked."""
    pass


def authorize_delegated_call(cred, root_revocation_view, staleness_bound_s, now):
    # Local cryptographic checks: necessary, never sufficient.
    if not cred.signature_valid():
        raise OrphanedCredentialError("signature invalid")
    if cred.expires_at <= now:
        raise OrphanedCredentialError("credential expired")
    if not cred.scope_covers(cred.requested_scope):
        raise OrphanedCredentialError("scope insufficient")

    # Path predicate: can we prove every ancestor link is live?
    if cred.parent_revocation_handle is not None:
        epoch = root_revocation_view.resolve(cred.parent_revocation_handle)
        if epoch is None or epoch >= cred.parent_issued_epoch:
            # Parent revoked at or after our issuance: we are orphaned.
            raise OrphanedCredentialError(
                f"ancestor link {cred.parent_cred_hash} revoked; leaf authority severed"
            )
        return True

    # No resolvable handle: fall back to staleness bound.
    age = now - cred.issued_at
    if age >= staleness_bound_s:
        raise OrphanedCredentialError(
            f"credential age {age}s exceeds staleness bound {staleness_bound_s}s "
            "with no resolvable parent-revocation handle; authority indeterminate"
        )
    return True
