Skip to content
Merged
30 changes: 30 additions & 0 deletions .changeset/33386-flow-doors-served-holders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
'@objectstack/runtime': patch
'@objectstack/service-automation': patch
---

fix(runtime,service-automation)!: the flow doors and the connector address judge the served holders of a name two packages hold, so a disabled holder's slot answers no bare name (ADR-0150 D3, #33386)

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

<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export changes shape. The moved answers are door and run reads under a reversible package state: no stored row, slot key, run key or dispatch key changes, so there is nothing for a migration to rewrite and no tombstone. -->

**BREAKING** for a caller of a flow or connector name that two packages hold while one of them is disabled. A disabled package holds no name (ADR-0150 D3). Until now the `/automation` by-name doors, the flow action and endpoint, the start admission's record-read walk and the engine's bare entries still counted a disabled holder for such a name. They now count only the packages that serve it. With no holder disabled, every answer is unchanged. Measured over a real `SchemaRegistry` (its `disablePackage`), in both registration orders. Packages CRM and HR each hold the flow `convert` and the connector `mail`, one engine slot or instance each, and CRM is disabled:

- A bare name with no package (`POST /automation/convert/trigger`, `GET /automation/convert`, the run doors, `GET /automation/connectors/mail/connection`, the engine's `execute`, `getFlow` and `toggleFlow`) was refused 409 naming CRM and HR. It is now served from HR's slot or instance.
- A flow action, a subflow call or a walk read from a package whose closure holds both was refused 409 naming both. It now reaches HR's flow (widening).
- CRM's own flow action, or a subflow call from CRM's own flow, ran CRM's flow. It now gets no flow (not found), and never HR's.
- A package that reaches the name only through CRM ran CRM's flow. It is now refused 409 `outside-closure` naming HR. A package that reaches neither holder is refused naming HR only (before: both).
- An address naming CRM (`/automation/crm/convert/…`, `/automation/trigger/crm/convert`) ran CRM's slot. It is now refused 409 `not-held-by-qualifier` naming HR. `/automation/connectors/crm/mail/connection` reached the service, which answered 404; it is now refused 409 naming HR, and the service is not asked.
- `_env` or another namespace (`/automation/_env/convert/…`, `/automation/connectors/ops/mail/connection`) was refused 409 naming both. It is now refused naming HR only.
- The definition writes (`PUT`, `DELETE`, clone) at an address naming CRM, at `_env` or at a third namespace were refused 409 naming both. They are now refused 409 naming HR only. With both disabled, such a write at an address answers 404.
- With CRM and HR both disabled, no door and no run reaches a flow. An address answers 404. The bare name and `_env` answer as for a flow the engine does not hold.

**Not moved.** HR's own address and HR's own callers answer as before. The durable coordinates stay judged over every holder, served or not: the engine's slot keys, a run's key, the schedule dispatch key (`flowDispatchHolder`), the credential channel's scoping, the subflow guards' arming verdicts, and which slot a run row is filed under. A disable is reversible, so it re-points no slot. A run filed under CRM's slot key is no served holder's: the run doors answer it as a run nobody holds, and it is never read or acted on as HR's. A definition write (`PUT`, `DELETE`, clone) at HR's own address keeps its 409 naming every holder; a bare definition write is refused by the metadata store over its stored rows, as before. A name only one package holds is judged as before.

**Deferred:** amendment 3's trigger binding (a runtime-disabled holder's trigger still fires its slot, as on main) → #33748.

**No migration.** No schema key, stored row, slot key, run key or dispatch key changes, so there is nothing to rewrite: the answers that move follow a reversible package state.

