Skip to content

Commit 4374517

Browse files
fix(formula)!: flow-condition verdicts agree with the run — severity, the index spelling, macro variables (#39072)
Part of #38959 — items 1, 5 and 2 are fixed here, item 3 is recorded as accepted fail-open (below), and item 4 (the wording of the date-against-text ordering verdict) is ruled (7344060050, option A: a refusal code of its own) and is built in a later `Part of #38959` PR after this one lands. Clause-②: no (narrowing) Metadata-protocol: narrowed Skills-docs: none — `skills/objectstack-formula/SKILL.md` rule 5 already says the build refuses date arithmetic and a date field in a timestamp-only call, with no exception for a source that also carries a type mismatch; no skill teaches the index spelling, the type-mismatch warning, or ordering a macro variable over date fields ## 维护者速读 - 用户能看到什么变化:编写流程的管理员在校验或发布流程时,以前「条件里同时有文本比数字的小问题和日期相减的大问题」只给一条警告就能发布、上线后每次运行都失败,现在会直接拒绝(字段显示条件、按钮显示条件、共享规则条件等记录级表达式也同样处理);用下标方式读取文本或布尔字段再和数字比较的写法,现在和点号写法一样被指出;对日期列表逐项和日期文本比较这种能正常运行的写法,在条件里不再被误拒;流程赋值里的取值表达式结论不变。 - 为什么做:属于「表达式校验与作者护栏」,替编写流程的管理员和代写流程的 AI 助手解决「校验通过、上线后每次运行都出错」与「能运行却被拒」两类误判。 - 出错会怎样:若误判,作者在校验或发布时会看到一条拒绝;已保存的流程照常加载注册(取值表达式的结论与改动前一致,不会因本改动在加载时被拒),只在下次发布时要求改写;本改动可整体撤回。 - 要你做什么:这是收紧已接受写法的破坏性变更,待座位契约审核通过、试跑全套通过后,需要你本人审阅并合并;另有一项「日期与文本比较的提示文案」需要你拍板(见报告里的待决问题)。 ## What changes All in `@objectstack/formula` (`validate.ts`, `cel-engine.ts`), plus the ADR-0087 semantic entry and the changeset. - **Item 1, precedence** (`typeSoundnessIssue`). The type check reports one finding per source and asked the text/boolean mismatch first; the date-text family was asked only when that answered null. Now, over a mismatch graded a **warning** (no declared outcome: a flow that is not record-triggered, `bareNamesUnproven`, and every record-scoped site with no fault outcome: a formula field `expression`, a field `visibleWhen` / `settledWhen` / `uiReadonlyWhen`, a field `conditionalFormatting[].condition`, a field group `visibleWhen`, an action `visible` / `disabled` / `pressed`, a sharing rule `condition`, the object `uiReadonlyWhen`), the date-text family is asked and its **error** wins. It is asked with the full field typing, then with only the date fields typed (text/boolean fields `dyn`), because the check reports its first fault and a mismatch met first masks the date-text fault behind it (`record.title > 5 && due_date - borrow_date > ...`). Only `date-text-arithmetic*` / `date-text-call*` errors are taken (never `date-duration-value`, pinned). Codes: `date-text-arithmetic-run-may-fail` / `date-text-call-run-may-fail` on a flow that is not record-triggered; under `bareNamesUnproven`, `date-text-arithmetic-run-fails` / `date-text-call-run-fails` over a `record.FIELD` read and the `-run-may-fail` codes over a bare one; `date-text-arithmetic` / `date-text-call` at a record-scoped site. A mismatch graded an **error** keeps its code and words (the record-triggered arm is unchanged), except one answered by an index read alone (item 5's new reach): there the date-text family is asked first, so every code the index spelling had from PR #38949 is kept. - **Item 5, index reads in the mismatch pass** (`firstTypeMismatch`). The record-root pass now opens on `RECORD_ROOT_DATE_TEXT_READ_RE` (member or index) as well as `RECORD_ROOT_READ_RE`. Where only an index read opened it, it takes the text/boolean mismatch and nothing else: date arithmetic against a number stays the date-text family's (PR #38949's codes), and the date-against-text-field ordering (S1, `record['due_date'] > record['title']`, which evaluates) is not judged (`confirmAgainstDateText(..., keepUndeclared)`), so S1 stays clean — that verdict is item 4's question. The index read is named by its member spelling, as the date-text pass already names it, also where a member read opened the pass (review S2): `record['title'] > 5 && record.status == 'open'` names `record.title` and takes `type-mismatch-run-fails` under `bareNamesUnproven` (it was the warning), and `record['title'] > 5 && record.notes == 'n'` names `record.title` (it named `record.notes`). Where two text fields are read the name is the first of them in the field table, for either spelling alike (stated limit N6, carried by #39105: `previous.notes > 5 && record['title'] == 'x'` names `record.title`). Review S3, seat ruling (a): where the mismatch kept is the ordering of a date against a text field (the `keepUndeclared` survivor of `confirmAgainstDateText`, #32595's verdict, which the run answers), an index read does not supply the name (`keptOrdering`), so `record.due_date > record['title']`, `record['due_date'] > record['title'] && record.status == 'open'` and `record.due_date > title && record['notes'] == 'n'` keep the base's `type-mismatch-run-may-fail` warning under `bareNamesUnproven`. Done together with item 1, never before it. - **Item 2, macro variables** (`comparesDateTextAgainstUndeclared`). A comparison that reads a variable a macro binds (`all`, `exists`, `exists_one`, `map`, `filter`) was kept as a mismatch unconditionally. The variable is now read as an element of the list it ranges over: the operand is checked with the variable replaced by `RECEIVER[0]` in the same env, inner macros shadowing the name. `[due_date, record.borrow_date].exists(d, d > '2026-01-01')` is no longer refused; `[title].exists(t, t > 5)` and `[due_date].exists(d, d > title)` keep the verdict their written-out forms take. Where the element does not check, the verdict is kept as before. This widens flow conditions and record-scoped sites only (review M1, seat ruling option a): `firstTypeMismatch` takes `options.macroElements`, and the flow value envelope's date-text check (`flowValueEnvelopeDateTextIssue`), which stands down when that check finds a mismatch, asks it with `macroElements: false`, the typing it had at the merge base. So `[record.due_date].exists(d, d > '2026-01-01') ? isoDate(record.picked_up_at) : 'x'` as an assignment value validates and registers as it did; no flow value envelope verdict changes. - `packages/spec/src/migrations/entries/semantic/19.flow-condition-mismatch-warning-and-index-mismatch-refused.ts`: the semantic entry (structured TODO, no mechanical rewrite), modelled on PR #38949's. - `.changeset/38959-flow-condition-verdicts.md` and the entry name every site and code above, and say that no value envelope verdict changes. - `.changeset/38959-flow-condition-verdicts.md`: `minor` for formula and spec, both declaration lines, BREAKING banner, FROM → TO, the registration marker. ## Tests `packages/formula/src/validate-date-text-arithmetic.test.ts`, new top-level describe for #38959 (21 tests): the five hidden-refusal sources on both arms (no outcome: the date-text error alone; record-triggered: `type-mismatch-run-fails` unchanged) plus a no-date-fault control that stays a warning, the record-scoped formula arm; five index/member pairs equal on three arms with equal params, the member-parity row for `record['due_date'] > 5`, the masked index order, PR #38949's index codes kept on three arms; five macro sources clean on three arms and evaluating `true`, and three that keep their verdict. Second review round (+1 in the describe, 27 now; file 149): the three S3 sources on the `unproven` arm stay the base's warning and evaluate, with the member spelling's error as control (S3); `[due_date].exists(d, d > [title].map(d, d)[0])` stays `type-mismatch-run-fails`, the pin for the `inner.delete` line (N3). First review round (+5 in the describe): an index read beside a member read on three arms, named `record.title` (S2); the family filter, `date-duration-value` under a mismatch warning stays the warning (N2); an inner macro rebinding the name, `[due_date].exists(d, d >= [record.borrow_date].filter(d, d > '2026-01-01')[0])` clean and evaluating (N3); the `bareNamesUnproven` and record-scope codes (N4); the review's two value envelopes give the merge-base answer (ok) at `validateExpression(..., { flowValueEnvelope: true })`, with controls (M1). `packages/services/service-automation/src/flow-registration-checks.test.ts`: both values register at `registerFlow`, the date-text fault alone is refused (M1). `packages/lint/src/validate-expressions.mismatch-precedence-38959.test.ts`: a field `visibleWhen`, an action `visible` and a sharing rule `condition` take the date-text error, the mismatch alone stays a warning (S1). One PR #38949 assertion that pinned the old reach (`previous['title'] > 5` not judged) is replaced by the new pin; its M1 and S1 tests are untouched and green. <details><summary>Second review round at 8755c4d: suites, ablations, unproven differential, gates</summary> - At `8755c4de17` (no merge; origin/main not re-merged): `packages/formula` vitest 72 files, 2238 passed; `pnpm typecheck` exit 0; `packages/lint` vitest 261 files, 10323 passed; `flow-registration-checks.test.ts` 35 passed; `packages/spec` `src/migrations` 213 passed; `migrate-meta-engine-guidance.test.ts` 3 passed (it reddened once on a tracker id in the entry's guidance, now removed; the id stays in the changeset). - Ablations through `scripts/ablation-replace.mjs` (formula's two test files, 300 tests, each restored to the HEAD blob with `git diff HEAD` empty): unmutated 300 passed; S3 guard off (`!keptOrdering` to `true`) 1 failed; only the `inner.delete(macro.name)` line removed 1 failed (the review's N3 gap); the earlier set unchanged in kind: precedence off 10, index gate off 6, `keepUndeclared` off 1, dates-only off 2, macro element typing off 7, index-first date-text off 3, index-only date-arithmetic guard off 1, index naming off 9, family filter off 1, shadowing branch off 1, envelope gate dropped 1. - `unproven` differential, base (merge base, `packages/formula` identical to `2595004f2e`) against this head: 10,725 generated conditions (an ordering or arithmetic over member, index, `previous` and bare reads of text, boolean, date, datetime and number fields and literals, alone and joined with `&&` to six clauses true on the row): 9,529 same, 36 error to error (code or params), 512 warning to error, 516 clean to error, 132 clean to warning. Each move to a stronger grade was evaluated on a row that makes every neighbouring clause true (the `&&` forces the moved clause to run): all 1,160 fault, 0 evaluate. Against the previous head `90c0977c55` the same set had 240 warning-to-error moves that evaluate, all the ordering of a date against a text field (S3); 0 now. The `rt`, `none` and `sweep` arms: every move faults; none weaker. - Corpus: 6,537 recorded calls 0 changed; 224 generated envelopes 0 changed. - `check:adr-0087-registration` exit 0; `dispatch-gates --ran` at `8755c4de17`: 99 derived, 95 run with exit 0, 4 NOT MEASURED (`check:dual-build-cjs-loads`, `check:query-options-erasure`, `check:slot-lookup`, `check:type-check-debt`: over the foreground cap or needing a full build, not started). </details> <details><summary>First review round at 90c0977: suites, ablations, corpus, gates</summary> - At `90c0977c55` (origin/main `4b8a67d8fa` merged in): `packages/formula` vitest 72 files, 2237 passed; `pnpm typecheck` exit 0; `packages/lint` vitest 261 files, 10323 passed; `flow-registration-checks.test.ts` 35 passed; `migrate-meta-engine-guidance.test.ts` 3 passed. - Ablations through `scripts/ablation-replace.mjs` (formula's two test files, 299 tests, each restored to the HEAD blob with `git diff HEAD` empty): unmutated 299 passed; precedence off 10 failed; index gate off 6; `keepUndeclared` off 1; dates-only pass off 2; macro element typing off 7; index-first date-text off 3; index-only date-arithmetic guard off 1; index naming off 9; family filter off 1 (review 9, was 0); inner-macro shadowing off 1 (review 10, was 0); envelope gate `macroElements: false` dropped 1; naming beside a member read back to `!memberRead` 1. With the formula rebuilt under the mutation: precedence off reds the lint pin 3 of 4, the envelope gate dropped reds the registration pin 2 of 35. The `severity === 'error'` half of the filter cannot red: every date-text verdict is an error today. - Corpus, base `2595004f2e` vs this head: the 6,537 recorded calls (2,446 unique, 11 macros, 46 value envelopes, 14 index reads) 0 changed; controls 3 of 3 changed. Generated: 224 value envelopes joining 7 macros to 8 values, 0 changed; 147 conditions with an index read beside a member read on 5 arms: 495 same, 188 params only (`reference` added or corrected), 52 warning to error (all `bareNamesUnproven`), none weaker. - `check:adr-0087-registration` exit 0; `dispatch-gates --ran`: 99 derived, 95 run with exit 0, 4 NOT MEASURED (`check:dual-build-cjs-loads` needs a full build, `check:query-options-erasure` exit 124 at my 280s cap, `check:slot-lookup` and `check:type-check-debt` over the cap last round, not started). </details> <details><summary>First round: suite runs, ablations, door and corpus readings</summary> - `pnpm exec vitest run --maxWorkers=2` in `packages/formula`: 72 files, 2232 passed. `pnpm typecheck` (formula, incl. `check:test-typecheck`): exit 0. - Lint suite against the head formula build: 260 files, 10318 passed. Service-automation files that read these verdicts (4): 140 passed. - Ablations (each mutation landed per `scripts/ablation-replace.mjs`, restored to the HEAD blob, `git diff HEAD` empty), two test files, 294 tests: precedence off 9 failed; index gate off 6 failed; `keepUndeclared` off 1 failed (the S1 control); macro element typing off 5 failed; date-only unmasking off 2 failed; index-first date-text off 3 failed (incl. PR #38949's M1 test). - Door (`os validate --json` on a scratch `defineStack` project, one flow per source, record-triggered and a time-relative sweep of another object, all fields required), base `2595004f2e` vs head: item 1 sources warning only → error (`date arithmetic DateText - DateText`, `isoDate(...)`); `record['title'] > 5` and `record['active'] + 1 > 3` clean → error / warning; the masked index order clean → error; the `.exists` source error / warning → clean. Unchanged: M1 index rows (refused), S1 rows (clean), `record.due_date > record.title` (refused), the nullable ternary (clean), `record['due_date'] > 5` (clean, as its member spelling). - Corpus: 6,537 recorded `validateExpression` calls (the lint suite, `os validate` on the five examples, and the service-automation suite up to where I stopped it), 2,446 unique, re-judged base vs head: 0 changes. Positive controls appended to the same run: 3 of 3 change (item 1, item 5, item 2 sources), 1 of 1 control unchanged. </details> ## Acceptance notes - **Carried elsewhere, not fixed here.** #39107: the value-envelope shape (a macro over a date read joined to a date-text fault, in a value), which this PR keeps at its merge-base verdict; it awaits the maintainer. #39105: the review's N1 (`['2026-01-01'].exists(x, due_date > x)` refused while it evaluates), N5 residual shapes, and N6, a stated limit: the name follows field-table order, not the operand at the faulting operator, so `previous.notes > 5 && record['title'] == 'x'` names `record.title` (it named `previous.notes`); naming by the operand is the follow-up there. #39104: item 3's wider shape (Timestamp minus date text with a left operand that is not a direct call faults on every row). - **Item 3, accepted fail-open.** `(status == 'open' ? now() : null) - record.picked_up_at > duration('1h')` passes and faults. Carrying the branch type cannot refuse it cleanly: typed as the branch, the check sees a Timestamp minus a date text, which the evaluator answers when it can see the Timestamp (`now() - record.picked_up_at > duration('1h')` evaluates). It faults because the null-ternary rewrite wraps the branch in `dyn(...)` and the evaluator's Timestamp-vs-date-text coercion does not look through it (`dyn(now()) - record.picked_up_at` faults the same way), and the null branch faults on its own (`null - TEXT`). Refusing it means modelling that wrapper in the check, which is not the branch type; the operand stays `dyn` and passes by the fail-open law. - **Item 4, not done here.** The verdict stays (its ruling). Its sentence is the `type-mismatch*` template, which says "against a number … the expression faults" and, per arm, that the run fails or the write is refused; none is true of an ordering that evaluates. `expression-refusal.ts` says a code names one template, so a true sentence needs its own code. Ruled 7344060050 (option A, a new refusal code); built in a later `Part of #38959` PR, which also rewrites the two comments that still call the question open (review N8). - A date ordered against a number (`due_date > 5`, `record.due_date > 5`, `record['due_date'] > 5`, `[due_date].exists(d, d > 5)`) passes and faults in every spelling; the index spelling now takes its member verdict, which is clean. Reported, not fixed: a new verdict for every spelling. - Masked mismatch in the other direction: an ordering of a date against a number ahead of date arithmetic (`due_date > 5 && due_date - borrow_date > ...`) is not unmasked by this change. ## Verification First read (by risk): `packages/formula/src/validate.ts` (`typeSoundnessIssue`, `dateTextOver`), `packages/formula/src/cel-engine.ts` (`firstTypeMismatch` root pass with its `keptOrdering` naming guard and its `macroElements` option, `comparesDateTextAgainstUndeclared`), and `flowValueEnvelopeDateTextIssue` in `validate.ts`, the one caller that asks for the old macro typing, the new describe in `validate-date-text-arithmetic.test.ts`. Two minutes: `pnpm --filter @objectstack/formula exec vitest run --maxWorkers=2 src/validate-date-text-arithmetic.test.ts` — expect 149 passed (27 in the describe named "flow-condition verdicts agree with the run"). Contract-tier review re-owed at the new head (narrowing), and the dogfood battery (`!` title). --- _Generated by [Claude Code](https://claude.ai/code/session_01QSeKYnrQ7UMtHofKQVjcH2)_ <!-- signed: repo:objectstack@os-justin · os-justin · session_01QSeKYnrQ7UMtHofKQVjcH2 --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent f6b008e commit 4374517

7 files changed

Lines changed: 649 additions & 51 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
'@objectstack/formula': minor
3+
'@objectstack/spec': minor
4+
---
5+
6+
fix(formula)!: a date-text fault is no longer hidden behind a type-mismatch warning, a text or boolean field read by index (`record['FIELD']`) takes the type-mismatch verdict, and a macro variable ordered against a text literal is no longer refused
7+
8+
Clause-②: no (narrowing)
9+
Metadata-protocol: narrowed
10+
Skills-docs: none — `skills/objectstack-formula/SKILL.md` rule 5 already states "The build refuses date arithmetic (`end - start`, `date + n`)" and refuses a `date` / `datetime` field in a timestamp-only call, with no exception for a source that also carries a type mismatch; no skill teaches the index spelling, the type-mismatch warning, or ordering a macro variable over date fields
11+
12+
<!-- adr-0087: registered flow-condition-mismatch-warning-and-index-mismatch-refused -->
13+
14+
**BREAKING** accept-set narrowing. A flow condition that faults fails the run. Two shapes that published and then failed every run are refused:
15+
16+
- **A date-text fault behind a type-mismatch warning.** The authoring check reported one finding per source and asked the text / boolean type mismatch first; the date-text check was asked only when that answered nothing. Where the mismatch is a warning, `due_date - borrow_date > duration('1h') && record.title > 5` and `record.active > 5 && isoDate(picked_up_at) == 'x'` published with the warning alone. The more severe verdict now wins: the date-text error is reported instead of the warning, also when the mismatch comes first in the source. The mismatch is a warning, so the change reaches: a flow condition on a flow that is not record-triggered (`date-text-arithmetic-run-may-fail`, `date-text-call-run-may-fail`); a flow condition whose bare name a node can rebind (`bareNamesUnproven`: `date-text-arithmetic-run-fails` / `date-text-call-run-fails` over a `record.FIELD` read, the `-run-may-fail` codes over a bare one); and every record-scoped site that declares no fault outcome (`date-text-arithmetic`, `date-text-call`): a formula field `expression`, a field `visibleWhen` / `settledWhen` / `uiReadonlyWhen`, a field `conditionalFormatting[].condition`, a field group `visibleWhen`, an action `visible` / `disabled` / `pressed`, a sharing rule `condition`, and the object `uiReadonlyWhen`. Where the mismatch is an error already (a record-triggered flow, a sweep of the typed object, a site with a declared fault outcome) it keeps its code.
17+
- **A type mismatch through an index read.** `record['title'] > 5`, `record['active'] + 1 > 3`, `previous['title'] > 5`, `record['title'] > record['active']` passed while their member spellings were judged: a text or boolean field read by index under an arithmetic or ordering operator the run has no overload for (against a number or a boolean), which faults on every row. An index read of `record` / `previous` now takes the text / boolean type-mismatch verdict its member spelling takes (`type-mismatch-run-fails`, `-sweep-run-fails`, or the `-run-may-fail` warning), named as its member spelling is named: by the member spelling, also where a member read sits beside it (`record['title'] > 5 && record.status == 'open'` names `record.title` and takes `type-mismatch-run-fails` under `bareNamesUnproven`, as `record.title > 5 && record.status == 'open'` does; before, it took the warning, and `record['title'] > 5 && record.notes == 'n'` named `record.notes`). Stated limit, carried by #39105: the name is the first field of the faulting type in the field table that the source reads, in either spelling, which need not be the operand (`previous.notes > 5 && record['title'] == 'x'` names `record.title`; it named `previous.notes`). The date-text verdicts an index read took before keep their codes. An ordering of a date read against a text field, which the run answers, with either side read by index keeps the verdict it had: unjudged where no member read is in the source (`record['due_date'] > record['title']`), otherwise the mismatch kept as before, named only by a member read, so under `bareNamesUnproven` it stays the `type-mismatch-run-may-fail` warning (`record.due_date > record['title']`, `record['due_date'] > record['title'] && record.status == 'open'`, `record.due_date > title && record['notes'] == 'n'`).
18+
19+
Widened in the same change, for flow conditions and record-scoped sites only: a variable a macro binds (`exists`, `all`, `exists_one`, `map`, `filter`) is typed as an element of the list it ranges over, so `[due_date, record.borrow_date].exists(d, d > '2026-01-01')`, which the runtime answers, is no longer refused as a type mismatch; `[title].exists(t, t > 5)` stays refused. A flow value envelope (an `assignment` value, a `create_record` / `update_record` `fields.*` value) keeps the verdict it had: its date-text check stands down where the mismatch check finds a mismatch and asks that check with the macro typing it had before, so `[record.due_date].exists(d, d > '2026-01-01') ? isoDate(record.picked_up_at) : 'x'` as a value registers and validates as it did. That envelope shape is carried by #39107.
20+
21+
- **Where.** `os validate` / `os build` report an `error`, and a door that runs the same expression pass refuses the write (the flow publish door with `INVALID_METADATA`, 422). For flow conditions `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; no flow value envelope verdict changes, so no stored flow that registered before is refused at registration. The run-time evaluator is unchanged.
22+
23+
## FROM → TO
24+
25+
| FROM | TO |
26+
|:---|:---|
27+
| `due_date - borrow_date > duration('1h') && record.title > 5` | `daysBetween(borrow_date, due_date) >= 1 && size(record.title) > 5` |
28+
| `record['title'] > 5` | `size(record['title']) > 5` |
29+
| `record['active'] + 1 > 3` | `record['active'] == true` |
30+
31+
One-line fix: compare like with like in a flow condition, whichever way a field is spelled; use `size(…)` / `double(…)` to make text a number, `true` / `false` for a boolean, and `daysBetween(a, b)` / `addDays(d, n)` / `date(…)` for a date.

0 commit comments

Comments
 (0)