Skip to content

Commit accfdc3

Browse files
committed
fix(objectql,metadata,spec): an installed package's apps and objects never keep a self-written _packageId
The owner-stamp judge (judgeCollectionItemOwnerStamp) now covers objects and apps: registerApp and a nested plugin refuse a foreign body stamp on either before anything of the body registers, the residual door refuses a residual object's, the metadata plugin's body registration refuses a judged collection's before it stamps, and the stack schema judges objects and apps at the build door. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5pevHhCDHNc3eWbkAg
1 parent 0c6b7b8 commit accfdc3

8 files changed

Lines changed: 657 additions & 16 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#28697] The metadata plugin's body registration (`registerArtifactBodyCollections`,
5+
* reached here through `MetadataPlugin.registerUnclaimedTopLevel`, the call a
6+
* config boot makes with a stack no schema parse judged) refuses an item whose
7+
* body already wears a `_packageId` other than the package it registers it
8+
* under — by the one judge (`judgeCollectionItemOwnerStamp`), over the build
9+
* door's collections, before the sink stores anything.
10+
*
11+
* Why it matters: `applyProtection` stamps an unset key only, so the body's own
12+
* stamp used to be what this door stored, and the engine's boot sync
13+
* (`ObjectQLPlugin.loadMetadataFromService`) files every `app` / `view` /
14+
* `flow` / `hook` it reads back under that stamp.
15+
*/
16+
17+
import { describe, it, expect } from 'vitest';
18+
import { judgeCollectionItemOwnerStamp } from '@objectstack/spec/kernel';
19+
import { MetadataPlugin } from './plugin.js';
20+
21+
const STACK = 'com.probe.stack';
22+
const OTHER = 'com.probe.a';
23+
const body = (id: string, namespace: string) => ({ id, name: namespace, namespace, version: '1.0.0', type: 'app' });
24+
25+
/** A two-body config stack whose top-level `extra` no body declares: the residual, owned by `STACK`. */
26+
const withResidual = (extra: Record<string, unknown>) => ({
27+
manifest: body(STACK, 'probestack'),
28+
packages: [{ manifest: body(OTHER, 'probea') }, { manifest: body('com.probe.b', 'probeb') }],
29+
...extra,
30+
});
31+
32+
async function registerResidual(stack: unknown): Promise<{ error: any; stored: Array<{ type: string; name: string; item: any }> }> {
33+
const stored: Array<{ type: string; name: string; item: any }> = [];
34+
let error: any;
35+
try {
36+
await MetadataPlugin.registerUnclaimedTopLevel(
37+
{ register: async (type, name, item) => { stored.push({ type, name, item }); }, warn: () => undefined },
38+
stack,
39+
{ door: 'config', label: 'probe' },
40+
);
41+
} catch (e) {
42+
error = e;
43+
}
44+
return { error, stored };
45+
}
46+
47+
const app = (stamp?: unknown) => ({ name: 'probe_app', label: 'Probe', ...(stamp === undefined ? {} : { _packageId: stamp }) });
48+
const flow = (stamp?: unknown) => ({ name: 'probe_flow', label: 'Probe', type: 'autolaunched', ...(stamp === undefined ? {} : { _packageId: stamp }) });
49+
const container = (stamp?: unknown) => ({
50+
object: 'probe_thing',
51+
list: { type: 'grid', columns: [{ field: 'name' }] },
52+
...(stamp === undefined ? {} : { _packageId: stamp }),
53+
});
54+
55+
describe('[#28697] the metadata door refuses a body stamp naming another package, before it stores anything', () => {
56+
it.each([
57+
['apps', 'app', 'probe_app', app],
58+
['flows', 'flow', 'probe_flow', flow],
59+
['views', 'view', 'probe_thing', container],
60+
] as const)('a residual %s entry wearing another package\'s id: VALIDATION_ERROR / 400 at `_packageId`, nothing stored', async (key, type, name, make) => {
61+
const { error, stored } = await registerResidual(withResidual({ [key]: [make(OTHER)] }));
62+
expect(error, 'the registration must be refused').toBeInstanceOf(Error);
63+
expect(error).toMatchObject({ code: 'VALIDATION_ERROR', status: 400, httpStatus: 400 });
64+
expect(error.issues).toEqual([{ path: '_packageId', code: 'custom', message: expect.any(String) }]);
65+
expect(error.message).toBe(judgeCollectionItemOwnerStamp(make(OTHER), type, name, STACK, 'register')!.message);
66+
expect(stored).toEqual([]);
67+
});
68+
69+
it('CONTROL: an absent stamp and a stamp equal to the owner are stored, stamped with the owner', async () => {
70+
for (const stamp of [undefined, STACK]) {
71+
const { error, stored } = await registerResidual(withResidual({ apps: [app(stamp)], flows: [flow(stamp)] }));
72+
expect(error, `stamp ${String(stamp)}`).toBeUndefined();
73+
expect(stored.map((s) => [s.type, s.name, s.item._packageId])).toEqual([
74+
['app', 'probe_app', STACK],
75+
['flow', 'probe_flow', STACK],
76+
]);
77+
}
78+
});
79+
80+
it('CONTROL: a collection the build door does not judge (`objectExtensions`) is stored as before', async () => {
81+
const ext = { name: 'probe_ext', extend: 'probe_thing', fields: {}, _packageId: OTHER };
82+
const { error } = await registerResidual(withResidual({ objectExtensions: [ext] }));
83+
expect(error).toBeUndefined();
84+
});
85+
});

