leviath-runtime 0.1.1

ECS-based agent execution engine for Leviath
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
//! The pipeline driver: a single [`PipelineWorld`] that hosts every agent as
//! ECS data and ticks the [`crate::pipeline`] systems over all of them - the
//! traditional-game-loop core of the shared world.
//!
//! The world owns the bevy [`World`], the tick [`Schedule`], the per-model
//! inference pools, and the async bridges (inference jobs + the tool worker).
//! Systems never block: they dispatch async work to the bridges and collect the
//! results on a later tick. Between ticks the driver **parks** on a wake
//! [`Notify`] until an async result lands or an external message arrives, so an
//! idle world costs ~0 CPU regardless of how many (paused/blocked) agents it
//! holds.
//!
//! ## Idle detection (no busy-spin)
//!
//! Each outer iteration drives the schedule to a **fixed point**: it ticks until
//! a tick produces no change in the per-phase marker counts (the "fingerprint").
//! At quiescence every remaining agent is either waiting on an in-flight async
//! job (which will `notify` on completion) or blocked on a resource that only an
//! async completion can free (a full pool) or on nothing at all (a missing
//! provider / no input) - so the driver parks on the wake instead of spinning.
//! A fresh async result or an external `send_message` fires the wake and the
//! fixed-point loop re-runs.

use std::sync::Arc;

use bevy_ecs::prelude::*;
use bevy_ecs::query::QueryFilter;
use leviath_providers::ProviderError;
use tokio::runtime::Handle;
use tokio::sync::Notify;
use tokio::sync::mpsc::{UnboundedSender, unbounded_channel};
use tokio::task::JoinHandle;

use crate::components::{AgentMessage, AgentState, AgentStatus};
use crate::inference_pool::{InferencePoolConfig, InferencePools};
use crate::persistence_bridge::persistence_worker;
use crate::pipeline::{
    AwaitingCompaction, AwaitingInference, AwaitingTools, AwaitingTransitionChoice,
    AwaitingTransitionResponse, CompactionResults, InferenceResults, InferenceStage, MessageIntake,
    PersistenceStage, ProcessResponse, Providers, ReadyForTools, ReadyForTransition, ReadyToInfer,
    ResolveTransition, ToolResults, ToolService, ToolServiceRes, ToolStage, TransitionResults,
    abort_terminal_work, check_workspace_health, collect_compaction, collect_inference,
    collect_tools, collect_transition_choice, deliver_messages, detect_stuck_stage,
    dispatch_compaction, dispatch_edge_compact, dispatch_inference, dispatch_persistence,
    dispatch_tools, dispatch_transition_choice, enforce_max_iterations, gate_requires_children,
    handle_empty_response, poll_dynamic_tool_refresh, process_response, reflect_interaction_status,
    refresh_advertised_tools, require_context_regions, resolve_transition, sync_tool_stages,
};
use crate::providers::ProviderRegistry;
use crate::tool_bridge::spawn_tool_pool;

/// Counts of agents in each phase-marker - the world's per-tick "fingerprint".
/// Two consecutive equal fingerprints mean a tick changed nothing (quiescence).
type Fingerprint = [usize; 12];

/// How many attributed system panics one [`PipelineWorld::run_to_fixed_point`]
/// round will absorb before it stops driving. Each one fails a different agent,
/// so this only bites if the world is thoroughly broken - it exists so a
/// pathological agent can't spin the loop.
const MAX_TICK_FAILURES_PER_ROUND: usize = 8;

/// A schedule configured the way the pipeline needs it.
///
/// Every pipeline system is `.chain()`ed, so the multi-threaded executor can
/// never overlap two of them - it only adds a hop through the compute task
/// pool. Running single-threaded keeps systems on the thread that catches their
/// panics, which is what lets [`run_isolated`] read the offending agent out of
/// the (thread-local) [`crate::tick_scope`].
fn tick_schedule() -> Schedule {
    let mut schedule = Schedule::default();
    // bevy_ecs 0.19 replaced `set_executor_kind(ExecutorKind::…)` with
    // `set_executor(<executor instance>)`.
    schedule.set_executor(bevy_ecs::schedule::SingleThreadedExecutor::new());
    schedule
}

/// What one [`PipelineWorld::tick`] did.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TickOutcome {
    /// Every system ran to completion.
    Clean,
    /// A system panicked and the agent responsible was failed; the rest of the
    /// world is unaffected and can keep being driven.
    AgentFailed,
    /// A system panicked with no agent in scope, so nothing could be failed.
    /// Re-ticking would just re-panic.
    Unattributed,
}

/// The shared ECS world that hosts and drives every agent.
pub struct PipelineWorld {
    world: World,
    schedule: Schedule,
    wake: Arc<Notify>,
    shutdown: Arc<Notify>,
    msg_tx: UnboundedSender<AgentMessage>,
    /// The tool worker pool tasks; kept so they live as long as the world. Each
    /// exits on its own when the world (and thus the [`ToolStage`] sender) is
    /// dropped. The pool size is the tool-lane concurrency cap.
    _tool_tasks: Vec<JoinHandle<()>>,
    /// The persistence worker task. Retained (rather than detached) so
    /// [`Self::flush_and_stop`] can close its channel and `await` it, guaranteeing
    /// every queued snapshot reaches disk before shutdown. `None` once flushed.
    persist_task: Option<JoinHandle<()>>,
}

