Skip to content

refactor(workflow-executor): workflow steps#1502

Open
Scra3 wants to merge 45 commits intofeat/prd-214-setup-workflow-executor-packagefrom
feat/trigger-action-step-executor
Open

refactor(workflow-executor): workflow steps#1502
Scra3 wants to merge 45 commits intofeat/prd-214-setup-workflow-executor-packagefrom
feat/trigger-action-step-executor

Conversation

@Scra3
Copy link
Member

@Scra3 Scra3 commented Mar 20, 2026

Summary

  • Extract ActionRef and FieldRef shared interfaces ({ name, displayName }) from inline types in TriggerActionStepExecutionData and UpdateRecordStepExecutionData
  • Replace FieldReadBase (private) with exported FieldRefFieldReadSuccess/FieldReadError now extend FieldRef
  • Rename actionDisplayName/actionNamedisplayName/name and fieldDisplayName/fieldNamedisplayName/name for consistency
  • Move resolveFieldName to handleFirstCall so the technical field name is stored in pendingUpdate at creation time (no re-resolution in resolveAndUpdate)
  • Add missing tests: resolveActionName not-found path, saveStepExecution not-called assertions on WorkflowExecutorError, trigger-action pending action in buildStepSummary
  • Export ActionRef, FieldRef, NoActionsError from index; update CLAUDE.md

Test plan

  • yarn workspace @forestadmin/workflow-executor test — 144 tests pass
  • yarn workspace @forestadmin/workflow-executor build — no errors
  • yarn workspace @forestadmin/workflow-executor lint — clean

🤖 Generated with Claude Code

Note

Add new workflow step executors for updating records, triggering actions, loading related records, and MCP tasks

  • Introduces four new step executors: UpdateRecordStepExecutor, TriggerRecordActionStepExecutor, LoadRelatedRecordStepExecutor, and McpTaskStepExecutor, each supporting automatic execution, user confirmation flow, and pending-data persistence.
  • Refactors BaseStepExecutor to centralize error handling in execute() (catching and converting errors to user-facing outcomes), add AI-assisted record/field/action selection helpers, schema caching, and a SafeAgentPort wrapper that normalizes unexpected errors into AgentPortError.
  • Renames AiTask* types to RecordTask* and adds a new mcp-task StepType; updates AgentPort method signatures to accept structured query objects instead of positional parameters.
  • Adds a Logger port and ConsoleLogger adapter; integrates optional logging into BaseStepExecutor and ExecutorHttpServer, which now returns a generic 'Internal server error' body instead of exposing error messages.
  • Replaces @langchain/core with @forestadmin/ai-proxy as the direct dependency for LangChain types across the package.
  • Risk: AgentPort interface is a breaking change — all implementations must be updated to use the new object-based query signatures.

Macroscope summarized 369a398.

alban bertolini and others added 25 commits March 20, 2026 09:30
…on flow

Implements PRD-219. Adds an executor for update-record steps with three
execution branches: automatic completion, awaiting user confirmation, and
re-entry after confirmation. Extracts shared record selection helpers from
ReadRecordStepExecutor into BaseStepExecutor for reuse.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…cordStepExecutor

Match on type + stepIndex only, then guard on toolConfirmationInterruption
presence separately. Removes redundant filter predicate.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…endingUpdate

The field stores the proposed field update (from AI or user-edited) pending
confirmation. "pendingUpdate" better describes the content than
"toolConfirmationInterruption" which described the mechanism.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…f error outcome

Missing pendingUpdate in Branch A is a bug (orchestrator/RunStore), not a
business case — throw WorkflowExecutorError instead of returning a graceful
error outcome. Also rename local variable interruption → execution.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… casting

Add { skipped: true } to the executionResult union type, removing the
unsafe `as unknown as` cast when the user rejects the update.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… traceability

Keep pendingUpdate in the saved execution data after both confirm and
reject — useful for audit trail (what was proposed vs what happened).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ecutor

Pass userInput as parameter to handleConfirmation() where it is already
narrowed, removing the `as { confirmed: boolean }` cast. Also move
resolveFieldName inside try-catch blocks so WorkflowExecutorError is
properly caught and returned as a status: 'error' outcome.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…prove coverage

