graphrt-llm 0.6.0

Model providers (OpenAI-compatible, Anthropic, Gemini, AWS Bedrock, TypeSafe Jev) and LLM, classifier, extractor and agent nodes for the graphrt workflow runtime.
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
//! `agent` node: a tool-calling loop.
//!
//! Each iteration calls the model with the available tools, runs the tool
//! calls it requests (concurrently when `parallel_tool_calls` is on) and feeds
//! the results back, until the model answers without calling tools. On the
//! last allowed iteration the tools stay declared (providers require that when
//! the history contains tool calls) but the model may not call them
//! (`tool_choice: none`), so the run always ends with an answer.
//!
//! Streaming: every turn streams its answer text on field `text` token by
//! token (reasoning on `reasoning`), including turns that may still call
//! tools, so the final answer reaches the client as it is generated.
//! Tool-call fragments are never streamed as text. Once a turn produces its
//! first tool-call fragment, the rest of that turn's text is no longer
//! streamed. Text a turn streamed before its tool calls (a *preamble* such as
//! "Let me look that up.") cannot be taken back, so it stays part of the
//! answer: the `text` output is every streamed preamble followed by the final
//! answer, separated by a blank line (streamed as its own `"\n\n"` chunk),
//! and the streamed text always equals the `text` output. The full text of a
//! turn that called tools is also reported as an `agent_thought` progress
//! event (`streamed: true` when part of it was streamed).
//!
//! `stream_tool_turns: false` restores buffered tool turns: only the final
//! answer is streamed (as one chunk when the turn could still have called
//! tools) and preambles are reported only as `agent_thought`, never in `text`.
//!
//! Tool-call arguments that are not a JSON object are not executed; the model
//! receives an error result explaining the problem so it can retry the call.
//!
//! Tools come from the [`ToolRegistry`] service and are referenced as
//! `provider/name` (or `provider/*` for every tool of a provider). Tools listed
//! in `require_approval` pause the run before they execute; other calls of the
//! same turn run immediately. The host resumes with `{"approved": true|false}`
//! for every pending call, or per call with `{"approvals": {"<call id>": true}}`,
//! optionally with a `reason`. Calls without a decision (including when the
//! resume input is missing or not understood) keep the run paused.
//!
//! Tool results may carry images (see `docs/tool-images.md`): they are checked
//! against the limits of [`check_images`] (invalid ones are dropped with a
//! note to the model) and follow the result text in the `tool` message.
//! `keep_tool_images` (default 3, `null` for all) bounds how many image-bearing
//! results keep their images.
//!
//! `tool_context` sends per-run values (e.g. the end user's JWT) to the tools of
//! a provider beside their arguments, as a [`ToolCallContext`]. The model never
//! sees them, they are never recorded, and they are only rendered (with secrets
//! exposed) for providers that [accept secrets](ToolRuntime::accepts_secrets);
//! any other provider fails the node. The values are registered as run secrets
//! and scrubbed from tool results and logged arguments.
//!
//! Progress events: `agent_step`, `agent_thought`, `tool_call`, `tool_result`,
//! `tool_denied`. Outputs: `text`, `tool_calls`
//! (`[{name, provider, tool, arguments, result, is_error, images}]`), `iterations`,
//! `stopped_reason` (`completed`, `max_iterations` or `length`), `usage`.

use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use futures::future::BoxFuture;
use graphrt_core::dsl::NodeDoc;
use graphrt_core::event::Usage;
use graphrt_core::node::{ConfigError, NodeOutcome, NodeResult, PauseRequest, parse_config};
use graphrt_core::redact::redact_secrets;
use graphrt_core::template::Template;
use graphrt_core::tool::{ToolCallContext, ToolImage, ToolOutput, ToolRegistry, ToolRuntime, ToolSpec, check_images};
use graphrt_core::value::Value;
use graphrt_core::{Node, NodeContext, NodeError, NodeOutput, Selector};
use serde::{Deserialize, Serialize};
use serde_json::{Value as Json, json};

use super::common::{
    FinishCheck, MemoryConfig, ModelConfig, StreamField, StreamMode, VisionConfig, check_finish, invoke, model_for,
    usage_value,
};
use crate::types::{
    ChatDelta, ChatModel, ChatRequest, ChatResponse, ContentPart, FinishReason, Message, Role, ToolCall, ToolChoice,
    collect,
};

/// Streamed between a preamble and the text that follows it in a later turn.
const PREAMBLE_SEPARATOR: &str = "\n\n";

/// Upper bound for `max_iterations`.
pub const MAX_ITERATIONS_LIMIT: u32 = 100;

/// Reference to a tool: `"provider/name"` or `{provider, name}`.
#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
pub enum ToolRef {
    Path(String),
    Pair { provider: String, name: String },
}

impl ToolRef {
    fn split(&self) -> Option<(String, String)> {
        match self {
            ToolRef::Path(p) => {
                let (provider, name) = p.split_once('/')?;
                if provider.is_empty() || name.is_empty() {
                    return None;
                }
                Some((provider.to_owned(), name.to_owned()))
            }
            ToolRef::Pair { provider, name } if !provider.is_empty() && !name.is_empty() => {
                Some((provider.clone(), name.clone()))
            }
            ToolRef::Pair { .. } => None,
        }
    }
}

fn default_query() -> Template {
    Template::parse("{{#sys.query#}}")
}

fn default_max_iterations() -> u32 {
    10
}

fn default_true() -> bool {
    true
}

fn default_max_tool_output() -> usize {
    32_000
}

fn default_keep_tool_images() -> Option<usize> {
    Some(3)
}

/// Configuration of the `agent` node.
#[derive(Clone, Debug, Deserialize)]
pub struct AgentConfig {
    pub model: ModelConfig,
    /// System prompt.
    #[serde(default)]
    pub instruction: Option<Template>,
    /// User message (default `{{#sys.query#}}`).
    #[serde(default = "default_query")]
    pub query: Template,
    #[serde(default)]
    pub tools: Vec<ToolRef>,
    /// Maximum model calls. Default 10.
    #[serde(default = "default_max_iterations")]
    pub max_iterations: u32,
    /// Tool calls beyond this number in one turn are rejected with an error result.
    #[serde(default)]
    pub max_tool_calls_per_turn: Option<usize>,
    #[serde(default = "default_true")]
    pub parallel_tool_calls: bool,
    #[serde(default)]
    pub memory: Option<MemoryConfig>,
    #[serde(default)]
    pub vision: VisionConfig,
    /// Tools that need host approval: `provider/name`, a tool name, or `*` for all.
    #[serde(default)]
    pub require_approval: Vec<String>,
    /// Tool results longer than this (in characters) are truncated. Default 32 000.
    #[serde(default = "default_max_tool_output")]
    pub max_tool_output_chars: usize,
    /// Keep the images of only the last N image-bearing tool results in the
    /// conversation (older ones become a text placeholder). Default 3, since
    /// images are resent on every turn; `null` keeps every image.
    #[serde(default = "default_keep_tool_images")]
    pub keep_tool_images: Option<usize>,
    /// Stream the text of turns that may still call tools as it is generated
    /// (see the module docs). Default `true`; `false` buffers those turns.
    #[serde(default = "default_true")]
    pub stream_tool_turns: bool,
    /// Per provider, named values sent with every call to that provider's tools
    /// (`{provider: {name: template}}`), outside the arguments. See the module docs.
    /// Kept as raw JSON (parsed by [`AgentNode::from_doc`]) so no extra serde
    /// code is generated for a rarely used field.
    #[serde(default)]
    pub tool_context: serde_json::Map<String, Json>,
}

/// A tool bound for one run.
#[derive(Clone, Debug)]
struct BoundTool {
    /// Name exposed to the model (unique, provider-safe characters).
    exposed: String,
    provider: String,
    name: String,
    runtime: Arc<dyn ToolRuntime>,
    spec: ToolSpec,
    /// Rendered `tool_context` of the provider, shared by its tools.
    context: Option<Arc<ToolCallContext>>,
}

impl BoundTool {
    fn context(&self) -> &ToolCallContext {
        self.context.as_deref().unwrap_or(ToolCallContext::empty())
    }
}

/// One executed (or rejected) tool call.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct ToolLogEntry {
    id: String,
    name: String,
    provider: String,
    tool: String,
    /// Parsed arguments, or the raw text when the model sent malformed JSON.
    arguments: Json,
    result: String,
    is_error: bool,
    /// Images sent to the model with the result.
    #[serde(default)]
    images: usize,
}

/// Persistable loop state, used to pause for tool approval.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
struct AgentState {
    messages: Vec<Message>,
    iteration: u32,
    log: Vec<ToolLogEntry>,
    usage: Usage,
    /// Calls awaiting approval.
    pending: Vec<ToolCall>,
    /// Answer text streamed so far (preambles of tool-calling turns).
    #[serde(default, skip_serializing_if = "String::is_empty")]
    answer: String,
}

/// Why the loop ended.
#[derive(Clone, Debug, PartialEq, Eq)]
enum StopReason {
    /// The model answered without calling tools.
    Completed,
    /// The iteration limit forced a final answer without tools.
    MaxIterations,
    /// The final answer was cut off by the output token limit.
    Length,
    /// The provider ended the turn with a reason graphrt does not recognize.
    Unrecognized(String),
}

