Repository navigation
feat(workflow-executor): pause a Trigger Action step on its approval and resume on the reviewer's branch [PRD-2004] #2063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3b31ca8
d052fd8
9053246
0fa16c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,13 @@ import type { | |
| } from '../types/step-execution-data'; | ||
| import type { ActionSchema, CollectionSchema, RecordRef } from '../types/validated/collection'; | ||
| import type { TriggerActionStepDefinition } from '../types/validated/step-definition'; | ||
| import type { ErrorKind, RecordStepStatus } from '../types/validated/step-outcome'; | ||
| import type { | ||
| ApprovalOutcome, | ||
| ApprovalRequestRef, | ||
| AwaitingInputReason, | ||
| ErrorKind, | ||
| RecordStepStatus, | ||
| } from '../types/validated/step-outcome'; | ||
|
|
||
| import { DynamicStructuredTool, HumanMessage, SystemMessage } from '@forestadmin/ai-proxy'; | ||
| import { isDeepStrictEqual } from 'util'; | ||
|
|
@@ -18,6 +24,10 @@ import { | |
| ActionFormValidationError, | ||
| ActionNotFoundError, | ||
| ActionRequiresApprovalError, | ||
| ApprovalBranchesNeedFullAiError, | ||
| ApprovalRequestFailedError, | ||
| ApprovalRequestGoneError, | ||
| ApprovalRequestUntrackedError, | ||
| NoActionsError, | ||
| PinnedArgNotFoundError, | ||
| StepStateError, | ||
|
|
@@ -73,17 +83,75 @@ interface ActionTarget extends ActionRef { | |
| } | ||
|
|
||
| export default class TriggerRecordActionStepExecutor extends RecordStepExecutor<TriggerActionStepDefinition> { | ||
| // approvalRequest lives here, off the shared record builder; super spreads it onto the outcome. | ||
| // The approval fields live here, off the shared record builder; super spreads them onto the outcome. | ||
| protected override buildOutcomeResult(outcome: { | ||
| status: RecordStepStatus; | ||
| error?: string; | ||
| errorKind?: ErrorKind; | ||
| errorSourceStepIndex?: number; | ||
| approvalRequest?: { id: string }; | ||
| awaitingInputReason?: AwaitingInputReason; | ||
| approvalRequests?: ApprovalRequestRef[]; | ||
| selectedOption?: ApprovalOutcome; | ||
| approverId?: number | null; | ||
| }): StepExecutionResult { | ||
| return super.buildOutcomeResult(outcome); | ||
| } | ||
|
|
||
| private get hasApprovalOutcomes(): boolean { | ||
| return this.context.stepDefinition.approvalOutcomes === true; | ||
| } | ||
|
|
||
| private buildApprovalPause(approvalRequest: { id: string }): StepExecutionResult { | ||
| return this.buildOutcomeResult({ | ||
| status: 'awaiting-input', | ||
| awaitingInputReason: 'pending-approval', | ||
| approvalRequests: [{ id: approvalRequest.id }], | ||
| }); | ||
| } | ||
|
|
||
| private buildExecutedResult(): StepExecutionResult { | ||
| return this.buildOutcomeResult({ | ||
| status: 'success', | ||
| ...(this.hasApprovalOutcomes && { selectedOption: 'approved' as const }), | ||
| }); | ||
| } | ||
|
|
||
| // The server hands back the review of the request the step paused on. Until it is closed the | ||
| // step pauses again; once closed it takes the branch the reviewer chose. | ||
| private async replayReviewedApproval( | ||
| existing: TriggerRecordActionStepExecutionData, | ||
| approvalRequest: { id: string }, | ||
| ): Promise<StepExecutionResult> { | ||
| const review = this.context.approvalRequests?.find(({ id }) => id === approvalRequest.id); | ||
|
|
||
| if (!review) return this.buildApprovalPause(approvalRequest); | ||
|
|
||
| if (review.status === 'approved' || review.status === 'rejected') { | ||
| await this.context.runStore.saveStepExecution(this.context.runId, { | ||
| ...existing, | ||
| executionResult: { | ||
| ...(existing.executionResult as Exclude< | ||
| TriggerRecordActionStepExecutionData['executionResult'], | ||
| { skipped: true } | ||
| >), | ||
| approvalOutcome: review.status, | ||
| }, | ||
| }); | ||
|
|
||
| return this.buildOutcomeResult({ | ||
| status: 'success', | ||
| selectedOption: review.status, | ||
| approverId: review.approverId ?? null, | ||
| }); | ||
| } | ||
|
|
||
| if (review.status === 'failed') throw new ApprovalRequestFailedError(approvalRequest.id); | ||
| if (review.status === undefined) throw new ApprovalRequestGoneError(approvalRequest.id); | ||
|
|
||
| return this.buildApprovalPause(approvalRequest); | ||
| } | ||
|
|
||
| protected override async checkIdempotency(): Promise<StepExecutionResult | null> { | ||
| const existing = await this.findPendingExecution<TriggerRecordActionStepExecutionData>( | ||
| 'trigger-action', | ||
|
|
@@ -95,6 +163,18 @@ export default class TriggerRecordActionStepExecutor extends RecordStepExecutor< | |
| result && !('skipped' in result) && result.submissionOutcome === 'pending-approval'; | ||
| const approvalRequest = isPendingApproval ? result.approvalRequest : undefined; | ||
|
|
||
| if (isPendingApproval && this.hasApprovalOutcomes) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Claude Opus 5.5 (claude-opus-5-5): Preferential: a paused approval step keeps a CodePaused runs persist a A way out: record the decision with
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Claude Opus 5.5 (claude-opus-5-5): No ADR, the paused WhyA later pause phase can still read today's |
||
| if (!approvalRequest) { | ||
| throw new ApprovalRequestUntrackedError(existing.executionParams?.name ?? 'unknown'); | ||
| } | ||
|
|
||
| return this.replayReviewedApproval(existing, approvalRequest); | ||
| } | ||
|
|
||
| if (!isPendingApproval && result && !('skipped' in result)) { | ||
| return this.buildExecutedResult(); | ||
| } | ||
|
|
||
| if (isPendingApproval && !approvalRequest) { | ||
| this.context.logger( | ||
| 'Warn', | ||
|
|
@@ -275,6 +355,10 @@ export default class TriggerRecordActionStepExecutor extends RecordStepExecutor< | |
| form?: { fields: ActionFormField[]; aiFilledValues: AiFilledFormValue[] }, | ||
| fullAiFallback?: FullAiFallback, | ||
| ): Promise<StepExecutionResult> { | ||
| if (fullAiFallback && this.hasApprovalOutcomes) { | ||
| throw new ApprovalBranchesNeedFullAiError(target.name, fullAiFallback.reason); | ||
| } | ||
|
|
||
| await this.context.runStore.saveStepExecution(this.context.runId, { | ||
| type: 'trigger-action', | ||
| stepIndex: this.context.stepIndex, | ||
|
|
@@ -536,6 +620,12 @@ export default class TriggerRecordActionStepExecutor extends RecordStepExecutor< | |
| idempotencyPhase: 'done', | ||
| }); | ||
|
|
||
| if (this.hasApprovalOutcomes) { | ||
| if (!outcome.approvalRequest) throw new ApprovalRequestUntrackedError(name); | ||
|
|
||
| return this.buildApprovalPause(outcome.approvalRequest); | ||
| } | ||
|
|
||
| return this.buildOutcomeResult({ | ||
| status: 'success', | ||
| ...(outcome.approvalRequest && { approvalRequest: outcome.approvalRequest }), | ||
|
|
@@ -556,7 +646,7 @@ export default class TriggerRecordActionStepExecutor extends RecordStepExecutor< | |
| idempotencyPhase: 'done', | ||
| }); | ||
|
|
||
| return this.buildOutcomeResult({ status: 'success' }); | ||
| return this.buildExecutedResult(); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.