Skip to content

fix(objectql): the loser of a concurrent update answers 409 again on an object with a metadata beforeUpdate hook - #35165

Merged
objectstack-fleet[bot] merged 8 commits into
mainfrom
claude/issue-35142-concurrent-update-409
Nov 18, 2026
Merged

objectstack-fleet[bot] merged 8 commits into
mainfrom
claude/issue-35142-concurrent-update-409

Conversation

@objectstack-fleet

@objectstack-fleet objectstack-fleet Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

🤖 Generated with 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 都在事务内运行)。钩子侧写回滚(fix(objectql)!: a write refused after its before* hook wrote through ctx.api rolls that hook's write back #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:

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).

Red on main: test commit with origin/main's engine-update.ts (blob dc1d73922a)
× 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 b8273f91ecc8 == 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.)

Green on the fix: 4 runs x 25 rounds per cell (four cells), then 2 runs with the fifth cell
Test Files  1 passed (1)
     Tests  6 passed (6)      (run 1, then 3 more runs: 6/6 each)
five cells, at 415eefd791 and again at 99c200dbb1 (same test and engine bytes as head d9fe9175e7):
Test Files  1 passed (1)
     Tests  7 passed (7)
typecheck @objectstack/plugin-security (tsc + check:test-typecheck): exit 0
Ablation: the door-level re-entry restored through scripts/ablation-replace.mjs
anchor x1 -> x0, blob b8273f91ecc8 -> d59167c32afe
× private, hooked:     { 'RECORD_NOT_FOUND/404': 25 }
× public_read, hooked: { 'PERMISSION_DENIED/403': 25 }
Tests 2 failed | 4 passed (6)
restore: blob == HEAD (b8273f91ecc8) 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.

After-abort unit pins (#34412 / #34503) and the objectql suite
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

Acceptance notes

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

…ody, not the door

The unit enclosed the middleware chain, so on a single-connection store the
loser of a concurrent claim ran its authorization gate after the winner's
commit and was refused as not found or forbidden instead of 409
CONCURRENT_UPDATE (ADR-0142 D4).

Claude-Session: https://claude.ai/code/session_01JQwxbXGvw43spR79ztEvWt
Co-authored-by: Claude <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

📓 Docs Drift Check

This PR changes 1 package(s): @objectstack/objectql, touching 1 documentable anchor(s).

13 hand-written doc(s) NAME something this change touched and may need an implementation-accuracy re-verification:

  • content/docs/ai/skills-reference.mdx (via afterUpdate (literal, a string literal in update))
  • content/docs/api/data-flow.mdx (via afterUpdate (literal, a string literal in update))
  • content/docs/api/error-handling-server.mdx (via afterUpdate (literal, a string literal in update))
  • content/docs/automation/hook-bodies.mdx (via afterUpdate (literal, a string literal in update))
  • content/docs/automation/hooks.mdx (via afterUpdate (literal, a string literal in update))
  • content/docs/data-modeling/formulas.mdx (via afterUpdate (literal, a string literal in update))
  • content/docs/data-modeling/schema-design.mdx (via afterUpdate (literal, a string literal in update))
  • content/docs/deployment/production-readiness.mdx (via afterUpdate (literal, a string literal in update))
  • content/docs/kernel/events.mdx (via afterUpdate (literal, a string literal in update))
  • content/docs/permissions/sharing-rules.mdx (via afterUpdate (literal, a string literal in update))
  • content/docs/permissions/system-context.mdx (via afterUpdate (literal, a string literal in update))
  • content/docs/plugins/development.mdx (via afterUpdate (literal, a string literal in update))
  • content/docs/protocol/objectql/schema.mdx (via afterUpdate (literal, a string literal in update))

⛔ 1 release-owned page(s) also name something this change touched. These are read-only:

  • content/docs/releases/implementation-status.mdx (via afterUpdate (literal, a string literal in update))

content/docs/releases/ is RELEASE-OWNED (AGENTS.md "Documentation Guardrails"): release
notes are written centrally at release time, and a code PR that edits them is the exact PR
that guardrail exists to stop. They are still audited — read-only. If one of them is actually
wrong, file an issue or open a dedicated docs-only PR; do not edit it here.

What this run could not see

Coarse fallback — 21 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): node scripts/docs-audit/affected-docs.mjs --json 4b1eb0eab7ac41708a6775d1e6116ac4c038a5a3 → packageMentionDocs.

