aion-server 0.31.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
//! LSUB-5: a RUNNABLE, watchable cross-node OWNER-KILL fan-out failover demo.
//!
//! Boots the LSUB-5 capstone scenario in one process — a 3-node real-loopback
//! haematite cluster, a real `liminal-server` over loopback TCP, and a real
//! remote worker — kills the shard owner MID-DISPATCH, and narrates the survivor
//! adopting the orphaned shard and re-driving the in-flight fan-out to an
//! exactly-once-per-ordinal completion.
//!
//! ## Run it
//!
//! ```text
//! cargo run -p aion-server --example lsub5_xnode_failover_demo \
//!     --features liminal-transport
//! ```
//!
//! ## Honest about what it is
//!
//! Everything is real: genuine bound replication endpoints, quorum-replicated
//! writes, per-shard election + `become_live` union-merge, the production
//! `Engine::adopt_shards` failover path driven by the real `ClusterSupervisor`,
//! a real `liminal-server` + a real `LiminalActivityWorker`, and the durable
//! outbox cutover (`record_fan_out_dispatch`) + `OutboxDispatcher` +
//! `ServerOutboxDeliveryCallback` completion seam. The ONE concession (see the
//! LSUB-5 report's "seam wall") is that the OWNER node is an engine-less shard
//! owner + dispatcher — a running engine cannot be torn down in-process to close
//! its replication endpoint — while the SURVIVOR runs the full engine that does
//! the real adopt + replay + re-arm + re-dispatch. The owner stages its fan-out
//! through the SAME `record_fan_out_dispatch` cutover the live engine's collect
//! NIF calls, so the chain under test is exercised faithfully end to end.
#![cfg(feature = "liminal-transport")]

use aion_server::namespace::NamespaceGuard;
use std::error::Error;
use std::net::SocketAddr;
use std::path::Path;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};

use aion::activity::bridge::{ActivityDispatch, ActivityDispatcher};
use aion::durability::{FanOutItem, Recorder, WorkflowStartRecord};
use aion::signal::ConcreteSignalRouter;
use aion::{Engine, EngineBuilder, RuntimeHandle, SignalRouter};
use aion_core::{
    ActivityId, DEFAULT_TASK_QUEUE, Event, PackageVersion, Payload, RunId, WorkflowId,
    WorkflowStatus,
};
use aion_package::{
    BeamModule, BeamSet, CURRENT_FORMAT_VERSION, DeclaredActivity, ExtractionLimits, Manifest,
    ManifestVersion, Package, PackageBuilder,
};
use aion_server::cluster::{ClusterSupervisor, SupervisorConfig, WatchedPeer};
use aion_server::worker::ActivityDispatcher as ServerActivityDispatcher;
use aion_server::worker::CompletionFences;
use aion_server::worker::liminal_task_delivery::LiminalTaskDelivery;
use aion_server::worker::task_delivery::WorkerTaskDelivery;
use aion_server::worker::{
    ConnectedWorkerRegistry, DeliveryGate, LiminalCompletionSource, LiminalConnectionNotifier,
    OutboxDeliveryCallback, OutboxDispatcher, OutboxDispatcherConfig, OutboxRowDispatch,
    ServerOutboxDeliveryCallback, WorkerOutboxDispatch,
};
use aion_store::{EventStore, OutboxRow, OutboxStatus, OutboxStore};
use aion_store_haematite::HaematiteStore;
use aion_worker::{ActivityRegistry, LiminalActivityWorker, WorkerConfig};
use haematite::db::respond_to_inbound_writes;
use haematite::sync::membership::WriteMembership;
use haematite::sync::{DistributionEndpoint, SyncNodeId};
use haematite::{Database, DatabaseConfig};
use liminal_server::config::{ChannelDef, ServerConfig};
use liminal_server::server::connection::{ConnectionSupervisor, LiminalConnectionServices};
use liminal_server::server::listener::ServerListener;
use serde_json::json;

type DemoError = Box<dyn Error + Send + Sync>;
type DemoResult<T> = Result<T, DemoError>;

