Skip to content

Commit 8b657ca

Browse files
Merge branch 'main' into claude/project-thread-gylh1m
2 parents b069d55 + a15b544 commit 8b657ca

59 files changed

Lines changed: 2387 additions & 40 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
'@objectstack/plugin-security': patch
3+
'@objectstack/plugin-sharing': patch
4+
---
5+
6+
feat(plugin-security, plugin-sharing): security and sharing stored references gain a package column, backfilled at boot from the one package that holds the name (ADR-0150 D8 R1, R3, R5, R6, R13)
7+
8+
Clause-②: no
9+
Metadata-protocol: none
10+
11+
Five stored references that name a declared item by its bare name now carry, beside the name, the package that holds it: `sys_user_permission_set.permission_set_package` (R1), `sys_audience_binding_suggestion.permission_set_package` (R3), `sys_sharing_rule.package_id` on package- and platform-managed rows (R5), `sys_queue_member.queue_package` (R6) and `sys_assignment_cursor.rule_package` (R13). Each is nullable, read-only to users, and filled in by the platform.
12+
13+
At every boot, once the registry has sealed its holders, each table's rows that name no package are walked and asked the one ADR-0150 D3 rule, from the package that wrote the reference. A grant, a sharing-rule row, a queue member and a cursor are the environment's own rows, so they are asked with no caller package. A suggestion is the reference of the package that made it, so it is asked from that package: its own set first, then the one holder among its declared dependencies. A suggesting package's own set therefore binds to it even where an unrelated package holds a set of the same name from before the one-holder rule, which is D3's answer for that package, not a pick; and a set only an unrelated package holds is left null and reported, never bound to it. A name exactly one package holds is bound to that package. A name the rule refuses (two or more holders, or for a suggestion none among the suggesting package's dependencies) is left null and logged as a warning naming every candidate; it is never bound to the first. A name that only the environment or the platform holds, or that nothing holds, is left null and reported. A row that already names a package is never read again, so a second boot writes nothing and reports the same names. Suggestion rows bound to a dependency of the suggesting package rather than to the package itself are logged too.
14+
15+
Nothing reads the new columns yet: who may hold what, which rule shares what, and who a queue routes to answer exactly as before, and no unique key changes. Writers do not stamp the columns yet either; each reader's own card moves its reader and writer together. If the boot pass fails, it logs a warning, leaves its rows as they are and is tried again at the next boot. Rolling back drops nothing a reader needs.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@objectstack/runtime': patch
3+
'@objectstack/objectql': patch
4+
---
5+
6+
fix(runtime): a job with no `organization` pulls as the organization its writes are stamped with, so its upsert match uses the organization index
7+
8+
Clause-②: yes (widening) — @objectstack/objectql gains `ObjectQL#systemWriteOrganization()`, the engine's own answer to which organization an organization-less system write resolves to
9+
Metadata-protocol: none
10+
11+
A `job` whose `pull` syncs a mapping and that declares no `organization` ran its pull with no organization in its context (`{ isSystem: true }`). Every write of that pull was still stamped with the install's organization: on the `single` posture with exactly one organization, the engine derives it for an organization-less system write (ADR-0131 D9). But the import's match lookup had no organization to scope by, so on `single`, where a read gets no tenant scope, it read the target table once per row instead of probing the `(organization_id, <key>)` unique index. A large sync on the most common setup stayed table-scan slow.
12+
13+
Each run of such a pull now asks the engine, once, before it reads or writes, the same decision the engine's system insert makes, and runs as that organization when the engine derives one. The lookup then carries it on the tenant column, and the rows land exactly where they did before. When the engine does not derive one (a `group` or `isolated` posture, zero or several organizations, a composition with no organization object), the pull runs as before, and its writes are refused or stamped exactly as before. A job that declares an `organization` is unchanged, and so are the `body` and `handler` run forms. If the organization probe fails, the run says so once at `warn`, naming the job, and goes on as before.

‎content/docs/permissions/tenant-audit-census.mdx‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ are reported as `undecidable` rather than assumed either way.
132132

133133
The same holds twice over for the context. An options argument spelled as a
134134
literal can be read; one spelled `options`, `{ ...opts }`, or handed through a
135-
forwarding shim cannot, and **57 of the 277 sites are spelled that way**. A
135+
forwarding shim cannot, and **57 of the 279 sites are spelled that way**. A
136136
context resolved from an inline literal or a local `const` can be tested for
137137
`isSystem`; one arriving from a helper call cannot.
138138

@@ -197,10 +197,10 @@ reproduce them. Where it disagrees, it disagrees on the page:
197197

198198
| carried figure | where it survives | this census |
199199
| :--- | :--- | ---: |
200-
| 175 write call sites | quoted in the merged changeset | **277** |
200+
| 175 write call sites | quoted in the merged changeset | **279** |
201201
| 24 carrying no tenant context | quoted in the merged changeset | **3** provable and tenancy-enabled; **30** more whose options argument is unreadable |
202-
| 127 of 175 statically decidable, 48 runtime-parameter-name sites | restated on the `isSystem`-scoping card | **197 of 277** decidable, **80** undecidable |
203-
| 135 (77%) silenced by the `isSystem` guard before the posture gate | the lost issue body — **no surviving corroboration** | **not reproduced**: 154 decidably elevated, 0 decidably not, 113 undecidable |
202+
| 127 of 175 statically decidable, 48 runtime-parameter-name sites | restated on the `isSystem`-scoping card | **197 of 279** decidable, **82** undecidable |
203+
| 135 (77%) silenced by the `isSystem` guard before the posture gate | the lost issue body — **no surviving corroboration** | **not reproduced**: 156 decidably elevated, 0 decidably not, 113 undecidable |
204204
| 141 and 132, two independent re-derivations | the card that filed this work | — |
205205

206206
**The differences are not reconciled, and deliberately so.** The old census's
@@ -217,11 +217,11 @@ would report a smaller number and would not say so.
217217

218218
The fourth row is the one worth flagging to anyone citing it. **The 135 / 77%
219219
figure has no surviving corroboration anywhere in the tree.** This census reads
220-
154 of 277 (56%) as decidably elevated, with 113 more whose elevation is a
220+
156 of 279 (56%) as decidably elevated, with 113 more whose elevation is a
221221
run-time fact — so the claim is neither confirmed nor refuted, and the honest
222222
answer is that a static reading cannot settle it.
223223

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

234234
| what | count |
235235
| :--- | ---: |
236-
| write call sites on the application surface | **277** |
236+
| write call sites on the application surface | **279** |
237237
| …whose object name is statically decidable | 197 |
238-
| …whose object name is chosen at run time | 80 |
238+
| …whose object name is chosen at run time | 82 |
239239
| …against an object with tenancy ENABLED | 191 |
240240
| …against an object that declares tenancy off | 6 |
241-
| threading a tenant context | 210 |
241+
| threading a tenant context | 212 |
242242
| PROVABLY carrying none (options read, no context key) | **10** |
243243
| …of those, against a decidably tenancy-enabled object | **3** |
244244
| options argument UNREADABLE — may or may not carry one | 57 |
245245
| …of those, against a decidably tenancy-enabled object | 30 |
246-
| threading a decidably ELEVATED (`isSystem`) context | 154 |
246+
| threading a decidably ELEVATED (`isSystem`) context | 156 |
247247
| threading a context that is decidably NOT elevated | 0 |
248248
| threading a context whose elevation is a run-time fact | 113 |
249249

250250
| how the instrument reached the site | count |
251251
| :--- | ---: |
252-
| receiver carried a readable engine type | 215 |
252+
| receiver carried a readable engine type | 217 |
253253
| receiver erased, placed by the object NAME | 52 |
254254
| receiver erased, placed by an `object: string` PARAMETER | 5 |
255255
| receiver erased, placed by an `UNTYPED_RECEIVERS` row | 5 |
256256

257257
| object name spelled inline | 125 |
258258
| object name spelled through a `const` | 72 |
259-
| object name is an `object: string` parameter | 7 |
259+
| object name is an `object: string` parameter | 9 |
260260
| object name is some other run-time expression | 73 |
261261

262262
### Subtractions the census could NOT defend — enforced
@@ -307,13 +307,13 @@ holds still. They are required to be HERE and to say WHEN they were true;
307307
their values are not compared. The reasoning, and the measurement behind it,
308308
are in `scripts/check-tenant-audit-census.mjs`.
309309

310-
Measured on 2026-11-09 at `a06abc67a`.
310+
Measured on 2026-11-09 at `2fd711123`.
311311

312312
| corpus scale (not enforced) | count |
313313
| :--- | ---: |
314-
| tracked non-test sources scanned | 1082 |
315-
| engine-shaped types recognised | 118 |
314+
| tracked non-test sources scanned | 1085 |
315+
| engine-shaped types recognised | 120 |
316316
| declared objects in the registry | 133 |
317-
| same-named calls subtracted as non-engine | 248 |
317+
| same-named calls subtracted as non-engine | 250 |
318318

319319
{/* END GENERATED: tenant-audit-census */}

‎docs/audits/2026-08-tenant-audit-write-call-sites.counts.md‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ silent, and `node scripts/tenant-audit-census.mjs --write` is the resolution.
3333

3434
| Measure | Value |
3535
|---|---:|
36-
| Write call sites | 277 |
36+
| Write call sites | 279 |
3737
| Object name statically decidable | 197 |
38-
| Object name chosen at run time | 80 |
38+
| Object name chosen at run time | 82 |
3939
| Against a tenancy-enabled object | 191 |
4040
| Against an object declaring tenancy off | 6 |
41-
| Threading a tenant context | 210 |
41+
| Threading a tenant context | 212 |
4242
| Provably carrying none | 10 |
4343
| …and decidably tenancy-enabled | 3 |
4444
| Options argument unreadable | 57 |
4545
| …and decidably tenancy-enabled | 30 |
46-
| Threading a decidably elevated context | 154 |
46+
| Threading a decidably elevated context | 156 |
4747
| Threading a decidably non-elevated context | 0 |
4848
| Threading a context of undecidable elevation | 113 |
4949

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

93-
Measured on 2026-11-09 at `a06abc67a`.
93+
Measured on 2026-11-09 at `2fd711123`.
9494

9595
| corpus scale (not enforced) | count |
9696
| :--- | ---: |
97-
| tracked non-test sources scanned | 1082 |
98-
| engine-shaped types recognised | 118 |
97+
| tracked non-test sources scanned | 1085 |
98+
| engine-shaped types recognised | 120 |
9999
| declared objects in the registry | 133 |
100-
| same-named calls subtracted as non-engine | 248 |
100+
| same-named calls subtracted as non-engine | 250 |
101101

102102
## Every site
103103

@@ -194,6 +194,7 @@ Measured on 2026-11-09 at `a06abc67a`.
194194
| `packages/plugins/plugin-security/src/claim-seed-ownership.ts` | `update` | `schema.name` | undecidable | elevated | 1 |
195195
| `packages/plugins/plugin-security/src/cleanup-package-permissions.ts` | `delete` | `object` | undecidable | elevated | 1 |
196196
| `packages/plugins/plugin-security/src/invitation-placement.ts` | `insert` | `sys_user_position` | enabled | elevated | 1 |
197+
| `packages/plugins/plugin-security/src/stored-permission-set-package-backfill.ts` | `update` | `object` | undecidable | elevated | 1 |
197198
| `packages/plugins/plugin-security/src/suggested-audience-bindings.ts` | `delete` | `sys_audience_binding_suggestion` | enabled | context, elevation undecidable | 1 |
198199
| `packages/plugins/plugin-security/src/suggested-audience-bindings.ts` | `insert` | `sys_audience_binding_suggestion` | enabled | context, elevation undecidable | 1 |
199200
| `packages/plugins/plugin-security/src/suggested-audience-bindings.ts` | `update` | `sys_audience_binding_suggestion` | enabled | context, elevation undecidable | 4 |
@@ -210,6 +211,7 @@ Measured on 2026-11-09 at `a06abc67a`.
210211
| `packages/plugins/plugin-sharing/src/sharing-service.ts` | `delete` | `sys_record_share` | enabled | elevated | 2 |
211212
| `packages/plugins/plugin-sharing/src/sharing-service.ts` | `insert` | `sys_record_share` | enabled | elevated | 1 |
212213
| `packages/plugins/plugin-sharing/src/sharing-service.ts` | `update` | `sys_record_share` | enabled | elevated | 1 |
214+
| `packages/plugins/plugin-sharing/src/stored-declaration-package-backfill.ts` | `update` | `object` | undecidable | elevated | 1 |
213215
| `packages/plugins/plugin-webhooks/src/migrate-webhook-secrets.ts` | `update` | `subscriptionsObject` | undecidable | options unreadable | 1 |
214216
| `packages/services/service-automation/src/builtin/crud-nodes.ts` | `delete` | `objectName` | undecidable | context, elevation undecidable | 1 |
215217
| `packages/services/service-automation/src/builtin/crud-nodes.ts` | `insert` | `objectName` | undecidable | context, elevation undecidable | 1 |

‎packages/objectql/src/engine-middleware-and-context.ts‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import type { ExecutionContext } from '@objectstack/spec/kernel';
4040
import { isWriteEpochOperation } from './write-epoch.js';
4141
import { bridgeAuthzInvalidation } from './authz-invalidation-bridge.js';
4242
import { isMissingTableError } from '@objectstack/metadata/errors';
43-
import { ORGANIZATION_OBJECT, SystemWriteOrganizationRequiredError, carriesOrganization, resolveSystemWriteOrganization, resolveTenantFieldName } from './tenancy/system-write-organization.js';
43+
import { ORGANIZATION_OBJECT, SystemWriteOrganizationRequiredError, carriesOrganization, resolveSystemWriteOrganization, resolveTenantFieldName, type SystemWriteOrganizationDecision } from './tenancy/system-write-organization.js';
4444
import { isFederatedObject } from './federated-object.js';
4545
import type { EngineMiddleware, OperationContext } from './engine-top-level.js';
4646
import type { ObjectQL } from './engine.js';
@@ -359,24 +359,34 @@ export class MiddlewareAndContextFacet {
359359
if (rows.every((row) => carriesOrganization(row?.[tenantField]))) return undefined;
360360

361361
const posture = this.resolveEnginePosture();
362+
const decision = await MiddlewareAndContextFacet.systemWriteOrganization.call(this, posture);
363+
if (decision.kind === 'derived') return decision.organizationId;
364+
if (decision.kind === 'no-organization-object') return undefined;
365+
throw new SystemWriteOrganizationRequiredError(
366+
object,
367+
posture,
368+
decision.reason,
369+
decision.organizationCount,
370+
);
371+
}
372+
373+
/**
374+
* The body of `ObjectQL#systemWriteOrganization`; the method's docblock there
375+
* is its contract. Also the decision half of
376+
* {@link resolveSystemInsertOrganization}, which hands in the posture it has
377+
* already read, so the insert and the public answer are one assembly.
378+
*/
379+
static async systemWriteOrganization(this: Pick<MiddlewareAndContextHost, '_registry' | 'probeInstallOrganizations' | 'resolveEnginePosture'>, posture = this.resolveEnginePosture()): Promise<SystemWriteOrganizationDecision> {
362380
// [ADR-0131 D9] Zero organizations is refused under `single` only where
363381
// the composition registers the organization object; the composition that
364382
// registers none keeps today's branch (`no-organization-object`). The
365383
// probe itself is unchanged and answers `[]` for both — this is the one
366384
// fact that tells them apart, read from the same registry the probe reads.
367-
const decision = await resolveSystemWriteOrganization({
385+
return resolveSystemWriteOrganization({
368386
posture,
369387
probeOrganizations: () => this.probeInstallOrganizations(),
370388
organizationObjectRegistered: !!this._registry.getObject(ORGANIZATION_OBJECT),
371389
});
372-
if (decision.kind === 'derived') return decision.organizationId;
373-
if (decision.kind === 'no-organization-object') return undefined;
374-
throw new SystemWriteOrganizationRequiredError(
375-
object,
376-
posture,
377-
decision.reason,
378-
decision.organizationCount,
379-
);
380390
}
381391

382392
/**

‎packages/objectql/src/engine.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ import { redactBoundStatement } from '@objectstack/types';
200200
// supplies the facts and turns a `refuse` into the throw.
201201
import {
202202
ORGANIZATION_OBJECT,
203+
type SystemWriteOrganizationDecision,
203204
} from './tenancy/system-write-organization.js';
204205
import {
205206
type TenancyPosture,
@@ -3054,6 +3055,32 @@ export class ObjectQL implements IObjectQLEngine {
30543055
return SecretsAndRoutingFacet.setTenancyPostureProvider.call(secretsAndRoutingHost.of(this), provider);
30553056
}
30563057

3058+
/**
3059+
* [ADR-0131 D9] The organization a SYSTEM-context write that carries none
3060+
* resolves to on this engine — the decision its own system insert makes
3061+
* (`resolveSystemInsertOrganization`), from the same three inputs: the
3062+
* posture in force (the injected tenancy-posture accessor, else the
3063+
* deployment's), the install's organizations (the memoised probe, capped at
3064+
* two), and whether this composition registers `sys_organization` at all.
3065+
* The predicate is `@objectstack/core`'s `resolveSystemWriteOrganization`,
3066+
* never a second spelling of it.
3067+
*
3068+
* For a caller that runs system work with no organization of its own and
3069+
* wants to carry, ahead of its writes, the organization those writes would
3070+
* be stamped with — a job that declares no `organization` resolves its pull's
3071+
* context here, so the pull's reads scope by the organization its writes
3072+
* land in. Not a write and not a gate: it refuses nothing itself. A `refuse`
3073+
* decision is the answer the write would turn into its refusal, and the
3074+
* write stays the one that refuses.
3075+
*
3076+
* A probe that could not run (an outage, not an empty table) propagates, as
3077+
* it does on the insert.
3078+
*/
3079+
systemWriteOrganization(): Promise<SystemWriteOrganizationDecision> {
3080+
// [#31823] The body lives in `engine-middleware-and-context.ts` (see `MiddlewareAndContextHost`).
3081+
return MiddlewareAndContextFacet.systemWriteOrganization.call(middlewareAndContextHost.of(this));
3082+
}
3083+
30573084
private async encryptSecretFields(
30583085
object: string,
30593086
row: Record<string, unknown>,

0 commit comments

Comments
 (0)