impl PipelineWorld {
    /// Build a world: wire the pool/bridge resources, register the providers and
    /// tool service, spawn the tool worker onto `runtime`, and assemble the tick
    /// schedule. Agents are added later via [`Self::spawn_agent`].
    pub fn new(
        providers: ProviderRegistry,
        tool_service: Arc<dyn ToolService>,
        pool_config: InferencePoolConfig,
        tool_concurrency: usize,
        runs_dir: std::path::PathBuf,
        runtime: Handle,
    ) -> Self {
        // `Query::par_iter` fans out over the compute task pool; initialize it
        // once (idempotent) so per-agent request assembly in `dispatch_inference`
        // runs in parallel. (The schedule executor itself is single-threaded -
        // see `tick_schedule`.)
        bevy_tasks::ComputeTaskPool::get_or_init(bevy_tasks::TaskPool::default);

        let wake = Arc::new(Notify::new());
        let shutdown = Arc::new(Notify::new());

        let (inf_tx, inf_rx) = unbounded_channel();
        let (trans_tx, trans_rx) = unbounded_channel();
        let (compact_tx, compact_rx) = unbounded_channel();
        let (tool_job_tx, tool_job_rx) = unbounded_channel();
        let (tool_res_tx, tool_res_rx) = unbounded_channel();
        let (persist_tx, persist_rx) = unbounded_channel();
        let (msg_tx, msg_rx) = unbounded_channel();
        let (ip_tx, ip_rx) = unbounded_channel();
        let (gp_tx, gp_rx) = unbounded_channel();
        let (cs_tx, cs_rx) = unbounded_channel();
        let (title_tx, title_rx) = unbounded_channel();

        let tool_tasks = spawn_tool_pool(
            &runtime,
            tool_job_rx,
            tool_res_tx,
            wake.clone(),
            tool_concurrency,
        );
        // Retained so `flush_and_stop` can drain it on shutdown. Left to its own
        // devices otherwise: it exits when the world (and thus its PersistenceStage
        // sender) is dropped.
        let persist_task = runtime.spawn(persistence_worker(runs_dir, persist_rx));
        let ip_runtime = runtime.clone();
        let gp_runtime = runtime.clone();

        let mut world = World::new();
        world.insert_resource(Providers(providers));
        world.insert_resource(InferenceStage {
            pools: Arc::new(InferencePools::new(pool_config)),
            outcomes: inf_tx,
            transition_outcomes: trans_tx,
            compaction_outcomes: compact_tx,
            content_summary_outcomes: cs_tx,
            wake: wake.clone(),
            runtime,
            exact_token_counting: false,
        });
        world.insert_resource(crate::context_transform::ContentSummaryResults(cs_rx));
        world.insert_resource(crate::title::TitleSink(title_tx));
        world.insert_resource(crate::title::TitleResults(title_rx));
        world.insert_resource(crate::interaction_points::InteractionPointStage {
            outcomes: ip_tx,
            wake: wake.clone(),
            runtime: ip_runtime,
        });
        world.insert_resource(crate::interaction_points::InteractionPointResults(ip_rx));
        world.insert_resource(crate::gate_prompt::GatePromptStage {
            outcomes: gp_tx,
            wake: wake.clone(),
            runtime: gp_runtime,
        });
        world.insert_resource(crate::gate_prompt::GatePromptResults(gp_rx));
        world.insert_resource(InferenceResults(inf_rx));
        world.insert_resource(TransitionResults(trans_rx));
        world.insert_resource(CompactionResults(compact_rx));
        world.insert_resource(ToolServiceRes(tool_service));
        world.insert_resource(ToolStage(tool_job_tx));
        world.insert_resource(ToolResults(tool_res_rx));
        world.insert_resource(PersistenceStage(persist_tx));
        world.insert_resource(MessageIntake(msg_rx));
        // Telemetry defaults to the no-op sink; a host that wants export
        // replaces the resource after construction (as `build_host` does).
        world.insert_resource(crate::telemetry::Telemetry(std::sync::Arc::new(
            leviath_core::telemetry::NoopSink,
        )));

        // The tick chain is split into two `.chain()`ed groups (bevy caps a
        // system tuple at 20); the second group runs strictly after the first.
        let mut schedule = tick_schedule();
        schedule.add_systems(
            (
                // First: stop whatever a now-terminal agent still has running in
                // the async lanes. Ahead of everything else so a cancel frees its
                // inference permit and tool-lane worker on the very next tick,
                // rather than whenever the provider or tool happens to answer.
                abort_terminal_work,
                deliver_messages,
                collect_compaction,
                // Apply any completed Summarize context-transform summaries into
                // the child's regions, then dispatch newly-queued ones.
                crate::context_transform::collect_content_summary,
                crate::context_transform::dispatch_content_summary,
                // Route edge-transform compaction through the compaction lane
                // before the threshold-based pass.
                dispatch_edge_compact,
                dispatch_compaction,
                // Cap a stage at its max_iterations before running more inference.
                enforce_max_iterations,
                // …then the softer guard: bail out of a stage that is burning
                // turns/edits without progress, when the blueprint declares a
                // `stuck` escape edge. Runs after the hard cap so that always wins.
                detect_stuck_stage,
                // Stop a run whose working directory vanished, rather than let
                // every tool fail with ENOENT for the rest of the run.
                check_workspace_health,
                // Tag dynamic_tools agents that have pending tool changes, then
                // apply the re-advertisement before the next request is assembled
                // so a newly-discovered tool is visible.
                poll_dynamic_tool_refresh,
                refresh_advertised_tools,
                dispatch_inference,
                collect_inference,
                // Intercept a fan-out stage's split response before normal routing.
                crate::fanout::fan_out_split,
                process_response,
                // Apply resolved taint gate prompts (re-arming ReadyForTools)
                // before the tool dispatch re-runs the held batch.
                crate::gate_prompt::collect_gate_prompt,
                dispatch_tools,
                collect_tools,
                // Apply any resolved stage-boundary interaction-point answers
                // before the stage decides its transition.
                crate::interaction_points::collect_interaction_point,
            )
                .chain(),
        );
        schedule.add_systems(
            (
                handle_empty_response,
                // Hold a `requires_children` stage until its sub-agents finish.
                gate_requires_children,
                // Re-run a stage that left a `required` context region empty
                // before it may transition or ask for approval.
                require_context_regions,
                // Intercept a would-be transition for an interactive-points stage
                // (e.g. plan_approval) and drive the interaction-point lane.
                crate::interaction_points::gate_interaction_points,
                crate::interaction_points::dispatch_interaction_point,
                resolve_transition,
                dispatch_transition_choice,
                collect_transition_choice,
                // Drive fan-out workers and merge once they finish.
                crate::fanout::fan_out_collect,
                // Narrate lifecycle/activity into the telemetry sink. Must run
                // before `sync_tool_stages` (which consumes the transient
                // `StageJustEntered` marker) and before `dispatch_persistence`
                // (which drains the log buffer this system only reads).
                crate::telemetry::observe_lifecycle,
                sync_tool_stages,
                // Store any finished run title, then start newly-marked ones.
                // Collect precedes persistence so a landed title is written on
                // this same tick.
                crate::title::collect_title,
                crate::title::dispatch_title,
                // Mirror open interaction-hub requests into agent status
                // (Active ↔ Waiting) so the dashboard surfaces blocked prompts;
                // must run before persistence so the status change is written.
                reflect_interaction_status,
                dispatch_persistence,
            )
                .chain()
                .after(crate::interaction_points::collect_interaction_point),
        );

        Self {
            world,
            schedule,
            wake,
            shutdown,
            msg_tx,
            _tool_tasks: tool_tasks,
            persist_task: Some(persist_task),
        }
    }

    /// Mutable access to the underlying ECS world, for spawning agents (the CLI /
    /// daemon builds each agent's component bundle) and inspection.
    pub fn world_mut(&mut self) -> &mut World {
        &mut self.world
    }

    /// Read-only access to the underlying ECS world.
    pub fn world(&self) -> &World {
        &self.world
    }

    /// Enable (or disable) the opt-in exact pre-inference budget guard for this
    /// world - see `inference_bridge::InferenceJob::exact_token_counting`.
    /// Call once at startup when the run config requests it, before serving.
    pub fn set_exact_token_counting(&mut self, enabled: bool) {
        // `InferenceStage` is inserted by every `PipelineWorld::new` path, so it
        // is a hard invariant here - `resource_mut` (which panics if absent) is
        // correct and keeps this branch-free.
        self.world
            .resource_mut::<crate::pipeline::InferenceStage>()
            .exact_token_counting = enabled;
    }

    /// Install the shared interaction hub as a world resource and attach this
    /// world's wake handle to it, so opening/answering a prompt wakes the driver
    /// and [`reflect_interaction_status`]
    /// mirrors the change into agent status. Call once at startup, before
    /// serving. Without this, that system is a no-op (test worlds).
    pub fn insert_interaction_hub(&mut self, hub: crate::interaction_hub::InteractionHub) {
        hub.attach_wake(self.wake.clone());
        self.world.insert_resource(hub);
    }

    /// Spawn an agent from its pre-built component bundle and wake the driver so
    /// the next fixed-point picks it up. Returns the new entity.
    pub fn spawn_agent(&mut self, bundle: impl Bundle) -> Entity {
        let e = self.world.spawn(bundle).id();
        self.wake.notify_one();
        e
    }

    /// Spawn an agent from a blueprint + task + per-stage resolution (see
    /// [`crate::pipeline::spawn_agent`]) and wake the driver. Returns the new
    /// entity, or an error if the first stage's system prompt doesn't fit.
    pub fn spawn_from_blueprint(
        &mut self,
        agent_id: String,
        blueprint: leviath_core::Blueprint,
        task: &str,
        stages: Vec<crate::pipeline::ResolvedStage>,
        global_batch_tool_hint: bool,
    ) -> Result<Entity, String> {
        let e = crate::pipeline::spawn_agent(
            &mut self.world,
            agent_id,
            blueprint,
            task,
            stages,
            global_batch_tool_hint,
        )?;
        self.wake.notify_one();
        Ok(e)
    }

    /// Deliver a message to a running agent (routed to its inbox on the next
    /// tick) and wake the driver.
    pub fn send_message(&self, msg: AgentMessage) -> Result<(), ProviderError> {
        self.msg_tx
            .send(msg)
            .map_err(|e| ProviderError::Other(format!("world message channel closed: {e}")))?;
        self.wake.notify_one();
        Ok(())
    }

    /// A clone of the wake handle, so external producers (e.g. a control socket)
    /// can nudge the driver after mutating the world directly.
    pub fn wake_handle(&self) -> Arc<Notify> {
        self.wake.clone()
    }

    /// Request the [`Self::run`] loop to stop after its current fixed point.
    pub fn shutdown(&self) {
        self.shutdown.notify_one();
    }

    /// A clone of the shutdown handle, so a supervisor can stop a [`Self::run`]
    /// loop that has taken ownership of the world on another task.
    pub fn shutdown_handle(&self) -> Arc<Notify> {
        self.shutdown.clone()
    }