/// Build the row dispatch a liminal-hosting server builds in production.
///
/// This mirrors `build_liminal_row_dispatch` in `run/outbox_commission.rs`: the
/// SHARED [`WorkerOutboxDispatch`] over one [`ServerActivityDispatcher`], with the
/// liminal delivery attached. Before #52 R4 this was a liminal-ONLY sink, and a
/// gRPC-registered worker selected for a fan-out row on such a server was
/// refused for not being liminal-delivered. One selection runs now, and each
/// chosen worker is served over the transport IT registered on.
///
/// 🔴 The delivery gate is SHARED with the caller, never defaulted. The row's
/// delivery intent re-asks whether its claim still stands, and a dispatcher
/// holding a private gate answers "released" for every key — a key that was
/// never begun is indistinguishable from one that was released — so every
/// dispatch would be abandoned, as a delivery failure wearing the wrong name.
fn liminal_row_dispatch(
    registry: ConnectedWorkerRegistry,
    callback: Arc<dyn OutboxDeliveryCallback>,
    delivery_gate: DeliveryGate,
) -> WorkerOutboxDispatch {
    // 🔴 ONE `CompletionFences`, shared by the dispatcher that MINTS the token
    // and the completion source that RECORDS the reply against it. Production
    // shares `state.pending_activities().completion_fences()` between exactly
    // these two (`build_liminal_row_dispatch`), and the sharing is mandatory
    // rather than tidy: the dispatcher now issues the token that the delivery
    // arm's reply is fenced by, so two default instances mean every reply
    // arrives bearing a token the recorder never minted and is refused. The
    // dispatch then reports every candidate undeliverable — "all matching
    // worker streams closed" — which names a dead stream for what is really an
    // unshared registry.
    //
    // This is the same shape as the delivery gate below, and for the same
    // reason: splitting mint from record made the sharing a precondition, and
    // a private default fails closed under someone else's name.
    let completion_fences = CompletionFences::default();
    let liminal_delivery: Arc<dyn WorkerTaskDelivery> =
        Arc::new(LiminalTaskDelivery::new(Arc::new(
            LiminalCompletionSource::new(callback)
                .with_completion_fences(completion_fences.clone()),
        )));
    WorkerOutboxDispatch::new(
        ServerActivityDispatcher::new(registry)
            .with_completion_fences(completion_fences)
            .with_delivery_gate(delivery_gate)
            .with_liminal_delivery(liminal_delivery),
    )
}

const NODE_NAMES: [&str; 3] = [
    "lsub5-demo-0@127.0.0.1",
    "lsub5-demo-1@127.0.0.1",
    "lsub5-demo-2@127.0.0.1",
];
const SHARD_COUNT: usize = 3;
const FAN_OUT: usize = 4;
const NAMESPACE: &str = "default";
const TASK_QUEUE: &str = "default";
const OUTBOX_MODULE: &str = "aion_outbox_fixture";
const OUTBOX_BEAM: &[u8] = include_bytes!("../../aion/tests/fixtures/aion_outbox_fixture.beam");
const OUTBOX_SOURCE: &[u8] = include_bytes!("../../aion/tests/fixtures/aion_outbox_fixture.erl");
const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(5);
const OP_TIMEOUT: Duration = Duration::from_secs(5);
const FAILOVER_DEADLINE: Duration = Duration::from_secs(40);

fn demo_error(message: impl std::fmt::Display) -> DemoError {
    message.to_string().into()
}

