Cap with a budget
When. No more than an amount, or a number of lines, may be filed in a period: 30 € of meals a day per person, three requests a day per room, 5,000 € of refunds a month for the station.
Steps
- Write the budget under
policies:, under a name of your choice:budget: {sum | count, per, scope, max}and areason.sum: "$item.price"(or$item.fields.<a number field>): each line counts this value × its quantity.count: true: each line counts 1.per:day,week(from Monday) ormonth(from the 1st), from 00:00 UTC.scope:identity(one signed-in person),roomorstation.
- For
scope: identity, the station must sign people in: declare a provider instation.identity.providers(Sign in with a code). - Cite it where it counts:
{policy: <name>}in a skill'sselect.guards(that skill's lines), or instation.policieswithat: selectorat: submit(every line of the station). - Check the config. At the choice, a line that alone goes beyond what is left is refused; at the submit, the whole submit is held and nothing is sent.
The example
cap-with-a-budget.yaml
# Cap with a budget: a sum or a number of lines, per day, week or month, for one signed-in person,
# one room or the whole station. A budget is a reusable policy (`policies:`) cited by its name; it
# counts what the submits FILE with the tenant. `scope: identity` needs a way to sign in: here a
# one-time code the tenant sends to the e-mail. Replace api.example.com by the API.
station:
id: office-canteen
name: Office Canteen
identity:
providers:
email:
mechanism: otp
verifies: email
type: email
via: tenant
send: sendVerificationCode
check: checkVerificationCode
policies:
- {policy: spend-per-day, at: submit} # every line of the station, judged at the submit
global:
language: en
currency: EUR
locale: en-GB
domain:
domain_type: retail
entity_name: meal
entity_name_plural: meals
voice:
tone: friendly
persona: "the canteen's cook, who knows today's menu"
rules:
- "Never say a meal is ordered before the submit returned it."
apis:
- name: canteen
openapi_url: https://api.example.com/openapi.json
base_url: https://api.example.com
operations:
- {id: listMeals, access: read}
- {id: orderMeals, access: write}
- {id: sendVerificationCode, access: write} # the sign-in's: never granted
- {id: checkVerificationCode, access: write}
auth: {type: none}
profiles:
everyone:
when: "true"
grants: [canteen.listMeals, canteen.orderMeals]
policies:
spend-per-day: # a sum, per signed-in person, per day
budget: {sum: "$item.price", per: day, scope: identity, max: 30}
reason: "30 € of meals a day per person, at most"
meals-per-room: # a number of lines, per room, per day
budget: {count: true, per: day, scope: room, max: 3}
reason: "three meals a day, at most"
skills:
- name: meals
cognitive:
objective: show today's meals, for the person to choose theirs
invoke: {method: listMeals}
kinds: {meal: {}}
present:
fields:
name: {from: name, type: text, role: title}
price: {from: price, type: price, role: billed}
select:
guards:
- {policy: meals-per-room} # this skill's lines only, judged at the choice too
submit:
- name: order
mode: request
tool: orderMeals
args:
body.items: "$selection[kind=meal]"
body.email: "$submit.email"
body.idempotency_key: "$action.key"
line: {meal_id: "$line.ref", quantity: "$line.quantity"}
render: {component: cards}
submit_guard:
form:
email: {type: email, required: true, label: "Your e-mail", identifies: true}
confirm: true
The pitfalls
- A budget counts what is filed, not what is chosen: a choice counts nothing; a submit the tenant refused gives its amount back.
- A budget is never written in a guard list nor in
invoke.guards: it is a policy underpolicies:, cited by its name where it counts. sumis one reference, never a total or an expression:$item.price, which the budget multiplies by each line's quantity.- A policy is a
requireor abudget, never both: write two policies. - A person not signed in who meets an
identitybudget is asked to sign in, and nothing is counted until then. - Try it first with
mode: observe: it refuses nothing, counts nothing, and the journal shows what it would have refused.
What Core says, and the fix
Each sentence below is Core's own, as it answers today (a test keeps them true):
- "a budget is a reusable policy: write it under
policies:" — fix: move it underpolicies:and cite it as{policy: <name>}. - "
scope: identitycounts a signed-in person, and this station has no identity provider" — fix: declare a provider instation.identity.providers, or writescope: room. - "a budget sums each line: write
$item.price, which it multiplies by the line's quantity" — fix:sum: "$item.price", never$selection.total. - "a rule policy is a
requireon one action or abudgetover several — not both" — fix: write two policies. - "Input should be 'day', 'week' or 'month'" — fix:
perisday,weekormonth. - "a budget counts what is filed, and a read files nothing" — fix: cite it in
select.guardsorstation.policies, never ininvoke.guards.