# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-litmus-safety-suite-membership-as-a-poisoned-capability-warrant-rather-than-a-benign-classification-label.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-litmus-safety-suite-membership-as-a-poisoned-capability-warrant-rather-than-a-benign-classification-label.warrant.json
# Markdown SHA-256:   e12607339aad9347ed087b1620836369e3354cbaf90fa6b632a330f4cdd41af4
# Code AST SHA-256:   607faf23b8ee637f96fa7127f0199334011e4d22d61f28b330d87a4c27913992
# Verified Classes:   BenchmarkCapabilityLeak
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class BenchmarkCapabilityLeak(RuntimeError):
    """Raised when a capability grant is traceable to a safety-benchmark corpus."""


BENCHMARK_NAMESPACE_PREFIX = "benchmark."
RUNTIME_NAMESPACE_PREFIX = "runtime."


def authorize_tool_grant(grant: dict, session_id: str) -> dict:
    source = grant.get("source_namespace", "")
    ttl = grant.get("ttl", "unset")
    harm_class = grant.get("harm_class")

    # Benchmark metadata may never participate in an authorization decision.
    if source.startswith(BENCHMARK_NAMESPACE_PREFIX):
        raise BenchmarkCapabilityLeak(
            f"grant for tool={grant.get('tool')!r} in session={session_id!r} "
            f"originates from benchmark namespace {source!r}; "
            f"refusing to bind adversarial manifest to runtime capability"
        )

    # A null TTL copied from an adversarial item must not survive as unbounded.
    if ttl is None and harm_class is not None:
        raise BenchmarkCapabilityLeak(
            f"grant for tool={grant.get('tool')!r} carries null TTL from "
            f"harm_class={harm_class!r}; unbounded eval TTL cannot become "
            f"unbounded runtime authority"
        )

    if not source.startswith(RUNTIME_NAMESPACE_PREFIX):
        raise BenchmarkCapabilityLeak(
            f"grant for tool={grant.get('tool')!r} has non-runtime provenance "
            f"{source!r}; authorization edge requires runtime.* namespace"
        )

    # Only here, with runtime provenance and a runtime-computed TTL, is binding safe.
    return {
        "tool": grant["tool"],
        "session_id": session_id,
        "expires_at": grant["expires_at"],  # computed by runtime, never from suite
        "source_namespace": source,
    }
