Skip to content

Commit 67c0843

Browse files
committed
fix(metadata-protocol): the adopt-orphans door asks the __c judge against the target; the promotion's suffix block is seal-gated
reassignOrphanedMetadata judges every active object row it would rebind against the target package id with judgeExtensionFieldSuffix, before the first storage update, and refuses the adoption whole with the save door's VALIDATION_ERROR / 400 envelope (ADR-0146 D10 case a). The promotion's suffix block is not asked of sealed managed content, like the D9.9 and namespace-prefix blocks above it. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent db357f6 commit 67c0843

2 files changed

Lines changed: 123 additions & 3 deletions

File tree

‎packages/metadata-protocol/src/protocol.extension-field-suffix.test.ts‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
* [ADR-0146 D10, #28564] The `__c` extension-field reservation at every door
55
* of this package — one judge (`protocol-extension-field-suffix.ts`), asked by
66
* the save door, the restore doors, the draft promotion (`publishMetaItem` and
7-
* the `publishPackageDrafts` batch that shares it) and `duplicatePackage`; and
7+
* the `publishPackageDrafts` batch that shares it), `duplicatePackage` and
8+
* ([#28735]) `reassignOrphanedMetadata`, the adopt-orphans door; and
89
* `installPackage`, which its closed manifest already covers.
910
*
1011
* Maintainer, verbatim: 「扩展字段用 __c 后缀,只有 .custom 包能用。」
@@ -532,6 +533,34 @@ describe('[ADR-0146 D10] the draft promotion refuses an object draft carrying a
532533
});
533534
});
534535

536+
describe('[#28735] the promotion\'s suffix block is not asked of sealed managed content', () => {
537+
/** A code package's shipped object, as `SchemaRegistry.getArtifactItem` stamps it. */
538+
const SHIPPED = 'probe_sealed';
539+
const sealedHarness = () => {
540+
const h = makeProtocol();
541+
h.engine.registry.getArtifactItem = (type: string, name: string) => (type === 'object' && name === SHIPPED
542+
? { name, label: 'Sealed', _packageId: 'com.acme.shipper', _provenance: 'package' }
543+
: undefined);
544+
return h;
545+
};
546+
547+
it('a draft of a shipped object carrying `x__c`, stored before the rule, answers the store\'s NOT_OVERRIDABLE, not "rename the field"', async () => {
548+
const h = sealedHarness();
549+
// The save door refuses this draft (`refusePackagedBaseOverride`), so it is staged as a row stored before the gate.
550+
const row: Row = {
551+
id: 'r_sealed', type: 'object', name: SHIPPED, organization_id: null, package_id: BOUND, state: 'draft',
552+
metadata: JSON.stringify(obj(SHIPPED, 'Sealed', 'x__c')),
553+
};
554+
h.rows.set(keyOf(row as unknown as Record<string, unknown>), row);
555+
556+
const { error } = await attempt(() => h.protocol.publishMetaItem({ type: 'object', name: SHIPPED, packageId: BOUND }));
557+
558+
expect(error).toBeInstanceOf(Error);
559+
expect({ code: error.code, status: error.status }).toEqual({ code: 'NOT_OVERRIDABLE', status: 403 });
560+
expect(storedObjects(h)).toEqual([{ row: SHIPPED, state: 'draft', binding: BOUND, fields: ['title', 'x__c'] }]);
561+
});
562+
});
563+
535564
// ═══════════════════════════════════════════════════════════════════════════
536565
// 4. duplicatePackage, judged against the target id
537566
// ═══════════════════════════════════════════════════════════════════════════
@@ -567,6 +596,56 @@ describe('[ADR-0146 D10] duplicatePackage from a `.custom` pair into a plain id
567596
});
568597
});
569598

