# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-stop-trusting-a-federated-peer-the-delegation-depth-collapse-at-the-trust-bootstrap-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-stop-trusting-a-federated-peer-the-delegation-depth-collapse-at-the-trust-bootstrap-boundary.warrant.json
# Markdown SHA-256:   3fa5b157316f880c25a586c638b0395bb8fa5c77dc4f3eb6bdfd7050b1d427d9
# Code AST SHA-256:   7d446a82f00c19738b5f5a14c6f58d0609907c5d1ff2e8be702746d2b8798365
# Verified Classes:   FederationTrustBoundaryError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class FederationTrustBoundaryError(RuntimeError):
    """Raised when a federated credential cannot be fully bounded by the verifier."""
    pass


def verify_federated_token(
    token_caveats: list[str],
    verifier_predicate_namespaces: set[str],
    pinned_narrowing_namespaces: set[str],
    clock_skew_seconds: float,
    max_clock_skew_seconds: float,
    delegation_depth: int,
    max_delegation_depth: int,
) -> None:
    # 1. Depth bound must be enforced by the verifier, not the minter.
    if delegation_depth > max_delegation_depth:
        raise FederationTrustBoundaryError(
            f"delegation depth {delegation_depth} exceeds pinned bound "
            f"{max_delegation_depth}"
        )

    # 2. Clock skew must be smaller than the smallest time-caveat granularity.
    if clock_skew_seconds > max_clock_skew_seconds:
        raise FederationTrustBoundaryError(
            f"clock skew {clock_skew_seconds}s exceeds pinned bound "
            f"{max_clock_skew_seconds}s; time caveats are unreliable"
        )

    # 3. Every caveat must be evaluable, or explicitly whitelisted as narrowing-only.
    for caveat in token_caveats:
        namespace = caveat.split(":", 1)[0].strip()
        if namespace in verifier_predicate_namespaces:
            continue
        if namespace in pinned_narrowing_namespaces:
            continue
        raise FederationTrustBoundaryError(
            f"cannot evaluate caveat namespace '{namespace}' in caveat "
            f"{caveat!r}; refusing to admit over-broad delegation"
        )


def admit_federated_task(request: dict, federation_config: dict) -> dict:
    verify_federated_token(
        token_caveats=request["caveats"],
        verifier_predicate_namespaces=federation_config["verifier_namespaces"],
        pinned_narrowing_namespaces=federation_config["narrowing_namespaces"],
        clock_skew_seconds=request["observed_clock_skew_s"],
        max_clock_skew_seconds=federation_config["max_clock_skew_s"],
        delegation_depth=request["depth"],
        max_delegation_depth=federation_config["max_depth"],
    )
    return {"admitted": True, "task": request["task"]}
