> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getlurk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Agent skill: rules

> The agent skill's reference for rules: their shape, what starts them, and how a person takes one live.

A rule is data. **WHEN** something starts it, **IF** conditions hold, at most **LIMIT** times, **DO** these changes.

```json theme={null}
{
  "name": "Morning regular",
  "when": { "event": "checkin.scanned" },
  "if": [{ "audience": "returning-users" }, { "time": { "between": ["07:00", "10:00"] } }],
  "limit": [{ "max": 1, "per": "day", "by": "user" }],
  "do": [{ "unit": "beans", "add": 10 }]
}
```

Create it with `POST /v1/rules` (a draft). Edit with `PATCH /v1/rules/{id}`. A change to the definition makes a new version. A name change alone
does not. A live rule keeps running its live version until a person takes the new one live. Preview with `POST /v1/rules/{id}/preview`. Only a person takes it live.

## What starts a rule

| `when` | Starts | Preview shows |
| - | - | - |
| `{ "event": "order.paid" }` | each time the event arrives | a replay of past events: who would have got what |
| `{ "unit": "stamps", "reaches": 10 }` | when another rule's change takes the balance from below 10 to 10 or more | which rules lead into it, and how many people already hold 10 |
| `{ "unit": "xp", "every": 100 }` | each time a change crosses a multiple of 100 (up to 10 at once) | the same |
| `{ "schedule": { "every": "week", "at": "09:00", "on": "mon" } }` | on the clock, in the project's time zone, for each person | the next five runs and how many people |

* `schedule.every` is `day`, `week` (`on`: `mon` to `sun`) or `month` (`on`: 1 to 28).
* A rule that follows a balance starts only after another rule changed that balance, in a chain of at most three steps; a rule that
  could start itself is refused when saved.
* A scheduled rule has no event, so it cannot read event properties and cannot hold a change.

## Actions (`do`)

* `{ "unit": "beans", "add": 10 }`, `{ "unit": "beans", "remove": 5 }`.
* An amount from the event: `{ "from": "total_cents", "per": 100 }` (one per 100 cents).
* A streak: `{ "unit": "streak", "streak": "extend" }` counts a day; `"reset"` zeroes it for anyone who missed yesterday (run it from a
  daily schedule).
* `hold_over` parks a change above a threshold for a person to give or refuse.
* Changes for some people: `"modify": [{ "if": [{ "audience": "vips" }], "times": 2 }]` (or `bonus`), applied in order after the rule's
  own amount.
* `budget` caps what the rule gives in total over a period.

## Conditions on the event's own properties

* `{ "property": { "name": "channel", "equals": "web" } }` is an exact match (text, number or true/false).
* `{ "property_range": { "name": "messages", "gte": 5 } }` asks for a number at least (`gte`), at most (`lte`) or between (both, ends
  included). A property that is missing or not a number never passes. A scheduled rule has no event, so it can use neither.

## Whose word a rule takes (`source`)

Device sign-in is on in production. A device sends events only for a person signed in through an active issuer, and only event names an owner or admin has allowed in the client events list; those events arrive as `untrusted`.

Every event and receipt knows where it came from: a server key, an agent, a person in Lurk and Lurk itself send `trusted` events; a signed-in device sends `untrusted` events. A rule says which it takes as an `if` clause:

| `if` | Takes |
| - | - |
| `{ "source": "trusted" }` | trusted events only |
| `{ "source": "any" }` | everything, devices too |

* A schedule has no event, so a `source` on it is refused. A rule that follows a balance reads the source of the event that started its chain.
* A preview and a replay judge each past event by the source it arrived with.

## Units

A unit is a thing a person has. Propose `units.create` with required `name` and `group` (`points`, `progress`, `badge` or `perk`), plus optional `key`, `singular`, `shows_as` (`amount`, `thing` or `level`), `color` and `switches`. Its settings bound what any rule can do: `goes_up`, `goes_down`, `cap`,
`min_balance`, `supply` (the most ever given in all), `expires_after_days`. A rule that would break a setting is skipped with a
reason (`supply_gone` when a limited supply has run out). Propose a change to a unit; do not try to work around it.

## Going live

`POST /v1/proposals` (or `lurk.apply`) makes a proposal. You can propose a full setup in one request, such as `templates.install` followed by `rules.publish` for each rule key the template adds, or `units.create` + `rules.create` + `rules.publish`.
The answer has a `url` for an owner or admin with approval rights. For project keys and agent keys:

Proposable: `rules.publish`, `rules.resume`, `rules.pause`, `rules.create`,
`rules.update`, `templates.install`, `templates.remove`, `units.create`, `units.update`, `audiences.create`, `audiences.add_users`, plus `api_keys.create` (for agent keys and signed-in assistants, at most one per proposal, creating a one-time claim rather than a raw secret). The proposed changes wait for approval. A member's assistant can also propose a gift as described in [`references/member-assistant.md`](https://docs.getlurk.com/agent-skill/member-assistant).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.