greentic-runner-host 1.2.0

Host runtime shim for Greentic runner: config, pack loading, activity handling
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
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;

use anyhow::{Context, Result, anyhow, bail};
use serde_json::Value;

use crate::activity::{Activity, WelcomeFlowHint};
use crate::boot;
use crate::component_api::node::{ExecCtx as ComponentExecCtx, TenantCtx as ComponentTenantCtx};
use crate::config::{Fast2FlowRoutingConfig, HostConfig};
use crate::engine::host::{SessionHost, StateHost};
use crate::engine::runtime::{FlowResumeStore, IngressEnvelope};
#[cfg(feature = "greentic-x-provider")]
use crate::greentic_x_provider::RunnerPackFast2FlowRoutingProvider;
use crate::http::health::HealthState;
use crate::pack::{IdentifyOutcome, PackRuntime};
use crate::runner::adapt_timer;
use crate::runner::engine::FlowEngine;
use crate::runtime::{ActivePacks, TenantRuntime};
use crate::secrets::{DynSecretsManager, default_manager};
use crate::storage::{
    DynSessionStore, DynStateStore, StorageConfig, session_host_from, state_host_from,
    stores_from_config,
};
use crate::wasi::RunnerWasiPolicy;
use greentic_deploy_spec::ids::{BundleId, DeploymentId, RevisionId};
#[cfg(feature = "greentic-x-provider")]
use greentic_x_runtime::{
    Fast2FlowDirective, Fast2FlowMessageEnvelope, Fast2FlowRouteRequest, Fast2FlowRoutingProvider,
};

#[derive(Clone, Debug)]
pub struct TelemetryCfg {
    pub config: greentic_telemetry::TelemetryConfig,
    pub export: greentic_telemetry::export::ExportConfig,
}

/// An LLM port an embedding host can inject so in-process tool extensions that
/// call `host.llm.complete()` (e.g. adaptive-cards `generate_card`) resolve the
/// LLM via the host's own per-tenant, admin-backed path instead of the env-only
/// fallback. Only meaningful with the agentic-worker runtime, so the type — and
/// everything that threads it — is gated behind that feature.
#[cfg(feature = "agentic-worker")]
pub type ExtLlmPort = Arc<dyn greentic_ext_runtime::host_ports::LlmPort>;

/// An MCP tool source an embedding host can inject so `mcp` flow nodes resolve
/// their catalog through the host's own per-tenant admin path instead of the
/// process-global `GREENTIC_AW_*` environment. See
/// [`HostBuilder::with_mcp_source`].
#[cfg(feature = "agentic-worker")]
pub type McpSource = Arc<greentic_aw_runtime::McpToolSource>;

/// Builder for composing multi-tenant host instances.
pub struct HostBuilder {
    configs: HashMap<String, HostConfig>,
    telemetry: Option<TelemetryCfg>,
    wasi_policy: RunnerWasiPolicy,
    secrets: Option<DynSecretsManager>,
    storage: StorageConfig,
    #[cfg(feature = "agentic-worker")]
    ext_llm_port: Option<ExtLlmPort>,
    #[cfg(feature = "agentic-worker")]
    mcp_source: Option<McpSource>,
}

impl HostBuilder {
    pub fn new() -> Self {
        Self {
            configs: HashMap::new(),
            telemetry: None,
            wasi_policy: RunnerWasiPolicy::default(),
            secrets: None,
            storage: StorageConfig::default(),
            #[cfg(feature = "agentic-worker")]
            ext_llm_port: None,
            #[cfg(feature = "agentic-worker")]
            mcp_source: None,
        }
    }

    pub fn with_config(mut self, config: HostConfig) -> Self {
        self.configs.insert(config.tenant.clone(), config);
        self
    }

    pub fn with_telemetry(mut self, telemetry: TelemetryCfg) -> Self {
        self.telemetry = Some(telemetry);
        self
    }

    pub fn with_wasi_policy(mut self, policy: RunnerWasiPolicy) -> Self {
        self.wasi_policy = policy;
        self
    }

    pub fn with_secrets_manager(mut self, manager: DynSecretsManager) -> Self {
        self.secrets = Some(manager);
        self
    }

    /// Choose where this host keeps its sessions and flow state.
    ///
    /// Unset means [`StorageConfig::default`] — both stores in memory, which is
    /// what every caller got before this method existed. A parked flow then
    /// dies with the process: correct for a desktop run, wrong for a deployed
    /// worker, where a revision rollout or a cold start silently restarts the
    /// conversation.
    ///
    /// A configured-but-unreachable backend makes [`HostBuilder::build`] fail.
    /// It never degrades to memory — a host that quietly forgets every parked
    /// conversation looks healthy from every angle except the user's.
    ///
    /// ```no_run
    /// # use greentic_runner_host::{HostBuilder, HostConfig};
    /// # use greentic_runner_host::storage::StorageConfig;
    /// # fn main() -> anyhow::Result<()> {
    /// # let config: HostConfig = unimplemented!();
    /// let storage = StorageConfig::redis("redis://redis:6379", "greentic:session:prod")?;
    /// let host = HostBuilder::new().with_config(config).with_storage(storage).build()?;
    /// # let _ = host;
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_storage(mut self, storage: StorageConfig) -> Self {
        self.storage = storage;
        self
    }

    /// Inject a host-provided LLM port for the in-process extension runtime.
    ///
    /// When `Some`, tool extensions that call `host.llm.complete()` resolve the
    /// LLM through this port (the embedding host's per-tenant, admin-backed
    /// path) instead of the env-only fallback. When `None`, the extension
    /// runtime falls back to the env-keyed `EnvLlmPort` (standalone runners) —
    /// so leaving this unset preserves the prior behaviour exactly.
    #[cfg(feature = "agentic-worker")]
    pub fn with_ext_llm_port(mut self, port: Option<ExtLlmPort>) -> Self {
        self.ext_llm_port = port;
        self
    }

    /// Inject a host-built MCP tool source for this host's flow engines.
    ///
    /// When `Some`, `mcp` flow nodes resolve their catalog through it. When
    /// `None`, each engine keeps deriving one from the process-global
    /// `GREENTIC_AW_*` environment — the standalone runner path, unchanged.
    ///
    /// A host that serves many tenants from one process must inject: the
    /// environment names a single tenant, so it cannot express a per-tenant
    /// catalog. Build one source per tenant, identified with
    /// `greentic_aw_runtime::McpCallerIdentity`.
    #[cfg(feature = "agentic-worker")]
    pub fn with_mcp_source(mut self, source: Option<McpSource>) -> Self {
        self.mcp_source = source;
        self
    }

    pub fn build(self) -> Result<RunnerHost> {
        if self.configs.is_empty() {
            bail!("at least one tenant configuration is required");
        }
        let wasi_policy = Arc::new(self.wasi_policy);
        let configs = self
            .configs
            .into_iter()
            .map(|(tenant, cfg)| (tenant, Arc::new(cfg)))
            .collect();
        let (session_store, state_store) = stores_from_config(&self.storage)?;
        // `SessionStoreHost` is deliberately NOT pointed at a durable store.
        //
        // It is write-only by construction: `put` goes through
        // `SessionStore::create_session`, which does not add the row to the
        // per-user wait index — and `get` reads exactly that index, via
        // `find_by_user`. Both backends behave this way, so `get` never returns
        // what `put` wrote, `state_machine::step`'s `is_new` is always true, and
        // every turn writes a fresh row under a random UUID.
        //
        // In memory that is a bounded-by-process-lifetime map nobody reads. On
        // Redis it would be one permanent key PER TURN — `create_session` takes
        // no TTL argument, so there is no expiry to attach and the
        // `DefaultWaitTtl` decorator cannot reach it — growing without bound
        // while buying nothing, because nothing can read it back.
        //
        // The parked-flow snapshot, which is what durability is for, does NOT
        // travel this path: `FlowResumeStore` uses `register_wait` /
        // `find_wait_by_scope` and gets `session_store` below. Closing the
        // write-only behaviour itself is an upstream change in greentic-session.
        let session_host = if self.storage.session.is_durable() {
            session_host_from(crate::storage::new_session_store())
        } else {
            // Unchanged for every caller that names no storage: one store,
            // shared, exactly as before.
            session_host_from(Arc::clone(&session_store))
        };
        let state_host = state_host_from(Arc::clone(&state_store));
        if self.storage.is_durable() {
            // Names, never the configured values: a Redis URL may carry
            // `redis://user:password@host`, and a startup log line is the last
            // place a credential should land.
            tracing::info!(
                session = self.storage.session.describe(),
                state = self.storage.state.describe(),
                "runner host using durable storage; parked flows survive a restart"
            );
        }
        let secrets = match self.secrets {
            Some(manager) => manager,
            None => default_manager().context("failed to initialise default secrets backend")?,
        };
        Ok(RunnerHost {
            configs,
            active: Arc::new(ActivePacks::new()),
            health: Arc::new(HealthState::new()),
            session_store,
            state_store,
            session_host,
            state_host,
            wasi_policy,
            secrets_manager: secrets,
            #[cfg(feature = "agentic-worker")]
            ext_llm_port: self.ext_llm_port,
            #[cfg(feature = "agentic-worker")]
            mcp_source: self.mcp_source,
            #[cfg(feature = "agentic-worker")]
            stream_observers: Arc::new(dashmap::DashMap::new()),
            telemetry: self.telemetry,
        })
    }
}

