Skip to content

Commit 455c1f1

Browse files
committed
fix(metadata-protocol): serve a pair-added bundle member on the base's address, keep a dependent's under its own package
The env-wide list narrowed a name one code package ships at name grain, so the pair's own added email template locale was read by name but never listed or executed, and the by-name reads on the base's address fell to the registry's cross-package tier and served a dependent's member under the base's name. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5m2Ryfx2Wubuz716mw
1 parent c9a9981 commit 455c1f1

3 files changed

Lines changed: 286 additions & 104 deletions

File tree

‎packages/metadata-protocol/src/protocol-environment-rows.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
* the code packages that ship the name. Not a moved body: the rule the two
1717
* by-name reads share. [#28409] …and `shippingPackageIds` /
1818
* `soleShippingPackage`, the code packages that ship a name, read one way by
19-
* those reads and by the env-wide list.
19+
* those reads and by the env-wide list. [#28802] …and
20+
* `baseAddressServesMember`, the member grain both apply to a bundle.
2021
*
2122
* A MOVE, not a redesign. Each function below is the body of the
2223
* `ObjectStackProtocolImplementation` method of the same name as it stood,
@@ -43,6 +44,7 @@
4344
* not move.
4445
*/
4546

47+
import { customizationPackageId } from '@objectstack/spec/kernel';
4648
import { PLURAL_TO_SINGULAR, SINGULAR_TO_PLURAL } from '@objectstack/spec/shared';
4749
import type { StoredOverlayRow } from './item-lock.js';
4850
import { overlayMemberWhere } from './migrations/overlay-index.js';
@@ -255,3 +257,24 @@ export function soleShippingPackage(shipped: readonly unknown[]): string | undef
255257
const shippers = shippingPackageIds(shipped);
256258
return shippers.size === 1 ? [...shippers][0] : undefined;
257259
}
260+
261+
/**
262+
* [#28802, ADR-0146 D2, #28390 ruling A] Whether the address of `base`, a
263+
* code package that ships a bundle's name (a type in
264+
* `ITEM_KEY_DISCRIMINATORS`), serves a member of that name bound to `bound`.
265+
* Member grain: the base's own members and a member bound to no package, and
266+
* the pair's own ADDED members, a member of the name the base does not ship
267+
* (`baseShipsMember` false), as the pair is the customer layer the base's
268+
* address serves. A member another package binds under the base's name (a
269+
* dependent's addition) is that package's own item, served under its own
270+
* address only: "a dependent adds items under its own names". So is a pair's
271+
* undeclared row at a member the base ships (refused at the write gate since
272+
* PR #28777). A declared override is served as the base's item by the
273+
* served-row resolution and is never asked about here.
274+
*/
275+
export function baseAddressServesMember(base: string, bound: string | undefined, baseShipsMember: boolean): boolean {
276+
if (bound === undefined || bound === base) return true;
277+
if (baseShipsMember) return false;
278+
const pair = customizationPackageId(base);
279+
return pair.ok && pair.value === bound;
280+
}

‎packages/metadata-protocol/src/protocol.ts‎

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ import { refuseUnmintableMetaType } from './protocol-unmintable-meta-type.js';
471471
// [#25467 cut 33] The deployment's SDUI component manifest, and the load-time page `requires` report against it.
472472
import { reportPageRequiresAbsentAtLoad, resolveSduiManifest } from './protocol-sdui-manifest.js';
473473
// [#25467 cut 35] The environment's own `sys_metadata` rows: the family read, the one-row read, the served row and the package-less address.
474-
import { findEnvironmentRow, findEnvironmentRows, findServedOverlayRow, packagelessAddress, packagelessContentPackage, soleShippingPackage } from './protocol-environment-rows.js';
474+
import { baseAddressServesMember, findEnvironmentRow, findEnvironmentRows, findServedOverlayRow, packagelessAddress, packagelessContentPackage, shippingPackageIds, soleShippingPackage } from './protocol-environment-rows.js';
475475
// [#25467 cut 35] The stored rows of a collection, folded over the registered ones.
476476
import { foldStoredCollection } from './protocol-stored-collection-fold.js';
477477
// [#25467 cut 35] The environment-wide package uninstall verb.
@@ -10492,6 +10492,24 @@ export class ObjectStackProtocolImplementation implements
1049210492
// MetadataService not available or doesn't support this type
1049310493
}
1049410494

