@@ -3141,6 +3141,34 @@ function withoutField(schema: ServiceObject, name: string): ServiceObject {
31413141 return { ...schema , fields : rest } as ServiceObject ;
31423142}
31433143
3144+ /**
3145+ * [#28697] The undo record of one registration run under
3146+ * {@link SchemaRegistry.restoringOnRefusal}: for each registry entry the run
3147+ * writes, the way to put back what the entry held just before the run's FIRST
3148+ * write to it. An entry the run never writes is never recorded, so a restore
3149+ * puts back exactly the entries the run touched and nothing else — another
3150+ * package's entries and a tenant's layers are left as they are.
3151+ */
3152+ class RegistrationRestorePoint {
3153+ private readonly touched = new Set < string > ( ) ;
3154+ private readonly undo : Array < ( ) => void > = [ ] ;
3155+
3156+ /**
3157+ * Record `key` before its first write in this run. `capture` reads the
3158+ * entry as it is now and returns how to put it back.
3159+ */
3160+ note ( key : string , capture : ( ) => ( ) => void ) : void {
3161+ if ( this . touched . has ( key ) ) return ;
3162+ this . touched . add ( key ) ;
3163+ this . undo . push ( capture ( ) ) ;
3164+ }
3165+
3166+ /** Put every recorded entry back, the last one written first. */
3167+ restore ( ) : void {
3168+ for ( let i = this . undo . length - 1 ; i >= 0 ; i -- ) this . undo [ i ] ( ) ;
3169+ }
3170+ }
3171+
31443172export class SchemaRegistry {
31453173 // ==========================================
31463174 // Logging control
@@ -3423,6 +3451,173 @@ export class SchemaRegistry {
34233451 this . initialDisabledPackageIds = new Set ( ids ) ;
34243452 }
34253453
3454+ // ==========================================
3455+ // Restore on refusal
3456+ // ==========================================
3457+
3458+ /** The open {@link RegistrationRestorePoint}, while {@link restoringOnRefusal} runs. */
3459+ private restorePoint : RegistrationRestorePoint | undefined ;
3460+
3461+ /**
3462+ * [#28697] Run `register` — one registration made of several registry
3463+ * writes, such as an upgrade of an installed package — and, when it throws,
3464+ * put back exactly the entries it wrote, then throw the same error.
3465+ *
3466+ * Every refusal a verb below raises keeps its code and words; this changes
3467+ * only what is left afterwards. A refusal raised after some of the run's
3468+ * writes (an object another package owns, a field with no `type`, a
3469+ * picklist value already added, …) would otherwise leave those writes
3470+ * serving beside the entries they replaced: an upgrade's new package record
3471+ * and the objects it reached, next to the installed version's remainder.
3472+ *
3473+ * What is recorded, each entry before the run's first write to it: an
3474+ * object's contributor list (an `extend` or overlay layer of another package
3475+ * or a tenant sits in it and is put back unchanged), a namespace's owners,
3476+ * an item slot (the package record included), this package's picklist
3477+ * extension of one list, and the security catalog and default record page
3478+ * claims the package door records. Nothing the run does not write is
3479+ * recorded or put back. After a restore every merged object is
3480+ * re-resolved, since a fold computed during the run may have read an entry
3481+ * the restore put back.
3482+ *
3483+ * ⛔ Not a way back from an operator's verb: {@link uninstallPackage},
3484+ * {@link unregisterObject} and the overlay removals are never journaled.
3485+ * Synchronous by contract: `register` must not return a promise, so no
3486+ * other caller's write can land between the run's writes and its restore.
3487+ * A nested call joins the run that is already open.
3488+ */
3489+ restoringOnRefusal < T > ( register : ( ) => T ) : T {
3490+ if ( this . restorePoint ) return register ( ) ;
3491+ const point = new RegistrationRestorePoint ( ) ;
3492+ this . restorePoint = point ;
3493+ try {
3494+ const result = register ( ) ;
3495+ if ( result instanceof Promise ) {
3496+ throw new TypeError ( '[Registry] restoringOnRefusal takes a synchronous registration; it was handed one that returned a promise.' ) ;
3497+ }
3498+ return result ;
3499+ } catch ( err ) {
3500+ point . restore ( ) ;
3501+ this . invalidateAll ( ) ;
3502+ throw err ;
3503+ } finally {
3504+ this . restorePoint = undefined ;
3505+ }
3506+ }
3507+
3508+ /** Record an object's contributor list (and each contributor's mutable slots) before its first write. */
3509+ private noteObjectEntry ( fqn : string ) : void {
3510+ this . restorePoint ?. note ( `object\u0000${ fqn } ` , ( ) => {
3511+ const list = this . objectContributors . get ( fqn ) ;
3512+ if ( ! list ) return ( ) => this . closeObjectEntry ( fqn ) ;
3513+ // The late-install branch re-classifies a sitting contributor in place,
3514+ // so its ownership and priority are kept beside the reference.
3515+ const saved = list . map ( ( c ) => ( { c, ownership : c . ownership , priority : c . priority } ) ) ;
3516+ return ( ) => {
3517+ for ( const s of saved ) {
3518+ s . c . ownership = s . ownership ;
3519+ s . c . priority = s . priority ;
3520+ }
3521+ list . splice ( 0 , list . length , ...saved . map ( ( s ) => s . c ) ) ;
3522+ this . mergedObjectCache . delete ( fqn ) ;
3523+ } ;
3524+ } ) ;
3525+ }
3526+
3527+ /** Record a namespace's owner set before its first write. */
3528+ private noteNamespace ( namespace : string ) : void {
3529+ this . restorePoint ?. note ( `namespace\u0000${ namespace } ` , ( ) => {
3530+ const owners = this . namespaceRegistry . get ( namespace ) ;
3531+ const saved = owners ? new Set ( owners ) : undefined ;
3532+ return ( ) => {
3533+ if ( saved ) this . namespaceRegistry . set ( namespace , saved ) ;
3534+ else this . namespaceRegistry . delete ( namespace ) ;
3535+ } ;
3536+ } ) ;
3537+ }
3538+
3539+ /** Record whether an item type had a store at all, before the run can create one. */
3540+ private noteItemType ( type : string ) : void {
3541+ this . restorePoint ?. note ( `type\u0000${ type } ` , ( ) => {
3542+ const hadType = this . metadata . has ( type ) ;
3543+ return ( ) => {
3544+ if ( ! hadType && this . metadata . get ( type ) ?. size === 0 ) this . metadata . delete ( type ) ;
3545+ } ;
3546+ } ) ;
3547+ }
3548+
3549+ /** Record one item slot before its first write. */
3550+ private noteItem ( type : string , key : string ) : void {
3551+ this . restorePoint ?. note ( `item\u0000${ type } \u0000${ key } ` , ( ) => {
3552+ const collection = this . metadata . get ( type ) ;
3553+ const had = collection ?. has ( key ) ?? false ;
3554+ const value = collection ?. get ( key ) ;
3555+ return ( ) => {
3556+ const now = this . metadata . get ( type ) ;
3557+ if ( had ) now ?. set ( key , value ) ;
3558+ else now ?. delete ( key ) ;
3559+ } ;
3560+ } ) ;
3561+ }
3562+
3563+ /** Record this package's extension of one picklist before its first write. */
3564+ private notePicklistExtension ( target : string , packageKey : string ) : void {
3565+ this . restorePoint ?. note ( `picklistExtension\u0000${ target } \u0000${ packageKey } ` , ( ) => {
3566+ const byPackage = this . picklistExtensionContributions . get ( target ) ;
3567+ const saved = byPackage ?. get ( packageKey ) ;
3568+ return ( ) => {
3569+ let now = this . picklistExtensionContributions . get ( target ) ;
3570+ if ( saved ) {
3571+ if ( ! now ) this . picklistExtensionContributions . set ( target , now = new Map ( ) ) ;
3572+ now . set ( packageKey , saved ) ;
3573+ } else if ( now ) {
3574+ now . delete ( packageKey ) ;
3575+ if ( now . size === 0 ) this . picklistExtensionContributions . delete ( target ) ;
3576+ }
3577+ } ;
3578+ } ) ;
3579+ }
3580+
3581+ /** Record who holds one security catalog name before its first write. */
3582+ private noteSecurityCatalogClaim ( type : SecurityCatalogType , name : string ) : void {
3583+ this . restorePoint ?. note ( `catalogClaim\u0000${ type } \u0000${ name } ` , ( ) => {
3584+ const saved = this . securityCatalogClaims . get ( type ) ?. get ( name ) ;
3585+ return ( ) => {
3586+ let byName = this . securityCatalogClaims . get ( type ) ;
3587+ if ( saved !== undefined ) {
3588+ if ( ! byName ) this . securityCatalogClaims . set ( type , byName = new Map ( ) ) ;
3589+ byName . set ( name , saved ) ;
3590+ } else {
3591+ byName ?. delete ( name ) ;
3592+ }
3593+ } ;
3594+ } ) ;
3595+ }
3596+
3597+ /**
3598+ * Record the default record page claims before `packageId`'s first rewrite
3599+ * of them ({@link recordDefaultRecordPageClaims} drops all of the package's
3600+ * claims, then adds what the manifest declares). Put back per object, and
3601+ * only for the objects `packageId` held a claim on before or after, each in
3602+ * its holders' original order.
3603+ */
3604+ private noteDefaultRecordPageClaims ( packageId : string ) : void {
3605+ this . restorePoint ?. note ( `defaultPageClaims\u0000${ packageId } ` , ( ) => {
3606+ const saved = new Map < string , Map < string , string > > ( ) ;
3607+ for ( const [ object , byPackage ] of this . defaultRecordPageClaims ) saved . set ( object , new Map ( byPackage ) ) ;
3608+ return ( ) => {
3609+ const objects = new Set < string > ( ) ;
3610+ for ( const [ object , byPackage ] of saved ) if ( byPackage . has ( packageId ) ) objects . add ( object ) ;
3611+ for ( const [ object , byPackage ] of this . defaultRecordPageClaims ) if ( byPackage . has ( packageId ) ) objects . add ( object ) ;
3612+ for ( const object of objects ) {
3613+ const before = saved . get ( object ) ;
3614+ if ( before ) this . defaultRecordPageClaims . set ( object , new Map ( before ) ) ;
3615+ else this . defaultRecordPageClaims . delete ( object ) ;
3616+ }
3617+ } ;
3618+ } ) ;
3619+ }
3620+
34263621 // ==========================================
34273622 // Namespace Management
34283623 // ==========================================
@@ -3433,6 +3628,7 @@ export class SchemaRegistry {
34333628 */
34343629 registerNamespace ( namespace : string , packageId : string ) : void {
34353630 if ( ! namespace ) return ;
3631+ this . noteNamespace ( namespace ) ;
34363632
34373633 let owners = this . namespaceRegistry . get ( namespace ) ;
34383634 if ( ! owners ) {
@@ -3449,6 +3645,7 @@ export class SchemaRegistry {
34493645 unregisterNamespace ( namespace : string , packageId : string ) : void {
34503646 const owners = this . namespaceRegistry . get ( namespace ) ;
34513647 if ( owners ) {
3648+ this . noteNamespace ( namespace ) ;
34523649 owners . delete ( packageId ) ;
34533650 if ( owners . size === 0 ) {
34543651 this . namespaceRegistry . delete ( namespace ) ;
@@ -3623,6 +3820,7 @@ export class SchemaRegistry {
36233820 * {@link defaultRecordPageClaims} for why a re-install replaces).
36243821 */
36253822 private recordDefaultRecordPageClaims ( packageId : string , manifest : ObjectStackManifest ) : void {
3823+ this . noteDefaultRecordPageClaims ( packageId ) ;
36263824 this . forgetDefaultRecordPageClaims ( packageId ) ;
36273825 for ( const { object, page } of declaredDefaultRecordPages ( manifest ) ) {
36283826 let byPackage = this . defaultRecordPageClaims . get ( object ) ;
@@ -3752,6 +3950,7 @@ export class SchemaRegistry {
37523950 byName = new Map ( ) ;
37533951 this . securityCatalogClaims . set ( type , byName ) ;
37543952 }
3953+ this . noteSecurityCatalogClaim ( type , name ) ;
37553954 byName . set ( name , packageId ) ;
37563955 }
37573956 }
@@ -4101,6 +4300,11 @@ export class SchemaRegistry {
41014300 // record — skip rather than registering `undefined` as an owner (which
41024301 // would also mean widening `registerNamespace`'s own required-string
41034302 // contract, out of scope here).
4303+ // [#28697] Recorded before the first write below (the namespace claim),
4304+ // so a refusal raised after it — the ownership conflict — puts this
4305+ // object's layers back exactly ({@link restoringOnRefusal}).
4306+ this . noteObjectEntry ( fqn ) ;
4307+
41044308 if ( namespace && packageId ) {
41054309 this . registerNamespace ( namespace , packageId ) ;
41064310 }
@@ -6122,6 +6326,7 @@ export class SchemaRegistry {
61226326 * Universal Register Method for non-object metadata.
61236327 */
61246328 registerItem < T > ( type : string , item : T , keyField : keyof T = 'name' as keyof T , packageId ?: string ) {
6329+ this . noteItemType ( type ) ;
61256330 if ( ! this . metadata . has ( type ) ) {
61266331 this . metadata . set ( type , new Map ( ) ) ;
61276332 }
@@ -6400,6 +6605,7 @@ export class SchemaRegistry {
64006605 }
64016606 }
64026607
6608+ this . noteItem ( type , storageKey ) ;
64036609 collection . set ( storageKey , item ) ;
64046610 if ( type === 'picklist' ) this . invalidateAll ( ) ;
64056611 // [#24400] A bound action is attached to its target on read
@@ -7165,6 +7371,7 @@ export class SchemaRegistry {
71657371 this . registerNamespace ( manifest . namespace , manifest . id ) ;
71667372 }
71677373
7374+ this . noteItemType ( 'package' ) ;
71687375 if ( ! this . metadata . has ( 'package' ) ) {
71697376 this . metadata . set ( 'package' , new Map ( ) ) ;
71707377 }
@@ -7174,6 +7381,7 @@ export class SchemaRegistry {
71747381 // a normal path, not an error — keep it at debug (#3420).
71757382 this . debug ( `[Registry] Overwriting package: ${ manifest . id } ` ) ;
71767383 }
7384+ this . noteItem ( 'package' , manifest . id ) ;
71777385 collection . set ( manifest . id , pkg ) ;
71787386 // The names this package now holds, recorded whether or not an item store
71797387 // entry will carry them (see `securityCatalogClaims`).
@@ -7699,6 +7907,7 @@ export class SchemaRegistry {
76997907
77007908 let byPackage = this . picklistExtensionContributions . get ( target ) ;
77017909 if ( ! byPackage ) this . picklistExtensionContributions . set ( target , byPackage = new Map ( ) ) ;
7910+ this . notePicklistExtension ( target , packageKey ) ;
77027911 byPackage . set ( packageKey , { packageId, options : merged } ) ;
77037912 this . invalidateAll ( ) ;
77047913 this . log ( `[Registry] Registered picklist extension: ${ target } (+${ extension . options ?. length ?? 0 } ) from ${ packageId } ` ) ;
0 commit comments