Skip to content

Commit 61a4cdb

Browse files
committed
fix(lint)!: a type mismatch in a record-triggered flow condition or an advisory validation rule is served a verdict that says what really happens
- Flow start / edge conditions of a record-triggered flow: refused with the run-fails wording (the run fails, the triggering write lands), the outcome and severity the null-guard twin already has there. - Advisory (`warning` / `info`) validation rules: still refused, as the twin refuses them, but both verdicts now say the rule is skipped and the write proceeds instead of "the write is refused". - One refusal code per outcome: type-mismatch (warning), type-mismatch-fail-closed, type-mismatch-run-fails, type-mismatch-fail-open; the role -> outcome decision lives in one ledger, `faultOutcomeOf`, for both verdicts. - D3 entry flow-condition-type-mismatch-refused; the validation-type-mismatch-refused entry's create answers 201. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzfu5kYQW45XjsF79Rp
1 parent 21510bb commit 61a4cdb

16 files changed

Lines changed: 554 additions & 102 deletions
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
'@objectstack/formula': minor
3+
'@objectstack/lint': minor
4+
'@objectstack/mcp': minor
5+
'@objectstack/spec': minor
6+
---
7+
8+
fix(lint)!: a text-vs-number comparison in a record-triggered flow condition is refused with what really happens (the run fails, the write lands), and in an advisory validation rule the refusal no longer says the write is refused (#29926)
9+
10+
Clause-②: no (narrowing) — the author-time gate (`os build`, `os validate`, the package publish and the flow publish door) refuses a `type-mismatch` in a record-triggered flow's start or edge condition that it used to pass with a warning; and the fail-closed arm of the verdict is served under its own code `type-mismatch-fail-closed` instead of `type-mismatch` with `params.outcome`. Added beside them: the codes `type-mismatch-run-fails` and `type-mismatch-fail-open`, and the exported type `ExprFaultOutcome`.
11+
Metadata-protocol: narrowed
12+
Skills-docs: updated here — `content/docs/data-modeling/formulas.mdx` (the type-soundness row names each outcome) and the MCP skill text in `packages/mcp/src/skill-md.ts` (`flow_condition` now errors too); `skills/**` teaches no text-vs-number comparison in a flow condition or an advisory rule, so no skill or eval moves.
13+
14+
<!-- adr-0087: registered flow-condition-type-mismatch-refused -->
15+
16+
**BREAKING** accept-set narrowing at the author-time expression gate, served by `os build`, `os validate` and `POST /api/v1/packages/:id/publish-drafts`.
17+
18+
- **Record-triggered flow conditions — before.** `name > 5` on a text field, as the start node's `condition` or an edge `condition` of a flow whose start `triggerType` is `record-*`, published with an `expression-invalid` warning saying the expression "silently evaluates to null". It does not: the engine's `evaluateCondition` throws on the fault, so the run fails at that condition, and the record-change trigger isolates the write that fired it, so the write lands and the flow's work is not done.
19+
- **Now.** The same condition is refused as `expression-invalid` at `severity: error`, code `type-mismatch-run-fails`, with a message saying the run fails at the condition and the triggering write still lands. This is the severity and outcome the nullable-comparison verdict already has on those conditions (#29026). A flow that is not record-triggered, and a node `condition` other than the start node's (inert), keep the warning. The `validate_expression` MCP tool answers the same for `site: 'flow_condition'`.
20+
- **Advisory validation rules** (`severity: 'warning'` / `'info'`). Still refused at publish, as the nullable twin refuses them, but under `type-mismatch-fail-open` with a message that says what happens: the rule is skipped, the write proceeds, and the rule never gives its advisory. The nullable twin's message on the same rule now says the same instead of "the write is rejected fail-closed". A `severity: 'error'` rule keeps both refusals and their wording.
21+
- **Refusal codes (`@objectstack/formula`).** A code names one message template, so each outcome has its own: `type-mismatch` (warning, reads null), `type-mismatch-fail-closed`, `type-mismatch-run-fails`, `type-mismatch-fail-open` (all errors). `ExprSchemaHint.faultOutcome` takes `'fail-closed' | 'run-fails' | 'fail-open'` (exported as `ExprFaultOutcome`); the `outcome` param on `type-mismatch` is gone.
22+
- **`@objectstack/spec`.** Upgrade guide entry `flow-condition-type-mismatch-refused` (semantic: no mechanical rewrite). The `validation-type-mismatch-refused` entry's acceptance criteria now say a create answers 201, the data door's status.
23+
24+
**FROM → TO.** Flow condition `name > 5` on a text field → `size(name) > 5` for its length, `score > 5` on a number field for a value, or `double(name) > 5` only when every value is numeric text. A boolean: `active > 0` → `active == true`. A consumer keyed to the code: `type-mismatch` with `params.outcome === 'fail-closed'` → `type-mismatch-fail-closed`.
25+
26+
**Supersedes, in the same release.** The #29904 changeset (`29904-type-mismatch-fail-closed.md`) says the refusal's `params` carry `outcome: 'fail-closed'`, and that flow conditions keep the warning. Both are replaced here: the fail-closed refusal is served as `type-mismatch-fail-closed` with no `outcome` param, and a record-triggered flow's start and edge conditions are refused as above.

‎content/docs/data-modeling/formulas.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ come at two severities: **errors** fail the build; **warnings** are advisory
242242
| **Qualify field references** — a formula / validation / predicate binds only the `record` namespace, so a bare `amount` resolves to nothing and evaluates to `null`. Write `record.amount`. | `amount > 100` → `record.amount > 100` | error |
243243
| **Field must exist** — a typo'd `record.<field>` is flagged with a did-you-mean. | `record.amont` → `record.amount` | error |
244244
| **Unknown function** — a misspelled or nonexistent stdlib call. | `isBlnk(record.x)` | error |
245-
| **Type soundness** — a text or boolean field used with an arithmetic (`+ - * / %`) or ordering (`< > <= >=`) operator against a number faults at runtime unless the text is numeric. In a formula it evaluates to `null`. Where a fault refuses the write instead — a validation rule, a field `requiredWhen` / `readonlyWhen`, an option `visibleWhen`, a hook `condition` — it is an error. Store it as a number field, compare text with text, or convert explicitly (`size(record.name)` for the length; `double(record.name)`, which itself faults on text that is not a number). | `record.title * 2`; `record.name > 5` in a validation rule → `size(record.name) > 5` | warning; error where a fault refuses the write |
245+
| **Type soundness** — a text or boolean field used with an arithmetic (`+ - * / %`) or ordering (`< > <= >=`) operator against a number faults at runtime unless the text is numeric. In a formula it evaluates to `null`. Where a fault does something else it is an error, worded for what happens: a validation rule, a field `requiredWhen` / `readonlyWhen`, an option `visibleWhen` or a hook `condition` refuses the write; a record-triggered flow's start or edge condition fails the run (the write that fired it still lands); an advisory (`severity: 'warning'` / `'info'`) validation rule is skipped, so it never gives its advisory. Store it as a number field, compare text with text, or convert explicitly (`size(record.name)` for the length; `double(record.name)`, which itself faults on text that is not a number). | `record.title * 2`; `record.name > 5` in a validation rule → `size(record.name) > 5` | warning in a formula or a display predicate; error at the sites named |
246246
| **Date arithmetic** — a `date` / `datetime` field used with `+` / `-` against a number (a `YYYY-MM-DD` value is a string at runtime, so this always faulted to `null`). Use the date helpers instead. | `record.end_date - record.start_date + 1` → `daysBetween(record.start_date, record.end_date) + 1`; `today() + 30` → `daysFromNow(30)`; `record.date + n` → `addDays(record.date, n)` | error (#3306) |
247247
| **Date subtraction** — the runtime holds a `date` / `datetime` field as its text (`YYYY-MM-DD`, or an ISO-8601 instant), so subtracting two of them faults and the formula reads `null`; a timestamp minus one computes a duration, which a field reads as `{}`. Use `daysBetween`. A condition may still compare a duration (`today() - record.start_date > duration("72h")`). | `record.end_date - record.start_date` → `daysBetween(record.start_date, record.end_date)`; `today() - record.start_date` → `daysBetween(record.start_date, today())` | error |
248248

‎packages/formula/src/expression-refusal-codes.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,62 @@ const PINS: { readonly [C in ExpressionRefusalCode]: readonly [Pin<C>, ...Pin<C>
371371
'field, or drop the arithmetic/comparison.',
372372
},
373373
],
374+
// [#29926] One code per fault outcome the caller declares (`faultOutcome`):
375+
// the three sentences differ in what the runtime does, so they are three
376+
// codes. `type-mismatch-fail-closed` is the sentence #29904 served as
377+
// `type-mismatch` with `outcome: 'fail-closed'`, byte for byte.
378+
'type-mismatch-fail-closed': [
379+
{
380+
produce: () =>
381+
onlyError('predicate', 'record.name > 5', { scope: 'record', fieldTypes: { name: 'text' }, faultOutcome: 'fail-closed' }),
382+
params: { operands: 'string > int', operator: '>', held: 'text', reference: 'record.name' },
383+
message:
384+
'type mismatch `string > int` — `record.name` holds text but is used with `>` against a number. CEL has no such operation, so ' +
385+
'wherever the value is not numeric text (an empty value included) the expression faults, and here ' +
386+
'an expression that cannot be evaluated fails closed: the write (or the operation a hook guards) is refused, not ' +
387+
'skipped. ' +
388+
'Compare like with like: use a number field, compare text with text, or convert explicitly — `size(record.name)` for its ' +
389+
'length, `double(record.name)` for its value (which itself faults on text that is not a number).',
390+
},
391+
{
392+
produce: () =>
393+
onlyError('predicate', 'record.active > 0', { scope: 'record', fieldTypes: { active: 'boolean' }, faultOutcome: 'fail-closed' }),
394+
params: { operands: 'bool > int', operator: '>', held: 'boolean', reference: 'record.active' },
395+
message:
396+
'type mismatch `bool > int` — `record.active` holds a boolean but is used with `>` against a number. CEL has no such operation, so ' +
397+
'wherever the value is not numeric text (an empty value included) the expression faults, and here ' +
398+
'an expression that cannot be evaluated fails closed: the write (or the operation a hook guards) is refused, not ' +
399+
'skipped. Compare a boolean with `true` / `false`, or use a number field.',
400+
},
401+
],
402+
'type-mismatch-run-fails': [
403+
{
404+
produce: () =>
405+
onlyError('predicate', 'name > 5', { scope: 'flattened', fields: ['name'], fieldTypes: { name: 'text' }, faultOutcome: 'run-fails' }),
406+
params: { operands: 'string > int', operator: '>', held: 'text', reference: 'name' },
407+
message:
408+
'type mismatch `string > int` — `name` holds text but is used with `>` against a number. CEL has no such operation, so ' +
409+
'wherever the value is not numeric text (an empty value included) the expression faults, and here ' +
410+
'a flow condition that cannot be evaluated fails the run: the flow stops at this condition, the write that fired ' +
411+
'it still lands, and the work the flow exists to do is not done. ' +
412+
'Compare like with like: use a number field, compare text with text, or convert explicitly — `size(name)` for its ' +
413+
'length, `double(name)` for its value (which itself faults on text that is not a number).',
414+
},
415+
],
416+
'type-mismatch-fail-open': [
417+
{
418+
produce: () =>
419+
onlyError('predicate', 'record.name > 5', { scope: 'record', fieldTypes: { name: 'text' }, faultOutcome: 'fail-open' }),
420+
params: { operands: 'string > int', operator: '>', held: 'text', reference: 'record.name' },
421+
message:
422+
'type mismatch `string > int` — `record.name` holds text but is used with `>` against a number. CEL has no such operation, so ' +
423+
'wherever the value is not numeric text (an empty value included) the expression faults, and here ' +
424+
'the rule is advisory, so a rule that cannot be evaluated is skipped, not refused: the write proceeds, the rule is ' +
425+
'never applied (its advisory never fires), and the only trace is a log line saying it could not be evaluated. ' +
426+
'Compare like with like: use a number field, compare text with text, or convert explicitly — `size(record.name)` for its ' +
427+
'length, `double(record.name)` for its value (which itself faults on text that is not a number).',
428+
},
429+
],
374430
'field-near-miss': [
375431
{
376432
produce: () => onlyWarning('predicate', "stauts == 'x'", { fields: ['status'], objectName: 'deal' }),
@@ -546,6 +602,15 @@ const SWEEP_SCHEMAS: readonly (ExprSchemaHint | undefined)[] = [
546602
optionFields: ['status'],
547603
},
548604
{ fields: ['status', 'close_date'], objectName: 'deal', fieldTypes: { close_date: 'date' } },
605+
// [#29926] The deal schema once per declared fault outcome, so the sweep
606+
// reaches each `type-mismatch-*` code and holds its params to its message.
607+
...(['fail-closed', 'run-fails', 'fail-open'] as const).map((faultOutcome): ExprSchemaHint => ({
608+
scope: 'record',
609+
objectName: 'deal',
610+
fields: ['amount', 'name', 'active'],
611+
fieldTypes: { name: 'text', active: 'boolean' },
612+
faultOutcome,
613+
})),
549614
{ fields: ['status'] },
550615
{ fieldTypes: { account: 'lookup' }, traversalHydration: true },
551616
];

‎packages/formula/src/expression-refusal.ts‎

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ export type CelFieldRole = Exclude<FieldRole, 'template'>;
5757
*/
5858
export type ExpressionSourceKind = 'array' | 'object' | 'number' | 'boolean' | 'bigint' | 'symbol' | 'function';
5959

60+
/**
61+
* The params every `type-mismatch*` code interpolates: the CEL operand types,
62+
* the `operator`, what the field holds, and the field as written when known.
63+
* The four codes differ in the outcome their sentence states, never in params.
64+
*/
65+
interface TypeMismatchParams {
66+
readonly operands: string;
67+
readonly operator: string;
68+
readonly held: 'text' | 'boolean';
69+
readonly reference?: string;
70+
}
71+
6072
/** Code → the params its message interpolates. The keys ARE the closed set. */
6173
export interface ExpressionRefusalParams {
6274
/** An envelope whose `source` is present but is not text. */
@@ -169,18 +181,30 @@ export interface ExpressionRefusalParams {
169181
*/
170182
'date-duration-value': { readonly reference?: string };
171183
/**
172-
* A text or boolean field used with an arithmetic or ordering `operator`
173-
* against a number. A WARNING (the expression reads null) unless `outcome`
174-
* is `'fail-closed'`: the site refuses the write it guards when the
175-
* expression faults, and the verdict is an ERROR (#29904).
184+
* WARNING, never an error: a text or boolean field used with an arithmetic
185+
* or ordering `operator` against a number, at a site where a fault reads
186+
* null (a formula field) or whose fault outcome the caller did not declare.
176187
*/
177-
'type-mismatch': {
178-
readonly operands: string;
179-
readonly operator: string;
180-
readonly held: 'text' | 'boolean';
181-
readonly reference?: string;
182-
readonly outcome?: 'fail-closed';
183-
};
188+
'type-mismatch': TypeMismatchParams;
189+
/**
190+
* [#29926] ERROR: the same mismatch at a site that FAILS CLOSED — the
191+
* expression faulting refuses the write (or aborts the operation a hook
192+
* guards). Served as `type-mismatch` with `outcome: 'fail-closed'` by #29904
193+
* until this code gave the sentence its own name.
194+
*/
195+
'type-mismatch-fail-closed': TypeMismatchParams;
196+
/**
197+
* [#29926] ERROR: the same mismatch in a record-triggered flow's start or
198+
* edge condition — the run fails at the condition, the write that fired it
199+
* lands.
200+
*/
201+
'type-mismatch-run-fails': TypeMismatchParams;
202+
/**
203+
* [#29926] ERROR: the same mismatch in an advisory (`warning` / `info`)
204+
* validation rule — the rule is skipped, the write proceeds, and the
205+
* advisory never fires.
206+
*/
207+
'type-mismatch-fail-open': TypeMismatchParams;
184208
/**
185209
* WARNING, never an error: a bare identifier in a flattened condition that
186210
* is one typo away from a field. Absent `objectName` reads "the trigger object".
@@ -243,6 +267,9 @@ const CODE_TABLE = {
243267
'date-text-arithmetic': true,
244268
'date-duration-value': true,
245269
'type-mismatch': true,
270+
'type-mismatch-fail-closed': true,
271+
'type-mismatch-run-fails': true,
272+
'type-mismatch-fail-open': true,
246273
'field-near-miss': true,
247274
'traversal-bare-and-traversed': true,
248275
'traversal-multi-hop': true,

‎packages/formula/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export type { UnknownFunctionCall } from './unknown-function';
165165
// ADR-0032 — shared validator + introspection (one validator for build,
166166
// registration, and the agent-callable validate_expression tool).
167167
export { validateExpression, introspectScope, expectedDialect, inferExpressionType, nearestName, CEL_STDLIB_FUNCTIONS } from './validate';
168-
export type { FieldRole, ExprInput, ExprSchemaHint, ExprValidationError, ExprValidationResult, InferredValueType } from './validate';
168+
export type { FieldRole, ExprInput, ExprSchemaHint, ExprFaultOutcome, ExprValidationError, ExprValidationResult, InferredValueType } from './validate';
169169
// #22727 — the declaration authoring stamps onto a formula field: its
170170
// `returnType`, and for a provably-money result its currency, copied from the
171171
// source field's own `currencyConfig`. The producer behind `returnType: 'currency'`.

‎packages/formula/src/validate.test.ts‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -771,20 +771,50 @@ describe('validateExpression (ADR-0032)', () => {
771771
expect(r.ok).toBe(false);
772772
expect(r.warnings).toHaveLength(0);
773773
expect(r.errors).toHaveLength(1);
774-
expect(r.errors[0]).toMatchObject({
775-
code: 'type-mismatch',
776-
params: { operands: 'string > int', operator: '>', held: 'text', reference: 'record.name', outcome: 'fail-closed' },
774+
// [#29926] Its own code: the fail-closed sentence is not the warning's.
775+
expect(r.errors[0]).toEqual({
776+
source: 'record.name > 5',
777+
message: expect.any(String),
778+
code: 'type-mismatch-fail-closed',
779+
params: { operands: 'string > int', operator: '>', held: 'text', reference: 'record.name' },
777780
});
778781
expect(r.errors[0].message).not.toMatch(/evaluates to null/);
779782
const bool = validateExpression('predicate', 'record.is_active > 0', { ...schema, faultOutcome: 'fail-closed' });
780-
expect(bool.errors.map((e) => e.code)).toEqual(['type-mismatch']);
783+
expect(bool.errors.map((e) => e.code)).toEqual(['type-mismatch-fail-closed']);
781784
// The conversions the message names validate clean in this dialect.
782785
for (const fixed of ['size(record.name) > 5', 'double(record.name) > 5']) {
783786
const ok = validateExpression('predicate', fixed, { ...schema, faultOutcome: 'fail-closed' });
784787
expect([ok.errors, ok.warnings], fixed).toEqual([[], []]);
785788
}
786789
});
787790

791+
// [#29926] The two other outcomes a caller can declare, each an error
792+
// under its own code, worded for what that runtime does.
793+
it('errors with the run-fails wording where the caller declares a flow condition (#29926)', () => {
794+
const flat = { ...schema, scope: 'flattened' as const, faultOutcome: 'run-fails' as const };
795+
const r = validateExpression('predicate', 'name > 5', flat);
796+
expect(r.warnings).toHaveLength(0);
797+
expect(r.errors.map((e) => [e.code, e.params])).toEqual([
798+
['type-mismatch-run-fails', { operands: 'string > int', operator: '>', held: 'text', reference: 'name' }],
799+
]);
800+
expect(r.errors[0].message).toContain('fails the run');
801+
expect(r.errors[0].message).toContain('the write that fired it still lands');
802+
expect(r.errors[0].message).not.toMatch(/evaluates to null|is refused/);
803+
for (const fixed of ['size(name) > 5', 'double(name) > 5']) {
804+
const ok = validateExpression('predicate', fixed, flat);
805+
expect([ok.errors, ok.warnings], fixed).toEqual([[], []]);
806+
}
807+
});
808+
809+
it('errors with the skipped-advisory wording where the caller declares fail-open (#29926)', () => {
810+
const r = validateExpression('predicate', 'record.name > 5', { ...schema, faultOutcome: 'fail-open' });
811+
expect(r.warnings).toHaveLength(0);
812+
expect(r.errors.map((e) => e.code)).toEqual(['type-mismatch-fail-open']);
813+
expect(r.errors[0].message).toContain('the write proceeds');
814+
expect(r.errors[0].message).toContain('its advisory never fires');
815+
expect(r.errors[0].message).not.toMatch(/evaluates to null|is refused/);
816+
});
817+
788818
it('warns on a text field ordered against a number', () => {
789819
const r = validateExpression('predicate', 'record.title >= 5', schema);
790820
expect(r.ok).toBe(true);

0 commit comments

Comments
 (0)