Skip to content
17 changes: 17 additions & 0 deletions .changeset/34348-environment-row-beside-two-shippers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@objectstack/metadata-protocol': patch
'@objectstack/spec': patch
---

fix(metadata-protocol): the environment's own row of a name two enabled packages ship is what the reads naming no package serve, as the save naming no package edits it

Clause-②: yes
Metadata-protocol: none

<!-- adr-0087: registered environment-row-beside-two-shippers-bare-read-served -->

Who sees it: an environment that stores its own (package-unbound) customization of a metadata item that two or more enabled packages ship, and reads it without naming a package.

- **Before:** every read naming no package (`GET /api/v1/meta/TYPE/NAME`, `/layers`, `/published`, `/meta/TYPE/_env/NAME`, and the in-process item, layered and execution reads) answered `409 RESOURCE_CONFLICT` naming the shippers, while the save naming no package answered `200` and edited that row. An environment could change an item no read would show it.
- **Now:** those reads serve the environment's row, stating no package. `/layers` reports no code layer and the row as overlay and effective. After a save naming no package, every read serves the saved body. Reads and writes give the same answer (ruling 6942981052, option (b), narrowed by 7187668758 and 7405866869).
- **Unchanged:** with no such row the reads still refuse naming the enabled shippers; beside a stored row bound to a package (a shipper's own copy), where the save naming no package edits that package's row or refuses, the reads answer as before; `?package=PACKAGE` answers as before; a disabled shipper counts for nothing; a name the packages hold only as stored rows (no shipper) keeps its refusal on reads and saves alike; a sealed managed type's package-unbound row is still declined.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* [#34348, ADR-0150 D3, ruling 6942981052 option (b) as narrowed by
* 7187668758 and 7405866869 and scoped by 7187358430] The environment's own item answers
* first: a package-unbound active row of a name two or more ENABLED packages
* ship in code is the environment's item, and the reads that name no package
* serve it, as the bare save already edits it.
*
* FROM (`main` 198301bd, measured before the change, both registration
* orders, both save orders, before and after a reboot, the runtime dispatcher
* and `RestServer`): the plain read, `/layers`, `/published`, `_env` and the
* execution read answered `409 RESOURCE_CONFLICT` naming both shippers
* (`ambiguous-in-environment`, PR #34355's refusal), while the bare save
* answered 200 and edited the package-unbound row. Reads and writes
* disagreed, and no read reached the row.
*
* Only that arrangement moves. Left as they were:
* - two shippers and no package-unbound row: the refusal naming both stands;
* - a row bound to a package beside the package-unbound one (a shipper's
* own copy or its pair's: the bare save edits that package's row, or
* refuses two): the reads stay as they were (contract review M1);
* - a disabled shipper counts for nothing (ruling 7187668758, 「我觉得停用就该
* 全部消失」): one enabled shipper beside a disabled one is one holder, and
* a name only disabled packages hold stays hidden at every door (#37499);
* - a name the packages hold only as stored rows (no shipper): the bare save
* refuses there too, so reads and writes already agree (re-measure on the
* card's claim amendment 7404361429);
* - a package-unbound row the reads decline (a sealed managed type's,
* `declinesStoredRow`): it customizes nothing, so it is no environment item;
* - the strict draft read: its address is the draft save's.
*/

import type { MetaItemFaceHost } from './protocol-meta-item-face.js';
import { shippingPackageIds } from './protocol-environment-rows.js';
import { ENVIRONMENT_READ_REFUSAL, type EnvironmentReadOutcome } from './protocol-enabled-holders.js';
import type { PackageScopedNameRefusedError } from '@objectstack/core';

/** The protocol reads the judgment below takes, typed as the item face's. */
export type EnvironmentRowHost = Pick<MetaItemFaceHost,
'engine' | 'packagelessAddress' | 'shippedArtifactsOf' | 'readItemActiveRows' | 'declinesStoredRow' | 'rethrowUnlessMetadataStoreUnprovisioned'>;

/**
* Whether the read of `(type, name)` naming no package serves the
* environment's own package-unbound row: a type the registry reads by
* package, a name two or more enabled packages ship in code at `member`, and
* at `member` an active package-unbound row the reads serve with no row bound
* to any package beside it, so the bare save addresses that row (contract
* review M1). A store that is not provisioned holds no row.
*/
export async function servesEnvironmentRowBesideShippers(
host: EnvironmentRowHost,
type: string, name: string, member?: string,
): Promise<boolean> {
const registry: any = host.engine?.registry;
if (typeof registry?.readsByPackage !== 'function' || registry.readsByPackage(type) !== true) return false;
const disabled = (id: string): boolean => typeof registry.isPackageDisabled === 'function' && registry.isPackageDisabled(id) === true;
const enabledShippers = [...shippingPackageIds(host.shippedArtifactsOf(type, name, undefined, member))].filter((id) => !disabled(id));
if (enabledShippers.length < 2) return false;
let activeRows: Awaited<ReturnType<EnvironmentRowHost['readItemActiveRows']>>;
try {
activeRows = await host.readItemActiveRows(type, name);
} catch (error) {
host.rethrowUnlessMetadataStoreUnprovisioned(error, 'sys_metadata');
return false;
}
// [#34348 contract review M1] The rows the bare save addresses: every
// active row at `member` the reads serve, whatever its binding, as
// `packagelessAddress` judges them for the save. A row bound to any
// package beside the unbound one (a shipper's own copy, its customization
// pair's, a disabled package's) makes the save address that package or
// refuse, so the read is left as it was there.
const served = activeRows.filter((row) => !host.declinesStoredRow(type, name, row as never));
const address = await host.packagelessAddress(type, name, member, 'active', served);
if (!address.unbound || address.candidates.length !== 0) return false;
// [#34348 contract review S1] The unbound rows at `member` are read at
// `package_id: null` by the content read: a row stored with the empty
// string is not, so it never makes the arrangement.
const atMember = await host.packagelessAddress(type, name, member, 'active',
served.filter((row) => (row as { package_id?: unknown }).package_id === ''));
return !atMember.unbound;
}

/**
* [#34348] `_env` beside two or more enabled packages that ship the name.
* The admission (`admitEnvironmentQualifier`) reads the registry alone, and
* the registry keeps no environment item of such a name (the hydrator lays
* no package-unbound row into the bare slot of a name two packages hold), so
* it refuses `not-held-by-qualifier` naming the shippers, also where the
* environment's own row is stored and the read naming no package serves it.
* `refusal` is marked with the admission's `marker` (its
* `DISABLED_HOLDERS_ONLY_REFUSAL` key: the `_env` doors read it as "ask the
* read naming no package") and carries the judgment under
* {@link ENVIRONMENT_READ_REFUSAL}: when that read serves the environment's
* own item (no package states it: neither the envelope's `packageId` nor the
* item's `_packageId`), `_env` is admitted and answers it; otherwise the
* refusal stands, naming the shippers, as before. Returns `refusal`.
*/
export function withEnvironmentRowAdmission(
refusal: PackageScopedNameRefusedError,
marker: symbol,
): PackageScopedNameRefusedError {
const judge = (outcome: EnvironmentReadOutcome): PackageScopedNameRefusedError | undefined => {
if (outcome.error !== undefined) return refusal;
const served = outcome.answer as { item?: { _packageId?: unknown } | null; packageId?: unknown } | null | undefined;
if (served?.item == null) return refusal;
const stated = served.packageId ?? served.item._packageId;
return typeof stated === 'string' && stated !== '' ? refusal : undefined;
};
Object.defineProperty(refusal, marker, { value: true });
Object.defineProperty(refusal, ENVIRONMENT_READ_REFUSAL, { value: judge });
return refusal;
}
18 changes: 17 additions & 1 deletion packages/metadata-protocol/src/protocol-exact-qualifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
import { ENVIRONMENT_ADDRESS_SEGMENT, readNamespacePositionSegment } from '@objectstack/spec/kernel';
import { installedManifestsOfEngine } from './installed-manifests.js';
import { disabledHoldersOnlyRefusal } from './protocol-enabled-holders.js';
import { withEnvironmentRowAdmission } from './protocol-environment-row-beside-shippers.js';
import { canonicalizeMetaRequestType } from './protocol-meta-type-schema.js';
import { bundleMemberDiscriminator } from './protocol-bundle-members.js';
import type { ProtocolHost } from './protocol-host.js';
Expand Down Expand Up @@ -1041,7 +1042,14 @@ export function admitEnvironmentQualifier(
callerPackageId: null,
candidates,
};
throw new PackageScopedNameRefusedError({ ...base, message: packageScopedRefusalMessage(base) });
const refusal = new PackageScopedNameRefusedError({ ...base, message: packageScopedRefusalMessage(base) });
// [#34348, ruling 6942981052 (b)] Two or more enabled holders: the read
// naming no package judges `_env`, which it admits where that read serves
// the environment's own package-unbound row ({@link withEnvironmentRowAdmission}).
if (served.length >= 2 && (registry as { readsByPackage?(type: string): boolean }).readsByPackage?.(request.type) === true) {
throw withEnvironmentRowAdmission(refusal, DISABLED_HOLDERS_ONLY_REFUSAL);
}
throw refusal;
}

/**
Expand All @@ -1067,5 +1075,13 @@ export function admitEnvironmentQualifier(
* registry has not read), never the disabled one; naming none, the door
* admits `_env`, which answers as that read does. FROM (`main`
* 8338d070): the refusal stood, naming the disabled package.
*
* [#34348 contract review N1] The mark also rides the refusal of a name two
* or more ENABLED packages hold, of a type the registry reads by package
* ({@link withEnvironmentRowAdmission}): it means "ask the read naming no
* package", not only "disabled holders alone". There that read never answers
* with no item (it serves the environment's own row or refuses naming the
* shippers), so the first step above is not reached; the second admits
* `_env` only where the read serves an item no package states.
*/
const DISABLED_HOLDERS_ONLY_REFUSAL: unique symbol = Symbol.for('objectstack.metadata-protocol.disabledHoldersOnlyRefusal') as never;
11 changes: 9 additions & 2 deletions packages/metadata-protocol/src/protocol-meta-item-face.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { boundObjectOf } from '@objectstack/metadata/stored-item-package';
import { RELATED_LISTS_ARRANGEMENT_DECORATION, type RelatedListsArrangement } from '@objectstack/spec/data';
import { overlayMemberValue, overlayMemberWhere } from './migrations/overlay-index.js';
import { isServedOverrideRowOf } from './served-override.js';
import { servesEnvironmentRowBesideShippers } from './protocol-environment-row-beside-shippers.js';
import {
isActionWithheldByDisabledPackage,
isItemOfDisabledPackage,
Expand Down Expand Up @@ -963,14 +964,20 @@ export async function readMetaItemFace(
// [#33210 (c)] …and a member only a dependent's addition holds, over a
// base holding the name only as stored rows, is read at that base
// ({@link rowHeldBaseAddressOf}), as it is when the base ships the name.
const servedAddress = request.packageId
// [#34348, ruling 6942981052 (b)] …and the environment's own
// package-unbound row of a name two or more enabled packages ship is
// what the read naming no package serves, as the bare save edits it
// ({@link servesEnvironmentRowBesideShippers}): no holder's address.
const environmentRow = !request.packageId && request.state !== 'draft'
&& await servesEnvironmentRowBesideShippers(judgeHost, request.type, request.name, member);
const servedAddress = request.packageId || environmentRow
? undefined
: await servedAddressOf(judgeHost, request.type, request.name, member, { refuseWithoutDisabled: true })
?? (request.state === 'draft'
? undefined
: await declaredMemberOverrideAddressOf(judgeHost, request.type, request.name, member)
?? await rowHeldBaseAddressOf(judgeHost, request.type, request.name, member));
const answer = await this.readMetaItem(servedAddress === undefined ? request : { ...request, packageId: servedAddress }, { face, ...(activeRows !== undefined ? { activeRows } : {}) });
const answer = await this.readMetaItem(servedAddress === undefined ? request : { ...request, packageId: servedAddress }, { face, ...(activeRows !== undefined ? { activeRows } : {}), ...(environmentRow ? { environmentRow: true } : {}) });
// [#28877] The served read of an `action` also withholds one that launches
// into a disabled package, as the served list does.
const withheldAction = request.type === 'action' && face === 'served'
Expand Down
32 changes: 29 additions & 3 deletions packages/metadata-protocol/src/protocol-meta-item-reads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,18 @@ export async function readMetaItem(host: Pick<ProtocolHost, 'artifactLockLayerAt
* query string decodes to.
*/
packageClosures?: Map<string, Promise<RuntimePendingDeclarations | undefined>>,
}, opts: { face?: 'served' | 'execution'; activeRows?: PrereadActiveRows } = {}) {
}, opts: {
face?: 'served' | 'execution'; activeRows?: PrereadActiveRows;
/**
* [#34348] The item face judged the read naming no package to serve
* the environment's own package-unbound row beside two or more
* enabled shippers (`servesEnvironmentRowBesideShippers`): the
* package-scoped resolver's refusal of the shippers is not asked, the
* row is read at no package's address, and no shipper lends it an
* envelope.
*/
environmentRow?: boolean;
} = {}) {
// #4432 — CANONICAL TYPE KEY. See {@link canonicalMetaType}.
request = canonicalizeMetaRequestType(request);
// [#23183] ADR-0048 §3.1: the app read keyed by PACKAGE ID, resolved
Expand Down Expand Up @@ -878,7 +889,7 @@ export async function readMetaItem(host: Pick<ProtocolHost, 'artifactLockLayerAt
? refuseAmbiguousBareDocRead(host, request.name, member)
: undefined;
const packagelessScopedRead: { item: unknown } | undefined =
!request.packageId && typeof registry?.readsByPackage === 'function' && registry.readsByPackage(request.type)
!request.packageId && opts.environmentRow !== true && typeof registry?.readsByPackage === 'function' && registry.readsByPackage(request.type)
? { item: registry.resolveItem(request.type, request.name, null, member) }
: undefined;
// [ADR-0150 D5, epic #31527 child G1] A read that names a package
Expand Down Expand Up @@ -1067,7 +1078,10 @@ export async function readMetaItem(host: Pick<ProtocolHost, 'artifactLockLayerAt
// item of the name, never a package's customization: the read naming no
// package serves it, and its `version`, where it served the one package
// the rows are bound to.
const environmentAction = packagelessRows !== undefined && holdsEnvironmentOwnAction(request.type, activeRows ?? []);
// [#34348] …and so is the environment's own package-unbound row of a name
// two or more enabled packages ship, as the item face judged it.
const environmentAction = packagelessRows !== undefined
&& (opts.environmentRow === true || holdsEnvironmentOwnAction(request.type, activeRows ?? []));
const packagelessAddressed = environmentAction ? { ...packagelessRows, packageId: null } : packagelessRows;
const packagelessBinding = packagelessAddressed?.packageId;
// [#28238] The content, though, is {@link packagelessContentPackage}'s:
Expand Down Expand Up @@ -1352,6 +1366,17 @@ export async function readMetaItem(host: Pick<ProtocolHost, 'artifactLockLayerAt
item = host.foldObjectExtendersFromRegistry(request.type, request.name, item);
}

// [#34348 contract review S1] The item face judged this read to serve the
// environment's own package-unbound row, so step 1 must have served it.
// Where it did not (no such row read at no package's address), the read
// never reaches the code layers, whose answer for a name two packages
// ship follows registration order: it gives the resolver's refusal
// naming the shippers, as it did before the judgment.
if (opts.environmentRow === true && (item === undefined || storedItemPackageId(item) !== undefined)) {
registry.resolveItem(request.type, request.name, null, member);
throw new Error(`the environment's row of ${request.type} '${request.name}' was judged served and was not read`);
}

// 1b. [#21442] A view name with no stored row of its own that a stored
// view container in this caller's scope EXPANDS — the item the
// object door (`GET /meta/view?object=`) lists under this name,
Expand Down Expand Up @@ -1606,6 +1631,7 @@ export async function readMetaItem(host: Pick<ProtocolHost, 'artifactLockLayerAt
request.type, request.name, packageId, bundleMemberOf(request.type, item), artifactOwner,
))
: servesEnvironmentOwnItem(bareDocEnvironmentEntry, item)
|| (opts.environmentRow === true && storedRowServed && storedItemPackageId(item) === undefined)
? undefined
: host.lookupArtifactItem(
request.type, request.name, envelopePackageId(request.packageId, item), bundleMemberOf(request.type, item),
Expand Down
Loading
Loading