Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
20eaff7
wip(objectql,plugin-auth): organization delete refuses while records …
claude Oct 20, 2026
de6b997
wip: dogfood pins, org-admin grants removed with the organization, po…
claude Oct 20, 2026
ffa920b
wip: changeset
claude Oct 20, 2026
a454cbc
wip: federated-reader census rows for the organization-delete count
claude Oct 20, 2026
4889c24
fix(plugin-auth): spell the organization-delete refusal code at its t…
claude Oct 20, 2026
037461e
docs(permissions): regenerate the tenant-audit census for the three o…
claude Oct 20, 2026
676a8b8
test(plugin-auth): the organization-delete engine double routes delet…
claude Oct 21, 2026
d1c373c
test(plugin-auth): the organization-delete double's WHERE matcher ref…
claude Oct 21, 2026
b753881
Merge remote-tracking branch 'origin/main' into claude/issue-24948-or…
claude Oct 21, 2026
0573a96
fix(objectql, plugin-auth): append-only ledgers neither refuse an org…
claude Oct 21, 2026
3889a6f
wip: organization delete review round (B1 B2 B4 B5 B9)
claude Oct 21, 2026
dbc61ef
wip: review round (B3 B6 B8 and the dogfood pins)
claude Oct 21, 2026
6635c3c
fix(plugin-auth): the Default Organization's last-administrator quest…
claude Oct 21, 2026
c1dd702
test(plugin-auth): drop an unused binding in the Default Organization…
claude Oct 21, 2026
ba33df8
fix(objectql): the organization refusal's disclosure count asks the c…
claude Oct 21, 2026
979fb8a
test(plugin-auth): type the member scan's query options in the #24948…
claude Oct 21, 2026
20f8395
Merge remote-tracking branch 'origin/main' into claude/issue-24948-or…
claude Oct 21, 2026
e184b51
fix(objectql, plugin-auth): engine-owned rows outlive the organizatio…
claude Oct 21, 2026
e40aea8
fix(objectql, plugin-auth): the Default Organization's delete is coun…
claude Oct 21, 2026
139abc4
test(dogfood): pin that a failed Default Organization lookup never re…
claude Oct 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .changeset/24948-organization-delete-refuse-nonempty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
'@objectstack/objectql': minor
'@objectstack/plugin-auth': minor
---

fix(objectql, plugin-auth)!: deleting an organization that still owns records is refused with 409 and a per-table count, before anything is deleted; an empty one is deleted whole, its teams and organization-admin grants with it; the engine never clears a tenant column

**The 18.0.0 defect this fixes.** `POST /api/v1/auth/organization/delete` ran better-auth's delete, which removes the organization's members, then its invitations, then the organization row, as three separate writes. The engine's delete cascade then applied the tenant column's `set_null` default to every row the organization still owned. On a database whose tenant column is NOT NULL (one that took the v18 organization-ownership ceremony), the delete answered 500 on `sys_team.organization_id` after the members and invitations were already gone, so the organization survived with no owner and nobody could delete it. On a database 18.0.0 created, where the column is still nullable, the delete answered 200 and left the organization's teams and records with `organization_id = NULL`, owned by nobody and visible to nobody. Every organization created through the door owns a default team, so on a NOT NULL database not even an empty organization could be deleted. Deployments that run `single` are not reached the same way: their one organization is the Default Organization, which the last-administrator guard answers first, and they cannot create another.

**What happens now.**

