Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .changeset/34144-bare-save-holder-judgment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
'@objectstack/metadata-protocol': patch
---

fix(metadata-protocol)!: a bare save or delete answers the holders the bare read counts: a name two enabled packages hold is refused naming both, and a name only disabled packages hold is refused naming none (ADR-0150 D3/D5)

Clause-②: no (narrowing)
Metadata-protocol: none

<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export changes shape. The moved answers are write doors under a reversible package state: no stored row is rewritten, so there is nothing for a migration to rewrite and no tombstone. -->

**BREAKING** for a writer that saves or deletes a name without naming a package (`PUT` or `DELETE /meta/:type/:name` with no `?package=`, or `saveMetaItem` / `deleteMetaItem` with no `packageId`), in two states. The bare write used to take its address from the stored rows alone, while the bare read counts the name's holders. It now counts them as the read does (ADR-0150 D3/D5, red line 2: ambiguity is reported, never guessed). Every refusal is the existing `409 RESOURCE_CONFLICT` with the `?package=` remedy; there is no new code. Measured on a booted kernel through the runtime dispatcher and `RestServer`, in both registration orders.

**Two or more enabled packages hold the name.** The bare read refuses 409 naming them.

- A publish-mode bare save used to answer 200. Over no stored row it wrote a package-unbound row beside both packages; over one package's row it updated that row. It is now refused naming both packages, and nothing is written. Over a row of each it was refused already, and still is.
- A draft-mode bare save (`?mode=draft`) used to write a package-unbound draft, or the one package's draft. It is now refused the same way.
- A bare delete used to remove the one package's row. It is now refused the same way, and the row stays.
- A bare delete or bare draft discard (`?state=draft`) over no stored row used to answer the no-op receipt (`success: true, reset: false`). It is now refused the same way.

