@@ -339,6 +339,82 @@ export function namedBaseOverlayWriteRefusal(
339339 return closedOverlayWriteError ( message ) ;
340340}
341341
342+ // ─────────────────────────────────────────────────────────────────────────
343+ // ADR-0146 D1, D11 — "the fix the close names can be taken": one predicate
344+ // ─────────────────────────────────────────────────────────────────────────
345+
346+ /** The pair an overlay-write close sends the write into, and the `basedOn` its declaration records. */
347+ export interface OverlayPairFix {
348+ readonly base : string ;
349+ readonly pair : string ;
350+ readonly basedOn : string ;
351+ }
352+
353+ /**
354+ * [ADR-0146 D1, D11] The registry half of whether the fix both overlay-write
355+ * closes name — write it into `base`'s pair, declaring the override — can be
356+ * taken, or `null` when it cannot: read from the steps that write takes.
357+ *
358+ * - the base has a pair id (`customizationPackageId`; an id ending in
359+ * `.custom` has none);
360+ * - the ensure door would answer or create the pair
361+ * ({@link customizationPairEnsureRefusal});
362+ * - the base records a version (`basedOn`, its `manifest.version`, the value
363+ * the ceremony records too).
364+ *
365+ * The other half is {@link overlayPairFixAdmits}, the override write gate on
366+ * the declared body. Two halves of one predicate so that a close may read its
367+ * own subject between them — the package-less close reads the item's stored
368+ * rows only for a name the registry has not already left open — and ⛔ each
369+ * close keeps only its own difference: which base it judges, and which body.
370+ * {@link namedBaseCloseSubject} and `packagelessCloseSubject`
371+ * (`protocol-packageless-overlay-migration.ts`) ask both. Registry reads only.
372+ */
373+ export function overlayPairFixOf (
374+ host : { readonly engine : ObjectStackProtocolImplementation [ 'engine' ] } ,
375+ base : string ,
376+ ) : OverlayPairFix | null {
377+ const pairId = customizationPackageId ( base ) ;
378+ if ( ! pairId . ok ) return null ; // no pair to name (the census's `no_pair`)
379+ const pair = pairId . value ;
380+ const registry = ( host . engine as { registry ?: PairRegistry } | undefined ) ?. registry ;
381+ if ( ! registry || typeof registry . getPackage !== 'function' ) return null ;
382+ if ( customizationPairEnsureRefusal ( host . engine , registry , base , pair ) !== undefined ) return null ; // the ensure door would refuse
383+ const basedOn = registry . getPackage ( base ) ?. manifest ?. version ;
384+ if ( typeof basedOn !== 'string' || basedOn === '' ) return null ; // no `basedOn` to declare
385+ return { base, pair, basedOn } ;
386+ }
387+
388+ /**
389+ * [ADR-0146 D1, D2, D11] The gate half of {@link overlayPairFixOf}: whether
390+ * the override write gate admits the body the fix writes into the pair — the
391+ * close's `body` (a save's, or the stored row's), else the item's bare
392+ * identity at `member`, declaring `overrides: { package: base, basedOn }`. An
393+ * `email_template` member at a locale the base does not ship is refused
394+ * there, so its write stays open.
395+ */
396+ export function overlayPairFixAdmits (
397+ host : { readonly overrideWriteVerdict : ( request : OverrideWriteSubject ) => OverrideWriteVerdict } ,
398+ fix : OverlayPairFix ,
399+ subject : {
400+ /** The singular type. */
401+ readonly type : string ;
402+ readonly name : string ;
403+ readonly body : Record < string , unknown > | undefined ;
404+ /** The bundle member, as `OVERLAY_MEMBER_COLUMN` holds it. */
405+ readonly member : string | null | undefined ;
406+ } ,
407+ ) : boolean {
408+ const { type, name, body, member } = subject ;
409+ const disc = member === undefined ? undefined : overlayMemberField ( type ) ;
410+ const declaredBody = {
411+ ...( body ?? { name, ...( disc && member !== null ? { [ disc ] : member } : { } ) } ) ,
412+ overrides : { package : fix . base , basedOn : fix . basedOn } ,
413+ } ;
414+ const verdict = host . overrideWriteVerdict ( { type, name, packageId : fix . pair , item : declaredBody } ) ;
415+ return verdict . verdict !== 'refuse' ;
416+ }
417+
342418/** The class members {@link namedBaseCloseSubject} reads, typed as the class declares them. */
343419export interface NamedBaseCloseHost {
344420 readonly engine : ObjectStackProtocolImplementation [ 'engine' ] ;
@@ -367,16 +443,15 @@ export interface NamedBaseWriteSubject {
367443 * overlay types — the package-less close's reading of "packaged"
368444 * (`overlayBaseShippers`, the reading `os migrate packageless-overlays`
369445 * classifies by), with the named package among the shippers — and where the
370- * fix the refusal names can be taken, read from the steps that write takes:
446+ * fix the refusal names can be taken: {@link overlayPairFixOf} and
447+ * {@link overlayPairFixAdmits}, the predicate the package-less close asks too
448+ * (a pair id, the ensure door, a base version, the override write gate
449+ * admitting the declared body — an `email_template` member at a locale the
450+ * base does not ship stays open, as the package-less one does).
371451 *
372- * - the base has a pair id (`customizationPackageId`);
373- * - the ensure door would answer or create the pair
374- * ({@link customizationPairEnsureRefusal});
375- * - the base records a version (`basedOn`, its `manifest.version`, the value
376- * the ceremony records too);
377- * - the override write gate admits the declared body into the pair (an
378- * `email_template` member at a locale the base does not ship is refused
379- * there, so its write stays open, as the package-less one does).
452+ * This close's own difference: it reads no row (the declared body is the
453+ * save's, else the item's identity), and the named package may be one of
454+ * several shippers.
380455 *
381456 * A body carrying `overrides` is not judged here: the D2 gate answers a
382457 * declaration outside the pair, the schema gate a malformed one, as before.
@@ -398,29 +473,17 @@ export function namedBaseCloseSubject(
398473 // a malformed one (`422 INVALID_METADATA`).
399474 if ( isPlainRecord ( request . item ) && 'overrides' in request . item ) return null ;
400475 if ( ! this . overlayBaseShippers ( request . type , request . name ) . includes ( base ) ) return null ;
401- const pairId = customizationPackageId ( base ) ;
402- if ( ! pairId . ok ) return null ; // no pair to name
403- const pair = pairId . value ;
404- const registry = ( this . engine as { registry ?: PairRegistry } | undefined ) ?. registry ;
405- if ( ! registry || typeof registry . getPackage !== 'function' ) return null ;
406- if ( customizationPairEnsureRefusal ( this . engine , registry , base , pair ) !== undefined ) return null ;
407- const basedOn = registry . getPackage ( base ) ?. manifest ?. version ;
408- if ( typeof basedOn !== 'string' || basedOn === '' ) return null ; // no `basedOn` to declare
476+ const fix = overlayPairFixOf ( this , base ) ;
477+ if ( fix === null ) return null ;
409478
410479 // The gate's verdict on the declared body, written into the pair.
411480 const type = PLURAL_TO_SINGULAR [ request . type ] ?? request . type ;
412481 const written = isPlainRecord ( request . item ) ? request . item : undefined ;
413482 const member = written !== undefined && request . member === undefined
414483 ? overlayMemberOfBody ( type , written )
415484 : overlayMemberValue ( type , request . member ) ;
416- const disc = member === undefined ? undefined : overlayMemberField ( type ) ;
417- const declaredBody = {
418- ...( written ?? { name : request . name , ...( disc && member !== null ? { [ disc ] : member } : { } ) } ) ,
419- overrides : { package : base , basedOn } ,
420- } ;
421- const verdict = this . overrideWriteVerdict ( { type, name : request . name , packageId : pair , item : declaredBody } ) ;
422- if ( verdict . verdict === 'refuse' ) return null ;
423- return { base, pair } ;
485+ if ( ! overlayPairFixAdmits ( this , fix , { type, name : request . name , body : written , member } ) ) return null ;
486+ return { base, pair : fix . pair } ;
424487}
425488
426489// ─────────────────────────────────────────────────────────────────────────
0 commit comments