Skip to content

Commit 77025c7

Browse files
committed
fix(objectql): store an object's own actions as their ActionSchema parse at registerObject
A code package's object registered through SchemaRegistry.registerObject (a raw manifest, a strict: false stack, a programmatic registerApp) now keeps its own actions as their ActionSchema parse, so a declared requiresFeature / requiresMembershipReach is lowered into visible. An action that does not parse is refused with INVALID_METADATA (422) before anything is stored. One parse helper (parsePackageAction) serves both the standalone action item and the object's own actions. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012FM5zuP45LkwYGZfazCj5z
1 parent 9ca36b1 commit 77025c7

2 files changed

Lines changed: 229 additions & 3 deletions

File tree

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#25744] An object's OWN `actions`, registered by a code package through
5+
* {@link SchemaRegistry.registerObject}, are stored as their `ActionSchema`
6+
* parse — the parse a standalone `action` item is stored as since #25649 — so a
7+
* declared `requiresFeature` / `requiresMembershipReach` is lowered into
8+
* `visible`, and an action that does not parse is refused (`INVALID_METADATA`,
9+
* 422) before anything of the object is stored.
10+
*
11+
* Before, a raw manifest or a `strict: false` stack registered with
12+
* `ObjectQL.registerApp` stored its objects' actions as written: served by
13+
* `GET /api/v1/meta/objects/:name` (`protocol.getMetaItem({ type: 'object' })`)
14+
* with `requiresFeature` riding along raw and no `visible` — a feature gate no
15+
* reader enforces. A strict `defineStack`, `os validate` and the metadata save
16+
* door already parse the same body through `ActionSchema`.
17+
*/
18+
19+
import { describe, it, expect } from 'vitest';
20+
import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol';
21+
import { defineStack } from '@objectstack/spec';
22+
import { ObjectSchema } from '@objectstack/spec/data';
23+
import { SysMetadataAuditObject, SysMetadataCommitObject, SysMetadataHistoryObject } from '@objectstack/metadata-core';
24+
import { ObjectQL } from './engine.js';
25+
26+
/** One gated declaration, authored on an object's own action. */
27+
const GATED = {
28+
name: 'gated', label: 'Gated', type: 'url' as const, target: 'https://example.com/gated',
29+
locations: ['record_header' as const], visible: "record.name != ''",
30+
requiresFeature: 'multiOrgEnabled' as const,
31+
};
32+
const LOWERED = { dialect: 'cel', source: "(record.name != '') && features.multiOrgEnabled != false" };
33+
34+
const objectWith = (actions: unknown[]) => ({
35+
name: 'oa_org', label: 'Org',
36+
fields: { name: { name: 'name', type: 'text', label: 'Name' } },
37+
actions,
38+
});
39+
const rawManifest = (actions: unknown[]) => ({ id: 'com.test.raw', name: 'raw', objects: [objectWith(actions)] });
40+
41+
/** A driver that holds nothing: the reads below never reach a stored row. */
42+
const emptyDriver: any = {
43+
name: 'memory', version: '0.0.0', supports: {},
44+
async connect() {}, async disconnect() {}, async checkHealth() { return true; },
45+
async execute() { return null; },
46+
async find() { return []; }, async findOne() { return null; }, async count() { return 0; },
47+
};
48+
49+
/** The stored-metadata family the by-name read consults for an overlay row (none here). */
50+
const sysMetadataObject = {
51+
name: 'sys_metadata', label: 'System Metadata',
52+
fields: {
53+
id: { name: 'id', label: 'ID', type: 'text' as const, primaryKey: true },
54+
type: { name: 'type', label: 'Type', type: 'text' as const, required: true },
55+
name: { name: 'name', label: 'Name', type: 'text' as const, required: true },
56+
organization_id: { name: 'organization_id', label: 'Org', type: 'text' as const },
57+
package_id: { name: 'package_id', label: 'Package', type: 'text' as const },
58+
metadata: { name: 'metadata', label: 'Body', type: 'textarea' as const },
59+
state: { name: 'state', label: 'State', type: 'text' as const },
60+
},
61+
};
62+
63+
async function host() {
64+
const engine = new ObjectQL();
65+
engine.registerDriver(emptyDriver, true);
66+
await engine.init();
67+
engine.registry.registerObject(sysMetadataObject);
68+
for (const o of [SysMetadataHistoryObject, SysMetadataAuditObject, SysMetadataCommitObject]) engine.registry.registerObject(o as any);
69+
return { engine, protocol: new ObjectStackProtocolImplementation(engine) };
70+
}
71+
72+
const servedAction = async (protocol: ObjectStackProtocolImplementation, name: string) => {
73+
const served: any = await protocol.getMetaItem({ type: 'object', name: 'oa_org' } as any);
74+
return (served.item.actions as any[]).find((a) => a?.name === name);
75+
};
76+
77+
const refusalOf = (fn: () => unknown): any => {
78+
try { fn(); } catch (e) { return e; }
79+
return undefined;
80+
};
81+
82+
describe("an object's own action is stored as its ActionSchema parse", () => {
83+
it('a raw manifest: GET /meta/object/<name> serves `requiresFeature` lowered into `visible`', async () => {
84+
const { engine, protocol } = await host();
85+
engine.registerApp(rawManifest([GATED]));
86+
const served = await servedAction(protocol, 'gated');
87+
expect(served.visible).toEqual(LOWERED);
88+
expect(served).not.toHaveProperty('requiresFeature');
89+
// The same lowering `ObjectSchema.create` gives a platform object's action of the same declaration.
90+
const authored: any = ObjectSchema.create(objectWith([GATED]) as any);
91+
expect(served.visible).toEqual(authored.actions[0].visible);
92+
// The registry's own resolved object holds the same parsed body.
93+
expect(engine.registry.getObject('oa_org')?.actions?.[0]).toEqual(served);
94+
});
95+
96+
it('an action with no `visible` of its own is served the gate alone', async () => {
97+
const { visible: _visible, ...ungated } = GATED;
98+
const { engine, protocol } = await host();
99+
engine.registerApp(rawManifest([ungated]));
100+
const served = await servedAction(protocol, 'gated');
101+
expect(served.visible).toEqual({ dialect: 'cel', source: 'features.multiOrgEnabled != false' });
102+
expect(served).not.toHaveProperty('requiresFeature');
103+
});
104+
105+
it('`requiresMembershipReach` is lowered the same way', async () => {
106+
const { requiresFeature: _feature, ...rest } = GATED;
107+
const { engine, protocol } = await host();
108+
engine.registerApp(rawManifest([{ ...rest, requiresMembershipReach: 'invite_member' }]));
109+
const served = await servedAction(protocol, 'gated');
110+
expect(served).not.toHaveProperty('requiresMembershipReach');
111+
expect(served.visible).toEqual(ObjectSchema.create(objectWith([{ ...rest, requiresMembershipReach: 'invite_member' }]) as any).actions![0].visible);
112+
});
113+
114+
it('a `strict: false` stack registered with registerApp is parsed at the registry', async () => {
115+
const stack: any = defineStack({
116+
manifest: { id: 'com.test.loose', name: 'loose', version: '1.0.0', type: 'app' },
117+
objects: [objectWith([GATED])],
118+
} as any, { strict: false });
119+
// The loose stack hands the body on as written.
120+
expect(stack.objects[0].actions[0]).toHaveProperty('requiresFeature');
121+
const { engine, protocol } = await host();
122+
engine.registerApp({ ...stack, ...stack.manifest });
123+
const served = await servedAction(protocol, 'gated');
124+
expect(served.visible).toEqual(LOWERED);
125+
expect(served).not.toHaveProperty('requiresFeature');
126+
});
127+
128+
it('a direct registerObject under a package is parsed; the caller\'s body is not mutated', async () => {
129+
const { engine } = await host();
130+
const def = objectWith([structuredClone(GATED)]);
131+
engine.registry.registerObject(def as any, 'com.test.direct');
132+
expect(engine.registry.getObject('oa_org')?.actions?.[0]).toMatchObject({ visible: LOWERED });
133+
expect(def.actions[0]).toHaveProperty('requiresFeature', 'multiOrgEnabled');
134+
});
135+
136+
it('control: an object a strict parse already produced is stored deep-equal to what it arrived as', async () => {
137+
const authored: any = ObjectSchema.create(objectWith([GATED]) as any);
138+
const { engine, protocol } = await host();
139+
engine.registerApp({ id: 'com.test.strict', name: 'strict', objects: [structuredClone(authored)] });
140+
expect(await servedAction(protocol, 'gated')).toEqual(authored.actions[0]);
141+
});
142+
143+
it('an action that does not parse is refused loudly, naming the action, its object and package — nothing stored', async () => {
144+
const { engine } = await host();
145+
const err = refusalOf(() => engine.registerApp(rawManifest([{ ...GATED, visible: false }])));
146+
expect(err).toMatchObject({ code: 'INVALID_METADATA', status: 422 });
147+
expect(err.message).toContain("Action 'gated' of object 'oa_org' in package 'com.test.raw' does not satisfy the action contract");
148+
expect(err.issues).toEqual(expect.arrayContaining([expect.objectContaining({ path: 'actions.0.requiresFeature' })]));
149+
expect(engine.registry.getObject('oa_org')).toBeUndefined();
150+
});
151+
152+
it('a refused re-registration leaves the object already registered under that package in place', async () => {
153+
const { engine } = await host();
154+
engine.registerApp(rawManifest([GATED]));
155+
const err = refusalOf(() => engine.registry.registerObject(objectWith([{ ...GATED, bogus: true }]) as any, 'com.test.raw'));
156+
expect(err).toMatchObject({ code: 'INVALID_METADATA', status: 422 });
157+
expect(err.issues).toEqual(expect.arrayContaining([expect.objectContaining({ code: 'unrecognized_keys' })]));
158+
expect(engine.registry.getObject('oa_org')?.actions?.[0]).toMatchObject({ name: 'gated', visible: LOWERED });
159+
});
160+
});

