You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 9524666
Browse filesBrowse the repository at this point in the historyBrowse files
fix(plugin-auth): the self-registration grant settles on the registrant's first membership (ADR-0131 D3, C8 8b-0f) (#24453)
Part of #24343
Part of #15212
Clause-②: no
C8 stage 8b-0f. It carries ruling Q1 A on #24377 (record 6257510973,
item 1): the self-registration grant settles on the first membership.
The card stays open because Q2 (stage 8b-0d) still lands on it.
## What changed
The declared self-registration set
(`auth.audience.selfRegistrationPermissionSet`) is an assignment, and
ADR-0131 D3 makes an assignment an organization row. So the grant is now
written with the registrant's first membership and owned by that
membership's organization, under every posture. Nothing is written to
`sys_user_permission_set` without an organization.
All runtime changes are in
`packages/plugins/plugin-auth/src/auth-manager.ts`:
- **`settleSelfRegistrationGrant`** (`user.create.after`). It consumes
the admission stage as before, then reads the registrant's membership.
- The registrant holds one (under `single`, the reconciler bound the
Default Organization a moment earlier in the same hook; or a host hook
provisioned an organization first). The grant lands now, owned by that
organization.
- The registrant holds none (a wall). No grant is written. A pending
claim is persisted instead.
- **The pending claim** is a `sys_verification` row. Its `identifier` is
`self-registration-grant:` plus the user id, its `value` is the set
name, and it waits 365 days.
- `sys_verification` is the auth layer's existing store of one-shot
claims keyed on an identifier with an expiry. It survives a restart, and
it has no organization column (ADR-0131 D1 fate 1, inventory
`noColumn`).
- No new object, no new column, no new field.
- **The first-membership seam.** `observeFirstMemberships` is registered
once per engine, from the constructor. It is a middleware on
`sys_member` inserts, the same seam plugin-security's
`organization_admin` auto-grant uses. Every membership writer inserts
through the engine: better-auth's organization create (the creator as
owner), invitation acceptance and server-side add-member, the membership
reconciler, the Default Organization bootstrap, and host hooks. One seam
therefore covers all of them, with no hook per path.
- **`settleDeferredSelfRegistrationGrant`** consumes the claim first
(read, delete by id), then writes the grant owned by that membership's
organization. A later membership finds no claim and writes no second
grant. The existing by-name check also holds.
- An engine with no middleware seam could never settle a claim. There, a
registrant with no organization is reported ungranted at `error` at
sign-up, and no claim is parked.
- **Helper pair, a minimal edit (named as the dispatch asks).** The doc
comment on `derivableWriteOrganizationId` no longer lists the
self-registration grant among its readers, because the grant does not
read it any more. Its code and `authMailOrganizationArg` are unchanged,
and no identity-mail send site was touched.
## Measured (A1–A5)
- **A1, the membership paths.** The writers of a user's `sys_member` row
on this tree are: better-auth `organization/create` (owner) and
`accept-invitation`, better-auth add-member, `reconcileMembership`
(`user.create.after`, `session.create.before`, the one-time backfill),
`ensureDefaultOrganization`'s owner bind, and host `user.create.after`
hooks. All of them write through the data engine, so the `sys_member`
insert middleware sees each one (#4802's corrected mechanism, quoted in
`auth-manager.ts`). The walled dogfood pin drives the real better-auth
organization create through it. Under `single`, the census pin shows the
sign-up still ends with exactly one grant, owned by the Default
Organization.
- **A2, persistence.** Admission decides "self-registrant" from the
request: the creation class, the bootstrap probe and the
pending-invitation lookup. None of those is stored. `sys_user.source`
separates only `idp_provisioned` from `env_native`, and
`sys_account.provider_id` is `credential` for admin-created users too.
So no existing user or account field marks a self-registrant, and a
persisted claim is needed. `sys_verification` is the narrowest existing
store that fits: deployment-level, durable, and with an expiry
better-auth already sweeps (`findVerificationValue` deletes expired
rows). The `value` column is `internal`, so it is read back through
`recoverInternalFieldsForSystemRead`.
- **A3, existing NULL grants.** Untouched here (the ceremony is
8b-0c's). Measurement and recommendation are in the report on #24343.
- **A4, pins.** See Tests.
- **A5, Clause-②.** `no`. No spec, schema, export or route changes.
`AudienceConfigSchema.selfRegistrationPermissionSet` describes "the
permission set granted to each self-registrant", and the settings
manifest and its translations say the same. None of them promises the
grant at sign-up time, and the generated reference page carries only
that `describe`. No `packages/spec` byte moved.
## Tests (head `29b223ed3`)
- **New:**
`plugin-auth/src/self-registration-grant-first-membership.test.ts`, 9
cases.
- Under `single`: one grant, owned by the Default Organization, no
claim.
- Walled sign-up: 0 grants, 1 claim with no organization column.
- The first membership: exactly 1 grant, owned by that organization, and
the claim is consumed.
- A second membership: no second grant.
- A restart: a fresh manager over the same store settles the claim.
- No claim (an invited user): no grant. An expired claim: no grant.
- No middleware seam: reported at `error`, nothing parked.
- Two managers on one engine: the seam is registered once.
- **New:**
`dogfood/test/self-registration-grant-first-membership-walled.dogfood.test.ts`,
3 cases, real stack, `isolated` (`posture-only`), open audience. The
sign-up writes no grant. `POST /auth/organization/create` lands exactly
one, owned by the new organization. A second organization lands none.
- **Changed:**
- `audience-posture.test.ts`: the four end-to-end "grant lands" cases
now run `single` (a tenancy service), so the reconciler binds a
membership, and one of them asserts the owner.
- `find-envelope-limb-removal.test.ts`: the D4 grant-by-name case binds
the membership first.
- `producers-name-organization.test.ts`: its two grant cases are
removed. The `single` one is re-pinned in the new file. The walled one
asserted the behaviour this stage retires (a grant with no
organization), and the new walled pins assert the opposite.
- **Ablation** (`scripts/ablation-replace.mjs`, EXIT trap). The claim
write was mutated back to a sign-up grant with no organization (marker
`ups_ABLATED_`, anchor 1→0, blob `debc7651`→`624afb11`). After the
rebuild, `ablation-dist-preflight` found the marker in `dist/index.js`
and `dist/index.mjs`. The DTS step failed on TS6133 (the now-unused
claim method), but the JS that the suite consumes was emitted.
- The walled dogfood pin went red, 2 failed / 1 passed: the sign-up
wrote a grant, and the organization grant read `null`.
- The unit file went red, 5 failed / 4 passed.
- Restore: blob == HEAD, `git diff HEAD` empty, rebuild, preflight
`--absent` exit 0, dogfood 3/3 green.
- **Suites, at `29b223ed3`:** `@objectstack/plugin-auth` test, 165
files, 3165 passed, 10 skipped; typecheck exit 0. Dogfood, after a
plugin-auth rebuild: the walled pin, `signup-organization-census`
(single) and `org-create-default-team` (walled), 3 files / 7 passed.
- **Gates, at `29b223ed3`:** `dispatch-gates.mjs --commands` (no paths),
108 families plus the 51-row artifact roster, each exit code captured
before any pipe. `--ran` reads: 108 derived, 108 run, 0 NOT-MEASURED.
- Fixed on the way: `check-tenant-audit-census` (the riders below),
`check:engine-double-contract` (ledger row), and `check:where-matcher`
(the new double now refuses WHERE combinators).
- Roster rows that need a PR context exited 2 (NOT WIRED) locally:
`check-closing-target-claim`, `check-single-claim-paths`, and
`check-governed-queue-guard` (no event payload, exit 1). CI's reading
counts for those. `check-partof-closing-keyword`, run with this body as
`PR_BODY`, exits 0.
## Riders
- **`content/docs/permissions/tenant-audit-census.mdx` and
`docs/audits/2026-08-tenant-audit-write-call-sites.counts.md`.** These
were regenerated (`node scripts/tenant-audit-census.mjs --write`) and
the page's prose figures updated, because `check-tenant-audit-census`
holds them to the census and this stage moves the write-call-site
population from 229 to 231. The three plugin-auth writes state `{
context: { isSystem: true } }` at the call, so they count as elevated.
The "cite `2 / N`" provable-yield figure stays 2.
- **`scripts/engine-double-contract.pinned.json`:** one row for the new
test's `delete` double (`check-engine-double-contract --write`).
## Acceptance notes
- The claim waits 365 days. A registrant who first joins an organization
later holds only the deployment's baseline. Cloud's report (cloud#3051)
shows its self-registration set is also its `isDefault` baseline, so
nothing changes there.
- Under `single` with `membershipPolicy: 'invite-only'`, a
self-registrant gets no membership at sign-up, so the grant now waits
for the first membership there too, as the ruling's "under every
posture" says. Under `auto` (the default), `single` is unchanged.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01KYBqeqPDxig2tPYhHQxYNX)_
---------
Co-authored-by: Claude <noreply@anthropic.com>
fix(plugin-auth): the self-registration grant settles on the registrant's first membership, owned by that organization (ADR-0131 D3)
6
+
7
+
Clause-②: no
8
+
9
+
The declared self-registration permission set (`auth.audience.selfRegistrationPermissionSet`) is an assignment, and an assignment is an organization row (ADR-0131 D3). It is now written with the registrant's first membership and owned by that membership's organization, under every posture. It is never written without an organization.
10
+
11
+
-**`single`: unchanged.** The membership reconciler binds the Default Organization in the same `user.create.after` hook, so a sign-up still ends with one grant, owned by the Default Organization.
12
+
-**A wall (`group`, `isolated`).** A self-registrant belongs to no organization at sign-up, so the sign-up writes no `sys_user_permission_set` row. It used to write one with no organization. The grant is held as a pending claim in `sys_verification`, which is deployment-level and survives a restart. The first `sys_member` row written for the user lands exactly one grant, owned by that organization. That row can come from creating an organization (the creator becomes its owner), accepting an invitation, being added by an administrator, or any other membership writer. A later membership writes no second grant.
13
+
-**The claim waits 365 days.** A registrant who first joins an organization after that holds only the deployment's baseline. An engine with no middleware seam cannot settle a claim, so there a registrant with no organization is reported ungranted at `error` at sign-up instead.
14
+
- Grants already written without an organization by earlier walled sign-ups are not touched here.
Copy file name to clipboardExpand all lines: content/docs/permissions/tenant-audit-census.mdx
+25-25Lines changed: 25 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -83,7 +83,7 @@ what moved this page's population from 225 to 227; nothing about the two sites
83
83
changed, only whether this instrument could see them.
84
84
85
85
**The expensive failure direction is a keyword.** Sites whose receiver the author
86
-
typed `any` have no type to read, and there are 38 of them — just under a fifth
86
+
typed `any` have no type to read, and there are 40 of them — just under a fifth
87
87
of the population, concentrated in exactly the seed and bootstrap paths this
88
88
control exists for. Scoring an unreadable receiver as "not an engine" would have
89
89
dropped every one of them silently, with a clean exit and a smaller number that
@@ -122,7 +122,7 @@ are reported as `undecidable` rather than assumed either way.
122
122
123
123
The same holds twice over for the context. An options argument spelled as a
124
124
literal can be read; one spelled `options`, `{ ...opts }`, or handed through a
125
-
forwarding shim cannot, and **53 of the 229 sites are spelled that way**. A
125
+
forwarding shim cannot, and **52 of the 231 sites are spelled that way**. A
126
126
context resolved from an inline literal or a local `const` can be tested for
127
127
`isSystem`; one arriving from a helper call cannot.
128
128
@@ -150,8 +150,8 @@ now **0**: nothing on this surface threads a context that provably lacks the fla
150
150
151
151
**"No tenant context" counted sites it had not read.** An options argument the
152
152
walker could not parse was folded into the same bucket as one it had read and
153
-
found empty. That published **61 sites "carrying no tenant context at all"**
154
-
when 8 said so and 53 were simply unread — an over-claim in the *alarming*
153
+
found empty. That published **60 sites "carrying no tenant context at all"**
154
+
when 8 said so and 52 were simply unread — an over-claim in the *alarming*
155
155
direction, on the very figure this page tells other cards to cite. `carries` is
156
156
now three-valued, and an unreadable argument can never contribute to the
157
157
provable count.
@@ -187,10 +187,10 @@ reproduce them. Where it disagrees, it disagrees on the page:
187
187
188
188
| carried figure | where it survives | this census |
189
189
| :--- | :--- | ---: |
190
-
| 175 write call sites | quoted in the merged changeset |**229**|
191
-
| 24 carrying no tenant context | quoted in the merged changeset |**2** provable and tenancy-enabled; **32** more whose options argument is unreadable |
192
-
| 127 of 175 statically decidable, 48 runtime-parameter-name sites | restated on the `isSystem`-scoping card |**158 of 229** decidable, **71** undecidable |
193
-
| 135 (77%) silenced by the `isSystem` guard before the posture gate | the lost issue body — **no surviving corroboration**|**not reproduced**: 125 decidably elevated, 0 decidably not, 96 undecidable |
190
+
| 175 write call sites | quoted in the merged changeset |**231**|
191
+
| 24 carrying no tenant context | quoted in the merged changeset |**2** provable and tenancy-enabled; **31** more whose options argument is unreadable |
192
+
| 127 of 175 statically decidable, 48 runtime-parameter-name sites | restated on the `isSystem`-scoping card |**160 of 231** decidable, **71** undecidable |
193
+
| 135 (77%) silenced by the `isSystem` guard before the posture gate | the lost issue body — **no surviving corroboration**|**not reproduced**: 128 decidably elevated, 0 decidably not, 95 undecidable |
194
194
| 141 and 132, two independent re-derivations | the card that filed this work | — |
195
195
196
196
**The differences are not reconciled, and deliberately so.** The old census's
@@ -200,21 +200,21 @@ be stated is what this instrument counts, which is written above and re-runnable
200
200
at any commit.
201
201
202
202
Two structural facts do plausibly widen this reading against any hand or regex
203
-
one, and both are counted in the generated tables below: the 38 sites reached
203
+
one, and both are counted in the generated tables below: the 40 sites reached
204
204
through an erased (`any`) receiver, and the 52 that name their object through a
205
205
`const` rather than inline. An instrument that read either the way a person does
206
206
would report a smaller number and would not say so.
207
207
208
208
The fourth row is the one worth flagging to anyone citing it. **The 135 / 77%
209
209
figure has no surviving corroboration anywhere in the tree.** This census reads
210
-
125 of 229 (55%) as decidably elevated, with 96 more whose elevation is a
210
+
128 of 231 (55%) as decidably elevated, with 95 more whose elevation is a
211
211
run-time fact — so the claim is neither confirmed nor refuted, and the honest
212
212
answer is that a static reading cannot settle it.
213
213
214
-
⇒ **Cite `2 / 229`, and say what it is**: the sites whose options argument was
214
+
⇒ **Cite `2 / 231`, and say what it is**: the sites whose options argument was
215
215
READ and holds no tenant context, against a decidably tenancy-enabled object.
216
216
That is the control's provable yield surface. ⛔ Do not cite it as "the sites
217
-
without tenant context" — **32 further sites** have an options argument this
217
+
without tenant context" — **31 further sites** have an options argument this
218
218
cannot read, and they are neither in nor out.
219
219
220
220
{/* BEGIN GENERATED: tenant-audit-census (scripts/tenant-audit-census.mjs) — DO NOT EDIT */}
@@ -223,28 +223,28 @@ cannot read, and they are neither in nor out.
223
223
224
224
| what | count |
225
225
| :--- | ---: |
226
-
| write call sites on the application surface |**229**|
227
-
| …whose object name is statically decidable |158|
226
+
| write call sites on the application surface |**231**|
227
+
| …whose object name is statically decidable |160|
228
228
| …whose object name is chosen at run time | 71 |
229
-
| …against an object with tenancy ENABLED |157|
229
+
| …against an object with tenancy ENABLED |159|
230
230
| …against an object that declares tenancy off | 1 |
231
-
| threading a tenant context |168|
231
+
| threading a tenant context |171|
232
232
| PROVABLY carrying none (options read, no context key) |**8**|
233
233
| …of those, against a decidably tenancy-enabled object |**2**|
234
-
| options argument UNREADABLE — may or may not carry one |53|
235
-
| …of those, against a decidably tenancy-enabled object |32|
236
-
| threading a decidably ELEVATED (`isSystem`) context |125|
234
+
| options argument UNREADABLE — may or may not carry one |52|
235
+
| …of those, against a decidably tenancy-enabled object |31|
236
+
| threading a decidably ELEVATED (`isSystem`) context |128|
237
237
| threading a context that is decidably NOT elevated | 0 |
238
-
| threading a context whose elevation is a run-time fact |96|
238
+
| threading a context whose elevation is a run-time fact |95|
239
239
240
240
| how the instrument reached the site | count |
241
241
| :--- | ---: |
242
242
| receiver carried a readable engine type | 191 |
243
-
| receiver erased, placed by the object NAME |28|
243
+
| receiver erased, placed by the object NAME |30|
244
244
| receiver erased, placed by an `object: string` PARAMETER | 5 |
245
245
| receiver erased, placed by an `UNTYPED_RECEIVERS` row | 5 |
246
246
247
-
| object name spelled inline |106|
247
+
| object name spelled inline |108|
248
248
| object name spelled through a `const`| 52 |
249
249
| object name is an `object: string` parameter | 7 |
250
250
| object name is some other run-time expression | 64 |
@@ -297,13 +297,13 @@ holds still. They are required to be HERE and to say WHEN they were true;
297
297
their values are not compared. The reasoning, and the measurement behind it,
0 commit comments