跳转至

mainspring.guardrails

输入 / 输出护栏与并行 fail-fast 管道

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

输入 / 输出护栏(T2-3):与主循环并行的 fail-fast 拦截。

Guardrail         护栏基类:name + check(text, ctx) → HookVerdict(只用 allow / deny)
InputGuardrail    输入护栏标记基类:与首步模型调用 asyncio.gather 并行,命中即取消在途调用
OutputGuardrail   输出护栏标记基类:模型产出后、工具执行前校验,失败给出可定位细节
GuardrailViolation 拦截事实(结构化:护栏名 / 阶段 / 原因 / 字段 / 细节)
GuardrailOutcome  一次检查的聚合结果(allowed + violations)
GuardrailPipeline 按阶段分组的并行执行器:全部并发、首个 deny 即取消其余(fail-fast)
PromptInjectionGuardrail 内置提示注入护栏:命中即 deny,与工具结果 untrusted 标记联动

与 hooks 的分工:护栏做内容级检查(输入文本 / 输出文本),Hook 做调用级策略 (pre_tool_use 裁决单次工具调用);工具白名单因此归 hooks.py 的 PermissionHook, 不在此处,审计线也随之分开(见下方「事件分流」)。

三态作用域(§2.2 2026-10-30 复审补):护栏复用 HookVerdict 但只用 allow / deny—— 输入 / 输出侧没有挂起点,HITL 只在工具执行前(require_approval 仅 pre_tool_use 可用)。

事件分流(§2.4):护栏落 guardrail 事件(phase 区分 input / output),工具级策略落 hook 事件 (T2-1 / T2-2),两条审计线不混。

终态映射:护栏拦截 → 运行级 finish_reason="guardrail"(§2.4 词表),与模型自身安全过滤器 (content_filter)同义收口;循环体不需要新分支——拦截结果以 finish_reason="content_filter" 的合成响应返回,现有映射直接落到 guardrail 终态。

Guardrail

Bases: ABC

护栏基类:check 返回 allow / deny(require_approval 在护栏里无意义,见 §2.2)。

name instance-attribute

name: str

check abstractmethod async

check(text: str, ctx: RunContext[Any]) -> HookVerdict

GuardrailOutcome dataclass

一次阶段检查的聚合结果:allowed 为 False 时 violations 非空。

allowed instance-attribute

allowed: bool

violations class-attribute instance-attribute

violations: tuple[GuardrailViolation, ...] = ()

reason property

reason: str

全部违规的单行拼接(分号分隔),用作拦截原因进事件与终态。

first property

first: GuardrailViolation | None

首个违规(并发完成序,不保证声明序),供单条摘要 / 日志取用。

__init__

__init__(
    allowed: bool,
    violations: tuple[GuardrailViolation, ...] = (),
) -> None

GuardrailPipeline dataclass

按阶段分组并行执行护栏:全部并发,首个 deny 立即取消其余(fail-fast)。

空管道一律放行(零行为变化);护栏自身抛异常按 deny 处理——与 Hook 同纪律(§2.2), 护栏崩溃不得让策略静默失效。注意 fail-fast 命中的是「最先完成的那个」,并行下 violations 的顺序是完成序而非声明序。

guardrails class-attribute instance-attribute

guardrails: Sequence[Guardrail] = field(
    default_factory=tuple
)

__init__

__init__(guardrails: Sequence[Guardrail] = tuple()) -> None

__len__

__len__() -> int

check_input async

check_input(
    text: str, ctx: RunContext[Any]
) -> GuardrailOutcome

check_output async

check_output(
    text: str, ctx: RunContext[Any]
) -> GuardrailOutcome

GuardrailViolation

Bases: MainspringError

护栏拦截事实:结构化到字段级,便于定位与审计(不直接抛给模型)。

guardrail_name instance-attribute

guardrail_name = guardrail_name

phase instance-attribute

phase = phase

reason instance-attribute

reason = reason

field_name instance-attribute

field_name = field_name

details instance-attribute

details = details or {}

__init__

__init__(
    guardrail_name: str,
    phase: GuardrailPhase,
    reason: str,
    *,
    field_name: str | None = None,
    details: dict[str, Any] | None = None,
) -> None

describe

describe() -> str

单行描述:护栏名 + 阶段 + 原因 + 字段定位(进事件与模型可见内容)。

InputGuardrail

Bases: Guardrail

输入护栏标记基类:用户输入进入模型前校验。

标记本身不带行为,仅供管道按阶段分组(check 仍须子类实现);与首步模型调用 并行、命中即取消在途请求的编排由 Runner 完成——输入护栏只作用于首步。

OutputGuardrail

Bases: Guardrail

输出护栏标记基类:模型产出进入工具 / 会话前校验。

校验对象是「模型即将说出口的话」;建议在 GuardrailViolation 里带上 field_name 与 details,让拦截原因可定位到具体字段(结构化输出场景尤其有用)。

PromptInjectionGuardrail

Bases: OutputGuardrail

提示注入检测护栏(开箱可抄):模型输出命中越权指令特征即 deny。

v1 用特征串匹配而非模型判定:确定性强、零额外成本、CI 可回归。工具结果的 untrusted 标记(T1-5)是上游第一道标记,本护栏是「模型被注入内容带偏后」的出口闸门。

name class-attribute instance-attribute

name = 'prompt_injection'

DEFAULT_MARKERS class-attribute instance-attribute

DEFAULT_MARKERS: tuple[str, ...] = (
    "ignore previous instructions",
    "ignore all previous",
    "disregard the above",
    "system prompt",
    "reveal your instructions",
    "忽略之前的指令",
    "忽略以上所有",
    "无视前述",
    "输出你的系统提示",
    "泄露提示词",
)

markers instance-attribute

markers: tuple[str, ...] = tuple(
    markers or self.DEFAULT_MARKERS
)

__init__

__init__(
    markers: Sequence[str] | None = None,
    *,
    name: str | None = None,
) -> None

check async

check(text: str, ctx: RunContext[Any]) -> HookVerdict