‎packages/objectql/src/registry.ts‎

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,35 @@ interface PackageActionSchemaError extends Error {
194194
* Refused rather than stored unparsed: an action that fails the parse would be
195195
* served with whatever gates it declares never taking effect, and `defineStack`
196196
* and `os validate` refuse the same declaration at authoring time.
197+
*
198+
* [#25744] The one parse for both places a code package declares an action:
199+
* a standalone `action` item ({@link SchemaRegistry.registerItem}) and an
200+
* object's own `actions` ({@link parsePackageObjectActions}, called by
201+
* {@link SchemaRegistry.registerObject}). `owner` names the object an own
202+
* action is declared on, so the refusal says which object and `issues[]`
203+
* locates each failure inside its body.
197204
*/
198-
function parsePackageAction(item: unknown, name: string, packageId: string): unknown {
205+
function parsePackageAction(
206+
item: unknown,
207+
name: string,
208+
packageId: string,
209+
owner?: { object: string; index: number },
210+
): unknown {
199211
const parsed = ActionSchema.safeParse(item);
200212
if (parsed.success) return parsed.data;
213+
// [#25744] An object's own action is located inside the object body it was
214+
// declared in (`actions.<index>.<key>`), the path `ObjectSchema` reports.
215+
const prefix = owner ? ['actions', String(owner.index)] : [];
201216
const issues = parsed.error.issues.map((issue) => ({
202-
path: issue.path.map(String).join('.'),
217+
path: [...prefix, ...issue.path.map(String)].join('.'),
203218
message: issue.message,
204219
code: issue.code,
205220
}));
221+
const subject = owner
222+
? `Action '${name}' of object '${owner.object}' in package '${packageId}'`
223+
: `Action '${name}' of package '${packageId}'`;
206224
const err = new Error(
207-
`Action '${name}' of package '${packageId}' does not satisfy the action contract (ActionSchema): `
225+
`${subject} does not satisfy the action contract (ActionSchema): `
208226
+ issues.map((issue) => (issue.path ? `${issue.path}: ${issue.message}` : issue.message)).join('; ')
209227
+ '. Every action a package ships is stored as its schema parse — the parse that lowers '
210228
+ '`requiresFeature` and `requiresMembershipReach` into `visible` — so an action that does not parse '
@@ -218,6 +236,35 @@ function parsePackageAction(item: unknown, name: string, packageId: string): unk
218236
throw err;
219237
}
220238

239+
/**
240+
* [#25744] A code package's object, its own `actions` stored as their
241+
* `ActionSchema` parse ({@link parsePackageAction}) — or the loud refusal of
242+
* the first one that does not parse.
243+
*
244+
* The object-path twin of {@link SchemaRegistry.registerItem}'s action parse.
245+
* `ObjectSchema`'s `actions` is `ActionSchema[]`, so a platform object
246+
* (`ObjectSchema.create`) and a strict `defineStack` already hand this method
247+
* parsed actions, and the parse being idempotent over its own output stores
248+
* them deep-equal to what arrived. A raw manifest, a `strict: false` stack or a
249+
* programmatic `registerApp` reached the store with its objects' actions as
250+
* written, so a declared `requiresFeature` / `requiresMembershipReach` was
251+
* served raw, never lowered into `visible`.
252+
*
253+
* Returns `schema` by reference when it declares no `actions` array, and a
254+
* shallow copy carrying the parsed array otherwise: the caller's body is
255+
* never mutated.
256+
*/
257+
function parsePackageObjectActions(schema: ServiceObject, packageId: string): ServiceObject {
258+
const actions = (schema as { actions?: unknown }).actions;
259+
if (!Array.isArray(actions) || actions.length === 0) return schema;
260+
const parsed = actions.map((action, index) => {
261+
const declared = (action as { name?: unknown } | null | undefined)?.name;
262+
const name = typeof declared === 'string' && declared !== '' ? declared : `#${index}`;
263+
return parsePackageAction(action, name, packageId, { object: schema.name, index });
264+
});
265+
return { ...schema, actions: parsed } as ServiceObject;
266+
}
267+
221268
/**
222269
* [#24472] The key {@link SchemaRegistry}'s bound-action collision audit is
223270
* carried under. A symbol, and ⛔ deliberately NOT re-exported from the package
@@ -3442,6 +3489,25 @@ export class SchemaRegistry {
34423489
throw new ExtensionMembershipKeysRefusedError(schema.name, packageId);
34433490
}
34443491

3492+
// [#25744] A code package's object keeps its own actions as their
3493+
// `ActionSchema` parse — `requiresFeature` / `requiresMembershipReach`
3494+
// lowered into `visible` — or is refused here, beside the doors above and
3495+
// for their reason: before any materialization and before the contributor
3496+
// list is opened, so a refused object leaves nothing behind and a refused
3497+
// re-registration leaves the registered one in place
3498+
// ({@link parsePackageObjectActions}). The test is `registerItem`'s action
3499+
// parse's, asked of the body as `applyProtection` below will stamp it (a
3500+
// package id it does not already carry, `'package'` provenance): ⛔ a
3501+
// registration with no `packageId`, the `sys_metadata` sentinel, or a
3502+
// tenant-authored body (`_provenance: 'org'` — the save door's write-through
3503+
// and the boot rehydration) is never judged here.
3504+
if (packageId && isCodeArtifactBody({
3505+
_packageId: (schema as { _packageId?: unknown })._packageId ?? packageId,
3506+
_provenance: (schema as { _provenance?: unknown })._provenance ?? 'package',
3507+
})) {
3508+
schema = parsePackageObjectActions(schema, packageId);
3509+
}
3510+
34453511
// [#23065] A `tree` field that names no `reference` references the object
34463512
// that declares it, and this is where that is written down for every door
34473513
// the parse does not cover: a metadata save stores the authored body

0 commit comments

Comments
 (0)