acativa 0.1.1

Multi-Agent Collaboration Strategy Compiler — generic runtime, state machine and durable strategy store
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
//! Continuous node driver with graceful lifecycle and source-identity result re-entry.
//!
//! Coordinates queue items, proxy receipts, node facades, feature indexes, and subscriptions.
//! Ensures independent node concurrency (A->C executes while independent B runs).
//! Collects per-node outcomes for graph pause/stop.
//! Re-enters execution results with source identity and pre-effect validity checks.

use crate::adapter::AdapterExecutionStatus;
use crate::control::ControlOperation;
use crate::node::{
    CancellationFacts, NodeExecutionError, NodeExecutionOutcome, NodeExecutionReceipt, NodeFacade,
    PauseResult, SteerResult, StopResult,
};
use crate::routing::{
    ActivationRule, FairDispatchQueue, FrozenTargets, NodeFeatureIndex, NodeLifecycleState,
    RecipientEffectStatus, SubscriptionPredicate, SubscriptionRegistry, SubscriptionScope,
};
#[cfg(test)]
use crate::routing::{ChannelKind, QueueCapacityExceeded};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fmt::{Display, Formatter};

/// Admitted message payload carried inside `QueuedItem.content`.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AdmittedMessagePayload {
    pub source_principal_id: String,
    pub graph_id: String,
    pub operation: ControlOperation,
    pub targets: FrozenTargets,
}

/// Result re-entry with source identity.
///
/// When an execution completes or fails, results re-enter through source identity
/// (principal, graph_id, node_id, invocation_id, generation, durable_cursor).
/// Stale generations are rejected and cannot satisfy new dependencies.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResultReentry {
    pub source_principal_id: String,
    pub graph_id: String,
    pub node_id: String,
    pub invocation_id: String,
    pub generation: u64,
    pub cursor: u64,
    pub attempt_token: String,
    pub outcome: NodeExecutionOutcome,
}

/// Report for an individual recipient's effect dispatch.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DispatchedEffectReport {
    pub delivery_id: String,
    pub recipient_node_id: String,
    pub status: RecipientEffectStatus,
    pub operation: String,
    pub invocation_id: Option<String>,
}

/// Per-node outcome when pausing a graph.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PerNodePauseOutcome {
    pub node_id: String,
    pub state_before: NodeLifecycleState,
    pub state_after: NodeLifecycleState,
    pub result: Result<PauseResult, String>,
}

/// Graph-wide pause report collecting individual node outcomes truthfully.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphPauseReport {
    pub graph_id: String,
    pub node_outcomes: Vec<PerNodePauseOutcome>,
    pub scope_barrier_established: bool,
}

/// Per-node outcome when stopping a graph.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PerNodeStopOutcome {
    pub node_id: String,
    pub state_before: NodeLifecycleState,
    pub state_after: NodeLifecycleState,
    pub cancellation_facts: Option<CancellationFacts>,
    pub result: Result<StopResult, String>,
}

/// Graph-wide stop report collecting individual node outcomes truthfully.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphStopReport {
    pub graph_id: String,
    pub node_outcomes: Vec<PerNodeStopOutcome>,
    pub scope_barrier_established: bool,
}

/// Outcome of one step of the continuous driver.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DriverStepOutcome {
    pub items_dispatched: usize,
    pub effects_attempted: usize,
    pub effects_succeeded: usize,
    pub stale_recipients_skipped: usize,
    pub completions_settled: usize,
    pub reports: Vec<DispatchedEffectReport>,
}

/// Errors originating in continuous driver operations.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum DriverError {
    GraphMismatch {
        expected: String,
        received: String,
    },
    NodeNotFound(String),
    StaleGenerationResult {
        node_id: String,
        expected: u64,
        received: u64,
    },
    ResultSourceMismatch {
        node_id: String,
        expected: String,
        received: String,
    },
    InvocationMismatch {
        node_id: String,
        expected: String,
        received: String,
    },
    AttemptTokenMismatch {
        node_id: String,
        expected: String,
        received: String,
    },
    StaleResultCursor {
        node_id: String,
        current: u64,
        received: u64,
    },
    ScopeAdmissionBarrierActive,
    NodeExecution(NodeExecutionError),
}

impl Display for DriverError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::GraphMismatch { expected, received } => {
                write!(
                    f,
                    "Graph mismatch: expected {expected}, received {received}"
                )
            }
            Self::NodeNotFound(id) => write!(f, "Node {id} not found in driver"),
            Self::StaleGenerationResult {
                node_id,
                expected,
                received,
            } => write!(
                f,
                "Stale generation result for node {node_id}: expected gen {expected}, got gen {received}"
            ),
            Self::ResultSourceMismatch {
                node_id,
                expected,
                received,
            } => write!(
                f,
                "Unverified result source for node {node_id}: expected {expected}, got {received}"
            ),
            Self::InvocationMismatch {
                node_id,
                expected,
                received,
            } => write!(
                f,
                "Invocation mismatch for node {node_id}: expected {expected}, got {received}"
            ),
            Self::AttemptTokenMismatch {
                node_id,
                expected,
                received,
            } => write!(
                f,
                "Attempt token mismatch for node {node_id}: expected {expected}, got {received}"
            ),
            Self::StaleResultCursor {
                node_id,
                current,
                received,
            } => write!(
                f,
                "Stale result cursor for node {node_id}: current {current}, got {received}"
            ),
            Self::ScopeAdmissionBarrierActive => {
                write!(f, "Scope admission barrier active; new work rejected")
            }
            Self::NodeExecution(err) => write!(f, "Node execution error: {err}"),
        }
    }
}

