1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (C) 2025 SyncMyOrders Sp. z o.o.
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Runtara Core - Durable Execution Engine
//!
//! This crate provides the execution engine for durable workflows. It manages checkpoints,
//! signals, and instance events, persisting all state to the database for crash resilience.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────────────┐
//! │ External Clients │
//! │ (runtara-management-sdk, CLI) │
//! └─────────────────────────────────────────────────────────────────────────┘
//! │
//! ▼
//! ┌─────────────────────────────────────────────────────────────────────────┐
//! │ runtara-environment │
//! │ (Image Registry, Instance Lifecycle, Wake Queue) │
//! │ Port 8002 │
//! └─────────────────────────────────────────────────────────────────────────┘
//! │ │
//! │ Shared Persistence │ Spawns
//! ▼ ▼
//! ┌───────────────────────┐ ┌─────────────────────────────┐
//! │ runtara-core │◄───────────────────│ Workflow Instances │
//! │ (This Crate) │ Instance Protocol │ (using runtara-sdk) │
//! │ Checkpoints/Signals │ │ │
//! │ Port 8001 │ └─────────────────────────────┘
//! └───────────────────────┘
//! │
//! ▼
//! ┌───────────────────────┐
//! │ PostgreSQL / SQLite │
//! │ (Durable Storage) │
//! └───────────────────────┘
//! ```
//!
//! # HTTP Server
//!
//! Core exposes one HTTP server:
//!
//! | Server | Port | Purpose |
//! |--------|------|---------|
//! | Instance Server | 8001 | Workflow instances connect here via runtara-sdk |
//!
//! Environment uses the shared `Persistence` trait directly instead of HTTP.
//!
//! # Instance Protocol (Port 8001)
//!
//! The instance protocol handles all communication between workflow instances and Core.
//! Instances use [`runtara-sdk`] which wraps this protocol.
//!
//! ## Operations
//!
//! | Operation | Description |
//! |-----------|-------------|
//! | `RegisterInstance` | Self-register on startup, optionally resume from checkpoint |
//! | `Checkpoint` | Save state (or return existing if checkpoint_id exists) + signal delivery |
//! | `GetCheckpoint` | Read-only checkpoint lookup |
//! | `Sleep` | Durable sleep - stores wake time in database |
//! | `InstanceEvent` | Fire-and-forget events (heartbeat, completed, failed, suspended) |
//! | `GetInstanceStatus` | Query instance status |
//! | `PollSignals` | Poll for pending cancel/pause/resume signals |
//! | `SignalAck` | Acknowledge receipt of a signal |
//!
//! ## Checkpoint Semantics
//!
//! The `Checkpoint` operation is the primary durability mechanism:
//!
//! 1. **First call with checkpoint_id**: Saves state, returns empty `existing_state`
//! 2. **Subsequent calls with same checkpoint_id**: Returns existing state (for resume)
//! 3. **Signal delivery**: Returns pending signals in response for efficient poll-free detection
//!
//! ## Durable Sleep
//!
//! The `Sleep` operation stores a `sleep_until` timestamp in the instances table.
//! Environment's wake scheduler polls for sleeping instances and relaunches them
//! when their wake time arrives. On resume, the SDK calculates remaining sleep time.
//!
//! # Instance Status State Machine
//!
//! ```text
//! ┌─────────┐
//! │ PENDING │
//! └────┬────┘
//! │ register
//! ▼
//! ┌─────────┐
//! ┌──────────│ RUNNING │──────────┐
//! │ └────┬────┘ │
//! │ │ │
//! pause│ sleep│ cancel
//! │ │ │
//! ▼ ▼ ▼
//! ┌──────────┐ ┌──────────┐ ┌───────────┐
//! │SUSPENDED │ │SUSPENDED │ │ CANCELLED │
//! └────┬─────┘ └────┬─────┘ └───────────┘
//! │ │
//! resume│ wake│
//! │ │
//! └───────┬───────┘
//! │
//! ▼
//! ┌─────────┐
//! │ RUNNING │──────────┬──────────┐
//! └─────────┘ │ │
//! complete fail
//! │ │
//! ▼ ▼
//! ┌───────────┐ ┌────────┐
//! │ COMPLETED │ │ FAILED │
//! └───────────┘ └────────┘
//! ```
//!
//! ## Status Descriptions
//!
//! | Status | Description |
//! |--------|-------------|
//! | `PENDING` | Instance created but not yet registered |
//! | `RUNNING` | Instance is actively executing |
//! | `SUSPENDED` | Instance paused (by signal) or sleeping (durable sleep) |
//! | `COMPLETED` | Instance finished successfully |
//! | `FAILED` | Instance failed with error |
//! | `CANCELLED` | Instance was cancelled via signal |
//!
//! # Configuration
//!
//! Configuration is loaded from environment variables:
//!
//! | Variable | Required | Default | Description |
//! |----------|----------|---------|-------------|
//! | `RUNTARA_DATABASE_URL` | Yes | - | PostgreSQL or SQLite connection string |
//! | `RUNTARA_HTTP_PORT` | No | `8001` | Instance HTTP server port |
//! | `RUNTARA_MAX_CONCURRENT_INSTANCES` | No | `32` | Maximum concurrent instances |
//!
//! # Modules
//!
//! - [`config`]: Server configuration from environment variables
//! - [`persistence`]: Database persistence layer for instances, checkpoints, events, signals
//! - [`error`]: Error types with RPC error code mapping
//! - [`instance_handlers`]: Instance protocol request handlers
//! - [`server`]: HTTP server implementation
/// Database migrations for runtara-core.
///
/// Products embedding runtara-core can use this module to run migrations:
///
/// ```ignore
/// use runtara_core::migrations;
///
/// let pool = PgPool::connect(&database_url).await?;
/// migrations::run_postgres(&pool).await?;
/// ```
/// Persistence layer for instances, checkpoints, events, and signals.
/// Error types for Core operations with RPC error code mapping.
/// Compensation framework for saga pattern support.
// Server-mode modules (require HTTP transport)
/// Server configuration loaded from environment variables.
/// Instance protocol handlers (registration, checkpoints, events, signals).
/// HTTP server implementation for the instance protocol.
/// Embeddable runtime for runtara-core.