- Extract resolveAndUpdate to deduplicate resolve+update+persist logic
- Extract buildOutcomeResult/buildSuccessResult/buildErrorResult helpers
- Move getCollectionSchema inside try-catch in confirmation flow
- Rename executionParams.fieldName to fieldDisplayName for consistency
- Add tests for resolveFieldName failure in both branches
- Add test for relationship field exclusion from update tool schema

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…dErrorResult wrappers

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ment executionResult

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…StepExecutionData

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…field resolution

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Move findField to BaseStepExecutor with displayName priority over fieldName
- Move buildOutcomeResult to BaseStepExecutor, use error !== undefined check
- Fix resolveAndUpdate try-catch scope (saveStepExecution outside try block)
- Fix ConditionStepExecutor catch-all to only catch WorkflowExecutorError
- Add pendingUpdate visibility in step summary
- Remove findField from types/record.ts and barrel export

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Condition steps handle BPMN routing, record-task steps operate on client
data. The previous "ai-task" name was misleading since both step types
use AI. Rename to "record-task" to reflect the actual distinction.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…Definition

Drop recordSourceStepId and remoteToolsSourceId — both were declared but
never read. allowedTools is kept as the orchestrator-provided tool filter.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Introduces StepType.ToolTask ('tool-task') and ToolTaskStepDefinition for
steps where the AI freely selects and executes a tool from allowedTools.
Moves allowedTools out of RecordTaskStepDefinition where it didn't belong.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…on type

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…AndUpdate arity

- Extract UpdateTarget interface {selectedRecordRef, fieldDisplayName, value} so
  resolveAndUpdate takes 2 params instead of 4 (addresses qlty reviewer comment)
- Collapse 3 uninitialized let declarations in handleFirstCall into one target object
- Fix missing runId in getAvailableRecordRefs and update-record saveStepExecution calls
- Strengthen test assertions to include runId as first arg

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ecutor

Code changes:
- Add JSDoc on buildOutcomeResult warning it is for record-task executors only
- Add runtime guard on userInput.type in handleConfirmation (WorkflowExecutorError
  if an unexpected type is received, guarding against future UserInput union extension)

New tests:
- buildStepSummary: pendingUpdate branch coverage (update-record step with
  pendingUpdate but no executionParams emits Pending: in AI context)
- handleConfirmation: stepIndex mismatch and missing pendingUpdate cases
- stepOutcome shape: type/stepId/stepIndex asserted on happy path
- unexpected userInput type: runtime guard verified
- findField fieldName fallback: update succeeds when AI returns raw fieldName
- schema caching: getCollectionSchema called once per collection in Branch B
- RunStore error propagation: all four saveStepExecution/getStepExecutions call sites

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…pletion→automaticExecution

- StepType.ToolTask → StepType.McpTask (wire value: 'tool-task' → 'mcp-task')
- ToolTaskStepDefinition → McpTaskStepDefinition
- mcpServerId: string[] → string (single ID)
- automaticCompletion → automaticExecution on both RecordTaskStepDefinition and McpTaskStepDefinition
- Remove TODO rename comments
- Update all tests accordingly

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…record-1'

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replaces the UserInput discriminated union with a plain boolean field
userConfirmed on ExecutionContext and PendingStepExecution. Since the
only user input type in practice is a boolean confirmation, the union
wrapper adds complexity with no benefit.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ion flow

Implements TriggerActionStepExecutor following the UpdateRecordStepExecutor
pattern (branches A/B/C, confirmation flow, automaticExecution).

- Add TriggerActionStepExecutionData type with executionParams (actionDisplayName
  + actionName), executionResult ({ success } | { skipped }), and pendingAction
- Add NoActionsError for collections with no actions
- Implement selectAction via AI tool with displayName enum and technical name hints
- resolveAndExecute stores the technical actionName in executionParams for
  traceability; action result discarded per privacy constraint
- Fix buildStepSummary in BaseStepExecutor to include trigger-action pendingAction
  in prior-step AI context (parity with update-record pendingUpdate)
- Export TriggerActionStepExecutor, TriggerActionStepExecutionData, NoActionsError

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…t and pendingAction

Resolve actionName once in handleFirstCall and store it in pendingAction,
so resolveAndExecute receives it directly via ActionTarget without re-fetching
the schema. Rename TriggerTarget → ActionTarget for consistency with English
naming conventions.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…f shapes to { name, displayName }