// One long, linear narrative (boot -> stage -> mid-dispatch -> kill -> adopt ->
// re-drive -> one-terminal proof); the same shape the showcase failover demos
// carry the `too_many_lines` allow for.
#[allow(clippy::too_many_lines)]
fn main() -> DemoResult<()> {
    banner();
    let package = build_package()?;

    // --- Boot the 3-node cluster (sync prologue: blocking elections). --------
    let node_count = NODE_NAMES.len();
    let dirs: Vec<tempfile::TempDir> = (0..node_count)
        .map(|_| tempfile::tempdir())
        .collect::<Result<_, _>>()
        .map_err(demo_error)?;
    let send_targets: Vec<Vec<&str>> = (0..node_count)
        .map(|i| {
            (0..node_count)
                .filter(|&j| j != i)
                .map(|j| NODE_NAMES[j])
                .collect()
        })
        .collect();
    let mut nodes: Vec<Option<Node>> = (0..node_count)
        .map(|i| Node::spawn(NODE_NAMES[i], dirs[i].path(), &send_targets[i]).map(Some))
        .collect::<Result<_, _>>()?;
    for a in 0..node_count {
        for b in (a + 1)..node_count {
            link_both(node_ref(&nodes, a)?, node_ref(&nodes, b)?)?;
        }
    }
    for (i, targets) in send_targets.iter().enumerate() {
        let node = node_ref(&nodes, i)?;
        node.database()
            .acquire_shard_and_serve(i, &membership(targets), OP_TIMEOUT)
            .map_err(demo_error)?;
        node.store.set_owned_shards([i]);
    }
    println!("--- ACT 1: the cluster is up and SERVING ---");
    println!("  node A owns shard 0 (the fan-out owner)");
    println!("  node B owns shard 1 (the survivor, full engine)");
    println!("  node C owns shard 2 (quorum-only)\n");

    // --- Real liminal server + a real remote worker. ------------------------
    let liminal = RunningLiminalServer::start()?;
    let address = liminal.address.to_string();
    let control = Arc::new(WorkerControl::default());
    let worker = SurvivorWorker::spawn(address, Arc::clone(&control));
    liminal.wait_for_worker()?;
    println!("  liminal server up; remote worker registered for the (default,default) pool.\n");

    let dispatcher_config = OutboxDispatcherConfig {
        poll_interval: Duration::from_millis(25),
        batch_size: 16,
        max_attempts: 8,
        backoff_base: Duration::from_secs(120),
        backoff_multiplier: 2,
        backoff_max: Duration::from_secs(240),
    };

    // --- Survivor B (engine) + owner A (shard owner + dispatcher). ----------
    let fired_b = Arc::new(AtomicBool::new(false));
    let server_b = Server::build(
        node_ref(&nodes, 1)?,
        1,
        &package,
        &liminal,
        &fired_b,
        dispatcher_config,
    )?;
    let store_a = Arc::clone(&node_ref(&nodes, 0)?.store);
    let mut owner = Some(OwnerServer::spawn(
        node_ref(&nodes, 0)?,
        &liminal,
        dispatcher_config,
    )?);

    // --- Stage the fan-out on shard 0 through the durable cutover. -----------
    let fanout_workflow = workflow_id_for_shard(&store_a, 0);
    let fanout_run = RunId::new_v4();
    server_b.block_on(stage_fanout(
        &store_a,
        &fanout_workflow,
        &fanout_run,
        &package,
    ))?;
    println!("--- ACT 2: a fan-out of {FAN_OUT} activities is dispatched on shard 0 ---");
    wait_until(Duration::from_secs(20), || {
        server_b
            .block_on(all_rows_present(&store_a, &fanout_workflow))
            .unwrap_or(false)
    });
    println!(
        "  {FAN_OUT} durable outbox rows staged Pending on shard 0 (record_fan_out_dispatch)."
    );
    wait_until(Duration::from_secs(20), || {
        control.dispatch_seen.load(Ordering::SeqCst)
    });
    let claimed = server_b.block_on(claimed_count(&store_a, &fanout_workflow))?;
    println!(
        "  owner A's dispatcher claimed + pushed to the worker MID-DISPATCH ({claimed} in flight).\n"
    );

    // --- Supervisor on B watching A (owner of shard 0). ---------------------
    let store_b = Arc::clone(&server_b.store);
    let mut supervisor = ClusterSupervisor::new(
        Arc::clone(&store_b),
        Arc::clone(&server_b.engine),
        vec![WatchedPeer {
            name: NODE_NAMES[0].to_owned(),
            owned_shards: vec![0],
        }],
        SupervisorConfig {
            poll_interval: Duration::from_millis(20),
            confirmations: 2,
        },
    );
    drop(server_b.block_on(supervisor.tick())); // alive: no adoption.

    // --- ACT 3: kill the owner mid-dispatch. --------------------------------
    println!("--- ACT 3: the owner DIES mid-dispatch ---");
    println!("  \u{1f480} kill node A (drop its dispatcher runtime + node -> endpoint closes)");
    control.released.store(true, Ordering::SeqCst);
    owner
        .take()
        .ok_or_else(|| demo_error("owner already killed"))?
        .kill();
    drop(store_a);
    let dead = nodes[0]
        .take()
        .ok_or_else(|| demo_error("owner node gone"))?;
    drop(dead);
    if !wait_until(Duration::from_secs(20), || {
        !store_b.peer_connected(NODE_NAMES[0])
    }) {
        return Err(demo_error("survivor never observed the owner's link drop"));
    }
    println!("  node B detected node A's replication link DROP (peer_connected -> false).\n");

    // --- ACT 4: automatic adoption + re-drive. ------------------------------
    println!("--- ACT 4: AUTOMATIC failover (no human, no manual adopt) ---");
    drop(server_b.block_on(supervisor.tick())); // debounce.
    let adopted = server_b.block_on(supervisor.tick());
    println!(
        "  node B AUTO-adopted shard {adopted:?} (debounced over the genuine link-down signal)."
    );
    let claim_scope = store_b.owned_shards().unwrap_or_default();
    println!("  node B's outbox claim scope refreshed in place to own {claim_scope:?} (the CRUX).");

    let started = Instant::now();
    let completed = wait_until(FAILOVER_DEADLINE, || {
        server_b.block_on(async {
            rows_all_done(&store_b, &fanout_workflow)
                .await
                .unwrap_or(false)
                && workflow_completed(&store_b, &fanout_workflow)
                    .await
                    .unwrap_or(false)
        })
    });
    if !completed {
        return Err(demo_error("fan-out did not re-drive to completion in time"));
    }
    println!("  node B re-armed the stranded rows and re-dispatched them to the worker.");
    println!(
        "  \u{2705} fan-out re-driven to completion in {:?}.\n",
        started.elapsed()
    );

    // --- The one-terminal proof. --------------------------------------------
    let history = server_b.block_on(read_history(&store_b, &fanout_workflow))?;
    let total = control.executions.load(Ordering::SeqCst);
    println!("--- RESULT: EXACTLY-ONCE per ordinal ---");
    for ordinal in 0..FAN_OUT as u64 {
        println!(
            "  ordinal {ordinal}: {} terminal event(s)",
            terminal_count_for(&history, ordinal)
        );
    }
    println!(
        "  workflow status: {:?}",
        aion_core::status_from_events(&history)
    );
    println!(
        "  the worker executed the activity {total} times (at-least-once); the completion dedup\n  \
         absorbed the redelivery so each ordinal has EXACTLY ONE terminal in history.\n"
    );
    closing_banner();

    // --- Teardown. ----------------------------------------------------------
    let _: Result<(), _> = server_b.dispatcher_shutdown.send(true);
    drop(supervisor);
    server_b.engine.shutdown().map_err(demo_error)?;
    let Server {
        runtime,
        store,
        engine,
        ..
    } = server_b;
    runtime.shutdown_timeout(Duration::from_secs(10));
    drop(engine);
    drop(store);
    worker.stop();
    liminal.shutdown()?;
    Ok(())
}

