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
13 changes: 13 additions & 0 deletions .changeset/38667-automation-engine-facets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@objectstack/service-automation': patch
'@objectstack/spec': patch
---

refactor(service-automation): `AutomationEngine` becomes a delegating facade over eight facet modules, unchanged

Clause-②: no
Metadata-protocol: none

Internal only: no behaviour changes and nothing is added to or removed from the package entry. The bodies of 145 `AutomationEngine` members move, byte for byte, into static methods of eight facet classes (dispatch, extensions, flow slots, registration, connector doors, enablement, run and resume); each member stays on the class with its signature (and, when public, its docblock) as a one-call stub, so the published declaration of `AutomationEngine`, an instance spy and a subclass override are what they were. The engine's state stays on the class, and a facet body reaches it through a host built once per engine.

- `@objectstack/spec`: the `flow` and `connector` liveness ledgers cite the facet modules that now hold the bodies they name (`engine-run-facet.ts#retryExecution` and the three retry-backoff keys; `engine-extensions-facet.ts#getConnectorDescriptors`); no verdict changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ a row or a number here — fix the code, or the census, and regenerate.

| Measure | Value |
|---|---:|
| Write call sites | 40 |
| Write call sites | 42 |
| Object name statically decidable | 34 |
| Object name chosen at run time | 6 |
| Object name chosen at run time | 8 |
| Object name spelled inline | 0 |
| Object name spelled through a `const` | 34 |
| Object name is an `object: string` parameter | 1 |
| Object name is an `object: string` parameter | 3 |
| Object name is some other run-time expression | 5 |
| Against a tenancy-enabled object | 34 |
| Against an object declaring tenancy off | 0 |
| Threading a tenant context | 40 |
| Provably carrying none (options read, no context key) | 0 |
| Provably carrying none (options read, no context key) | 2 |
| …and decidably tenancy-enabled | 0 |
| Options argument unreadable | 0 |
| …and decidably tenancy-enabled | 0 |
Expand All @@ -33,7 +33,7 @@ a row or a number here — fix the code, or the census, and regenerate.
| Threading a context of undecidable elevation | 9 |
| Receiver carried a readable engine type | 30 |
| Receiver erased, placed by the object NAME | 10 |
| Receiver erased, placed by an `object: string` PARAMETER | 0 |
| Receiver erased, placed by an `object: string` PARAMETER | 2 |
| Receiver erased, placed by an `UNTYPED_RECEIVERS` row | 0 |

## Subtractions the census could NOT defend — enforced
Expand All @@ -58,6 +58,7 @@ a row or a number here — fix the code, or the census, and regenerate.
| `packages/services/service-automation/src/connector-credentials.ts` | `insert` | `sys_connector_credential` | enabled | context, elevation undecidable | 1 |
| `packages/services/service-automation/src/connector-credentials.ts` | `update` | `sys_connector_credential` | enabled | context, elevation undecidable | 1 |
| `packages/services/service-automation/src/connector-credentials.ts` | `update` | `sys_connector_credential` | enabled | elevated | 3 |
| `packages/services/service-automation/src/engine-registration-facet.ts` | `delete` | `name` | undecidable | PROVABLY NONE | 2 |
| `packages/services/service-automation/src/flow-credential-channel.ts` | `delete` | `sys_flow_credential` | enabled | elevated | 5 |
| `packages/services/service-automation/src/flow-credential-channel.ts` | `insert` | `sys_flow_credential` | enabled | elevated | 1 |
| `packages/services/service-automation/src/flow-credential-channel.ts` | `update` | `sys_flow_credential` | enabled | elevated | 2 |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.

