From 8dcc48ec21dfb38392b1d1ee79ae7615b0d22fe5 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 09:58:28 +0000 Subject: [PATCH 1/7] wip(lint): refuse list-equality on a hop target's list-valued field Claude-Session: https://claude.ai/code/session_011y7As7qKGpBTzxc5S8q3Ko Co-authored-by: Claude --- packages/formula/src/index.ts | 4 ++ .../formula/src/relationship-traversal.ts | 21 +++++++ packages/formula/src/validate.ts | 60 ++++++++++++++++++- packages/lint/src/validate-expressions.ts | 38 ++++++++++++ 4 files changed, 121 insertions(+), 2 deletions(-) diff --git a/packages/formula/src/index.ts b/packages/formula/src/index.ts index 783f21cc353..86642725eb0 100644 --- a/packages/formula/src/index.ts +++ b/packages/formula/src/index.ts @@ -183,6 +183,10 @@ export type { UnknownFunctionCall } from './unknown-function'; // ADR-0032 — shared validator + introspection (one validator for build, // registration, and the agent-callable validate_expression tool). export { validateExpression, introspectScope, expectedDialect, inferExpressionType, nearestName, CEL_STDLIB_FUNCTIONS } from './validate'; +// [#36237] The `list-equality` refusal one relationship hop away, for the +// caller that resolves hop targets (`@objectstack/lint`): the hint describes +// the root object's fields only. +export { findHopListEquality } from './validate'; export type { FieldRole, ExprInput, ExprSchemaHint, ExprFaultOutcome, ExprValidationError, ExprValidationResult, InferredValueType } from './validate'; // #22727 — the declaration authoring stamps onto a formula field: its // `returnType`, and for a provably-money result its currency, copied from the diff --git a/packages/formula/src/relationship-traversal.ts b/packages/formula/src/relationship-traversal.ts index d9a3c0d55ce..d2cbfc2012b 100644 --- a/packages/formula/src/relationship-traversal.ts +++ b/packages/formula/src/relationship-traversal.ts @@ -238,6 +238,27 @@ export function rootMemberOf(node: unknown, roots: readonly string[]): { readonl return op === 'id' && typeof args === 'string' && roots.includes(args) ? { root: args, field: member.name } : undefined; } +/** + * [#36237] The ONE-HOP read `node` itself is — `record.account.labels`, + * `record['account']['labels']`, any mix of the {@link asMember} spellings — + * as `{ root, field, leaf }`, or `undefined` when `node` is anything else: a + * read of the root's own member (`record.labels`, {@link rootMemberOf}'s + * answer), a deeper read (`record.a.b.c`), a call, a literal. + * + * {@link rootMemberOf} one segment further, for a check that judges an + * OPERAND reached through a relationship: `@objectstack/lint`'s hop pass asks + * whether one side of a `==` is a read of the hop target's field. + */ +export function hopMemberOf( + node: unknown, + roots: readonly string[], +): { readonly root: string; readonly field: string; readonly leaf: string } | undefined { + const member = asMember(node); + if (!member) return undefined; + const read = rootMemberOf(member.receiver, roots); + return read ? { ...read, leaf: member.name } : undefined; +} + /** * Analyse one authored CEL source for the hops it takes through `root`. * diff --git a/packages/formula/src/validate.ts b/packages/formula/src/validate.ts index d551b90c24f..d4d27f9bd02 100644 --- a/packages/formula/src/validate.ts +++ b/packages/formula/src/validate.ts @@ -34,7 +34,7 @@ import { type FieldCelType, } from './cel-engine'; import { templateEngine } from './template-engine'; -import { findTraversalConflicts, readRootMembers, rootMemberOf, traversalsOf, type RootMemberRead } from './relationship-traversal'; +import { findTraversalConflicts, hopMemberOf, readRootMembers, rootMemberOf, traversalsOf, type RootMemberRead } from './relationship-traversal'; import { MULTI_OPTION_TYPES, REFERENCE_VALUE_TYPES } from '@objectstack/spec/data'; // #13594 — the one reader of cel-js's `found no matching overload for '…'` // template. Both this module (which asks whether the name is ADVERTISED, to word @@ -1567,6 +1567,62 @@ function checkListEquality( : undefined; }; const filterCompiled = schema?.filterCompiled === true; + forEachListEquality(tree, subjectOf, (read, op, operand) => { + errors.push(listEqualityRefusal(source, read, op, operand, filterCompiled)); + }); +} + +/** + * [#36237] {@link checkListEquality} one relationship hop away: `==` / `!=` + * between a read of a list-valued field ON A HOP TARGET + * (`record.account.labels`) and a non-null scalar literal, refused under the + * same `list-equality` code, in the root case's words and with its + * `has()`-guarded `in` prescription (`record.account.labels` is a + * `.` read of the related record the hop hydrates). + * + * {@link ExprSchemaHint} describes the root object's fields and no hop + * target's, so this is a function of its own for the caller that resolves hop + * targets — `@objectstack/lint`, at the two sites that serve a hop (a + * validation rule's hydrated predicate, a formula's `expression`). + * `listLeaves` maps each single-valued reference field of the root object to + * the list-valued fields its target declares; a read it does not name gets no + * verdict. Only `record` hops are judged ({@link DEFAULT_TRAVERSAL_ROOT}, the + * root a hop is served from), and a source that does not compile gets none — + * that is `validateExpression`'s `invalid-cel` verdict. + */ +export function findHopListEquality( + source: string, + listLeaves: ReadonlyMap>, +): ExprValidationError[] { + const errors: ExprValidationError[] = []; + if (listLeaves.size === 0 || (!source.includes('==') && !source.includes('!='))) return errors; + if (!compileCel(source).result.ok) return errors; + const tree = parseCelToAst(source); + if (tree == null) return errors; + const subjectOf = (node: unknown, bound: ReadonlySet): ListSubjectRead | undefined => { + const read = hopMemberOf(node, [DEFAULT_TRAVERSAL_ROOT]); + return read && !bound.has(read.root) && listLeaves.get(read.field)?.has(read.leaf) + ? { reference: `${read.root}.${read.field}.${read.leaf}`, bare: false } + : undefined; + }; + forEachListEquality(tree, subjectOf, (read, op, operand) => { + errors.push(listEqualityRefusal(source, read, op, operand, false)); + }); + return errors; +} + +/** + * Every `==` / `!=` in `tree` with a list subject (`subjectOf`) on one side + * and a scalar literal ({@link scalarLiteralOf}) on the other, either order, + * handed to `onHit` once per comparison. `subjectOf` is asked with the names + * the enclosing comprehension macros bind ({@link COMPREHENSION_MACRO_NAMES}): + * the receiver is read outside the binding, the body inside it. + */ +function forEachListEquality( + tree: unknown, + subjectOf: (node: unknown, bound: ReadonlySet) => ListSubjectRead | undefined, + onHit: (read: ListSubjectRead, op: '==' | '!=', operand: string) => void, +): void { const walk = (node: unknown, bound: ReadonlySet): void => { if (!node || typeof node !== 'object') return; if (Array.isArray(node)) { @@ -1592,7 +1648,7 @@ function checkListEquality( const read = subjectOf(args[side], bound); const operand = read ? scalarLiteralOf(args[1 - side]) : undefined; if (!read || operand === undefined) continue; - errors.push(listEqualityRefusal(source, read, op, operand, filterCompiled)); + onHit(read, op, operand); break; } } diff --git a/packages/lint/src/validate-expressions.ts b/packages/lint/src/validate-expressions.ts index c10ac8bb787..1f65b455283 100644 --- a/packages/lint/src/validate-expressions.ts +++ b/packages/lint/src/validate-expressions.ts @@ -95,6 +95,7 @@ import { analyzeRelationshipTraversals, buildScope, declaresOptionsOnceServed, + findHopListEquality, } from '@objectstack/formula'; import type { ExprFaultOutcome, RelationshipTraversalAnalysis } from '@objectstack/formula'; import { @@ -2808,6 +2809,40 @@ export function runStackExpressionPasses(stack: AnyRec, options: StackExpression } }; + /** + * [#36237] The `list-equality` refusal one hop away: `==` / `!=` between a + * read of a list-valued field on a hop target (`record.account.labels`) and + * a scalar, at the same two sites that serve a hop as + * {@link refuseUndeclaredHopField}. The hop loader hydrates the related + * row, so the read is the stored list and the comparison answers one + * constant on every row — the root case's defect, which the shared + * validator cannot see here because its hint carries the root object's + * fields only. Its words and code are the root case's + * (`findHopListEquality`). + * + * The reference and its target are read as {@link refuseUndeclaredHopField} + * reads them (the objects as served); a reference that is itself + * multi-valued, and a target outside the index or in the stored-metadata + * family, are not judged. + */ + const refuseHopListEquality = (where: string, raw: unknown, objectName: string): void => { + const source = celSourceOf(raw); + if (!source) return; + const listLeaves = new Map>(); + const ownLists = multiValueFieldIndex.get(objectName) ?? []; + for (const obj of typedObjects) { + if (obj.name !== objectName) continue; + for (const [field, def] of fieldEntries(obj)) { + const target = ownLists.includes(field) ? undefined : referenceTargetOf(def)?.trim(); + const leaves = target && !isStoredMetadataBodyObject(target) ? multiValueFieldIndex.get(target) : undefined; + if (leaves && leaves.length > 0) listLeaves.set(field, new Set(leaves)); + } + } + for (const e of findHopListEquality(source, listLeaves)) { + issues.push({ where, message: e.message, source: e.source, severity: 'error' }); + } + }; + // [#8116] object name → the injected anchors registered with NO storage // behind them (non-empty only for ADR-0015 `external` objects). The other // half of {@link buildFieldIndex}'s #5378 widening: the anchors ARE @@ -3050,6 +3085,7 @@ export function runStackExpressionPasses(stack: AnyRec, options: StackExpression } if (objectName && traversalHydration) refuseStoredMetadataBodyHop(where, raw, objectName, 'rule'); if (objectName && traversalHydration) refuseUndeclaredHopField(where, raw, objectName, 'rule'); + if (objectName && traversalHydration) refuseHopListEquality(where, raw, objectName); // [#8116] Provenance rides every object-bound predicate this helper // validates, whichever scope: the `record`/`previous` roots are explicit // in the source, so flattened flow conditions are covered without ever @@ -3943,6 +3979,8 @@ export function runStackExpressionPasses(stack: AnyRec, options: StackExpression if (objectName) refuseStoredMetadataBodyHop(fieldWhere, f.expression, objectName, 'formula'); // [#24410] And a hop to a field the target does not declare. if (objectName) refuseUndeclaredHopField(fieldWhere, f.expression, objectName, 'formula'); + // [#36237] And a list-valued hop target field compared with a scalar. + if (objectName) refuseHopListEquality(fieldWhere, f.expression, objectName); // [#8116] Formulas read the same record binding the predicates do, so // a `record.` read inside one degrades identically. warnUnprovisionedAnchors(fieldWhere, f.expression, objectName); From c8ea6571738d891bf0db008c153da7c620d0e534 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 10:08:48 +0000 Subject: [PATCH 2/7] test: pin list-equality through a lookup hop Claude-Session: https://claude.ai/code/session_011y7As7qKGpBTzxc5S8q3Ko Co-authored-by: Claude --- .../validate-hop-list-equality-36237.test.ts | 90 +++++++++++ ...xpressions.hop-list-equality-36237.test.ts | 146 ++++++++++++++++++ 2 files changed, 236 insertions(+) create mode 100644 packages/formula/src/validate-hop-list-equality-36237.test.ts create mode 100644 packages/lint/src/validate-expressions.hop-list-equality-36237.test.ts diff --git a/packages/formula/src/validate-hop-list-equality-36237.test.ts b/packages/formula/src/validate-hop-list-equality-36237.test.ts new file mode 100644 index 00000000000..969dccc613a --- /dev/null +++ b/packages/formula/src/validate-hop-list-equality-36237.test.ts @@ -0,0 +1,90 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#36237] `findHopListEquality` — the `list-equality` refusal one + * relationship hop away. `record.account.labels == 'vip'`, where `account` is + * a reference and `labels` a list-valued field of its target, compares the + * whole hydrated list with the scalar: one constant on every row, the root + * case's defect (`validate-list-equality-36167.test.ts`). The hint carries the + * root object's fields only, so the caller that resolves hop targets hands + * the reference → list-valued-leaves map in. + * + * The first describe proves the premise on the evaluator, over the shape the + * hop loader binds (the related row nested under the reference field). + */ + +import { describe, it, expect } from 'vitest'; + +import { celEngine } from './cel-engine'; +import { findHopListEquality } from './validate'; + +/** `account` reaches a target whose `labels` is list-valued. */ +const LEAVES: ReadonlyMap> = new Map([['account', new Set(['labels'])]]); + +describe('the premise — a hydrated hop to a list, compared with a scalar, is a constant', () => { + const run = (source: string, labels: unknown) => + celEngine.evaluate({ dialect: 'cel', source }, { record: { account: { id: 'a1', labels } } }); + it.each([[['vip', 'eu']], [['eu']], [[]], [null]])('labels = %j', (labels) => { + expect(run("record.account.labels == 'vip'", labels)).toEqual({ ok: true, value: false }); + expect(run("record.account.labels != 'vip'", labels)).toEqual({ ok: true, value: true }); + }); +}); + +describe('findHopListEquality — refused', () => { + it.each([ + ["record.account.labels == 'vip'", '==', 'false'], + ["'vip' != record.account.labels", '!=', 'true'], + ["record['account']['labels'] == 'vip'", '==', 'false'], + ["record.amount > 1 && record.account.labels != 'vip'", '!=', 'true'], + ] as const)('%s, under the root case\'s code and params', (source, operator, answer) => { + const errors = findHopListEquality(source, LEAVES); + expect(errors).toHaveLength(1); + const [e] = errors; + expect(e.code).toBe('list-equality'); + expect(e.code === 'list-equality' && e.params.reference).toBe('record.account.labels'); + expect(e.code === 'list-equality' && e.params.operator).toBe(operator); + expect(e.code === 'list-equality' && e.params.answer).toBe(answer); + expect(e.source).toBe(source); + }); + + it('the prescribed rewrite answers per row on the hydrated hop, and is not itself refused', () => { + const [eq] = findHopListEquality("record.account.labels == 'vip'", LEAVES); + const [ne] = findHopListEquality("record.account.labels != 'vip'", LEAVES); + const eqRewrite = eq!.code === 'list-equality' ? eq!.params.rewrite : ''; + const neRewrite = ne!.code === 'list-equality' ? ne!.params.rewrite : ''; + expect(eqRewrite).toBe('(has(record.account.labels) && record.account.labels != null && "vip" in record.account.labels)'); + const run = (source: string, labels: unknown) => + celEngine.evaluate({ dialect: 'cel', source }, { record: { account: { id: 'a1', labels } } }); + expect(run(eqRewrite, ['vip', 'eu'])).toEqual({ ok: true, value: true }); + expect(run(eqRewrite, ['eu'])).toEqual({ ok: true, value: false }); + expect(run(eqRewrite, null)).toEqual({ ok: true, value: false }); + expect(run(neRewrite, ['vip'])).toEqual({ ok: true, value: false }); + expect(run(neRewrite, null)).toEqual({ ok: true, value: true }); + expect(findHopListEquality(eqRewrite, LEAVES)).toEqual([]); + expect(findHopListEquality(neRewrite, LEAVES)).toEqual([]); + }); +}); + +describe('findHopListEquality — CONTROL, accepted', () => { + it.each([ + // A scalar field on the same hop target. + "record.account.name == 'vip'", + "record.account.labels == null", + "'vip' in record.account.labels", + // The list-valued field of the ROOT object is the shared validator's verdict, not this one's. + "record.labels == 'vip'", + // Another reference the map does not name. + "record.parent.labels == 'vip'", + // Two hops deep is the traversal checks' refusal. + "record.account.owner.labels == 'vip'", + // A comprehension macro binding `record` holds an item, not the row. + "[{'account': {'labels': 'x'}}].exists(record, record.account.labels == 'x')", + ])('%s', (source) => { + expect(findHopListEquality(source, LEAVES)).toEqual([]); + }); + + it('an empty map, and a source that does not compile, get no verdict', () => { + expect(findHopListEquality("record.account.labels == 'vip'", new Map())).toEqual([]); + expect(findHopListEquality("record.account.labels == 'vip' &&", LEAVES)).toEqual([]); + }); +}); diff --git a/packages/lint/src/validate-expressions.hop-list-equality-36237.test.ts b/packages/lint/src/validate-expressions.hop-list-equality-36237.test.ts new file mode 100644 index 00000000000..ac371c5bbbf --- /dev/null +++ b/packages/lint/src/validate-expressions.hop-list-equality-36237.test.ts @@ -0,0 +1,146 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#36237] The publish lint refuses `==` / `!=` between a list-valued field + * reached through a lookup hop (`record.account.labels`) and a scalar, at the + * two sites that serve a hop — a validation rule's hydrated `condition` / + * `when` and a formula's `expression` — in the root case's words + * (`validate-expressions.list-equality-36167.test.ts`). The hop loader + * hydrates the related row, so the read is the stored list and the + * comparison answers one constant on every row. + * + * Every fixture goes through `ObjectStackSchema.safeParse` first, so no case + * pins a branch an author cannot reach. + */ + +import { describe, it, expect } from 'vitest'; + +import { ObjectStackSchema } from '@objectstack/spec'; + +import { validateStackExpressions } from './validate-expressions.js'; + +const MANIFEST = { id: 'com.example.hop-list-equality', name: 'hop_list_equality_probe', version: '1.0.0', type: 'app' } as const; + +function specValid(stack: Record): Record { + const result = ObjectStackSchema.safeParse({ manifest: MANIFEST, ...stack }); + if (!result.success) { + throw new Error( + 'fixture is not spec-valid: ' + + result.error.issues.map((i) => `${i.path.join('.') || '(root)'}: ${i.message}`).join('; '), + ); + } + return result.data as unknown as Record; +} + +const OPTIONS = [ + { label: 'VIP', value: 'vip' }, + { label: 'EU', value: 'eu' }, +]; + +const account = { + name: 'hop_account', + label: 'Account', + fields: { + name: { type: 'text', label: 'Name' }, + tier: { type: 'select', label: 'Tier', options: OPTIONS }, + labels: { type: 'multiselect', label: 'Labels', options: OPTIONS }, + owners: { type: 'user', label: 'Owners', multiple: true }, + owner: { type: 'user', label: 'Owner' }, + }, +}; + +function contactWith(extra: { validations?: unknown[]; fields?: Record }) { + return { + name: 'hop_contact', + label: 'Contact', + fields: { + name: { type: 'text', label: 'Name' }, + account: { type: 'lookup', label: 'Account', reference: 'hop_account' }, + accounts: { type: 'lookup', label: 'Accounts', reference: 'hop_account', multiple: true }, + ...extra.fields, + }, + ...(extra.validations ? { validations: extra.validations } : {}), + }; +} + +const listEqualityErrors = (stack: Record) => + validateStackExpressions(specValid(stack)).filter((i) => i.severity === 'error' && i.message.includes('holds a list')); + +/** The list-equality errors the lint reports for one validation rule `condition` on the contact. */ +function ruleErrors(condition: string) { + return listEqualityErrors({ + objects: [account, contactWith({ validations: [{ name: 'probe_rule', type: 'script', severity: 'error', message: 'm', condition }] })], + }); +} + +/** Every error the lint reports for the same rule — to read a rewrite as accepted. */ +function allRuleErrors(condition: string) { + return validateStackExpressions(specValid({ + objects: [account, contactWith({ validations: [{ name: 'probe_rule', type: 'script', severity: 'error', message: 'm', condition }] })], + })).filter((i) => i.severity === 'error'); +} + +describe('publish lint — list-equality through a lookup hop', () => { + it.each([ + // An inherently multi-value type on the target. + ["record.account.labels == 'vip'", 'record.account.labels', '(has(record.account.labels) && record.account.labels != null && "vip" in record.account.labels)'], + // One hop to a field declared `multiple: true` — a type alone does not mark it a list. + ["record.account.owners != 'u1'", 'record.account.owners', '(!has(record.account.owners) || record.account.owners == null || !("u1" in record.account.owners))'], + ])('refuses the validation rule %s, naming the hop read and the `in` rewrite', (condition, reference, rewrite) => { + const errors = ruleErrors(condition); + expect(errors).toHaveLength(1); + expect(errors[0]!.where).toContain("validation 'probe_rule'"); + expect(errors[0]!.path).toBe('objects[1].validations[0].condition'); + expect(errors[0]!.message).toContain(`\`${reference}\` holds a list`); + expect(errors[0]!.message).toContain(`write \`${rewrite}\``); + // The prescribed text is itself accepted where it was prescribed. + expect(allRuleErrors(rewrite)).toEqual([]); + }); + + it.each([ + // One hop to a scalar field. + "record.account.tier == 'vip'", + "record.account.owner == 'u1'", + "record.account.labels == null", + "'vip' in record.account.labels", + ])('CONTROL — accepts the validation rule %s', (condition) => { + expect(allRuleErrors(condition)).toEqual([]); + }); + + it('refuses a conditional rule\'s `when` and a formula `expression` too', () => { + const errors = listEqualityErrors({ + objects: [ + account, + contactWith({ + fields: { is_vip: { type: 'formula', label: 'Is VIP', expression: "record.account.labels == 'vip'" } }, + validations: [ + { + name: 'guarded', + type: 'conditional', + severity: 'error', + message: 'm', + when: "record.account.owners == 'u1'", + then: { name: 'inner', type: 'script', severity: 'error', message: 'm', condition: "record.name == ''" }, + }, + ], + }), + ], + }); + expect(errors.map((e) => e.source).sort()).toEqual(["record.account.labels == 'vip'", "record.account.owners == 'u1'"]); + }); + + // A reference that is itself multi-valued holds a list of ids: its hop is + // not this check's shape, and the target's leaf is not judged through it. + it('CONTROL — a multi-valued reference is not judged', () => { + expect(ruleErrors("record.accounts.labels == 'vip'")).toEqual([]); + }); + + // An action's `visible` serves no hop: `record.account` is the stored id + // there, so the list-equality prescription would not apply. + it('CONTROL — a site that serves no hop is not judged', () => { + expect(listEqualityErrors({ + objects: [account, contactWith({})], + actions: [{ name: 'probe_action', label: 'Probe', objectName: 'hop_contact', type: 'script', target: 'fn', visible: "record.account.labels == 'vip'" }], + })).toEqual([]); + }); +}); From ff7e133da461950aecbde56a1887d819cc07e583 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 10:12:23 +0000 Subject: [PATCH 3/7] fix(lint): refuse list-equality through a lookup hop; changeset Claude-Session: https://claude.ai/code/session_011y7As7qKGpBTzxc5S8q3Ko Co-authored-by: Claude --- .changeset/36237-hop-list-equality.md | 23 +++++++++++++++++++ ...xpressions.hop-list-equality-36237.test.ts | 17 +++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 .changeset/36237-hop-list-equality.md diff --git a/.changeset/36237-hop-list-equality.md b/.changeset/36237-hop-list-equality.md new file mode 100644 index 00000000000..a0da3336502 --- /dev/null +++ b/.changeset/36237-hop-list-equality.md @@ -0,0 +1,23 @@ +--- +'@objectstack/formula': minor +'@objectstack/lint': minor +--- + +fix(lint): `==` / `!=` between a list-valued field reached through a lookup hop and a scalar is refused, as the root-field case is + +Clause-②: no +Metadata-protocol: narrowed +Skills-docs: none — the `objectstack-formula` skill already teaches that a list field compared with a scalar literal is refused and the `has()`-guarded `in` rewrite; a hop read of a list field takes the same rewrite + +`record.labels == 'vip'` on a list-valued field has been refused as `list-equality` since the shared validator learned it, but the same comparison one hop away, `record.account.labels == 'vip'` where `account` is a lookup and `labels` a list-valued field of its target, was refused nowhere. A validation rule's `condition` / `when` and a formula's `expression` are the two sites that serve a hop: the engine hydrates the related row, so `record.account.labels` is the stored list, and comparing it with a scalar answers `false` (for `!=`, `true`) on every row. A rule written that way never fires and saves clean. + +- **The refusal.** At those two sites, the publish lint (`os validate` / `os build`) and the object save door refuse a `==` / `!=` between a read of a hop target's list-valued field (an inherently multi-value type, or a multi-capable type declared `multiple: true`) and a non-null scalar literal, either operand order, in the root case's words, prescribing the root case's membership test on the hop read. +- **What carries it.** `@objectstack/formula` exports `findHopListEquality(source, listLeaves)`, which returns the `list-equality` refusals (same code and params as `validateExpression`'s) for the reference → list-valued-leaves map its caller hands in. `ExprSchemaHint` is unchanged. +- **Still accepted.** A hop to a scalar field (`record.account.tier == 'vip'`), `record.account.labels == null`, `'vip' in record.account.labels`, a hop through a reference that is itself multi-valued, and every site that serves no hop (an action's `visible`, a flow condition), where `record.account` is the stored id. + +FROM → TO: + +- `record.account.labels == 'vip'` → `(has(record.account.labels) && record.account.labels != null && "vip" in record.account.labels)` +- `record.account.labels != 'vip'` → `(!has(record.account.labels) || record.account.labels == null || !("vip" in record.account.labels))` + +One-line fix: test membership with `in` on the hop read, guarded as the refusal prescribes. diff --git a/packages/lint/src/validate-expressions.hop-list-equality-36237.test.ts b/packages/lint/src/validate-expressions.hop-list-equality-36237.test.ts index ac371c5bbbf..c89e0671a3c 100644 --- a/packages/lint/src/validate-expressions.hop-list-equality-36237.test.ts +++ b/packages/lint/src/validate-expressions.hop-list-equality-36237.test.ts @@ -17,7 +17,7 @@ import { describe, it, expect } from 'vitest'; import { ObjectStackSchema } from '@objectstack/spec'; -import { validateStackExpressions } from './validate-expressions.js'; +import { runStackExpressionPasses, validateStackExpressions } from './validate-expressions.js'; const MANIFEST = { id: 'com.example.hop-list-equality', name: 'hop_list_equality_probe', version: '1.0.0', type: 'app' } as const; @@ -129,6 +129,21 @@ describe('publish lint — list-equality through a lookup hop', () => { expect(errors.map((e) => e.source).sort()).toEqual(["record.account.labels == 'vip'", "record.account.owners == 'u1'"]); }); + it('the object save door (`runtimeWriteType: object`) refuses the same hop, rule and formula', () => { + const stack = specValid({ + objects: [ + account, + contactWith({ + fields: { is_vip: { type: 'formula', label: 'Is VIP', expression: "record.account.labels == 'vip'" } }, + validations: [{ name: 'probe_rule', type: 'script', severity: 'error', message: 'm', condition: "record.account.owners != 'u1'" }], + }), + ], + }); + const errors = runStackExpressionPasses(stack, { runtimeWriteType: 'object' }) + .filter((i) => i.severity === 'error' && i.message.includes('holds a list')); + expect(errors.map((e) => e.source).sort()).toEqual(["record.account.labels == 'vip'", "record.account.owners != 'u1'"]); + }); + // A reference that is itself multi-valued holds a list of ids: its hop is // not this check's shape, and the target's leaf is not judged through it. it('CONTROL — a multi-valued reference is not judged', () => { From 72a1bda7b46d393d44b8f5653572f3f427a1e104 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 10:19:24 +0000 Subject: [PATCH 4/7] chore(changeset): state the ADR-0087 disposition Claude-Session: https://claude.ai/code/session_011y7As7qKGpBTzxc5S8q3Ko Co-authored-by: Claude --- .changeset/36237-hop-list-equality.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.changeset/36237-hop-list-equality.md b/.changeset/36237-hop-list-equality.md index a0da3336502..f4f90ff4dbd 100644 --- a/.changeset/36237-hop-list-equality.md +++ b/.changeset/36237-hop-list-equality.md @@ -9,6 +9,8 @@ Clause-②: no Metadata-protocol: narrowed Skills-docs: none — the `objectstack-formula` skill already teaches that a list field compared with a scalar literal is refused and the `has()`-guarded `in` rewrite; a hop read of a list field takes the same rewrite + + `record.labels == 'vip'` on a list-valued field has been refused as `list-equality` since the shared validator learned it, but the same comparison one hop away, `record.account.labels == 'vip'` where `account` is a lookup and `labels` a list-valued field of its target, was refused nowhere. A validation rule's `condition` / `when` and a formula's `expression` are the two sites that serve a hop: the engine hydrates the related row, so `record.account.labels` is the stored list, and comparing it with a scalar answers `false` (for `!=`, `true`) on every row. A rule written that way never fires and saves clean. - **The refusal.** At those two sites, the publish lint (`os validate` / `os build`) and the object save door refuse a `==` / `!=` between a read of a hop target's list-valued field (an inherently multi-value type, or a multi-capable type declared `multiple: true`) and a non-null scalar literal, either operand order, in the root case's words, prescribing the root case's membership test on the hop read. From d26a320b302eaec401912a7299d77fccdd45650b Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 11:00:44 +0000 Subject: [PATCH 5/7] test(lint): classify the hop list-equality pass in the gate ledgers Claude-Session: https://claude.ai/code/session_011y7As7qKGpBTzxc5S8q3Ko Co-authored-by: Claude --- packages/lint/src/authored-site-expression.test.ts | 10 ++++++++++ packages/lint/src/validate-expressions.test.ts | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/packages/lint/src/authored-site-expression.test.ts b/packages/lint/src/authored-site-expression.test.ts index e242f840afa..b2022902117 100644 --- a/packages/lint/src/authored-site-expression.test.ts +++ b/packages/lint/src/authored-site-expression.test.ts @@ -193,6 +193,9 @@ const EXT = { const VALIDATOR = { kind: 'validator', why: 'the shared validator: the tool runs `validateExpression` itself' } as const; const HOP_FIELD: Fixture = { objects: [REQ, BAL], expression: 'record.balance.nope > 1', severity: 'error', names: 'nope' }; +// [#36237] A list-valued field on the hop target, compared with a scalar. +const BAL_TAGS = { name: 'bal', fields: { ...BAL.fields, tags: { type: 'tags', label: 'Tags' } } }; +const HOP_LIST: Fixture = { objects: [REQ, BAL_TAGS], expression: "record.balance.tags == 'x'", severity: 'error', names: 'record.balance.tags' }; const BODY_HOP: Fixture = { objects: [REQ, BAL], expression: "record.meta.name == 'x'", severity: 'error', names: 'sys_metadata' }; const ANCHOR: Fixture = { objects: [EXT], expression: "record.owner_id == 'u1'", severity: 'warning', names: 'owner_id' }; const asValue = (f: Fixture): Fixture => ({ ...f, expression: `${f.expression} ? 1 : 0` }); @@ -204,6 +207,7 @@ const SITE_CALLS: Record> = { isBareReferenceToAny: { kind: 'plumbing', why: "filters the shared validator's findings on a field-rule slot" }, refuseStoredMetadataBodyHop: { kind: 'pass', fixture: BODY_HOP }, refuseUndeclaredHopField: { kind: 'pass', fixture: HOP_FIELD }, + refuseHopListEquality: { kind: 'pass', fixture: HOP_LIST }, warnUnprovisionedAnchors: { kind: 'pass', fixture: ANCHOR }, atSlot: { kind: 'plumbing', why: "sets the judged slot's key path on the findings" }, celSourceOf: { kind: 'plumbing', why: 'reads the CEL source off the slot' }, @@ -224,6 +228,7 @@ const SITE_CALLS: Record> = { celSourceOf: { kind: 'plumbing', why: 'reads the CEL source off the field' }, refuseStoredMetadataBodyHop: { kind: 'pass', fixture: asValue(BODY_HOP) }, refuseUndeclaredHopField: { kind: 'pass', fixture: asValue(HOP_FIELD) }, + refuseHopListEquality: { kind: 'pass', fixture: asValue(HOP_LIST) }, warnUnprovisionedAnchors: { kind: 'pass', fixture: asValue(ANCHOR) }, // [#33100] The formula-cycle pass, after the fields loop. It locates each // finding on a cycle member's own `expression`; the probe is named @@ -530,6 +535,11 @@ const FLOW_CALLS: Record = { why: 'runs only where a relationship hop is hydrated (a rule condition, a formula); nothing hydrates one at a flow condition', fixture: { objects: [FLOW_OBJ, BAL], expression: 'record.balance.nope > 1' }, }, + refuseHopListEquality: { + kind: 'pass-silent-here', + why: 'runs only where a relationship hop is hydrated (a rule condition, a formula); nothing hydrates one at a flow condition', + fixture: { objects: [FLOW_OBJ, BAL_TAGS], expression: "record.balance.tags == 'x'" }, + }, }; /** The start node of a hand-written flow on `obj`, with the trigger `kind` names. */ diff --git a/packages/lint/src/validate-expressions.test.ts b/packages/lint/src/validate-expressions.test.ts index e8370a926dd..8cd59992989 100644 --- a/packages/lint/src/validate-expressions.test.ts +++ b/packages/lint/src/validate-expressions.test.ts @@ -4577,6 +4577,11 @@ describe('validateStackExpressions — reads only keys the spec declares (meta-t // The served body the hop's field index is read from is indexed by // position (`typedObjects[objectPosition]`), so it is no receiver here. 'inputs', 'fieldDefs', 'finding', + // [#36237] The hop list-equality refusal: the object's own list-valued + // field names, one hop target's, and the reference → leaves map handed + // to the formula package. Their keys are Array / Map methods, never + // metadata keys; the reference is read off the tabled `def` receiver. + 'ownLists', 'leaves', 'listLeaves', ]); expect(receivers.filter((r) => !tabled.has(r) && !PLUMBING.has(r))).toEqual([]); }); From 22261181b09bdcab1d75c3fc4297f15b40d00269 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 14:17:37 +0000 Subject: [PATCH 6/7] =?UTF-8?q?chore(changeset):=20declare=20Clause-?= =?UTF-8?q?=E2=91=A1=20yes=20(narrowing)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Claude-Session: https://claude.ai/code/session_011y7As7qKGpBTzxc5S8q3Ko Co-authored-by: Claude --- .changeset/36237-hop-list-equality.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.changeset/36237-hop-list-equality.md b/.changeset/36237-hop-list-equality.md index f4f90ff4dbd..77954d1e270 100644 --- a/.changeset/36237-hop-list-equality.md +++ b/.changeset/36237-hop-list-equality.md @@ -5,12 +5,14 @@ fix(lint): `==` / `!=` between a list-valued field reached through a lookup hop and a scalar is refused, as the root-field case is -Clause-②: no +Clause-②: yes (narrowing) Metadata-protocol: narrowed Skills-docs: none — the `objectstack-formula` skill already teaches that a list field compared with a scalar literal is refused and the `has()`-guarded `in` rewrite; a hop read of a list field takes the same rewrite +**BREAKING** accept-set narrowing, shipped as `minor`; the new `@objectstack/formula` export `findHopListEquality` widens the package's public surface. + `record.labels == 'vip'` on a list-valued field has been refused as `list-equality` since the shared validator learned it, but the same comparison one hop away, `record.account.labels == 'vip'` where `account` is a lookup and `labels` a list-valued field of its target, was refused nowhere. A validation rule's `condition` / `when` and a formula's `expression` are the two sites that serve a hop: the engine hydrates the related row, so `record.account.labels` is the stored list, and comparing it with a scalar answers `false` (for `!=`, `true`) on every row. A rule written that way never fires and saves clean. - **The refusal.** At those two sites, the publish lint (`os validate` / `os build`) and the object save door refuse a `==` / `!=` between a read of a hop target's list-valued field (an inherently multi-value type, or a multi-capable type declared `multiple: true`) and a non-null scalar literal, either operand order, in the root case's words, prescribing the root case's membership test on the hop read. From a59d4a847b86a08a2050a908a78b41e9e0f148b7 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Nov 2026 00:17:17 +0000 Subject: [PATCH 7/7] docs(spec): the cel-list-equality-refused entry names the one-hop read and its doors Claude-Session: https://claude.ai/code/session_011y7As7qKGpBTzxc5S8q3Ko Co-authored-by: Claude --- .changeset/36237-hop-list-equality.md | 7 ++++--- .../semantic/18.cel-list-equality-refused.ts | 13 +++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/.changeset/36237-hop-list-equality.md b/.changeset/36237-hop-list-equality.md index 77954d1e270..3571034cc83 100644 --- a/.changeset/36237-hop-list-equality.md +++ b/.changeset/36237-hop-list-equality.md @@ -1,22 +1,23 @@ --- '@objectstack/formula': minor '@objectstack/lint': minor +'@objectstack/spec': minor --- -fix(lint): `==` / `!=` between a list-valued field reached through a lookup hop and a scalar is refused, as the root-field case is +fix(lint)!: `==` / `!=` between a list-valued field reached through a lookup hop and a scalar is refused, as the root-field case is Clause-②: yes (narrowing) Metadata-protocol: narrowed Skills-docs: none — the `objectstack-formula` skill already teaches that a list field compared with a scalar literal is refused and the `has()`-guarded `in` rewrite; a hop read of a list field takes the same rewrite - + **BREAKING** accept-set narrowing, shipped as `minor`; the new `@objectstack/formula` export `findHopListEquality` widens the package's public surface. `record.labels == 'vip'` on a list-valued field has been refused as `list-equality` since the shared validator learned it, but the same comparison one hop away, `record.account.labels == 'vip'` where `account` is a lookup and `labels` a list-valued field of its target, was refused nowhere. A validation rule's `condition` / `when` and a formula's `expression` are the two sites that serve a hop: the engine hydrates the related row, so `record.account.labels` is the stored list, and comparing it with a scalar answers `false` (for `!=`, `true`) on every row. A rule written that way never fires and saves clean. - **The refusal.** At those two sites, the publish lint (`os validate` / `os build`) and the object save door refuse a `==` / `!=` between a read of a hop target's list-valued field (an inherently multi-value type, or a multi-capable type declared `multiple: true`) and a non-null scalar literal, either operand order, in the root case's words, prescribing the root case's membership test on the hop read. -- **What carries it.** `@objectstack/formula` exports `findHopListEquality(source, listLeaves)`, which returns the `list-equality` refusals (same code and params as `validateExpression`'s) for the reference → list-valued-leaves map its caller hands in. `ExprSchemaHint` is unchanged. +- **What carries it.** `@objectstack/formula` exports `findHopListEquality(source, listLeaves)`, which returns the `list-equality` refusals (same code and params as `validateExpression`'s) for the reference → list-valued-leaves map its caller hands in. `ExprSchemaHint` is unchanged. The ADR-0087 entry `cel-list-equality-refused` now names the one-hop read and its doors; the Studio draft check and the shared-validator half of the MCP `validate_expression` tool do not see a hop. - **Still accepted.** A hop to a scalar field (`record.account.tier == 'vip'`), `record.account.labels == null`, `'vip' in record.account.labels`, a hop through a reference that is itself multi-valued, and every site that serves no hop (an action's `visible`, a flow condition), where `record.account` is the stored id. FROM → TO: diff --git a/packages/spec/src/migrations/entries/semantic/18.cel-list-equality-refused.ts b/packages/spec/src/migrations/entries/semantic/18.cel-list-equality-refused.ts index fc41921f4ab..f164774e8e7 100644 --- a/packages/spec/src/migrations/entries/semantic/18.cel-list-equality-refused.ts +++ b/packages/spec/src/migrations/entries/semantic/18.cel-list-equality-refused.ts @@ -19,7 +19,12 @@ export const entry: SemanticMigration = { + 'string, number or boolean literal, in either operand order. Not the shape: a name a comprehension macro ' + 'binds (`record.items.exists(labels, labels == \'a\')`, where `labels` is one item), a bare name a ' + 'declared flow variable holds, nor any read in a flow no record write or sweep starts (an API, scheduled or ' - + 'trigger-less flow), whose run holds what its entrance handed it', + + 'trigger-less flow), whose run holds what its entrance handed it. ' + + 'Also the one-hop read `record.REF.LEAF` at the two sites that serve a hop, a validation rule\'s `condition` ' + + '/ `when` (nested branches included) and a field `expression`: REF a single-valued reference field and LEAF ' + + 'a list-valued field of its target (`record.account.labels == \'vip\'`), compared with the same literals. Not ' + + 'that shape: a reference that is itself multi-valued, a second hop, a `previous` hop, nor a hop at any other ' + + 'site, where `record.REF` holds the stored id', replacement: 'Test membership, parenthesised whole so it stands where the comparison stood, in the spelling the site reads. ' + 'A `record.` / `previous.` read evaluated as CEL: `(has(record.labels) && record.labels != null && \'frontend\' ' @@ -32,7 +37,11 @@ export const entry: SemanticMigration = { + 'refusal names the rewrite for its site. Compare the list with `null` to test whether it is set, and with a list ' + 'literal for a whole-list match. ' + '`os validate`, `os build`, the MCP `validate_expression` tool and the Studio object editor\'s draft check ' - + 'refuse the shape as `list-equality`, an error; the flow registration pass logs it.', + + 'refuse the shape as `list-equality`, an error; the flow registration pass logs it. ' + + 'The one-hop read takes the same rewrite on the hop read, `(has(record.account.labels) && ' + + 'record.account.labels != null && \'vip\' in record.account.labels)` for `==` and its `!=` twin, and is refused ' + + 'as `list-equality` by `os validate`, `os build` and the object save door; the Studio draft check and ' + + 'the shared-validator half of the MCP `validate_expression` tool do not see a hop target\'s fields.', reason: 'CEL compares the whole list with the scalar, so `[\'frontend\', \'qa\'] == \'frontend\'` is `false`, and so ' + 'is `null == \'frontend\'`: the expression answers `false` (`true` for `!=`) on every row whatever the list '