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
26 changes: 26 additions & 0 deletions .changeset/37766-publish-gate-approvals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
'@objectstack/plugin-approvals': patch
---

feat(approvals): a flow or an object that an approval could not open on is refused where it is published (ADR-0162 Execution item 7, reported at authoring)

Clause-②: yes (widening)
Metadata-protocol: none

Widened: `ApprovalService` gains `judgePublishedItem(item)`. The plugin's message catalog gains the sentence `approvalPublishRefusedLead` in all 11 locale packs.

At kernel:ready the plugin adds publish gates for `flow` and `object` beside the owners' gates. They judge a write that goes `active` by the conditions an approval opens on (the check at an approval's first request, ADR-0162 D5), as the engine will hold things once the write lands.

- **A flow.** It is judged as an approval's root, together with every flow that calls it through a `subflow` or a `map`, to any depth, with the written body in place. The walk up the callers ends on a flow that calls itself and on flows that call each other. It stops after 64 roots: past that, nothing is refused for the cap, and one warning says so.
- **An object.** Every flow that can start on one of its records, or maps over them, is judged with the written object in place. Only gaps on the written object are reported.
- **A package publish.** The batch's other objects and flows are read in place of the live ones of the same name. A package that declares `closedStates` and repairs the flow it closes on in the same publish goes through, whichever draft is promoted first.

The refusal is a 400 `VALIDATION_FAILED` that names the item, then gives one sentence per gap: the flow, the step, the object, the field and what is missing, in the first locale the write door states. These are the same sentences an approval's first request is refused with.

Not refused at publish:
- a draft (it is judged when it is promoted);
- a flow that a node calls and the engine does not hold;
- a host with no `job` service (the first request still refuses both);
- an application in which no object declares `closedStates`, where nothing is read.

A judge that fails is logged, and the write goes ahead. The rule stays the refusal at the approval's first request: the objects and the flows are published apart, and only that request has them all in hand.
18 changes: 18 additions & 0 deletions .changeset/37766-publish-gate-door.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@objectstack/metadata-protocol': patch
---

feat(metadata-protocol): a plugin can add an authoring gate beside a type's owner gate, and an added gate also judges a draft's promotion

Clause-②: yes (widening)
Metadata-protocol: none

Widened: `ObjectStackProtocolImplementation` gains `addAuthoringGate(type, gate)`, and `MetadataAuthoringGateContext` gains an optional `pending`.

`registerAuthoringGate` keeps one gate per type, and a second registration still replaces the first. A plugin that needs to judge a type another plugin owns now adds its gate with `addAuthoringGate` instead. plugin-security owns `object`, for example.

- An added gate runs after the owner gate, in the order gates were added, on every write the owner gate runs on: a save in either mode, and a restore.
- An added gate also runs when a draft is promoted to `active` (`publishMetaItem`, a package publish, a judge-only call), with `state: 'active'`. The owner gate still does not run there.
- At a package publish's promotion, an added gate is handed `pending`: the `object` and `flow` bodies of the batch's drafts, which go `active` with this one. A gate that reads other items reads these in place of the live ones of the same name, so a batch is judged as it will be live. `pending` is absent on a save, a restore and a single publish.
- A declared `package-author` channel runs no added gate, as it runs no owner gate.
- A throw from an added gate refuses the write before it is stored, as an owner gate's does.
9 changes: 9 additions & 0 deletions packages/metadata-protocol/src/protocol-authoring-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ export interface MetadataAuthoringGateContext {
* the same name.
*/
packageId?: string | null;
/**
* [#37766, ADR-0162 item 7] At a package publish's promotion of a draft:
* the `object` and `flow` bodies of the batch's drafts, which go `active`
* with this one. A gate that reads other items reads these in place of the
* live ones of the same name, so a batch is judged as it will be live, not
* draft by draft against the engine as it stood before the batch. Absent
* on a save, a restore and a single publish.
*/
pending?: { objects: readonly unknown[]; flows: readonly unknown[] };
}

export type MetadataAuthoringGate = (ctx: MetadataAuthoringGateContext) => void | Promise<void>;
Expand Down
37 changes: 28 additions & 9 deletions packages/metadata-protocol/src/protocol-authoring-gate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
*/

import type { ObjectStackProtocolImplementation } from './protocol.js';
import type { MetadataAuthoringGateContext } from './protocol-authoring-channel.js';
import { permissionSetOwnerBindingRefusal, runtimeHookWithoutBodyRefusal } from './protocol.js';
import { objectBodyExtensionFieldSuffixRefusal } from './protocol-extension-field-suffix.js';
import { datasourceCredentialsRefSaveRefusal } from './protocol-datasource-credentials-ref.js';
Expand Down Expand Up @@ -76,6 +77,7 @@ export interface AuthoringGateHost {
assertRuntimeAuthoringRules: ObjectStackProtocolImplementation['assertRuntimeAuthoringRules'];
readonly authoringChannel: ObjectStackProtocolImplementation['authoringChannel'];
readonly authoringGates: ObjectStackProtocolImplementation['authoringGates'];
readonly additionalAuthoringGates: ObjectStackProtocolImplementation['additionalAuthoringGates'];
collectBatchPendingDeclarations: ObjectStackProtocolImplementation['collectBatchPendingDeclarations'];
credentialChannelFor: ObjectStackProtocolImplementation['credentialChannelFor'];
defaultRecordPageSaveRefusal: ObjectStackProtocolImplementation['defaultRecordPageSaveRefusal'];
Expand Down Expand Up @@ -206,24 +208,35 @@ export async function refuseRestoredBodyTheSaveDoorRefuses(
}
}

