# 规则与运行方式

```
class Policy:
    on: set[str]          # event types watched
    lane: "gate" | "observer"
    cooldown_s: float     # silence after acting
    name: str             # defaults to the class name

    def evaluate(self, event, state) -> Action | None: ...
    def dedup_key(self, event, state) -> str: ...   # what counts as "the same situation"
```

|  | gate | observer |
| --- | --- | --- |
| 时机 | 事前：只看 `tool.before` | 事后：任意事件 |
| 路径 | 同步，经 `tool.before` gate | 总线订阅者；先折叠状态，再唤醒 `on` 中的规则 |
| 速度 | 要快：不调 LLM，不读网络，不读需要推断的状态 | 可以慢，但绝不能拖慢 Agent |
| 返回 | `Gate.allow()`、`Gate.deny(reason)`、`Gate.ask(reason)` | `Notify`、`Inject`、`Prepare` |
| 多条规则命中 | 取最严：deny、ask、allow | 各自出手 |
| 子 Agent | 覆盖；gate 在审批包装之外，`bypass` 跳不过 | 同一事件流覆盖 |

两条车道：`cooldown_s` 按 `dedup_key` 用事件时钟检查。`evaluate` 抛错时记录，并按不出手处理。

## 动作

| 动作 | 设计 | 当前 |
| --- | --- | --- |
| `Gate.deny` | 拦截，理由给模型 | 与设计一致 |
| `Gate.ask` | 经审批流程询问用户 | 按 deny 拦截，措辞提示手动放行后重试 |
| `Notify` | 不打断的提醒 | `proactive.notify` WebSocket 帧 |
| `Inject` | 模型下一轮前的提示，不打扰用户 | 只记录 `proactive.inject` 事件；转向尚未接通 |
| `Prepare` | 只读后台任务先核实，再决定是否 Notify | 只记录 `proactive.prepare_requested`；没有执行者 |

## 新增规则

1. 在 `openprogram/runtime/proactive/policies/` 写一个类，定义 `on`、`lane`、`cooldown_s`。
2. 写 `evaluate`。
3. `register_policy` 注册（同名重复注册被忽略）。

引擎、事件流和折叠都不用动。打扰预算和自动熔断不在范围内，`cooldown_s` 是唯一的旋钮。gate 机制见[事件层](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/event-layer.zh.html)。

## 实现状态

引擎、注册表和三条规则都已存在并有测试。worker 启动时没有调用 `install_proactive()` 和 `install_mvp_policies()`，所以生产环境中没有规则运行。

[三条模板规则](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/policies-mvp.zh.html)
