Skip to content
Merged
31 changes: 31 additions & 0 deletions .changeset/35295-date-text-mixed-spelling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
'@objectstack/formula': minor
'@objectstack/spec': minor
---

fix(formula)!: a flow condition's date arithmetic that mixes a bare date field with `record.FIELD` / `previous.FIELD`, and a field read by index (`record['FIELD']`), now take the verdicts their consistent spellings already took

Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: none — `skills/objectstack-formula/SKILL.md` already states "The build refuses date arithmetic (`end - start`, `date + n`)" and that a `date` / `datetime` field is refused in a timestamp-only call, naming no spelling those rules are limited to, so they now hold for the mixed and index spellings as written; no skill teaches the index spelling

<!-- adr-0087: registered flow-condition-mixed-or-index-spelling-date-text-refused -->

**BREAKING** accept-set narrowing. In a flow condition (a start, edge or `decision` branch condition) the run binds the record's fields under their bare names and also as `record`, with the row before the write as `previous`. A `date` or `datetime` field reaches the run as its text, so subtracting one from another faults on every run and fails the flow. Two spellings of that mistake passed `os validate` and the publish door with no finding:

- **Mixed operands.** `due_date - record.borrow_date > duration('24h')`, `previous.due_date - due_date > duration('24h')`. The all-bare (`due_date - borrow_date …`) and all-root (`record.due_date - record.borrow_date …`) spellings were refused already. The check typed the bare names and the root reads in two separate passes, each leaving the other spelling untyped; they are now typed against the object's field table in one pass.
- **Index reads.** `timestamp(record['borrow_date']) < now()`, `record['due_date'] - record['borrow_date'] > duration('24h')`, `isoDate(record['picked_up_at']) == '2026-11-16'`, `record['due_date'] + 1 > 5`. The record-root date-text pass only looked at the member spelling; it now has a gate of its own that admits an index read of `record` or `previous`, so the index read takes the date-text verdicts the member read takes, named by its member spelling, and those only (`record['due_date'] + 1 > 5` takes `date-text-arithmetic`, where its member spelling takes `date-arithmetic`; both prescribe `daysBetween` / `addDays`). The text and boolean mismatch keeps its member-only gate: through an index read, `record['title'] > 5` and an ordering against a text field (`record['due_date'] > record['title']`) are judged exactly as before, and no refusal an expression had before is stood down to a warning.

- **The verdict.** The code, outcome arm and words of the consistent spelling: `date-text-arithmetic-run-fails` on a record-triggered flow or a sweep of the typed object, `-run-may-fail` elsewhere. A mixed verdict that rests on a bare name takes the bare name's `-run-may-fail` arm where a node can rebind it.
- **Where.** `os validate` / `os build` (`validateStackExpressions`) report an `error`, and the flow publish door refuses the publish with `INVALID_METADATA` (422). `registerFlow` logs it as an advisory and still registers the flow, so a stored flow keeps its run-time behaviour until it is next published.
- **Not refused.** An operand the field table does not type keeps passing: a flow variable or node output holding an instant (`cutoff - record.due_date > duration('24h')`, `get_policy.effective_at - record.due_date > duration('24h')`), a bare name a flow variable shadows, a concatenation of the date text (`prefix + record.due_date`); through an index read, the text or boolean mismatch, an ordering against a text field and equality. The run-time evaluator is unchanged.

## FROM → TO

| FROM | TO |
|:---|:---|
| `due_date - record.borrow_date > duration('24h')` | `daysBetween(record.borrow_date, due_date) > 1` |
| `timestamp(record['borrow_date']) < now()` | `date(record['borrow_date']) < now()` |
| `record['due_date'] - record['borrow_date'] > duration('24h')` | `daysBetween(record['borrow_date'], record['due_date']) > 1` |

One-line fix: in a flow condition, never subtract one date field from another or hand one to a timestamp-only call, whichever way it is spelled; use `daysBetween(a, b)` for a span, `addDays(d, n)` to shift a date and `date(…)` to parse it.
29 changes: 24 additions & 5 deletions packages/formula/src/cel-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1261,26 +1261,45 @@ function withoutNames(

/**
* [#29959, #34662] A `record.` / `previous.` read spelled member-wise — the gate
* of every flattened record-root pass ({@link firstTypeMismatch}'s and
* `validate.ts`'s date-text one), so the two can never disagree on whether a
* source reads a root.
* of {@link firstTypeMismatch}'s flattened record-root pass. [#35295] It stays
* member-only: that pass answers before the date-text family is asked, so
* opening it to the index spelling would hand an index read the text/boolean
* mismatch verdict, which can stand a date-text refusal down to a warning and
* refuses an ordering against a text field the runtime answers. The date-text
* family has its own gate, {@link RECORD_ROOT_DATE_TEXT_READ_RE}.
*/
export const RECORD_ROOT_READ_RE = /(?<![\w$.])(?:record|previous)\s*\./;

/**
* [#35295] The gate of `validate.ts`'s flattened record-root date-text pass: a
* `record.` / `previous.` read spelled member-wise or by index
* (`record['borrow_date']`, `previous["due_date"]`). The typed env types the
* index read as the member read and the run hands both the same text, so the
* date-text verdicts — and only those — reach both spellings.
*/
export const RECORD_ROOT_DATE_TEXT_READ_RE = /(?<![\w$.])(?:record|previous)\s*[.[]/;

/**
* [#29959] The `record.<field>` / `previous.<field>` read of a field of
* `celType` in a flattened condition, as written — what
* {@link firstTypeMismatch}'s record-root pass names. `null` when no such read
* is spelled member-wise (an index read, say), so the message names no field.
* [#35295] With `indexReads` (the date-text pass, which
* {@link RECORD_ROOT_DATE_TEXT_READ_RE} opens to the index spelling) an index
* read (`record['due_date']`) is named too, by its member spelling
* (`record.due_date`) — as {@link typedFieldRef} names it for a call — never as
* the bare name its quoted key would otherwise match.
*/
function offendingRecordRead(
source: string,
fieldCelTypes: Readonly<Record<string, FieldCelType>>,
celType: FieldCelType,
indexReads = false,
): { field: string; root: 'record' | 'previous' } | null {
for (const [name, t] of Object.entries(fieldCelTypes)) {
if (t !== celType) continue;
const m = new RegExp(`(?<![\\w$.])(record|previous)\\.${name}(?![\\w$])`).exec(source);
const index = indexReads ? `|\\s*\\[\\s*(['"])${name}\\2\\s*\\]` : '';
const m = new RegExp(`(?<![\\w$.])(record|previous)(?:\\.${name}(?![\\w$])${index})`).exec(source);
if (m) return { field: name, root: m[1] as 'record' | 'previous' };
}
return null;
Expand Down Expand Up @@ -1588,7 +1607,7 @@ export function firstDateTextVerdict(
| { valid?: boolean; type?: unknown; error?: { message?: string } }
| undefined;
if (!result) return null;
const read = rootTypes ? offendingRecordRead(source, rootTypes, 'timestamp') : null;
const read = rootTypes ? offendingRecordRead(source, rootTypes, 'timestamp', true) : null;
const named = read
? { field: read.field, root: read.root }
: { field: offendingField(source, fieldCelTypes, 'timestamp', scope) };
Expand Down
Loading
Loading