State#
State is every value the app keeps or works out. Each entry has a name, a type and a description, and is one of three kinds:
| Kind | Written as | Its value | Who writes it |
|---|---|---|---|
| Stored | initial, or no kind key at all |
Starts at initial (or null) and keeps what steps write |
Actions, and screens through bindings |
| Derived | derive: an expression, no = |
Worked out again whenever what it reads changes | Nobody: it is read-only |
| Sourced | source: a capability operation |
What the operation last returned, null before the first read |
Nobody: the host reads it |
The weather example has all three:
JSON
{
"city": { "type": "string", "initial": "Lisbon", "description": "The city whose weather is shown." },
"forecast": {
"type": "core:HttpResponse",
"sensitive": false,
"description": "The weather service's answer for the city.",
"source": {
"call": "weather.get",
"with": { "path": "/v1/forecast", "query": { "city": "=state.city" } },
"refresh": { "every": "PT1H" }
}
},
"found": {
"type": "boolean",
"derive": "state.forecast != null && state.forecast.status == 200",
"description": "Whether the service knew the city."
},
"days": {
"type": "Day[]",
"derive": "state.found ? default(state.forecast.body.days, []) : []",
"description": "The forecast for the next days."
}
}
Expressions read an entry as state.city. When an action sets city, the host reads the forecast again (its with changed), and found and days follow.
Where a value lives: scope#
A stored entry has a scope. Choose it first: it decides who sees the value.
scope |
One value for | Who may write it | Use it for |
|---|---|---|---|
user (the default) |
Each person, on all their devices | That person's actions and screens | A personal list, a person's settings |
shared |
The whole app, everyone using it | Actions only, never a screen directly | A team's records, settings everyone sees |
session |
One visit on one device; gone when it ends | Actions and screens | Something about this visit only |
device |
Each person on each device | Actions and screens | A preference for one device |
conflict says what happens when two people change the value at once, or a device comes back online with changes. lastWrite (the default) keeps the later write. merge with key matches list elements by that field, so two people adding different requests both keep theirs. reject refuses a write based on a value that changed meanwhile (state.conflict). Use merge with "key": "id" for a shared list of records.
Types#
Every entry states its type, a derived one too (schema.invalid without it). A type is a short name, or a JSON Schema object written in place.
| Type | Values |
|---|---|
string, number, integer, boolean |
As in JSON. |
date |
A string YYYY-MM-DD, such as "2026-11-13". |
datetime |
A string such as "2026-11-13T09:00:00Z". |
duration |
An ISO 8601 duration string, such as "P1D" or "PT30M". |
any |
Anything. |
Book |
A type declared under types. Type names start with a capital letter. |
core:HttpResponse |
A type of the core catalog, through its alias. |
Book[], string?, Book[]? |
A list, a value that may be null, both (list mark first). |
A derived entry's value must fit its type: the check reports a derive whose value it knows does not (expression.typeMismatch).
Declare a record's shape under types and refer to it by name. Inside a record type each field is JSON Schema, not a short name: start shows the common fields, among them a date, a field that may be null, a list of dates and a list of records. A derived entry takes a short name, ? when it may be missing and [] for a list:
JSON
{
"items": { "type": "Item[]", "initial": [], "scope": "shared", "conflict": "merge", "key": "id", "description": "The list." },
"bought": { "type": "integer", "derive": "state.items.count(i, i.bought)", "description": "Ticked off." },
"next": { "type": "Item?", "derive": "state.items.find(i, !i.bought)", "description": "First to buy." },
"names": { "type": "string[]", "derive": "state.items.map(i, i.name)", "description": "The names." },
"toBuy": { "type": "Item[]", "derive": "state.items.filter(i, !i.bought)", "description": "Still to buy." }
}
A short name in a record type is schema.invalid:
JSON
{ "type": "date" }
Only these schema keywords are allowed: type, properties, required, additionalProperties, items, minItems, maxItems, uniqueItems, enum, const, oneOf, format (date, date-time, duration, uri), minimum, maximum, minLength, maxLength, pattern, default, description, title and $ref. Anything else is schema.invalid.
Write "additionalProperties": false on record types. The check then knows every field, and a misspelt field in an expression is an error (expression.unknownMember) instead of a null at run time.
A value that does not fit its type is refused when it is written (state.invalidValue). A literal that does not fit is reported by the check (value.typeMismatch).
Stored entries and initial#
initial is the value a stored entry starts with. It is worked out once, when the app is installed, and may read only env: "initial": "=env.today" is the install day, and stays that day. (A view's own state is worked out each time the view opens, and its initial may read more: see views.) Without initial the entry starts as null, so give its type a ?. Keep initial small: a definition holds no one's data, and it may be shared.
Derived entries#
A derived entry is a formula. Use one for anything you can work out from other values, rather than storing a copy that can fall out of step:
derive |
Gives |
|---|---|
state.books.count(b, b.status == 'toRead') |
How many books are not started. |
state.books.filter(b, b.status == 'read').map(b, b.title) |
The titles read. |
size(state.books) == 0 ? 'Empty' : 'Has books' |
Text that depends on the list. |
- Nothing writes a derived entry: a step that tries is
state.readonly. - Derived entries must not read each other in a circle (
state.cycle). - A
derivethat readsuserhas one value for each person:state.requests.filter(r, r.personId == user.person.id)is "my requests". - A
derivethat readsenv.noworenv.todayis worked out again as time passes: while the app is open, the host movesenv.nowon at least once a minute, andenv.todaywhen the day changes. - A
derivethat fails, such as one reading a member ofnull, givesnull. Guard it withdefaultor a comparison withnull.
Sourced entries#
A sourced entry reads an operation of a capability: a web service, a record collection, the directory. The operation must only read (its effect is read), and a grant request must cover it (see capabilities).
callnames it as<alias>.<operation>, such asweather.get.withis the operation's input; expressions in it are followed, and a change reads again.whenis an optional expression: the entry is read only while it holds.refreshsays when to read again:every(a duration) andon(a list ofopen,change, and capability events).onis["open", "change"]when left out.pages(1 to 50) follows the pages of an operation that has them, such ascore:recordslist.
The value is null until the first read succeeds, whatever the type says, and a failed read keeps the last value. Guard what reads it, as found does above.
The body of a core:HttpResponse is any: nothing under it is checked, so a misspelt field is not reported and reads as missing:
JSON
"state.found ? default(state.forecast.body.dayz, []) : []"
To have the fields checked, derive an entry of a named type from the body, as days is a Day[]: what reads state.days is then checked against Day. With "additionalProperties": false on Day, a misspelt field is reported too, but an answer whose days carry any other field no longer fits, and days is null. Keep only the declared fields, and the answer fits when the service adds one:
JSON
"state.found ? default(state.forecast.body.days, []).map(d, {date: d.date, high: d.high, low: d.low, summary: d.summary}) : []"
The body is the service's JSON as it is. A service that answers with a bare list gives a list:
JSON
"state.found ? default(state.forecast.body, []) : []"
sources.<name> tells the screen what is going on:
| Expression | Means |
|---|---|
sources.forecast.status == 'loading' |
A read is under way. status is idle, loading, ready or error. |
sources.forecast.status == 'error' |
The last read failed. |
default(sources.forecast.error.message, '') |
Why it failed, when it did. |
sources.forecast.updatedAt != null |
Whether a read ever succeeded. |
A source on a scope of high sensitivity, such as every core:http read, must say whether its value is sensitive: whether what the app keeps from the answer is private. It changes where the value may go (see below), not the permission the source needs. Without it the check warns:
JSON
{
"type": "core:HttpResponse",
"description": "The weather service's answer for the city.",
"source": { "call": "weather.get", "with": { "path": "/v1/forecast", "query": { "city": "=state.city" } } }
}
Keeping values private#
"sensitive": truekeeps the value out of the activity log and off the agent surface (unless the agent surface names it). A derived entry that reads a sensitive one is sensitive too, unless it says"sensitive": false."internal": truekeeps an entry off the agent surface and out of exports altogether. Use it for helpers that exist only for the screen, such as the screen title in the reading list example.
Next#
expressionsto writederive,whenand the rest.actionsfor the steps that write stored state.viewsfor bindings that write state from a screen.capabilitiesfor what a source can read.