impl Default for HostBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of [`RunnerHost::handle_activity_traced`]: the same outbound replies
/// as [`RunnerHost::handle_activity`], plus this turn's per-step node outputs
/// (keyed by each step's `operation`; for a `dw.agent` step that is the
/// agent_id). Observability only.
pub struct TurnTrace {
    pub replies: Vec<Activity>,
    pub node_outputs: HashMap<String, Value>,
}

/// Outcome of [`RunnerHost::build_prepared`]: either a fast2flow short-circuit
/// (a pre-built response returned without entering the state machine) or a
/// canonical ingress envelope the caller runs through `handle`/`handle_traced`.
enum Prepared {
    ShortCircuit(Vec<Activity>),
    Run(Box<IngressEnvelope>),
}

/// Runtime host that manages tenant-bound packs and flow execution.
pub struct RunnerHost {
    configs: HashMap<String, Arc<HostConfig>>,
    active: Arc<ActivePacks>,
    health: Arc<HealthState>,
    session_store: DynSessionStore,
    state_store: DynStateStore,
    session_host: Arc<dyn SessionHost>,
    state_host: Arc<dyn StateHost>,
    wasi_policy: Arc<RunnerWasiPolicy>,
    secrets_manager: DynSecretsManager,
    #[cfg(feature = "agentic-worker")]
    ext_llm_port: Option<ExtLlmPort>,
    /// MCP tool source injected by an embedding host, or `None` to let each
    /// flow engine derive one from `GREENTIC_AW_*` env (standalone runners).
    #[cfg(feature = "agentic-worker")]
    mcp_source: Option<McpSource>,
    /// Session-id → active streaming observer, shared by every `dw.agent`
    /// node handler this host builds (writer: the `POST /agent/chat/stream`
    /// SSE handler via `ServerState`; reader: `RuntimeAgentNodeHandler::execute`).
    /// Always present (an empty registry, not `None`) — no external
    /// dependency gates it, unlike `ext_llm_port`.
    #[cfg(feature = "agentic-worker")]
    stream_observers: crate::http::agent_stream::StreamObserverRegistry,
    telemetry: Option<TelemetryCfg>,
}

/// Handle exposing tenant internals for embedding hosts (e.g. CLI server).
#[derive(Clone)]
pub struct TenantHandle {
    runtime: Arc<TenantRuntime>,
}

impl RunnerHost {
    pub async fn start(&self) -> Result<()> {
        boot::init(&self.health, self.telemetry.as_ref())?;
        Ok(())
    }

    pub async fn stop(&self) -> Result<()> {
        self.active.replace(HashMap::new());
        Ok(())
    }

    pub async fn load_pack(&self, tenant: &str, pack_path: &Path) -> Result<()> {
        let archive_source = if is_pack_archive(pack_path) {
            Some(pack_path)
        } else {
            None
        };
        let runtime = self
            .prepare_runtime(tenant, pack_path, archive_source)
            .await
            .with_context(|| format!("failed to load tenant {tenant}"))?;
        self.active.insert_pack(tenant, runtime);
        tracing::info!(tenant, pack = %pack_path.display(), "pack loaded");
        Ok(())
    }

    pub async fn handle_activity(&self, tenant: &str, activity: Activity) -> Result<Vec<Activity>> {
        let (runtime, prepared) = self.prepare_turn(tenant, activity).await?;
        match prepared {
            Prepared::ShortCircuit(replies) => Ok(replies),
            Prepared::Run(envelope) => {
                let result = runtime.state_machine().handle(*envelope).await?;
                Ok(normalize_replies(result, tenant))
            }
        }
    }

    /// Like [`Self::handle_activity`], but also returns this turn's per-step
    /// node-output map (keyed by each step's `operation`; for a `dw.agent` step
    /// that is the agent_id). Observability only — `replies` are byte-identical
    /// to [`Self::handle_activity`] for the same input.
    pub async fn handle_activity_traced(
        &self,
        tenant: &str,
        activity: Activity,
    ) -> Result<TurnTrace> {
        let (runtime, prepared) = self.prepare_turn(tenant, activity).await?;
        match prepared {
            Prepared::ShortCircuit(replies) => Ok(TurnTrace {
                replies,
                node_outputs: HashMap::new(),
            }),
            Prepared::Run(envelope) => {
                let (result, node_outputs) =
                    runtime.state_machine().handle_traced(*envelope).await?;
                let replies = normalize_replies(result, tenant);
                let node_outputs = match node_outputs {
                    Value::Object(map) => map.into_iter().collect(),
                    _ => HashMap::new(),
                };
                Ok(TurnTrace {
                    replies,
                    node_outputs,
                })
            }
        }
    }

    /// Shared setup for [`Self::handle_activity`] / [`Self::handle_activity_traced`]:
    /// resolve the tenant runtime and build the canonical ingress envelope. The
    /// single seam guarantees the traced and untraced paths cannot diverge.
    async fn prepare_turn(
        &self,
        tenant: &str,
        activity: Activity,
    ) -> Result<(Arc<TenantRuntime>, Prepared)> {
        let runtime = self
            .active
            .load_pack(tenant)
            .with_context(|| format!("tenant {tenant} not loaded"))?;
        let prepared = self.build_prepared(&runtime, tenant, activity).await?;
        Ok((runtime, prepared))
    }

    /// Execute an activity against a specific deployment/bundle/revision runtime.
    ///
    /// Unlike [`handle_activity`](Self::handle_activity), which resolves the
    /// tenant-only (legacy) runtime, this targets a fully-qualified revision
    /// entry inserted by [`ActivePacks::insert_revision`]. A tenant can host
    /// several concurrent revisions under a traffic split, so the legacy
    /// tenant-only lookup cannot disambiguate them — the ingress revision
    /// dispatcher selects the revision and calls this.
    ///
    /// # Session isolation contract
    ///
    /// This method runs the selected revision's runtime against **whatever
    /// session/state stores that runtime was built with** (at
    /// [`TenantRuntime::load_revision`] time). It does *not* add a revision
    /// dimension to the session key: the session/resume/state backend keys on
    /// `(env, tenant, user)` plus pack/flow, **not** on the revision. If two
    /// live revisions of the same pack for one tenant share a single session
    /// backend, a `wait`/resume snapshot created by revision A can be fetched —
    /// or clobbered — by revision B during a traffic split, retry, or
    /// rebalance, resuming a snapshot against a different flow graph.
    ///
    /// Callers that load more than one revision per tenant onto one host
    /// (i.e. every traffic-split producer) **MUST give each revision an
    /// isolated session and state store** (a per-revision store instance, or a
    /// revision-namespaced backend) when calling `load_revision`. The shared
    /// `RunnerHost` stores (`session_store()`/`state_store()`) are only safe to
    /// reuse across revisions when at most one revision is ever live per
    /// tenant. The greentic-start activation path enforces this.
    pub async fn handle_activity_for_revision(
        &self,
        tenant: &str,
        deployment_id: DeploymentId,
        bundle_id: BundleId,
        revision_id: RevisionId,
        activity: Activity,
    ) -> Result<Vec<Activity>> {
        let runtime = self
            .active
            .load_revision(tenant, deployment_id, bundle_id, revision_id)
            .with_context(|| {
                format!(
                    "revision runtime not loaded for tenant {tenant} \
                     (deployment {deployment_id}, revision {revision_id})"
                )
            })?;
        self.dispatch_activity(&runtime, tenant, activity).await
    }

    /// Resolve the per-revision tenant runtime, attaching a uniform "not
    /// loaded" context to the error. The three per-revision identify
    /// fan-out APIs all need this exact lookup; sharing it keeps the
    /// error chain identical across them.
    fn load_revision_runtime(
        &self,
        tenant: &str,
        deployment_id: DeploymentId,
        bundle_id: BundleId,
        revision_id: RevisionId,
    ) -> Result<Arc<crate::runtime::TenantRuntime>> {
        self.active
            .load_revision(tenant, deployment_id, bundle_id, revision_id)
            .with_context(|| {
                format!(
                    "revision runtime not loaded for tenant {tenant} \
                     (deployment {deployment_id}, revision {revision_id})"
                )
            })
    }