- Extract ActionRef { name, displayName } from inline types in TriggerActionStepExecutionData
- Extract FieldRef { name, displayName } replacing FieldReadBase in ReadRecord types
- UpdateRecordStepExecutionData executionParams/pendingUpdate now use FieldRef & { value }
- Rename actionDisplayName/actionName → displayName/name, fieldDisplayName/fieldName → displayName/name
- Move resolveFieldName to handleFirstCall (no re-resolution in resolveAndUpdate)
- Add missing tests: resolveActionName not-found path, saveStepExecution not-called assertions, trigger-action pendingAction in buildStepSummary
- Export ActionRef, FieldRef, NoActionsError from index; update CLAUDE.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@qltysh
Copy link

qltysh bot commented Mar 20, 2026

Qlty

Coverage Impact

Unable to calculate total coverage change because base branch coverage was not found.

Modified Files with Diff Coverage (18)

RatingFile% DiffUncovered Line #s
New file Coverage rating: A
packages/workflow-executor/src/executors/safe-agent-port.ts100.0%
New file Coverage rating: F
packages/workflow-executor/src/adapters/console-logger.ts50.0%5
New file Coverage rating: A
...ages/workflow-executor/src/executors/mcp-task-step-executor.ts100.0%
New file Coverage rating: A
...ow-executor/src/executors/load-related-record-step-executor.ts99.0%138
New file Coverage rating: A
...orkflow-executor/src/executors/summary/step-summary-builder.ts100.0%
New file Coverage rating: A
...s/workflow-executor/src/executors/read-record-step-executor.ts100.0%
New file Coverage rating: A
...ges/workflow-executor/src/executors/condition-step-executor.ts100.0%
New file Coverage rating: A
packages/workflow-executor/src/types/step-definition.ts100.0%
New file Coverage rating: A
packages/workflow-executor/src/executors/base-step-executor.ts100.0%
New file Coverage rating: A
packages/workflow-executor/src/http/executor-http-server.ts100.0%
New file Coverage rating: A
...s/workflow-executor/src/executors/record-task-step-executor.ts100.0%
New file Coverage rating: A
...workflow-executor/src/executors/update-record-step-executor.ts100.0%
New file Coverage rating: A
...-executor/src/executors/trigger-record-action-step-executor.ts100.0%
New file Coverage rating: A
...ages/workflow-executor/src/adapters/agent-client-agent-port.ts100.0%
New file Coverage rating: A
packages/workflow-executor/src/errors.ts100.0%
New file Coverage rating: A
packages/workflow-executor/src/index.ts100.0%
New file Coverage rating: A
...ow-executor/src/executors/summary/step-execution-formatters.ts100.0%
New file Coverage rating: A
packages/ai-proxy/src/index.ts100.0%
Total99.5%
🤖 Increase coverage with AI coding...

In the `feat/trigger-action-step-executor` branch, add test coverage for this new code:

- `packages/workflow-executor/src/adapters/console-logger.ts` -- Line 5
- `packages/workflow-executor/src/executors/load-related-record-step-executor.ts` -- Line 138

🚦 See full report on Qlty Cloud »

🛟 Help
  • Diff Coverage: Coverage for added or modified lines of code (excludes deleted files). Learn more.

  • Total Coverage: Coverage for the whole repository, calculated as the sum of all File Coverage. Learn more.

  • File Coverage: Covered Lines divided by Covered Lines plus Missed Lines. (Excludes non-executable lines including blank lines and comments.)

    • Indirect Changes: Changes to File Coverage for files that were not modified in this PR. Learn more.

alban bertolini and others added 3 commits March 20, 2026 12:50
…store display names in executionParams

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…rams for consistency

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…-executor

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
alban bertolini and others added 4 commits March 20, 2026 14:20
…y" wording

Use fieldNames instead of fieldDisplayNames and actionName instead of
displayName in tool schemas exposed to the AI, to avoid confusion between
property names and their semantics (values remain display names).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ize pending state

