Repository navigation
Commit 401eb44
fix(app-shell): the formula inspector asks inferFormulaReturn before it keeps a currency result type (#35774)
repo:objectstack#7 · os-tesla · session_01L9qibvVRcFWKrfWeG5jzPb
<!-- claude-projects-attribution: {"github_login":"os-tesla"} -->
_Requested by **Tesla** · [project
thread](https://claude.ai/code/project/chan_01AXsQkLFKRdQCqQ3RTr6Jao?thread=cmsg_01AXsQkLFKRdQCqQ3RTr6JaoK8YCcS3hkJHvVLxZNeiKG8)_
Fixes #35631
Clause-②: yes (widening)
Metadata-protocol: none
## 维护者速读
- 在 Studio
里编辑公式字段的建模者会看到:公式从算金额改成算数量后,结果类型由"货币"改回"数字",不再按金额显示;仍在算金额的公式保持"货币"并带上源金额的币种;无法判定是否为金额的公式(例如引用汇总字段)保持原样。
- 为什么做:修正公式结果类型在编辑后变旧的问题,替在 Studio 里写货币公式的建模者解决显示错误。
- 出错会怎样:只影响 Studio 编辑公式时写入草稿的结果类型,存储的数值不变;可直接撤回本次合并。
- 要你做什么:无。
## What changes
`settleFormulaType` in the Studio field inspector now asks the engine's
`inferFormulaReturn` (from `@objectstack/formula`, loaded through the
same lazy seam as the other CEL verdicts) whether an edited formula
computes money, over the object's declared field map:
- **Provably money** stamps `returnType: 'currency'` with the proven
`currencyConfig` (absent for the tenant default currency), also on a
formula that was `number`.
- **A `number` answer withdraws a declared `currency`**: `returnType`
becomes `number` and the stale `currencyConfig` is removed (the
producer's contract in `formula-return.ts`: the declaration is the pair,
and an absent member is cleared).
- **Unproven** (`{}`: a field-over-field quotient, two currencies
meeting) keeps a declared `currency`, as before. An engine without the
export gives no money verdict and also keeps it.
- **`percent` is unchanged** (the #35047 rule): it survives a `number`
or unproven verdict, and a money verdict too (never re-stamped
`currency`; the spec text now says so). A text, boolean or date verdict
still stamps, and now also clears a stale `currencyConfig`.
The money verdict lands one microtask after the two editor verdicts. A
newer verdict supersedes it (identity check on the verdict record), and
the write uses the latest render's draft, so it builds on the current
definition and not on an earlier closure.
`packages/formula/src/formula-return.ts` (the producer, PM ruling A,
completed after the L2 record at `e5a7e08b`): a `number` answer means
"proved not money". On the numeric path the answer is `{}` (unproven)
unless one of two things holds. Either the unit proof succeeds (a plain
quantity or constant), or every read of the record is one segment naming
a declared field whose unit can be read and is not money. This covers
all four spellings, `record.x`, `record['x']`, `record.?x`,
`record[?'x']`, and a bare `x`. Only then does the CEL inference answer.
Unproven, measured, with the answer at `e5a7e08b` in brackets:
- a hop `record.account.balance * 0.9` [number];
- a formula operand with no `returnType`, `record.subtotal * 0.2`
[number];
- `record['amount'] * 0.1` [number];
- `record.unknown_field * 2` [number];
- a roll-up, division by an amount, a remainder, two currencies, amount
times amount, an unparseable currency [number since round 2];
- `record.?amount * 0.1` and `record[?'amount'] * 0.1` [`{}`: the CEL
checker does not type the optional spellings, so they never reach the
numeric path].
One shape per leg of the unit check: `100 / record.weighted` (a formula
declared `currency`) and `100 / amount` (a bare identifier).
CONTROLs that still answer `number`: declared quantities in any spelling
(`record['quantity'] * 2`, a bare `quantity * 2`), a percent field, a
formula declaring `number`, and a `count` roll-up whose
`summaryOperations` is passed.
A call with no field map now answers `{}` for a numeric expression that
reads the record, because no read has a declared unit; it was `number`.
A bare same-currency ratio (`record.amount / record.tax`) is proven
plain, but cel-js types it `dyn`, so it answers `{}` as before.
`FormulaSourceField` gains the optional member `summaryOperations` (a
widening, hence `Clause-②: yes (widening)`). A roll-up passed without
it, a `count` included, is read as possibly money.
The header, the `FormulaReturnDeclaration` doc (an absent `returnType`
is no verdict; the pair rule covers `currencyConfig` beside a `currency`
answer) and the function doc say exactly this. Runtime callers on
`main`: only the inspector, plus the showcase test
`money-format-declarations` (proven money, unchanged, green).
`packages/spec/src/data/field.zod.ts`: the `returnType` JSDoc and
`.describe()` said a declared `currency` survives a `number` verdict.
That is no longer true, so the prose now restates the card's ruling (the
Done-when on #35631): `currency` is stamped when money is proven and
withdrawn for `number`, an unproven verdict keeps it, and a declared
`percent` is kept under a money verdict. The generated
`content/docs/references` pages were regenerated with `gen:docs`. Prose
only: no key, value or shape changes.
Files to read first, by risk:
1.
`ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.tsx`:
`settleFormulaType` / `stampFormulaType` (the async hand-off and the
decision order).
2. `ui/packages/app-shell/src/views/metadata-admin/celAuthoring.ts`:
`inferFormulaReturnDeclaration` (feature-detected, error-swallowing,
`null` means no verdict).
3. `packages/formula/src/formula-return.ts`: `hasPlainUnit` /
`readsOnlyPlainFields` / `memberRead` and the one new early return.
Two-minute check: `cd ui && pnpm exec vitest run
packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.returnTypeHeld-27485.test.tsx`.
Expect 24 passing, including the thirteen in the `objectstack#35631`
describe. Producer: `pnpm --filter @objectstack/formula exec vitest run
src/formula-return.test.ts`.
## Tests
New top-level describe in the existing harness file
`ObjectFieldInspector.returnTypeHeld-27485.test.tsx`. Its stub keeps the
editor verdicts deterministic and hands in the real, unstubbed
`inferFormulaReturn`:
- money to count (`record.amount * 0.1` to `record.est_hours * 2`) drops
`currency` and `currencyConfig`;
- an edit that still proves money keeps `currency` and `{ fixed, USD }`;
- a `number` formula edited into money is stamped `currency` with the
source's config;
- an unproven field-over-field quotient keeps a declared `currency`;
- a roll-up the proof cannot read as money (`record.total * 0.9`) is
unproven and keeps a declared `currency`;
- a NEW formula over an amount the proof cannot follow (`100 /
record.amount`, verdict `{}`) is still stamped `number` from the
editor's CEL type. The non-currency stamp reads the editor's type, not
this verdict, so Studio's save is unchanged for new formulas;
- CONTROL: a declared `percent` is kept under a `number` verdict. The
edit is now to a different quantity expression (`record.est_hours * 3`);
the old one equalled the harness default, so no stamp ran;
- a declared `percent` edited to `record.amount * 0.2` (a money verdict)
stays `percent`;
- config-update path: a USD amount to a EUR amount updates the config; a
move to a dynamic amount removes it; a record-mode source stamps its
code field;
- a hand-declared `currency` over a formula with no `returnType`
survives `record.subtotal * 0.1` to `* 0.12` (route b; it was withdrawn
at `e5a7e08b`);
- CONTROL: a boolean verdict still stamps over `currency` and clears the
config.
Producer, in `formula-return.test.ts`: one describe pins each unproven
shape as `{}`. It lists the round-2 shapes, the shapes that already
answered `{}`, the four M1 routes (a hop, a formula with no
`returnType`, the three other spellings, an undeclared name) and one
shape per leg (`100 / record.weighted`, `100 / amount`). CONTROLs:
proven money still stamps `currency` with its config; quantities in
every spelling, a percent field and a `number` formula still answer
`number`; a `count` roll-up still answers `number`. The plain-inference
pin now covers only declared non-money units and non-numeric results.
The no-field-map pin is retitled: `{}` for a record read, `number` for a
constant.
The existing pin "a `number` verdict keeps a declared `currency` too" is
renamed to say it runs with no money verdict from the engine. It holds
unchanged.
Readings at the final head `b9ce479e` unless named:
<details><summary>Test runs</summary>
- `@objectstack/formula` test: Test Files 69 passed (69), Tests 1906
passed (1906). `typecheck`: exit 0 (at `ae8b9cf9`, producer source
unchanged since).
- Inspector, celAuthoring and CelPredicateField suites plus
`column-identity.ratchet.test.ts`, run with `@objectstack/formula`
rebuilt first: Test Files 40 passed (40), Tests 510 passed (510). The
harness file alone: 24 passed.
- `pnpm --filter @object-ui/app-shell type-check` (both tsc programs):
exit 0.
- `@objectstack/example-showcase` `money-format-declarations.test.ts`: 5
passed.
- `@objectstack/spec` `formula-currency-result`,
`form-return-type-options`, `field`: 327 passed.
- `pnpm --dir ui exec vitest run scripts/`: 171 files passed, 2 skipped;
5092 tests passed, 2 skipped.
- Round 2's ratchet fix stands: the stamp's field check is two plain
comparisons, not a column-identity dual read. There is no inventory
entry.
</details>
<details><summary>Ablations (scripts/ablation-replace.mjs; each leg
landed on disk and was restored to blob == HEAD with an empty git diff
HEAD)</summary>
- A (4ea7e78), the withdraw branch: replaced with a bare return. Red:
"the card's repro ... drops currency and its currencyConfig".
- B (4ea7e78), the money-proof branch: the condition replaced with
`false`. Red: "a number formula edited into money is stamped currency".
- C (76bbb54), the producer's unproven early return: removed. Red:
"each answers {}".
- D (ae8b9cf), M1 route (b): `hasPlainUnit` read a formula with no
`returnType` as plain (`return field.returnType !== 'currency'`), blob
61722a6 to 7b5806c3. Red: "each answers {}" (`record.subtotal * 0.2`)
and the no-field-map pin.
- E (ae8b9cf), S1, the percent guard in `stampFormulaType` removed,
blob 80b3157 to 4817a67a. Red: the corrected CONTROL "a declared
percent is still kept under a number verdict", "a declared percent is
kept under a money verdict too", and the two #35047 percent pins.
- F (ae8b9cf), S1, the config comparison forced equal
(`sameFlatRecord(...)` to `true`), blob 80b3157 to d6c9c6f3. Red: the
USD-to-EUR, dynamic and record-mode pins.
- A's first attempt used a replacement text already present in the file.
The tool refused it before running, so that attempt measured nothing.
</details>
## Acceptance notes
- **L2 record at `e5a7e08b` (FAIL): M1 and S1 to S4 are fixed here.** N1
and N2 are left for follow-up cards, as directed.
- The optional spellings `record.?amount` and `record[?'amount']`
answered `{}` at `e5a7e08b` too: the CEL checker does not type them, so
they never reach the numeric path. Their pins guard the new read check,
but they would not discriminate against that head.
- **Summary roll-ups: ruled A by the PM, done here.** The producer
answers `{}` for a money-reaching expression it cannot prove, so a
currency formula over a roll-up keeps its declared `currency` on edit.
Before, it was withdrawn on a `number` answer.
- The card's example `count(record.lines)` answers `{}` (unproven:
`count` is not a CEL function the checker types), so a declared
`currency` is kept for that exact text. The pins use a typed count
(`record.est_hours * 2`), which answers `number`.
- When money is proven, the stamp writes the proven `currencyConfig` and
clears one that is absent. A `currencyConfig` the author set by hand on
a currency formula over a dynamic amount is therefore removed on the
next expression edit. That is the producer's stated contract (the
declaration is the pair). Selecting a field still writes nothing.
- File surface: beyond the claim's surface, this PR also touches
`@objectstack/formula` (the producer, ruling A), `celAuthoring.ts` (the
lazy engine seam; a static import would put cel-js in the main bundle
and bypass the test seam) and the spec prose plus its regenerated
reference docs (they would otherwise state the old rule). The spec edit
is prose only, but it touches `packages/spec/src/**`, so an L2 record is
owed.
## Verification
<details><summary>Local gates at b9ce479</summary>
- Derived with `node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack`: 117 families. `--ran` reconciliation: 117
of 117 accounted for, exit 0.
- 112 exit 0 on the first pass, and the ui scripts vitest passed.
- `check:dts-closure` went red during the concurrent client build, which
was rewriting the spec dist at the time; re-run once it finished: exit
0, 228 of 228 declaration files present.
- `check:skill-examples` answered exit 3 before the client packages were
built; re-run after: exit 0, 346 examples.
- The 22 families that read the spec dist were re-run on a stable dist:
all exit 0.
- Two NOT MEASURED (exit 3, PREREQUISITE NOT MET), which CI runs:
`check-plugin-teardown-shape --self-test` (shallow clone) and
`check:dual-build-cjs-loads` (needs a full build).
- `overlap-check`: VERDICT clean before the push. `check-nul-bytes`: OK.
</details>
CI runs the full farm.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01L9qibvVRcFWKrfWeG5jzPb)_
---
Body refreshed 2026-11-20T09:05Z for rework round 3 (contract review
7034135842: M1, S1–S4), head b9ce479.
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent cbc50a5 commit 401eb44
10 files changed
Lines changed: 475 additions & 62 deletions
File tree
- .changeset
- content/docs/references
- data
- system
- packages
- formula/src
- spec/src/data
- ui/packages/app-shell/src/views/metadata-admin
- inspectors
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
| 101 | + | |
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
277 | 277 | | |
278 | 278 | | |
279 | 279 | | |
280 | | - | |
| 280 | + | |
281 | 281 | | |
282 | 282 | | |
283 | 283 | | |
| |||
657 | 657 | | |
658 | 658 | | |
659 | 659 | | |
660 | | - | |
| 660 | + | |
661 | 661 | | |
662 | 662 | | |
663 | 663 | | |
| |||
0 commit comments