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
2 changes: 1 addition & 1 deletion .changeset/37013-platform-skill-own-slot.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
'@objectstack/spec': minor
'@objectstack/metadata': minor
Expand All @@ -21,7 +21,7 @@
- The readers that rebuild the store from loaders and rows keep both records: the plural read (`loadMany`, `loadManyKeyed`) gives the platform's entry a key of its own, `list` keeps a loader's platform row beside a package's slot and a loader's package item beside a platform slot, and a read naming a package is never answered from the platform's record.
- `MetadataManager.unregister(type, name, { origin: 'platform' })` withdraws the platform's record only, from the registry, every loader (`MetadataLoaderDeleteOptions.platformOnly`) and every cluster peer. A package's item of the name and the environment's own item are never taken, for any type, and nothing is refused however many packages hold the name. Before, a removal naming no package took every slot of a name one package held. On a host that declares a package id, the boot pass leaves the platform's stored record without the host's package, for every type, so the withdrawal takes the registry's copy too.
- `MetadataManager.getPlatformItem(type, name)` reads the platform's record only, never a package's item and never the environment's own.
- No other write carries the platform stamp. `register` without `origin: 'platform'`, `registerInMemory`, `save` and `rollback` store the body without it, and so do the metadata protocol's save door and its restore doors (`rollbackMetaItem` and the restore limb of `revertCommit`); a stored row the boot pass re-registers keeps it only when the row held it as read. `rollback` whose row is the platform's own record is refused (`409 RESOURCE_CONFLICT`, naming the fix), with nothing written. The metadata protocol's two undo writes, of a refused batch publish's landed promotions and of a failed batch row write, put a row back as the same operation found it, byte for byte, so the mark comes back only where the row held it. A body served from a loader other than a `datasource:` one is served without it; `getPublished` serves it on the answer's top level only when the stored item carries it there, and never inside a publish snapshot; and the artifact door refuses an item that carries it, with or without a manifest id (`VALIDATION_ERROR` at `_provenance`, naming the fix). A `_provenance` key nested in the body (an authored `metadata` record, for one) is the author's data: it is kept as written and never read as the mark. A body that names a package is that package's item, whatever `_provenance` it carries.
- No other write carries the platform stamp. `register` without `origin: 'platform'`, `registerInMemory`, `save` and `rollback` store the body without it, and so do the metadata protocol's save door and its restore doors (`rollbackMetaItem` and the restore limb of `revertCommit`) on every restore whose row is not the platform's own record; a stored row the boot pass re-registers keeps it only when the row held it as read. A restore whose row is the platform's own record is refused (`409 RESOURCE_CONFLICT`, naming the fix), with nothing written, at all three restore doors: `rollback`, and the metadata protocol's `rollbackMetaItem` and the restore limb of `revertCommit`. The metadata protocol's two undo writes, of a refused batch publish's landed promotions and of a failed batch row write, put a row back as the same operation found it, byte for byte, so the mark comes back only where the row held it. A body served from a loader other than a `datasource:` one is served without it; `getPublished` serves it on the answer's top level only when the stored item carries it there (the platform's own record's publish snapshot, served as the answer, keeps the mark it carries at its top), and never on a snapshot nested inside the answer; and the artifact door refuses an item that carries it, with or without a manifest id (`VALIDATION_ERROR` at `_provenance`, naming the fix). A `_provenance` key nested in the body (an authored `metadata` record, for one) is the author's data: it is kept as written and never read as the mark. A body that names a package is that package's item, whatever `_provenance` it carries.
- `@objectstack/metadata` exports the mark's one definition (`PLATFORM_PROVENANCE`, `carriesPlatformStamp`, `isPlatformOrigin`, `withoutPlatformOrigin`); the artifact door and the metadata protocol's doors read it.
- `@objectstack/metadata-protocol`: the boot census `metadata_sealed_overlay_unserved` no longer names the platform's own row beside a package that ships the name, nor prescribes deleting it.
- Reads naming no package, and every removal and write without the option, keep their rules.
Expand Down
30 changes: 30 additions & 0 deletions .changeset/37130-platform-record-restore-doors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
'@objectstack/metadata': patch
'@objectstack/metadata-protocol': patch
---

fix(metadata-protocol)!: the protocol's restore doors refuse a restore whose row is the platform's own record, and the history selector never addresses the package-less row for a package that neither holds nor ships the name

