graphrt-core 0.7.0

Executor-agnostic graph runtime for agentic AI workflows: graph model, variable pool, scheduler, events, snapshots and built-in control nodes.
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
//! Frame execution: runs one scope (the root graph or one container body run).
//!
//! A frame owns its variable pool and per-node / per-edge state; the graph is
//! shared and immutable. Node futures run concurrently in a
//! [`FuturesUnordered`] (a node running alone is polled directly, without the
//! task allocation that costs); the frame loop is the only place where state changes,
//! so no locking is needed. Container nodes receive a [`ScopeRunner`] that runs
//! their body as a nested frame with the same machinery.

use std::collections::{HashMap, HashSet, VecDeque};
use std::panic::AssertUnwindSafe;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::time::Duration;

use futures::future::{BoxFuture, Either};
use futures::stream::FuturesUnordered;
use futures::{FutureExt, StreamExt};
use indexmap::IndexMap;

use super::Limits;
use super::control::{Control, Stop};
use super::ids::{new_id, new_id_arc};
use crate::dsl::{ErrorStrategy, RetryPolicy};
use crate::event::{Event, FrameId, PauseReason, Usage};
use crate::graph::{EdgeIx, FAIL_BRANCH, Graph, NodeIx};
use crate::node::{
    Clock, EventSink, Node, NodeContext, NodeError, NodeOutcome, NodeOutput, NodeResult, NodeRole, PauseRequest,
    Resume, Services, Subgraph, SubgraphRequest, SubgraphResult, merge_writes,
};
use crate::pool::{ENV, SYS, Selector, VariablePool};
use crate::redact::SecretSet;
use crate::snapshot::{EdgeStatus, FrameSnapshot, NodeSnapshot, NodeStatus};
use crate::value::Value;
use crate::variables::Variables;

/// Node kind whose completion marks a loop body run as "broke".
const LOOP_END_KIND: &str = "loop-end";

/// Upper bound for a single retry delay, whatever the policy or server asks for.
const MAX_RETRY_DELAY_MS: u64 = 5 * 60 * 1000;

/// State shared by every frame of one run.
pub(crate) struct RunShared {
    /// The token and cost budget of the run, shared by every frame and child
    /// (builds with model nodes, cargo feature `budgets`; the others keep
    /// what a resumed snapshot carried, unchanged).
    #[cfg(feature = "budgets")]
    pub(crate) budget: Arc<crate::budget::RunBudget>,
    #[cfg(not(feature = "budgets"))]
    pub(crate) budget: crate::budget::BudgetState,
    pub(crate) graph: Arc<Graph>,
    pub(crate) services: Arc<Services>,
    pub(crate) clock: Arc<dyn Clock>,
    pub(crate) limits: Limits,
    pub(crate) control: Arc<Control>,
    pub(crate) sink: EventSink,
    pub(crate) steps: AtomicU64,
    pub(crate) handled_errors: AtomicU32,
    pub(crate) usage: Mutex<Usage>,
    /// The executions of nodes that track their turns, so what one charged is
    /// counted when the run ends while it still runs (a sibling stopped the run).
    #[cfg(feature = "budgets")]
    pub(crate) executions: Mutex<Vec<Arc<crate::budget::NodeProgress>>>,
    /// Environment variables re-applied to every restored frame (snapshots exclude them).
    pub(crate) env: Vec<(String, Value)>,
    /// Re-supplied plaintext for redacted secrets outside `env` (see `ResumeFrom::secrets`).
    pub(crate) secrets: Vec<(Selector, Value)>,
    /// Host input for paused nodes, keyed by `"<frame>/<node_id>"` or plain node id.
    pub(crate) resume_inputs: IndexMap<String, Value>,
    /// Plaintext of every secret seen in this run, scrubbed from node error messages.
    pub(crate) secret_values: Arc<SecretSet>,
    /// What a snapshot of this run carries (see `Engine::fingerprint`):
    /// the graph's own without linked workflows.
    #[cfg(feature = "subagents")]
    pub(crate) fingerprint: String,
    /// Workflows the run's nodes may start as child runs.
    #[cfg(feature = "subagents")]
    pub(crate) linked: Arc<IndexMap<String, super::LinkedWorkflow>>,
    /// The nodes (as `frame/node`) whose child runs paused in this run: only
    /// their pauses of kind `CHILD_PAUSE` are passed on without a reason.
    #[cfg(feature = "subagents")]
    pub(crate) parents: Mutex<HashSet<String>>,
    /// Frame ids of the child runs running now, every level of nesting included.
    #[cfg(feature = "subagents")]
    pub(crate) running_children: Mutex<HashSet<String>>,
}

impl std::fmt::Debug for RunShared {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RunShared").field("steps", &self.steps).finish_non_exhaustive()
    }
}

impl RunShared {
    /// `error` with every secret this run has seen masked in its message: nodes that
    /// use a secret's plaintext can get it echoed back (e.g. in a provider error body),
    /// and the message goes to events, retries, outputs and run records.
    pub(crate) fn scrub_error(&self, mut error: NodeError) -> NodeError {
        self.secret_values.scrub_in_place(&mut error.message);
        error
    }

    pub(crate) fn emit(&self, event: Event) {
        (self.sink)(event);
    }

    /// Count one node attempt. Returns `false` (and stops the run) when the step budget is exhausted.
    fn take_step(&self) -> bool {
        let used = self.steps.fetch_add(1, Ordering::AcqRel).saturating_add(1);
        if used > self.limits.max_steps {
            self.control.stop(Stop::Fail(format!("step limit of {} exceeded", self.limits.max_steps)));
            return false;
        }
        true
    }

    fn add_usage(&self, usage: &Usage) {
        let mut total = self.usage.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
        total.add(usage);
    }

    pub(crate) fn total_usage(&self) -> Usage {
        *self.usage.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// Keep `progress` to count it when the run ends before the execution does.
    #[cfg(feature = "budgets")]
    fn track(&self, progress: &Arc<crate::budget::NodeProgress>) {
        let mut executions = self.executions.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
        executions.retain(|known| !known.settle_peek());
        executions.push(Arc::clone(progress));
    }

    /// The usage of a run that ended (succeeded, failed, stopped): what nodes
    /// still running had charged counts too. Not for a pause, whose nodes report
    /// their whole usage when they finish after the resume.
    pub(crate) fn final_usage(&self) -> Usage {
        #[cfg(feature = "budgets")]
        {
            let executions =
                std::mem::take(&mut *self.executions.lock().unwrap_or_else(std::sync::PoisonError::into_inner));
            for progress in executions {
                if progress.settle() {
                    self.add_usage(&progress.charged());
                }
            }
        }
        self.total_usage()
    }

    fn now_ms(&self) -> u64 {
        self.clock.now_ms()
    }

    /// Host input for a node resumed in `frame`: the frame-qualified key
    /// (`"iter[2]/approve"`) wins over the plain node id (`"approve"`). Inside
    /// a child run only the qualified key counts: two workflows may name a
    /// node the same, and an answer meant for one must not reach the other.
    fn resume_input(&self, frame: &str, node_id: &str, qualified_only: bool) -> Option<Value> {
        if !frame.is_empty()
            && let Some(value) = self.resume_inputs.get(&format!("{frame}/{node_id}"))
        {
            return Some(value.clone());
        }
        #[cfg(feature = "subagents")]
        if qualified_only {
            return None;
        }
        #[cfg(not(feature = "subagents"))]
        let _ = qualified_only;
        self.resume_inputs.get(node_id).cloned()
    }
}

/// How a frame ended.
#[derive(Debug)]
pub(crate) enum FrameOutcome {
    Completed {
        pool: VariablePool,
        outputs: IndexMap<String, Value>,
        writes: Vec<(Selector, Value)>,
        usage: Usage,
        broke: bool,
    },
    Failed {
        error: NodeError,
        usage: Usage,
    },
    Paused {
        snapshot: Box<FrameSnapshot>,
        reasons: Vec<PauseReason>,
    },
    /// The run was aborted or hit a run-wide limit (see [`Control::stop_reason`]).
    Stopped,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum State {
    Ready,
    Running,
    Succeeded,
    Failed,
    Skipped,
    Paused,
}

#[derive(Debug)]
struct NodeRt {
    state: State,
    /// Minted when the node starts, is skipped or is snapshotted, whichever
    /// comes first, so a node costs one clock read for its id and its start time.
    exec_id: Option<Arc<str>>,
    attempt: u32,
    /// Saved state of a node that paused itself (kept while paused, handed back on resume).
    resume_state: Option<serde_json::Value>,
    /// The checkpoint of this execution, for a node that tracks its turns:
    /// shared with the running task, kept when the run is preempted.
    progress: Option<Arc<crate::budget::NodeProgress>>,
    /// What the run counted of this execution when it paused: the node reports
    /// it again in its usage when it finishes after the resume.
    counted: Usage,
}

/// Result of one node task (all attempts).
struct TaskDone {
    ix: NodeIx,
    attempt: u32,
    result: NodeResult,
    elapsed_ms: u64,
    /// Clock reading taken when the task finished, if it read the clock.
    finished_ms: Option<u64>,
}

/// One executing frame.
pub(crate) struct Frame {
    run: Arc<RunShared>,
    /// The graph this frame executes: the run's own, or a workflow linked to
    /// it that an agent started as a child run. Builds without child runs
    /// always run the run's own graph and keep no field for it.
    #[cfg(feature = "subagents")]
    graph: Arc<Graph>,
    id: FrameId,
    depth: u32,
    is_root: bool,
    /// A child run, or a scope inside one: resume inputs need qualified keys.
    #[cfg(feature = "subagents")]
    in_child: bool,
    /// What the child run this frame belongs to has of its own (`None`: the run's root lineage).
    #[cfg(feature = "subagents")]
    child: Option<Arc<ChildLink>>,
    scope_nodes: Vec<NodeIx>,
    /// Ids of the scope's nodes, built on the first variable write that needs them.
    scope_ids: std::cell::OnceCell<HashSet<Arc<str>>>,
    pool: VariablePool,
    nodes: ByIndex<NodeRt>,
    edges: ByIndex<EdgeStatus>,
    outputs: IndexMap<String, Value>,
    writes: Vec<(Selector, Value)>,
    usage: Usage,
    broke: bool,
    reasons: Vec<PauseReason>,
    ready: VecDeque<NodeIx>,
    /// Incoming-edge tallies of nodes not yet decided, so readiness checks cost
    /// O(1) per resolved edge instead of rescanning every incoming edge (which
    /// made wide fan-ins quadratic). Built lazily from `edges` on first use.
    incoming: ByIndex<Incoming>,
    /// Clock reading shared by the work done between two waits of the frame
    /// loop (see [`Frame::now_ms`]); cleared before every wait.
    burst_now_ms: Option<u64>,
}

/// Resolution state of a node's incoming edges.
#[derive(Clone, Copy, Debug)]
struct Incoming {
    unresolved: usize,
    taken: bool,
}

/// Hasher for the frame's maps keyed by graph indices (`NodeIx`, `EdgeIx`): one
/// multiplication instead of SipHash. The keys are internal indices, never user
/// input, so hash-flooding resistance buys nothing here.
#[derive(Clone, Copy, Default)]
struct IndexHasher(u64);

impl std::hash::Hasher for IndexHasher {
    fn finish(&self) -> u64 {
        self.0
    }

