diff --git a/.changeset/33748-trigger-disabled-holder.md b/.changeset/33748-trigger-disabled-holder.md new file mode 100644 index 00000000000..ef771a1d1ff --- /dev/null +++ b/.changeset/33748-trigger-disabled-holder.md @@ -0,0 +1,20 @@ +--- +'@objectstack/service-automation': patch +--- + +fix(service-automation)!: a trigger binding does not dispatch a slot whose package is disabled (ADR-0150 D3) + +Clause-②: no (narrowing) +Metadata-protocol: none + + + +**BREAKING** for a deployment where two packages each ship a flow of the same name and one of them is disabled at runtime: that package's flow no longer runs on its trigger (record-change, schedule, time-relative, or an api inbound hook's queued post). Before this release it kept running. + +When two packages each ship a flow of the same name, the engine holds one slot per package and arms each slot's trigger. Until now a slot whose package was disabled at runtime kept running on its trigger, because the run a trigger fires executed the armed slot without asking whether its package is still served. + +Now the engine asks when the trigger fires. While the slot's package is disabled, the run is skipped: nothing runs, no other package's flow of the name runs in its place, and no run-history row is written. The skip is logged once at `info` per slot while the package stays disabled. When the package is enabled again, the slot runs on its trigger again, with no re-arm. + +An event that fires while the package is disabled is dropped, not replayed when the package is enabled again: a one-shot schedule or time-relative fire that falls inside the disable is lost, as it is for a switched-off flow. + +Nothing durable moves: slot keys, run keys, the trigger bindings and the dispatch holder stay judged over every holder, so there is nothing to migrate. A flow name only one package ships answers as before. diff --git a/packages/services/service-automation/src/engine.ts b/packages/services/service-automation/src/engine.ts index f1d40265eb1..a1b58d9187b 100644 --- a/packages/services/service-automation/src/engine.ts +++ b/packages/services/service-automation/src/engine.ts @@ -2942,6 +2942,13 @@ export class AutomationEngine implements IAutomationService { private readonly flowSlots = new FlowSlotTable((name) => this.durableFlowHolder(name)?.status === 'refused'); /** [#31696] Slot keys whose trigger decline ({@link activateFlowTrigger}) has been said. */ private readonly sharedSlotTriggerWarned = new Set(); + /** + * [ADR-0150 D3, #33748] The slots (`PACKAGE/NAME`) whose trigger fired + * while their package was disabled, and whose skipped dispatch has been + * said ({@link triggerRun}). A slot leaves the set when it dispatches + * again, so the next disable is said again. + */ + private readonly disabledHolderDispatchSaid = new Set(); /** * [#31696] Where each key a slot change moved off went, for a run still * in flight under its start key ({@link flowKeyNow}). A key moved onto @@ -4413,6 +4420,13 @@ export class AutomationEngine implements IAutomationService { * released (an event the trigger took before the release landed) fires * nothing (`undefined`): the binding under its new key fires it once. A * slot of an unshared name runs through {@link execute}, as before. + * + * [ADR-0150 D3, #33748; seat ruling 6884403301, amendment 3] A slot of a + * name two packages hold whose package is disabled is not dispatched: no + * flow, no run, never another slot's. Asked when the trigger fires, not + * when it was armed: a runtime disable leaves the slot armed (a disable is + * reversible and moves no key, amendment 7), so the slot dispatches again + * the moment its package is enabled, with no re-arm. */ private triggerRun(armedKey: string): (ctx: AutomationContext) => Promise { const slot = this.flowSlots.slot(armedKey); @@ -4424,6 +4438,26 @@ export class AutomationEngine implements IAutomationService { return undefined; } if (!slot || !this.flowSlots.isShared(slot.name)) return this.execute(key, ctx); + if (slot.packageId !== null) { + const said = qualifiedFlowKey(slot.packageId, slot.name); + if (this.isDisabledHolderSlot(slot.name, slot.packageId)) { + // Said once per slot while its package stays disabled, at + // `info`: the skip IS the package switch working, and + // nothing that claims to be persisted is lost. + if (!this.disabledHolderDispatchSaid.has(said)) { + this.disabledHolderDispatchSaid.add(said); + this.logger.info( + `Trigger fired flow '${slot.name}' of package '${slot.packageId}', which is disabled — ` + + 'the flow was not run, no other package\'s flow of the name was run in its place, ' + + 'and no run-history row records it. It runs on its trigger again once the package ' + + 'is enabled.', + { flow: slot.name, package: slot.packageId, slot: key }, + ); + } + return undefined; + } + this.disabledHolderDispatchSaid.delete(said); + } // A trigger seeds `params.flowName` with the key it was armed // under; the run reads its flow's name there, never a slot key. const params = ctx.params as Record | undefined; @@ -5288,6 +5322,25 @@ export class AutomationEngine implements IAutomationService { return 'key' in resolved ? resolved.key : undefined; } + /** + * [ADR-0150 D3, #33748; seat ruling 6884403301, amendment 3] Is the slot + * `(packageId, name)` held by a package that is disabled now? Asked of + * the one resolver ({@link flowNameResolver}), with that package as the + * caller: its own holding answers on the served read, and the run's read + * (`servedCaller`) answers `not-found` for it only while the package is + * disabled (amendment 4's caller-first rule). Both readings are needed: + * `not-found` alone would also cover a slot whose package the registry + * does not hold at all, which this question does not judge. No resolver + * attached: nothing is disabled here. + */ + private isDisabledHolderSlot(name: string, packageId: string): boolean { + const resolver = this.flowNameResolver; + if (resolver === undefined) return false; + const held = resolver(name, packageId); + if (held?.status !== 'resolved' || held.via !== 'own') return false; + return resolver(name, packageId, { servedCaller: true })?.status === 'not-found'; + } + /** [#31696] The package of the slot `key` files, or `undefined` (a package-less slot, or none). */ private flowSlotPackage(key: string): string | undefined { return this.flowSlots.slot(key)?.packageId ?? undefined; diff --git a/packages/services/service-automation/src/flow-slots-per-package.test.ts b/packages/services/service-automation/src/flow-slots-per-package.test.ts index 6a4e7fd9b3f..5d24798de88 100644 --- a/packages/services/service-automation/src/flow-slots-per-package.test.ts +++ b/packages/services/service-automation/src/flow-slots-per-package.test.ts @@ -1069,3 +1069,129 @@ describe('[#33386] a run of a name two slots hold judges the served holders', () expect(answers[goOf(OPS)]).toMatchObject({ success: false, ran: [] }); }); }); + +/** + * [ADR-0150 D3, #33748; seat ruling 6884403301, amendment 3] A trigger + * binding of a slot whose package is disabled does not dispatch it: no run, + * never another package's flow in its place. Judged when the trigger fires, + * so a runtime disable (both slots stay armed) stops the slot and a re-enable + * dispatches it again with no re-arm. A boot disable arms nothing of the + * disabled package; the re-sync that arms it on enable makes it dispatch. + * The slot keys, the bindings and the dispatch holder do not move + * (amendment 7). A name one package holds: its trigger answers as on `main` + * today (CONTROL). + */ +describe('[#33748] a trigger binding does not dispatch a slot whose package is disabled', () => { + const tagOf = (packageId: string) => packageId.split('.').pop()!; + let armed: Map Promise>; + let started: string[]; + + beforeEach(() => { + armed = new Map(); + started = []; + engine.registerTrigger({ + type: 'record_change', + start: (b: { flowName: string }, callback: (c: unknown) => Promise) => { + started.push(b.flowName); + armed.set(b.flowName, callback); + }, + stop: (key: string) => { armed.delete(key); }, + } as unknown as FlowTrigger); + }); + + /** Fire every armed binding once, as a matching write would; the tags of what ran, sorted. */ + async function fireAll(): Promise { + ran = []; + for (const [key, callback] of armed) await callback({ params: { flowName: key } }); + return [...ran].sort(); + } + + function heldOnly(packageId: string, body: { name: string }): void { + registry.registerItem('flow', { ...body, _packageId: packageId }, 'name' as never, packageId); + loaderSets[body.name] = [...new Set([...(loaderSets[body.name] ?? []), packageId])]; + } + + const skipSaid = () => engineLogger.info.mock.calls.filter(([message]: [string]) => /which is disabled — the flow was not run/.test(message)); + + describe.each([[P1, P2], [P2, P1]].map((order) => [order.map(tagOf).join(' then '), order] as const))('registered %s', (_o, order) => { + it.each([[P1, P2], [P2, P1]])('runtime disable, %s disabled: its slot is not dispatched, the other is; re-enabled, it dispatches again with no re-arm', async (off, on) => { + for (const holder of order) ship(holder, triggered('t', tagOf(holder))); + const bindings = engine.getActiveTriggerBindings(); + const startedBefore = [...started]; + expect([...armed.keys()].sort()).toEqual([`${P1}/t`, `${P2}/t`]); + expect(await fireAll()).toEqual([tagOf(P1), tagOf(P2)].sort()); + + registry.disablePackage(off); + expect(await fireAll()).toEqual([tagOf(on)]); + expect(await fireAll()).toEqual([tagOf(on)]); + expect(skipSaid()).toHaveLength(1); + expect(skipSaid()[0][1]).toMatchObject({ flow: 't', package: off, slot: `${off}/t` }); + expect(engineLogger.error).not.toHaveBeenCalled(); + // Amendment 7: nothing durable moved, nothing was re-armed or released. + expect(engine.flowSlotKey(off, 't')).toBe(`${off}/t`); + expect(engine.flowSlotKey(on, 't')).toBe(`${on}/t`); + expect(engine.flowDispatchHolder('t')).toMatchObject({ claimBare: false }); + expect(engine.getActiveTriggerBindings()).toEqual(bindings); + + registry.enablePackage(off); + expect(await fireAll()).toEqual([tagOf(P1), tagOf(P2)].sort()); + expect(started).toEqual(startedBefore); + // A later disable is said again. + registry.disablePackage(off); + expect(await fireAll()).toEqual([tagOf(on)]); + expect(skipSaid()).toHaveLength(2); + }); + + it('runtime disable, both disabled: no slot is dispatched; each is said once', async () => { + for (const holder of order) ship(holder, triggered('t', tagOf(holder))); + registry.disablePackage(P1); + registry.disablePackage(P2); + expect(await fireAll()).toEqual([]); + expect(await fireAll()).toEqual([]); + expect(skipSaid().map(([, meta]: [string, { package: string }]) => meta.package).sort()).toEqual([P1, P2]); + expect(engine.flowSlotKey(P1, 't')).toBe(`${P1}/t`); + expect(engine.flowSlotKey(P2, 't')).toBe(`${P2}/t`); + }); + + it.each([[P1, P2], [P2, P1]])('boot disable, %s disabled (its flows not armed): it is not dispatched; enabled and re-synced, it is', async (off, on) => { + for (const holder of order) (holder === off ? heldOnly : ship)(holder, triggered('t', tagOf(holder))); + registry.disablePackage(off); + expect([...armed.keys()].some((key) => key.startsWith(`${off}/`))).toBe(false); + expect(await fireAll()).not.toContain(tagOf(off)); + + // The enable's re-sync registers the package's flow (ADR-0150 D4: one slot per holder). + registry.enablePackage(off); + ship(off, triggered('t', tagOf(off))); + expect([...armed.keys()].sort()).toEqual([`${P1}/t`, `${P2}/t`]); + expect(await fireAll()).toEqual([tagOf(off), tagOf(on)].sort()); + }); + }); + + it('CONTROL — a name two packages hold, nothing disabled: both slots dispatch, nothing is said', async () => { + ship(P1, triggered('t', 'alpha')); + ship(P2, triggered('t', 'beta')); + expect(await fireAll()).toEqual(['alpha', 'beta']); + expect(skipSaid()).toEqual([]); + }); + + it('CONTROL — two slots the registry no longer holds the name for (nothing disabled): both dispatch, as on `main`', async () => { + ship(P1, triggered('t', 'alpha')); + ship(P2, triggered('t', 'beta')); + // The resolver answers `not-found` for either package now; that is not a disable. + registry.unregisterItem('flow', 't'); + registry.unregisterItem('flow', 't'); + expect(flowNameResolver(ctx)('t', P1)).toEqual({ status: 'not-found' }); + expect(flowNameResolver(ctx)('t', P2)).toEqual({ status: 'not-found' }); + expect(await fireAll()).toEqual(['alpha', 'beta']); + expect(skipSaid()).toEqual([]); + }); + + it('CONTROL — a name one package holds: its trigger answers as on `main` today, disabled or not', async () => { + ship(P1, triggered('solo', 'alpha')); + expect([...armed.keys()]).toEqual(['solo']); + expect(await fireAll()).toEqual(['alpha']); + registry.disablePackage(P1); + expect(await fireAll()).toEqual(['alpha']); + expect(skipSaid()).toEqual([]); + }); +});