Repository navigation
Commit c480f33
fix(objectql): the loser of a concurrent update answers 409 again on an object with a metadata beforeUpdate hook (#35165)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01JQwxbXGvw43spR79ztEvWt
Fixes #35142
Clause-②: no
Metadata-protocol: none
## 维护者速读
- **改了什么**:凡是自开事务单元的 update(对象带元数据 `beforeUpdate` 钩子,或带阻断式 / 带条件的元数据
`afterUpdate` 钩子),「after-abort
事务单元」原先包住整个写入(含授权中间件),现在只包住引擎本体(钩子、写行、after 链);中间件在事务开启之前运行,与无钩子对象一致。
- **为什么改**:在 SQLite(单连接)上两人同时认领同一条记录时,输家等赢家提交后才做授权读,被拒为 404(private)或
403(public_read),而不是 ADR-0142 D4 承诺的 `409
CONCURRENT_UPDATE`。`beforeUpdate` 钩子一支自 18.6.0 起出错(18.5.0 为 409);阻断式
`afterUpdate` 钩子一支在 18.5.0 已是 404/403。HotCRM 的 18.6.0 升级被它挡住。
- **风险与回滚**:风险在于中间件 `next()` 之后的代码对这类 update
改为在提交之后运行,与无钩子对象一致;`beforeUpdate` 一支等于回到 18.5.0,阻断式 `afterUpdate`
一支则是新变化(18.0.0 至 18.6.0 都在事务内运行)。钩子侧写回滚(#34503 的目的)不变。回滚 = revert 本 PR 的
`engine-update.ts` 一处改动。
- **先看**:`packages/objectql/src/engine-update.ts`(门入口
`ownUnit`/`inOwnUnit` 与 `executeWithMiddleware(opCtx, inOwnUnit(...))`
两处);`packages/plugins/plugin-security/src/ownership-claim-race-sqlite.test.ts`。
- **两分钟验证**:`cd packages/plugins/plugin-security && pnpm exec vitest run
src/ownership-claim-race-sqlite.test.ts`,预期 7/7 通过;五格各 25 轮,输家全部为
`CONCURRENT_UPDATE/409`。
- **需要你做的**:无,席位落地。
## What changes
`UpdateFacet.update` no longer re-enters itself inside
`inAfterAbortUnit` at the door. It decides at the door whether the write
needs its own unit (`opensAfterAbortUnit`, unchanged), and wraps only
the executor it hands to `executeWithMiddleware` in that unit. So the
middleware chain (the authorization gate's caller-scoped pre-image read
among it) runs before `BEGIN`, the way it does for an update that opens
no unit, and everything the unit exists for (the `before*` hooks and
their `ctx.api` side writes, the row write, the `after*` chain) still
runs inside it and rolls back together.
The change applies to any update that opens its own unit, which is
`opensAfterAbortUnit`'s whole answer, and that predicate has two arms:
- **`sideWriting`: a metadata `beforeUpdate` hook.** `a8e09bbc31`
(#34503) added this term (traced in the card's trace comment), new in
18.6.0. Race answer: `409` in 18.5.0, `404` / `403` in 18.6.0, `409`
again here.
- **`aborting`: a blocking (`onError` `abort`, the default) or
conditioned metadata `afterUpdate` hook (#22782).** Its door-level unit
already shipped in `@objectstack/objectql@18.5.0`:
`engine-update.ts:151-153` at tag `@objectstack/objectql@18.5.0`
(`b914e7247f`) reads `if (this.opensAfterAbortUnit('afterUpdate',
object, options?.context)) { return this.inAfterAbortUnit(() =>
this.update(object, data, options)); }`, and that tag's
`opensAfterAbortUnit` has the `aborting` term and no `sideWriting` term.
Race answer: `404` / `403` already in 18.5.0 and in 18.6.0 (the new
fifth cell reads `RECORD_NOT_FOUND/404` x25 on main's
`engine-update.ts`), `409` here for the first time.
With the unit around the whole door, the SQLite driver's single
connection made the loser's whole write, gate included, wait for the
winner's commit. The gate then read a row owned by the winner and
refused: 404 under `private`, 403 under `public_read`. The conditional
write whose 0-row answer is `ConcurrentUpdateError` was never reached.
Insert and delete keep their door-level unit. Measured with the same
rig, 20 rounds per cell, `private` and `public_read`, with and without a
metadata `before*` hook on the verb:
- Concurrent insert of one id: the loser answers `DUPLICATE_RECORD/409`
in all four cells. No race-shaped defect, so no change.
- Concurrent delete of one row: with no hook the loser's `delete`
resolves `false`; with a hook it answers `RECORD_NOT_FOUND/404`. Both
say the row is gone, which it is. D4 governs updates, and the 404 is not
a lie about a readable row. So it stays out of this PR's scope (see
Acceptance notes).
What a middleware does after `next()` on an update that opens its own
unit now runs after the commit, as it does on every update that opens no
unit. For the `beforeUpdate` arm that is how 18.5.0 behaved. For the
`afterUpdate` arm it is a change, not a return: in every release from
18.0.0 (the first `@objectstack/objectql` tag whose source calls
`opensAfterAbortUnit`; 17.7.0 has 0 hits) through 18.6.0, that code ran
inside the row's unit on an object with a blocking or conditioned
metadata `afterUpdate` hook. The post-`next()` code that runs on an
update in this tree:
- plugin-security authz: the `postHookWriteImageCheck` honoured-flag
assertion (it fails closed if the engine never ran the seam; the engine
always runs it inside the executor), then field-level masking of the
result (no write).
- plugin-security start hooks: the platform-admin bootstrap replay
(`shouldReplayBootstrapFor`) and the `sys_member` org-admin grant
reconcile (writes, `sys_member` only).
- organizations, plugin-auth, runtime app-plugin, plugin-sharing:
post-`next()` branches that act on insert/create only.
On a `sys_member` update that opens its own unit, the org-admin grant
reconcile is no longer in the same unit as the row. With a metadata
`beforeUpdate` hook that matches 18.5.0; with a blocking or conditioned
metadata `afterUpdate` hook it is new. Either way it matches every
unhooked object.
## Tests
New pin
`packages/plugins/plugin-security/src/ownership-claim-race-sqlite.test.ts`:
the real `ObjectQL`, `SqlDriver` on better-sqlite3 `:memory:`,
`SecurityPlugin` and `SharingService`. It has five cells, 25 rounds
each: `private`/`public_read`, each with and without a metadata
`beforeUpdate` hook, plus `private` on an object whose only metadata
hook is a blocking `afterUpdate` (`onError: 'abort'`), which holds the
predicate's other arm. Every round asserts that exactly one claim
fulfils, that the loser's `{code}/{status}` is `CONCURRENT_UPDATE/409`,
and that the stored owner is the winner. Two non-regression cases run on
the hooked `private` object: a deleted row answers
`RECORD_NOT_FOUND/404`, and a row owned by someone else answers as not
found, never as forbidden (no existence leak).
<details><summary>Red on main: test commit with origin/main's
engine-update.ts (blob dc1d739)</summary>
```
× exactly one claim lands ... (private, hooked) AssertionError: expected { 'RECORD_NOT_FOUND/404': 25 } to deeply equal { 'CONCURRENT_UPDATE/409': 25 }
× exactly one claim lands ... (public_read, hooked) AssertionError: expected { 'PERMISSION_DENIED/403': 25 } to deeply equal { 'CONCURRENT_UPDATE/409': 25 }
unhooked cells and the no-leak case: green
```
Fifth cell (blocking `afterUpdate` only), commit `415eefd791` with
origin/main `7d8dcf693a`'s `engine-update.ts` (blob `71476062da`, door
re-entry line present x1), swapped in a script with an EXIT/INT/TERM
trap:
```
FAIL ... private OWD, metadata beforeUpdate hook (the unit opens)
FAIL ... private OWD, only a blocking metadata afterUpdate hook (the unit opens)
AssertionError: expected { 'RECORD_NOT_FOUND/404': 25 } to deeply equal { 'CONCURRENT_UPDATE/409': 25 }
FAIL ... public_read OWD, metadata beforeUpdate hook (the unit opens)
AssertionError: expected { 'PERMISSION_DENIED/403': 25 } to deeply equal { 'CONCURRENT_UPDATE/409': 25 }
Tests 3 failed | 4 passed (7)
restore: blob b8273f9 == HEAD, git diff HEAD empty
```
(A third red in that run came from the not-found case's own setup, which
deleted without a principal. Commit `06f9b81ab8` fixed the setup; the
case is green on main and on the fix.)
</details>
<details><summary>Green on the fix: 4 runs x 25 rounds per cell (four
cells), then 2 runs with the fifth cell</summary>
```
Test Files 1 passed (1)
Tests 6 passed (6) (run 1, then 3 more runs: 6/6 each)
five cells, at 415eefd and again at 99c200d (same test and engine bytes as head d9fe917):
Test Files 1 passed (1)
Tests 7 passed (7)
typecheck @objectstack/plugin-security (tsc + check:test-typecheck): exit 0
```
</details>
<details><summary>Ablation: the door-level re-entry restored through
scripts/ablation-replace.mjs</summary>
```
anchor x1 -> x0, blob b8273f9 -> d59167c32afe
× private, hooked: { 'RECORD_NOT_FOUND/404': 25 }
× public_read, hooked: { 'PERMISSION_DENIED/403': 25 }
Tests 2 failed | 4 passed (6)
restore: blob == HEAD (b8273f9) and git diff HEAD is empty
```
`@objectstack/objectql` resolves to `src/` in this suite (the
plugin-security vitest alias), so the mutation reaches the run with no
`dist/` leg.
</details>
<details><summary>After-abort unit pins (#34412 / #34503) and the
objectql suite</summary>
```
engine-after-hook-abort-unit.test.ts, engine-hook-stale-summary-refusal.test.ts, ownership-queue.test.ts: 3 files, 74 tests passed
@objectstack/objectql (vitest --project local): Test Files 611 passed (611), Tests 13791 passed (13791)
typecheck @objectstack/objectql, @objectstack/plugin-security: exit 0
```
</details>
## Acceptance notes
- Concurrent delete answers the loser differently by unit shape: `false`
with no metadata `beforeDelete` hook, `RECORD_NOT_FOUND/404` with one.
No declared contract picks between them, and neither misreports a
readable row. Noted, not filed.
- Insert and delete keep their door-level unit, so their middleware's
post-`next()` work stays inside it while update's does not. Whether they
should move the same way is carried by #35167.
- overlap-check at `d9fe9175e7`: origin/main and #34978 (shares
`engine-update.ts`) both merge cleanly.
## Verification
On head `d9fe9175e7` (merge base `99a0e4fbf`; overlap-check against
origin/main and #34978: merges cleanly, so main is not merged in again),
`node scripts/pm/dispatch-gates.mjs --commands` derived 70 families, and
all 70 exit 0 (`check:dual-build-cjs-loads` and `check:i18n` found their
dists this time and measured). `check:query-options-erasure` and
`check:type-check-debt` each outran a 240 s per-gate runner timeout and
exited 0 when rerun alone. `--ran` reconciliation with exit codes: 70
derived, 70 run, 0 NOT-MEASURED, 0 UNRUN. Between `99c200dbb1` and
`d9fe9175e7` only the changeset text moved, so the race-test runs above
(7/7 at `99c200dbb1`) read the same test and engine bytes. `test:repo`
and the plugin-security suite outside the new file are left to CI (Test
Core). The objectql public surface is byte-unchanged.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01JQwxbXGvw43spR79ztEvWt)_
<!-- signed: repo:objectstack#1 · os-warren ·
session_01JQwxbXGvw43spR79ztEvWt -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent c9b3f7a commit c480f33
3 files changed
Lines changed: 222 additions & 5 deletions
File tree
- .changeset
- packages
- objectql/src
- plugins/plugin-security/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 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
161 | | - | |
162 | | - | |
163 | | - | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
164 | 175 | | |
165 | 176 | | |
166 | 177 | | |
| |||
461 | 472 | | |
462 | 473 | | |
463 | 474 | | |
464 | | - | |
| 475 | + | |
465 | 476 | | |
466 | 477 | | |
467 | 478 | | |
| |||
2374 | 2385 | | |
2375 | 2386 | | |
2376 | 2387 | | |
2377 | | - | |
| 2388 | + | |
2378 | 2389 | | |
2379 | 2390 | | |
2380 | 2391 | | |
| |||
| 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 | + | |
0 commit comments