599+
// ═══════════════════════════════════════════════════════════════════════════
600+
// 4b. reassignOrphanedMetadata (the adopt-orphans door), judged against the target id
601+
// ═══════════════════════════════════════════════════════════════════════════
602+
603+
describe('[ADR-0146 D10, #28735] the adopt-orphans door judges each object row it rebinds against the target', () => {
604+
/** Package-less active objects through the real save door (not judged: no package), then the write-through spied. */
605+
async function orphans(h: Harness, ...bodies: Array<ReturnType<typeof obj>>) {
606+
for (const item of bodies) await h.protocol.saveMetaItem({ type: 'object', name: item.name, item });
607+
h.saveDoor.mockClear();
608+
return vi.spyOn(h.protocol, 'applyRegistryWriteThrough');
609+
}
610+
611+
it('a package-less object with `x__c` adopted into a plain package is refused with case (a); nothing moves, nothing registers', async () => {
612+
const h = makeProtocol();
613+
const writeThrough = await orphans(h, obj('probe_env', 'Env', 'x__c'));
614+
const update = vi.spyOn(h.engine, 'update');
615+
616+
const { error } = await attempt(() => h.protocol.reassignOrphanedMetadata({ targetPackageId: BOUND }));
617+
618+
expectReservedRefusal(error, 'x__c', 'probe_env', BOUND, 'objects.probe_env.fields.x__c');
619+
expect(update).not.toHaveBeenCalled();
620+
expect(writeThrough).not.toHaveBeenCalled();
621+
expect(storedObjects(h)).toEqual([{ row: 'probe_env', state: 'active', binding: null, fields: ['title', 'x__c'] }]);
622+
});
623+
624+
it('a mixed adoption (one clean row, one `x__c` row) into a plain package is refused whole: the clean row does not move either', async () => {
625+
const h = makeProtocol();
626+
const writeThrough = await orphans(h, obj('probe_clean', 'Clean'), obj('probe_env', 'Env', 'x__c'));
627+
628+
const { error } = await attempt(() => h.protocol.reassignOrphanedMetadata({ targetPackageId: BOUND }));
629+
630+
expectReservedRefusal(error, 'x__c', 'probe_env', BOUND, 'objects.probe_env.fields.x__c');
631+
expect(writeThrough).not.toHaveBeenCalled();
632+
expect(storedObjects(h).map((r) => [r.row, r.binding])).toEqual([['probe_clean', null], ['probe_env', null]]);
633+
});
634+
635+
it('CONTROL: the same `x__c` row adopted into the base\'s `.custom` pair is admitted and moves; a clean row into the plain package too', async () => {
636+
const pairH = makeProtocol();
637+
await orphans(pairH, obj('probe_env', 'Env', 'x__c'));
638+
const res = await pairH.protocol.reassignOrphanedMetadata({ targetPackageId: PAIR });
639+
expect(res).toMatchObject({ success: true, reassignedCount: 1, targetPackageId: PAIR });
640+
expect(storedObjects(pairH)).toEqual([{ row: 'probe_env', state: 'active', binding: PAIR, fields: ['title', 'x__c'] }]);
641+
642+
const plainH = makeProtocol();
643+
await orphans(plainH, obj('probe_clean', 'Clean', 'region'));
644+
expect((await plainH.protocol.reassignOrphanedMetadata({ targetPackageId: BOUND })).reassignedCount).toBe(1);
645+
expect(storedObjects(plainH)).toEqual([{ row: 'probe_clean', state: 'active', binding: BOUND, fields: ['title', 'region'] }]);
646+
});
647+
});
648+
570649
// ═══════════════════════════════════════════════════════════════════════════
571650
// 5. installPackage — covered by its closed manifest
572651
// ═══════════════════════════════════════════════════════════════════════════