    fn write(&mut self, bytes: &[u8]) {
        for byte in bytes {
            self.0 = (self.0.rotate_left(8) ^ u64::from(*byte)).wrapping_mul(0x9e37_79b9_7f4a_7c15);
        }
    }

    fn write_usize(&mut self, index: usize) {
        // Fibonacci hashing: spreads consecutive indices over the high bits the
        // hash table uses for its control bytes.
        self.0 = (index as u64).wrapping_mul(0x9e37_79b9_7f4a_7c15);
    }
}

/// A map keyed by graph indices.
type ByIndex<V> = HashMap<usize, V, std::hash::BuildHasherDefault<IndexHasher>>;

/// An empty [`ByIndex`] sized for `capacity` entries.
fn by_index<V>(capacity: usize) -> ByIndex<V> {
    HashMap::with_capacity_and_hasher(capacity, std::hash::BuildHasherDefault::default())
}

impl Frame {
    /// The graph this frame executes.
    fn graph(&self) -> &Arc<Graph> {
        #[cfg(feature = "subagents")]
        return &self.graph;
        #[cfg(not(feature = "subagents"))]
        return &self.run.graph;
    }

    /// Whether this frame belongs to a child run (resume inputs need qualified keys).
    fn in_child(&self) -> bool {
        #[cfg(feature = "subagents")]
        return self.in_child;
        #[cfg(not(feature = "subagents"))]
        return false;
    }

    /// A fresh frame for the scope owned by `owner` (`None` = root).
    pub(crate) fn fresh(
        run: Arc<RunShared>,
        id: FrameId,
        owner: Option<NodeIx>,
        depth: u32,
        pool: VariablePool,
    ) -> Result<Self, NodeError> {
        #[cfg(feature = "subagents")]
        let graph = Arc::clone(&run.graph);
        Self::fresh_in(
            run,
            #[cfg(feature = "subagents")]
            graph,
            id,
            owner,
            depth,
            pool,
        )
    }

    /// [`Frame::fresh`] for a scope of `graph` (builds with child runs: the
    /// others always run the run's own graph).
    pub(crate) fn fresh_in(
        run: Arc<RunShared>,
        #[cfg(feature = "subagents")] graph: Arc<Graph>,
        id: FrameId,
        owner: Option<NodeIx>,
        depth: u32,
        pool: VariablePool,
    ) -> Result<Self, NodeError> {
        let mut frame = Self::empty(
            run,
            #[cfg(feature = "subagents")]
            graph,
            id,
            owner,
            depth,
            pool,
        )?;
        let entry = frame.scope_entry(owner)?;
        frame.mark_ready(entry);
        Ok(frame)
    }

    /// A frame restored from a snapshot. Environment variables are re-applied.
    pub(crate) fn restore(
        run: Arc<RunShared>,
        id: FrameId,
        owner: Option<NodeIx>,
        depth: u32,
        snapshot: FrameSnapshot,
    ) -> Result<Self, NodeError> {
        #[cfg(feature = "subagents")]
        let graph = Arc::clone(&run.graph);
        Self::restore_in(
            run,
            #[cfg(feature = "subagents")]
            graph,
            id,
            owner,
            depth,
            snapshot,
            #[cfg(feature = "subagents")]
            false,
        )
    }

    /// [`Frame::restore`] for a scope of `graph`. `in_child`: the frame belongs
    /// to a child run (even one of the run's own workflow).
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn restore_in(
        run: Arc<RunShared>,
        #[cfg(feature = "subagents")] graph: Arc<Graph>,
        id: FrameId,
        owner: Option<NodeIx>,
        depth: u32,
        snapshot: FrameSnapshot,
        #[cfg(feature = "subagents")] in_child: bool,
    ) -> Result<Self, NodeError> {
        #[cfg(not(feature = "subagents"))]
        let in_child = false;
        let mut pool = VariablePool::from_snapshot(snapshot.pool);
        // The run's environment belongs to the run's root lineage. A child run
        // gets its own back from the runner that restores it.
        if !in_child {
            for (name, value) in &run.env {
                pool.insert(ENV, name, value.clone());
            }
        }
        // The run's re-supplied secrets name selectors of the run's own frames:
        // a child's nodes are other nodes, even when named the same.
        let secrets: &[(Selector, Value)] = if in_child { &[] } else { &run.secrets };
        for (selector, value) in secrets {
            if pool.get(selector).is_some_and(|v| v.is_redacted_secret()) {
                // The selector exists, so the write cannot fail on a missing index.
                if pool.set_path(selector, value.clone()).is_err() {
                    continue;
                }
            }
        }
        let mut frame = Self::empty(
            run,
            #[cfg(feature = "subagents")]
            graph,
            id,
            owner,
            depth,
            pool,
        )?;
        #[cfg(feature = "subagents")]
        {
            frame.in_child = in_child;
        }
        frame.outputs = snapshot.outputs.into_iter().map(|(k, v)| (k, v.from_snapshot_value())).collect();
        frame.writes = snapshot.writes.into_iter().map(|(s, v)| (s, v.from_snapshot_value())).collect();
        frame.broke = snapshot.broke;
        frame.usage = snapshot.usage;
        let graph = Arc::clone(frame.graph());
        for (node_id, ns) in snapshot.nodes {
            let ix = graph
                .index_of(&node_id)
                .filter(|ix| frame.scope_nodes.contains(ix))
                .ok_or_else(|| NodeError::fatal("snapshot", format!("snapshot references unknown node `{node_id}`")))?;
            let state = match ns.status {
                NodeStatus::Pending | NodeStatus::Paused => State::Ready,
                NodeStatus::Succeeded => State::Succeeded,
                NodeStatus::Failed => State::Failed,
                NodeStatus::Skipped => State::Skipped,
            };
            // A paused node always resumes, even when its saved state is `null`
            // (which a JSON round trip turns into `None`).
            // A node preempted while running starts its next attempt from its checkpoint.
            #[cfg(feature = "budgets")]
            let progress = ns.checkpoint.map(|state| {
                let progress = Arc::new(crate::budget::NodeProgress::with(Some(state)));
                frame.run.track(&progress);
                progress
            });
            #[cfg(not(feature = "budgets"))]
            let progress = None;
            let resume_state = if ns.status == NodeStatus::Paused {
                Some(ns.resume_state.unwrap_or(serde_json::Value::Null))
            } else {
                None
            };
            frame.nodes.insert(
                ix,
                NodeRt {
                    state,
                    exec_id: Some(Arc::from(ns.exec_id)),
                    attempt: ns.attempt,
                    resume_state,
                    progress,
                    counted: ns.counted.unwrap_or_default(),
                },
            );
        }
        let edge_index: HashMap<&str, EdgeIx> =
            graph.edges().iter().enumerate().map(|(ex, e)| (e.id.as_ref(), ex)).collect();
        for (edge_id, status) in snapshot.edges {
            let ex = edge_index
                .get(edge_id.as_str())
                .copied()
                .ok_or_else(|| NodeError::fatal("snapshot", format!("snapshot references unknown edge `{edge_id}`")))?;
            frame.edges.insert(ex, status);
        }
        // Queue pending nodes in topological order.
        let order = frame.scope_nodes.clone();
        for ix in order {
            if frame.nodes.get(&ix).is_some_and(|n| n.state == State::Ready) {
                frame.ready.push_back(ix);
            }
        }
        Ok(frame)
    }

