aion-server 0.30.0

Aion workflow server library: HTTP, gRPC, WebSocket, and worker endpoints. Run it with the `aion` binary from the aion-cli crate.
Documentation
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Standing a built-in agent worker up because a deployed document asked for
//! one — and saying so, on every arm, including the arms that mint nothing.
//!
//! # The rules, in order
//!
//! 1. **No `harness` section, no record.** Auto-provision keys on the section,
//!    never on deploys generally — and the rule is enforced in BOTH directions:
//!    a redeploy that removed the section (or renamed the queue) retires the
//!    record this server minted for it, rather than leaving a worker alive on
//!    a launch the document no longer declares.
//! 2. **An operator's record wins.** If a record whose name is not `auto/…`
//!    could serve the queue on this node in this namespace, nothing is minted,
//!    nothing is replaced, and the skip says which record won.
//! 3. **A dark outbox is a REFUSAL, not a mint.** Without a liminal worker
//!    listener a record would dial nothing forever; the refusal names the
//!    `[outbox]` settings that change it.
//! 4. **A redeploy RE-MINTS.** A record replays its argv verbatim, so a new
//!    document is a new argv, and the worker is restarted onto it. A redeploy
//!    of identical bytes resolves to the identical argv and the running worker
//!    is left alone.
//! 5. **An operator's STOP is preserved.** A record they stopped stays stopped
//!    across a redeploy: the desired state carries forward, and the outcome
//!    says the queue is unserved by the operator's own decision.
//!
//! # It never claims a worker it did not start
//!
//! Every arm that reports a running worker does so only after a convergence
//! RETURNED success. A record written durably onto a server with no
//! `[worker_supervision]` policy is [`AutoWorkerDecision::RecordedNotRunning`]
//! — the durable record and the live process disagreeing while the record
//! looks healthy is the whole defect this lane exists to end, and a status
//! surface that claimed otherwise would rebuild it one layer up.

use std::path::Path;

use aion_awl::{CompiledHarness, CompiledHarnessKind, CwdSource};
use aion_core::{ClusterEvent, PutOutcome};
use aion_package::AwlSource;
use aion_store::{DesiredState, WorkerDeployment, WorkerDeploymentListing};

use crate::ServerState;
use crate::worker::supervisor::{Convergence, converge_and_report};

use super::documents;
use super::listener::worker_dial_address;
use super::narrate;
use super::outcome::{AutoWorkerDecision, AutoWorkerOutcome};
use super::queues::{self, HarnessQueue};
use super::record;
use super::retire;

/// The operation label every auto-provision log line carries.
pub(super) const OPERATION: &str = "worker.auto_provision";

/// Provision a built-in agent worker for every queue `awl` declares one on,
/// and retire the records this workflow type's earlier deploys left behind.
///
/// Never fails: a server whose auto-provision could not run is a server whose
/// queue is unserved, not a server that must refuse the deploy that already
/// succeeded. Every arm returns an outcome carrying its own account, and the
/// outcomes are also recorded on the supervisor so the managed-worker surface
/// can answer "why is nothing serving this queue" later, not just at deploy.
pub async fn provision(
    state: &ServerState,
    root: &Path,
    awl: &AwlSource,
    workflow_type: &str,
) -> Vec<AutoWorkerOutcome> {
    let queues = match queues::harness_queues(awl.document()) {
        Ok(queues) => queues,
        Err(error) => return finish(state, vec![document_failure(&error, workflow_type)]),
    };
    let listing = match state
        .worker_deployment_store()
        .list_worker_deployments()
        .await
    {
        Ok(listing) => listing,
        Err(error) => {
            return finish(
                state,
                vec![AutoWorkerOutcome::document_level(
                    workflow_type,
                    format!(
                        "the durable worker deployments could not be listed, so this server \
                         cannot tell what already serves the queues `{workflow_type}` declares, \
                         and provisioned nothing: {error}"
                    ),
                )],
            );
        }
    };

    let mut outcomes = Vec::with_capacity(queues.len().saturating_add(1));
    for queue in &queues {
        outcomes.push(provision_queue(state, root, awl, workflow_type, queue, &listing).await);
    }
    // Rule 1, the other direction. Run even when `queues` is empty — that IS
    // the "the harness section was removed" case, and it is the one where a
    // worker is otherwise left alive replaying a launch nobody declares.
    outcomes.extend(retire::withdraw_undeclared(state, workflow_type, &queues, &listing).await);
    retire::prune_snapshots(state, root).await;
    finish(state, outcomes)
}