- Make buildOutcomeResult abstract on BaseStepExecutor; each branch owns its outcome shape
- Add RecordTaskStepExecutor intermediate class implementing buildOutcomeResult for 'record-task'
- Add pendingData to BaseStepExecutionData, replacing pendingUpdate/pendingAction per-type fields; each executor sets it when saving, base class reads it directly with no type discrimination
- Rename TriggerActionStepExecutor → TriggerRecordActionStepExecutor for naming consistency with ReadRecord/UpdateRecord

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…idate error handling

- Introduce doExecute() as the abstract hook; execute() in the base class wraps it
  with the single try-catch that converts WorkflowExecutorErrors to error outcomes
  and rethrows infrastructure errors — removing all try-catch boilerplate from subclasses
- Inline the former buildErrorOutcomeOrThrow helper directly into execute(), it no
  longer needs to be a named method
- Add StepPersistenceError (extends WorkflowExecutorError) for post-side-effect
  persistence failures; these are now consistently converted to error outcomes
- Extract handleConfirmationFlow into RecordTaskStepExecutor to DRY the
  confirmation pattern shared by update-record and trigger-record-action
- Split the !execution?.pendingData guard into two distinct WorkflowExecutorErrors
  for clearer debug messages
- Export BaseStepStatus from step-outcome; remove pendingData from BaseStepExecutionData
  (declared only on the concrete types that need it)
- Fix extractToolCallArgs to throw MalformedToolCallError when args is null/undefined

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…error hierarchy, and AI-first Branch C

- Add LoadRelatedRecordStepExecutor with Branch A/B/C confirmation flow
- Replace candidates[] in LoadRelatedRecordPendingData with AI-selected suggestedRecordId/suggestedFields/relatedCollectionName
- Extract selectBestFromRelatedData to share AI selection logic between Branch B and Branch C
- Make WorkflowExecutorError abstract; introduce InvalidAIResponseError, RelationNotFoundError, FieldNotFoundError, ActionNotFoundError, StepStateError
- Fix selectRelevantFields to use displayName in Zod enum and map back to fieldName
- Add getRelatedData fields param forwarding in AgentClientAgentPort
- Update CLAUDE.md with displayName-in-AI-tools and error hierarchy rules

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@qltysh
Copy link

qltysh bot commented Mar 21, 2026

6 new issues

Tool Category Rule Count
qlty Structure Function with high complexity (count = 18): execute 2
qlty Structure Deeply nested control flow (level = 4) 2
qlty Duplication Found 16 lines of similar code in 2 locations (mass = 92) 2