    fn empty(
        run: Arc<RunShared>,
        #[cfg(feature = "subagents")] graph: Arc<Graph>,
        id: FrameId,
        owner: Option<NodeIx>,
        depth: u32,
        pool: VariablePool,
    ) -> Result<Self, NodeError> {
        #[cfg(not(feature = "subagents"))]
        let graph = &run.graph;
        let scope = match owner {
            None => graph.root_scope(),
            Some(o) => graph.scope_of(o),
        }
        .ok_or_else(|| NodeError::fatal("graph", "scope not found"))?;
        let scope_nodes = scope.nodes.clone();
        let scope_len = scope_nodes.len();
        Ok(Self {
            is_root: owner.is_none(),
            #[cfg(feature = "subagents")]
            in_child: false,
            #[cfg(feature = "subagents")]
            child: None,
            run,
            #[cfg(feature = "subagents")]
            graph,
            id,
            depth,
            scope_nodes,
            scope_ids: std::cell::OnceCell::new(),
            pool,
            nodes: by_index(scope_len),
            edges: by_index(scope_len),
            outputs: IndexMap::new(),
            writes: Vec::new(),
            usage: Usage::default(),
            broke: false,
            reasons: Vec::new(),
            ready: VecDeque::new(),
            incoming: by_index(scope_len),
            burst_now_ms: None,
        })
    }

    fn scope_ids(&self) -> &HashSet<Arc<str>> {
        self.scope_ids.get_or_init(|| {
            self.scope_nodes.iter().filter_map(|&ix| self.graph().node(ix)).map(|n| n.id.clone()).collect()
        })
    }

    fn scope_entry(&self, owner: Option<NodeIx>) -> Result<NodeIx, NodeError> {
        let scope = match owner {
            None => self.graph().root_scope(),
            Some(o) => self.graph().scope_of(o),
        };
        scope.map(|s| s.entry).ok_or_else(|| NodeError::fatal("graph", "scope not found"))
    }

    /// Set a variable directly in this frame's pool.
    pub(crate) fn set_variable(&mut self, namespace: &str, name: &str, value: Value) {
        self.pool.insert(namespace, name, value);
    }

    fn mark_ready(&mut self, ix: NodeIx) {
        let rt = self.nodes.entry(ix).or_insert_with(|| NodeRt {
            state: State::Ready,
            exec_id: None,
            attempt: 0,
            resume_state: None,
            progress: None,
            counted: Usage::default(),
        });
        rt.state = State::Ready;
        self.ready.push_back(ix);
    }

    /// The current time for bookkeeping done without waiting: node starts (id
    /// and start time) and skips.
    ///
    /// Everything between two waits of the frame loop takes microseconds, so it
    /// shares one clock reading, seeded by the finishing time a completed task
    /// already read. A chain then reads the clock once per node instead of
    /// twice, and a batch of nodes started together reads it once.
    fn now_ms(&mut self) -> u64 {
        *self.burst_now_ms.get_or_insert_with(|| self.run.now_ms())
    }

    fn node_id(&self, ix: NodeIx) -> Arc<str> {
        self.graph().node(ix).map_or_else(|| Arc::from(""), |n| Arc::clone(&n.id))
    }

    /// Run the frame to completion, failure, pause or stop.
    pub(crate) async fn run(mut self) -> FrameOutcome {
        let control = self.run.control.clone();
        let mut running = FuturesUnordered::new();
        // A node that runs alone (the common case in chains) is polled from
        // this slot instead of `running`, which would allocate a task for it.
        let mut solo = std::pin::pin!(None);
        loop {
            if control.is_stopped() {
                return FrameOutcome::Stopped;
            }
            if control.preempt_requested() {
                return self.preempt(solo.as_mut(), &mut running);
            }
            let seen = control.version();
            if self.is_root {
                self.apply_external_updates();
            }
            // Schedule ready nodes.
            if !control.pause_requested() {
                let max_concurrency = self.run.limits.max_concurrency.max(1);
                while running.len() + usize::from(solo.is_some()) < max_concurrency {
                    let Some(ix) = self.ready.pop_front() else { break };
                    let task = self.start(ix);
                    if solo.is_none() && running.is_empty() && self.ready.is_empty() {
                        solo.set(Some(task));
                    } else {
                        running.push(task);
                    }
                }
            }
            if running.is_empty() && solo.is_none() {
                return self.finish();
            }
            self.burst_now_ms = None;
            let next = futures::future::poll_fn(|cx| poll_tasks(cx, solo.as_mut(), &mut running));
            let changed = control.changed(seen);
            futures::pin_mut!(changed);
            match futures::future::select(next, changed).await {
                Either::Left((Some(done), _)) => {
                    if let Some(outcome) = self.on_done(done) {
                        return outcome;
                    }
                    if let Some(outcome) = self.commit_finished(&mut running) {
                        return outcome;
                    }
                }
                Either::Left((None, _)) | Either::Right(((), _)) => {}
            }
        }
    }

    /// Apply every task in `running` that has already finished, before the
    /// loop starts new nodes.
    ///
    /// Running nodes hold snapshots of the pool, so the first commit after a
    /// node starts copies the namespace table and the touched shard. Starting a
    /// node between every two commits (as happens in a wide fan-out) made every
    /// commit pay that copy; committing the finished batch first makes it share
    /// one copy of the table and of each shard it touches.
    ///
    /// The tasks are polled with a no-op waker. That is sound because the frame
    /// loop always polls `running` again with its real waker before waiting.
    fn commit_finished<F: Future<Output = TaskDone>>(
        &mut self,
        running: &mut FuturesUnordered<F>,
    ) -> Option<FrameOutcome> {
        if running.is_empty() {
            return None;
        }
        let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
        while let std::task::Poll::Ready(Some(done)) = running.poll_next_unpin(&mut cx) {
            if let Some(outcome) = self.on_done(done) {
                return Some(outcome);
            }
        }
        None
    }

    fn apply_external_updates(&mut self) {
        for (selector, value) in self.run.control.take_updates() {
            // An update addressing a missing array index cannot be applied; it is
            // dropped without a `VariableUpdated` event.
            if self.pool.set_path(&selector, value.clone()).is_err() {
                continue;
            }
            self.run.emit(Event::VariableUpdated { frame: self.id.clone(), node_id: Arc::from(""), selector, value });
        }
    }