10495+
// [#28802, #28390 ruling A] …except, for a bundle the package ships
10496+
// (`ITEM_KEY_DISCRIMINATORS`), its pair's row at a member the package
10497+
// does not ship: the pair's own added member, which the package's
10498+
// address serves beside the package's members
10499+
// ({@link baseServesBundleMember}), as the env-wide list below does.
10500+
if (unseated && servedOverride && packageId && ITEM_KEY_DISCRIMINATORS[request.type] !== undefined) {
10501+
const pairOfList = customizationPackageId(packageId);
10502+
const typeShipping = this.shippingPackagesOf(request.type);
10503+
for (const it of items as any[]) {
10504+
if (!(it && typeof it === 'object' && unseated.has(it))) continue;
10505+
if (!pairOfList.ok || boundPackageOf(it._packageId) !== pairOfList.value || typeof it.name !== 'string') continue;
10506+
if (this.shipsBundleName(request.type, it.name, packageId, typeShipping)
10507+
&& this.baseServesBundleMember(request.type, it.name, packageId, it, typeShipping)) {
10508+
unseated.delete(it);
10509+
}
10510+
}
10511+
}
10512+
1049510513
// [#21817] A stand-in no layer seated is a package-less row of an item
1049610514
// the package does not ship: it is not in the package's list.
1049710515
if (unseated) {
@@ -10524,8 +10542,16 @@ export class ObjectStackProtocolImplementation implements
1052410542
// every item of a name no package or several packages ship (ADR-0048
1052510543
// §3.3). The `package` type is never narrowed: its items are the
1052610544
// packages themselves.
10545+
//
10546+
// [#28802, #28390 ruling A] A bundle's name (`ITEM_KEY_DISCRIMINATORS`)
10547+
// is narrowed at MEMBER grain: the sole shipper's list also keeps its
10548+
// pair's own added members, a member the base does not ship
10549+
// ({@link baseServesBundleMember}), so a locale the customer adds in
10550+
// `.custom` is listed and executed (the email loader reads this list).
10551+
// A dependent's member under the name is still dropped here.
1052710552
if (servedOverride && !packageId && request.type !== 'package') {
1052810553
const typeShipping = this.shippingPackagesOf(request.type);
10554+
const isBundle = ITEM_KEY_DISCRIMINATORS[request.type] !== undefined;
1052910555
const soleShipperOf = new Map<string, string | undefined>();
1053010556
items = (items as any[]).filter((it) => {
1053110557
const itemName = it?.name;
@@ -10535,7 +10561,8 @@ export class ObjectStackProtocolImplementation implements
1053510561
soleShipperOf.set(itemName, soleShippingPackage(this.shippedArtifactsOf(request.type, itemName, typeShipping)));
1053610562
}
1053710563
const soleShipper = soleShipperOf.get(itemName);
10538-
return soleShipper === undefined || soleShipper === bound;
10564+
if (soleShipper === undefined || soleShipper === bound) return true;
10565+
return isBundle && this.baseServesBundleMember(request.type, itemName, soleShipper, it, typeShipping);
1053910566
});
1054010567
}
1054110568

@@ -11486,6 +11513,18 @@ export class ObjectStackProtocolImplementation implements
1148611513
...(readState === 'active' ? { servedOverride: true } : {}),
1148711514
}, activeRows);
1148811515
let record = served?.row;
11516+
// [#28802, #28390 ruling A] A member of a bundle the address's
11517+
// package ships and does not ship at that member: the pair's own
11518+
// added member is the address's, served from the pair's row under
11519+
// the pair's binding (a dependent's member never is).
11520+
if (!record && readState === 'active' && wantedMember !== undefined && typeof contentPackageId === 'string') {
11521+
const pairOfAddition = this.pairOfAddedMember(request.type, request.name, contentPackageId, wantedMember);
11522+
if (pairOfAddition !== undefined) {
11523+
record = (await this.findServedOverlayRow({
11524+
type: request.type, name: request.name, state: readState, packageId: pairOfAddition, ...memberOption(member),
11525+
}, activeRows))?.row;
11526+
}
11527+
}
1148911528
// [#23715] Two or more packages and no package-unbound draft: the
1149011529
// best-effort first match ADR-0048 §3.3 states, as before, served
1149111530
// with `version: null` (no row at the address the save naming none
@@ -11711,6 +11750,15 @@ export class ObjectStackProtocolImplementation implements
1171111750
if (this.isStoredFlowEntryOfShippedName(request.type, item)) {
1171211751
item = this.lookupArtifactItem(request.type, request.name, request.packageId);
1171311752
}
11753+
// [#28802, #28390 ruling A] A bundle member on a base's address
11754+
// (the package named, else the one whose content a read naming
11755+
// none serves) is one that address serves: the registry's last
11756+
// tier, the first composite match across packages (ADR-0048 §3.3),
11757+
// no longer answers a dependent's member under the base's name.
11758+
const memberAddress = request.packageId || (typeof contentPackageId === 'string' ? contentPackageId : undefined);
11759+
if (item !== undefined && wantedMember !== undefined && memberAddress !== undefined) {
11760+
item = this.registryMemberServedAt(request.type, request.name, memberAddress, item, member);
11761+
}
1171411762
}
1171511763

