Action conditions

Action conditions are the business rules an action enforces: milk older than 72 hours is never drawn, the person who placed a hold cannot release it, wholesale orders lock at 14:00 the day before delivery. Each criterion is a condition with its own message. They are evaluated live while a form is filled in, and finally at the snapshot the action commits against, for every caller, whether it is a person, a script or an agent

StructureTrees of all, any and none, each group with a message
OperandsThe user, parameters, object properties, the time and functions
FailuresEvery failing criterion is reported, not only the first
EvaluatedLive in the form, final at the commit snapshot

In the platform today

Built
  • Action conditions run in the platform service, written under conditions on an action type in ontology.yaml
  • Each group has a name, a message template and a tree of all, any and none
  • The built operators are is, isNot, exists, notExists, lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqual, includes, notIncludes, isIn and notIn
  • The built operands are parameters and their object properties, currentUser (id, name, groups, accessLabels, attributes.x), now, fixed values, add, sub, addHours, template and a registered function, or one field of its result with property
  • :check and :run report every failing group with its message
  • A run evaluates its conditions at the check and again just before it commits, and fails with CONDITION_FAILED when an earlier run's edits made one false, or with CONCURRENCY_CONFLICT.OBJECT_CHANGED when its objects keep changing while it commits
  • Conditions cannot read the outputs of a writeback call made before the edits
  • currentUser groups, access labels and attributes include what the directory adds, from memberships and identity provider groups pushed over SCIM 2.0
On request
  • The other operators and operands, function criteria as groups, the evaluated detail, stored test cases and the limits

Criteria, permissions and security

Three separate controls decide whether an apply goes through, and each answers a different question. Keeping them separate is what keeps each one simple

ControlQuestionCreamery example
Permission to applyMay this person use this action at allOnly quality and operations can apply release-batch
Row and field access, access labelsMay this person see the objects involvedOnly people with QUALITY see QA test results
Action conditionsIs this apply, with these values, allowed right nowNot by the person who placed the hold, and not while a retest is pending

A person without permission does not see the action. A person with permission whose apply fails a criterion sees the action, sees why it is not allowed, and usually knows what to do next. Use permissions for who, and criteria for when and with what

Structure

An action has a list of criteria groups. Each group has a message and a condition. A condition is either a comparison, or all, any or none over a list of conditions, which can nest. All groups must pass for the apply to proceed, and each failing group contributes its own message

# send-silo-to-cheese: book a cheese make from a silo before its 72 h limit
conditions:
  - name: notEmpty
    message: "{{silo.name}} is empty"
    all:
      - { left: { parameter: silo, property: volume }, op: greaterThan, right: 0 }

  - name: beforeLimit
    message: "{{silo.name}} is past its 72 h limit; the milk can't be used"
    all:
      - { left: { parameter: silo, property: hoursToLimit }, op: greaterThan, right: 0 }

  - name: enoughMilk
    message: "{{silo.name}} only holds {{silo.volume}} L"
    all:
      - { left: { parameter: litres }, op: lessThanOrEqual,
          right: { parameter: silo, property: volume } }

  - name: notCleaning
    message: "{{silo.name}} is being cleaned"
    all:
      - { left: { parameter: silo, property: status }, op: isNot, right: CIP }

# release-batch: one group whose tree passes when either comparison holds
conditions:
  - name: heldOrUngraded
    message: "{{batch.batchNo}} is not on hold"
    any:
      - { left: { parameter: batch, property: hold }, op: is, right: true }
      - { left: { parameter: batch, property: qaStatus }, op: is, right: Pending QA }

Write one group per rule a person would recognize, with a message that says what is wrong and, where it helps, what to do. A single group that combines four rules can only say "not allowed"

Operands

The left and right sides of a comparison draw from a fixed set of operands

OperandSyntaxNotes
User id{ currentUser: id }For agents, the person the agent acts for
User groups{ currentUser: groups }The person's groups in the ontology, plus memberships and identity provider groups from the directory, pushed over SCIM 2.0 or read from their OpenID Connect token
User attributes{ currentUser: attributes.shops }Attributes such as the shops a person covers
User access labels{ currentUser: accessLabels }Access labels the user holds
A parameter{ parameter: litres }Its resolved value after parameter defaults
A property of an object parameter{ parameter: silo, property: volume }Stored or derived properties of the object, at the snapshot
The current time{ now: true }With addHours for a time before or after it
Arithmetic and text{ add: [a, b] }, { sub: [a, b] }, { addHours: [ts, n] }, { template: "…" }Built from other operands
A function{ function: name, args: { … } }The result of a registered function, or one field of it with property
A static value{ value: … } or a bare literalVisible to anyone who can read the definition
No valueexists and notExists operatorsThe only way to test for null

A property operand reaches one hop, from the parameter to its own properties, including derived properties defined on the object type. A rule that needs to look further, such as the reefer runs already booked on a sailing, should use a derived property on the type or a function

Operators

OperatorTrue when
is, isNotEqual, not equal. isNot is true when one side has no value and the other has one, so pair it with a check that the value exists where that matters
isIn, notInThe value is, or is not, one of a list
lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqualNumeric, date and time comparisons. Units must match
includes, notIncludesA list on the left contains, or does not contain, a value
exists, notExistsThe operand is present, or null or empty

On request, more operators, such as pattern matches, time windows and checks over every item in a list

