Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .changeset/33456-action-envelope-owner-key.md
Original file line number Diff line number Diff line change
@@ -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).
Original file line number Diff line number Diff line change
@@ -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 `<packageId>:approve` composites. */
const engineOf = (items: ReadonlyArray<ReturnType<typeof action>>) => {
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<ReturnType<typeof action>>,
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<typeof action> | undefined;

const holderOf = (artifact: ReturnType<typeof action> | 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`);
});
});
});
45 changes: 45 additions & 0 deletions packages/metadata-protocol/src/protocol-artifact-lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<ArtifactLookupHost, 'engine'>,
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<ArtifactLookupHost, 'engine'>,
type: string, name: string, currentPackageId?: string, member?: string,
): unknown {
Expand Down
17 changes: 16 additions & 1 deletion packages/metadata-protocol/src/protocol-meta-item-reads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1236,12 +1246,17 @@ export async function readMetaItem(host: Pick<ProtocolHost, 'artifactLockLayerAt
// [#32501] …and, at an admitted exact address, at the package that
// address reads, strictly ({@link exactAddressArtifact}): never the
// first registered holder the pair id's lookup fell back to.
// [ADR-0150 D4, #33456] …and, for an action, at the served item's own
// owner key: an artifact another object binds the name to is another
// item, and lends this one no envelope ({@link artifactOwnerKeyOf}).
const artifactOwner = artifactOwnerKeyOf(request.type, item);
const artifactItem = exactAddress
? exactAddressArtifact(exactAddress, (packageId) => 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Site & { readonly noPackage: boolean; readonly disposition: Disposition; readonly why: string }> = [
{ 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<ArtifactLookupHost, \'engine\'>, 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<ArtifactLookupHost, \'engine\'>, 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',
Expand Down
16 changes: 13 additions & 3 deletions packages/metadata-protocol/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<typeof lookupArtifactItem> = {
get engine() { return readEngine(); },
};
return lookupArtifactItem.call(host, type, name, currentPackageId, member);
return lookupArtifactItem.call(host, type, name, currentPackageId, member, owner);
}

/**
Expand Down
Loading
Loading