From 3c947dc0af3bbc365f980dfbb5062b5842095146 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 11:57:23 +0000 Subject: [PATCH 1/4] fix(metadata-protocol): an action's artifact envelope is grafted only at the served item's own owner key ADR-0150 D4 keys an action by (package, object, name). The by-name read looked the served item's artifact envelope up by name alone, so a package-less action bound to one object wore the envelope of the one package that binds the name to another object. Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ Co-Authored-By: Claude --- .../src/protocol-artifact-lookup.ts | 45 +++++++++++++++++++ .../src/protocol-meta-item-reads.ts | 17 ++++++- packages/metadata-protocol/src/protocol.ts | 16 +++++-- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/packages/metadata-protocol/src/protocol-artifact-lookup.ts b/packages/metadata-protocol/src/protocol-artifact-lookup.ts index ddb7e0ccf89..288b99c4f89 100644 --- a/packages/metadata-protocol/src/protocol-artifact-lookup.ts +++ b/packages/metadata-protocol/src/protocol-artifact-lookup.ts @@ -37,8 +37,13 @@ * What the bodies read at module level they import from where `protocol.ts` * imported it; `ShippingPackages` is `protocol.ts`'s own exported type. * Nothing re-exports this module, so the published surface does not move. + * + * [ADR-0150 D4, #33456] Since the move, `lookupArtifactItem` takes one more + * argument, an action's owner key: the by-name body is + * `lookupArtifactItemByName`, unedited, and the owner-key rule wraps it. */ +import { boundObjectOf } from '@objectstack/metadata/stored-item-package'; import { PLURAL_TO_SINGULAR } from '@objectstack/spec/shared'; import { resolveArtifactLockLayer, type ItemAddress } from './item-lock.js'; import type { ObjectStackProtocolImplementation, ShippingPackages } from './protocol.js'; @@ -57,8 +62,48 @@ export interface ArtifactLookupHost { shippedArtifactsOf: ObjectStackProtocolImplementation['shippedArtifactsOf']; } +/** + * [ADR-0150 D4, #33456] The owner key an `action` artifact must be bound to + * for {@link lookupArtifactItem} to answer it: the object of the item the + * envelope is for, `undefined` for an object-less one. + */ +export interface ArtifactOwnerKey { + readonly object: string | undefined; +} + /** The body of `ObjectStackProtocolImplementation#lookupArtifactItem`; the method's docblock there is its contract. */ export function lookupArtifactItem( + this: Pick, + type: string, name: string, currentPackageId?: string, member?: string, owner?: ArtifactOwnerKey, +): unknown { + const artifact = lookupArtifactItemByName.call(this, type, name, currentPackageId, member); + const singular = PLURAL_TO_SINGULAR[type] ?? type; + if (owner === undefined || singular !== 'action') return artifact; + // [ADR-0150 D4, #33456] An action is keyed `(package, object, name)`: an + // artifact bound to another owner key is another item, and lends this one + // no envelope. A package-less item's holder is the ONE package whose own + // artifact is bound to the item's owner key; none, or two, and no + // artifact answers (an ambiguity is not guessed). + if (artifact !== undefined && boundObjectOf(artifact) === owner.object) return artifact; + if (currentPackageId) return undefined; + const registry = (this.engine as any)?.registry; + if (typeof registry?.packageScopedHoldersOf !== 'function') return undefined; + let holders: unknown; + try { + holders = registry.packageScopedHoldersOf(singular, name, null, { includeDisabledPackages: true }); + } catch { + return undefined; + } + const owners: unknown[] = []; + for (const packageId of new Set(Array.isArray(holders) ? holders : [])) { + const own = lookupArtifactItemByName.call(this, type, name, packageId, member) as { _packageId?: unknown } | undefined; + if (own?._packageId === packageId && boundObjectOf(own) === owner.object) owners.push(own); + } + return owners.length === 1 ? owners[0] : undefined; +} + +/** The artifact {@link lookupArtifactItem} answers by name alone, as it did before #33456. */ +function lookupArtifactItemByName( this: Pick, type: string, name: string, currentPackageId?: string, member?: string, ): unknown { diff --git a/packages/metadata-protocol/src/protocol-meta-item-reads.ts b/packages/metadata-protocol/src/protocol-meta-item-reads.ts index ca66612b20d..f9892f6b760 100644 --- a/packages/metadata-protocol/src/protocol-meta-item-reads.ts +++ b/packages/metadata-protocol/src/protocol-meta-item-reads.ts @@ -119,6 +119,16 @@ function actionObjectOf(item: unknown): string | undefined { return undefined; } +/** + * [ADR-0150 D4, #33456] The owner key a served `action` item's artifact + * envelope is looked up at (`lookupArtifactItem`'s `owner`): the object the + * item binds its name to, `undefined` for an object-less one. `undefined` + * (look up by name) for every other type, and for no item. + */ +function artifactOwnerKeyOf(type: string, item: unknown): { object: string | undefined } | undefined { + return type === 'action' && item !== null && typeof item === 'object' ? { object: boundObjectOf(item) } : undefined; +} + /** * Is `packageId` a package the registry has switched off? A disabled package * holds nothing to a by-name action read (#25308), as @@ -1236,12 +1246,17 @@ export async function readMetaItem(host: Pick host.lookupArtifactItem( - request.type, request.name, packageId, bundleMemberOf(request.type, item), + request.type, request.name, packageId, bundleMemberOf(request.type, item), artifactOwner, )) : host.lookupArtifactItem( request.type, request.name, envelopePackageId(request.packageId, item), bundleMemberOf(request.type, item), + artifactOwner, ); // [#21738] The lock is the one item-lock resolution's, over the layers // this read resolved from its address: [#21803] every installed diff --git a/packages/metadata-protocol/src/protocol.ts b/packages/metadata-protocol/src/protocol.ts index 0df42f12dab..185ea229675 100644 --- a/packages/metadata-protocol/src/protocol.ts +++ b/packages/metadata-protocol/src/protocol.ts @@ -322,7 +322,9 @@ import { // [#25467 cut 14] The publish batch's seed apply and its own pending declarations. import { applySeedBodiesOnHost, collectBatchPendingDeclarations } from './protocol-publish-batch.js'; // [#25467 cut 15] The artifact registry's lookups: an item's artifact, the packages that ship a type, and the lock's artifact layer. -import { artifactLockLayerAt, lookupArtifactItem, shippedArtifactsOf, shippingPackagesOf } from './protocol-artifact-lookup.js'; +import { + type ArtifactOwnerKey, artifactLockLayerAt, lookupArtifactItem, shippedArtifactsOf, shippingPackagesOf, +} from './protocol-artifact-lookup.js'; // [#25467 cut 16] The stored-overlay row reads: a type's active rows, their parsed entries, the names with a row of their own, and one item's rows. import { namesWithOwnStoredRow, readActiveOverlayRows, readItemActiveRows, storedOverlayEntries } from './protocol-stored-overlay-reads.js'; // [#25467 cut 17] The object-owner verdicts: which contributor kind a stored object row registers as, and handing an adopted object's name to its rebound row. @@ -7089,14 +7091,22 @@ export class ObjectStackProtocolImplementation implements * member whose artifact to answer, as its discriminator value; omitted, * the bundle's artifact canonical-first. A registry without the parameter * ignores it, so a caller that needs the member checks the answer. + * + * [ADR-0150 D4, #33456] `owner` — for an `action`, the owner key of the + * item the envelope is for: only an artifact bound to that object (or, + * `object` undefined, object-less) answers, since an action is keyed + * `(package, object, name)`. Naming no package, it is the one package + * whose own artifact is bound there, else none. Omitted, by name, as + * before. */ - private lookupArtifactItem(type: string, name: string, currentPackageId?: string, member?: string): unknown { + private lookupArtifactItem(type: string, name: string, currentPackageId?: string, member?: string, + owner?: ArtifactOwnerKey): unknown { // [#25467 cut 15] The body moved to `protocol-artifact-lookup.ts` (see `ArtifactLookupHost`). const readEngine = () => this.engine; const host: ThisParameterType = { get engine() { return readEngine(); }, }; - return lookupArtifactItem.call(host, type, name, currentPackageId, member); + return lookupArtifactItem.call(host, type, name, currentPackageId, member, owner); } /** From e39b70a48317a4a32c7400b46cd08f9990a65f54 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 12:08:11 +0000 Subject: [PATCH 2/4] test(metadata-protocol): pin the action artifact lookup at the served item's owner key, and record the moved call sites Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ Co-Authored-By: Claude --- ...l-artifact-lookup.action-owner-key.test.ts | 94 +++++++++++++++++++ ...ol.lookup-artifact-item-call-sites.test.ts | 16 ++-- 2 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 packages/metadata-protocol/src/protocol-artifact-lookup.action-owner-key.test.ts diff --git a/packages/metadata-protocol/src/protocol-artifact-lookup.action-owner-key.test.ts b/packages/metadata-protocol/src/protocol-artifact-lookup.action-owner-key.test.ts new file mode 100644 index 00000000000..a0136b2543c --- /dev/null +++ b/packages/metadata-protocol/src/protocol-artifact-lookup.action-owner-key.test.ts @@ -0,0 +1,94 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [ADR-0150 D4, #33456] The artifact an action's envelope is grafted from is + * the one bound to the served item's own owner key: an action is keyed + * `(package, object, name)`, so a package's artifact bound to another object + * is another item and lends none. Naming no package, the holder is the ONE + * package whose own artifact is bound there. + * + * BEFORE (the owner-key rule removed): every case marked FROM below answered + * the first composite by name, ALPHA's `account` artifact, whatever object + * the served item was bound to. + * + * The registry double keeps the two reads the lookup makes the way + * objectql's `SchemaRegistry` answers them: `getArtifactItem` prefers the + * named package's composite, else the first composite; `packageScopedHoldersOf` + * lists every package holding a composite of the name. + */ + +import { describe, expect, it } from 'vitest'; +import { lookupArtifactItem } from './protocol-artifact-lookup.js'; + +const A = 'com.acme.alpha'; +const B = 'com.acme.beta'; + +const action = (packageId: string, objectName: string | undefined) => ({ + name: 'approve', type: 'script', ...(objectName ? { objectName } : {}), + _packageId: packageId, _provenance: 'package', +}); + +/** The engine a lookup reads, its registry holding `items` as `:approve` composites. */ +const engineOf = (items: ReadonlyArray>) => { + const composites = new Map(items.map((item) => [`${item._packageId}:approve`, item] as const)); + return { + registry: { + getArtifactItem(_type: string, name: string, packageId?: string) { + if (name !== 'approve') return undefined; + return (packageId ? composites.get(`${packageId}:approve`) : undefined) ?? composites.values().next().value; + }, + packageScopedHoldersOf(_type: string, name: string) { + return name === 'approve' ? [...composites.values()].map((item) => item._packageId) : []; + }, + }, + }; +}; + +const lookup = ( + items: ReadonlyArray>, + args: { type?: string; packageId?: string; owner?: { object: string | undefined } }, +) => lookupArtifactItem.call({ engine: engineOf(items) as never }, args.type ?? 'action', 'approve', args.packageId, undefined, args.owner) as + ReturnType | undefined; + +const holderOf = (artifact: ReturnType | undefined) => + artifact === undefined ? undefined : `${artifact._packageId}@${artifact.objectName ?? 'object-less'}`; + +describe('[#33456] an action artifact lends its envelope only at the served item\'s own owner key (ADR-0150 D4)', () => { + it('mirror shape: beside ALPHA on `account`, a package-less item on `contact` wears no artifact (FROM ALPHA\'s)', () => { + expect(holderOf(lookup([action(A, 'account')], { owner: { object: 'contact' } }))).toBeUndefined(); + }); + + it('single-holder cell: beside BETA on `contact`, a package-less item on `account` wears no artifact (FROM BETA\'s)', () => { + expect(holderOf(lookup([action(B, 'contact')], { owner: { object: 'account' } }))).toBeUndefined(); + }); + + it('ALPHA on `account`, BETA on `contact`: a package-less item on `contact` wears BETA\'s, the one holder there (FROM ALPHA\'s, the first composite)', () => { + expect(holderOf(lookup([action(A, 'account'), action(B, 'contact')], { owner: { object: 'contact' } }))).toBe(`${B}@contact`); + }); + + it('object-less is its own owner key: ALPHA object-less lends nothing to an item on `contact`, nor ALPHA on `account` to an object-less item (FROM ALPHA\'s)', () => { + expect(holderOf(lookup([action(A, undefined)], { owner: { object: 'contact' } }))).toBeUndefined(); + expect(holderOf(lookup([action(A, 'account')], { owner: { object: undefined } }))).toBeUndefined(); + }); + + it('naming a package: that package\'s artifact only when it is bound to the owner key (FROM its artifact on another object)', () => { + expect(holderOf(lookup([action(A, 'account')], { packageId: A, owner: { object: 'contact' } }))).toBeUndefined(); + }); + + describe('CONTROLs (unchanged)', () => { + it('one package on one object, the item on that object (its customization): that package\'s artifact', () => { + expect(holderOf(lookup([action(A, 'account')], { owner: { object: 'account' } }))).toBe(`${A}@account`); + expect(holderOf(lookup([action(A, 'account')], { packageId: A, owner: { object: 'account' } }))).toBe(`${A}@account`); + expect(holderOf(lookup([action(A, undefined)], { owner: { object: undefined } }))).toBe(`${A}@object-less`); + }); + + it('two packages on one object: the first composite, as before (the bare read refuses that name before any envelope)', () => { + expect(holderOf(lookup([action(A, 'account'), action(B, 'account')], { owner: { object: 'account' } }))).toBe(`${A}@account`); + }); + + it('no owner key, or a type other than `action`: by name, as before', () => { + expect(holderOf(lookup([action(A, 'account')], {}))).toBe(`${A}@account`); + expect(holderOf(lookup([action(A, 'account')], { type: 'flow', owner: { object: 'contact' } }))).toBe(`${A}@account`); + }); + }); +}); 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 018d027a764..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 @@ -130,14 +130,14 @@ type Disposition = 'definition' | 'served-package envelope' | 'exact-address env * envelope never reaches an answer whose body came from another package. */ const SITES: ReadonlyArray = [ - { member: 'lookupArtifactItem', args: 'type: string, name: string, currentPackageId?: string, member?: string', noPackage: false, disposition: 'definition', - why: 'the definition itself: the class member\'s declared signature (its body delegates to the seam function below)' }, - { file: SEAM_FILE, member: 'lookupArtifactItem', args: 'this: Pick, type: string, name: string, currentPackageId?: string, member?: string', noPackage: false, disposition: 'definition', - why: '[#25467 cut 15] the definition\'s body, the function the class member delegates to' }, - { file: ITEM_READS_FILE, member: 'readMetaItem', args: 'request.type, request.name, envelopePackageId(request.packageId, item), bundleMemberOf(request.type, item)', noPackage: true, disposition: 'served-package envelope', - why: 'the by-name read\'s envelope: naming no package, the package of the item it serves (the list\'s rule); [#23074] at the served item\'s own bundle member' }, - { file: ITEM_READS_FILE, member: 'readMetaItem', args: 'request.type, request.name, packageId, bundleMemberOf(request.type, item)', noPackage: false, disposition: 'exact-address envelope', - why: '[#32501] the by-name read\'s envelope at an admitted exact address: the package the address reads, its own artifact only (`exactAddressArtifact` drops another package\'s), never the pair id\'s first-registered fallback' }, + { member: 'lookupArtifactItem', args: 'type: string, name: string, currentPackageId?: string, member?: string, owner?: ArtifactOwnerKey', noPackage: false, disposition: 'definition', + why: 'the definition itself: the class member\'s declared signature (its body delegates to the seam function below); [#33456] `owner`, an action\'s owner key' }, + { file: SEAM_FILE, member: 'lookupArtifactItem', args: 'this: Pick, type: string, name: string, currentPackageId?: string, member?: string, owner?: ArtifactOwnerKey', noPackage: false, disposition: 'definition', + why: '[#25467 cut 15] the definition\'s body, the function the class member delegates to; [#33456] an action\'s artifact only at `owner`\'s object, and naming no package, the one package bound there' }, + { file: ITEM_READS_FILE, member: 'readMetaItem', args: 'request.type, request.name, envelopePackageId(request.packageId, item), bundleMemberOf(request.type, item), artifactOwner', noPackage: true, disposition: 'served-package envelope', + why: 'the by-name read\'s envelope: naming no package, the package of the item it serves (the list\'s rule); [#23074] at the served item\'s own bundle member; [#33456] for an action, at its own owner key (ADR-0150 D4)' }, + { file: ITEM_READS_FILE, member: 'readMetaItem', args: 'request.type, request.name, packageId, bundleMemberOf(request.type, item), artifactOwner', noPackage: false, disposition: 'exact-address envelope', + why: '[#32501] the by-name read\'s envelope at an admitted exact address: the package the address reads, its own artifact only (`exactAddressArtifact` drops another package\'s), never the pair id\'s first-registered fallback; [#33456] for an action, at its own owner key' }, { file: LIST_READS_FILE, member: 'readFlattenedMetaItems', args: 'request.type, itemName, itemPackageId, bundleMemberOf(request.type, it)', noPackage: true, disposition: 'served-package envelope', why: 'the list\'s envelope: each item\'s own package, the rule the by-name read shares; [#23074] at each item\'s own bundle member' }, { file: ITEM_READS_FILE, member: 'readMetaItem', args: 'request.type, request.name, request.packageId', noPackage: true, disposition: 'no', From 4cbb6a900923318611207cb83e51e1c7b5ec6af4 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 12:19:39 +0000 Subject: [PATCH 3/4] test(rest, runtime): pin the package-less action row served as the environment's on both transports, both faces and its route Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ Co-Authored-By: Claude --- .../src/meta-qualified-item-route.test.ts | 92 ++++++++++++++++++ .../src/action-package-addressing.test.ts | 58 +++++++++++ .../domains/meta-qualified-item-read.test.ts | 97 +++++++++++++++++++ 3 files changed, 247 insertions(+) diff --git a/packages/rest/src/meta-qualified-item-route.test.ts b/packages/rest/src/meta-qualified-item-route.test.ts index 4d8a899bd96..bc4ad4e88ea 100644 --- a/packages/rest/src/meta-qualified-item-route.test.ts +++ b/packages/rest/src/meta-qualified-item-route.test.ts @@ -978,3 +978,95 @@ describe('#32501 — a pair address reads the package it names, never the first }); } }); + +/** + * [#33456, ADR-0150 D4] The bare action read's envelope names a package only + * for the item that package holds at its own `(object, name)`. An action is + * keyed `(package, object, name)`, so the environment's own item bound to an + * object no package binds the name to is served as the environment's, with no + * package's `_packageId` / `_provenance` grafted from a holder on another + * object. Both registration orders. + * + * BASE (main 1e65a389e1, the owner-key rule ablated in `lookupArtifactItem` + * and rebuilt): the mirror shape and the single-holder cell served the + * environment's item under the holder's `_packageId` and `_provenance: + * 'package'`. The CONTROLs read the same before and after. + */ +describe('#33456 — the bare action read grafts a holder\'s envelope only at its own (object, name)', () => { + let storedRows: any[] = []; + /** A fresh composition whose store answers `storedRows` for `sys_metadata`. */ + const compose = () => { + registry = new SchemaRegistry({ multiTenant: false }); + for (const id of [A, B]) { + registry.installPackage({ id, name: id, version: '1.0.0', type: 'app', namespace: id.split('.').pop()! } as never, 'code'); + } + storedRows = []; + const engine = { + registry, + find: vi.fn(async (object: string) => (object === 'sys_metadata' ? storedRows.map((row) => ({ ...row })) : [])), + findOne: vi.fn().mockResolvedValue(null), + insert: vi.fn(), + update: vi.fn(), + delete: vi.fn(), + count: vi.fn().mockResolvedValue(0), + aggregate: vi.fn().mockResolvedValue([]), + }; + const protocol = new ObjectStackProtocolImplementation(engine as never); + rest = new RestServer(mockServer() as any, protocol as any, { api: { requireAuth: false } } as any); + (rest as any).resolveExecCtx = async () => ({ isSystem: true }); + rest.registerRoutes(); + }; + beforeEach(compose); + const body = (label: string, objectName: string) => ({ + name: 'approve', label, type: 'script', objectName, target: 'approve', + body: { language: 'js', source: 'return 1;', capabilities: [] }, + }); + const shipAction = (packageId: string, objectName: string) => + registry.registerItem('action', body(packageId.split('.').pop()!, objectName), 'name' as never, packageId); + /** The environment's own item: the bare slot, and its active package-less row in the store. */ + const environmentAction = (objectName: string) => { + registry.registerItem('action', body('env', objectName), 'name' as never); + storedRows.push({ id: 'r-env', type: 'action', name: 'approve', package_id: null, state: 'active', metadata: JSON.stringify(body('env', objectName)) }); + }; + const bothOrders = (steps: Array<() => void>) => [steps, [...steps].reverse()]; + const envelopeOf = async (query: Record = {}) => { + const answer = await drive(ITEM, { type: 'action', name: 'approve' }, query); + const item = answer.body?.item ?? answer.body?.data?.item; + return [answer.status, item?.label, item?.objectName, item?._packageId, item?._provenance === 'package']; + }; + + for (const [shape, holder, held, other] of [ + ['mirror shape: A on `account`, the environment\'s item on `contact`', A, 'account', 'contact'], + ['single-holder cell: B on `contact`, the environment\'s item on `account`', B, 'contact', 'account'], + ] as const) { + it(`${shape}: the environment's item with no package's envelope (BASE: the holder's), and \`?package=\` the holder's own`, async () => { + for (const steps of bothOrders([() => shipAction(holder, held), () => environmentAction(other)])) { + compose(); + steps.forEach((step) => step()); + expect(await envelopeOf()).toEqual([200, 'env', other, undefined, false]); + expect(await envelopeOf({ package: holder })).toEqual([200, holder.split('.').pop(), held, holder, true]); + } + }); + } + + describe('CONTROLs (unchanged)', () => { + it('one package on one object with the environment\'s item on that object: its customization, under the package\'s envelope', async () => { + shipAction(A, 'account'); + environmentAction('account'); + expect(await envelopeOf()).toEqual([200, 'env', 'account', A, true]); + }); + + it('two packages on one object: refused, naming both', async () => { + shipAction(A, 'account'); + shipAction(B, 'account'); + const answer = await drive(ITEM, { type: 'action', name: 'approve' }); + expect([answer.status, codeOf(answer)], JSON.stringify(answer.body)).toEqual([409, 'RESOURCE_CONFLICT']); + expect(answer.body?.details?.candidates).toEqual([{ kind: 'package', packageId: A }, { kind: 'package', packageId: B }]); + }); + + it('no package-less row: the one holder\'s own item, under its envelope', async () => { + shipAction(A, 'account'); + expect(await envelopeOf()).toEqual([200, 'alpha', 'account', A, true]); + }); + }); +}); diff --git a/packages/runtime/src/action-package-addressing.test.ts b/packages/runtime/src/action-package-addressing.test.ts index adf0ef71b6d..5a5f548af69 100644 --- a/packages/runtime/src/action-package-addressing.test.ts +++ b/packages/runtime/src/action-package-addressing.test.ts @@ -1771,6 +1771,64 @@ describe('[#32681] /actions/account/approve judges and runs one item over the st }); } } + /** + * [#33456, ADR-0150 D4] …and that row's item is served as the + * environment's: the bare read's artifact envelope names a package only + * for the item that package holds at its own `(object, name)`, so the + * holder's `account` artifact lends nothing to the `contact` row (mirror) + * and BETA's `contact` artifact nothing to the `account` row (cell). On + * the execution face, the served face, and the declaration the row's + * route judges; the holder's exact address keeps its own envelope. + * + * BASE (main 1e65a389e1, the owner-key rule ablated in + * `lookupArtifactItem` and rebuilt): each of those three answered the + * package-less row under the holder's `_packageId` and `_provenance: + * 'package'`, while the route's `packageId` stayed unset and the + * environment's handler ran. + * + * Still not pinned: the holder's own route, which judges no declaration + * on this host before and after (the note above). The CONTROL, one + * package and the package-less row on one object, reads the same before + * and after: its customization, under the package's envelope. + */ + const envelopeOf = (item: any) => (item ? [item.label, item.objectName, item._packageId, item._provenance === 'package'] : item); + const stored = async (holder: string, held: string, other: string, order: 'the package-less save last' | 'the package-less save first') => { + const { rowEngine, rows } = writableEngine(); + const saves = [() => save(rowEngine, holder, held, holder === A ? 'alpha' : 'beta'), () => save(rowEngine, undefined, other, 'custom')]; + for (const step of order === 'the package-less save last' ? saves : [...saves].reverse()) await step(); + install(holder); + ship(holder, held); + engine.registerAction(held, 'approve', () => `ran ${holder}`, `app:${holder}`); + if (other !== held) engine.registerAction(other, 'approve', () => 'ran env'); + await boot(rowEngine, rows); + }; + for (const { shape, holder, held, label, other } of SHAPES) { + for (const order of ['the package-less save last', 'the package-less save first'] as const) { + it(`[#33456] ${shape} (${order}): the package-less row is served as the environment's on both faces and on its route, never under the holder's envelope`, async () => { + await stored(holder, held, other, order); + const environment = ['custom', other, undefined, false]; + expect(envelopeOf((await protocol.getMetaItemForExecution({ type: 'action', name: 'approve' }) as any)?.item)).toEqual(environment); + expect(envelopeOf((await protocol.getMetaItem({ type: 'action', name: 'approve' }) as any)?.item)).toEqual(environment); + const route = await routeTo(other); + expect([...envelopeOf(route.action), route.packageId]).toEqual([...environment, undefined]); + expect((await judgedAndRun(other)).run).toBe('ran env'); + // CONTROL: the holder's exact address keeps its own item and envelope. + expect(envelopeOf((await protocol.getMetaItemForExecution({ type: 'action', name: 'approve', packageId: holder }) as any)?.item)) + .toEqual([label, held, holder, true]); + }); + } + } + for (const order of ['the package-less save last', 'the package-less save first'] as const) { + it(`[#33456] CONTROL — A and the package-less row both on \`account\` (${order}): its customization, under A's envelope, unchanged`, async () => { + await stored(A, 'account', 'account', order); + // One row: the package-less save is A's `account` row; its content is the save made last. + const label = order === 'the package-less save last' ? 'custom' : 'alpha'; + for (const face of ['getMetaItemForExecution', 'getMetaItem'] as const) { + expect(envelopeOf(((await (protocol as any)[face]({ type: 'action', name: 'approve' })) as any)?.item), face).toEqual([label, 'account', A, true]); + } + expect(await judgedAndRun('account')).toEqual({ judged: label, refusal: undefined, degraded: undefined, run: `ran ${A}` }); + }); + } }); /** diff --git a/packages/runtime/src/domains/meta-qualified-item-read.test.ts b/packages/runtime/src/domains/meta-qualified-item-read.test.ts index 84b08a64d5a..576d57e41c2 100644 --- a/packages/runtime/src/domains/meta-qualified-item-read.test.ts +++ b/packages/runtime/src/domains/meta-qualified-item-read.test.ts @@ -1132,3 +1132,100 @@ describe('#32501 — a pair address reads the package it names, never the first }); } }); + +/** + * [#33456, ADR-0150 D4] The bare action read's envelope names a package only + * for the item that package holds at its own `(object, name)`. An action is + * keyed `(package, object, name)`, so the environment's own item bound to an + * object no package binds the name to is served as the environment's, with no + * package's `_packageId` / `_provenance` grafted from a holder on another + * object. Both registration orders. + * + * On the runtime dispatcher, the transport `RestServer`'s pin beside this one + * drives. BASE (main 1e65a389e1, the owner-key rule ablated in + * `lookupArtifactItem` and rebuilt): the mirror shape and the single-holder cell served the + * environment's item under the holder's `_packageId` and `_provenance: + * 'package'`. The CONTROLs read the same before and after. + */ +describe('#33456 — the bare action read grafts a holder\'s envelope only at its own (object, name)', () => { + let storedRows: any[] = []; + /** A fresh composition whose store answers `storedRows` for `sys_metadata`. */ + const compose = () => { + registry = new SchemaRegistry({ multiTenant: false }); + for (const id of [A, B]) { + registry.installPackage({ id, name: id, version: '1.0.0', type: 'app', namespace: id.split('.').pop()! } as never, 'code'); + } + storedRows = []; + const engine = { + registry, + find: vi.fn(async (object: string) => (object === 'sys_metadata' ? storedRows.map((row) => ({ ...row })) : [])), + findOne: vi.fn().mockResolvedValue(null), + insert: vi.fn(), + update: vi.fn(), + delete: vi.fn(), + count: vi.fn().mockResolvedValue(0), + aggregate: vi.fn().mockResolvedValue([]), + }; + const protocol = new ObjectStackProtocolImplementation(engine as never); + const services: Record = { protocol, objectql: engine }; + const resolve = (name: string) => services[name] ?? null; + dispatcher = new HttpDispatcher({ + getService: resolve, + getServiceAsync: async (name: string) => resolve(name), + context: { getService: resolve }, + } as never); + }; + beforeEach(compose); + const body = (label: string, objectName: string) => ({ + name: 'approve', label, type: 'script', objectName, target: 'approve', + body: { language: 'js', source: 'return 1;', capabilities: [] }, + }); + const shipAction = (packageId: string, objectName: string) => + registry.registerItem('action', body(packageId.split('.').pop()!, objectName), 'name' as never, packageId); + /** The environment's own item: the bare slot, and its active package-less row in the store. */ + const environmentAction = (objectName: string) => { + registry.registerItem('action', body('env', objectName), 'name' as never); + storedRows.push({ id: 'r-env', type: 'action', name: 'approve', package_id: null, state: 'active', metadata: JSON.stringify(body('env', objectName)) }); + }; + const bothOrders = (steps: Array<() => void>) => [steps, [...steps].reverse()]; + const envelopeOf = async (query: Record = {}) => { + const answer = await read('/action/approve', query); + const item = answer.body?.data?.item ?? answer.body?.item; + return [answer.status, item?.label, item?.objectName, item?._packageId, item?._provenance === 'package']; + }; + + for (const [shape, holder, held, other] of [ + ['mirror shape: A on `account`, the environment\'s item on `contact`', A, 'account', 'contact'], + ['single-holder cell: B on `contact`, the environment\'s item on `account`', B, 'contact', 'account'], + ] as const) { + it(`${shape}: the environment's item with no package's envelope (BASE: the holder's), and \`?package=\` the holder's own`, async () => { + for (const steps of bothOrders([() => shipAction(holder, held), () => environmentAction(other)])) { + compose(); + steps.forEach((step) => step()); + expect(await envelopeOf()).toEqual([200, 'env', other, undefined, false]); + expect(await envelopeOf({ package: holder })).toEqual([200, holder.split('.').pop(), held, holder, true]); + } + }); + } + + describe('CONTROLs (unchanged)', () => { + it('one package on one object with the environment\'s item on that object: its customization, under the package\'s envelope', async () => { + shipAction(A, 'account'); + environmentAction('account'); + expect(await envelopeOf()).toEqual([200, 'env', 'account', A, true]); + }); + + it('two packages on one object: refused, naming both', async () => { + shipAction(A, 'account'); + shipAction(B, 'account'); + const answer = await read('/action/approve'); + expect([answer.status, answer.body?.error?.code], JSON.stringify(answer.body)).toEqual([409, 'RESOURCE_CONFLICT']); + expect(answer.body?.error?.details?.candidates).toEqual([{ kind: 'package', packageId: A }, { kind: 'package', packageId: B }]); + }); + + it('no package-less row: the one holder\'s own item, under its envelope', async () => { + shipAction(A, 'account'); + expect(await envelopeOf()).toEqual([200, 'alpha', 'account', A, true]); + }); + }); +}); From d26eba6dc3ef22a1fdb7c342a252be8cce29551a Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 12:23:44 +0000 Subject: [PATCH 4/4] test(rest, runtime): pin the object-less package-less row beside a bound holder; add the changeset Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ Co-Authored-By: Claude --- .changeset/33456-action-envelope-owner-key.md | 17 +++++++++++++++++ .../rest/src/meta-qualified-item-route.test.ts | 7 ++++--- .../domains/meta-qualified-item-read.test.ts | 7 ++++--- 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 .changeset/33456-action-envelope-owner-key.md diff --git a/.changeset/33456-action-envelope-owner-key.md b/.changeset/33456-action-envelope-owner-key.md new file mode 100644 index 00000000000..e3aecce9ec4 --- /dev/null +++ b/.changeset/33456-action-envelope-owner-key.md @@ -0,0 +1,17 @@ +--- +'@objectstack/metadata-protocol': patch +--- + +fix(metadata-protocol): the by-name read of an action grafts a package's artifact envelope only onto the item that package holds at its own `(object, name)`, so the environment's own action on another object is served as the environment's (#33456) + +Clause-②: no +Metadata-protocol: none + +ADR-0150 D4 keys an action by `(package, object, name)`. The by-name read (`getMetaItem`, `getMetaItemForExecution`) looked the served item's artifact envelope up by name alone, so beside ONE package that binds an action name to one object, the environment's own package-less action of that name bound to another object (or object-less) was served carrying that package's `_packageId`, `_packageVersion` and `_provenance: 'package'`. + +What moves, with ALPHA and BETA two packages and `approve` the action name: + +- ALPHA binds `approve` to `account`, the environment's own row binds it to `contact` (or BETA binds it to `contact` alone and the row binds it to `account`): `GET /api/v1/meta/action/approve` on `RestServer` and the runtime dispatcher, `getMetaItemForExecution`, and the declaration `/actions/contact/approve` (`/actions/account/approve` in the second shape) judges, serve the environment's item with no package envelope, FROM the holder's `_packageId` and `_provenance: 'package'`. The route's `packageId` stays unset and the environment's handler runs, as before. +- The same with the row object-less beside ALPHA on `account`, or bound to `account` beside ALPHA object-less: no package envelope, FROM ALPHA's. + +Not changed: a package-less row on the object a package binds the name to (that package's customization, still under its envelope); a name two packages hold on one object (refused, naming both); a name one package holds with no package-less row; a read naming a package (`?package=`), which keeps that package's own item and envelope; every type other than `action`; stored rows (no migration). diff --git a/packages/rest/src/meta-qualified-item-route.test.ts b/packages/rest/src/meta-qualified-item-route.test.ts index bc4ad4e88ea..b794c6ec928 100644 --- a/packages/rest/src/meta-qualified-item-route.test.ts +++ b/packages/rest/src/meta-qualified-item-route.test.ts @@ -1017,14 +1017,14 @@ describe('#33456 — the bare action read grafts a holder\'s envelope only at it rest.registerRoutes(); }; beforeEach(compose); - const body = (label: string, objectName: string) => ({ - name: 'approve', label, type: 'script', objectName, target: 'approve', + const body = (label: string, objectName: string | undefined) => ({ + name: 'approve', label, type: 'script', ...(objectName ? { objectName } : {}), target: 'approve', body: { language: 'js', source: 'return 1;', capabilities: [] }, }); const shipAction = (packageId: string, objectName: string) => registry.registerItem('action', body(packageId.split('.').pop()!, objectName), 'name' as never, packageId); /** The environment's own item: the bare slot, and its active package-less row in the store. */ - const environmentAction = (objectName: string) => { + const environmentAction = (objectName: string | undefined) => { registry.registerItem('action', body('env', objectName), 'name' as never); storedRows.push({ id: 'r-env', type: 'action', name: 'approve', package_id: null, state: 'active', metadata: JSON.stringify(body('env', objectName)) }); }; @@ -1038,6 +1038,7 @@ describe('#33456 — the bare action read grafts a holder\'s envelope only at it for (const [shape, holder, held, other] of [ ['mirror shape: A on `account`, the environment\'s item on `contact`', A, 'account', 'contact'], ['single-holder cell: B on `contact`, the environment\'s item on `account`', B, 'contact', 'account'], + ['object-less: A on `account`, the environment\'s item object-less', A, 'account', undefined], ] as const) { it(`${shape}: the environment's item with no package's envelope (BASE: the holder's), and \`?package=\` the holder's own`, async () => { for (const steps of bothOrders([() => shipAction(holder, held), () => environmentAction(other)])) { diff --git a/packages/runtime/src/domains/meta-qualified-item-read.test.ts b/packages/runtime/src/domains/meta-qualified-item-read.test.ts index 576d57e41c2..8a70c7d84e0 100644 --- a/packages/runtime/src/domains/meta-qualified-item-read.test.ts +++ b/packages/runtime/src/domains/meta-qualified-item-read.test.ts @@ -1176,14 +1176,14 @@ describe('#33456 — the bare action read grafts a holder\'s envelope only at it } as never); }; beforeEach(compose); - const body = (label: string, objectName: string) => ({ - name: 'approve', label, type: 'script', objectName, target: 'approve', + const body = (label: string, objectName: string | undefined) => ({ + name: 'approve', label, type: 'script', ...(objectName ? { objectName } : {}), target: 'approve', body: { language: 'js', source: 'return 1;', capabilities: [] }, }); const shipAction = (packageId: string, objectName: string) => registry.registerItem('action', body(packageId.split('.').pop()!, objectName), 'name' as never, packageId); /** The environment's own item: the bare slot, and its active package-less row in the store. */ - const environmentAction = (objectName: string) => { + const environmentAction = (objectName: string | undefined) => { registry.registerItem('action', body('env', objectName), 'name' as never); storedRows.push({ id: 'r-env', type: 'action', name: 'approve', package_id: null, state: 'active', metadata: JSON.stringify(body('env', objectName)) }); }; @@ -1197,6 +1197,7 @@ describe('#33456 — the bare action read grafts a holder\'s envelope only at it for (const [shape, holder, held, other] of [ ['mirror shape: A on `account`, the environment\'s item on `contact`', A, 'account', 'contact'], ['single-holder cell: B on `contact`, the environment\'s item on `account`', B, 'contact', 'account'], + ['object-less: A on `account`, the environment\'s item object-less', A, 'account', undefined], ] as const) { it(`${shape}: the environment's item with no package's envelope (BASE: the holder's), and \`?package=\` the holder's own`, async () => { for (const steps of bothOrders([() => shipAction(holder, held), () => environmentAction(other)])) {