- An organization that still owns records is refused with `409` and the standard `DELETE_RESTRICTED` code. The body names each table and its row count, `dependents: [{ object, count }]`, and never a row's values. The refusal runs before better-auth's handler, so nothing is written, the caller's active organization included. A caller the organization does not let delete it still gets better-auth's own `403`, with no count.
- Append-only ledgers (objects declared `managedBy: 'append-only'`, such as `sys_activity`, `sys_audit_log` and `sys_email`) are history, not records. They never refuse the delete, and their rows are kept: every reference they hold to the organization (`organization_id` on `sys_activity` and `sys_email`, the attribution `tenant_id` on `sys_audit_log`) goes on naming the deleted organization, never NULL and never deleted. The delete writes activity rows of its own, so counting them would refuse every delete.
- Engine-owned rows (objects declared `managedBy: 'engine-owned'`) are treated the same way. The platform writes them as a consequence of records or activity, and an administrator cannot delete them over the data API: approval requests, approvers and tokens; automation runs; notifications, their receipts and deliveries; outgoing HTTP deliveries; mailboxes and mirrored mail; record shares and share links; import jobs; audience-binding suggestions; and organization-level settings values and their audit. They never refuse the delete, and they are kept, still naming the deleted organization.
- So engine-owned and append-only rows outlive the organization, and go on naming it. Under tenant scoping no organization reads them.
- An attribution column `organization_id` on an object whose tenancy is disabled is kept the same way: not counted, not cleared. A pointer to the organization, such as a session's or an API key's `active_organization_id`, is cleared as before.
- The Default Organization (slug `default`, or the deployment's only organization, never under an organization wall) is asked the last-administrator guard's question first, and answers `403 PERMISSION_DENIED` while deleting it would leave no platform administrator. Once the guard admits, it is counted like any other organization, before better-auth deletes anything, except that only its members and invitations are left out: its teams and grants are not removed, so they count, and the delete is refused `409 DELETE_RESTRICTED` while it holds them.
- An organization that owns nothing but its own auth structure is deleted whole: its members and invitations (better-auth), and its teams, their members and the organization-admin grants its owners and admins hold (plugin-auth, from `beforeDeleteOrganization`). Any other grant scoped to the organization counts as a record.
- The request runs as one engine transaction and rolls back on any error answer, so a delete either removes all of that or none of it. On a datasource whose driver has no transactions, the refusals are the guarantee, because all of them run before the first write: the record count above, and, for the Default Organization, the last-administrator guard, whose `403` is now asked before better-auth deletes any member (it used to arrive after the members and invitations were gone).
- Under all of this, the engine itself refuses to delete a `sys_organization` row while any row still names it in its tenant column, whatever path reaches the delete: `409 DELETE_RESTRICTED` with the same `dependents` list, which better-auth's door now carries to the wire too. It counts each table rather than reading its rows. An organization is deleted by id only: a delete matching organizations by a filter (`multi: true`) is refused with `409 DELETE_RESTRICTED` naming the by-id route, because only the by-id delete runs this referential pass. A tenant column is never set to NULL.
- `ObjectQL.organizationOwnedRowCounts(organizationId, { exclude })` answers, per object, how many rows name an organization in their tenant column. It is the question the refusal asks.

Clause-②: yes (narrowing)

<!-- adr-0087: not-required (no-migration-prescription) No metadata moves: no spec key, authorable spelling, object definition or stored shape is added, removed, renamed or re-shaped, and no exported declaration is removed or narrowed, so there is nothing for `objectstack migrate meta` to rewrite. What changes is the answer the organization delete gives while the organization still owns records (409 instead of a 500 that left a partial delete, or a 200 that orphaned the rows to NULL), and the engine's answer to a `sys_organization` delete that rows still name in their tenant column. The remedy is an operator action on the organization's records, never a rewrite of a package source or a stored row. The other categories are closed on facts: both packages publish (not unpublished); no ADR-0087 id is named or touched (not registered or already-registered); and nothing exported is removed or narrowed, one engine method is added (not runtime-interface-only, type-surface-only or runtime-export-retired). -->

**BREAKING** (the organization delete's accept set narrows), shipped as `minor` under the launch-window convention for breaking changes.

- **FROM:** deleting an organization that still owned records answered `500` after deleting its members and invitations (tenant column NOT NULL), or `200` with its teams and records orphaned to `organization_id = NULL` (tenant column nullable, an 18.0.0-born database). A direct engine delete of such a `sys_organization` row did the same.
- **TO:** both answer `409 DELETE_RESTRICTED`, with `dependents: [{ object, count }]` naming every table that still holds rows. Nothing is deleted or changed.
- **Fix:** delete the organization's records, or move them to another organization, then delete the organization. The refusal names the tables to look at. Business units, positions and sharing rules count as records. Activity, audit and mail-log rows do not, nor do engine-owned rows (approval state, runs, deliveries, shares, settings values).
- **Seed datasets:** on a composition with the organizations runtime (`@objectstack/organizations`) and an app that ships seed datasets, a new organization receives that seed replay at creation, so it owns records from its first moment and its delete is refused until they are removed. The refusal lists them. Delete them (or move them) like any other records, then delete the organization. Measured on the showcase: a fresh organization owns 132 seeded rows in 19 tables.
- **Already orphaned on 18.0.0?** Rows an earlier organization delete set to `organization_id = NULL` are not touched by this release. Organization-less rows are what the v18 organization-ownership ceremony (`os migrate organization-ownership`) attributes or reports.
34 changes: 17 additions & 17 deletions content/docs/permissions/tenant-audit-census.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ are reported as `undecidable` rather than assumed either way.

The same holds twice over for the context. An options argument spelled as a
literal can be read; one spelled `options`, `{ ...opts }`, or handed through a
forwarding shim cannot, and **51 of the 233 sites are spelled that way**. A
forwarding shim cannot, and **51 of the 236 sites are spelled that way**. A
context resolved from an inline literal or a local `const` can be tested for
`isSystem`; one arriving from a helper call cannot.

Expand Down Expand Up @@ -187,10 +187,10 @@ reproduce them. Where it disagrees, it disagrees on the page:

| carried figure | where it survives | this census |
| :--- | :--- | ---: |
| 175 write call sites | quoted in the merged changeset | **233** |
| 175 write call sites | quoted in the merged changeset | **236** |
| 24 carrying no tenant context | quoted in the merged changeset | **2** provable and tenancy-enabled; **29** more whose options argument is unreadable |
| 127 of 175 statically decidable, 48 runtime-parameter-name sites | restated on the `isSystem`-scoping card | **156 of 233** decidable, **77** undecidable |
| 135 (77%) silenced by the `isSystem` guard before the posture gate | the lost issue body — **no surviving corroboration** | **not reproduced**: 131 decidably elevated, 0 decidably not, 94 undecidable |
| 127 of 175 statically decidable, 48 runtime-parameter-name sites | restated on the `isSystem`-scoping card | **159 of 236** decidable, **77** undecidable |
| 135 (77%) silenced by the `isSystem` guard before the posture gate | the lost issue body — **no surviving corroboration** | **not reproduced**: 134 decidably elevated, 0 decidably not, 94 undecidable |
| 141 and 132, two independent re-derivations | the card that filed this work | — |

**The differences are not reconciled, and deliberately so.** The old census's
Expand All @@ -201,17 +201,17 @@ at any commit.

Two structural facts do plausibly widen this reading against any hand or regex
one, and both are counted in the generated tables below: the 41 sites reached
through an erased (`any`) receiver, and the 49 that name their object through a
through an erased (`any`) receiver, and the 50 that name their object through a
`const` rather than inline. An instrument that read either the way a person does
would report a smaller number and would not say so.

The fourth row is the one worth flagging to anyone citing it. **The 135 / 77%
figure has no surviving corroboration anywhere in the tree.** This census reads
131 of 233 (56%) as decidably elevated, with 94 more whose elevation is a
134 of 236 (57%) as decidably elevated, with 94 more whose elevation is a
run-time fact — so the claim is neither confirmed nor refuted, and the honest
answer is that a static reading cannot settle it.

⇒ **Cite `2 / 233`, and say what it is**: the sites whose options argument was
⇒ **Cite `2 / 236`, and say what it is**: the sites whose options argument was
READ and holds no tenant context, against a decidably tenancy-enabled object.
That is the control's provable yield surface. ⛔ Do not cite it as "the sites
without tenant context" — **29 further sites** have an options argument this
Expand All @@ -223,29 +223,29 @@ cannot read, and they are neither in nor out.

| what | count |
| :--- | ---: |
| write call sites on the application surface | **233** |
| …whose object name is statically decidable | 156 |
| write call sites on the application surface | **236** |
| …whose object name is statically decidable | 159 |
| …whose object name is chosen at run time | 77 |
| …against an object with tenancy ENABLED | 155 |
| …against an object with tenancy ENABLED | 158 |
| …against an object that declares tenancy off | 1 |
| threading a tenant context | 174 |
| threading a tenant context | 177 |
| PROVABLY carrying none (options read, no context key) | **8** |
| …of those, against a decidably tenancy-enabled object | **2** |
| options argument UNREADABLE — may or may not carry one | 51 |
| …of those, against a decidably tenancy-enabled object | 29 |
| threading a decidably ELEVATED (`isSystem`) context | 131 |
| threading a decidably ELEVATED (`isSystem`) context | 134 |
| threading a context that is decidably NOT elevated | 0 |
| threading a context whose elevation is a run-time fact | 94 |

| how the instrument reached the site | count |
| :--- | ---: |
| receiver carried a readable engine type | 192 |
| receiver carried a readable engine type | 195 |
| receiver erased, placed by the object NAME | 31 |
| receiver erased, placed by an `object: string` PARAMETER | 5 |
| receiver erased, placed by an `UNTYPED_RECEIVERS` row | 5 |

| object name spelled inline | 107 |
| object name spelled through a `const` | 49 |
| object name spelled inline | 109 |
| object name spelled through a `const` | 50 |
| object name is an `object: string` parameter | 7 |
| object name is some other run-time expression | 70 |

Expand Down Expand Up @@ -297,11 +297,11 @@ holds still. They are required to be HERE and to say WHEN they were true;
their values are not compared. The reasoning, and the measurement behind it,
are in `scripts/check-tenant-audit-census.mjs`.

Measured on 2026-10-20 at `39fc914f7`.
Measured on 2026-10-20 at `a454cbcc4`.

| corpus scale (not enforced) | count |
| :--- | ---: |
| tracked non-test sources scanned | 681 |
| tracked non-test sources scanned | 684 |
| engine-shaped types recognised | 82 |
| declared objects in the registry | 118 |
| same-named calls subtracted as non-engine | 169 |
Expand Down
17 changes: 10 additions & 7 deletions docs/audits/2026-08-tenant-audit-write-call-sites.counts.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ silent, and `node scripts/tenant-audit-census.mjs --write` is the resolution.

| Measure | Value |
|---|---:|
| Write call sites | 233 |
| Object name statically decidable | 156 |
| Write call sites | 236 |
| Object name statically decidable | 159 |
| Object name chosen at run time | 77 |
| Against a tenancy-enabled object | 155 |
| Against a tenancy-enabled object | 158 |
| Against an object declaring tenancy off | 1 |
| Threading a tenant context | 174 |
| Threading a tenant context | 177 |
| Provably carrying none | 8 |
| …and decidably tenancy-enabled | 2 |
| Options argument unreadable | 51 |
| …and decidably tenancy-enabled | 29 |
| Threading a decidably elevated context | 131 |
| Threading a decidably elevated context | 134 |
| Threading a decidably non-elevated context | 0 |
| Threading a context of undecidable elevation | 94 |

Expand Down Expand Up @@ -90,11 +90,11 @@ holds still. They are required to be HERE and to say WHEN they were true;
their values are not compared. The reasoning, and the measurement behind it,
are in `scripts/check-tenant-audit-census.mjs`.

Measured on 2026-10-20 at `39fc914f7`.
Measured on 2026-10-20 at `a454cbcc4`.

| corpus scale (not enforced) | count |
| :--- | ---: |
| tracked non-test sources scanned | 681 |
| tracked non-test sources scanned | 684 |
| engine-shaped types recognised | 82 |
| declared objects in the registry | 118 |
| same-named calls subtracted as non-engine | 169 |
Expand Down Expand Up @@ -155,6 +155,9 @@ Measured on 2026-10-20 at `39fc914f7`.
| `packages/plugins/plugin-auth/src/objectql-adapter.ts` | `delete` | `objectName` | undecidable | PROVABLY NONE | 5 |
| `packages/plugins/plugin-auth/src/objectql-adapter.ts` | `insert` | `objectName` | undecidable | options unreadable | 2 |
| `packages/plugins/plugin-auth/src/objectql-adapter.ts` | `update` | `objectName` | undecidable | options unreadable | 5 |
| `packages/plugins/plugin-auth/src/organization-delete.ts` | `delete` | `sys_team` | enabled | elevated | 1 |
| `packages/plugins/plugin-auth/src/organization-delete.ts` | `delete` | `sys_team_member` | enabled | elevated | 1 |
| `packages/plugins/plugin-auth/src/organization-delete.ts` | `delete` | `sys_user_permission_set` | enabled | elevated | 1 |
| `packages/plugins/plugin-auth/src/reconcile-membership.ts` | `insert` | `sys_member` | enabled | context, elevation undecidable | 1 |
| `packages/plugins/plugin-auth/src/scim-connection-service.ts` | `insert` | `sys_scim_connection_credential` | enabled | PROVABLY NONE | 1 |
| `packages/plugins/plugin-auth/src/session-tombstone.ts` | `update` | `objectName` | undecidable | options unreadable | 1 |
Expand Down
Loading
Loading