fn banner() {
    println!("\n==================================================================");
    println!(" Aion  |  LSUB-5: cross-node OWNER-KILL fan-out failover (exactly-once)");
    println!("==================================================================");
    println!(" in-process, real loopback haematite cluster + real liminal worker\n");
}

fn closing_banner() {
    println!("==================================================================");
    println!(" The owner died MID-DISPATCH; the survivor auto-adopted its shard,");
    println!(" re-armed the in-flight outbox rows, re-dispatched them to a live");
    println!(" worker, and the completion dedup delivered EXACTLY ONCE per ordinal.");
    println!("==================================================================\n");
}

// ===========================================================================
// Harness (mirrors the lsub5 e2e test; kept self-contained for a runnable demo).
// ===========================================================================

fn build_package() -> DemoResult<Package> {
    let beams =
        BeamSet::new(vec![BeamModule::new(OUTBOX_MODULE, OUTBOX_BEAM)]).map_err(demo_error)?;
    let manifest = Manifest {
        entry_module: OUTBOX_MODULE.to_owned(),
        entry_function: "collect_four".to_owned(),
        input_schema: json!({ "type": "object" }),
        output_schema: json!({}),
        timeout: Some(Duration::from_secs(60)),
        activities: vec![DeclaredActivity {
            activity_type: "fixture_activity".to_owned(),
        }],
        version: ManifestVersion::new("stamped-by-builder"),
        format_version: CURRENT_FORMAT_VERSION,
        additional_workflows: Vec::new(),
    };
    let archive =
        PackageBuilder::with_source(manifest, beams, [(OUTBOX_MODULE, OUTBOX_SOURCE.to_vec())])
            .write_to_bytes()
            .map_err(demo_error)?;
    Package::load_from_bytes(archive, ExtractionLimits::unbounded()).map_err(demo_error)
}

fn worker_result(ordinal: u64) -> serde_json::Value {
    json!(format!("worker-{ordinal}"))
}

fn membership(send_targets: &[&str]) -> WriteMembership {
    WriteMembership {
        total_nodes: NODE_NAMES.len(),
        send_targets: send_targets
            .iter()
            .map(|name| SyncNodeId::from(*name))
            .collect(),
    }
}

fn wait_until(timeout: Duration, mut predicate: impl FnMut() -> bool) -> bool {
    let deadline = Instant::now() + timeout;
    loop {
        if predicate() {
            return true;
        }
        if Instant::now() >= deadline {
            return false;
        }
        std::thread::sleep(Duration::from_millis(10));
    }
}

struct Node {
    store: Arc<HaematiteStore>,
    event_store: Arc<haematite::EventStore>,
    addr: SocketAddr,
    name: &'static str,
    responder: Option<JoinHandle<()>>,
    running: Arc<AtomicBool>,
}

