Skip to content

Commit 7c8d689

Browse files
skills(data): seed rows obey the package's own validation rules in load order; docs: seed load order comes from lookups and dependsOn (#35652)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} --> _Requested by **Elon** · [project thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9D4n2wzAmmp4Z6W5jsqtM2qx)_ Part of #34986 Fixes #35327 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:用 AI 搭应用的作者照着数据技能写演示数据时,会被告知演示数据同样受应用自己的校验规则约束、要按加载顺序逐条过关,并且不要把汇总值直接写进父记录;文档里「数组按依赖顺序导出」的旧说法改正。 - 为什么做:属于「问数评测」这条线。订单到回款场景的一次搭建里,72 条回款演示数据有 66 条被应用自己的「回款不得超过未结余额」规则拒收,技能里没有一句话提醒作者。 - 出错会怎样:这只是教学文字、一道评测题和两处文档措辞;写错了作者会照错的方式写演示数据,可直接撤回。 - 要你做什么:无。「搭建时把被拒的演示数据报给作者」那一半检查不在本次改动内,另行安排。 ## What changes - `skills/objectstack-data/references/seeds.md` gains one paragraph, each clause measured on `main` (see Acceptance notes): seed rows obey the object's validation rules, all but `state_machine`; a refused row never lands; `os validate` does not run those rules on seed rows and `os verify` fails on each dropped row; a rule reading a parent roll-up sees the total earlier rows left, so children must fit in record order; a roll-up value written on a seed row is stored as written and must not be seeded (a cap then refuses the payments that total already counts). - `skills/objectstack-data/evals/hooks-security-seeds-datasources.json`: one case, id 32. - `content/docs/data-modeling/seed-data.mdx`: the multi-object layout no longer says `index.ts` exports the array "in dependency order", and "One file per object" no longer says the index "orders" the datasets. Both now agree with the page's own Load Order section and with the skill after PR #35312: lookups and `dependsOn` order the load, array position only breaks ties. No other sentence on the page makes the old claim. - `.changeset/34986-skills-seed-rows-obey-validation-rules.md`: `@objectstack/skills` patch. ### Token ratchet: paid in full by deletion, the row does not move Standing ruling 6513306068, 「A:先削重复,余额按实测提高」: restatement is cut in the same file first, and the row moves only by a measured remainder. Here the cuts cover the teaching (3 bytes under), so `scripts/check-skills-token-ratchet.mjs` is not edited. | `seeds.md` | bytes | tokens, ceil(bytes / 4) | ceiling | lines | |:---|---:|---:|---:|---:| | before (`eca20d2219`) | 7036 | 1759 | 1759 | 178 | | after the cuts alone | 6589 | 1648 | 1759 | | | after (`f2d0e25610`) | 7033 | 1759 | 1759 | 182 | Cuts (-447 bytes), each a restatement inside the file: the `'name'` default row of the key table (the field table states the default); "Pick a stable natural key" (the field table's "Stable business key"); "(not its UUID)" and the "natural key of an Account record" comment (the sentence above both); "at load time" (the same sentence's "order the load"); the second half of the id-fallback note (it restated the first half); the second `defineSeed` import (the quick start imports it); "and breaks build determinism" (the determinism gate line says it); the `applyOn` comment (the sentence above the block); "(fixtures, reference rows, bootstrap data)" (the quick start's two comments); and the "never lands" clause of the malformed-value paragraph, folded into one sentence. The teaching is +444 bytes. Whole shipped tree at this head, by the gate's own price tag: 275304 tokens; the only priced file this PR touches is `seeds.md` (1759 before and after), and the rest of the movement is the eval case, which is measured and not priced. Lines: `seeds.md` 178 to 182; every `SKILL.md` in the catalog together 4853 to 4853; the whole `skills/` tree 22229 to 22249 (16 of the 20 are the eval case, which the ratchet measures and does not price). ## Evals Case 32: a `payment` rule that refuses a payment above the invoice's open balance, reading `record.invoice.amount_paid`, a `sum` roll-up; seed one fully paid and one part-paid invoice. The expected answer seeds the invoices without a roll-up value and payments that each fit what the rows before left, and does not say seeds skip validation rules or weaken the rule. `must_contain`: `defineSeed`, `invoice: 'INV-1'`. `must_not_contain`: `amount_paid: 4800`, `amount_paid: 2000`, the two seeded roll-up literals. They are code spellings with the case's own numbers; the skill text uses a different number, so an answer that quotes the skill's warning does not trip them. The grader is a literal substring match (`packages/qa/scenarios/src/skill-evals/grade.ts`). No model run was made for this case. ## Verification Gates below ran on `4408d12775`; after the review delta (head `f2d0e25610`: the roll-up sentence now says a cap refuses the payments the seeded total already counts, and eval case 32 says the payments on the fully paid invoice are refused, not every payment) the token ratchet and its self-test, `check:skill-eval-tokens`, the skill-evals test (36 of 36), `check:nul-bytes` and the overlap check were run again and exit 0. - Two-minute check: `node scripts/check-skills-token-ratchet.mjs` prints `skills/objectstack-data/references/seeds.md is 1759 tokens (ceiling 1759; headroom 0)` and exits 0. - Read first: the new paragraph in `seeds.md` (the riskiest hunk: it ships verbatim to customer agents), then the cuts in the same file, then eval case 32. <details><summary>Local gates and tests</summary> - `pnpm --filter @objectstack/scenarios exec vitest run --maxWorkers=2 test/skill-evals/skill-evals.test.ts`: `Test Files 1 passed (1)`, `Tests 36 passed (36)`, `VERDICT command-exit 0`. - `node scripts/pm/dispatch-gates.mjs --repo objectstack-ai/objectstack --commands`: 73 families. `--ran` with recorded exit codes: `73 derived, 72 run, 1 NOT-MEASURED, 0 UNRUN`. 72 exited 0, among them `check-skills-token-ratchet` and its self-test, `check:skill-examples` (`346 prose examples type-check across 3 surface(s)`), `check:skill-served-sections`, `check:skill-links`, `check:doc-authoring`, `check:nul-bytes`, `check-closing-keyword-parity`, `check-empty-changeset`. - NOT MEASURED: `pnpm check:dual-build-cjs-loads`, exit 3, PREREQUISITE NOT MET (it needs every package built; this diff builds none). CI owns it. - `node scripts/pm/overlap-check.mjs --branch=claude/area-a4A-34986`: `VERDICT clean exit 0` against `origin/main` and PR #35649, which shares the eval file. - No ablation: the diff changes no behaviour a test pins. </details> ## Acceptance notes **Which half is left.** This PR is the teaching half of the direction on the card. The other half, a build or publish-time finding that names the refusing rule and the count, is not in this PR and no check code was written; the card stays open for it. **What the two builds did differently (the reading the direction asked for).** The build records named in the reading on the sibling card (two directories under a project-files mount of another project) are not reachable from this session, so the rule text of each build was not read. What the card's own rows prove, in `docs/qa/scenarios/order_to_cash.json` on `main`: the 95 seeded invoices carry their end state (`amount_paid`, `balance`), and exactly 66 of the 72 payments are larger than their invoice's seeded `balance` (all 66 belong to a `paid` invoice whose seeded balance is 0); the other 6 belong to part-paid invoices and fit. So the refusing build judged each payment against a balance that already counted it. A later rebuild pair recorded on the sibling card split the same way (one build 619 of 619, the other 66 refused), so the split is not tied to the skills version. A roll-up that merely lags its inserts does not produce this number: measured below, the rule does see earlier rows. **Measured on `main`** (scratch probe, real engine, sqlite, the real seed loader; not committed): - (a) No exemption beyond `state_machine`. The loader writes every row under `SEED_WRITE_EXECUTION_CONTEXT` (`packages/metadata-protocol/src/seed-loader.ts`, `SEED_OPTIONS`); the engine skips only `state_machine` for it (`shouldSkipStateMachine`, `packages/objectql/src/engine-top-level.ts`; the insert path hands it to `evaluateValidationRules` in `engine-insert.ts`). Probe: a `script` rule refused a seeded payment with `Failed to write xt_payment record #0 (name=P1): A payment may not exceed the invoice's open balance.` - (b) The rule runs before the row is written and the roll-up recompute runs after it (`recomputeSummaries`, same file), and inside one batch the rows already admitted are overlaid on the value the rule reads (`summary-batch-accumulation.ts`, pinned by `packages/rest/src/import-batch-parent-summary-rule.test.ts`). Probe: payments of 2000, 2000, 1000 against an invoice of 3000 land the first and third and refuse the second; with the payments dataset listed first the lookup still loads the invoice first. - A seed write is a system write, so a roll-up value on a seeded parent is stored as written; the same value sent by a user write is reset to 0. Probe: an invoice seeded with `amount: 3000, amount_paid: 3000` keeps 3000 and its only payment is refused, leaving a paid total with no payment under it. This is the shape of the 66. - (c) Reporting today. `os validate` and `os build` judge seed values by field shape and against declared state-machine states only; neither evaluates a validation rule against a seed row, so they pass. At boot the loader returns the refusal in `result.errors` and `AppPlugin` logs a `[Seeder]` warning with the dropped count and the first 20 messages (the rule's message, not its name). `os verify` counts every dropped seed record as a failure and exits 1, printing the count and pointing at the log; it names no rule. Nothing at build or publish time reports it. **Noted, not filed.** The scenario card's seed rows carry end-state roll-up values for the invoices, which is what a build that declares those columns as roll-ups is then judged against. Carrier: the seat that owns the scenario cards. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- signed: repo:objectstack#4 · os-elon-musk · session_016WV4d1ZJL93Kin2UHFXbFv --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent d44b5e3 commit 7c8d689

