For a person or a model about to write an app; a model gets the same text from oad guide.

Contents

Rules#

A rule is "when this happens, do that": a trigger in when, an optional condition in if, and steps in do. Use one for what the app does on its own, in answer to a change, an event or time. What a person or an agent asks for is an action, not a rule.

JSON

{
  "allRead": {
    "description": "When every book on the list has been read, let your agents know.",
    "when": { "becomes": "size(state.books) > 0 && state.finished == size(state.books)" },
    "do": [ { "emit": "allRead" } ]
  }
}

description is required. do holds steps, as an action's steps do, and its writes are applied together.

Triggers#

when holds exactly one trigger:

Trigger Fires event holds
{ "becomes": expression } When the expression turns from not true to true. Nothing useful.
{ "changes": path } When the value at a state path changes. previous and current.
{ "event": name } On an event: one the app emits (allRead), a capability event (clock.wake), an action that succeeded (action:addBook), a flow transition (flow:approval). The event's payload.
{ "every": duration } Repeatedly, such as every PT1M, while the app is open. Nothing.
{ "lifecycle": "install" } When the app is installed; also open and close, as a person opens or closes it. Nothing.

An event name must exist: an emitted name needs an emit step somewhere, and a capability event must be one of its contract (event.unknown):

JSON

{ "event": "clock.alarm" }

becomes and if#

becomes fires on the edge, once, when its condition turns true; it does not fire again while the condition stays true, and it does not fire when the app opens and the condition is already true (add "initial": true beside it for that).

Put every part of the condition that the edge depends on inside becomes, not in if, which is tested only when the edge happens. A condition already true at the start never becomes true: the reading list's rule tests size(state.books) > 0 inside becomes, since with no books every book is read.

Following a capability event#

The plant care app schedules a wake for each plant (clock.wakeAt), and a rule follows the wake event. event.key and event.at are its payload:

JSON

{
  "description": "On the morning a plant is due, tell you to water it.",
  "when": { "event": "clock.wake" },
  "do": [
    { "let": "plant", "to": "=state.plants.find(p, p.id == event.key)" },
    {
      "if": "plant != null",
      "then": [
        { "call": "notify.send", "once": "=`water:${event.key}:${event.at}`", "with": { "title": "=`Water ${plant.name}`", "body": "=`${plant.name} needs water today.`", "view": "home" }, "onError": [] }
      ]
    }
  ]
}

A grant must cover the event's scope, as for an operation. once makes sure the reminder is sent once even if the rule runs again for the same wake.

Timers#

An every rule runs only while someone has the app open, once per interval in each open session. Say so with "foreground": true beside every; otherwise the check warns (trigger.noBackground). To run while the app is closed, ask for core:scheduler (see capabilities); to act at one time rather than repeatedly, schedule a wake with wakeAt, as the plant care app does.

A clock for a game. Count time, not firings. A firing that falls due while other work runs is folded into the next one, a host may hold every to a longer interval, and each open window of a person fires the rule once more. So keep the moment of the last step in the state (not in session scope), step when durationBetween from it to env.now reaches the step's length, and keep env.now as the new moment. Let the rule fire a few times a step, such as PT0.05S for steps of a fifth of a second. What should look smooth between two steps is the canvas's motion (see controls), not a faster rule. In a chat's frame a change is drawn soon after it happens, at most about ten times a second, or about once a second in a chat that cannot keep a call open.

A scene within its budget. A canvas's scene is evaluated again on every frame, and like every expression it may use 100,000 cost units: a frame whose scene needs more is not drawn, so an animation stutters, and a canvas whose scene cannot be drawn at all shows a placeholder. A shape costs about 20 to 40 units for a cell placed and colored from its index, so one expression builds a few thousand such shapes at most. What is the same on every frame belongs in a derived state entry, which is evaluated only when what it reads changes and has a budget of its own; the scene adds the shapes that move to it with +. A scene may also hold several expressions, each with its own budget, such as groups whose shapes are an expression each. A board of 3,000 cells is over the budget as one expression, and drawn on every frame as two derived entries or two groups of 1,500. create_app, change_app and look_at_app name each expression that used more than 80 percent of its budget, and say how many frames of an animation could not be drawn.

Every day at a time of day. A wake comes once. To act every day at one local time, the rule that follows it schedules the next, as this last step of remind would. A time the person chooses is kept as text HH:MM (see start); pass it to atTime in place of '09:00':

JSON

{ "call": "clock.wakeAt", "with": { "key": "=event.key", "at": "=atTime(addDays(env.today, 1), '09:00')" }, "onError": [] }

The first wake, when the record is added, is today at that time while it is still to come, else tomorrow. A datetime from atTime compares in time order with env.now, to the second; this is true:

EXPR

atTime('2026-11-24', '09:00', 'UTC') > '2026-11-24T08:59:59.500Z'

So the first wake is:

EXPR

let(at, atTime(env.today, '09:00'), at > env.now ? at : atTime(addDays(env.today, 1), '09:00'))

Who a rule acts as#

A rule acts as the app, on behalf of the person whose work caused it: in a rule, user.kind is app and user.roles is empty, and user.person is that person. So record who did something in the action they ran (user.person.name in askForTimeOff), not in a rule it causes. A rule's writes are made for that person: a rule that one person's change fires does not write another person's user state.

A rule may not run an action that has confirm (action.confirmBypassed): nobody is there to agree.

Next#

  • flows for processes with named stages.
  • actions for the steps in do.
  • capabilities for events, schedules and notifications.