impl Node {
    fn spawn(name: &'static str, dir: &Path, send_targets: &[&str]) -> DemoResult<Self> {
        let endpoint =
            DistributionEndpoint::bind(name, "127.0.0.1:0".parse().map_err(demo_error)?, 1, None)
                .map_err(demo_error)?;
        let addr = endpoint.local_addr();
        let database = Database::create(DatabaseConfig {
            data_dir: dir.join("db"),
            shard_count: SHARD_COUNT,
            executor_threads: None,
            distributed: None,
            // No byte bound, stated out loud: this fixture measures
            // replication/routing and never re-reads enough nodes for a ceiling
            // to bind, so a number here would be decoration. haematite has no
            // default for this; `Unlimited` is the pre-budget behaviour, named.
            node_cache_budget: Some(haematite::NodeCacheBudget::Unlimited),
        })
        .map_err(demo_error)?
        .with_distribution(endpoint);
        let store = Arc::new(HaematiteStore::with_distribution(
            database,
            membership(send_targets),
            OP_TIMEOUT,
            name.to_owned(),
        ));
        let event_store = Arc::clone(store.event_store());
        let running = Arc::new(AtomicBool::new(true));
        let responder_store = Arc::clone(&event_store);
        let responder_running = Arc::clone(&running);
        let responder = std::thread::spawn(move || {
            while responder_running.load(Ordering::Relaxed) {
                drop(respond_to_inbound_writes(
                    responder_store.database(),
                    Duration::from_millis(50),
                ));
            }
        });
        Ok(Self {
            store,
            event_store,
            addr,
            name,
            responder: Some(responder),
            running,
        })
    }

    fn database(&self) -> &Database {
        self.event_store.database()
    }
}

impl Drop for Node {
    fn drop(&mut self) {
        self.running.store(false, Ordering::Relaxed);
        if let Some(handle) = self.responder.take() {
            drop(handle.join());
        }
    }
}

fn node_ref(nodes: &[Option<Node>], index: usize) -> DemoResult<&Node> {
    nodes
        .get(index)
        .and_then(Option::as_ref)
        .ok_or_else(|| demo_error(format!("node {index} gone")))
}

fn link(from: &Node, to: &Node) -> DemoResult<()> {
    let endpoint = from
        .database()
        .distribution()
        .ok_or_else(|| demo_error("no endpoint"))?;
    endpoint.add_peer(to.name, to.addr);
    endpoint.connect(to.name).map_err(demo_error)?;
    if !wait_until(HANDSHAKE_TIMEOUT, || endpoint.is_connected(to.name)) {
        return Err(demo_error(format!(
            "{} never linked to {}",
            from.name, to.name
        )));
    }
    Ok(())
}

fn link_both(a: &Node, b: &Node) -> DemoResult<()> {
    link(a, b)?;
    link(b, a)
}

fn workflow_id_for_shard(store: &HaematiteStore, shard: usize) -> WorkflowId {
    loop {
        let candidate = WorkflowId::new_v4();
        if store.shard_for_workflow(&candidate) == shard {
            return candidate;
        }
    }
}

struct RunningLiminalServer {
    listener: Option<ServerListener>,
    registry: ConnectedWorkerRegistry,
    address: SocketAddr,
    /// The runtime the listener's synchronous registration callback bridges
    /// the namespace admission onto; this harness starts from a plain thread,
    /// so it owns one for the listener's lifetime.
    _admission_runtime: tokio::runtime::Runtime,
}

impl RunningLiminalServer {
    fn start() -> DemoResult<Self> {
        let health = {
            let l = std::net::TcpListener::bind("127.0.0.1:0").map_err(demo_error)?;
            let a = l.local_addr().map_err(demo_error)?;
            drop(l);
            a
        };
        let config = ServerConfig {
            listen_address: "127.0.0.1:0".parse().map_err(demo_error)?,
            health_listen_address: health,
            channels: Vec::<ChannelDef>::new(),
            routing_rules: Vec::new(),
            persistence_path: None,
            cluster: None,
            // Open at the liminal layer (no Connect token), matching the embedded
            // production listener; aion-level registration metadata is the auth story.
            auth: None,
            drain_timeout_ms: 30_000,
            // liminal 0.2.4 defaults = the 0.2.3 behaviour (full profile, signed caps).
            services: liminal_server::config::ServicesConfig::default(),
            limits: liminal_server::config::LimitsConfig::default(),
            // liminal 0.3.0: no WebSocket listener, participant capability
            // disabled — byte-identical to the pre-0.3.0 build, matching run.rs.
            websocket: None,
            participant: None,
        };
        let admission_runtime = tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .map_err(demo_error)?;
        let registry = ConnectedWorkerRegistry::default();
        let notifier = Arc::new(
            LiminalConnectionNotifier::new(registry.clone()).with_admission(
                NamespaceGuard::shared_engine(),
                false,
                admission_runtime.handle().clone(),
            ),
        );
        let services =
            Arc::new(LiminalConnectionServices::from_config(&config).map_err(demo_error)?);
        let supervisor =
            ConnectionSupervisor::with_services_and_notifier(services, notifier.clone())
                .map_err(demo_error)?;
        if !notifier.bind_supervisor(supervisor.clone()) {
            return Err(demo_error("notifier supervisor already bound"));
        }
        let listener = ServerListener::bind(&config, supervisor).map_err(demo_error)?;
        let address = listener.local_addr();
        Ok(Self {
            listener: Some(listener),
            registry,
            address,
            _admission_runtime: admission_runtime,
        })
    }