impl std::error::Error for DriverError {}

impl From<NodeExecutionError> for DriverError {
    fn from(err: NodeExecutionError) -> Self {
        Self::NodeExecution(err)
    }
}

/// Continuous driver orchestrating node facades, feature indexing, queues, and result re-entry.
pub struct ContinuousNodeDriver {
    pub graph_id: String,
    /// Facades are crate-internal: mutable node access would hand callers a
    /// `submit` path around `InterventionProxy::admit`. Hosts register
    /// facades; the driver owns their dispatch from there.
    nodes: BTreeMap<String, NodeFacade>,
    pub index: NodeFeatureIndex,
    /// The dispatch queue is private: admitted work enters only through
    /// `InterventionProxy` — handing the queue to callers would hand them a
    /// path around the admission boundary.
    queue: FairDispatchQueue,
    pub subscriptions: SubscriptionRegistry,
    pub active_invocations: BTreeMap<String, String>, // invocation_id -> node_id
    pub scope_barrier_active: bool,
    pub durable_cursor: u64,
}

impl ContinuousNodeDriver {
    pub fn new(
        graph_id: impl Into<String>,
        queue: FairDispatchQueue,
        subscriptions: SubscriptionRegistry,
    ) -> Self {
        Self {
            graph_id: graph_id.into(),
            nodes: BTreeMap::new(),
            index: NodeFeatureIndex::new(),
            queue,
            subscriptions,
            active_invocations: BTreeMap::new(),
            scope_barrier_active: false,
            durable_cursor: 0,
        }
    }

    /// Register a new node facade into the driver and feature index.
    pub fn register_node(&mut self, facade: NodeFacade) {
        let meta = facade.metadata();
        let node_id = facade.node_id.clone();
        self.nodes.insert(node_id, facade);
        self.index.register_or_update(meta);
    }

    /// Access a node facade by ID.
    #[cfg(test)]
    pub(crate) fn get_node(&self, node_id: &str) -> Option<&NodeFacade> {
        self.nodes.get(node_id)
    }

    /// Mutably access a node facade by ID.
    #[cfg(test)]
    pub(crate) fn get_node_mut(&mut self, node_id: &str) -> Option<&mut NodeFacade> {
        self.nodes.get_mut(node_id)
    }

    /// Test-only helper to enqueue an admitted message payload.
    ///
    /// Admission flows through `InterventionProxy`, the only boundary that
    /// verifies the principal; this helper is test plumbing, not a host API.
    #[cfg(test)]
    fn enqueue_admitted(
        &mut self,
        channel: ChannelKind,
        item_id: impl Into<String>,
        payload: AdmittedMessagePayload,
        timestamp_unix_ms: i64,
    ) -> Result<u64, QueueCapacityExceeded> {
        let content = serde_json::to_value(&payload).unwrap_or_default();
        self.queue
            .enqueue(channel, item_id, content, timestamp_unix_ms)
    }

    /// Remove driver entries whose facade has already settled the invocation.
    /// A direct stop/cancel may settle a node before the next driver poll; the
    /// index must not turn that settled observation into a second re-entry.
    fn prune_settled_invocations(&mut self) {
        let stale_ids: Vec<String> = self
            .active_invocations
            .iter()
            .filter_map(|(invocation_id, node_id)| {
                let active_id = self
                    .nodes
                    .get(node_id)
                    .and_then(|facade| facade.active_invocation.as_ref())
                    .map(|invocation| invocation.invocation_id.as_str());
                (active_id != Some(invocation_id.as_str())).then(|| invocation_id.clone())
            })
            .collect();

        for invocation_id in stale_ids {
            self.active_invocations.remove(&invocation_id);
        }
    }

    /// Refresh the live metadata used by pre-effect validation for the
    /// recipients in one admitted publication. This keeps the feature index
    /// authoritative even when a caller has just observed or controlled a
    /// facade through the narrow mutable access port.
    fn refresh_recipient_index(&mut self, targets: &FrozenTargets) {
        let metadata: Vec<(String, Option<_>)> = targets
            .recipients
            .iter()
            .map(|recipient| {
                (
                    recipient.node_id.clone(),
                    self.nodes.get(&recipient.node_id).map(NodeFacade::metadata),
                )
            })
            .collect();

        for (node_id, metadata) in metadata {
            if let Some(metadata) = metadata {
                self.index.register_or_update(metadata);
            } else {
                self.index.remove(&node_id);
            }
        }
    }

    /// Turn one terminal adapter observation into a source-authenticated
    /// result re-entry. The active invocation and attempt token are copied
    /// from the live facade, never from an untrusted observation.
    fn result_reentry_from_status(
        &self,
        invocation_id: &str,
        node_id: &str,
        cursor: u64,
        status: AdapterExecutionStatus,
    ) -> Option<ResultReentry> {
        let facade = self.nodes.get(node_id)?;
        let active = facade.active_invocation.as_ref()?;
        if active.invocation_id != invocation_id {
            return None;
        }

        let outcome = match status {
            AdapterExecutionStatus::Completed { output } => {
                NodeExecutionOutcome::Success { output }
            }
            AdapterExecutionStatus::Failed { error, retryable } => {
                NodeExecutionOutcome::Failure { error, retryable }
            }
            AdapterExecutionStatus::Cancelled { acknowledged } => {
                NodeExecutionOutcome::Cancelled { acknowledged }
            }
            AdapterExecutionStatus::Running | AdapterExecutionStatus::Suspended => return None,
        };

        Some(ResultReentry {
            source_principal_id: facade.adapter.adapter_identity().to_string(),
            graph_id: self.graph_id.clone(),
            node_id: node_id.to_string(),
            invocation_id: invocation_id.to_string(),
            generation: facade.generation,
            cursor,
            attempt_token: active.attempt_token.clone(),
            outcome,
        })
    }

