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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//! `Engine` runtime package-load seam: live load, routing, listing, unload.
//!
//! Decision record (#62, adopted 2026-06-12): D1 always-latest-at-record-time
//! with durable pinning, D2 manual unload with engine-enforced safety checks,
//! D3 embedded-only API with serde-ready types (the server endpoint is a
//! follow-up brief), D4 required `package_version` on start events.
use aion_core::Event;
use aion_package::ContentHash;
use crate::loader::{LoadedWorkflow, WorkflowVersionInfo};
use crate::{EngineError, WorkflowCatalog};
use super::api::Engine;
use super::builder::{WorkflowPackageSource, package_from_source};
impl Engine {
/// Loads a validated package into the running engine and atomically
/// routes its workflow type's new dispatches to it.
///
/// Every start that resolved before the route flip completes on the
/// version it resolved (loads never unregister anything); every start
/// after this call returns resolves the new version. Re-loading an
/// already-loaded hash is idempotent (nothing registers) but still
/// re-points the route at it — re-deploying a previously rolled-back
/// version must take effect.
///
/// # Errors
///
/// Returns [`EngineError::ShuttingDown`] once shutdown begins, and
/// [`EngineError::Load`] for archive, collision, registration, or
/// entry-verification failures. On failure the catalog is untouched:
/// routing, loaded versions, and in-flight dispatches are unaffected.
pub async fn load_package(
&self,
source: impl Into<WorkflowPackageSource>,
) -> Result<LoadedWorkflow, EngineError> {
// A load is new-work admission, not a wind-down operation: refuse
// after shutdown begins so modules never register into a dying VM.
let operation = self.shutdown_gate.begin_start()?;
let result = async {
let package = package_from_source(source.into())?;
self.workflow_catalog()
.load_package(self.runtime(), &package)
.await
}
.await;
drop(operation);
result
}
/// Lists every loaded workflow version with its routing flag, sorted by
/// `(workflow_type, loaded_at)`.
///
/// # Errors
///
/// Returns [`EngineError::CatalogPoisoned`] when the catalog lock is poisoned.
pub fn list_workflow_versions(&self) -> Result<Vec<WorkflowVersionInfo>, EngineError> {
self.workflow_catalog().versions()
}
/// Re-points routing for `workflow_type` at an already-loaded version
/// (rollback / roll-forward). Atomic and idempotent.
///
/// # Errors
///
/// Returns [`EngineError::ShuttingDown`] once shutdown begins, and
/// [`EngineError::Load`] naming the loaded set when `(type, version)` is
/// not loaded — routing to a never-loaded hash is impossible.
pub async fn route_workflow_version(
&self,
workflow_type: &str,
version: &ContentHash,
) -> Result<(), EngineError> {
let operation = self.shutdown_gate.begin_operation()?;
let result = self
.workflow_catalog()
.route_version(workflow_type, version)
.await;
drop(operation);
result
}
/// Unloads a workflow version after verifying nothing pins it (D2).
///
/// Refusal conditions, each typed and naming what pins the version:
/// route-inactive is required (the route-active version of a type can
/// never be unloaded), no in-flight start may pin it, no live registry
/// handle may run on it, and no recoverable instance in the store —
/// including a recorded-but-never-started child — may be pinned to it.
///
/// The engine owns the mechanism; the embedding platform owns *when* to
/// unload. There is no automatic garbage collection.
///
/// # Errors
///
/// Returns [`EngineError::ShuttingDown`] once shutdown begins,
/// [`EngineError::Load`] for refusals (with the catalog restored
/// untouched), and [`EngineError::Runtime`] when module unregistration
/// fails after the catalog commit.
pub async fn unload_workflow_version(
&self,
workflow_type: &str,
version: &ContentHash,
) -> Result<(), EngineError> {
let operation = self.shutdown_gate.begin_operation()?;
let result = self
.unload_workflow_version_inner(workflow_type, version)
.await;
drop(operation);
result
}
async fn unload_workflow_version_inner(
&self,
workflow_type: &str,
version: &ContentHash,
) -> Result<(), EngineError> {
let catalog = self.workflow_catalog();
let _mutation = catalog.begin_mutation().await;
// Swap the version out FIRST: from this instant no new resolution can
// produce it, so the checks below cannot be invalidated by a racing
// start (a start that already resolved holds a pin and is detected).
let removed = catalog.swap_out_version(workflow_type, version)?;
if let Err(error) = self
.verify_unload_unpinned(catalog, workflow_type, version)
.await
{
catalog.restore_version(removed)?;
return Err(error);
}
self.unregister_unloaded_modules(workflow_type, version, &removed)
}
/// Verifies no in-flight start, resident handle, or recoverable instance
/// pins `(type, version)`.
async fn verify_unload_unpinned(
&self,
catalog: &WorkflowCatalog,
workflow_type: &str,
version: &ContentHash,
) -> Result<(), EngineError> {
if catalog.has_pinned_starts(workflow_type, version)? {
return Err(EngineError::Load {
reason: format!(
"cannot unload workflow `{workflow_type}` version `{version}`: an in-flight start is pinned to it"
),
});
}
for handle in self.registry().list()? {
if handle.workflow_type() == workflow_type
&& handle.loaded_version() == version
&& !handle.cached_status().is_terminal()
{
return Err(EngineError::Load {
reason: format!(
"cannot unload workflow `{workflow_type}` version `{version}`: live run `{}/{}` is pinned to it",
handle.workflow_id(),
handle.run_id()
),
});
}
}
let recorded = crate::loader::package_version_of(version);
let store = self.store();
for workflow_id in store.list_active().await? {
let history = store.read_history(&workflow_id).await?;
let current_run_pin = history.iter().rev().find_map(|event| match event {
Event::WorkflowStarted {
workflow_type: started_type,
package_version,
..
} => Some(started_type == workflow_type && package_version == &recorded),
_ => None,
});
if current_run_pin == Some(true) {
return Err(EngineError::Load {
reason: format!(
"cannot unload workflow `{workflow_type}` version `{version}`: recoverable run `{workflow_id}` is pinned to it"
),
});
}
if let Some(child_workflow_id) = self
.recorded_unstarted_child_pin(&history, workflow_type, &recorded)
.await?
{
return Err(EngineError::Load {
reason: format!(
"cannot unload workflow `{workflow_type}` version `{version}`: child `{child_workflow_id}` recorded by `{workflow_id}` is pinned to it and has not started"
),
});
}
}
Ok(())
}
/// A recorded-but-never-started child pinned to the target version, if
/// any: its `ChildWorkflowStarted` carries the version and its own
/// history is still empty, so the crash-repair sweep would have to start
/// it on exactly this version.
async fn recorded_unstarted_child_pin(
&self,
parent_history: &[Event],
workflow_type: &str,
recorded: &aion_core::PackageVersion,
) -> Result<Option<aion_core::WorkflowId>, EngineError> {
let store = self.store();
for event in parent_history {
let Event::ChildWorkflowStarted {
child_workflow_id,
workflow_type: child_type,
package_version,
..
} = event
else {
continue;
};
if child_type != workflow_type || package_version != recorded {
continue;
}
if store.read_history(child_workflow_id).await?.is_empty() {
return Ok(Some(child_workflow_id.clone()));
}
}
Ok(None)
}
/// Unregisters the removed version's modules from the runtime, skipping
/// host NIF modules that were never BEAM-registered.
fn unregister_unloaded_modules(
&self,
workflow_type: &str,
version: &ContentHash,
removed: &crate::loader::catalog::RemovedVersion,
) -> Result<(), EngineError> {
let nif_modules = self.runtime().registered_nif_modules();
let mut failures = Vec::new();
for deployed_name in removed.module_names() {
let original = deployed_name.split('$').next().unwrap_or(deployed_name);
if nif_modules.iter().any(|name| name == original) {
continue;
}
if let Err(error) = self.runtime().unregister_module(deployed_name) {
failures.push(format!("{deployed_name}: {error}"));
}
}
if failures.is_empty() {
Ok(())
} else {
// The catalog commit stands: the version is unloaded and its
// names are unreachable (content-hash unique), but the runtime
// retains orphaned module entries.
Err(EngineError::Runtime {
reason: format!(
"workflow `{workflow_type}` version `{version}` was removed from the catalog but module unregistration failed for {}",
failures.join(", ")
),
})
}
}
}