    fn wait_for_worker(&self) -> DemoResult<()> {
        let ok = wait_until(HANDSHAKE_TIMEOUT, || {
            (0..FAN_OUT).all(|ordinal| {
                self.registry
                    .select_and_reserve(NAMESPACE, TASK_QUEUE, &format!("fan:{ordinal}"), None)
                    .map(|selected| selected.map(|(worker, _reservation)| worker))
                    .ok()
                    .flatten()
                    .is_some()
            })
        });
        if ok {
            Ok(())
        } else {
            Err(demo_error("worker never registered for the pool"))
        }
    }

    fn shutdown(mut self) -> DemoResult<()> {
        if let Some(listener) = self.listener.take() {
            listener.shutdown().map_err(demo_error)?;
        }
        Ok(())
    }
}

#[derive(Default)]
struct WorkerControl {
    executions: AtomicUsize,
    dispatch_seen: AtomicBool,
    released: AtomicBool,
}

struct SurvivorWorker {
    stop: Arc<AtomicBool>,
    handle: Option<JoinHandle<()>>,
}

impl SurvivorWorker {
    fn spawn(address: String, control: Arc<WorkerControl>) -> Self {
        let stop = Arc::new(AtomicBool::new(false));
        let thread_stop = Arc::clone(&stop);
        let handle = std::thread::spawn(move || {
            let runtime = match tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
            {
                Ok(runtime) => runtime,
                Err(error) => {
                    eprintln!("worker runtime build failed: {error}");
                    return;
                }
            };
            runtime.block_on(async move {
                if let Err(error) = serve_worker(&address, &control, &thread_stop).await {
                    eprintln!("worker ended with error: {error}");
                }
            });
        });
        Self {
            stop,
            handle: Some(handle),
        }
    }

    fn stop(mut self) {
        self.stop.store(true, Ordering::SeqCst);
        if let Some(handle) = self.handle.take() {
            drop(handle.join());
        }
    }
}

async fn serve_worker(
    address: &str,
    control: &Arc<WorkerControl>,
    stop: &Arc<AtomicBool>,
) -> DemoResult<()> {
    let mut registry = ActivityRegistry::new();
    for ordinal in 0..FAN_OUT as u64 {
        let control = Arc::clone(control);
        registry = registry
            .register_activity(
                format!("fan:{ordinal}"),
                move |_input: serde_json::Value, _ctx| {
                    let control = Arc::clone(&control);
                    Box::pin(async move {
                        control.executions.fetch_add(1, Ordering::SeqCst);
                        control.dispatch_seen.store(true, Ordering::SeqCst);
                        while !control.released.load(Ordering::SeqCst) {
                            tokio::time::sleep(Duration::from_millis(10)).await;
                        }
                        Ok(worker_result(ordinal))
                    })
                },
            )
            .map_err(demo_error)?;
    }
    let config = WorkerConfig::builder()
        .endpoint("unused-direct-address")
        .namespace(NAMESPACE)
        .task_queue(TASK_QUEUE)
        .identity("lsub5-demo-worker")
        // FAN_OUT, and it now means something on this transport: liminal
        // enforces the configured concurrency as one shared budget and announces
        // it to the server, so this demo's fan is served as wide as it asks.
        .max_concurrency(FAN_OUT)
        .reconnect_initial_backoff(Duration::from_millis(5))
        .reconnect_max_backoff(Duration::from_millis(20))
        .reconnect_max_attempts(3)
        .build()
        .map_err(demo_error)?;
    let worker =
        LiminalActivityWorker::connect(address, &config, Arc::new(registry)).map_err(demo_error)?;
    worker
        .serve_until(|| stop.load(Ordering::SeqCst))
        .await
        .map_err(demo_error)
}

#[derive(Debug, Default)]
struct NoopDeliveryCallback;

