Skip to content

Commit 4426723

Browse files
committed
fix(formula)!: a flow condition's record. / previous. date arithmetic takes the bare name's date-arithmetic verdict
firstTypeMismatch's flattened record-root pass now answers date-arith for date/duration arithmetic against a number on a record.FIELD / previous.FIELD read, naming the root; typeSoundnessIssue gives a root read the row's arm. Claude-Session: https://claude.ai/code/session_01FEkQ5pb2t8Z8C64PkPyVt1 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6ca7c77 commit 4426723

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,10 @@ export function firstTypeMismatch(
13391339
operands: string;
13401340
celType: FieldCelType;
13411341
field: string | null;
1342-
/** [#29959] Set when a flattened condition's mismatch is a `record.<field>` / `previous.<field>` read. */
1342+
/**
1343+
* [#29959, #34946] Set when a flattened condition's mismatch (`type-mismatch`
1344+
* or `date-arith`) is a `record.<field>` / `previous.<field>` read.
1345+
*/
13431346
root?: 'record' | 'previous';
13441347
category: 'date-arith' | 'type-mismatch';
13451348
} | null {
@@ -1419,12 +1422,24 @@ export function firstTypeMismatch(
14191422
}
14201423
// [#29959] A flattened condition reads the same field as `record.<field>`
14211424
// (and `previous.<field>`), which the bare typing above leaves `dyn`. A
1422-
// second pass types those roots too, for the text/boolean mismatch only:
1423-
// the same verdict the bare name gets. The `date-arith` verdict stays the
1424-
// bare name's alone, as before; the date-text family reaches the roots
1425-
// through `validate.ts`'s record-root pass (#34662).
1425+
// second pass types those roots too, for the verdicts the bare name gets
1426+
// here: the text/boolean mismatch, and (#34946) date/duration arithmetic
1427+
// against a number — `record.due_date + 1` faults on the field's text on
1428+
// every run exactly as `due_date + 1` does. Either names the read as
1429+
// written (`root`). The date-text family reaches the roots through
1430+
// `validate.ts`'s record-root pass (#34662), which stands down on the
1431+
// date-arith shape answered here ({@link recordRootReadsFaultDateArith}).
14261432
if (scope !== 'flattened' || !RECORD_ROOT_READ_RE.test(source)) return null;
14271433
const rootTyped = unsoundOverload(buildTypedEnv(bareTypes, 'flattened', 'timestamp', fieldCelTypes));
1434+
if (rootTyped && isDateArith(rootTyped)) {
1435+
const read = offendingRecordRead(source, fieldCelTypes, 'timestamp');
1436+
return {
1437+
operator: rootTyped[2], operands: `${rootTyped[1]} ${rootTyped[2]} ${rootTyped[3]}`,
1438+
celType: 'timestamp', category: 'date-arith',
1439+
field: read?.field ?? null,
1440+
...(read ? { root: read.root } : {}),
1441+
};
1442+
}
14281443
const r = rootTyped && textOrBool(rootTyped) ? confirmAgainstDateText(rootTyped, fieldCelTypes) : null;
14291444
const celType = r ? textOrBool(r) : null;
14301445
if (!r || !celType) return null;
@@ -1595,7 +1610,8 @@ export function firstDateTextVerdict(
15951610
* name `dyn`) is date/duration arithmetic against a number ({@link isDateArith})
15961611
* — `record.due_date + 1`. That shape is the `date-arithmetic` family's, and it
15971612
* is exactly where the bare name's own order stops: {@link firstTypeMismatch}
1598-
* answers `date-arith` there and the date-text check is never asked. Any other
1613+
* answers `date-arith` there (for the root read too, #34946) and the date-text
1614+
* check is never asked. Any other
15991615
* Timestamp-typing fault — `record.end_date - record.start_date > 3` (a Duration
16001616
* ordered against a number), `record.d >= "2026-01-01"` (the text ordering the
16011617
* runtime answers) — leaves the bare name's mismatch check at null and the

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,11 @@ describe('a flow condition\'s record.<field> / previous.<field> read takes the d
13971397
});
13981398

13991399
it('CONTROL: an ordering against today() and the date-text shapes keep their verdicts, with no second finding', () => {
1400-
for (const source of ['record.due_date < today()', 'previous.due_date < today()', 'date(record.due_date) < now()']) {
1400+
for (const source of [
1401+
'record.due_date < today()', 'previous.due_date < today()', 'date(record.due_date) < now()',
1402+
// The rewrites the verdict prescribes (the changeset's FROM → TO).
1403+
'daysBetween(record.due_date, today()) > 5', 'addDays(record.due_date, 1) < today()', 'addDays(previous.due_date, -7) < today()',
1404+
]) {
14011405
expect(validateExpression('predicate', source, CONDITION), source).toEqual({ ok: true, errors: [], warnings: [] });
14021406
}
14031407
expect(codes('timestamp(record.borrow_date) < now()')).toEqual(['date-text-call-run-fails']);

‎packages/formula/src/validate.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,8 @@ const DATE_FAIL_CLOSED_CLAUSE =
519519
* (`date − date + 1`, `today() + 30`) — always faults at runtime and never
520520
* recovers, so it blocks the build. A formula reads that fault as null; a
521521
* flattened (flow) condition's fault fails the run, under the
522-
* `date-arithmetic-run-fails` / `-run-may-fail` codes (#30016), a
522+
* `date-arithmetic-run-fails` / `-run-may-fail` codes (#30016) — its
523+
* `record.<field>` / `previous.<field>` read as its bare name (#34946) — a
523524
* fail-closed site's refuses the write, under `date-arithmetic-fail-closed`
524525
* (#30250), and an advisory rule's skips the rule, under
525526
* `date-arithmetic-fail-open` (#30494, {@link dateArm}).
@@ -596,7 +597,10 @@ function typeSoundnessIssue(
596597
const use =
597598
`Use \`daysBetween(a, b)\` for the span in whole days, and \`daysFromNow(n)\` / ` +
598599
`\`addDays(d, n)\` / \`addMonths(d, n)\` to shift a date.`;
599-
const arm = dateArm(scope, faultOutcome, bareNamesUnproven);
600+
// [#34946] A `record.<field>` / `previous.<field>` read keeps the row's
601+
// outcome whatever binds the bare names (#30025), as its type mismatch and
602+
// its date-text verdicts do ({@link recordRootDateTextIssue}).
603+
const arm = dateArm(scope, faultOutcome, mismatch.root !== undefined ? false : bareNamesUnproven);
600604
return {
601605
severity: 'error',
602606
issue: arm

0 commit comments

Comments
 (0)