"""Agent-specific exceptions."""

from pydantic import BaseModel, ConfigDict


class ErrorCause(BaseModel):
    """Structured metadata about the cause of an agent exit error.

    Always includes error_type. May include additional fields like block_type
    for specific error types (e.g., UnsupportedAwaitUserBlockError).
    """

    error_type: str
    model_config = ConfigDict(extra="allow")


class AgentRuntimeError(RuntimeError):
    """Raised when an agent exits with a non-zero exit code.

    Raised by the agent decorator instead of calling sys.exit, so callers
    can catch a normal exception and e.g. call sys.exit(exit_code) in a runner.
    """

    def __init__(
        self,
        message: str,
        *,
        exit_code: int,
        reason: str | None = None,
        agent_name: str | None = None,
        agent_identifier: str,
        error_cause: ErrorCause,
    ) -> None:
        super().__init__(message)
        self.exit_code = exit_code
        self.reason = reason
        self.agent_name = agent_name
        self.agent_identifier = agent_identifier
        self.error_cause = error_cause