4 files changed

Lines changed: 47 additions & 18 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@objectstack/skills': patch
3+
---
4+
5+
docs(skills): `objectstack-data`'s seeds reference says seed rows obey the object's validation rules (all but `state_machine`) as they load: a rule reading a parent roll-up sees the total earlier rows left, a seeded roll-up value is stored as written, and `os verify`, not `os validate`, fails on a dropped row. One eval case covers it; the file pays in deleted restatement and its token ceiling does not move.
6+
7+
Clause-②: no
8+
Metadata-protocol: none

‎content/docs/data-modeling/seed-data.mdx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ export a single aggregate array.
841841
```
842842
src/
843843
data/
844-
index.ts ← exports SeedData array in dependency order
844+
index.ts ← exports the SeedData array (the loader orders it)
845845
accounts.seed.ts
846846
contacts.seed.ts
847847
leads.seed.ts
@@ -920,7 +920,8 @@ company names, email addresses, and values — not `foo`, `bar`, or `test123`.
920920
### One file per object
921921

922922
Split large seed payloads into `{object}.seed.ts` files. A single `index.ts` that
923-
re-exports and orders them keeps the entry point clean.
923+
re-exports them keeps the entry point clean; lookups and `dependsOn` order the
924+
load, and array position only breaks ties.
924925

925926
---
926927

‎skills/objectstack-data/evals/hooks-security-seeds-datasources.json‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,22 @@
310310
"must_contain": ["CrossDatasourceTransactionWriteError", "default datasource"],
311311
"must_not_contain": []
312312
}
313+
},
314+
{
315+
"id": 32,
316+
"prompt": "`payment` has a validation rule that refuses a payment above the invoice's open balance (`record.amount > record.invoice.amount - record.invoice.amount_paid`); `invoice.amount_paid` is a `sum` roll-up of its payments. Seed demo data: invoice `INV-1` for 4800, fully paid in two payments, and `INV-2` for 5000 with 2000 paid.",
317+
"expected_output": "Two `defineSeed` sets. The invoice rows carry `number` and `amount` and no roll-up value: a seed write stores a supplied `amount_paid` as written, the rule then reads a balance already spent, and both payments on the fully paid `INV-1` are refused (the 2000 on `INV-2` still fits its 3000 open balance). The payments name their invoice by natural key (`invoice: 'INV-1'`) and each fits what the rows before it left: two that sum to 4800 on `INV-1`, one of 2000 on `INV-2`; the roll-up then computes the paid totals. Does not say seed rows skip validation rules (only `state_machine` is skipped) and does not weaken the rule to make the seed load; `os verify` fails on a dropped seed row.",
318+
"files": [],
319+
"assertions": {
320+
"must_contain": [
321+
"defineSeed",
322+
"invoice: 'INV-1'"
323+
],
324+
"must_not_contain": [
325+
"amount_paid: 4800",
326+
"amount_paid: 2000"
327+
]
328+
}
313329
}
314330
]
315331
}

‎skills/objectstack-data/references/seeds.md‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Seed Data & Fixtures (`defineSeed()`)
22

33
Object definition and seed data live together — a `*.object.ts` usually
4-
pairs with a `*.seed.ts` (fixtures, reference rows, bootstrap data).
4+
pairs with a `*.seed.ts`.
55

66
> The factory is `defineSeed` — **not** `defineDataset`, which is the
77
> unrelated ADR-0021 analytics semantic layer (`@objectstack/spec/ui`).
@@ -85,42 +85,40 @@ mapping entry's `applyOn` (objectstack-api).
8585
defineSeed(Invoice, {
8686
externalId: 'number',
8787
mode: 'upsert',
88-
columns: { status: { applyOn: 'create' } }, // the payment job owns it after the first seed
88+
columns: { status: { applyOn: 'create' } },
8989
records: [{ number: 'INV-001', status: 'draft', amount: 1200 }],
9090
});
9191
```
9292