    /// Per-revision per-`provider_type` `identify-instance` probe (M1 IID.4).
    ///
    /// Given the candidate `provider_types` an env declares messaging
    /// endpoints for, ask each pack loaded under this revision (main +
    /// overlays) which `provider_id` the inbound `payload` claims to address.
    /// The greentic-start resolver pairs the returned `provider_id` with the
    /// `provider_type` and looks the `MessagingEndpointId` up in the env's
    /// admit table; that's how a header-less webhook gets auto-routed to the
    /// right endpoint.
    ///
    /// `payload` is forwarded opaque to every probed component. The M1
    /// IID.4d wrapper convention from `greentic-start` is
    /// `{headers: [{name,value}], body: <parsed-or-null>}`. See the WIT
    /// docstring on `greentic:provider-instance-identity@0.1.0/identify-instance`
    /// for the full contract.
    ///
    /// This is the unscoped legacy API; new callers should use
    /// [`identify_messaging_endpoints_for_revision_scoped`] for per-provider
    /// header allowlist scoping (Phase D). Merge lattice:
    /// `Identified > NoMatch > Unsupported` — first pack to `Identified`
    /// wins and that type drops out of remaining probing.
    ///
    /// The per-pack loop is inlined (rather than factored into a shared
    /// `AsyncFnMut`-based helper) deliberately: routing the loop through an
    /// `AsyncFnMut` closure destabilises HRTB `Send` inference for
    /// downstream consumers spawning the returned future (greentic-start's
    /// hyper `service_fn`). The `Send`-bound test
    /// [`identify_futures_are_send`] guards against silent regression.
    ///
    /// [`identify_messaging_endpoints_for_revision_scoped`]:
    ///     RunnerHost::identify_messaging_endpoints_for_revision_scoped
    pub async fn identify_messaging_endpoints_for_revision(
        &self,
        tenant: &str,
        deployment_id: DeploymentId,
        bundle_id: BundleId,
        revision_id: RevisionId,
        provider_types: &[&str],
        payload: &[u8],
    ) -> Result<HashMap<String, IdentifyOutcome>> {
        if provider_types.is_empty() {
            return Ok(HashMap::new());
        }
        let runtime = self.load_revision_runtime(tenant, deployment_id, bundle_id, revision_id)?;
        // Seed every type at Unsupported — the floor of the merge lattice
        // (see `IdentifyOutcome::merge_in`).
        let mut merged: HashMap<String, IdentifyOutcome> = provider_types
            .iter()
            .map(|ty| ((*ty).to_string(), IdentifyOutcome::Unsupported))
            .collect();
        for pack in runtime.all_packs() {
            // Skip types already at the lattice top — no probe could improve them.
            let remaining: Vec<&str> = provider_types
                .iter()
                .copied()
                .filter(|ty| !matches!(merged.get(*ty), Some(IdentifyOutcome::Identified(_))))
                .collect();
            if remaining.is_empty() {
                break;
            }
            let probe = pack
                .identify_endpoints_by_provider_type(&remaining, payload)
                .await?;
            for (ty, outcome) in probe {
                if let Some(existing) = merged.get_mut(&ty) {
                    existing.merge_in(outcome);
                }
            }
        }
        Ok(merged)
    }

    /// Per-provider scoped variant of
    /// [`identify_messaging_endpoints_for_revision`].
    ///
    /// The wrapper is built **per-provider** from the component's cached
    /// `describe-identify-instance` hint (see
    /// [`PackRuntime::resolve_identify_hint`]): hinted components receive
    /// ONLY the headers their hint declares; unhinted components receive
    /// every header the caller passed in (back-compat).
    ///
    /// Loop inlined for the same reason as
    /// [`identify_messaging_endpoints_for_revision`].
    ///
    /// [`identify_messaging_endpoints_for_revision`]:
    ///     RunnerHost::identify_messaging_endpoints_for_revision
    #[allow(clippy::too_many_arguments)]
    pub async fn identify_messaging_endpoints_for_revision_scoped(
        &self,
        tenant: &str,
        deployment_id: DeploymentId,
        bundle_id: BundleId,
        revision_id: RevisionId,
        provider_types: &[&str],
        headers: &[(String, String)],
        body: &Value,
    ) -> Result<HashMap<String, IdentifyOutcome>> {
        if provider_types.is_empty() {
            return Ok(HashMap::new());
        }
        let runtime = self.load_revision_runtime(tenant, deployment_id, bundle_id, revision_id)?;
        let mut merged: HashMap<String, IdentifyOutcome> = provider_types
            .iter()
            .map(|ty| ((*ty).to_string(), IdentifyOutcome::Unsupported))
            .collect();
        for pack in runtime.all_packs() {
            let remaining: Vec<&str> = provider_types
                .iter()
                .copied()
                .filter(|ty| !matches!(merged.get(*ty), Some(IdentifyOutcome::Identified(_))))
                .collect();
            if remaining.is_empty() {
                break;
            }
            let probe = pack
                .identify_endpoints_by_provider_type_scoped(&remaining, headers, body)
                .await?;
            for (ty, outcome) in probe {
                if let Some(existing) = merged.get_mut(&ty) {
                    existing.merge_in(outcome);
                }
            }
        }
        Ok(merged)
    }

    /// Per-revision describe-identify-instance hint discovery.
    ///
    /// Fans the cached describe probe out across main pack + overlays;
    /// first non-`None` hint per `provider_type` wins. Lets callers inspect
    /// the per-provider header allowlist without running the expensive
    /// identify-instance probe. `None` value means no pack in this revision
    /// exposes a usable hint for that `provider_type`.
    ///
    /// Loop inlined for the same reason as
    /// [`identify_messaging_endpoints_for_revision`].
    pub async fn describe_identify_instances_for_revision(
        &self,
        tenant: &str,
        deployment_id: DeploymentId,
        bundle_id: BundleId,
        revision_id: RevisionId,
        provider_types: &[&str],
    ) -> Result<HashMap<String, Option<crate::identify_hint::IdentifyInstanceHint>>> {
        if provider_types.is_empty() {
            return Ok(HashMap::new());
        }
        let runtime = self.load_revision_runtime(tenant, deployment_id, bundle_id, revision_id)?;
        let mut merged: HashMap<String, Option<crate::identify_hint::IdentifyInstanceHint>> =
            provider_types
                .iter()
                .map(|ty| ((*ty).to_string(), None))
                .collect();
        for pack in runtime.all_packs() {
            // First non-`None` hint per type wins — anything already populated
            // is at the lattice top. Mirror the `matches!` shape the sibling
            // identify fns use so the predicate is consistent across files.
            let remaining: Vec<&str> = provider_types
                .iter()
                .copied()
                .filter(|ty| !matches!(merged.get(*ty), Some(Some(_))))
                .collect();
            if remaining.is_empty() {
                break;
            }
            let probe = pack
                .describe_identify_hints_by_provider_type(&remaining)
                .await?;
            for (ty, hint) in probe {
                if let Some(slot) = merged.get_mut(&ty)
                    && slot.is_none()
                {
                    *slot = hint;
                }
            }
        }
        Ok(merged)
    }

    /// Per-revision provider invocation (Phase D).
    ///
    /// Locates the unique pack in `(deployment_id, bundle_id, revision_id)`
    /// whose `greentic.provider-extension.v1` binds the requested
    /// `provider_type`, verifies `op` is in that declaration's allowlist,
    /// then calls `op` on it with `input_json`.
    ///
    /// Used by greentic-start's Phase D `ProviderRoute` admission arm to
    /// run provider webhooks (e.g. `ingest_http`) without round-tripping
    /// through the flow engine. The provider component returns the parsed
    /// HTTP-out envelope verbatim; greentic-start dispatches the events
    /// it carries back through the flow runtime separately.
    ///
    /// `correlation_id` is threaded into the `ComponentExecCtx` as both
    /// `correlation_id` and `idempotency_key` (mirroring the operator-API
    /// pattern in `build_exec_ctx`). `trace_id` rides through as-is.
    ///
    /// Fails closed when:
    /// - the revision isn't loaded (error chain names deployment + revision)
    /// - no pack in the revision binds `provider_type`
    /// - **multiple packs in the revision bind `provider_type`** — the URL
    ///   → provider routing the caller did at the route table is unable to
    ///   disambiguate at invoke time. Mirrors the within-pack ambiguity
    ///   check in [`ProviderRegistry::resolve`], lifted to the revision
    ///   level so an overlay accidentally redeclaring a main-pack provider
    ///   can't silently shadow the wrong runtime. A future revision of
    ///   this API can accept an explicit `provider_id` to disambiguate
    ///   when D.3 wires identification + invocation together.
    /// - `op` is not in the resolved provider's declared `ops` allowlist
    ///   (defense-in-depth: a caller bug or misconfigured `ProviderRoute`
    ///   cannot smuggle an undeclared op past the schema-core boundary).
    ///
    /// Loop inlined for the same reason as
    /// [`identify_messaging_endpoints_for_revision`].
    ///
    /// [`ProviderRegistry::resolve`]: crate::provider::ProviderRegistry::resolve
    #[allow(clippy::too_many_arguments)]
    pub async fn invoke_provider_for_revision(
        &self,
        tenant: &str,
        deployment_id: DeploymentId,
        bundle_id: BundleId,
        revision_id: RevisionId,
        provider_type: &str,
        op: &str,
        input_json: Vec<u8>,
        correlation_id: Option<String>,
        trace_id: Option<String>,
    ) -> Result<Value> {
        let runtime = self.load_revision_runtime(tenant, deployment_id, bundle_id, revision_id)?;
        // Walk ALL packs to detect cross-pack ambiguity. First-match-wins
        // would let main pack silently shadow an overlay that binds the
        // same provider_type — the identify-side merge lattice can return
        // outcomes from any pack, so the invoke side must refuse to guess.
        let mut matched = None;
        for pack in runtime.all_packs() {
            let Some(registry) = pack.provider_registry_optional()? else {
                continue;
            };
            let Some((binding, declared_ops)) =
                registry.try_resolve_with_ops(None, Some(provider_type))?
            else {
                continue;
            };
            if matched.is_some() {
                bail!(
                    "ambiguous provider_type `{provider_type}` in revision \
                     (deployment {deployment_id}, revision {revision_id}): \
                     multiple packs bind the same type; pack manifests must \
                     declare each provider_type at most once across main + overlays"
                );
            }
            matched = Some((Arc::clone(pack), binding, declared_ops));
        }
        let Some((pack, binding, declared_ops)) = matched else {
            bail!(
                "no pack in revision binds provider_type `{provider_type}` \
                 (deployment {deployment_id}, revision {revision_id})"
            );
        };
        if !declared_ops.iter().any(|d| d == op) {
            bail!(
                "op `{op}` is not declared for provider_type `{provider_type}` \
                 in revision (deployment {deployment_id}, revision {revision_id}); \
                 declared ops: {declared_ops:?}"
            );
        }
        let exec_ctx = ComponentExecCtx {
            tenant: ComponentTenantCtx {
                tenant: tenant.to_string(),
                team: None,
                user: None,
                trace_id,
                i18n_id: None,
                correlation_id: correlation_id.clone(),
                deadline_unix_ms: None,
                attempt: 1,
                idempotency_key: correlation_id,
            },
            i18n_id: None,
            flow_id: format!("provider-webhook/{provider_type}"),
            node_id: None,
        };
        pack.invoke_provider(&binding, exec_ctx, op, input_json)
            .await
    }

