# `CMDC.Plugin.Builtin.ContentPolicy`
[🔗](https://github.com/tupleyun/cmdc/blob/v0.5.3/lib/cmdc/plugin/builtin/content_policy.ex#L1)

LLM-as-Judge 内容安全拦截 Plugin。

用低成本 LLM（如 GPT-mini / Gemini Flash）在 `:before_request` 评估用户输入，
拦截越狱 / 有害指令 / 离题等不合规内容。

## 与 cmdc_gateway denylist 的关系

- **cmdc_gateway denylist**：HTTP 层 string 匹配，快速、防显式词
- **ContentPolicy LLM-judge**：Plugin 层语义判断，防多轮越狱 / 绕过 denylist
- 推荐**双层使用**：denylist 拦显式、ContentPolicy 拦语义

## 4 大策略类别

1. **指令颠覆（越狱）**：`"忽略之前的规则"` / 请求暴露内部 prompt
2. **有害内容**：仇恨言论 / 危险活动 / 露骨内容 / 辱骂
3. **离题/越界**：政治 / 宗教 / 体育 / 学术作弊 / 与 Agent 无关的闲聊
4. **品牌/竞争**：诋毁本品牌 / 讨论竞争对手

## 配置

    {ContentPolicy, [
      judge_model: "gemini/gemini-2.0-flash",          # 低成本 judge model
      judge_provider_opts: [temperature: 0.0],          # 推荐 deterministic
      custom_rubric: "...",                             # 可选自定义评估准则
      brand_keywords: ["MyProduct"],                    # 可选品牌词
      competitor_keywords: ["CompetitorX"],             # 可选竞争对手
      fail_open: true                                   # judge 失败时是否放行（默认 true）
    ]}

## 行为

- `:before_request` 取最新 user message → 调 judge → JSON schema 验证
- 不合规 → `:abort` action + emit `:content_policy_violated`
- judge 调用失败 → `fail_open: true` 默认放行（避免 judge service 故障阻塞主流程）

## v0.4.0 实现说明

judge LLM 调用走 `CMDC.Provider`（req_llm 封装），与主 Agent 共享 provider 池。
这意味着 judge model 切换是 provider 级的（不会影响主 model）。

---

*Consult [api-reference.md](api-reference.md) for complete listing*