impl StopReason {
    fn as_str(&self) -> String {
        match self {
            StopReason::Completed => "completed".into(),
            StopReason::MaxIterations => "max_iterations".into(),
            StopReason::Length => "length".into(),
            StopReason::Unrecognized(reason) => format!("finish:{reason}"),
        }
    }
}

/// The `agent` node.
#[derive(Debug)]
pub struct AgentNode {
    config: AgentConfig,
    refs: Vec<(String, String)>,
    /// Parsed `tool_context`.
    tool_context: Vec<ProviderContext>,
}

/// One provider's `tool_context`: provider name and `(name, template)` pairs.
type ProviderContext = (String, Vec<(String, Template)>);

/// Parse `tool_context`: every provider must appear in `tools`, every value
/// must be a non-empty name mapped to a template string.
fn parse_tool_context(
    doc: &NodeDoc,
    raw: &serde_json::Map<String, Json>,
    refs: &[(String, String)],
) -> Result<Vec<ProviderContext>, ConfigError> {
    let mut parsed = Vec::with_capacity(raw.len());
    for (provider, values) in raw {
        if !refs.iter().any(|(p, _)| p == provider) {
            return Err(ConfigError::new(
                doc,
                format!("tool_context provider `{provider}` is not used by any entry of `tools`"),
            ));
        }
        let Json::Object(values) = values else {
            return Err(ConfigError::new(doc, format!("tool_context `{provider}` must map names to templates")));
        };
        let mut templates = Vec::with_capacity(values.len());
        for (name, value) in values {
            let (false, Json::String(text)) = (name.is_empty(), value) else {
                return Err(ConfigError::new(
                    doc,
                    format!("tool_context `{provider}.{name}` must be a non-empty name with a template string"),
                ));
            };
            templates.push((name.clone(), Template::parse(text)));
        }
        parsed.push((provider.clone(), templates));
    }
    Ok(parsed)
}

impl AgentNode {
    pub fn from_doc(doc: &NodeDoc) -> Result<Self, ConfigError> {
        let config: AgentConfig = parse_config(doc)?;
        if config.max_iterations == 0 || config.max_iterations > MAX_ITERATIONS_LIMIT {
            return Err(ConfigError::new(doc, format!("max_iterations must be between 1 and {MAX_ITERATIONS_LIMIT}")));
        }
        let refs = config
            .tools
            .iter()
            .map(|r| {
                r.split()
                    .ok_or_else(|| ConfigError::new(doc, format!("invalid tool reference {r:?}; use `provider/name`")))
            })
            .collect::<Result<Vec<_>, _>>()?;
        let tool_context = parse_tool_context(doc, &config.tool_context, &refs)?;
        Ok(Self { config, refs, tool_context })
    }

    /// Resolve tool references against the registry, assigning unique exposed names.
    async fn bind_tools(&self, ctx: &NodeContext) -> Result<Vec<BoundTool>, NodeError> {
        if self.refs.is_empty() {
            return Ok(Vec::new());
        }
        let registry = ctx.require_service::<ToolRegistry>("tool registry (graphrt_core::tool::ToolRegistry)")?;
        let mut listings: HashMap<String, Vec<ToolSpec>> = HashMap::new();
        let mut bound: Vec<BoundTool> = Vec::new();
        let mut used: HashSet<String> = HashSet::new();
        for (provider, name) in &self.refs {
            let runtime = registry
                .get(provider)
                .cloned()
                .ok_or_else(|| NodeError::fatal("tool_not_found", format!("unknown tool provider `{provider}`")))?;
            if !listings.contains_key(provider) {
                let specs = runtime.list().await.map_err(|e| NodeError::new("tool", e.to_string()))?;
                listings.insert(provider.clone(), specs);
            }
            let specs = listings.get(provider).map(Vec::as_slice).unwrap_or_default();
            for spec in select_specs(specs, provider, name)? {
                if bound.iter().any(|b| &b.provider == provider && b.name == spec.name) {
                    continue;
                }
                let exposed = unique_name(&spec.name, provider, &mut used);
                bound.push(BoundTool {
                    exposed: exposed.clone(),
                    provider: provider.clone(),
                    name: spec.name.clone(),
                    runtime: Arc::clone(&runtime),
                    spec: ToolSpec {
                        name: exposed,
                        description: spec.description.clone(),
                        parameters: spec.parameters.clone(),
                    },
                    context: None,
                });
            }
        }
        self.attach_tool_context(ctx, &mut bound)?;
        Ok(bound)
    }

    /// Render `tool_context` (secrets exposed) for each configured provider and
    /// share it with that provider's bound tools. Fails when a provider does not
    /// accept secrets: silently dropping the values would hide a misconfiguration.
    fn attach_tool_context(&self, ctx: &NodeContext, bound: &mut [BoundTool]) -> Result<(), NodeError> {
        for (provider, templates) in &self.tool_context {
            let Some(first) = bound.iter().find(|b| &b.provider == provider) else { continue };
            if !first.runtime.accepts_secrets() {
                return Err(NodeError::fatal(
                    "tool_context_denied",
                    format!(
                        "tool provider `{provider}` does not accept a tool context (a plugin needs \
                         `permissions.secrets` and must be trusted, e.g. `--trust-plugin {provider}`)"
                    ),
                ));
            }
            let mut context = ToolCallContext::default();
            for (name, template) in templates {
                let value = template.render_exposed(&ctx.pool);
                // A missing variable renders empty: leave the name out so the tool can refuse.
                if value.is_empty() {
                    continue;
                }
                ctx.observe_secret(&value);
                context.secrets.insert(name.clone(), value);
            }
            let context = Arc::new(context);
            for tool in bound.iter_mut().filter(|b| &b.provider == provider) {
                tool.context = Some(Arc::clone(&context));
            }
        }
        Ok(())
    }

    fn needs_approval(&self, tool: &BoundTool) -> bool {
        self.config.require_approval.iter().any(|r| {
            r == "*" || *r == tool.exposed || *r == tool.name || *r == format!("{}/{}", tool.provider, tool.name)
        })
    }

    fn call_needs_approval(&self, tools: &[BoundTool], call: &ToolCall) -> bool {
        bound_tool(tools, call).is_some_and(|t| self.needs_approval(t))
    }

    fn initial_state(&self, ctx: &NodeContext) -> Result<AgentState, NodeError> {
        let cfg = &self.config;
        let pool = &ctx.pool;
        let mut messages = Vec::new();
        if let Some(instruction) = &cfg.instruction {
            let text = instruction.render(pool);
            if !text.trim().is_empty() {
                messages.push(Message::system(text));
            }
        }
        if let Some(memory) = &cfg.memory {
            messages.extend(memory.history(pool)?);
        }
        let query = cfg.query.render(pool);
        let images = cfg.vision.image_parts(pool)?;
        if query.trim().is_empty() && images.is_empty() {
            if messages.iter().all(|m| m.role == Role::System) {
                return Err(NodeError::invalid_input("agent query is empty"));
            }
        } else {
            let mut user = Message::user(query);
            user.content.extend(images);
            messages.push(user);
        }
        Ok(AgentState { messages, ..AgentState::default() })
    }

    /// Apply the host's decisions to the pending calls. Returns the calls that
    /// still have no decision.
    async fn apply_decisions(
        &self,
        ctx: &NodeContext,
        state: &mut AgentState,
        tools: &[BoundTool],
        input: Option<&Value>,
    ) -> Vec<ToolCall> {
        let decisions = Approvals::from_input(input);
        let mut approved = Vec::new();
        let mut undecided = Vec::new();
        for call in std::mem::take(&mut state.pending) {
            match decisions.decision(&call.id) {
                Some(true) => approved.push(call),
                Some(false) => self.deny(ctx, state, tools, &call, decisions.reason.as_deref()),
                None => undecided.push(call),
            }
        }
        self.run_calls(ctx, state, tools, approved).await;
        undecided
    }

    async fn execute(&self, ctx: NodeContext) -> NodeResult {
        let tools = self.bind_tools(&ctx).await?;
        let model = model_for(&ctx, &self.config.model)?;
        let mut state = match &ctx.resume {
            Some(resume) => {
                let mut state: AgentState = serde_json::from_value(resume.state.clone())
                    .map_err(|e| NodeError::fatal("invalid_resume_state", e.to_string()))?;
                let undecided = self.apply_decisions(&ctx, &mut state, &tools, resume.input.as_ref()).await;
                if !undecided.is_empty() {
                    state.pending = undecided;
                    return self.pause(&state, &tools);
                }
                state
            }
            None => self.initial_state(&ctx)?,
        };
        let (final_text, stop) = loop {
            state.iteration += 1;
            let offer =
                TurnTools { offered: !tools.is_empty(), final_turn: state.iteration >= self.config.max_iterations };
            let turn = self.model_turn(&ctx, model.as_ref(), &mut state, &tools, offer).await?;
            if !offer.callable() || turn.response.tool_calls.is_empty() {
                break self.answer_turn(&ctx, &mut state, turn, offer);
            }
            self.tool_turn(&ctx, &mut state, &tools, turn).await;
            if !state.pending.is_empty() {
                return self.pause(&state, &tools);
            }
        };
        Ok(agent_outputs(state, final_text, &stop))
    }