    /// Perform a single drive step:
    /// 1. Pull next queue item.
    /// 2. Validate live recipient states and generations.
    /// 3. Dispatch to node facades.
    /// 4. Poll and re-enter settled invocations.
    pub fn drive_step(&mut self) -> Result<DriverStepOutcome, DriverError> {
        let mut outcome = DriverStepOutcome::default();
        self.prune_settled_invocations();

        // 1. Process one queued item if available
        if let Some(item) = self.queue.dequeue() {
            outcome.items_dispatched += 1;
            self.durable_cursor = self.durable_cursor.max(item.cursor);

            if let Ok(payload) = serde_json::from_value::<AdmittedMessagePayload>(item.content) {
                if payload.graph_id != self.graph_id {
                    return Err(DriverError::GraphMismatch {
                        expected: self.graph_id.clone(),
                        received: payload.graph_id,
                    });
                }
                if self.scope_barrier_active
                    && matches!(&payload.operation, ControlOperation::SubmitTask { .. })
                {
                    return Err(DriverError::ScopeAdmissionBarrierActive);
                }

                self.refresh_recipient_index(&payload.targets);
                let validations = payload.targets.validate_before_effect(&self.index);
                let op_str = format!("{:?}", payload.operation);

                for (recipient, status) in validations {
                    outcome.effects_attempted += 1;
                    match status {
                        RecipientEffectStatus::Valid => {
                            let mut inv_id = None;

                            if let Some(facade) = self.nodes.get_mut(&recipient.node_id) {
                                match &payload.operation {
                                    ControlOperation::SubmitTask { input } => {
                                        if let Ok(inv) = facade.submit(input.clone(), item.cursor) {
                                            inv_id = Some(inv.invocation_id.clone());
                                            self.active_invocations.insert(
                                                inv.invocation_id.clone(),
                                                recipient.node_id.clone(),
                                            );
                                            outcome.effects_succeeded += 1;
                                        }
                                    }
                                    ControlOperation::Steer {
                                        invocation_id,
                                        instruction,
                                        follow_up,
                                    } => {
                                        match facade.steer(
                                            invocation_id.as_str(),
                                            instruction.as_str(),
                                            *follow_up,
                                        ) {
                                            Ok(
                                                SteerResult::AppliedInFlight
                                                | SteerResult::FollowUpQueued { .. },
                                            ) => {
                                                outcome.effects_succeeded += 1;
                                            }
                                            Ok(SteerResult::Unsupported) | Err(_) => {}
                                        }
                                    }
                                    ControlOperation::Pause { .. } => match facade.pause() {
                                        Ok(PauseResult::Unsupported) | Err(_) => {}
                                        Ok(_) => outcome.effects_succeeded += 1,
                                    },
                                    ControlOperation::Resume { .. } if facade.resume().is_ok() => {
                                        outcome.effects_succeeded += 1;
                                    }
                                    ControlOperation::Stop { reason, .. }
                                        if facade.stop(reason.as_str()).is_ok() =>
                                    {
                                        outcome.effects_succeeded += 1;
                                    }
                                    _ => {
                                        // Other control operations handled at graph level
                                    }
                                }

                                // Update feature index with updated metadata
                                let meta = facade.metadata();
                                self.index.register_or_update(meta);
                            }

                            outcome.reports.push(DispatchedEffectReport {
                                delivery_id: recipient.delivery_id,
                                recipient_node_id: recipient.node_id,
                                status: RecipientEffectStatus::Valid,
                                operation: op_str.clone(),
                                invocation_id: inv_id,
                            });
                        }
                        RecipientEffectStatus::StateChanged { expected, current } => {
                            outcome.stale_recipients_skipped += 1;
                            outcome.reports.push(DispatchedEffectReport {
                                delivery_id: recipient.delivery_id,
                                recipient_node_id: recipient.node_id,
                                status: RecipientEffectStatus::StateChanged { expected, current },
                                operation: op_str.clone(),
                                invocation_id: None,
                            });
                        }
                        RecipientEffectStatus::GenerationStale { expected, current } => {
                            outcome.stale_recipients_skipped += 1;
                            outcome.reports.push(DispatchedEffectReport {
                                delivery_id: recipient.delivery_id,
                                recipient_node_id: recipient.node_id,
                                status: RecipientEffectStatus::GenerationStale {
                                    expected,
                                    current,
                                },
                                operation: op_str.clone(),
                                invocation_id: None,
                            });
                        }
                        RecipientEffectStatus::NodeMissing => {
                            outcome.stale_recipients_skipped += 1;
                            outcome.reports.push(DispatchedEffectReport {
                                delivery_id: recipient.delivery_id,
                                recipient_node_id: recipient.node_id,
                                status: RecipientEffectStatus::NodeMissing,
                                operation: op_str.clone(),
                                invocation_id: None,
                            });
                        }
                    }
                }
            }
        }

        // 2. Poll active invocations for backend completions
        let mut settled_reentries = Vec::new();
        let mut next_result_cursor = self.durable_cursor;
        for (inv_id, node_id) in &self.active_invocations {
            if let Some(facade) = self.nodes.get(node_id) {
                next_result_cursor = next_result_cursor.max(facade.durable_cursor);
                let active_matches = facade
                    .active_invocation
                    .as_ref()
                    .is_some_and(|invocation| invocation.invocation_id.as_str() == inv_id.as_str());
                if active_matches
                    && let Ok(status) = facade.adapter.poll_status(inv_id)
                    && matches!(
                        &status,
                        AdapterExecutionStatus::Completed { .. }
                            | AdapterExecutionStatus::Failed { .. }
                            | AdapterExecutionStatus::Cancelled { .. }
                    )
                {
                    next_result_cursor = next_result_cursor.saturating_add(1);
                    if let Some(reentry) =
                        self.result_reentry_from_status(inv_id, node_id, next_result_cursor, status)
                    {
                        settled_reentries.push(reentry);
                    }
                }
            }
        }

        for reentry in settled_reentries {
            self.reenter_result(reentry)?;
            outcome.completions_settled += 1;
        }

        Ok(outcome)
    }