    /// Shared activity-execution body: resolve the flow, build the canonical
    /// ingress envelope, run the state machine, and normalize replies. Both the
    /// legacy and revision entry points funnel through here so flow resolution
    /// and reply shaping never drift between them.
    /// Build the ingress envelope for a turn (fast2flow routing + welcome-flow
    /// override), or short-circuit with a pre-built response (fast2flow
    /// Respond/Deny). The caller runs the state machine on the `Run` arm,
    /// so the traced (`handle_traced`) and untraced (`handle`) paths share this
    /// single envelope-building seam and cannot diverge.
    async fn build_prepared(
        &self,
        runtime: &TenantRuntime,
        tenant: &str,
        activity: Activity,
    ) -> Result<Prepared> {
        let activity = apply_fast2flow_routing(runtime, tenant, activity)?;

        // Fast2Flow Respond/Deny returns a pre-built response activity
        // (kind = Custom { action: "response" }, no flow_id/pack_id).
        // Short-circuit: return it directly — do NOT resolve a flow or
        // enter the state machine, otherwise a Deny still executes the
        // tenant entry flow with the denial payload.
        if activity.action() == Some("response") && activity.flow_id().is_none() {
            return Ok(Prepared::ShortCircuit(vec![activity]));
        }

        let (pack_id, flow_id) = resolve_flow_id(runtime, &activity)?;
        let action = activity.action().map(|value| value.to_string());
        let session = activity.session_id().map(|value| value.to_string());
        let provider = activity.provider_id().map(|value| value.to_string());
        let messaging_endpoint_id = activity
            .messaging_endpoint_id()
            .map(|value| value.to_string());
        let channel = activity.channel().map(|value| value.to_string());
        let conversation = activity.conversation().map(|value| value.to_string());
        let user = activity.user().map(|value| value.to_string());
        let welcome_flow_hint = activity.welcome_flow_hint().cloned();
        let resolved_flow_type =
            activity
                .flow_type()
                .map(|value| value.to_string())
                .or_else(|| {
                    runtime
                        .engine()
                        .flow_by_key(&pack_id, &flow_id)
                        .map(|desc| desc.flow_type.clone())
                });
        let mut payload = activity.into_payload();

        // A card button says where to go next in `nextCardId` and friends.
        // Usually that names another CARD, which the adaptive-card component
        // renders. When it names a FLOW NODE the pack's graph continues there
        // instead — and the target has to be lifted out of the payload, because
        // the component prefers an inbound `nextCardId` over its node's own
        // asset and would fail to resolve a node id as a card
        // (`AC_ASSET_NOT_FOUND`, surfacing as a generic service error).
        //
        // greentic-start does this for the messaging path it owns; this is the
        // same rule for the in-process path, which previously had none — card
        // navigation worked and flow-node navigation did not.
        let entry_node = {
            let node_ids = runtime.engine().flow_node_ids(&pack_id, &flow_id).await;
            let target = crate::runner::card_nav::entry_node_from_card_nav(
                payload.get("metadata").unwrap_or(&serde_json::Value::Null),
                &node_ids,
            );
            if target.is_some()
                && let Some(metadata) = payload.get_mut("metadata")
            {
                crate::runner::card_nav::strip_card_nav_keys(metadata);
            }
            target
        };

        let mut envelope = IngressEnvelope {
            entry_node,
            tenant: tenant.to_string(),
            env: std::env::var("GREENTIC_ENV").ok(),
            pack_id: Some(pack_id.clone()),
            flow_id: flow_id.clone(),
            flow_type: resolved_flow_type,
            action,
            session_hint: session,
            provider,
            messaging_endpoint_id,
            channel,
            conversation,
            user,
            activity_id: None,
            timestamp: None,
            payload,
            metadata: None,
            reply_scope: None,
        }
        .canonicalize();

        let hint_flow_type = welcome_flow_hint.as_ref().and_then(|hint| {
            runtime
                .engine()
                .flow_by_key(&hint.pack_id, &hint.flow_id)
                .map(|desc| desc.flow_type.clone())
        });
        apply_welcome_flow_override(
            runtime.session_store(),
            &mut envelope,
            welcome_flow_hint.as_ref(),
            hint_flow_type,
        )
        .await?;

        Ok(Prepared::Run(Box::new(envelope)))
    }

    /// Execute an activity against a specific deployment/bundle/revision runtime.
    /// Builds the envelope via [`Self::build_prepared`] then runs the state
    /// machine (or returns the fast2flow short-circuit replies directly).
    async fn dispatch_activity(
        &self,
        runtime: &TenantRuntime,
        tenant: &str,
        activity: Activity,
    ) -> Result<Vec<Activity>> {
        match self.build_prepared(runtime, tenant, activity).await? {
            Prepared::ShortCircuit(replies) => Ok(replies),
            Prepared::Run(envelope) => {
                let result = runtime.state_machine().handle(*envelope).await?;
                Ok(normalize_replies(result, tenant))
            }
        }
    }

    pub async fn tenant(&self, tenant: &str) -> Option<TenantHandle> {
        self.active
            .load_pack(tenant)
            .map(|runtime| TenantHandle { runtime })
    }

    pub fn active_packs(&self) -> Arc<ActivePacks> {
        Arc::clone(&self.active)
    }

    pub fn health_state(&self) -> Arc<HealthState> {
        Arc::clone(&self.health)
    }

    pub fn wasi_policy(&self) -> Arc<RunnerWasiPolicy> {
        Arc::clone(&self.wasi_policy)
    }

    pub fn session_store(&self) -> DynSessionStore {
        Arc::clone(&self.session_store)
    }

    pub fn state_store(&self) -> DynStateStore {
        Arc::clone(&self.state_store)
    }

    pub fn session_host(&self) -> Arc<dyn SessionHost> {
        Arc::clone(&self.session_host)
    }

    pub fn state_host(&self) -> Arc<dyn StateHost> {
        Arc::clone(&self.state_host)
    }

    pub fn secrets_manager(&self) -> DynSecretsManager {
        Arc::clone(&self.secrets_manager)
    }

    /// The host-injected extension LLM port, if any. `None` for standalone
    /// runners, in which case the extension runtime uses its env-keyed fallback.
    #[cfg(feature = "agentic-worker")]
    pub fn ext_llm_port(&self) -> Option<ExtLlmPort> {
        self.ext_llm_port.clone()
    }

    /// The host-injected MCP tool source, if any. `None` for standalone
    /// runners, in which case each flow engine derives one from
    /// `GREENTIC_AW_*` env.
    #[cfg(feature = "agentic-worker")]
    pub fn mcp_source(&self) -> Option<McpSource> {
        self.mcp_source.clone()
    }

    /// The shared session-keyed streaming-observer registry (R2). Cloning
    /// this `Arc` and handing it to both `TenantRuntime` construction (so
    /// `RuntimeAgentNodeHandler::execute` can read it) and `ServerState` (so
    /// the `POST /agent/chat/stream` SSE handler can write to it) is what
    /// keeps the two sides talking about the same registry.
    #[cfg(feature = "agentic-worker")]
    pub fn stream_observers(&self) -> crate::http::agent_stream::StreamObserverRegistry {
        self.stream_observers.clone()
    }

    pub fn tenant_configs(&self) -> HashMap<String, Arc<HostConfig>> {
        self.configs.clone()
    }

