diff --git a/.changeset/32354-stored-rows-served-set.md b/.changeset/32354-stored-rows-served-set.md new file mode 100644 index 00000000000..29e3e6f2a2b --- /dev/null +++ b/.changeset/32354-stored-rows-served-set.md @@ -0,0 +1,33 @@ +--- +'@objectstack/metadata-protocol': patch +--- + +fix(metadata-protocol)!: a name a disabled package holds through a stored row is judged over the served set, never by save order (ADR-0150 D3, #32354) + +Clause-②: yes (narrowing) +Metadata-protocol: none +Skills-docs: none — no skill or doc teaches the by-name answer for a name a disabled package holds through a stored row; `content/docs/deployment/marketplace-handbook.mdx` already says a disabled module is hidden. + + + +**BREAKING** answer narrowing at the `/meta` bare reads: a name held by the stored rows of two or more enabled packages beside a disabled one is refused `409 RESOURCE_CONFLICT` naming only the enabled holders, where the plain read and `/published` answered by save order; and an address naming a disabled customization pair whose base is enabled answers 404, where it served the base's item. + +A read naming no package of a name a disabled package holds now judges the name's holders over the stored rows as well as the registry. The registry keeps one bare slot per name, so it holds at most one of two packages' stored rows of a name nobody ships, and the answer followed save order. The holders are the registry's served holders and the packages the active stored rows are bound to, less every disabled package. A row the reads decline (a shipped flow name's undeclared row, a sealed managed name's row) and a row bound to a holder's customization pair (bound over every holder it is a pair of, #32408) hold nothing of their own. This applies only while some installed package is disabled, to a type the registry reads by package, when no enabled package ships the name in code, and when no package-less row or environment item of the name is stored. The in-process layered read (`getMetaItemLayered`) is re-addressed to the one served holder but never refuses: its resolution readers (the security catalog echo, the packaged-permission-set lock gate) keep the answer they had, and the `/layers` door refuses. + +**Every moved answer, before → after** (before = 18.4.0). Both transports (`RestServer` and the runtime `HttpDispatcher`), both registration orders, both save orders, before and after a reboot, except where a line names one transport. First-party count 0 for each: no first-party app disables a package at boot. + +Widening: + +- Stored rows only (no package ships the name), a row bound to an enabled package E and one bound to a disabled package D. Bare `GET /meta/:type/:name`, `/layers` and `/published`, D's row saved first: 404 → 200 with E's row. E's row saved first: the plain read and `/published` unchanged (E's row); `/layers` 409 naming E and D → 200 with E's layers. +- D ships the name in code and E holds it only as a stored row, D disabled: the bare plain read, `/layers` and `/published` 404 → 200 with E's row. The same when D's customization pair also holds D's declared override row of the name (`RestServer`). + +Narrowing: + +- Stored rows of two or more enabled packages and of a disabled one, no shipper, the bare plain read and `/published`: 404 (D's row read first and withheld) or 200 with the first-saved enabled row → 409 `RESOURCE_CONFLICT` naming only the enabled holders, with `details.candidates` on the plain read. +- `GET /meta/:type/:name?package=PAIR`, PAIR a disabled customization pair whose base is enabled and ships the name, with or without PAIR's declared override row: 200 with the base's item (the override's body when there is one) → 404, PAIR's withheld item (`RestServer`). + +Same status, the disabled package no longer named: + +- Two or more enabled packages' rows and a disabled one's, bare `/layers`: 409 naming every row's package, D included → 409 naming only the enabled holders. + +**Not moved.** `GET /meta/:type/:name?package=D`, D disabled and holding the name only as a stored row while an enabled package ships it: 404 since 18.4.0 (the strict envelope of #32501 leaves D's row its own stamp, which the item face withholds); this change adds a second guard for the same answer. With no disabled package, nothing moves (two enabled packages' stored rows of a name nobody ships keep their answers; #32607 carries that case). A name an enabled package ships in code is judged as before (#32183's registry read). A shipped flow name whose shipper is disabled, with rows of two other packages, answers the missing name as before. Re-enabling the package restores every answer above. The save door is unchanged (#32605 carries it). The environment's own item beside an enabled and a disabled shipper keeps the enabled holder's code layer on `/layers`: the registry answers the environment's item only when no package ships the name, so that shape cannot arise. diff --git a/packages/metadata-protocol/src/protocol-exact-qualifier.ts b/packages/metadata-protocol/src/protocol-exact-qualifier.ts index cfbd9ab253b..2af562758d8 100644 --- a/packages/metadata-protocol/src/protocol-exact-qualifier.ts +++ b/packages/metadata-protocol/src/protocol-exact-qualifier.ts @@ -322,8 +322,9 @@ export async function admitExactQualifier( /** * [#32183, ruling (c)] The disabled package an exact address reads, when every - * package it reads is disabled: the package `packageId` names when it holds the - * name, else the bases it is a pair of when none of them is enabled (the + * package it reads is disabled: the package `packageId` names when it is + * disabled ([#32354] whether it holds the name in the registry or only as a + * stored row), else the bases it is a pair of when none of them is enabled (the * bases {@link admitExactQualifier} admits it at). `undefined` when the * address reads an enabled package, reads no holder, or the type is not * package-scoped. The item face answers such an address as that package's @@ -339,8 +340,14 @@ export function disabledExactAddressHolder( const registry = registryOf(host) as (Partial & { isPackageDisabled?(id: string): boolean }) | undefined; if (typeof registry?.packageScopedHoldersOf !== 'function' || typeof registry.isPackageDisabled !== 'function') return undefined; const disabled = (id: string) => registry.isPackageDisabled!(id) === true; + // [#32354] A disabled package named is its own withheld item whether it + // holds the name in the registry or only as a stored row, which the + // registry need not know (one bare slot per name): the read reached its + // row, and served its body under the envelope of the package that ships + // the name. + if (disabled(request.packageId)) return request.packageId; const every = registry.packageScopedHoldersOf(request.type, request.name, request.packageId, { includeDisabledPackages: true }) ?? []; - if (every.includes(request.packageId)) return disabled(request.packageId) ? request.packageId : undefined; + if (every.includes(request.packageId)) return undefined; const installed = installedManifestsOfEngine(host.engine); const bases = every.filter((holder) => pairsOf(holder, installed).includes(request.packageId)); return bases.length > 0 && bases.every(disabled) ? bases[0] : undefined; diff --git a/packages/metadata-protocol/src/protocol-meta-item-face.ts b/packages/metadata-protocol/src/protocol-meta-item-face.ts index 390b913f9a8..63ac6038632 100644 --- a/packages/metadata-protocol/src/protocol-meta-item-face.ts +++ b/packages/metadata-protocol/src/protocol-meta-item-face.ts @@ -34,7 +34,16 @@ * surface does not move. */ +import { + PackageScopedNameRefusedError, + packageScopedRefusalMessage, + type PackageScopedResolvableType, +} from '@objectstack/core'; import type { ObjectStackProtocolImplementation } from './protocol.js'; +import { shippingPackageIds } from './protocol-environment-rows.js'; +import { resolveBundleMemberSelector } from './protocol-bundle-members.js'; +import { installedManifestsOfEngine } from './installed-manifests.js'; +import { pairsOf } from '@objectstack/metadata-core'; import { disabledExactAddressHolder } from './protocol-exact-qualifier.js'; import { isActionWithheldByDisabledPackage, @@ -53,8 +62,17 @@ import { export interface MetaItemFaceHost { readonly engine: ObjectStackProtocolImplementation['engine']; readonly readMetaItem: ObjectStackProtocolImplementation['readMetaItem']; + readonly packagelessAddress: ObjectStackProtocolImplementation['packagelessAddress']; + readonly shippedArtifactsOf: ObjectStackProtocolImplementation['shippedArtifactsOf']; + readonly readItemActiveRows: ObjectStackProtocolImplementation['readItemActiveRows']; + readonly declinesStoredRow: ObjectStackProtocolImplementation['declinesStoredRow']; + readonly rethrowUnlessMetadataStoreUnprovisioned: ObjectStackProtocolImplementation['rethrowUnlessMetadataStoreUnprovisioned']; } +/** The host {@link servedRowHolderOf} and {@link servedAddressJudgment} read. */ +type ServedRowHost = Pick; + /** * [#32183] The package holding `(type, name)` when every package that holds * it is disabled; `undefined` when none holds it or an enabled one does. @@ -104,6 +122,136 @@ export function servedHolderAddressOf(registry: any, type: string, name: string) return served[0]; } +/** + * [#32354, ADR-0150 D3] The served holder of a name for a read naming no + * package, judged over the stored rows too: what {@link servedHolderAddressOf} + * answers from the registry alone, for the names whose holders live only in + * `sys_metadata`. The registry keeps one bare slot per name, so a stored row a + * package saved under a name no enabled package ships is in it only when it + * was the last one written or hydrated; the rows themselves name every + * package that holds the name ({@link packagelessAddress}'s candidates). + * + * Asked only when a package is disabled, of a type the registry reads by + * package, when an enabled package ships no code item of the name (otherwise + * the shipped item is the name's, ADR-0048 §3.3, and a row of another package + * never stands in for it: {@link servedHolderAddressOf} and + * `packagelessContentPackage` judge it), and when no package-less row and no + * environment item is stored (a customization of no one holder, judged as + * before). Then the holders are the registry's served holders and the + * packages the active rows are bound to, less every disabled package: + * + * - one → `{ kind: 'served' }`, read at its address, as the registry's + * served holder is; + * - two or more → `{ kind: 'refused' }`, the by-name read's own refusal + * (`ambiguous-in-environment`), naming only the served holders (epic ruling + * on #32354, Q2): a disabled package is never named; + * - none, or no disabled package among them → `undefined`, the read as it was. + * + * [#32354 contract review S4] A row the reads decline + * ({@link ObjectStackProtocolImplementation.declinesStoredRow}: a shipped flow + * name's undeclared row, a sealed managed name's row) serves nothing at its + * package's address, so it makes that package no holder here: counted, it + * re-addressed the read to an address that serves nothing, and two of them + * were refused naming two such addresses. It still counts toward whether a + * disabled package holds the name, and an unbound one still leaves the read + * as it was. + * + * [#32354 contract review N2, #32408] Nor does a row bound to a holder's + * customization pair (`pairsOf`): a pair row is bound over every holder it + * is a pair of, never a holder of its own, so a disable never moves it (a + * disabled holder's declared override row is that holder's item, ADR-0146 + * D9, withheld with it). Counted for the enabled pair, it served a disabled + * holder's customization at the bare address, where the read answered the + * missing name. + * + * Every holder is judged by save order no more: the disabled package's row + * read first answered the missing name, the enabled one's served it. + */ +export async function servedRowHolderOf( + host: ServedRowHost, + type: string, name: string, member?: string, +): Promise<{ kind: 'served'; packageId: string } | { kind: 'refused'; error: PackageScopedNameRefusedError } | undefined> { + const registry: any = host.engine?.registry; + if (typeof registry?.readsByPackage !== 'function' || registry.readsByPackage(type) !== true) return undefined; + if (typeof registry.packageScopedHoldersOf !== 'function' || typeof registry.isPackageDisabled !== 'function') return undefined; + const disabled = (id: string): boolean => registry.isPackageDisabled(id) === true; + // No disabled package at all: no read moves, and no row is read for it. + // The predicate `isPackageDisabled` reads, asked of each installed record. + if (typeof registry.getAllPackages === 'function' + && !(registry.getAllPackages() as ReadonlyArray<{ enabled?: unknown; status?: unknown } | undefined>) + .some((pkg) => pkg?.enabled === false || pkg?.status === 'disabled')) { + return undefined; + } + if ([...shippingPackageIds(host.shippedArtifactsOf(type, name, undefined, member))].some((id) => !disabled(id))) return undefined; + if (typeof registry.getEnvironmentScopedItem === 'function' && registry.getEnvironmentScopedItem(type, name, member) !== undefined) { + return undefined; + } + // The item's active rows, read once: every row binds a holder, and only + // the rows the reads serve make a served one (S4). An unprovisioned store + // holds no row. + let activeRows: Awaited>; + try { + activeRows = await host.readItemActiveRows(type, name); + } catch (error) { + host.rethrowUnlessMetadataStoreUnprovisioned(error, 'sys_metadata'); + activeRows = []; + } + const rows = await host.packagelessAddress(type, name, member, 'active', activeRows); + if (rows.unbound) return undefined; + const every: readonly string[] = registry.packageScopedHoldersOf(type, name, null, { includeDisabledPackages: true }) ?? []; + if (![...every, ...rows.candidates].some(disabled)) return undefined; + const installed = installedManifestsOfEngine(host.engine); + const pairRow = (row: { package_id?: unknown }): boolean => + typeof row.package_id === 'string' && every.some((holder) => pairsOf(holder, installed).includes(row.package_id as string)); + const servingRows = await host.packagelessAddress(type, name, member, 'active', activeRows.filter((row) => + !host.declinesStoredRow(type, name, row) && !pairRow(row))); + const served = [...new Set([...(registry.packageScopedHoldersOf(type, name, null) ?? []), ...servingRows.candidates])] + .filter((id) => !disabled(id)); + if (served.length === 1) return { kind: 'served', packageId: served[0] }; + if (served.length === 0) return undefined; + const base = { + status: 'refused' as const, + reason: 'ambiguous-in-environment' as const, + type: type as PackageScopedResolvableType, + name, + callerPackageId: null, + candidates: served.sort().map((packageId) => ({ kind: 'package' as const, packageId })), + }; + return { kind: 'refused', error: new PackageScopedNameRefusedError({ ...base, message: packageScopedRefusalMessage(base) }) }; +} + +/** + * [#32183, #32354] The judgment of the address a read naming no package takes + * for a name a disabled package also holds: the registry's served holder + * ({@link servedHolderAddressOf}), else the stored rows' ({@link + * servedRowHolderOf}), which may be the refusal naming two or more served + * holders; `undefined` leaves the read as it was. Answered, never thrown: the + * plain read throws the refusal ({@link servedAddressOf}); the layered read + * only re-addresses, and leaves the refusal to its door (contract review S1). + */ +export async function servedAddressJudgment( + host: ServedRowHost, + type: string, name: string, member?: string, +): Promise<{ kind: 'served'; packageId: string } | { kind: 'refused'; error: PackageScopedNameRefusedError } | undefined> { + const registryAddress = servedHolderAddressOf(host.engine?.registry, type, name); + if (registryAddress !== undefined) return { kind: 'served', packageId: registryAddress }; + return servedRowHolderOf(host, type, name, member); +} + +/** + * [#32183, #32354] {@link servedAddressJudgment} for the plain read: the + * served address, or `undefined`; throws the refusal when two or more served + * packages hold the name. + */ +export async function servedAddressOf( + host: ServedRowHost, + type: string, name: string, member?: string, +): Promise { + const judged = await servedAddressJudgment(host, type, name, member); + if (judged?.kind === 'refused') throw judged.error; + return judged?.packageId; +} + /** * [#32183, contract review M4] Whether `copy`, the metadata service's copy of * a name read at `address` (the served holder {@link servedHolderAddressOf} @@ -127,7 +275,7 @@ export function isAnotherHoldersCopy(registry: any, type: string, name: string, /** The body of `ObjectStackProtocolImplementation#readMetaItemFace`; the method's docblock there is its contract. */ export async function readMetaItemFace( - this: Pick, + this: Pick & ServedRowHost, request: { type: string, name: string, packageId?: string, state?: 'active' | 'draft', previewDrafts?: boolean, member?: string, @@ -139,8 +287,14 @@ export async function readMetaItemFace( } // [#32183] A name a disabled package also holds is read at the served // holder's address ({@link servedHolderAddressOf}), so no layer of the - // read can be the disabled holder's. - const servedAddress = request.packageId ? undefined : servedHolderAddressOf(this.engine?.registry, request.type, request.name); + // read can be the disabled holder's. [#32354] …judged over the stored + // rows too ({@link servedAddressOf}), so the answer never follows save order. + // [contract review N1] The member selector resolved once, as the layered + // read resolves it: a malformed one is the read's own 400, never a 409 + // judged at a member nobody named. + const servedAddress = request.packageId + ? undefined + : await servedAddressOf(this, request.type, request.name, resolveBundleMemberSelector(request.type, request.member)); const answer = await this.readMetaItem(servedAddress === undefined ? request : { ...request, packageId: servedAddress }, { face }); // [#28877] The served read of an `action` also withholds one that launches // into a disabled package, as the served list does. diff --git a/packages/metadata-protocol/src/protocol-meta-layered-reads.ts b/packages/metadata-protocol/src/protocol-meta-layered-reads.ts index 627237abb0c..19511372f35 100644 --- a/packages/metadata-protocol/src/protocol-meta-layered-reads.ts +++ b/packages/metadata-protocol/src/protocol-meta-layered-reads.ts @@ -78,11 +78,11 @@ import { boundPackageOf, withRowBinding } from './protocol-package-bindings.js'; import type { RowlessExpandedView } from './protocol-stored-overlay-types.js'; import { type ServedOverrideRow, isServedOverrideRowOf } from './served-override.js'; import { installedManifestsOfEngine } from './installed-manifests.js'; -import { isAnotherHoldersCopy, servedHolderAddressOf } from './protocol-meta-item-face.js'; +import { isAnotherHoldersCopy, servedAddressJudgment } from './protocol-meta-item-face.js'; import { packagelessAddressAmbiguityRefusal } from './sys-metadata-repository.js'; /** The body of `ObjectStackProtocolImplementation#packagelessLayeredReadRefusal`; the method's docblock there is its contract. */ -export async function packagelessLayeredReadRefusal(host: Pick, request: { +export async function packagelessLayeredReadRefusal(host: Pick, request: { type: string; name: string; /** The bundle member the layered read addresses ({@link resolveBundleMemberSelector}); omitted, the canonical one. */ @@ -93,8 +93,14 @@ export async function packagelessLayeredReadRefusal(host: Pick 1 ? packagelessAddressAmbiguityRefusal('layered read', type, name, address.candidates) @@ -102,7 +108,7 @@ export async function packagelessLayeredReadRefusal(host: Pick, request: { +export async function getMetaItemLayered(host: Pick, request: { type: string; name: string; packageId?: string; @@ -165,7 +171,16 @@ export async function getMetaItemLayered(host: Pick = { get engine() { return readEngine(); }, readMetaItem: (...a) => this.readMetaItem(...a), + packagelessAddress: (...a) => this.packagelessAddress(...a), + shippedArtifactsOf: (...a) => this.shippedArtifactsOf(...a), + readItemActiveRows: (...a) => this.readItemActiveRows(...a), + declinesStoredRow: (...a) => this.declinesStoredRow(...a), + rethrowUnlessMetadataStoreUnprovisioned: (...a) => this.rethrowUnlessMetadataStoreUnprovisioned(...a), }; return readMetaItemFace.call(host, request, face); } @@ -4185,6 +4190,13 @@ export class ObjectStackProtocolImplementation implements * its in-process resolution readers (the security catalog echo, the * packaged-permission-set lock gate, `/published`) read the layers of a * name with no package and keep the answer they had. + * + * [#32354, ADR-0150 D3] While a package is disabled, a name the stored + * rows of two or more served packages hold beside it is refused here + * naming only those served packages (the plain read's own refusal), and a + * name with one served holder is no ambiguity: {@link getMetaItemLayered} + * reads it at that holder's address. Both are judged by the same + * `servedAddressJudgment`, which neither of them throws. */ async packagelessLayeredReadRefusal(request: { type: string; diff --git a/packages/rest/src/meta-disabled-package-doors.test.ts b/packages/rest/src/meta-disabled-package-doors.test.ts index 418eea5ee27..eeb4de4cc4e 100644 --- a/packages/rest/src/meta-disabled-package-doors.test.ts +++ b/packages/rest/src/meta-disabled-package-doors.test.ts @@ -502,6 +502,197 @@ describe('[#32183] RestServer: a name an enabled and a disabled package both shi }, 60_000); }); +/** + * [#32354, ADR-0150 D3] The row layer judges a name's holders over the served + * set too: a holder is a registry holder or a package a stored row of the name + * is bound to. With one of them disabled, the bare doors read at the one + * served holder's address, and refuse naming only the served holders when two + * or more are; `?package=DISABLED` is that package's withheld item whether it + * holds the name in the registry or only as a stored row. The runtime + * dispatcher's pins are in + * `packages/runtime/src/domains/meta-disabled-holder-served-stack.test.ts`. + * + * Measured on the #32183 head (6655b742, step 0 of #32354): rows saved by the + * save door (which writes each through to the registry's one bare slot of the + * name), the disabled row saved first: the plain read and /layers answered + * 404; the enabled row first: /layers answered 409 naming the disabled + * package. `?package=DISABLED` of a name the enabled package ships in code + * served the disabled row's body under the enabled package's envelope. + */ +describe('[#32354] RestServer: stored rows judged over the served set', () => { + const THIRD_PKG = 'app.third_demo'; + const tag = (pkg: string) => (pkg === PKG ? 'disabled' : pkg === THIRD_PKG ? 'third' : 'healthy'); + const save = (host: ReturnType, name: string, pkg: string) => host.protocol.saveMetaItem({ + type: 'dashboard', name, packageId: pkg, item: { name, label: `${tag(pkg)} row`, widgets: [] }, + }); + const noDisabledTrace = (answers: ReadonlyArray<{ body: unknown }>) => { + for (const answer of answers) { + const text = JSON.stringify(answer.body ?? null); + expect(text).not.toMatch(/disabled (code|row)/); + expect(text).not.toContain(PKG); + } + }; + const ORDERS = [['disabled first', [PKG, OTHER_PKG, THIRD_PKG]], ['enabled first', [OTHER_PKG, THIRD_PKG, PKG]]] as const; + + it.each(ORDERS.flatMap(([order, packages]) => [ + [`${order}, the disabled row saved first`, packages, [PKG, OTHER_PKG]], + [`${order}, the enabled row saved first`, packages, [OTHER_PKG, PKG]], + ] as const))('%s: no package ships the name; the plain read, /layers and /published serve the enabled row', async (_label, packages, saved) => { + const host = setup(); + for (const pkg of packages) host.registry.installPackage({ id: pkg, name: pkg, version: '1.0.0' } as any, 'code'); + for (const pkg of saved) await save(host, 'rows_only_dash', pkg); + host.registry.disablePackage(PKG); + const read = await host.plain('dashboard', 'rows_only_dash'); + expect([read.status, read.body?.item?.label, read.body?.packageId], JSON.stringify(read.body)).toEqual([200, 'healthy row', OTHER_PKG]); + const layers = await host.layers('dashboard', 'rows_only_dash'); + expect([layers.status, layers.body?.overlay?.label, layers.body?.effective?.label], JSON.stringify(layers.body)) + .toEqual([200, 'healthy row', 'healthy row']); + const published = await host.published('dashboard', 'rows_only_dash'); + expect([published.status, published.body?.label], JSON.stringify(published.body)).toEqual([200, 'healthy row']); + const exact = await host.plainWith('dashboard', 'rows_only_dash', { package: PKG }); + expect(exact.status, JSON.stringify(exact.body)).toBe(404); + noDisabledTrace([read, layers, published, exact]); + // Re-enabled: two holders again, and /layers refuses naming both. + host.registry.enablePackage(PKG); + const again = await host.layers('dashboard', 'rows_only_dash'); + expect(again.status).toBe(409); + expect(JSON.stringify(again.body)).toContain(PKG); + }, 60_000); + + it.each(ORDERS)('%s, ruling Q2: two served row holders beside a disabled one are refused naming only the served holders, the plain read with details.candidates', async (_order, packages) => { + const host = setup(); + for (const pkg of packages) host.registry.installPackage({ id: pkg, name: pkg, version: '1.0.0' } as any, 'code'); + for (const pkg of [PKG, OTHER_PKG, THIRD_PKG]) await save(host, 'three_rows_dash', pkg); + host.registry.disablePackage(PKG); + const read = await host.plain('dashboard', 'three_rows_dash'); + expect([read.status, read.body?.code], JSON.stringify(read.body)).toEqual([409, 'RESOURCE_CONFLICT']); + expect([...(read.body?.details?.candidates ?? [])].map((c: any) => c.packageId).sort(), JSON.stringify(read.body)) + .toEqual([OTHER_PKG, THIRD_PKG].sort()); + // The layered door answers a package-scoped refusal by its sentence alone, as for two registry holders (#32622). + const layers = await host.layers('dashboard', 'three_rows_dash'); + expect(layers.status, JSON.stringify(layers.body)).toBe(409); + for (const pkg of [OTHER_PKG, THIRD_PKG]) expect(JSON.stringify(layers.body)).toContain(pkg); + // [contract review S3] Bare /published asks the plain read first and answers its refusal. + const published = await host.published('dashboard', 'three_rows_dash'); + expect([published.status, published.body?.code ?? published.body?.error?.code], JSON.stringify(published.body)) + .toEqual([409, 'RESOURCE_CONFLICT']); + for (const pkg of [OTHER_PKG, THIRD_PKG]) expect(JSON.stringify(published.body)).toContain(pkg); + noDisabledTrace([read, layers, published]); + // [contract review S1] In-process, the layered read never throws the refusal: its resolution + // readers (the security catalog echo, the packaged-permission-set lock gate) keep the answer + // they had. The door's step 1c is the one that refuses, naming only the served holders. + await expect(host.protocol.getMetaItemLayered({ type: 'dashboard', name: 'three_rows_dash' })).resolves.toBeDefined(); + const refusal: any = await host.protocol.packagelessLayeredReadRefusal({ type: 'dashboard', name: 'three_rows_dash' }); + expect([refusal?.code, refusal?.status]).toEqual(['RESOURCE_CONFLICT', 409]); + expect(String(refusal?.message)).not.toContain(PKG); + for (const pkg of [OTHER_PKG, THIRD_PKG]) expect(String(refusal?.message)).toContain(pkg); + }, 60_000); + + it.each(ORDERS)('%s, contract review S4: a shipped flow name whose shipper is disabled, with rows of two other packages the reads decline, is not refused naming them', async (_order, packages) => { + const host = setup(); + for (const pkg of packages) host.registry.installPackage({ id: pkg, name: pkg, version: '1.0.0' } as any, 'code'); + host.registry.registerItem('flow', { name: 'shipped_flow', label: 'disabled code', type: 'autolaunched', nodes: [], edges: [] } as any, 'name', PKG); + const engine: any = (host.protocol as any).engine; + for (const pkg of [OTHER_PKG, THIRD_PKG]) { + await engine.insert('sys_metadata', { + type: 'flow', name: 'shipped_flow', package_id: pkg, state: 'active', scope: 'platform', strategy: 'merge', version: 1, + metadata: JSON.stringify({ name: 'shipped_flow', label: `${tag(pkg)} row`, type: 'autolaunched', nodes: [], edges: [] }), + }); + } + host.registry.disablePackage(PKG); + // Neither row is served (an undeclared row of a shipped flow name is declined), so neither + // package holds the name: the read is the missing name's, as before, never a 409 naming them. + for (const answer of [await host.plain('flow', 'shipped_flow'), await host.layers('flow', 'shipped_flow'), await host.published('flow', 'shipped_flow')]) { + expect(answer.status, JSON.stringify(answer.body)).toBe(404); + expect(JSON.stringify(answer.body ?? null)).not.toMatch(/healthy row|third row|disabled code/); + } + }, 60_000); + + it.each(ORDERS)('%s, case 3: ?package=DISABLED, where the enabled package ships the name and the disabled one holds it only as a stored row, is the withheld 404', async (_order, packages) => { + const host = setup(); + for (const pkg of packages) host.registry.installPackage({ id: pkg, name: pkg, version: '1.0.0' } as any, 'code'); + host.registry.registerItem('dashboard', { name: 'shipped_dash', label: 'healthy code', widgets: [] } as any, 'name', OTHER_PKG); + await save(host, 'shipped_dash', PKG); + // CONTROL: enabled, that address serves the row. + const own = await host.plainWith('dashboard', 'shipped_dash', { package: PKG }); + expect([own.status, own.body?.item?.label], JSON.stringify(own.body)).toEqual([200, 'disabled row']); + host.registry.disablePackage(PKG); + const exact = await host.plainWith('dashboard', 'shipped_dash', { package: PKG }); + expect(exact.status, JSON.stringify(exact.body)).toBe(404); + const bare = await host.plain('dashboard', 'shipped_dash'); + expect([bare.status, bare.body?.item?.label], JSON.stringify(bare.body)).toEqual([200, 'healthy code']); + noDisabledTrace([exact, bare]); + }, 60_000); +}); + +/** + * [#32354 contract review S2, N2] A customization pair is a package of its + * own. S2: `?package=PAIR` of a disabled pair whose base is enabled is the + * pair's withheld 404 with its declared override row; with no row the pair + * holds nothing and the strict envelope refuses the address naming the base + * (#32501), enabled or not. N2: a disabled base's declared override row, + * bound to its enabled pair, is the base's item (ADR-0146 D9): the bare doors + * never serve it. + * + * Measured on `main` (2c98485d, this change's four source files put back): + * every answer here is the same, so neither case moves. With this change's + * row holders counting the pair's override row for the enabled pair (fix + * round 2, before the N2 exclusion), N2's three doors answered 200 with the + * pair override. + */ +describe('[#32354] RestServer: a disabled customization pair and a disabled base\'s pair row', () => { + // A base whose id derives a pair (`MANIFEST_ID_PATTERN`: no underscore), and that pair. + const BASE = 'com.qa.pair-base'; + const PAIR = `${BASE}.custom`; + const install = (host: ReturnType) => { + host.registry.installPackage({ id: BASE, name: BASE, version: '1.0.0' } as any, 'code'); + host.registry.installPackage({ id: PAIR, name: PAIR, version: '1.0.0', dependencies: { [BASE]: '^1' } } as any, 'authored'); + host.registry.registerItem('dashboard', { name: 'pair_dash', label: 'base code', widgets: [] } as any, 'name', BASE); + }; + const overrideRow = (host: ReturnType) => (host.protocol as any).engine.insert('sys_metadata', { + type: 'dashboard', name: 'pair_dash', package_id: PAIR, state: 'active', scope: 'platform', strategy: 'merge', version: 1, + metadata: JSON.stringify({ name: 'pair_dash', label: 'pair override', widgets: [], overrides: { package: BASE, basedOn: '1.0.0' } }), + }); + + it.each([['without an override row', false], ['with its declared override row', true]] as const)('S2, %s: ?package=PAIR of a disabled pair is the withheld 404 while its base is enabled', async (_label, withRow) => { + const host = setup(); + install(host); + if (withRow) await overrideRow(host); + // CONTROL: the pair enabled, its address serves its override row, or with none the base's item. + const own = await host.plainWith('dashboard', 'pair_dash', { package: PAIR }); + expect([own.status, own.body?.item?.label], JSON.stringify(own.body)).toEqual([200, withRow ? 'pair override' : 'base code']); + host.registry.disablePackage(PAIR); + const exact = await host.plainWith('dashboard', 'pair_dash', { package: PAIR }); + expect([exact.status, exact.body?.error?.code], JSON.stringify(exact.body)).toEqual([404, 'RESOURCE_NOT_FOUND']); + expect(JSON.stringify(exact.body ?? null)).not.toMatch(/pair override|base code/); + // The base's own address is untouched. + const base = await host.plainWith('dashboard', 'pair_dash', { package: BASE }); + expect(base.status, JSON.stringify(base.body)).toBe(200); + }, 60_000); + + it.each([['alone', false], ['beside an enabled package\'s row', true]] as const)('N2, %s: a disabled base\'s declared override row at its enabled pair is never served by the bare read, and holds nothing', async (_label, withOther) => { + const host = setup(); + install(host); + await overrideRow(host); + if (withOther) { + host.registry.installPackage({ id: 'com.qa.other', name: 'other', version: '1.0.0' } as any, 'code'); + await (host.protocol as any).engine.insert('sys_metadata', { + type: 'dashboard', name: 'pair_dash', package_id: 'com.qa.other', state: 'active', scope: 'platform', strategy: 'merge', version: 1, + metadata: JSON.stringify({ name: 'pair_dash', label: 'other row', widgets: [] }), + }); + } + host.registry.disablePackage(BASE); + const read = await host.plain('dashboard', 'pair_dash'); + const layers = await host.layers('dashboard', 'pair_dash'); + const published = await host.published('dashboard', 'pair_dash'); + // Alone: the missing name. Beside an enabled package's row: that row, the one served holder. + expect([read.status, read.body?.item?.label], JSON.stringify(read.body)).toEqual(withOther ? [200, 'other row'] : [404, undefined]); + expect([layers.status, layers.body?.effective?.label], JSON.stringify(layers.body)).toEqual(withOther ? [200, 'other row'] : [404, undefined]); + expect([published.status, published.body?.label], JSON.stringify(published.body)).toEqual(withOther ? [200, 'other row'] : [404, undefined]); + for (const answer of [read, layers, published]) expect(JSON.stringify(answer.body ?? null)).not.toMatch(/pair override|base code/); + }, 60_000); +}); + /** * [ruling record 6787890900] An object the caller may not read is answered on * every object door exactly as a name with nothing behind it — decided BEFORE diff --git a/packages/runtime/src/domains/meta-disabled-holder-served-stack.test.ts b/packages/runtime/src/domains/meta-disabled-holder-served-stack.test.ts index 8a0059ae098..ba2ba0592cf 100644 --- a/packages/runtime/src/domains/meta-disabled-holder-served-stack.test.ts +++ b/packages/runtime/src/domains/meta-disabled-holder-served-stack.test.ts @@ -135,3 +135,201 @@ describe.each([ } }); }); + +// --------------------------------------------------------------------------- +// [#32354, ADR-0150 D3] The row layer judges a name's holders over the served +// set too. A holder is a registry holder or a package a stored row of the name +// is bound to; when one of them is disabled, the bare doors read at the one +// served holder's address, answer the missing name when none is served, and +// refuse naming only the served holders when two or more are. An exact address +// naming a disabled package is that package's withheld item whether it holds +// the name in the registry or only as a stored row. +// +// Measured on the #32183 head (6655b742, step 0 of #32354): stored rows only, +// rows bound to an enabled and a disabled package, the answer followed save +// order (the disabled row saved first: 404 on the plain read, /layers and +// /published; the enabled row first: its row served, /layers 409 naming the +// disabled package). `?package=DISABLED` of a name an enabled package ships in +// code and the disabled one holds only as a row served the row's body under the +// enabled package's envelope. +// --------------------------------------------------------------------------- + +const ROW_E = 'com.qa.row_alpha'; +const ROW_F = 'com.qa.row_gamma'; +const ROW_D = 'com.qa.row_beta'; +const rowTag = (id: string) => (id === ROW_D ? 'disabled' : id === ROW_F ? 'third' : 'enabled'); +const rowPackageOf = (id: string) => ({ + id, name: id.split('.').pop(), namespace: id.split('.').pop(), version: '1.0.0', type: 'app', objects: [], + dashboards: [ + // Case 3: one package ships the name in code, the other holds it only as a stored row. + ...(id === ROW_E ? [{ name: 'e_ships_dash', label: 'enabled code', widgets: [] }] : []), + ...(id === ROW_D ? [{ name: 'd_ships_dash', label: 'disabled code', widgets: [] }] : []), + // Case 2 CONTROL: both ship it. + ...(id !== ROW_F ? [{ name: 'both_ship_dash', label: `${rowTag(id)} code`, widgets: [] }] : []), + ], +}); + +async function bootRowKernel(order: readonly string[], file: string): Promise { + const kernel = new ObjectKernel({ logger: { level: 'silent' }, gracefulShutdown: false }); + const root = mkdtempSync(join(tmpdir(), 'os-meta-row-holder-')); + const engine = new ObjectQL(); + engine.registerDriver(new SqlDriver({ client: 'better-sqlite3', connection: { filename: file }, useNullAsDefault: true }) as any, true); + await kernel.use(new MetadataPlugin({ watch: false, rootDir: root })); + await kernel.use(new ObjectQLPlugin({ ql: engine })); + const host: Plugin = { + name: 'qa.row-holder-host', + dependencies: ['com.objectstack.engine.objectql'], + init: async (ctx: PluginContext) => { + for (const id of order) await ctx.getService('manifest').register(rowPackageOf(id)); + }, + }; + await kernel.use(host); + await kernel.bootstrap(); + await engine.syncSchemas(); + return { kernel, engine, root }; +} + +const layStoredRow = (engine: ObjectQL, name: string, packageId: string | null, label: string) => engine.insert('sys_metadata', { + type: 'dashboard', name, namespace: 'default', package_id: packageId, scope: 'platform', strategy: 'merge', state: 'active', version: 1, + metadata: JSON.stringify({ name, label, widgets: [] }), +}, { context: { isSystem: true } } as any); + +const unwrap = (body: any) => body?.data ?? body; +const noDisabledTrace = (answers: ReadonlyArray<{ body: unknown }>) => { + for (const answer of answers) { + const text = JSON.stringify(answer.body ?? null); + expect(text).not.toMatch(/disabled (code|row)/); + expect(text).not.toContain(ROW_D); + } +}; + +async function expectServedRow(kernel: ObjectKernel, name: string, label: string, packageId: string) { + const read = await get(kernel, `/meta/dashboard/${name}`); + expect([read.status, unwrap(read.body)?.item?.label, unwrap(read.body)?.packageId], JSON.stringify(read.body)).toEqual([200, label, packageId]); + const layers = await get(kernel, `/meta/dashboard/${name}/layers`); + expect([layers.status, unwrap(layers.body)?.overlay?.label, unwrap(layers.body)?.effective?.label], JSON.stringify(layers.body)) + .toEqual([200, label, label]); + const published = await get(kernel, `/meta/dashboard/${name}/published`); + expect([published.status, unwrap(published.body)?.label], JSON.stringify(published.body)).toEqual([200, label]); + const exact = await get(kernel, `/meta/dashboard/${name}`, { package: ROW_D }); + expect([exact.status, codeOf(exact.body)], JSON.stringify(exact.body)).toEqual([404, 'RESOURCE_NOT_FOUND']); + noDisabledTrace([read, layers, published, exact]); +} + +describe.each([ + { regOrder: 'disabled first', registered: [ROW_D, ROW_E, ROW_F] }, + { regOrder: 'enabled first', registered: [ROW_E, ROW_F, ROW_D] }, +])('[#32354] stored rows judged over the served set, packages registered $regOrder', ({ registered }) => { + const roots: string[] = []; + afterAll(() => { for (const dir of roots) rmSync(dir, { recursive: true, force: true }); }); + const freshFile = () => { + const dir = mkdtempSync(join(tmpdir(), 'os-meta-row-holder-db-')); + roots.push(dir); + return join(dir, 'meta.sqlite'); + }; + const down = async (booted: Booted) => { + if (booted.kernel.getState() === 'running') await booted.kernel.shutdown(); + roots.push(booted.root); + }; + + it.each([ + { saveOrder: 'disabled row saved first', saved: [ROW_D, ROW_E] }, + { saveOrder: 'enabled row saved first', saved: [ROW_E, ROW_D] }, + ])('case 1, $saveOrder: no package ships the name; the plain read, /layers and /published serve the enabled row, before and after a reboot', async ({ saved }) => { + const file = freshFile(); + let booted = await bootRowKernel(registered, file); + try { + for (const id of saved) await layStoredRow(booted.engine, 'rows_only_dash', id, `${rowTag(id)} row`); + // CONTROL: enabled, the disabled package's own address serves its row. + const own = await get(booted.kernel, '/meta/dashboard/rows_only_dash', { package: ROW_D }); + expect([own.status, unwrap(own.body)?.item?.label]).toEqual([200, 'disabled row']); + booted.engine.registry.disablePackage(ROW_D); + await expectServedRow(booted.kernel, 'rows_only_dash', 'enabled row', ROW_E); + // Re-enabled: the two rows are two holders again, and /layers refuses naming both. + booted.engine.registry.enablePackage(ROW_D); + const layers = await get(booted.kernel, '/meta/dashboard/rows_only_dash/layers'); + expect([layers.status, codeOf(layers.body)]).toEqual([409, 'RESOURCE_CONFLICT']); + expect(JSON.stringify(layers.body)).toContain(ROW_D); + } finally { + await down(booted); + } + // After a reboot the registry holds one of the two rows (one bare slot per name). + booted = await bootRowKernel(registered, file); + try { + booted.engine.registry.disablePackage(ROW_D); + await expectServedRow(booted.kernel, 'rows_only_dash', 'enabled row', ROW_E); + } finally { + await down(booted); + } + }, 120_000); + + it('case 1, the disabled package ships the name in code and the enabled one holds it only as a stored row: the enabled row is served', async () => { + const booted = await bootRowKernel(registered, freshFile()); + try { + await layStoredRow(booted.engine, 'd_ships_dash', ROW_E, 'enabled row'); + booted.engine.registry.disablePackage(ROW_D); + await expectServedRow(booted.kernel, 'd_ships_dash', 'enabled row', ROW_E); + const exact = await get(booted.kernel, '/meta/dashboard/d_ships_dash', { package: ROW_E }); + expect([exact.status, unwrap(exact.body)?.item?.label], JSON.stringify(exact.body)).toEqual([200, 'enabled row']); + } finally { + await down(booted); + } + }, 120_000); + + it('ruling Q2: two served row holders beside a disabled one are refused naming only the served holders, the plain read with details.candidates', async () => { + const booted = await bootRowKernel(registered, freshFile()); + try { + for (const id of [ROW_D, ROW_E, ROW_F]) await layStoredRow(booted.engine, 'three_rows_dash', id, `${rowTag(id)} row`); + booted.engine.registry.disablePackage(ROW_D); + const read = await get(booted.kernel, '/meta/dashboard/three_rows_dash'); + expect([read.status, codeOf(read.body)], JSON.stringify(read.body)).toEqual([409, 'RESOURCE_CONFLICT']); + expect([...(read.body?.error?.details?.candidates ?? [])].map((c: any) => c.packageId).sort(), JSON.stringify(read.body)) + .toEqual([ROW_E, ROW_F].sort()); + // The layered door answers a package-scoped refusal by its sentence alone, as for two registry holders (#32622). + const layers = await get(booted.kernel, '/meta/dashboard/three_rows_dash/layers'); + expect([layers.status, codeOf(layers.body)], JSON.stringify(layers.body)).toEqual([409, 'RESOURCE_CONFLICT']); + for (const id of [ROW_E, ROW_F]) expect(JSON.stringify(layers.body)).toContain(id); + // [contract review S3] Bare /published asks the plain read first and answers its refusal. + const published = await get(booted.kernel, '/meta/dashboard/three_rows_dash/published'); + expect([published.status, codeOf(published.body)], JSON.stringify(published.body)).toEqual([409, 'RESOURCE_CONFLICT']); + for (const id of [ROW_E, ROW_F]) expect(JSON.stringify(published.body)).toContain(id); + noDisabledTrace([read, layers, published]); + } finally { + await down(booted); + } + }, 120_000); + + it('case 3: ?package=DISABLED, where the enabled package ships the name in code and the disabled one holds it only as a stored row, is the withheld 404', async () => { + const booted = await bootRowKernel(registered, freshFile()); + try { + await layStoredRow(booted.engine, 'e_ships_dash', ROW_D, 'disabled row'); + // CONTROL: enabled, that address serves the row. + const own = await get(booted.kernel, '/meta/dashboard/e_ships_dash', { package: ROW_D }); + expect([own.status, unwrap(own.body)?.item?.label]).toEqual([200, 'disabled row']); + booted.engine.registry.disablePackage(ROW_D); + const exact = await get(booted.kernel, '/meta/dashboard/e_ships_dash', { package: ROW_D }); + expect([exact.status, codeOf(exact.body)], JSON.stringify(exact.body)).toEqual([404, 'RESOURCE_NOT_FOUND']); + const bare = await get(booted.kernel, '/meta/dashboard/e_ships_dash'); + expect([bare.status, unwrap(bare.body)?.item?.label]).toEqual([200, 'enabled code']); + noDisabledTrace([exact, bare]); + } finally { + await down(booted); + } + }, 120_000); + + it('case 2 CONTROL: the environment\'s package-less row of a name an enabled and a disabled package both ship never makes /layers take the disabled holder\'s code layer', async () => { + const booted = await bootRowKernel(registered, freshFile()); + try { + await layStoredRow(booted.engine, 'both_ship_dash', null, 'environment row'); + booted.engine.registry.disablePackage(ROW_D); + // overlaySlotBinding answers the environment's item only when no package ships the name. + expect((booted.engine.registry as any).getEnvironmentScopedItem('dashboard', 'both_ship_dash')).toBeUndefined(); + const layers = await get(booted.kernel, '/meta/dashboard/both_ship_dash/layers'); + expect([layers.status, unwrap(layers.body)?.code?.label, unwrap(layers.body)?.effective?.label], JSON.stringify(layers.body)) + .toEqual([200, 'enabled code', 'environment row']); + noDisabledTrace([layers]); + } finally { + await down(booted); + } + }, 120_000); +});