‎packages/metadata/src/plugin.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Plugin, PluginContext } from '@objectstack/core';
1414
import { resolveArtifactPackageOrder, artifactPackageId } from '@objectstack/core';
1515
import { NodeMetadataManager } from './node-metadata-manager.js';
1616
import { MemoryLoader } from './loaders/memory-loader.js';
17-
import { DEFAULT_METADATA_TYPE_REGISTRY } from '@objectstack/spec/kernel';
17+
import { DEFAULT_METADATA_TYPE_REGISTRY, judgeCollectionItemOwnerStamp, OWNER_STAMP_JUDGED_COLLECTIONS } from '@objectstack/spec/kernel';
1818
import type { MetadataPluginConfig } from '@objectstack/spec/kernel';
1919
import { applyProtection } from '@objectstack/spec/shared';
2020
import {
@@ -459,6 +459,43 @@ export function unclaimedTopLevel(stack: unknown): UnclaimedTopLevel | undefined
459459
* the single-package branch D7 pins.
460460
* @returns How many items this body registered.
461461
*/
462+
/**
463+
* [#28697] The owner-stamp refusal at this door: an item of a judged
464+
* collection ({@link OWNER_STAMP_JUDGED_COLLECTIONS}) whose body already wears
465+
* a `_packageId` other than the body's own package, judged by the one judge
466+
* (`judgeCollectionItemOwnerStamp`, `@objectstack/spec/kernel`) in the
467+
* registration door's words and envelope (`VALIDATION_ERROR` / 400, `issues[]`
468+
* at `_packageId`, the envelope the engine's body seam throws).
469+
*
470+
* Why here: `applyProtection` below stamps an unset key only, so a body's own
471+
* stamp used to be what this door stored, and the engine's boot sync
472+
* (`ObjectQLPlugin.loadMetadataFromService`) files every `app` / `view` /
473+
* `flow` / `hook` it reads back under that stamp. A stack the schema parsed
474+
* was judged at the build door first; a body no parse judged (a config boot's
475+
* residual from a `strict: false` stack) was not, so this door judges every
476+
* body it registers. The collections are the build door's, so neither door
477+
* refuses what the other accepts.
478+
*/
479+
function artifactItemOwnerStampRefusal(
480+
field: string,
481+
metaType: string,
482+
name: string,
483+
item: unknown,
484+
packageId: string | undefined,
485+
): (Error & { code: 'VALIDATION_ERROR'; status: 400; httpStatus: 400; issues: Array<{ path: string; message: string; code: string }> }) | undefined {
486+
if (!(OWNER_STAMP_JUDGED_COLLECTIONS as readonly string[]).includes(field)) return undefined;
487+
const refusal = judgeCollectionItemOwnerStamp(item, metaType, name, packageId, 'register');
488+
if (!refusal) return undefined;
489+
const err = new Error(refusal.message) as Error & {
490+
code: 'VALIDATION_ERROR'; status: 400; httpStatus: 400; issues: Array<{ path: string; message: string; code: string }>;
491+
};
492+
err.code = 'VALIDATION_ERROR';
493+
err.status = 400;
494+
err.httpStatus = 400;
495+
err.issues = [{ path: '_packageId', message: refusal.issueMessage, code: 'custom' }];
496+
return err;
497+
}
498+
462499
async function registerArtifactBodyCollections(
463500
sink: ArtifactRegistrationSink,
464501
body: Record<string, unknown>,
@@ -496,6 +533,8 @@ async function registerArtifactBodyCollections(
496533
// write; row 1 is unchanged for every type.
497534
const nameRefusal = viewContainerNameRefusal(item, sourceLabel, packageId);
498535
if (nameRefusal) throw nameRefusal;
536+
const ownerStamp = artifactItemOwnerStampRefusal(field, metaType, viewObject, item, packageId);
537+
if (ownerStamp) throw ownerStamp;
499538
applyProtection(item as any, {
500539
packageId: packageId,
501540
packageVersion: packageVersion,
@@ -537,6 +576,8 @@ async function registerArtifactBodyCollections(
537576
// it adds there is exactly the leading `object` term this path
538577
// was missing.
539578
const name = slot.name;
579+
const ownerStamp = artifactItemOwnerStampRefusal(field, metaType, name, item, packageId);
580+
if (ownerStamp) throw ownerStamp;
540581
// ADR-0010 §3.7 — translate the author-facing
541582
// `protection` block into the private `_lock` envelope
542583
// and stamp package provenance in one call. Strips the

‎packages/objectql/src/engine-package-registration.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
* `registerMetadataCollections`, `stampObjectPackage`). One home each: they
3838
* stay declared in `engine.ts`, exported in place, and are imported here,
3939
* never copied. Nothing re-exports them from the package entry, so the
40-
* published surface does not move.
40+
* published surface does not move. [#28697] Two more follow the same rule:
41+
* `packageBodyObjectAndAppOwnerStampRefusal` and `registerAppNotingStamp`
42+
* (`registerApp` and `registerPlugin`).
4143
*
4244
* That import is a run-time module cycle (`engine.ts` imports this module,
4345
* this module imports `engine.ts`). Nothing here runs at module evaluation,
@@ -50,7 +52,12 @@
5052
import type { ServiceObject } from '@objectstack/spec/data';
5153
import { applyArtifactForwardConversions, resolveDeclaredRange } from '@objectstack/metadata-core';
5254
import { computeFQN } from './registry.js';
53-
import { stampObjectPackage, stampPackage } from './engine.js';
55+
import {
56+
packageBodyObjectAndAppOwnerStampRefusal,
57+
registerAppNotingStamp,
58+
stampObjectPackage,
59+
stampPackage,
60+
} from './engine.js';
5461
import type { ObjectQL } from './engine.js';
5562

5663
/**
@@ -143,6 +150,18 @@ export function registerPlugin(
143150
// contribute objects UNDER the parent's ownership.
144151
const ownerId = parentId;
145152

153+
// [#28697] The owner-stamp refusal for the objects and the plugin-as-app
154+
// below, before anything of the plugin registers and outside the two
155+
// `try` blocks, which log a registry failure and go on: a body stamp is
156+
// refused as the collection seam refuses it, not logged past.
157+
{
158+
const refusal = packageBodyObjectAndAppOwnerStampRefusal(plugin, ownerId, {
159+
apps: false,
160+
selfAsApp: Boolean(plugin.name && plugin.navigation),
161+
});
162+
if (refusal) throw refusal;
163+
}
164+
146165
// Register objects (supports both Array and Map formats)
147166
if (plugin.objects) {
148167
try {
@@ -172,7 +191,7 @@ export function registerPlugin(
172191
if (plugin.name && plugin.navigation) {
173192
try {
174193
const resolved = pluginNamespace ? this.resolveNavObjectNames(plugin, pluginNamespace) : plugin;
175-
this._registry.registerApp(stampPackage(resolved, ownerId, parentVersion), ownerId);
194+
registerAppNotingStamp(this._registry, stampPackage(resolved, ownerId, parentVersion), ownerId);
176195
this.logger.debug('Registered plugin-as-app', { app: plugin.name, from: pluginName });
177196
} catch (err: any) {
178197
this.logger.warn('Failed to register plugin as app', { pluginName, error: err.message });

‎packages/objectql/src/engine.ts‎

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4910,6 +4910,77 @@ export function collectionItemOwnerStampRefusal(
49104910
return err;
49114911
}
49124912

4913+
/**
4914+
* [#28697] {@link collectionItemOwnerStampRefusal} for the two collections a
4915+
* package body files through verbs of their own rather than the collection
4916+
* loop: its OBJECTS (`registerObject`, the array and the name-keyed map form)
4917+
* and its APPS (`registerApp`: each `apps[]` entry the loop would register,
4918+
* and the body itself when it registers as its own app). The first refusal
4919+
* found, in that order, or `undefined`.
4920+
*
4921+
* Asked BEFORE the body registers anything, so a refused body leaves no
4922+
* package record, object or app behind. An app wearing another package's
4923+
* `_packageId` kept it (`applyProtection` stamps an unset key only), and every
4924+
* reader that asks an app's owner — the per-package list filter, the
4925+
* disabled-package filter, the packaged-base gate — then filed it under the
4926+
* package it named. An object's stamp is read by the action-parse predicate
4927+
* of `registerObject` (`schema._packageId ?? packageId`). Refused, not
4928+
* overwritten, for the reason the collection loop refuses: one judge, so the
4929+
* build door (the stack schema, which judges `objects` and `apps` too) and
4930+
* this door cannot disagree.
4931+
*
4932+
* Called by {@link ObjectQL.registerApp} and by a nested plugin's
4933+
* registration (`registerPlugin`, which registers objects and the plugin
4934+
* itself as an app, and no `apps[]`).
4935+
*
4936+
* @param body The package body or nested plugin, as it arrived.
4937+
* @param ownerId The package the door registers it under.
4938+
* @param scope Which of the app shapes this door registers.
4939+
*/
4940+
export function packageBodyObjectAndAppOwnerStampRefusal(
4941+
body: any,
4942+
ownerId: string | undefined,
4943+
scope: { apps: boolean; selfAsApp: boolean },
4944+
): ReturnType<typeof collectionItemOwnerStampRefusal> {
4945+
const objects = body?.objects;
4946+
if (objects && typeof objects === 'object') {
4947+
const entries: Array<[unknown, unknown]> = Array.isArray(objects)
4948+
? objects.map((def: any) => [def?.name, def])
4949+
: Object.entries(objects);
4950+
for (const [name, def] of entries) {
4951+
const refusal = collectionItemOwnerStampRefusal(def, 'object', typeof name === 'string' && name !== '' ? name : '(unnamed)', ownerId);
4952+
if (refusal) return refusal;
4953+
}
4954+
}
4955+
if (scope.apps && Array.isArray(body?.apps)) {
4956+
for (const app of body.apps) {
4957+
// The loop registers an app with a name only; a nameless one is skipped.
4958+
const appName = app?.name || app?.id;
4959+
if (!appName) continue;
4960+
const refusal = collectionItemOwnerStampRefusal(app, 'app', String(appName), ownerId);
4961+
if (refusal) return refusal;
4962+
}
4963+
}
4964+
if (scope.selfAsApp) {
4965+
const refusal = collectionItemOwnerStampRefusal(body, 'app', String(body?.name), ownerId);
4966+
if (refusal) return refusal;
4967+
}
4968+
return undefined;
4969+
}
4970+
4971+
/**
4972+
* [#28697] Register an app object through the registry's app verb, recording
4973+
* the stamp the registration writes onto the caller's own object
4974+
* ({@link noteRegistrationStamp}): `registerItem` stamps the object in place,
4975+
* so a module constant handed to a second registration arrives wearing the
4976+
* first one's stamp, which no body wrote.
4977+
*/
4978+
export function registerAppNotingStamp(registry: SchemaRegistry, app: any, ownerId: string): void {
4979+
const stampBefore = (app as { _packageId?: unknown } | null | undefined)?._packageId;
4980+
registry.registerApp(app, ownerId);
4981+
noteRegistrationStamp(app, stampBefore);
4982+
}
4983+
49134984
/**
49144985
* [#5320] The registration door's refusal of a `views:` entry that is not a
49154986
* view CONTAINER (`isViewContainerShaped` is false: a ViewItem's
@@ -8930,6 +9001,16 @@ export class ObjectQL implements IObjectQLEngine {
89309001
// Stamped beside `id` on every item below ({@link stampPackage}), so an
89319002
// item served from this path tells an upgrade's new item from its old one.
89329003
const version = manifest.version as string | undefined;
9004+
// [#28697] The owner-stamp refusal for the objects and apps below, which
9005+
// file through verbs of their own (steps 2-4), asked before anything of
9006+
// this body registers. See {@link packageBodyObjectAndAppOwnerStampRefusal}.
9007+
{
9008+
const refusal = packageBodyObjectAndAppOwnerStampRefusal(manifest, id, {
9009+
apps: true,
9010+
selfAsApp: Boolean(manifest.name && manifest.navigation && !manifest.apps?.length),
9011+
});
9012+
if (refusal) throw refusal;
9013+
}
89339014
this.invalidateSummaryIndex(); // new objects may add/change summary fields
89349015
this.logger.debug('Registering package manifest', { id, namespace });
89359016

@@ -9014,7 +9095,7 @@ export class ObjectQL implements IObjectQLEngine {
90149095
const appName = app.name || app.id;
90159096
if (appName) {
90169097
const resolved = namespace ? this.resolveNavObjectNames(app, namespace) : app;
9017-
this._registry.registerApp(stampPackage(resolved, id, version), id);
9098+
registerAppNotingStamp(this._registry, stampPackage(resolved, id, version), id);
90189099
this.logger.debug('Registered App', { app: appName, from: id });
90199100
}
90209101
}
@@ -9024,7 +9105,7 @@ export class ObjectQL implements IObjectQLEngine {
90249105
// This handles the case where the manifest IS the app definition (legacy/simple packages)
90259106
if (manifest.name && manifest.navigation && !manifest.apps?.length) {
90269107
const resolved = namespace ? this.resolveNavObjectNames(manifest, namespace) : manifest;
9027-
this._registry.registerApp(stampPackage(resolved, id, version), id);
9108+
registerAppNotingStamp(this._registry, stampPackage(resolved, id, version), id);
90289109
this.logger.debug('Registered manifest-as-app', { app: manifest.name, from: id });
90299110
}
90309111

0 commit comments

Comments
 (0)