@@ -12,7 +12,8 @@ import {
1212 type InstalledManifestLookup ,
1313} from '@objectstack/metadata-core' ;
1414import { canonicalMetaUrlType } from '@objectstack/spec/shared' ;
15- import { isServedOverrideRowOf } from './served-override.js' ;
15+ import { isServedOverrideRowOf , type ServedOverrideRow } from './served-override.js' ;
16+ import { isMovedBaseRowOf , isMovedOverrideRowOf , type ShipsName } from './suite-moved-overlay.js' ;
1617import {
1718 listSlotNameOf ,
1819} from './protocol-object-extension-item.js' ;
@@ -39,6 +40,40 @@ interface ServedOverlayRowCandidate {
3940 * package's own item, never the base's.
4041 */
4142 readonly overrideOf ?: string ;
43+ /**
44+ * [#31593] Set on the two candidates that hold a tenant's edit of the
45+ * item from before it moved to `overrideOf` from another package of the
46+ * same App (`suite-moved-overlay.ts`): `'override'`, a declared override
47+ * in the App's pair that names the former owner
48+ * ({@link isMovedOverrideRowOf}), at `packageId` the pair; `'base'`, a row
49+ * bound to the former owner itself ({@link isMovedBaseRowOf}), at any
50+ * package (`packageId` undefined). A row holds such a candidate only when
51+ * its predicate admits it, and it is then served as `overrideOf`'s item.
52+ */
53+ readonly moved ?: 'override' | 'base' ;
54+ }
55+
56+ /**
57+ * [#31593] Whether `row` holds `candidate`'s place beyond the `where` it
58+ * matched: the declared override when the candidate is one
59+ * ({@link isServedOverrideRowOf}), the moved edit when the candidate is one
60+ * ({@link isMovedOverrideRowOf}, {@link isMovedBaseRowOf}), and any row
61+ * otherwise. The one reading both readers of the candidates take
62+ * ({@link findServedOverlayRow}, {@link mergePackageAwareOverlay}).
63+ */
64+ export function holdsServedOverlayCandidate (
65+ candidate : ServedOverlayRowCandidate ,
66+ row : ServedOverrideRow ,
67+ installed : InstalledManifestLookup | undefined ,
68+ ships : ShipsName | undefined ,
69+ ) : boolean {
70+ if ( candidate . overrideOf === undefined ) return true ;
71+ if ( installed === undefined ) return false ;
72+ if ( candidate . moved === undefined ) return isServedOverrideRowOf ( row , candidate . overrideOf , installed ) ;
73+ if ( ships === undefined ) return false ;
74+ return candidate . moved === 'override'
75+ ? isMovedOverrideRowOf ( row , candidate . overrideOf , installed , ships )
76+ : isMovedBaseRowOf ( row , candidate . overrideOf , installed , ships ) ;
4277}
4378
4479/**
@@ -112,6 +147,14 @@ export function withoutServedRowName(item: unknown): Record<string, unknown> | u
112147 * the save never wrote (#23165's shape). A package-unbound row still wins
113148 * over the code package and over another package's row (ADR-0146 D8
114149 * item 1) until the migration moves it into the pair (D11).
150+ * - [#31593] …and, when the read also names which packages ship the name
151+ * (`ships`), a tenant's edit of the item from before it moved to the
152+ * package from another package of the same App (`suite-moved-overlay.ts`):
153+ * the App's pair's override that declares the former owner, right after
154+ * the package's own declared override; and a row bound to the former owner
155+ * itself, LAST, after the package-less row, which wins over another
156+ * package's row (D8 item 1). A read that names no `ships` (every write
157+ * address) is unchanged.
115158 *
116159 * Both readers of stored content take this order, so they cannot pick
117160 * differently: {@link ObjectStackProtocolImplementation.findServedOverlayRow}
@@ -133,13 +176,18 @@ export function servedOverlayRowCandidates(address: ServedOverlayAddress): Serve
133176 const pairs = address . servedOverride === true && typeof base === 'string' && base !== ''
134177 ? pairsOf ( base , address . installed )
135178 : [ ] ;
136- const places : ReadonlyArray < Pick < ServedOverlayRowCandidate , 'packageId' | 'overrideOf' > > = base === null
179+ // [#31593] The moved edits are candidates only when the read names the
180+ // packages that ship the name (`ships`): the predicates need it.
181+ const moved = typeof base === 'string' && base !== '' && address . servedOverride === true && address . ships !== undefined ;
182+ const places : ReadonlyArray < Pick < ServedOverlayRowCandidate , 'packageId' | 'overrideOf' | 'moved' > > = base === null
137183 ? [ { packageId : null } ]
138184 : base
139185 ? [
140186 ...pairs . map ( ( pair ) => ( { packageId : pair , overrideOf : base } ) ) ,
187+ ...( moved ? pairs . map ( ( pair ) => ( { packageId : pair , overrideOf : base , moved : 'override' as const } ) ) : [ ] ) ,
141188 { packageId : base } ,
142189 { packageId : null } ,
190+ ...( moved ? [ { packageId : undefined , overrideOf : base , moved : 'base' as const } ] : [ ] ) ,
143191 ]
144192 : [ { packageId : undefined } ] ;
145193 return ( [ 'canonical' , 'other' ] as const ) . flatMap ( ( spelling ) =>
@@ -177,6 +225,12 @@ export type ServedOverlayAddress =
177225 readonly servedOverride : boolean | undefined ;
178226 /** The installed manifests of the registry the read serves, read for the address's pair. */
179227 readonly installed : InstalledManifestLookup ;
228+ /**
229+ * [#31593] Which code packages ship the item the read serves, in the
230+ * registry it serves. Named, the moved edits of the item are
231+ * candidates too (`suite-moved-overlay.ts`); omitted, they are not.
232+ */
233+ readonly ships ?: ShipsName ;
180234 } ;
181235
182236/**
@@ -359,20 +413,28 @@ export function mergePackageAwareOverlay(
359413 list : readonly Contribution [ ] ,
360414 real : string | undefined ,
361415 ) : { item : any ; overrideOf ?: string } | undefined => {
416+ // [#31593] The code packages that ship the slot's item: its
417+ // lower-layer contributions, each under its own package.
418+ const shippers = new Set ( list
419+ . filter ( ( c ) => c . stored === undefined && ! c . standIn && c . pkg !== undefined )
420+ . map ( ( c ) => c . pkg as string ) ) ;
421+ const ships = ( packageId : string ) : boolean => shippers . has ( packageId ) ;
362422 for ( const candidate of servedOverlayRowCandidates ( options . installed !== undefined
363- ? { packageId : real , servedOverride : options . servedOverride , installed : options . installed }
423+ ? { packageId : real , servedOverride : options . servedOverride , installed : options . installed , ships }
364424 : { packageId : real } ) ) {
365- const hit = list . find ( ( c ) => c . stored !== undefined
425+ const hits = list . filter ( ( c ) => c . stored !== undefined
366426 && ( c . stored . type === type ? 'canonical' : 'other' ) === candidate . spelling
367427 && ( candidate . packageId === undefined || ( c . pkg ?? null ) === candidate . packageId )
368428 // [#28409] The pair's row holds the override candidate only
369429 // when it declares the override: judged on the row's own
370430 // binding (the contribution's `pkg`, the row's `package_id`)
371431 // and its body, never on a stamp the body carries.
372- && ( candidate . overrideOf === undefined || isServedOverrideRowOf (
373- { type : c . stored . type , package_id : c . pkg , metadata : c . item } , candidate . overrideOf ,
374- options . installed ! ,
375- ) ) ) ;
432+ // [#31593] …and a moved candidate, when its predicate admits it.
433+ && holdsServedOverlayCandidate ( candidate ,
434+ { type : c . stored . type , package_id : c . pkg , metadata : c . item } , options . installed , ships ) ) ;
435+ // [#31593] Two former owners' rows could follow the item: neither
436+ // is guessed (`suite-moved-overlay.ts`).
437+ const hit = candidate . moved === 'base' ? ( hits . length === 1 ? hits [ 0 ] : undefined ) : hits [ 0 ] ;
376438 if ( hit ) return candidate . overrideOf === undefined ? { item : hit . item } : { item : hit . item , overrideOf : candidate . overrideOf } ;
377439 }
378440 return undefined ;
0 commit comments