    /// Re-enter an execution result carrying full source identity.
    /// Stale generations are rejected and cannot satisfy new dependencies.
    pub fn reenter_result(
        &mut self,
        reentry: ResultReentry,
    ) -> Result<NodeExecutionReceipt, DriverError> {
        if reentry.graph_id != self.graph_id {
            return Err(DriverError::GraphMismatch {
                expected: self.graph_id.clone(),
                received: reentry.graph_id,
            });
        }

        let facade = self
            .nodes
            .get(&reentry.node_id)
            .ok_or_else(|| DriverError::NodeNotFound(reentry.node_id.clone()))?;

        // The adapter identity is the authenticated result port for this
        // facade. A serialized source label alone cannot authorize re-entry.
        let expected_source = facade.adapter.adapter_identity().to_string();
        if expected_source != reentry.source_principal_id {
            return Err(DriverError::ResultSourceMismatch {
                node_id: reentry.node_id.clone(),
                expected: expected_source,
                received: reentry.source_principal_id,
            });
        }

        // Verify generation matches live node generation.
        if facade.generation != reentry.generation {
            return Err(DriverError::StaleGenerationResult {
                node_id: reentry.node_id.clone(),
                expected: facade.generation,
                received: reentry.generation,
            });
        }

        let active = facade.active_invocation.as_ref().ok_or_else(|| {
            DriverError::NodeExecution(
                if facade
                    .invocation_history
                    .iter()
                    .any(|record| record.invocation_id == reentry.invocation_id)
                {
                    NodeExecutionError::InvocationSettled(reentry.invocation_id.clone())
                } else {
                    NodeExecutionError::InvocationNotFound(reentry.invocation_id.clone())
                },
            )
        })?;

        if active.invocation_id != reentry.invocation_id {
            return Err(DriverError::InvocationMismatch {
                node_id: reentry.node_id.clone(),
                expected: active.invocation_id.clone(),
                received: reentry.invocation_id,
            });
        }
        if active.attempt_token != reentry.attempt_token {
            return Err(DriverError::AttemptTokenMismatch {
                node_id: reentry.node_id.clone(),
                expected: active.attempt_token.clone(),
                received: reentry.attempt_token,
            });
        }
        if reentry.cursor < facade.durable_cursor {
            return Err(DriverError::StaleResultCursor {
                node_id: reentry.node_id.clone(),
                current: facade.durable_cursor,
                received: reentry.cursor,
            });
        }

        let facade = self
            .nodes
            .get_mut(&reentry.node_id)
            .ok_or_else(|| DriverError::NodeNotFound(reentry.node_id.clone()))?;

        // Settle invocation completion on facade
        let receipt =
            facade.settle_invocation_completion(reentry.outcome.clone(), reentry.cursor)?;
        self.active_invocations.remove(&reentry.invocation_id);
        self.durable_cursor = self.durable_cursor.max(reentry.cursor);

        // Update index with new lifecycle state
        let meta = facade.metadata();
        self.index.register_or_update(meta);

        // Match subscriptions against this transition / event
        let scope = SubscriptionScope::Node {
            graph_id: self.graph_id.clone(),
            node_id: reentry.node_id.clone(),
        };
        let matched = self.subscriptions.match_event(
            &scope,
            &SubscriptionPredicate::OnLifecycleState(receipt.resulting_state),
            Some(receipt.resulting_state),
        );

        for sub in matched {
            match sub.activation_rule {
                ActivationRule::AutoResume => {
                    // Authorized auto-resume rule resumes paused node
                    if !self.scope_barrier_active
                        && let Some(facade) = self.nodes.get_mut(&reentry.node_id)
                        && facade.lifecycle_state == NodeLifecycleState::Paused
                    {
                        let _ = facade.resume();
                        let updated_meta = facade.metadata();
                        self.index.register_or_update(updated_meta);
                    }
                }
                ActivationRule::NotifyOnly => {}
                ActivationRule::TriggerTransition { .. } => {}
            }
        }

        Ok(receipt)
    }

    /// Graceful graph pause: establishes scope admission barrier and requests pause on all active nodes.
    /// Collects per-node outcomes without fabricating success for unavailable backends.
    pub fn pause_graph(&mut self) -> Result<GraphPauseReport, DriverError> {
        self.scope_barrier_active = true;
        let mut outcomes = Vec::new();

        for (node_id, facade) in &mut self.nodes {
            if facade.lifecycle_state.is_active() {
                let before = facade.lifecycle_state;
                let res = facade.pause().map_err(|e| e.to_string());
                let after = facade.lifecycle_state;
                outcomes.push(PerNodePauseOutcome {
                    node_id: node_id.clone(),
                    state_before: before,
                    state_after: after,
                    result: res,
                });
            }
        }

        // Update all metadata in index
        for facade in self.nodes.values() {
            self.index.register_or_update(facade.metadata());
        }

        Ok(GraphPauseReport {
            graph_id: self.graph_id.clone(),
            node_outcomes: outcomes,
            scope_barrier_established: true,
        })
    }