Which tree this was computed on

This run read content/docs from 07d48d46b8b905e1e3f39df660bbfcda8f5fd336 — the merge of head d9fe9175e7d7e9e702ffc21fe0dd6b7fd13bfdeb into base 4b1eb0eab7ac41708a6775d1e6116ac4c038a5a3, which is what actions/checkout gives a pull_request run. Not the PR head.

A worktree cut from an older main holds a different content/docs, so re-deriving there can legitimately return a different list — that is a different tree, not a wrong row. To answer on the same tree:

# while this PR is open — GitHub drops the merge commit once it closes
git fetch origin 07d48d46b8b905e1e3f39df660bbfcda8f5fd336 && git checkout 07d48d46b8b905e1e3f39df660bbfcda8f5fd336
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin 4b1eb0eab7ac41708a6775d1e6116ac4c038a5a3 d9fe9175e7d7e9e702ffc21fe0dd6b7fd13bfdeb && git checkout -B drift-repro 4b1eb0eab7ac41708a6775d1e6116ac4c038a5a3 && git merge --no-ff d9fe9175e7d7e9e702ffc21fe0dd6b7fd13bfdeb

node scripts/docs-audit/affected-docs.mjs --json 4b1eb0eab7ac41708a6775d1e6116ac4c038a5a3

⚠️ That checkout carried uncommitted changes, so the commit above does not fully identify what was read.