    /// Preempt the frame: apply what already finished, then cancel the rest.
    ///
    /// Every task gets one last poll first (with a no-op waker, the tasks are
    /// dropped right after): nodes that are done get committed, and container
    /// nodes see the preempt in their own body frames and pause with a snapshot,
    /// so they resume where they stopped instead of starting over. Nodes still
    /// running after that are cancelled and saved as pending (keeping their
    /// `exec_id` and attempt), so they run again on resume.
    fn preempt<F: Future<Output = TaskDone>>(
        mut self,
        mut solo: std::pin::Pin<&mut Option<F>>,
        running: &mut FuturesUnordered<F>,
    ) -> FrameOutcome {
        let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
        if let Some(task) = solo.as_mut().as_pin_mut()
            && let std::task::Poll::Ready(done) = task.poll(&mut cx)
        {
            solo.set(None);
            if let Some(outcome) = self.on_done(done) {
                return outcome;
            }
        }
        if let Some(outcome) = self.commit_finished(running) {
            return outcome;
        }
        // A node that finished in that last poll may have passed the budget.
        if self.run.control.is_stopped() {
            return FrameOutcome::Stopped;
        }
        let mut cancelled: Vec<NodeIx> =
            self.nodes.iter().filter(|(_, rt)| rt.state == State::Running).map(|(ix, _)| *ix).collect();
        cancelled.sort_unstable();
        for ix in cancelled {
            if let Some(rt) = self.nodes.get_mut(&ix) {
                // A resumed node already handed its saved state to the cancelled
                // attempt, so it starts over (at-least-once).
                rt.state = State::Ready;
            }
            self.ready.push_back(ix);
        }
        self.finish()
    }

    /// Nothing is running: decide between completed and paused.
    fn finish(self) -> FrameOutcome {
        let has_paused = self.nodes.values().any(|n| n.state == State::Paused);
        if has_paused || !self.ready.is_empty() {
            let reasons = self.reasons.clone();
            return FrameOutcome::Paused { snapshot: Box::new(self.snapshot()), reasons };
        }
        FrameOutcome::Completed {
            pool: self.pool,
            outputs: self.outputs,
            writes: self.writes,
            usage: self.usage,
            broke: self.broke,
        }
    }

    /// Serializable state of this frame.
    pub(crate) fn snapshot(&self) -> FrameSnapshot {
        let mut nodes = IndexMap::new();
        for &ix in &self.scope_nodes {
            let Some(rt) = self.nodes.get(&ix) else { continue };
            let status = match rt.state {
                State::Ready | State::Running => {
                    if rt.resume_state.is_some() {
                        NodeStatus::Paused
                    } else {
                        NodeStatus::Pending
                    }
                }
                State::Paused => NodeStatus::Paused,
                State::Succeeded => NodeStatus::Succeeded,
                State::Failed => NodeStatus::Failed,
                State::Skipped => NodeStatus::Skipped,
            };
            nodes.insert(
                String::from(&*self.node_id(ix)),
                NodeSnapshot {
                    status,
                    exec_id: rt.exec_id.as_deref().map_or_else(|| new_id(self.run.now_ms()), String::from),
                    attempt: rt.attempt,
                    resume_state: rt.resume_state.clone(),
                    #[cfg(feature = "budgets")]
                    checkpoint: rt.progress.as_ref().and_then(|progress| progress.get()),
                    #[cfg(not(feature = "budgets"))]
                    checkpoint: None,
                    counted: (!rt.counted.is_zero()).then_some(rt.counted),
                },
            );
        }
        let mut edges = IndexMap::new();
        let mut known: Vec<(&EdgeIx, &EdgeStatus)> = self.edges.iter().collect();
        known.sort_by_key(|(ex, _)| **ex);
        for (ex, status) in known {
            if let Some(e) = self.graph().edge(*ex) {
                edges.insert(String::from(&*e.id), *status);
            }
        }
        FrameSnapshot {
            pool: self.pool.to_snapshot(),
            nodes,
            edges,
            outputs: self.outputs.iter().map(|(k, v)| (k.clone(), v.to_snapshot_value())).collect(),
            writes: self.writes.iter().map(|(s, v)| (s.clone(), v.to_snapshot_value())).collect(),
            broke: self.broke,
            usage: self.usage,
        }
    }

    /// Start a node: emit `NodeStarted` and build its task future.
    ///
    /// The future is returned unboxed: [`FuturesUnordered`] already stores each
    /// future in its own task allocation, so boxing it first would cost a
    /// second allocation per node execution.
    fn start(&mut self, ix: NodeIx) -> impl Future<Output = TaskDone> + Send + 'static {
        let run = self.run.clone();
        let graph = Arc::clone(self.graph());
        let Some(gn) = graph.node(ix) else {
            let error = NodeError::fatal("graph", "unknown node index");
            return Either::Left(futures::future::ready(TaskDone {
                ix,
                attempt: 1,
                result: Err(error),
                elapsed_ms: 0,
                finished_ms: None,
            }));
        };
        let started = self.now_ms();
        let rt = self.nodes.entry(ix).or_insert_with(|| NodeRt {
            state: State::Ready,
            exec_id: None,
            attempt: 0,
            resume_state: None,
            progress: None,
            counted: Usage::default(),
        });
        rt.state = State::Running;
        let exec_id = Arc::clone(rt.exec_id.get_or_insert_with(|| new_id_arc(started)));
        // Only a node that tracks its turns pays for a handle (one allocation).
        #[cfg(feature = "budgets")]
        let progress = if gn.node.tracks_turns() {
            Some(Arc::clone(rt.progress.get_or_insert_with(|| {
                let progress = Arc::default();
                run.track(&progress);
                progress
            })))
        } else {
            None
        };
        // Boxed: resuming is rare, and the task future carries this for its whole life.
        let resume: Option<Box<Resume>> = rt.resume_state.take().map(|state| {
            Box::new(Resume { state, input: run.resume_input(&self.id, gn.id.as_ref(), self.in_child()) })
        });

        run.emit(Event::NodeStarted {
            frame: self.id.clone(),
            node_id: Arc::clone(&gn.id),
            exec_id: Arc::clone(&exec_id),
            kind: gn.kind.clone(),
            title: gn.title.clone(),
            attempt: 1,
        });

        let subgraph: Option<Arc<dyn Subgraph>> = if gn.role == NodeRole::Container {
            Some(Arc::new(ScopeRunner {
                run: run.clone(),
                #[cfg(feature = "subagents")]
                graph: Arc::clone(self.graph()),
                #[cfg(feature = "subagents")]
                in_child: self.in_child,
                #[cfg(feature = "subagents")]
                child: self.child.clone(),
                owner: ix,
                parent_frame: self.id.clone(),
                depth: self.depth,
            }))
        } else {
            None
        };
        #[cfg(feature = "subagents")]
        let children: Option<Arc<dyn crate::node::ChildRuns>> = if run.linked.is_empty() {
            None
        } else {
            Some(Arc::new(ChildRunner {
                run: run.clone(),
                parent_frame: self.id.clone(),
                node_id: gn.id.clone(),
                depth: self.depth,
            }))
        };
        let node: Arc<dyn Node> = gn.node.clone();
        let node_id: Arc<str> = gn.id.clone();
        // Only the retry policy is needed while running; cloning the whole policy
        // would also copy (and allocate for) `default-value` error outputs.
        let retry = gn.policy.retry.clone();
        let timeout_ms = gn.policy.timeout_ms.or(run.limits.default_node_timeout_ms);
        let pool = self.pool.clone();
        let frame = self.id.clone();
        // A child run uses the services its host gave its own workflow.
        #[cfg(feature = "subagents")]
        let services = self.child.as_ref().map_or_else(|| run.services.clone(), |child| child.services.clone());

        Either::Right(async move {
            let mut attempt: u32 = 1;
            loop {
                if !run.take_step() {
                    let error = NodeError::fatal("limit_exceeded", "step limit exceeded");
                    return TaskDone { ix, attempt, result: Err(error), elapsed_ms: 0, finished_ms: None };
                }
                let deadline_ms = match timeout_ms {
                    Some(ms) if run.clock.supports_timers() => Some(run.now_ms().saturating_add(ms)),
                    _ => None,
                };
                let ctx = NodeContext::new(
                    node_id.clone(),
                    exec_id.clone(),
                    frame.clone(),
                    attempt,
                    pool.clone(),
                    {
                        #[cfg(feature = "subagents")]
                        let services = services.clone();
                        #[cfg(not(feature = "subagents"))]
                        let services = run.services.clone();
                        services
                    },
                    run.clock.clone(),
                    run.sink.clone(),
                )
                .with_resume(resume.as_deref().cloned())
                .with_subgraph(subgraph.clone())
                .with_deadline(deadline_ms)
                .with_secret_values(Arc::clone(&run.secret_values));
                #[cfg(feature = "budgets")]
                let ctx = ctx.with_budget(Arc::clone(&run.budget), progress.clone());
                #[cfg(feature = "subagents")]
                let ctx = ctx.with_children(children.clone());
                // The result and the error stay inside this block, so the task
                // does not reserve room for them while it waits for a retry.
                let delay_ms = {
                    let result = run_attempt(node.as_ref(), ctx, timeout_ms, run.clock.as_ref())
                        .await
                        .map_err(|error| run.scrub_error(error));
                    let error = match result {
                        Err(error) if error.retryable && attempt <= retry.max_retries && !run.control.is_stopped() => {
                            error
                        }
                        result => {
                            let finished = run.now_ms();
                            let elapsed_ms = finished.saturating_sub(started);
                            return TaskDone { ix, attempt, result, elapsed_ms, finished_ms: Some(finished) };
                        }
                    };
                    let delay_ms = retry_delay_ms(&retry, attempt, &error);
                    run.emit(Event::NodeRetry {
                        frame: frame.clone(),
                        node_id: Arc::clone(&node_id),
                        exec_id: Arc::clone(&exec_id),
                        attempt: attempt.saturating_add(1),
                        error: error.to_string(),
                        delay_ms,
                    });
                    delay_ms
                };
                attempt = attempt.saturating_add(1);
                if delay_ms > 0 {
                    run.clock.sleep(Duration::from_millis(delay_ms)).await;
                }
            }
        })
    }