/// Log every outcome, record it on the supervisor, and hand it back.
fn finish(state: &ServerState, outcomes: Vec<AutoWorkerOutcome>) -> Vec<AutoWorkerOutcome> {
    for outcome in &outcomes {
        log_outcome(outcome);
    }
    state.worker_supervisor().record_auto_provision(&outcomes);
    outcomes
}

/// A harness-queue read failure, attributed to the queue when it named one.
fn document_failure(error: &queues::HarnessQueueError, workflow_type: &str) -> AutoWorkerOutcome {
    match error {
        queues::HarnessQueueError::Parse { .. } => {
            AutoWorkerOutcome::document_level(workflow_type, error.to_string())
        }
        queues::HarnessQueueError::Harness { task_queue, .. }
        | queues::HarnessQueueError::NoServiceableAction { task_queue, .. } => {
            AutoWorkerOutcome::new(
                task_queue.clone(),
                workflow_type,
                AutoWorkerDecision::Failed,
                None,
                error.to_string(),
            )
        }
    }
}

/// The decision for one declaring queue.
async fn provision_queue(
    state: &ServerState,
    root: &Path,
    awl: &AwlSource,
    workflow_type: &str,
    queue: &HarnessQueue,
    listing: &WorkerDeploymentListing,
) -> AutoWorkerOutcome {
    let task_queue = queue.task_queue.as_str();
    let name = record::auto_name(task_queue);
    let namespace = state.runtime_config().default_namespace.clone();
    let node = state.cluster_self_node();

    if let Some(skip) = operator_record_skip(listing, task_queue, &namespace, node, workflow_type) {
        return skip;
    }

    // The most specific refusal first: a harness naming paths this host does
    // not have would mint a worker that fails every dispatch routed to it
    // (measured 2026-08-26 — a fleet document deployed cross-host stood two
    // such workers up on the server's own machine, and one in three dispatches
    // on those queues burned an attempt on a spawn that could only fail).
    if let Some(why) = harness_unrunnable_here(&queue.harness) {
        return narrate::unrunnable_harness(task_queue, workflow_type, &why);
    }

    let dial = match worker_dial_address(&state.runtime_config().outbox) {
        Ok(dial) => dial,
        Err(dark) => return narrate::dark_outbox(task_queue, workflow_type, &dark),
    };

    let staged = match documents::stage(root, awl, workflow_type) {
        Ok(staged) => staged,
        Err(error) => {
            return narrate::failure(
                task_queue,
                workflow_type,
                format!(
                    "the deployed document could not be staged for a built-in agent worker on \
                     `{task_queue}`, so nothing was minted: {error}"
                ),
            );
        }
    };
    let binary = match crate::worker::capture_binary_identity() {
        Ok(binary) => binary,
        Err(error) => {
            return narrate::failure(
                task_queue,
                workflow_type,
                format!(
                    "the running server executable could not be identified, so no built-in agent \
                     worker was minted for `{task_queue}`: {error}"
                ),
            );
        }
    };

    let current = listing
        .deployments
        .iter()
        .find(|record| record.name == name);
    // An operator's STOP is durable intent and survives a redeploy. Only a
    // record that does not yet exist is minted `Running`.
    let desired = current.map_or(DesiredState::Running, |record| record.desired);
    let mut requested = record::new_deployment(task_queue, &staged.path, &dial, &namespace, binary);
    requested.desired = desired;

    // An identical redeploy must not restart a healthy worker. The document is
    // content-addressed, so "same document" is exactly "same argv", and that is
    // the whole test — no durable write, no cluster event, no replacement.
    if let Some(current) = current
        && current.artifact == requested.artifact
        && current.task_queue == requested.task_queue
        && current.namespaces == requested.namespaces
    {
        let converged = converge(state, &name, Convergence::Idempotent).await;
        return narrate::unchanged(
            &narrate::Subject {
                task_queue,
                workflow_type,
                name: &name,
                namespace: &namespace,
                document: &staged.path,
            },
            desired,
            converged,
        );
    }

    mint(
        state,
        MintRequest {
            subject: narrate::Subject {
                task_queue,
                workflow_type,
                name: &name,
                namespace: &namespace,
                document: &staged.path,
            },
            dial: &dial,
        },
        requested,
    )
    .await
}

