Skip to content

Commit e90a02e

Browse files
committed
Merge remote-tracking branch 'origin/main' into claude/issue-34682-bundle-member-selector
2 parents d226b31 + c809deb commit e90a02e

7 files changed

Lines changed: 583 additions & 27 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
'@objectstack/formula': minor
3+
---
4+
5+
fix(formula)!: a flow condition that hands `timestamp()` a date field through `record.FIELD` or `previous.FIELD` is now refused, as the bare field name already was
6+
7+
Clause-②: no (narrowing)
8+
Metadata-protocol: narrowed
9+
Skills-docs: none — `skills/objectstack-formula/SKILL.md` already states the `date-text-call` rule (a `date` / `datetime` field handed to a timestamp-only call, and `timestamp()` over a `date` field) with `record.FIELD` spellings and names no site it is limited to, so it now holds for a flow condition's root reads as written
10+
11+
<!-- adr-0087: not-required (already-registered formula-timestamp-date-only-refused, formula-date-text-call-refused, formula-date-subtraction-refused) The three semantic entries already carry these verdicts, their codes and their rewrites to the upgrade guide, and each entry's surface already names flow conditions among the CEL predicates judged with the object's field types, with record.FIELD spellings as its examples. This change makes that text true for a flow condition that reads the field through record or previous, where only the bare field name was judged before; the verdicts, codes, rewrites and acceptance criteria are the entries' own. No id is added, no key is added, removed or re-shaped, and nothing an author wrote is rewritten. -->
12+
13+
**BREAKING** accept-set narrowing, shipped as `minor`. 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`. The date-text checks judged only the bare name. `timestamp(borrow_date) < now()` over a `date` field was refused, but `timestamp(record.borrow_date) < now()` passed `os validate` and the publish door. The flow then failed on every run, because a date field reaches the run as its `YYYY-MM-DD` text, which `timestamp()` does not parse.
14+
15+
- **The verdicts.** A `record.FIELD` / `previous.FIELD` read in a flow condition now gets the date-text checks its bare name gets, with the same code and outcome arm (`date-text-call-run-fails` / `date-text-arithmetic-run-fails` on a record-triggered flow or a sweep of the typed object, `-run-may-fail` elsewhere). The message names the read as written and prescribes `date(record.FIELD)`, or `isoDate(addDays(record.FIELD, N))` to shift by whole days. This covers the calls the text cannot take (`timestamp(record.FIELD)` over a `date` field, `isoDate(record.FIELD)` over a `date` or `datetime` field) and arithmetic on the text (`record.end_date - record.start_date`).
16+
- **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, as it does for the bare name, and still registers the flow, so a stored flow keeps running until it is next published.
17+
- **Not refused.** `timestamp()` over a `datetime` field. A shape the Timestamp typing already faults on (`record.due_date + 1`), which is another verdict's. An `input.FIELD` read. The run-time evaluator is unchanged.
18+
19+
## FROM → TO
20+
21+
| FROM | TO |
22+
|:---|:---|
23+
| `timestamp(record.borrow_date) < now()` | `date(record.borrow_date) < now()` |
24+
| `timestamp(previous.borrow_date) < now()` | `date(previous.borrow_date) < now()` |
25+
| `isoDate(record.picked_up_at) == '2026-11-16'` | `isoDate(date(record.picked_up_at)) == '2026-11-16'` |
26+
27+
One-line fix: in a flow condition, parse a date field with `date(record.FIELD)` before comparing it with a timestamp, never `timestamp(record.FIELD)`.

‎packages/formula/src/cel-engine.ts‎

Lines changed: 89 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,14 @@ function withoutNames(
12551255
return out;
12561256
}
12571257

1258+
/**
1259+
* [#29959, #34662] A `record.` / `previous.` read spelled member-wise — the gate
1260+
* of every flattened record-root pass ({@link firstTypeMismatch}'s and
1261+
* `validate.ts`'s date-text one), so the two can never disagree on whether a
1262+
* source reads a root.
1263+
*/
1264+
export const RECORD_ROOT_READ_RE = /(?<![\w$.])(?:record|previous)\s*\./;
1265+
12581266
/**
12591267
* [#29959] The `record.<field>` / `previous.<field>` read of a field of
12601268
* `celType` in a flattened condition, as written — what
@@ -1408,9 +1416,10 @@ export function firstTypeMismatch(
14081416
// [#29959] A flattened condition reads the same field as `record.<field>`
14091417
// (and `previous.<field>`), which the bare typing above leaves `dyn`. A
14101418
// second pass types those roots too, for the text/boolean mismatch only:
1411-
// the same verdict the bare name gets. The date verdicts stay the bare
1412-
// name's alone, as before.
1413-
if (scope !== 'flattened' || !/(?<![\w$.])(?:record|previous)\s*\./.test(source)) return null;
1419+
// the same verdict the bare name gets. The `date-arith` verdict stays the
1420+
// bare name's alone, as before; the date-text family reaches the roots
1421+
// through `validate.ts`'s record-root pass (#34662).
1422+
if (scope !== 'flattened' || !RECORD_ROOT_READ_RE.test(source)) return null;
14141423
const rootTyped = unsoundOverload(buildTypedEnv(bareTypes, 'flattened', 'timestamp', fieldCelTypes));
14151424
const r = rootTyped && textOrBool(rootTyped) ? confirmAgainstDateText(rootTyped, fieldCelTypes) : null;
14161425
const celType = r ? textOrBool(r) : null;
@@ -1510,10 +1519,15 @@ function registerDateTextAnswersOnString(env: Environment): void {
15101519
* answer, so the expression faults and the field reads null;
15111520
* - `type` — the expression checks clean, and `type` is the CEL type it
15121521
* computes (`google.protobuf.Duration` for `today() - record.start_date`).
1522+
* `root` is set on the flattened record-root pass (`recordRootTypes`, #34662)
1523+
* when `field` is named by its `record.<field>` / `previous.<field>` read.
15131524
*/
15141525
export type DateTextVerdict =
1515-
| { readonly kind: 'fault'; readonly operator: string; readonly operands: string; readonly field: string | null }
1516-
| { readonly kind: 'type'; readonly type: string; readonly field: string | null };
1526+
| {
1527+
readonly kind: 'fault'; readonly operator: string; readonly operands: string; readonly field: string | null;
1528+
readonly root?: 'record' | 'previous';
1529+
}
1530+
| { readonly kind: 'type'; readonly type: string; readonly field: string | null; readonly root?: 'record' | 'previous' };
15171531

15181532
/**
15191533
* #28944 — type-check `source` with every `date`/`datetime` field declared as
@@ -1535,32 +1549,73 @@ export type DateTextVerdict =
15351549
* Returns `null` when the source does not parse, or the check faults on
15361550
* anything other than arithmetic with a date field's text — that is another
15371551
* check's to report, and this one never guesses.
1552+
*
1553+
* [#34662] `recordRootTypes` (flattened scope only) types a flow condition's
1554+
* `record.<field>` / `previous.<field>` reads as well, the record-root pass
1555+
* {@link firstTypeMismatch} runs for the text/boolean mismatch: the field is
1556+
* then named by its root read (`root`) wherever one is spelled member-wise.
15381557
*/
15391558
export function firstDateTextVerdict(
15401559
source: string,
15411560
fieldCelTypes: Readonly<Record<string, FieldCelType>>,
15421561
scope: 'record' | 'flattened' = 'record',
1562+
recordRootTypes?: Readonly<Record<string, FieldCelType>>,
15431563
): DateTextVerdict | null {
15441564
if (typeof source !== 'string' || !source.trim()) return null;
1565+
const rootTypes = scope === 'flattened' ? recordRootTypes : undefined;
15451566
try {
1546-
const env = buildTypedEnv(fieldCelTypes, scope, 'date-text');
1567+
const env = buildTypedEnv(fieldCelTypes, scope, 'date-text', rootTypes);
15471568
const result = env.parse(rewriteNullableTernary(rewriteIndexedHas(source))).check?.() as
15481569
| { valid?: boolean; type?: unknown; error?: { message?: string } }
15491570
| undefined;
15501571
if (!result) return null;
1551-
const field = offendingField(source, fieldCelTypes, 'timestamp', scope);
1572+
const read = rootTypes ? offendingRecordRead(source, rootTypes, 'timestamp') : null;
1573+
const named = read
1574+
? { field: read.field, root: read.root }
1575+
: { field: offendingField(source, fieldCelTypes, 'timestamp', scope) };
15521576
if (result.valid !== false) {
1553-
return typeof result.type === 'string' ? { kind: 'type', type: result.type, field } : null;
1577+
return typeof result.type === 'string' ? { kind: 'type', type: result.type, ...named } : null;
15541578
}
15551579
const m = UNSOUND_OVERLOAD_RE.exec(result.error?.message ?? '');
15561580
if (!m || !ARITH_OPS.has(m[2]) || (m[1] !== DATE_TEXT_CEL_TYPE && m[3] !== DATE_TEXT_CEL_TYPE)) return null;
1557-
return { kind: 'fault', operator: m[2], operands: `${m[1]} ${m[2]} ${m[3]}`, field };
1581+
return { kind: 'fault', operator: m[2], operands: `${m[1]} ${m[2]} ${m[3]}`, ...named };
15581582
} catch {
15591583
// A parse/other fault is the syntax checker's job (celEngine.compile).
15601584
return null;
15611585
}
15621586
}
15631587

1588+
/**
1589+
* [#34662] Whether the FIRST operator fault the Timestamp typing of a flattened
1590+
* condition's `record.<field>` / `previous.<field>` reads meets (every bare
1591+
* name `dyn`) is date/duration arithmetic against a number ({@link isDateArith})
1592+
* — `record.due_date + 1`. That shape is the `date-arithmetic` family's, and it
1593+
* is exactly where the bare name's own order stops: {@link firstTypeMismatch}
1594+
* answers `date-arith` there and the date-text check is never asked. Any other
1595+
* Timestamp-typing fault — `record.end_date - record.start_date > 3` (a Duration
1596+
* ordered against a number), `record.d >= "2026-01-01"` (the text ordering the
1597+
* runtime answers) — leaves the bare name's mismatch check at null and the
1598+
* date-text check asked, so it does not stand the record-root pass down
1599+
* either. `false` when the source does not parse.
1600+
*/
1601+
export function recordRootReadsFaultDateArith(
1602+
source: string,
1603+
recordRootTypes: Readonly<Record<string, FieldCelType>>,
1604+
): boolean {
1605+
if (typeof source !== 'string' || !source.trim()) return false;
1606+
try {
1607+
const result = buildTypedEnv({}, 'flattened', 'timestamp', recordRootTypes)
1608+
.parse(rewriteNullableTernary(rewriteIndexedHas(source))).check?.() as
1609+
| { valid?: boolean; error?: { message?: string } }
1610+
| undefined;
1611+
if (result?.valid !== false) return false;
1612+
const m = UNSOUND_OVERLOAD_RE.exec(result.error?.message ?? '');
1613+
return m !== null && isDateArith(m);
1614+
} catch {
1615+
return false;
1616+
}
1617+
}
1618+
15641619
/**
15651620
* cel-js's code for a CALL no overload accepts — `isoDate(string)`,
15661621
* `string.getFullYear()` — raised by the checker and the evaluator alike
@@ -1622,24 +1677,39 @@ export interface DateTextCall {
16221677
* wrap the field for, so the rewrite is `date(<field>)` in place of the call,
16231678
* and `shift` carries the whole-day shift the stdlib teaches,
16241679
* `isoDate(addDays(<field>, N))`, which reads the date text as it is.
1680+
*
1681+
* [#34662] `recordRootTypes` (flattened scope only): a flow condition's
1682+
* `record.<field>` / `previous.<field>` reads are typed with these, as
1683+
* {@link firstDateTextVerdict}'s record-root pass types them, and a root read
1684+
* qualifies by them alone (an `input.<field>` read then does not qualify: a
1685+
* flow does not bind `input` to the row the types describe). Absent, every
1686+
* reference is judged by `fieldCelTypes`, exactly as before.
16251687
*/
16261688
export function firstDateTextCall(
16271689
source: string,
16281690
fieldCelTypes: Readonly<Record<string, FieldCelType>>,
16291691
scope: 'record' | 'flattened' = 'record',
16301692
dateOnlyFields: ReadonlySet<string> = new Set(),
1693+
recordRootTypes?: Readonly<Record<string, FieldCelType>>,
16311694
): DateTextCall | null {
16321695
if (typeof source !== 'string' || !source.trim()) return null;
1633-
const dateFields: Record<string, FieldCelType> = {};
1634-
let any = false;
1635-
for (const [name, t] of Object.entries(fieldCelTypes)) {
1636-
dateFields[name] = t === 'timestamp' ? 'timestamp' : 'dyn';
1637-
any ||= t === 'timestamp';
1638-
}
1696+
const textTyping = (types: Readonly<Record<string, FieldCelType>>): Record<string, FieldCelType> =>
1697+
Object.fromEntries(Object.entries(types).map(([name, t]) => [name, t === 'timestamp' ? 'timestamp' : 'dyn']));
1698+
const dateFields = textTyping(fieldCelTypes);
1699+
const rootDateFields = scope === 'flattened' && recordRootTypes ? textTyping(recordRootTypes) : undefined;
1700+
/** The date-text typing a reference is judged by: its root's, on the record-root pass. */
1701+
const typingOf = (written: string): Record<string, FieldCelType> | undefined => {
1702+
if (!rootDateFields) return dateFields;
1703+
const root = written.includes('.') ? written.slice(0, written.indexOf('.')) : undefined;
1704+
return root === undefined ? dateFields : root === 'record' || root === 'previous' ? rootDateFields : undefined;
1705+
};
1706+
const isDate = (name: string, types: Record<string, FieldCelType> | undefined): boolean =>
1707+
types?.[name] === 'timestamp';
1708+
const any = [dateFields, rootDateFields].some((types) => types && Object.values(types).includes('timestamp'));
16391709
if (!any) return null;
1640-
const anyDateOnly = [...dateOnlyFields].some((name) => dateFields[name] === 'timestamp');
1710+
const anyDateOnly = [...dateOnlyFields].some((name) => isDate(name, dateFields) || isDate(name, rootDateFields));
16411711
try {
1642-
const env = buildTypedEnv(dateFields, scope, 'text');
1712+
const env = buildTypedEnv(dateFields, scope, 'text', rootDateFields);
16431713
const parsed = env.parse(rewriteNullableTernary(rewriteIndexedHas(source)));
16441714
// The whole expression checking clean is the common case: nothing to find,
16451715
// unless a date-only field can reach `timestamp()`, which checks clean.
@@ -1655,7 +1725,7 @@ export function firstDateTextCall(
16551725
};
16561726
const dateRef = (node: unknown): string | null => {
16571727
const ref = typedFieldRef(node, scope);
1658-
return ref && dateFields[ref.name] === 'timestamp' ? ref.written : null;
1728+
return ref && isDate(ref.name, typingOf(ref.written)) ? ref.written : null;
16591729
};
16601730
let verdict: DateTextCall | null = null;
16611731
const visit = (v: unknown): void => {
@@ -1672,7 +1742,7 @@ export function firstDateTextCall(
16721742
// [#34363] `timestamp(<date-only field>)`: clean on the text, faults on it.
16731743
if (v.op === 'call' && Array.isArray(args) && args[0] === 'timestamp' && operands?.length === 1) {
16741744
const ref = typedFieldRef(operands[0], scope);
1675-
if (ref && dateFields[ref.name] === 'timestamp' && dateOnlyFields.has(ref.name)) {
1745+
if (ref && isDate(ref.name, typingOf(ref.written)) && dateOnlyFields.has(ref.name)) {
16761746
const rewrite = wrapInDate(operands[0] as CelNode);
16771747
if (checkOf(rewrite) === true) {
16781748
verdict = {

0 commit comments

Comments
 (0)