/** The body of `ObjectStackProtocolImplementation#runAuthoringGate`; the method's docblock there is its contract. */
export async function runAuthoringGate(
this: Pick<AuthoringGateHost, 'authoringGates' | 'engine' | 'isArtifactBacked'>,
evt: {
/** The write a gate is run for: what the save, restore and promotion doors state. */
type AuthoringGateEvent = {
type: string; name: string; state: 'draft' | 'active'; body: unknown;
/** [#37766] Run the gates added beside the owner (`addAuthoringGate`) alone: a draft's promotion, which the owner gate does not run on. */
addedOnly?: boolean;
userTextLocales?: readonly (string | undefined)[]; packageId?: string | null;
}): Promise<void> {
/** [#37766] A package publish's other drafts, handed to the gates as {@link MetadataAuthoringGateContext.pending}. */
pending?: RuntimePendingDeclarations;
};

/** The body of `ObjectStackProtocolImplementation#runAuthoringGate`; the method's docblock there is its contract. */
export async function runAuthoringGate(
this: Pick<AuthoringGateHost, 'authoringGates' | 'additionalAuthoringGates' | 'engine' | 'isArtifactBacked'>,
evt: AuthoringGateEvent,
): Promise<void> {
const singular = PLURAL_TO_SINGULAR[evt.type] ?? evt.type;
const gate = this.authoringGates.get(singular);
if (!gate) return;
const gate = evt.addedOnly ? undefined : this.authoringGates.get(singular);
// [ADR-0162 item 7, #37766] The gates added beside the owner
// (`addAuthoringGate`) run after it, in the order they were added.
const added = this.additionalAuthoringGates.get(singular) ?? [];
if (!gate && added.length === 0) return;
const artifactBacked = this.isArtifactBacked(evt.type, evt.name);
let declaredBody: unknown;
if (artifactBacked && typeof this.engine.registry?.getItem === 'function') {
const alt = PLURAL_TO_SINGULAR[evt.type] ?? SINGULAR_TO_PLURAL[evt.type];
declaredBody = this.engine.registry.getItem(evt.type, evt.name)
?? (alt ? this.engine.registry.getItem(alt, evt.name) : undefined);
}
await gate({
const ctx: MetadataAuthoringGateContext = {
type: singular,
name: evt.name,
state: evt.state,
Expand All @@ -235,7 +248,13 @@ export async function runAuthoringGate(
: {}),
// [ADR-0150 D2, #34226] The package the write is addressed to.
...(typeof evt.packageId === 'string' && evt.packageId !== '' ? { packageId: evt.packageId } : {}),
});
// [#37766] A package publish's other drafts, as the batch will leave them live.
...(evt.pending?.objects !== undefined || evt.pending?.flows !== undefined
? { pending: { objects: evt.pending.objects ?? [], flows: evt.pending.flows ?? [] } }
: {}),
};
if (gate) await gate(ctx);
for (const extra of added) await extra(ctx);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/metadata-protocol/src/protocol-class-head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ export class ProtocolClassHeadFacet {
host.authoringGates.set(singular, gate);
}

/**
* The body of `ObjectStackProtocolImplementation#addAuthoringGate`; the method's docblock there is its contract.
* Handed the protocol's map of added gates itself (the facade holds no host member for it, to stay within its
* line ceiling): singular or plural type names both resolve, and each call appends one gate.
*/
static addAuthoringGate(added: Map<string, MetadataAuthoringGate[]>, type: string, gate: MetadataAuthoringGate): void {
const singular = PLURAL_TO_SINGULAR[type] ?? type;
const gates = added.get(singular);
if (gates) gates.push(gate);
else added.set(singular, [gate]);
}

/** The body of `ObjectStackProtocolImplementation#registerCredentialChannel`; the method's docblock there is its contract. */
static registerCredentialChannel(host: Pick<ProtocolHost, 'credentialChannels'>, type: string, channel: MetadataCredentialChannel): void {
const singular = PLURAL_TO_SINGULAR[type] ?? type;
Expand Down
21 changes: 20 additions & 1 deletion packages/metadata-protocol/src/protocol-history-and-publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ export class ProtocolHistoryAndPublishFacet {
* `recordPendingDenialAudit`, rethrow. Skip it and the refusal is silent
* again — the pre-#7748 state, not merely a worse one.
*/
static async promoteDraftForPublish(host: Pick<ProtocolHost, 'anonymousElevatedFlowRefusal' | 'applyRegistryWriteThrough' | 'expandStoredViewContainers' | 'readFlattenedMetaItems' | 'readActiveOverlayRows' | 'restoreArtifactRegistryView' | 'storedOverlayEntries' | 'assertRuntimeAuthoringRules' | 'classifyObjectContribution' | 'credentialChannelFor' | 'declinesStoredRow' | 'defaultRecordPageSaveRefusal' | 'engine' | 'ensureOverlayIndex' | 'expandRuntimeViewContainer' | 'externalBindingSaveRefusal' | 'getOverlayRepo' | 'isArtifactBacked' | 'isOverlayAllowed' | 'isRuntimeCreateAllowed' | 'isSealedManagedItem' | 'isWritablePackage' | 'lockWriteRefusal' | 'metadataConflictRefusal' | 'namedBaseOverlayWriteRefusal' | 'objectExtensionItemRefusal' | 'objectNamespaceObjectNameClash' | 'objectNamespacePrefixViolation' | 'objectRegistrationRefusal' | 'optimisticConflictAuditEntry' | 'overlayPackageMismatchError' | 'overrideWriteVerdict' | 'packagelessAddress' | 'packagelessDraftAddress' | 'permissionSetNameCollisionRefusal' | 'readItemActiveRows' | 'resolveSduiManifest' | 'rethrowUnlessMetadataStoreUnprovisioned' | 'seedExternalIdSaveRefusal' | 'shippedArtifactsOf' | 'storedParentForToken' | 'uninstalledNamedBaseRefusal' | 'unknownDatasourceSaveRefusal' | 'unknownGrantObjectSaveRefusal' | 'unknownPicklistBindingSaveRefusal'>, request: {
static async promoteDraftForPublish(host: Pick<ProtocolHost, 'anonymousElevatedFlowRefusal' | 'authoringChannel' | 'runAuthoringGate' | 'applyRegistryWriteThrough' | 'expandStoredViewContainers' | 'readFlattenedMetaItems' | 'readActiveOverlayRows' | 'restoreArtifactRegistryView' | 'storedOverlayEntries' | 'assertRuntimeAuthoringRules' | 'classifyObjectContribution' | 'credentialChannelFor' | 'declinesStoredRow' | 'defaultRecordPageSaveRefusal' | 'engine' | 'ensureOverlayIndex' | 'expandRuntimeViewContainer' | 'externalBindingSaveRefusal' | 'getOverlayRepo' | 'isArtifactBacked' | 'isOverlayAllowed' | 'isRuntimeCreateAllowed' | 'isSealedManagedItem' | 'isWritablePackage' | 'lockWriteRefusal' | 'metadataConflictRefusal' | 'namedBaseOverlayWriteRefusal' | 'objectExtensionItemRefusal' | 'objectNamespaceObjectNameClash' | 'objectNamespacePrefixViolation' | 'objectRegistrationRefusal' | 'optimisticConflictAuditEntry' | 'overlayPackageMismatchError' | 'overrideWriteVerdict' | 'packagelessAddress' | 'packagelessDraftAddress' | 'permissionSetNameCollisionRefusal' | 'readItemActiveRows' | 'resolveSduiManifest' | 'rethrowUnlessMetadataStoreUnprovisioned' | 'seedExternalIdSaveRefusal' | 'shippedArtifactsOf' | 'storedParentForToken' | 'uninstalledNamedBaseRefusal' | 'unknownDatasourceSaveRefusal' | 'unknownGrantObjectSaveRefusal' | 'unknownPicklistBindingSaveRefusal'>, request: {
type: string; name: string; actor?: string; message?: string;
/**
* [#8907] ADR-0048 — the package binding of the draft being promoted,
Expand Down Expand Up @@ -1459,6 +1459,25 @@ export class ProtocolHistoryAndPublishFacet {
})
: [];

// [ADR-0162 item 7, #37766] The gates a plugin added beside the type's
// owner gate (`addAuthoringGate`) judge the body as it goes `active`.
// The owner gate ran at the draft's save and is not run again; an added
// gate that judges only `active` bodies meets a draft's body here, on
// the save door's channel rule, and a judge-only call meets it too.
if (draftForGate && host.authoringChannel !== 'package-author') {
await host.runAuthoringGate({
type: singularType,
name: request.name,
state: 'active',
addedOnly: true,
body: storedFlowVerdictBody(singularType, draftForGate.body),
...(request.userTextLocales !== undefined ? { userTextLocales: request.userTextLocales } : {}),
...(request.packageId !== undefined ? { packageId: request.packageId } : {}),
// The batch's other drafts: an added gate judges the batch as it will be live.
...(request.pending !== undefined ? { pending: request.pending } : {}),
});
}

// [ADR-0146 D2] The override write gate's refusal, the last before the store.
if (overrideVerdict.verdict === 'refuse') throw overrideWriteRefusalError(overrideVerdict);
// [#32396] Every refusal above has had its turn; a judge-only call stops
Expand Down
Loading
Loading