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

fix(objectql,runtime)!: a package-less action is kept on its own owner key, and a route serves the one on its key, so the declaration it judges and the handler it runs are one item's (ADR-0150 D4)

Clause-②: no (narrowing) — `/actions/OBJECT/NAME` answers with the declaration of the package-less action bound to `OBJECT` (its `requiredPermissions` and every other gate) wherever the platform binds `NAME` to `OBJECT` beside the environment's object-less `NAME`, in either registration order; a caller those requirements refuse is refused `403 PERMISSION_DENIED`. Widened beside it: the environment's object-less `NAME` answers every other route in both registration orders, where one order answered no declaration there. No export is removed; `SchemaRegistry.getEnvironmentScopedActions` is added.
Metadata-protocol: none
Skills-docs: none — `skills/**` and `content/docs/**` already teach that an action is keyed by its package, its object and its name, and that the router reaches an action bound to the object before an object-less one; no authored key, value or meaning changes.

<!-- adr-0087: not-required (no-migration-prescription) No key of any authorable schema is added, removed, renamed or re-shaped and no stored row changes: what moves is which of two registered items a route reads, so there is nothing for `objectstack migrate meta` to rewrite. -->

**The rule.** An action is identified by `(owner, owning object, name)` (ADR-0150 D4). The platform's package-less action bound to `lead` and the environment's object-less action of the same name are two items under two keys, never one contested name:

- The metadata registry keeps both, in either registration order. One owner key keeps one item: the later registration of that key stays, as before. A stored row saved again bound to another object replaces the name's stored row, since the environment's store holds one row of a name.
- Binding first, then owner: `/actions/lead/NAME` serves the platform's item bound to `lead`, and the environment's object-less item serves every other route. ADR-0150 D3's environment-first applies within one key: to change `lead`, the environment authors `NAME` bound to `lead`.
- On every route, the declaration the gates judge and the handler the dispatch runs are one item's.

`@objectstack/objectql`: `SchemaRegistry` keeps a package-less action of another owner key than the bare slot's in that key's own slot; `getEnvironmentScopedActions(name)` lists the environment-scoped action items of a name, one per owner key, and the owner-key view of the package resolver (`actionOwnerScopedRegistry`) reads the item on its own key. The removal of a stored row retires that row, never the platform's item of another key. `@objectstack/runtime`: the action route reads the package-less action on its own key.

**BREAKING** — who moves: a caller of `/actions/OBJECT/NAME` on a deployment where host code or a plugin registers a package-less `NAME` bound to `OBJECT` and the environment stores an object-less `NAME`. That route now always answers with the platform item's declaration, so a caller must hold that item's `requiredPermissions`. No first-party composition registers such a pair, so no answer changes in this repository.

The fix, for an environment that meant its object-less `NAME` to answer `OBJECT` too:

- FROM an object-less `NAME` expected to answer `/actions/OBJECT/NAME` beside the platform's `NAME` bound to `OBJECT`, TO the environment's `NAME` saved bound to `OBJECT` (`objectName: 'OBJECT'`), which answers that route as the environment's own item. The environment stores one row of a name, so that row then answers `OBJECT` alone; an environment action meant for every other route as well takes a second name.
87 changes: 87 additions & 0 deletions packages/objectql/src/action-package-addressing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,90 @@ describe('ADR-0150 D4 — a package-less action customizes only the holder whose
expect(registry.getEnvironmentScopedItem('mapping', 'feed')).toBeUndefined();
});
});

