diff --git a/.changeset/33851-revert-non-object-write-through-fault.md b/.changeset/33851-revert-non-object-write-through-fault.md new file mode 100644 index 00000000000..8a3835c99b6 --- /dev/null +++ b/.changeset/33851-revert-non-object-write-through-fault.md @@ -0,0 +1,16 @@ +--- +'@objectstack/metadata-protocol': patch +--- + +fix(metadata-protocol): `revertCommit` reports an item of any type whose registry write-through or heal faults + +Clause-②: no +Metadata-protocol: none + +`revertCommit` now puts a restored `view`, `permission`, `object_extension` or other non-object item into `failed[]` when the runtime registry write-through for its restored body throws, and the answer's `success` is `false`. The entry names the row state ("its row was restored to the body of version N") beside the registry's reason, and carries the reason's `code` where it declares one, the same way a refused `object` has been reported since the previous change to this door. Before, the write-through's non-object branches (the overlay hydration, and the `object_extension` registration) only logged the fault at `warn`, so such a revert answered `success: true` with the item in `reverted[]`, while the process kept serving the body the revert replaced. `rollbackToPackageCommit` carries each such item into its own `failed[]` and answers `success: false`. + +A removed item is reported the same way. When the revert soft-removes an item the reverted commit created and the registry heal after the removal stops (a registry throw, or the registry refusing a row of the item still stored under another package), the item goes into `failed[]` naming "its row was removed from sys_metadata" and that the registry may still serve the removed row, with the reason and its `code`. Before, the heal's stop was only an `error` line in the server log, and the item answered `removed`. The `error` line is unchanged. + +This supersedes the sentence of the note on the previous change to this door that says a write-through fault of every type other than `object` "is still not seen by the revert": after this change, the revert sees it, and a stopped heal too. + +Save, publish and delete do not change. Save and publish do not ask the write-through for these faults: under the same fault, `saveMetaItem` and `publishMetaItem` of a non-object item still answer success with no advisory, and the fault is still the same `warn` line in the server log. `deleteMetaItem`, a publish's undo and a peer's convergence ignore the heal's answer, so a stopped heal still answers what it answered before there, with the same `error` line. diff --git a/packages/metadata-protocol/src/protocol-package-commits.ts b/packages/metadata-protocol/src/protocol-package-commits.ts index 663eec162dc..6e576f0d6c2 100644 --- a/packages/metadata-protocol/src/protocol-package-commits.ts +++ b/packages/metadata-protocol/src/protocol-package-commits.ts @@ -1567,7 +1567,19 @@ export class ProtocolPackageCommitsFacet { // `object` serves no field afterwards, so it changes none. const removedCapture = captureStoredExpressionChange(host.engine, PLURAL_TO_SINGULAR[it.type] ?? it.type, it.name); if (removedCapture && !storedExpressions.has(removedCapture.object)) storedExpressions.set(removedCapture.object, removedCapture); - await host.restoreArtifactRegistryView(it.type, it.name, null, commitPackageId, member); + // [#33851] The heal's stop is this item's outcome too, the way + // the restore limb reports the write-through's refusal: the + // row is removed and the registry may still serve it. Only + // where a row was removed; with none, the heal is a + // self-heal of nothing this revert wrote, and stays logged. + const healStop = await host.restoreArtifactRegistryView(it.type, it.name, null, commitPackageId, member); + if (healStop && current) { + notApplied.push( + 'the runtime registry was not healed after the removal, so it may still serve the removed row (' + + `${clientFacingFailureText(healStop.error, 'the reason is in the server log')})`, + ); + notAppliedCode = clientFacingFailureCode(healStop.error); + } // [#14179] …and announce it on the ONE choke point, // {@link emitMetadataMutation}, exactly as the sibling // delete caller {@link deleteMetaItem} does after its own @@ -1744,6 +1756,10 @@ export class ProtocolPackageCommitsFacet { // [#33563] The registry's refusal of the restored object is // this item's outcome, not a line on stderr: the row holds // the pre-commit body and the registry does not serve it. + // [#33851] …and so is a fault of any other type's branch + // (the overlay hydration, the `object_extension` + // registration), which the write-through answers only to a + // caller that asks (`answerFaults`). const registryRefusal = await host.applyRegistryWriteThrough({ type: PLURAL_TO_SINGULAR[it.type] ?? it.type, name: it.name, @@ -1757,6 +1773,7 @@ export class ProtocolPackageCommitsFacet { // said "the row's OWN scope" while passing the REQUEST's // org; the resolution above is what makes the comment true. organizationId: null, + answerFaults: true, }); if (registryRefusal) { notApplied.push( diff --git a/packages/metadata-protocol/src/protocol-registry-hydration.ts b/packages/metadata-protocol/src/protocol-registry-hydration.ts index 4f9d7c44397..c47e94f74b6 100644 --- a/packages/metadata-protocol/src/protocol-registry-hydration.ts +++ b/packages/metadata-protocol/src/protocol-registry-hydration.ts @@ -1458,21 +1458,14 @@ export class ProtocolRegistryHydrationFacet { * The orphan adoption ({@link reassignOrphanedMetadata}) calls it for each * view row it rebinds, with `reboundFrom`, and [#23646] for each object * row, once {@link releaseAdoptedObjectOwner} has handed it the name. + * + * [#33851] What it answers: the registry's refusal of an object (the + * object branch, #33005), and nothing else unless the caller asks + * ({@link RegistryWriteThroughRequest.answerFaults}). A non-object branch + * that throws logs the fault at `warn` and, for a caller that asked, + * answers it in the same shape. */ - static async applyRegistryWriteThrough(host: Pick, request: { - type: string; - name: string; - item?: any; - packageId?: string | null; - /** The row's org scope — `null` for an env-wide row. [#6602] */ - organizationId: string | null; - /** - * [#23198] The binding the write moved the row from, when it moved it - * (an orphan adoption): `null` for the package-less row. See - * {@link HydrationOptions}. - */ - reboundFrom?: { readonly packageId: string | null }; - }): Promise { + static async applyRegistryWriteThrough(host: Pick, request: RegistryWriteThroughRequest): Promise { // [#8862] The `|| request.type === 'objects'` limb is GONE — this // branch admitted the plural that {@link applyObjectRegistryMutation} // then consumed. All four call sites of this method fold at their @@ -1527,6 +1520,8 @@ export class ProtocolRegistryHydrationFacet { console.warn( `[Protocol] registry write-through failed for ${request.type}/${request.name}: ${err?.message ?? err}`, ); + // [#33851] …and answered, to a caller that asked. + if (request.answerFaults === true) return { error: err }; } return undefined; } @@ -1546,6 +1541,10 @@ export class ProtocolRegistryHydrationFacet { console.warn( `[Protocol] registry write-through failed for ${request.type}/${request.name}: ${err?.message ?? err}`, ); + // [#33851] …and answered, to a caller that asked: the write is + // still not failed here, the caller decides what the fault means + // for its own answer. + if (request.answerFaults === true) return { error: err }; } return undefined; } @@ -1734,6 +1733,15 @@ export class ProtocolRegistryHydrationFacet { * naming no package answers the store's refusal, naming every holder. So it * does for a row bound to a non-holder of the name whose pair relation names * zero or several bases (epic ruling 6818436831, #32620). + * + * [#33851] What it answers: the walk's stop ({@link RegistryHealStop}), + * when a throw stopped it (the logged stop above) or the registry refused or + * faulted on a row still stored (the step that registers it again); + * otherwise `undefined`, a designed outcome included (a refused baseline + * read that retires the entry, a degraded read that keeps it). Only + * `revertCommit` reads it, and reports the item in `failed[]`; every other + * caller ignores it, so a delete, a publish undo and a peer's convergence + * answer what they answered before. */ static async restoreArtifactRegistryView(host: Pick, type: string, name: string, @@ -1755,7 +1763,7 @@ export class ProtocolRegistryHydrationFacet { * rows are still stored and served, so their registry entries stay. * No effect for a type with no members. */ - member?: string,): Promise { + member?: string,): Promise { // [#6780] ADR-0005 — the plain-key entry belongs to the env-wide row, // so only an env-wide removal may heal (or retire) it. if (organizationId !== null && organizationId !== undefined) return; @@ -1895,16 +1903,22 @@ export class ProtocolRegistryHydrationFacet { } } } + // [#33851] A remaining row the registry does not take is the + // heal's stop too: the first refusal or fault is answered, after + // every row has been tried. + let refused: RegistryHealStop | undefined; for (const row of remainingRows) { - await host.applyRegistryWriteThrough({ + const refusal = await host.applyRegistryWriteThrough({ type: canonical, name, item: host.convertStoredItem(String(row.type ?? canonical), storedRowDocument(row)), packageId: boundPackageOf(row.package_id) ?? null, organizationId: null, + answerFaults: true, }); + refused ??= refusal; } - return; + return refused; } if (healed) return; @@ -2074,7 +2088,10 @@ export class ProtocolRegistryHydrationFacet { // [#32366] Best-effort, never silent: the delete has committed and // is not failed here, and what stopped the walk is said once. console.error(registryHealStoppedMessage(type, name, error)); + // [#33851] …and answered: `revertCommit` reports it per item. + return { error }; } + return undefined; } /** @@ -2473,11 +2490,59 @@ export function objectStorageSyncRefusedNotice(name: string, err: unknown): Runt * answers when the registry refused the object it was asked to register: the * thrown value, whole. The row is already persisted; this is the one fact the * write-through used to keep to a `warn` line. + * + * [#33851] The write-through answers a non-object branch's fault in this + * shape too, to a caller that asked ({@link RegistryWriteThroughRequest.answerFaults}). */ export interface ObjectRegistryRefusal { readonly error: unknown; } +/** + * [#33851] What {@link ProtocolRegistryHydrationFacet.restoreArtifactRegistryView} + * answers when its walk stopped (a throw, logged at `error` as before) or a row + * still stored was refused by the registry: the thrown or refused value, whole. + * The row removal is already committed; this is the fact the heal used to keep + * to its log line. + */ +export interface RegistryHealStop { + readonly error: unknown; +} + +/** + * What {@link ProtocolRegistryHydrationFacet.applyRegistryWriteThrough} is + * asked to make live: the row's type, name and body, its binding and scope. + */ +export interface RegistryWriteThroughRequest { + type: string; + name: string; + item?: any; + packageId?: string | null; + /** The row's org scope — `null` for an env-wide row. [#6602] */ + organizationId: string | null; + /** + * [#23198] The binding the write moved the row from, when it moved it + * (an orphan adoption): `null` for the package-less row. See + * {@link HydrationOptions}. + */ + reboundFrom?: { readonly packageId: string | null }; + /** + * [#33851] `true`: a non-object branch whose registration throws (the + * `object_extension` registration, the overlay hydration) answers the + * fault as the object branch answers the registry's refusal, beside the + * `warn` line it logs. `revertCommit` asks, so a revert whose restored + * body the registry does not serve answers the item in `failed[]` + * (ADR-0067 D3, the method's docblock: a revert that answers success is + * one the running process has acted on). + * + * Left unset, the fault is only logged and the answer is `undefined`, as + * before. The save and publish doors leave it unset: they report a + * returned refusal as an object's (`object-registry-refused`), and their + * answers for every other type do not change. + */ + answerFaults?: boolean; +} + /** * [#33005] The rule id of the advisory a publish answers with when the runtime * registry refused an object the publish made live. Stable across releases, diff --git a/packages/metadata-protocol/src/protocol.revert-commit-honest-outcome.test.ts b/packages/metadata-protocol/src/protocol.revert-commit-honest-outcome.test.ts index ff8044715a0..9c57c09ac6c 100644 --- a/packages/metadata-protocol/src/protocol.revert-commit-honest-outcome.test.ts +++ b/packages/metadata-protocol/src/protocol.revert-commit-honest-outcome.test.ts @@ -17,6 +17,10 @@ * `{ success: true, reverted: [{ …, action: 'restored' | 'removed' }], * failed: [] }`, and the only trace was a `warn` line on stderr. * + * §3 [#33851] …and so does a restored non-object item (`view`, `permission`, + * `object_extension`) whose registry write-through throws, while save and + * publish under the same fault keep their answers. + * * The harness is an in-memory stub engine under the real * `SysMetadataRepository` (the pattern of * `protocol.recovery-doors-mutation-projector.test.ts`), with a commit store @@ -447,3 +451,298 @@ describe('[#33564] reverting a revert commit restores what the revert replaced', expect(storedLabel(rows)).toBe('Renamed'); }); }); + +// ═══════════════════════════════════════════════════════════════════════════ +// §3 [#33851] A non-object type's write-through fault is a failed item too +// ═══════════════════════════════════════════════════════════════════════════ +// +// The write-through's non-object branches (the overlay hydration every type +// but `object` and `object_extension` takes, and the `object_extension` +// registration) caught their own faults and only logged them, so a revert of +// a `view`, a `permission` or an `object_extension` whose registration threw +// answered `success: true`. Measured on this harness before the fix: each +// fault case below answered `{ success: true, reverted: [{ …, action: +// 'restored' }], failed: [] }`. The revert now asks those branches for the +// fault and reports it as §1 reports an object's refusal. Save and publish do +// not ask: under the same fault they answer what they answered before, and +// the fault stays a `warn` line (the controls below, green before the fix too). +// +// The kernel is one whose registry takes this kernel's stored rows +// (`storeHydratedIntoRegistry`), so the overlay branch reaches the registry at +// all; on the plain `env_prod` kernel of §1 it returns before it. + +const REFUSAL = (type: string) => + Object.assign(new Error(`The registry is sealed for ${type} entries.`), { code: 'NOT_OVERRIDABLE', status: 403 }); + +interface NonObjectCase { + readonly type: string; + readonly name: string; + readonly body: (label: string) => Record; + /** The label a stored body carries. */ + readonly labelOf: (body: any) => unknown; + /** The registry verb this type's write-through branch registers through. */ + readonly verb: 'registerItem' | 'registerObjectExtension'; +} + +const NON_OBJECT_CASES: readonly NonObjectCase[] = [ + { + // The overlay hydration branch. + type: 'view', + name: 'ticket.grid', + body: (label) => ({ + name: 'ticket.grid', label, object: 'ticket', viewKind: 'list', + config: { type: 'grid', data: { provider: 'object', object: 'ticket' }, columns: ['title'] }, + }), + labelOf: (body) => body.label, + verb: 'registerItem', + }, + { + // The overlay hydration branch, a type with no container. + type: 'permission', + name: 'ticket_reader', + body: (label) => ({ name: 'ticket_reader', label, objects: {} }), + labelOf: (body) => body.label, + verb: 'registerItem', + }, + { + // The `object_extension` branch. + type: 'object_extension', + name: 'ticket', + body: (label) => ({ extend: 'ticket', fields: { tier: { type: 'text', label } } }), + labelOf: (body) => body.fields?.tier?.label, + verb: 'registerObjectExtension', + }, +]; + +/** The stored active row's label for the case's item. */ +const storedLabelOf = (rows: Map, c: NonObjectCase): unknown => { + for (const r of rows.values()) { + if (r.type === c.type && r.name === c.name && r.state === 'active') return c.labelOf(JSON.parse(r.metadata)); + } + return undefined; +}; + +/** + * A kernel whose registry takes its stored rows, so the non-object + * write-through registers, with the object the view and the extension read + * already saved. `registered` records each registration of the case's verb. + */ +async function makeHydratingProtocol(c: NonObjectCase) { + const rig = makeProtocol(); + rig.protocol.storeHydratedIntoRegistry = true; + await rig.protocol.saveMetaItem({ type: 'object', name: 'ticket', item: objectBody('Ticket'), packageId: PKG }); + // The stub registry keeps no objects: served here, for the extension's `extend`. + rig.engine.registry.getObject = (name: string) => (name === 'ticket' ? objectBody('Ticket') : undefined); + const registered: unknown[] = []; + rig.engine.registry[c.verb] = (...args: unknown[]) => { registered.push(args); }; + /** Make the case's verb throw from now on. */ + const refuse = () => { rig.engine.registry[c.verb] = () => { throw REFUSAL(c.type); }; }; + return { ...rig, registered, refuse }; +} + +async function nonObjectEditedTwice(c: NonObjectCase) { + const rig = await makeHydratingProtocol(c); + await rig.protocol.saveMetaItem({ type: c.type, name: c.name, item: c.body('First'), packageId: PKG }); + await rig.protocol.saveMetaItem({ type: c.type, name: c.name, item: c.body('Renamed'), packageId: PKG }); + recordApply(rig.commits, 'c1', [{ type: c.type, name: c.name, existedBefore: true, prevVersion: 1 }]); + return rig; +} + +/** The `warn` line the write-through's non-object branch logs for a fault. */ +const writeThroughWarnLine = (c: NonObjectCase) => + `[Protocol] registry write-through failed for ${c.type}/${c.name}: The registry is sealed for ${c.type} entries.`; + +describe('[#33851] revertCommit reports a non-object write-through fault in failed[]', () => { + for (const c of NON_OBJECT_CASES) { + describe(c.type, () => { + it('control: a clean restore reverts it, registers the restored body and answers success', async () => { + const { protocol, rows, registered } = await nonObjectEditedTwice(c); + registered.length = 0; + + const res = await protocol.revertCommit({ commitId: 'c1', packageId: PKG }); + + expect(res).toMatchObject({ success: true, revertedCount: 1, failedCount: 0, failed: [] }); + expect(res.reverted).toEqual([{ type: c.type, name: c.name, action: 'restored' }]); + expect(storedLabelOf(rows, c)).toBe('First'); + // Non-vacuity: the restore reached the branch's registry verb. + expect(registered.length).toBeGreaterThan(0); + }); + + it('a write-through that throws: failed[], row state and code named, success false', async () => { + const { protocol, rows, commits, refuse } = await nonObjectEditedTwice(c); + vi.spyOn(console, 'warn').mockImplementation(() => {}); + refuse(); + + const res = await protocol.revertCommit({ commitId: 'c1', packageId: PKG }); + + expect(res.success).toBe(false); + expect(res.reverted).toEqual([]); + expect(res.failedCount).toBe(1); + expect(res.failed[0]).toMatchObject({ type: c.type, name: c.name, code: 'NOT_OVERRIDABLE' }); + expect(res.failed[0].error).toContain('its row was restored to the body of version 1'); + expect(res.failed[0].error).toContain(`The registry is sealed for ${c.type} entries.`); + // The row change happened, and the revert's own commit records it. + expect(storedLabelOf(rows, c)).toBe('First'); + const [revert] = revertsOf(commits, 'c1'); + expect(JSON.parse(revert.items).map((i: any) => [i.type, i.name])).toEqual([[c.type, c.name]]); + }); + + it('control: a save under the same fault answers as before, the fault only logged', async () => { + const { protocol, rows, refuse } = await makeHydratingProtocol(c); + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + refuse(); + + const res = await protocol.saveMetaItem({ type: c.type, name: c.name, item: c.body('First'), packageId: PKG }); + + expect(res.success).toBe(true); + expect(res.advisories ?? []).toEqual([]); + expect(storedLabelOf(rows, c)).toBe('First'); + expect(warn.mock.calls.map((a) => String(a[0]))).toContain(writeThroughWarnLine(c)); + }); + + it('control: a publish of a draft under the same fault answers as before, the fault only logged', async () => { + const { protocol, rows, refuse } = await makeHydratingProtocol(c); + await protocol.saveMetaItem({ type: c.type, name: c.name, item: c.body('First'), packageId: PKG, mode: 'draft' }); + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + refuse(); + + const res = await protocol.publishMetaItem({ type: c.type, name: c.name, packageId: PKG }); + + expect(res.success).toBe(true); + expect(res.advisories ?? []).toEqual([]); + expect(storedLabelOf(rows, c)).toBe('First'); + expect(warn.mock.calls.map((a) => String(a[0]))).toContain(writeThroughWarnLine(c)); + }); + }); + } +}); + +// ═══════════════════════════════════════════════════════════════════════════ +// §4 [#33851] A removed item whose registry heal stops is a failed item too +// ═══════════════════════════════════════════════════════════════════════════ +// +// The soft-remove limb heals the registry after it removes the row +// (`restoreArtifactRegistryView`). A throw that stopped the heal was logged at +// `error` and not read, so the revert answered `{ success: true, action: +// 'removed' }` while the registry could still serve the removed row (measured +// on this harness before the fix). The heal now answers its stop, and the +// revert reports the item as §1 and §3 report theirs. The heal's `error` line +// is unchanged, and every other caller ignores the answer: the delete, save +// and publish controls answer what they answered before. +// +// So does a row still stored under another package that the heal registers +// again and the registry refuses. + +/** The non-object cases whose heal walks the overlay tiers (an extension resyncs instead). */ +const HEAL_CASES = NON_OBJECT_CASES.filter((c) => c.verb === 'registerItem'); + +/** Saved once, by the commit under revert: the revert soft-removes it. */ +async function nonObjectCreatedByTheCommit(c: NonObjectCase) { + const rig = await makeHydratingProtocol(c); + await rig.protocol.saveMetaItem({ type: c.type, name: c.name, item: c.body('First'), packageId: PKG }); + recordApply(rig.commits, 'c1', [{ type: c.type, name: c.name, existedBefore: false, prevVersion: null }]); + return rig; +} + +/** The registry's tier-1 verbs throwing, which stops the heal's walk at its outer catch. */ +function healStops(engine: any, type: string) { + engine.registry.removePackageOverlaySlots = () => { throw REFUSAL(type); }; + engine.registry.removeRuntimeShadow = () => { throw REFUSAL(type); }; +} + +const healStoppedLine = (c: NonObjectCase) => + `[Protocol] the registry heal after the removal of ${c.type} '${c.name}' stopped: The registry is sealed for ${c.type} entries.`; + +describe('[#33851] revertCommit reports a removed item whose registry heal stops in failed[]', () => { + for (const c of HEAL_CASES) { + describe(c.type, () => { + it('control: a clean soft-remove reverts it and answers success', async () => { + const { protocol, rows } = await nonObjectCreatedByTheCommit(c); + + const res = await protocol.revertCommit({ commitId: 'c1', packageId: PKG }); + + expect(res).toMatchObject({ success: true, revertedCount: 1, failedCount: 0, failed: [] }); + expect(res.reverted).toEqual([{ type: c.type, name: c.name, action: 'removed' }]); + expect(storedLabelOf(rows, c)).toBeUndefined(); + }); + + it('a heal that stops: failed[], the removed row and the code named, success false, the error line kept', async () => { + const { protocol, rows, commits, engine } = await nonObjectCreatedByTheCommit(c); + const error = vi.spyOn(console, 'error').mockImplementation(() => {}); + healStops(engine, c.type); + + const res = await protocol.revertCommit({ commitId: 'c1', packageId: PKG }); + + expect(res.success).toBe(false); + expect(res.reverted).toEqual([]); + expect(res.failedCount).toBe(1); + expect(res.failed[0]).toMatchObject({ type: c.type, name: c.name, code: 'NOT_OVERRIDABLE' }); + expect(res.failed[0].error).toContain('its row was removed from sys_metadata'); + expect(res.failed[0].error).toContain('may still serve the removed row'); + expect(res.failed[0].error).toContain(`The registry is sealed for ${c.type} entries.`); + expect(error.mock.calls.some((a) => String(a[0]).startsWith(healStoppedLine(c)))).toBe(true); + // The row is gone, and the revert's own commit records it. + expect(storedLabelOf(rows, c)).toBeUndefined(); + expect(JSON.parse(revertsOf(commits, 'c1')[0].items)).toHaveLength(1); + }); + + it('control: a delete under the same fault answers as before, the stop only logged', async () => { + const { protocol, rows, engine } = await makeHydratingProtocol(c); + await protocol.saveMetaItem({ type: c.type, name: c.name, item: c.body('First'), packageId: PKG }); + const error = vi.spyOn(console, 'error').mockImplementation(() => {}); + healStops(engine, c.type); + + const res = await protocol.deleteMetaItem({ type: c.type, name: c.name, packageId: PKG }); + + expect(res.success).toBe(true); + expect(storedLabelOf(rows, c)).toBeUndefined(); + expect(error.mock.calls.some((a) => String(a[0]).startsWith(healStoppedLine(c)))).toBe(true); + }); + + it('control: a save and a publish under the same fault answer as before', async () => { + const { protocol, rows, engine } = await makeHydratingProtocol(c); + vi.spyOn(console, 'error').mockImplementation(() => {}); + healStops(engine, c.type); + + const saved = await protocol.saveMetaItem({ type: c.type, name: c.name, item: c.body('First'), packageId: PKG }); + await protocol.saveMetaItem({ type: c.type, name: c.name, item: c.body('Draft'), packageId: PKG, mode: 'draft' }); + const published = await protocol.publishMetaItem({ type: c.type, name: c.name, packageId: PKG }); + + expect(saved.success).toBe(true); + expect(saved.advisories ?? []).toEqual([]); + expect(published.success).toBe(true); + expect(published.advisories ?? []).toEqual([]); + expect(storedLabelOf(rows, c)).toBe('Draft'); + }); + }); + } + + it('a row still stored under another package that the registry refuses: failed[], success false', async () => { + // An `action`: a type two packages may each hold a row of under one name. + const c: NonObjectCase = { + type: 'action', name: 'close_ticket', verb: 'registerItem', labelOf: (body) => body.label, + body: (label) => ({ name: 'close_ticket', label, type: 'url', target: 'https://example.com/close' }), + }; + const OTHER = 'app.other'; + const { protocol, rows, commits, engine } = await makeHydratingProtocol(c); + engine.registry.getPackage = (id: string) => (id === PKG || id === OTHER ? { manifest: { id } } : undefined); + await protocol.saveMetaItem({ type: c.type, name: c.name, item: c.body('Other'), packageId: OTHER }); + await protocol.saveMetaItem({ type: c.type, name: c.name, item: c.body('First'), packageId: PKG }); + recordApply(commits, 'c1', [{ type: c.type, name: c.name, existedBefore: false, prevVersion: null }]); + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + const error = vi.spyOn(console, 'error').mockImplementation(() => {}); + engine.registry.registerItem = () => { throw REFUSAL(c.type); }; + + const res = await protocol.revertCommit({ commitId: 'c1', packageId: PKG }); + + expect(res.success).toBe(false); + expect(res.failed[0]).toMatchObject({ type: c.type, name: c.name, code: 'NOT_OVERRIDABLE' }); + expect(res.failed[0].error).toContain('its row was removed from sys_metadata'); + // Non-vacuity: the stop is the remaining row's write-through fault, not + // the walk's outer catch. + expect(warn.mock.calls.map((a) => String(a[0]))).toContain(writeThroughWarnLine(c)); + expect(error.mock.calls.some((a) => String(a[0]).startsWith(healStoppedLine(c)))).toBe(false); + // The other package's row stays stored. + expect(storedLabelOf(rows, c)).toBe('Other'); + }); +}); diff --git a/packages/metadata-protocol/src/protocol.ts b/packages/metadata-protocol/src/protocol.ts index b28c45d112c..ae548d425a6 100644 --- a/packages/metadata-protocol/src/protocol.ts +++ b/packages/metadata-protocol/src/protocol.ts @@ -615,7 +615,7 @@ import { ProtocolWriteGatesFacet } from './protocol-write-gates.js'; // [#29985 codemod] The static tables of the facet above: the class's fields read them (#30147). import { ProtocolWriteGatesTables } from './protocol-write-gates-tables.js'; // [#29985 codemod] Registry hydration, view expansion, write-through, object storage: the members below delegate to its facet, `ProtocolRegistryHydrationFacet`. -import { ProtocolRegistryHydrationFacet, type ObjectRegistryRefusal } from './protocol-registry-hydration.js'; +import { ProtocolRegistryHydrationFacet, type ObjectRegistryRefusal, type RegistryHealStop, type RegistryWriteThroughRequest } from './protocol-registry-hydration.js'; import type { HealPackageBinding } from './protocol-heal-binding.js'; // [#29985 codemod] Save refusals, saveMetaItem, saveConflict, migrateStoredMetadata: the members below delegate to its facet, `ProtocolSaveFacet`. import { ProtocolSaveFacet } from './protocol-save.js'; @@ -8396,20 +8396,7 @@ export class ObjectStackProtocolImplementation implements } /** Docblock and body: {@link ProtocolRegistryHydrationFacet.applyRegistryWriteThrough} in `protocol-registry-hydration.ts` [#29985 codemod]. */ - private async applyRegistryWriteThrough(request: { - type: string; - name: string; - item?: any; - packageId?: string | null; - /** The row's org scope — `null` for an env-wide row. [#6602] */ - organizationId: string | null; - /** - * [#23198] The binding the write moved the row from, when it moved it - * (an orphan adoption): `null` for the package-less row. See - * {@link HydrationOptions}. - */ - reboundFrom?: { readonly packageId: string | null }; - }): Promise { + private async applyRegistryWriteThrough(request: RegistryWriteThroughRequest): Promise { // [#29985 codemod] The body moved to `protocol-registry-hydration.ts`; it reaches this class through `protocol-host.ts`. return ProtocolRegistryHydrationFacet.applyRegistryWriteThrough(protocolHost(this), request); } @@ -8431,7 +8418,7 @@ export class ObjectStackProtocolImplementation implements * No effect for a type with no members. */ member?: string, - ): Promise { + ): Promise { // [#29985 codemod] The body moved to `protocol-registry-hydration.ts`; it reaches this class through `protocol-host.ts`. return ProtocolRegistryHydrationFacet.restoreArtifactRegistryView(protocolHost(this), type, name, organizationId, packageId, member); } @@ -10860,9 +10847,11 @@ export class ObjectStackProtocolImplementation implements * advisory stays on `advisories`), its mutation projector failed, or a * step after the row write threw. The entry names the row state (removed, * or restored to the body of version N) beside the step that failed, and - * the revert's own commit still records the row change. A write-through - * fault the registry helper only logs (every type but `object`) is not - * seen here. + * the revert's own commit still records the row change. + * [#33851] That holds for every type: a restored item whose registry + * write-through threw (not only an `object` the registry refused), and a + * removed item whose registry heal stopped, so the registry may still + * serve the removed row. * * [#33564, ADR-0067 D3] The revert's own commit records, per row it * changed, the version that row held before the revert (`prevVersion`),