Repository navigation
Commit 842b468
fix(metadata-protocol): a seed key reference empty on the first load holds one row across loads (#34854)
<!-- claude-projects-attribution: {"github_login":"os-support-ai"} -->
_Requested by **Yibo Wang** · [project
thread](https://claude.ai/code/project/chan_01BVUyyPie8gHwoPM18iABwB?thread=cmsg_01BVUyyPie8gHwoPM18iABwBQ3DFv6XZAspQRYGQoEWhr8)_
Fixes #34800
Clause-②: no
Metadata-protocol: none
repo:objectstack#3 · os-support-ai · session_01WGufzgNCAXyGfBQFZBwixH
## 维护者速读
- 改了什么:种子数据集的复合 `externalId` 里含一个首次加载时只能留空的引用(如尚未注册的人员)时,这行以「按作者写下的键派生的
id」落库;之后每次加载按这个 id 找回同一行,引用一旦可解析就补写到这一行上。
- 为什么改:之前每次启动/每次组织重放都再插一行(实测 1 → 2 → 3;人员在两次启动之间注册则 1 → 2 →
2,旧行永远留空)。#34613 退役 insert/replace 后,默认模式 + 复合键正是推荐写法。
- 风险与回滚:只影响「键里有引用、且该引用在插入时被延后」的行;引用首次即可解析的复合键、纯字段复合键照旧(引擎生成
id)。已经产生重复行的安装不会自动清理,下次加载最多再落一行,之后稳定。回滚 = revert 本 PR。
- 先看:`packages/metadata-protocol/src/seed-loader.ts` 里
`rowLandedWithEmptyKeyReference`(按 id 认领的守卫条件)、派生 id 赋值块与 `ignore` 模式的
pass-2 补写块;`seedDeferredKeyRowId`。
- 两分钟验证:`pnpm --filter @objectstack/metadata-protocol exec vitest run
--maxWorkers=2 src/seed-loader-deferred-key-reference.test.ts`,预期 9
个用例全绿(三次加载始终 1 行、人员出现后引用补到首行、两条仅引用不同的记录保持 2 行、客户手建的空引用行不被占用)。
- 需要你做的:无,席位落地。
## What changes
`packages/metadata-protocol/src/seed-loader.ts`:
- A row that lands while a reference in its key is deferred (pass 1
could not resolve it, so the column is left out of the insert) now lands
with an id derived from the key as authored:
`seedDeferredKeyRowId(object, row organization, authoredKey)`, where
`authoredKey` is `seedLandedKey` over the record before its references
resolve — the person's natural key, never the id it resolves to. Only
for `upsert` / `ignore`, only when the record names no `id` of its own,
and never an id a row of this organization or another record of the load
already holds.
- When a record's natural key finds no row,
`rowLandedWithEmptyKeyReference` looks the row up by that id (or by the
record's own `id`), from the same pre-load read, now also indexed by id
(`loadExistingRecords`' optional `byId`). It takes the row only when its
other key columns equal the record's and each reference column it holds
is empty or the very value the record resolved, and only for one record
of the load. An exact natural-key match always wins. The write decision,
the create-only check, seed files and the #31021 withdrawal question all
read the found row through one view (`existingRowsFor`).
- Once the reference resolves it lands on the row first landed: `upsert`
updates the row as it does any matched row; `ignore` writes nothing over
a row it found, so the one column is back-filled by pass 2 (the held
person reference path writes the same way onto the row the seed landed
on).
Route (the claim's): the replay finds the row as first landed. Why an id
and not the #31021 ledger: the ledger stores keys, never which row a key
landed on, so it can say "this key landed here" but cannot find the row;
and it exists only where a composition registers `sys_seed_landed_key`
and the load has an organization. The id is carried by the row itself,
so both paths are covered by one mechanism, and a row a person created
(random id) can never be taken. Authoring-time refusal of such a key was
not taken (it narrows the accept set; the claim excludes it).
## Tests
- New
`packages/metadata-protocol/src/seed-loader-deferred-key-reference.test.ts`
(9 cases, a fresh loader per load as a restart does): `ignore` and
`upsert` × {person never appears: 3 loads → 1 row; person appears
between loads: 1 row, reference on the row first landed}; the landed-key
record kept (`landedKeys: 'honour'`): 1 row, skipped not withdrawn; two
records differing only in the reference stay two rows, each filled with
its own person; a customer row with the reference empty is never taken.
CONTROLs: a composite key whose reference resolves on the first load,
and a composite key over plain fields, match as before with
engine-minted ids.
- New
`packages/runtime/src/seed-deferred-key-reference-real-driver.integration.test.ts`
(2 cases): real `SeedLoaderService` + real `ObjectQL` + real `SqlDriver`
on one better-sqlite3 file, three boots (new engine/driver/loader each):
per-organization replay with the landed-key record, person never appears
→ `[1, 1, 1]`; no landed-key record, `upsert`, person appears before
boot 2 → `[1, 1, 1]` and the reference lands.
- `scripts/engine-double-contract.pinned.json`: the three pinned doubles
of the new test file, written by `node
scripts/check-engine-double-contract.mjs --write` as the gate prescribes
(add-only, "0 lost").
## Acceptance notes
- Already-duplicated installs keep their copies (the old rows carry
engine-minted ids, which are indistinguishable from a customer's row);
the next load lands at most one more row, which every later load finds.
Stated in the changeset.
- `SeedDatasetFiles.open` still receives the natural-key map only; a row
found by id is passed to `prepareRow` through `existingRowsFor`, which
is the call that reads it.
- A row that landed by its derived id and was filled later, whose person
is then removed: the record defers again and still finds the row by id.
NOT covered, unchanged from main: a row that landed with its reference
resolved (engine-minted id) whose person is later removed keys empty on
the next load, and a new row lands.
- The held person reference path (`resolveHeldPersonReferences`) is not
edited: the deferral's internal id is the found row's id, so a held
reference is written onto that row.
## Verification
Measured on `origin/main` c513d62 before code, with the same harness as
the runtime test (scratch file, deleted): every composition duplicated.
<details><summary>Reproduction on main (three boots over one sqlite
file)</summary>
```
MEASURE mode=default ledger=true sole=false signUpAfter=null rows=1 -> 2 -> 3
MEASURE mode=default ledger=true sole=true signUpAfter=null rows=1 -> 2 -> 3
MEASURE mode=default ledger=true sole=false signUpAfter=1 rows=1 -> 2 -> 2
MEASURE mode=default ledger=false sole=false signUpAfter=null rows=1 -> 2 -> 3
MEASURE mode=default ledger=false sole=false signUpAfter=1 rows=1 -> 2 -> 2
MEASURE mode=upsert ledger=true sole=false signUpAfter=null rows=1 -> 2 -> 3
MEASURE mode=upsert ledger=false sole=false signUpAfter=1 rows=1 -> 2 -> 2
(the 1 -> 2 -> 2 rows: the second copy carried the person, the first stayed NULL)
```
Same harness on this branch: every line `rows=1 -> 1 -> 1`, reference
filled where the person signed up.
</details>
<details><summary>Ablation (unit file; it imports ./seed-loader.js from
src, so no dist leg)</summary>
Fix committed first; mutation via `scripts/ablation-replace.mjs`,
restore by `git checkout HEAD --`, proven by blob == HEAD and `git diff
HEAD` empty.
1. Find-by-id removed (`const row = existingRowsById.get(rowId);` →
`const row = undefined as any;`, blob 195e71f → 50685164): 7 failed / 2
passed — every duplicate and fill case red; the two CONTROLs green.
2. Guard replaced by the naive fallback (take any stored row whose
reference column is empty): 1 failed / 8 passed — the customer-row case
red, as it must be. A first attempt at this mutation reassigned a
`const` and threw TypeError in every case; that run was discarded as a
non-mutation and redone.
</details>
<details><summary>Local readings at head 58dc0df (CI runs the full
farm)</summary>
- `pnpm --filter @objectstack/metadata-protocol typecheck` + full
`vitest run` (after merging origin/main dfbc316 and rebuilding the
closure): VERDICT command-exit 0 · Test Files 349 passed / 9 skipped
(358) · Tests 23100 passed / 45 skipped.
- `pnpm --filter @objectstack/runtime typecheck` (tsc +
check:test-typecheck OK) and the new runtime file: Test Files 1 passed ·
Tests 2 passed.
- Derived gate families (`dispatch-gates.mjs --commands`, 75 for this
diff): `--ran` verdict "75 derived famil(ies) accounted for — 73 run, 2
NOT-MEASURED". NOT MEASURED: `check:dual-build-cjs-loads` (PREREQUISITE
NOT MET: reads every package's dist; only the touched closures are built
here) and `check-plugin-teardown-shape.mjs --self-test` (shallow clone
cannot reach its pinned fixture commit; checker-health only).
`check:engine-double-contract` was red until the ledger rows above, exit
0 after.
- `overlap-check.mjs`: VERDICT clean — merges cleanly with origin/main
and with #34791 (shares seed-loader.ts) and #34784 (shares the ledger).
- Package `test:repo` projects and the rest of the farm: declared to CI.
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01WGufzgNCAXyGfBQFZBwixH)_
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01WGufzgNCAXyGfBQFZBwixH
<!-- signed: repo:objectstack#3 · os-support-ai ·
session_01WGufzgNCAXyGfBQFZBwixH -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 73609d0 commit 842b468
5 files changed
Lines changed: 573 additions & 7 deletions
File tree
- .changeset
- packages
- metadata-protocol/src
- runtime/src
- 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 | + | |
| 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 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
0 commit comments