Skip to content

Commit d71dd76

Browse files
committed
fix(metadata-protocol): close only the package-less writes the ceremony moves
The package-less save, publish, delete and rollback onto a packaged item of the five overlay types are refused only where `os migrate packageless-overlays` would classify the row (or a first write's body) movable and its move can succeed: the census's own classification, the ensure door's refusals, the base's version and the override write gate for the pair. A name several packages ship, a base with no pair or an unensurable one, a stored row with a foreign declaration or one the pair shadows, and an email_template locale the base does not ship keep their package-less write. The ceremony's remedies for multi-shipped and shadowed rows name only doors that do what they say. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5ni5yjiTswP6crgAkX
1 parent 55478e2 commit d71dd76

12 files changed

Lines changed: 477 additions & 172 deletions

‎packages/cli/src/commands/migrate/packageless-overlays.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,19 @@ function reasonOf(row: CensusRow): string {
5858
switch (row.disposition) {
5959
case 'multi_shipped':
6060
return `several packages ship this name (${row.shippers.join(', ')}), so it overlays none of them for certain. `
61-
+ 'Left as it is: move it by hand into the pair of the package it customizes, or delete it.';
61+
+ 'Left as it is: delete it (a delete naming no package removes it while no package holds a row of '
62+
+ 'this name), then, to keep the customization, save it into the pair of the package it customizes.';
6263
case 'no_pair':
6364
return `'${row.base}' has no paired .custom package id, so there is nowhere to move it. Left as it is.`;
6465
case 'foreign_declaration':
6566
return `its body already carries an \`overrides\` key that does not name '${row.base}'. Left as it is: `
6667
+ 'correct or remove the key, then re-run.';
6768
case 'shadowed':
6869
return `'${row.pair}' already ${row.shadowDeclares ? 'overrides this item (a customization made in Studio)' : 'holds a row of this name'}, `
69-
+ 'and that row is the one served — this row is never merged into it. Two ways out: keep the Studio '
70-
+ `customization and delete this package-less row (id ${row.id}), or use "Restore package original" in `
71-
+ `Studio to delete '${row.pair}''s row and re-run this command to move this one.`;
70+
+ 'and that row is the one served — this row is never merged into it. Left as it is: to keep the Studio '
71+
+ 'customization, nothing is owed (this row is not served while that one stands); to make this row the '
72+
+ `customization instead, use "Restore package original" in Studio to delete '${row.pair}''s row, then `
73+
+ 're-run this command to move this one.';
7274
default:
7375
return '';
7476
}

‎packages/metadata-protocol/src/migrations/overlay-index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,11 @@ function overlayDiscriminatorOf(type: string): { field: string; canonical: strin
501501
return ITEM_KEY_DISCRIMINATORS[PLURAL_TO_SINGULAR[type] ?? type];
502502
}
503503

504+
/** The body field that names `type`'s bundle member (`email_template`: `locale`), or `undefined` for a type with none. */
505+
export function overlayMemberField(type: string): string | undefined {
506+
return overlayDiscriminatorOf(type)?.field;
507+
}
508+
504509
/**
505510
* The value {@link OVERLAY_MEMBER_COLUMN} holds for the member a caller names.
506511
*

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

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export interface CustomizationPackageHost {
128128
}
129129

130130
/** The registry slice the door and the predicate read. */
131-
interface PairRegistry {
131+
export interface PairRegistry {
132132
getPackage(id: string): PairPackageRow | undefined;
133133
getNamespaceOwners?(namespace: string): string[];
134134
}
@@ -289,34 +289,41 @@ export async function ensureCustomizationPackage(
289289
}
290290
}
291291

292-
/** One judgement of the registry, and the create when the pair is absent. */
293-
async function ensurePairOnce(
294-
this: CustomizationPackageHost,
292+
/**
293+
* [ADR-0146 D11, #28576] Why the ensure door would refuse to ensure `baseId`'s
294+
* pair on this registry now, or `undefined` when it would answer the existing
295+
* pair or create it. ONE reading of the door's registry-decidable refusals:
296+
* {@link ensurePairOnce} throws the error this answers, and the close of the
297+
* package-less write (`packagelessOverlayWriteRefusal`) leaves a name's
298+
* package-less write open where it answers one — `os migrate
299+
* packageless-overlays` could not move such a row (its step 1 fails). Pure
300+
* over the registry; it writes nothing.
301+
*/
302+
export function customizationPairEnsureRefusal(
303+
engine: ObjectStackProtocolImplementation['engine'],
295304
registry: PairRegistry,
296305
baseId: string,
297306
pairId: string,
298-
): Promise<EnsureCustomizationPackageResponse> {
307+
): Error | undefined {
299308
const base = registry.getPackage(baseId);
300309
if (!base) {
301-
throw refusal(404, `Package '${baseId}' not found: a paired customization package is ensured for an installed `
310+
return refusal(404, `Package '${baseId}' not found: a paired customization package is ensured for an installed `
302311
+ 'base.');
303312
}
304313
// ADR-0070 D2's one predicate. A writable base is the customer's own and
305314
// takes their changes directly; the pair exists for a base the customer
306315
// cannot author into.
307-
if (isWritablePackage(this.engine, baseId)) {
308-
throw refusal(409, `Package '${baseId}' is writable: customize it directly. A paired customization package `
316+
if (isWritablePackage(engine, baseId)) {
317+
return refusal(409, `Package '${baseId}' is writable: customize it directly. A paired customization package `
309318
+ `('${pairId}') holds the changes to a read-only base — one provided by code or an installed app `
310319
+ '(ADR-0146 D3).');
311320
}
312321
const namespace = customizationNamespace(base.manifest?.namespace);
313322
if (!namespace.ok) {
314-
throw refusal(409, `Cannot ensure '${pairId}': ${noPairNamespaceClause(baseId, base.manifest?.namespace, namespace.reason)} `
323+
return refusal(409, `Cannot ensure '${pairId}': ${noPairNamespaceClause(baseId, base.manifest?.namespace, namespace.reason)} `
315324
+ '(ADR-0146 D3).');
316325
}
317-
318-
// Idempotent: an existing pair is returned unchanged, or refused naming
319-
// what it lacks — ⛔ never adopted, never rewritten.
326+
// An existing pair is returned unchanged, or refused naming what it lacks.
320327
const existing = registry.getPackage(pairId);
321328
if (existing) {
322329
const lacks: string[] = [];
@@ -326,24 +333,16 @@ async function ensurePairOnce(
326333
}
327334
const declaredRange = declaredRangeOn(existing.manifest, baseId);
328335
if (declaredRange === undefined) lacks.push(`a dependency on '${baseId}' in manifest.dependencies`);
329-
if (lacks.length > 0 || declaredRange === undefined) {
330-
throw refusal(409, `Package '${pairId}' is installed but is not the paired customization package of `
336+
if (lacks.length > 0) {
337+
return refusal(409, `Package '${pairId}' is installed but is not the paired customization package of `
331338
+ `'${baseId}': it lacks ${lacks.join(', and ')}. It is not adopted or rewritten — delete it, or `
332339
+ 'install a manifest over it that declares both, then ensure the pair again (ADR-0146 D3).');
333340
}
334-
return {
335-
basePackageId: baseId,
336-
packageId: pairId,
337-
namespace: namespace.value,
338-
dependencyRange: declaredRange,
339-
created: false,
340-
writable: isWritablePackage(this.engine, pairId),
341-
};
341+
return undefined;
342342
}
343-
344343
const range = customizationDependencyRange(base.manifest?.version);
345344
if (!range.ok) {
346-
throw refusal(409, `Cannot ensure '${pairId}': package '${baseId}' is at version `
345+
return refusal(409, `Cannot ensure '${pairId}': package '${baseId}' is at version `
347346
+ `'${String(base.manifest?.version)}', which is not a SemVer 2.0.0 version, so the pair's dependency `
348347
+ 'range — the major of the base\'s version — cannot be declared (ADR-0146 D3).');
349348
}
@@ -352,12 +351,44 @@ async function ensurePairOnce(
352351
// would read.
353352
const owner = registry.getNamespaceOwners?.(namespace.value)?.find((id) => id !== pairId);
354353
if (owner !== undefined) {
355-
throw refusal(422, `Cannot ensure '${pairId}': its namespace '${namespace.value}' is already owned by package `
354+
return refusal(422, `Cannot ensure '${pairId}': its namespace '${namespace.value}' is already owned by package `
356355
+ `'${owner}'. The pair's namespace is the base's followed by '_custom' and has no alternative; uninstall `
357356
+ `'${owner}' or move it off that namespace, then ensure the pair again (ADR-0146 D3).`,
358357
'NAMESPACE_CONFLICT');
359358
}
359+
return undefined;
360+
}
360361

362+
/** One judgement of the registry, and the create when the pair is absent. */
363+
async function ensurePairOnce(
364+
this: CustomizationPackageHost,
365+
registry: PairRegistry,
366+
baseId: string,
367+
pairId: string,
368+
): Promise<EnsureCustomizationPackageResponse> {
369+
// Every refusal the registry decides, read once ({@link customizationPairEnsureRefusal}).
370+
const refused = customizationPairEnsureRefusal(this.engine, registry, baseId, pairId);
371+
if (refused) throw refused;
372+
const base = registry.getPackage(baseId)!;
373+
const namespace = customizationNamespace(base.manifest?.namespace);
374+
if (!namespace.ok) throw new Error('unreachable: the namespace was judged above');
375+
376+
// Idempotent: an existing pair is returned unchanged — ⛔ never adopted,
377+
// never rewritten (its refusal is above).
378+
const existing = registry.getPackage(pairId);
379+
if (existing) {
380+
return {
381+
basePackageId: baseId,
382+
packageId: pairId,
383+
namespace: namespace.value,
384+
dependencyRange: declaredRangeOn(existing.manifest, baseId)!,
385+
created: false,
386+
writable: isWritablePackage(this.engine, pairId),
387+
};
388+
}
389+
390+
const range = customizationDependencyRange(base.manifest?.version);
391+
if (!range.ok) throw new Error('unreachable: the version was judged above');
361392
const baseName = typeof base.manifest?.name === 'string' && base.manifest.name !== '' ? base.manifest.name : baseId;
362393
const manifest = {
363394
id: pairId,

‎packages/metadata-protocol/src/protocol-packaged-base-refusal.ts‎

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ import { ITEM_KEY_DISCRIMINATORS, itemDiscriminator } from '@objectstack/metadat
5959
*/
6060
export interface PackagedBaseRefusalHost {
6161
isSealedManagedItem: ObjectStackProtocolImplementation['isSealedManagedItem'];
62-
overlayBaseShippers: ObjectStackProtocolImplementation['overlayBaseShippers'];
6362
isWritablePackage: ObjectStackProtocolImplementation['isWritablePackage'];
6463
packagedBaseRefusal: ObjectStackProtocolImplementation['packagedBaseRefusal'];
6564
packagedArtifactOwner: ObjectStackProtocolImplementation['packagedArtifactOwner'];
@@ -217,66 +216,44 @@ function sealedDeclareOrRenameClause(type: string, basePackageId: unknown, write
217216
export type PackagelessWriteOperation = 'save' | 'publish' | 'delete' | 'rollback';
218217

219218
/**
220-
* The body of `ObjectStackProtocolImplementation#packagelessOverlayWriteRefusal`;
221-
* the method's docblock there is its contract.
219+
* The refusal `ObjectStackProtocolImplementation#packagelessOverlayWriteRefusal`
220+
* throws; the method's docblock there is its contract.
222221
*
223-
* [ADR-0146 D11] The closure of the package-less write onto a packaged item:
224-
* a write naming no package (`packageId` absent, `null` or empty) onto an item
225-
* of one of the five overlay types that a read-only code package ships
226-
* ({@link PackagedBaseRefusalHost.overlayBaseShippers}, the reading
227-
* `os migrate packageless-overlays` classifies by), is refused
228-
* `403 NOT_OVERRIDABLE` — the code and status the sealed write already
229-
* answers, so no door gains a code. Every other type's packaged item is
230-
* sealed already ({@link refusePackagedBaseOverride}).
231-
*
232-
* The sentence names the fix the writer has: the base's paired `.custom`
222+
* [ADR-0146 D11] A write naming no package (`packageId` absent, `null` or
223+
* empty) onto an item of one of the five overlay types that one read-only code
224+
* package ships, whose row `os migrate packageless-overlays` would move into
225+
* the base's pair (`packagelessCloseSubject`, the ceremony's own reading), is
226+
* refused `403 NOT_OVERRIDABLE` — the code and status the sealed write already
227+
* answers, so no door gains a code. Every row the ceremony leaves keeps its
228+
* package-less write, so the sentence's two doors are always open: the pair
233229
* (`?package=<base>.custom`), where the customization is a declared override,
234-
* and `os migrate packageless-overlays`, the ceremony that moves a
235-
* package-less row stored before the close there. A row that ceremony has not
236-
* moved yet is still served (the read path is not this function's); only its
237-
* package-less write is refused, by this same sentence.
230+
* and the ceremony, which moves a row stored before the close there. Such a
231+
* row is still served until it is moved (the read path is not this
232+
* function's); only its package-less write is refused.
238233
*
239-
* `null` when the write names a package; when no read-only code package ships
240-
* the name (environment metadata keeps its package-less write, ADR-0146 D11
241-
* "is not touched"); and when no base that ships it has a pair at all
242-
* (`customizationPackageId` refuses every one of their ids): the ceremony
243-
* lists such a row `no_pair` and cannot move it, and D11 closes only what it
244-
* has migrated — closing it would leave that customization no write door.
234+
* The fix first, then the rule and the ceremony: the REST door bounds a
235+
* refusal's text (`CLIENT_MESSAGE_MAX`, 500 characters, `@objectstack/types`),
236+
* and the metadata item page shows that text as sent.
245237
*/
246238
export function packagelessOverlayWriteRefusal(
247-
this: Pick<PackagedBaseRefusalHost, 'overlayBaseShippers'>,
248-
request: { type: string; name: string; packageId?: string | null },
239+
request: { type: string; name: string },
240+
closed: { base: string; pair: string },
249241
operation: PackagelessWriteOperation,
250-
): Error | null {
251-
if (typeof request.packageId === 'string' && request.packageId.length > 0) return null;
252-
const shippers = this.overlayBaseShippers(request.type, request.name);
253-
const pairs = shippers.flatMap((base) => {
254-
const derived = customizationPackageId(base);
255-
return derived.ok ? [{ base, pair: derived.value }] : [];
256-
});
257-
if (pairs.length === 0) return null;
242+
): Error {
258243
const type = PLURAL_TO_SINGULAR[request.type] ?? request.type;
259244
const item = `'${type}/${request.name}'`;
260-
const shipped = `ships in ${shippers.map((id) => `'${id}'`).join(' and ')}`;
261-
const sole = pairs.length === 1 ? pairs[0]! : undefined;
262-
// One base: its pair, by name. Several: the pair of the base the writer
263-
// customizes, each named — never guessed (the ceremony lists such a row).
264-
const pair = sole ? `'${sole.pair}' (?package=${sole.pair})` : `the pair of the package it customizes (${pairs.map((p) => `'${p.pair}'`).join(' or ')})`;
265-
const declared = sole ? `overrides: { package: '${sole.base}', basedOn }` : 'overrides: { package: <that base>, basedOn }';
266-
// The fix first, then the rule and the ceremony: the REST door bounds a
267-
// refusal's text (`CLIENT_MESSAGE_MAX`, 500 characters, `@objectstack/types`),
268-
// and Setup shows that text as sent.
269-
const ceremony = sole
270-
? `\`os migrate packageless-overlays\` moves it into '${sole.pair}'`
271-
: '`os migrate packageless-overlays` lists it for you to place';
272-
const closed = 'A write naming no package onto a packaged item is closed (ADR-0146 D11); '
273-
+ `a package-less customization stored before it is still served until ${ceremony}.`;
245+
const shipped = `ships in '${closed.base}'`;
246+
const pair = `'${closed.pair}' (?package=${closed.pair})`;
247+
const declared = `overrides: { package: '${closed.base}', basedOn }`;
248+
const rule = 'A write naming no package onto a packaged item is closed (ADR-0146 D11); '
249+
+ 'a package-less customization stored before it is still served until '
250+
+ `\`os migrate packageless-overlays\` moves it into '${closed.pair}'.`;
274251
const message = operation === 'rollback'
275252
? `${item} ${shipped}, and a rollback of its package-less row is closed (ADR-0146 D11): run `
276253
+ `\`os migrate packageless-overlays\` to move the row into ${pair}, then roll it back there.`
277254
: operation === 'delete'
278-
? `${item} ${shipped}: remove its customization from ${pair}. ${closed}`
279-
: `${item} ${shipped}: ${operation === 'publish' ? 'publish' : 'save'} it into ${pair}, declaring ${declared}. ${closed}`;
255+
? `${item} ${shipped}: remove its customization from ${pair}. ${rule}`
256+
: `${item} ${shipped}: ${operation === 'publish' ? 'publish' : 'save'} it into ${pair}, declaring ${declared}. ${rule}`;
280257
const err = new Error(message);
281258
(err as any).code = 'NOT_OVERRIDABLE';
282259
(err as any).status = 403;

0 commit comments

Comments
 (0)