/**
* [#38667] The credential and connection doors: the flow credential source, connector pulls, connector credentials and connections, and flow credential holdings: the bodies of
* `setFlowCredentialSource`, `pullConnectorSource`,
* `requireConnectorCredentialDoor`, `attachConnectorCredentialPubSub`,
* `resyncConnectorCredentialPubSub`, `listConnectorCredentialRefs`,
* `setConnectorCredential`, `clearConnectorCredential`,
* `requireConnectorConnectionDoor`, `getConnectorConnection`,
* `beginConnectorConnection`, `completeConnectorConnection`,
* `revokeConnectorConnection`, `holdsFlowCredential`, `resolveFlowCredential`,
* `flowCredentialHoldings`
* moved out of `engine.ts` whole.
*
* A MOVE, not a redesign. {@link ConnectorDoorsFacet} is a stateless facet of
* `AutomationEngine`: each static method of it is the body of the
* `AutomationEngine` method of the same name exactly as it stood, not
* re-indented, not formatted, not edited, with a `this` parameter added when
* the body reads the engine. The method stays on the class as a one-call stub
* with its signature, so the published declaration, an instance spy and a
* subclass override are what they were. A public method keeps its docblock on
* the class, the contract; a private method's docblock moved here with its
* body. The state stays on `AutomationEngine`.
*
* The bodies reach the engine through {@link ConnectorDoorsHost}: exactly the
* `AutomationEngine` members they read, private ones included. Every stub
* hands its body, by a `.call`, the same host: `connectorDoorsHost` in
* `engine-facet-hosts.ts`, built once per engine by the class's static block.
* Methods go as closures that call them on the engine at call time, so a spy
* or a subclass override is still the one reached; fields go as getters, read
* when the body reads them, and a field a body assigns also goes with a setter
* that assigns it on the engine.
*
* The module imports `engine.ts` for types only, so the two modules form no
* evaluation cycle. `index.ts` re-exports nothing from this module.
*/

import type {
ConnectorSourcePullRequest,
ConnectorSourcePullResult,
ConnectorCredentialRef,
ConnectorCredentialSetRequest,
ConnectorCredentialClearRequest,
ConnectorConnectionState,
ConnectorConnectionRequest,
ConnectorConnectionBegin,
ConnectorConnectionCallbackRequest,
IPubSub,
} from '@objectstack/spec/contracts';
import { FlowCredentialUnresolvableError } from './flow-credential-channel.js';
import { flowCredentialHoldings } from './flow-definitions.js';
import { PackageScopedNameRefusedError } from '@objectstack/core';
import { isSlotKeySpelling } from './flow-slots.js';
import type {
FlowCredentialSource,
ConnectorCredentialDoor,
ConnectorConnectionDoor,
FlowCredentialHolding,
} from './engine-registration.js';
import type { AutomationEngine } from './engine.js';

/**
* The `AutomationEngine` members the bodies of {@link ConnectorDoorsFacet} read — every one
* of them, and no other. Each is typed AS the class member (an indexed access
* on the class), so a change to a member's signature on the class is a change
* here too. `readonly` members are the getters; the others are fields a body
* assigns.
*/
export interface ConnectorDoorsHost {
readonly connectorConnectionDoor: AutomationEngine['connectorConnectionDoor'];
readonly connectorCredentialDoor: AutomationEngine['connectorCredentialDoor'];
readonly connectorPullSource: AutomationEngine['connectorPullSource'];
flowCredentialSource: AutomationEngine['flowCredentialSource'];
readonly flowSlots: AutomationEngine['flowSlots'];
readonly flows: AutomationEngine['flows'];
readonly requireConnectorConnectionDoor: AutomationEngine['requireConnectorConnectionDoor'];
readonly requireConnectorCredentialDoor: AutomationEngine['requireConnectorCredentialDoor'];
readonly sharedFlowNameRefusal: AutomationEngine['sharedFlowNameRefusal'];
}