    /// Build a minimal `RunnerHost` suitable for unit tests. The host has no
    /// packs loaded into `active`, so `handle_activity` for any tenant will
    /// return a "not loaded" error — which is exactly the error path the
    /// `POST /agent/chat` handler tests exercise.
    #[cfg(test)]
    pub(crate) fn for_test() -> Arc<Self> {
        use crate::storage::{new_session_store, new_state_store};

        use crate::config::{
            FlowRetryConfig, OperatorPolicy, RateLimits, SecretsPolicy, StateStorePolicy,
            WebhookPolicy,
        };
        let config = Arc::new(HostConfig {
            tenant: "test".into(),
            bindings_path: std::path::PathBuf::from("<test>"),
            flow_type_bindings: std::collections::HashMap::new(),
            rate_limits: RateLimits::default(),
            retry: FlowRetryConfig::default(),
            http_enabled: false,
            secrets_policy: SecretsPolicy::allow_all(),
            state_store_policy: StateStorePolicy::default(),
            webhook_policy: WebhookPolicy::default(),
            timers: Vec::new(),
            oauth: None,
            mocks: None,
            pack_bindings: Vec::new(),
            env_passthrough: Vec::new(),
            trace: crate::trace::TraceConfig::from_env(),
            validation: crate::validate::ValidationConfig::from_env(),
            operator_policy: OperatorPolicy::allow_all(),
            fast2flow: Default::default(),
            #[cfg(feature = "agentic-worker")]
            agents: std::collections::HashMap::new(),
            #[cfg(feature = "agentic-worker")]
            graphs: std::collections::HashMap::new(),
        });
        let session_store = new_session_store();
        let session_host = session_host_from(Arc::clone(&session_store));
        let state_store = new_state_store();
        let state_host = state_host_from(Arc::clone(&state_store));
        let secrets_manager = default_manager().expect("test secrets manager");
        Arc::new(Self {
            configs: std::collections::HashMap::from([("test".to_string(), config)]),
            active: Arc::new(ActivePacks::new()),
            health: Arc::new(HealthState::new()),
            session_store,
            state_store,
            session_host,
            state_host,
            wasi_policy: Arc::new(RunnerWasiPolicy::default()),
            secrets_manager,
            #[cfg(feature = "agentic-worker")]
            ext_llm_port: None,
            #[cfg(feature = "agentic-worker")]
            mcp_source: None,
            #[cfg(feature = "agentic-worker")]
            stream_observers: Arc::new(dashmap::DashMap::new()),
            telemetry: None,
        })
    }

    async fn prepare_runtime(
        &self,
        tenant: &str,
        pack_path: &Path,
        archive_source: Option<&Path>,
    ) -> Result<Arc<TenantRuntime>> {
        let config = self
            .configs
            .get(tenant)
            .cloned()
            .with_context(|| format!("tenant {tenant} not registered"))?;
        if config.tenant != tenant {
            bail!(
                "tenant mismatch: config declares '{}' but '{tenant}' was requested",
                config.tenant
            );
        }
        let runtime = TenantRuntime::load(
            pack_path,
            Arc::clone(&config),
            None,
            archive_source,
            None,
            self.wasi_policy(),
            self.session_host(),
            self.session_store(),
            self.state_store(),
            self.state_host(),
            self.secrets_manager(),
            #[cfg(feature = "agentic-worker")]
            self.ext_llm_port(),
            #[cfg(feature = "agentic-worker")]
            self.mcp_source(),
            #[cfg(feature = "agentic-worker")]
            Some(self.stream_observers()),
        )
        .await?;
        let timers = adapt_timer::spawn_timers(Arc::clone(&runtime))?;
        runtime.register_timers(timers);
        Ok(runtime)
    }
}

impl TenantHandle {
    pub fn config(&self) -> Arc<HostConfig> {
        Arc::clone(self.runtime.config())
    }

    pub fn pack(&self) -> Arc<PackRuntime> {
        self.runtime.pack()
    }

    pub fn engine(&self) -> Arc<FlowEngine> {
        Arc::clone(self.runtime.engine())
    }

    pub fn overlays(&self) -> Vec<Arc<PackRuntime>> {
        self.runtime.overlays()
    }

    pub fn overlay_digests(&self) -> Vec<Option<String>> {
        self.runtime.overlay_digests()
    }
}

/// M1.5 welcome-flow override: swap the envelope's `(pack_id, flow_id,
/// flow_type)` to the producer-supplied [`WelcomeFlowHint`] when ALL of:
/// the hint is present, the envelope carries a `messaging_endpoint_id`,
/// the welcome-seen marker is absent for this `(tenant, env, eid, user)`
/// (set atomically on success), and `FlowResumeStore::fetch` finds no
/// active wait snapshot. Any missing precondition is a silent no-op.
///
/// **The welcome-seen marker is the durable first-contact gate.** Without
/// it, post-completion / no-wait / TTL-expired turns would re-fire welcome
/// because the wait-snapshot check is only positive while a flow is paused.
/// The marker lives in the shared session store under a synthetic scope
/// (`welcome-seen::ep=<eid>`) distinct from the flow's own conversation, so
/// flow-completion `clear_wait` does NOT drop it.
///
/// The wait-snapshot check is kept as a belt-and-braces safety net: the
/// marker check + write is two operations against the store with a small
/// race window (Phase D will add an atomic `register_wait_if_absent`).
/// The safety net guarantees an in-flight flow is never overridden even if
/// two concurrent first-ever turns both pass the marker probe.
///
/// `session_store` + `hint_flow_type` are passed as primitives so the logic
/// is unit-testable without a `TenantRuntime`; the caller does the engine
/// lookup that produces `hint_flow_type`.
///
/// `async` because both probes are session-store round-trips, and that store
/// may be Redis — see [`FlowResumeStore`].
async fn apply_welcome_flow_override(
    session_store: &DynSessionStore,
    envelope: &mut IngressEnvelope,
    hint: Option<&WelcomeFlowHint>,
    hint_flow_type: Option<String>,
) -> Result<()> {
    let Some(hint) = hint else {
        return Ok(());
    };
    if envelope.messaging_endpoint_id.is_none() {
        return Ok(());
    }

    if !try_mark_welcome_first_contact(session_store, envelope).await? {
        return Ok(());
    }

    let resume = FlowResumeStore::new(Arc::clone(session_store));
    let snapshot = resume
        .fetch(envelope)
        .await
        .map_err(|err| anyhow!("welcome-flow first-contact probe failed: {err}"))?;
    if snapshot.is_some() {
        return Ok(());
    }

    envelope.pack_id = Some(hint.pack_id.clone());
    envelope.flow_id = hint.flow_id.clone();
    envelope.flow_type = hint_flow_type;
    Ok(())
}

/// Persists a per-`(tenant, env, eid, user)` welcome-seen marker on first
/// contact and returns `true` only when this turn observed no marker AND
/// wrote one. Subsequent turns short-circuit to `false`.
///
/// Returns `false` (without writing) if the envelope lacks a
/// `messaging_endpoint_id` — no marker bucket is derivable.
///
/// Race window: check + mark is two store calls, not one atomic CAS. Two
/// concurrent first-ever turns can both observe "no marker" and both fire
/// welcome once — bounded harm, mitigated by the wait-snapshot safety net
/// in [`apply_welcome_flow_override`]. A real atomic primitive
/// (`register_wait_if_absent`) is Phase D.
async fn try_mark_welcome_first_contact(
    store: &DynSessionStore,
    envelope: &IngressEnvelope,
) -> Result<bool> {
    let Some(scope) = welcome_marker_scope(envelope) else {
        return Ok(false);
    };
    let (ctx, user) = FlowResumeStore::contact_identity(envelope)
        .map_err(|e| anyhow!("welcome marker identity probe failed: {e}"))?;

    // One offload, not two: check and mark are already documented as a
    // non-atomic pair, and splitting them across two blocking hops would widen
    // the race window for nothing.
    let store = Arc::clone(store);
    let marked = tokio::task::spawn_blocking(move || -> Result<bool> {
        if store
            .find_wait_by_scope(&ctx, &user, &scope)
            .map_err(|e| anyhow!("welcome marker probe failed: {e}"))?
            .is_some()
        {
            return Ok(false);
        }

        let data = marker_session_data(&ctx, &user);
        let session_key = marker_session_key(&ctx, &user, &scope);
        store
            .register_wait(&ctx, &user, &scope, &session_key, data, None)
            .map_err(|e| anyhow!("welcome marker register failed: {e}"))?;
        Ok(true)
    })
    .await
    .map_err(|err| anyhow!("welcome marker probe panicked or was cancelled: {err}"))??;
    Ok(marked)
}

/// Stable, identity-scoped session key for the welcome marker.
///
/// **The session key is the store's per-entry identity.** Both backends
/// (in-memory + Redis) overwrite or reject an existing entry on a
/// `register_wait` collision, so a scope-only `SessionKey` would collapse
/// every `(tenant, env, user)` on the same endpoint onto one row:
/// in-memory's `ensure_ctx_preserved` would reject User B's first turn
/// outright; Redis's unconditional `SET` would overwrite User A's entry
/// and dangle User A's scope index to User B's data.
///
/// Fix: SHA-256 over `(env, tenant, team, user, conversation)`. The `v1`
/// prefix lets us bump the derivation without colliding on old markers.
fn marker_session_key(
    ctx: &greentic_types::TenantCtx,
    user: &greentic_types::UserId,
    scope: &greentic_types::ReplyScope,
) -> greentic_session::SessionKey {
    use sha2::{Digest, Sha256};
    let team = match ctx.team_id.as_ref().or(ctx.team.as_ref()) {
        Some(t) => t.as_str(),
        None => "<none>",
    };
    let digest = Sha256::digest(
        format!(
            "welcome-marker:v1\0{}\0{}\0team={team}\0{}\0{}",
            ctx.env.as_str(),
            ctx.tenant_id.as_str(),
            user.as_str(),
            scope.conversation,
        )
        .as_bytes(),
    );
    greentic_session::SessionKey::new(format!("welcome-marker::{}", hex::encode(digest)))
}

/// Synthetic [`ReplyScope`] keyed on `messaging_endpoint_id` so the marker
/// is partitioned per-endpoint AND disjoint from any real conversation
/// scope. Returns `None` when the envelope lacks an eid — the marker has
/// no meaningful bucket then, and the caller exits early.
fn welcome_marker_scope(envelope: &IngressEnvelope) -> Option<greentic_types::ReplyScope> {
    let eid = envelope.messaging_endpoint_id.as_deref()?;
    Some(greentic_types::ReplyScope {
        conversation: format!("welcome-seen::ep={eid}"),
        thread: None,
        reply_to: None,
        correlation: None,
    })
}

