You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 6ee2a63
Browse filesBrowse the repository at this point in the historyBrowse files
fix(formula): a proven quantity typed dyn answers number, so Studio withdraws a stale currency (#38108)
Fixes#35815
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 在 Studio 编辑公式字段的作者:把金额公式改成"数量乘数量"或"同币种金额相除"后,字段不再继续显示货币符号,而是改为普通数字。
- 属于公式货币结果类型这一组修复的后续,替在 Studio 里重构公式的作者解决"不再是金额却仍按金额显示"的问题。
- 出错时只影响作者编辑公式那一刻的类型标注,已存数据不变;作者可以在检查器里把结果类型改回去,也可直接撤回本改动。
- 无。
## What changes
`inferFormulaReturn` (`packages/formula/src/formula-return.ts`) now
answers `{ returnType: 'number' }` when the unit proof (`dimOf`) proves
a plain number and the CEL type is `dyn`. A field read is `dyn` to
cel-js, so any operator over two field reads types `dyn`; before this
change such an expression fell through to `inferExpressionType`, which
maps `dyn` to `unknown`, and the answer was `{}` (unproven) even though
every operand had been read as a declared number. Studio's formula
inspector withdraws a declared `currency` only on a `number` answer, so
the edit `record.amount * 0.1` into `record.quantity * record.hours`
kept `returnType: 'currency'` and its `currencyConfig`.
- Both field-over-field shapes the card names now answer `number`:
quantity arithmetic (`record.quantity * record.hours`, `/`, `+`, bare
`quantity * hours`, a ternary or `max` over two quantities) and a ratio
of two amounts in one currency (`record.amount / record.tax`).
- Unchanged: money over two field reads (`record.amount *
record.quantity`) still answers `currency`; whatever the proof cannot
follow still answers `{}` (a ratio across two currencies, anything
divided by an amount, an amount times an amount, an operand whose unit
cannot be read). The published direction holds: an unproven verdict
keeps the declaration.
- Relationship hop (`record.account.employee_count * 2`): still `{}`,
now recorded as an **accepted limit** in the producer header. Proving it
needs the related object's declared fields as a further input to
`inferFormulaReturn`, which is a change to its published signature (and
to the inspector, which would have to load them). That arm is reported
to the seat as a decision, not built here.
- No export, signature or `FormulaSourceField` member changes.
`listMembershipSource` and every other export are untouched.
Rider N3 (test side):
- The `record.?amount * 0.1` and `record[?'amount'] * 0.1` pins answered
`{}` with or without the unit check: cel-js does not type an optional as
a number, so the numeric path never ran. They are replaced by
`record.?amount.orValue(0) * 0.1` and `record[?'amount'].orValue(0) *
0.1`, which type `double` and are judged, with CONTROLs
`record.?quantity.orValue(0) * 2` and `record[?'quantity'].orValue(0) *
2` that answer `number`. The test comment now says why the bare optional
spelling pins nothing.
- The released changelog sentence is NOT edited here (see Acceptance
notes).
Inspector side (test only,
`ObjectFieldInspector.returnTypeHeld-27485.test.tsx`, the component's
shared stamp harness): a new top-level describe pins the withdrawal for
`record.est_hours * record.budget` and `record.amount / record.tax`,
with CONTROLs that a ratio across two currencies keeps `currency` and
that an amount times a quantity over two field reads still proves money
(EUR). One existing pin, "an unproven verdict (a field-over-field
quotient) still keeps a declared `currency`", used `record.est_hours /
record.budget`, two plain numbers: that is the shape this card fixes, so
it now uses `record.est_hours / record.amount`, which is still unproven.
The engine stub's `dyn` regex now covers `*` as well as `/`, matching
the engine.
## Read first
1. `packages/formula/src/formula-return.ts`, the new line after the
money returns in `inferFormulaReturn` (`celType === 'dyn' &&
isPlainNumber(dim)`): the only behaviour change. Risk: a `dyn`
expression `dimOf` calls a quantity that is not numeric. `dimOf` only
answers quantity or constant for numeric literals, declared numeric
non-money fields, and arithmetic or unit-keeping calls over those.
2. `packages/formula/src/formula-return.test.ts`, the `#35815` describe
and the edited `UNPROVEN_OVER_MONEY` list.
3. The inspector test's swapped pin (`record.est_hours /
record.amount`).
## Two-minute verification
`pnpm --filter @objectstack/formula exec vitest run --maxWorkers=2
src/formula-return.test.ts`: 22 tests pass, among them `#35815 — a
proven plain number whose CEL type is dyn answers number`. Delete the
`celType === 'dyn'` line and the two `answers number` cases go red.
## Tests
All runs below are on head `b9bc5f06` (after merging `origin/main`),
unless the line says otherwise.
<details><summary>formula: package suite and typecheck</summary>
- `pnpm --filter @objectstack/formula exec vitest run --maxWorkers=2`:
`Test Files 72 passed (72)`, `Tests 2167 passed (2167)`, lock `VERDICT
command-exit 0`.
- `pnpm --filter @objectstack/formula typecheck`: exit 0;
`check:test-typecheck: OK`, the test layer compiles under
`tsconfig.test.json`, debt ledger held.
</details>
<details><summary>app-shell: the inspector stamp harness</summary>
- `pnpm exec vitest run --root ../.. --maxWorkers=2
packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.returnTypeHeld-27485.test.tsx`
(from `ui/packages/app-shell`, after building `@objectstack/formula`,
`lint` and `client`, which the suite reads from `dist/`): `Tests 28
passed (28)`.
- `Type Check UI` is left to CI (the edit is string literals and a regex
in one test file).
</details>
<details><summary>Ablations (fix committed first,
`scripts/ablation-replace.mjs`, restore proven by blob == HEAD and an
empty `git diff HEAD`)</summary>
- Producer, src: delete the `celType === 'dyn'` line ⇒
`formula-return.test.ts` `Tests 2 failed | 20 passed (22)`:
"field-over-field quantity arithmetic answers number" and "a ratio of
two amounts in one currency answers number". Restored.
- Inspector, through `dist/`: same deletion, then `pnpm --filter
@objectstack/formula build`, then the inspector file ⇒ `Tests 2 failed |
26 passed (28)`: "the card's repro: an edit from an amount to
quantity-times-quantity drops currency and its currencyConfig" and "a
ratio of two amounts in one currency drops currency too". Restored and
rebuilt; `ablation-dist-preflight.mjs @objectstack/formula` reports the
marker present in 2 built files and the tree clean against HEAD.
- N3, src: delete the `readsOnlyPlainFields` guard (the #35631 unit
check). `record.?amount * 0.1` and `record[?'amount'] * 0.1` stay `{}`
(they pin nothing); `record.?amount.orValue(0) * 0.1` and
`record[?'amount'].orValue(0) * 0.1` flip to `{ returnType: 'number' }`
(they discriminate). With the guard restored, they answer `{}` and both
CONTROLs answer `number`.
</details>
## Acceptance notes
- **N3's changelog sentence is not corrected in this PR.** The sentence
("the indexed and optional spellings of an amount (`record['amount'] *
0.1`)" moved from `number` to `{}`) is in the consumed
`.changeset/pre/35631-formula-currency-verdict.md` and was already
released into the `19.0.0-next.1` sections of
`packages/formula/CHANGELOG.md`, `ui/packages/app-shell/CHANGELOG.md`
and `packages/spec/CHANGELOG.md`. AGENTS.md (Documentation Guardrails)
says a factual error in a released entry is amended in a dedicated
docs-only PR, never as a rider on a code change. Proposed amended text:
"the indexed spelling of an amount (`record['amount'] * 0.1`) and the
optional spellings read through `orValue` (`record.?amount.orValue(0) *
0.1`); a bare optional (`record.?amount * 0.1`) never typed as a number
and answered `{}` before and after". Handed to the seat.
- **Pre-existing inspector behaviour, not changed (test-only surface):**
a formula declared `number` that is edited into `record.quantity *
record.hours` has its `returnType` cleared. The inspector stamps the CEL
type (`unknown` for `dyn`) and reads the money verdict only to stamp or
withdraw `currency`. The existing pin "CONTROL — the same quotient still
clears a declared `number`, as before" asserts this.
- A `count` roll-up operand in a field-over-field product
(`record.line_count * record.quantity`) still answers `{}`: `dimOf` does
not read a roll-up as a quantity, though the read check accepts a
`count`. That is the safe direction, and no surface was widened for it.
- Local gates are the `dispatch-gates.mjs --commands` subset; every
other family is CI's.
## Verification
- `node scripts/pm/overlap-check.mjs` before each push: `VERDICT clean`.
- Local derived gates: see the report comment on the card.
---
_Generated by [Claude
Code](https://claude.ai/code/session_017xRBcSAaBEfQpU2yJexSJ8)_
---------
Co-authored-by: Claude <noreply@anthropic.com>
fix(formula): `inferFormulaReturn` answers `number` for a proven plain number whose CEL type is `dyn`, so Studio withdraws a stale `currency` on a field-over-field quantity edit
6
+
7
+
Clause-②: no
8
+
Metadata-protocol: none
9
+
10
+
A field read is `dyn` to cel-js, so an operator over two field reads types `dyn`. `inferFormulaReturn` answered such an expression `{}` (unproven) even when its unit proof had read every operand as a declared number. Studio's formula inspector withdraws a declared `currency` only on a `number` answer, so an author who edited `record.amount * 0.1` into `record.quantity * record.hours` kept `returnType: 'currency'` and its `currencyConfig`, and the value was still drawn as money.
11
+
12
+
FROM `inferFormulaReturn('record.quantity * record.hours', fields)` → `{}` TO → `{ returnType: 'number' }`, over a field map that declares both as plain numeric fields. The same holds for every expression the unit proof proves a plain number whose CEL type is `dyn`: `record.quantity / record.hours`, `record.quantity + record.hours`, `quantity * hours`, a ternary or `max` over two quantities, and a ratio of two amounts in one currency (`record.amount / record.tax`). Money over two field reads (`record.amount * record.quantity`) still answers `currency`. What the proof cannot follow still answers `{}`: a ratio across two currencies, anything divided by an amount, an amount times an amount, an operand whose unit cannot be read.
13
+
14
+
A quantity read through a relationship (`record.account.employee_count * 2`) still answers `{}`: the related object's fields are not an input to `inferFormulaReturn`, so the leaf's unit cannot be read, and a declared `currency` is kept on that edit. The function's header now records this as an accepted limit.
15
+
16
+
Nothing to migrate. A caller that read `{}` for these shapes as "keep the declaration" now gets `number`, which means proved not money.
@@ -119,9 +120,6 @@ describe('CONTROL — a numeric formula stays `number`', () => {
119
120
it('nothing it cannot prove is money: a numeric expression over declared non-money units, or one that is not numeric, gets exactly the plain inference',()=>{
120
121
for(constsourceof[
121
122
'record.quantity * 2',
122
-
// A ratio of two amounts in one currency is proven a plain number, and
123
-
// cel-js types it `dyn`: the plain inference answers `{}`.
124
-
'record.amount / record.tax',
125
123
'record.amount > 100',
126
124
'upper(record.name)',
127
125
'today()',
@@ -194,8 +192,12 @@ describe('#35631 — an amount the proof cannot follow is unproven, never `numbe
194
192
'record.account.balance * 0.9',// a hop through a reference
195
193
'record.subtotal * 0.2',// a formula that declares no returnType
196
194
"record['amount'] * 0.1",// the indexed spelling of an amount
197
-
'record.?amount * 0.1',// the optional spelling
198
-
"record[?'amount'] * 0.1",// the optional indexed spelling
195
+
// The optional spellings read through `orValue`: a bare `record.?amount`
196
+
// is an optional, which cel-js does not type as a number, so
197
+
// `record.?amount * 0.1` answers `{}` with or without the unit check and
198
+
// pins nothing. Unwrapped, the read types `double` and is judged.
199
+
'record.?amount.orValue(0) * 0.1',// the optional spelling
200
+
"record[?'amount'].orValue(0) * 0.1",// the optional indexed spelling
199
201
'record.unknown_field * 2',// a name the map does not declare
200
202
// One shape per leg of the unit check.
201
203
'100 / record.weighted',// a formula declared `currency`
@@ -215,10 +217,66 @@ describe('#35631 — an amount the proof cannot follow is unproven, never `numbe
215
217
});
216
218
217
219
it('CONTROL — every spelling of a quantity read, and a formula declared `number`, still answers `number`',()=>{
Copy file name to clipboardExpand all lines: ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.returnTypeHeld-27485.test.tsx
0 commit comments