Clause-②: yes (narrowing)
Metadata-protocol: none
Skills-docs: none — no `skills/` page teaches the protocol's restore doors, the history `?package=` selector or `listPackageBindings`

<!-- adr-0087: not-required (no-migration-prescription) No schema key, accepted value or metadata type is added, removed, renamed or re-shaped, and no stored row is rewritten. What moves is that two restore doors and the history selector refuse requests they wrote or answered before, and that the metadata service files and reports a stored row's binding by its package column; there is nothing for a migration to rewrite and no tombstone. -->

**BREAKING**, in two places, each a refusal of a request that was answered before:

- **The metadata protocol's `rollbackMetaItem` and the restore limb of `revertCommit` refuse a restore whose row is the platform's own record** (the row `MetadataManager.register(…, { origin: 'platform' })` writes), with the refusal the metadata service's `rollback` gives: `409 RESOURCE_CONFLICT`, naming the fix, nothing written. `revertCommit` reports that item in `failed[]` and still reverts the commit's other items. Before, both doors wrote the version back without the platform's mark, so the row stopped being the platform's record, and a process primed from the rows answered a read naming the name's one package holder with the platform's old body until the platform registered again.
What to do: nothing for a package's or the environment's own item. To restore an earlier version of a package's item of the name, name that package; to make an earlier version the environment's own item of the name, save that version's body again.
- **The history, diff and rollback doors' `?package=` selector refuses a package that neither holds a row of the name nor ships it** with `409 RESOURCE_CONFLICT` `not-held-by-qualifier`, naming every holder, as its description states, also when the name's package-less row is stored. Before, such a package addressed the package-less row a read naming it falls back to, and read or restored that row's versions. The package-less row is a package's slot only when the package ships the name.
What to do: address the package-less row through the package that ships the name, or bare when no other row of the name is stored.

Also in `@objectstack/metadata`:

- `refusePlatformRecordRestore(type, name, row)` is exported: the one refusal the service's `rollback` and the protocol's two restore doors ask, so the three cannot disagree.
- `list()` lists a stored row whose body's `_packageId` stamp differs from its package column once, under the column, after the boot priming pass, where it listed the row twice (the primed copy under the stamp). A read naming the column's package answers it, as on a process that never primed it.
- `listPackageBindings` is `degraded`, naming the loader in `errors`, when a loader of stored rows (`datasource:`) offers no `loadRowBindings`: the items it holds answer their registration's binding, which is not their rows'. No such loader ships in this repository.
- A node whose boot priming pass finds a legacy row already bound by another node answers the bound package for that row's item, where it answered `null` (fail-closed) until it restarted.

Also in `@objectstack/metadata-protocol`:

