diff --git a/.changeset/36217-formula-skill-list-equality.md b/.changeset/36217-formula-skill-list-equality.md new file mode 100644 index 00000000000..56c7b83b5df --- /dev/null +++ b/.changeset/36217-formula-skill-list-equality.md @@ -0,0 +1,20 @@ +--- +'@objectstack/skills': patch +--- + +docs(skills): `objectstack-formula` says that `==` / `!=` between a list-valued field and a scalar is refused, and teaches the membership test each site takes + +Clause-②: no +Metadata-protocol: none + +The CEL cheat-sheet of the `objectstack-formula` skill gains a note under its operator table: `==` / `!=` between a list-valued field (`multiselect`, `checkboxes`, `tags`, or a `select` / `radio` / `lookup` / `user` / `file` / `image` field with `multiple: true`) and a scalar literal is refused by the validator as `list-equality` ("list compared with a scalar"), because CEL compares the whole list and the expression answers one constant on every row. The note shows the refused form and the rewrite the refusal prescribes at each site, null-guarded (`in` faults on a `null` list) and parenthesised whole, for `==` and for `!=`: + +- where the expression is evaluated as CEL: `(has(record.tags) && record.tags != null && 'vip' in record.tags)` and `(!has(record.tags) || record.tags == null || !('vip' in record.tags))`; the `has()` covers a sparse action row and a `previous` that is `null`; +- a bare name in a flow condition: `(tags != null && 'vip' in tags)` and `(tags == null || !('vip' in tags))`, since `has()` takes no bare name; +- a sharing rule's `condition`, which is compiled to a query filter: `(record.tags != null && record.tags.contains('vip'))` and `(record.tags == null || !record.tags.contains('vip'))`, since `has()` and an `in` with the literal first do not lower there. + +This is the behaviour `@objectstack/formula` already ships; the skill only teaches it. + +Paid for by deletion in the same file, with the token ceiling unchanged (6008): twelve restatements were cut, each keeping another home — the `has()` section's opening sentence (the cheat-sheet `Key existence (NOT null-safety)` row and the paragraph that follows it), the Salesforce-flavor block's `os build` / runtime clause (the `Verify your work` section), the sentence saying hook conditions and validation predicates bind the same two roots (the `previous` table under it), the sentence saying record-change flow triggers ride the same dispatch (the same table's `after*` row), the closing "It is a refusal" sentence of the 10 000-row paragraph (that paragraph's own "the platform refuses"), the translation table's `"abc"` row (the cheat-sheet `String literal` row), the stdlib intro's "there is no `src/` to read" clause (the same sentence's "ships `dist` only"), the "use `daysBetween` / two `datetime(…)`" sentence of the date-arithmetic note (the table under it), the sub-day-offset row's "the calendar helpers land on UTC midnight" (the `today()` / `daysFromNow` / `daysAgo` rows), the "`previous.status`, where `previous` is `null`" parenthesis of the unevaluable-condition warning (the `previous` table's insert row), the second transition-condition example (the section's first example, the same shape), and the first half of the translation table's `previous.x` warning (the `OLD.x` and `ISCHANGED(x)` rows above it and the same insert row). + +Two eval cases exercise the note: a hook `condition` over a `tags` field (the CEL spelling) and a sharing rule `condition` over the same field (the `.contains()` spelling). diff --git a/skills/objectstack-formula/SKILL.md b/skills/objectstack-formula/SKILL.md index 5740844212e..a845dd92564 100644 --- a/skills/objectstack-formula/SKILL.md +++ b/skills/objectstack-formula/SKILL.md @@ -82,15 +82,30 @@ transforms — see objectstack-data), not an expression dialect. | Key existence (NOT null-safety) | `has(record.foo)` | | Null check | `record.foo == null` or `isBlank(record.foo)` | +**`==` / `!=` between a list field and a scalar literal is refused** (`list-equality`, "list compared +with a scalar": one constant on every row) — `multiselect` / `checkboxes` / `tags`, or `select` / +`radio` / `lookup` / `user` / `file` / `image` with `multiple: true`. Test membership, null-guarded +(`in` faults on `null`) and parenthesised, per site — the `==` rewrite, then the `!=` one: + +```text +record.tags == 'vip' # REFUSED +# evaluated as CEL; has() covers a sparse action row and a null `previous` +(has(record.tags) && record.tags != null && 'vip' in record.tags) +(!has(record.tags) || record.tags == null || !('vip' in record.tags)) +# bare name in a flow condition (has() takes no bare name) +(tags != null && 'vip' in tags) +(tags == null || !('vip' in tags)) +# SharingRule `condition`: compiled to a query filter, where has() and a literal-first `in` do not lower +(record.tags != null && record.tags.contains('vip')) +(record.tags == null || !record.tags.contains('vip')) +``` + The org context is `{ id, tier }` — there is no `os.org.slug` or `os.org.name`. The evaluator also binds the current user as `current_user` (alias `user`) per ADR-0068 — spec field docs write predicates like `current_user.positions`. ### `has()` is NOT a null check -`has(record.x)` is **true whenever the key exists**, even when its value is -`null`. - Every server-evaluated predicate — validation rules, `requiredWhen`, option `visibleWhen`, `readonlyWhen`, hook `condition`, the flow trigger record — reads a `record` (and, where §5 binds it, a `previous`) that is **total over @@ -140,8 +155,7 @@ has(record.viewer) && has(record.viewer.can_approve) && record.viewer.can_approv ## ObjectStack CEL standard library -Registered automatically by `@objectstack/formula`, which ships `dist` only — -there is no `src/` to read in an installed app. Its exported +Registered automatically by `@objectstack/formula`, which ships `dist` only. Its exported `CEL_STDLIB_FUNCTIONS` is the advertised bare-callable subset, pinned by two tests: every entry resolves at runtime, and this table documents them all. @@ -166,7 +180,7 @@ tests: every entry resolves at runtime, and this table documents them all. > **No date arithmetic.** The build refuses `date + n` and `end - start`: a > date or date-time mixed with a number, or minus another, reads `null`, and -> `today()` / `now()` minus a date field reads `{}`. Use `daysBetween(a, b)` for whole days, two `datetime(…)` for hours (row below). +> `today()` / `now()` minus a date field reads `{}`. | Want | Write — never bare `end - start`, `date + n`, `today() + 30` | |:---|:---| @@ -174,7 +188,7 @@ tests: every entry resolves at runtime, and this table documents them all. | Shift a date | `daysFromNow(n)` / `addDays(d, n)` / `addMonths(d, n)` | | Tenure in years | `daysBetween(record.hire_date, today()) / 365` | | Hours between | `double((datetime(record.end_at) - datetime(record.start_at)).getMinutes()) / 60.0` | -| Sub-day offset | `now() + duration("3h")` — the calendar helpers land on UTC midnight | +| Sub-day offset | `now() + duration("3h")` | **Numbers** @@ -278,8 +292,7 @@ P`!isBlank(record.po_number)`; Conditional requiredness: [objectstack-data](../objectstack-data/SKILL.md#conditional-field-rules). -❌ Salesforce-flavor — **fails CEL compile**: `os build` errors with a -located message, and the flow engine throws if it ever reaches runtime: +❌ Salesforce-flavor — **fails CEL compile**: ```ts "status = 'qualified'" @@ -321,8 +334,7 @@ P`previous.status != 'escalated' && record.status == 'escalated'` `record` is the record's **state**, not this write's diff: stored row ⊕ payload, so `record.status == 'escalated'` is true on *every* update of an already-escalated record. Comparing against `previous` is the only way to say -"just became". Hook `condition`s and validation predicates bind the same two -roots — one scope, one meaning, whichever surface reads it. +"just became". **Where `previous` is bound, and where it is not:** @@ -336,7 +348,7 @@ roots — one scope, one meaning, whichever surface reads it. | `before*` hook `condition` on a predicate (`multi: true`) write | **that row's pre-write row** — `beforeUpdate` / `beforeDelete` dispatch once PER MATCHED ROW too, each on a single-record-shaped context whose `input.id` names the row (ADR-0058 Addendum II, D1/D2); `record` is that row's state, stored ⊕ payload, as on any update. Only the *payload* stays batch-scoped — see below the table | ⚠️ **An unevaluable condition ABORTS the operation.** A column read off -`previous` on an insert (`previous.status`, where `previous` is `null`) — like +`previous` on an insert — like a typo'd key (`record.stauts`), a retired field, or a comparison CEL has no overload for — does **not** degrade to "the hook did not fire": it **fails the write**, with an error naming the hook and the key — `before*` and `after*` @@ -345,19 +357,12 @@ the condition is evaluated before any handler runs). **A transition condition needs no special handling for bulk writes.** Write it once, on a `before*` or an `after*` event, and it means the same thing -whether the write carries an id or a predicate: - - -```ts -import { P } from '@objectstack/spec'; -P`previous.status != 'done' && record.status == 'done'` -``` +whether the write carries an id or a predicate. A predicate (`multi: true`) write is N record changes, so every record-scoped declaration on it is evaluated **per row** — `previous` is that row's own pre-write state, `record` its real state, not the bare payload (ADR-0058, -bulk-write Addenda I and II). Record-change flow triggers ride the same -dispatch. `before*` is no exception to the *condition*; the asymmetry is what a +bulk-write Addenda I and II). `before*` is no exception to the *condition*; the asymmetry is what a `before*` handler WRITES: one `updateMany` takes one SET clause, so every row's dispatch carries the one payload and a rewrite made on any row applies to every matched row (D3). A rewrite *decided* per row must assign the same key set on @@ -369,8 +374,7 @@ batch whole, before any write, when two rows disagree Above 10 000 matched rows the platform refuses a predicate write on an object with per-row hooks in either phase — one ceiling for `before*` and `after*`, checked before the first dispatch (ADR-0058 Addendum II, D6) — rather than fan -out that many handler runs inside one write; paginate the write. It is a -refusal, never a silent downgrade to one hook call. +out that many handler runs inside one write; paginate the write. --- @@ -388,7 +392,6 @@ When migrating Salesforce-flavor metadata, apply these rules in order: | `AND` | `&&` | | `OR` | `\|\|` | | `NOT(x)` | `!x` | -| `"abc"` | `'abc'` | | `IF(c, a, b)` | `c ? a : b` | | `ISBLANK(x)` | `isBlank(record.x)` | | `CONCAT(a, b)` | `coalesce(a, '') + coalesce(b, '')` | @@ -397,9 +400,8 @@ When migrating Salesforce-flavor metadata, apply these rules in order: | `ISCHANGED(x)` | `previous.x != record.x` | | `MONTH_DIFF`, `MID`, `LEFT`, `RIGHT`, `SUBSTITUTE` | _not in stdlib — propose addition_ | -> ⚠️ `OLD.x` and `ISCHANGED(x)` both land on `previous.x`, which is `null` on an -> insert event and **fails the write** there unless guarded by `previous != null -> &&` (§5). +> ⚠️ `previous.x` **fails the write** on an insert unless guarded by +> `previous != null &&` (§5). > ⚠️ **Flow conditions are the exception to row 1.** The engine spreads the > trigger record's fields, nulls included, to top level wherever no flow variable diff --git a/skills/objectstack-formula/evals/cel-predicates-formulas.json b/skills/objectstack-formula/evals/cel-predicates-formulas.json index 704dafc4c22..7f17ee7694a 100644 --- a/skills/objectstack-formula/evals/cel-predicates-formulas.json +++ b/skills/objectstack-formula/evals/cel-predicates-formulas.json @@ -120,6 +120,26 @@ "must_contain": ["isoDate(addDays(record.borrow_date, 7))", "F`"], "must_not_contain": ["timestamp(record.borrow_date)", "{record."] } + }, + { + "id": 13, + "prompt": "On `ticket`, `labels` is a `tags` field. Write the hook `condition` that is true when the ticket carries the `urgent` label.", + "expected_output": "`P`(has(record.labels) && record.labels != null && 'urgent' in record.labels)``. `labels` holds a list, so `record.labels == 'urgent'` compares the whole list with a scalar and is refused (`list-equality`): it answers `false` on every row. A hook `condition` is evaluated as CEL, so membership is `in`, behind a `!= null` guard, because `in` faults on a `null` list.", + "files": [], + "assertions": { + "must_contain": ["record.labels != null", "in record.labels", "P`"], + "must_not_contain": ["P`record.labels == 'urgent'", "{record."] + } + }, + { + "id": 14, + "prompt": "On `ticket`, `labels` is a `tags` field. Write the sharing rule `condition` that matches the tickets carrying the `urgent` label.", + "expected_output": "`P`(record.labels != null && record.labels.contains('urgent'))``. A sharing rule's `condition` is compiled to a query filter, not evaluated as CEL, so membership is `.contains()`: `has()` and an `in` with the literal first do not lower there, and the rule would grant nothing. `record.labels == 'urgent'` compares the whole list with a scalar and is refused (`list-equality`).", + "files": [], + "assertions": { + "must_contain": ["record.labels != null", "record.labels.contains("], + "must_not_contain": ["P`record.labels == 'urgent'", "has(record.labels) &&", "{record."] + } } ] }