diff --git a/.changeset/34682-bundle-member-selector.md b/.changeset/34682-bundle-member-selector.md new file mode 100644 index 00000000000..0ba630f74ee --- /dev/null +++ b/.changeset/34682-bundle-member-selector.md @@ -0,0 +1,41 @@ +--- +'@objectstack/metadata-protocol': patch +'@objectstack/spec': patch +--- + +fix(metadata-protocol)!: the read naming no bundle member addresses the canonical member alone, as `?member=en-US` and `/published` do + +Clause-②: no (narrowing) +Metadata-protocol: none +Skills-docs: none — no page under `skills/**` or `content/docs/**` teaches the read naming no member of a bundle that has no canonical member; `content/docs/references` prints the selector sentence this change now follows, and the ADR-0146 paragraph of `skills/objectstack-platform/SKILL.md` names members only at a base or a package address. No taught sentence moves. + + + +**BREAKING**: where the address holds no canonical member of a bundled name, the read naming no member (`GET /meta/TYPE/NAME`, with or without `?package=`), `/layers` naming none and the execution read naming none move from serving another member (200) to `404 RESOURCE_NOT_FOUND` (the execution read: `null`). Name the member (`?member=fr-FR`) to read it, as before. + +The by-name doors' member selector reads a request naming no member as the canonical one (`en-US` for `email_template`), and naming the canonical value is the same request. That is the selector's declared contract (`GetMetaItemRequest.member`). For a bundle with no canonical member, the two requests got different answers: + +- The plain read `GET /api/v1/meta/email_template/NAME` and `/layers` served the bundle's other member (for example `fr-FR`, status 200). The execution read (`getMetaItemForExecution`) returned that member too. +- `?member=en-US` and `/published` answered 404. + +The same split held in two more arrangements: + +- A base ships only `fr-FR` and a dependent that declares it stores `en-US`. The read naming no package and no member served the base's `fr-FR`. +- `?package=X` where X ships no `en-US` while another package does (a dependent, or a disabled holder that the read naming no package re-addresses past to X). The read naming no member served X's `fr-FR`. `?package=X&member=en-US` answered nothing. + +For a bundle that only stores a non-canonical row (a package-less `fr-FR` row with no `en-US` row), the plain read also changed across a restart. It answered 404 before the restart. After the restart it served the row (200), because the restart had moved the row into the registry's bare slot. + +The plain read, `/layers` and the execution read now ask every layer for the canonical member when no member is named. They take the registry's bare-name answer only when it is that member. The base-address fallback serves only the base's canonical member, and an exact address (`?package=`) replaces another package's item only with the holder's item at the addressed member. So each of these arrangements answers 404 on every door naming no member, the same as `?member=en-US` and `/published`, before and after a restart. + +A bundle that ships or stores its canonical member keeps its status and served body, and so does any named member. Three more answers move: + +- `/layers` naming no member, where the canonical member is stored over a package that ships only another member: `code` goes from that member's body to `null`, as `/layers?member=en-US` already answered. Status and `effective` do not move. +- For a type with no members, `/layers?package=X`, where X holds nothing of the name and the only holder is disabled: `code` goes from the disabled holder's artifact to `null`. A disabled package holds no name. +- `?package=X` naming no member, where X does not hold the name and the environment stores it only in a non-canonical member: the read was refused `409 RESOURCE_CONFLICT` (`not-held-by-qualifier`, naming the environment) once a restart had hydrated the row. It now answers the `404` that `?member=en-US` already answered, on the plain read and `/layers`. A non-canonical member is still read with its own selector, for example `?member=fr-FR`, which the Studio list already links to for each member. + +**Reverses two statements of the 18.6.0 note** for the arrangement where base A ships `email_template` `welcome` in `fr-FR` only and dependent D, which declares A, stores `en-US`: + +- 18.6.0 said "`GET /meta/email_template/welcome/layers` (no member) moves FROM `404` after a restart TO A's `fr-FR` (200)". Now `/layers` naming no member answers `404 RESOURCE_NOT_FOUND` on both sides of a restart, as `/layers?member=en-US` does. +- 18.6.0 said "**Not moved.** The plain read, `?member=CANONICAL` and the execution read of the first shape (A's `fr-FR` naming no member, 404 at `en-US`)." Now only `?member=CANONICAL` is unmoved. The plain read naming no member answers 404 and the execution read naming no member answers `null`, where both served A's `fr-FR`. + +Migration: a caller that read a bundle with no canonical member without naming a member must name the member it wants (`?member=fr-FR`). The email template loader reads the list (`getMetaItemsForExecution`) and does not use this read, so it is not affected. diff --git a/packages/metadata-protocol/src/protocol-bundle-members.ts b/packages/metadata-protocol/src/protocol-bundle-members.ts index 1bbff0a2b57..d8a49b44114 100644 --- a/packages/metadata-protocol/src/protocol-bundle-members.ts +++ b/packages/metadata-protocol/src/protocol-bundle-members.ts @@ -47,6 +47,13 @@ export const BUNDLE_MEMBER_MAX_LENGTH = 100; * well-formed member with nothing stored or shipped is not refused here: each * door answers it as it answers an absent item. * + * [#34682] …the canonical member included, named or not: a bundle that ships + * or stores no canonical member answers the read naming no member as absent, + * as it answers `?member=en-US` and `/published`. The registry's bare-name + * read still answers such a bundle with its first member; the by-name reads + * (`getMetaItem`, `getMetaItemLayered`) take that answer only when it is the + * canonical member, so it reaches no door. + * * Refuses, with a located 400 `VALIDATION_ERROR` — the code the REST door's * recognition refusal answers for a misspelled selector on the same routes — * a selector on a type whose identity is its name alone, and a value that diff --git a/packages/metadata-protocol/src/protocol-exact-qualifier.ts b/packages/metadata-protocol/src/protocol-exact-qualifier.ts index ed233dcf70e..1e8ec8b19a8 100644 --- a/packages/metadata-protocol/src/protocol-exact-qualifier.ts +++ b/packages/metadata-protocol/src/protocol-exact-qualifier.ts @@ -58,6 +58,7 @@ import { import { ENVIRONMENT_ADDRESS_SEGMENT, readNamespacePositionSegment } from '@objectstack/spec/kernel'; import { installedManifestsOfEngine } from './installed-manifests.js'; import { canonicalizeMetaRequestType } from './protocol-meta-type-schema.js'; +import { bundleMemberDiscriminator } from './protocol-bundle-members.js'; import type { ProtocolHost } from './protocol-host.js'; import type { ServedOverrideRow } from './served-override.js'; @@ -364,7 +365,13 @@ export async function admitExactQualifier( } const candidates: PackageScopedHolder[] = holders.map((holder) => ({ kind: 'package' as const, packageId: holder })); - if (registry.getEnvironmentScopedItem(request.type, request.name, request.member) !== undefined) { + // [#34682] The environment's item AT the addressed member: the named one, + // else the canonical one (naming no member is naming the canonical value). + // Asked with the raw selector, a read naming no member counted the + // environment's first member of a bundle it holds only in another member, + // so `?package=X` was refused 409 naming the environment where + // `?package=X&member=` was admitted and answered 404. + if (registry.getEnvironmentScopedItem(request.type, request.name, bundleMemberDiscriminator(request.type, request.member)) !== undefined) { candidates.push({ kind: 'environment' }); } // No holder anywhere: nothing to name, and the read answers its own miss. @@ -491,7 +498,15 @@ export function exactQualifierItem( address.registryHolders.map((holder) => ({ kind: 'package' as const, packageId: holder }))); } const [holder] = address.registryHolders; - return holder === undefined ? undefined : registry.getPackageScopedItem(request.type, request.name, holder, member); + // [#34682] The admitted holder's item AT the addressed member: the named + // one, else the canonical one (the selector's contract: naming no member + // is naming the canonical value). Asked with the raw selector, a read + // naming no member got the holder's FIRST member when it ships no + // canonical one, while `?member=` answered nothing. `undefined` + // for a type with no members, which reads as before. + return holder === undefined + ? undefined + : registry.getPackageScopedItem(request.type, request.name, holder, bundleMemberDiscriminator(request.type, member)); } /** diff --git a/packages/metadata-protocol/src/protocol-meta-item-reads.ts b/packages/metadata-protocol/src/protocol-meta-item-reads.ts index b9768ae625d..e45ab949c25 100644 --- a/packages/metadata-protocol/src/protocol-meta-item-reads.ts +++ b/packages/metadata-protocol/src/protocol-meta-item-reads.ts @@ -1363,9 +1363,12 @@ export async function readMetaItem(host: Pick` and `/published` answer it. // [ADR-0150 D3/D5] Whether the answer below is the code layers' (the // artifact lookup and the registry), judged at the end of step 3. const reachedCodeLayers = item === undefined; @@ -1404,7 +1407,10 @@ export async function readMetaItem(host: Pick` does, and that bundle has no layer of it. const runtimeOnly = (item: unknown): unknown => item !== undefined && isTenantAuthored(item) ? undefined : item; const namedMemberOnly = (item: unknown): unknown => - member !== undefined && item !== undefined && !isWantedMember(item) ? undefined : item; + wantedMember !== undefined && item !== undefined && !isWantedMember(item) ? undefined : item; // [#32365] The bare action read's judged answer: the one enabled // holder's artifact (looked up at that holder), or the environment's // own item (a runtime registration; a stored row is the overlay layer). @@ -447,22 +449,8 @@ export async function getMetaItemLayered(host: Pick` is answered; FROM (`main` d947a93cf8): + * the base's first shipped member (`fr-FR`). */ static registryMemberServedAt(host: Pick, type: string, name: string, base: string, item: unknown, member: string | undefined): unknown { const shipping = host.shippingPackagesOf(type); if (!host.shipsBundleName(type, name, base, shipping)) return item; if (host.baseServesBundleMember(type, name, base, item, shipping)) return item; if (member !== undefined) return undefined; + const canonical = bundleMemberDiscriminator(type, undefined); const own = host.shippedArtifactsOf(type, name, shipping) - .find((artifact) => (artifact as { _packageId?: unknown } | null)?._packageId === base); + .find((artifact) => (artifact as { _packageId?: unknown } | null)?._packageId === base + && itemDiscriminator(canonicalMetaType(type), artifact) === canonical); if (own === undefined) return undefined; const candidate = host.engine.registry.getItem(type, name, base, itemDiscriminator(canonicalMetaType(type), own)); return candidate !== undefined && host.baseServesBundleMember(type, name, base, candidate, shipping) ? candidate : undefined; 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 746a314b987..cd07393ca65 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 @@ -146,8 +146,6 @@ const SITES: ReadonlyArray { + it('a dependent\'s added member (a locale its base does not ship): naming the dependent is admitted at that member; naming no member is naming en-US, which the dependent does not hold', async () => { tpl('en-US', A); tpl('fr-FR', D); // R2: the addition holds no name for a bare read, so a caller with no package reads alpha's. expect(registry.packageScopedHoldersOf('email_template', 'welcome')).toEqual([A]); expect(labelOf(await read(D, 'fr-FR'))).toBe('alphaaddon fr-FR'); - // The read that names no member (the layered door's disabled-package probe - // asks it) is the dependent's own holding, never `not-held-by-qualifier`. - expect(labelOf(await read(D))).toBe('alphaaddon fr-FR'); + // [#34682] The read that names no member addresses the canonical member + // (the member selector's declared sentence: naming the canonical value is + // the same request), so at the dependent's address it answers as + // `?member=en-US` does: absent, and never `not-held-by-qualifier`. FROM + // (`main` d947a93cf8): it served the dependent's `fr-FR` (the holder's + // first member, asked with no member). + const bare = await read(D); + const named = await read(D, 'en-US'); + const itemOf = (answer: unknown) => (answer as { item?: unknown } | undefined)?.item; + expect([itemOf(bare), itemOf(named)]).toEqual([undefined, undefined]); }); it.each(['app', 'page', 'dashboard', 'report', 'book', 'analytics_cube', 'tool', 'skill', 'agent'])( @@ -299,6 +306,89 @@ describe('[#31699] a bundled type (email_template): the exact qualifier reads a * registered; the `?package=` read for a name alpha and beta hold, naming gamma, * served alpha's. */ +// [#34682] Naming no member is naming the canonical member (the member +// selector's declared sentence in `packages/spec/src/api/protocol.zod.ts`: +// "naming the canonical value is the same request"). A base that ships its +// bundle in `fr-FR` only, beside another package that ships `en-US`, answers +// the read naming no member at its address exactly as it answers `?member=en-US`: +// absent, on the plain read and the layered read, at `?package=BASE` and +// through the face's re-address to the served holder (`servedHolderAddressOf`, +// beside a disabled holder). FROM (`main` d947a93cf8): the exact qualifier +// swapped the other package's `en-US` for the base's item asked with no +// member, so both reads served the base's `fr-FR` (200) where `?member=en-US` +// answered nothing. +describe('[#34682] a base shipping no canonical member: at its address, naming no member is naming en-US', () => { + const D = 'com.acme.alphaaddon'; + const tpl = (locale: string, packageId: string) => registry.registerItem('email_template', { + name: 'welcome', label: `${packageId.split('.').pop()} ${locale}`, locale, + subject: `${packageId.split('.').pop()} ${locale}`, bodyHtml: '

