Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .changeset/36167-cel-list-equality-refused.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
'@objectstack/formula': minor
'@objectstack/lint': minor
'@objectstack/mcp': minor
'@objectstack/service-automation': minor
'@objectstack/spec': minor
'@objectstack/scenarios': minor
'@object-ui/app-shell': minor
---

fix(formula)!: `validateExpression` refuses `==` / `!=` between a list-valued field and a scalar, which answers a constant on every row

Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: follow-up #36217

<!-- adr-0087: registered cel-list-equality-refused -->

**BREAKING** accept-set narrowing, shipped as `minor`. CEL compares the whole list with the scalar, so `record.labels == 'frontend'` is `false` for `['frontend', 'qa']` and for `null` alike, and `record.labels != 'frontend'` is always `true`: a condition written that way never fires (or always does), with no fault and no log line. The shared validator accepted it.

- **The refusal.** `validateExpression` refuses a `==` / `!=` between a read of a list-valued field on `record` / `previous` (`record.f`, `record['f']`, either operand order) — or, at a flattened (flow) condition, its bare name `f`, which the run binds to the trigger record's field, unless a flow variable holds the name or a node can bind names no reader sees — and a non-null string, number or boolean literal, as the new `list-equality` code (params `reference`, `operator`, `operand`, `answer`, `rewrite`), prescribing the null-guarded membership test.
- **Which fields are lists.** A `fieldTypes` entry of an inherently multi-value type (`multiselect`, `checkboxes`, `tags`), or a field named in the new optional `ExprSchemaHint.multiValueFields` — the fields a caller reads as multi-valued through `isMultiValueField` (`@objectstack/spec/data`), which is how a `select` / `radio` / `lookup` / `user` / `file` / `image` field declared `multiple: true` becomes a list here. Absent, such a field is not judged, as before.
- **Who fills it.** The publish lint (`os validate` / `os build`, every CEL site that types the object's fields), the MCP `validate_expression` tool (read off the served definition, `McpDataBridge.servedObjectDefinitionOf`, at the validation / formula / flow-condition sites; a bridge without it answers as before), `registerFlow`'s schema pass (`parseObjectFieldSchema` returns `multiValueFields`; logged, never thrown, like every finding of that pass), the scenario scorer, and Studio's object draft check (field rules, formulas, validation rules; `CelSchemaHint.multiValueFields`).
- **Still accepted.** `S == null` / `S != null` (either side), `'a' in S`, a list compared with a list literal or with another list field, and a comparison whose other operand is not a literal.

FROM → TO:

- `record.labels == 'frontend'` → `record.labels != null && 'frontend' in record.labels`
- `record.labels != 'frontend'` → `record.labels == null || !('frontend' in record.labels)`
- at a flow condition, `labels == 'frontend'` → `labels != null && 'frontend' in labels`

One-line fix: test membership with `in`, null-guarded (`'a' in null` faults); compare the list with `null` to test whether it is set.
10 changes: 7 additions & 3 deletions packages/formula/src/cel-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ function comparesDateTextAgainstUndeclared(source: string, env: Environment): bo
// A macro (`list.exists(x, …)`): `rcall` [name, receiver, [variable, …body]].
const args = v.args;
if (v.op === 'rcall' && Array.isArray(args) && args.length === 3 && Array.isArray(args[2])
&& MACRO_NAMES.has(String(args[0]))) {
&& COMPREHENSION_MACRO_NAMES.has(String(args[0]))) {
const [variable, ...body] = args[2] as unknown[];
const name = isCelNode(variable) && variable.op === 'id' && typeof variable.args === 'string' ? variable.args : null;
visit(args[1], bound);
Expand All @@ -1073,8 +1073,12 @@ function comparesDateTextAgainstUndeclared(source: string, env: Environment): bo
return found;
}

/** The CEL macros that bind a variable over a list's items (`list.exists(x, …)`). */
const MACRO_NAMES: ReadonlySet<string> = new Set(['all', 'exists', 'exists_one', 'map', 'filter']);
/**
* The CEL macros that bind a variable over a list's items (`list.exists(x, …)`).
* Package-internal (not re-exported from the entry): `validate.ts`'s
* list-equality pass reads the same set, so a name a macro binds is read alike.
*/
export const COMPREHENSION_MACRO_NAMES: ReadonlySet<string> = new Set(['all', 'exists', 'exists_one', 'map', 'filter']);

/**
* A typed environment for the soundness check. Each field carries a concrete
Expand Down
48 changes: 47 additions & 1 deletion packages/formula/src/expression-refusal-codes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,50 @@ const PINS: { readonly [C in ExpressionRefusalCode]: readonly [Pin<C>, ...Pin<C>
"label() reads a select field's option label; read any other field directly (record.first).",
},
],
// [#36167] A list-valued field compared with a scalar answers a constant.
'list-equality': [
{
produce: () =>
onlyError('predicate', "record.labels == 'frontend'", {
scope: 'record',
fields: ['labels'],
fieldTypes: { labels: 'multiselect' },
}),
params: {
reference: 'record.labels',
operator: '==',
operand: '"frontend"',
answer: 'false',
rewrite: 'record.labels != null && "frontend" in record.labels',
},
message:
'list compared with a scalar — `record.labels` holds a list (a multi-value field), and `==` compares the whole ' +
'list with `"frontend"`, so the expression answers `false` on every row whatever the list holds: the condition ' +
'never changes. Test membership with `in`: write `record.labels != null && "frontend" in record.labels`. ' +
'To test whether the list is set, compare it with `null`.',
},
{
produce: () =>
onlyError('predicate', "'u1' != previous.owners", {
scope: 'record',
fields: ['owners'],
fieldTypes: { owners: 'user' },
multiValueFields: ['owners'],
}),
params: {
reference: 'previous.owners',
operator: '!=',
operand: '"u1"',
answer: 'true',
rewrite: 'previous.owners == null || !("u1" in previous.owners)',
},
message:
'list compared with a scalar — `previous.owners` holds a list (a multi-value field), and `!=` compares the whole ' +
'list with `"u1"`, so the expression answers `true` on every row whatever the list holds: the condition ' +
'never changes. Test membership with `in`: write `previous.owners == null || !("u1" in previous.owners)`. ' +
'To test whether the list is set, compare it with `null`.',
},
],
'empty-expression': [
{
produce: () => rootsRefusal(' '),
Expand Down Expand Up @@ -926,6 +970,8 @@ const SWEEP_SOURCES: readonly string[] = [
'has(status) && other == 1',
'daysBetween(record.a, record.b) > 3',
"label(record.name) + label(record.status)",
// [#36167] A list-valued field compared with a scalar.
"record.tags == 'frontend'",
];

const SWEEP_INPUTS: readonly ExprInput[] = [
Expand All @@ -950,7 +996,7 @@ const SWEEP_SCHEMAS: readonly (ExprSchemaHint | undefined)[] = [
scope: 'record',
objectName: 'deal',
fields: ['amount', 'close_date', 'name', 'active', 'account', 'status'],
fieldTypes: { close_date: 'date', name: 'text', active: 'boolean', account: 'lookup' },
fieldTypes: { close_date: 'date', name: 'text', active: 'boolean', account: 'lookup', tags: 'multiselect' },
roleCatalog: ['org_admin', 'sales'],
optionFields: ['status'],
},
Expand Down
17 changes: 17 additions & 0 deletions packages/formula/src/expression-refusal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,22 @@ export interface ExpressionRefusalParams {
* fault sentence for that call.
*/
'label-field-no-options': { readonly field: string };
/**
* [#36167] `==` / `!=` between a list-valued field read (`ExprSchemaHint.multiValueFields`,
* or a `fieldTypes` type that is inherently multi) and a non-null scalar
* literal: CEL compares the whole list with the scalar, so the expression
* answers the same constant on every row. `reference` is the read as
* `<root>.<field>`, `operand` the literal as CEL prints it, `answer` the
* constant (`false` for `==`, `true` for `!=`), and `rewrite` the
* null-guarded membership test that replaces the comparison.
*/
'list-equality': {
readonly reference: string;
readonly operator: '==' | '!=';
readonly operand: string;
readonly answer: 'false' | 'true';
readonly rewrite: string;
};
/** `collectCelRootIdentifiers` was handed no expression. */
'empty-expression': Readonly<Record<string, never>>;
/** `collectCelRootIdentifiers` could not parse the source; `detail` is the parser's reason. */
Expand Down Expand Up @@ -395,6 +411,7 @@ const CODE_TABLE = {
'traversal-bare-and-traversed': true,
'traversal-multi-hop': true,
'label-field-no-options': true,
'list-equality': true,
'empty-expression': true,
'cel-parse-failed': true,
} as const satisfies Record<ExpressionRefusalCode, true>;
Expand Down
21 changes: 21 additions & 0 deletions packages/formula/src/relationship-traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,27 @@ export function readRootMembers(ast: CelAstNode, roots: readonly string[]): Root
return reads;
}

/**
* The member `node` ITSELF reads on one of `roots` — `record.tags`,
* `record.?tags`, `record['tags']`, `previous[?'tags']` — or `undefined` when
* `node` is anything else: a deeper read (`record.a.b`), a call, a literal, a
* computed key (`record[someVar]`).
*
* The same {@link asMember} spellings {@link readRootMembers} collects, asked
* of one node instead of gathered from a tree, for a check that judges an
* OPERAND rather than every read: `validateExpression`'s list-equality pass
* asks whether one side of a `==` is a field read, and gets the answer the
* field-existence pass would give for the same spelling.
*/
export function rootMemberOf(node: unknown, roots: readonly string[]): { readonly root: string; readonly field: string } | undefined {
const member = asMember(node);
if (!member) return undefined;
const receiver = member.receiver as { op?: unknown; args?: unknown } | null | undefined;
if (!receiver || typeof receiver !== 'object') return undefined;
const { op, args } = receiver;
return op === 'id' && typeof args === 'string' && roots.includes(args) ? { root: args, field: member.name } : undefined;
}

/**
* Analyse one authored CEL source for the hops it takes through `root`.
*
Expand Down
Loading
Loading