impl OutboxDeliveryCallback for NoopDeliveryCallback {
    fn deliver_completion(
        &self,
        _workflow_id: &WorkflowId,
        _activity_id: &ActivityId,
        _run_id: Option<&RunId>,
        _result: String,
    ) -> Result<bool, aion_server::ServerError> {
        Ok(true)
    }

    fn deliver_failure(
        &self,
        _workflow_id: &WorkflowId,
        _activity_id: &ActivityId,
        _run_id: Option<&RunId>,
        _reason: String,
    ) -> Result<bool, aion_server::ServerError> {
        Ok(true)
    }
}

struct OwnerServer {
    runtime: tokio::runtime::Runtime,
    dispatcher_shutdown: tokio::sync::watch::Sender<bool>,
}

impl OwnerServer {
    fn spawn(
        node: &Node,
        liminal: &RunningLiminalServer,
        dispatcher_config: OutboxDispatcherConfig,
    ) -> DemoResult<Self> {
        let runtime = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .map_err(demo_error)?;
        let callback: Arc<dyn OutboxDeliveryCallback> = Arc::new(NoopDeliveryCallback);
        let outbox_store: Arc<dyn OutboxStore> = Arc::clone(&node.store) as Arc<dyn OutboxStore>;
        let dispatcher_builder = OutboxDispatcher::new(outbox_store, dispatcher_config);
        let dispatch: Arc<dyn OutboxRowDispatch> = Arc::new(liminal_row_dispatch(
            liminal.registry.clone(),
            Arc::clone(&callback),
            dispatcher_builder.delivery_gate(),
        ));
        let dispatcher = dispatcher_builder
            .with_dispatch(dispatch)
            .with_delivery_callback(callback);
        let (dispatcher_shutdown, shutdown_rx) = tokio::sync::watch::channel(false);
        runtime.spawn(dispatcher.run(shutdown_rx));
        Ok(Self {
            runtime,
            dispatcher_shutdown,
        })
    }

    fn kill(self) {
        let _: Result<(), _> = self.dispatcher_shutdown.send(true);
        self.runtime.shutdown_timeout(Duration::from_secs(10));
    }
}

struct StubDispatcher {
    fired: Arc<AtomicBool>,
}

impl ActivityDispatcher for StubDispatcher {
    fn dispatch(&self, request: ActivityDispatch) -> Result<String, String> {
        self.fired.store(true, Ordering::SeqCst);
        Err(format!(
            "in-process dispatcher fired for {} — outbox cutover broken",
            request.name
        ))
    }
}

struct Server {
    runtime: tokio::runtime::Runtime,
    store: Arc<HaematiteStore>,
    engine: Arc<Engine>,
    dispatcher_shutdown: tokio::sync::watch::Sender<bool>,
}

impl Server {
    fn build(
        node: &Node,
        owned_shard: usize,
        package: &Package,
        liminal: &RunningLiminalServer,
        fired: &Arc<AtomicBool>,
        dispatcher_config: OutboxDispatcherConfig,
    ) -> DemoResult<Self> {
        let runtime = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(3)
            .enable_all()
            .build()
            .map_err(demo_error)?;
        let store_dyn: Arc<dyn EventStore> = Arc::clone(&node.store) as Arc<dyn EventStore>;
        let fired = Arc::clone(fired);
        let registry = liminal.registry.clone();
        let package = package.clone();
        let engine = runtime.block_on(async move {
            EngineBuilder::new()
                // AE-017: the demo's own stop patience — the shipped configs carry 30 s.
                .stop_drain_timeout(std::time::Duration::from_secs(30))
                .store_arc(store_dyn)
                .in_memory_visibility()
                .scheduler_threads(1)
                .signal_router_factory(|runtime: Arc<RuntimeHandle>, handoff| {
                    Arc::new(ConcreteSignalRouter::new(runtime, handoff)) as Arc<dyn SignalRouter>
                })
                .outbox_enabled(true)
                .activity_dispatcher(Arc::new(StubDispatcher { fired }))
                .bootstrap_schedule_coordinator(false)
                .owned_shards([owned_shard])
                .load_workflows(package)
                .build()
                .await
                .map_err(demo_error)
        })?;
        let engine = Arc::new(engine);
        let callback: Arc<dyn OutboxDeliveryCallback> =
            Arc::new(ServerOutboxDeliveryCallback::new(Arc::clone(&engine)));
        let outbox_store: Arc<dyn OutboxStore> = Arc::clone(&node.store) as Arc<dyn OutboxStore>;
        let dispatcher_builder = OutboxDispatcher::new(outbox_store, dispatcher_config);
        let dispatch: Arc<dyn OutboxRowDispatch> = Arc::new(liminal_row_dispatch(
            registry,
            Arc::clone(&callback),
            dispatcher_builder.delivery_gate(),
        ));
        let dispatcher = dispatcher_builder
            .with_dispatch(dispatch)
            .with_delivery_callback(callback);
        let (dispatcher_shutdown, shutdown_rx) = tokio::sync::watch::channel(false);
        runtime.spawn(dispatcher.run(shutdown_rx));
        Ok(Self {
            runtime,
            store: Arc::clone(&node.store),
            engine,
            dispatcher_shutdown,
        })
    }