Messages and results

Messages are templates over parameters and object properties, rendered for the caller. A template that names a property the caller cannot see renders it as restricted, so a message never reveals more than the person could read. Every failing group is reported. Here is the check request and response for a cheese make booked from a silo that is being cleaned, which breaks every rule at once

POST /api/v2/actions/send-silo-to-cheese:check
{ "parameters": { "silo": "SIL-4", "litres": 8000, "start": "Tonight 22:00" } }
{
  "result": "INVALID",
  "parameters": { "silo": { "result": "VALID", "required": true },
                  "litres": { "result": "VALID", "required": true },
                  "start": { "result": "VALID", "required": true } },
  "conditions": [
    { "name": "notEmpty", "result": "INVALID", "message": "Silo 4 is empty" },
    { "name": "beforeLimit", "result": "INVALID",
      "message": "Silo 4 is past its 72 h limit; the milk can't be used" },
    { "name": "enoughMilk", "result": "INVALID", "message": "Silo 4 only holds 0.0 L" },
    { "name": "notCleaning", "result": "INVALID", "message": "Silo 4 is being cleaned" }
  ]
}

On request, an evaluated detail on each failing group lists the left value, operator, right value and result of every comparison, for example { "left": 8000, "op": "lessThanOrEqual", "right": 0.0, "result": false }. The detail, showing the values each comparison saw, is returned to callers with edit rights on the action type, and in test runs. Other callers get the messages only

When criteria are evaluated

  1. Before the form opens. Criteria that depend only on the user and on the object the action was opened from are evaluated first. If one fails, the action shows as unavailable, with its message as the reason, before anyone types anything
  2. While the form is filled in. Each change re-runs the criteria that depend on the changed parameter, through the validate endpoint, debounced by 150 ms. Messages appear next to the form as the person works
  3. At submit. The action service evaluates every criterion at the snapshot the apply reads. This evaluation is the one that counts. The earlier ones are a courtesy and can be skipped by any client
  4. Just before the commit. The criteria are evaluated once more just before the edits commit, so another apply that committed in the meantime is seen
  5. On a rerun. If the commit finds that an object a parameter read has changed, the apply reruns from the start, with automatic retries, and the criteria are evaluated again against the new state. On request, this also covers an object a criterion read only through a function
Criteria close the write skew gap

Two people who each pass "at most two reefer runs per sailing" against the same sailing at the same moment cannot both commit: the second apply evaluates the criterion again just before it commits, sees the first one's run, and fails with its message. Every object the parameters name is also version-checked before the commit. On request, every object a criterion reads through a function joins the apply's read set as well

Function criteria

A comparison can use the result of a registered function as an operand, or one field of it with property, for a rule that needs more than the parameters and their properties. On request, a criterion can call a read-only function, and test cases stored with the action type run on every change

Patterns and pitfalls

Four eyes

The analyst who ran a test cannot also enter its result. The test records who ran it, and record-result compares

# record-result
- message: "A result must be entered by someone other than the analyst who ran the test"
  all:
    - { left: { currentUser: name }, op: isNot, right: { parameter: qatest, property: analyst } }
    - { left: { parameter: qatest, property: analyst }, op: exists }

The second comparison matters. Without it, a test with no recorded analyst would pass the first comparison for everyone, which is the opposite of the rule's intent

Cut-off times

Wholesale customers can change tomorrow's order until 14:00 Atlantic time today. Keep the cut-off on the order as a full timestamp, stored or derived, and compare the current time with it

- message: "Orders due {{order.due}} closed at 14:00 the day before"
  all:
    - { left: { parameter: order, property: cutoff }, op: exists }
    - { left: { now: true }, op: lessThan, right: { parameter: order, property: cutoff } }

Tighter rules for agents

On request, a condition can tell whether the caller is a person, a script or an agent, so agents can be held to a tighter rule than people, such as proposing reroutes only for runs that have not left the yard

Pitfalls

Do not negate groups, access labels or attributes

A condition such as "user's site is not the west depot" passes for a user with no site attribute at all, which is usually not what was meant. Write the positive form, "user's site is the plant", or add an exists check beside the negation

  • Static values are readable. Anyone who can read the action type can read the values in its criteria. Do not encode a restricted list, such as confidential supplier names, as static values
  • Time is in UTC. now is the current time in UTC. The island is on Atlantic time, three or four hours behind UTC depending on the season, so store cut-offs as full timestamps rather than times of day
  • Criteria do not guard source data. Values that arrive by sync from the ERP never pass through an action. Rules that must hold for all data belong in value types and data quality checks
  • Do not hide an action with a criterion. If a group should never use an action, take away its permission. A criterion that always fails for them still shows them the action

Limitations

  • A property operand reaches one hop, from the parameter to its own properties. Anything further needs a derived property or a function
  • Only the evaluation at submit counts. Live checks in the form are a courtesy that any client can skip
  • Criteria guard applies only. Values that arrive by sync from a source system never pass through them
  • Negated conditions on groups, access labels or attributes pass for a user who has no value at all, unless an exists check is added
  • Static values in criteria are readable by anyone who can read the action type's definition
  • The values each comparison saw are returned only to callers with edit rights on the action type and in test runs
  • When the service runs as several copies, two copies cannot both pass one condition. A busy commit can be refused with 503 COMMIT_BUSY, which is safe to retry

Related