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
16 changes: 16 additions & 0 deletions .changeset/action-package-less-owner-key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@objectstack/objectql': patch
---

fix(objectql): a package-less action customizes a package's action only on that action's owner key

Clause-②: no
Metadata-protocol: none

An action is identified by its package, its owning object and its name (ADR-0150 D4). The registry's package-scoped reads now follow that identity for a package-less action (one saved with no package, or registered by host or plugin code with no package):

- On the same owner key as exactly one package's action of that name, it is still that package's customization, as before.
- On another object (or object-less beside an action bound to an object), it is the environment's own item. `getPackageScopedItem('action', NAME, PKG)` answers the package's own action, and `getEnvironmentScopedItem('action', NAME)` answers the package-less one.
- On the owner key of two or more packages' actions, it is still no package's item and no environment item, as before.

So `resolveActionByPackage` resolves a package caller's action on its object to that package's own declaration, under that package's coordinate, and an `/actions/:object/:action` route judges the declaration its own package declared there. A package caller on the object of a package-less action is refused naming the environment (ADR-0150 D3), never answered as another package's. A route that names no package answers as before. Other metadata types keep their one-holder reading.
114 changes: 114 additions & 0 deletions packages/objectql/src/action-package-addressing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,117 @@ describe.each([
expect(resolve('account', 'approve', B)).toMatchObject({ status: 'resolved', via: 'own', holder: { kind: 'package', packageId: B } });
});
});