    /// Cleanly stop the world, guaranteeing every queued snapshot reaches disk.
    ///
    /// The persistence lane is async and fire-and-forget, so a plain shutdown (the
    /// [`Self::run`]/`serve` loop returning, then the world dropping) can lose
    /// snapshots still queued in the channel. This method closes that gap: it
    /// signals shutdown, drives one last fixed point so any state that settled
    /// after the loop parked is dispatched to the lane, then **closes the lane and
    /// awaits the worker** so all queued writes (`meta.json` / `context.json` /
    /// `run.lvr`) land before it returns.
    ///
    /// Call it after the serve loop has returned (the tokio runtime must still be
    /// alive for the worker to be scheduled). Idempotent: a second call is a no-op
    /// because the persistence resource is already removed and the task taken.
    pub async fn flush_and_stop(&mut self) {
        // Idempotent - the serve loop has usually already returned on this signal.
        self.shutdown.notify_one();
        // Dispatch anything that settled between the last park and now (e.g. an
        // inference result that woke the loop the same instant shutdown fired).
        self.run_to_fixed_point();
        // Drop the *only* `PersistJob` sender so the worker's `recv()` loop drains
        // its queue and then ends.
        self.world.remove_resource::<PersistenceStage>();
        // Wait for every queued write to hit disk.
        if let Some(task) = self.persist_task.take() {
            let _ = task.await;
        }
        // Push any buffered telemetry export out before the process goes away;
        // the final fixed point above already emitted the last events. The
        // resource always exists - `new()` installs the no-op default.
        self.world
            .resource::<crate::telemetry::Telemetry>()
            .0
            .force_flush();
    }

    /// The status of an agent, if it still exists.
    pub fn agent_status(&self, entity: Entity) -> Option<AgentStatus> {
        self.world
            .get::<AgentState>(entity)
            .map(|s| s.status.clone())
    }

    /// Set an agent's status and wake the driver. Returns `false` if the agent no
    /// longer exists. The async-starting dispatchers only act on `Active` agents,
    /// so this is how the world pauses/resumes/cancels an agent - a non-`Active`
    /// agent is simply data the systems skip until it is `Active` again.
    pub fn set_status(&mut self, entity: Entity, status: AgentStatus) -> bool {
        let Some(mut state) = self.world.get_mut::<AgentState>(entity) else {
            return false;
        };
        state.status = status;
        self.wake.notify_one();
        true
    }

    /// Pause an agent (it finishes any in-flight step, then stops before starting
    /// new work). Returns `false` if the agent no longer exists.
    pub fn pause(&mut self, entity: Entity) -> bool {
        self.set_status(entity, AgentStatus::Idle)
    }

    /// Resume a paused agent.
    pub fn resume(&mut self, entity: Entity) -> bool {
        self.set_status(entity, AgentStatus::Active)
    }

    /// Cancel an agent (it stops starting new work; in-flight results still land).
    pub fn cancel(&mut self, entity: Entity) -> bool {
        self.set_status(entity, AgentStatus::Cancelled)
    }

    /// Run one schedule tick over every agent, catching a panic from any system
    /// so one bad agent can't crash the daemon and take every other hosted agent
    /// with it.
    ///
    /// When the panic can be traced to a specific agent (the usual case - see
    /// `tick_scope`), that agent is failed with the panic message so it
    /// stops being driven, its run is persisted as errored, and the host reaps
    /// it. Without that, the world would re-tick the same unchanged state on
    /// every wake and panic again indefinitely.
    pub fn tick(&mut self) -> TickOutcome {
        let Err(panicked) = run_isolated(&mut self.schedule, &mut self.world) else {
            // A clean unwind doesn't mean a clean tick: work that ran on the
            // compute pool catches its own panics, since they can't unwind back
            // here, and leaves a marker instead.
            return self.fail_agents_panicked_in_parallel();
        };
        let message = panic_status_message(&panicked.message);
        match panicked.entity {
            Some(entity) if self.set_status(entity, AgentStatus::Error { message }) => {
                tracing::error!(
                    ?entity,
                    panic = %panicked.message,
                    "a pipeline system panicked; failing that agent - the daemon and every \
                     other run keep going"
                );
                TickOutcome::AgentFailed
            }
            _ => {
                tracing::error!(
                    panic = %panicked.message,
                    "a pipeline system panicked outside any agent's scope; the daemon survived \
                     (an agent may be wedged - cancel it via `lev cancel <run-id>`)"
                );
                TickOutcome::Unattributed
            }
        }
    }

    /// Fail every agent that a compute-pool body marked
    /// [`PanickedInParallel`](crate::tick_scope::PanickedInParallel), and report
    /// whether there were any.
    ///
    /// These panics were caught on a task-pool thread rather than unwinding into
    /// `tick`, so the marker component is how they reach the driver - but from
    /// here on they are handled exactly like an attributed unwind: the agent is
    /// failed, stops being driven, and its run persists as errored.
    fn fail_agents_panicked_in_parallel(&mut self) -> TickOutcome {
        let mut query = self
            .world
            .query::<(Entity, &crate::tick_scope::PanickedInParallel)>();
        let failed: Vec<(Entity, String)> = query
            .iter(&self.world)
            .map(|(entity, p)| (entity, p.message.clone()))
            .collect();
        if failed.is_empty() {
            return TickOutcome::Clean;
        }
        for (entity, message) in failed {
            self.world
                .entity_mut(entity)
                .remove::<crate::tick_scope::PanickedInParallel>();
            let status = AgentStatus::Error {
                message: panic_status_message(&message),
            };
            // The entity came straight out of the query above, so it exists.
            let _ = self.set_status(entity, status);
        }
        TickOutcome::AgentFailed
    }

    /// Append a system to the schedule (test-only, for panic-isolation tests).
    #[cfg(test)]
    pub(crate) fn add_test_system<M>(
        &mut self,
        // `IntoSystemConfigs` became `IntoScheduleConfigs<ScheduleSystem, _>` in
        // bevy_ecs 0.19 (it now also describes observer and other schedulables,
        // so the schedulable kind is an explicit parameter).
        system: impl bevy_ecs::schedule::IntoScheduleConfigs<bevy_ecs::system::ScheduleSystem, M>,
    ) {
        self.schedule.add_systems(system);
    }

    fn count<F: QueryFilter>(&mut self) -> usize {
        let mut q = self.world.query_filtered::<(), F>();
        q.iter(&self.world).count()
    }

    /// Snapshot the per-phase marker counts.
    fn fingerprint(&mut self) -> Fingerprint {
        [
            self.count::<With<ReadyToInfer>>(),
            self.count::<With<AwaitingInference>>(),
            self.count::<With<ProcessResponse>>(),
            self.count::<With<ReadyForTools>>(),
            self.count::<With<ReadyForTransition>>(),
            self.count::<With<ResolveTransition>>(),
            self.count::<With<AwaitingTools>>(),
            self.count::<With<AwaitingTransitionChoice>>(),
            self.count::<With<AwaitingTransitionResponse>>(),
            self.count::<With<AwaitingCompaction>>(),
            self.count::<With<crate::title::PendingTitle>>(),
            self.count::<With<crate::title::AwaitingTitle>>(),
        ]
    }

    /// Any agent waiting on an in-flight async job (inference, tools, a
    /// transition choice, or compaction) whose completion will wake the driver.
    fn has_async_inflight(&mut self) -> bool {
        self.count::<With<AwaitingInference>>() > 0
            || self.count::<With<AwaitingTools>>() > 0
            || self.count::<With<AwaitingTransitionResponse>>() > 0
            || self.count::<With<AwaitingCompaction>>() > 0
            || self.count::<With<crate::title::AwaitingTitle>>() > 0
    }

