# Rules and how they run

```
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 |
| --- | --- | --- |
| Moment | Before: `tool.before` only | After: any event |
| Path | Synchronous, through the `tool.before` gate | Bus subscriber; state is folded first, then rules in `on` wake |
| Speed | Fast: no LLM, no network, no state needing inference | May be slow, must never slow the Agent |
| Returns | `Gate.allow()`, `Gate.deny(reason)`, `Gate.ask(reason)` | `Notify`, `Inject`, `Prepare` |
| Several rules hit | Strictest: deny, then ask, then allow | Each acts |
| Subagents | Covered; the gate is outside the approval wrapper, so `bypass` cannot skip it | Covered by the same stream |

Both lanes: `cooldown_s` is checked against the event clock per `dedup_key`. A raising `evaluate` is logged and treated as no action.

## Actions

| Action | Design | Today |
| --- | --- | --- |
| `Gate.deny` | Blocks; the reason goes to the model | As designed |
| `Gate.ask` | Asks the user through the approval flow | Blocked like deny, with wording asking to allow by hand and retry |
| `Notify` | Reminder that does not interrupt | `proactive.notify` WebSocket frame |
| `Inject` | Hint before the model's next round; the user is not disturbed | Records a `proactive.inject` event only; steering is not connected |
| `Prepare` | Read-only background task checks first, then may Notify | Records `proactive.prepare_requested` only; nothing runs it |

## Add a rule

1. Write a class in `openprogram/runtime/proactive/policies/` with `on`, `lane` and `cooldown_s`.
2. Write `evaluate`.
3. `register_policy` it (same name twice is ignored).

The engine, event stream and fold stay untouched. Interruption budgets and automatic breakers are out of scope; `cooldown_s` is the one knob. Mechanics of the gate: [Event layer](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/event-layer.html).

## Implementation status

The engine, registry and three rules exist and are tested. `install_proactive()` and `install_mvp_policies()` are not called at worker start, so no rule runs in production.

[Three template rules](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/policies-mvp.html)