x

', + }, 'name' as never, packageId); + const itemOf = (answer: unknown) => (answer as { item?: { label?: string } } | undefined)?.item?.label ?? null; + const effectiveOf = (answer: unknown) => (answer as { effective?: { label?: string } | null } | undefined)?.effective?.label ?? null; + /** The plain read and the layered read at one address and member, as labels (`null`: nothing served). */ + const both = async (packageId: string | undefined, member?: string) => { + const request = { type: 'email_template', name: 'welcome', ...(packageId ? { packageId } : {}), ...(member ? { member } : {}) }; + return [itemOf(await protocol.getMetaItem(request as never)), effectiveOf(await protocol.getMetaItemLayered(request as never))]; + }; + + it.each([ + { holder: 'a dependent ships en-US', other: D }, + { holder: 'a disabled package ships en-US', other: B }, + ])('$holder: ?package=BASE naming no member answers as naming en-US (absent); the base\'s fr-FR named is the base\'s', async ({ other }) => { + if (other === D) registry.installPackage({ ...(manifest(D) as object), dependencies: { [A]: '^1.0.0' } } as never, 'code'); + tpl('fr-FR', A); + tpl('en-US', other); + if (other === B) registry.disablePackage(B); + expect({ bare: await both(A), named: await both(A, 'en-US') }).toEqual({ bare: [null, null], named: [null, null] }); + expect(await both(A, 'fr-FR')).toEqual(['alpha fr-FR', 'alpha fr-FR']); + }); + + // N6: two unrelated packages hold the name, each in a different + // non-canonical member. Naming no package, the read naming no member + // answers what naming `en-US` answers, on the plain and the layered read. + it('two unrelated holders in different non-canonical members: naming no package, naming no member answers as naming en-US', async () => { + tpl('fr-FR', A); + tpl('de-DE', B); + const outcome = async (member?: string) => { + try { + return await both(undefined, member); + } catch (error) { + const e = error as { status?: unknown; code?: unknown }; + return ['threw', e.status, e.code]; + } + }; + expect(await outcome()).toEqual(await outcome('en-US')); + }); + + it('through the re-address to the served holder (a disabled holder ships en-US): naming no package and no member answers as naming en-US', async () => { + tpl('fr-FR', A); + tpl('en-US', B); + registry.disablePackage(B); + expect({ bare: await both(undefined), named: await both(undefined, 'en-US') }).toEqual({ bare: [null, null], named: [null, null] }); + expect(await both(undefined, 'fr-FR')).toEqual(['alpha fr-FR', 'alpha fr-FR']); + }); +}); + +// [#34682, contract review S3 (b)] A type with no members: `/layers?package=C`, +// where C holds nothing and the only holder is disabled, reports no code layer. +// A disabled package holds no name (ruling 6917064203). FROM (`main` +// d947a93cf8, read from the tree): the member-less artifact lookup this PR +// removes put the disabled holder's artifact back as `code`. +describe('[#34682] /layers at a non-holder address beside a disabled sole holder, for a type with no members', () => { + it('doc: ?package=C, the only holder disabled: no code layer', async () => { + registry.registerItem('doc', { name: 'guide', label: 'beta doc' }, 'name' as never, B); + registry.disablePackage(B); + let answer: unknown; + try { + answer = await protocol.getMetaItemLayered({ type: 'doc', name: 'guide', packageId: C } as never); + } catch (error) { + const e = error as { status?: unknown; code?: unknown }; + answer = ['threw', e.status, e.code]; + } + expect(Array.isArray(answer) ? answer : ((answer as { code?: unknown } | undefined)?.code ?? null)).toBeNull(); + }); +}); + describe('fail closed — a registry with no package-scoped resolver refuses what it cannot judge', () => { const SLICE = new Set([ 'readsByPackage', 'packageScopedHoldersOf', 'getEnvironmentScopedItem', 'getPackageScopedItem', 'resolveItem', diff --git a/packages/objectql/src/protocol-bundle-member-base-address.test.ts b/packages/objectql/src/protocol-bundle-member-base-address.test.ts index a0d92ba0211..9930c8e7c08 100644 --- a/packages/objectql/src/protocol-bundle-member-base-address.test.ts +++ b/packages/objectql/src/protocol-bundle-member-base-address.test.ts @@ -289,14 +289,23 @@ describe('[#28802] a bundle member on the base\'s address, at member grain', () expect(await executing(protocol, NAME)).toEqual([BASE_EN, PAIR_FR]); }); - it('a read naming no member of a bundle with no canonical member serves the base\'s member, not a dependent\'s canonical one', async () => { + // [#34682] A read naming no member addresses the canonical member, the + // same request as naming `en-US` (the member selector's declared + // contract): the base ships none, so it answers 404 at the base's address + // and naming no package. FROM (`main` d947a93cf8): both served the base's + // `fr-FR` (the registry's bare-name read of a bundle with no canonical + // member) while `?member=en-US` answered 404. + it('a read naming no member of a bundle with no canonical member answers as naming the canonical member does, never a dependent\'s canonical one', async () => { const { protocol } = await boot(); await install(protocol, DEPENDENT); await publishInto(protocol, DEPENDENT, FR_ONLY, 'en-US', 'Dependent late EN'); - expect(await byName(protocol, FR_ONLY, undefined, BASE)).toEqual(['Base late FR', 'fr-FR', BASE]); - expect(await byName(protocol, FR_ONLY)).toEqual(['Base late FR', 'fr-FR', BASE]); + expect(await byName(protocol, FR_ONLY, undefined, BASE)).toBe(404); + expect(await byName(protocol, FR_ONLY)).toBe(404); expect(await byName(protocol, FR_ONLY, 'en-US', BASE)).toBe(404); + expect(await byName(protocol, FR_ONLY, 'en-US')).toBe(404); + // The base's own member, named, is the base's. + expect(await byName(protocol, FR_ONLY, 'fr-FR')).toEqual(['Base late FR', 'fr-FR', BASE]); expect(await byName(protocol, FR_ONLY, 'en-US', DEPENDENT)).toEqual(['Dependent late EN', 'en-US', DEPENDENT]); }); diff --git a/packages/runtime/src/domains/meta-bundle-member-one-request.test.ts b/packages/runtime/src/domains/meta-bundle-member-one-request.test.ts new file mode 100644 index 00000000000..32e143a7c95 --- /dev/null +++ b/packages/runtime/src/domains/meta-bundle-member-one-request.test.ts @@ -0,0 +1,256 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. +// +// [#34682] The read naming no bundle member and the read naming the canonical +// member (`?member=en-US`) are one request (`resolveBundleMemberSelector`; the +// spec's selector sentence: "Absent, the canonical member (`en-US` for +// `email_template`) […]; naming the canonical value is the same request"), and +// `/published` reads that same canonical member. Each pair gets one answer, on +// every by-name door (the plain read, `/layers`, the execution read), before +// and after a restart, with a type that has no members as the CONTROL. +// +// FROM (`main` d947a93cf8), a bundle with no canonical member: +// - shipped in `fr-FR` only: the plain read and `/layers` served the `fr-FR` +// member (200) and the execution read returned it, naming no package and +// naming the shipping package (`?package=`), while `?member=en-US` and +// `/published` answered 404; +// - stored in `fr-FR` only (a package-less row): 404 before a restart, then +// 200 with the `fr-FR` row after it (the hydrated bare slot), while +// `?member=en-US` and `/published` answered 404 on both sides. +// Driven on a booted kernel through `HttpDispatcher.dispatch`. + +import { afterAll, describe, expect, it } from 'vitest'; +import { mkdtempSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { ObjectKernel, type Plugin, type PluginContext } from '@objectstack/core'; +import { MetadataPlugin } from '@objectstack/metadata'; +import { ObjectQL, ObjectQLPlugin } from '@objectstack/objectql'; +import { SqlDriver } from '@objectstack/driver-sql'; +import { resolveThrownHttpError } from '@objectstack/types'; +import { HttpDispatcher } from '../http-dispatcher.js'; + +const CALLER = { userId: 'u_member_one_request', tenantId: 'org_qa', isSystem: true, positions: [], permissions: [], systemPermissions: ['manage_metadata'] }; +const A = 'com.qa.memberbase'; +const CANONICAL = 'en-US'; + +interface ManifestDoor { register(artifact: unknown): Promise | void } +interface Booted { kernel: ObjectKernel; engine: ObjectQL; root: string } +interface Answer { status?: number; body: any } + +const template = (locale: string, subject: string) => ({ label: subject, locale, subject, bodyHtml: `