The fix for a caller: re-enable the package, or address the served holder by its namespace.
66 changes: 59 additions & 7 deletions packages/runtime/src/domains/automation-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@
* {@link bareNameRefusal} is the same resolution for a bare address: a bare
* name the resolver refuses is answered `409` naming every candidate (D5), on
* the doors whose own answer was no such `409`.
*
* [ADR-0150 D3, #33386; seat ruling 6884403301] A name two or more packages
* hold, one of them disabled, is judged over the SERVED holders at the doors
* (a disabled package holds no name), the shape the email send took
* (plugin-email `template-loader.ts`):
*
* - a bare name ({@link bareNameAdmission}): one served holder is that
* holder's slot, two or more are refused naming the served holders only,
* and none is no item (the door's own `404`). A slot is `(package, name)`
* and is never re-pointed: a disable only moves which slot the bare name
* reaches;
* - an address naming the disabled holder's namespace is never another
* package's item: `not-held-by-qualifier` naming the served holders, or
* `404` when none remains; `_env` and any other namespace name the served
* holders only.
*
* The durable coordinates stay judged over EVERY holder (amendment 7): the
* engine's slot keys, a run's key, and {@link bareNameRefusal}, which the MCP
* resume door reads to tell a run row's slot. So does the refusal a slot
* carries for the doors that cannot serve from the slot (the definition
* writes): a write targets the name every holder holds.
*/

import {
Expand Down Expand Up @@ -259,6 +280,33 @@ export function bareNameRefusal(
return resolution.status === 'refused' ? resolution : undefined;
}

/**
* [ADR-0150 D3, #33386] A BARE by-name door's admission (see the module note):
* the bare name's `409` naming the served holders when two or more serve it,
* the slot of its one served holder when the name is held by two or more
* packages over every holder (each holder's item is then its own slot), and
* `undefined` otherwise — the bare path, as before, which answers its own
* `404` when no served holder remains. A name held by one package or none is
* judged as {@link bareNameRefusal} judges it, unchanged. Read-only.
*/
export function bareNameAdmission(
registry: AutomationAddressRegistry | undefined,
type: 'flow' | 'connector',
name: string,
): AutomationAddressRefusal | AutomationAddressSlot | undefined {
if (!registry || !canResolve(registry)) return undefined;
const every = resolvePackageScopedName(registry, { type, name, callerPackageId: null, includeDisabledPackages: true });
if (every.status !== 'refused' || holdersOf(registry, type, name, { includeDisabledPackages: true }).length < 2) {
return every.status === 'refused' ? { status: 409, refusal: every } : undefined;
}
const served = resolvePackageScopedName(registry, { type, name, callerPackageId: null });
if (served.status === 'refused') return { status: 409, refusal: served };
if (served.status === 'resolved' && served.holder.kind === 'package') {
return { status: 'slot', packageId: served.holder.packageId, refusal: every };
}
return undefined;
}

/**
* Judge an addressed path's qualifier (see the module note). `undefined`
* admits it: the path is then served as its bare path, whose item is the
Expand All @@ -279,14 +327,16 @@ export function admitAutomationAddress(
? undefined
: { status: 404, message: `${noun} '${name}' not found in namespace '${qualifier.namespace}'` };
}
// [#32183, R4-G] A name two or more packages hold over every holder (the
// state #31696 opened for a flow, each holder's flow in its own engine
// slot) is judged over every holder, a disabled package's included, so a
// disable moves no address of it (`main`'s answer). A one-holder name is
// judged over the served holders, as every by-name door is.
// [ADR-0150 D3, #33386] Judged over the served holders, as every by-name
// door is: a disabled package holds no name, so an address naming it is
// never another package's item, and a refusal names the served holders
// only (seat ruling 6884403301, amendments 2 and 6). A name two or more
// packages hold over EVERY holder (the state #31696 opened, each holder's
// item in its own engine slot) is still served from the addressed
// holder's slot: the slots are keyed over every holder (amendment 7).
const everyHolder = holdersOf(registry, type, name, { includeDisabledPackages: true });
const shared = everyHolder.length > 1;
const holders = shared ? everyHolder : holdersOf(registry, type, name);
const holders = holdersOf(registry, type, name);
let holder: PackageScopedHolder;
if (qualifier.kind === 'environment') {
if (registry.getEnvironmentScopedItem(type, name) !== undefined || holders.length === 0) return undefined;
Expand All @@ -313,7 +363,9 @@ export function admitAutomationAddress(
// [#31696] A flow name two packages hold: each holder's flow is its own
// slot, and the address names one holder of it. [ADR-0150 D9, #31699]
// So is a connector name: each holder's instance is its own, and the
// connection door connects the addressed one.
// connection door connects the addressed one. [#33386] The addressed
// holder is a served one (above); the slot's refusal, for the doors
// that cannot serve from it, stays the name's over every holder.
return (type === 'flow' || type === 'connector') && holder.kind === 'package'
? { status: 'slot', packageId: holder.packageId, refusal: bare }
: { status: 409, refusal: bare };
Expand Down
144 changes: 144 additions & 0 deletions packages/runtime/src/domains/automation-addressed-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { describe, it, expect, vi } from 'vitest';
import '@objectstack/metadata';

import { HttpDispatcher } from '../http-dispatcher.js';
import { SchemaRegistry } from '@objectstack/objectql';

const CRM = 'com.crm.pkg';
const HR = 'com.hr.pkg';
Expand Down Expand Up @@ -492,3 +493,146 @@ describe('/automation/connectors/:ns/:name/connection — a connector instance b
expect(automation.getConnectorConnection).not.toHaveBeenCalled();
});
});

/**
* [ADR-0150 D3, #33386; seat rulings 6884403301 and Q2 6888807772] A name two
* packages hold, one of them disabled, through the real dispatcher over a real
* `SchemaRegistry` (its `disablePackage`, its served filter), in both
* registration orders. The doors judge the SERVED holders: the bare name is the
* one served holder's slot or instance; an address naming the disabled holder
* is refused naming the served holder only; `_env` and a third namespace name
* the served holder only; with both disabled, nothing is reached.
*/
describe('[#33386] the by-name doors judge the served holders of a name two packages hold', () => {
const OPS = 'com.ops.pkg';
const ORDERS = [[CRM, HR], [HR, CRM]];

function registryOf(order: readonly string[], disabled: readonly string[]) {
const registry = new SchemaRegistry();
registry.logLevel = 'silent';
for (const [id, namespace] of [[CRM, 'crm'], [HR, 'hr'], [OPS, 'ops']]) {
registry.installPackage({ id, name: id, version: '1.0.0', type: 'app', namespace } as never, 'code');
}
for (const holder of order) {
registry.registerItem('flow', { name: 'convert', _packageId: holder }, 'name' as never, holder);
registry.registerItem('connector', { name: 'mail', _packageId: holder }, 'name' as never, holder);
}
for (const id of disabled) registry.disablePackage(id);
return registry;
}

function dispatcherOver(registry: SchemaRegistry) {
const slotOf = (pkg: string | null, name: string) => (name === 'convert' && pkg !== null && [CRM, HR].includes(pkg) ? `${pkg}/convert` : undefined);
const automation = {
handlerReady: true,
getFlow: vi.fn(async () => null),
execute: vi.fn(async (name: string) => ({ success: false, error: `Flow '${name}' not found` })),
toggleFlow: vi.fn(async () => {}),
listRunsPage: vi.fn(async () => ({ runs: [] as unknown[], hasMore: false })),
getConnectorConnection: vi.fn(async (request: { connector: string }) => ({ connector: request.connector, status: 'not_connected' })),
getActionDescriptors: vi.fn(() => []),
getFlowRuntimeStates: vi.fn(() => []),
flowSlotKey: vi.fn(slotOf),
getFlowSlot: vi.fn((pkg: string | null, name: string) => (slotOf(pkg, name) ? { name, label: `${pkg} flow` } : null)),
executeSlot: vi.fn(async (pkg: string | null) => ({ success: true, output: { ran: pkg } })),
toggleFlowSlot: vi.fn(async () => {}),
};
const services: Record<string, unknown> = { automation, objectql: { registry } };
const resolve = (name: string) => services[name];
const kernel: any = { getService: resolve, getServiceAsync: async (name: string) => resolve(name), context: { getService: resolve } };
const dispatcher = new HttpDispatcher(kernel);
const call = async (path: string, method: string, body: unknown = {}) => {
const answered = await dispatcher.handleAutomation(path, method, body, { request: {}, executionContext: ADMIN } as any, {});
return { status: answered.response?.status ?? 200, body: answered.response?.body as any };
};
return { call, automation };
}

const packagesOf = (a: { body?: any }) => (candidatesOf(a) ?? []).map((c: { packageId?: string }) => c.packageId);

describe.each(ORDERS.map((order) => [order.join(' then '), order] as const))('registered %s', (_o, order) => {
it('CONTROL — nothing disabled: the bare doors refuse naming both; each address is its own slot or instance', async () => {
const { call, automation } = dispatcherOver(registryOf(order, []));
const bare = await call('/convert/trigger', 'POST');
expect([bare.status, packagesOf(bare)]).toEqual([409, [...order]]);
const mail = await call('/connectors/mail/connection', 'GET');
expect([mail.status, packagesOf(mail)]).toEqual([409, [...order]]);
expect((await call('/crm/convert/trigger', 'POST')).status).toBe(200);
expect(automation.executeSlot.mock.calls.map((c) => c[0])).toEqual([CRM]);
expect((await call('/connectors/hr/mail/connection', 'GET')).status).toBe(200);
expect(automation.getConnectorConnection.mock.calls.at(-1)?.[0]).toMatchObject({ connector: 'mail', package: HR });
});

it('CRM disabled — flow: the bare door runs HR\'s slot (FROM 409 naming both); CRM\'s address is refused naming HR (FROM CRM\'s slot ran); HR\'s address is HR\'s slot', async () => {
const { call, automation } = dispatcherOver(registryOf(order, [CRM]));
expect((await call('/convert/trigger', 'POST')).status).toBe(200);
expect(automation.executeSlot.mock.calls.map((c) => c[0])).toEqual([HR]);
const off = await call('/crm/convert/trigger', 'POST');
expect([off.status, codeOf(off), packagesOf(off)]).toEqual([409, 'RESOURCE_CONFLICT', [HR]]);
expect((await call('/hr/convert/trigger', 'POST')).status).toBe(200);
for (const path of ['/_env/convert/trigger', '/ops/convert/trigger']) {
const other = await call(path, 'POST');
expect([path, other.status, packagesOf(other)]).toEqual([path, 409, [HR]]);
}
expect(automation.executeSlot.mock.calls.map((c) => c[0])).toEqual([HR, HR]);
expect(automation.execute).not.toHaveBeenCalled();
});

it('CRM disabled — connector (Q2 rows): the address naming CRM is refused naming HR (FROM 404 from the service); HR\'s address and the bare name connect HR\'s instance (bare FROM 409 naming both); `_env` and a third namespace name HR only (FROM naming both)', async () => {
const { call, automation } = dispatcherOver(registryOf(order, [CRM]));
const off = await call('/connectors/crm/mail/connection', 'GET');
expect([off.status, codeOf(off), packagesOf(off)]).toEqual([409, 'RESOURCE_CONFLICT', [HR]]);
expect(automation.getConnectorConnection).not.toHaveBeenCalled();
for (const path of ['/connectors/hr/mail/connection', '/connectors/mail/connection']) {
expect((await call(path, 'GET')).status, path).toBe(200);
expect(automation.getConnectorConnection.mock.calls.at(-1)?.[0], path).toMatchObject({ connector: 'mail', package: HR });
}
for (const path of ['/connectors/_env/mail/connection', '/connectors/ops/mail/connection']) {
const other = await call(path, 'GET');
expect([path, other.status, packagesOf(other)]).toEqual([path, 409, [HR]]);
}
expect(automation.getConnectorConnection).toHaveBeenCalledTimes(2);
});

it('the definition writes (PUT, DELETE, clone) under a disable: an address naming CRM, `_env` or a third namespace is refused naming HR only (FROM: naming both); HR\'s address still names both; both disabled, an address answers 404 (FROM: 409)', async () => {
const writes = (prefix: string) => [
[`${prefix}/convert`, 'PUT', { name: 'convert', type: 'autolaunched' }],
[`${prefix}/convert`, 'DELETE', {}],
[`${prefix}/convert/clone`, 'POST', { name: 'convert_copy' }],
] as const;
const oneOff = dispatcherOver(registryOf(order, [CRM]));
for (const prefix of ['/crm', '/_env', '/ops']) {
for (const [path, method, body] of writes(prefix)) {
const answer = await oneOff.call(path, method, body);
expect([path, method, answer.status, codeOf(answer), packagesOf(answer)]).toEqual([path, method, 409, 'RESOURCE_CONFLICT', [HR]]);
}
}
// Not moved: HR's own address keeps the 409 naming every holder.
for (const prefix of ['/hr']) {
for (const [path, method, body] of writes(prefix)) {
const answer = await oneOff.call(path, method, body);
expect([path, method, answer.status, packagesOf(answer).sort()]).toEqual([path, method, 409, [CRM, HR].sort()]);
}
}
const bothOff = dispatcherOver(registryOf(order, [CRM, HR]));
for (const prefix of ['/crm', '/hr']) {
for (const [path, method, body] of writes(prefix)) {
expect([path, method, (await bothOff.call(path, method, body)).status]).toEqual([path, method, 404]);
}
}
for (const h of [oneOff, bothOff]) expect(h.automation.executeSlot).not.toHaveBeenCalled();
});

it('both disabled: no address reaches a flow or an instance (404); the bare door and `_env` reach no slot, and the service answers them with no package', async () => {
const { call, automation } = dispatcherOver(registryOf(order, [CRM, HR]));
for (const path of ['/crm/convert/trigger', '/hr/convert/trigger']) expect((await call(path, 'POST')).status, path).toBe(404);
for (const path of ['/connectors/crm/mail/connection', '/connectors/hr/mail/connection']) expect((await call(path, 'GET')).status, path).toBe(404);
expect((await call('/convert/trigger', 'POST')).status).toBe(404);
expect(automation.executeSlot).not.toHaveBeenCalled();
for (const path of ['/connectors/mail/connection', '/connectors/_env/mail/connection']) {
await call(path, 'GET');
expect(automation.getConnectorConnection.mock.calls.at(-1)?.[0], path).not.toHaveProperty('package');
}
});
});
});
Loading
Loading