/// Minimal `SessionData` for the marker. The store accepts any record
/// aligned with `(ctx, user)`; the marker carries no flow semantics, so
/// the placeholder `flow_id`/`pack_id` is fixed and validates as an
/// identifier (ascii + `.`/`-`/`_`).
fn marker_session_data(
    ctx: &greentic_types::TenantCtx,
    user: &greentic_types::UserId,
) -> greentic_session::SessionData {
    use std::str::FromStr;
    use std::sync::LazyLock;
    static FLOW_ID: LazyLock<greentic_types::FlowId> =
        LazyLock::new(|| greentic_types::FlowId::from_str("welcome-marker").expect("valid id"));
    static PACK_ID: LazyLock<greentic_types::PackId> =
        LazyLock::new(|| greentic_types::PackId::from_str("welcome-marker").expect("valid id"));
    let cursor = greentic_types::SessionCursor::new("marker".to_string());
    let ctx = ctx.clone().with_user(Some(user.clone()));
    greentic_session::SessionData {
        tenant_ctx: ctx,
        flow_id: FLOW_ID.clone(),
        pack_id: Some(PACK_ID.clone()),
        cursor,
        context_json: "{}".to_string(),
    }
}

fn apply_fast2flow_routing(
    runtime: &TenantRuntime,
    tenant: &str,
    activity: Activity,
) -> Result<Activity> {
    let config = &runtime.config().fast2flow;
    if !config.enabled || activity.flow_id().is_some() {
        return Ok(activity);
    }
    apply_fast2flow_routing_enabled(runtime, tenant, activity, config)
}

#[cfg(feature = "greentic-x-provider")]
fn apply_fast2flow_routing_enabled(
    runtime: &TenantRuntime,
    tenant: &str,
    activity: Activity,
    config: &Fast2FlowRoutingConfig,
) -> Result<Activity> {
    let Some(text) = activity.payload().get("text").and_then(Value::as_str) else {
        return Ok(activity);
    };
    if text.trim().is_empty() {
        return Ok(activity);
    }

    let mut envelope = Fast2FlowMessageEnvelope::new(text.trim().to_owned());
    if let Some(channel) = activity.channel() {
        envelope = envelope.with_channel(channel.to_owned());
    }
    if let Some(provider) = activity.provider_id() {
        envelope = envelope.with_provider(provider.to_owned());
    }
    let request = Fast2FlowRouteRequest {
        scope: config.scope.clone().unwrap_or_else(|| tenant.to_owned()),
        envelope,
        session_active: activity.session_id().is_some(),
        input_locale: "en".to_owned(),
        time_budget_ms: config.time_budget_ms,
        registry_path: config.registry_path.clone(),
        indexes_path: config.indexes_path.clone(),
        now_unix_ms: chrono::Utc::now().timestamp_millis().max(0) as u64,
        metadata: Default::default(),
    };
    let provider = RunnerPackFast2FlowRoutingProvider::new(runtime.pack())
        .map_err(|err| anyhow!(err.to_string()))?
        .with_component_ref(config.component_ref.clone())
        .with_operation(config.operation.clone())
        .with_tenant(tenant.to_owned());
    let route = provider
        .route_intent(request)
        .map_err(|err| anyhow!(err.to_string()))?;

    match route.directive {
        Fast2FlowDirective::Continue => Ok(activity),
        Fast2FlowDirective::Dispatch {
            target, entities, ..
        } => apply_fast2flow_target(activity, &target, entities),
        Fast2FlowDirective::Respond { message } => Ok(Activity::custom(
            "response",
            serde_json::json!({ "messages": [{ "text": message }] }),
        )
        .ensure_tenant(tenant)),
        Fast2FlowDirective::Deny { reason } => Ok(Activity::custom(
            "response",
            serde_json::json!({ "messages": [{ "text": reason }] }),
        )
        .ensure_tenant(tenant)),
    }
}

#[cfg(not(feature = "greentic-x-provider"))]
fn apply_fast2flow_routing_enabled(
    _runtime: &TenantRuntime,
    _tenant: &str,
    _activity: Activity,
    _config: &Fast2FlowRoutingConfig,
) -> Result<Activity> {
    bail!("fast2flow routing requires the greentic-x-provider feature")
}

#[cfg(feature = "greentic-x-provider")]
fn apply_fast2flow_target(
    activity: Activity,
    target: &str,
    entities: Vec<greentic_x_runtime::Fast2FlowRoutingEntity>,
) -> Result<Activity> {
    let target = target.trim();
    if target.is_empty() {
        bail!("fast2flow dispatch target is empty");
    }
    if let Some((pack_id, flow_id)) = target.split_once('/') {
        if pack_id.trim().is_empty() || flow_id.trim().is_empty() {
            bail!("fast2flow dispatch target `{target}` must be `pack_id/flow_id` or `flow_id`");
        }
        return Ok(attach_fast2flow_entities(
            activity.with_pack(pack_id.trim()).with_flow(flow_id.trim()),
            entities,
        ));
    }
    Ok(attach_fast2flow_entities(
        activity.with_flow(target),
        entities,
    ))
}

#[cfg(feature = "greentic-x-provider")]
fn attach_fast2flow_entities(
    activity: Activity,
    entities: Vec<greentic_x_runtime::Fast2FlowRoutingEntity>,
) -> Activity {
    if entities.is_empty() {
        return activity;
    }
    activity.with_payload_field(
        "fast2flow",
        serde_json::json!({
            "entities": entities,
        }),
    )
}

fn resolve_flow_id(runtime: &TenantRuntime, activity: &Activity) -> Result<(String, String)> {
    let engine = runtime.engine();
    if let Some(flow_id) = activity.flow_id() {
        if let Some(pack_id) = activity.pack_id() {
            if engine.flow_by_key(pack_id, flow_id).is_none() {
                bail!("flow {flow_id} not registered for pack {pack_id}");
            }
            return Ok((pack_id.to_string(), flow_id.to_string()));
        }
        if let Some(flow) = engine.flow_by_id(flow_id) {
            return Ok((flow.pack_id.clone(), flow.id.clone()));
        }
        bail!("flow {flow_id} is ambiguous; pack_id is required");
    }

    if let Some(flow_type) = activity.flow_type() {
        if let Some(pack_id) = activity.pack_id() {
            if let Some(flow) = engine
                .flows()
                .iter()
                .find(|flow| flow.pack_id == pack_id && flow.flow_type == flow_type)
            {
                return Ok((pack_id.to_string(), flow.id.clone()));
            }
            bail!("flow type {flow_type} not registered for pack {pack_id}");
        }
        if let Some(flow) = engine.flow_by_type(flow_type) {
            return Ok((flow.pack_id.clone(), flow.id.clone()));
        }
        // More than one flow of this type exists. Provider ingress is routed by
        // type alone (no flow_id/pack_id), so a pack with one public entrypoint
        // plus internal helper flows of the same type would otherwise be
        // rejected as ambiguous. Narrow to entrypoint flows before giving up:
        // internal flows are only reachable via `flow.call` and must never be
        // selected for an inbound event.
        if let Some(flow) = engine.entry_flow_by_type(flow_type) {
            return Ok((flow.pack_id.clone(), flow.id.clone()));
        }
        bail!("flow type {flow_type} is ambiguous; pack_id is required");
    }

    let pack = runtime.pack();
    let flow_id = pack
        .metadata()
        .entry_flows
        .first()
        .cloned()
        .ok_or_else(|| anyhow!("no entry flows registered for tenant {}", runtime.tenant()))?;
    Ok((pack.metadata().pack_id.clone(), flow_id))
}

fn normalize_replies(result: Value, tenant: &str) -> Vec<Activity> {
    result
        .as_array()
        .cloned()
        .unwrap_or_else(|| vec![result])
        .into_iter()
        .map(|payload| Activity::from_output(payload, tenant))
        .collect()
}

fn is_pack_archive(path: &Path) -> bool {
    path.extension()
        .and_then(|ext| ext.to_str())
        .map(|ext| ext.eq_ignore_ascii_case("gtpack"))
        .unwrap_or(false)
}

#[cfg(test)]
mod welcome_flow_tests {
    use super::*;
    use crate::engine::runtime::IngressEnvelope;
    use crate::runner::engine::{ExecutionState, FlowSnapshot, FlowWait};
    use crate::storage::new_session_store;
    use greentic_types::ReplyScope;
    use serde_json::json;

    fn sample_envelope(endpoint_id: Option<&str>) -> IngressEnvelope {
        sample_envelope_for_user(endpoint_id, "user-1")
    }

    fn sample_envelope_for_user(endpoint_id: Option<&str>, user: &str) -> IngressEnvelope {
        IngressEnvelope {
            tenant: "demo".into(),
            env: Some("local".into()),
            pack_id: Some("pack.default".into()),
            flow_id: "flow.default".into(),
            flow_type: Some("messaging".into()),
            action: Some("messaging".into()),
            session_hint: None,
            provider: Some("teams".into()),
            messaging_endpoint_id: endpoint_id.map(String::from),
            channel: Some("chan".into()),
            conversation: Some(format!("conv-{user}")),
            user: Some(user.to_string()),
            entry_node: None,
            activity_id: None,
            timestamp: None,
            payload: json!({}),
            metadata: None,
            reply_scope: Some(ReplyScope {
                conversation: format!("conv-{user}"),
                thread: None,
                reply_to: None,
                correlation: None,
            }),
        }
        .canonicalize()
    }