    /// Drive the schedule until a tick changes nothing (quiescence). Public so a
    /// host loop can interleave control operations between quiescent points.
    pub fn run_to_fixed_point(&mut self) {
        let mut prev = self.fingerprint();
        let mut failures = 0;
        loop {
            let outcome = self.tick();
            match outcome {
                TickOutcome::Clean => {}
                // The offending agent has been failed, so it won't be driven
                // again. Keep ticking: the rest of the world still has work to
                // do, and only a later tick reaches `dispatch_persistence` (the
                // last system in the chain) to record the failure on disk. The
                // budget stops a pathological agent that somehow panics again
                // from spinning this loop.
                TickOutcome::AgentFailed if failures < MAX_TICK_FAILURES_PER_ROUND => {
                    failures += 1;
                }
                // Nothing to fail, so re-ticking would just re-panic: stop
                // driving this round. The daemon stays alive, other agents keep
                // running, and a wedged agent can be cancelled via the control
                // socket (dispatch systems skip non-Active agents once
                // cancelled).
                TickOutcome::AgentFailed | TickOutcome::Unattributed => break,
            }
            let now = self.fingerprint();
            // Quiescence, but only trust it after a clean tick: a panicking tick
            // abandons the rest of the chain (and its buffered commands), so the
            // markers can look unchanged while the world very much has changed.
            // Force at least one more tick so the failed agent gets persisted.
            if now == prev && outcome == TickOutcome::Clean {
                break;
            }
            prev = now;
        }
    }

    /// Drive every agent as far as it can go **right now**, then, while async
    /// work is in flight, wait for each completion and drive again - returning
    /// once the world is fully quiescent with nothing in flight. Bounded by
    /// `max_waits` wake-waits as a safety valve so a lost/never-arriving wake
    /// can't hang a caller (e.g. a test) forever.
    pub async fn run_until_idle(&mut self, max_waits: usize) {
        self.run_to_fixed_point();
        let mut waits = 0;
        while self.has_async_inflight() && waits < max_waits {
            self.wake.notified().await;
            waits += 1;
            self.run_to_fixed_point();
        }
    }

    /// Run forever: drive to quiescence, then park until an async completion or
    /// an external `send_message`/`spawn_agent` wakes the driver. Returns when
    /// [`Self::shutdown`] is signalled.
    pub async fn run(&mut self) {
        loop {
            self.run_to_fixed_point();
            tokio::select! {
                _ = self.wake.notified() => {}
                _ = self.shutdown.notified() => return,
            }
        }
    }
}

/// How a caught panic is recorded on the agent it is blamed on. Shared by the
/// unwind path and the compute-pool path so a run's `error` reads the same
/// either way.
fn panic_status_message(panic: &str) -> String {
    format!("internal error: a pipeline system panicked: {panic}")
}

/// A panic caught while ticking the schedule, and the agent it belongs to.
struct TickPanic {
    /// The agent being processed when the panic fired, if the pipeline had
    /// recorded one (see [`crate::tick_scope`]).
    entity: Option<Entity>,
    /// The panic payload rendered as text.
    message: String,
}

/// Run a schedule over a world, catching a panic from any system so it can't
/// unwind the daemon's drive loop and take down every hosted agent.
///
/// The world may be partially updated after a panic: the panicking system's
/// buffered `Commands` are lost, but resources and components already written
/// are intact, so the caller can still fail the offending agent.
fn run_isolated(schedule: &mut Schedule, world: &mut World) -> Result<(), TickPanic> {
    // Clear first: the slot is thread-local and survives across ticks, so a
    // stale entity from an earlier tick must not be blamed for this one.
    crate::tick_scope::clear();
    match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| schedule.run(world))) {
        Ok(()) => Ok(()),
        Err(payload) => {
            reset_executor(schedule);
            Err(TickPanic {
                entity: crate::tick_scope::current(),
                message: leviath_core::panic_message(payload.as_ref()),
            })
        }
    }
}

