Repository navigation
Commit 275ad42
fix(organizations): claimOrgSeedOwnership claims by paged predicate writes until no owner-less row is left (#23071)
Fixes #23054
Clause-②: no — a runtime fix inside one function: no published accept
set and no public signature changes (`OrgScopingEngine` is
package-private).
## What changed
`claimOrgSeedOwnership`
(`packages/plugins/organizations/src/claim-org-seed-ownership.ts`) read
each object's owner-less ids once, at `limit: 10_000`, and updated them
one at a time. Nothing read again, and the ADR-0093 D7 once-gate never
re-runs the claim, so rows past 10,000 stayed owner-less for good.
It now claims by paged predicate writes, the shape the twin
`claimSeedOwnership` took in #14530 and #14719:
- **One predicate write per object:** `update(schema.name, { owner_id:
owner }, { where: { organization_id: org, owner_id: null }, multi: true,
context: CLAIM_WRITE_CTX })`. Below the engine's per-row hook ceiling,
that is the whole claim, and it issues no read.
- **A page when the engine refuses it.** Above
`MAX_BULK_PER_ROW_HOOK_ROWS` (10,000) matched rows the engine refuses
the write whole with `ERR_BULK_PER_ROW_HOOK_LIMIT` and writes nothing
(ADR-0058 D6). The claim then reads one page of ids (`CLAIM_PAGE_ROWS` =
ceiling / 2 = 5,000, under `{ isSystem: true }`), re-owns that page, and
retries the whole remainder until one write carries it.
- **The page write keeps the full predicate** and ANDs `id: { $in: page
}` onto it. The twin writes the page by id alone. Here, a row claimed by
someone else between the page read and its write keeps its owner, and
the write never leaves the organization.
- **`count` is the sum of the affected-row counts** of every write the
claim made for the object, never a length the function counted itself.
- **Termination guards, each with a warning:** a page that re-owns 0
rows stops the claim (otherwise it would read the same page forever); so
does a refused write whose page read finds nothing, and a result that is
not an affected-row count. A 1,000-page belt covers a driver that
over-reports. Each object is best-effort: a failure is logged with the
count already claimed, that count stays in the result, and later objects
are still claimed.
- `CLAIM_WRITE_CTX = { isSystem: true, skipAutomations: true }` (#23008)
is unchanged and is on every write.
Changeset: `.changeset/23054-org-seed-claim-paged.md` (patch,
`@objectstack/organizations`).
One test-infrastructure line beyond the claim file and its tests:
`packages/plugins/organizations/vitest.config.ts` gains one anchored
alias, `@objectstack/spec/data` to `packages/spec/src/data/index.ts`.
The claim and its tests now import `MAX_BULK_PER_ROW_HOOK_ROWS` and
`BULK_PER_ROW_HOOK_LIMIT_ERROR_CODE` as values, and
`check:test-source-alias` refused the suite reading them from `dist/`.
This alias is the gate's prescribed fix (precedent:
`packages/triggers/trigger-record-change/vitest.config.ts`).
## Measurements against the dispatch's hypotheses
- **H1 holds** on `84c4fffbc`. Line 120 is `{ where: { organization_id:
organizationId, owner_id: null }, limit: 10_000, fields: ['id'] }`, and
line 134 is the per-row `ql.update(schema.name, { id: row.id, owner_id:
ownerUserId }, { context: CLAIM_WRITE_CTX })`.
- **H2 holds: the shape fits.**
- The two doors it uses, `find` and `update(object, data, options?:
any)`, are already named on `OrgScopingEngine`, so
`org-scoping-engine.ts` is untouched.
- On a real `ObjectQL` over `SqliteWasmDriver`, with one code-registered
hook on the object, a `multi: true` write over 10,500 rows under `{
isSystem: true, skipAutomations: true }` is refused with `code:
ERR_BULK_PER_ROW_HOOK_LIMIT, limit: 10000`, nothing written. The claim
then pages to every row. Pinned in the new real-engine case.
- The `tenant-audit-census` row for this file is unchanged (`update`,
`schema.name`, `undecidable`, `elevated`, 1): the write still names
`schema.name` at its call site.
- **H3 holds on the predicate path.**
- `engine.ts` `buildSession` stamps `skipAutomations` and `skipTriggers`
on the batch context. `dispatchPerRowBeforeHooks` and
`buildPerRowAfterContexts` spread that context into every per-row
context, and `triggerHooks` skips `entry.meta` hooks.
- Measured on the real engine: 0 metadata-hook write dispatches over 12
rows (whole-set write) and over 10,500 rows (paged). 2N code-hook
dispatches, one per row and phase, and every one carries
`skipAutomations: true` and `skipTriggers: true`.
- Positive controls on the same engine: the same predicate shape under
plain `{ isSystem: true }` fires the metadata hook 2N times (12 rows)
and 6 times (3 rows). On the paged run, the page read is seen by the
metadata hook as `beforeFind`/`afterFind` with `isSystem: true` and no
`skipAutomations`.
## Pin: 10,500 owner-less rows are all owned after one pass
- **Real engine** (`claim-org-seed-ownership-dispatch.pin.test.ts`, new
case): `SqliteWasmDriver :memory:`, 10,500 owner-less rows in one
organization, plus one owned row and one row of another organization.
The result is `[{ object: probe_deal, count: 10500 }]`. The read-back
sees 10,502 rows and none of the 10,500 is owner-less. The owned row and
the foreign row are untouched. There are exactly 10,500 per-row
dispatches of each phase.
- **Unit** (`claim-org-seed-ownership.test.ts`): the same pin over a
fake that imports `assertEngineUpdateDispatch` (and refuses any
non-`multi` write) and models D6 with the contract's own code. Also: a
single predicate write and no read below the ceiling; the page/remainder
split (5,000 then 5,500) with the predicate on the page write; a row
claimed between page read and write is left alone; a page that re-owns 0
rows stops after 2 writes and warns; a later failure keeps the count
already landed, and the next object is still claimed.
- **Cost:** the real-engine 10,500 case takes **1,516 ms** on the fix
(verbose run, claim blob `d60e2ef023e0`, the same in both commits). On
the old single-pass code it took 7,877 ms and 8,396 ms (the two ablation
runs below). The unit 10,500 case takes 47 ms. These are shared-box
seconds; the ratio is the portable reading. The dispatch-pin file stays
under 10 s.
## Negative-pin ablation (committed first, trap-restored)
Run twice: on `1faee8fa7` (the fix commit) and again on the final head
`ddefed998` (after the alias commit). Both runs gave the same reading.
The mutation went through `node scripts/ablation-replace.mjs` (anchor
hit x1, blob `d60e2ef023e0` to `7b1b63404b95`, `grep -c
'claimUnowned(io'` 1 to 0, `grep -c 'limit: 10_000'` 1 to 2). It put the
old single-pass block back verbatim: the read at `limit: 10_000` and the
per-row `update` by id. The run was wrapped in a `trap restore EXIT INT
TERM` that restores with `git checkout HEAD --` an absolute path and
compares blob hashes.
No build leg: the subject is imported by relative path
(`./claim-org-seed-ownership.js`) from both test files, so vitest reads
`src/`, not a `dist/`.
Red, real engine:
```text
× ... [#23054] 10 500 owner-less rows are all owned after one pass: refused whole under the flag, then paged to every row 8396ms
AssertionError: expected [ { object: 'probe_deal', ...(1) } ] to deeply equal [ { object: 'probe_deal', ...(1) } ]
- "count": 10500,
+ "count": 10000,
Test Files 2 failed (2)
Tests 8 failed | 5 passed (13)
```
The 7 other reds: the unit fakes refuse a by-id write as not `multi`
(the old shape), so every claiming unit case answers `[]`, and the
"stops after 2 writes" case saw 10,000 writes. The 12-row real-engine
case saw the old per-object read (`beforeFind`/`afterFind`) where the
fix issues none.
Restore, proven on disk: `blob after restore d60e2ef == blob at
HEAD d60e2ef`, `git diff HEAD` empty, `git status --porcelain`
empty. Green re-run from the committed state: `Tests 13 passed (13)`.
The second run's restore proof is the same: `trap-restore: blob
d60e2ef... == HEAD blob`, `git diff HEAD` empty.
## Local verification (HEAD `ddefed998`)
- `pnpm --filter @objectstack/organizations test`: `Test Files 13 passed
(13) · Tests 160 passed (160)`.
- `pnpm --filter @objectstack/organizations typecheck` (`tsc --noEmit` +
`check:test-typecheck`, test layer included): green, 0 debt.
- Gates: `node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack` derived the same 64 commands as the
dispatch's list. See the report for the per-command readings and the two
that need a prerequisite this checkout lacks.
## Acceptance notes
- **Below the ceiling the claim no longer reads.** A metadata-bound
`beforeFind` hook on the object therefore no longer sees the claim, and
no longer narrows which rows it selects. The selection is the write's
own predicate, composed by the same write-scoping middleware chain as
any predicate write. The twin has the same shape. The page read on the
paged path is still a plain system read.
- **The unit of failure moved from the row to the write.** A hook or
validation rule that refuses a predicate write refuses all of it, so
that object (or page) stays owner-less, and the warning names the object
and what was already claimed. The twin made the same trade.
- **The unreleased sibling changeset will read stale next to this one.**
`.changeset/23008-org-seed-claim-skip-automations.md` says "The read is
unchanged. The claim's per-object read is still a plain system read."
When both compile into the same release, this PR's changeset states the
new behaviour. The 23008 file is not edited here; whoever compiles the
release may want to drop that bullet.
- **The read no longer accepts a `{ records }` envelope,** matching the
twin, which measured that `ObjectQL.find` resolves a bare array. An
engine that answered an envelope would page nothing and get a warning,
not a silent pass.
- **Not filed (read-only inference, not reproduced):** the twin's page
write is by id alone (`{ id: { $in: ids } }`), so a row given a human
owner between its page read and its write would be re-owned to the
platform admin. It is in `plugin-security`, epic #15194 territory, which
this card does not write.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01FzQy26KznSa5j2gnf3AzsE)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent f93bf12 commit 275ad42
5 files changed
Lines changed: 601 additions & 106 deletions
File tree
- .changeset
- packages/plugins/organizations
- src
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| |||
24 | 26 | | |
25 | 27 | | |
26 | 28 | | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
32 | 33 | | |
33 | | - | |
34 | | - | |
35 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
36 | 41 | | |
37 | 42 | | |
38 | 43 | | |
39 | 44 | | |
40 | 45 | | |
| 46 | + | |
41 | 47 | | |
42 | 48 | | |
43 | 49 | | |
| |||
174 | 180 | | |
175 | 181 | | |
176 | 182 | | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
186 | 187 | | |
187 | 188 | | |
188 | 189 | | |
| |||
198 | 199 | | |
199 | 200 | | |
200 | 201 | | |
201 | | - | |
202 | | - | |
| 202 | + | |
| 203 | + | |
203 | 204 | | |
204 | 205 | | |
205 | | - | |
206 | | - | |
207 | | - | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
208 | 211 | | |
209 | 212 | | |
210 | 213 | | |
| |||
225 | 228 | | |
226 | 229 | | |
227 | 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 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
228 | 295 | | |
0 commit comments