**Every package that holds the name is disabled.** A disabled package holds no name (ruling A on #34144 and the registry's served-holder rule, #32183), so the bare read answers 404. The bare write is now refused naming no package (ruling A):

- A publish-mode bare save of a name whose only holder D is disabled used to answer 200. Over D's row alone it updated D's row; over no stored row it wrote a package-unbound row that every read then withheld. It is now refused, and nothing is written.
- A bare save of a bundled name (an `email_template`) whose every holder is disabled, at a member the holder never ships, used to answer 200 and store a package-unbound row that the by-name read and both lists withheld until the package was enabled again. It is now refused, and nothing is stored. This replaces the "accepted and kept" save described in this release's note on reads of a disabled package's customization: FROM `success: true` with the row stored and hidden, TO `409 RESOURCE_CONFLICT` with the `?package=` remedy, naming no package, no row stored. The line of `.changeset/33385-disabled-holders-env-404.md` that says this save "is accepted and kept, as before" is superseded by this note: while every holder is disabled, the save is refused and nothing is stored. A delete of such a row while the package is disabled still removes it, a save at a member the package ships is still refused `403 NOT_OVERRIDABLE` where the package door refuses it, and once the package is enabled again the same save is kept.
- A draft-mode bare save (`?mode=draft`) of such a name used to write a package-unbound draft. It is now refused the same way.
- A bare delete of such a name used to remove D's row. It is now refused, and D's row stays.
- A bare delete or bare draft discard (`?state=draft`) of such a name over no stored row used to answer the no-op receipt (`success: true, reset: false`). It is now refused the same way.

**Not moved.** A write naming the package (`?package=`, `packageId`) answers as before. So do a name one enabled package holds, a name an enabled and a disabled package both hold, a name no package holds (the environment's own item), and a package-unbound row stored at the address: the save edits that row and the delete removes it, as before. Once a disabled package is enabled again, its bare writes answer as before. A type the registry does not read by package (`action`, `doc`, whose bare reads refuse through their own judges) is not judged here and answers as before. A bare save in these two states that a later gate already refused (an invalid body, a read-only inherited package) is still refused, now with this `409` first: refused to refused.

**No migration.** No schema key or stored row changes. To write one holder's item, name its package; to write a disabled package's item, name it or enable it again.
18 changes: 18 additions & 0 deletions packages/metadata-protocol/src/protocol-diff-delete-load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import { withRefusalAudit } from './protocol-refusal-audit.js';
import { captureStoredExpressionChange, fillStoredExpressionChange } from './protocol-stored-expression-fill.js';
import type { RuntimeAuthoringIssue } from './runtime-authoring-gate.js';
import type { ProtocolHost } from './protocol-host.js';
import { packagelessHolderRefusal } from './protocol-save.js';

/**
* [#33308] The row slot a `sys_metadata` row is: its bound package, or `null`
Expand Down Expand Up @@ -730,6 +731,23 @@ export class ProtocolDiffDeleteLoadFacet {
);
throw addressRefusal;
}
// [ADR-0150 D3/D5, red line 2, ruling A] …and the holders the
// read naming no package counts, as the save door asks them
// ({@link packagelessHolderRefusal}): two or more enabled holders
// are refused naming them, and a name only disabled packages hold
// is refused naming none, never one row removed. Over no stored
// row too (the delete and the draft discard answered the no-op
// receipt). A package-unbound row at the address is the
// environment's own, and its removal is left as it was.
if (request.packageId == null) {
const holderRefusal = packagelessHolderRefusal(
host, 'delete', request.type, singularTypeForRepo, request.name, member, rowsAtAddress,
);
if (holderRefusal) {
addressRefusal = holderRefusal;
throw addressRefusal;
}
}
const packageId: string | null = request.packageId
?? (draftAddress !== undefined ? draftAddress.key : rowsAtAddress.packageId);
// Probe first — "no overlay exists" is a success/no-op, not
Expand Down
153 changes: 147 additions & 6 deletions packages/metadata-protocol/src/protocol-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ import { saveRegistryQuestion } from './registry-question.js';
import { OBJECT_EXTENSION_TYPE, registrableObjectExtension } from './protocol-object-extension-item.js';
import { servedAddressJudgment } from './protocol-meta-item-face.js';
import { packagelessContentPackage, shippingPackageIds } from './protocol-environment-rows.js';
import { installedManifestsOfEngine } from './installed-manifests.js';
import { packageScopedRefusalOf } from './protocol-meta-item-reads.js';
import { pairsOf } from '@objectstack/metadata-core';
import { BatchRowHealError, canJudgeBatchObjects, isBatchObjectType, readActiveBatchRow, settleBatchObjectRows, undoBatchRowWrite, undoOutcomeForLog, type WrittenBatchRow } from './batch-object-registration.js';

export class ProtocolSaveFacet {
Expand Down Expand Up @@ -826,6 +829,15 @@ export class ProtocolSaveFacet {
if (address.candidates.length > 1) {
throw packagelessAddressAmbiguityRefusal('save', request.type, request.name, address.candidates);
}
// [ADR-0150 D3/D5, red line 2, ruling A] …and the holders the
// read naming no package counts ({@link packagelessHolderRefusal}):
// two or more enabled holders are refused naming them, and a name
// only disabled packages hold is refused naming none, never
// written at one row's package or as a new package-unbound row.
const holderRefusal = packagelessHolderRefusal(
host, 'save', request.type, writeType, request.name, itemDiscriminator(writeType, request.item), address,
);
if (holderRefusal) throw holderRefusal;
if (address.packageId !== null) request = { ...request, packageId: address.packageId };
}
}
Expand All @@ -846,6 +858,21 @@ export class ProtocolSaveFacet {
singularType, request.name, itemDiscriminator(singularType, request.item),
)
: undefined;
// [ADR-0150 D3/D5, red line 2, ruling A] The draft save naming no
// package answers the holders as the publish-mode save above does
// ({@link packagelessHolderRefusal}), judged over the item's active rows
// and its drafts: the draft address alone took the one package with a
// row, or a new package-unbound draft. An ambiguous draft address is
// left to the repository's `409`, as before.
if (draftAddress !== undefined && draftAddress.kind !== 'ambiguous') {
const member = itemDiscriminator(singularType, request.item);
const active = await host.packagelessAddress(singularType, request.name, member, 'active');
const holderRefusal = packagelessHolderRefusal(host, 'save', request.type, singularType, request.name, member, {
candidates: [...new Set([...active.candidates, ...draftAddress.drafts.candidates])],
unbound: active.unbound || draftAddress.drafts.unbound,
});
if (holderRefusal) throw holderRefusal;
}

// [#26921] THE NAMED-BASE RULE for the binding a save naming no
// package INHERITS: the package its address gives (the resolved
Expand Down Expand Up @@ -2835,9 +2862,10 @@ export class ProtocolSaveFacet {
/**
* [#32605, epic ruling 6781691009 on #32354, ADR-0150 D3/D4/D5] THE WRITE
* ADDRESS of a publish-mode save naming no package, for a name a DISABLED
* package also holds: the address the read naming no package serves, so the
* save writes exactly the row the next read answers. A disabled package holds
* nothing an address reaches, so it is never the address and never named.
* package also holds: in the cells below, the address the read naming no
* package serves, so the save writes the row the next read answers. A
* disabled package holds nothing an address reaches, so it is never the
* address and never named.
*
* - The read's own judgment first ({@link servedAddressJudgment}, the one
* #32183 and #32354 gave the reads): its served holder is the address; its
Expand All @@ -2850,9 +2878,15 @@ export class ProtocolSaveFacet {
* the package-unbound row when one is stored (the save edits it, as the
* read serves it), else the one enabled package that ships the name (the
* read's content package, {@link packagelessContentPackage}).
* - `undefined` leaves the address to {@link packagelessAddress}, as before:
* no disabled holder, or one only disabled packages hold (no served holder
* to take the write; the save keeps naming that package, as it did).
* - `undefined` leaves the address to {@link packagelessAddress} and
* {@link packagelessHolderRefusal}: no disabled holder, or one only
* disabled packages hold (no served holder to take the write; that judgment
* refuses it naming no package).
*
* Known gap: two or more ENABLED packages hold the name and the stored rows
* are bound to a disabled package and to exactly one of them. The enabled
* bindings above pick that one (the save answers 200) where the read refuses
* `409` naming every enabled holder. A follow-up card is filed.
*
* Without it the save door counted the disabled holder as a holder: over no
* stored row it wrote a package-unbound row (an environment-own item that
Expand Down Expand Up @@ -2886,3 +2920,110 @@ async function servedPackagelessSaveAddress(
const content = packagelessContentPackage({ packageId: null, candidates: [] }, shippers.map((id) => ({ _packageId: id })));
return typeof content === 'string' ? { packageId: content } : undefined;
}

/**
* [ADR-0150 D3, ruling A on the disabled-only name] The refusal of a write
* naming no package of a name every holder of which is disabled. A disabled
* package holds no name, so the read naming no package answers the missing
* name; the write names no package either, and points to the one way to act
* on that package's item. `409 RESOURCE_CONFLICT`, the refusal family of
* {@link packagelessAddressAmbiguityRefusal}, with no new code.
*/
export function disabledHoldersOnlyRefusal(
verb: 'save' | 'delete',
type: string,
name: string,
): Error & { code: 'RESOURCE_CONFLICT'; status: 409 } {
const [nothing, single] = verb === 'save'
? ['Nothing was written.', 'no row to write']
: ['Nothing was deleted.', 'no row to remove'];
const err = new Error(
`Name the package this ${verb} is for, then send it again: on the REST door add ?package= with the package id, `
+ `in process pass packageId. ${nothing} ${type} '${name}' is held by no enabled package, and a ${verb} `
+ `that names none has ${single}.`,
) as Error & { code: 'RESOURCE_CONFLICT'; status: 409 };
err.code = 'RESOURCE_CONFLICT';
err.status = 409;
return err;
}

/**
* [ADR-0150 D3/D5 and red line 2] THE HOLDERS a write naming no package
* counts before it takes its address from the stored rows' bindings
* ({@link packagelessAddress}): the holders the read naming no package
* counts, so the bare save (publish and draft mode), the bare delete and the
* bare draft discard answer as the bare read does in the two states below,
* whether or not a row is stored at the address (over none, the delete and
* the discard are refused too, not answered the no-op receipt).
* Asked after {@link servedPackagelessSaveAddress} (a name an enabled and a
* disabled package both hold) and after the rows' own refusal (two or more
* bindings), so neither answer moves.
*
* - Two or more ENABLED packages hold the name: the package-scoped
* resolver's own refusal (`resolveItem` naming no package, the judgment
* the by-name read asks before any layer), restated as the write's
* refusal naming those packages. The rows' bindings alone picked the one
* package with a row, or wrote a package-unbound row beside both.
* - Every package holding the name is disabled (a registry holder, a code
* shipper or a stored row's binding; a row bound to a disabled holder's
* customization pair is that holder's): {@link disabledHoldersOnlyRefusal},
* naming no package. The write edited the disabled package's row, or
* stored a row every read then withholds.
* - `undefined` otherwise: no holder, one enabled holder, a package-unbound
* row stored at the address or the environment's own item (ADR-0150 D3
* (a): the row the write edits), a type the registry does not read by
* package, or a registry without the reads.
*
* Known gap: with an enabled and a disabled holder, the draft save and the
* delete have no {@link servedPackagelessSaveAddress} step and still take
* their address from the rows, and that step's own gap stands for the publish
* save. A follow-up card is filed.
*/
export function packagelessHolderRefusal(
host: Pick<ProtocolHost, 'engine' | 'shippedArtifactsOf'>,
verb: 'save' | 'delete',
shownType: string,
type: string,
name: string,
member: string | undefined,
address: { readonly candidates: readonly string[]; readonly unbound: boolean },
): (Error & { code: 'RESOURCE_CONFLICT'; status: 409 }) | undefined {
if (address.unbound) return undefined;
const registry = host.engine?.registry as HolderRegistry | undefined;
if (typeof registry?.readsByPackage !== 'function' || registry.readsByPackage(type) !== true) return undefined;
if (typeof registry.getEnvironmentScopedItem === 'function' && registry.getEnvironmentScopedItem(type, name, member) !== undefined) {
return undefined;
}
if (typeof registry.resolveItem === 'function') {
try {
registry.resolveItem(type, name, null, member);
} catch (error) {
const refusal = packageScopedRefusalOf(error);
if (refusal === undefined) throw error;
const held = refusal.reason === 'ambiguous-in-environment'
? refusal.candidates.flatMap((candidate) => (candidate.kind === 'package' ? [candidate.packageId] : []))
: [];
if (held.length > 1) return packagelessAddressAmbiguityRefusal(verb, shownType, name, held);
}
}
if (typeof registry.packageScopedHoldersOf !== 'function' || typeof registry.isPackageDisabled !== 'function') return undefined;
const disabled = (id: string): boolean => registry.isPackageDisabled?.(id) === true;
const known = [...new Set([
...(registry.packageScopedHoldersOf(type, name, null, { includeDisabledPackages: true }) ?? []),
...shippingPackageIds(host.shippedArtifactsOf(type, name)),
])];
const installed = installedManifestsOfEngine(host.engine);
const disabledHoldersPair = (id: string): boolean => known.some((holder) => disabled(holder) && pairsOf(holder, installed).includes(id));
const every = [...new Set([...known, ...address.candidates])];
if (every.length === 0 || every.some((id) => !disabled(id) && !disabledHoldersPair(id))) return undefined;
return disabledHoldersOnlyRefusal(verb, shownType, name);
}

/** The registry reads {@link packagelessHolderRefusal} asks, each optional-called. */
interface HolderRegistry {
readsByPackage?(type: string): boolean;
getEnvironmentScopedItem?(type: string, name: string, member?: string): unknown;
resolveItem?(type: string, name: string, callerPackageId: null, member?: string): unknown;
packageScopedHoldersOf?(type: string, name: string, callerPackageId?: string | null, options?: { includeDisabledPackages?: boolean }): readonly string[] | undefined;
isPackageDisabled?(id: string): boolean;
}
Loading
Loading