    /// Graceful graph stop: establishes scope admission barrier and cooperatively cancels all active nodes.
    /// Collects per-node outcomes and preserves requested, acknowledged, and effect-unknown facts.
    pub fn stop_graph(&mut self, reason: &str) -> Result<GraphStopReport, DriverError> {
        self.scope_barrier_active = true;
        let mut outcomes = Vec::new();

        for (node_id, facade) in &mut self.nodes {
            if facade.lifecycle_state.is_active() {
                let before = facade.lifecycle_state;
                let res = facade.stop(reason).map_err(|e| e.to_string());
                let after = facade.lifecycle_state;
                outcomes.push(PerNodeStopOutcome {
                    node_id: node_id.clone(),
                    state_before: before,
                    state_after: after,
                    cancellation_facts: facade.cancellation_facts.clone(),
                    result: res,
                });
            }
        }

        // Update all metadata in index
        for facade in self.nodes.values() {
            self.index.register_or_update(facade.metadata());
        }

        Ok(GraphStopReport {
            graph_id: self.graph_id.clone(),
            node_outcomes: outcomes,
            scope_barrier_established: true,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::adapter::{CooperativeDrainAdapter, SyntheticCapabilityAdapter};
    use crate::routing::{
        DeliveryMode, FrozenTargetRecipient, QueueBounds, Subscription, TargetSelector,
    };
    use serde_json::json;
    use std::sync::Arc;

    fn test_queue() -> FairDispatchQueue {
        FairDispatchQueue::new(
            QueueBounds {
                max_entries: 100,
                max_bytes: 1024 * 1024,
            },
            QueueBounds {
                max_entries: 100,
                max_bytes: 1024 * 1024,
            },
            QueueBounds {
                max_entries: 100,
                max_bytes: 1024 * 1024,
            },
            4,
        )
    }

    #[test]
    fn test_driver_step_dispatches_queued_item_and_updates_index() {
        let queue = test_queue();
        let subs = SubscriptionRegistry::new();
        let mut driver = ContinuousNodeDriver::new("graph-1", queue, subs);

        let adapter = Arc::new(SyntheticCapabilityAdapter::new("adapt-1"));
        let facade = NodeFacade::new("node-1", "graph-1", 1, "worker", adapter, None, None);
        driver.register_node(facade);

        // Verify index has node-1 as Ready
        let matches = driver
            .index
            .query(&TargetSelector::ExactNode("node-1".into()));
        assert_eq!(matches.len(), 1);

        // Enqueue task
        let targets = FrozenTargets {
            frozen_at_cursor: 1,
            mode: DeliveryMode::DispatchOne,
            recipients: vec![FrozenTargetRecipient {
                delivery_id: "del-1".into(),
                node_id: "node-1".into(),
                generation: 1,
                admitted_state: NodeLifecycleState::Ready,
            }],
        };

        driver
            .enqueue_admitted(
                ChannelKind::Data,
                "msg-1",
                AdmittedMessagePayload {
                    source_principal_id: "admin".into(),
                    graph_id: "graph-1".into(),
                    operation: ControlOperation::SubmitTask {
                        input: json!({"run": true}),
                    },
                    targets,
                },
                1000,
            )
            .unwrap();

        // Drive one step
        let outcome = driver.drive_step().unwrap();
        assert_eq!(outcome.items_dispatched, 1);
        assert_eq!(outcome.effects_succeeded, 1);

        // Node is now Running and feature index reflects Running
        let node = driver.get_node("node-1").unwrap();
        assert_eq!(node.lifecycle_state, NodeLifecycleState::Running);
        let running_nodes = driver
            .index
            .query(&TargetSelector::Lifecycle(NodeLifecycleState::Running));
        assert!(running_nodes.contains("node-1"));
    }

    #[test]
    fn test_driver_pre_effect_validation_skips_stale_or_missing_recipients() {
        let queue = test_queue();
        let subs = SubscriptionRegistry::new();
        let mut driver = ContinuousNodeDriver::new("graph-1", queue, subs);

        let adapter = Arc::new(SyntheticCapabilityAdapter::new("adapt-1"));
        // Registered at generation 2
        let facade = NodeFacade::new("node-recycled", "graph-1", 2, "worker", adapter, None, None);
        driver.register_node(facade);

        // Target was frozen with stale generation 1
        let targets = FrozenTargets {
            frozen_at_cursor: 1,
            mode: DeliveryMode::DispatchOne,
            recipients: vec![FrozenTargetRecipient {
                delivery_id: "del-stale".into(),
                node_id: "node-recycled".into(),
                generation: 1, // Stale!
                admitted_state: NodeLifecycleState::Ready,
            }],
        };

        driver
            .enqueue_admitted(
                ChannelKind::Data,
                "msg-stale",
                AdmittedMessagePayload {
                    source_principal_id: "admin".into(),
                    graph_id: "graph-1".into(),
                    operation: ControlOperation::SubmitTask {
                        input: json!({"work": 1}),
                    },
                    targets,
                },
                1000,
            )
            .unwrap();

        // Drive step
        let outcome = driver.drive_step().unwrap();
        assert_eq!(outcome.items_dispatched, 1);
        assert_eq!(outcome.stale_recipients_skipped, 1);
        assert_eq!(outcome.effects_succeeded, 0);

        // Node remains Ready; did NOT start work!
        let node = driver.get_node("node-recycled").unwrap();
        assert_eq!(node.lifecycle_state, NodeLifecycleState::Ready);
    }

    #[test]
    fn test_driver_rejects_wrong_graph_and_scope_barrier_submissions() {
        let queue = test_queue();
        let subs = SubscriptionRegistry::new();
        let mut driver = ContinuousNodeDriver::new("graph-guard", queue, subs);
        driver.register_node(NodeFacade::new(
            "node-guard",
            "graph-guard",
            1,
            "worker",
            Arc::new(SyntheticCapabilityAdapter::new("guard-adapter")),
            None,
            None,
        ));

        let targets = FrozenTargets {
            frozen_at_cursor: 1,
            mode: DeliveryMode::DispatchOne,
            recipients: vec![FrozenTargetRecipient {
                delivery_id: "del-guard".into(),
                node_id: "node-guard".into(),
                generation: 1,
                admitted_state: NodeLifecycleState::Ready,
            }],
        };

        driver
            .enqueue_admitted(
                ChannelKind::Data,
                "msg-barrier",
                AdmittedMessagePayload {
                    source_principal_id: "admin".into(),
                    graph_id: "graph-guard".into(),
                    operation: ControlOperation::SubmitTask {
                        input: json!({"blocked": true}),
                    },
                    targets: targets.clone(),
                },
                1000,
            )
            .unwrap();
        driver.scope_barrier_active = true;
        assert!(matches!(
            driver.drive_step(),
            Err(DriverError::ScopeAdmissionBarrierActive)
        ));
        assert_eq!(
            driver.get_node("node-guard").unwrap().lifecycle_state,
            NodeLifecycleState::Ready
        );

        driver.scope_barrier_active = false;
        driver
            .enqueue_admitted(
                ChannelKind::Data,
                "msg-wrong-graph",
                AdmittedMessagePayload {
                    source_principal_id: "admin".into(),
                    graph_id: "other-graph".into(),
                    operation: ControlOperation::SubmitTask {
                        input: json!({"blocked": true}),
                    },
                    targets,
                },
                1001,
            )
            .unwrap();
        assert!(matches!(
            driver.drive_step(),
            Err(DriverError::GraphMismatch { .. })
        ));
        assert_eq!(
            driver.get_node("node-guard").unwrap().lifecycle_state,
            NodeLifecycleState::Ready
        );
    }

    #[test]
    fn test_driver_result_reentry_with_source_identity() {
        let queue = test_queue();
        let subs = SubscriptionRegistry::new();
        let mut driver = ContinuousNodeDriver::new("graph-1", queue, subs);

        let adapter = Arc::new(SyntheticCapabilityAdapter::new("adapt-1"));
        let facade = NodeFacade::new("node-reentry", "graph-1", 1, "worker", adapter, None, None);
        driver.register_node(facade);

        // Start task on node
        let inv = driver
            .get_node_mut("node-reentry")
            .unwrap()
            .submit(json!({"compute": 123}), 1)
            .unwrap();

        // Re-enter successful result with matching source identity
        let receipt = driver
            .reenter_result(ResultReentry {
                source_principal_id: "adapt-1".into(),
                graph_id: "graph-1".into(),
                node_id: "node-reentry".into(),
                invocation_id: inv.invocation_id.clone(),
                generation: 1,
                cursor: 2,
                attempt_token: inv.attempt_token.clone(),
                outcome: NodeExecutionOutcome::Success {
                    output: json!({"result": 456}),
                },
            })
            .unwrap();

        assert_eq!(receipt.resulting_state, NodeLifecycleState::Ready);
        assert_eq!(driver.durable_cursor, 2);

        // Re-entering result with stale generation is rejected
        let stale_res = driver.reenter_result(ResultReentry {
            source_principal_id: "adapt-1".into(),
            graph_id: "graph-1".into(),
            node_id: "node-reentry".into(),
            invocation_id: "inv-old".into(),
            generation: 99, // Stale!
            cursor: 3,
            attempt_token: "att-old".into(),
            outcome: NodeExecutionOutcome::Success { output: json!({}) },
        });

        assert!(matches!(
            stale_res,
            Err(DriverError::StaleGenerationResult {
                expected: 1,
                received: 99,
                ..
            })
        ));
    }

    #[test]
    fn test_driver_result_reentry_rejects_unverified_invocation_identity() {
        let queue = test_queue();
        let subs = SubscriptionRegistry::new();
        let mut driver = ContinuousNodeDriver::new("graph-identity", queue, subs);
        let adapter = Arc::new(SyntheticCapabilityAdapter::new("verified-adapter"));
        driver.register_node(NodeFacade::new(
            "node-identity",
            "graph-identity",
            1,
            "worker",
            adapter,
            None,
            None,
        ));
        let invocation = driver
            .get_node_mut("node-identity")
            .unwrap()
            .submit(json!({"work": true}), 1)
            .unwrap();

        let make_reentry =
            |source: &str, invocation_id: &str, attempt_token: &str, cursor: u64| ResultReentry {
                source_principal_id: source.to_string(),
                graph_id: "graph-identity".to_string(),
                node_id: "node-identity".to_string(),
                invocation_id: invocation_id.to_string(),
                generation: 1,
                cursor,
                attempt_token: attempt_token.to_string(),
                outcome: NodeExecutionOutcome::Success {
                    output: json!({"ok": true}),
                },
            };

        assert!(matches!(
            driver.reenter_result(make_reentry(
                "forged-adapter",
                &invocation.invocation_id,
                &invocation.attempt_token,
                2,
            )),
            Err(DriverError::ResultSourceMismatch { .. })
        ));
        assert!(matches!(
            driver.reenter_result(make_reentry(
                "verified-adapter",
                "inv-other",
                &invocation.attempt_token,
                2,
            )),
            Err(DriverError::InvocationMismatch { .. })
        ));
        assert!(matches!(
            driver.reenter_result(make_reentry(
                "verified-adapter",
                &invocation.invocation_id,
                "attempt-other",
                2,
            )),
            Err(DriverError::AttemptTokenMismatch { .. })
        ));
        assert!(matches!(
            driver.reenter_result(make_reentry(
                "verified-adapter",
                &invocation.invocation_id,
                &invocation.attempt_token,
                0,
            )),
            Err(DriverError::StaleResultCursor { .. })
        ));

        let receipt = driver
            .reenter_result(make_reentry(
                "verified-adapter",
                &invocation.invocation_id,
                &invocation.attempt_token,
                2,
            ))
            .unwrap();
        assert_eq!(receipt.resulting_state, NodeLifecycleState::Ready);
    }

    #[test]
    fn test_driver_independent_node_concurrency() {
        // In a graph with A->C and independent B:
        // C can start when A finishes while B is still running!
        let queue = test_queue();
        let subs = SubscriptionRegistry::new();
        let mut driver = ContinuousNodeDriver::new("graph-concur", queue, subs);

        let adapt_a = Arc::new(SyntheticCapabilityAdapter::new("adapt-a"));
        let adapt_b = Arc::new(SyntheticCapabilityAdapter::new("adapt-b"));
        let adapt_c = Arc::new(SyntheticCapabilityAdapter::new("adapt-c"));

        driver.register_node(NodeFacade::new(
            "node-A",
            "graph-concur",
            1,
            "task-a",
            adapt_a,
            None,
            None,
        ));
        driver.register_node(NodeFacade::new(
            "node-B",
            "graph-concur",
            1,
            "task-b",
            adapt_b,
            None,
            None,
        ));
        driver.register_node(NodeFacade::new(
            "node-C",
            "graph-concur",
            1,
            "task-c",
            adapt_c,
            None,
            None,
        ));

        // Submit to A and B
        let inv_a = driver
            .get_node_mut("node-A")
            .unwrap()
            .submit(json!({"step": "A"}), 1)
            .unwrap();
        let _inv_b = driver
            .get_node_mut("node-B")
            .unwrap()
            .submit(json!({"step": "B"}), 2)
            .unwrap();

        // Both A and B are Running
        assert_eq!(
            driver.get_node("node-A").unwrap().lifecycle_state,
            NodeLifecycleState::Running
        );
        assert_eq!(
            driver.get_node("node-B").unwrap().lifecycle_state,
            NodeLifecycleState::Running
        );
        assert_eq!(
            driver.get_node("node-C").unwrap().lifecycle_state,
            NodeLifecycleState::Ready
        );

        // Settle A
        driver
            .reenter_result(ResultReentry {
                source_principal_id: "adapt-a".into(),
                graph_id: "graph-concur".into(),
                node_id: "node-A".into(),
                invocation_id: inv_a.invocation_id,
                generation: 1,
                cursor: 3,
                attempt_token: inv_a.attempt_token,
                outcome: NodeExecutionOutcome::Success {
                    output: json!({"a": "done"}),
                },
            })
            .unwrap();

        // A is Ready (idle), B is STILL Running, C starts!
        assert_eq!(
            driver.get_node("node-A").unwrap().lifecycle_state,
            NodeLifecycleState::Ready
        );
        assert_eq!(
            driver.get_node("node-B").unwrap().lifecycle_state,
            NodeLifecycleState::Running
        );

        let _inv_c = driver
            .get_node_mut("node-C")
            .unwrap()
            .submit(json!({"step": "C", "input_from_a": "done"}), 4)
            .unwrap();

        // Now C is Running concurrently while B is still Running!
        assert_eq!(
            driver.get_node("node-C").unwrap().lifecycle_state,
            NodeLifecycleState::Running
        );
        assert_eq!(
            driver.get_node("node-B").unwrap().lifecycle_state,
            NodeLifecycleState::Running
        );
    }

    #[test]
    fn test_driver_assigns_distinct_cursors_to_independent_completions() {
        let queue = test_queue();
        let subs = SubscriptionRegistry::new();
        let mut driver = ContinuousNodeDriver::new("graph-cursors", queue, subs);
        let adapter_a = Arc::new(SyntheticCapabilityAdapter::new("cursor-a"));
        let adapter_b = Arc::new(SyntheticCapabilityAdapter::new("cursor-b"));
        driver.register_node(NodeFacade::new(
            "node-a",
            "graph-cursors",
            1,
            "worker",
            adapter_a.clone(),
            None,
            None,
        ));
        driver.register_node(NodeFacade::new(
            "node-b",
            "graph-cursors",
            1,
            "worker",
            adapter_b.clone(),
            None,
            None,
        ));
        let invocation_a = driver
            .get_node_mut("node-a")
            .unwrap()
            .submit(json!({"node": "a"}), 1)
            .unwrap();
        let invocation_b = driver
            .get_node_mut("node-b")
            .unwrap()
            .submit(json!({"node": "b"}), 1)
            .unwrap();
        driver
            .active_invocations
            .insert(invocation_a.invocation_id.clone(), "node-a".to_string());
        driver
            .active_invocations
            .insert(invocation_b.invocation_id.clone(), "node-b".to_string());
        adapter_a.set_status(
            &invocation_a.invocation_id,
            AdapterExecutionStatus::Completed {
                output: json!({"done": "a"}),
            },
        );
        adapter_b.set_status(
            &invocation_b.invocation_id,
            AdapterExecutionStatus::Completed {
                output: json!({"done": "b"}),
            },
        );

        let outcome = driver.drive_step().unwrap();
        assert_eq!(outcome.completions_settled, 2);
        assert_eq!(driver.durable_cursor, 3);
        assert!(driver.active_invocations.is_empty());
    }

    #[test]
    fn test_driver_graceful_graph_pause_and_stop_collects_per_node_outcomes() {
        let queue = test_queue();
        let subs = SubscriptionRegistry::new();
        let mut driver = ContinuousNodeDriver::new("graph-lifecycle", queue, subs);

        let adapt_1 = Arc::new(CooperativeDrainAdapter::new("coop-1"));
        let adapt_2 = Arc::new(CooperativeDrainAdapter::new("coop-2"));

        driver.register_node(NodeFacade::new(
            "node-1",
            "graph-lifecycle",
            1,
            "worker",
            adapt_1,
            None,
            None,
        ));
        driver.register_node(NodeFacade::new(
            "node-2",
            "graph-lifecycle",
            1,
            "worker",
            adapt_2,
            None,
            None,
        ));
        // Start node-1 running, leave node-2 ready
        let invocation = driver
            .get_node_mut("node-1")
            .unwrap()
            .submit(json!({}), 1)
            .unwrap();
        driver
            .active_invocations
            .insert(invocation.invocation_id, "node-1".to_string());

        // Pause graph
        let pause_report = driver.pause_graph().unwrap();
        assert!(pause_report.scope_barrier_established);
        assert_eq!(pause_report.node_outcomes.len(), 2);

        // Node 1 was running -> entered PauseRequested (draining to safe boundary)
        let o1 = pause_report
            .node_outcomes
            .iter()
            .find(|o| o.node_id == "node-1")
            .unwrap();
        assert_eq!(o1.state_before, NodeLifecycleState::Running);
        assert_eq!(o1.state_after, NodeLifecycleState::PauseRequested);

        // Node 2 was ready -> directly entered Paused
        let o2 = pause_report
            .node_outcomes
            .iter()
            .find(|o| o.node_id == "node-2")
            .unwrap();
        assert_eq!(o2.state_before, NodeLifecycleState::Ready);
        assert_eq!(o2.state_after, NodeLifecycleState::Paused);

        // Stop graph
        let stop_report = driver.stop_graph("shutdown").unwrap();
        assert!(stop_report.scope_barrier_established);
        assert_eq!(stop_report.node_outcomes.len(), 2);

        for outcome in stop_report.node_outcomes {
            assert_eq!(outcome.state_after, NodeLifecycleState::Stopped);
            let facts = outcome.cancellation_facts.unwrap();
            assert!(facts.requested);
            assert!(facts.acknowledged);
            assert_eq!(facts.reason, "shutdown");
        }

        // The acknowledged stop settled node-1 before the next poll; the
        // driver must prune that map entry rather than re-entering twice.
        assert!(driver.drive_step().is_ok());
        assert!(driver.active_invocations.is_empty());
    }

    #[test]
    fn test_driver_subscription_auto_resume_on_matching_event() {
        let queue = test_queue();
        let mut subs = SubscriptionRegistry::new();

        // Register auto-resume subscription on node-paused when it enters Paused
        subs.subscribe(Subscription {
            subscription_id: "sub-auto-resume".into(),
            subscriber_id: "subscriber-1".into(),
            scope: SubscriptionScope::Node {
                graph_id: "graph-sub".into(),
                node_id: "node-paused".into(),
            },
            predicate: SubscriptionPredicate::OnLifecycleState(NodeLifecycleState::Paused),
            activation_rule: ActivationRule::AutoResume,
            durable_cursor: 0,
            is_control: false,
            active: true,
        });

        let mut driver = ContinuousNodeDriver::new("graph-sub", queue, subs);

        let adapter = Arc::new(CooperativeDrainAdapter::new("coop"));
        let facade = NodeFacade::new("node-paused", "graph-sub", 1, "worker", adapter, None, None);
        driver.register_node(facade);

        // Submit task to node
        let inv = driver
            .get_node_mut("node-paused")
            .unwrap()
            .submit(json!({"task": 1}), 1)
            .unwrap();

        // Request pause on running node: enters PauseRequested
        let pause_res = driver.get_node_mut("node-paused").unwrap().pause().unwrap();
        assert!(matches!(pause_res, PauseResult::DrainingToSafeBoundary));
        assert_eq!(
            driver.get_node("node-paused").unwrap().lifecycle_state,
            NodeLifecycleState::PauseRequested
        );

        // An invocation completion arrives: settles invocation, reaches safe boundary -> Paused,
        // and triggers the registered auto-resume subscription!
        let receipt = driver
            .reenter_result(ResultReentry {
                source_principal_id: "coop".into(),
                graph_id: "graph-sub".into(),
                node_id: "node-paused".into(),
                invocation_id: inv.invocation_id,
                generation: 1,
                cursor: 5,
                attempt_token: inv.attempt_token,
                outcome: NodeExecutionOutcome::Success {
                    output: json!({"wake": true}),
                },
            })
            .unwrap();

        assert_eq!(receipt.resulting_state, NodeLifecycleState::Paused);

        // The auto-resume rule triggered on Paused, resuming the node to Ready!
        let node = driver.get_node("node-paused").unwrap();
        assert_eq!(node.lifecycle_state, NodeLifecycleState::Ready);
    }
}