# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-an-a2a-v1-0-delegated-task-when-the-peer-s-signed-capability-manifest-is-cryptographically-valid-but-sem.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-an-a2a-v1-0-delegated-task-when-the-peer-s-signed-capability-manifest-is-cryptographically-valid-but-sem.warrant.json
# Markdown SHA-256:   09c454c6468377f4cef88e5dfcb2ecb7d88babb50c0edadbdafeb95a3244fa0a
# Code AST SHA-256:   fb3440f5c42113418dc63104c42ffc99e6917ae7812c2fca563914726a500ad8
# Verified Classes:   StaleManifestWarrantError, CapabilityManifest, DelegatedTask
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from datetime import datetime, timezone


class StaleManifestWarrantError(RuntimeError):
    """Raised when a signed manifest is cryptographically valid but its
    asserted capability is not live at the moment of privileged dispatch."""


@dataclass
class CapabilityManifest:
    signer_key_id: str
    issued_at: datetime
    expires_at: datetime
    scopes: dict[str, str]
    revalidation: str  # "live" | "none"


@dataclass
class DelegatedTask:
    task_id: str
    tool: str
    scope: str
    irreversible: bool


def _signature_valid(manifest: CapabilityManifest) -> bool:
    # Placeholder: real impl verifies against cached JWKS.
    return bool(manifest.signer_key_id)


def _live_probe(manifest: CapabilityManifest, scope: str) -> bool:
    # Placeholder: real impl calls caller's capabilities/check endpoint.
    return manifest.scopes.get(scope, "").startswith(scope)


def gate_dispatch(manifest: CapabilityManifest, task: DelegatedTask) -> None:
    now = datetime.now(timezone.utc)

    if not _signature_valid(manifest):
        raise StaleManifestWarrantError(
            f"task {task.task_id}: manifest signature invalid"
        )
    if manifest.expires_at <= now:
        raise StaleManifestWarrantError(
            f"task {task.task_id}: manifest expired at {manifest.expires_at.isoformat()}"
        )

    if task.irreversible:
        if manifest.revalidation == "none":
            # Caller explicitly opted out of live revalidation; callee policy
            # forbids accepting that for irreversible tools.
            raise StaleManifestWarrantError(
                f"task {task.task_id}: irreversible tool {task.tool!r} "
                f"under revalidation=none manifest; refusing stale warrant"
            )
        if not _live_probe(manifest, task.scope):
            raise StaleManifestWarrantError(
                f"task {task.task_id}: live probe rejected scope {task.scope!r} "
                f"for tool {task.tool!r}; aborting whole task, not retrying"
            )
    else:
        if task.scope not in manifest.scopes:
            raise StaleManifestWarrantError(
                f"task {task.task_id}: scope {task.scope!r} absent from manifest"
            )
