diff --git a/.changeset/32820-reenable-heal-upgrade-guide-entry.md b/.changeset/32820-reenable-heal-upgrade-guide-entry.md new file mode 100644 index 00000000000..ecdbe74ac2f --- /dev/null +++ b/.changeset/32820-reenable-heal-upgrade-guide-entry.md @@ -0,0 +1,10 @@ +--- +'@objectstack/spec': patch +--- + +docs(spec): the protocol upgrade guide names what a holder's registry item answers after a second holder of an overlay name is enabled again: its own item, not the package-less row the hydrator laid in while the second was disabled (ADR-0150 D4) + +Clause-②: no +Metadata-protocol: none + +The semantic entry `package-less-overlay-row-holder-slot-on-reenable` (`packages/spec/src/migrations/entries/semantic/18.package-less-overlay-row-holder-slot-on-reenable.ts`) registers the narrowing `.changeset/32820-reenable-heals-holder-slot.md` declares (ADR-0087, `adr-0087: registered`): no automatic rewrite, because which holder a package-less row customizes is a judgement only its author can make. It adds no key and no export. diff --git a/.changeset/32820-reenable-heals-holder-slot.md b/.changeset/32820-reenable-heals-holder-slot.md new file mode 100644 index 00000000000..b6113ece7a1 --- /dev/null +++ b/.changeset/32820-reenable-heals-holder-slot.md @@ -0,0 +1,25 @@ +--- +'@objectstack/metadata-protocol': minor +--- + +fix(metadata-protocol)!: enabling a second holder of an overlay name takes back the package-less row the read-side hydrator laid into the first holder's registry slot while the second was disabled (ADR-0150 D4) + +Clause-②: no (narrowing) — a served answer that guessed a holder goes away: after the enable, a holder's registry item, its scoped list and its registry list answer the holder's own item where they answered the environment's package-less row as the holder's own. Nothing that was refused is now served, and no export is added (the heal is module-internal). +Metadata-protocol: narrowed +Skills-docs: none — `skills/**` and `content/docs/**` teach no registry read of a holder's item after a disable and enable; the prescription ships in the FROM → TO below. + + + +**BREAKING** for a registry reader of a holder's item of an overlay-type name (`email_template`, `dashboard`, `report`) that another installed package also holds, when a package-less `sys_metadata` row of the name is stored: `getPackageScopedItem(type, name, holder)` (what a send-time read takes), the list scoped to the holder (`GET /meta/:type?package=`) and the registry list scoped to it. + +While one of the two holders is disabled, the other is the one served holder (#32183), so the package-less row is its customization: the read-side hydrator (an unscoped list read, the boot load) lays the row into that holder's slot under its envelope. Enabling the disabled holder ran no hydration, so the slot kept the row, and until the next unscoped list read the holder's registry item and its scoped list answered the environment's row as the holder's own, and its registry list listed both. The protocol's exact read (`?package=`) already refused it `409` naming both holders. + +The enable now builds the entry again, as the hydrator builds it for the holders served then: with two served, no holder's envelope, so each holder's slot answers its own item, as when the hydrator ran with both served. It runs inside the enable, so every enable door (`PATCH /packages/:id/enable`, an install's `enableOnInstall`) answers after it. An entry another write replaced after the hydration is that write's, and is not touched. + +Not changed: the disabled period itself (the one served holder's slot serves the row, as #32183 rules), the exact reads, and a package installed after the hydration that ships the name (the slot keeps the row until the next unscoped hydration, as before: no registry notice reaches the protocol for a new holder). + +No first-party composition has two packages holding one overlay name with a package-less row, so no answer changes in this repository. + +The fix: + +- FROM a package-less overlay row of a name two packages hold, read as one holder's own through the registry after a disable and enable, TO the declared override of that holder in its paired `.custom` package (`?package=.custom` with `overrides: { package: , basedOn }`), saved after deleting the package-less row (a package-less delete removes the one bound row there is, so after the save it would remove the override). Before that delete, keep the package-less row's stored body and clear any row of the name bound to a package, as `package-less-row-exact-address-by-type-class` prescribes: the delete hard-deletes the row, and with a bound row present it addresses that row instead. diff --git a/packages/metadata-protocol/src/protocol-registry-hydration.ts b/packages/metadata-protocol/src/protocol-registry-hydration.ts index 3d568f9ac16..71bac53651d 100644 --- a/packages/metadata-protocol/src/protocol-registry-hydration.ts +++ b/packages/metadata-protocol/src/protocol-registry-hydration.ts @@ -170,6 +170,102 @@ function soleHolderOf(registry: { packageScopedHoldersOf?(type: string, name: st return holders[0]; } +/** + * [#32820] A package-less row of a name ONE package held when the read-side + * hydrator laid it in, under that holder's artifact envelope + * ({@link soleHolderOf}): what the registry entry was built from, so it can be + * built again when the holder set changes. + */ +interface SoleHolderEnvelope { + readonly host: Pick; + readonly type: string; + readonly canonicalType: string; + readonly name: string; + readonly member: string | undefined; + readonly data: unknown; + /** The holder whose envelope the entry wears. */ + readonly holder: string; + /** The entry as registered: the heal touches the slot only while it still holds this object. */ + readonly registered: unknown; +} + +/** The engine's registry, as the hydrator reads it (its registration is the hydrator's own call, untyped there too). */ +type EnvelopeRegistry = { + readonly [member: string]: any; + getItem?(type: string, name: string, currentPackageId?: string, member?: string): unknown; + packageScopedHoldersOf?(type: string, name: string): readonly string[] | undefined; + onPackageEnabled?(listener: (packageId: string) => void): () => void; +}; + +/** + * [#32820] The overlay types whose package-less row the heal rebuilds: the + * three the narrowing declares (its changeset and ADR-0087 entry + * `package-less-overlay-row-holder-slot-on-reenable`). A package-less + * `permission` row also wears its sole holder's envelope, but two holders of a + * permission set are refused by the registry today; when that rule opens, the + * heal widens to it only in a change that declares it (contract review s1). + */ +const HEALED_OVERLAY_TYPES: ReadonlySet = new Set(['email_template', 'dashboard', 'report']); + +/** Per registry, the {@link SoleHolderEnvelope} of each bare-slot key (`type`, `name`, member). */ +const soleHolderEnvelopes = new WeakMap>(); + +const envelopeKey = (type: string, name: string, member: string | undefined) => `${type}\u0000${name}\u0000${member ?? ''}`; + +/** + * [#32820, ADR-0150 D4] Record (or, with no `holder`, forget) the envelope a + * package-less row's registry entry wears. The first record for a registry + * subscribes {@link healSoleHolderEnvelopes} to its package enables. + */ +function recordSoleHolderEnvelope(registry: EnvelopeRegistry, key: string, envelope: SoleHolderEnvelope | undefined): void { + let records = soleHolderEnvelopes.get(registry); + if (envelope === undefined) { + records?.delete(key); + return; + } + if (records === undefined) { + if (typeof registry.onPackageEnabled !== 'function') return; + const created = new Map(); + records = created; + soleHolderEnvelopes.set(registry, created); + registry.onPackageEnabled(() => healSoleHolderEnvelopes(registry, created)); + } + records.set(key, envelope); +} + +/** + * [#32820, ADR-0150 D4] A package-less overlay row the read-side hydrator laid + * into a holder's slot while that holder alone was served (another holder of + * the name disabled) wears that holder's envelope, so the registry reads it + * as that holder's own item (`getPackageScopedItem`, the scoped list, the + * execution list). An enable that serves a second holder makes it a + * customization no holder can claim: the entry is built again as the + * hydrator builds it for the holders served now (none's envelope with two), + * so each holder's slot answers its own item again, as with no hydration in + * the disabled period. Synchronous, inside the enable, so every enable door + * (`PATCH /packages/:id/enable`, an install's `enableOnInstall`) answers + * after it. + * + * Only an entry the slot still holds as registered: a later write, delete or + * hydration replaced it and owns the slot. + */ +function healSoleHolderEnvelopes(registry: EnvelopeRegistry, records: Map): void { + for (const [key, record] of [...records]) { + if (typeof registry.getItem !== 'function' + || registry.getItem(record.type, record.name, undefined, record.member) !== record.registered) { + records.delete(key); + continue; + } + const holder = soleHolderOf(registry, record.canonicalType, record.name); + if (holder === record.holder) continue; + const artifact = holder === null ? undefined : record.host.lookupArtifactItem(record.type, record.name, holder ?? undefined, record.member); + const registered = mergeArtifactProtection(record.type, stateTenantAuthorship(withRowBinding(record.data, undefined)), artifact); + registry.registerItem(record.type, registered, 'name'); + if (typeof holder === 'string' && artifact) records.set(key, { ...record, holder, registered }); + else records.delete(key); + } +} + export class ProtocolRegistryHydrationFacet { /** * Mirror an object-type overlay write into the in-memory engine @@ -676,6 +772,15 @@ export class ProtocolRegistryHydrationFacet { if (!incomingTakesStoredFlowSlot(held, registered, shippers, installedManifestsOf(registry))) return true; } registry.registerItem(type, registered, 'name' as any); + // [#32820] A package-less row wearing the envelope of the one holder + // served now is recorded, so an enable serving a second holder builds + // it again ({@link healSoleHolderEnvelopes}); any other registration + // of the key forgets it. + const member = bundleMemberOf(type, data); + recordSoleHolderEnvelope(registry, envelopeKey(type, rowName, member), + !options.packageId && typeof envelopePackage === 'string' && envelope && HEALED_OVERLAY_TYPES.has(canonicalType) + ? { host, type, canonicalType, name: rowName, member, data, holder: envelopePackage, registered } + : undefined); await host.hydrateExpandedViewItems(type, data, options); return true; } diff --git a/packages/metadata-protocol/src/protocol.lookup-artifact-item-call-sites.test.ts b/packages/metadata-protocol/src/protocol.lookup-artifact-item-call-sites.test.ts index 3441df84270..018d027a764 100644 --- a/packages/metadata-protocol/src/protocol.lookup-artifact-item-call-sites.test.ts +++ b/packages/metadata-protocol/src/protocol.lookup-artifact-item-call-sites.test.ts @@ -166,6 +166,8 @@ const SITES: ReadonlyArray { @@ -2073,6 +2070,129 @@ describe('[#32568] a package-less row at a holder\'s exact address: declined for }); }); +/** + * [#32820, ADR-0150 D4] A package-less overlay row the read-side hydrator lays + * in while one of two holders is disabled wears the served holder's envelope: + * that holder's slot answers it as its own. Enabling the other holder builds + * the entry again for the holders served now, so each holder's slot, the list + * scoped to it and the registry's execution list answer the holder's own item, + * as when the hydrator ran with both served. + */ +describe('[#32820] re-enabling a holder: the package-less overlay row the hydrator laid into the other holder\'s slot leaves it', () => { + const body = (type: string, label: string) => (type === 'email_template' + ? { name: 'welcome', label, locale: 'en-US', subject: 's', bodyHtml: '

x

' } + : { name: 'welcome', label }); + const holderOwn = (type: string, id: string) => registry.registerItem(type, body(type, `${id.split('.').pop()} own`), 'name' as never, id); + const storeEnvRow = (type: string) => rows.push({ + id: `${type}_env`, type, name: 'welcome', state: 'active', package_id: null, bundle_member: null, + metadata: JSON.stringify(body(type, 'env row')), + }); + /** The three registry readers of a holder's item, and the slot's stamp. */ + const readers = async (type: string, at: string) => { + const slot = registry.getPackageScopedItem(type, 'welcome', at) as { label?: string; _packageId?: string } | undefined; + const scoped = ((await protocol.getMetaItems({ type, packageId: at } as never)) as { items?: Array<{ name?: string; label?: string }> }) + .items?.filter((i) => i.name === 'welcome').map((i) => i.label); + const execution = (registry.listItems(type, at) as Array<{ name?: string; label?: string }>) + .filter((i) => i.name === 'welcome').map((i) => i.label); + return { slot: slot?.label, stamp: slot?._packageId, scoped, execution }; + }; + const own = (id: string) => { + const label = `${id.split('.').pop()} own`; + return { slot: label, stamp: id, scoped: [label], execution: [label] }; + }; + + for (const type of ['email_template', 'dashboard', 'report']) { + for (const order of [[A, B], [B, A]]) { + it(`${type}, registered ${order.map((id) => id.split('.').pop()).join(' then ')}: B disabled while the hydrator runs, then enabled: A's slot, A's scoped list and A's execution list answer A's own (BASE: the environment's row, listed beside A's own)`, async () => { + for (const id of order) holderOwn(type, id); + storeEnvRow(type); + registry.disablePackage(B); + await protocol.getMetaItems({ type } as never); + // The disabled period: A is the one served holder, so the row is its customization. + expect((await readers(type, A)).slot, 'B disabled').toBe('env row'); + registry.enablePackage(B); + expect(await readers(type, A), 'B enabled').toEqual(own(A)); + expect(await readers(type, B), 'B enabled').toEqual(own(B)); + // The protocol's exact read is unchanged: refused naming both. + const err = await refusalOf(() => protocol.getMetaItem({ type, name: 'welcome', packageId: A } as never)); + expect([err.status, err.refusal.reason]).toEqual([409, 'ambiguous-in-closure']); + }); + } + } + + it('CONTROL — the slot after the heal is the one the hydrator builds with both holders served', async () => { + holderOwn('email_template', A); + holderOwn('email_template', B); + storeEnvRow('email_template'); + await protocol.getMetaItems({ type: 'email_template' } as never); + const hydrated = await readers('email_template', A); + expect(hydrated).toEqual(own(A)); + const bare = registry.getItem('email_template', 'welcome', undefined, 'en-US'); + registry.disablePackage(B); + await protocol.getMetaItems({ type: 'email_template' } as never); + registry.enablePackage(B); + expect(await readers('email_template', A)).toEqual(hydrated); + expect(registry.getItem('email_template', 'welcome', undefined, 'en-US')).toEqual(bare); + }); + + it('CONTROL — an enable that leaves A the one served holder keeps the row as A\'s customization', async () => { + holderOwn('email_template', A); + storeEnvRow('email_template'); + registry.disablePackage(C); + await protocol.getMetaItems({ type: 'email_template' } as never); + const before = await readers('email_template', A); + expect([before.slot, before.stamp]).toEqual(['env row', A]); + registry.enablePackage(C); + expect(await readers('email_template', A)).toEqual(before); + }); + + it('CONTROL — an entry written over the slot after the hydration is the slot\'s: the enable does not touch it', async () => { + holderOwn('email_template', A); + holderOwn('email_template', B); + storeEnvRow('email_template'); + registry.disablePackage(B); + await protocol.getMetaItems({ type: 'email_template' } as never); + const written = { ...body('email_template', 'written later'), _packageId: A }; + registry.registerItem('email_template', written, 'name' as never); + registry.enablePackage(B); + expect(registry.getItem('email_template', 'welcome', undefined, 'en-US')).toBe(written); + }); + + it('the holder set moves from A alone to B alone, then to both: the entry is built again under B\'s envelope, then under none (review s2)', async () => { + holderOwn('email_template', A); + holderOwn('email_template', B); + storeEnvRow('email_template'); + registry.disablePackage(B); + await protocol.getMetaItems({ type: 'email_template' } as never); + const slotAt = (at: string) => { + const slot = registry.getPackageScopedItem('email_template', 'welcome', at) as { label?: string; _packageId?: string } | undefined; + return [slot?.label, slot?._packageId]; + }; + expect(slotAt(A), 'B disabled, hydrated: A alone served').toEqual(['env row', A]); + registry.disablePackage(A); + registry.enablePackage(B); + // B is now the one served holder: the row is its customization, recorded again under its envelope. + expect(slotAt(B), 'A disabled, B enabled: B alone served').toEqual(['env row', B]); + registry.enablePackage(A); + // Both served: the second heal builds it under no holder's envelope. + expect(await readers('email_template', A), 'both enabled').toEqual(own(A)); + expect(await readers('email_template', B), 'both enabled').toEqual(own(B)); + }); + + it('a package-less permission row is not recorded: an enable leaves the hydrated slot as the hydrator laid it (review s1; the heal moves only the three declared overlay types)', async () => { + admitSecondPermissionHolder(); + holderOwn('permission', A); + holderOwn('permission', B); + storeEnvRow('permission'); + registry.disablePackage(B); + await protocol.getMetaItems({ type: 'permission' } as never); + const hydrated = registry.getItem('permission', 'welcome') as { label?: string } | undefined; + expect(hydrated?.label, 'B disabled, hydrated: the row in A\'s slot').toBe('env row'); + registry.enablePackage(B); + expect(registry.getItem('permission', 'welcome')).toBe(hydrated); + }); +}); + /** * [#32183, ruling (c)] An App's pair (`.custom`) is the pair of every * module of the App, so two modules holding one name are both its bases. A diff --git a/packages/spec/src/migrations/entries/semantic/18.package-less-overlay-row-holder-slot-on-reenable.ts b/packages/spec/src/migrations/entries/semantic/18.package-less-overlay-row-holder-slot-on-reenable.ts new file mode 100644 index 00000000000..794bddf3ab8 --- /dev/null +++ b/packages/spec/src/migrations/entries/semantic/18.package-less-overlay-row-holder-slot-on-reenable.ts @@ -0,0 +1,47 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +import type { SemanticMigration } from '../../types.js'; + +// ADR-0150 D4 at the registry: a package-less row of an overlay type is the +// customization of the one served holder of its name, and of none when two +// are served. The read-side hydrator built the registry entry for the holders +// served when it ran, and an enable that served a second holder left the entry +// wearing the first holder's envelope. A narrowing of answers, not of any key: +// no schema key moves and no stored row changes shape, so no tombstone and no +// D2 conversion. No relevance question: the moved answer turns on what two +// installed packages hold and on a disable in between, which no question over +// one stack can name. +export const entry: SemanticMigration = { + id: 'package-less-overlay-row-holder-slot-on-reenable', + surface: + 'The registry\'s item of a holder of an overlay-type name (email_template, dashboard, report) that a second ' + + 'installed package also holds, when a package-less row of the name is stored and the read-side hydrator ran ' + + '(an unscoped list read, the boot load) while the other holder was disabled: the holder\'s package-scoped ' + + 'registry item, its scoped list (GET /api/v1/meta/TYPE?package=ID) and the registry list scoped to it, after ' + + 'the other holder is enabled again', + replacement: + 'First keep the package-less row\'s stored body and clear any row of the name bound to a package, as the entry ' + + 'package-less-row-exact-address-by-type-class prescribes: the delete below hard-deletes the row, and with a ' + + 'bound row present it addresses that row instead. Then delete the package-less row (DELETE ' + + '/api/v1/meta/TYPE/NAME, naming no package; ?state=draft for a draft), then save the customization ' + + 'as the declared override of the holder it customizes: in the holder\'s paired .custom package ' + + '(PUT /api/v1/meta/TYPE/NAME?package=ID.custom with overrides: { package: ID, basedOn: VERSION }). Delete ' + + 'first: a package-less delete of the name removes the one bound row there is, so after the save it would ' + + 'remove the override', + reason: + 'A package-less row of an overlay type customizes one holder only while that holder is the one served holder ' + + 'of the name (ADR-0150 D4). With one of two holders disabled, the read-side hydrator laid the row into the ' + + 'served holder\'s registry slot under that holder\'s envelope, and enabling the other holder left it there, ' + + 'so until the next unscoped hydration the holder\'s registry item, its scoped list and its registry list ' + + 'answered the environment\'s row as the holder\'s own (the registry list carried both). The protocol\'s exact ' + + 'read already refused it 409 naming both holders. The enable now builds the entry again for the holders ' + + 'served, so each holder answers its own item, as when the hydrator ran with both served. Nothing is ' + + 'rewritten. Measured when the change landed: no first-party composition has two packages holding one ' + + 'overlay name with a package-less row.', + acceptanceCriteria: + 'For every package-less email_template, dashboard or report row of a name two installed packages hold, no ' + + 'package-less row of the name remains; the change lives in the declared override of the holder it customizes, ' + + 'in that holder\'s .custom package, and GET /api/v1/meta/TYPE/NAME?package=ID answers the override while ' + + '?package= naming the other holder answers that holder\'s own item, whether or not a holder was disabled and ' + + 'enabled in between.', +};