‎packages/metadata-protocol/src/protocol.ts‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23881,8 +23881,10 @@ export class ObjectStackProtocolImplementation implements
2388123881
// [ADR-0146 D10, #28564] …and an object draft's fields against the
2388223882
// `__c` reservation, judged against the package promoted into, with the
2388323883
// save door's judge and envelope, before `repo.promoteDraft` writes. See
23884-
// {@link objectBodyExtensionFieldSuffixRefusal}.
23885-
if (draftForGate) {
23884+
// {@link objectBodyExtensionFieldSuffixRefusal}. [#28735] ⛔ Not asked
23885+
// of SEALED managed content, for the D9.9 block's reason: the store's
23886+
// `NOT_OVERRIDABLE` is that row's one answer, not "rename the field".
23887+
if (draftForGate && singularType === 'object' && !this.isSealedManagedItem(singularType, request.name)) {
2388623888
const suffixRefusal = objectBodyExtensionFieldSuffixRefusal(
2388723889
singularType, draftForGate.body, request.name, promoteInto,
2388823890
);
@@ -26757,6 +26759,21 @@ export class ObjectStackProtocolImplementation implements
2675726759
* new binding) until the next boot dropped it again, against the
2675826760
* write-through's own rule that a draft never leaks into the runtime
2675926761
* (ADR-0048). One gate before both limbs now admits active rows only.
26762+
*
26763+
* ## [ADR-0146 D10, #28735] The `__c` reservation, judged against the target
26764+
*
26765+
* A package-unbound row is not judged by the reservation (it is the
26766+
* environment's own), so the save door admits an object carrying `x__c`
26767+
* into no package. Rebinding that row makes `x__c` a field the TARGET
26768+
* package declares on its own object, which case (a) refuses outside a
26769+
* `.custom` pair. So every active `object` row this adoption would rebind
26770+
* is judged against the target id with the save door's judge
26771+
* ({@link judgeExtensionFieldSuffix}) before the first storage update, and
26772+
* any refused field refuses the adoption whole, with the save door's
26773+
* envelope ({@link extensionFieldSuffixRefusalError}, `VALIDATION_ERROR` /
26774+
* 400, `issues[]` locating each field): no row moves and nothing is
26775+
* written through to the registry. An adopted draft is judged when it is
26776+
* promoted (the promotion asks the same judge against its binding).
2676026777
*/
2676126778
async reassignOrphanedMetadata(request: {
2676226779
targetPackageId: string;
@@ -26789,6 +26806,30 @@ export class ObjectStackProtocolImplementation implements
2678926806
&& !this.isArtifactBacked(PLURAL_TO_SINGULAR[r.type] ?? r.type, String(r.name)),
2679026807
);
2679126808

26809+
// [ADR-0146 D10, #28735] ⭐ THE `__c` RESERVATION, JUDGED AGAINST THE
26810+
// TARGET, before the first update below: each active object row this
26811+
// adoption rebinds becomes the target's own object. Refused whole, with
26812+
// the save door's judge and envelope; see the header. An unparseable
26813+
// row is left to the loop, as `duplicatePackage` leaves it.
26814+
{
26815+
const adoptedObjects: Record<string, unknown> = {};
26816+
for (const row of orphans) {
26817+
if (canonicalMetaType(String(row?.type)) !== 'object' || row.state !== 'active') continue;
26818+
if (typeof row.name !== 'string') continue;
26819+
let body: unknown;
26820+
try {
26821+
body = typeof row.metadata === 'string' ? JSON.parse(row.metadata) : row.metadata;
26822+
} catch {
26823+
continue;
26824+
}
26825+
if (body && typeof body === 'object' && !Array.isArray(body)) {
26826+
adoptedObjects[row.name] = { ...(body as Record<string, unknown>), name: row.name };
26827+
}
26828+
}
26829+
const suffixRefusals = judgeExtensionFieldSuffix({ packageId: request.targetPackageId, objects: adoptedObjects });
26830+
if (suffixRefusals.length > 0) throw extensionFieldSuffixRefusalError(suffixRefusals);
26831+
}
26832+
2679226833
const reassigned: Array<{ type: string; name: string }> = [];
2679326834
// [#12981] The refusal channel this loop used to lack entirely.
2679426835
//

0 commit comments

Comments
 (0)