@@ -39,6 +39,7 @@ import {
3939} from '@objectstack/objectql' ;
4040import type { SharingEngine } from './sharing-service.js' ;
4141import type { SharingService } from './sharing-service.js' ;
42+ import { isInternalRecipientRefusal } from './sharing-service.js' ;
4243import { normalizeAccessLevel , normalizeStoredAccessLevel } from './access-level.js' ;
4344import { parseCriteria , isMatchAllCriteria , MATCH_ALL_CRITERIA_MESSAGE } from './rule-criteria.js' ;
4445import { TeamGraphService , teamHasMember } from './team-graph.js' ;
@@ -390,6 +391,22 @@ function byName(a: SharingRuleRow, b: SharingRuleRow): number {
390391export interface SharingRuleReconcilePassResult extends SharingRuleEvaluationResult {
391392 /** Grants the engine's organization rule refused; each one is also logged. */
392393 grantsRefused : number ;
394+ /**
395+ * [#25549] One line, present only when the pass skipped recipients: the rule
396+ * is anchored on a public object, where a share reaches an `external` member
397+ * of the record's organization only, so no row was written for the internal
398+ * recipients it expands to. The count is in the pass's log line.
399+ */
400+ note ?: string ;
401+ }
402+
403+ /** What one attempted grant of a pass came to. */
404+ type RuleGrantOutcome = 'granted' | 'refused' | 'internal' ;
405+
406+ /** [#25549] The pass's one-line note when it skipped internal recipients. */
407+ function internalHalfNote ( object : string ) : string {
408+ return `"${ object } " has a public sharing model: rows were written for external recipients only; `
409+ + 'the internal recipients this rule expands to already reach its records, so none was written for them' ;
393410}
394411
395412export interface SharingRuleServiceOptions {
@@ -1231,6 +1248,7 @@ export class SharingRuleService implements ISharingRuleService {
12311248 total . grantsUpdated += pass . grantsUpdated ;
12321249 total . grantsRevoked += pass . grantsRevoked ;
12331250 total . grantsRefused += pass . grantsRefused ;
1251+ if ( pass . note ) total . note = pass . note ;
12341252 }
12351253 return total ;
12361254 }
@@ -1635,19 +1653,23 @@ export class SharingRuleService implements ISharingRuleService {
16351653 pass . expandedUsers = 1 ;
16361654 const matchedIds = await this . findMatchingRecords ( rule ) ;
16371655 pass . matchedRecords = matchedIds . length ;
1656+ let skipped = 0 ;
16381657 for ( const recordId of matchedIds ) {
16391658 const cur = held . get ( recordId ) ;
16401659 if ( cur ) {
16411660 // Out of the stale set BEFORE the attempt — the twin of the note in
16421661 // {@link reconcile }: a refused level update leaves the desired row.
16431662 held . delete ( recordId ) ;
16441663 if ( cur . access_level === rule . access_level ) continue ;
1645- if ( await this . grantOrAbsorbOrganizationRefusal ( rule , recordId , userId ) ) pass . grantsUpdated += 1 ;
1646- else pass . grantsRefused += 1 ;
1647- } else if ( await this . grantOrAbsorbOrganizationRefusal ( rule , recordId , userId ) ) {
1648- pass . grantsCreated += 1 ;
1664+ const outcome = await this . grantOrAbsorbOrganizationRefusal ( rule , recordId , userId ) ;
1665+ if ( outcome === 'granted' ) pass . grantsUpdated += 1 ;
1666+ else if ( outcome === 'refused' ) pass . grantsRefused += 1 ;
1667+ else skipped += 1 ;
16491668 } else {
1650- pass . grantsRefused += 1 ;
1669+ const outcome = await this . grantOrAbsorbOrganizationRefusal ( rule , recordId , userId ) ;
1670+ if ( outcome === 'granted' ) pass . grantsCreated += 1 ;
1671+ else if ( outcome === 'refused' ) pass . grantsRefused += 1 ;
1672+ else skipped += 1 ;
16511673 }
16521674 }
16531675 // The user's grants on records the rule no longer matches — what the
@@ -1656,7 +1678,7 @@ export class SharingRuleService implements ISharingRuleService {
16561678 await this . sharing . revoke ( String ( stale . id ) , SYSTEM_CTX ) ;
16571679 pass . grantsRevoked += 1 ;
16581680 }
1659- return pass ;
1681+ return this . withInternalHalfNote ( rule , pass , skipped ) ;
16601682 }
16611683
16621684 // ── internals ─────────────────────────────────────────────────────
@@ -2291,18 +2313,26 @@ export class SharingRuleService implements ISharingRuleService {
22912313 * retire that decision as a side effect, and its three pins are what
22922314 * notices.
22932315 *
2294- * Returns `true ` when the grant landed, `false ` when it was refused —
2316+ * Returns `'granted' ` when the grant landed, `'refused' ` when it was refused —
22952317 * never throws for the refusal, and the caller counts what it is told.
2318+ *
2319+ * [#25549] …and `'internal'` for the one other refusal a pass absorbs: on a
2320+ * public object the grant service admits an `external` member of the
2321+ * record's organization only ({@link isInternalRecipientRefusal}). That
2322+ * recipient is skipped, not counted as refused — the row would never be
2323+ * consulted for it — and the pass reports the skipped half in one line
2324+ * ({@link withInternalHalfNote}). Every other D7 refusal (bypass, owner-less,
2325+ * `controlled_by_parent`, phantom anchor) still aborts the rule's pass.
22962326 */
22972327 private async grantOrAbsorbOrganizationRefusal (
22982328 rule : SharingRuleRow ,
22992329 recordId : string ,
23002330 recipientId : string ,
2301- ) : Promise < boolean > {
2331+ ) : Promise < RuleGrantOutcome > {
23022332 // A rule that may sweep nowhere matched nothing, so it never asks for a
23032333 // grant; refused here too, so no caller can write one around the sweep.
23042334 const context = this . sweepContextOrReport ( rule ) ;
2305- if ( ! context ) return false ;
2335+ if ( ! context ) return 'refused' ;
23062336 try {
23072337 await this . sharing . grant (
23082338 {
@@ -2317,8 +2347,9 @@ export class SharingRuleService implements ISharingRuleService {
23172347 } as any ,
23182348 context ,
23192349 ) ;
2320- return true ;
2350+ return 'granted' ;
23212351 } catch ( err : any ) {
2352+ if ( isInternalRecipientRefusal ( err ) ) return 'internal' ;
23222353 if ( ! isSystemWriteOrganizationRequiredError ( err ) ) throw err ;
23232354 this . logger ?. warn ?.(
23242355 '[sharing-rule] grant refused by the engine organization rule — counted, pass continues' ,
@@ -2330,10 +2361,29 @@ export class SharingRuleService implements ISharingRuleService {
23302361 code : SYSTEM_WRITE_ORGANIZATION_REQUIRED_CODE ,
23312362 } ,
23322363 ) ;
2333- return false ;
2364+ return 'refused' ;
23342365 }
23352366 }
23362367
2368+ /**
2369+ * [#25549] Put the one-line note on a pass that skipped internal recipients,
2370+ * and log the count once per pass — never once per skipped grant, which on a
2371+ * rule over N records and M staff would be N×M lines of the same fact.
2372+ */
2373+ private withInternalHalfNote (
2374+ rule : SharingRuleRow ,
2375+ pass : SharingRuleReconcilePassResult ,
2376+ skipped : number ,
2377+ ) : SharingRuleReconcilePassResult {
2378+ if ( skipped === 0 ) return pass ;
2379+ this . logger ?. info ?.( '[sharing-rule] internal recipients skipped on a public object — external recipients only' , {
2380+ rule : rule . name ?? rule . id ,
2381+ object : rule . object_name ,
2382+ skipped,
2383+ } ) ;
2384+ return { ...pass , note : internalHalfNote ( rule . object_name ) } ;
2385+ }
2386+
23372387 /**
23382388 * Diff a whole-rule pass's desired pairs against the rule's materialised
23392389 * grants: upsert what is wanted, revoke the remainder.
@@ -2362,6 +2412,7 @@ export class SharingRuleService implements ISharingRuleService {
23622412 let updated = 0 ;
23632413 let revoked = 0 ;
23642414 let refused = 0 ;
2415+ let skipped = 0 ;
23652416
23662417 // Upsert desired. [#14754] Per grant, not per pass: a refusal is counted
23672418 // and the loop goes on, so the revoke loop below is REACHED.
@@ -2375,16 +2426,16 @@ export class SharingRuleService implements ISharingRuleService {
23752426 // inverted, and strictly worse than the abort it replaces.
23762427 existingMap . delete ( k ) ;
23772428 if ( cur . access_level !== rule . access_level ) {
2378- if ( await this . grantOrAbsorbOrganizationRefusal ( rule , want . record_id , want . recipient_id ) ) {
2379- updated += 1 ;
2380- } else {
2381- refused += 1 ;
2382- }
2429+ const outcome = await this . grantOrAbsorbOrganizationRefusal ( rule , want . record_id , want . recipient_id ) ;
2430+ if ( outcome === 'granted' ) updated += 1 ;
2431+ else if ( outcome === 'refused' ) refused += 1 ;
2432+ else skipped += 1 ;
23832433 }
2384- } else if ( await this . grantOrAbsorbOrganizationRefusal ( rule , want . record_id , want . recipient_id ) ) {
2385- created += 1 ;
23862434 } else {
2387- refused += 1 ;
2435+ const outcome = await this . grantOrAbsorbOrganizationRefusal ( rule , want . record_id , want . recipient_id ) ;
2436+ if ( outcome === 'granted' ) created += 1 ;
2437+ else if ( outcome === 'refused' ) refused += 1 ;
2438+ else skipped += 1 ;
23882439 }
23892440 }
23902441 // Revoke stale.
@@ -2393,15 +2444,15 @@ export class SharingRuleService implements ISharingRuleService {
23932444 revoked += 1 ;
23942445 }
23952446
2396- return {
2447+ return this . withInternalHalfNote ( rule , {
23972448 ruleId : rule . id ,
23982449 matchedRecords : desiredSet . matchedRecords ,
23992450 expandedUsers : desiredSet . expandedUsers ,
24002451 grantsCreated : created ,
24012452 grantsUpdated : updated ,
24022453 grantsRevoked : revoked ,
24032454 grantsRefused : refused ,
2404- } ;
2455+ } , skipped ) ;
24052456 }
24062457
24072458 private async reconcileForRecord (
@@ -2423,6 +2474,7 @@ export class SharingRuleService implements ISharingRuleService {
24232474 let updated = 0 ;
24242475 let revoked = 0 ;
24252476 let refused = 0 ;
2477+ let skipped = 0 ;
24262478
24272479 if ( match ) {
24282480 // [#14754] Per grant, exactly as in {@link reconcile}: one recipient the
@@ -2436,16 +2488,16 @@ export class SharingRuleService implements ISharingRuleService {
24362488 // desired row standing, never fall through to the revoke loop.
24372489 existingMap . delete ( userId ) ;
24382490 if ( cur . access_level !== rule . access_level ) {
2439- if ( await this . grantOrAbsorbOrganizationRefusal ( rule , recordId , userId ) ) {
2440- updated += 1 ;
2441- } else {
2442- refused += 1 ;
2443- }
2491+ const outcome = await this . grantOrAbsorbOrganizationRefusal ( rule , recordId , userId ) ;
2492+ if ( outcome === 'granted' ) updated += 1 ;
2493+ else if ( outcome === 'refused' ) refused += 1 ;
2494+ else skipped += 1 ;
24442495 }
2445- } else if ( await this . grantOrAbsorbOrganizationRefusal ( rule , recordId , userId ) ) {
2446- created += 1 ;
24472496 } else {
2448- refused += 1 ;
2497+ const outcome = await this . grantOrAbsorbOrganizationRefusal ( rule , recordId , userId ) ;
2498+ if ( outcome === 'granted' ) created += 1 ;
2499+ else if ( outcome === 'refused' ) refused += 1 ;
2500+ else skipped += 1 ;
24492501 }
24502502 }
24512503 }
@@ -2456,15 +2508,15 @@ export class SharingRuleService implements ISharingRuleService {
24562508 revoked += 1 ;
24572509 }
24582510
2459- return {
2511+ return this . withInternalHalfNote ( rule , {
24602512 ruleId : rule . id ,
24612513 matchedRecords : match ? 1 : 0 ,
24622514 expandedUsers : users . length ,
24632515 grantsCreated : created ,
24642516 grantsUpdated : updated ,
24652517 grantsRevoked : revoked ,
24662518 grantsRefused : refused ,
2467- } ;
2519+ } , skipped ) ;
24682520 }
24692521
24702522 /** Revoke every grant `scope` names ({@link ruleGrantScope}), one row at a time through the sharing service. */
0 commit comments