    /// Ask the model for the next turn (streaming per `stream_tool_turns`) and
    /// add its usage to the state.
    async fn model_turn(
        &self,
        ctx: &NodeContext,
        model: &dyn ChatModel,
        state: &mut AgentState,
        tools: &[BoundTool],
        offer: TurnTools,
    ) -> Result<Turn, NodeError> {
        let cfg = &self.config;
        if let Some(keep) = cfg.keep_tool_images {
            prune_tool_images(&mut state.messages, keep);
        }
        let mut request = cfg.model.request(state.messages.clone(), &ctx.pool)?;
        if offer.offered {
            // Tools stay declared on the final turn: the history may contain tool calls,
            // which providers only accept alongside tool definitions.
            request.tools = tools.iter().map(|t| t.spec.clone()).collect();
            request.tool_choice = Some(if offer.callable() { ToolChoice::Auto } else { ToolChoice::None });
        }
        ctx.progress(
            "agent_step",
            Value::from_json(json!({"iteration": state.iteration, "tools_enabled": offer.callable()})),
        );
        let plan = TurnPlan {
            live: cfg.stream_tool_turns || !offer.callable(),
            stop_at_tool_call: offer.callable(),
            separate: !state.answer.is_empty(),
        };
        let (response, streamed) = stream_turn(ctx, model, request, plan).await?;
        state.usage.add(&response.usage);
        // A content-filtered or malformed turn is an error, never an empty "completed" answer.
        let finish = check_finish(&response)?;
        Ok(Turn { response, streamed, finish, live: plan.live })
    }

    /// A turn without tool calls ends the loop: its text completes the answer.
    fn answer_turn(
        &self,
        ctx: &NodeContext,
        state: &mut AgentState,
        mut turn: Turn,
        offer: TurnTools,
    ) -> (String, StopReason) {
        let response = turn.response;
        if !turn.live && !response.text.is_empty() {
            ctx.chunk_shared(StreamField::Text.shared(), response.text.clone());
            turn.streamed = response.text.len();
        }
        let stop = if response.finish_reason == Some(FinishReason::Length) {
            StopReason::Length
        } else if let FinishCheck::Unknown(reason) = turn.finish {
            StopReason::Unrecognized(reason)
        } else if offer.offered && offer.final_turn {
            StopReason::MaxIterations
        } else {
            StopReason::Completed
        };
        state.messages.push(Message::assistant(response.text.clone()));
        (final_answer(std::mem::take(&mut state.answer), response.text, turn.streamed), stop)
    }

    /// A turn with tool calls: keep its preamble, then reject, run or hold
    /// (in `state.pending`, for approval) each call.
    async fn tool_turn(&self, ctx: &NodeContext, state: &mut AgentState, tools: &[BoundTool], turn: Turn) {
        let response = turn.response;
        if !response.text.trim().is_empty() {
            ctx.progress(
                "agent_thought",
                Value::from_json(
                    json!({"iteration": state.iteration, "text": response.text, "streamed": turn.streamed > 0}),
                ),
            );
        }
        commit_preamble(&mut state.answer, &response.text, turn.streamed);
        state.messages.push(replayable_message(&response));
        let runnable = self.runnable_calls(state, tools, response.tool_calls);
        let (gated, free): (Vec<ToolCall>, Vec<ToolCall>) =
            runnable.into_iter().partition(|c| self.call_needs_approval(tools, c));
        self.run_calls(ctx, state, tools, free).await;
        state.pending = gated;
    }

    /// The calls that can run. Calls past `max_tool_calls_per_turn` and calls
    /// with malformed arguments get an error result for the model instead.
    fn runnable_calls(&self, state: &mut AgentState, tools: &[BoundTool], mut calls: Vec<ToolCall>) -> Vec<ToolCall> {
        if let Some(limit) = self.config.max_tool_calls_per_turn
            && calls.len() > limit
        {
            for call in calls.split_off(limit) {
                self.record(state, tools, &call, ToolOutput::error("rejected: too many tool calls in one turn"));
            }
        }
        let mut runnable = Vec::with_capacity(calls.len());
        for call in calls {
            match call.arguments_json() {
                Ok(_) => runnable.push(call),
                Err(e) => {
                    let message = format!("error: {e}. Call the tool again with a valid JSON object.");
                    self.record(state, tools, &call, ToolOutput::error(message));
                }
            }
        }
        runnable
    }

    fn pause(&self, state: &AgentState, tools: &[BoundTool]) -> NodeResult {
        let calls: Vec<Json> = state
            .pending
            .iter()
            .map(|c| json!({"id": c.id, "name": c.name, "arguments": log_arguments(c, tools)}))
            .collect();
        let saved = serde_json::to_value(state)
            .map_err(|e| NodeError::fatal("agent_state", format!("cannot save agent state: {e}")))?;
        Ok(NodeOutcome::Paused(PauseRequest {
            kind: "tool_approval".to_owned(),
            data: Value::from_json(json!({"calls": calls})),
            state: saved,
        }))
    }

    fn deny(
        &self,
        ctx: &NodeContext,
        state: &mut AgentState,
        tools: &[BoundTool],
        call: &ToolCall,
        reason: Option<&str>,
    ) {
        let text = match reason {
            Some(r) if !r.is_empty() => format!("The user did not approve this tool call: {r}"),
            _ => "The user did not approve this tool call.".to_owned(),
        };
        ctx.progress("tool_denied", Value::from_json(json!({"id": call.id, "name": call.name})));
        self.record(state, tools, call, ToolOutput::error(text));
    }

    /// A tool result as the model, the `tool_calls` output and the captured
    /// `tool_result` progress all see it: the tool's context masked, cut to
    /// `max_tool_output_chars`, and a note in place of images that fail
    /// [`check_images`].
    ///
    /// A tool may echo its context (e.g. an upstream error repeating the JWT):
    /// the model, the outputs and the progress events must never see it. Masking
    /// comes first so a cut through the secret cannot leave its prefix behind.
    /// Every consumer takes the text from here, so none can skip the masking.
    fn visible_result(&self, tools: &[BoundTool], call: &ToolCall, output: ToolOutput) -> VisibleResult {
        let bound = bound_tool(tools, call);
        let scrubbed = bound.map_or(Cow::Borrowed(output.text.as_str()), |b| scrub_context(&output.text, b.context()));
        let mut text = truncate(&scrubbed, self.config.max_tool_output_chars);
        let images = match check_images(&output.images) {
            Ok(()) => output.images,
            Err(e) => {
                let note = format!("[images dropped: {e}]");
                text = if text.is_empty() { note } else { format!("{text}\n{note}") };
                Vec::new()
            }
        };
        VisibleResult { text, images, is_error: output.is_error }
    }

    /// Append a tool result to the conversation and the log.
    fn record(&self, state: &mut AgentState, tools: &[BoundTool], call: &ToolCall, output: ToolOutput) {
        let result = self.visible_result(tools, call, output);
        push_result(state, tools, call, result);
    }

    async fn run_calls(&self, ctx: &NodeContext, state: &mut AgentState, tools: &[BoundTool], calls: Vec<ToolCall>) {
        if calls.is_empty() {
            return;
        }
        for call in &calls {
            ctx.progress(
                "tool_call",
                Value::from_json(json!({"id": call.id, "name": call.name, "arguments": log_arguments(call, tools)})),
            );
        }
        let outputs = self.call_tools(tools, &calls).await;
        let capture = ctx.captures_content();
        for (call, output) in calls.iter().zip(outputs) {
            // The tool's own image count, including images the result drops.
            let offered_images = output.images.len();
            let result = self.visible_result(tools, call, output);
            let mut data =
                json!({"id": call.id, "name": call.name, "is_error": result.is_error, "images": offered_images});
            if capture && let Some(fields) = data.as_object_mut() {
                fields.insert("result".to_owned(), Json::String(result.text.clone()));
            }
            ctx.progress("tool_result", Value::from_json(data));
            push_result(state, tools, call, result);
        }
    }

    /// Run `calls` concurrently or one after another, per `parallel_tool_calls`.
    async fn call_tools(&self, tools: &[BoundTool], calls: &[ToolCall]) -> Vec<ToolOutput> {
        if self.config.parallel_tool_calls {
            return futures::future::join_all(calls.iter().map(|c| call_tool(tools, c))).await;
        }
        let mut outputs = Vec::with_capacity(calls.len());
        for call in calls {
            outputs.push(call_tool(tools, call).await);
        }
        outputs
    }
}

/// A tool result after [`AgentNode::visible_result`]: safe to show to the model and the host.
struct VisibleResult {
    text: String,
    images: Vec<ToolImage>,
    is_error: bool,
}

/// Append a visible tool result to the conversation and the log.
fn push_result(state: &mut AgentState, tools: &[BoundTool], call: &ToolCall, result: VisibleResult) {
    let bound = bound_tool(tools, call);
    state.messages.push(Message::tool_with_images(
        call.id.clone(),
        result.text.clone(),
        result.images.iter().map(ToolImage::to_url),
    ));
    state.log.push(ToolLogEntry {
        id: call.id.clone(),
        name: call.name.clone(),
        provider: bound.map(|b| b.provider.clone()).unwrap_or_default(),
        tool: bound.map(|b| b.name.clone()).unwrap_or_default(),
        arguments: log_arguments(call, tools),
        result: result.text,
        is_error: result.is_error,
        images: result.images.len(),
    });
}

/// Whether a turn offers tools, and whether it is the last one the iteration limit allows.
#[derive(Clone, Copy, Debug)]
struct TurnTools {
    offered: bool,
    final_turn: bool,
}

