Skip to main content

Reuse a policy

When. The same condition holds in several skills, or for the whole station: nothing above 500 €, nothing out of stock. Or a new rule must be tried before it refuses anyone.

Steps​

  1. Write the policy under policies:, its name as its key: require (the condition), reason, and if needed when (the lines it judges) and mode.
  2. Cite it in a skill: {policy: <name>} in select.guards (or kinds.<k>.select.guards, or invoke.guards for a read). It judges that skill's items.
  3. Or for the whole station: station.policies: [{policy: <name>, at: select | submit}]. At select, every item chosen in every skill; at submit, every line the submit would send, before the person's confirmation.
  4. To try it first, mode: observe (it refuses nothing; the journal says what it would have refused) or mode: suggest (its reason is given as advice). Then enforce (the default).
  5. Check the config.

The example​

reuse-a-policy.yaml
# Reuse a policy: a condition written once under `policies:`, cited by its name in a skill's guards
# (`{policy: <name>}`) and for the whole station (`station.policies`, at the choice or at the
# submit). A policy can be tried first: `mode: observe` refuses nothing and writes what it would
# have refused to the journal. Replace api.example.com by the API.
station:
id: wine-cellar
name: Wine Cellar
policies:
- {policy: under-500, at: submit} # every line the submit would send, before the yes

global:
language: en
currency: EUR
locale: en-GB

domain:
domain_type: retail
entity_name: wine
entity_name_plural: wines

voice:
tone: friendly
persona: "a cellar master who knows every bottle"
rules:
- "Never say an order is placed before the submit returned it."

apis:
- name: cellar
openapi_url: https://api.example.com/openapi.json
base_url: https://api.example.com
operations:
- {id: listWines, access: read}
- {id: listGifts, access: read}
- {id: placeOrder, access: write}
auth: {type: none}

profiles:
everyone:
when: "true"
grants: [cellar.listWines, cellar.listGifts, cellar.placeOrder]

policies:
under-500: # its name is its key
require: "$item.price <= 500"
reason: "Above 500 €, a bottle is ordered by phone."
in-stock:
require: "$item.fields.stock > 0"
reason: "This one is out of stock."
vintage-only: # tried before it applies
require: "$item.fields.year <= 2018"
when: "$item.kind == 'wine'" # the lines it judges
reason: "We advise a wine of 2018 or before."
mode: observe # refuses nothing; the journal says what it would have

skills:
- name: wines
cognitive:
objective: show the wines, for the person to choose theirs
invoke: {method: listWines}
kinds: {wine: {}}
present:
fields:
name: {from: name, type: text, role: title}
price: {from: price, type: price, role: billed}
stock: {from: stock, type: integer}
year: {from: year, type: integer}
select:
guards:
- {policy: in-stock} # the same policy, cited in two skills
- {policy: vintage-only}
submit:
- name: order-wines
mode: request
tool: placeOrder
args:
body.items: "$selection[kind=wine]"
body.idempotency_key: "$action.key"
line: {ref: "$line.ref", quantity: "$line.quantity"}
render: {component: cards}

- name: gifts
cognitive:
objective: show the gift boxes, for the person to add one
invoke: {method: listGifts}
kinds: {gift: {}}
present:
fields:
name: {from: name, type: text, role: title}
price: {from: price, type: price, role: billed}
stock: {from: stock, type: integer}
select:
guards:
- {policy: in-stock}
submit:
- name: order-gifts
mode: request
tool: placeOrder
args:
body.items: "$selection[kind=gift]"
body.idempotency_key: "$action.key"
line: {ref: "$line.ref", quantity: "$line.quantity"}
render: {component: cards}

submit_guard:
confirm: true

The pitfalls​

  • A policy reads the item only: $item.fields.<name>, $item.price… never $params, which belong to one skill, nor $identity.
  • A station policy reads what every line has: each field it reads must be declared by every skill with items. Otherwise cite it in the skills that have the field.
  • Layers only tighten: a station policy never opens what a skill refuses.
  • at: submit holds the whole submit on one line: the sentence names the line to take out.
  • A policy nobody cites is refused, and so is a citation of a policy that does not exist.
  • Its name is its key: name: is not written in a policy.

What Core says, and the fix​

Each sentence below is Core's own, as it answers today (a test keeps them true):

  • "is cited by nothing — cite it" — fix: cite it with {policy: <name>}, or drop it.
  • "which policies does not declare" — fix: cite a name declared under policies:.
  • "'vintage-only' runs on every line, and a line of skills.gifts (kind 'gift') does not have what it reads" — fix: cite it in the skills that present the field, not in station.policies.
  • "a reusable policy is shared by skills, and a parameter belongs to one" — fix: write that require in the skill's own guards.
  • "a station-wide policy is built at select or submit" — fix: at: select or at: submit.

Reference: Reusable policies, station.policies, mode.