    /// Apply a finished node. Returns `Some` when the frame must end now.
    fn on_done(&mut self, done: TaskDone) -> Option<FrameOutcome> {
        let TaskDone { ix, attempt, result, elapsed_ms, finished_ms } = done;
        if finished_ms.is_some() {
            self.burst_now_ms = finished_ms;
        }
        // The execution ended: its checkpoint is done with.
        let (progress, counted) = self.nodes.get_mut(&ix).map_or((None, Usage::default()), |rt| {
            rt.attempt = attempt;
            // Counted at a pause: taken back from what the node reports when it ends.
            let counted =
                if matches!(result, Ok(NodeOutcome::Paused(_))) { rt.counted } else { std::mem::take(&mut rt.counted) };
            (rt.progress.take(), counted)
        });
        let mut result = result;
        if let Some(progress) = progress
            && progress.settle()
        {
            let spent = progress.charged();
            match &mut result {
                // A node that did not finish reports no usage: what it charged was spent all the
                // same (a run its budget stopped, a model error after some turns), so the run counts it.
                // A paused node is counted now too, so a run that then ends another way (a sibling
                // failed) counts it; when it finishes after the resume, this part is taken back.
                Err(_) | Ok(NodeOutcome::Paused(_)) => {
                    if !spent.is_zero() {
                        self.usage.add(&spent);
                        self.run.add_usage(&spent);
                        if matches!(result, Ok(NodeOutcome::Paused(_)))
                            && let Some(rt) = self.nodes.get_mut(&ix)
                        {
                            rt.counted.add(&spent);
                        }
                    }
                }
                // A node may report only its last attempt: the run counts every attempt it charged.
                // After a resume, it charged `spent` on top of what was counted at the pause.
                Ok(NodeOutcome::Completed(output)) => {
                    let mut floor = spent;
                    floor.add(&counted);
                    output.usage = Some(output.usage.map_or(floor, |usage| usage.at_least(&floor)));
                }
            }
        }
        if self.run.control.is_stopped() {
            // A node that finished as the run stopped was paid for all the same.
            if let Ok(NodeOutcome::Completed(output)) = &result
                && let Some(usage) = &output.usage
                && self.graph().node(ix).is_some_and(|gn| gn.role != NodeRole::Container)
            {
                let usage = usage.saturating_sub(&counted);
                self.usage.add(&usage);
                self.run.add_usage(&usage);
            }
            return Some(FrameOutcome::Stopped);
        }
        let Some(gn) = self.graph().node(ix) else {
            return Some(FrameOutcome::Failed {
                error: NodeError::fatal("graph", "unknown node index"),
                usage: self.usage,
            });
        };
        let role = gn.role;
        match result {
            Ok(NodeOutcome::Completed(output)) => {
                if role == NodeRole::Branch && output.branch.is_none() {
                    if let Some(usage) = &output.usage {
                        let usage = usage.saturating_sub(&counted);
                        self.usage.add(&usage);
                        self.run.add_usage(&usage);
                    }
                    let error = NodeError::fatal("invalid_branch", "branch node completed without selecting a branch");
                    return self.on_error(ix, error, elapsed_ms);
                }
                self.on_success(ix, output, counted, elapsed_ms)
            }
            Ok(NodeOutcome::Paused(request)) => {
                self.on_paused(ix, request);
                None
            }
            Err(error) => self.on_error(ix, error, elapsed_ms),
        }
    }

    fn exec_id(&self, ix: NodeIx) -> Arc<str> {
        self.nodes.get(&ix).and_then(|n| n.exec_id.clone()).unwrap_or_else(|| Arc::from(""))
    }

    fn set_state(&mut self, ix: NodeIx, state: State) {
        if let Some(rt) = self.nodes.get_mut(&ix) {
            rt.state = state;
        }
    }

    /// `counted`: what the run counted of this execution when it paused.
    fn on_success(&mut self, ix: NodeIx, output: NodeOutput, counted: Usage, elapsed_ms: u64) -> Option<FrameOutcome> {
        let graph = Arc::clone(self.graph());
        let gn = graph.node(ix)?;
        let node_id = Arc::clone(&gn.id);
        // Counted first: a node whose outputs cannot be kept was paid for all the same.
        if let Some(usage) = &output.usage {
            let new = usage.saturating_sub(&counted);
            self.usage.add(&new);
            if gn.role != NodeRole::Container {
                self.run.add_usage(&new);
                // A node that tracks its turns charged every response itself.
                // Any other is charged its tokens now. A charge past a limit
                // stops the run (the budget's callback), so the frame ends at
                // its next step: the result of this node is kept.
                #[cfg(feature = "budgets")]
                if !gn.node.tracks_turns() {
                    // An error here was reported already: the budget stopped the run with it.
                    let _limit_reached = self.run.budget.charge(usage, 0).is_err();
                }
            }
        }
        if let Err(error) = self.commit(ix, &output.outputs, &output.writes) {
            return self.on_error(ix, error, elapsed_ms);
        }
        if gn.role == NodeRole::Response {
            merge_response(&mut self.outputs, &output.outputs);
        }
        if &*gn.kind == LOOP_END_KIND {
            self.broke = true;
        }
        self.set_state(ix, State::Succeeded);
        self.run.emit(Event::NodeSucceeded {
            frame: self.id.clone(),
            node_id,
            exec_id: self.exec_id(ix),
            outputs: output.outputs,
            branch: output.branch.clone(),
            usage: output.usage,
            metadata: output.metadata,
            elapsed_ms,
        });
        match (gn.role, output.branch) {
            (NodeRole::Branch, Some(handle)) => self.traverse(ix, |h| h == handle),
            _ => self.traverse(ix, |h| h != FAIL_BRANCH),
        }
        None
    }

    fn on_paused(&mut self, ix: NodeIx, request: PauseRequest) {
        let node_id = self.node_id(ix);
        let role = self.graph().node(ix).map(|n| n.role);
        let reason = PauseReason {
            frame: self.id.clone(),
            node_id: Some(String::from(&*node_id)),
            kind: request.kind,
            data: request.data,
        };
        if let Some(rt) = self.nodes.get_mut(&ix) {
            rt.state = State::Paused;
            rt.resume_state = Some(request.state);
        }
        // Container pauses are caused by inner nodes, which already reported
        // their reasons, and so are the pauses a node passes on from its child runs.
        #[cfg(feature = "subagents")]
        let passed_on = reason.kind == crate::node::CHILD_PAUSE
            && self
                .run
                .parents
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .contains(&format!("{}/{node_id}", self.id));
        #[cfg(not(feature = "subagents"))]
        let passed_on = false;
        if role != Some(NodeRole::Container) && !passed_on {
            self.run.control.add_reason(reason.clone());
        }
        self.reasons.push(reason.clone());
        self.run.emit(Event::NodePaused { frame: self.id.clone(), node_id, exec_id: self.exec_id(ix), reason });
    }