    fn hint() -> WelcomeFlowHint {
        WelcomeFlowHint {
            pack_id: "pack.welcome".into(),
            flow_id: "flow.welcome".into(),
        }
    }

    async fn seed_resume(store: &DynSessionStore, envelope: &IngressEnvelope) {
        // Plant a snapshot in the exact bucket `fetch` would query so the
        // next call resolves to a resume — proves the override skips when a
        // session already exists.
        let resume = FlowResumeStore::new(Arc::clone(store));
        let state: ExecutionState = serde_json::from_value(json!({
            "input": { "text": "hi" },
            "nodes": {},
            "egress": []
        }))
        .expect("state");
        let wait = FlowWait {
            reason: Some("await-user".into()),
            snapshot: FlowSnapshot {
                pack_id: envelope.pack_id.clone().expect("pack_id"),
                flow_id: envelope.flow_id.clone(),
                next_flow: None,
                next_node: "node-2".into(),
                awaiting_submit: false,
                state,
            },
        };
        resume.save(envelope, &wait).await.expect("seed save");
    }

    #[tokio::test]
    async fn override_is_no_op_when_hint_absent() {
        // Pre-M1.5 producers don't attach a hint — flow resolution must
        // stay exactly the same.
        let store = new_session_store();
        let mut envelope = sample_envelope(Some("teams-legal"));
        let before = envelope.clone();
        apply_welcome_flow_override(&store, &mut envelope, None, None)
            .await
            .expect("ok");
        assert_eq!(envelope.pack_id, before.pack_id);
        assert_eq!(envelope.flow_id, before.flow_id);
        assert_eq!(envelope.flow_type, before.flow_type);
    }

    #[tokio::test]
    async fn override_is_no_op_when_endpoint_id_absent() {
        // Non-messaging traffic carries no endpoint id and must never hit
        // the welcome-flow path even if the hint is somehow set.
        let store = new_session_store();
        let mut envelope = sample_envelope(None);
        let before = envelope.clone();
        apply_welcome_flow_override(&store, &mut envelope, Some(&hint()), Some("welcome".into()))
            .await
            .expect("ok");
        assert_eq!(envelope.pack_id, before.pack_id);
        assert_eq!(envelope.flow_id, before.flow_id);
    }

    #[tokio::test]
    async fn override_swaps_pack_flow_and_threads_flow_type_through() {
        // Both axes covered: when the caller pre-resolved the welcome
        // flow's type, it lands on the envelope; when the resolver
        // returned None (unknown flow in engine), it lands as None and
        // downstream resolution defaults take over.
        for hint_flow_type in [Some("welcome".to_string()), None] {
            let store = new_session_store();
            let mut envelope = sample_envelope(Some("teams-legal"));
            apply_welcome_flow_override(
                &store,
                &mut envelope,
                Some(&hint()),
                hint_flow_type.clone(),
            )
            .await
            .expect("ok");
            assert_eq!(envelope.pack_id.as_deref(), Some("pack.welcome"));
            assert_eq!(envelope.flow_id, "flow.welcome");
            assert_eq!(envelope.flow_type, hint_flow_type);
        }
    }

    #[tokio::test]
    async fn override_is_no_op_on_repeat_turn_with_existing_session() {
        // Resume path: an already-active session in the same bucket means
        // this isn't first contact. The user must continue on the resumed
        // flow, NOT be redirected to the welcome flow.
        let store = new_session_store();
        let envelope_template = sample_envelope(Some("teams-legal"));
        seed_resume(&store, &envelope_template).await;

        let mut envelope = envelope_template.clone();
        apply_welcome_flow_override(&store, &mut envelope, Some(&hint()), Some("welcome".into()))
            .await
            .expect("ok");
        assert_eq!(envelope.pack_id, envelope_template.pack_id);
        assert_eq!(envelope.flow_id, envelope_template.flow_id);
        assert_eq!(envelope.flow_type, envelope_template.flow_type);
    }

    #[tokio::test]
    async fn override_is_no_op_post_completion_when_marker_present() {
        // POST-COMPLETION REGRESSION GUARD (Codex #201): the welcome-seen
        // marker is durable and survives flow completion. After welcome
        // fires once + the flow finishes (wait cleared), the next turn
        // must NOT re-fire welcome — the marker is the gate, not the
        // active-wait snapshot.
        let store = new_session_store();
        let mut first = sample_envelope(Some("teams-legal"));
        apply_welcome_flow_override(&store, &mut first, Some(&hint()), Some("welcome".into()))
            .await
            .expect("first turn ok");
        assert_eq!(
            first.pack_id.as_deref(),
            Some("pack.welcome"),
            "first turn fires welcome"
        );

        // Simulate the welcome flow completing: the engine clears the
        // wait at end-of-flow (mirror `FlowResumeStore::clear`). The
        // marker must NOT be in the wait scope, so this clear has no
        // effect on the marker.
        let resume = FlowResumeStore::new(Arc::clone(&store));
        resume
            .clear(&first)
            .await
            .expect("clear post-completion wait");

        // Second turn arrives — producer still attaches the hint (it
        // does not know flow-completion happened). The marker keeps the
        // override off.
        let mut second = sample_envelope(Some("teams-legal"));
        apply_welcome_flow_override(&store, &mut second, Some(&hint()), Some("welcome".into()))
            .await
            .expect("second turn ok");
        assert_eq!(
            second.pack_id.as_deref(),
            Some("pack.default"),
            "second turn must NOT re-fire welcome"
        );
        assert_eq!(second.flow_id, "flow.default");
    }

    #[tokio::test]
    async fn override_is_no_op_on_second_turn_after_marker_set() {
        // No-wait variant of the post-completion test: a welcome flow
        // without `session.wait` leaves no snapshot AT ALL. Marker is the
        // only thing standing between turn 2 and a welcome re-fire.
        let store = new_session_store();
        let mut first = sample_envelope(Some("teams-legal"));
        apply_welcome_flow_override(&store, &mut first, Some(&hint()), Some("welcome".into()))
            .await
            .expect("first turn ok");
        assert_eq!(first.pack_id.as_deref(), Some("pack.welcome"));

        let mut second = sample_envelope(Some("teams-legal"));
        apply_welcome_flow_override(&store, &mut second, Some(&hint()), Some("welcome".into()))
            .await
            .expect("second turn ok");
        assert_eq!(
            second.pack_id.as_deref(),
            Some("pack.default"),
            "second turn must NOT re-fire welcome"
        );
    }

    #[tokio::test]
    async fn override_partitions_marker_per_endpoint() {
        // The marker is keyed by `(tenant, env, eid, user)` — a user
        // marked seen on `teams-legal` is still first contact on
        // `teams-accounting`. Welcome must fire independently on each
        // endpoint.
        let store = new_session_store();
        let mut legal = sample_envelope(Some("teams-legal"));
        apply_welcome_flow_override(&store, &mut legal, Some(&hint()), Some("welcome".into()))
            .await
            .expect("legal first turn ok");
        assert_eq!(legal.pack_id.as_deref(), Some("pack.welcome"));

        let mut accounting = sample_envelope(Some("teams-accounting"));
        apply_welcome_flow_override(
            &store,
            &mut accounting,
            Some(&hint()),
            Some("welcome".into()),
        )
        .await
        .expect("accounting first turn ok");
        assert_eq!(
            accounting.pack_id.as_deref(),
            Some("pack.welcome"),
            "different endpoint = independent first contact"
        );
    }

    #[tokio::test]
    async fn override_partitions_marker_per_user_on_same_endpoint() {
        // Codex adversarial review of #382 (high): a session-key derived
        // only from the eid collapses every user on that endpoint onto one
        // store row — in-memory rejects User B's first turn with a hard
        // error, Redis silently overwrites and lets User A re-welcome on
        // their next turn.
        //
        // Regression guard: two users on the same eid each get welcome on
        // their own first turn; the second user's first contact does NOT
        // fail; both subsequent turns are no-ops.
        let store = new_session_store();

        // User A's first turn
        let mut a1 = sample_envelope_for_user(Some("teams-legal"), "user-a");
        apply_welcome_flow_override(&store, &mut a1, Some(&hint()), Some("welcome".into()))
            .await
            .expect("user-a first ok");
        assert_eq!(a1.pack_id.as_deref(), Some("pack.welcome"));

        // User B's first turn — must independently fire welcome, NOT error.
        let mut b1 = sample_envelope_for_user(Some("teams-legal"), "user-b");
        apply_welcome_flow_override(&store, &mut b1, Some(&hint()), Some("welcome".into()))
            .await
            .expect("user-b first must not collide with user-a marker");
        assert_eq!(
            b1.pack_id.as_deref(),
            Some("pack.welcome"),
            "user-b is independent first contact"
        );

        // User A's second turn — marker still intact, no re-fire.
        let mut a2 = sample_envelope_for_user(Some("teams-legal"), "user-a");
        apply_welcome_flow_override(&store, &mut a2, Some(&hint()), Some("welcome".into()))
            .await
            .expect("user-a second ok");
        assert_eq!(
            a2.pack_id.as_deref(),
            Some("pack.default"),
            "user-a must not be re-welcomed after user-b joined"
        );

        // User B's second turn — same.
        let mut b2 = sample_envelope_for_user(Some("teams-legal"), "user-b");
        apply_welcome_flow_override(&store, &mut b2, Some(&hint()), Some("welcome".into()))
            .await
            .expect("user-b second ok");
        assert_eq!(b2.pack_id.as_deref(), Some("pack.default"));
    }

