Skip to content

Commit 8fa01d1

Browse files
committed
fix(plugin-sharing): the per-record rule pass is best-effort per rule; name the read-filter in the approvals warning
A rule whose recipients cannot be read now refuses; evaluateAllForRecord catches per rule, as evaluateAllRulesForObject does, so the record's other rules still reconcile. The approvals warning names the directory or read-filter lookup, and approvals.mdx documents it. Claude-Session: https://claude.ai/code/session_01ADHkmS2E1xdkLMJPnabYgC Co-authored-by: Claude <noreply@anthropic.com>
1 parent bf9ca7d commit 8fa01d1

5 files changed

Lines changed: 91 additions & 10 deletions

File tree

‎content/docs/automation/approvals.mdx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,11 @@ What an entry that resolves to nobody leaves on the slate depends on its kind:
455455
decide it (the admin-override callout under
456456
[Acting on requests in the console](#acting-on-requests-in-the-console)).
457457
Beside entries that do resolve, a literal is a slot like any other, so a
458-
`behavior` that needs every slot (`unanimous`) waits on it too.
458+
`behavior` that needs every slot (`unanimous`) waits on it too. A group entry
459+
whose people could not be read, for example because the membership read
460+
failed, keeps its literal the same way. It logs `could not be expanded — a
461+
directory or read-filter lookup failed` with the `error`, never
462+
`expanded to nobody`, and never routes to part of the group.
459463
- **A slate left empty, or holding only literals,** goes to the node's
460464
`onEmptyApprovers` policy, whose four values are listed under
461465
[Approving across organizations](#cross-org-approvers). Under the default,

‎packages/plugins/plugin-approvals/src/approval-service.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,9 +2691,10 @@ export class ApprovalService implements IApprovalService {
26912691
if (directoryFailure !== undefined) {
26922692
// [#29362] A failed read is not an unstaffed approver: say which one
26932693
// this is, and carry the error, so an operator does not go staffing a
2694-
// position whose holders simply could not be read.
2694+
// position whose holders simply could not be read. The try above also
2695+
// wraps the cross-org read filter (`bounded`), so the line names both.
26952696
this.logger?.warn?.(
2696-
`[approvals] approver '${type}:${a.value}' could not be expanded — a directory read failed, so the `
2697+
`[approvals] approver '${type}:${a.value}' could not be expanded — a directory or read-filter lookup failed, so the `
26972698
+ `slot falls back to its literal rather than route to part of its people. Fix the read (see error); `
26982699
+ `a slate left naming no one is decided by the node's onEmptyApprovers.`,
26992700
{ type, value: a.value, organizationId: organizationId ?? null, error: directoryFailure },

‎packages/plugins/plugin-approvals/src/member-tier-holders-complete.integration.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ describe('[#29362] approvals membership-tier expansion reads every holder (drive
177177
const failed = rig.warn.mock.calls.filter(([, meta]) => typeof meta?.error === 'string');
178178
expect(failed.map(([, meta]) => meta.type)).toEqual(['position', 'org_membership_level']);
179179
for (const [line, meta] of failed) {
180-
expect(line).toMatch(/could not be expanded/);
180+
expect(line).toMatch(/could not be expanded — a directory or read-filter lookup failed/);
181181
expect(meta.error).toMatch(/SQLITE_IOERR/);
182182
}
183183
});

‎packages/plugins/plugin-sharing/src/recipient-width.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,3 +583,66 @@ describe('#14547 — an org-stamped rule against a SEEDED business unit', () =>
583583
});
584584
});
585585
});
586+
587+
describe('#29362 — the per-record pass is best-effort per rule', () => {
588+
// A rule whose recipients cannot be read now REFUSES (the role read past a
589+
// failed `sys_member` read throws instead of answering nobody). The record's
590+
// write hook reconciles every rule on the object in one pass, so the refusal
591+
// must stay with its own rule: that rule's grants stay as they were, it is
592+
// reported, and the rules after it still reconcile.
593+
const POSITION_RULE = 'share_note_with_sales_reps';
594+
const USER_RULE = 'share_note_with_auditor';
595+
596+
it('the first rule refuses, its grant stays, and the second rule still reconciles', async () => {
597+
const engine = makeEngine();
598+
const warn = vi.fn();
599+
const sharing = new SharingService({ engine: engine as any });
600+
const rules = new SharingRuleService({ engine: engine as any, sharing, logger: { warn, info: vi.fn(), error: vi.fn(), debug: vi.fn() } as any });
601+
engine.seed('showcase_private_note', [{ id: 'note_1', tag: 'shared', owner_id: 'author' }]);
602+
engine.seed('sys_member', [{ id: 'mem_rep', user_id: 'u_rep', organization_id: null, role: 'sales_rep' }]);
603+
// Listed in this order: the position rule is reconciled FIRST.
604+
engine.seed('sys_sharing_rule', [
605+
{
606+
id: 'srule_position', organization_id: null, name: POSITION_RULE,
607+
label: 'Note → sales reps', object_name: 'showcase_private_note',
608+
criteria_json: JSON.stringify({ tag: 'shared' }),
609+
recipient_type: 'position', recipient_id: 'sales_rep',
610+
access_level: 'read', active: true, managed_by: 'package',
611+
},
612+
{
613+
id: 'srule_user', organization_id: null, name: USER_RULE,
614+
label: 'Note → auditor', object_name: 'showcase_private_note',
615+
criteria_json: JSON.stringify({ tag: 'shared' }),
616+
recipient_type: 'user', recipient_id: 'u_auditor',
617+
access_level: 'read', active: true, managed_by: 'package',
618+
},
619+
]);
620+
const grantsOf = (sourceId: string) => (engine._tables.sys_record_share ?? [])
621+
.filter((r) => r.record_id === 'note_1' && r.source === 'rule' && r.source_id === sourceId)
622+
.map((r) => String(r.recipient_id));
623+
624+
// Both rules materialise while the membership is readable.
625+
await rules.evaluateAllForRecord('showcase_private_note', 'note_1', SYS);
626+
expect(grantsOf('srule_position')).toEqual(['u_rep']);
627+
expect(grantsOf('srule_user')).toEqual(['u_auditor']);
628+
629+
// The auditor's grant goes missing, and the membership read starts failing.
630+
const shares = engine._tables.sys_record_share!;
631+
shares.splice(shares.findIndex((r) => r.source_id === 'srule_user'), 1);
632+
const find = engine.find.bind(engine);
633+
engine.find = async (o: string, opts?: any) => {
634+
if (o === 'sys_member') throw new Error('SQLITE_IOERR: disk I/O error');
635+
return find(o, opts);
636+
};
637+
638+
const results = await rules.evaluateAllForRecord('showcase_private_note', 'note_1', SYS);
639+
640+
expect(grantsOf('srule_position')).toEqual(['u_rep']); // not revoked on a read it could not make
641+
expect(grantsOf('srule_user')).toEqual(['u_auditor']); // the next rule still reconciled
642+
expect(results).toHaveLength(1);
643+
const failed = warn.mock.calls.filter(([line]) => line === '[sharing-rule] record reconcile failed for rule');
644+
expect(failed).toHaveLength(1);
645+
expect(failed[0]![1]).toMatchObject({ rule: POSITION_RULE, recordId: 'note_1' });
646+
expect(failed[0]![1].error).toMatch(/could not be read/);
647+
});
648+
});

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,12 +1410,25 @@ export class SharingRuleService implements ISharingRuleService {
14101410
// deactivation is honoured immediately.
14111411
const pass: RuleEvaluationPass = {};
14121412
for (const rule of rules) {
1413-
// An inactive rule desires nothing; skip the criteria query entirely.
1414-
// [#15072] The match read answers with the ROW, not a boolean: for a
1415-
// `field` rule the recipients are on it, read by the per-record switch.
1416-
const matched = rule.active ? await this.matchRecord(rule, recordId) : null;
1417-
const users = matched ? await this.expandRecipientForRecord(rule, matched, pass) : [];
1418-
results.push(await this.reconcileForRecord(rule, recordId, matched !== null, users));
1413+
// [#29362] Best-effort per rule, as {@link evaluateAllRulesForObject} is:
1414+
// a rule whose recipients cannot be read (a membership read that failed
1415+
// now refuses instead of answering nobody) leaves ITS grants as they
1416+
// were and is reported, and the record's other rules still reconcile.
1417+
try {
1418+
// An inactive rule desires nothing; skip the criteria query entirely.
1419+
// [#15072] The match read answers with the ROW, not a boolean: for a
1420+
// `field` rule the recipients are on it, read by the per-record switch.
1421+
const matched = rule.active ? await this.matchRecord(rule, recordId) : null;
1422+
const users = matched ? await this.expandRecipientForRecord(rule, matched, pass) : [];
1423+
results.push(await this.reconcileForRecord(rule, recordId, matched !== null, users));
1424+
} catch (err: any) {
1425+
this.logger?.warn?.('[sharing-rule] record reconcile failed for rule', {
1426+
object,
1427+
recordId,
1428+
rule: rule.name ?? rule.id,
1429+
error: err?.message,
1430+
});
1431+
}
14191432
}
14201433
return results;
14211434
}

0 commit comments

Comments
 (0)