# Rule-based agents

> A database agent that decides by rules instead of a model: no LLM key, no tokens, no network, and every write journaled and undoable.

Source: https://osevel.com/docs/guides/rule-based-agents/

A rule-based agent decides without a model: its rules say which rows to read, which of them
matter, and what to do for each. It needs no LLM key, uses no tokens, and runs in a sandbox
container with **no network at all** (`--network none`); its only channel is the MCP connection to
osevel on its stdio. It goes through the same rules as any agent: its access level, its
sealed profile and the capabilities granted to it. Every write is journaled and can be undone.

## A rule set

```yaml
version: 1
rules:
  - name: low-stock
    read:
      tool: low_stock_products # a read-only (STABLE) capability granted to the agent
      args: { warehouse: main }
    when: # every condition must hold; leave it out for every row
      - { column: stock, op: '<', value: 10 }
    call:
      tool: flag_order # a capability granted to the agent
      args: { product_id: $row.id, reason: low stock }
    max_calls: 20 # at most this many calls per run (1-100, default 10)
```

- `read.tool` must be a capability granted to the agent and read-only (declared `STABLE` or
  `IMMUTABLE`); `call.tool` must be granted to it. Raw SQL tools (`query`, `execute`, `migrate`)
  and the task tools cannot be named.
- Rows: a capability returning `SETOF jsonb` gives one row per object, its keys as columns; a
  scalar one gives one column named after the capability.
- Conditions: `==`, `!=`, `<`, `<=`, `>`, `>=` (numbers compare as numbers, so Postgres numerics
  that arrive as text work), `contains` (text), `is_null`, `not_null`.
- Arguments: a literal, or `$row.<column>` for the kept row's value. A row without that column
  is counted as failed and skipped.
- At most 50 rules. JSON works too (a `.json` file).

## Commands

```bash
osevel agent add stock-bot --profile sealed --access write --project shop
osevel capability grant stock-bot low_stock_products --project shop
osevel capability grant stock-bot flag_order --project shop
osevel rule set stock-bot rules.yaml --project shop    # asks for the code
osevel rule show stock-bot --project shop
osevel task add stock-bot "Check stock" --project shop
osevel agent run stock-bot --project shop              # or: watch add stock-bot --every 1d --task "Check stock"
osevel rule remove stock-bot --project shop
```

`rule set` checks every tool against the agent's grants before it asks for the confirmation
code, and stores the canonical JSON with its SHA-256 in the journal (`agent_reverse.agent_rules`,
journal v9). Only the approver role (your terminal) writes it, so no agent can change its own
rules. A run reads it back, refuses it if the digest does not match, and hands it to the
container, which validates it again.

## What a run does

1. `next_task`: without an open task, the agent stops.
2. For each rule in order: call the read tool, keep the rows that meet every condition, call the
   rule's tool once per kept row (up to `max_calls`, 16 per round).
3. `complete_task` with a note per rule: rows matched, calls done, calls failed.

A rule whose tool the server does not offer (a grant was revoked since) is skipped and noted.
