77 *
88 * ## The door
99 *
10+ * The unit is the App (ADR-0146 D3 as amended, ruling record 6691022506): the
11+ * pair of a base is the pair of the App that carries it ({@link pairOf},
12+ * `@objectstack/metadata-core`), so the door asked for any package of an App
13+ * answers that App's one pair, `<app id>.custom` — creating it on the first
14+ * ask and answering it after, never `422` for a package of the same App. A
15+ * package that declares no App is its own unit, as before.
16+ *
1017 * {@link ensureCustomizationPackage} is the body of
1118 * `ObjectStackProtocolImplementation#ensureCustomizationPackage`, served over
1219 * HTTP as `POST /api/v1/packages/:id/ensure-customization`
1522 * the pair (ADR-0146 D3: "no second door builds the pair"), and the one Studio,
1623 * the clone door and the migration call on a customer's first customization.
1724 *
18- * Everything the pair is made of is the base's, through the pairing rules of
19- * `@objectstack/spec/kernel` (`customization-package.ts`), ⛔ never a local
20- * spelling:
25+ * Everything the pair is made of is its App's — the asked base's App, which
26+ * is the base itself for an App or an own-unit package — through the pairing
27+ * rules of `@objectstack/spec/kernel` (`customization-package.ts`), ⛔ never a
28+ * local spelling:
2129 *
22- * - id — `customizationPackageId`, asked through {@link pairOf}: `<base >.custom`;
23- * - namespace — {@link customizationNamespace}: `<base namespace>_custom`, SET
30+ * - id — `customizationPackageId`, asked through {@link pairOf}: `<app id >.custom`;
31+ * - namespace — {@link customizationNamespace}: `<app namespace>_custom`, SET
2432 * EXPLICITLY on the manifest so `installPackage`'s id-derived default
2533 * (`deriveNamespaceFromPackageId('hotcrm.custom')` = `custom`) never runs
2634 * (ADR-0146 D3, Q4 「全部同意」);
27- * - dependency — {@link customizationDependencyRange}: `^<major>` of the base 's
28- * `manifest.version` at creation, declared on the base in
35+ * - dependency — {@link customizationDependencyRange}: `^<major>` of the App 's
36+ * `manifest.version` at creation, declared on the App in
2937 * `manifest.dependencies` (ADR-0141 D1);
3038 * - writable — a scope-less authored package, the same record `POST
3139 * /packages` writes for a Studio-created base.
5967 *
6068 * - `400` — the request is malformed, or the base id has no pair (it is
6169 * itself a pair, or `<base>.custom` is not a valid package id);
70+ * - `409` — the base reaches several Apps and its namespace picks none or
71+ * more than one of them (ADR-0146 D3 merge question 11: no single App, so
72+ * no pair, never a guess);
6273 * - `404` — the base is not installed;
6374 * - `409` — the base's state rules the pair out: the base is a kernel
6475 * package ({@link kernelPackageScopeOf}), whose objects are not
6879 * pair namespace fails the manifest grammar (longer than
6980 * {@link MAX_CUSTOMIZATION_BASE_NAMESPACE_LENGTH}, no truncation and no
7081 * hashing); its version is not SemVer, so no range can be declared; or an
71- * installed `<base>.custom` lacks the namespace or the dependency;
72- * - `422 NAMESPACE_CONFLICT` — another package owns the pair's namespace: the
82+ * installed pair lacks the namespace or the dependency; or the pair's
83+ * namespace is held by the per-package pair an earlier version created for
84+ * a member of the same App (ADR-0146 D3 merge question 12: that pair stays
85+ * served, and the App's pair waits for it to be re-identified — never
86+ * `422` for a package of the same App);
87+ * - `422 NAMESPACE_CONFLICT` — a package outside the App owns the pair's namespace: the
7388 * code and status `SchemaRegistry.installPackage` stamps for the same
7489 * condition (`@objectstack/objectql`; that package depends on this one, so
7590 * the string is spelled in each).
@@ -102,10 +117,11 @@ import type {
102117import {
103118 MAX_CUSTOMIZATION_BASE_NAMESPACE_LENGTH ,
104119 customizationBasePackageId ,
120+ customizationPackageId ,
105121 customizationDependencyRange ,
106122 customizationNamespace ,
107123} from '@objectstack/spec/kernel' ;
108- import { pairOf } from '@objectstack/metadata-core' ;
124+ import { appOf , pairOf } from '@objectstack/metadata-core' ;
109125import { installedManifestsOfEngine } from './installed-manifests.js' ;
110126import type { CustomizationNamespaceRefusal } from '@objectstack/spec/kernel' ;
111127
@@ -287,6 +303,11 @@ export async function ensureCustomizationPackage(
287303 const baseId = declared . data . basePackageId ;
288304
289305 const pair = pairOf ( baseId , installedManifestsOfEngine ( this . engine ) ) ;
306+ if ( ! pair . ok && pair . reason === 'no_single_app' ) {
307+ throw refusal ( 409 , `Package '${ baseId } ' declares several Apps and its namespace picks no single one of them, `
308+ + 'so it has no paired customization package: a customization lands in the pair of the one App that '
309+ + 'carries the package, and none is chosen by guess (ADR-0146 D3).' ) ;
310+ }
290311 if ( ! pair . ok ) {
291312 throw refusal ( 400 , pair . reason === 'base_is_customization_package'
292313 ? `Package '${ baseId } ' is itself a paired customization package, and a pair is never paired again: `
@@ -374,9 +395,18 @@ export function customizationPairEnsureRefusal(
374395 + `('${ pairId } ') holds the changes to a read-only base — one provided by code or an installed app `
375396 + '(ADR-0146 D3).' ) ;
376397 }
377- const namespace = customizationNamespace ( base . manifest ?. namespace ) ;
398+ // [ruling record 6691022506] The pair is its App's: its namespace, its
399+ // dependency and its range are read off the App (the base itself for an
400+ // App or an own-unit package).
401+ const appId = customizationBasePackageId ( pairId ) ?? baseId ;
402+ const app = appId === baseId ? base : registry . getPackage ( appId ) ;
403+ if ( ! app ) {
404+ return refusal ( 404 , `Package '${ appId } ', the App that carries '${ baseId } ', is not installed: its paired `
405+ + `customization package '${ pairId } ' is ensured for an installed App (ADR-0146 D3).` ) ;
406+ }
407+ const namespace = customizationNamespace ( app . manifest ?. namespace ) ;
378408 if ( ! namespace . ok ) {
379- return refusal ( 409 , `Cannot ensure '${ pairId } ': ${ noPairNamespaceClause ( baseId , base . manifest ?. namespace , namespace . reason ) } `
409+ return refusal ( 409 , `Cannot ensure '${ pairId } ': ${ noPairNamespaceClause ( appId , app . manifest ?. namespace , namespace . reason ) } `
380410 + '(ADR-0146 D3).' ) ;
381411 }
382412 // An existing pair is returned unchanged, or refused naming what it lacks.
@@ -387,28 +417,42 @@ export function customizationPairEnsureRefusal(
387417 if ( declaredNamespace !== namespace . value ) {
388418 lacks . push ( `namespace '${ namespace . value } ' (it declares ${ typeof declaredNamespace === 'string' && declaredNamespace !== '' ? `'${ declaredNamespace } '` : 'none' } )` ) ;
389419 }
390- const declaredRange = declaredRangeOn ( existing . manifest , baseId ) ;
391- if ( declaredRange === undefined ) lacks . push ( `a dependency on '${ baseId } ' in manifest.dependencies` ) ;
420+ const declaredRange = declaredRangeOn ( existing . manifest , appId ) ;
421+ if ( declaredRange === undefined ) lacks . push ( `a dependency on '${ appId } ' in manifest.dependencies` ) ;
392422 if ( lacks . length > 0 ) {
393423 return refusal ( 409 , `Package '${ pairId } ' is installed but is not the paired customization package of `
394- + `'${ baseId } ': it lacks ${ lacks . join ( ', and ' ) } . It is not adopted or rewritten — delete it, or `
424+ + `'${ appId } ': it lacks ${ lacks . join ( ', and ' ) } . It is not adopted or rewritten — delete it, or `
395425 + 'install a manifest over it that declares both, then ensure the pair again (ADR-0146 D3).' ) ;
396426 }
397427 return undefined ;
398428 }
399- const range = customizationDependencyRange ( base . manifest ?. version ) ;
429+ const range = customizationDependencyRange ( app . manifest ?. version ) ;
400430 if ( ! range . ok ) {
401- return refusal ( 409 , `Cannot ensure '${ pairId } ': package '${ baseId } ' is at version `
402- + `'${ String ( base . manifest ?. version ) } ', which is not a SemVer 2.0.0 version, so the pair's dependency `
403- + 'range — the major of the base \'s version — cannot be declared (ADR-0146 D3).' ) ;
431+ return refusal ( 409 , `Cannot ensure '${ pairId } ': package '${ appId } ' is at version `
432+ + `'${ String ( app . manifest ?. version ) } ', which is not a SemVer 2.0.0 version, so the pair's dependency `
433+ + 'range — the major of the App \'s version — cannot be declared (ADR-0146 D3).' ) ;
404434 }
405435 // The pair's namespace is the pair's alone: an owner other than the pair
406436 // is refused here, before the install, whatever co-ownership the registry
407437 // would read.
408438 const owner = registry . getNamespaceOwners ?.( namespace . value ) ?. find ( ( id ) => id !== pairId ) ;
409439 if ( owner !== undefined ) {
440+ // [ADR-0146 D3 merge question 12] The per-package pair an earlier
441+ // version created for a member of this App holds the namespace: it
442+ // stays served as it is, and the App's pair waits for it to be
443+ // re-identified as the App's pair — a resource-state conflict inside
444+ // the App, never the `422` kept for a package outside it.
445+ const ownerBase = customizationBasePackageId ( owner ) ;
446+ const installed = installedManifestsOfEngine ( engine ) ;
447+ if ( ownerBase !== null && customizationPackageId ( ownerBase ) . ok && appOf ( ownerBase , installed ) === appId ) {
448+ return refusal ( 409 , `Cannot ensure '${ pairId } ' yet: its namespace '${ namespace . value } ' is held by `
449+ + `'${ owner } ', the paired customization package an earlier version created for '${ ownerBase } ', a `
450+ + `package of the App '${ appId } '. That package stays installed and its customizations stay served; `
451+ + `'${ pairId } ' is created once '${ owner } ' is re-identified as the App's pair, and is not created `
452+ + 'beside it (ADR-0146 D3).' ) ;
453+ }
410454 return refusal ( 422 , `Cannot ensure '${ pairId } ': its namespace '${ namespace . value } ' is already owned by package `
411- + `'${ owner } '. The pair's namespace is the base 's followed by '_custom' and has no alternative; uninstall `
455+ + `'${ owner } '. The pair's namespace is the App 's followed by '_custom' and has no alternative; uninstall `
412456 + `'${ owner } ' or move it off that namespace, then ensure the pair again (ADR-0146 D3).` ,
413457 'NAMESPACE_CONFLICT' ) ;
414458 }
@@ -425,38 +469,40 @@ async function ensurePairOnce(
425469 // Every refusal the registry decides, read once ({@link customizationPairEnsureRefusal}).
426470 const refused = customizationPairEnsureRefusal ( this . engine , registry , baseId , pairId ) ;
427471 if ( refused ) throw refused ;
428- const base = registry . getPackage ( baseId ) ! ;
429- const namespace = customizationNamespace ( base . manifest ?. namespace ) ;
472+ // The pair is its App's (the refusal above read the same App).
473+ const appId = customizationBasePackageId ( pairId ) ?? baseId ;
474+ const app = registry . getPackage ( appId ) ! ;
475+ const namespace = customizationNamespace ( app . manifest ?. namespace ) ;
430476 if ( ! namespace . ok ) throw new Error ( 'unreachable: the namespace was judged above' ) ;
431477
432478 // Idempotent: an existing pair is returned unchanged — ⛔ never adopted,
433479 // never rewritten (its refusal is above).
434480 const existing = registry . getPackage ( pairId ) ;
435481 if ( existing ) {
436482 return {
437- basePackageId : baseId ,
483+ basePackageId : appId ,
438484 packageId : pairId ,
439485 namespace : namespace . value ,
440- dependencyRange : declaredRangeOn ( existing . manifest , baseId ) ! ,
486+ dependencyRange : declaredRangeOn ( existing . manifest , appId ) ! ,
441487 created : false ,
442488 writable : isWritablePackage ( this . engine , pairId ) ,
443489 } ;
444490 }
445491
446- const range = customizationDependencyRange ( base . manifest ?. version ) ;
492+ const range = customizationDependencyRange ( app . manifest ?. version ) ;
447493 if ( ! range . ok ) throw new Error ( 'unreachable: the version was judged above' ) ;
448- const baseName = typeof base . manifest ?. name === 'string' && base . manifest . name !== '' ? base . manifest . name : baseId ;
494+ const appName = typeof app . manifest ?. name === 'string' && app . manifest . name !== '' ? app . manifest . name : appId ;
449495 const manifest = {
450496 id : pairId ,
451- name : `${ baseName } (custom)` ,
497+ name : `${ appName } (custom)` ,
452498 version : CREATED_PAIR_VERSION ,
453499 type : CREATED_PAIR_TYPE ,
454500 namespace : namespace . value ,
455- dependencies : { [ baseId ] : range . value } ,
501+ dependencies : { [ appId ] : range . value } ,
456502 } ;
457503 await this . installPackage ( { manifest } as InstallPackageRequest ) ;
458504 return {
459- basePackageId : baseId ,
505+ basePackageId : appId ,
460506 packageId : pairId ,
461507 namespace : namespace . value ,
462508 dependencyRange : range . value ,
0 commit comments