    fn on_error(&mut self, ix: NodeIx, error: NodeError, elapsed_ms: u64) -> Option<FrameOutcome> {
        let graph = Arc::clone(self.graph());
        let gn = graph.node(ix)?;
        let node_id = Arc::clone(&gn.id);
        let mut outputs = Variables::new();
        let handled = !matches!(gn.policy.on_error, ErrorStrategy::Fail);
        if let ErrorStrategy::DefaultValue { values } = &gn.policy.on_error {
            outputs.extend(values.iter().map(|(k, v)| (k, v.clone())));
        }
        if handled {
            outputs.insert("error_message", Value::from(error.message.as_str()));
            outputs.insert("error_type", Value::from(error.kind.as_str()));
        }
        self.set_state(ix, State::Failed);
        self.run.emit(Event::NodeFailed {
            frame: self.id.clone(),
            node_id,
            exec_id: self.exec_id(ix),
            error: error.message.clone(),
            error_type: error.kind.clone(),
            handled,
            outputs: outputs.clone(),
            elapsed_ms,
        });
        match &gn.policy.on_error {
            ErrorStrategy::Fail => Some(FrameOutcome::Failed { error, usage: self.usage }),
            ErrorStrategy::FailBranch => {
                self.run.handled_errors.fetch_add(1, Ordering::AcqRel);
                if let Err(error) = self.commit(ix, &outputs, &[]) {
                    return Some(FrameOutcome::Failed { error, usage: self.usage });
                }
                self.traverse(ix, |h| h == FAIL_BRANCH);
                None
            }
            ErrorStrategy::DefaultValue { values } => {
                self.run.handled_errors.fetch_add(1, Ordering::AcqRel);
                if let Err(error) = self.commit(ix, &outputs, &[]) {
                    return Some(FrameOutcome::Failed { error, usage: self.usage });
                }
                if gn.role == NodeRole::Branch {
                    // Branch nodes follow the handle named by the `branch` default, if any;
                    // otherwise every outgoing path is skipped.
                    let handle = values.get("branch").and_then(Value::as_str).map(str::to_owned);
                    self.traverse(ix, |h| handle.as_deref() == Some(h));
                } else {
                    self.traverse(ix, |h| h != FAIL_BRANCH);
                }
                None
            }
        }
    }

    /// Store outputs under the node namespace and apply its variable writes.
    ///
    /// All-or-nothing: writes to `sys.*` / `env.*` (host-owned) and writes to
    /// out-of-range array indexes are rejected before anything is committed.
    fn commit(&mut self, ix: NodeIx, outputs: &Variables, writes: &[(Selector, Value)]) -> Result<(), NodeError> {
        let node_id = self.node_id(ix);
        // Learn secrets produced by nodes (e.g. secret-typed start inputs) so later
        // error messages that echo them are scrubbed. A variant check per value.
        for value in outputs.values().chain(writes.iter().map(|(_, value)| value)) {
            if matches!(value, Value::Secret(_)) {
                self.run.secret_values.observe(value);
            }
        }
        for (selector, _) in writes {
            let namespace = selector.namespace();
            if namespace == SYS || namespace == ENV {
                return Err(NodeError::fatal(
                    "forbidden_write",
                    format!("nodes may not write `{selector}`: `{namespace}` variables are owned by the host"),
                ));
            }
        }
        if writes.is_empty() {
            // Nothing can fail: skip the undo log (one selector and one value
            // lookup per output), which most nodes would never use.
            self.pool.set_outputs_shared(&node_id, outputs);
            return Ok(());
        }
        // Apply in place (cloning the pool here would force a copy-on-write copy on
        // every commit), remembering prior values so a failing write is undone.
        let mut undo: Vec<(Selector, Option<Value>)> = Vec::with_capacity(outputs.len() + writes.len());
        for key in outputs.keys() {
            let root = Selector::of(&node_id, key);
            let previous = self.pool.get(&root);
            undo.push((root, previous));
        }
        self.pool.set_outputs_shared(&node_id, outputs);
        for (selector, value) in writes {
            let root = selector.root();
            let previous = self.pool.get(&root);
            undo.push((root, previous));
            if let Err(e) = self.pool.set_path(selector, value.clone()) {
                for (root, previous) in undo.into_iter().rev() {
                    match previous {
                        Some(value) => self.pool.set(&root, value),
                        None => {
                            self.pool.remove(&root);
                        }
                    }
                }
                return Err(NodeError::fatal("invalid_write", e.to_string()));
            }
        }
        let external: Vec<(Selector, Value)> =
            writes.iter().filter(|(selector, _)| !self.scope_ids().contains(selector.namespace())).cloned().collect();
        merge_writes(&mut self.writes, external);
        for (selector, value) in writes {
            self.run.emit(Event::VariableUpdated {
                frame: self.id.clone(),
                node_id: node_id.clone(),
                selector: selector.clone(),
                value: value.clone(),
            });
        }
        Ok(())
    }

    /// Resolve every outgoing edge of `ix` (taken when `take(handle)`), then
    /// propagate readiness and skips downstream.
    fn traverse(&mut self, ix: NodeIx, take: impl Fn(&str) -> bool) {
        let graph = Arc::clone(self.graph());
        for &ex in graph.outgoing(ix) {
            let Some(edge) = graph.edge(ex) else { continue };
            let status = if take(&edge.handle) { EdgeStatus::Taken } else { EdgeStatus::Skipped };
            self.resolve_edge(ex, edge.target, status);
        }
        // Last edge first, as the stack-based resolution always did.
        self.resolve_targets(graph.outgoing(ix).iter().rev().filter_map(|&ex| graph.edge(ex)).map(|e| e.target));
    }

    /// Record an edge's status, keeping the target's incoming tally current.
    fn resolve_edge(&mut self, ex: EdgeIx, target: NodeIx, status: EdgeStatus) {
        let previous = self.edges.insert(ex, status);
        if previous.is_some_and(|p| p != EdgeStatus::Unknown) {
            return; // already counted
        }
        if let Some(tally) = self.incoming.get_mut(&target) {
            tally.unresolved = tally.unresolved.saturating_sub(1);
            tally.taken |= status == EdgeStatus::Taken;
        }
    }

    /// Incoming tally of `target`, computed from the edge states on first use.
    fn incoming_tally(&mut self, target: NodeIx) -> Incoming {
        if let Some(tally) = self.incoming.get(&target) {
            return *tally;
        }
        let mut tally = Incoming { unresolved: 0, taken: false };
        for &ex in self.graph().incoming(target) {
            match self.edges.get(&ex).copied().unwrap_or(EdgeStatus::Unknown) {
                EdgeStatus::Unknown => tally.unresolved += 1,
                EdgeStatus::Taken => tally.taken = true,
                EdgeStatus::Skipped => {}
            }
        }
        self.incoming.insert(target, tally);
        tally
    }

    /// Worklist propagation: a node becomes ready once every incoming edge is
    /// resolved and at least one is taken; it is skipped when all are skipped.
    fn resolve_targets(&mut self, targets: impl Iterator<Item = NodeIx>) {
        let graph = Arc::clone(self.graph());
        // Targets reached by skip propagation are resolved before the remaining
        // initial ones (a stack); the stack only allocates when a node is skipped.
        let mut work: Vec<NodeIx> = Vec::new();
        let mut targets = targets;
        while let Some(target) = work.pop().or_else(|| targets.next()) {
            if self.nodes.contains_key(&target) {
                continue; // already decided
            }
            let tally = self.incoming_tally(target);
            if tally.unresolved > 0 {
                continue;
            }
            self.incoming.remove(&target);
            if tally.taken {
                self.mark_ready(target);
                continue;
            }
            // Every incoming edge skipped: skip the node and its outgoing edges.
            let now = self.now_ms();
            self.nodes.insert(
                target,
                NodeRt {
                    state: State::Skipped,
                    exec_id: Some(new_id_arc(now)),
                    attempt: 0,
                    resume_state: None,
                    progress: None,
                    counted: Usage::default(),
                },
            );
            self.run.emit(Event::NodeSkipped { frame: self.id.clone(), node_id: self.node_id(target) });
            for &ex in graph.outgoing(target) {
                if let Some(edge) = graph.edge(ex) {
                    self.resolve_edge(ex, edge.target, EdgeStatus::Skipped);
                    work.push(edge.target);
                }
            }
        }
    }
}

/// Poll the solo task, then the task set, for the next finished node. `None`
/// means nothing is running.
fn poll_tasks<F: Future<Output = TaskDone>>(
    cx: &mut std::task::Context<'_>,
    mut solo: std::pin::Pin<&mut Option<F>>,
    running: &mut FuturesUnordered<F>,
) -> std::task::Poll<Option<TaskDone>> {
    use std::task::Poll;
    if let Some(task) = solo.as_mut().as_pin_mut()
        && let Poll::Ready(done) = task.poll(cx)
    {
        solo.set(None);
        return Poll::Ready(Some(done));
    }
    match running.poll_next_unpin(cx) {
        Poll::Ready(None) if solo.is_some() => Poll::Pending,
        other => other,
    }
}

/// Merge a response node's outputs into the frame outputs: `answer` is
/// concatenated, other keys overwrite.
fn merge_response(frame_outputs: &mut IndexMap<String, Value>, outputs: &Variables) {
    for (key, value) in outputs {
        if key == "answer"
            && let Some(Value::String(existing)) = frame_outputs.get(key)
        {
            let joined = format!("{existing}{}", value.to_text());
            frame_outputs.insert(key.to_owned(), Value::from(joined));
            continue;
        }
        frame_outputs.insert(key.to_owned(), value.clone());
    }
}

/// Delay before retrying after failed attempt number `attempt`: the larger of
/// the policy delay and the error's `retry_after_ms`, capped.
fn retry_delay_ms(retry: &RetryPolicy, attempt: u32, error: &NodeError) -> u64 {
    retry.delay_ms(attempt).max(error.retry_after_ms.unwrap_or(0)).min(MAX_RETRY_DELAY_MS)
}

/// Run one attempt with an optional timeout, converting panics into node errors.
///
/// The node's future and the timer are created before the returned future is
/// first polled, so it holds only those two (not the [`NodeContext`]): this
/// future lives inside every node task, and a smaller task is cheaper to build
/// and move.
fn run_attempt<'a>(
    node: &'a dyn Node,
    ctx: NodeContext,
    timeout_ms: Option<u64>,
    clock: &dyn Clock,
) -> impl Future<Output = NodeResult> + Send + 'a {
    let guarded = AssertUnwindSafe(node.run(ctx)).catch_unwind().map(|outcome| outcome.unwrap_or_else(panic_error));
    let timer = match timeout_ms {
        Some(ms) if clock.supports_timers() => Some((ms, clock.sleep(Duration::from_millis(ms)))),
        _ => None,
    };
    async move {
        let Some((ms, sleep)) = timer else {
            return guarded.await;
        };
        match futures::future::select(guarded, sleep).await {
            Either::Left((result, _)) => result,
            Either::Right(((), _)) => Err(NodeError::timeout(Duration::from_millis(ms))),
        }
    }
}

