Repository navigation
Commit d10f652
feat(spec, metadata-protocol): a seed dataset can declare dependsOn, the objects it loads after (#34276)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} -->
_Requested by **Steedos** · [project
thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbKnsd8ZLsq9hBbRRm5JvJb2)_
Fixes #33812
Clause-②: yes (widening)
Metadata-protocol: added
Skills-docs: follow-up #34277 —
`skills/objectstack-data/references/seeds.md` (its relationship section
and its best-practice table tell the author to order seeds parent before
child in the exported array, which orders nothing; `dependsOn` is not
taught)
## 维护者速读
- 改了什么:种子数据集新增 `dependsOn`(填对象名),加载器把它当作依赖边并入与 lookup
字段同一张依赖图;声明成环、指向本次加载未装载的对象、或指向自身时,整次加载拒绝,一行不写。
- 为什么改:加载器只认 lookup 边,一个 beforeInsert 钩子要读另一种子对象的数据集没法排到它之后(blackcat-crm
的评分在评分档之前写入,全部算成 0 分)。维护者裁定「新增 dependsOn 显式声明」。
- 风险与回滚:不写 `dependsOn`
的应用顺序逐字节不变(有对照测试钉住);拒绝只发生在写了该键的数据集上。单租户启动种子被拒时,启动横幅 `Seeds:`
行标警告、`seed-settled` 消息报 `ok: false`,不再看似干净。单独发布的种子草稿是一次独立加载,其
`dependsOn` 指向别的条目时会被拒绝,需一起发布(见 Acceptance notes)。回滚即撤销本 PR。
-
先看:`packages/metadata-protocol/src/seed-loader.ts`(`collectDeclaredDependencies`、`findDeclaredEdgeCycles`、`orderByDeclaredEdges`、`loadDatasets`
中的拒绝分支);`packages/spec/src/data/seed.zod.ts`(新键与 `defineSeed`
自依赖拒绝);`packages/runtime/src/app-plugin.ts`(启动种子把整次被拒计入 rejected)。
- 两分钟验证:`pnpm --filter @objectstack/metadata-protocol exec vitest run
src/seed-loader-depends-on-33812.test.ts`,预期 12 个用例全绿;其中声明 dependsOn
的评分钩子看到 2 个评分档,对照组看到 0 个。
- 需要你做的:无,席位落地(新 spec 键需 L2 契约复审,席位已申请)。
## What changes
- **`Seed.dependsOn: string[]`** (`packages/spec/src/data/seed.zod.ts`):
the objects a dataset loads after, by object name, for an order no
lookup field gives (most often a `beforeInsert` hook that reads another
seeded object). Optional, no default; each item is held to the object
machine-name pattern. `defineSeed` refuses a dataset that names its own
object when it runs.
- **The seed loader** (`packages/metadata-protocol/src/seed-loader.ts`):
each name becomes an edge in the graph the lookup fields already feed
(`buildDependencyGraph` gains an optional `declared` map; the
`ISeedLoaderService` contract signature is unchanged), so
`topologicalSort` and `orderDatasets` put the dataset after the named
object. Declarations of several datasets of one object are pooled.
Objects left over after the Kahn pass (behind a lookup cycle) are
appended in an order that keeps the declared edges; with none declared
the append order is the old one.
- **Refusals, before any row is written.** A load whose declarations it
cannot honour answers `success: false`, `results: []`, and one
`errors[]` entry per refused declaration (`field: 'dependsOn'`,
`sourceObject` the declaring object, `targetObject` the named one,
`targetField: '(dataset)'`). Three cases: the dataset's own object; a
name no dataset of the same load seeds (the message says when the named
object's datasets were all scoped out by `env` or `locale`); and a
declared edge that closes a cycle. The refusal is RETURNED, not thrown,
so every caller reports it as a failed load; a throw would have sent the
single-tenant boot seed into AppPlugin's basic-insert fallback, which
writes every row in authored order.
- **The cycle rule, precisely:** a declared edge `A dependsOn B` is
refused when B already depends on A through any chain of edges, lookup
or declared (self-references excluded, as the sort excludes them);
equivalently, the edge lies on a cycle of the combined graph. A cycle
made of lookup edges alone is untouched and still loads in deferred
passes.
- **A refused boot seed reads as a failed seed**
(`packages/runtime/src/app-plugin.ts`). The single-tenant boot seed
records its outcome for the banner's `Seeds:` line and the
`objectstack:seed-settled` message (`recordSeedOutcome`). A refused load
writes no row and leaves every row counter at zero, so from the counters
alone the source was dropped from the `Seeds:` line and the message
answered `ok: true`. The boot seed now counts each error of a load
refused before any dataset ran (`success: false` with no results) as a
rejected row, the same failure shape the marketplace heal already
records (`rejected: summary.errors`); the CLI channels then show the
source with a warning and answer `ok: false`. A load that ran keeps its
per-dataset counts unchanged.
- **CLI, no behaviour change:** `composeSeedSettledMessage` moves out of
`packages/cli/src/commands/serve.ts` into
`packages/cli/src/utils/seed-settled-message.ts` (re-exported from
`serve.ts`), so the new test reaches it without importing the serve
command; `packages/cli/src/commands` holds only command modules.
- **Ledger, docs, generated artifacts:**
`packages/spec/liveness/seed.json` gains a `live` `dependsOn` row
(evidence anchored on the three loader symbols, the runtime proof named
in its note); `content/docs/data-modeling/seed-data.mdx` gains a "Load
Order — `dependsOn`" section and stops saying the array order is the
load order, and says that the dev hot-reload seeder and a marketplace
install load only part of an app's seeds and refuse the same way; the
ObjectDependencyNode TSDoc names the declared edges; `check:generated
--fix` regenerated the reference pages (`seed`, `seed-loader`,
`package-api`, `manifest`), `authorable-surface/data.json` and
`liveness/state-counts/seed.md`.
## Tests
- `packages/metadata-protocol/src/seed-loader-depends-on-33812.test.ts`
(12 cases): the card's shape (a rating that looks up its rule only,
bands two master_detail hops below the rule, the fake engine stamping
each rating with the band count it sees at insert time); the
no-`dependsOn` control pinning today's exact order; pooled declarations;
a dataset downstream of a lookup cycle; the four refusals (lookup-closed
cycle, declared-only cycle, own object, unknown name, env-scoped-out
name), a scoped-out dataset that is not judged, the lookup-only cycle
control, and the dry run.
- `packages/spec/src/data/seed.test.ts` (6 cases added beside the other
`SeedSchema` pins): the key parses and is kept, stays absent when
omitted, refuses a non-machine name and a bare string; `defineSeed`
carries it and refuses the own-object case.
- `packages/cli/src/commands/serve-seed-refused-load-33812.test.ts` (2
cases): AppPlugin's real boot seed and SeedLoaderService over an
in-memory engine double, then the `seed-summary` service as the CLI
reads it, the settled message and the banner line. A load refused by
`dependsOn: ['no_such_object']` writes nothing, records `rejected > 0`,
answers `ok: false` and prints `⚠ Seeds: … refused_load_app`; the
control, the same boot with a load that succeeds, writes 2 rows, answers
`ok: true` and prints `refused_load_app 2 rows` with no warning.
- `packages/qa/dogfood/test/seed-depends-on-33812.dogfood.test.ts` (3
cases, ADR-0054 runtime proof): a real stack boot whose sandboxed
`beforeInsert` hook counts seeded bands, read back over REST: the rating
declaring `dependsOn` sees 2, the same rating without it sees 0.
<details><summary>Red first, then green (head 4575480)</summary>
- Red on the base loader (`aab8b3188a` blob of `seed-loader.ts` swapped
in, restored to the HEAD blob `eb0ca78055f4`, hash-checked): `Tests 9
failed | 3 passed (12)`; the card case failed on `expected 3 to be less
than 2` (bands inserted after the rating). The three that stayed green
are the controls: no-`dependsOn` order, lookup-only cycle, scoped-out
dataset not judged.
- Green: `pnpm --filter @objectstack/metadata-protocol test` `Test Files
345 passed | 9 skipped (354)`, `Tests 22958 passed | 45 skipped`;
`typecheck` exit 0.
- `pnpm --filter @objectstack/spec test` `Test Files 845 passed (845)`,
`Tests 27011 passed | 1 todo`; `typecheck` exit 0.
- Dogfood: `seed-depends-on-33812.dogfood.test.ts` 3/3; with
`seed-external-id-refusal`, `showcase-verify-seed`,
`meta-types-create-seed` 4 files, 15/15 (at `0d4aeb9798`, before the
main merge).
- Not run locally, declared to CI: spec `test:repo`; the full dogfood
battery (no `!`, no narrowing arm); the `cloud-connection` suite, a
caller of the loader: the contract signature it compiles against is
unchanged (the `runtime` suite ran in round 2, below).
</details>
<details><summary>Round 2: the refused boot seed (head a0e2353; code
at b0cd771)</summary>
- Head `a0e2353145` only adds `'@objectstack/cli': minor` to the
changeset (the cli diff moves, and a non-`none` kind takes `minor` on
every moved package); the code and tests are byte-identical to
`b0cd771c69`, where CI was 58 success, 10 skipped.
- New test green: `serve-seed-refused-load-33812.test.ts` `Tests 2
passed (2)`.
- Red first and the one ablation, in one step: the new count was removed
at `dist/` (`rejected: totalErrored + refusedLoad` swapped for a
zero-length planted marker, which is the pre-fix count;
`ablation-dist-preflight` marker present in `dist/index.js` and
`index.cjs`). Result: `Tests 1 failed | 1 passed (2)`, the refused case
red on `expected 0 to be greater than 0`, the control green. The source
was restored to the HEAD blob `d8a3078d1c71` (`git diff HEAD` empty),
runtime rebuilt, the marker `--absent` from all 12 built files, tree
clean, and the test green again (2/2).
- `pnpm --filter @objectstack/runtime test`: `Test Files 607 passed
(607)`, `Tests 11089 passed | 19 skipped`; `typecheck` exit 0.
- `pnpm --filter @objectstack/cli exec vitest run --project unit`: `Test
Files 2 failed | 422 passed (424)`, `Tests 5983 passed | 29 skipped`.
The two failed suites are `test/published-subpath-console.pin.test.ts`
and `test/published-subpath-hook-body.pin.test.ts`, both at setup on
`packages/cli is not built (./dist/index.js is absent)`: only the cli's
dependency closure had been built, not the cli. They are NOT MEASURED
locally; CI (which builds the cli) is green at `b0cd771c69`. The cli
`typecheck` exit 0. The integration tier is declared to CI.
- `pnpm --filter @objectstack/metadata-protocol test`: `Test Files 345
passed | 9 skipped (354)`, `Tests 22958 passed | 45 skipped`.
- `packages/spec` `src/data/seed.test.ts`: `Tests 40 passed (40)`; spec
`typecheck` exit 0.
- `check:engine-double-contract`: on the first CI run the new cli test's
double was not in the ledger. `--write` added its `findOne` and `update`
rows to `scripts/engine-double-contract.pinned.json`, and the rerun
exited 0.
- `check:test-source-alias` exit 0 (the test reaches the helper through
`utils/`, not through `serve.ts`).
- `check:cli-command-ids` exit 0: `97 module(s) under
packages/cli/src/commands examined`.
- dispatch-gates at `b0cd771c69` derived 130 families. Local sweep: 112
of 130 families ran before it was stopped to hand this body over (the
other 18 are NOT MEASURED locally; CI runs them); every one that ran
exited 0 except five that exited 3 (`PREREQUISITE NOT MET`, NOT
MEASURED, because their `dist` was not built locally): `check:i18n`,
`check:i18n-coverage`, `check:i18n-walk-parity` (they need the cli
`dist`), the spec `check:skill-examples` (needs the client `dist`), and
`check:dual-build-cjs-loads` (needs a full build). The derivation warns
that the tree is 22 commits behind origin/main. CI at `b0cd771c69` is 58
success, 10 skipped.
</details>
<details><summary>Ablations (each restored to the HEAD blob, `git diff
HEAD` empty)</summary>
All through `scripts/ablation-replace.mjs`:
- Declared edges never join the graph: 4 red (the card, pooled
declarations, downstream of a lookup cycle, declared-only cycle).
- The same at `dist/` for the dogfood proof (`ablation-dist-preflight`
marker present, then `--absent` after the restore and rebuild, tree
clean): the card case red, `expected +0 to be 2`; the control stayed
green.
- Cycle refusal removed: 2 red (both cycle cases).
- Unknown-name refusal removed: 3 red (unknown name, scoped-out name,
dry run).
- Leftover-node ordering removed: 1 red (downstream of a lookup cycle).
- Own-object guard removed: stays GREEN. The case is still refused as a
one-object cycle (see Acceptance notes).
- `defineSeed` own-object refusal removed: 1 red.
</details>
<details><summary>Gate families (derived by dispatch-gates at
94efc9d, reconciled at 4575480)</summary>
- `node scripts/pm/dispatch-gates.mjs --ran`: `123 derived famil(ies)
accounted for — 122 run, 1 NOT-MEASURED`.
- NOT MEASURED: `check:dual-build-cjs-loads`, reason: exit 3, it needs
every package's `dist` (a full workspace build). CI runs it.
- `check:engine-double-contract` exit 1 on the first pass: the new
test's double was not in the pinned ledger. `--write` recorded it
(`scripts/engine-double-contract.pinned.json`, 3 rows); rerun on
4575480: `OK — 1327 pinned`.
- `check:generated` found `check:docs` and `check:liveness` stale;
`--fix` regenerated both and re-checked them green.
`check:authorable-surface` green.
- Every other family: exit 0. `check:nul-bytes` exit 0.
- `git merge-tree --write-tree HEAD origin/main` exit 0 (origin/main 9
commits ahead, no overlap).
</details>
## Acceptance notes
- **Files outside the claimed surface, added in round 2 for the L2
finding:** `packages/runtime/src/app-plugin.ts` (the refused-load
count), `packages/cli/src/commands/serve.ts` and
`packages/cli/src/utils/seed-settled-message.ts` (the helper move, no
behaviour change),
`packages/cli/src/commands/serve-seed-refused-load-33812.test.ts` (the
pin and its control) and `scripts/engine-double-contract.pinned.json`
(its double's ledger rows). The changeset names `@objectstack/runtime`
minor for the visible change and `@objectstack/cli` minor for the moved
helper.
- **"Same load" is the request the loader is handed.** The boot seed and
the per-organization replay load every dataset of the app (the replay,
every installed package's), so a `dependsOn` across them resolves. A
draft publish loads only the bodies being published, so a published seed
whose `dependsOn` names an object seeded by another item is refused
until both are published together; the changeset and the docs page say
so. This is the ruling applied literally; whether a publish should judge
against already-landed rows instead is raised as an open question in the
report, not decided here.
- **A declared edge that a deferred lookup could have satisfied is still
refused.** Lookup edges are deferrable (the row is written and its
reference filled in later), so `A dependsOn B` while B looks up A is
orderable in principle; the ruling refuses any cycle a declared edge
closes, and so does this PR.
- **`errors[].recordIndex` is 0 on a refusal.** The error schema
requires a non-negative row index and a refusal belongs to the dataset,
not to a row; `field: 'dependsOn'` and `targetField: '(dataset)'` mark
it. Widening the error shape is a contract change and not part of this
card.
- **The ledger row carries no `proof` field.** The ADR-0054 `proof` form
needs the dogfood file's `@proof:` tag registered in
`packages/spec/scripts/liveness/proof-registry.mts`, whose classes are
the high-risk list of ADR-0054 §2a; seed ordering is in none of them and
that file is outside this card's surface. The dogfood proof is named in
the row's note.
- **The own-object guard is redundant with the cycle rule** (observed by
ablation: disabling it leaves the case refused, as a one-object cycle).
It stays for its clearer message and because `defineSeed` refuses the
same case at call time.
- **Marketplace purge and sample-data scope** call
`buildDependencyGraph(objectNames)` without declared edges, so their
(reverse) order ignores `dependsOn`; that path deletes rows and the key
is about insert-time reads, so nothing there is wrong today. Noted only.
## Verification
Hypotheses from the dispatch, each measured:
- H1 held: on the base loader the card shape is red (rating inserted at
index 2, bands at 3; the hook saw 0 bands).
- H2 held: the declared edge joins `buildDependencyGraph`'s node
`dependsOn`, `topologicalSort` orders by it, and authored order stays
the Kahn tie-break.
- H3 held: lookup-only cycles still resolve by deferred passes. Rule as
built: a declared edge is refused when its target already depends on its
declarer through any chain of edges (it lies on a cycle of the combined
graph).
- H4: refusals sit at load time, plus `defineSeed` at call time for the
one case a dataset can be judged on alone (its own object). The set of
objects in the same load exists only at load time: `env` and `locale`
filtering, several packages in one replay, and a draft publish's
one-item load all change it after authoring.
- H5: `Metadata-protocol: added` takes `minor` on every moved published
package (`@objectstack/spec`, `@objectstack/metadata-protocol`,
`@objectstack/runtime`, `@objectstack/cli`) per AGENTS.md Post-Task step
3; the private dogfood package carries no changeset.
Session: https://claude.ai/code/session_01QsR2jUg7BKfRBcTPyMxbLh
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- signed: domain:server#5 · zhuangjianguo ·
session_01QsR2jUg7BKfRBcTPyMxbLh -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 803f909 commit d10f652
20 files changed
Lines changed: 1061 additions & 32 deletions
File tree
- .changeset
- content/docs
- data-modeling
- references
- api
- data
- kernel
- packages
- cli/src
- commands
- utils
- metadata-protocol/src
- qa/dogfood/test
- runtime/src
- spec
- authorable-surface
- liveness
- state-counts
- src/data
- scripts
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
457 | 457 | | |
458 | 458 | | |
459 | 459 | | |
460 | | - | |
| 460 | + | |
461 | 461 | | |
462 | 462 | | |
463 | 463 | | |
| |||
539 | 539 | | |
540 | 540 | | |
541 | 541 | | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
542 | 592 | | |
543 | 593 | | |
544 | 594 | | |
| |||
705 | 755 | | |
706 | 756 | | |
707 | 757 | | |
708 | | - | |
| 758 | + | |
709 | 759 | | |
710 | 760 | | |
711 | 761 | | |
712 | | - | |
| 762 | + | |
713 | 763 | | |
714 | 764 | | |
715 | 765 | | |
| |||
753 | 803 | | |
754 | 804 | | |
755 | 805 | | |
756 | | - | |
| 806 | + | |
757 | 807 | | |
758 | | - | |
759 | | - | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
760 | 812 | | |
761 | 813 | | |
762 | 814 | | |
| |||
783 | 835 | | |
784 | 836 | | |
785 | 837 | | |
| 838 | + | |
786 | 839 | | |
787 | 840 | | |
788 | 841 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
| 250 | + | |
250 | 251 | | |
251 | 252 | | |
252 | 253 | | |
| |||
355 | 356 | | |
356 | 357 | | |
357 | 358 | | |
| 359 | + | |
358 | 360 | | |
359 | 361 | | |
360 | 362 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
234 | 234 | | |
235 | 235 | | |
236 | 236 | | |
| 237 | + | |
237 | 238 | | |
238 | 239 | | |
239 | 240 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| 104 | + | |
104 | 105 | | |
105 | 106 | | |
106 | 107 | | |
| |||
0 commit comments