9393
### `externalId` selection
9494

95-
Pick a stable natural key. **Never use `id`** for generated UUIDs — they
95+
**Never use `id`** for generated UUIDs — they
9696
differ across environments.
9797

9898
| Scenario | Key |
9999
|:---------|:----|
100100
| Named entities (country, currency) | `'code'` / `'slug'` |
101101
| Users / contacts | `'email'` |
102102
| Externally sourced | `'external_id'` |
103-
| Generic | `'name'` (default) |
104103

105104
### Relationship references
106105

107-
For `lookup` fields, supply the **natural key** of the target record (not
108-
its UUID). The loader resolves it at load time, and lookups order the load:
106+
For `lookup` fields, supply the target record's **natural key**. The
107+
loader resolves it, and lookups order the load:
109108
a child loads after its parent however the array lists them (array order
110109
only breaks ties).
111110

112111
> If a lookup value matches no natural key, the loader falls back to
113-
> resolving it as the target's `id` — so a reference to an existing record
114-
> by internal id resolves instead of dangling to null. Rely on it only for
115-
> records you didn't seed (e.g. a system user).
112+
> resolving it as the target's `id`. Rely on it only for records you didn't
113+
> seed (e.g. a system user).
116114
117115
```typescript
118116
const contacts = defineSeed(Contact, {
119117
externalId: 'email',
120118
records: [{
121119
email: 'john@acme.example.com',
122120
first_name: 'John',
123-
account: 'Acme Corporation', // natural key of an Account record
121+
account: 'Acme Corporation',
124122
}],
125123
});
126124
```
@@ -130,15 +128,21 @@ A `beforeInsert` hook reading a seeded object no lookup links to needs
130128
object, one no dataset of the load seeds (misspelt, or `env`/`locale`-scoped
131129
out), or a cycle refuses the whole load; nothing is written.
132130