/// The node error for a panic payload caught while polling a node.
fn panic_error(panic: Box<dyn std::any::Any + Send>) -> NodeResult {
    let message = panic
        .downcast_ref::<&str>()
        .map(|s| (*s).to_owned())
        .or_else(|| panic.downcast_ref::<String>().cloned())
        .unwrap_or_else(|| "node panicked".to_owned());
    Err(NodeError::fatal("panic", message))
}

/// [`Subgraph`] implementation handed to container nodes.
#[derive(Debug)]
pub(crate) struct ScopeRunner {
    run: Arc<RunShared>,
    /// The graph of the frame that owns the container.
    #[cfg(feature = "subagents")]
    graph: Arc<Graph>,
    /// Whether that frame belongs to a child run.
    #[cfg(feature = "subagents")]
    in_child: bool,
    /// What that child run has of its own, handed on to the scope's frame.
    #[cfg(feature = "subagents")]
    child: Option<Arc<ChildLink>>,
    owner: NodeIx,
    parent_frame: FrameId,
    depth: u32,
}

impl ScopeRunner {
    fn graph(&self) -> &Arc<Graph> {
        #[cfg(feature = "subagents")]
        return &self.graph;
        #[cfg(not(feature = "subagents"))]
        return &self.run.graph;
    }
}

impl Subgraph for ScopeRunner {
    fn run(&self, request: SubgraphRequest) -> BoxFuture<'_, SubgraphResult> {
        Box::pin(async move {
            let depth = self.depth.saturating_add(1);
            if depth > self.run.limits.max_depth {
                return SubgraphResult::Failed {
                    error: NodeError::fatal(
                        "limit_exceeded",
                        format!("container nesting depth limit of {} exceeded", self.run.limits.max_depth),
                    ),
                    usage: Usage::default(),
                };
            }
            let owner_id = self.graph().node(self.owner).map(|n| String::from(&*n.id)).unwrap_or_default();
            let segment = format!("{owner_id}[{}]", request.index);
            let id: FrameId = if self.parent_frame.is_empty() {
                Arc::from(segment)
            } else {
                Arc::from(format!("{}/{segment}", self.parent_frame))
            };
            let frame = match request.resume {
                Some(snapshot) => Frame::restore_in(
                    self.run.clone(),
                    #[cfg(feature = "subagents")]
                    Arc::clone(self.graph()),
                    id,
                    Some(self.owner),
                    depth,
                    snapshot,
                    #[cfg(feature = "subagents")]
                    self.in_child,
                ),
                None => Frame::fresh_in(
                    self.run.clone(),
                    #[cfg(feature = "subagents")]
                    Arc::clone(self.graph()),
                    id,
                    Some(self.owner),
                    depth,
                    request.pool,
                ),
            };
            let frame = match frame {
                #[cfg_attr(not(feature = "subagents"), allow(unused_mut))]
                Ok(mut f) => {
                    #[cfg(feature = "subagents")]
                    {
                        f.in_child = self.in_child;
                    }
                    #[cfg(feature = "subagents")]
                    if let Some(child) = &self.child {
                        for (name, value) in &child.env {
                            f.pool.insert(ENV, name, value.clone());
                        }
                        f.child = Some(Arc::clone(child));
                    }
                    f
                }
                Err(error) => return SubgraphResult::Failed { error, usage: Usage::default() },
            };
            match frame.run().await {
                FrameOutcome::Completed { pool, outputs, writes, usage, broke } => {
                    SubgraphResult::Completed { pool, outputs, writes, usage, broke }
                }
                FrameOutcome::Failed { error, usage } => SubgraphResult::Failed { error, usage },
                FrameOutcome::Paused { snapshot, reasons } => SubgraphResult::Paused { snapshot: *snapshot, reasons },
                FrameOutcome::Stopped => {
                    let reason = match self.run.control.stop_reason() {
                        Some(Stop::Abort(r) | Stop::Fail(r)) => r,
                        None => "stopped".to_owned(),
                    };
                    SubgraphResult::Failed { error: NodeError::fatal("aborted", reason), usage: Usage::default() }
                }
            }
        })
    }
}

/// What a child run has of its own: its environment (resolved against its
/// workflow's declarations) and the services its host gave its workflow.
#[cfg(feature = "subagents")]
#[derive(Debug)]
pub(crate) struct ChildLink {
    env: Vec<(String, Value)>,
    services: Arc<crate::node::Services>,
}

/// [`crate::node::ChildRuns`] handed to the nodes of a run that has linked workflows.
#[cfg(feature = "subagents")]
#[derive(Debug)]
pub(crate) struct ChildRunner {
    run: Arc<RunShared>,
    parent_frame: FrameId,
    node_id: Arc<str>,
    depth: u32,
}

/// Removes a child's frame id from the run's running children when the child ends
/// (or its future is dropped by a timeout or an abort).
#[cfg(feature = "subagents")]
struct Running<'a> {
    run: &'a RunShared,
    id: String,
}

#[cfg(feature = "subagents")]
impl Drop for Running<'_> {
    fn drop(&mut self) {
        self.run.running_children.lock().unwrap_or_else(std::sync::PoisonError::into_inner).remove(&self.id);
    }
}

#[cfg(feature = "subagents")]
impl crate::node::sealed::Sealed for ChildRunner {}

#[cfg(feature = "subagents")]
impl crate::node::ChildRuns for ChildRunner {
    fn workflows(&self) -> Vec<String> {
        self.run.linked.keys().cloned().collect()
    }

    fn inputs_schema(&self, workflow: &str) -> Result<serde_json::Value, NodeError> {
        let link = self.run.linked.get(workflow).ok_or_else(|| {
            NodeError::fatal("unknown_workflow", format!("workflow `{workflow}` is not linked to this run"))
        })?;
        start_schema(&link.graph).map_err(|why| NodeError::fatal("unusable_workflow", why))
    }

    fn max_concurrency(&self) -> usize {
        self.run.limits.max_concurrency
    }

    fn run_stopped(&self) -> bool {
        self.run.control.stop_reason().is_some()
    }