impl TurnTools {
    /// The model may call tools: they are offered and this is not the forced final turn.
    fn callable(self) -> bool {
        self.offered && !self.final_turn
    }
}

/// One model turn: the response, how many leading bytes of its text were
/// already streamed, its finish check, and whether it streamed live.
struct Turn {
    response: ChatResponse,
    streamed: usize,
    finish: FinishCheck,
    live: bool,
}

/// The assistant message of a tool turn as it is kept in the history. Malformed
/// arguments are never replayed to the provider: the history keeps `{}` and
/// the tool result explains the problem to the model.
fn replayable_message(response: &ChatResponse) -> Message {
    let mut assistant = response.to_message();
    for call in &mut assistant.tool_calls {
        if call.arguments_json().is_err() {
            call.arguments = "{}".to_owned();
        }
    }
    assistant
}

/// The node outputs of a finished loop.
fn agent_outputs(state: AgentState, final_text: String, stop: &StopReason) -> NodeOutcome {
    let log: Vec<Value> = state
        .log
        .iter()
        .map(|e| {
            Value::from_json(json!({
                "name": e.name, "provider": e.provider, "tool": e.tool,
                "arguments": e.arguments, "result": e.result, "is_error": e.is_error, "images": e.images,
            }))
        })
        .collect();
    NodeOutput::new()
        .with("text", final_text)
        .with("tool_calls", Value::from(log))
        .with("iterations", i64::from(state.iteration))
        .with("stopped_reason", stop.as_str())
        .with("usage", usage_value(&state.usage))
        .usage(state.usage)
        .into()
}

/// The tools a `provider/name` reference selects from the provider's listing:
/// all of them for `*`, else the one named (an error when it is missing).
fn select_specs<'a>(specs: &'a [ToolSpec], provider: &str, name: &str) -> Result<Vec<&'a ToolSpec>, NodeError> {
    if name == "*" {
        return Ok(specs.iter().collect());
    }
    let spec = specs
        .iter()
        .find(|s| s.name == name)
        .ok_or_else(|| NodeError::fatal("tool_not_found", format!("provider `{provider}` has no tool `{name}`")))?;
    Ok(vec![spec])
}

/// The bound tool a call names (by its exposed, possibly de-duplicated name).
fn bound_tool<'a>(tools: &'a [BoundTool], call: &ToolCall) -> Option<&'a BoundTool> {
    tools.iter().find(|t| t.exposed == call.name)
}

/// How one model turn streams.
#[derive(Clone, Copy, Debug)]
struct TurnPlan {
    /// Stream text and reasoning as they arrive (otherwise nothing is streamed).
    live: bool,
    /// Stop streaming text at the turn's first tool-call fragment.
    stop_at_tool_call: bool,
    /// Earlier turns streamed text: send [`PREAMBLE_SEPARATOR`] before this turn's first text.
    separate: bool,
}

/// Run one turn. Returns the response and how many leading bytes of its text
/// were streamed on field `text` (always whole deltas, so a char boundary).
/// Deltas are moved into chunks, never copied.
async fn stream_turn(
    ctx: &NodeContext,
    model: &dyn ChatModel,
    request: ChatRequest,
    plan: TurnPlan,
) -> Result<(ChatResponse, usize), NodeError> {
    if !plan.live {
        return Ok((invoke(ctx, model, request, StreamMode::Off).await?, 0));
    }
    let stream = model.chat(request).await?;
    let mut streamed = 0;
    let mut text_closed = false;
    let mut separator_pending = plan.separate;
    let response = collect(stream, |delta| match delta {
        ChatDelta::ToolCall { .. } => text_closed |= plan.stop_at_tool_call,
        ChatDelta::Text(text) if !text.is_empty() && !text_closed => {
            if std::mem::take(&mut separator_pending) {
                ctx.chunk_shared(StreamField::Text.shared(), PREAMBLE_SEPARATOR);
            }
            streamed += text.len();
            ctx.chunk_shared(StreamField::Text.shared(), text);
        }
        ChatDelta::Reasoning(text) if !text.is_empty() => ctx.chunk_shared(StreamField::Reasoning.shared(), text),
        _ => {}
    })
    .await?;
    Ok((response, streamed))
}

/// Append the streamed part of a tool-calling turn's text to the answer,
/// mirroring what [`stream_turn`] sent (separator included).
fn commit_preamble(answer: &mut String, text: &str, streamed: usize) {
    let Some(preamble) = text.get(..streamed).filter(|p| !p.is_empty()) else { return };
    if !answer.is_empty() {
        answer.push_str(PREAMBLE_SEPARATOR);
    }
    answer.push_str(preamble);
}

/// The `text` output: streamed preambles, then the final turn's text (which
/// is always streamed whole). Without preambles the final text is moved as is.
fn final_answer(mut answer: String, text: String, streamed: usize) -> String {
    if answer.is_empty() {
        return text;
    }
    commit_preamble(&mut answer, &text, streamed);
    answer
}

/// Replace the images of all but the last `keep` image-bearing tool results
/// with a text placeholder, so a screenshot loop does not resend every
/// screenshot on every turn.
fn prune_tool_images(messages: &mut [Message], keep: usize) {
    let mut seen = 0;
    for message in messages.iter_mut().rev().filter(|m| m.role == Role::Tool) {
        let count = message.images().count();
        if count == 0 {
            continue;
        }
        seen += 1;
        if seen <= keep {
            continue;
        }
        let note = if count == 1 {
            "[1 image removed from history]".to_owned()
        } else {
            format!("[{count} images removed from history]")
        };
        let text = message.text();
        let text = if text.is_empty() { note } else { format!("{text}\n{note}") };
        message.content = vec![ContentPart::Text { text }];
    }
}

/// Arguments as shown in logs and approval requests: the parsed object, or the
/// raw text when it is not valid JSON (so nothing is hidden from the reader),
/// with the tool's context values masked.
fn log_arguments(call: &ToolCall, tools: &[BoundTool]) -> Json {
    let mut arguments = match call.arguments_json() {
        Ok(map) => Json::Object(map),
        Err(_) => Json::String(call.arguments.clone()),
    };
    if let Some(tool) = bound_tool(tools, call)
        && !tool.context().is_empty()
    {
        scrub_json(&mut arguments, tool.context());
    }
    arguments
}

/// `text` with every value of `context` masked. Borrows when there is nothing to mask.
fn scrub_context<'a>(text: &'a str, context: &ToolCallContext) -> Cow<'a, str> {
    if context.is_empty() {
        return Cow::Borrowed(text);
    }
    let values: Vec<&str> = context.secrets.values().map(String::as_str).collect();
    redact_secrets(text, &values)
}

/// Mask the values of `context` in every string of `value`.
fn scrub_json(value: &mut Json, context: &ToolCallContext) {
    match value {
        Json::String(text) => {
            if let Cow::Owned(scrubbed) = scrub_context(text, context) {
                *text = scrubbed;
            }
        }
        Json::Array(items) => items.iter_mut().for_each(|item| scrub_json(item, context)),
        Json::Object(map) => map.values_mut().for_each(|item| scrub_json(item, context)),
        _ => {}
    }
}

async fn call_tool(tools: &[BoundTool], call: &ToolCall) -> ToolOutput {
    let Some(tool) = bound_tool(tools, call) else {
        return ToolOutput::error(format!("error: unknown tool `{}`", call.name));
    };
    let arguments = match call.arguments_json() {
        Ok(map) => Json::Object(map),
        Err(e) => return ToolOutput::error(format!("error: {e}")),
    };
    match tool.runtime.call(&tool.name, arguments, tool.context()).await {
        Ok(output) => output,
        Err(e) => ToolOutput::error(format!("error: {e}")),
    }
}

/// Host decision for paused tool calls.
#[derive(Debug, Default)]
struct Approvals {
    all: Option<bool>,
    per_call: HashMap<String, bool>,
    reason: Option<String>,
}

impl Approvals {
    /// Recognized forms: `true`/`false`, `{"approved": bool}`,
    /// `{"approvals": {"<call id>": bool}}` (both may be combined, per-call wins),
    /// plus an optional `reason`. Anything else decides nothing.
    fn from_input(input: Option<&Value>) -> Self {
        let mut out = Self::default();
        match input {
            Some(Value::Bool(b)) => out.all = Some(*b),
            Some(Value::Object(map)) => {
                out.all = map.get("approved").and_then(Value::as_bool);
                if let Some(Value::Object(per)) = map.get("approvals") {
                    for (id, v) in per.iter() {
                        if let Some(b) = v.as_bool() {
                            out.per_call.insert(id.clone(), b);
                        }
                    }
                }
                out.reason = map.get("reason").and_then(Value::as_str).map(str::to_owned);
            }
            _ => {}
        }
        out
    }

    /// `Some(true)` approved, `Some(false)` denied, `None` no decision yet.
    fn decision(&self, id: &str) -> Option<bool> {
        self.per_call.get(id).copied().or(self.all)
    }
}