/**
* ADR-0150 D4: an action is identified by `(package, owning object, name)`, so
* a package-less action customizes a package's action only on that action's
* owner key. On another object it is the environment's own item, and each
* package keeps its own item and its own caller's resolution.
*
* Each pin was ablated once (the owner-key test removed from the binding, the
* pin read red, restored); the readings are in the PR body.
*/
describe('ADR-0150 D4 — a package-less action customizes only the holder whose action has its owner key', () => {
const item = (packageId: string | null) => (packageId === null
? registry.getEnvironmentScopedItem<{ label?: string }>('action', 'approve')
: registry.getPackageScopedItem<{ label?: string }>('action', 'approve', packageId))?.label;

it('beside one holder on another object, it is the environment\'s own item and the holder keeps its own, in either registration order', () => {
for (const authoredFirst of [false, true]) {
registry = new SchemaRegistry();
registry.logLevel = 'silent';
install(A);
if (authoredFirst) authored('approve', 'contact');
ship('approve', A, 'account');
if (!authoredFirst) authored('approve', 'contact');
const order = `authored ${authoredFirst ? 'first' : 'last'}`;
expect([item(A), item(null)], order).toEqual(['alpha account', 'env contact']);
expect(registry.packageScopedHoldersOf('action', 'approve'), order).toEqual([A]);
}
});

it('the holder\'s own caller resolves its own action on its object, with its own coordinate', () => {
install(A);
install(P, [A]);
ship('approve', A, 'account');
authored('approve', 'contact');
for (const caller of [A, P]) {
const r = resolve('account', 'approve', caller);
expect(r, caller).toMatchObject({ status: 'resolved', holder: { kind: 'package', packageId: A } });
expect(label(r), caller).toBe('alpha account');
}
});

it('the authored action\'s own object answers the environment\'s item, never a package holder\'s', () => {
install(A);
install(P, [A]);
ship('approve', A, 'account');
authored('approve', 'contact');
const bare = resolve('contact', 'approve');
expect(bare).toMatchObject({ status: 'resolved', via: 'environment', holder: { kind: 'environment' } });
expect(label(bare)).toBe('env contact');
// D3 (a): a package caller never reaches the environment's item.
expect(resolve('contact', 'approve', P)).toMatchObject({
status: 'refused', reason: 'outside-closure', candidates: [{ kind: 'environment' }],
});
});

it('an object-less authored action beside a holder bound to an object is the environment\'s own item', () => {
install(A);
ship('approve', A, 'account');
authored('approve');
expect([item(A), item(null)]).toEqual(['alpha account', 'env global']);
});

it('beside two holders, it customizes the one on its owner key; on a third object it is the environment\'s own', () => {
install(A);
install(B);
ship('approve', A, 'account');
ship('approve', B, 'contact');
authored('approve', 'lead');
expect([item(A), item(B), item(null)], 'on a third object').toEqual(['alpha account', 'beta contact', 'env lead']);
authored('approve', 'account');
expect([item(A), item(B), item(null)], 'on A\'s object').toEqual(['env account', 'beta contact', undefined]);
});

it('CONTROL — beside the one holder on its own owner key it is still that holder\'s customization (object-bound and object-less)', () => {
install(A);
ship('approve', A, 'account');
authored('approve', 'account');
expect([item(A), item(null)], 'bound').toEqual(['env account', undefined]);
registry = new SchemaRegistry();
registry.logLevel = 'silent';
install(A);
ship('approve', A);
authored('approve');
expect([item(A), item(null)], 'object-less').toEqual(['env global', undefined]);
});

it('beside a bound holder and an object-less one, it customizes the holder on its own owner key only', () => {
install(A);
install(B);
ship('approve', A, 'account');
ship('approve', B);
authored('approve', 'account');
expect([item(A), item(B), item(null)], 'bound').toEqual(['env account', 'beta global', undefined]);
authored('approve');
expect([item(A), item(B), item(null)], 'object-less').toEqual(['alpha account', 'env global', undefined]);
});

it('CONTROL — two holders on its owner key: no package\'s item and no environment item, as before', () => {
install(A);
install(B);
ship('approve', A, 'account');
ship('approve', B, 'account');
authored('approve', 'account');
expect([item(A), item(B), item(null)]).toEqual(['alpha account', 'beta account', undefined]);
});

it('CONTROL — another type keeps the one-holder reading: its package-less row is the holder\'s customization whatever its body', () => {
install(A);
registry.registerItem('mapping', { name: 'feed', label: 'alpha', objectName: 'account' } as never, 'name' as never, A);
registry.registerItem('mapping', { name: 'feed', label: 'env', objectName: 'contact' } as never, 'name' as never);
expect(registry.getPackageScopedItem<{ label?: string }>('mapping', 'feed', A)?.label).toBe('env');
expect(registry.getEnvironmentScopedItem('mapping', 'feed')).toBeUndefined();
});
});
33 changes: 31 additions & 2 deletions packages/objectql/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9395,13 +9395,23 @@ export class SchemaRegistry {
*
* `holders` is the composite holder set, passed in when the caller already
* has it.
*
* [ADR-0150 D4] An `action` is identified by `(package, owning object,
* name)`, so for a package-less action the holders counted above are only
* those whose own item has the same owner key ({@link standaloneActionOwnerKey},
* the router's own test; every object-less spelling is one key): a
* package-less action on another object customizes no holder's action. It
* is the environment's own item (`null`) when no holder shares its owner
* key, that holder's customization when exactly one does, and no package's
* (`undefined`) when two or more do.
*/
private overlaySlotBinding(type: string, item: unknown, name: string, collection: Map<string, any>, holders?: ReadonlySet<string>): string | null | undefined {
const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId;
const composite = holders ?? new Set(this.compositeHoldersOf(collection, name));
if (typeof stamped !== 'string' || stamped === '' || stamped === 'sys_metadata') {
if (composite.size === 0) return null;
return composite.size === 1 ? [...composite][0] : undefined;
const customized = type === 'action' ? actionHoldersOnOwnerKeyOf(item, name, collection, composite) : [...composite];
if (customized.length === 0) return null;
return customized.length === 1 ? customized[0] : undefined;
}
if (composite.has(stamped)) return stamped;
const paired = this.pairRowBinding(type, stamped, item, name, collection, composite);
Expand Down Expand Up @@ -12147,6 +12157,25 @@ function pairRowDeclaresAnotherHolder(item: unknown, packageId: string, holders:
return declared !== undefined && declared !== packageId && holders.has(declared);
}

/**
* [ADR-0150 D4] The packages among `holders` whose own `action` item of `name`
* (its composite slot) has the owner key of the package-less action `item`
* ({@link standaloneActionOwnerKey}; every object-less spelling is one key):
* the holders that action can customize, in `holders`' order.
*/
function actionHoldersOnOwnerKeyOf(item: unknown, name: string, collection: Map<string, any>,
holders: ReadonlySet<string>): string[] {
const ownerKey = (action: unknown): string => {
const owner = standaloneActionOwnerKey(action);
return isObjectLessActionKey(owner) ? GLOBAL_ACTION_OBJECT_KEY : owner;
};
const key = ownerKey(item);
return [...holders].filter((holder) => {
const own = collection.get(`${holder}:${name}`);
return own !== null && typeof own === 'object' && ownerKey(own) === key;
});
}

/** The base a row's body declares it customizes (`overrides.package`, ADR-0146 D2), or `undefined`. */
function declaredOverrideBase(item: unknown): string | undefined {
const declaration = (item as { overrides?: unknown } | null | undefined)?.overrides;
Expand Down
80 changes: 80 additions & 0 deletions packages/runtime/src/action-package-addressing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1772,3 +1772,83 @@ describe('[#32681] /actions/account/approve judges and runs one item over the st
}
}
});

/**
* ADR-0150 D4: executing an action resolves the action its own package
* declared on its owner key, never a same-named action of another object, and
* the gates run on that resolved declaration. A package-less action on
* another object is the environment's own item (or the platform's), never the
* customization of the one package that holds the name.
*
* Each pin was ablated once (the owner-key test removed from the registry
* binding, rebuilt, the pin read red, restored and rebuilt); the readings are
* in the PR body.
*/
describe('ADR-0150 D4 — the action route resolves the declaration its own package declared on its owner key', () => {
const shipAt = (packageId: string | undefined, label: string, objectName: string | undefined, requiredPermissions: string[]) =>
engine.registry.registerItem('action', {
name: 'approve', label, type: 'script', ...(objectName ? { objectName } : {}), target: 'approve', requiredPermissions,
body: { language: 'js', source: 'return 1;', capabilities: [] },
} as never, 'name' as never, packageId);
const storeEnvironmentRow = (objectName: string) => {
storedRows = [{
id: 'env-approve', type: 'action', name: 'approve', state: 'active', package_id: null,
metadata: JSON.stringify({ name: 'approve', label: 'env', type: 'script', objectName, requiredPermissions: ['env_gate'] }),
}];
};
const routeTo = (objectName: string, callerPackageId?: string) => resolveRouteActionDeclaration(deps, { request: {} } as any, {
ql: engine, objectName, actionName: 'approve', ...(callerPackageId !== undefined ? { callerPackageId } : {}),
});
const answerOf = (d: Awaited<ReturnType<typeof routeTo>>) =>
[d.refusal?.reason, d.degraded, d.packageId, d.action?.label, d.action?.requiredPermissions];
/** A on `account`, P depending on A, and a package-less `approve` on `contact` (the environment's, or the platform's). */
const compose = (whose: 'env' | 'platform', packagelessFirst: boolean) => {
install(A);
install(P, [A]);
const packageless = () => shipAt(undefined, whose, 'contact', [`${whose}_gate`]);
if (packagelessFirst) packageless();
shipAt(A, 'alpha', 'account', ['alpha_gate']);
if (!packagelessFirst) packageless();
if (whose === 'env') storeEnvironmentRow('contact');
engine.registerAction('account', 'approve', () => `ran ${A}`, `app:${A}`);
engine.registerAction('contact', 'approve', () => 'ran package-less', 'metadata-service');
};
const COMPOSITIONS = (['env', 'platform'] as const).flatMap((whose) =>
[false, true].map((packagelessFirst) => ({ whose, packagelessFirst, name: `${whose} on contact, registered ${packagelessFirst ? 'first' : 'last'}` })));

for (const { whose, packagelessFirst, name } of COMPOSITIONS) {
it(`${name}: a caller of A, or of a package depending on A, resolves A's own declaration on account under A's coordinate and runs A's handler (BASE: the declaration came back with no package)`, async () => {
compose(whose, packagelessFirst);
for (const caller of [A, P]) {
const declaration = await routeTo('account', caller);
expect(answerOf(declaration), caller).toEqual([undefined, undefined, A, 'alpha', ['alpha_gate']]);
expect(await executeRegisteredAction(deps, engine, 'account', ['approve'], {}, declaration.packageId), caller)
.toEqual({ dispatched: true, result: `ran ${A}` });
}
});

it(`${name}: a package caller on contact is refused naming the package-less holder, never answered as A's (BASE: no declaration under A's coordinate)`, async () => {
compose(whose, packagelessFirst);
for (const caller of [A, P]) {
const declaration = await routeTo('contact', caller);
expect(answerOf(declaration), caller).toEqual(['outside-closure', undefined, undefined, undefined, undefined]);
expect(declaration.refusal?.candidates, caller).toEqual([{ kind: 'environment' }]);
}
});

it(`${name}: CONTROL — a caller naming no package keeps each route's own declaration and its gates`, async () => {
compose(whose, packagelessFirst);
expect(answerOf(await routeTo('account'))).toEqual([undefined, undefined, undefined, 'alpha', ['alpha_gate']]);
expect(answerOf(await routeTo('contact'))).toEqual([undefined, undefined, undefined, whose, [`${whose}_gate`]]);
});
}

it('CONTROL — a package-less action on A\'s own object is A\'s customization for A\'s caller, as before', async () => {
install(A);
shipAt(A, 'alpha', 'account', ['alpha_gate']);
shipAt(undefined, 'env', 'account', ['env_gate']);
storeEnvironmentRow('account');
const declaration = await routeTo('account', A);
expect([declaration.refusal, declaration.packageId]).toEqual([undefined, A]);
});
});
Loading