1171611764
// [#5840] The MetadataService half of the #5532 rule, and the last
@@ -17325,6 +17373,59 @@ export class ObjectStackProtocolImplementation implements
1732517373
return shippedArtifactsOf.call(host, type, name, shipping, member);
1732617374
}
1732717375

17376+
/** [#28802] Whether `base` is a code package that ships the bundle's name `name`, any member. */
17377+
private shipsBundleName(type: string, name: string, base: string, shipping: ShippingPackages): boolean {
17378+
return shippingPackageIds(this.shippedArtifactsOf(type, name, shipping)).has(base);
17379+
}
17380+
17381+
/**
17382+
* [#28802, ADR-0146 D2, #28390 ruling A] Whether the address of `base`, a
17383+
* code package that ships the bundle's name `name` (a type in
17384+
* `ITEM_KEY_DISCRIMINATORS`), serves `item`, a member of that name: by
17385+
* {@link baseAddressServesMember}, given whether `base` ships the member
17386+
* `item` is. The one member-grain answer the env-wide list, the list
17387+
* scoped to `base` and the by-name reads give.
17388+
*/
17389+
private baseServesBundleMember(type: string, name: string, base: string, item: unknown, shipping: ShippingPackages): boolean {
17390+
const member = itemDiscriminator(canonicalMetaType(type), item);
17391+
const baseShipsMember = shippingPackageIds(this.shippedArtifactsOf(type, name, shipping, member)).has(base);
17392+
return baseAddressServesMember(base, boundPackageOf((item as { _packageId?: unknown } | null | undefined)?._packageId), baseShipsMember);
17393+
}
17394+
17395+
/**
17396+
* [#28802, #28390 ruling A] The pair of `base` when the bundle member
17397+
* `member` (its discriminator value) of the name `name` is one only the
17398+
* pair can add on `base`'s address: `base` ships the name and not that
17399+
* member. Else `undefined`, and the by-name read serves as before.
17400+
*/
17401+
private pairOfAddedMember(type: string, name: string, base: string, member: string): string | undefined {
17402+
const shipping = this.shippingPackagesOf(type);
17403+
if (!this.shipsBundleName(type, name, base, shipping)) return undefined;
17404+
if (shippingPackageIds(this.shippedArtifactsOf(type, name, shipping, member)).has(base)) return undefined;
17405+
const pair = customizationPackageId(base);
17406+
return pair.ok ? pair.value : undefined;
17407+
}
17408+
17409+
/**
17410+
* [#28802] The registry's answer `item` for a bundle member read on the
17411+
* address of `base`, when that address serves it
17412+
* ({@link baseServesBundleMember}); else, for a read that named no
17413+
* member, the base's own member its artifact answers canonical-first,
17414+
* and otherwise nothing. An address that ships no member of the name is
17415+
* no base of it: `item` as before.
17416+
*/
17417+
private registryMemberServedAt(type: string, name: string, base: string, item: unknown, member: string | undefined): unknown {
17418+
const shipping = this.shippingPackagesOf(type);
17419+
if (!this.shipsBundleName(type, name, base, shipping)) return item;
17420+
if (this.baseServesBundleMember(type, name, base, item, shipping)) return item;
17421+
if (member !== undefined) return undefined;
17422+
const own = this.shippedArtifactsOf(type, name, shipping)
17423+
.find((artifact) => (artifact as { _packageId?: unknown } | null)?._packageId === base);
17424+
if (own === undefined) return undefined;
17425+
const candidate = this.engine.registry.getItem(type, name, base, itemDiscriminator(canonicalMetaType(type), own));
17426+
return candidate !== undefined && this.baseServesBundleMember(type, name, base, candidate, shipping) ? candidate : undefined;
17427+
}
17428+
1732817429
/**
1732917430
* [#21803] The packages that can ship an item of `type`, read off the
1733017431
* registry's own listings:

0 commit comments

Comments
 (0)