/** The bodies of the "The credential and connection doors" members of `AutomationEngine`: a stateless facet, called by the class's stubs. */
export class ConnectorDoorsFacet {
/** The body of `AutomationEngine#setFlowCredentialSource`; the method's docblock there is its contract. */
static setFlowCredentialSource(this: Pick<ConnectorDoorsHost, 'flowCredentialSource' | 'flowSlots' | 'sharedFlowNameRefusal'>, source: FlowCredentialSource | undefined): void {
// [ADR-0150 D8 row R9, #31696] Asked by slot key, answered by the
// flow's name and — for a name two packages hold — its package. A
// bare name two packages hold names neither flow, so it is refused
// (409, naming both), as at every bare door.
const at = (key: string): [string, string | undefined] => {
const slot = this.flowSlots.slot(key);
if (!slot) {
if (this.flowSlots.isShared(key)) {
throw new PackageScopedNameRefusedError(this.sharedFlowNameRefusal(key, null));
}
return [key, undefined];
}
if (!this.flowSlots.isShared(slot.name)) return [slot.name, undefined];
return [slot.name, slot.packageId ?? ''];
};
// [S3] A source that does not answer by package is never asked for a
// slot of a shared name: it fails closed, naming the slot.
const scoped = source?.readsByPackage === true;
const unscopedRefusal = (name: string, flowPackage: string) => {
const others = this.flowSlots.slotsOf(name)
.map((slot) => slot.packageId ?? '')
.filter((packageId) => packageId !== flowPackage);
return new FlowCredentialUnresolvableError(
`Flow '${name}' of package "${flowPackage}": its credentials cannot be read. `
+ `${others.map((p) => `Package "${p}"`).join(', ')} also ${others.length === 1 ? 'holds' : 'hold'} a flow `
+ `named '${name}', and the flow credential source attached to this engine reads by flow name alone, `
+ 'so it could answer another package\'s secret. Nothing was read. Attach a source that reads by '
+ 'package (FlowCredentialSource.readsByPackage).',
);
};
this.flowCredentialSource = source && {
readsByPackage: true,
holds: (key, nodeId, credentialKey) => {
const [name, flowPackage] = at(key);
if (flowPackage === undefined) return source.holds(name, nodeId, credentialKey);
// [S1] By name alone, a "no" means no package holds one, so an unsigned node runs.
return scoped ? source.holds(name, nodeId, credentialKey, flowPackage) : source.holds(name, nodeId, credentialKey);
},
held: (key) => {
const [name, flowPackage] = at(key);
if (flowPackage === undefined) return source.held(name);
if (!scoped) throw unscopedRefusal(name, flowPackage);
return source.held(name, flowPackage);
},
resolve: async (key, nodeId, credentialKey) => {
const [name, flowPackage] = at(key);
if (flowPackage === undefined) return source.resolve(name, nodeId, credentialKey);
if (!scoped) throw unscopedRefusal(name, flowPackage);
return source.resolve(name, nodeId, credentialKey, flowPackage);
},
};
}

/** The body of `AutomationEngine#pullConnectorSource`; the method's docblock there is its contract. */
static async pullConnectorSource(this: Pick<ConnectorDoorsHost, 'connectorPullSource'>, request: ConnectorSourcePullRequest): Promise<ConnectorSourcePullResult> {
if (!this.connectorPullSource) {
// ADR-0112: the code the executor itself answers for "cannot pull
// now" (a degraded connector), so a caller branches on one pair.
throw Object.assign(
new Error(
`[Automation] pullConnectorSource('${request.mapping}'): this automation engine has no connector sync `
+ 'executor attached — it is attached by AutomationServicePlugin at init(); a bare engine cannot pull. Nothing was pulled.',
),
{ code: 'SERVICE_UNAVAILABLE', status: 503 },
);
}
return this.connectorPullSource(request);
}

/** [#27237] The door, or the refusal a bare engine answers: nothing here can store a credential. */
static requireConnectorCredentialDoor(this: Pick<ConnectorDoorsHost, 'connectorCredentialDoor'>, verb: string): ConnectorCredentialDoor {
if (this.connectorCredentialDoor) return this.connectorCredentialDoor;
throw Object.assign(
new Error(
`[Automation] ${verb}: this automation engine has no connector credential door attached — it is attached by `
+ 'AutomationServicePlugin at init(); a bare engine has no declared connectors and no credential store.',
),
{ code: 'SERVICE_UNAVAILABLE', status: 503 },
);
}

/** The body of `AutomationEngine#attachConnectorCredentialPubSub`; the method's docblock there is its contract. */
static attachConnectorCredentialPubSub(this: Pick<ConnectorDoorsHost, 'requireConnectorCredentialDoor'>, pubsub: IPubSub, nodeId: string): () => void {
return this.requireConnectorCredentialDoor('attachConnectorCredentialPubSub()').attachClusterPubSub(pubsub, nodeId);
}

/** The body of `AutomationEngine#resyncConnectorCredentialPubSub`; the method's docblock there is its contract. */
static async resyncConnectorCredentialPubSub(this: Pick<ConnectorDoorsHost, 'requireConnectorCredentialDoor'>): Promise<void> {
await this.requireConnectorCredentialDoor('resyncConnectorCredentialPubSub()').resyncFromCluster?.();
}

/** The body of `AutomationEngine#listConnectorCredentialRefs`; the method's docblock there is its contract. */
static async listConnectorCredentialRefs(this: Pick<ConnectorDoorsHost, 'requireConnectorCredentialDoor'>): Promise<ConnectorCredentialRef[]> {
return this.requireConnectorCredentialDoor('listConnectorCredentialRefs()').list();
}

/** The body of `AutomationEngine#setConnectorCredential`; the method's docblock there is its contract. */
static async setConnectorCredential(this: Pick<ConnectorDoorsHost, 'requireConnectorCredentialDoor'>, request: ConnectorCredentialSetRequest): Promise<ConnectorCredentialRef> {
return this.requireConnectorCredentialDoor(`setConnectorCredential('${request.ref}')`).set(request);
}

/** The body of `AutomationEngine#clearConnectorCredential`; the method's docblock there is its contract. */
static async clearConnectorCredential(this: Pick<ConnectorDoorsHost, 'requireConnectorCredentialDoor'>, request: ConnectorCredentialClearRequest): Promise<ConnectorCredentialRef> {
return this.requireConnectorCredentialDoor(`clearConnectorCredential('${request.ref}')`).clear(request);
}

/** ADR-0143 D5 — the door, or the refusal a bare engine answers: nothing here can connect an instance. */
static requireConnectorConnectionDoor(this: Pick<ConnectorDoorsHost, 'connectorConnectionDoor'>, verb: string): ConnectorConnectionDoor {
if (this.connectorConnectionDoor) return this.connectorConnectionDoor;
throw Object.assign(
new Error(
`[Automation] ${verb}: this automation engine has no connector connection door attached — it is attached by `
+ 'AutomationServicePlugin at init(); a bare engine has no declared connectors and no connection store.',
),
{ code: 'SERVICE_UNAVAILABLE', status: 503 },
);
}

/** The body of `AutomationEngine#getConnectorConnection`; the method's docblock there is its contract. */
static async getConnectorConnection(this: Pick<ConnectorDoorsHost, 'requireConnectorConnectionDoor'>, request: ConnectorConnectionRequest): Promise<ConnectorConnectionState> {
return this.requireConnectorConnectionDoor(`getConnectorConnection('${request.connector}')`).get(request);
}

/** The body of `AutomationEngine#beginConnectorConnection`; the method's docblock there is its contract. */
static async beginConnectorConnection(this: Pick<ConnectorDoorsHost, 'requireConnectorConnectionDoor'>, request: ConnectorConnectionRequest): Promise<ConnectorConnectionBegin> {
return this.requireConnectorConnectionDoor(`beginConnectorConnection('${request.connector}')`).begin(request);
}

/** The body of `AutomationEngine#completeConnectorConnection`; the method's docblock there is its contract. */
static async completeConnectorConnection(this: Pick<ConnectorDoorsHost, 'requireConnectorConnectionDoor'>, request: ConnectorConnectionCallbackRequest): Promise<ConnectorConnectionState> {
return this.requireConnectorConnectionDoor('completeConnectorConnection()').complete(request);
}

/** The body of `AutomationEngine#revokeConnectorConnection`; the method's docblock there is its contract. */
static async revokeConnectorConnection(this: Pick<ConnectorDoorsHost, 'requireConnectorConnectionDoor'>, request: ConnectorConnectionRequest): Promise<ConnectorConnectionState> {
return this.requireConnectorConnectionDoor(`revokeConnectorConnection('${request.connector}')`).revoke(request);
}

/** The body of `AutomationEngine#holdsFlowCredential`; the method's docblock there is its contract. */
static holdsFlowCredential(this: Pick<ConnectorDoorsHost, 'flowCredentialSource'>, flowName: string, nodeId: string, key: string): boolean {
return this.flowCredentialSource?.holds(flowName, nodeId, key) ?? false;
}

/** The body of `AutomationEngine#resolveFlowCredential`; the method's docblock there is its contract. */
static async resolveFlowCredential(this: Pick<ConnectorDoorsHost, 'flowCredentialSource'>, flowName: string, nodeId: string, key: string): Promise<string | undefined> {
if (!this.flowCredentialSource) return undefined;
return this.flowCredentialSource.resolve(flowName, nodeId, key);
}

/** The body of `AutomationEngine#flowCredentialHoldings`; the method's docblock there is its contract. */
static flowCredentialHoldings(this: Pick<ConnectorDoorsHost, 'flowCredentialSource' | 'flows'>, name: string): FlowCredentialHolding[] {
// [#31696] By name: a slot key is no flow's name, and a name two
// packages hold names neither flow (no slot is filed under it).
if (isSlotKeySpelling(name)) return [];
// The body lives in ./flow-definitions.ts (#25222).
const readFlowCredentialSource = () => this.flowCredentialSource;
const host: ThisParameterType<typeof flowCredentialHoldings> = {
flows: this.flows,
get flowCredentialSource() { return readFlowCredentialSource(); },
};
return flowCredentialHoldings.call(host, name);
}
}
Loading
Loading