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 eb7ad70
Browse filesBrowse the repository at this point in the historyBrowse files
fix(metadata-protocol)!: a seed never stores a roll-up (summary) value; the children compute it, and the load says what it dropped (#36118)
Fixes#35655
Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: follow-up #36122
## 维护者速读
-
用户能看到什么变化:应用作者在演示数据或安装包自带的数据里给父记录写的汇总数(如发票的「已收金额」)不再被存下,汇总数一律由明细算出;只写了汇总数、没带明细的父记录现在显示
0,启动日志会点名是哪个对象、哪些字段、丢了几个值。
- 为什么做:按你选的 D「一律不存汇总数」;过去写进去的数时对时错,重启后还会把真实合计盖回去,明细也可能因为写的合计被应用自己的规则拒掉。
- 出错会怎样:受影响的只有在演示数据里写了汇总数的应用作者,他们会看到数字变成明细合计或
0,并在日志里看到提示;不改任何已存数据,撤回本次改动即恢复旧行为。
- 要你做什么:这次改动让演示数据和安装包里原来能存下的汇总数改为不存(收紧),按规则需要你本人批准后才能合并。
## What changes
A seed never stores a value a seed record supplies for a roll-up
(`summary`) field, on insert or on update. This follows maintainer
ruling D as analysed in 7062334196, with the seat's correction
7064615114. The roll-up is computed from the child rows. The drop is
reported by one warn notice per dataset, which is the whole report: no
load-result key is added. The change is in the loader only:
`packages/objectql/src/engine.ts` is not touched.
- `packages/metadata-protocol/src/seed-loader.ts`
- Module helper `summaryFieldNames(definition)` returns every field a
definition declares `type: 'summary'`, whatever its aggregate and
whether or not it is `readonly`. It keys on `type === 'summary'`
literally, as the engine's roll-up readers do.
- In `loadDataset`, the roll-up fields are the union of
`summaryFieldNames` over the resolved definition AND over the engine's
merged schema (`engine.getSchema`, read through the new private
`engineSchemaOf`, which `resolveObjectDefinition` now shares). The
metadata service's copy wins `resolveObjectDefinition` whenever it has
`fields`, and that copy carries no object extension, so a roll-up an
extension contributes is read from the engine's schema (contract review
S1b). An engine without a schema registry, or whose read throws, leaves
the resolved definition the only source.
- Each such key is deleted right after the record's expressions resolve,
before anything else reads the record. So the no-op replay compare, the
insert and the update all see the record without it.
- A created row starts the column at its empty-child-list value (the
engine's existing `initializeSummaryFields`), and the child inserts
compute it, as before.
- A matched row keeps the value it already holds.
- One `warn` per dataset names the object, the fields and how many
values were dropped (`{ object, fields, droppedValues }`). A supplied
`null` counts as a value. A dry run (`validate`) logs the same notice.
- `success` and `errored` do not move. One row counter does: on a replay
(`upsert` or `update`), a matched row whose only difference from the
seed record was a roll-up value is now counted `skipped`, with nothing
written. It used to be counted `updated`.
-
`packages/spec/src/migrations/entries/semantic/18.seed-summary-value-not-stored.ts`:
the ADR-0087 semantic entry for the narrowing. No tombstone and no D2
conversion, because no key is added or removed and no rewrite is
mechanical. It names the manual repair for a parent with no child rows
that an earlier load stored a total on (review nit n3): insert a child
row under it and delete that row again.
- `.changeset/35655-seed-summary-value-not-stored.md`:
`@objectstack/spec` and `@objectstack/metadata-protocol` at `minor`,
with a BREAKING banner, `adr-0087: registered
seed-summary-value-not-stored`, FROM → TO, the compatibility note for
seed authors, and the same manual repair (n3).
- `content/docs/data-modeling/fields.mdx`: the sentence "seed data,
package installs and a historical import … keep a pre-computed total" is
rewritten to the ruled behaviour. The historical import
(`treatAsHistorical: true`) keeps its sentence, because it is a separate
door. The degraded path is named (review S2): when the runtime has no
metadata service, or the seed loader throws, it falls back to a basic
insert of the seed records, and that fallback still stores a supplied
summary value. Ruling D keeps the change loader-only.
- `content/docs/protocol/objectql/security.mdx` (review S1a): Rule 1's
sentence no longer says seed data and package installs write a roll-up
from a system context. It names the roll-up's own recompute and a
historical import, and says the seed loader takes a seed record's value
out before the write.
- Comment-only edits in objectql, to text this change made false:
- the `summaryFieldsLockedOnWrite` docblock in `engine-top-level.ts`;
- the `summary` note in `validation/rule-validator.ts`;
- the header and one test name of `engine-summary-update-lock.test.ts`.
They no longer say a seed load keeps a pre-computed total. The engine
behaviour they describe (an `isSystem` write keeps its value) is
unchanged, and so is the test's assertion.
- Stale test comments (review nit n1):
`packages/objectql/src/summary-rollup.test.ts` (the comment above, and
the name of, the "system writer supplied on insert" case no longer list
seed loads) and
`packages/spec/src/data/field-autonumber-readonly.test.ts` (the
`summary` note no longer says a caller may seed one). Assertions
unchanged.
-
`packages/metadata-protocol/src/seed-loader-engine-schema-fallback.test.ts`:
the "prefers the metadata service definition" pin asserted that the
engine is never probed for a seeded object. The S1b union probes it once
per seeded object, for the roll-up strip alone, so the pin now asserts
exactly that probe list. Its claim stands: the engine double registers
no schema, so the resolved references prove the metadata definition was
used.
Package installs go through the same loader:
- `packages/runtime/src/domains/packages.ts` applies a package's seeds
with `new SeedLoaderService(...)`;
- so does `packages/metadata-protocol/src/protocol-publish-batch.ts`;
- so do the single-tenant boot seed, the per-organization replay and the
reseed paths in `packages/runtime/src/app-plugin.ts`.
The only seed path outside the loader is the runtime's basic-insert
fallback (see above and the Acceptance notes).
No existing pin asserted that a seed load keeps a roll-up value.
`engine-summary-update-lock.test.ts` "an isSystem create keeps its
value" pins the engine's `isSystem` exemption, which this PR does not
change.
## Tests
Pins. Each asserts the stored rows or the engine's calls, plus the warn
notice. None reads a count key.
- `packages/objectql/src/seed-summary-never-stored.test.ts`, on the real
ObjectQL engine and the real `SeedLoaderService`. The parent has a
`sum`, a `count` and a `readonly` `max` roll-up, seeded with 999 / 99 /
500, over lines of 10 and 32.
- Case 3: a parent alone stores 0 / 0 / empty, with one notice `{
object: 'ss_inv', fields: [line_total, line_count, line_max],
droppedValues: 3 }`.
- Case 1 (control): parent and children in one load store 42 / 2 / 32.
- Case 4: a re-seed over existing children keeps 42 / 2 / 32 and is
`skipped`, in `upsert` and in `update` mode.
- Case 6: the same seed replayed stores 42 / 2 / 32 on both loads.
- A roll-up an extension contributes (`registerObject(..., 'extend')`,
engine-fallback rung) is dropped like the base object's own.
- A seed with no roll-up value gives no notice.
- `packages/metadata-protocol/src/seed-loader-summary-stale.test.ts`, on
the file's existing engine double:
- `a seed never hands the engine a roll-up value`: no `insert` or
`update` payload carries a roll-up key; a supplied `null` counts; a
roll-up-only difference writes nothing; another change is written
without the roll-up; `validate` gives the notice and writes nothing; a
`currency` CONTROL is written as authored with no notice.
- New this round (S1b), `a roll-up an object extension contributes is
never seeded either`: the metadata double's copy lacks the extension's
roll-up and the engine double's `getSchema` has it. Neither roll-up
reaches the engine, and the one notice reads `{ object: 'ext_invoice',
fields: ['paid', 'ext_refunds'], droppedValues: 2 }`. CONTROL: an engine
whose `getSchema` throws leaves the metadata copy the only source; its
roll-up is still dropped, and `ext_refunds` (declared a roll-up nowhere)
is written as authored.
<details>
<summary>Ablation of the S1b union (this round, head
e6b5e1a)</summary>
The metadata-protocol pins import `./seed-loader.js` from source, so no
rebuild is involved. `node scripts/ablation-replace.mjs --file
packages/metadata-protocol/src/seed-loader.ts --anchor '
...summaryFieldNames(this.engineSchemaOf(objectName)),' --delete -- …`:
- Mutation: anchor x1 → x0, blob 7ace59d → d6f9588ece80; on-disk
count of the union line read 0 inside the wrapped command.
- Run: `Tests 2 failed | 15 passed (17)`, VERDICT command-exit 1. Red:
the extension pin (`expected [ 'name', 'total', 'ext_refunds' ] to not
include 'ext_refunds'`) and the engine-schema-fallback probe pin
(`expected [] to deeply equal [ 'crm_account', 'crm_contact', … ]`). The
throwing-schema CONTROL stayed green.
- Restore: blob after restore 7ace59d == HEAD blob 7ace59d,
`git diff HEAD` empty, `git status --porcelain` empty.
- Green at the head: `Tests 17 passed (17)`, VERDICT command-exit 0.
Round 2's ablation of the strip loop itself (objectql 6 red / 1 green
control, metadata-protocol 4 red / 8 green, restore proven) is recorded
in dev report 7065542104.
</details>
<details>
<summary>Suite readings (this round)</summary>
- `@objectstack/metadata-protocol` typecheck: VERDICT command-exit 0 (at
e6b5e1a).
- `@objectstack/metadata-protocol` test at 9737321: `Test Files 1
failed | 362 passed | 9 skipped (372)`, `Tests 1 failed | 23311 passed |
45 skipped (23357)`. The one red was the engine-schema-fallback probe
pin the S1b union made false; it is updated in c4dc426. At e6b5e1a
the two changed files read `Test Files 2 passed (2)`, `Tests 17 passed
(17)`; no source file changed after 9737321.
- Edited test files in other packages: objectql `summary-rollup.test.ts`
+ `seed-summary-never-stored.test.ts` `Tests 133 passed (133)`; spec
`field-autonumber-readonly.test.ts` `Tests 8 passed (8)`.
</details>
Dogfood battery at the final head `e6b5e1af11`: `pnpm --filter
@objectstack/dogfood test` (no shard, no path) under the verify lock, on
the closure built at 9737321 (`pnpm --workspace-concurrency=2
--filter '@objectstack/dogfood^...' --filter
'@objectstack/metadata-protocol' build`, VERDICT command-exit 0). Every
commit after 9737321 changes test files only, so that build is the
head's source.
- `Test Files 483 passed | 3 skipped (486)`
- `Tests 4101 passed | 68 skipped (4169)`
- `os-verify-lock: VERDICT command-exit 0 · held the lock 2099s`
- No failures.
Gates at the final head `e6b5e1af11`: `dispatch-gates.mjs --commands`
derives 123 families; the list is identical at c4dc426.
<details>
<summary>Gate readings</summary>
- All 123 run locally at e6b5e1a, each exit captured before any pipe:
123 exit 0.
- `--ran` reconciliation: "123 derived, 123 run, 0 NOT-MEASURED, 0
UNRUN", exit 0.
- `check-adr-0087-registration`:
`[BREAKING+bang+clause-②-narrowing+metadata-protocol-narrowed]
registered seed-summary-value-not-stored`.
- At c4dc426, `check:engine-double-contract` exited 1 on the
extension pin's new engine double in a new file (ledger RETAINED rows).
The pin moved into `seed-loader-summary-stale.test.ts` on that file's
already-pinned double (e6b5e1a), and the gate exits 0.
`check:skill-examples` and `check:dual-build-cjs-loads` exited 3
(PREREQUISITE NOT MET, no local `dist/` for packages outside this diff);
after building those packages both exit 0.
- Local-only `check:dev-prereqs` exit 1: 35 of 108 workspace packages
unbuilt in this container (mostly `ui/`), not this diff.
- `overlap-check` before each of the three pushes this round: VERDICT
clean, exit 0. No `origin/main` merge this round (no conflict, CI
green).
- CI at e6b5e1a: 70 check-runs, 60 success and 10 skipped, 0 failure.
`Lint & Repo Gates`, `TypeScript Type Check`, `Test Core` (all 10
shards), `Build Core`, `Governed Surface Queue Guard`, `Type Check UI`,
`Test UI`, `Build & E2E UI`, `Live E2E UI`, `Dogfood Regression Gate`
and `Check Changeset` read success; `Lint UI` and the sharded dogfood
matrix read skipped.
</details>
## Acceptance notes
- The runtime's basic-insert fallback seeder
(`packages/runtime/src/app-plugin.ts`, the two `ql.insert` loops under
"basic insert fallback") writes seed records with a plain `ql.insert`.
It runs only when no metadata service is present (logged at debug) or
the loader throws (logged at warn). Ruling D routes the change through
the loader only, so on that path a roll-up value still reaches the
engine. `fields.mdx` and the changeset now both say so.
- The loader does not rewrite rows that an earlier load stored a
supplied total on. The next child write recomputes them; a parent with
no child rows gets none, and `os migrate summary-nulls` backfills only
`NULL` columns, so the changeset and the migration entry name the manual
repair.
- A dataset keyed on a summary field (an `externalId` naming a roll-up)
would now see that key blank, and the record would be refused as a
blank-key record. No such seed exists in the tree, and a roll-up is not
a natural key.
- The drop is counted before the record's fate is known: a record later
refused or skipped as create-only still counts in the notice (review nit
n2, not taken).
- The published data skill
(`skills/objectstack-data/references/seeds.md:146-147`) gives the right
instruction ("Never seed the roll-up"), but its reason ("stored as
written") is now out of date. Follow-up #36122.
## Verification
Read first, in order of risk:
1. `packages/metadata-protocol/src/seed-loader.ts`: the `summaryFields`
union in `loadDataset` and the strip loop right after `const record = {
...(seedResult.value ...) }`. The strip must run before every read of
the record.
2. `packages/objectql/src/seed-summary-never-stored.test.ts`: cases 3, 4
and 6.
3. `.changeset/35655-seed-summary-value-not-stored.md`: the FROM → TO,
the row-counter sentence, the compatibility note and the manual repair.
Two-minute check: `pnpm --filter @objectstack/metadata-protocol exec
vitest run --maxWorkers=2 src/seed-loader-summary-stale.test.ts`. Expect
`Tests 14 passed (14)`; the extension case's notice names `paid` and
`ext_refunds`.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01FEkQ5miLqNucRZevceHwNh)_
<!-- signed: repo:objectstack#4 · os-project-manager ·
session_01FEkQ5krBqrrgxCYgjXmfbv -->
---------
Co-authored-by: Claude <noreply@anthropic.com>
**BREAKING** for a seed record that supplies a value for a `summary` (roll-up) field, in any seed: a `defineStack({ data })` dataset, `defineSeed`, a `seed` metadata item, or the seed data a package install loads. Every aggregate (`count`, `sum`, `min`, `max`, `avg`) is treated the same, whether or not the field is declared `readonly`.
14
+
15
+
- FROM: the seed loader writes as the system principal, so the supplied value was stored as written. It stayed wherever no child write came after it: a parent seeded with a total and no children kept the total; a re-seed over existing children wrote it over the computed value; and a keep-current (`upsert`) seed replayed on the next boot stored the computed total on the first load and the supplied one on the second.
16
+
- TO: the loader takes every such value out of the record before the replay compare and the write, on insert and on update, and writes the rest of the record. A created row starts the column at its empty-child-list value (`0` for `count` and `sum`, empty for `min`, `max` and `avg`), and the seeded child rows compute it. A matched row keeps the value it holds, so a replay whose only difference is the roll-up writes nothing. A parent seeded with a total and no children now reads `0`.
17
+
- Reported, not silent: the load logs one warning per dataset naming the object, the fields and how many values it did not write. A dry run (`validate`) logs the same warning. `success` and `errored` do not move, and the load result gains no key. One row counter does: on a replay (`upsert` or `update`), a matched row whose only difference from the seed record was a roll-up value is now counted `skipped`, with nothing written, where it used to be counted `updated`.
18
+
19
+
Compatibility note for seed authors: to show a total, seed the child rows it is computed from and leave the summary field out of the parent's seed record. A figure no child rows back is not a roll-up: declare it a `number` or `currency` field. A legacy total that must land as written goes through a historical import (`treatAsHistorical: true`), which is unchanged. Rows an earlier load stored a supplied total on are not rewritten by the loader; the next child write recomputes them. A parent with no child rows gets no such write, and `os migrate summary-nulls` backfills only `NULL` columns, so repair one by hand: insert a child row under it and delete that row again, and the recompute leaves the empty-set value.
20
+
21
+
Not changed: the engine (`packages/objectql`), which still keeps a value an `isSystem` write sends (the roll-up recompute, the backfill, a historical import); the import and REST doors; and the runtime's basic-insert fallback seeder, which runs only when the seed loader is unavailable or throws.
0 commit comments