diff --git a/.changeset/33832-heal-no-picked-holder.md b/.changeset/33832-heal-no-picked-holder.md new file mode 100644 index 00000000000..db9adbc5668 --- /dev/null +++ b/.changeset/33832-heal-no-picked-holder.md @@ -0,0 +1,8 @@ +--- +'@objectstack/metadata-protocol': patch +--- + +Clause-②: no +Metadata-protocol: none + +The registry heal after a metadata delete no longer registers a picked holder's item in the bare slot. A `dashboard`, `report` or other type addressed by `(package, name)` that the metadata service stores by name alone keeps only the item registered last, and answers it to a read naming any package. When two or more packages held the name in the registry, the heal's baseline read took that answer and registered it in the bare slot, so every bare registry lookup answered the last-registered holder's item and the registry listed it twice (ADR-0150 D3: a bare read never picks between holders). The heal now reads no baseline there: the bare slot stays empty and a read naming no package answers the refusal naming every holder, as the boot leaves it. A name one package holds heals as before. diff --git a/packages/metadata-protocol/src/protocol-registry-hydration.ts b/packages/metadata-protocol/src/protocol-registry-hydration.ts index c47e94f74b6..e31d31d79f2 100644 --- a/packages/metadata-protocol/src/protocol-registry-hydration.ts +++ b/packages/metadata-protocol/src/protocol-registry-hydration.ts @@ -141,6 +141,18 @@ type ServiceRead = { data: unknown; degraded: boolean; errors: string[] }; * Asked of the service rather than the registry: tier 1 has already healed * whenever the registry holds the base's composite artifact under a shadow, so * this read is reached exactly where it does not. + * + * [#33832, ADR-0150 D3, epic ruling 6818436831] ⛔ Except where the service + * cannot name a holder. A type addressed by `(package, name)` that the service + * stores by name alone (outside `PACKAGE_SLOTTED_STORE_TYPES`: `dashboard`, + * `report`, `page`, `app`, …) keeps ONE item per name, the one registered + * last, and answers it to a read naming any package or none. Where the + * registry holds the name for two or more packages, that answer is a pick, and + * registered in the bare slot it served the last-registered holder's item to + * every bare registry lookup and listed it twice. So no baseline is read: a + * settled miss, and tier 3 retires only the removed row's own entry. The bare + * slot stays empty, and a read naming no package answers the registry's + * refusal naming every holder, as the hydrator leaves it. */ async function readHealBaseline( host: Pick, @@ -148,6 +160,12 @@ async function readHealBaseline( name: string, packageId: string | undefined, ): Promise { + const canonical = canonicalMetaType(type); + if (isPackageScopedType(canonical) && !PACKAGE_SLOTTED_STORE_TYPES.has(canonical)) { + const registry = (host.engine as { registry?: { packageScopedHoldersOf?(type: string, name: string, callerPackageId?: string | null, options?: { includeDisabledPackages?: boolean }): readonly string[] | undefined } } | undefined)?.registry; + const holders = registry?.packageScopedHoldersOf?.(canonical, name, undefined, { includeDisabledPackages: true }) ?? []; + if (holders.length > 1) return { data: undefined, degraded: false, errors: [] }; + } const own = await host.readItemFromMetadataService(type, name, packageId); if (packageId === undefined || (own.data !== undefined && own.data !== null) || own.degraded) return own; try { diff --git a/packages/objectql/src/protocol-registry-heal-packageless-refusal.test.ts b/packages/objectql/src/protocol-registry-heal-packageless-refusal.test.ts index 1b5fd23c0e0..7ab64629f6d 100644 --- a/packages/objectql/src/protocol-registry-heal-packageless-refusal.test.ts +++ b/packages/objectql/src/protocol-registry-heal-packageless-refusal.test.ts @@ -244,3 +244,94 @@ describe('[#32620, ruling 6818436831] a refused heal of a non-holder\'s row reti expect(lines[0]).toContain('retired from its own package\'s slot'); }); }); + +/** + * [#33832, ADR-0150 D3, epic ruling 6818436831] A `dashboard` or `report` is + * addressed by `(package, name)` but stored in the metadata service by name + * alone, so the service keeps the item registered LAST and answers it to a read + * naming any package. The heal's tier 2 read it as the baseline after the + * delete of a non-holder's row and registered it in the bare slot: a pick + * between the two holders. + * + * The shape a real boot leaves (measured on a kernel booted from a compiled + * two-package artifact through the artifact door and `AppPlugin`, both kernel + * shapes, both orders): each holder in its composite slot, both in the + * service with the last-registered one kept, and the non-holder's stored row + * declined from the bare slot. There, at 45dee177, the delete left the + * last-registered holder's item in the bare slot: every bare registry lookup + * answered it and the registry listed it twice. The protocol's reads did not + * move, because the composite slots answer them. Bare slot: picked holder's + * item → empty. + */ +describe('[#33832, ruling 6818436831] the heal never registers a picked holder of a name the service stores by name alone', () => { + const O = 'com.acme.other'; + const shippedItem = (type: string, id: string) => (type === 'report' + ? { name: NAME, label: `shipped:${id}`, type: 'summary', dataset: 'qa_metrics', values: ['total'], _packageId: id } + : { name: NAME, label: `shipped:${id}`, widgets: [], _packageId: id }); + + async function boot(type: string, order: readonly string[]) { + const registry = new SchemaRegistry({ multiTenant: false }); + registry.logLevel = 'silent' as never; + for (const id of [O, ...order]) registry.installPackage(manifest(id), 'code'); + // Each holder in its composite slot, as the package door registers it. + for (const id of order) registry.registerItem(type as never, shippedItem(type, id), 'name' as never, id); + // The service keeps one item per name for these types: the last one. + const manager = new MetadataManager({ formats: ['json'], loaders: [] }); + for (const id of order) manager.registerInMemory(type as never, NAME, shippedItem(type, id)); + const rows: Array> = [{ + id: 'row_other', type, name: NAME, organization_id: null, package_id: O, state: 'active', checksum: 'sha256:head', + metadata: JSON.stringify({ ...shippedItem(type, O), label: 'row:other', _packageId: undefined }), + }]; + const engine = engineOver(registry, rows); + const protocol: any = new ObjectStackProtocolImplementation(engine as never, () => new Map([['metadata', manager]]) as never, undefined); + vi.spyOn(console, 'warn').mockImplementation(() => {}); + const logged = vi.spyOn(console, 'error').mockImplementation(() => {}); + await protocol.loadMetaFromDb(); + const bareEntry = () => ((registry as any).metadata.get(type) as Map | undefined)?.get(NAME)?.label; + const listed = () => (registry.listItems(type as never) as Array<{ name?: string; label?: string }>) + .filter((item) => item?.name === NAME).map((item) => item.label).sort(); + const read = async (packageId?: string) => { + try { + const res = await protocol.getMetaItem({ type, name: NAME, ...(packageId ? { packageId } : {}) }); + return (res?.item as { label?: string } | undefined)?.label; + } catch (err) { + expect(err).toBeInstanceOf(PackageScopedNameRefusedError); + const refusal = err as PackageScopedNameRefusedError; + return [refusal.code, refusal.status, refusal.refusal.candidates]; + } + }; + return { protocol, rows, logged, bareEntry, listed, read }; + } + + for (const type of ['dashboard', 'report']) { + for (const order of [[A, B], [B, A]] as const) { + it(`${type} (${order[0] === A ? 'alpha' : 'beta'} first): the delete of a non-holder's row leaves the bare slot empty; the registry lists each holder once, and every read answers as before the delete`, async () => { + const h = await boot(type, order); + const holders = order.map((id) => ({ kind: 'package', packageId: id })); + // The real boot's shape: the non-holder's row is not in the bare slot. + expect(h.bareEntry()).toBeUndefined(); + + const result = await h.protocol.deleteMetaItem({ type, name: NAME, packageId: O }); + + expect(result.success).toBe(true); + expect(h.rows).toHaveLength(0); + // At 45dee177: `shipped:`, listed a second time. + expect(h.bareEntry()).toBeUndefined(); + expect(h.listed()).toEqual([`shipped:${A}`, `shipped:${B}`]); + expect([await h.read(A), await h.read(B)]).toEqual([`shipped:${A}`, `shipped:${B}`]); + expect(await h.read()).toEqual(['RESOURCE_CONFLICT', 409, holders]); + expect(healLines(h.logged)).toEqual([]); + }); + } + } + + it('CONTROL — dashboard, a name ONE package holds: the delete of a non-holder\'s row still registers that holder\'s baseline, as before', async () => { + const h = await boot('dashboard', [A]); + + await h.protocol.deleteMetaItem({ type: 'dashboard', name: NAME, packageId: O }); + + expect(h.bareEntry()).toBe(`shipped:${A}`); + expect(await h.read(A)).toBe(`shipped:${A}`); + expect(healLines(h.logged)).toEqual([]); + }); +});