Skip to content
Draft
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
23 changes: 23 additions & 0 deletions .changeset/35924-publish-reparses-object-draft.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@objectstack/metadata-protocol': minor
'@objectstack/spec': patch
---

fix(metadata-protocol)!: publishing an object draft judges it by the current ObjectSchema

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

<!-- adr-0087: registered publish-refuses-object-draft-the-current-schema-refuses -->

**BREAKING** for an object draft the current `ObjectSchema` refuses. Before this change it published; now it is refused at publish, and the refusal names the fix. Stored metadata is not rewritten or refused at boot, and an active row is unchanged. The refusal comes at the next publish of such a draft.

A save checks an object body against the current `ObjectSchema` and refuses it with `422 INVALID_METADATA`. Publishing a draft did not repeat that check. So an object draft saved before a stricter `ObjectSchema` rule landed went live at publish, even though that rule refuses it. One example is a sealed object with no `tenant_id` chain column (ADR-0154 D10).

Both publishes now check the draft the same way a save does: per item (`POST /api/v1/meta/object/:name/publish`) and per package (`POST /api/v1/packages/:id/publish-drafts`), including the pass that judges a batch before it writes on a store that cannot roll back. They refuse it with the same `422 INVALID_METADATA` and the same `issues[]` before anything is written. The draft stays a draft, and a package publish refuses its whole batch with nothing promoted. The draft is checked as the publish would serve it, after the stored-row conversions are applied. So a draft stored in a spelling a later release retired, which those conversions rewrite, still publishes as before.

## FROM → TO

- A pending object draft the current schema refuses, for example a sealed audit object (`lifecycle.seal`) with no `chainField` and no `tenant_id` field. That draft is refused at publish. The fix: declare the chain column, `fields: { tenant_id: { type: 'lookup', reference: 'sys_organization' } }`, or name one, `lifecycle: { seal: { valueFields: [...], chainField: 'organization_id' } }`. Then save the draft again and publish. Whatever the refusal's `issues[]` names, the fix is the one a save of the same body asks for. To find such drafts before upgrading, read each pending object draft: those whose `_diagnostics.valid` is `false` are the ones a publish will refuse.

`@objectstack/spec` adds only the upgrade-guide entry `publish-refuses-object-draft-the-current-schema-refuses`.
12 changes: 12 additions & 0 deletions packages/metadata-protocol/src/protocol-history-and-publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import { historySlotOf, refusePackageSelectorOutsideSlottedTypes } from './proto
import { captureStoredExpressionChange, fillStoredExpressionChange, type StoredExpressionFillCapture } from './protocol-stored-expression-fill.js';
import { OBJECT_REGISTRY_REFUSED, reportObjectRegistryRefusal } from './protocol-registry-hydration.js';
import { OBJECT_EXTENSION_TYPE, registrableObjectExtension } from './protocol-object-extension-item.js';
import { promotedObjectDraftSpecRefusal } from './protocol-promoted-draft-spec-refusal.js';
import { retireOverlayCacheOnRollback } from './meta-overlay-cache.js';

/**
Expand Down Expand Up @@ -1098,6 +1099,17 @@ export class ProtocolHistoryAndPublishFacet {
},
);
}
// [#35924] …and the save door's spec parse, asked of the draft
// this promotion makes live: a draft stored before a narrowing
// `ObjectSchema` refinement landed was never judged by it, and was
// promoted unjudged. Judged as the publish serves it (the ADR-0087
// chain applied), with the save door's `422 INVALID_METADATA` and
// `issues[]`, before `repo.promoteDraft` writes; a judge-only call
// raises it too. Asked ahead of the registry's refusal, as the save
// door orders them, so a body both refuse keeps the structured
// `issues[]` (#5364). See {@link promotedObjectDraftSpecRefusal}.
const specRefusal = promotedObjectDraftSpecRefusal(request.name, draftForGate.body);
if (specRefusal) throw specRefusal;
// [#29673] …and the registry's refusal of the body this promotion
// makes live, on the same row and for the D9.9 refusal's reason:
// the write-through runs after the row is active, where a refusal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* [#35924] The save door's spec parse, asked again of an object draft at its
* promotion.
*
* `saveMetaItem` parses every object body against the current `ObjectSchema`
* (`resolveOverlaySchema` → `getMetadataTypeSchema('object')`) and refuses a
* body it rejects `422 INVALID_METADATA`. `promoteDraftForPublish` re-runs the
* save door's other refusals on the stored draft, because a draft stored
* before a rule landed was never judged by it (#4463 D1), but it did not ask
* the parse. So an object draft saved before a narrowing `ObjectSchema`
* refinement landed was promoted live at publish, unjudged by that refinement
* — the ADR-0154 D10 chain-column rule's sealed object without `tenant_id`
* is the first measured case. This is the parse, asked of the draft the
* promotion reads, with the save door's envelope: the same code and status,
* the same `issues[]` (registered refusal params included, #36594) and the
* same sentence on the default face.
*
* ## The body judged is the body the publish serves
*
* A stored row is read through the ADR-0087 conversion chain
* (`applyConversionsToStoredItem`, replayed with its retired entries by the
* protocol's read and by the database loader that registers the row), so a
* draft stored in a spelling a later release retired is served — and
* registered — in today's canonical shape. The parse judges that converted
* body ({@link convertStoredItemDetailed}, no notice logged here: the read
* warns once when it serves the row), not the stored bytes, so a draft the
* chain converts keeps publishing exactly as before. What is still refused
* after the chain is what the publish would make live while the current
* schema refuses it.
*
* `object` only: the type the card measured. The save door parses every type
* with a registered schema; whether the promotion should ask every one of
* them is a separate question (each type's stored-draft population differs —
* a `flow` is judged through `storedFlowVerdictBody`, a `view` stores its
* parsed projection), and is not answered here.
*/

import { resolveOverlaySchema } from './protocol-meta-type-schema.js';
import { zodIssuesToMetadataIssues, type MetadataIssueEntry } from './protocol-metadata-issues.js';
import { convertStoredItemDetailed } from './protocol-stored-item-conversion.js';
import { specValidationFindings } from './protocol-write-face.js';

/** The refusal {@link promotedObjectDraftSpecRefusal} answers: the save door's spec-validation 422. */
export type PromotedDraftSpecRefusal = Error & { code: 'INVALID_METADATA'; status: 422; issues: MetadataIssueEntry[] };

/**
* The save door's spec-validation refusal of the object draft `body` the
* promotion of `object/name` reads, judged as the publish would serve it;
* `undefined` when the current `ObjectSchema` admits it (or no schema is
* registered for `object`, as the save door then parses nothing).
*/
export function promotedObjectDraftSpecRefusal(name: string, body: unknown): PromotedDraftSpecRefusal | undefined {
const schema = resolveOverlaySchema('object', body);
if (!schema) return undefined;
const served = convertStoredItemDetailed('object', body).item;
const parsed = schema.safeParse(served);
if (parsed.success) return undefined;
const issues = zodIssuesToMetadataIssues(parsed.error.issues, { refusalParams: true });
// No write face: the publish doors declare none, so the sentence carries
// the findings in full, as every undeclared face of the save door does.
const err = new Error(
`object/${name} failed spec validation: ${specValidationFindings(undefined, issues)}`,
) as PromotedDraftSpecRefusal;
err.code = 'INVALID_METADATA';
err.status = 422;
err.issues = issues;
return err;
}
Loading
Loading