-
Notifications
You must be signed in to change notification settings - Fork 0
Project Overview Core Concepts Workflow Orchestration Engine
Referenced Files in This Document
- workflow-activate.md
- workflow-forward-first-call.md
- workflow-forward-continue.md
- workflow-reward.md
- http-api-begin.ts
- http-api-update.ts
- tools/forward.ts
- tools/next.ts
- tools/reward.ts
- services/execution-trace-store.ts
- utils/audit-log-events.ts
- mcp-audit-emit.ts
- services/key-value-store-factory.ts
- services/key-value-store.ts
- services/redis-cache.ts
- services/concurrency-limit.ts
- services/metrics/registry.ts
- Introduction
- Project Structure
- Core Components
- Architecture Overview
- Detailed Component Analysis
- Dependency Analysis
- Performance Considerations
- Troubleshooting Guide
- Conclusion
- Appendices
This document explains the workflow orchestration engine’s lifecycle, state management, persistence and recovery, and operational patterns such as activate-forward-reward, conditional branching, error handling with retries, and audit logging. It also covers workflow definition formats, step execution context, parameter passing between steps, monitoring, examples, debugging techniques, performance optimization, concurrent execution, and scaling considerations.
The workflow orchestration spans HTTP endpoints, tool implementations, services for persistence and tracing, and utilities for auditing and metrics. The key areas are:
- HTTP API layer that exposes workflow operations (begin/update)
- Tool layer implementing forward, next, and reward semantics
- Persistence and tracing services for state and audit data
- Concurrency control and metrics instrumentation
graph TB
Client["Client"] --> HTTP["HTTP API<br/>begin, update"]
HTTP --> Tools["Tools<br/>forward, next, reward"]
Tools --> KV["Key-Value Store<br/>state persistence"]
Tools --> Trace["Execution Trace Store<br/>step traces"]
Tools --> Audit["Audit Emit<br/>MCP audit events"]
Tools --> Metrics["Metrics Registry<br/>counters/histograms"]
KV --> Redis["Redis Cache"]
Diagram sources
- http-api-begin.ts
- http-api-update.ts
- tools/forward.ts
- tools/next.ts
- tools/reward.ts
- services/key-value-store-factory.ts
- services/key-value-store.ts
- services/redis-cache.ts
- services/execution-trace-store.ts
- mcp-audit-emit.ts
- services/metrics/registry.ts
Section sources
- http-api-begin.ts
- http-api-update.ts
- tools/forward.ts
- tools/next.ts
- tools/reward.ts
- services/key-value-store-factory.ts
- services/key-value-store.ts
- services/redis-cache.ts
- services/execution-trace-store.ts
- mcp-audit-emit.ts
- services/metrics/registry.ts
- HTTP API Layer
- begin: Initializes a new workflow run and returns initial state or first step instructions.
- update: Advances workflow state by applying user input or system actions to move from one step to the next.
- Tools Layer
- forward: Executes the current step, resolves parameters, invokes adapters/tools, and persists outputs.
- next: Determines the next step based on current state and conditions; supports branching.
- reward: Records evaluation signals and propagates feedback into the system.
- Persistence and Tracing
- Key-Value Store: Durable state storage for workflow runs and step contexts.
- Execution Trace Store: Captures per-step execution details for observability and debugging.
- Auditing and Metrics
- MCP Audit Emit: Emits structured audit events for compliance and traceability.
- Metrics Registry: Exposes counters and histograms for monitoring.
Section sources
- http-api-begin.ts
- http-api-update.ts
- tools/forward.ts
- tools/next.ts
- tools/reward.ts
- services/key-value-store-factory.ts
- services/key-value-store.ts
- services/execution-trace-store.ts
- mcp-audit-emit.ts
- services/metrics/registry.ts
The orchestration follows an event-driven, state-machine-like flow:
- Activate begins a run and establishes initial context.
- Forward executes the active step, producing outputs and side effects.
- Next computes the subsequent step using conditions and previous outputs.
- Reward records evaluation outcomes and may influence future routing or scoring.
sequenceDiagram
participant C as "Client"
participant H as "HTTP API"
participant F as "Forward Tool"
participant N as "Next Tool"
participant R as "Reward Tool"
participant KV as "Key-Value Store"
participant T as "Trace Store"
participant A as "Audit Emit"
participant M as "Metrics"
C->>H : "Begin workflow"
H-->>C : "Initial state / first step"
C->>H : "Update with inputs"
H->>F : "Execute current step"
F->>KV : "Persist step context"
F->>T : "Record execution trace"
F->>A : "Emit audit event"
F->>M : "Increment metrics"
F-->>H : "Step result"
H->>N : "Compute next step"
N->>KV : "Read state"
N-->>H : "Next step info"
H-->>C : "Updated state / next action"
C->>R : "Submit reward signal"
R->>KV : "Persist reward"
R->>A : "Emit audit event"
R->>M : "Increment metrics"
R-->>C : "Acknowledgement"
Diagram sources
- http-api-begin.ts
- http-api-update.ts
- tools/forward.ts
- tools/next.ts
- tools/reward.ts
- services/key-value-store-factory.ts
- services/key-value-store.ts
- services/execution-trace-store.ts
- mcp-audit-emit.ts
- services/metrics/registry.ts
- States include initialization, active step execution, waiting for user/system input, completion, and terminal error states.
- Transitions are driven by update calls that apply inputs and compute the next step via the next tool.
- State is persisted after each transition to ensure durability and recoverability.
Practical example references:
Section sources
- Begin initializes a new run, sets up context, and returns the first actionable step.
- The client then proceeds with forward calls to execute steps.
sequenceDiagram
participant C as "Client"
participant H as "HTTP API"
participant KV as "Key-Value Store"
participant A as "Audit Emit"
C->>H : "Begin"
H->>KV : "Create run context"
H->>A : "Emit activation audit event"
H-->>C : "Run ID + first step"
Diagram sources
Section sources
- Forward executes the current step, resolves parameters, invokes adapters/tools, and persists outputs.
- Supports retry logic and error handling, emitting audit events and metrics.
flowchart TD
Start(["Forward Entry"]) --> LoadState["Load current step state"]
LoadState --> ResolveParams["Resolve parameters from context"]
ResolveParams --> InvokeStep["Invoke adapter/tool"]
InvokeStep --> Success{"Success?"}
Success --> |Yes| PersistOutput["Persist outputs and metadata"]
PersistOutput --> EmitAudit["Emit audit event"]
EmitAudit --> UpdateMetrics["Update metrics"]
UpdateMetrics --> ReturnResult["Return step result"]
Success --> |No| HandleError["Handle error and decide retry"]
HandleError --> RetryCheck{"Retry allowed?"}
RetryCheck --> |Yes| Backoff["Apply backoff and schedule retry"]
Backoff --> LoadState
RetryCheck --> |No| FailStep["Mark step failed and persist"]
FailStep --> EmitAudit
EmitAudit --> ReturnError["Return error response"]
Diagram sources
- tools/forward.ts
- services/key-value-store-factory.ts
- services/key-value-store.ts
- services/execution-trace-store.ts
- mcp-audit-emit.ts
- services/metrics/registry.ts
Section sources
- tools/forward.ts
- services/key-value-store-factory.ts
- services/key-value-store.ts
- services/execution-trace-store.ts
- mcp-audit-emit.ts
- services/metrics/registry.ts
- Next evaluates conditions based on current state and previous outputs to determine the next step.
- Supports multiple branches and decision trees defined in workflow definitions.
flowchart TD
Enter(["Next Entry"]) --> ReadState["Read persisted state"]
ReadState --> EvaluateConditions["Evaluate branch conditions"]
EvaluateConditions --> HasBranch{"Branch found?"}
HasBranch --> |Yes| SelectNext["Select next step and build context"]
SelectNext --> PersistTransition["Persist transition metadata"]
PersistTransition --> ReturnNext["Return next step info"]
HasBranch --> |No| Terminal["Mark workflow terminal or error"]
Terminal --> PersistTerminal["Persist terminal state"]
PersistTerminal --> ReturnTerminal["Return terminal status"]
Diagram sources
Section sources
- Reward records evaluation signals, persists them, emits audit events, and updates metrics.
- Can influence future routing or scoring depending on configuration.
sequenceDiagram
participant C as "Client"
participant H as "HTTP API"
participant R as "Reward Tool"
participant KV as "Key-Value Store"
participant A as "Audit Emit"
participant M as "Metrics"
C->>H : "Submit reward"
H->>R : "Process reward"
R->>KV : "Persist reward record"
R->>A : "Emit audit event"
R->>M : "Update reward metrics"
R-->>H : "Acknowledgement"
H-->>C : "Reward accepted"
Diagram sources
- tools/reward.ts
- services/key-value-store-factory.ts
- services/key-value-store.ts
- mcp-audit-emit.ts
- services/metrics/registry.ts
Section sources
- tools/reward.ts
- services/key-value-store-factory.ts
- services/key-value-store.ts
- mcp-audit-emit.ts
- services/metrics/registry.ts
- Errors during step execution are captured and logged.
- Retry policies can be applied with exponential backoff and maximum attempts.
- Failed transitions are persisted to allow inspection and recovery.
flowchart TD
Start(["Step Execution"]) --> TryInvoke["Try invoke step"]
TryInvoke --> Ok{"Invocation OK?"}
Ok --> |Yes| Complete["Complete successfully"]
Ok --> |No| Classify["Classify error type"]
Classify --> Retryable{"Retryable?"}
Retryable --> |Yes| Schedule["Schedule retry with backoff"]
Schedule --> Wait["Wait until next attempt"]
Wait --> TryInvoke
Retryable --> |No| Fail["Fail step and persist error"]
Fail --> End(["Exit with error"])
Complete --> End
[No sources needed since this diagram shows conceptual workflow, not actual code structure]
- Structured audit events are emitted for activation, step execution, rewards, and errors.
- Events are persisted and queryable for compliance and debugging.
Section sources
- Metrics registry provides counters and histograms for operational visibility.
- Execution traces capture detailed per-step information for diagnostics.
Section sources
The following diagram maps core dependencies among components:
graph LR
HTTP_API["HTTP API<br/>begin, update"] --> TOOLS["Tools<br/>forward, next, reward"]
TOOLS --> KV_STORE["Key-Value Store"]
KV_STORE --> REDIS["Redis Cache"]
TOOLS --> TRACE["Execution Trace Store"]
TOOLS --> AUDIT["Audit Emit"]
TOOLS --> METRICS["Metrics Registry"]
Diagram sources
- http-api-begin.ts
- http-api-update.ts
- tools/forward.ts
- tools/next.ts
- tools/reward.ts
- services/key-value-store-factory.ts
- services/key-value-store.ts
- services/redis-cache.ts
- services/execution-trace-store.ts
- mcp-audit-emit.ts
- services/metrics/registry.ts
Section sources
- http-api-begin.ts
- http-api-update.ts
- tools/forward.ts
- tools/next.ts
- tools/reward.ts
- services/key-value-store-factory.ts
- services/key-value-store.ts
- services/redis-cache.ts
- services/execution-trace-store.ts
- mcp-audit-emit.ts
- services/metrics/registry.ts
- Concurrency Control
- Use concurrency limits to prevent overload when invoking external tools or adapters.
- Caching
- Leverage Redis-backed cache for frequently accessed state and results to reduce latency.
- Metrics and Tracing
- Instrument hot paths with metrics and traces to identify bottlenecks.
- Batch Operations
- Where possible, batch writes to persistence layers to reduce overhead.
Section sources
- services/concurrency-limit.ts
- services/redis-cache.ts
- services/metrics/registry.ts
- services/execution-trace-store.ts
- Inspect execution traces for step-level details and failures.
- Review audit logs for compliance and timeline reconstruction.
- Check metrics for error rates, latency spikes, and throughput anomalies.
- Validate state persistence integrity by querying the key-value store.
Section sources
- services/execution-trace-store.ts
- mcp-audit-emit.ts
- services/metrics/registry.ts
- services/key-value-store.ts
The workflow orchestration engine provides a robust, observable, and auditable framework for executing multi-step workflows. Its design emphasizes durable state management, clear separation of concerns across HTTP, tools, and services, and comprehensive monitoring and auditing capabilities. By leveraging conditional branching, retry logic, and concurrency controls, it supports complex workflows at scale while maintaining reliability and transparency.
- Definitions describe steps, conditions, parameters, and outputs.
- Refer to architecture docs for concrete examples and schemas.
Section sources
- Context includes run identifiers, previous outputs, and environment variables.
- Parameters are resolved before invocation and passed to adapters/tools.
Section sources
- Activation: See activation documentation for end-to-end setup.
- Forward first call: Understand initial step execution and output shaping.
- Forward continue: Learn how subsequent steps consume prior outputs.
- Reward: Explore recording evaluations and their impact.
Section sources
- Enable detailed traces and audit events for problematic runs.
- Correlate metrics with specific time windows to isolate issues.
- Use run IDs to track state transitions across components.
Section sources
- Horizontal scaling: Stateless HTTP API with shared persistence and caching.
- Queue-based retries: Offload long-running steps to background workers if needed.
- Partitioning: Distribute runs across namespaces or spaces to reduce contention.
[No sources needed since this section provides general guidance]
-
- Authentication and Authorization Model
- Model Context Protocol (MCP) Fundamentals
- Tool and Adapter System
- Memory and Semantic Search System
- Workflow Orchestration Engine