/// Give `schedule` a fresh executor after a caught panic.
///
/// bevy's executors mark a system "completed" *before* running it and only
/// clear that set when `run` returns normally. A panic therefore leaves every
/// system up to and including the offending one marked done, so the **next**
/// tick silently skips them and only runs the tail of the chain - a partial
/// tick that would, among other things, keep `dispatch_persistence` from ever
/// seeing an agent we just failed. Swapping the executor kind and back is the
/// public API for forcing a rebuild.
///
/// One call suffices on bevy_ecs 0.19: `set_executor` takes an executor
/// *instance* and unconditionally replaces `schedule.executor` with it (clearing
/// `executor_initialized` too), so the fresh `SingleThreadedExecutor` arrives
/// with an empty `completed_systems`.
///
/// On 0.15 this had to set two different *kinds* and swap back, because
/// `set_executor_kind` was a no-op when the kind was unchanged - and
/// `SimpleExecutor`, the other kind it used, no longer exists.
fn reset_executor(schedule: &mut Schedule) {
    schedule.set_executor(bevy_ecs::schedule::SingleThreadedExecutor::new());
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Serializes every test in this binary that swaps the **process-global**
    /// panic hook - see the definition for why they can't run concurrently.
    use crate::test_support::PANIC_HOOK_LOCK;

    /// Run `f` with the process panic hook silenced (the panic is expected), and
    /// serialized against the other hook-swapping tests.
    fn with_silent_panics<T>(f: impl FnOnce() -> T) -> T {
        let _hook_guard = PANIC_HOOK_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let prev_hook = std::panic::take_hook();
        std::panic::set_hook(Box::new(|_| {}));
        let out = f();
        std::panic::set_hook(prev_hook);
        out
    }

    #[test]
    fn run_isolated_catches_a_system_panic_and_reports_the_agent() {
        fn ok_system() {}
        fn boom_system() {
            panic!("simulated system panic");
        }
        // A system that panics *while working on a specific agent* - the shape
        // every real pipeline system has.
        fn boom_on_agent_system() {
            crate::tick_scope::enter(
                Entity::from_raw_u32(41)
                    .expect("a small literal index is always a valid entity id"),
            );
            panic!("agent-scoped panic");
        }
        let mut world = World::new();

        // A clean schedule ticks normally.
        let mut ok = tick_schedule();
        ok.add_systems(ok_system);
        assert!(run_isolated(&mut ok, &mut world).is_ok());

        // A panicking system is caught (the daemon would survive) and, with no
        // agent in scope, reports no entity to blame.
        let mut bad = tick_schedule();
        bad.add_systems(boom_system);
        let err = with_silent_panics(|| run_isolated(&mut bad, &mut world))
            .expect_err("the panic must be caught");
        assert_eq!(err.entity, None);
        assert_eq!(err.message, "simulated system panic");

        // With an agent in scope, the panic is attributed to it.
        let mut blamed = tick_schedule();
        blamed.add_systems(boom_on_agent_system);
        let err = with_silent_panics(|| run_isolated(&mut blamed, &mut world))
            .expect_err("the panic must be caught");
        assert_eq!(
            err.entity,
            Some(
                Entity::from_raw_u32(41)
                    .expect("a small literal index is always a valid entity id")
            )
        );
        assert_eq!(err.message, "agent-scoped panic");

        // A later clean tick must not inherit the previous tick's entity.
        assert!(run_isolated(&mut ok, &mut world).is_ok());
        assert_eq!(crate::tick_scope::current(), None);
    }

    use crate::components::{AgentState, ContextWindow, InferenceConfig};
    use crate::pipeline::{
        AgentBlueprint, MessageIntake, StageCursor, StageInference, StageInferences, StageProgress,
        StageSetup, StageSetups, VisitCounts,
    };
    use crate::tool_bridge::BoxedToolExec;
    use leviath_core::{Region, RegionKind};
    use leviath_providers::{
        FinishReason, InferenceRequest, InferenceResponse, ModelCapabilities, Provider, TokenUsage,
        ToolCall,
    };
    use std::sync::Mutex;

    /// A provider scripted with a queue of responses; each `infer` pops the next.
    struct Script {
        responses: Mutex<std::collections::VecDeque<InferenceResponse>>,
    }

    #[async_trait::async_trait]
    impl Provider for Script {
        async fn infer(
            &self,
            _req: InferenceRequest,
        ) -> leviath_providers::Result<InferenceResponse> {
            let next = self.responses.lock().unwrap().pop_front();
            next.ok_or_else(|| ProviderError::Other("script exhausted".to_string()))
        }
        async fn count_tokens(&self, _t: &str, _m: &str) -> usize {
            1
        }
        fn max_context_tokens(&self, _m: &str) -> usize {
            100_000
        }
        fn name(&self) -> &str {
            "script"
        }
        fn capabilities(&self, _m: &str) -> ModelCapabilities {
            ModelCapabilities::default()
        }
    }

    fn text(content: &str) -> InferenceResponse {
        InferenceResponse {
            content: content.to_string(),
            tool_calls: vec![],
            tokens_used: TokenUsage {
                prompt_tokens: 1,
                completion_tokens: 1,
                total_tokens: 2,
                cached_tokens: 0,
                cache_write_tokens: 0,
            },
            finish_reason: FinishReason::Complete,
        }
    }

    fn with_tool(id: &str, name: &str) -> InferenceResponse {
        let mut r = text("");
        r.tool_calls.push(ToolCall {
            id: id.to_string(),
            name: name.to_string(),
            arguments: serde_json::json!({}),
            thought_signature: None,
        });
        r
    }

    /// A tool service that returns a fixed result string for every call.
    struct EchoTools;
    impl ToolService for EchoTools {
        fn exec_for(&self, _entity: Entity, calls: Vec<ToolCall>) -> BoxedToolExec {
            Box::new(move || {
                Box::pin(async move {
                    calls
                        .into_iter()
                        .map(|c| (c.id, "ok".to_string()))
                        .collect()
                })
            })
        }
    }

    fn window() -> ContextWindow {
        let mut w = ContextWindow::new(10_000);
        w.add_region(Region::new("sys".to_string(), RegionKind::Pinned, 2000));
        w.add_region(Region::new(
            "conversation".to_string(),
            RegionKind::Clearable,
            10_000,
        ));
        w.add_region(Region::new(
            "tool_results".to_string(),
            RegionKind::Temporary,
            5000,
        ));
        w
    }

    fn agent_state() -> AgentState {
        AgentState {
            agent_id: "a".to_string(),
            current_stage: "s".to_string(),
            iteration: 0,
            status: AgentStatus::Active,
            spawned_children_ids: vec![],
            pending_wait: None,
            accepts_messages: true,
        }
    }

    /// A stage advertising the tools the scripted responses here actually call.
    ///
    /// Advertising them is load-bearing: dispatch refuses tools a stage never
    /// offered, so with an empty tool list every end-to-end test that drives a
    /// tool call would short-circuit into a refusal and the tool service would
    /// never be reached at all.
    fn stage(model: &str) -> StageInference {
        StageInference {
            provider_name: "script".to_string(),
            model: model.to_string(),
            tools: ["do", "read"]
                .iter()
                .map(|n| leviath_providers::Tool {
                    name: (*n).to_string(),
                    description: String::new(),
                    parameters: serde_json::json!({}),
                })
                .collect(),
            tool_filter: None,
        }
    }

    fn setup() -> StageSetup {
        StageSetup {
            inference_config: InferenceConfig {
                temperature: None,
                max_output_tokens: None,
                extra_params: Default::default(),
                batch_tool_hint: false,
                request_timeout_secs: None,
            },
            routing: None,
            accepts_messages: true,
            context_layout: None,
            system_prompt: None,
        }
    }

    fn blueprint() -> leviath_core::Blueprint {
        let layout = leviath_core::layout::ContextLayout::new(
            vec![leviath_core::layout::RegionDefinition::new(
                "conversation".to_string(),
                RegionKind::Clearable,
                10_000,
            )],
            12_000,
        );
        let s = leviath_core::Stage::new(
            "s".to_string(),
            leviath_core::blueprint::ModelConfig::new("script".to_string(), "m".to_string()),
        );
        leviath_core::Blueprint::new("t".to_string(), "d".to_string(), vec![s], layout)
    }

    /// Spawn a single-stage agent, initially ready to infer.
    fn spawn(world: &mut PipelineWorld) -> Entity {
        world.spawn_agent((
            AgentBlueprint(blueprint()),
            StageCursor { index: 0 },
            agent_state(),
            crate::components::MessageInbox::default(),
            StageProgress::default(),
            StageInferences(vec![stage("m")]),
            StageSetups(vec![setup()]),
            VisitCounts::default(),
            window(),
            stage("m"),
            setup().inference_config,
            ReadyToInfer,
        ))
    }

    fn build_world(providers: ProviderRegistry) -> PipelineWorld {
        // These agents carry no RunMetadata, so persistence never fires and the
        // runs dir is never written; any path is fine.
        PipelineWorld::new(
            providers,
            Arc::new(EchoTools),
            InferencePoolConfig::new(),
            1,
            std::env::temp_dir(),
            Handle::current(),
        )
    }

    #[tokio::test]
    async fn set_exact_token_counting_toggles_the_stage_flag() {
        let mut world = build_world(ProviderRegistry::new());
        // Default is off.
        assert!(
            !world
                .world()
                .resource::<crate::pipeline::InferenceStage>()
                .exact_token_counting
        );
        world.set_exact_token_counting(true);
        assert!(
            world
                .world()
                .resource::<crate::pipeline::InferenceStage>()
                .exact_token_counting
        );
    }

    #[tokio::test]
    async fn run_to_fixed_point_survives_a_panicking_system() {
        // A system that panics must not hang or crash the drive loop - it's
        // caught and the loop breaks (the daemon survives).
        fn boom_system() {
            panic!("simulated system panic");
        }
        let mut world = build_world(ProviderRegistry::new());
        world.add_test_system(boom_system);
        // Unattributed: nothing to fail, so the round stops immediately.
        with_silent_panics(|| world.run_to_fixed_point());
    }

    #[tokio::test]
    async fn a_panic_on_the_compute_pool_is_attributed_to_its_agent() {
        // `dispatch_inference` fans its per-agent work out over the compute task
        // pool, where the thread-local scope can't reach the driver thread that
        // catches unwinds. Those bodies run under `run_agent_parallel`, which
        // catches on the pool thread and marks the agent instead - this proves
        // the marker makes it back and fails the right run (issue #109).
        fn boom_in_parallel(
            agents: Query<(Entity, &AgentState)>,
            par_commands: bevy_ecs::system::ParallelCommands,
        ) {
            agents.par_iter().for_each(|(entity, state)| {
                if state.status != AgentStatus::Active {
                    return; // already failed - nothing left to blow up
                }
                // Clear the thread-local first: whatever attributes this panic,
                // it is demonstrably not the `enter`/`current` mechanism.
                crate::tick_scope::clear();
                crate::tick_scope::run_agent_parallel(entity, &par_commands, &mut || {
                    panic!("blew up on the compute pool");
                });
            });
        }

        let mut world = build_world(ProviderRegistry::new());
        let entity = spawn(&mut world);
        world.add_test_system(boom_in_parallel);
        with_silent_panics(|| world.run_to_fixed_point());

        let status = world.agent_status(entity);
        assert!(
            matches!(status, Some(AgentStatus::Error { ref message })
                if message.contains("a pipeline system panicked")
                    && message.contains("blew up on the compute pool")),
            "got: {status:?}"
        );
        // The marker is consumed, so a later tick doesn't re-fail the agent.
        assert!(
            world
                .world()
                .entity(entity)
                .get::<crate::tick_scope::PanickedInParallel>()
                .is_none(),
            "the marker must be drained once acted on"
        );
    }

    #[tokio::test]
    async fn a_panicking_system_fails_its_agent_instead_of_looping_forever() {
        // Before issue #109 was fixed, a panicking system was swallowed
        // anonymously: nothing changed, so the very next wake re-ticked the same
        // state and panicked again, forever, while every other agent stalled.
        // Now the agent in scope is failed, which takes it out of the dispatch
        // systems (they only act on `Active` agents) and lets the world settle.
        static VICTIM: std::sync::Mutex<Option<Entity>> = std::sync::Mutex::new(None);
        static PANICS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);

        fn boom_on_active_agent(agents: Query<(Entity, &AgentState)>) {
            // No trailing statements after the `panic!`: an unreachable tail
            // would read as uncovered under the workspace's 100% gate.
            let Some((entity, _)) = agents
                .iter()
                .find(|(_, state)| state.status == AgentStatus::Active)
            else {
                return; // the agent has been failed - nothing left to blow up
            };
            crate::tick_scope::enter(entity);
            *VICTIM
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(entity);
            PANICS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            panic!("blew up on this agent");
        }

        let mut world = build_world(ProviderRegistry::new());
        let entity = spawn(&mut world);
        world.add_test_system(boom_on_active_agent);
        with_silent_panics(|| world.run_to_fixed_point());

        let victim = VICTIM
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .take();
        assert_eq!(victim, Some(entity), "the system saw the spawned agent");
        let status = world.agent_status(entity);
        assert!(
            matches!(status, Some(AgentStatus::Error { ref message })
                if message.contains("a pipeline system panicked")
                    && message.contains("blew up on this agent")),
            "got: {status:?}"
        );
        // The loop terminated rather than re-panicking without bound.
        assert!(
            PANICS.load(std::sync::atomic::Ordering::SeqCst) <= MAX_TICK_FAILURES_PER_ROUND + 1,
            "the panic budget must stop the round"
        );
    }

    fn registry_with(responses: Vec<InferenceResponse>) -> ProviderRegistry {
        let mut r = ProviderRegistry::new();
        r.register(
            "script".to_string(),
            Arc::new(Script {
                responses: Mutex::new(responses.into_iter().collect()),
            }),
        );
        r
    }

    #[tokio::test]
    async fn agent_completes_after_nudges_exhausted() {
        // Text-only responses with no tool calls get nudged up to the max; the
        // response after the last nudge is accepted and the single-stage
        // blueprint terminates the agent. (Exercises the handle_empty_response
        // nudge loop end-to-end through the driver.)
        let mut world = build_world(registry_with(vec![
            text("thinking"),
            text("still"),
            text("more"),
            text("final"),
        ]));
        let e = spawn(&mut world);

        world.run_until_idle(30).await;

        assert_eq!(world.agent_status(e), Some(AgentStatus::Complete));
    }

    #[tokio::test]
    async fn agent_runs_tools_then_completes() {
        // First response calls a tool; after the tool result comes back the
        // second response is text-only, finishing the run.
        let mut world = build_world(registry_with(vec![with_tool("c1", "do"), text("done")]));
        let e = spawn(&mut world);

        world.run_until_idle(20).await;

        assert_eq!(world.agent_status(e), Some(AgentStatus::Complete));
        // With no routing configured, tool results land in the conversation
        // region.
        assert!(
            world
                .world()
                .get::<ContextWindow>(e)
                .unwrap()
                .get_region("conversation")
                .unwrap()
                .current_tokens
                > 0
        );
    }

    #[tokio::test]
    async fn insert_interaction_hub_installs_resource_and_attaches_wake() {
        use crate::dynamic_interaction::InteractionBackend;
        use crate::interaction_hub::InteractionHub;
        let mut world = build_world(registry_with(vec![]));
        let hub = InteractionHub::new();
        world.insert_interaction_hub(hub.clone());

        // The hub is now a world resource the reflect system reads.
        assert!(world.world().get_resource::<InteractionHub>().is_some());

        // The wake handle was attached: opening a request nudges the same wake
        // the driver parks on (a later notified() returns immediately).
        let backend = hub.backend_for("x");
        let asking = tokio::spawn(async move {
            backend
                .ask(leviath_core::interaction::InteractionRequest::free_text(
                    "q", "p", "s", true,
                ))
                .await
        });
        for _ in 0..8 {
            tokio::task::yield_now().await;
        }
        world.wake_handle().notified().await;
        hub.cancel("q");
        let _ = asking.await;
    }

    #[tokio::test]
    async fn provider_error_marks_agent_error() {
        // Empty script ⇒ the very first infer errors.
        let mut world = build_world(registry_with(vec![]));
        let e = spawn(&mut world);

        world.run_until_idle(20).await;

        assert_eq!(
            std::mem::discriminant(&world.agent_status(e).unwrap()),
            std::mem::discriminant(&AgentStatus::Error {
                message: String::new()
            })
        );
    }

    #[tokio::test]
    async fn send_message_reaches_the_agent_inbox() {
        // No responses queued: the agent dispatches inference and parks awaiting
        // it. We deliver a message; the deliver system routes it to context.
        let mut world = build_world(registry_with(vec![]));
        let e = spawn(&mut world);
        // Drive to the point the first (doomed) inference is dispatched/collected.
        world.run_until_idle(20).await;

        world
            .send_message(AgentMessage {
                agent_id: "a".to_string(),
                content: "hello".to_string(),
                target_region: Some("conversation".to_string()),
                priority: 0,
            })
            .unwrap();
        world.tick(); // deliver_messages runs

        assert!(
            world
                .world()
                .get::<ContextWindow>(e)
                .unwrap()
                .get_region("conversation")
                .unwrap()
                .current_tokens
                > 0
        );
    }

    #[tokio::test]
    async fn run_returns_on_shutdown() {
        let mut world = build_world(registry_with(vec![text("done")]));
        spawn(&mut world);
        world.shutdown(); // pre-signal: run parks then returns
        // Must return rather than loop forever.
        world.run().await;
    }

    #[tokio::test]
    async fn run_wakes_then_shuts_down() {
        // Drives run() on its own task: a wake makes it loop once (wake branch),
        // then a shutdown makes it return (shutdown branch).
        let mut world = build_world(registry_with(vec![
            text("t1"),
            text("t2"),
            text("t3"),
            text("t4"),
        ]));
        spawn(&mut world);
        let wake = world.wake_handle();
        let shutdown = world.shutdown_handle();
        let handle = tokio::spawn(async move { world.run().await });

        wake.notify_one();
        tokio::task::yield_now().await;
        shutdown.notify_one();

        handle.await.unwrap(); // returns cleanly
    }

    #[tokio::test]
    async fn send_message_errors_when_intake_dropped() {
        let mut world = build_world(registry_with(vec![]));
        // Drop the intake receiver via the world accessor, closing the channel.
        let removed = world.world_mut().remove_resource::<MessageIntake>();
        drop(removed);

        let err = world.send_message(AgentMessage {
            agent_id: "a".to_string(),
            content: "x".to_string(),
            target_region: None,
            priority: 0,
        });
        assert!(err.is_err());
    }

    #[tokio::test]
    async fn script_provider_metadata_is_exercised() {
        // Keep the mock's non-`infer`/`capabilities` methods measured.
        let p = Script {
            responses: Mutex::new(std::collections::VecDeque::new()),
        };
        assert_eq!(p.name(), "script");
        assert_eq!(p.count_tokens("t", "m").await, 1);
        assert_eq!(p.max_context_tokens("m"), 100_000);
        let _ = p.capabilities("m");
    }

    #[tokio::test]
    async fn agent_status_is_none_for_unknown_entity() {
        let world = build_world(registry_with(vec![]));
        assert_eq!(
            world.agent_status(
                Entity::from_raw_u32(999)
                    .expect("a small literal index is always a valid entity id")
            ),
            None
        );
    }

    #[tokio::test]
    async fn paused_agent_does_not_progress_until_resumed() {
        let mut world = build_world(registry_with(vec![
            text("t1"),
            text("t2"),
            text("t3"),
            text("t4"),
        ]));
        let e = spawn(&mut world);
        assert!(world.pause(e));

        world.run_until_idle(30).await;
        // Paused ⇒ parked as Idle, never inferred.
        assert_eq!(world.agent_status(e), Some(AgentStatus::Idle));

        assert!(world.resume(e));
        world.run_until_idle(30).await;
        assert_eq!(world.agent_status(e), Some(AgentStatus::Complete));
    }

    #[tokio::test]
    async fn cancelled_agent_stops_progressing() {
        let mut world = build_world(registry_with(vec![with_tool("c1", "do"), text("done")]));
        let e = spawn(&mut world);
        assert!(world.cancel(e));

        world.run_until_idle(20).await;

        assert_eq!(world.agent_status(e), Some(AgentStatus::Cancelled));
    }

    #[tokio::test]
    async fn status_ops_return_false_for_unknown_entity() {
        let mut world = build_world(registry_with(vec![]));
        assert!(!world.pause(
            Entity::from_raw_u32(999).expect("a small literal index is always a valid entity id")
        ));
        assert!(!world.resume(
            Entity::from_raw_u32(999).expect("a small literal index is always a valid entity id")
        ));
        assert!(!world.cancel(
            Entity::from_raw_u32(999).expect("a small literal index is always a valid entity id")
        ));
    }

    #[tokio::test]
    async fn spawn_from_blueprint_builds_a_runnable_agent() {
        // End-to-end via the blueprint resolver: build → drive → complete.
        let mut world = build_world(registry_with(vec![with_tool("c1", "do"), text("done")]));
        let e = world
            .spawn_from_blueprint(
                "agent-1".to_string(),
                blueprint(),
                "do the task",
                vec![crate::pipeline::ResolvedStage {
                    provider_name: "script".to_string(),
                    model: "m".to_string(),
                    tools: vec![],
                }],
                true,
            )
            .unwrap();

        world.run_until_idle(20).await;

        assert_eq!(world.agent_status(e), Some(AgentStatus::Complete));
    }

    #[tokio::test]
    async fn persists_agent_snapshot_to_runs_dir() {
        // An agent carrying RunMetadata + TokenTotals is snapshotted to disk as it
        // runs; after it completes, meta.json exists with the final status.
        let dir = tempfile::tempdir().unwrap();
        let mut world = PipelineWorld::new(
            registry_with(vec![with_tool("c1", "do"), text("done")]),
            Arc::new(EchoTools),
            InferencePoolConfig::new(),
            1,
            dir.path().to_path_buf(),
            Handle::current(),
        );
        world.spawn_agent((
            AgentBlueprint(blueprint()),
            StageCursor { index: 0 },
            agent_state(),
            crate::components::MessageInbox::default(),
            StageProgress::default(),
            StageInferences(vec![stage("m")]),
            StageSetups(vec![setup()]),
            VisitCounts::default(),
            window(),
            stage("m"),
            setup().inference_config,
            crate::persistence::RunMetadata {
                run_id: "run-42".to_string(),
                agent_name: "a".to_string(),
                agent_path: "/p".to_string(),
                task: "t".to_string(),
                model: None,
                // A real directory: the tick chain fails a run whose workspace is gone.
                workdir: std::env::temp_dir().to_string_lossy().to_string(),
                num_stages: 1,
                started_at: 0,
                parent_run_id: None,
                metadata: std::collections::HashMap::new(),
                callback_url: None,
                callback_secret: None,
                title: None,
            },
            crate::persistence::TokenTotals::default(),
            crate::pipeline::PersistWatermark::default(),
            ReadyToInfer,
        ));

        world.run_until_idle(20).await;

        // The persistence worker is fire-and-forget on its own task; poll until the
        // final (Complete) snapshot has been flushed. A short real sleep between
        // polls (rather than a bare `yield_now`) gives the worker's write actual
        // wall-clock time to land under load - otherwise the loop can spin through
        // every iteration before the write completes and spuriously time out.
        let meta_path = dir.path().join("run-42").join("meta.json");
        let mut meta = None;
        for _ in 0..200 {
            if let Ok(text) = std::fs::read_to_string(&meta_path)
                && let Ok(m) = serde_json::from_str::<leviath_core::run_meta::RunMeta>(&text)
                && m.status == leviath_core::run_meta::RunStatus::Complete
            {
                meta = Some(m);
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        }

        let meta = meta.expect("final Complete snapshot flushed to disk");
        assert_eq!(meta.run_id, "run-42");
        assert!(dir.path().join("run-42").join("context.json").exists());
    }

    #[tokio::test]
    async fn a_panicked_agent_is_recorded_as_errored_on_disk() {
        // The reported symptom in issue #109: a crashed run stayed `"running"`
        // in meta.json forever. `dispatch_persistence` is the *last* system in
        // the chain, so the tick that panics never reaches it - which is exactly
        // why `run_to_fixed_point` keeps driving after failing the agent.
        fn boom_on_active_agent(agents: Query<(Entity, &AgentState)>) {
            let Some((entity, _)) = agents
                .iter()
                .find(|(_, state)| state.status == AgentStatus::Active)
            else {
                return; // the agent has been failed - nothing left to blow up
            };
            crate::tick_scope::enter(entity);
            panic!("exploded mid-stage");
        }

        let dir = tempfile::tempdir().unwrap();
        let mut world = PipelineWorld::new(
            registry_with(vec![]),
            Arc::new(EchoTools),
            InferencePoolConfig::new(),
            1,
            dir.path().to_path_buf(),
            Handle::current(),
        );
        world.spawn_agent((
            AgentBlueprint(blueprint()),
            StageCursor { index: 0 },
            agent_state(),
            crate::components::MessageInbox::default(),
            StageProgress::default(),
            StageInferences(vec![stage("m")]),
            StageSetups(vec![setup()]),
            VisitCounts::default(),
            window(),
            stage("m"),
            setup().inference_config,
            crate::persistence::RunMetadata {
                run_id: "run-boom".to_string(),
                agent_name: "a".to_string(),
                agent_path: "/p".to_string(),
                task: "t".to_string(),
                model: None,
                workdir: "/w".to_string(),
                num_stages: 1,
                started_at: 0,
                parent_run_id: None,
                metadata: std::collections::HashMap::new(),
                callback_url: None,
                callback_secret: None,
                title: None,
            },
            crate::persistence::TokenTotals::default(),
            crate::pipeline::PersistWatermark::default(),
            ReadyToInfer,
        ));
        world.add_test_system(boom_on_active_agent);
        with_silent_panics(|| world.run_to_fixed_point());

        let meta_path = dir.path().join("run-boom").join("meta.json");
        let mut meta = None;
        for _ in 0..200 {
            if let Ok(text) = std::fs::read_to_string(&meta_path)
                && let Ok(m) = serde_json::from_str::<leviath_core::run_meta::RunMeta>(&text)
                && m.status == leviath_core::run_meta::RunStatus::Error
            {
                meta = Some(m);
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        }
        let meta = meta.expect("the panicked run must be persisted as errored");
        let error = meta.error.unwrap_or_default();
        assert!(error.contains("a pipeline system panicked"), "got: {error}");
        assert!(error.contains("exploded mid-stage"), "got: {error}");
    }

    /// A single-stage blueprint whose stage is an `interactive_points` stage with a
    /// `plan_approval` point (the shape that blocks awaiting human approval).
    fn interactive_blueprint() -> leviath_core::Blueprint {
        use leviath_core::blueprint::{InteractionPoint, InteractionStyle, StageMode};
        let layout = leviath_core::layout::ContextLayout::new(
            vec![leviath_core::layout::RegionDefinition::new(
                "conversation".to_string(),
                RegionKind::Clearable,
                10_000,
            )],
            12_000,
        );
        let mut s = leviath_core::Stage::new(
            "plan".to_string(),
            leviath_core::blueprint::ModelConfig::new("script".to_string(), "m".to_string()),
        );
        s.mode = StageMode::InteractivePoints {
            points: vec![InteractionPoint {
                name: "plan_approval".to_string(),
                prompt: "Approve?".to_string(),
                required: true,
                style: InteractionStyle::MultipleChoice,
                options: vec!["Approve".to_string(), "Abort".to_string()],
                directives: std::collections::HashMap::new(),
                abort_options: vec!["Abort".to_string()],
                edit_options: vec![],
                document_region: None,
            }],
        };
        leviath_core::Blueprint::new("t".to_string(), "d".to_string(), vec![s], layout)
    }

    #[tokio::test]
    async fn persists_interaction_point_when_a_live_agent_blocks() {
        // Drive a real agent through inference → transition → the interaction-point
        // lane until it blocks awaiting approval, and assert the daemon wrote the
        // `interactions.json` sidecar - the issue #38 persist side, end-to-end
        // through the live lane (a tool call first, then a text "plan", so the stage
        // transitions into the interaction point rather than looping on nudges).
        let dir = tempfile::tempdir().unwrap();
        let mut world = PipelineWorld::new(
            registry_with(vec![with_tool("c1", "read"), text("## Plan\n1. do it")]),
            Arc::new(EchoTools),
            InferencePoolConfig::new(),
            1,
            dir.path().to_path_buf(),
            Handle::current(),
        );
        world.insert_interaction_hub(crate::interaction_hub::InteractionHub::new());
        let e = world.spawn_agent((
            AgentBlueprint(interactive_blueprint()),
            StageCursor { index: 0 },
            agent_state(),
            crate::components::MessageInbox::default(),
            StageProgress::default(),
            StageInferences(vec![stage("m")]),
            StageSetups(vec![setup()]),
            VisitCounts::default(),
            window(),
            stage("m"),
            setup().inference_config,
            crate::persistence::RunMetadata {
                run_id: "run-ip".to_string(),
                agent_name: "a".to_string(),
                agent_path: "/p".to_string(),
                task: "t".to_string(),
                model: None,
                // A real directory: the tick chain fails a run whose workspace is gone.
                workdir: std::env::temp_dir().to_string_lossy().to_string(),
                num_stages: 1,
                started_at: 0,
                parent_run_id: None,
                metadata: std::collections::HashMap::new(),
                callback_url: None,
                callback_secret: None,
                title: None,
            },
            crate::persistence::TokenTotals::default(),
            crate::pipeline::PersistWatermark::default(),
            ReadyToInfer,
        ));

        world.run_until_idle(30).await;
        // `run_until_idle` stops once no inference/tool is in flight, but the
        // interaction-point ask task registers in the hub just after; the real
        // daemon's `run()` loop catches its wake, so pump fixed points here until
        // `reflect_interaction_status` flips the agent to Waiting (and persistence
        // captures the sidecar).
        for _ in 0..50 {
            if world.agent_status(e) == Some(AgentStatus::Waiting) {
                break;
            }
            tokio::task::yield_now().await;
            world.run_to_fixed_point();
        }
        assert_eq!(world.agent_status(e), Some(AgentStatus::Waiting));

        // Poll until the interaction sidecar lands (the persistence worker writes it
        // on its own task once the agent is parked Waiting at the point).
        let path = dir.path().join("run-ip").join("interactions.json");
        let mut sidecar = None;
        for _ in 0..200 {
            if let Ok(t) = std::fs::read_to_string(&path)
                && let Ok(s) =
                    serde_json::from_str::<crate::interaction_points::InteractionPointState>(&t)
            {
                sidecar = Some(s);
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        }
        let s = sidecar.expect("interaction-point sidecar flushed to disk");
        assert_eq!(s.cursor, 0);
        assert_eq!(s.round, 0);
        assert_eq!(s.body, "## Plan\n1. do it");
    }

    #[tokio::test]
    async fn flush_and_stop_drains_queued_snapshots() {
        // Unlike a plain shutdown, `flush_and_stop` awaits the persistence worker,
        // so the final snapshot is guaranteed on disk the instant it returns - no
        // filesystem polling required (contrast the test above).
        let dir = tempfile::tempdir().unwrap();
        let mut world = PipelineWorld::new(
            registry_with(vec![with_tool("c1", "do"), text("done")]),
            Arc::new(EchoTools),
            InferencePoolConfig::new(),
            1,
            dir.path().to_path_buf(),
            Handle::current(),
        );
        world.spawn_agent((
            AgentBlueprint(blueprint()),
            StageCursor { index: 0 },
            agent_state(),
            crate::components::MessageInbox::default(),
            StageProgress::default(),
            StageInferences(vec![stage("m")]),
            StageSetups(vec![setup()]),
            VisitCounts::default(),
            window(),
            stage("m"),
            setup().inference_config,
            crate::persistence::RunMetadata {
                run_id: "run-flush".to_string(),
                agent_name: "a".to_string(),
                agent_path: "/p".to_string(),
                task: "t".to_string(),
                model: None,
                // A real directory: the tick chain fails a run whose workspace is gone.
                workdir: std::env::temp_dir().to_string_lossy().to_string(),
                num_stages: 1,
                started_at: 0,
                parent_run_id: None,
                metadata: std::collections::HashMap::new(),
                callback_url: None,
                callback_secret: None,
                title: None,
            },
            crate::persistence::TokenTotals::default(),
            crate::pipeline::PersistWatermark::default(),
            ReadyToInfer,
        ));

        world.run_until_idle(20).await;
        world.flush_and_stop().await;

        // Read immediately - the drain guarantees the write landed.
        let meta_path = dir.path().join("run-flush").join("meta.json");
        let text = std::fs::read_to_string(&meta_path).expect("meta.json flushed on stop");
        let meta: leviath_core::run_meta::RunMeta = serde_json::from_str(&text).unwrap();
        assert_eq!(meta.run_id, "run-flush");
        assert_eq!(meta.status, leviath_core::run_meta::RunStatus::Complete);

        // A second call is a no-op (resource already removed, task taken) - no panic.
        world.flush_and_stop().await;
        assert!(meta_path.exists());
    }

    #[tokio::test]
    async fn world_init_and_restore_needs_no_daemon_infra() {
        // `PipelineWorld::new` + `restore::restore_agent` form a self-contained
        // spin-up→restore path: no control socket, HTTP server, PID files, or build
        // markers - only providers, a tool service, a runs dir, and a runtime. This
        // locks that in so the daemon wiring stays optional.
        use leviath_core::region::EntryKind;
        use leviath_core::run_meta::{ContextSnapshot, RegionEntrySnapshot, RegionSnapshot};

        let dir = tempfile::tempdir().unwrap();
        let mut world = PipelineWorld::new(
            registry_with(vec![text("unused")]),
            Arc::new(EchoTools),
            InferencePoolConfig::new(),
            1,
            dir.path().to_path_buf(),
            Handle::current(),
        );
        let entity = world.spawn_agent((
            AgentBlueprint(blueprint()),
            StageCursor { index: 0 },
            agent_state(),
            crate::components::MessageInbox::default(),
            StageProgress::default(),
            StageInferences(vec![stage("m")]),
            StageSetups(vec![setup()]),
            VisitCounts::default(),
            window(),
            stage("m"),
            setup().inference_config,
            crate::persistence::TokenTotals::default(),
        ));

        let snapshot = ContextSnapshot {
            stage_name: "s0".to_string(),
            total_tokens: 4,
            max_tokens: 10_000,
            regions: vec![RegionSnapshot {
                name: "conversation".to_string(),
                kind: "clearable".to_string(),
                current_tokens: 4,
                max_tokens: 10_000,
                entries: vec![RegionEntrySnapshot {
                    content: "restored turn".to_string(),
                    tokens: 4,
                    kind: EntryKind::UserMessage,
                    metadata: None,
                    key: None,
                    taint: Default::default(),
                }],
            }],
        };
        crate::restore::restore_agent(
            world.world_mut(),
            entity,
            &snapshot,
            0,
            3,
            crate::persistence::TokenTotals::default(),
        );

        let state = world
            .world()
            .get::<crate::components::AgentState>(entity)
            .unwrap();
        assert_eq!(state.status, AgentStatus::Active);
        assert_eq!(state.iteration, 3);
        let win = world
            .world()
            .get::<crate::components::ContextWindow>(entity)
            .unwrap();
        assert_eq!(
            win.get_region("conversation").unwrap().content[0].content,
            "restored turn"
        );
    }

    #[tokio::test]
    async fn spawn_from_blueprint_errors_on_oversized_system_prompt() {
        let mut world = build_world(registry_with(vec![]));
        // A blueprint whose stage carries an enormous system prompt in a tiny
        // pinned region overflows at spawn.
        let layout = leviath_core::layout::ContextLayout::new(
            vec![leviath_core::layout::RegionDefinition::new(
                "task".to_string(),
                RegionKind::Pinned,
                50,
            )],
            1000,
        );
        let mut s = leviath_core::Stage::new(
            "s".to_string(),
            leviath_core::blueprint::ModelConfig::new("script".to_string(), "m".to_string()),
        );
        s.config.insert(
            "system_prompt".to_string(),
            serde_json::Value::String("x".repeat(100_000)),
        );
        let bp = leviath_core::Blueprint::new("t".to_string(), "d".to_string(), vec![s], layout);

        let err = world.spawn_from_blueprint(
            "a".to_string(),
            bp,
            "task",
            vec![crate::pipeline::ResolvedStage {
                provider_name: "script".to_string(),
                model: "m".to_string(),
                tools: vec![],
            }],
            true,
        );
        assert!(err.is_err());
    }

    #[tokio::test]
    async fn wake_handle_and_run_until_idle_bound_are_exposed() {
        // Exercises the wake handle accessor and the max-waits safety bound on a
        // world with an agent parked on an in-flight inference that never
        // resolves within the bound (script returns after we stop waiting).
        let mut world = build_world(registry_with(vec![with_tool("c1", "do"), text("done")]));
        let _ = world.wake_handle();
        let e = spawn(&mut world);
        world.run_until_idle(0).await; // bound 0 ⇒ no extra waits
        // With no waits allowed we may not have observed completion yet; drain.
        world.run_until_idle(20).await;
        assert_eq!(world.agent_status(e), Some(AgentStatus::Complete));
    }
}