    fn block_on<F: std::future::Future>(&self, future: F) -> F::Output {
        self.runtime.block_on(future)
    }
}

async fn stage_fanout(
    store: &Arc<HaematiteStore>,
    workflow_id: &WorkflowId,
    run_id: &RunId,
    package: &Package,
) -> DemoResult<()> {
    let store_dyn: Arc<dyn EventStore> = Arc::clone(store) as Arc<dyn EventStore>;
    let mut recorder = Recorder::new(workflow_id.clone(), store_dyn).with_run_id(run_id.clone());
    recorder
        .record_workflow_started(
            chrono::Utc::now(),
            WorkflowStartRecord {
                workflow_type: OUTBOX_MODULE.to_owned(),
                input: Payload::from_json(&json!({ "fixture": "fanout" })).map_err(demo_error)?,
                run_id: run_id.clone(),
                parent_run_id: None,
                parent_workflow_id: None,
                package_version: PackageVersion::new(package.content_hash().to_string()),
            },
        )
        .await
        .map_err(demo_error)?;
    let items: Vec<FanOutItem> = (0..FAN_OUT as u64)
        .map(|ordinal| {
            Ok(FanOutItem {
                ordinal,
                namespace: NAMESPACE.to_owned(),
                task_queue: DEFAULT_TASK_QUEUE.to_owned(),
                node: None,
                activity_type: format!("fan:{ordinal}"),
                input: Payload::from_json(&json!("in")).map_err(demo_error)?,
                attempt: 1,
            })
        })
        .collect::<Result<_, DemoError>>()?;
    recorder
        .record_fan_out_dispatch(chrono::Utc::now(), &items)
        .await
        .map_err(demo_error)
}

async fn all_rows_present(
    store: &Arc<HaematiteStore>,
    workflow_id: &WorkflowId,
) -> DemoResult<bool> {
    for ordinal in 0..FAN_OUT as u64 {
        let key = OutboxRow::dispatch_key_for(workflow_id, ordinal);
        if store
            .outbox_row_status(&key)
            .await
            .map_err(demo_error)?
            .is_none()
        {
            return Ok(false);
        }
    }
    Ok(true)
}

async fn claimed_count(store: &Arc<HaematiteStore>, workflow_id: &WorkflowId) -> DemoResult<usize> {
    let mut claimed = 0;
    for ordinal in 0..FAN_OUT as u64 {
        let key = OutboxRow::dispatch_key_for(workflow_id, ordinal);
        if store.outbox_row_status(&key).await.map_err(demo_error)? == Some(OutboxStatus::Claimed) {
            claimed += 1;
        }
    }
    Ok(claimed)
}

async fn rows_all_done(store: &Arc<HaematiteStore>, workflow_id: &WorkflowId) -> DemoResult<bool> {
    for ordinal in 0..FAN_OUT as u64 {
        let key = OutboxRow::dispatch_key_for(workflow_id, ordinal);
        if store.outbox_row_status(&key).await.map_err(demo_error)? != Some(OutboxStatus::Done) {
            return Ok(false);
        }
    }
    Ok(true)
}

async fn workflow_completed(
    store: &Arc<HaematiteStore>,
    workflow_id: &WorkflowId,
) -> DemoResult<bool> {
    let history = read_history(store, workflow_id).await?;
    Ok(aion_core::status_from_events(&history) == WorkflowStatus::Completed)
}

async fn read_history(
    store: &Arc<HaematiteStore>,
    workflow_id: &WorkflowId,
) -> DemoResult<Vec<Event>> {
    let store_dyn: Arc<dyn EventStore> = Arc::clone(store) as Arc<dyn EventStore>;
    store_dyn
        .read_history(workflow_id)
        .await
        .map_err(demo_error)
}

fn terminal_count_for(history: &[Event], ordinal: u64) -> usize {
    history
        .iter()
        .filter(|event| match event {
            Event::ActivityCompleted { activity_id, .. }
            | Event::ActivityFailed { activity_id, .. }
            | Event::ActivityCancelled { activity_id, .. } => {
                activity_id.sequence_position() == ordinal
            }
            _ => false,
        })
        .count()
}