Skip to content

Commit 408cc35

Browse files
committed
refactor(metadata-protocol): fold the two overlay-write closes' fix-can-be-taken steps into one predicate
namedBaseCloseSubject and packagelessCloseSubject repeated the same four steps (the pair id, the ensure door, the base's version, the override write gate on the declared body). overlayPairFixOf / overlayPairFixAdmits now hold them once in protocol-packaged-base-refusal.ts; each close keeps only its own difference (which base, which body, the package-less close's row read between the two halves). Move-only: no refusal and no call order changes. Claude-Session: https://claude.ai/code/session_01JQwxbZz2XXsYuaJz9sPuGU Co-authored-by: Claude <noreply@anthropic.com>
1 parent 0dc436c commit 408cc35

3 files changed

Lines changed: 112 additions & 52 deletions

File tree

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

Lines changed: 88 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,82 @@ export function namedBaseOverlayWriteRefusal(
339339
return closedOverlayWriteError(message);
340340
}
341341

342+
// ─────────────────────────────────────────────────────────────────────────
343+
// ADR-0146 D1, D11 — "the fix the close names can be taken": one predicate
344+
// ─────────────────────────────────────────────────────────────────────────
345+
346+
/** The pair an overlay-write close sends the write into, and the `basedOn` its declaration records. */
347+
export interface OverlayPairFix {
348+
readonly base: string;
349+
readonly pair: string;
350+
readonly basedOn: string;
351+
}
352+
353+
/**
354+
* [ADR-0146 D1, D11] The registry half of whether the fix both overlay-write
355+
* closes name — write it into `base`'s pair, declaring the override — can be
356+
* taken, or `null` when it cannot: read from the steps that write takes.
357+
*
358+
* - the base has a pair id (`customizationPackageId`; an id ending in
359+
* `.custom` has none);
360+
* - the ensure door would answer or create the pair
361+
* ({@link customizationPairEnsureRefusal});
362+
* - the base records a version (`basedOn`, its `manifest.version`, the value
363+
* the ceremony records too).
364+
*
365+
* The other half is {@link overlayPairFixAdmits}, the override write gate on
366+
* the declared body. Two halves of one predicate so that a close may read its
367+
* own subject between them — the package-less close reads the item's stored
368+
* rows only for a name the registry has not already left open — and ⛔ each
369+
* close keeps only its own difference: which base it judges, and which body.
370+
* {@link namedBaseCloseSubject} and `packagelessCloseSubject`
371+
* (`protocol-packageless-overlay-migration.ts`) ask both. Registry reads only.
372+
*/
373+
export function overlayPairFixOf(
374+
host: { readonly engine: ObjectStackProtocolImplementation['engine'] },
375+
base: string,
376+
): OverlayPairFix | null {
377+
const pairId = customizationPackageId(base);
378+
if (!pairId.ok) return null; // no pair to name (the census's `no_pair`)
379+
const pair = pairId.value;
380+
const registry = (host.engine as { registry?: PairRegistry } | undefined)?.registry;
381+
if (!registry || typeof registry.getPackage !== 'function') return null;
382+
if (customizationPairEnsureRefusal(host.engine, registry, base, pair) !== undefined) return null; // the ensure door would refuse
383+
const basedOn = registry.getPackage(base)?.manifest?.version;
384+
if (typeof basedOn !== 'string' || basedOn === '') return null; // no `basedOn` to declare
385+
return { base, pair, basedOn };
386+
}
387+
388+
/**
389+
* [ADR-0146 D1, D2, D11] The gate half of {@link overlayPairFixOf}: whether
390+
* the override write gate admits the body the fix writes into the pair — the
391+
* close's `body` (a save's, or the stored row's), else the item's bare
392+
* identity at `member`, declaring `overrides: { package: base, basedOn }`. An
393+
* `email_template` member at a locale the base does not ship is refused
394+
* there, so its write stays open.
395+
*/
396+
export function overlayPairFixAdmits(
397+
host: { readonly overrideWriteVerdict: (request: OverrideWriteSubject) => OverrideWriteVerdict },
398+
fix: OverlayPairFix,
399+
subject: {
400+
/** The singular type. */
401+
readonly type: string;
402+
readonly name: string;
403+
readonly body: Record<string, unknown> | undefined;
404+
/** The bundle member, as `OVERLAY_MEMBER_COLUMN` holds it. */
405+
readonly member: string | null | undefined;
406+
},
407+
): boolean {
408+
const { type, name, body, member } = subject;
409+
const disc = member === undefined ? undefined : overlayMemberField(type);
410+
const declaredBody = {
411+
...(body ?? { name, ...(disc && member !== null ? { [disc]: member } : {}) }),
412+
overrides: { package: fix.base, basedOn: fix.basedOn },
413+
};
414+
const verdict = host.overrideWriteVerdict({ type, name, packageId: fix.pair, item: declaredBody });
415+
return verdict.verdict !== 'refuse';
416+
}
417+
342418
/** The class members {@link namedBaseCloseSubject} reads, typed as the class declares them. */
343419
export interface NamedBaseCloseHost {
344420
readonly engine: ObjectStackProtocolImplementation['engine'];
@@ -367,16 +443,15 @@ export interface NamedBaseWriteSubject {
367443
* overlay types — the package-less close's reading of "packaged"
368444
* (`overlayBaseShippers`, the reading `os migrate packageless-overlays`
369445
* classifies by), with the named package among the shippers — and where the
370-
* fix the refusal names can be taken, read from the steps that write takes:
446+
* fix the refusal names can be taken: {@link overlayPairFixOf} and
447+
* {@link overlayPairFixAdmits}, the predicate the package-less close asks too
448+
* (a pair id, the ensure door, a base version, the override write gate
449+
* admitting the declared body — an `email_template` member at a locale the
450+
* base does not ship stays open, as the package-less one does).
371451
*
372-
* - the base has a pair id (`customizationPackageId`);
373-
* - the ensure door would answer or create the pair
374-
* ({@link customizationPairEnsureRefusal});
375-
* - the base records a version (`basedOn`, its `manifest.version`, the value
376-
* the ceremony records too);
377-
* - the override write gate admits the declared body into the pair (an
378-
* `email_template` member at a locale the base does not ship is refused
379-
* there, so its write stays open, as the package-less one does).
452+
* This close's own difference: it reads no row (the declared body is the
453+
* save's, else the item's identity), and the named package may be one of
454+
* several shippers.
380455
*
381456
* A body carrying `overrides` is not judged here: the D2 gate answers a
382457
* declaration outside the pair, the schema gate a malformed one, as before.
@@ -398,29 +473,17 @@ export function namedBaseCloseSubject(
398473
// a malformed one (`422 INVALID_METADATA`).
399474
if (isPlainRecord(request.item) && 'overrides' in request.item) return null;
400475
if (!this.overlayBaseShippers(request.type, request.name).includes(base)) return null;
401-
const pairId = customizationPackageId(base);
402-
if (!pairId.ok) return null; // no pair to name
403-
const pair = pairId.value;
404-
const registry = (this.engine as { registry?: PairRegistry } | undefined)?.registry;
405-
if (!registry || typeof registry.getPackage !== 'function') return null;
406-
if (customizationPairEnsureRefusal(this.engine, registry, base, pair) !== undefined) return null;
407-
const basedOn = registry.getPackage(base)?.manifest?.version;
408-
if (typeof basedOn !== 'string' || basedOn === '') return null; // no `basedOn` to declare
476+
const fix = overlayPairFixOf(this, base);
477+
if (fix === null) return null;
409478

410479
// The gate's verdict on the declared body, written into the pair.
411480
const type = PLURAL_TO_SINGULAR[request.type] ?? request.type;
412481
const written = isPlainRecord(request.item) ? request.item : undefined;
413482
const member = written !== undefined && request.member === undefined
414483
? overlayMemberOfBody(type, written)
415484
: overlayMemberValue(type, request.member);
416-
const disc = member === undefined ? undefined : overlayMemberField(type);
417-
const declaredBody = {
418-
...(written ?? { name: request.name, ...(disc && member !== null ? { [disc]: member } : {}) }),
419-
overrides: { package: base, basedOn },
420-
};
421-
const verdict = this.overrideWriteVerdict({ type, name: request.name, packageId: pair, item: declaredBody });
422-
if (verdict.verdict === 'refuse') return null;
423-
return { base, pair };
485+
if (!overlayPairFixAdmits(this, fix, { type, name: request.name, body: written, member })) return null;
486+
return { base, pair: fix.pair };
424487
}
425488

426489
// ─────────────────────────────────────────────────────────────────────────

‎packages/metadata-protocol/src/protocol-packageless-overlay-migration.ts‎

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,15 @@ import {
131131
} from '@objectstack/spec/kernel';
132132
import { PLURAL_TO_SINGULAR, SINGULAR_TO_PLURAL } from '@objectstack/spec/shared';
133133
import { storedRowDocument } from './item-lock.js';
134-
import { OVERLAY_MEMBER_COLUMN, overlayMemberField, overlayMemberOfBody, overlayMemberValue } from './migrations/overlay-index.js';
134+
import { OVERLAY_MEMBER_COLUMN, overlayMemberOfBody, overlayMemberValue } from './migrations/overlay-index.js';
135135
import type { ObjectStackProtocolImplementation } from './protocol.js';
136-
import { customizationPairEnsureRefusal, type PairRegistry } from './protocol-customization-package.js';
137-
import { overrideWriteMemberOf, type OverrideWriteSubject, type OverrideWriteVerdict } from './protocol-packaged-base-refusal.js';
136+
import {
137+
overlayPairFixAdmits,
138+
overlayPairFixOf,
139+
overrideWriteMemberOf,
140+
type OverrideWriteSubject,
141+
type OverrideWriteVerdict,
142+
} from './protocol-packaged-base-refusal.js';
138143
import { readOnlyShippingPackages, shippingPackageIds } from './protocol-environment-rows.js';
139144
import { isServedOverrideRowOf } from './served-override.js';
140145
import { SysMetadataRepository } from './sys-metadata-repository.js';
@@ -710,14 +715,15 @@ export interface PackagelessWriteSubject {
710715
* read-only package ships). `shadowed` applies to a stored row only: a
711716
* first write has nothing in the pair to be shadowed by;
712717
* - its move must be able to succeed, read from the steps the ceremony
713-
* takes: the ensure door would ensure the pair
714-
* ({@link customizationPairEnsureRefusal}), the base records a version
715-
* (`basedOn`), and the override write gate admits the declared body into
716-
* the pair. An `email_template` member at a locale the base does not ship
717-
* is refused there (#28777) and stays open: the ceremony moves such a row
718-
* as the pair's own member, declaring nothing (#28904), and the close
719-
* reaches only the declared-override moves, so its package-less write is
720-
* the one it had before the close.
718+
* takes: `overlayPairFixOf` and `overlayPairFixAdmits`
719+
* (`protocol-packaged-base-refusal.ts`), the predicate the close of the
720+
* write naming the base asks too — the ensure door would ensure the pair,
721+
* the base records a version (`basedOn`), and the override write gate
722+
* admits the declared body into the pair. An `email_template` member at a
723+
* locale the base does not ship is refused there (#28777) and stays open:
724+
* the ceremony moves such a row as the pair's own member, declaring
725+
* nothing (#28904), and the close reaches only the declared-override
726+
* moves, so its package-less write is the one it had before the close.
721727
*
722728
* Registry reads first; the one store read (the item's rows) only for a name
723729
* the registry has not already left open.
@@ -731,14 +737,9 @@ export async function packagelessCloseSubject(
731737
const shippers = this.overlayBaseShippers(request.type, request.name);
732738
if (shippers.length !== 1) return null; // environment metadata, or `multi_shipped`
733739
const base = shippers[0]!;
734-
const pairId = customizationPackageId(base);
735-
if (!pairId.ok) return null; // `no_pair`
736-
const pair = pairId.value;
737-
const registry = (this.engine as { registry?: PairRegistry } | undefined)?.registry;
738-
if (!registry || typeof registry.getPackage !== 'function') return null;
739-
if (customizationPairEnsureRefusal(this.engine, registry, base, pair) !== undefined) return null; // step 1 fails
740-
const basedOn = baseVersionOf(this, base);
741-
if (basedOn === undefined) return null; // no `basedOn` to record
740+
// `no_pair`, a pair step 1 cannot ensure, or no `basedOn` to record.
741+
const fix = overlayPairFixOf(this, base);
742+
if (fix === null) return null;
742743

743744
// The item's stored rows, both spellings, every package: the census's read.
744745
const spellings = [...new Set([type, SINGULAR_TO_PLURAL[type] ?? type])];
@@ -768,12 +769,6 @@ export async function packagelessCloseSubject(
768769
if (classified.disposition !== 'movable') return null;
769770

770771
// Step 2's gate: the declared body, into the pair.
771-
const disc = member === undefined ? undefined : overlayMemberField(type);
772-
const declaredBody = {
773-
...(body ?? { name: request.name, ...(disc && member !== null ? { [disc]: member } : {}) }),
774-
overrides: { package: base, basedOn },
775-
};
776-
const verdict = this.overrideWriteVerdict({ type, name: request.name, packageId: pair, item: declaredBody });
777-
if (verdict.verdict === 'refuse') return null;
778-
return { base, pair };
772+
if (!overlayPairFixAdmits(this, fix, { type, name: request.name, body, member })) return null;
773+
return { base, pair: fix.pair };
779774
}

‎packages/metadata-protocol/src/protocol.named-base-overlay-write.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ describe('CONTROLS: every step the fix takes, failing, leaves the write naming t
408408
});
409409

410410
it('a read-only base that records no version, its pair installed, keeps the write naming it: there is no `basedOn` to declare', async () => {
411+
// The ensure door answers an installed pair without reading the base's
412+
// version, so this is the version step's own case.
411413
const { protocol, rows } = makeProtocol();
412414

413415
const saved = await protocol.saveMetaItem({

0 commit comments

Comments
 (0)