- `SysMetadataRepository.restoreVersion` takes `refusePlatformRecord: true` to ask `refusePlatformRecordRestore` (`@objectstack/metadata`) on the row it writes; the undo writes do not pass it.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* a named version another holder owns (or one attributed to no package,
* while two hold the name) is refused `404 VERSION_NOT_FOUND` saying whose;
* - `?package=` naming a package that neither holds nor ships the name is
* refused `409 not-held-by-qualifier`; the environment's slot addressed
* through a package (the read falls back to the package-less row) takes
* that row as its `to` side;
* refused `409 not-held-by-qualifier`, [#37130] beside the package-less row
* too; the environment's slot addressed through a package that ships the
* name (the read falls back to the package-less row) takes that row as its
* `to` side;
* - CONTROL: a name one slot holds diffs bare as before, its whole log;
* - `?package=` on a type not stored per package is refused `400`.
*
Expand Down Expand Up @@ -48,7 +49,7 @@ const ACTIVE = [
{ package_id: ALPHA, version: 3, state: 'active', metadata: JSON.stringify(body('alpha r2')) },
];

function fakeHost(options: { candidates: string[]; unbound?: boolean; history?: unknown[]; active?: unknown[] }) {
function fakeHost(options: { candidates: string[]; unbound?: boolean; history?: unknown[]; active?: unknown[]; shippers?: string[] }) {
const reads: string[] = [];
const host: any = {
engine: { registry: { readsByPackage: () => true, resolveItem: () => ({ status: 'resolved' }) } },
Expand All @@ -59,7 +60,7 @@ function fakeHost(options: { candidates: string[]; unbound?: boolean; history?:
const row = active.find((r) => r.package_id === q.packageId) ?? active.find((r) => r.package_id === undefined);
return row === undefined ? undefined : { row };
},
shippedArtifactsOf: () => [],
shippedArtifactsOf: () => (options.shippers ?? []).map((id) => ({ _packageId: id })),
resolveRowlessExpandedView: async () => undefined,
rethrowUnlessMetadataStoreUnprovisioned: (error: unknown) => { throw error; },
findEnvironmentRows: async (table: string) => {
Expand Down Expand Up @@ -157,7 +158,7 @@ describe('diffMetaItem reads one holder\'s versions of a by-package name (#33308
expect(reads).toEqual([]);
});

it('the environment\'s slot addressed through a package: the package-less row is the to side, never the bound one', async () => {
it('the environment\'s slot addressed through a package that ships the name: the package-less row is the to side, never the bound one; [#37130] one that neither holds nor ships it is refused', async () => {
// Alpha's row first, so a read that picks the name's first active row picks the wrong slot.
const active = [
{ package_id: ALPHA, version: 3, state: 'active', metadata: JSON.stringify(body('alpha r1')) },
Expand All @@ -168,7 +169,12 @@ describe('diffMetaItem reads one holder\'s versions of a by-package name (#33308
{ version: 2, package_id: ENVIRONMENT_HISTORY_MARK, metadata: JSON.stringify(body('env r2')) },
{ version: 3, package_id: ALPHA, metadata: JSON.stringify(body('alpha r1')) },
];
const { host } = fakeHost({ candidates: [ALPHA], unbound: true, history, active });
// [#37130] A package that neither holds nor ships the name never addresses the package-less row.
const unshipped = await refusalOf(fakeHost({ candidates: [ALPHA], unbound: true, history, active }).host
.diffMetaItem({ type: 'flow', name: 'shared_flow', packageId: BETA }));
expect(unshipped).toBeInstanceOf(PackageScopedNameRefusedError);
expect(unshipped.refusal.reason).toBe('not-held-by-qualifier');
const { host } = fakeHost({ candidates: [ALPHA], unbound: true, history, active, shippers: [BETA] });
const bare = await refusalOf(host.diffMetaItem({ type: 'flow', name: 'shared_flow' }));
expect(bare.refusal.candidates).toEqual([{ kind: 'package', packageId: ALPHA }, { kind: 'environment' }]);
const env = await host.diffMetaItem({ type: 'flow', name: 'shared_flow', packageId: BETA });
Expand Down
30 changes: 26 additions & 4 deletions packages/metadata-protocol/src/protocol-history-slots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* [ADR-0150 D3/D4/D5] Which row slot the history and rollback doors address
* (`historySlotOf`), judged against a fake host so each arm is pinned on its
* own: the served row's slot for `?package=P` (P's own, or the package-less
* row the read falls back to, whether or not P ships the name), the bare
* row the read falls back to when P ships the name; [#37130] a P that neither
* holds nor ships the name is refused, never addressed at that row), the bare
* plain read's refusal counting holders in code, `?package=` refused on a
* type not stored per package, and whether the addressed slot holds a row.
*/
Expand Down Expand Up @@ -67,12 +68,33 @@ describe('historySlotOf: ?package=P addresses the slot of the row a read naming
expect(slot).toEqual({ owner: { packageId: null, sole: true }, addressed: true, held: true });
});

it('P does not ship the name and the read falls back to the package-less row: the environment slot', async () => {
const slot = await historySlotOf(
it('[#37130] P neither holds nor ships the name and the read falls back to the package-less row: refused not-held-by-qualifier, never that row (BASE: the environment slot)', async () => {
const refused = await historySlotOf(
fakeHost({ unbound: true, served: { package_id: null }, shippers: [] }),
{ type: 'flow', name: 'n', packageId: 'com.a' },
).catch((error) => error);
expect(refused).toBeInstanceOf(PackageScopedNameRefusedError);
expect(refused.refusal.reason).toBe('not-held-by-qualifier');
expect(refused.refusal.callerPackageId).toBe('com.a');
expect(refused.refusal.candidates).toEqual([{ kind: 'environment' }]);
});

it('[#37130] beside another package\'s row too: a P that neither holds nor ships the name is refused naming both holders (BASE: the environment slot)', async () => {
const refused = await historySlotOf(
fakeHost({ candidates: ['com.b'], unbound: true, served: { package_id: null }, shippers: ['com.b'] }),
{ type: 'flow', name: 'n', packageId: 'com.a' },
).catch((error) => error);
expect(refused).toBeInstanceOf(PackageScopedNameRefusedError);
expect(refused.refusal.reason).toBe('not-held-by-qualifier');
expect(refused.refusal.candidates).toEqual([{ kind: 'package', packageId: 'com.b' }, { kind: 'environment' }]);
});

it('CONTROL: P\'s .custom pair row that declares the override of P is P\'s address: that row\'s slot', async () => {
const slot = await historySlotOf(
fakeHost({ candidates: ['com.a.custom'], unbound: true, served: { package_id: 'com.a.custom' }, shippers: [] }),
{ type: 'flow', name: 'n', packageId: 'com.a' },
);
expect(slot).toEqual({ owner: { packageId: null, sole: true }, addressed: true, held: true });
expect(slot).toEqual({ owner: { packageId: 'com.a.custom', sole: false }, addressed: true, held: true });
});

it("P's own row: P's slot, not sole beside the package-less row", async () => {
Expand Down
14 changes: 11 additions & 3 deletions packages/metadata-protocol/src/protocol-history-slots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
* package-less row that read falls back to — else `P`'s own slot when `P`
* ships the name in code and holds no row of it yet. A package that
* neither holds a row of the name nor ships it is refused `409` naming
* every holder (`not-held-by-qualifier`).
* every holder (`not-held-by-qualifier`). [#37130] The package-less row is
* `P`'s slot only when `P` ships the name (ADR-0150 D4: that row is then
* `P`'s customization): a read naming any other package falls back to it
* too, and ⛔ such a package never addresses it.
*
* A version belongs to a slot by its history row's attribution
* (`historyRowAttribution`, `@objectstack/metadata`). A version attributed to
Expand Down Expand Up @@ -166,7 +169,8 @@ function refuseWhereBareReadRefuses(host: Pick<ProtocolHost, 'engine'>, type: st
* @throws {PackageScopedNameRefusedError} `ambiguous-in-environment` for a
* bare request of a name two or more row slots hold (other than a row-held
* base's, above), or the bare plain read's own refusal; `not-held-by-qualifier` for a package that neither
* holds a row of the name nor ships it, while another slot holds one.
* holds a row of the name nor ships it, while any slot holds one (the
* package-less row included, which such a package never addresses).
*/
export async function historySlotOf(
host: Pick<ProtocolHost, 'declinesStoredRow' | 'engine' | 'findServedOverlayRow' | 'packagelessAddress' | 'readItemActiveRows' | 'rethrowUnlessMetadataStoreUnprovisioned' | 'shippedArtifactsOf'>,
Expand Down Expand Up @@ -196,7 +200,11 @@ export async function historySlotOf(
const slot: string | null = served === undefined
? packageId
: typeof binding === 'string' && binding !== '' ? binding : null;
if (!slots.includes(slot) && slots.length > 0
// [#37130] `P` holds the slot it addresses by a row bound to it (its own,
// its pair's declared override); the package-less row the read falls back
// to is held by no package, and is `P`'s only when `P` ships the name.
const holdsSlot = slot !== null && slots.includes(slot);
if (!holdsSlot && slots.length > 0
&& !shippingPackageIds(host.shippedArtifactsOf(type, name, undefined, member)).has(packageId)) {
throw slotRefusal(type, name, 'not-held-by-qualifier', packageId, slots);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/metadata-protocol/src/protocol-package-commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,11 @@ export class ProtocolPackageCommitsFacet {
// [ADR-0146 D2] …and, for the pair's row, the override write gate.
deriveRestoredBody,
...memberOption(member),
// [#37130] The platform's own record is never restored:
// refused into `failed[]` as {@link rollbackMetaItem}
// refuses it, nothing written; the commit's other items
// still revert (ADR-0067 D3).
refusePlatformRecord: true,
});
rowWritten = `its row was restored to the body of version ${restoreToVersion}`;
revertPlan.push({
Expand Down Expand Up @@ -2258,6 +2263,9 @@ export class ProtocolPackageCommitsFacet {
packageId: rollbackPackageId,
// [#32905] …and only a version of that row's slot.
...(slot?.addressed ? { versionOf: slot.owner } : {}),
// [#37130] The platform's own record is never restored: refused
// as the metadata service's `rollback` refuses it, nothing written.
refusePlatformRecord: true,
});
// #4521 — a rollback is a live write like any other: the restored
// body must be the one the runtime dispatches on immediately, not
Expand Down
Loading
Loading