Skip to content

Commit 6a3a1e2

Browse files
committed
fix(lint): a flow variable's bare name is not typed as the field, and a time-relative sweep is graded like a record trigger
A flow run seeds its declared variables and node outputs before it flattens the record, and the flattening skips a bound name, so a bare read of such a name holds the variable: the type check no longer types it as the field it shadows (new optional ExprSchemaHint.flowVariables), while record.NAME and previous.NAME stay typed. A time-relative sweep launches each run with the swept row as its record, so a sweep of the object the conditions are typed against gets run-fails at the start, edge and decision sites, in the lint ledger and the MCP flow_condition map alike. The run-may-fail warning no longer asserts the flow's kind. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzf4UmgvS2cusNbWnvy
1 parent 5a5fd5b commit 6a3a1e2

12 files changed

Lines changed: 298 additions & 64 deletions

‎.changeset/29959-flow-condition-type-mismatch-every-site.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
'@objectstack/spec': minor
66
---
77

8-
fix(lint)!: every flow condition's text-vs-number comparison gets a verdict that says what the run does — a record-triggered flow's `record.<field>` read and `decision` branch are refused like its bare name, and any other flow's warning says the run fails instead of "evaluates to null" (#29959)
8+
fix(lint)!: every flow condition's text-vs-number comparison gets a verdict that says what the run does — a record-triggered flow's `record.<field>` read and `decision` branch are refused like its bare name, a time-relative sweep of the flow's object is refused like a record-triggered flow, a bare name a flow variable holds is no longer typed as the field, and any other flow's warning says the run fails instead of "evaluates to null" (#29959)
99

10-
Clause-②: yes (narrowing) — the author-time gate (`os build`, `os validate`, the package publish and the flow publish door) refuses, on a record-triggered flow, a `type-mismatch` in a `decision` branch and in a `record.<field>` / `previous.<field>` read of a start or edge condition, which it passed with no verdict before; and a flow condition's `type-mismatch` warning is served under its own code `type-mismatch-run-may-fail` instead of `type-mismatch`. Added beside them (the widening half): the optional `severity` and `triggerKind` inputs of the `validate_expression` MCP tool, and the code `type-mismatch-run-may-fail` in `@objectstack/formula`'s `ExpressionRefusalCode`.
10+
Clause-②: yes (narrowing) — the author-time gate (`os build`, `os validate`, the package publish and the flow publish door) refuses, on a record-triggered flow, a `type-mismatch` in a `decision` branch and in a `record.<field>` / `previous.<field>` read of a start or edge condition, which it passed with no verdict before; refuses the same on a time-relative flow sweeping the object its conditions are typed against, where 18.3.0 served a warning; and a flow condition's `type-mismatch` warning is served under its own code `type-mismatch-run-may-fail` instead of `type-mismatch`. Added beside them (the widening half): the optional `severity` and `triggerKind` inputs of the `validate_expression` MCP tool, the code `type-mismatch-run-may-fail` in `@objectstack/formula`'s `ExpressionRefusalCode`, the optional `flowVariables` key of `@objectstack/formula`'s `ExprSchemaHint`, and a bare flow-condition name that a declared flow variable or node id holds is no longer typed as the field it shadows (no verdict where 18.3.0 served a `type-mismatch` warning).
1111
Metadata-protocol: narrowed
1212
Skills-docs: updated here — `content/docs/data-modeling/formulas.mdx` (the type-soundness row names the decision branch, the `record.<field>` read and the warning on other flows) and the MCP skill text in `packages/mcp/src/skill-md.ts` (the two new `validate_expression` inputs); `skills/**` teaches no text-vs-number comparison in a flow condition (grep for `type mismatch`, `evaluates to null`, `validate_expression` and the decision examples), so no skill or eval moves.
1313

@@ -17,8 +17,10 @@ Skills-docs: updated here — `content/docs/data-modeling/formulas.mdx` (the typ
1717

1818
- **Record-triggered flows — before.** The start and edge conditions refused `name > 5` on a text field (`type-mismatch-run-fails`), but `record.name > 5` in the same slot, and `name > 5` in a `decision` node's `config.conditions[].expression`, published with no verdict at all. Both fail the run the same way: the engine's `evaluateCondition` throws on the fault, the run binds the trigger record both as `record` and flattened to bare names, and a decision branch is evaluated over the same variables as an edge.
1919
- **Now.** Both are refused as `expression-invalid` at `severity: error`, code `type-mismatch-run-fails`, with the message the bare name already gets: the run fails at the condition and the triggering write still lands. `previous.<field>` is judged the same way. The date verdicts stay on the bare name only, as before.
20-
- **Any other flow** (no `triggerType`, a `schedule` or time-relative flow, an `api` flow) and an inert node `condition` keep a warning. Its text said the expression "silently evaluates to null", which is false on every flow: a condition that faults fails the run. It is now `type-mismatch-run-may-fail`, worded for the failed run, and a warning because such a flow is handed whatever its entrance carries (an API request body, an action's partial record, a parent flow's record, or nothing), so the field's type is not proven to be what the name holds. A `record.<field>` read and a `decision` branch there get the same warning. The flow registration log line reads the same text.
21-
- **`validate_expression` (MCP).** Two optional inputs, so the tool answers what the gate will: `severity` (`error` | `warning` | `info`, site `validation`; default `error`) gives an advisory rule the gate's `type-mismatch-fail-open` instead of the fail-closed verdict, and `triggerKind` (`record_change` | `time_relative` | `schedule` | `api` | `none`, site `flow_condition`; default `record_change`) gives a flow that is not record-triggered the gate's warning instead of the run-fails error. Either input on a site it does not describe is refused as a tool error. A call that passes neither answers as before.
20+
- **Time-relative flows.** A sweep launches each run with the swept row as its record, so on a flow whose `timeRelative.object` is the start node's `objectName` (or is omitted) the start, edge and `decision` conditions get the record-triggered verdict above, bare name included: `type-mismatch-run-fails`, where 18.3.0 served a `type-mismatch` warning. A sweep of another object keeps the warning below.
21+
- **Any other flow** (no `triggerType`, a `schedule` flow, an `api` flow) and an inert node `condition` keep a warning. Its text said the expression "silently evaluates to null", which is false on every flow: a condition that faults fails the run. It is now `type-mismatch-run-may-fail`, worded for the failed run, and a warning because such a flow is handed whatever its entrance carries (an API request body, an action's partial record, a parent flow's record, or nothing), so the field's type is not proven to be what the name holds. A `record.<field>` read and a `decision` branch there get the same warning. The flow registration log line reads the same text.
22+
- **A bare name a flow variable holds.** The run seeds a flow's declared variables (and each node's outputs, under its id) before it flattens the record, and the flattening skips a name already bound. So in a flow that declares `variables: [{ name: 'title', type: 'number' }]` over a text field `title`, a bare `title > 5` reads the number and runs; it was refused as `type-mismatch-run-fails` on the head that added that error, and warned on 18.3.0. It now gets no type verdict (the `flow-variable-shadows-field` warning still names the shadowing). `record.title > 5` reads the field and stays refused.
23+
- **`validate_expression` (MCP).** Two optional inputs, so the tool answers what the gate will: `severity` (`error` | `warning` | `info`, site `validation`; default `error`) gives an advisory rule the gate's `type-mismatch-fail-open` instead of the fail-closed verdict, and `triggerKind` (`record_change` | `time_relative` | `schedule` | `api` | `none`, site `flow_condition`; default `record_change`) gives a `schedule`, `api` or `none` flow the gate's warning instead of the run-fails error (`time_relative` answers run-fails, its record being a row of `objectName`). Either input on a site it does not describe is refused as a tool error. A call that passes neither answers as before.
2224
- **`@objectstack/spec`.** Upgrade guide entry `flow-record-read-and-decision-type-mismatch-refused` (semantic: no mechanical rewrite).
2325

24-
**FROM → TO.** In a record-triggered flow: `record.name > 5` (or a decision branch `name > 5`) on a text field → `size(record.name) > 5` for its length, `record.score > 5` on a number field for a value, or `double(record.name) > 5` only when every value is numeric text. A boolean: `record.active > 0` → `record.active == true`. A consumer keyed to the code of a flow condition's warning: `type-mismatch` → `type-mismatch-run-may-fail` (`@objectstack/formula@18.3.0` and `@objectstack/mcp@18.3.0` serve `type-mismatch` there; the record-scoped `type-mismatch`, on a formula field or a display predicate, is unchanged).
26+
**FROM → TO.** In a record-triggered flow, or a time-relative flow sweeping its own object: `record.name > 5` (or a decision branch `name > 5`) on a text field → `size(record.name) > 5` for its length, `record.score > 5` on a number field for a value, or `double(record.name) > 5` only when every value is numeric text. A boolean: `record.active > 0` → `record.active == true`. A consumer keyed to the code of a flow condition's warning: `type-mismatch` → `type-mismatch-run-may-fail` (`@objectstack/formula@18.3.0` and `@objectstack/mcp@18.3.0` serve `type-mismatch` there; the record-scoped `type-mismatch`, on a formula field or a display predicate, is unchanged).

‎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 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, edge or `decision` branch condition fails the run (the write that fired it still lands), whether it reads the field bare or as `record.<field>`; 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, and in a condition of a flow that is not record-triggered, where the warning says the run fails if the condition faults (what such a flow binds under the name depends on how it is started); error at the sites named |
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, edge or `decision` branch condition fails the run (the write that fired it still lands), whether it reads the field bare or as `record.<field>`, and so does a time-relative flow's that sweeps the same object; a bare name that a declared flow variable holds reads the variable, so it is not judged as the field; 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, and in a condition of any other flow, where the warning says the run fails if the condition faults (what such a flow binds under the name depends on how it is started); 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/cel-engine.ts‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,14 @@ function buildTypedEnv(
966966
fieldCelTypes: Readonly<Record<string, FieldCelType>>,
967967
scope: 'record' | 'flattened',
968968
temporalAsText = false,
969-
flattenedRecordRoots = false,
969+
/**
970+
* [#29959] Flattened scope only: the field types to type `record.<field>` /
971+
* `previous.<field>` with. Absent, those roots stay `map` (dyn members). It
972+
* is a map of its own, not `fieldCelTypes`, because a bare name a flow
973+
* variable holds is left out of the bare typing while `record.<name>` still
974+
* reads the field.
975+
*/
976+
recordRootTypes?: Readonly<Record<string, FieldCelType>>,
970977
): Environment {
971978
// `temporalAsText` declares a `date`/`datetime` field as the text the runtime
972979
// holds ({@link DATE_TEXT_CEL_TYPE}) instead of a Timestamp — see
@@ -982,13 +989,13 @@ function buildTypedEnv(
982989
registerStdLib(env, () => new Date(0));
983990
registerNullComparisons(env);
984991
if (temporalAsText) registerDateTextOverloads(env);
985-
if (flattenedRecordRoots) {
992+
if (recordRootTypes) {
986993
// [#29959] A flow run binds the record it was handed as `record` too
987994
// (`seedRunVariables`), beside the flattened bare names, so
988995
// `record.<field>` is the same value as the bare `<field>` and is typed
989996
// the same: the `record`-scope struct, on `record` and `previous`.
990997
const fields: Record<string, string> = {};
991-
for (const [name, t] of Object.entries(fieldCelTypes)) fields[name] = typeName(t);
998+
for (const [name, t] of Object.entries(recordRootTypes)) fields[name] = typeName(t);
992999
try { env.registerType('OsRecordScope', { fields }); } catch { /* invalid field name — ignore */ }
9931000
for (const root of ['record', 'previous']) {
9941001
try { env.registerVariable(root, 'OsRecordScope'); } catch { /* duplicate — ignore */ }
@@ -1051,6 +1058,20 @@ function offendingField(
10511058
return null;
10521059
}
10531060

1061+
/**
1062+
* [#29959] `fieldCelTypes` without `names` — the bare typing of a flattened
1063+
* condition whose run binds those names as variables, which win over the
1064+
* field (see {@link firstTypeMismatch}'s `bareShadowed`).
1065+
*/
1066+
function withoutNames(
1067+
fieldCelTypes: Readonly<Record<string, FieldCelType>>,
1068+
names: ReadonlySet<string>,
1069+
): Record<string, FieldCelType> {
1070+
const out: Record<string, FieldCelType> = {};
1071+
for (const [name, t] of Object.entries(fieldCelTypes)) if (!names.has(name)) out[name] = t;
1072+
return out;
1073+
}
1074+
10541075
/**
10551076
* [#29959] The `record.<field>` / `previous.<field>` read of a field of
10561077
* `celType` in a flattened condition, as written — what
@@ -1106,11 +1127,18 @@ function offendingRecordRead(
11061127
* `scope` selects how fields are bound: `'record'` (default) for
11071128
* `record.<field>` sites; `'flattened'` for bare-field flow/automation
11081129
* conditions.
1130+
*
1131+
* `bareShadowed` (flattened scope only, #29959): names the run binds BEFORE it
1132+
* flattens the record's fields — a flow's declared variables and node ids.
1133+
* `seedRunVariables` seeds those first and the flattening skips a bound name,
1134+
* so a bare read of one holds the variable, not the field: it stays `dyn`
1135+
* here. `record.<name>` still reads the field and stays typed.
11091136
*/
11101137
export function firstTypeMismatch(
11111138
source: string,
11121139
fieldCelTypes: Readonly<Record<string, FieldCelType>>,
11131140
scope: 'record' | 'flattened' = 'record',
1141+
bareShadowed?: ReadonlySet<string>,
11141142
): {
11151143
operator: string;
11161144
operands: string;
@@ -1139,8 +1167,11 @@ export function firstTypeMismatch(
11391167
m[1] === 'string' || m[1] === 'bool' ? (m[1] as FieldCelType)
11401168
: m[3] === 'string' || m[3] === 'bool' ? (m[3] as FieldCelType)
11411169
: null;
1170+
const bareTypes = scope === 'flattened' && bareShadowed && bareShadowed.size > 0
1171+
? withoutNames(fieldCelTypes, bareShadowed)
1172+
: fieldCelTypes;
11421173
try {
1143-
const m = unsoundOverload(buildTypedEnv(fieldCelTypes, scope));
1174+
const m = unsoundOverload(buildTypedEnv(bareTypes, scope));
11441175
if (m) {
11451176
const operator = m[2];
11461177
const operands = `${m[1]} ${operator} ${m[3]}`;
@@ -1150,15 +1181,15 @@ export function firstTypeMismatch(
11501181
|| (NUMERIC_CEL_TYPES.has(m[1]) && DATE_CEL_TYPES.has(m[3])))) {
11511182
return {
11521183
operator, operands, celType: 'timestamp', category: 'date-arith',
1153-
field: offendingField(source, fieldCelTypes, 'timestamp', scope),
1184+
field: offendingField(source, bareTypes, 'timestamp', scope),
11541185
};
11551186
}
11561187
// #1928 — text/boolean field in arithmetic/ordering against a number: warning.
11571188
const celType = textOrBool(m);
11581189
if (!celType) return null;
11591190
return {
11601191
operator, operands, celType, category: 'type-mismatch',
1161-
field: offendingField(source, fieldCelTypes, celType, scope),
1192+
field: offendingField(source, bareTypes, celType, scope),
11621193
};
11631194
}
11641195
// [#29959] A flattened condition reads the same field as `record.<field>`
@@ -1167,7 +1198,7 @@ export function firstTypeMismatch(
11671198
// the same verdict the bare name gets. The date verdicts stay the bare
11681199
// name's alone, as before.
11691200
if (scope !== 'flattened' || !/(?<![\w$.])(?:record|previous)\s*\./.test(source)) return null;
1170-
const r = unsoundOverload(buildTypedEnv(fieldCelTypes, 'flattened', false, true));
1201+
const r = unsoundOverload(buildTypedEnv(bareTypes, 'flattened', false, fieldCelTypes));
11711202
const celType = r ? textOrBool(r) : null;
11721203
if (!r || !celType) return null;
11731204
const read = offendingRecordRead(source, fieldCelTypes, celType);

0 commit comments

Comments
 (0)