@@ -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 ( / c o u l d n o t b e r e a d / ) ;
647+ } ) ;
648+ } ) ;
0 commit comments