/**
* [ADR-0150 D4] An action is identified by `(owner, owning object, name)`: the
* platform's package-less action bound to `lead` and the environment's
* object-less action of the same name are two items under two keys. The
* registry keeps both, in either registration order, and each owner key reads
* its own; one key keeps one item, the later registration.
*/
describe('ADR-0150 D4 — the registry keeps one package-less action per owner key', () => {
/** Code the host or a plugin registers with no package. */
const platform = (name: string, objectName?: string) =>
registry.registerItem('action', {
name, label: `platform ${objectName ?? 'global'}`, type: 'script',
body: { language: 'js', source: 'return 1;', capabilities: [] },
...(objectName ? { objectName } : {}),
}, 'name' as never);
const fresh = () => {
registry = new SchemaRegistry();
registry.logLevel = 'silent';
};
const bothOrders = (first: () => unknown, second: () => unknown): Array<[string, () => void]> => [
['registered in order', () => { first(); second(); }],
['registered in reverse', () => { second(); first(); }],
];
const labels = (items: unknown[]) => items.map((i) => (i as { label?: string }).label).sort();
const onKey = (objectName: string) => label(resolve(objectName, 'approve'));

it('the platform\'s action bound to `lead` and the environment\'s object-less one are both kept, each on its own key', () => {
for (const [order, register] of bothOrders(() => platform('approve', 'lead'), () => authored('approve'))) {
fresh();
register();
expect(labels(registry.listItems('action')), order).toEqual(['env global', 'platform lead']);
expect(labels(registry.getEnvironmentScopedActions('approve')), order).toEqual(['env global', 'platform lead']);
expect([onKey('lead'), onKey('contact'), onKey('global')], order).toEqual(['platform lead', 'env global', 'env global']);
}
});

it('beside a package holder on another object, each package-less action still reads on its own key', () => {
for (const [order, register] of bothOrders(() => platform('approve', 'lead'), () => authored('approve'))) {
fresh();
install(A);
ship('approve', A, 'account');
register();
expect([onKey('lead'), onKey('account'), onKey('contact')], order).toEqual(['platform lead', 'alpha account', 'env global']);
}
});

it('the environment\'s item bound to `lead` registered after the platform\'s on `lead` takes that key', () => {
fresh();
platform('approve', 'lead');
authored('approve', 'lead');
expect(labels(registry.listItems('action'))).toEqual(['env lead']);
expect(onKey('lead')).toBe('env lead');
});

it('a stored row saved again on another key replaces the name\'s stored row and keeps the platform\'s item', () => {
fresh();
platform('approve', 'lead');
authored('approve', 'contact');
authored('approve');
expect(labels(registry.listItems('action'))).toEqual(['env global', 'platform lead']);
});

it('the stored row\'s removal retires the stored row, never the platform\'s item, in either registration order', () => {
for (const [order, register] of bothOrders(() => platform('approve', 'lead'), () => authored('approve'))) {
fresh();
register();
expect(registry.removeOverlayEntry('action', 'approve'), order).toBe(true);
expect(labels(registry.listItems('action')), order).toEqual(['platform lead']);
expect(registry.getItem<{ label?: string }>('action', 'approve')?.label, order).toBe('platform lead');
}
});

it('CONTROL — one package-less action is one item, and re-registering its key replaces it', () => {
fresh();
platform('approve', 'lead');
platform('approve', 'lead');
expect(labels(registry.listItems('action'))).toEqual(['platform lead']);
expect([onKey('lead'), onKey('contact')]).toEqual(['platform lead', undefined]);
fresh();
authored('approve');
expect(labels(registry.listItems('action'))).toEqual(['env global']);
expect([onKey('lead'), onKey('contact')]).toEqual(['env global', 'env global']);
expect(registry.removeOverlayEntry('action', 'approve')).toBe(true);
expect(registry.listItems('action')).toEqual([]);
});
});
10 changes: 10 additions & 0 deletions packages/objectql/src/action-package-addressing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,20 @@ export function actionOwnerScopedRegistry(registry: PackageScopedRegistry, owner
const item = registry.getPackageScopedItem<T>(type, name, packageId, member);
return type !== 'action' || owns(item) ? item : undefined;
},
// [ADR-0150 D4] The environment-scoped action ON this owner key: a
// registry that keeps one package-less action per owner key
// (`getEnvironmentScopedActions`) is asked for every one of them, never
// only the bare slot's, which may sit on another key.
getEnvironmentScopedItem: <T>(type: string, name: string, member?: string) => {
if (type === 'action' && typeof registry.getEnvironmentScopedActions === 'function') {
return registry.getEnvironmentScopedActions<T>(name).find(owns);
}
const item = registry.getEnvironmentScopedItem<T>(type, name, member);
return type !== 'action' || owns(item) ? item : undefined;
},
...(typeof registry.getEnvironmentScopedActions === 'function'
? { getEnvironmentScopedActions: <T>(name: string) => registry.getEnvironmentScopedActions!<T>(name).filter(owns) }
: {}),
};
}

Expand Down
9 changes: 7 additions & 2 deletions packages/objectql/src/package-scoped-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,16 @@ export interface PackageScopedQuery extends PackageScopedHoldersQuery {
readonly includeDisabledPackages?: boolean;
}

/** The registry slice the registry adapter reads — every member of it, and no other. */
/**
* The registry slice the registry adapter reads — every member of it, and no other.
* [ADR-0150 D4] `getEnvironmentScopedActions`, where a registry has it, is the
* environment-scoped `action` items of a name, one per owner key: the
* owner-key view (`actionOwnerScopedRegistry`) reads the item on its own key.
*/
export type PackageScopedRegistry = Pick<
SchemaRegistry,
'getPackage' | 'getAllPackages' | 'packageScopedHoldersOf' | 'getPackageScopedItem' | 'getEnvironmentScopedItem'
>;
> & Partial<Pick<SchemaRegistry, 'getEnvironmentScopedActions'>>;

/**
* Resolve a bare name by ADR-0150 D3 over the registry's holders: the rule
Expand Down
Loading
Loading