    #[tokio::test]
    async fn marker_is_not_written_when_hint_absent() {
        // Marker writes are gated on the hint+eid preconditions — a
        // pre-M1.5 turn (no hint) MUST NOT leak a marker, otherwise a
        // producer that later enables welcome would treat that user as
        // already-contacted and never fire the override.
        //
        // Mid-conversation users (active-wait safety net path) DO get a
        // marker — that's deliberate: they don't get retroactive welcomes.
        // This test only guards the no-hint gate.
        let store = new_session_store();
        let mut envelope = sample_envelope(Some("teams-legal"));
        apply_welcome_flow_override(&store, &mut envelope, None, None)
            .await
            .expect("ok");

        let mut next = sample_envelope(Some("teams-legal"));
        apply_welcome_flow_override(&store, &mut next, Some(&hint()), Some("welcome".into()))
            .await
            .expect("ok");
        assert_eq!(
            next.pack_id.as_deref(),
            Some("pack.welcome"),
            "no marker leaked from hint-absent path"
        );
    }
}

#[cfg(test)]
mod identify_endpoints_tests {
    use super::*;
    use crate::storage::{new_session_store, new_state_store};

    fn dummy_runner_host() -> RunnerHost {
        let session_store = new_session_store();
        let state_store = new_state_store();
        RunnerHost {
            #[cfg(feature = "agentic-worker")]
            ext_llm_port: None,
            #[cfg(feature = "agentic-worker")]
            mcp_source: None,
            #[cfg(feature = "agentic-worker")]
            stream_observers: Arc::new(dashmap::DashMap::new()),
            configs: HashMap::new(),
            active: Arc::new(ActivePacks::new()),
            health: Arc::new(HealthState::new()),
            session_host: session_host_from(session_store.clone()),
            state_host: state_host_from(state_store.clone()),
            session_store,
            state_store,
            wasi_policy: Arc::new(RunnerWasiPolicy::new()),
            secrets_manager: default_manager().expect("default secrets manager"),
            telemetry: None,
        }
    }

    #[tokio::test]
    async fn empty_provider_types_returns_empty_map_without_loading_revision() {
        // No revision is loaded; this proves the fast-path short-circuits
        // before `load_revision` so the caller can ask "any types?" cheaply
        // when an env declares zero messaging endpoints.
        let host = dummy_runner_host();
        let map = host
            .identify_messaging_endpoints_for_revision(
                "demo",
                DeploymentId::new(),
                BundleId::new("anything"),
                RevisionId::new(),
                &[],
                b"{}",
            )
            .await
            .expect("empty types is the cheap fast path");
        assert!(map.is_empty());
    }

    #[tokio::test]
    async fn missing_revision_surfaces_clear_error() {
        // Non-empty types but the revision was never loaded — the error
        // chain must name the revision so operators can correlate it with
        // their dispatch log.
        let host = dummy_runner_host();
        let deployment = DeploymentId::new();
        let revision = RevisionId::new();
        let err = host
            .identify_messaging_endpoints_for_revision(
                "demo",
                deployment,
                BundleId::new("missing"),
                revision,
                &["teams"],
                b"{}",
            )
            .await
            .expect_err("missing revision must fail closed");
        let msg = format!("{err:#}");
        assert!(
            msg.contains("revision runtime not loaded"),
            "error chain should name the failure mode, got: {msg}"
        );
        assert!(
            msg.contains(&deployment.to_string()),
            "error chain should name the deployment id, got: {msg}"
        );
        assert!(
            msg.contains(&revision.to_string()),
            "error chain should name the revision id, got: {msg}"
        );
    }

    #[tokio::test]
    async fn scoped_empty_provider_types_returns_empty_map() {
        let host = dummy_runner_host();
        let map = host
            .identify_messaging_endpoints_for_revision_scoped(
                "demo",
                DeploymentId::new(),
                BundleId::new("anything"),
                RevisionId::new(),
                &[],
                &[],
                &Value::Null,
            )
            .await
            .expect("empty types is the cheap fast path");
        assert!(map.is_empty());
    }

    #[tokio::test]
    async fn scoped_missing_revision_surfaces_clear_error() {
        let host = dummy_runner_host();
        let deployment = DeploymentId::new();
        let revision = RevisionId::new();
        let err = host
            .identify_messaging_endpoints_for_revision_scoped(
                "demo",
                deployment,
                BundleId::new("missing"),
                revision,
                &["teams"],
                &[],
                &Value::Null,
            )
            .await
            .expect_err("missing revision must fail closed");
        let msg = format!("{err:#}");
        assert!(
            msg.contains("revision runtime not loaded"),
            "error chain should name the failure mode, got: {msg}"
        );
        assert!(
            msg.contains(&deployment.to_string()),
            "error chain should name the deployment id, got: {msg}"
        );
        assert!(
            msg.contains(&revision.to_string()),
            "error chain should name the revision id, got: {msg}"
        );
    }

    /// Regression guard: the futures returned by the per-revision identify
    /// APIs MUST be `Send`. Downstream consumers (greentic-start's hyper
    /// `service_fn`) spawn them through tokio; a non-`Send` future at this
    /// boundary breaks every spawned-service consumer with a confusing
    /// "implementation of Send is not general enough" diagnostic that
    /// surfaces far from the offending change.
    ///
    /// Concrete history: PR #394 routed all three identify entry points
    /// through a shared `fan_out_across_packs` helper bounded on
    /// `AsyncFnMut`. The HRTB inference for the resulting future
    /// destabilised `Send` proof for all three APIs, even the legacy
    /// `identify_messaging_endpoints_for_revision` that itself hadn't
    /// changed shape — greentic-start failed to compile on the next
    /// dev-publish bump. This test would have caught it.
    #[test]
    fn identify_futures_are_send() {
        fn assert_send<F: Send>(_: F) {}
        let host = dummy_runner_host();
        // Each call has to be wrapped in its own scope so the borrows
        // don't outlive the host's reference per call — the point is to
        // assert each returned future type is Send-clean in isolation.
        assert_send(host.identify_messaging_endpoints_for_revision(
            "demo",
            DeploymentId::new(),
            BundleId::new("anything"),
            RevisionId::new(),
            &["teams"],
            b"{}",
        ));
        assert_send(host.identify_messaging_endpoints_for_revision_scoped(
            "demo",
            DeploymentId::new(),
            BundleId::new("anything"),
            RevisionId::new(),
            &["teams"],
            &[("x-telegram-bot-api-secret-token".into(), "tok".into())],
            &Value::Null,
        ));
        assert_send(host.describe_identify_instances_for_revision(
            "demo",
            DeploymentId::new(),
            BundleId::new("anything"),
            RevisionId::new(),
            &["teams"],
        ));
        assert_send(host.invoke_provider_for_revision(
            "demo",
            DeploymentId::new(),
            BundleId::new("anything"),
            RevisionId::new(),
            "messaging.telegram.bot",
            "ingest_http",
            b"{}".to_vec(),
            None,
            None,
        ));
    }

    #[tokio::test]
    async fn invoke_provider_missing_revision_surfaces_clear_error() {
        // Non-empty types but the revision was never loaded — the error
        // chain must name the revision so operators can correlate it with
        // their dispatch log. Mirrors the identify-side sibling so a future
        // refactor can't quietly drop the lookup-context wrapper.
        let host = dummy_runner_host();
        let deployment = DeploymentId::new();
        let revision = RevisionId::new();
        let err = host
            .invoke_provider_for_revision(
                "demo",
                deployment,
                BundleId::new("missing"),
                revision,
                "messaging.telegram.bot",
                "ingest_http",
                b"{}".to_vec(),
                None,
                None,
            )
            .await
            .expect_err("missing revision must fail closed");
        let msg = format!("{err:#}");
        assert!(
            msg.contains("revision runtime not loaded"),
            "error chain should name the failure mode, got: {msg}"
        );
        assert!(
            msg.contains(&deployment.to_string()),
            "error chain should name the deployment id, got: {msg}"
        );
        assert!(
            msg.contains(&revision.to_string()),
            "error chain should name the revision id, got: {msg}"
        );
    }
}

#[cfg(all(test, feature = "greentic-x-provider"))]
mod fast2flow_tests {
    use greentic_x_runtime::Fast2FlowRoutingEntity;

    use super::*;

    #[test]
    fn dispatch_target_attaches_prefill_entities_to_payload() {
        let activity = Activity::text("show traffic tomorrow");
        let routed = apply_fast2flow_target(
            activity,
            "telco-x/prefix-traffic",
            vec![Fast2FlowRoutingEntity::new("date", "20260611").with_format("iso", "2026-06-11")],
        )
        .expect("target should route");

        assert_eq!(routed.pack_id(), Some("telco-x"));
        assert_eq!(routed.flow_id(), Some("prefix-traffic"));
        assert_eq!(
            routed.payload()["fast2flow"]["entities"][0]["normalized"],
            "20260611"
        );
        assert_eq!(
            routed.payload()["fast2flow"]["entities"][0]["formats"]["iso"],
            "2026-06-11"
        );
    }
}