${subject}

` }); +const MANIFEST = { + id: A, name: 'memberbase', namespace: 'memberbase', version: '1.0.0', type: 'app', objects: [], + emailTemplates: [ + { name: 'ship_fr_mail', ...template('fr-FR', 'ship fr') }, + { name: 'mixed_fr_mail', ...template('fr-FR', 'ship fr') }, + { name: 'ship_both_mail', ...template('en-US', 'ship en') }, + { name: 'ship_both_mail', ...template('fr-FR', 'ship fr') }, + ], + dashboards: [{ name: 'ship_dash', label: 'ship dash', widgets: [] }], +}; + +// Package-less rows of the environment, as a save of each member stores them: +// a non-canonical member carries its `bundle_member` key column. +const ROWS: readonly [type: string, name: string, member: string | null, body: Record][] = [ + ['email_template', 'row_fr_mail', 'fr-FR', template('fr-FR', 'row fr')], + // The canonical member stored over a package that ships only `fr-FR`. + ['email_template', 'mixed_fr_mail', null, template('en-US', 'row en')], + ['email_template', 'row_both_mail', null, template('en-US', 'row en')], + ['email_template', 'row_both_mail', 'fr-FR', template('fr-FR', 'row fr')], + ['dashboard', 'row_dash', null, { label: 'row dash', widgets: [] }], +]; + +async function boot(file: string): Promise { + const kernel = new ObjectKernel({ logger: { level: 'silent' }, gracefulShutdown: false }); + const root = mkdtempSync(join(tmpdir(), 'os-meta-member-one-request-')); + 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.member-one-request-host', + dependencies: ['com.objectstack.engine.objectql'], + init: async (ctx: PluginContext) => { await ctx.getService('manifest').register(MANIFEST); }, + }; + await kernel.use(host); + await kernel.bootstrap(); + await engine.syncSchemas(); + return { kernel, engine, root }; +} + +async function get(kernel: ObjectKernel, path: string, query: Record = {}): Promise { + const dispatcher = new HttpDispatcher(kernel as any, undefined, { enforceProjectMembership: false }); + (dispatcher as any).timedResolveExecutionContext = async () => ({ ...CALLER }); + const raw = new Request(`http://localhost/api/v1${path}`, { method: 'GET' }); + try { + const out: any = await dispatcher.dispatch('GET', path, {}, query, { request: raw } as any, '/api/v1'); + return { status: out.response?.status as number | undefined, body: out.response?.body }; + } catch (e) { + const thrown = resolveThrownHttpError(e); + return { status: thrown.status, body: { error: { code: thrown.code, message: String((e as Error)?.message ?? '') } } }; + } +} + +const unwrap = (body: any) => body?.data ?? body; +const codeOf = (body: any): unknown => (body?.error && typeof body.error === 'object' ? body.error.code : body?.code); +/** A refusal or a miss as `[status, code]`; a served document as `[status, its identity and content]`. */ +const shape = (answer: Answer, pick: (served: any) => unknown): unknown[] => + answer.status === 200 ? [200, pick(unwrap(answer.body))] : [answer.status, codeOf(answer.body)]; +const doc = (d: any) => (d == null ? d : { name: d.name, locale: d.locale, subject: d.subject, label: d.label }); + +async function doors(b: Booted, type: string, name: string, member?: string, pkg?: string) { + const query: Record = { ...(member === undefined ? {} : { member }), ...(pkg === undefined ? {} : { package: pkg }) }; + const protocol = b.kernel.getService('protocol') as any; + let executed: unknown; + try { + executed = doc((await protocol.getMetaItemForExecution({ + type, name, ...(member === undefined ? {} : { member }), ...(pkg === undefined ? {} : { packageId: pkg }), + }))?.item ?? null); + } catch (e) { + executed = ['threw', (e as { status?: unknown }).status, (e as { code?: unknown }).code]; + } + const read = await get(b.kernel, `/meta/${type}/${name}`, query); + const layers = await get(b.kernel, `/meta/${type}/${name}/layers`, query); + return { + read: shape(read, (u) => doc(u?.item)), + // The whole served envelope, so "one request" is pinned on the body and not on a picked field. + readBody: read.status === 200 ? unwrap(read.body) : undefined, + layers: shape(layers, (u) => [doc(u?.effective), doc(u?.code), doc(u?.overlay)]), + executed, + }; +} + +describe('[#34682] naming no member and naming the canonical member are one request, and /published reads that member', () => { + const roots: string[] = []; + afterAll(() => { for (const dir of roots) rmSync(dir, { recursive: true, force: true }); }); + + it('each pair answers alike on a bundled type, before and after a restart; a type with no members is the CONTROL', async () => { + const dir = mkdtempSync(join(tmpdir(), 'os-meta-member-one-request-db-')); + roots.push(dir); + const file = join(dir, 'meta.sqlite'); + + const observe = async (b: Booted, leg: string) => { + for (const [name, served] of [ + // No canonical member, shipped or stored: absent on every door. + ['ship_fr_mail', undefined], + ['row_fr_mail', undefined], + // POSITIVE: the canonical member, shipped or stored, is served on every door. + ['ship_both_mail', { name: 'ship_both_mail', locale: 'en-US', subject: 'ship en', label: 'ship en' }], + ['row_both_mail', { name: 'row_both_mail', locale: 'en-US', subject: 'row en', label: 'row en' }], + ['mixed_fr_mail', { name: 'mixed_fr_mail', locale: 'en-US', subject: 'row en', label: 'row en' }], + ] as const) { + const bare = await doors(b, 'email_template', name); + const named = await doors(b, 'email_template', name, CANONICAL); + // Pair 2: `?member=en-US` is the read naming no member, status and body. + expect({ leg, name, ...named }, JSON.stringify(named)).toEqual({ leg, name, ...bare }); + if (served === undefined) { + expect({ leg, name, read: bare.read, layers: bare.layers, executed: bare.executed }).toEqual({ + leg, name, read: [404, 'RESOURCE_NOT_FOUND'], layers: [404, 'RESOURCE_NOT_FOUND'], executed: null, + }); + } else { + expect({ leg, name, read: bare.read, effective: (bare.layers[1] as unknown[] | undefined)?.[0], executed: bare.executed }) + .toEqual({ leg, name, read: [200, served], effective: served, executed: served }); + } + // The preserved half: the `fr-FR` member, named, is served as before. + const fr = await doors(b, 'email_template', name, 'fr-FR'); + const frSubject = name.startsWith('ship_') || name === 'mixed_fr_mail' ? 'ship fr' : 'row fr'; + expect([leg, name, fr.read]).toEqual([leg, name, [200, { name, locale: 'fr-FR', subject: frSubject, label: frSubject }]]); + // …and at the shipping package's own address (`?package=`), for a shipped bundle. + if (name.startsWith('ship_')) { + const atBase = await doors(b, 'email_template', name, undefined, A); + const atBaseNamed = await doors(b, 'email_template', name, CANONICAL, A); + expect({ leg, name, ...atBaseNamed }, JSON.stringify(atBaseNamed)).toEqual({ leg, name, ...atBase }); + expect({ leg, name, read: atBase.read, executed: atBase.executed }) + .toEqual({ leg, name, read: served === undefined ? [404, 'RESOURCE_NOT_FOUND'] : [200, served], executed: served ?? null }); + } + // The canonical member stored over a package that ships only + // `fr-FR`: `/layers` naming no member reports no code layer, + // as `/layers?member=en-US` does. FROM (`main` d947a93cf8): + // `code` was the shipped `fr-FR` body. + if (name === 'mixed_fr_mail') { + expect([leg, name, bare.layers]).toEqual([leg, name, [200, [served, null, served]]]); + } + // At an address that holds nothing of a name the environment + // stores only in `fr-FR` (`?package=A`): naming no member is + // naming `en-US`, admitted and absent on the plain read, `/layers` + // and the execution read. FROM (`main` d947a93cf8): after a + // restart it was refused 409 naming the environment + // (`not-held-by-qualifier`), the hydrated `fr-FR` counted. + if (name === 'row_fr_mail') { + const atOther = await doors(b, 'email_template', name, undefined, A); + const atOtherNamed = await doors(b, 'email_template', name, CANONICAL, A); + expect({ leg, name, ...atOtherNamed }, JSON.stringify(atOtherNamed)).toEqual({ leg, name, ...atOther }); + expect({ leg, name, read: atOther.read, layers: atOther.layers, executed: atOther.executed }).toEqual({ + leg, name, read: [404, 'RESOURCE_NOT_FOUND'], layers: [404, 'RESOURCE_NOT_FOUND'], executed: null, + }); + } + // `/published` takes no member selector: it reads the canonical member alone. + const named400 = await get(b.kernel, `/meta/email_template/${name}/published`, { member: CANONICAL }); + expect([leg, name, named400.status, codeOf(named400.body)]).toEqual([leg, name, 400, 'VALIDATION_ERROR']); + } + + // Pair 1: `/published` answers what the plain read answers wherever + // the canonical member is published (a stored row) or nothing is. + for (const [name, served] of [ + ['ship_fr_mail', undefined], + ['row_fr_mail', undefined], + ['row_both_mail', { name: 'row_both_mail', locale: 'en-US', subject: 'row en', label: 'row en' }], + ] as const) { + const published = shape(await get(b.kernel, `/meta/email_template/${name}/published`), doc); + const read = shape(await get(b.kernel, `/meta/email_template/${name}`), (u) => doc(u?.item)); + expect({ leg, name, published, read }).toEqual({ + leg, name, + published: served === undefined ? [404, 'RESOURCE_NOT_FOUND'] : [200, served], + read: served === undefined ? [404, 'RESOURCE_NOT_FOUND'] : [200, served], + }); + } + + // CONTROL, a type with no members: the selector is refused on the + // plain read, `/layers` and the execution read (located 400), and a + // stored row is served alike by the plain read and `/published`. + const control = await doors(b, 'dashboard', 'row_dash', CANONICAL); + expect({ leg, read: control.read, layers: control.layers, executed: control.executed }).toEqual({ + leg, read: [400, 'VALIDATION_ERROR'], layers: [400, 'VALIDATION_ERROR'], executed: ['threw', 400, 'VALIDATION_ERROR'], + }); + const rowDash = { name: 'row_dash', locale: undefined, subject: undefined, label: 'row dash' }; + expect({ + leg, + read: shape(await get(b.kernel, '/meta/dashboard/row_dash'), (u) => doc(u?.item)), + published: shape(await get(b.kernel, '/meta/dashboard/row_dash/published'), doc), + }).toEqual({ leg, read: [200, rowDash], published: [200, rowDash] }); + + // Not the member selector's: an item only code ships has no published + // document, bundled or not, so `/published` answers 404 beside the + // plain read's 200 for the CONTROL type exactly as for a bundle. + for (const [type, name, label] of [['dashboard', 'ship_dash', 'ship dash'], ['email_template', 'ship_both_mail', 'ship en']] as const) { + expect({ + leg, type, + read: (shape(await get(b.kernel, `/meta/${type}/${name}`), (u) => u?.item?.label)), + published: shape(await get(b.kernel, `/meta/${type}/${name}/published`), (d) => d?.label), + }).toEqual({ leg, type, read: [200, label], published: [404, 'RESOURCE_NOT_FOUND'] }); + } + }; + + let booted = await boot(file); + try { + for (const [type, name, member, body] of ROWS) { + await booted.engine.insert('sys_metadata', { + type, name, namespace: 'default', package_id: null, scope: 'platform', strategy: 'merge', state: 'active', version: 1, + ...(member === null ? {} : { bundle_member: member }), + metadata: JSON.stringify({ name, ...body }), + }, { context: { isSystem: true } } as any); + } + await observe(booted, 'before a restart'); + } finally { + await booted.kernel.shutdown(); + roots.push(booted.root); + } + booted = await boot(file); + try { + await observe(booted, 'after a restart'); + } finally { + await booted.kernel.shutdown(); + roots.push(booted.root); + } + }, 120_000); +}); 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 58f31f96488..cd5b6b6f86f 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 @@ -864,10 +864,8 @@ describe('[#32607 M1, M2] the no-disabled arm judges holders at the rows\' grain // row as on `main`, in both save orders, before and after a reboot. FROM // (head b959edb979, arm unguarded): the read naming no member, `/published`, // `/layers` and the execution face answered 404 before a reboot, and the - // plain read and the execution face served A's `fr-FR` after it. No - // shipped CONTROL is pinned: with A shipping only `fr-FR`, the read naming - // no member serves A's `fr-FR`, and `/published` and `/layers` move across - // a reboot (200 then 404), so there is no one shipped answer to align with. + // plain read and the execution face served A's `fr-FR` after it. The + // shipped mirror is pinned below (cell 1). it.each([ { order: 'A saved first', saved: [A, D] }, { order: 'D saved first', saved: [D, A] }, @@ -934,10 +932,14 @@ describe('[#32607 M1, M2] the no-disabled arm judges holders at the rows\' grain executedNamed: (await protocol.getMetaItemForExecution({ type: 'email_template', name: 'shipfrmail', member: 'en-US' }))?.item?.subject ?? null, }; }; - // The CONTROL, read on this boot before D's row is stored. + // The CONTROL, read on this boot before D's row is stored. [#34682] + // Naming no member is naming `en-US`, which A does not ship: every + // door answers as `?member=en-US` does. FROM (`main` d947a93cf8): the + // plain read and `/layers` served A's `fr-FR` (200) and the execution + // read returned it. const control = { - read: [200, 'base fr'], named: [404, 'RESOURCE_NOT_FOUND'], layers: [200, ['base fr', null]], layersNamed: [404, 'RESOURCE_NOT_FOUND'], - published: [404, 'RESOURCE_NOT_FOUND'], executed: 'base fr', executedNamed: null, + read: [404, 'RESOURCE_NOT_FOUND'], named: [404, 'RESOURCE_NOT_FOUND'], layers: [404, 'RESOURCE_NOT_FOUND'], layersNamed: [404, 'RESOURCE_NOT_FOUND'], + published: [404, 'RESOURCE_NOT_FOUND'], executed: null, executedNamed: null, }; const own = async (b: Booted, leg: string) => { const r = await get(b.kernel, '/meta/email_template/shipfrmail', { member: 'en-US', package: D }); diff --git a/packages/spec/src/migrations/entries/semantic/18.bundle-without-canonical-member-bare-read-absent.ts b/packages/spec/src/migrations/entries/semantic/18.bundle-without-canonical-member-bare-read-absent.ts new file mode 100644 index 00000000000..92bde78175d --- /dev/null +++ b/packages/spec/src/migrations/entries/semantic/18.bundle-without-canonical-member-bare-read-absent.ts @@ -0,0 +1,47 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +import type { SemanticMigration } from '../../types.js'; + +// A narrowing of the by-name reads naming no bundle member, not of any key: +// #34682 aligns them to the member selector's declared sentence (absent means +// the canonical member; naming the canonical value is the same request; a +// member with nothing stored or shipped answers as an absent item). For a +// bundle that ships or stores no canonical member, the plain read, the layered +// read and the execution read naming no member served another member (the +// registry's bare-name read, the base-address fallback and the exact +// qualifier each answered such an address with its first member), +// while ?member=CANONICAL and /published answered 404; a bundle stored only as +// a non-canonical row moved from 404 to 200 across a restart. No key is added, +// removed or re-shaped and no stored row changes shape, so no tombstone and no +// D2 conversion. No relevance question: the move turns on which members a +// name's holders ship and store, which no question over one stack can name. +export const entry: SemanticMigration = { + id: 'bundle-without-canonical-member-bare-read-absent', + surface: + 'GET /api/v1/meta/TYPE/NAME and GET /api/v1/meta/TYPE/NAME/layers naming no bundle member, and the execution read ' + + 'naming none, for a bundled metadata name (an email_template) whose canonical member (en-US) the address does ' + + 'not hold. Three arrangements moved from serving another member of the bundle (status 200) to 404: (1) no layer ' + + 'ships or stores the canonical member, naming no package or naming a holder with ?package= (a bundle stored only ' + + 'as a non-canonical row answered 404 before a restart and served that row after one); (2) a base ships only ' + + 'another member and a dependent that declares it stores the canonical member, naming no package (the base\'s ' + + 'other member was served); (3) ?package=X where X ships no canonical member while another package ships it (a ' + + 'dependent, or a disabled holder, which the read naming no package re-addresses to X): X\'s other member was ' + + 'served. Two more answers move with them: /layers naming no member, where the canonical member is stored over a ' + + 'package that ships only another member, reports code null where it reported that member\'s body (status and ' + + 'effective do not move); and ?package=X naming no member, where X does not hold the name and the environment ' + + 'stores it only in a non-canonical member, answers 404 where it was refused 409 after a restart. ' + + '?member=CANONICAL, a named non-canonical member and /published do not move', + replacement: + 'Name the member: GET /api/v1/meta/TYPE/NAME?member=MEMBER (for example ?member=fr-FR), with ?package= where the ' + + 'address needs it, serves that member, as before.', + reason: + 'A read naming no member addresses the canonical member, and naming the canonical value is the same request: the ' + + 'member selector declares this on every by-name read. An address that holds no canonical member therefore ' + + 'answers that read as an absent item, as ?member=CANONICAL and /published already answered it, before and after ' + + 'a restart. There is no mechanical rewrite: which member a caller meant is a judgement about that caller.', + acceptanceCriteria: + 'For every bundled name an address holds without its canonical member: every caller that reads it by name ' + + 'without a member names the member it wants with ?member=MEMBER (the execution read with its member option) ' + + 'instead; at that address the read naming no member, /layers naming none and the execution read naming none ' + + 'answer 404 (null), the same as ?member=CANONICAL and /published, before and after a restart.', +}; diff --git a/packages/spec/src/migrations/entries/semantic/18.shipped-base-dependent-canonical-row-not-published.ts b/packages/spec/src/migrations/entries/semantic/18.shipped-base-dependent-canonical-row-not-published.ts index ead7abae2da..ae680be671f 100644 --- a/packages/spec/src/migrations/entries/semantic/18.shipped-base-dependent-canonical-row-not-published.ts +++ b/packages/spec/src/migrations/entries/semantic/18.shipped-base-dependent-canonical-row-not-published.ts @@ -21,7 +21,8 @@ export const entry: SemanticMigration = { 'GET /api/v1/meta/TYPE/NAME/published, naming no package, for a bundled metadata name a base package ships ' + 'only in members other than the canonical one, where a dependent package (one that declares the base) stores ' + 'a row of the canonical member. Before a restart it served the dependent\'s row; after one it answered 404. ' - + 'The plain read, ?member=CANONICAL and the execution read do not move', + + '?member=CANONICAL does not move. The plain read and the execution read naming no member served the base\'s ' + + 'other member there and now answer 404 too: that move is registered as bundle-without-canonical-member-bare-read-absent', replacement: 'Name the dependent: GET /api/v1/meta/TYPE/NAME?package=DEPENDENT&member=CANONICAL serves its row, as before.', reason: