Skip to content

Commit 4f11b21

Browse files
committed
fix(plugin-sharing): the share-table self-scope lands in where only; parity pins below the cap; the record and the skill line follow
- sharing-plugin: the sys_record_share self-scope branch no longer mirrors its scope under the AST's filter key, as the general read merge already does not; pinned for the plain and principal-less cases. - shared-record-ids-switch: at 5,000 grants, the nested position (a composed ReadableIds scope whose parent scope is the leaf, against the same composition handed the rebuilt capped list) and the delegated position (the two leaves AND-ed against the two rebuilt lists, read and update) admit the same rows. The counts above the cap are unchanged. - ADR-0155: a second dated, append-only note that the switch landed before plan step 4, so driver-memory takes R2 until a driver declares the leaf. - objectstack-data datasources rule: the 422 is the grant and rule-save doors'; a declared sharingRules rule aborts its reconcile instead. Paid in the same file (909 -> 911 tokens, ceiling 911 unchanged). Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibyXo4gSy8DJPNz4ANz
1 parent 53f788e commit 4f11b21

5 files changed

Lines changed: 68 additions & 13 deletions

File tree

‎docs/adr/0155-sharing-scope-share-grants-lower-to-an-engine-internal-semi-join-leaf.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,18 @@ The rest of the census is ②, ruled row by row in [Q2](#q2--an-evaluator-that-c
529529
`driver-odata`, a driver this repository does not ship, an object on another datasource, the analytics read-scope
530530
compilers, the explainer's record pass, and the host class D4 recognises.
531531

532+
> **Note (2026-11-21, #33787) — the switch landed before plan step 4, so `driver-memory` takes R2 until a driver
533+
> declares the leaf.** Plan step 4 (#35873, `driver-memory`'s in-process evaluation) is on hold. Ruling
534+
> [`7045701086`](https://github.com/objectstack-ai/objectstack/issues/33787#issuecomment-7045701086) made it no
535+
> prerequisite of plan step 6, and seat 7's call in claim amendment
536+
> [`7060526112`](https://github.com/objectstack-ai/objectstack/issues/33787#issuecomment-7060526112) confirmed what
537+
> `driver-memory` answers meanwhile. It declares no `fixedReadLeaves`, so from the switch on it answers as ②'s R2 row
538+
> does: its share grants go inert, stop counting and fail closed (neither scope carries the share arm, and the edit
539+
> gate reads no grant), and the grant door and the sharing-rule save door refuse with `SHARING_NOT_ENABLED` / 422.
540+
> The `driver-memory` row above, Q2's closing sentence and plan step 6's "lands last" record the order the plan
541+
> expected. They stand as written, and this note records the order that happened. Once plan step 4 lands and the
542+
> driver declares the leaf, share grants on `driver-memory` count again, with no change to plugin-sharing.
543+
532544
### D6 — What the switch must show
533545

534546
The plugin-sharing switch is where #33787 ends. Its card carries the pins the defect card and the seat set (#33787's

‎packages/plugins/plugin-sharing/src/shared-record-ids-switch.test.ts‎

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ for (const cell of CELLS) {
9494
let knex: any;
9595
/** The queue and approval arms are wired while this is on. */
9696
let arms = true;
97+
/** What the `security` service hands a composed `ReadableIds` scope for DEAL: the leaf, or today's list. */
98+
let parentScope: 'leaf' | 'list' = 'leaf';
9799
let grantsSeeded = 0;
98100
let marker = 0;
99101

@@ -122,11 +124,11 @@ for (const cell of CELLS) {
122124
expect(Number(stored), 'the grants the caller holds on the object').toBe(n);
123125
}
124126

125-
/** The retired statement's scope: the grant rows under `limit: 5000`, merged with the approval ids. */
126-
async function todaysScope(verb: 'read' | 'update'): Promise<Record<string, unknown>> {
127+
/** The retired statement's scope for `user`: the grant rows under `limit: 5000`, merged with the approval ids. */
128+
async function todaysScope(verb: 'read' | 'update', user: string = ME): Promise<Record<string, unknown>> {
127129
const grants = await ql.find('sys_record_share', {
128130
where: {
129-
object_name: DEAL, recipient_type: 'user', recipient_id: ME,
131+
object_name: DEAL, recipient_type: 'user', recipient_id: user,
130132
...(verb === 'update' ? { access_level: { $in: [...WRITE_ACCESS_LEVELS] } } : {}),
131133
},
132134
fields: ['record_id'],
@@ -136,8 +138,8 @@ for (const cell of CELLS) {
136138
(grants as Array<{ record_id: unknown }>)
137139
.map((g) => g.record_id).filter((v) => v != null).map(String).filter((v) => v !== ''),
138140
);
139-
if (verb === 'read' && arms) listed.add(APPROVAL);
140-
const disjuncts: unknown[] = [{ owner_id: ME }];
141+
if (verb === 'read' && arms && user === ME) listed.add(APPROVAL);
142+
const disjuncts: unknown[] = [{ owner_id: user }];
141143
if (arms) disjuncts.push({ owner_id: null, queue: QUEUE });
142144
if (listed.size > 0) disjuncts.push({ id: { $in: [...listed] } });
143145
return disjuncts.length === 1 ? (disjuncts[0] as Record<string, unknown>) : { $or: disjuncts };
@@ -159,9 +161,9 @@ for (const cell of CELLS) {
159161
await ql.update(DEAL, { name }, { where: { name: { $ne: '__never__' } }, multi: true, context } as never);
160162
return ids(await ql.find(DEAL, { where: { name }, fields: ['id'] }, { context: SYSTEM } as never));
161163
}
162-
async function listUpdate(): Promise<string[]> {
164+
async function listUpdate(scope?: Record<string, unknown>): Promise<string[]> {
163165
const name = `touched_${++marker}`;
164-
const where = { $and: [{ name: { $ne: '__never__' } }, await todaysScope('update')] };
166+
const where = { $and: [{ name: { $ne: '__never__' } }, scope ?? await todaysScope('update')] };
165167
await ql.update(DEAL, { name }, { where, multi: true, context: SYSTEM } as never);
166168
return ids(await ql.find(DEAL, { where: { name }, fields: ['id'] }, { context: SYSTEM } as never));
167169
}
@@ -184,7 +186,9 @@ for (const cell of CELLS) {
184186
// here, one whose read filter for an object is the sharing scope.
185187
const services = new Map<string, unknown>([['security', {
186188
canReadObject: async () => true,
187-
getReadFilter: (object: string, context: unknown) => service.buildReadFilter(object, context as never),
189+
getReadFilter: (object: string, context: unknown) => (parentScope === 'list' && object === DEAL
190+
? todaysScope('read')
191+
: service.buildReadFilter(object, context as never)),
188192
explain: async () => ({ allowed: true }),
189193
}]]);
190194
await (new ObjectQLPlugin({ ql, registerProtocol: false } as never) as any).init({
@@ -255,6 +259,39 @@ for (const cell of CELLS) {
255259
expect(viaLeaf).not.toContain(shrId(5000));
256260
});
257261

262+
it('5,000 grants, nested: the leaf inside a composed ReadableIds scope admits the rows today\'s list does', async () => {
263+
arms = true;
264+
const where = { deal_id: { $in: ReadableIds.of(DEAL) } };
265+
const lineIds = async () => ids(await ql.find(LINE, { where, fields: ['id'] }, { context: asMe() } as never));
266+
const lineCount = () => ql.count(LINE, { where }, { context: asMe() } as never);
267+
const viaLeaf = await lineIds();
268+
const countViaLeaf = await lineCount();
269+
parentScope = 'list';
270+
const listed = await Promise.resolve()
271+
.then(async () => ({ ids: await lineIds(), count: await lineCount() }))
272+
.finally(() => { parentScope = 'leaf'; });
273+
const viaList = listed.ids;
274+
const countViaList = listed.count;
275+
// Owned, the 5,000 granted, the queued and the approval row: one line each.
276+
expect(viaLeaf).toHaveLength(OWNED + 5000 + 2);
277+
expect(viaLeaf).toEqual(viaList);
278+
expect(countViaLeaf).toBe(countViaList);
279+
});
280+
281+
it('5,000 grants, delegated: the two leaves, AND-ed, admit and update the rows today\'s two lists do', async () => {
282+
arms = false;
283+
const listScope = async (verb: 'read' | 'update') => ({ $and: [await todaysScope(verb, AGENT), await todaysScope(verb, ME)] });
284+
const viaLeaf = await leafIds(asAgentForMe());
285+
const viaList = ids(await ql.find(DEAL, { where: await listScope('read'), fields: ['id'] }, { context: SYSTEM } as never));
286+
// The agent's 100 grants on shr_*, all within the delegator's 5,000, and its 50 on rows the delegator owns.
287+
expect(viaLeaf).toHaveLength(150);
288+
expect(viaLeaf).toEqual(viaList);
289+
expect(await leafCount(asAgentForMe())).toBe(await ql.count(DEAL, { where: await listScope('read') }, { context: SYSTEM } as never));
290+
const touched = await leafUpdate(asAgentForMe());
291+
expect(touched).toHaveLength(50);
292+
expect(touched).toEqual(await listUpdate(await listScope('update')));
293+
});
294+
258295
it('5,001 grants: the leaf counts the 5,001st, where today\'s list stops at 5,000', async () => {
259296
arms = false;
260297
await growGrantsTo(5001);

‎packages/plugins/plugin-sharing/src/sharing-plugin.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1512,8 +1512,10 @@ export function buildSharingMiddleware(
15121512
// platform-authored as any sharing filter.
15131513
markFilterSubtreeProvenance(selfScope, 'policy');
15141514
vouchCallerWhereBeforeRewrite(ast, ctx.options);
1515+
// [ADR-0155 plan step 6] `where` only, as in the general read merge
1516+
// below: the AST's `filter` key is not walked by the executor seam
1517+
// and no driver reads it, so the scope is no longer mirrored there.
15151518
ast.where = composeAnd(ast.where, selfScope);
1516-
ast.filter = composeAnd(ast.filter, selfScope);
15171519
ctx.ast = ast;
15181520
}
15191521
return next();

‎packages/plugins/plugin-sharing/src/sharing-service.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,8 @@ describe('[ADR-0111 D5] sys_record_share read self-scope (middleware)', () => {
15191519
expect(ctx.ast.where).toEqual({
15201520
$or: [{ recipient_id: 'mallory' }, { granted_by: 'mallory' }],
15211521
});
1522+
// [ADR-0155 plan step 6] `where` only, as on the general read path.
1523+
expect(ctx.ast.filter).toBeUndefined();
15221524
});
15231525

15241526
it('manage_sharing (and the legacy manage_platform_settings) keeps the tenant-wide list', async () => {
@@ -1535,6 +1537,7 @@ describe('[ADR-0111 D5] sys_record_share read self-scope (middleware)', () => {
15351537
const ctx = findCtx({});
15361538
await mw(ctx, async () => {});
15371539
expect(ctx.ast.where).toEqual({ id: '__deny_all__' });
1540+
expect(ctx.ast.filter).toBeUndefined();
15381541
});
15391542

15401543
it('system context is untouched', async () => {

‎skills/objectstack-data/rules/datasources.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ connection to a data store. Objects route to one via their `datasource` field.
55
`default` is host-reserved (connected from `OS_DATABASE_URL`; declaring one
66
named `default` is refused). Declare one under its own name only for a
77
**separate** or **external** store. Share grants count only on
8-
`sys_record_share`'s datasource under a SQL driver; elsewhere a grant or
9-
sharing rule is refused `SHARING_NOT_ENABLED` (422).
8+
`sys_record_share`'s datasource under a SQL driver; elsewhere a grant or rule
9+
save is refused `SHARING_NOT_ENABLED` (422), and a `sharingRules` reconcile
10+
aborts on it.
1011

1112
## `schemaMode` — who owns the schema
1213

1314
| Mode | Meaning |
1415
|:--|:--|
1516
| `managed` (default) | ObjectStack owns the schema; DDL + migrations allowed. |
1617
| `external` | A mature external DB ObjectStack does **not** own; DDL forbidden; boot mismatch **fails**. |
17-
| `validate-only` | Like `external`, but a mismatch **warns** instead of failing boot. |
18+
| `validate-only` | Like `external`, but a mismatch **warns**. |
1819

1920
## Federated (external) objects
2021

@@ -27,7 +28,7 @@ ObjectSchema.create({
2728
sharingModel: 'public_read',
2829
external: {
2930
remoteName: 'customers', // remote TABLE
30-
// remoteSchema: 'public', // optional schema/namespace (pg/mysql)
31+
// remoteSchema: 'public', // schema/namespace (pg/mysql)
3132
// columnMap: { cust_region: 'region' }, // remoteColumn → localField
3233
},
3334
fields: { id: { type: 'text' }, name: { type: 'text' }, region: { type: 'text' } },

0 commit comments

Comments
 (0)