LLM 应用与 Agent 的护栏流水线。当模型被骗时——它一定会被骗——重要的东西不能坏。
数据访问守卫与 LLM I/O 守卫,装进同一条 in-process 生命周期钩子流水线。
enforce / observe / off。新规则先影子运行,对账审计流之后再切执法——上线不是赌博。
规则自己挂了 ≠ 内容违规。per-rule failMode: open | closed,末道闸类规则挂了也不放行。
私有数据 + 不可信内容 + 对外通信,三要素齐备即升级人工。交给审批人的是原始底层调用,不是模型写的摘要。
防篡改哈希链审计:离线校验、断电残行截断、跨重启续链。WebCrypto 实现,edge 也能签验。
只考核拦截率,护栏会被调成拦一切。内置版本戳数据集与参考守卫,CI 直接钉住指标数值。
零依赖结构化类型:不引入 peer 依赖,一行接入 wrapLanguageModel 或 inputProcessors。
import { createGuard, lens } from '@yiong/railguard'
import { faithfulness, injection, inputHygiene, linkPolicy } from '@yiong/railguard/rules'
import { consoleSink } from '@yiong/railguard/audit'
const guard = createGuard({
audit: consoleSink(),
hooks: {
onInput: [inputHygiene(), injection({ mode: 'block' })],
onOutput: [
faithfulness({ resolve: (c) => corpus.slice(c), quoteOf: (c) => c.quote }),
lens(linkPolicy({ allow: ['https://docs.example.com/'] }), (p) => p.answer, (p, v) => ({ ...p, answer: v })),
],
},
})
const input = await guard.run('onInput', userQuestion, guard.context())
if (!input.ok) return refuse(input.blocked?.reason)设计哲学(OWASP GenAI 2026):概率层(注入启发式)单独 enforce 不构成安全边界—— 真正的边界是输出侧核验与确定性规则。这是本包的文档承诺,不是免责声明。