diff --git a/.changeset/26506-toggle-flow-caller.md b/.changeset/26506-toggle-flow-caller.md new file mode 100644 index 00000000000..1bb8455c7e0 --- /dev/null +++ b/.changeset/26506-toggle-flow-caller.md @@ -0,0 +1,12 @@ +--- +'@objectstack/spec': patch +--- + +feat(spec): `IAutomationService.toggleFlow` takes an optional caller, so switching a packaged flow on or off can name the admin who did it + +Clause-②: yes (widening) +Metadata-protocol: none + +`toggleFlow?(name, enabled)` gains an optional third parameter, `caller?: Pick`. A door that serves a person passes that person's id, so the `sys_metadata_activation` row the switch writes, and its `sys_audit_log` row, can name the administrator instead of a null actor. An absent caller, or one without a `userId`, means an unattributed write. The channel carries the acting user only: it grants nothing, since the authority gate stays at the door, and it names no tenant, since the activation ledger has no organization column. + +The parameter is optional, so every current implementer and caller compiles unchanged. This is the contract half: the toggle door and `@objectstack/service-automation`'s `toggleFlow` start passing and writing the caller in a later change, and until then the flow switch's ledger row still carries no actor. diff --git a/packages/spec/src/contracts/automation-service.ts b/packages/spec/src/contracts/automation-service.ts index 10e36dd70f0..8fc1e1ccba3 100644 --- a/packages/spec/src/contracts/automation-service.ts +++ b/packages/spec/src/contracts/automation-service.ts @@ -991,10 +991,31 @@ export interface IAutomationService { * the automation service refuses it with `RESOURCE_CONFLICT` / 409 and * changes nothing. That flow's switch is its own `status` (`'obsolete'` / * `'active'`), published through {@link registerFlow}. + * + * `caller` names who made the request, so the activation-ledger row the + * switch writes, and the audit row that write produces, attribute it to + * that user. It carries {@link AutomationContext.userId} and nothing else: + * the key {@link execute} reads for its acting user, narrowed to what the + * write records. + * - No positions, permissions or posture. This method is no authority + * gate: ADR-0126 §5 gates the switch at the door that receives the + * request, and the ledger write itself runs elevated. A channel that + * carried grants would invite a second, divergent gate here. + * - No tenant. The ledger is deployment state with no organization column + * (ADR-0126 as amended by ADR-0131 D6). + * + * A door that serves a person passes that person's id. An absent `caller`, + * or one without a `userId`, means an unattributed write: the ledger row + * and its audit row carry no actor. That is the honest answer for an + * in-process caller with no person behind it, never the answer for a door + * that has one. Attribution only: the channel grants nothing, and an + * implementation neither refuses nor widens the switch on it. + * * @param name - Flow name (snake_case) * @param enabled - Whether to enable (true) or disable (false) + * @param caller - Who made the request; absent means an unattributed write */ - toggleFlow?(name: string, enabled: boolean): Promise; + toggleFlow?(name: string, enabled: boolean, caller?: Pick): Promise; /** * List execution runs for a flow diff --git a/packages/spec/src/contracts/automation-toggle-flow-caller.pin.test.ts b/packages/spec/src/contracts/automation-toggle-flow-caller.pin.test.ts new file mode 100644 index 00000000000..8d2eefb705e --- /dev/null +++ b/packages/spec/src/contracts/automation-toggle-flow-caller.pin.test.ts @@ -0,0 +1,86 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#26506] `IAutomationService.toggleFlow`'s caller channel — who switched a + * packaged flow on or off, so the activation-ledger row and its audit row can + * name that user instead of writing a null actor. + * + * Pinned here, at the type level, because a TypeScript interface member has no + * Zod schema to assert against (the `automation-context-caller-param-keys` + * pin's form, compiled by `check:test-typecheck` under `tsconfig.test.json`): + * + * 1. **The third parameter is exactly `Pick`, + * optional.** The acting-user key `execute` already reads, and nothing + * else: no grants (the authority gate stays at the door) and no tenant + * (the ledger has no organization column). A caller literal carrying + * positions or a record does not compile. + * 2. **Additive.** A two-argument call still compiles, and so does an + * implementation written against the two-parameter signature, so no + * current implementer or caller moves. + * + * ⛔ Not pinned here: that the doors pass the caller and that the ledger write + * carries it. That is the server half, `@objectstack/runtime`'s toggle door + * and `@objectstack/service-automation`'s `toggleFlow` (#26474). + */ + +import { describe, it, expect } from 'vitest'; + +import type { AutomationContext, IAutomationService } from './automation-service'; + +/** Type-level identity: true iff A and B are the same type. */ +type Eq< A, B > = (< T >() => T extends A ? 1 : 2) extends (< T >() => T extends B ? 1 : 2) ? true : false; +/** Compile error when the argument is not `true`. */ +type Assert< T extends true > = T; + +type ToggleFlow = NonNullable< IAutomationService['toggleFlow'] >; +type ToggleFlowParams = Parameters< ToggleFlow >; + +// Exported deliberately — an unread alias is TS6196, and a pin no program compiles is no pin at all. + +/** The third parameter is the caller, narrowed to the acting user, optional. */ +export type CallerIsTheActingUserOnly = Assert< Eq< ToggleFlowParams[2], Pick< AutomationContext, 'userId' > | undefined > >; +/** It is the last parameter, and the two existing ones keep their place. */ +export type ToggleFlowArity = Assert< Eq< ToggleFlowParams['length'], 2 | 3 > >; +export type NameAndEnabledUnchanged = Assert< Eq< [ToggleFlowParams[0], ToggleFlowParams[1]], [string, boolean] > >; + +/** Additive: an implementation written against the two-parameter signature still satisfies the contract. */ +export const twoParameterToggle: ToggleFlow = async (_name: string, _enabled: boolean) => {}; +export class TwoParameterService implements Pick< IAutomationService, 'execute' | 'listFlows' | 'toggleFlow' > { + async execute() { return { success: true }; } + async listFlows() { return []; } + async toggleFlow(_name: string, _enabled: boolean): Promise< void > {} +} + +/** Additive: a two-argument call still compiles. */ +export const twoArgumentCall = (service: IAutomationService) => service.toggleFlow?.('showcase_urgent_task_alert', false); +export const callerCall = (service: IAutomationService) => + service.toggleFlow?.('showcase_urgent_task_alert', false, { userId: 'usr_admin' }); + +/** A caller that names no user is allowed, and reads as an unattributed write. */ +export const anonymousCaller: ToggleFlowParams[2] = {}; + +// @ts-expect-error — grants are not the channel's: the authority gate stays at the door. +export const callerWithPositions: ToggleFlowParams[2] = { userId: 'usr_admin', positions: ['admin'] }; +// @ts-expect-error — the channel is not an execution context: no record, no params. +export const callerWithRecord: ToggleFlowParams[2] = { userId: 'usr_admin', record: { id: 'r1' } }; + +describe('IAutomationService.toggleFlow — the caller channel', () => { + it('an implementation reads the caller id without a cast, and absent when not passed', async () => { + const seen: Array< string | undefined > = []; + const service: Pick< IAutomationService, 'toggleFlow' > = { + toggleFlow: async (_name, _enabled, caller?) => { + seen.push(caller?.userId); + }, + }; + await service.toggleFlow?.('showcase_urgent_task_alert', false, { userId: 'usr_admin' }); + await service.toggleFlow?.('showcase_urgent_task_alert', true); + expect(seen).toEqual(['usr_admin', undefined]); + }); + + it('a two-parameter implementation still runs when a caller is passed', async () => { + const service = new TwoParameterService(); + await expect(service.toggleFlow('showcase_urgent_task_alert', false)).resolves.toBeUndefined(); + const asContract: Pick< IAutomationService, 'toggleFlow' > = service; + await expect(asContract.toggleFlow?.('showcase_urgent_task_alert', false, { userId: 'usr_admin' })).resolves.toBeUndefined(); + }); +});