/// Everything the mint's own reporting needs, held together so the write and
/// the sentence describing it cannot disagree about what was written.
struct MintRequest<'a> {
    subject: narrate::Subject<'a>,
    dial: &'a str,
}

/// Write the record and converge this node onto it.
async fn mint(
    state: &ServerState,
    request: MintRequest<'_>,
    requested: aion_store::NewWorkerDeployment,
) -> AutoWorkerOutcome {
    let stopped = requested.desired == DesiredState::Stopped;
    let record = match WorkerDeployment::new(requested, chrono::Utc::now()) {
        Ok(record) => record,
        Err(error) => {
            return narrate::failure(
                request.subject.task_queue,
                request.subject.workflow_type,
                format!(
                    "the auto worker-deployment record for `{}` is invalid: {error}",
                    request.subject.task_queue
                ),
            );
        }
    };
    let result = match state
        .worker_deployment_store()
        .put_worker_deployment(record)
        .await
    {
        Ok(result) => result,
        Err(error) => {
            return narrate::failure(
                request.subject.task_queue,
                request.subject.workflow_type,
                format!(
                    "the auto worker-deployment record for `{}` could not be persisted: {error}",
                    request.subject.task_queue
                ),
            );
        }
    };
    publish_put(state, &result);

    // A replaced record rewrote the argv a live worker replays verbatim, so
    // the running process is serving the SUPERSEDED document and must be
    // replaced — that is what "re-minting is the fix" means mechanically.
    let mode = match result.outcome {
        PutOutcome::Created => Convergence::Idempotent,
        PutOutcome::Replaced => Convergence::Replacing,
    };
    let converged = converge(state, request.subject.name, mode).await;
    // The decision is chosen HERE, from the put outcome joined with whether the
    // convergence actually succeeded, so the sentence below can only repeat a
    // fact rather than assert one.
    let decision = match (stopped, result.outcome, converged) {
        (true, _, _) => AutoWorkerDecision::OperatorStopped,
        (false, _, false) => AutoWorkerDecision::RecordedNotRunning,
        (false, PutOutcome::Created, true) => AutoWorkerDecision::Minted,
        (false, PutOutcome::Replaced, true) => AutoWorkerDecision::Reminted,
    };
    narrate::written(&request.subject, decision, request.dial)
}

/// The operator's own record for this queue, if one could actually serve it
/// HERE — the whole of rule 2, held apart so the skip and the mint cannot grow
/// into one another.
///
/// Node and namespace are part of the question, not decoration. A record pinned
/// to another node, or bound to namespaces this server's default is not in,
/// serves this queue somewhere else; treating it as a winner would leave the
/// queue unserved on this box with an outcome saying it was served.
fn operator_record_skip(
    listing: &WorkerDeploymentListing,
    task_queue: &str,
    namespace: &str,
    node: Option<&str>,
    workflow_type: &str,
) -> Option<AutoWorkerOutcome> {
    let operator = listing.deployments.iter().find(|record| {
        record.task_queue == task_queue
            && !record::is_auto_name(&record.name)
            && record.namespaces.contains(namespace)
            && match record.node.as_deref() {
                None => true,
                Some(pinned) => node == Some(pinned),
            }
    })?;
    Some(AutoWorkerOutcome::new(
        task_queue,
        workflow_type,
        AutoWorkerDecision::OperatorRecord,
        Some(operator.name.clone()),
        format!(
            "worker deployment `{}` already serves task queue `{task_queue}` in namespace \
             `{namespace}` and was authored by an operator, so nothing was minted or replaced: an \
             explicit record wins over the document's declaration. Delete or rename it if you want \
             this server to provision the queue itself",
            operator.name
        ),
    ))
}

