diff --git a/.changeset/33757-flow-dry-run-contract.md b/.changeset/33757-flow-dry-run-contract.md new file mode 100644 index 00000000000..69df3e0e043 --- /dev/null +++ b/.changeset/33757-flow-dry-run-contract.md @@ -0,0 +1,19 @@ +--- +'@objectstack/spec': patch +--- + +feat(spec): `IAutomationService.dryRun`, a dry run of a flow definition that need not be published: once, unarmed, as a named user, against a supplied start, without changing live data (#33757) + +Clause-②: yes (widening) — a new optional member on `IAutomationService` and new exported types. No existing member, type or code changes, and an implementation without the member still satisfies the contract. +Metadata-protocol: none + +The contract half of #33661 (objectstack-ai/cloud#3309 item 5, option A, ruled by the account owner); #33661 implements it in `@objectstack/service-automation` and `@objectstack/runtime`. The method's doc block states the semantics the implementation owes: + +- **Writes are simulated, never made.** `create_record`, `update_record` and `delete_record` report the write they would make (object, operation, target rows, values as computed) and a three-way `permission`: an insert or update answers `allowed` or `refused` from the object-level write check, a delete answers `not_judged`, and every write answers `not_judged` when no object-level check is installed; `not_judged` never means allowed, and row-level refusals are never reported. A create binds an opaque placeholder id that later reads by id see, holding only the fields the flow set; an updated row reads as the live row with the simulated fields laid over it. No hook, trigger, other flow or audit row runs, since nothing is written. +- **Identity is the named user**, whose own grants the engine resolves, and the run is never elevated, a `runAs: 'system'` definition included. A `tenantId` must be an organization the user is a member of. Admission is the by-name start rule (`flowStartRefusal`), judged on the supplied definition against the named user; a refusal answers `status: 'refused'` with `PERMISSION_DENIED`, before any node runs. +- **A start condition that does not hold** answers `status: 'condition_not_met'` with the condition, mirroring the live run's skip exit. One whose evaluation throws answers `status: 'failed'` with no steps, the condition and the thrown message in `startCondition`, as the live run's catch does on the default error strategy; under `errorHandling.strategy: 'retry'` the live run's retry attempts do not evaluate the start condition (#33935), so there the dry run's answer is its own. Both answer before the node input-schema check. +- **Approval nodes and waits stop the run** (every node type whose descriptor declares `supportsPause: true`), reported with who would be asked, what would resume it, the screen, or the flow it would start. Nothing is created or scheduled. +- **Side effects outside the data** (`notify`, `http`, `connector_action`, `script`, `save_notice`) are never sent, only reported with their computed payload. +- **Unarmed and unregistered**: never registered or listed, the trigger never armed, no run persisted, one pass with no retry. An invalid definition throws what `registerFlow` throws. + +**In-process APIs.** `@objectstack/spec/contracts` exports `FlowDryRunOptions`, `FlowDryRunResult`, `FlowDryRunStatus`, `FlowDryRunRefusalCode`, `FlowDryRunWritePermission`, `FlowDryRunStep` and the step types it unions over (`FlowDryRunStepBase`, `FlowDryRunRouteStep`, `FlowDryRunAssignmentStep`, `FlowDryRunDecisionStep`, `FlowDryRunReadStep`, `FlowDryRunWriteStep`, `FlowDryRunOutboundStep`, `FlowDryRunStopStep`). All additive. diff --git a/packages/spec/api-surface/contracts.json b/packages/spec/api-surface/contracts.json index 062f768ad51..5f02b9e20ee 100644 --- a/packages/spec/api-surface/contracts.json +++ b/packages/spec/api-surface/contracts.json @@ -130,6 +130,20 @@ "FieldFileDescription (interface)", "FieldFileSlot (interface)", "FinishReason (type)", + "FlowDryRunAssignmentStep (interface)", + "FlowDryRunDecisionStep (interface)", + "FlowDryRunOptions (interface)", + "FlowDryRunOutboundStep (interface)", + "FlowDryRunReadStep (interface)", + "FlowDryRunRefusalCode (type)", + "FlowDryRunResult (interface)", + "FlowDryRunRouteStep (interface)", + "FlowDryRunStatus (type)", + "FlowDryRunStep (type)", + "FlowDryRunStepBase (interface)", + "FlowDryRunStopStep (interface)", + "FlowDryRunWritePermission (type)", + "FlowDryRunWriteStep (interface)", "FlowRuntimeState (interface)", "FlowUnboundCause (type)", "GenerateDraftOpts (interface)", diff --git a/packages/spec/export-origins/contracts.json b/packages/spec/export-origins/contracts.json index 136cdb67531..9fd8dab1644 100644 --- a/packages/spec/export-origins/contracts.json +++ b/packages/spec/export-origins/contracts.json @@ -130,6 +130,20 @@ "FieldFileDescription": "src/contracts/storage-service.ts#FieldFileDescription (interface)", "FieldFileSlot": "src/contracts/storage-service.ts#FieldFileSlot (interface)", "FinishReason": "node_modules/ai/dist/index.d.ts#FinishReason (type)", + "FlowDryRunAssignmentStep": "src/contracts/automation-service.ts#FlowDryRunAssignmentStep (interface)", + "FlowDryRunDecisionStep": "src/contracts/automation-service.ts#FlowDryRunDecisionStep (interface)", + "FlowDryRunOptions": "src/contracts/automation-service.ts#FlowDryRunOptions (interface)", + "FlowDryRunOutboundStep": "src/contracts/automation-service.ts#FlowDryRunOutboundStep (interface)", + "FlowDryRunReadStep": "src/contracts/automation-service.ts#FlowDryRunReadStep (interface)", + "FlowDryRunRefusalCode": "src/contracts/automation-service.ts#FlowDryRunRefusalCode (type)", + "FlowDryRunResult": "src/contracts/automation-service.ts#FlowDryRunResult (interface)", + "FlowDryRunRouteStep": "src/contracts/automation-service.ts#FlowDryRunRouteStep (interface)", + "FlowDryRunStatus": "src/contracts/automation-service.ts#FlowDryRunStatus (type)", + "FlowDryRunStep": "src/contracts/automation-service.ts#FlowDryRunStep (type)", + "FlowDryRunStepBase": "src/contracts/automation-service.ts#FlowDryRunStepBase (interface)", + "FlowDryRunStopStep": "src/contracts/automation-service.ts#FlowDryRunStopStep (interface)", + "FlowDryRunWritePermission": "src/contracts/automation-service.ts#FlowDryRunWritePermission (type)", + "FlowDryRunWriteStep": "src/contracts/automation-service.ts#FlowDryRunWriteStep (interface)", "FlowRuntimeState": "src/contracts/automation-service.ts#FlowRuntimeState (interface)", "FlowUnboundCause": "src/contracts/automation-service.ts#FlowUnboundCause (type)", "GenerateDraftOpts": "src/contracts/external-datasource-service.ts#GenerateDraftOpts (interface)", diff --git a/packages/spec/src/contracts/automation-dry-run.pin.test.ts b/packages/spec/src/contracts/automation-dry-run.pin.test.ts new file mode 100644 index 00000000000..c89998fded5 --- /dev/null +++ b/packages/spec/src/contracts/automation-dry-run.pin.test.ts @@ -0,0 +1,269 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33757] `IAutomationService.dryRun`: run a flow definition that need not be + * published, once, unarmed, as a named user, against a supplied start, without + * changing live data. The contract half of #33661, which implements it. + * + * Pinned at the type level, because a TypeScript interface member has no Zod + * schema to assert against (the `automation-toggle-flow-caller` pin's form, + * compiled by `check:test-typecheck` under `tsconfig.test.json`): + * + * 1. **The signature.** Optional, `(definition: unknown, options: + * FlowDryRunOptions) => Promise`, so an implementation + * without it still satisfies the contract. + * 2. **The options name a user and carry no grants.** `userId` is a required + * string; positions, permission sets, posture and email do not compile, so + * a caller cannot dry-run with grants the user does not hold. + * 3. **The closed unions.** The five statuses, the two refusal codes (each + * an existing `AutomationResult.code` member, none minted), the seven + * step kinds, the three-way write permission and the six stop reasons. + * 4. **The documented semantics, and the answer order.** The method's doc block states each ruled + * point (writes, identity, admission, approvals and waits, outbound + * nodes, unarmed and unregistered, the start condition, step order). Prose is unassertable except by + * reading it, so the contract source is read. + * + * ⛔ Not pinned here: the behaviour. That is #33661, in + * `@objectstack/service-automation` and `@objectstack/runtime`. + */ + +import { readFileSync } from 'node:fs'; +import { fileURLToPath } from 'node:url'; + +import { describe, it, expect } from 'vitest'; + +import type { + AutomationResult, + FlowDryRunOptions, + FlowDryRunRefusalCode, + FlowDryRunResult, + FlowDryRunStatus, + FlowDryRunStep, + FlowDryRunWritePermission, + 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 DryRun = NonNullable< IAutomationService['dryRun'] >; + +// Exported deliberately — an unread alias is TS6196, and a pin no program compiles is no pin at all. + +/** 1. The signature. */ +export type DryRunSignature = Assert< Eq< DryRun, (definition: unknown, options: FlowDryRunOptions) => Promise< FlowDryRunResult > > >; +/** Optional: the member may be absent. */ +export type DryRunIsOptional = Assert< Eq< undefined extends IAutomationService['dryRun'] ? true : false, true > >; +export class ServiceWithoutDryRun implements Pick< IAutomationService, 'execute' | 'listFlows' | 'dryRun' > { + async execute() { return { success: true }; } + async listFlows() { return []; } +} + +/** 2. The options: a required acting user, and the start keys `execute` reads. */ +export type UserIdIsRequired = Assert< Eq< FlowDryRunOptions['userId'], string > >; +export type UserIdNotOptional = Assert< Eq< {} extends Pick< FlowDryRunOptions, 'userId' > ? true : false, false > >; +export const minimalOptions: FlowDryRunOptions = { userId: 'usr_maker' }; +export const startOptions: FlowDryRunOptions = { + userId: 'usr_maker', + object: 'opportunity', + event: 'on_update', + record: { id: 'opp_1', stage: 'won' }, + previous: { id: 'opp_1', stage: 'open' }, + params: { note: 'x' }, + locale: 'zh-CN', + tenantId: 'org_1', +}; +// @ts-expect-error — the acting user is required. +export const noUser: FlowDryRunOptions = { record: { id: 'opp_1' } }; +// @ts-expect-error — no grants: the engine resolves the named user's own positions. +export const withPositions: FlowDryRunOptions = { userId: 'usr_maker', positions: ['admin'] }; +// @ts-expect-error — no grants: the engine resolves the named user's own permission sets. +export const withPermissions: FlowDryRunOptions = { userId: 'usr_maker', permissions: ['modify_all'] }; +// @ts-expect-error — no grants: the engine resolves the named user's own posture. +export const withPosture: FlowDryRunOptions = { userId: 'usr_maker', posture: 'PLATFORM_ADMIN' }; +// @ts-expect-error — no grants: the engine resolves the named user's own email. +export const withEmail: FlowDryRunOptions = { userId: 'usr_maker', email: 'boss@example.com' }; +// @ts-expect-error — the run is never elevated: there is no run-as channel. +export const withRunAs: FlowDryRunOptions = { userId: 'usr_maker', runAs: 'system' }; + +/** 3. The closed unions, spelled once. `satisfies` proves membership; `Eq` proves there is nothing else. */ +export const DRY_RUN_STATUSES = ['completed', 'condition_not_met', 'stopped', 'failed', 'refused'] as const satisfies readonly FlowDryRunStatus[]; +export type StatusIsExactlyTheFive = Assert< Eq< FlowDryRunStatus, (typeof DRY_RUN_STATUSES)[number] > >; +export type ResultStatusIsTheUnion = Assert< Eq< FlowDryRunResult['status'], FlowDryRunStatus > >; + +export const DRY_RUN_REFUSAL_CODES = ['PERMISSION_DENIED', 'FLOW_INPUT_SCHEMA_INVALID'] as const satisfies readonly FlowDryRunRefusalCode[]; +export type RefusalCodeIsExactlyTheTwo = Assert< Eq< FlowDryRunRefusalCode, (typeof DRY_RUN_REFUSAL_CODES)[number] > >; +// @ts-expect-error — registration refuses a definition with no start node, so a dry run throws before it could answer this code. +export const noStartNodeCode: FlowDryRunRefusalCode = 'FLOW_NO_START_NODE'; +export type StartConditionShape = Assert< Eq< + NonNullable< FlowDryRunResult['startCondition'] >, + { expression: string; result: false; fault?: never } | { expression: string; fault: string; result?: never } +> >; +// @ts-expect-error — a start condition either did not hold or could not be evaluated, never both. +export const bothResultAndFault: NonNullable< FlowDryRunResult['startCondition'] > = { expression: 'record.x', result: false, fault: 'boom' }; +/** No code is minted: every refusal code is an existing `AutomationResult.code` member. */ +export type RefusalCodesAreExisting = Assert< Eq< FlowDryRunRefusalCode extends NonNullable< AutomationResult['code'] > ? true : false, true > >; +export type ResultRefusalShape = Assert< Eq< NonNullable< FlowDryRunResult['refusal'] >, { code: FlowDryRunRefusalCode; message: string } > >; + +export const DRY_RUN_STEP_KINDS = ['route', 'assignment', 'decision', 'read', 'write', 'outbound', 'stop'] as const satisfies readonly FlowDryRunStep['kind'][]; +export type StepKindIsExactlyTheSeven = Assert< Eq< FlowDryRunStep['kind'], (typeof DRY_RUN_STEP_KINDS)[number] > >; +export type WriteOperations = Assert< Eq< Extract< FlowDryRunStep, { kind: 'write' } >['operation'], 'create' | 'update' | 'delete' > >; +export type StopReasons = Assert< Eq< Extract< FlowDryRunStep, { kind: 'stop' } >['reason'], 'approval' | 'wait' | 'screen' | 'starts_flow' | 'suspends' | 'not_simulated' > >; +export const WRITE_PERMISSIONS = ['allowed', 'refused', 'not_judged'] as const satisfies readonly FlowDryRunWritePermission[]; +export type WritePermissionIsExactlyTheThree = Assert< Eq< FlowDryRunWritePermission, (typeof WRITE_PERMISSIONS)[number] > >; +/** Every write step answers the permission; it is never left out. */ +export type WriteStepPermissionRequired = Assert< Eq< Extract< FlowDryRunStep, { kind: 'write' } >['permission'], FlowDryRunWritePermission > >; +/** A decision reports every expression that held, not one. */ +export type DecisionExpressions = Assert< Eq< Extract< FlowDryRunStep, { kind: 'decision' } >['expressions'], string[] > >; + +/** A result an implementation can build without a cast: one ordered step list. */ +export const stoppedAtApproval: FlowDryRunResult = { + status: 'stopped', + steps: [ + { kind: 'route', nodeId: 'start', nodeType: 'start' }, + { kind: 'decision', nodeId: 'big_deal', nodeType: 'decision', branch: 'big', expressions: ['record.amount > 100000'], next: ['create_task'] }, + { kind: 'write', nodeId: 'create_task', nodeType: 'create_record', operation: 'create', object: 'task', recordIds: ['dryrun:create_task:1'], values: { subject: 'Review' }, permission: 'allowed' }, + { kind: 'assignment', nodeId: 'remember', nodeType: 'assignment', values: { taskId: 'dryrun:create_task:1' } }, + { kind: 'outbound', nodeId: 'tell_owner', nodeType: 'notify', payload: { to: ['usr_owner'], body: 'Review' } }, + { kind: 'stop', nodeId: 'approve', nodeType: 'approval', reason: 'approval', approvers: ['usr_manager'] }, + ], +}; +export const refusedAtAdmission: FlowDryRunResult = { + status: 'refused', + steps: [], + refusal: { code: 'PERMISSION_DENIED', message: 'This caller may not start this flow.' }, +}; +export const failedOnPermission: FlowDryRunResult = { + status: 'failed', + steps: [ + { kind: 'route', nodeId: 'start', nodeType: 'start' }, + { + kind: 'write', + nodeId: 'close_deal', + nodeType: 'update_record', + operation: 'update', + object: 'opportunity', + recordIds: ['opp_1'], + values: { stage: 'closed' }, + permission: 'refused', + refusal: { code: 'PERMISSION_DENIED', message: 'the named user may not update opportunity' }, + }, + ], + error: 'update_record close_deal: the named user may not update opportunity', +}; +export const conditionNotMet: FlowDryRunResult = { + status: 'condition_not_met', + steps: [], + startCondition: { expression: 'record.stage == "won" && previous.stage != "won"', result: false }, +}; +/** A start condition that throws when evaluated: `'failed'`, no steps, the fault beside the condition. */ +export const conditionFaulted: FlowDryRunResult = { + status: 'failed', + steps: [], + startCondition: { + expression: 'record.amount > "high"', + fault: 'condition failed to evaluate as CEL: no such overload — source: `record.amount > "high"`.', + }, + error: 'condition failed to evaluate as CEL: no such overload — source: `record.amount > "high"`.', +}; +// @ts-expect-error — a dry run has no run to resume or read back: no `runId`. +export const withRunId: FlowDryRunResult = { status: 'completed', steps: [], runId: 'run_1' }; + +describe('IAutomationService.dryRun — the contract (#33757)', () => { + it('an implementation reads the options and answers the result without a cast', async () => { + const seen: string[] = []; + const service: Pick< IAutomationService, 'dryRun' > = { + dryRun: async (_definition, options) => { + seen.push(options.userId); + return stoppedAtApproval; + }, + }; + const result = await service.dryRun?.({ name: 'draft' }, startOptions); + expect(seen).toEqual(['usr_maker']); + expect(result?.steps.map((s) => s.kind)).toEqual(['route', 'decision', 'write', 'assignment', 'outbound', 'stop']); + }); + + it('a service without dryRun still satisfies the contract, and a caller finds the member absent', () => { + const service: Pick< IAutomationService, 'execute' | 'listFlows' | 'dryRun' > = new ServiceWithoutDryRun(); + expect(service.dryRun).toBeUndefined(); + }); + + it("the method's doc block states each ruled point", () => { + const source = readFileSync(fileURLToPath(new URL('./automation-service.ts', import.meta.url)), 'utf8'); + const end = source.indexOf('dryRun?(definition: unknown, options: FlowDryRunOptions)'); + expect(end).toBeGreaterThan(0); + const start = source.lastIndexOf('/**', end); + const doc = source.slice(start, end); + for (const heading of [ + '**Unarmed and unregistered.**', + '**Identity: the named user.**', + '**Admission: the by-name start rule.**', + '**Writes: simulated, never made.**', + '**Side effects outside the data.**', + '**Approval nodes and waits: the run stops.**', + '**The name.**', + '**Step order.**', + ]) { + expect(doc, heading).toContain(heading); + } + for (const sentence of [ + 'It is never elevated.', + 'never registered', + 'No run is persisted', + 'no other flow, no audit row.', + 'never send or call anything', + 'no child run starts.', + 'every named user.', + "`status: 'condition_not_met'`", + "`'not_judged'` never means allowed.", + 'the simulated fields laid over it', + 'holds only the fields the flow set', + 'each branch\'s steps together', + 'could not continue past this', + 'With no object-level check\n * installed, every write step answers `\'not_judged\'`.', + "its step carries\n * `refusal` (`code: 'PERMISSION_DENIED'`)", + "For a node's unconditional\n * out-edges this is the dry run's own order", + "A `condition` whose evaluation throws", + "`status: 'failed'` and no steps, the condition and the thrown message\n * in `startCondition` (`fault`)", + 'cannot fire for a dry run: a dry run holds no\n * active-run key and writes nothing.', + "On the default error strategy that is\n * the live run's answer", + "Under `errorHandling.strategy: 'retry'` the answer is the dry run's\n * own", + ]) { + expect(doc, sentence).toContain(sentence); + } + }); + + it('a faulting start condition follows the live run only on the default error strategy (#33935)', () => { + const source = readFileSync(fileURLToPath(new URL('./automation-service.ts', import.meta.url)), 'utf8'); + const statusEnd = source.indexOf('export type FlowDryRunStatus ='); + const resultStart = source.indexOf('export interface FlowDryRunResult {'); + const methodEnd = source.indexOf('dryRun?(definition: unknown, options: FlowDryRunOptions)'); + const docs = { + "the 'failed' status doc": source.slice(source.lastIndexOf('/**', statusEnd), statusEnd), + 'the startCondition doc': source.slice(resultStart, source.indexOf('startCondition?:', resultStart)), + 'the dryRun doc block': source.slice(source.lastIndexOf('/**', methodEnd), methodEnd), + }; + for (const [where, doc] of Object.entries(docs)) { + expect(doc, where).toContain('On the default error strategy'); + expect(doc, where).toContain("`errorHandling.strategy: 'retry'`"); + expect(doc, where).toContain('#33935'); + } + expect(docs["the 'failed' status doc"]).toContain("Under\n * `errorHandling.strategy: 'retry'` it is the dry run's own answer, not a\n * prediction"); + expect(source).not.toContain('This one follows the live run, whose'); + }); + + it('answers in the live run\'s order: admission, then the start condition (unmet, then faulted), then the input-schema check', () => { + const source = readFileSync(fileURLToPath(new URL('./automation-service.ts', import.meta.url)), 'utf8'); + const end = source.indexOf('dryRun?(definition: unknown, options: FlowDryRunOptions)'); + const doc = source.slice(source.lastIndexOf('/**', end), end); + const order = [ + doc.indexOf("then admission: `status: 'refused'`, `PERMISSION_DENIED` in"), + doc.indexOf('Then the start condition.'), + doc.indexOf('A `condition` whose evaluation throws'), + doc.indexOf('Then the node input-schema check'), + ]; + expect(order.every((i, k) => i >= 0 && (k === 0 || order[k - 1] < i)), `answer order at ${order.join(', ')}`).toBe(true); + }); +}); diff --git a/packages/spec/src/contracts/automation-service.ts b/packages/spec/src/contracts/automation-service.ts index 6747277f5d4..da99c730352 100644 --- a/packages/spec/src/contracts/automation-service.ts +++ b/packages/spec/src/contracts/automation-service.ts @@ -1437,6 +1437,277 @@ export interface ConnectorConnectionCallbackRequest { context?: ExecutionContext; } +/** + * What {@link IAutomationService.dryRun} is asked to run the definition + * against (#33757): the acting user, and the start a trigger would hand the + * run. The start keys are {@link AutomationContext}'s own, read as + * {@link IAutomationService.execute} reads them. + * + * ⛔ No grants: no positions, permission sets, posture or email. The engine + * resolves the named user's own, so a caller cannot run a dry run with grants + * the user does not hold. + */ +export interface FlowDryRunOptions extends Pick { + /** + * The acting user, a `sys_user` id. Required: a dry run always acts as a + * named user, so it never meets `AUTOMATION_UNSCOPED_RUN_DATA_ACCESS`. + */ + userId: string; + /** + * The organization the run is in, read as {@link AutomationContext.tenantId}. + * It must be an organization the named user is a member of; any other is + * refused before any node runs (`status: 'refused'`, + * `PERMISSION_DENIED`). That refusal is the dry run's own: a live run's + * setup refuses no organization, it only resolves none of the user's + * grants there. Absent, the run is in no organization, as a by-name start + * that carries none. + */ + tenantId?: string; +} + +/** + * The overall answer of {@link IAutomationService.dryRun}. + * + * - `'completed'`: the run reached an `end` node. An `end` node declaring + * `outcome: 'refused'` completes too; its step carries the rendered + * `refusalMessage`. + * - `'condition_not_met'`: the start node's `condition` did not hold for the + * supplied `record` / `previous`, so no node ran and there are no steps. + * It mirrors the live run's skip exit (`execute` answers + * `output: { skipped: true, reason: 'condition_not_met' }`); the result + * carries the condition in {@link FlowDryRunResult.startCondition}. + * - `'stopped'`: the run reached a node it does not go past (see + * {@link FlowDryRunStopStep}); that node is the last step. + * - `'failed'`: the dry run could not continue past a step, or could not + * evaluate the start condition. A node failed, or met a refusal, that no + * `fault` edge or `try_catch` region contains; that node is the last + * step. It is the dry run's own answer, not a prediction: a dry run can + * fail where the live run would not (a later node reads an outbound + * node's output, which a dry run leaves unset; a `runAs: 'system'` draft, + * which a dry run never elevates, meets a refusal; a filtered read misses + * a simulated write), and pass where the live run would fail (a + * validation rule, a uniqueness constraint, a required field, or any + * refusal a dry run does not judge). + * A start node whose `condition` throws when it is evaluated against the + * supplied `record` / `previous` fails before any node runs: there are no + * steps, and {@link FlowDryRunResult.startCondition} carries the condition + * and the fault. On the default error strategy this one follows the live + * run, whose `execute` answers the same throw `status: 'failed'`. Under + * `errorHandling.strategy: 'retry'` it is the dry run's own answer, not a + * prediction: the live run hands the throw to `retryExecution`, whose + * attempts (`executeWithoutRetry`) do not evaluate the start condition and + * run the nodes (#33935, the engine card; this clause goes when it lands). + * - `'refused'`: the run never started (see {@link FlowDryRunResult.refusal}). + * ⛔ Not {@link AutomationResult.status}'s `'refused'`, which is an `end` + * node's outcome; here that case is `'completed'`. + */ +export type FlowDryRunStatus = 'completed' | 'condition_not_met' | 'stopped' | 'failed' | 'refused'; + +/** + * Why a dry run never started: existing {@link AutomationResult.code} members + * only, each with the meaning it has there. ⛔ No new code is minted for a dry + * run. (`FLOW_NO_START_NODE` is not among them: a definition with no `start` + * node is one {@link IAutomationService.registerFlow} refuses, so a dry run + * throws for it before it could answer.) + */ +export type FlowDryRunRefusalCode = Extract, 'PERMISSION_DENIED' | 'FLOW_INPUT_SCHEMA_INVALID'>; + +/** What every step of a dry run reports. */ +export interface FlowDryRunStepBase { + /** The node's id in the definition. */ + nodeId: string; + /** The node's `type` as the definition declares it. */ + nodeType: string; + /** + * A refusal this node met, as a failed live run reports it + * ({@link AutomationResult.innerRefusal}): a read refused in the named + * user's scope, for example. Absent when the node met none. + */ + refusal?: FlowInnerRefusal; + /** The node's failure text when it failed for a reason other than a refusal. */ + error?: string; +} + +/** + * A node that only routes the run: `start`, `end`, `loop`, `parallel` and + * `try_catch`. + */ +export interface FlowDryRunRouteStep extends FlowDryRunStepBase { + kind: 'route'; + /** An `end` node declaring `outcome: 'refused'`: its message, rendered. */ + refusalMessage?: string; +} + +/** An `assignment` node: the variables it set, with the values it computed. */ +export interface FlowDryRunAssignmentStep extends FlowDryRunStepBase { + kind: 'assignment'; + values: Record; +} + +/** A `decision` node: which way the run went, and why. */ +export interface FlowDryRunDecisionStep extends FlowDryRunStepBase { + kind: 'decision'; + /** + * The branch label: the `label` of the first `config.conditions` entry that + * held, or `'default'` when none held. Absent on a decision that declares + * no conditions, whose out-edges carry the branching. + */ + branch?: string; + /** + * Why: every authored expression that held, in declaration order. One for + * a `config.conditions` entry or an exclusive decision's out-edge; one per + * branch taken for `mode: 'inclusive'`. Empty when the run took the + * default branch because none held. + */ + expressions: string[]; + /** The ids of the nodes the run went on to; more than one only for `mode: 'inclusive'`. */ + next: string[]; +} + +/** A `get_record` node: the rows it read, in the named user's scope. */ +export interface FlowDryRunReadStep extends FlowDryRunStepBase { + kind: 'read'; + object: string; + /** The ids of the rows the read answered, placeholder ids included. */ + recordIds: string[]; +} + +/** + * Whether the named user may make a simulated write, as far as a dry run can + * judge it without writing. + * + * - `'allowed'` / `'refused'`: an insert or an update, answered by the + * object-level write check (`canWriteObject`, insert or update) for the + * named user. `'allowed'` says that check admits the write, never that the + * write would succeed. + * - `'not_judged'`: every delete (the check has no delete arm), and every + * write when no object-level check is installed. `canWriteObject` is a + * method of `@objectstack/plugin-security`'s service, not of + * `ISecurityService`, so a kernel without that plugin has none. ⛔ + * `'not_judged'` never means allowed. + * + * Never reported, for any operation, whatever the answer: the refusals that + * check does not judge, which are row-level and post-image rules (the RLS + * `using` pre-image and `check` post-image), controlled-by-parent, the tenant + * post-image, provenance gates and the `owner_id` forge. + */ +export type FlowDryRunWritePermission = 'allowed' | 'refused' | 'not_judged'; + +/** + * A `create_record`, `update_record` or `delete_record` node: the write it + * would make. ⛔ Nothing was written. + */ +export interface FlowDryRunWriteStep extends FlowDryRunStepBase { + kind: 'write'; + operation: 'create' | 'update' | 'delete'; + object: string; + /** + * The rows the write targets. For an update or a delete, the live rows (or + * placeholder ids) it would change, as the named user's scope reads them. + * For a create, the one placeholder id the run binds to the new row. + */ + recordIds: string[]; + /** + * The field values as the node computed them; absent for a delete. + */ + values?: Record; + /** The named user's permission for this write, as far as a dry run judges it. */ + permission: FlowDryRunWritePermission; +} + +/** + * A node with an effect outside the data: `notify`, `http`, + * `connector_action`, `script` and `save_notice`. ⛔ Nothing was sent or + * called. + */ +export interface FlowDryRunOutboundStep extends FlowDryRunStepBase { + kind: 'outbound'; + /** + * What the node would send, as it computed it: the message, the request, + * the connector input, or (`script`) the function name and its inputs. + */ + payload: unknown; +} + +/** + * The node the run stopped at. Always the last step, and only on a + * `'stopped'` result. + */ +export interface FlowDryRunStopStep extends FlowDryRunStepBase { + kind: 'stop'; + /** + * What the node would do that a dry run does not: + * - `'approval'`: ask people to decide (the approvals plugin's `approval` + * and `approval_revise` nodes); + * - `'wait'`: wait for a timer or a signal (`wait`); + * - `'screen'`: ask the person for input (`screen`); + * - `'starts_flow'`: start another flow (`subflow`, `map`); + * - `'suspends'`: suspend the run for another reason (any other node type + * whose descriptor declares `supportsPause: true`); + * - `'not_simulated'`: something a dry run cannot know (a node type + * outside every class this contract names). + */ + reason: 'approval' | 'wait' | 'screen' | 'starts_flow' | 'suspends' | 'not_simulated'; + /** An approval node: the ids of the users it would ask, when the run can resolve them. */ + approvers?: string[]; + /** A `wait` node: what would resume it, from its `waitEventConfig`. */ + wait?: { eventType: string; timerDuration?: string; signalName?: string }; + /** A `screen` node: the screen it would show. */ + screen?: ScreenSpec; + /** A `subflow` or `map` node: the flow it would start. */ + flowName?: string; +} + +/** One executed node of a dry run. */ +export type FlowDryRunStep = + | FlowDryRunRouteStep + | FlowDryRunAssignmentStep + | FlowDryRunDecisionStep + | FlowDryRunReadStep + | FlowDryRunWriteStep + | FlowDryRunOutboundStep + | FlowDryRunStopStep; + +/** What {@link IAutomationService.dryRun} answers. */ +export interface FlowDryRunResult { + status: FlowDryRunStatus; + /** + * One entry per node execution: a node inside a loop appears once per + * pass. Empty when `status` is `'refused'` or `'condition_not_met'`, and + * when it is `'failed'` because the start condition could not be evaluated. + * Branches that run side by side (a `parallel` node's branches, a node's + * several unconditional out-edges) are listed one branch after another, in + * declaration order (`config.branches`, then out-edges), each branch's + * steps together; an inclusive decision's branches likewise, as the live + * run takes them one at a time. For unconditional out-edges that is the + * dry run's own order, and it can differ from the live step log, which can + * interleave them. Otherwise steps are in the order the run executed them. + */ + steps: FlowDryRunStep[]; + /** Set exactly when `status` is `'refused'`: why the run never started. */ + refusal?: { code: FlowDryRunRefusalCode; message: string }; + /** + * The start node's `condition` as authored (its `source`, for an expression + * envelope), set exactly when the dry run ended on it: + * - with `result: false` when `status` is `'condition_not_met'`: it did not + * hold against the supplied `record` / `previous`; + * - with `fault` when `status` is `'failed'` and there are no steps: its + * evaluation threw. `fault` is the thrown message, and `error` carries + * the same text. On the default error strategy it is the text `execute` + * answers as `error` for that throw. Under + * `errorHandling.strategy: 'retry'` the live run does not answer it: its + * retry attempts do not evaluate the start condition (#33935). + */ + startCondition?: + | { expression: string; result: false; fault?: never } + | { expression: string; fault: string; result?: never }; + /** + * Set exactly when `status` is `'failed'`: the run's failure text (for a + * start condition that could not be evaluated, the thrown message). + */ + error?: string; +} + export interface IAutomationService { /** * Execute a named flow or script @@ -1513,6 +1784,174 @@ export interface IAutomationService { todos?: ConversionTodoNotice[]; }; + /** + * Dry-run a flow definition that need not be published: once, unarmed, as + * a named user, against a supplied start, without changing live data + * (#33757, the contract half of #33661; objectstack-ai/cloud#3309 item 5, + * option A). A maker sees what a drafted flow would do before publishing it. + * + * Optional, like every member past {@link execute} and {@link listFlows}: + * only the engine that runs flows can offer it, and a caller that finds it + * absent says a dry run is not available rather than guessing. + * + * **In what order it answers.** + * - `options.userId` that is not a non-empty string: throws a `TypeError` + * before the definition is read. + * - A definition {@link registerFlow} would refuse under the definition's + * own `name`: throws what {@link registerFlow} throws for it. Nothing is + * registered. + * - A `tenantId` naming an organization the named user is not a member + * of, then admission: `status: 'refused'`, `PERMISSION_DENIED` in + * `refusal`, and no steps. + * - Then the start condition. A start node whose `condition` does not hold + * for the supplied `record` / `previous` answers + * `status: 'condition_not_met'`, the condition and its value in + * `startCondition`, and no steps. This mirrors the live run's skip exit + * (`execute`, `output: { skipped: true, reason: 'condition_not_met' }`, + * `engine.ts` in `@objectstack/service-automation`), in the live run's + * order: `execute` evaluates the start condition before its node + * input-schema check, so a condition that does not hold answers first. + * A `condition` whose evaluation throws (a CEL error against the + * supplied `record` / `previous`, for instance) answers + * `status: 'failed'` and no steps, the condition and the thrown message + * in `startCondition` (`fault`) and the same message in `error`, also + * before the input-schema check. On the default error strategy that is + * the live run's answer: `execute`'s catch answers that throw + * `status: 'failed'` with the message as `error`, before any node runs. + * Under `errorHandling.strategy: 'retry'` the answer is the dry run's + * own: `execute` hands the throw to `retryExecution`, whose attempts + * (`executeWithoutRetry`) do not evaluate the start condition, and the + * live run goes on to run the nodes (#33935, the engine card; this + * clause goes when it lands). + * - `execute`'s re-entrancy guard (`reentrancy_loop_guard`), which sits + * between the two checks, cannot fire for a dry run: a dry run holds no + * active-run key and writes nothing. + * - Then the node input-schema check {@link execute} runs before dispatch: + * `status: 'refused'`, `FLOW_INPUT_SCHEMA_INVALID` in `refusal`, and no + * steps. + * - No node has run before any of these answers. Otherwise the nodes run, + * as described below. + * + * **The name.** The definition's own `name` is the flow's name for the + * call: the name registration judges, and the one nodes read their per-flow + * keys under (the `flows..…` translation keys). It is never looked + * up among registered flows, and a registered flow of that name is neither + * read nor touched. + * + * **Unarmed and unregistered.** + * - The definition is never registered: {@link listFlows} and + * {@link getFlow} do not answer it during or after the call. + * - Its trigger (a record change, a schedule, an API route) is never armed. + * - The definition runs once per call, with no retry. + * - No run is persisted: there is no `runId`, and {@link getRun}, + * {@link listRuns} and the run history never show it. + * + * **Identity: the named user.** + * - The run acts as `options.userId`. The engine resolves that user's own + * grants (positions, permission sets, posture, email), as it does for a + * run whose context carries none; the options have no slot for grants. + * - It is never elevated. A definition declaring `runAs: 'system'` also + * runs as the named user: every read and every permission judgement is + * that user's, so its report can show a refusal the published flow, + * running elevated, would not meet. + * - The method does not judge whether its caller may act as `userId`, as + * {@link execute} does not judge `AutomationContext.userId`. A door that + * serves a person passes that person's id. + * + * **Admission: the by-name start rule.** The rules that admit a start by + * name (`flowStartRefusal`, `@objectstack/runtime`'s + * `flow-start-admission.ts`), judged on the supplied definition's `type`, + * `runAs` and `requiredPermissions` against the named user. There is no + * system-principal bypass, since the run always names a user. + * - A definition declaring `runAs: 'system'` whose `type` starts on its own + * trigger (`autolaunched`, `record_change`, `schedule`) is refused for + * every named user. + * - A definition declaring `runAs: 'system'` is refused when the named user + * is an AI service account (ADR-0151 D3), as at the trigger routes: a dry + * run starts with no action in hand, so no AI opt-in stands. + * - A named user missing any capability the definition's + * `requiredPermissions` names is refused. + * - Each refusal is `code: 'PERMISSION_DENIED'` with the message the + * by-name start answers for the same rule. + * + * **Writes: simulated, never made.** + * - `create_record`, `update_record` and `delete_record` write nothing, and + * nothing is rolled back, since nothing was written. Each reports the + * write it would make ({@link FlowDryRunWriteStep}). + * - A create binds a placeholder id to the new row: opaque, unique within + * the call, and never the id of a live row. + * - A later read by id of a row the run updated answers the live row with + * the simulated fields laid over it; of a row the run deleted, nothing. + * - A row the run created holds only the fields the flow set. The fields + * the engine sets on a real insert (defaults, autonumbers, formulas, the + * owner, timestamps) are absent from it, so a later node that reads one + * finds it unset. + * - A read that filters rather than names an id answers the live rows the + * named user can read, and does not see the run's simulated writes. + * - Since nothing is written, nothing a write sets off runs: no record + * hook, no record-change trigger, no other flow, no audit row. + * - The data engine does not judge a simulated write, so a write it would + * refuse for a validation rule, a uniqueness constraint or a required + * field reports as it would be made. + * - Each write step carries a three-way `permission` + * ({@link FlowDryRunWritePermission}). An insert or an update answers + * `'allowed'` or `'refused'`, from the object-level write check + * (`canWriteObject`, insert or update) for the named user; `'allowed'` + * never means the write would succeed. A delete answers `'not_judged'`, + * since that check has no delete arm. With no object-level check + * installed, every write step answers `'not_judged'`. + * `'not_judged'` never means allowed. + * - The refusals that check does not judge are never reported, for any + * operation: row-level and post-image rules, controlled-by-parent, the + * tenant post-image, provenance gates, the `owner_id` forge. + * - A `'refused'` write is a refusal the step met: its step carries + * `refusal` (`code: 'PERMISSION_DENIED'`), and the run goes on as after + * any refusal (below). + * + * **Reads.** `get_record` reads live data, in the named user's scope. + * + * **A node that meets a refusal or fails.** It is reported, never skipped + * silently: its step carries the refusal or the failure, and the run goes + * on along a `fault` edge or out of a `try_catch` region that contains it, + * or else it ends `'failed'`: the dry run could not continue past this + * step. That is the dry run's own answer, in both directions: it can fail + * where the live run would not, and pass where the live run would fail + * ({@link FlowDryRunStatus}). + * + * **Side effects outside the data.** `notify`, `http`, `connector_action`, + * `script` and `save_notice` never send or call anything. Each reports the + * payload it computed ({@link FlowDryRunOutboundStep}) and binds no output, + * so a later node that reads its output finds it unset. + * + * **Approval nodes and waits: the run stops.** At the first node whose type's + * descriptor declares `supportsPause: true` (an approval, a `wait`, a + * `screen`, a `subflow`, a `map`), the run stops with `status: 'stopped'` + * and reports the node and what it would do: who it would ask, how long or + * for what it would wait, the screen it would show, or the flow it would + * start ({@link FlowDryRunStopStep}). No approval request is created, no + * resume is scheduled, and no child run starts. A node type outside every + * class named here also stops the run (`reason: 'not_simulated'`). + * + * **Nodes that route or compute** (`start`, `end`, `decision`, + * `assignment`, `loop`, `parallel`, `try_catch`) run as the live run runs + * them, and report what they did. + * + * **Step order.** One list ({@link FlowDryRunResult.steps}). Branches that + * run side by side (a `parallel` node's branches, a node's several + * unconditional out-edges) are listed one branch after another in + * declaration order, each branch's steps together, as the `parallel` node's + * own run record lists them; an inclusive decision's branches likewise, as + * the live run takes them one at a time. For a node's unconditional + * out-edges this is the dry run's own order: the live run runs those + * branches under `Promise.all`, and its step log can interleave them. + * + * @param definition - The flow definition (raw, pre-parse), as + * {@link registerFlow} takes it + * @param options - The acting user and the start + * @returns The run's status and its ordered steps + */ + dryRun?(definition: unknown, options: FlowDryRunOptions): Promise; + /** * Unregister a flow by name * @param name - Flow name (snake_case)