/// Model-safe tool name: `[A-Za-z0-9_-]{1,64}`, unique within the run.
fn unique_name(name: &str, provider: &str, used: &mut HashSet<String>) -> String {
    let clean = |s: &str| -> String {
        let mut out: String = s
            .chars()
            .map(|c| if c.is_ascii_alphanumeric() || c == '_' || c == '-' { c } else { '_' })
            .take(64)
            .collect();
        if out.is_empty() {
            out.push_str("tool");
        }
        out
    };
    let mut candidate = clean(name);
    if used.contains(&candidate) {
        candidate = clean(&format!("{provider}_{name}"));
    }
    let base = candidate.clone();
    let mut n = 2;
    while used.contains(&candidate) {
        let suffix = format!("_{n}");
        let keep = 64usize.saturating_sub(suffix.len());
        candidate = format!("{}{suffix}", base.chars().take(keep).collect::<String>());
        n += 1;
    }
    used.insert(candidate.clone());
    candidate
}

fn truncate(text: &str, max_chars: usize) -> String {
    if max_chars == 0 || text.chars().count() <= max_chars {
        return text.to_owned();
    }
    let mut out: String = text.chars().take(max_chars).collect();
    out.push_str("\n[truncated]");
    out
}

impl Node for AgentNode {
    fn run(&self, ctx: NodeContext) -> BoxFuture<'_, NodeResult> {
        Box::pin(self.execute(ctx))
    }

    fn inputs(&self) -> Vec<Selector> {
        let cfg = &self.config;
        let mut out: Vec<Selector> = cfg.query.selectors().cloned().collect();
        if let Some(i) = &cfg.instruction {
            out.extend(i.selectors().cloned());
        }
        if let Some(memory) = &cfg.memory {
            out.extend(memory.selectors());
        }
        out.extend(cfg.vision.selectors());
        out.extend(cfg.model.selectors());
        out.extend(self.tool_context.iter().flat_map(|(_, t)| t).flat_map(|(_, t)| t.selectors().cloned()));
        out
    }
}

