跳转至

mainspring.errors

异常层级与错误分类表(可重试 / 可降级)

本页由 scripts/gen_api_docs.py 从模块 __all__ 自动生成,请勿手改。 新增或删除公开符号:先改模块 __all__ 与 tests/test_public_api.py 快照, 再重新生成本页。

异常层级单点收口(§2.11 三轮 / 五轮复审;ADR-012)。

ModelAPIError       模型层可重试 / 不可重试错误,调用方靠 .retryable 与 .error_type 区分
AgentRunError       run 控制流错误(非法 approve/deny 调用、脚本耗尽等),不可重试
ToolRetryError      工具层要求重试(仅幂等工具允许,退避复用 RetryPolicy,T1-5)
SessionBusy         同一 session 并发 run(§2.9 控制流异常,不进可重试层级)
UnsupportedCapability  Profile 缺能力时明确报错,不撞 500(T1-3)
ConfigError          yaml 装载 / 装配错误,带来源、行号与字段路径(T1-11)

消费链路(T2-9):provider 把 HTTP 状态 / 异常映射成 ModelAPIError(error_type 取分类表 标签);RetryPolicy 只重试 retryable=True 且标签在其白名单内的错误;FallbackModel 只对 DEGRADABLE_ERROR_TYPES 内的标签换模型。表外标签 fail-closed:不重试、不降级,直接上抛。

MaxTurnsExceeded 已除名(五轮复审):max_turns 超限是 finish_reason="max_turns" 优雅终态。

DEGRADABLE_ERROR_TYPES module-attribute

DEGRADABLE_ERROR_TYPES = frozenset(
    label
    for label, error_class in ERROR_CLASSIFICATION.items()
    if error_class.degrade
)

ERROR_CLASSIFICATION module-attribute

ERROR_CLASSIFICATION: dict[str, ErrorClass] = {
    "rate_limit": ErrorClass(
        True, True, "429 限流:退避重试,耗尽后换模型"
    ),
    "server_error": ErrorClass(
        True, True, "5xx / 连接层错误:退避重试,耗尽后换模型"
    ),
    "timeout": ErrorClass(
        True, True, "超时:退避重试,耗尽后换模型"
    ),
    "auth_error": ErrorClass(
        False, True, "鉴权失败:重试无意义,换 key 或换厂商"
    ),
    "invalid_response": ErrorClass(
        False, True, "响应不可解析:换一家可能就好了"
    ),
    "unknown": ErrorClass(
        False, True, "未分类:不重试,但值得换厂商试一次"
    ),
    "invalid_request": ErrorClass(
        False, False, "请求本身非法:换厂商也一样错,直接上抛"
    ),
    "content_filter": ErrorClass(
        False, False, "内容被拦截:重试与降级都无意义"
    ),
}

RETRYABLE_ERROR_TYPES module-attribute

RETRYABLE_ERROR_TYPES = frozenset(
    label
    for label, error_class in ERROR_CLASSIFICATION.items()
    if error_class.retryable
)

AgentRunError

Bases: MainspringError

run 层控制流错误(调用方 bug / 状态不合法),不可重试。

ConfigError

Bases: MainspringError

yaml 装载 / 装配错误(T1-11):消息内含来源、行号与字段路径,便于定位。

ErrorClass

Bases: NamedTuple

一个错误标签的两项处置:是否退避重试、是否值得换模型(T2-9 错误分类表)。

retryable instance-attribute

retryable: bool

degrade instance-attribute

degrade: bool

description instance-attribute

description: str

MainspringError

Bases: Exception

框架异常基类,便于调用方一次兜底。

框架所有「可预期异常」的唯一根:except MainspringError 即可与真正的编程 bug (TypeError / KeyError 之类)划清边界——前者应处理或转终态,后者应直接崩。

ModelAPIError

Bases: MainspringError

模型 API 调用错误。

error_type 为稳定标签,取值见上方 ERROR_CLASSIFICATION;retryable 缺省按标签查表 推导,provider 掌握更精确事实时可显式传参覆盖。 provider / model / status_code 不参与任何判定,仅供日志、指标与降级归因读取。

message instance-attribute

message = message

provider instance-attribute

provider = provider

model instance-attribute

model = model

status_code instance-attribute

status_code = status_code

error_type instance-attribute

error_type = error_type

retryable instance-attribute

retryable = (
    error_type in RETRYABLE_ERROR_TYPES
    if retryable is None
    else retryable
)

__init__

__init__(
    message: str,
    *,
    provider: str | None = None,
    model: str | None = None,
    status_code: int | None = None,
    error_type: str = "unknown",
    retryable: bool | None = None,
) -> None

SessionBusy

Bases: MainspringError

同一 session_id 已有活跃 run(挂起视作活跃,§2.9)。

ToolRetryError

Bases: MainspringError

工具要求重试;仅声明幂等的工具允许触发(T1-5),退避复用 RetryPolicy。

UnsupportedCapability

Bases: MainspringError

ModelProfile 缺能力(T1-3):明确报错,不撞 provider 500。