    fn run(&self, request: crate::node::ChildRequest) -> BoxFuture<'_, SubgraphResult> {
        Box::pin(async move {
            let failed = |error: NodeError| SubgraphResult::Failed { error, usage: Usage::default() };
            let Some(link) = self.run.linked.get(&request.workflow).cloned() else {
                return failed(NodeError::fatal(
                    "unknown_workflow",
                    format!("workflow `{}` is not linked to this run", request.workflow),
                ));
            };
            // Containers and child runs count against one depth limit, so a
            // workflow that starts itself ends there instead of recursing for ever.
            let depth = self.depth.saturating_add(1);
            if depth > self.run.limits.max_depth {
                return failed(NodeError::fatal(
                    "limit_exceeded",
                    format!("nesting depth limit of {} exceeded by child runs", self.run.limits.max_depth),
                ));
            }
            let segment = format!("{}.{}[{}]", self.node_id, request.workflow, request.index);
            let id: FrameId = if self.parent_frame.is_empty() {
                Arc::from(segment)
            } else {
                Arc::from(format!("{}/{segment}", self.parent_frame))
            };
            // One frame per id, and a bound on the child runs of the whole run,
            // every level of nesting included. A parent keeps its place while its
            // children run, so the bound leaves room for a full chain of nesting
            // at the run's concurrency: `max_concurrency` per level of depth.
            let _running = {
                let mut running = self.run.running_children.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
                if running.contains(id.as_ref()) {
                    return failed(NodeError::fatal("duplicate_child", format!("child run `{id}` is running already")));
                }
                let depth_room = usize::try_from(self.run.limits.max_depth.max(1)).unwrap_or(usize::MAX);
                let bound = self.run.limits.max_concurrency.max(1).saturating_mul(depth_room);
                if running.len() >= bound {
                    let why = format!(
                        "at most {bound} child runs at once in one run ({} per level of `max_concurrency` x {} levels of `max_depth`)",
                        self.run.limits.max_concurrency, self.run.limits.max_depth
                    );
                    return failed(NodeError::new("limit_exceeded", why));
                }
                running.insert(id.to_string());
                Running { run: &self.run, id: id.to_string() }
            };
            let graph = link.graph().clone();
            // Its own environment, not the parent's, resolved like a run's: defaults
            // apply and `secret` declarations make secrets, which every event and
            // the parent's view of the answer mask.
            // Hosts check this when they link (`LinkedWorkflow::check`).
            #[cfg(feature = "bundle-env")]
            if let Some(why) = link.refused() {
                return failed(NodeError::fatal("env_refused", why));
            }
            let env = match link.resolved_env() {
                Ok(env) => env,
                Err(why) => return failed(NodeError::fatal("child_misconfigured", why)),
            };
            for (_, value) in &env {
                self.run.secret_values.observe(value);
            }
            let child = Arc::new(ChildLink {
                env,
                services: link.services().cloned().unwrap_or_else(|| self.run.services.clone()),
            });
            let frame = match request.resume {
                Some(snapshot) => {
                    Frame::restore_in(self.run.clone(), graph, id, None, depth, snapshot, true).map(|mut frame| {
                        // Environment values are never in a snapshot: the child's own come back here.
                        for (name, value) in &child.env {
                            frame.pool.insert(ENV, name, value.clone());
                        }
                        frame
                    })
                }
                None => match child_pool(&graph, &child.env, request.sys, request.inputs) {
                    Ok(pool) => Frame::fresh_in(self.run.clone(), graph, id, None, depth, pool),
                    Err(error) => return failed(error),
                },
            };
            let frame = match frame {
                Ok(mut frame) => {
                    // A child is not the run's root: host updates of variables
                    // are the root's, and resume inputs need qualified keys.
                    frame.is_root = false;
                    frame.in_child = true;
                    frame.child = Some(child);
                    frame
                }
                Err(error) => return failed(error),
            };
            match frame.run().await {
                FrameOutcome::Completed { pool, outputs, writes, usage, broke } => {
                    SubgraphResult::Completed { pool, outputs, writes, usage, broke }
                }
                FrameOutcome::Failed { error, usage } => SubgraphResult::Failed { error, usage },
                FrameOutcome::Paused { snapshot, reasons } => {
                    // This node holds a waiting child: its pause of kind CHILD_PAUSE
                    // passes the child's reasons on, and adds none of its own.
                    self.run
                        .parents
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner)
                        .insert(format!("{}/{}", self.parent_frame, self.node_id));
                    SubgraphResult::Paused { snapshot: *snapshot, reasons }
                }
                FrameOutcome::Stopped => {
                    let reason = match self.run.control.stop_reason() {
                        Some(Stop::Abort(r) | Stop::Fail(r)) => r,
                        None => "stopped".to_owned(),
                    };
                    failed(NodeError::fatal("aborted", reason))
                }
            }
        })
    }
}

/// The pool a fresh child starts with: its environment, the `sys` values it
/// is given and its start inputs, nothing else of the parent.
///
/// @throws NodeError - `invalid_input` for an argument the workflow does not
///   offer (undeclared, or a secret), or one shaped like a file reference.
#[cfg(feature = "subagents")]
fn child_pool(
    graph: &Graph,
    env: &[(String, Value)],
    sys: IndexMap<String, Value>,
    inputs: IndexMap<String, Value>,
) -> Result<VariablePool, NodeError> {
    let mut pool = VariablePool::new();
    for (name, value) in env {
        pool.insert(ENV, name, value.clone());
    }
    for (name, value) in &sys {
        pool.insert(SYS, name, value.clone());
    }
    let offered = start_schema(graph).map_err(|why| NodeError::fatal("unusable_workflow", why))?;
    let properties = offered.get("properties").and_then(serde_json::Value::as_object);
    for (name, value) in &inputs {
        if !properties.is_some_and(|p| p.contains_key(name)) {
            return Err(NodeError::invalid_input(format!("the workflow takes no argument `{name}`")));
        }
        // A model passes data, never a file: an object shaped like a file
        // reference would become one when a snapshot of the child is read.
        if holds_file_marker(value) {
            return Err(NodeError::invalid_input(format!("argument `{name}` holds a `$file` object")));
        }
    }
    if let Some(entry) = graph.root_scope().and_then(|scope| graph.node(scope.entry)) {
        for (name, value) in inputs {
            pool.insert(&entry.id, &name, value);
        }
    }
    Ok(pool)
}

#[cfg(feature = "subagents")]
fn holds_file_marker(value: &Value) -> bool {
    match value {
        Value::Object(map) => map.contains_key("$file") || map.values().any(holds_file_marker),
        Value::Array(items) => items.iter().any(holds_file_marker),
        _ => false,
    }
}

/// The JSON schema of the start variables of `graph`: what a model passes to
/// start it, with the types the start node accepts.
///
/// @throws String - when a required variable is one a model cannot supply
///   (a secret or a file), so the workflow cannot be called as a tool.
#[cfg(feature = "subagents")]
pub(crate) fn start_schema(graph: &Graph) -> Result<serde_json::Value, String> {
    use crate::value::ValueType;
    use serde_json::{Value as Json, json};
    let start = graph
        .root_scope()
        .and_then(|scope| graph.node(scope.entry))
        .and_then(|entry| graph.workflow().graph.nodes.iter().find(|n| n.id == *entry.id));
    let config: crate::nodes::start::StartConfig = match start {
        Some(doc) => serde_json::from_value(serde_json::Value::Object(doc.config.clone()))
            .map_err(|e| format!("start variables: {e}"))?,
        None => crate::nodes::start::StartConfig::default(),
    };
    fn json_type(ty: ValueType) -> Option<Json> {
        let name = match ty {
            ValueType::String => "string",
            ValueType::Number | ValueType::Float => "number",
            ValueType::Integer => "integer",
            ValueType::Boolean => "boolean",
            ValueType::Object => "object",
            ValueType::Null => "null",
            _ => return None,
        };
        Some(json!({"type": name}))
    }
    let mut properties = serde_json::Map::new();
    let mut required = Vec::new();
    for variable in &config.variables {
        let ty = variable.value_type()?;
        let mut property = match ty {
            ValueType::Secret | ValueType::File | ValueType::ArrayFile => {
                // With a default the start node does not need it from the model.
                if variable.required && variable.default.is_none() {
                    return Err(format!(
                        "required start variable `{}` is a {} a model cannot pass",
                        variable.variable,
                        if ty == ValueType::Secret { "secret" } else { "file" }
                    ));
                }
                continue;
            }
            ValueType::Any => json!({}),
            array if array.element().is_some() => {
                let items = array.element().and_then(json_type).unwrap_or_else(|| json!({}));
                json!({"type": "array", "items": items})
            }
            scalar => json_type(scalar).unwrap_or_else(|| json!({})),
        };
        if let Some(fields) = property.as_object_mut() {
            if let Some(label) = &variable.label {
                fields.insert("description".to_owned(), Json::String(label.clone()));
            }
            if variable.kind == "select" && !variable.options.is_empty() {
                fields.insert("enum".to_owned(), json!(variable.options));
            }
            if let Some(max) = variable.max_length {
                fields.insert("maxLength".to_owned(), json!(max));
            }
        }
        properties.insert(variable.variable.clone(), property);
        if variable.required && variable.default.is_none() {
            required.push(Json::String(variable.variable.clone()));
        }
    }
    Ok(json!({"type": "object", "properties": properties, "required": required, "additionalProperties": false}))
}