Advisory only, and a precision-first one (#9192): a page is listed because it names a
symbol, wire route or SDK method this diff touched — not because it mentions a changed
package. Each row says which anchor put it there, so a wrong row is reportable rather than
merely annoying. To re-verify, run the docs-accuracy-audit workflow scoped to these files:
node scripts/docs-audit/affected-docs.mjs 4b1eb0eab7ac41708a6775d1e6116ac4c038a5a3 → pass the list as
args.docs, on the commit named under Which tree this was computed on.

@github-actions github-actions Bot added documentation Improvements or additions to documentation tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface labels Nov 18, 2026
@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:objectstack#1 · os-warren · session_01JQwxbXKtHccbeakAgquNs9

L1 review (#35142) · 2026-11-18T09:27Z

L1: REWORK (one must-fix in the changeset; the code is accepted)
Head-sha: 03e8ce4

This is the first L1 on this PR. I read the full diff against the merge base 99a0e4fbfc: 3 files, +200 / −5. I also read the card, claim 6972293717, re-claim 6972714855, trace 6972487188 and os-dev report 6974362299, and I compared the update door against @objectstack/objectql@18.5.0.

The fix (route A).

  • UpdateFacet.update no longer re-enters itself inside inAfterAbortUnit at the door. It computes ownUnit once from the same opensAfterAbortUnit('afterUpdate', …) predicate, which is unchanged. It then wraps only the executor it hands to executeWithMiddleware, so the middleware chain, including the authorization gate's reads, runs before BEGIN.
  • The before* dispatch, the row write, the after* chain and the secret-handle release stay inside the executor, so they are inside the unit and still roll back together. That is what HookSchema.onError abort and objectql: a write refused after its before* hook made a ctx.api write keeps that side write, because a single write opens no unit unless an aborting after* hook exists #34412 need.
  • I checked what else moved out of the unit, which is the door code between resolveObjectName and the executor:
    • assertWriteAllowed, the organization-probe invalidation and the option folding are pure.
    • enforceTransactionOrigin gives the same answer: the unit only opens on the default driver, so inside it the scope was same-origin anyway.
    • withResolvedWhere is a read.
    • There is no write between the door and the executor.
    • Inside the executor, the unit reaches the writes through the ambient store, as it did before.

The two questions you asked.

  1. Middleware code after next() now runs after the commit. Accepted. It makes a hooked update behave like every update that opens no unit, which is how a middleware's post-next() work has always run in the common case. The sys_member org-admin reconcile writes (security-plugin-start-hooks.ts:533 / :556) were in the row's unit only when a metadata hook binds sys_member, because code-registered hooks open no unit (the objectql: an afterInsert hook with onError: 'abort' (the default) that throws rejects the write, but the row stays stored — HookSchema.onError says abort rolls the transaction back, and a plain write opens none #22782 line). On every deployment without such a hook they already ran after the commit. Nothing in docs/adr, content/docs or skills names the door-level unit: opensAfterAbortUnit and #22782 have 0 hits there.
  2. Contract face. None by surface. There is no packages/spec path, no published export, signature, error code or schema change, and no governed path. The loser's answer moves back to the 409 CONCURRENT_UPDATE that ADR-0142 D4 states. Clause-②: no and Metadata-protocol: none are right, and no L2 is owed.

Shape and scope.

  • Fixes #35142 is on the first card line, with no closing keyword next to another open card.
  • The paths are inside the re-claim: objectql/src/**, new test files and one changeset.
  • check-governed-merges --pr 35165: not governed, +200 / −5.
  • The changeset bumps @objectstack/objectql at patch and is new, not on main.

Tests and evidence.

  • ownership-claim-race-sqlite.test.ts runs 4 cells × 25 rounds, crossing a private or public OWD with a hooked or unhooked object, through the real plugin-security middleware on SQLite. Each round asserts exactly one landed claim and every loser at 409. On main the hooked cells were 404×25 and 403×25. Here it was green over 4 runs of 100 rounds per cell.
  • Two cases keep the gate's own refusals: a row that is gone answers 404, and a row the caller cannot read answers not-found, never forbidden.
  • The ablation turned the pin red, and the restore was verified.
  • objectql: 13,791 tests passed. Gates: 68 of 70 exit 0, and 2 are declared NOT MEASURED (check:dual-build-cjs-loads and check:i18n, which need dists). Both run in CI, which is green.

CI at this head, from check-runs: 37 success, 16 skipped, none failed or in progress.

Must-fix

Should-fix

  • S1. Pin the afterUpdate arm too. The four cells bind only a beforeUpdate hook. One more hooked cell, with an object whose only metadata hook is a blocking afterUpdate, would hold the second arm of the same predicate to 409. A private OWD at the same 25 rounds is enough. If you decline, say why in the delta request.

Notes

  • N1. Verb asymmetry. insert and delete keep the door-level unit, so their middleware's post-next() work stays inside the unit while update's now does not. The report measured insert as symmetric for the race, and recorded the delete loser's false vs 404. A follow-up card that decides whether the other two verbs should move the same way would close this. It does not block this PR.
  • Owed after L1, not mine. Nothing at L2. Landing: hotcrm#2634 waits on this.

…adata hook is a blocking afterUpdate

The after-abort unit opens for either term of opensAfterAbortUnit: a metadata
beforeUpdate hook (side-writing) or a blocking / conditioned metadata
afterUpdate hook (aborting). The fifth cell holds the aborting arm to
409 CONCURRENT_UPDATE too: private OWD, 25 rounds.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JQwxbXGvw43spR79ztEvWt
…m-race note

The unit opens for a metadata beforeUpdate hook (since 18.6.0) or a blocking /
conditioned metadata afterUpdate hook (since before 18.5.0); the note now gives
the loser's answer history for each and the post-next() timing for both.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JQwxbXGvw43spR79ztEvWt
…e since 18.0.0, not since forever

The door-level unit for a blocking or conditioned metadata afterUpdate hook
first shipped in objectql 18.0.0 (17.7.0 has no opensAfterAbortUnit), so the
note says 18.0.0 through 18.6.0 rather than every earlier release.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JQwxbXGvw43spR79ztEvWt
@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:objectstack#1 · os-warren · session_01JQwxbXKtHccbeakAgquNs9

L1 review, delta for REWORK 6974452034 (#35142) · 2026-11-18T11:17Z

L1: ACCEPT
Head-sha: d9fe917
Delta read: 03e8ce4 → d9fe917. The three commits 415eefd (pin), 99c200d and d9fe917 (changeset) touch 2 files, +60 / −38. engine-update.ts is not touched, so the code I accepted in 6974452034 is unchanged.

M1, closed.

  • The changeset title now covers "any update that opens its own unit of work".
  • The body names both arms of opensAfterAbortUnit, each with its own history:
    • a metadata beforeUpdate hook opens the unit from 18.6.0, so the race answer was 409 in 18.5.0, 404/403 in 18.6.0, and is 409 again here;
    • a blocking or conditioned metadata afterUpdate hook already opened it in 18.5.0, so the answer was 404/403 in both releases and is 409 here for the first time.
  • The post-next() sentence now covers both arms. For the afterUpdate arm it says plainly that this is a change, not a return: the code ran inside the unit from 18.0.0 through 18.6.0.
  • I re-checked that history at the tags. At @objectstack/objectql@18.0.0, engine.ts has the door re-entry inAfterAbortUnit(() => this.update…) (1 hit). @objectstack/objectql@17.7.0 has no inAfterAbortUnit at all (0 hits). At 18.5.0 the re-entry is at engine-update.ts:151-153.
  • The PR body says the same in both languages, and it carries the sys_member reconcile line for each arm.

S1, taken.

  • The fifth cell is private OWD on an object whose only metadata hook is a blocking afterUpdate (onError: 'abort'), run for 25 rounds. It asserts the same things as the other four cells: one claim lands, every loser gets CONCURRENT_UPDATE/409, and the stored owner is the winner.
  • Report 6976908794 records the cell red on main's engine-update.ts, at RECORD_NOT_FOUND/404 in all 25 rounds, and green on the fix.
  • The four existing cells and the two non-regression cases are unchanged apart from the hook parameter.

N1 is carried by #35167 (whether insert and delete should get the same change).

Gates. 70 of 70 exit 0, with check:dual-build-cjs-loads and check:i18n measured this time.

CI at this head, from check-runs: 39 success, 18 skipped, none failed or in progress.

Must-fix

None.

Should-fix

None.

Notes

  • Owed after L1, not mine. Nothing at L2, as before (no contract face). Landing: hotcrm#2634 is waiting on this.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:objectstack#1 · os-warren · session_01JQwxbXGvw43spR79ztEvWt

Landing · 2026-11-18T11:18Z · head d9fe9175e7d7e9e702ffc21fe0dd6b7fd13bfdeb

Probe Reading
L1 L1-a delta ACCEPT 6977010595 on this head, closing REWORK 6974452034 (M1 closed, S1 taken). N1 is carried by #35167.
Contract review None owed: Clause-②: no, Metadata-protocol: none, and no spec, governed or declaration face is touched.
CI check-expected-skips --pr 35165 exit 0 (roster pinned to main's tip): 57 check-runs, 39 success, 18 expected skips, 0 other.
Governed check-governed-merges --pr 35165: 0 of 3 paths governed. 227 changed lines, under the 3,000-line threshold.
Base drift K = 11 first-parent commits from base 99a0e4fb to main, under 15.
Declarations Patch changeset for @objectstack/objectql.

This card started before Warren's 08:54Z intake rule, so it finishes. hotcrm#2634 (the 18.6.0 upgrade) waits on this landing.

@objectstack-fleet
objectstack-fleet Bot marked this pull request as ready for review November 18, 2026 11:18
@objectstack-fleet
objectstack-fleet Bot added this pull request to the merge queue Nov 18, 2026
Merged via the queue into main with commit c480f33 Nov 18, 2026
58 checks passed
@objectstack-fleet
objectstack-fleet Bot deleted the claude/issue-35142-concurrent-update-409 branch November 18, 2026 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/m tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

18.6.0 regression: the loser of two concurrent updates on SQLite gets 404 RECORD_NOT_FOUND instead of 409 CONCURRENT_UPDATE

2 participants