/// Bring the freshly written record's live state to its durable desired state
/// on this node, answering whether it actually happened.
///
/// The boolean is the whole point: a caller that reported "started it" without
/// reading it would be making the claim this lane exists to stop anyone making.
async fn converge(state: &ServerState, name: &str, mode: Convergence) -> bool {
    converge_and_report(state.worker_supervisor(), name, mode, OPERATION)
        .await
        .is_ok()
}

/// Publish the same put event the worker-deployment transport publishes, so the
/// console's live feed cannot tell an auto-provisioned record from a typed one.
fn publish_put(state: &ServerState, result: &aion_store::WorkerDeploymentPutResult) {
    let name = result.deployment.name.clone();
    let outcome = result.outcome;
    let desired_state = result.deployment.desired;
    let binary_version = result.deployment.binary.version.clone();
    let binary_content_hash = result.deployment.binary.content_hash.clone();
    drop(
        state
            .cluster_publisher()
            .emit(|meta| ClusterEvent::WorkerDeploymentPut {
                meta,
                name,
                outcome,
                desired_state,
                binary_version,
                binary_content_hash,
            }),
    );
}

/// One structured line per queue, at the severity the decision earns: a refusal
/// an operator has to act on is never an `info` they scroll past.
pub(super) fn log_outcome(outcome: &AutoWorkerOutcome) {
    let task_queue = outcome.task_queue.as_str();
    let workflow_type = outcome.workflow_type.as_str();
    let decision = outcome.decision.token();
    let detail = outcome.detail.as_str();
    if outcome.decision.is_refusal() {
        tracing::error!(
            operation = OPERATION,
            workflow_type,
            task_queue,
            decision,
            "{detail}"
        );
    } else {
        tracing::info!(
            operation = OPERATION,
            workflow_type,
            task_queue,
            decision,
            "{detail}"
        );
    }
}

#[cfg(test)]
#[path = "provision_tests.rs"]
mod tests;

/// Why this host cannot launch the document's harness, when it cannot.
///
/// The probe is deliberately shallow — existence and the executable bit, never
/// a spawn: presence is not proof the agent WORKS, and no answer here claims
/// it is. What the probe refuses is the one face that can never succeed: an
/// agent binary or a literal working directory that does not exist on this
/// machine. A `{workspace_root}`-templated or dispatch-bound cwd is resolvable
/// only later and is not judged here.
fn harness_unrunnable_here(harness: &CompiledHarness) -> Option<String> {
    let (command, cwd) = match &harness.kind {
        CompiledHarnessKind::Acp { command, cwd, .. } => (Some(command.clone()), cwd),
        CompiledHarnessKind::Norn { binary, cwd } => (binary.clone(), cwd),
    };
    if let Some(command) = command {
        if let Some(why) = executable_missing(&command) {
            return Some(why);
        }
    } else if let Some(why) = resolves_nowhere_on_path("norn") {
        return Some(why);
    }
    if let CwdSource::Literal(path) = cwd
        && path.is_absolute()
        && !path.is_dir()
    {
        return Some(format!(
            "the harness working directory `{}` is not a directory on this host",
            path.display()
        ));
    }
    None
}

/// The named agent executable's absence, as a sentence, or `None` when it is
/// present and executable.
fn executable_missing(command: &Path) -> Option<String> {
    let Ok(metadata) = std::fs::metadata(command) else {
        return Some(format!(
            "the harness agent executable `{}` does not exist on this host",
            command.display()
        ));
    };
    if !metadata.is_file() {
        return Some(format!(
            "the harness agent path `{}` is not a file on this host",
            command.display()
        ));
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt as _;
        if metadata.permissions().mode() & 0o111 == 0 {
            return Some(format!(
                "the harness agent `{}` exists on this host but is not executable",
                command.display()
            ));
        }
    }
    None
}

/// Whether `name` resolves to an executable file on this process's own PATH —
/// the environment the managed worker inherits — reported as the refusal
/// sentence when it does not.
fn resolves_nowhere_on_path(name: &str) -> Option<String> {
    let path = std::env::var_os("PATH")?;
    let resolves =
        std::env::split_paths(&path).any(|dir| executable_missing(&dir.join(name)).is_none());
    if resolves {
        None
    } else {
        Some(format!(
            "the harness names no binary and `{name}` resolves nowhere on this server's PATH"
        ))
    }
}