/// Factory for the node registry.
pub fn factory(doc: &NodeDoc) -> Result<Arc<dyn Node>, ConfigError> {
    Ok(Arc::new(AgentNode::from_doc(doc)?))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mock::MockModel;
    use crate::registry::ModelRegistry;
    use crate::types::Role;
    use graphrt_core::event::Event;
    use graphrt_core::node::Resume;
    use graphrt_core::tool::ToolError;
    use graphrt_core::{Services, VariablePool};
    use std::sync::Mutex;

    /// Tools: `add {a, b}` and `fail {}`.
    #[derive(Debug, Default)]
    struct MathTools {
        calls: Mutex<Vec<String>>,
    }

    impl ToolRuntime for MathTools {
        fn list(&self) -> BoxFuture<'_, Result<Vec<ToolSpec>, ToolError>> {
            Box::pin(async {
                Ok(vec![
                    ToolSpec {
                        name: "add".into(),
                        description: "add numbers".into(),
                        parameters: json!({"type": "object"}),
                    },
                    ToolSpec {
                        name: "fail".into(),
                        description: "always fails".into(),
                        parameters: json!({"type": "object"}),
                    },
                ])
            })
        }

        fn call<'a>(
            &'a self,
            name: &'a str,
            arguments: Json,
            _context: &'a ToolCallContext,
        ) -> BoxFuture<'a, Result<ToolOutput, ToolError>> {
            self.calls.lock().unwrap().push(name.to_owned());
            Box::pin(async move {
                match name {
                    "add" => {
                        let a = arguments["a"].as_i64().unwrap_or(0);
                        let b = arguments["b"].as_i64().unwrap_or(0);
                        Ok(ToolOutput::text((a + b).to_string()))
                    }
                    _ => Err(ToolError { tool: name.to_owned(), message: "broken".into() }),
                }
            })
        }
    }

    /// Tools: `screenshot {}` (one PNG), `bad_image {}` (an unsupported type).
    #[derive(Debug, Default)]
    struct ScreenTools;

    impl ToolRuntime for ScreenTools {
        fn list(&self) -> BoxFuture<'_, Result<Vec<ToolSpec>, ToolError>> {
            let spec = |name: &str| ToolSpec { name: name.into(), description: "d".into(), parameters: json!({}) };
            Box::pin(async move { Ok(vec![spec("screenshot"), spec("bad_image")]) })
        }

        fn call<'a>(
            &'a self,
            name: &'a str,
            _arguments: Json,
            _context: &'a ToolCallContext,
        ) -> BoxFuture<'a, Result<ToolOutput, ToolError>> {
            Box::pin(async move {
                Ok(match name {
                    "screenshot" => ToolOutput::text("800x600").with_image(ToolImage::png("aGVsbG8=")),
                    _ => ToolOutput::text("odd")
                        .with_image(ToolImage::Base64 { media_type: "image/bmp".into(), data_base64: "aGVs".into() }),
                })
            })
        }
    }

    struct Setup {
        model: Arc<MockModel>,
        tools: Arc<MathTools>,
        events: Arc<Mutex<Vec<Event>>>,
    }

    fn setup(responses: Vec<Vec<crate::ChatDelta>>) -> Setup {
        Setup {
            model: Arc::new(MockModel::new(responses)),
            tools: Arc::new(MathTools::default()),
            events: Arc::default(),
        }
    }

    fn ctx(s: &Setup) -> NodeContext {
        let mut tool_registry = ToolRegistry::new();
        tool_registry.add("math", Arc::clone(&s.tools) as Arc<dyn ToolRuntime>);
        tool_registry.add("screen", Arc::new(ScreenTools) as Arc<dyn ToolRuntime>);
        let services = Services::new()
            .with(ModelRegistry::new().with("openai", Arc::clone(&s.model) as Arc<dyn crate::ChatModel>))
            .with(tool_registry);
        let mut pool = VariablePool::new();
        pool.insert("sys", "query", Value::from("what is 2+3?"));
        let events = Arc::clone(&s.events);
        NodeContext::for_test("agent", pool)
            .with_services(Arc::new(services))
            .with_sink(Arc::new(move |e| events.lock().unwrap().push(e)))
    }

    fn agent(extra: Json) -> AgentNode {
        let mut config = json!({
            "model": {"provider": "openai", "name": "gpt"},
            "instruction": "You are a calculator.",
            "tools": ["math/*"],
        });
        if let (Some(c), Json::Object(e)) = (config.as_object_mut(), extra) {
            c.extend(e);
        }
        AgentNode::from_doc(&NodeDoc::new("agent", "agent", config)).unwrap()
    }

    fn completed(r: NodeResult) -> NodeOutput {
        match r.unwrap() {
            NodeOutcome::Completed(o) => o,
            NodeOutcome::Paused(p) => panic!("unexpected pause: {p:?}"),
        }
    }

    #[tokio::test]
    async fn loops_until_answer() {
        let s = setup(vec![
            MockModel::tool_calls(&[("c1", "add", r#"{"a":2,"b":3}"#), ("c2", "fail", "{}")]),
            MockModel::text("The answer is 5."),
        ]);
        let out = completed(agent(json!({})).run(ctx(&s)).await);
        assert_eq!(out.outputs["text"], Value::from("The answer is 5."));
        assert_eq!(out.outputs["iterations"], Value::Int(2));
        let log = out.outputs["tool_calls"].as_array().unwrap().to_vec();
        assert_eq!(log.len(), 2);
        assert_eq!(log[0].get_path(&["result"]), Some(Value::from("5")));
        assert_eq!(log[1].get_path(&["is_error"]), Some(Value::Bool(true)));
        assert_eq!(out.usage.unwrap().total_tokens, 4);

        let requests = s.model.requests();
        assert_eq!(requests[0].tools.len(), 2);
        let second = &requests[1].messages;
        assert_eq!(second[0].role, Role::System);
        assert_eq!(second[2].tool_calls.len(), 2);
        assert_eq!(second[3].tool_call_id.as_deref(), Some("c1"));
        assert_eq!(second[3].text(), "5");
        assert!(second[4].text().contains("broken"));

        let progress: Vec<String> = s
            .events
            .lock()
            .unwrap()
            .iter()
            .filter_map(|e| match e {
                Event::NodeProgress { name, .. } => Some(name.clone()),
                _ => None,
            })
            .collect();
        assert_eq!(progress, ["agent_step", "tool_call", "tool_call", "tool_result", "tool_result", "agent_step"]);
    }

    const JWT: &str = "eyJhbGciOiJIUzI1NiJ9.user-token";

    /// Tool `whoami {}` that echoes its context (as a careless upstream error would).
    #[derive(Debug)]
    struct VaultTools {
        accepts: bool,
        contexts: Mutex<Vec<ToolCallContext>>,
    }

    impl VaultTools {
        fn new(accepts: bool) -> Arc<Self> {
            Arc::new(Self { accepts, contexts: Mutex::default() })
        }
    }

    impl ToolRuntime for VaultTools {
        fn list(&self) -> BoxFuture<'_, Result<Vec<ToolSpec>, ToolError>> {
            let spec = ToolSpec { name: "whoami".into(), description: "d".into(), parameters: json!({}) };
            Box::pin(async move { Ok(vec![spec]) })
        }

        fn call<'a>(
            &'a self,
            _name: &'a str,
            _arguments: Json,
            context: &'a ToolCallContext,
        ) -> BoxFuture<'a, Result<ToolOutput, ToolError>> {
            self.contexts.lock().unwrap().push(context.clone());
            let token = context.secret("user_jwt").unwrap_or("none").to_owned();
            Box::pin(async move { Ok(ToolOutput::text(format!("rejected token {token}"))) })
        }

        fn accepts_secrets(&self) -> bool {
            self.accepts
        }
    }

    fn vault_ctx(s: &Setup, vault: &Arc<VaultTools>) -> NodeContext {
        let mut registry = ToolRegistry::new();
        registry.add("vault", Arc::clone(vault) as Arc<dyn ToolRuntime>);
        let services = Services::new()
            .with(ModelRegistry::new().with("openai", Arc::clone(&s.model) as Arc<dyn crate::ChatModel>))
            .with(registry);
        let mut pool = VariablePool::new();
        pool.insert("sys", "query", Value::from("who am I?"));
        pool.insert("start", "user_jwt", Value::Secret(Arc::from(JWT)));
        let events = Arc::clone(&s.events);
        NodeContext::for_test("agent", pool)
            .with_services(Arc::new(services))
            .with_sink(Arc::new(move |e| events.lock().unwrap().push(e)))
    }

    fn vault_agent(extra: Json) -> AgentNode {
        let mut config = json!({
            "model": {"provider": "openai", "name": "gpt"},
            "tools": ["vault/whoami"],
            "tool_context": {"vault": {"user_jwt": "{{#start.user_jwt#}}", "missing": "{{#start.nope#}}"}},
        });
        if let (Some(c), Json::Object(e)) = (config.as_object_mut(), extra) {
            c.extend(e);
        }
        AgentNode::from_doc(&NodeDoc::new("agent", "agent", config)).unwrap()
    }

    #[tokio::test]
    async fn tool_context_reaches_the_tool_but_nothing_else() {
        let s = setup(vec![
            MockModel::tool_calls(&[("c1", "whoami", &format!(r#"{{"echo":"{JWT}"}}"#))]),
            MockModel::text("done"),
        ]);
        let vault = VaultTools::new(true);
        let secrets = Arc::new(graphrt_core::redact::SecretSet::new());
        let ctx = vault_ctx(&s, &vault).with_secret_values(Arc::clone(&secrets));
        let node = vault_agent(json!({}));
        let out = completed(node.run(ctx).await);

        let contexts = vault.contexts.lock().unwrap().clone();
        assert_eq!(contexts.len(), 1);
        assert_eq!(contexts[0].secret("user_jwt"), Some(JWT));
        assert_eq!(contexts[0].secret("missing"), None, "empty renders are left out");

        // Not in the model's history, the outputs or any event.
        let requests = s.model.requests();
        assert_eq!(requests[1].messages.last().unwrap().text(), "rejected token ******");
        // (The model's own arguments stay in its history as it wrote them.)
        let tool_messages: Vec<_> = requests[1].messages.iter().filter(|m| m.role == Role::Tool).collect();
        assert!(!format!("{tool_messages:?}").contains(JWT));
        assert!(!serde_json::to_string(&out.outputs).unwrap().contains(JWT));
        let events = serde_json::to_string(&*s.events.lock().unwrap()).unwrap();
        assert!(!events.contains(JWT), "{events}");
        // Registered with the run so later node errors are scrubbed too.
        assert_eq!(secrets.scrub(&format!("bad {JWT}")), "bad ******");
        assert!(node.inputs().iter().any(|sel| sel.to_string() == "start.user_jwt"));
    }

    #[tokio::test]
    async fn captured_tool_results_never_carry_the_tool_context() {
        let s = setup(vec![MockModel::tool_calls(&[("c1", "whoami", "{}")]), MockModel::text("done")]);
        let vault = VaultTools::new(true);
        let base = vault_ctx(&s, &vault);
        let services = (*base.services).clone().with(graphrt_core::node::CaptureContent);
        completed(vault_agent(json!({})).run(base.with_services(Arc::new(services))).await);
        assert_eq!(tool_results(&s)[0].get_path(&["result"]), Some(Value::from("rejected token ******")));
        let events = serde_json::to_string(&*s.events.lock().unwrap()).unwrap();
        assert!(!events.contains(JWT), "{events}");
    }

    #[tokio::test]
    async fn tool_results_are_masked_before_they_are_cut() {
        let s = setup(vec![MockModel::tool_calls(&[("c1", "whoami", "{}")]), MockModel::text("done")]);
        let vault = VaultTools::new(true);
        let base = vault_ctx(&s, &vault);
        let services = (*base.services).clone().with(graphrt_core::node::CaptureContent);
        // The cut falls inside the echoed token: masking afterwards would miss its prefix.
        let limit = "rejected token ".len() + 12;
        let node = vault_agent(json!({"max_tool_output_chars": limit}));
        completed(node.run(base.with_services(Arc::new(services))).await);
        let token_prefix = &JWT[..12];
        let events = serde_json::to_string(&*s.events.lock().unwrap()).unwrap();
        assert!(!events.contains(token_prefix), "{events}");
        let requests = s.model.requests();
        assert!(!format!("{:?}", requests[1].messages).contains(token_prefix));
    }

    #[tokio::test]
    async fn tool_context_is_sent_again_after_approval() {
        let s = setup(vec![MockModel::tool_calls(&[("c1", "whoami", "{}")]), MockModel::text("done")]);
        let vault = VaultTools::new(true);
        let node = vault_agent(json!({"require_approval": ["*"]}));
        let paused = match node.run(vault_ctx(&s, &vault)).await.unwrap() {
            NodeOutcome::Paused(p) => p,
            NodeOutcome::Completed(_) => panic!("expected a pause"),
        };
        assert!(!paused.state.to_string().contains(JWT), "the snapshot state never holds the context");
        let resume = Resume { state: paused.state, input: Some(Value::Bool(true)) };
        completed(node.run(vault_ctx(&s, &vault).with_resume(Some(resume))).await);
        assert_eq!(vault.contexts.lock().unwrap()[0].secret("user_jwt"), Some(JWT));
    }

    #[tokio::test]
    async fn tool_context_needs_a_provider_that_accepts_secrets() {
        let s = setup(vec![MockModel::text("unused")]);
        let vault = VaultTools::new(false);
        let error = vault_agent(json!({})).run(vault_ctx(&s, &vault)).await.unwrap_err();
        assert_eq!(error.kind, "tool_context_denied");
        assert!(vault.contexts.lock().unwrap().is_empty());
    }

    #[test]
    fn tool_context_providers_must_be_used() {
        let config = json!({
            "model": {"provider": "openai", "name": "gpt"},
            "tools": ["math/add"],
            "tool_context": {"vault": {"user_jwt": "x"}},
        });
        let error = AgentNode::from_doc(&NodeDoc::new("agent", "agent", config)).unwrap_err();
        assert!(error.to_string().contains("`vault` is not used"), "{error}");
        for bad in [json!({"math": {"jwt": 1}}), json!({"math": {"": "x"}}), json!({"math": "x"})] {
            let config =
                json!({"model": {"provider": "openai", "name": "gpt"}, "tools": ["math/add"], "tool_context": bad});
            assert!(AgentNode::from_doc(&NodeDoc::new("agent", "agent", config)).is_err());
        }
    }

    fn tool_results(s: &Setup) -> Vec<Value> {
        let events = s.events.lock().unwrap();
        let results = events.iter().filter_map(|e| match e {
            Event::NodeProgress { name, data, .. } if name == "tool_result" => Some(data.clone()),
            _ => None,
        });
        results.collect()
    }

    #[tokio::test]
    async fn tool_results_carry_text_only_when_the_host_captures_content() {
        let responses = || vec![MockModel::tool_calls(&[("c1", "add", r#"{"a":2,"b":3}"#)]), MockModel::text("5")];
        let s = setup(responses());
        completed(agent(json!({})).run(ctx(&s)).await);
        assert_eq!(tool_results(&s)[0].get_path(&["result"]), None);

        let s = setup(responses());
        let base = ctx(&s);
        let services = (*base.services).clone().with(graphrt_core::node::CaptureContent);
        completed(agent(json!({})).run(base.with_services(Arc::new(services))).await);
        assert_eq!(tool_results(&s)[0].get_path(&["result"]), Some(Value::from("5")));
    }

    #[tokio::test]
    async fn tool_images_reach_the_model() {
        let s = setup(vec![MockModel::tool_calls(&[("c1", "screenshot", "{}")]), MockModel::text("a login form")]);
        let out = completed(agent(json!({"tools": ["screen/*"]})).run(ctx(&s)).await);
        let requests = s.model.requests();
        let result = &requests[1].messages[3];
        assert_eq!(result.role, Role::Tool);
        assert_eq!(result.text(), "800x600");
        assert_eq!(
            result.images().cloned().collect::<Vec<_>>(),
            vec![ContentPart::Image { url: "data:image/png;base64,aGVsbG8=".into(), detail: None }]
        );
        let log = out.outputs["tool_calls"].as_array().unwrap().to_vec();
        assert_eq!(log[0].get_path(&["images"]), Some(Value::Int(1)));
        let reported = s.events.lock().unwrap().iter().find_map(|e| match e {
            Event::NodeProgress { name, data, .. } if name == "tool_result" => data.get_path(&["images"]),
            _ => None,
        });
        assert_eq!(reported, Some(Value::Int(1)));
    }

    #[tokio::test]
    async fn invalid_tool_images_are_dropped_with_a_note() {
        let s = setup(vec![MockModel::tool_calls(&[("c1", "bad_image", "{}")]), MockModel::text("ok")]);
        completed(agent(json!({"tools": ["screen/*"]})).run(ctx(&s)).await);
        let result = &s.model.requests()[1].messages[3];
        assert_eq!(result.images().count(), 0);
        assert!(
            result.text().starts_with("odd\n[images dropped: image #0: image media type `image/bmp`"),
            "{}",
            result.text()
        );
    }

    #[tokio::test]
    async fn captured_tool_result_text_matches_what_the_model_sees() {
        let s = setup(vec![MockModel::tool_calls(&[("c1", "bad_image", "{}")]), MockModel::text("ok")]);
        let base = ctx(&s);
        let services = (*base.services).clone().with(graphrt_core::node::CaptureContent);
        let out = completed(agent(json!({"tools": ["screen/*"]})).run(base.with_services(Arc::new(services))).await);
        let seen_by_model = s.model.requests()[1].messages[3].text();
        assert!(seen_by_model.contains("[images dropped:"), "{seen_by_model}");
        let reported = tool_results(&s)[0].get_path(&["result"]);
        assert_eq!(reported, Some(Value::from(seen_by_model.clone())), "progress event");
        let log = out.outputs["tool_calls"].as_array().unwrap().to_vec();
        assert_eq!(log[0].get_path(&["result"]), Some(Value::from(seen_by_model)), "tool_calls output");
    }

    #[tokio::test]
    async fn keep_tool_images_prunes_older_screenshots() {
        let s = setup(vec![
            MockModel::tool_calls(&[("c1", "screenshot", "{}")]),
            MockModel::tool_calls(&[("c2", "screenshot", "{}")]),
            MockModel::tool_calls(&[("c3", "screenshot", "{}")]),
            MockModel::text("done"),
        ]);
        completed(agent(json!({"tools": ["screen/*"], "keep_tool_images": 2})).run(ctx(&s)).await);
        let last = s.model.requests().last().cloned().unwrap();
        let results: Vec<&Message> = last.messages.iter().filter(|m| m.role == Role::Tool).collect();
        assert_eq!(results.iter().map(|m| m.images().count()).collect::<Vec<_>>(), [0, 1, 1]);
        assert_eq!(results[0].text(), "800x600\n[1 image removed from history]");
        // The second request still had every image (only two existed).
        let second = &s.model.requests()[2];
        assert_eq!(second.messages.iter().map(|m| m.images().count()).sum::<usize>(), 2);
    }

    fn four_screenshots() -> Vec<Vec<crate::ChatDelta>> {
        vec![
            MockModel::tool_calls(&[("c1", "screenshot", "{}")]),
            MockModel::tool_calls(&[("c2", "screenshot", "{}")]),
            MockModel::tool_calls(&[("c3", "screenshot", "{}")]),
            MockModel::tool_calls(&[("c4", "screenshot", "{}")]),
            MockModel::text("done"),
        ]
    }

    fn images_per_tool_result(s: &Setup) -> Vec<usize> {
        let last = s.model.requests().last().cloned().unwrap();
        last.messages.iter().filter(|m| m.role == Role::Tool).map(|m| m.images().count()).collect()
    }

    #[tokio::test]
    async fn keep_tool_images_defaults_to_the_last_three() {
        let s = setup(four_screenshots());
        completed(agent(json!({"tools": ["screen/*"]})).run(ctx(&s)).await);
        assert_eq!(images_per_tool_result(&s), [0, 1, 1, 1]);
    }

    #[tokio::test]
    async fn keep_tool_images_null_keeps_every_image() {
        let s = setup(four_screenshots());
        completed(agent(json!({"tools": ["screen/*"], "keep_tool_images": null})).run(ctx(&s)).await);
        assert_eq!(images_per_tool_result(&s), [1, 1, 1, 1]);
    }

    #[tokio::test]
    async fn last_iteration_keeps_tools_but_forbids_calls() {
        let s = setup(vec![MockModel::tool_calls(&[("c1", "add", r#"{"a":1,"b":1}"#)]), MockModel::text("2")]);
        let out = completed(agent(json!({"max_iterations": 2})).run(ctx(&s)).await);
        assert_eq!(out.outputs["text"], Value::from("2"));
        assert_eq!(out.outputs["stopped_reason"], Value::from("max_iterations"));
        let requests = s.model.requests();
        assert_eq!(requests[0].tool_choice, Some(ToolChoice::Auto));
        assert!(!requests[1].tools.is_empty(), "tool definitions stay for the history's tool calls");
        assert_eq!(requests[1].tool_choice, Some(ToolChoice::None));
    }

    fn text_chunks(s: &Setup) -> Vec<String> {
        s.events
            .lock()
            .unwrap()
            .iter()
            .filter_map(|e| match e {
                Event::NodeChunk { field, chunk, .. } if field.as_ref() == "text" => Some(chunk.clone()),
                _ => None,
            })
            .collect()
    }

    fn thought(s: &Setup) -> Option<Value> {
        s.events.lock().unwrap().iter().find_map(|e| match e {
            Event::NodeProgress { name, data, .. } if name == "agent_thought" => Some(data.clone()),
            _ => None,
        })
    }

    fn tokens(pieces: &[&str]) -> Vec<crate::ChatDelta> {
        let mut out: Vec<crate::ChatDelta> = pieces.iter().map(|p| crate::ChatDelta::Text((*p).into())).collect();
        out.push(crate::ChatDelta::Finish(FinishReason::Stop));
        out
    }

    #[tokio::test]
    async fn final_answer_streams_token_by_token() {
        let s =
            setup(vec![MockModel::tool_calls(&[("c1", "add", r#"{"a":2,"b":3}"#)]), tokens(&["It", " is", " 5", "."])]);
        let out = completed(agent(json!({})).run(ctx(&s)).await);
        // One chunk per provider delta, on a turn that was still allowed to call tools.
        assert_eq!(text_chunks(&s), ["It", " is", " 5", "."]);
        assert_eq!(out.outputs["text"], Value::from("It is 5."));
        assert_eq!(s.model.requests()[1].tool_choice, Some(ToolChoice::Auto));
    }

    #[tokio::test]
    async fn preamble_before_a_tool_call_is_streamed_and_kept_in_the_answer() {
        let mut first = tokens(&["Let me", " check."]);
        first.pop();
        first.extend(MockModel::tool_calls(&[("c1", "add", r#"{"a":2,"b":3}"#)]));
        let s = setup(vec![first, tokens(&["It is", " 5."])]);
        let out = completed(agent(json!({})).run(ctx(&s)).await);
        let chunks = text_chunks(&s);
        assert_eq!(chunks, ["Let me", " check.", "\n\n", "It is", " 5."]);
        assert_eq!(out.outputs["text"], Value::from(chunks.concat().as_str()));
        let thought = thought(&s).unwrap();
        assert_eq!(thought.get_path(&["text"]), Some(Value::from("Let me check.")));
        assert_eq!(thought.get_path(&["streamed"]), Some(Value::Bool(true)));
        // The history keeps each turn separate: the preamble belongs to the tool-call turn.
        let second = &s.model.requests()[1].messages;
        assert_eq!(second[2].text(), "Let me check.");
        assert_eq!(second[2].tool_calls.len(), 1);
    }

    #[tokio::test]
    async fn tool_call_fragments_and_later_text_are_never_streamed() {
        use crate::ChatDelta::{Finish, Text, ToolCall};
        let first = vec![
            Text("A".into()),
            ToolCall { index: 0, id: Some("c1".into()), name: Some("add".into()), arguments: r#"{"a":"#.into() },
            Text("B".into()),
            ToolCall { index: 0, id: None, name: None, arguments: "1}".into() },
            Finish(FinishReason::ToolCalls),
        ];
        let s = setup(vec![first, MockModel::text("done")]);
        let out = completed(agent(json!({})).run(ctx(&s)).await);
        assert_eq!(text_chunks(&s), ["A", "\n\n", "done"]);
        assert_eq!(out.outputs["text"], Value::from("A\n\ndone"));
        assert_eq!(thought(&s).unwrap().get_path(&["text"]), Some(Value::from("AB")));
        assert_eq!(s.tools.calls.lock().unwrap().as_slice(), ["add"]);
    }

    #[tokio::test]
    async fn stream_tool_turns_false_buffers_tool_turns() {
        let mut first = vec![crate::ChatDelta::Text("Let me check.".into())];
        first.extend(MockModel::tool_calls(&[("c1", "add", r#"{"a":2,"b":3}"#)]));
        let s = setup(vec![first, tokens(&["It is", " 5."])]);
        let out = completed(agent(json!({"stream_tool_turns": false})).run(ctx(&s)).await);
        assert_eq!(out.outputs["stopped_reason"], Value::from("completed"));
        assert_eq!(text_chunks(&s), ["It is 5."]);
        assert_eq!(out.outputs["text"], Value::from("It is 5."));
        let thought = thought(&s).unwrap();
        assert_eq!(thought.get_path(&["text"]), Some(Value::from("Let me check.")));
        assert_eq!(thought.get_path(&["streamed"]), Some(Value::Bool(false)));
    }

    #[tokio::test]
    async fn forced_final_turn_streams_token_by_token() {
        let s = setup(vec![MockModel::tool_calls(&[("c1", "add", r#"{"a":1,"b":1}"#)]), tokens(&["=", "2"])]);
        let out = completed(agent(json!({"max_iterations": 2, "stream_tool_turns": false})).run(ctx(&s)).await);
        assert_eq!(text_chunks(&s), ["=", "2"]);
        assert_eq!(out.outputs["text"], Value::from("=2"));
    }

    #[tokio::test]
    async fn preamble_survives_a_tool_approval_pause() {
        let mut first = vec![crate::ChatDelta::Text("Checking.".into())];
        first.extend(MockModel::tool_calls(&[("c1", "add", r#"{"a":2,"b":2}"#)]));
        let s = setup(vec![first, MockModel::text("4")]);
        let node = agent(json!({"require_approval": ["add"]}));
        let NodeOutcome::Paused(paused) = node.run(ctx(&s)).await.unwrap() else { panic!("expected pause") };
        let state: Json = serde_json::from_str(&paused.state.to_string()).unwrap();
        let resumed =
            ctx(&s).with_resume(Some(Resume { state, input: Some(Value::from_json(json!({"approved": true}))) }));
        let out = completed(node.run(resumed).await);
        assert_eq!(out.outputs["text"], Value::from("Checking.\n\n4"));
        assert_eq!(text_chunks(&s), ["Checking.", "\n\n", "4"]);
    }

    #[tokio::test]
    async fn malformed_arguments_are_reported_back_to_the_model() {
        let s = setup(vec![MockModel::tool_calls(&[("c1", "add", r#"{"a":2,"#)]), MockModel::text("sorry")]);
        let out = completed(agent(json!({})).run(ctx(&s)).await);
        assert!(s.tools.calls.lock().unwrap().is_empty(), "malformed calls never run");
        let log = out.outputs["tool_calls"].as_array().unwrap().to_vec();
        assert_eq!(log[0].get_path(&["arguments"]), Some(Value::from(r#"{"a":2,"#)));
        assert_eq!(log[0].get_path(&["is_error"]), Some(Value::Bool(true)));
        let second = &s.model.requests()[1];
        assert_eq!(second.messages[2].tool_calls[0].arguments, "{}", "history never replays malformed JSON");
        assert!(second.messages[3].text().contains("invalid JSON arguments"));
    }

    #[tokio::test]
    async fn per_turn_limit_rejects_extra_calls() {
        let s = setup(vec![
            MockModel::tool_calls(&[("c1", "add", r#"{"a":1,"b":1}"#), ("c2", "add", r#"{"a":2,"b":2}"#)]),
            MockModel::text("ok"),
        ]);
        let out =
            completed(agent(json!({"max_tool_calls_per_turn": 1, "parallel_tool_calls": false})).run(ctx(&s)).await);
        assert_eq!(s.tools.calls.lock().unwrap().len(), 1);
        let log = out.outputs["tool_calls"].as_array().unwrap().to_vec();
        assert_eq!(log[0].get_path(&["is_error"]), Some(Value::Bool(true)));
    }

    #[tokio::test]
    async fn approval_pauses_and_resumes() {
        let s = setup(vec![MockModel::tool_calls(&[("c1", "add", r#"{"a":2,"b":2}"#)]), MockModel::text("4")]);
        let node = agent(json!({"tools": [{"provider": "math", "name": "add"}], "require_approval": ["math/add"]}));
        let paused = match node.run(ctx(&s)).await.unwrap() {
            NodeOutcome::Paused(p) => p,
            NodeOutcome::Completed(_) => panic!("expected pause"),
        };
        assert_eq!(paused.kind, "tool_approval");
        assert_eq!(paused.data.get_path(&["calls", "0", "name"]), Some(Value::from("add")));
        assert!(s.tools.calls.lock().unwrap().is_empty(), "nothing runs before approval");

        // The saved state survives serialization (snapshots store it as JSON).
        let state: Json = serde_json::from_str(&paused.state.to_string()).unwrap();
        let resumed =
            ctx(&s).with_resume(Some(Resume { state, input: Some(Value::from_json(json!({"approved": true}))) }));
        let out = completed(node.run(resumed).await);
        assert_eq!(out.outputs["text"], Value::from("4"));
        assert_eq!(s.tools.calls.lock().unwrap().as_slice(), ["add"]);
    }

    #[tokio::test]
    async fn denied_calls_are_reported_to_the_model() {
        let s = setup(vec![MockModel::tool_calls(&[("c1", "add", "{}")]), MockModel::text("fine")]);
        let node = agent(json!({"require_approval": ["*"]}));
        let NodeOutcome::Paused(p) = node.run(ctx(&s)).await.unwrap() else { panic!("expected pause") };
        let input = Value::from_json(json!({"approvals": {"c1": false}, "reason": "not now"}));
        let out = completed(node.run(ctx(&s).with_resume(Some(Resume { state: p.state, input: Some(input) }))).await);
        assert_eq!(out.outputs["text"], Value::from("fine"));
        assert!(s.tools.calls.lock().unwrap().is_empty());
        let last_request = s.model.requests().pop().unwrap();
        assert!(last_request.messages.iter().any(|m| m.text().contains("not now")));
    }

    #[tokio::test]
    async fn only_gated_calls_wait_for_approval() {
        let s = setup(vec![
            MockModel::tool_calls(&[("c1", "add", r#"{"a":1,"b":1}"#), ("c2", "fail", "{}")]),
            MockModel::text("done"),
        ]);
        let node = agent(json!({"require_approval": ["math/fail"]}));
        let NodeOutcome::Paused(p) = node.run(ctx(&s)).await.unwrap() else { panic!("expected pause") };
        assert_eq!(s.tools.calls.lock().unwrap().as_slice(), ["add"], "ungated calls run immediately");
        assert_eq!(p.data.get_path(&["calls", "0", "id"]), Some(Value::from("c2")));
        assert!(p.data.get_path(&["calls", "1"]).is_none());

        // No decision (or one that is not understood) keeps the run paused.
        for input in [None, Some(Value::from("yes")), Some(Value::from_json(json!({"approved": "true"})))] {
            let resumed = ctx(&s).with_resume(Some(Resume { state: p.state.clone(), input }));
            assert!(matches!(node.run(resumed).await.unwrap(), NodeOutcome::Paused(_)));
        }
        assert_eq!(s.tools.calls.lock().unwrap().len(), 1);

        let input = Value::from_json(json!({"approvals": {"c2": true}}));
        let out = completed(node.run(ctx(&s).with_resume(Some(Resume { state: p.state, input: Some(input) }))).await);
        assert_eq!(out.outputs["text"], Value::from("done"));
        assert_eq!(s.tools.calls.lock().unwrap().as_slice(), ["add", "fail"]);
    }

    #[tokio::test]
    async fn per_call_decisions_can_arrive_in_steps() {
        let s = setup(vec![
            MockModel::tool_calls(&[("c1", "add", r#"{"a":1,"b":1}"#), ("c2", "add", r#"{"a":2,"b":2}"#)]),
            MockModel::text("ok"),
        ]);
        let node = agent(json!({"require_approval": ["*"], "parallel_tool_calls": false}));
        let NodeOutcome::Paused(p) = node.run(ctx(&s)).await.unwrap() else { panic!("expected pause") };
        let input = Value::from_json(json!({"approvals": {"c1": true}}));
        let NodeOutcome::Paused(p2) =
            node.run(ctx(&s).with_resume(Some(Resume { state: p.state, input: Some(input) }))).await.unwrap()
        else {
            panic!("c2 is still undecided")
        };
        assert_eq!(s.tools.calls.lock().unwrap().len(), 1);
        assert_eq!(p2.data.get_path(&["calls", "0", "id"]), Some(Value::from("c2")));
        let input = Value::from_json(json!({"approved": false}));
        let out = completed(node.run(ctx(&s).with_resume(Some(Resume { state: p2.state, input: Some(input) }))).await);
        assert_eq!(out.outputs["text"], Value::from("ok"));
        assert_eq!(s.tools.calls.lock().unwrap().len(), 1, "c2 was denied");
    }

    #[test]
    fn inputs_cover_templates_and_memory() {
        let node = agent(json!({"query": "{{#start.q#}}", "instruction": "{{#env.persona#}}", "memory": {}}));
        let inputs: Vec<String> = node.inputs().iter().map(ToString::to_string).collect();
        for expected in ["start.q", "env.persona", "sys.history", "sys.query"] {
            assert!(inputs.iter().any(|i| i == expected), "missing {expected} in {inputs:?}");
        }
    }

    #[tokio::test]
    async fn unknown_provider_is_fatal() {
        let s = setup(vec![]);
        let err = agent(json!({"tools": ["nope/x"]})).run(ctx(&s)).await.unwrap_err();
        assert!(!err.retryable);
        assert_eq!(err.kind, "tool_not_found");
    }

    #[test]
    fn config_validation_and_names() {
        let bad = NodeDoc::new("a", "agent", json!({"model": {"provider": "p", "name": "m"}, "tools": ["no-slash"]}));
        assert!(AgentNode::from_doc(&bad).is_err());
        let bad = NodeDoc::new("a", "agent", json!({"model": {"provider": "p", "name": "m"}, "max_iterations": 0}));
        assert!(AgentNode::from_doc(&bad).is_err());
        let mut used = HashSet::new();
        assert_eq!(unique_name("search", "web", &mut used), "search");
        assert_eq!(unique_name("search", "docs", &mut used), "docs_search");
        assert_eq!(unique_name("search", "docs", &mut used), "docs_search_2");
        assert_eq!(unique_name("a.b/c", "p", &mut used), "a_b_c");
    }
}