131+
**Seed rows obey the object's validation rules** (all but `state_machine`); a
132+
refused row never lands. `os validate` does not run them on seed rows;
133+
`os verify` fails on each dropped row. A rule reading a parent roll-up
134+
(`record.invoice.amount_paid`) sees the total earlier rows left, so children
135+
must fit in record order. ⛔ Never seed the roll-up: a seeded `amount_paid` is
136+
stored as written, and a cap on payments refuses those it counts.
137+
133138
### Dynamic values (CEL)
134139

135140
Any field value may be a CEL expression evaluated at install time against
136141
a single per-load pinned `now`. This is the **only** correct way to author
137142
time-based or identity-derived seed values — `new Date()` ships the package
138-
author's clock to every customer and breaks build determinism.
143+
author's clock to every customer.
139144

140145
```typescript
141-
import { defineSeed } from '@objectstack/spec/data';
142146
import { cel } from '@objectstack/spec';
143147

144148
defineSeed(Opportunity, {
@@ -165,10 +169,10 @@ produce byte-identical `dist/objectstack.json`.
165169
|:---------|:----|
166170
| TypeScript: `defineSeed()`, never `SeedSchema.parse()` | Lose compile-time field checks otherwise |
167171

168-
**Fictional is fine; malformed is not.** A seed row is written through the same
169-
field validation as a user's insert, and a value the field type refuses never
170-
lands — the table loads short or empty while sibling rows point at records that
171-
do not exist. `os validate` / `os build` refuse such a value first, at the
172+
**Fictional is fine; malformed is not.** A seed row passes the same field
173+
validation as a user's insert; a refused value leaves the table short or empty
174+
while sibling rows point at records that do not exist.
175+
`os validate` / `os build` refuse such a value first, at the
172176
record's field — run them before trusting the load. So never mask or
173177
placeholder a sample value: no `138****4207`,
174178
`***`, `XXX`, `N/A`, `—`, `TBD`. Invent a plausible well-formed one — `+86 138

0 commit comments

Comments
 (0)