Comment on lines +1467 to +1469
expect(selectRecordTool.schema.shape.recordIdentifier.options).not.toContain(
expect.stringContaining('stepIndex: 3'),
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Low executors/load-related-record-step-executor.test.ts:1467

The assertion not.toContain(expect.stringContaining('stepIndex: 3')) at line 1467 always passes because stringContaining('stepIndex: 3') never matches the 'Step N - Collection #id' format used by select-record tool options (e.g., 'Step 2 - Orders #99'). This gives false confidence that pending executions without a record are excluded from the pool. Consider asserting against the actual identifier format, such as checking that the options array has length 2 and excludes 'Step 3 - Invoices #55'.

-      expect(selectRecordTool.schema.shape.recordIdentifier.options).not.toContain(
-        expect.stringContaining('stepIndex: 3'),
-      );
+      expect(selectRecordTool.schema.shape.recordIdentifier.options).toHaveLength(2);
+      expect(selectRecordTool.schema.shape.recordIdentifier.options).not.toContain(
+        'Step 3 - Invoices #55',
+      );
🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
In file packages/workflow-executor/test/executors/load-related-record-step-executor.test.ts around lines 1467-1469:

The assertion `not.toContain(expect.stringContaining('stepIndex: 3'))` at line 1467 always passes because `stringContaining('stepIndex: 3')` never matches the `'Step N - Collection #id'` format used by `select-record` tool options (e.g., `'Step 2 - Orders #99'`). This gives false confidence that pending executions without a `record` are excluded from the pool. Consider asserting against the actual identifier format, such as checking that the options array has length 2 and excludes `'Step 3 - Invoices #55'`.

Evidence trail:
1. Test assertion at packages/workflow-executor/test/executors/load-related-record-step-executor.test.ts:1467-1469 - shows `not.toContain(expect.stringContaining('stepIndex: 3'))`
2. Record identifier format at packages/workflow-executor/src/executors/base-step-executor.ts:228-233 - comment says 'Step X - CollectionDisplayName #id' and implementation shows: `Step ${record.stepIndex} - ${schema.collectionDisplayName} #${record.recordId}`
3. Test mock at line 1427: `{ recordIdentifier: 'Step 2 - Orders #99' }` confirms the actual format used
4. pendingExecution definition at lines 1388-1398 shows stepIndex: 3 with pendingData.displayName: 'Invoice'

alban bertolini and others added 8 commits March 21, 2026 13:33
…ser facing messages

Separates technical error messages (dev logs) from user-facing messages
(Forest Admin UI). WorkflowExecutorError now carries a readonly `userMessage`
field; base-step-executor uses it instead of `message` when building the
step outcome error. Each subclass declares its own user-oriented message.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…nject logger into execution context

Introduces a Logger interface (port) and ConsoleLogger (default implementation).
Adds logger to ExecutionContext, masks raw error messages from the HTTP API
(security), and logs unhandled HTTP errors with context instead.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…eLogger output

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ated-record executor, rename ids→id

- Add StepExecutionFormatters (static per-type formatters) and StepSummaryBuilder
  (orchestrates step summary for AI context); decouple formatting from BaseStepExecutor
- Load-related-record executionResult is now self-contained: { relation: RelationRef; record: RecordRef }
- Rename ids → id in AgentPort and all callers (id is a composite key of one record, not multiple records)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ve summary to sub-folder

- Replace QueryBase intersections with named query types (GetRecordQuery, UpdateRecordQuery,
  GetRelatedDataQuery, ExecuteActionQuery) for better DX and readability
- Save executeAction return value in executionResult.actionResult instead of discarding it
- Move StepSummaryBuilder and StepExecutionFormatters to executors/summary/ sub-folder

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…edData signature

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… into McpTaskStepExecutor

remoteTools was a dependency of McpTaskStepExecutor only — 5 of 6 executors never
used it. Inject it explicitly via the constructor so ExecutionContext stays focused
on execution state, and remove the now-meaningless remoteTools: [] boilerplate
from every other executor test.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…utor

Any WorkflowExecutorError with a cause is now logged automatically by the
base executor using error.message as the log message. Removes the manual
logger.error call from McpTaskStepExecutor and the StepPersistenceError-
specific guard. Moves cause? up to the base class, removing duplicate
declarations from StepPersistenceError and McpToolInvocationError.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Scra3 Scra3 changed the title refactor(workflow-executor): extract ActionRef/FieldRef, align Ref shapes refactor(workflow-executor): workflow steps Mar 23, 2026
alban bertolini and others added 5 commits March 23, 2026 10:27
Add AgentPortError and SafeAgentPort to centralise infra error handling
for all agentPort operations (getRecord, updateRecord, getRelatedData,
executeAction).

- add AgentPortError extending WorkflowExecutorError with a user-friendly
  message and structured cause logging
- add SafeAgentPort implementing AgentPort: wraps infra errors in
  AgentPortError, passes through WorkflowExecutorError subclasses unchanged
- expose this.agentPort (SafeAgentPort) as a protected property in
  BaseStepExecutor; all executors use it instead of this.context.agentPort
- export AgentPortError from index.ts
- add unit tests for SafeAgentPort and one integration test per executor
  verifying userMessage and logger.error cause on infra failures

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ask dependencies

Add McpToolRef, McpToolCall and McpTaskStepExecutionData to step-execution-data,
required by mcp-task-step-executor. Update package.json to depend on
@forestadmin/ai-proxy and align @langchain/core version.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Export RemoteTool default, BaseChatModel, BaseMessage, HumanMessage,
SystemMessage, StructuredToolInterface and DynamicStructuredTool so
consumers of ai-proxy do not need a direct @langchain/core dependency.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…restadmin/ai-proxy

Replace all direct @langchain/core imports in src and tests with
@forestadmin/ai-proxy re-exports. Add moduleNameMapper in jest.config.ts
to resolve @anthropic-ai/sdk wildcard exports (Jest < 30 workaround,
same pattern as ai-proxy).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@langchain/core is now consumed transitively via @forestadmin/ai-proxy.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant