@@ -4812,6 +4812,63 @@ export function permissionSetOwnerRegistrationRefusal(
48124812 return err;
48134813}
48144814
4815+ /**
4816+ * [#28534] The registration door's refusal of a collection item whose body
4817+ * already carries a `_packageId` other than the package it is registered under
4818+ * (`ownerId`; none for a manifest that carries no id).
4819+ *
4820+ * `_packageId` is the ADR-0010 envelope stamp: the package that registered the
4821+ * item, written by the door (`stampPackage` here, `registerItem` behind it,
4822+ * both through `applyProtection`). `applyProtection` writes it only while it is
4823+ * `undefined`, and every item schema that spreads `MetadataProtectionFields`
4824+ * (a connector entry among them) declares the key, so a body could arrive
4825+ * wearing ANOTHER package's id and keep it: an installed package naming the
4826+ * host's own id, or an id no package holds, and every reader that asks an
4827+ * item's owner (the connector materializer judging whose root a file ref
4828+ * resolves against, the disabled-package filter, the ADR-0130 artifact gates)
4829+ * then reads a stamp the body wrote about itself.
4830+ *
4831+ * Refused rather than overwritten, beside the two other body-against-key
4832+ * refusals of the same loop (the divergent view-container `name`, the
4833+ * permission set's `packageId`): resolving a disagreement between the body and
4834+ * the key it is filed under silently, in either direction, files the item
4835+ * under something nobody wrote (commit d0ee598e6). A stamp EQUAL to the owner
4836+ * is accepted — it is what an item re-registered under its own package (a
4837+ * ledger rehydrate, an artifact the metadata door already stamped in place
4838+ * with the same body id) carries. `null` and the empty string are stamps too:
4839+ * neither is overwritten by `applyProtection`, so either would survive as an
4840+ * owner no row answers.
4841+ *
4842+ * `VALIDATION_ERROR` / 400, `issues[]` at `_packageId`: the envelope of
4843+ * {@link permissionSetOwnerRegistrationRefusal}. Exported for the stack's
4844+ * RESIDUAL registration (`registerUnclaimedTopLevelItems` in `plugin.ts`),
4845+ * which files top-level items under the residual owner and refuses the same
4846+ * stamp in this same envelope. ⛔ Never rebuild the envelope at a second door.
4847+ */
4848+ export function collectionItemOwnerStampRefusal(
4849+ item: unknown,
4850+ type: string,
4851+ name: string,
4852+ ownerId: string | undefined,
4853+ ): (Error & { code: 'VALIDATION_ERROR'; status: 400; httpStatus: 400; issues: Array<{ path: string; message: string; code: string }> }) | undefined {
4854+ if (!item || typeof item !== 'object') return undefined;
4855+ const stamped = (item as { _packageId?: unknown })._packageId;
4856+ if (stamped === undefined || stamped === ownerId) return undefined;
4857+ const owner = ownerId === undefined ? 'no package' : `package '${ownerId}'`;
4858+ const issueMessage = `carries _packageId ${JSON.stringify(stamped)}, but it is registered under ${owner}`;
4859+ const err = new Error(
4860+ `Drop \`_packageId\` from ${type} '${name}': it ${issueMessage}. The package an item belongs to is the one `
4861+ + 'that registers it, stamped by the registration; a body never names its own owner.',
4862+ ) as Error & {
4863+ code: 'VALIDATION_ERROR'; status: 400; httpStatus: 400; issues: Array<{ path: string; message: string; code: string }>;
4864+ };
4865+ err.code = 'VALIDATION_ERROR';
4866+ err.status = 400;
4867+ err.httpStatus = 400;
4868+ err.issues = [{ path: '_packageId', message: issueMessage, code: 'custom' }];
4869+ return err;
4870+ }
4871+
48154872/**
48164873 * [#5320] The registration door's refusal of a `views:` entry that is not a
48174874 * view CONTAINER (`isViewContainerShaped` is false: a ViewItem's
@@ -5049,6 +5106,40 @@ export class ObjectQL implements IObjectQLEngine {
50495106 // Package manifests registry (for defaultDatasource lookup)
50505107 private manifests = new Map<string, any>();
50515108
5109+ // [#28534] Where each ADR-0130 residual owner came from — see
5110+ // {@link recordResidualOwner}.
5111+ private readonly residualOwnerSources = new Map<string, 'code' | 'marketplace'>();
5112+
5113+ /**
5114+ * [#28534] Record the origin of an artifact's RESIDUAL owner: the id the
5115+ * residual rule files a multi-package stack's unclaimed top-level items
5116+ * under (`unclaimedTopLevel`, `@objectstack/metadata`), as the door that
5117+ * registered the artifact states it — `'code'` for the deployment's own
5118+ * boot, `'marketplace'` for an install.
5119+ *
5120+ * The residual is not a package, so no package row records that id (unless
5121+ * a body carries it, and then the body's row answers), yet its items wear it
5122+ * as their `_packageId`. Without this record a reader asking an item's
5123+ * origin could not tell the host's own residual from an installed bundle's.
5124+ * Called by the `manifest` service, the one place that knows both the
5125+ * residual's id and the door's statement. Once an id is recorded as
5126+ * `'marketplace'` it stays so for this engine: an install can never turn
5127+ * into the host's own by re-registering under the same id.
5128+ */
5129+ recordResidualOwner(ownerId: string, source: 'code' | 'marketplace'): void {
5130+ if (this.residualOwnerSources.get(ownerId) === 'marketplace') return;
5131+ this.residualOwnerSources.set(ownerId, source);
5132+ }
5133+
5134+ /**
5135+ * [#28534] The origin {@link recordResidualOwner} recorded for a residual
5136+ * owner id, or `undefined` when no artifact this engine registered named it
5137+ * as its residual's owner.
5138+ */
5139+ residualOwnerSource(ownerId: string): 'code' | 'marketplace' | undefined {
5140+ return this.residualOwnerSources.get(ownerId);
5141+ }
5142+
50525143 // Datasource definitions by name (ADR-0015): carries schemaMode +
50535144 // external.allowWrites so the write gate (Gate 3) can enforce federation
50545145 // ownership, and external.credentialsRef so a sys_secret reference sweep can
@@ -8921,6 +9012,10 @@ export class ObjectQL implements IObjectQLEngine {
89219012 this.logger.debug('Registering seed data datasets', { id, count: seedData.length });
89229013 for (const dataset of seedData) {
89239014 if (dataset.object) {
9015+ // [#28534] The collection loop's owner-stamp refusal, for the
9016+ // one collection filed outside it.
9017+ const ownerStamp = collectionItemOwnerStampRefusal(dataset, 'data', String(dataset.object), id);
9018+ if (ownerStamp) throw ownerStamp;
89249019 this._registry.registerItem('data', stampPackage(dataset, id, version), 'object' as any, id);
89259020 }
89269021 }
@@ -9113,6 +9208,15 @@ export class ObjectQL implements IObjectQLEngine {
91139208 const refusal = permissionSetOwnerRegistrationRefusal(item, itemName, ownerId);
91149209 if (refusal) throw refusal;
91159210 }
9211+ // [#28534] …and an item of ANY collection whose body already wears
9212+ // a `_packageId` other than `ownerId`: `stampPackage` and
9213+ // `registerItem` stamp the owner only while the key is unset, so
9214+ // the body's own stamp used to win. See
9215+ // {@link collectionItemOwnerStampRefusal}.
9216+ {
9217+ const refusal = collectionItemOwnerStampRefusal(item, pluralToSingular(key), itemName, ownerId);
9218+ if (refusal) throw refusal;
9219+ }
91169220 const toRegister = item.name === itemName ? item : { ...item, name: itemName };
91179221 // [#5320] The `views:` tighten — containers ONLY, the contract the
91189222 // stack schema has always declared (`stack.zod.ts`,
@@ -9186,6 +9290,8 @@ export class ObjectQL implements IObjectQLEngine {
91869290 this.logger.warn('Skipping viewItems entry without a derivable name', { id: ownerId });
91879291 continue;
91889292 }
9293+ const ownerStamp = collectionItemOwnerStampRefusal(body, 'view', itemName, ownerId);
9294+ if (ownerStamp) throw ownerStamp;
91899295 const toRegister = body.name === itemName ? body : { ...body, name: itemName };
91909296 this._registry.registerItem('view', stampPackage(toRegister, ownerId, ownerVersion), 'name' as any, ownerId);
91919297 }
0 commit comments