reddb-io-server 1.34.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
//! ASK planner-first typed plan (ADR 0068, issue #1747).
//!
//! The planner runs *after* the deterministic AskPipeline funnel has
//! narrowed the schema slice (candidate collections + columns + scores).
//! The narrowed slice — never the raw catalog — is the only schema the
//! planner LLM ever sees. The model emits a **typed plan** whose `query`
//! step carries a read-only RQL candidate; the candidate is re-validated
//! through the production parser and the read-only classifier
//! ([`super::ask_rql_planner`]) before it can execute.
//!
//! This module is pure: it holds the plan types, the JSON plan parser, the
//! narrowed-slice prompt builder, and the routing/refusal decision. The LLM
//! transport, auto-execution, and synthesis live in the orchestrator
//! ([`super::super::impl_search`]). The [`PlannerModel`] closure seam lets
//! the routing/refusal logic be unit-tested without any HTTP round-trip.

use std::collections::BTreeSet;

use crate::api::{RedDBError, RedDBResult};
/// Typed calls and phases from the protocol-neutral RedDB tool registry.
pub use crate::runtime::agent_tools::{QueryPhase, RedToolCall as PlannerToolCall};

use super::ask_rql_planner::{validate_candidate, CandidateDisposition, ValidatedCandidate};

pub const ASK_PLANNER_SYSTEM_PROMPT: &str = include_str!("prompts/ask_planner.txt");

/// The intent the planner routes a question to (ADR 0068 §2).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AskIntent {
    /// "what happened to passport 123?" — generate read-only RQL over the
    /// full read-only surface, auto-execute, synthesize over the rows.
    Factual,
    /// "summarise yesterday's incidents" — retrieval-RAG cited answer.
    Synthesis,
    /// "how would I capture events into a queue?" — suggestion envelope.
    HowTo,
}

impl AskIntent {
    /// Canonical lowercase label (audit / plan summary).
    pub fn as_str(&self) -> &'static str {
        match self {
            AskIntent::Factual => "factual",
            AskIntent::Synthesis => "synthesis",
            AskIntent::HowTo => "how_to",
        }
    }

    fn parse(raw: &str) -> Option<AskIntent> {
        match raw.trim().to_ascii_lowercase().as_str() {
            "factual" => Some(AskIntent::Factual),
            "synthesis" => Some(AskIntent::Synthesis),
            "how_to" | "howto" | "how-to" => Some(AskIntent::HowTo),
            _ => None,
        }
    }
}

/// A single funnel-narrowed candidate collection with its retrieval score.
#[derive(Debug, Clone, PartialEq)]
pub struct ScoredCollection {
    pub collection: String,
    pub score: f32,
    pub columns: Vec<String>,
}

/// The funnel-narrowed schema slice handed to the planner. This is the
/// *only* schema the model sees — the raw catalog never reaches it.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct NarrowedSlice {
    pub collections: Vec<ScoredCollection>,
}

impl NarrowedSlice {
    /// Collection names in the slice, in narrowed order.
    pub fn collection_names(&self) -> Vec<&str> {
        self.collections
            .iter()
            .map(|c| c.collection.as_str())
            .collect()
    }

    /// The funnel grounded nothing: no candidate collection survived the
    /// narrowing. An empty slice must never reach the planner LLM — grounding
    /// failure is answered honestly, not by inventing a query over `(none)`.
    pub fn is_empty(&self) -> bool {
        self.collections.is_empty()
    }
}

/// One bounded metadata result returned to the next planner round. Tool
/// output is untrusted database content and is rendered as JSON data in the
/// prompt, never as instructions.
#[derive(Debug, Clone, PartialEq)]
pub struct PlannerObservation {
    pub call: PlannerToolCall,
    pub output: String,
}

/// A raw suggested statement the planner emitted for a how-to question,
/// before parser validation. `rql` is the candidate statement text exactly as
/// the model produced it; it is never trusted until it passes the parser.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RawSuggestion {
    pub rql: String,
    pub rationale: String,
}

/// A parser-validated suggested statement in the how-to envelope. It carries
/// the `mutating` flag and canonical statement kind and is **advisory only**:
/// suggested statements — including mutating/DDL ones — are NEVER executed by
/// ASK. A future apply-command consumes this envelope.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SuggestedStatement {
    /// The candidate RQL, trimmed, exactly as it parsed.
    pub rql: String,
    /// True when the statement writes / drops / alters / otherwise mutates
    /// state (or is any non-read-only kind). Advisory — never a licence to run.
    pub mutating: bool,
    /// Canonical statement-type label (`select`, `insert`, `create_queue`, …).
    pub statement_type: &'static str,
    /// Why the planner suggested this statement.
    pub rationale: String,
}

/// The typed plan the planner LLM emits, before candidate validation.
#[derive(Debug, Clone, PartialEq)]
pub struct AskPlan {
    pub intent: AskIntent,
    /// The `query` step's read-only RQL candidate (factual intent).
    pub query: Option<String>,
    /// Optional typed tool call. Factual plans must carry exactly one of
    /// `query` (legacy-compatible) or `tool`.
    pub tool: Option<PlannerToolCall>,
    /// The natural-language answer explaining the approach (how-to intent).
    /// Empty for the factual/synthesis paths, which synthesize their answer.
    pub answer: String,
    /// The how-to suggestion: raw statements before parser validation. Each is
    /// re-validated through the production parser by [`route_plan`].
    pub suggestion: Vec<RawSuggestion>,
    /// Model rationale / self-critique, surfaced to plan summary + audit.
    pub rationale: String,
}

impl AskPlan {
    /// A short, single-line plan summary for the audit row.
    pub fn summary(&self) -> String {
        let mut out = format!("intent={}", self.intent.as_str());
        if let Some(query) = &self.query {
            out.push_str("; query=");
            out.push_str(query.trim());
        }
        if let Some(tool) = &self.tool {
            out.push_str("; tool=");
            out.push_str(tool.name());
        }
        out
    }
}

/// Validate each suggested statement through the production parser, **dropping**
/// any that do not parse (unparseable model output is never returned raw), and
/// flag each read-only vs mutating. Mutating/DDL statements are kept in the
/// envelope — so a future apply-command can consume them — but ASK never runs
/// them; this function only classifies, it never executes.
pub fn validate_suggestions(raw: &[RawSuggestion]) -> Vec<SuggestedStatement> {
    let mut out = Vec::new();
    for item in raw {
        // A candidate that fails the production parser is dropped, never
        // returned raw. The suggestion is advisory regardless of disposition.
        if let Ok(candidate) = validate_candidate(&item.rql) {
            out.push(SuggestedStatement {
                mutating: !candidate.is_read_only(),
                statement_type: candidate.statement_type,
                rql: candidate.rql,
                rationale: item.rationale.clone(),
            });
        }
    }
    out
}

/// A model that turns a planner prompt into a typed-plan JSON document.
///
/// The blanket impl over `Fn(&str) -> RedDBResult<String>` lets callers
/// pass a closure wrapping the configured planner provider (production) or
/// a canned string (tests / mock model) without a bespoke type — this is
/// the closure-model seam the routing/refusal unit tests use.
pub trait PlannerModel {
    fn plan(&self, prompt: &str) -> RedDBResult<String>;
}

impl<F> PlannerModel for F
where
    F: Fn(&str) -> RedDBResult<String>,
{
    fn plan(&self, prompt: &str) -> RedDBResult<String> {
        self(prompt)
    }
}

/// Structured model seam used by production providers and deterministic
/// harnesses. It mirrors RedCode's model boundary: the runner owns the
/// transcript and tools, while the adapter only performs one model turn.
pub trait PlannerChatModel {
    fn chat(
        &self,
        messages: &[crate::ai::AiChatMessage],
        tools: &[crate::ai::AiToolDefinition],
    ) -> RedDBResult<crate::ai::AiChatResponse>;
}

impl<F> PlannerChatModel for F
where
    F: Fn(
        &[crate::ai::AiChatMessage],
        &[crate::ai::AiToolDefinition],
    ) -> RedDBResult<crate::ai::AiChatResponse>,
{
    fn chat(
        &self,
        messages: &[crate::ai::AiChatMessage],
        tools: &[crate::ai::AiToolDefinition],
    ) -> RedDBResult<crate::ai::AiChatResponse> {
        self(messages, tools)
    }
}

/// Typed registry shared by every ASK provider adapter.
pub fn ask_tool_definitions() -> Vec<crate::ai::AiToolDefinition> {
    crate::runtime::agent_tools::definitions(crate::runtime::agent_tools::ToolSurface::Ask)
        .into_iter()
        .map(|definition| crate::ai::AiToolDefinition {
            name: definition.name.to_string(),
            description: definition.description.to_string(),
            input_schema: definition.input_schema,
        })
        .collect()
}

/// How the planner routed a plan after candidate validation.
#[derive(Debug, Clone, PartialEq)]
pub enum PlanRouting {
    /// Factual intent with a validated **read-only** candidate ready to
    /// auto-execute under the caller's EffectiveScope.
    Execute {
        candidate: ValidatedCandidate,
        params: Vec<crate::json::Value>,
        purpose: String,
    },
    /// A bounded metadata call whose result is fed into a later planner
    /// round. The orchestrator charges the step before executing it.
    UseTool { call: PlannerToolCall },
    /// Factual intent whose candidate is **mutating** — a structured
    /// refusal. Mutating candidates are never executed under any flag; the
    /// suggestion envelope arrives in a later slice.
    RefuseMutating {
        statement_type: &'static str,
        rql: String,
    },
    /// How-to intent: an advisory suggestion envelope. `answer` explains the
    /// approach in natural language; `suggestion` carries the parser-validated
    /// statements, each flagged `mutating`, plus their rationale. Suggested
    /// statements — including mutating/DDL ones — are NEVER executed; a future
    /// apply-command consumes this envelope.
    Suggest {
        answer: String,
        suggestion: Vec<SuggestedStatement>,
    },
    /// A remaining non-factual intent (synthesis). The orchestrator decides
    /// the fallback (retrieval-RAG cited answer).
    Unsupported { intent: AskIntent },
}

/// A planner pass: the prompt sent, the parsed plan, and the routing.
#[derive(Debug, Clone, PartialEq)]
pub struct PlannedRoute {
    pub prompt: String,
    pub plan: AskPlan,
    pub routing: PlanRouting,
}

/// Assemble the planner prompt from *only* the narrowed slice. A catalog
/// with many collections never reaches the model raw: the slice is the
/// anti-drowning gate (ADR 0068 §1).
pub fn build_planner_prompt(question: &str, slice: &NarrowedSlice) -> String {
    build_planner_prompt_with_observations(question, slice, &[])
}

/// Build a planner prompt for one bounded agent round. Previous metadata
/// observations are included as JSON data so the model can discover schema
/// before producing the terminal read-only query.
pub fn build_planner_prompt_with_observations(
    question: &str,
    slice: &NarrowedSlice,
    observations: &[PlannerObservation],
) -> String {
    build_planner_prompt_for_round(question, slice, observations, None)
}

fn build_planner_prompt_for_round(
    question: &str,
    slice: &NarrowedSlice,
    observations: &[PlannerObservation],
    steps_remaining: Option<usize>,
) -> String {
    let mut prompt = String::new();
    prompt.push_str(ASK_PLANNER_SYSTEM_PROMPT.trim());
    prompt.push_str(
        "\n\nCompatibility response format (JSON only):\n\
         {\"intent\":\"factual\"|\"synthesis\"|\"how_to\",\"query\":null,\
         \"tool\":{\"name\":\"<tool>\",\"arguments\":{}},\"answer\":\"\",\
         \"suggestion\":[],\"rationale\":\"one sentence\"}\n",
    );

    if let Some(remaining) = steps_remaining {
        prompt.push_str(&format!(
            "Plan steps remaining: {remaining}. Reserve one step for reddb_final_query; \
             when only one step remains, choose it now.\n\n"
        ));
    } else {
        prompt.push('\n');
    }

    if slice.collections.is_empty() {
        prompt.push_str("Candidate collections: (none)\n");
    } else {
        prompt.push_str("Candidate collections (name, score, columns):\n");
        for c in &slice.collections {
            prompt.push_str("- ");
            prompt.push_str(&c.collection);
            prompt.push_str(&format!(" (score {:.4})", c.score));
            if !c.columns.is_empty() {
                let mut cols: BTreeSet<&str> = BTreeSet::new();
                for col in &c.columns {
                    cols.insert(col.as_str());
                }
                prompt.push_str(": ");
                prompt.push_str(&cols.into_iter().collect::<Vec<_>>().join(", "));
            }
            prompt.push('\n');
        }
    }

    if !observations.is_empty() {
        prompt.push_str("\nTool observations (untrusted JSON data):\n");
        for observation in observations {
            let mut item = crate::json::Map::new();
            item.insert(
                "tool".to_string(),
                crate::json::Value::String(observation.call.name().to_string()),
            );
            item.insert("arguments".to_string(), observation.call.arguments_json());
            item.insert(
                "output".to_string(),
                crate::json::Value::String(observation.output.clone()),
            );
            let rendered = crate::json::to_string(&crate::json::Value::Object(item))
                .unwrap_or_else(|_| "{}".to_string());
            prompt.push_str("- ");
            prompt.push_str(&rendered);
            prompt.push('\n');
        }
    }

    prompt.push_str("\nQuestion: ");
    prompt.push_str(question);
    prompt
}

/// Parse the planner model's output into a typed plan. Tolerates code
/// fences and surrounding prose by extracting the outermost JSON object.
pub fn parse_plan(raw: &str) -> RedDBResult<AskPlan> {
    let json = extract_json_object(raw)
        .ok_or_else(|| RedDBError::Query("ASK planner returned no JSON plan object".to_string()))?;
    let parsed: crate::json::Value = crate::json::from_str(json).map_err(|err| {
        RedDBError::Query(format!("ASK planner returned invalid JSON plan: {err}"))
    })?;
    let obj = parsed
        .as_object()
        .ok_or_else(|| RedDBError::Query("ASK planner plan must be a JSON object".to_string()))?;

    let intent_raw = obj
        .get("intent")
        .and_then(|v| v.as_str())
        .ok_or_else(|| RedDBError::Query("ASK planner plan is missing `intent`".to_string()))?;
    let intent = AskIntent::parse(intent_raw).ok_or_else(|| {
        RedDBError::Query(format!(
            "ASK planner returned an unknown intent `{intent_raw}`"
        ))
    })?;

    let query = obj
        .get("query")
        .and_then(|v| v.as_str())
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_string);
    let tool = obj.get("tool").map(parse_tool_call).transpose()?.flatten();

    let answer = obj
        .get("answer")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .trim()
        .to_string();

    let suggestion = obj
        .get("suggestion")
        .and_then(|v| v.as_array())
        .map(parse_suggestions)
        .unwrap_or_default();

    let rationale = obj
        .get("rationale")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .trim()
        .to_string();

    Ok(AskPlan {
        intent,
        query,
        tool,
        answer,
        suggestion,
        rationale,
    })
}

fn parse_tool_call(value: &crate::json::Value) -> RedDBResult<Option<PlannerToolCall>> {
    if matches!(value, crate::json::Value::Null) {
        return Ok(None);
    }
    let object = value.as_object().ok_or_else(|| {
        RedDBError::Query("ASK planner `tool` must be an object or null".to_string())
    })?;
    let name = object
        .get("name")
        .and_then(crate::json::Value::as_str)
        .ok_or_else(|| RedDBError::Query("ASK planner tool is missing `name`".to_string()))?;
    let arguments = object
        .get("arguments")
        .cloned()
        .unwrap_or_else(|| crate::json::Value::Object(crate::json::Map::new()));
    crate::runtime::agent_tools::parse_call(
        name,
        &arguments,
        crate::runtime::agent_tools::ToolParseMode::AskLegacy,
    )
    .map(Some)
}

fn parse_native_tool_call(call: &crate::ai::AiToolCall) -> RedDBResult<PlannerToolCall> {
    crate::runtime::agent_tools::parse_call(
        &call.name,
        &call.arguments,
        crate::runtime::agent_tools::ToolParseMode::AskNative,
    )
}

/// Read the `suggestion` array of a how-to plan into raw statements. Each item
/// may be a bare RQL string or a `{ "rql": "...", "rationale": "..." }` object;
/// items without any statement text are skipped. Parser validation happens
/// later in [`validate_suggestions`], not here.
fn parse_suggestions(items: &[crate::json::Value]) -> Vec<RawSuggestion> {
    let mut out = Vec::new();
    for item in items {
        if let Some(rql) = item.as_str() {
            let rql = rql.trim();
            if !rql.is_empty() {
                out.push(RawSuggestion {
                    rql: rql.to_string(),
                    rationale: String::new(),
                });
            }
        } else if let Some(obj) = item.as_object() {
            let rql = obj
                .get("rql")
                .or_else(|| obj.get("statement"))
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .trim()
                .to_string();
            if rql.is_empty() {
                continue;
            }
            let rationale = obj
                .get("rationale")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .trim()
                .to_string();
            out.push(RawSuggestion { rql, rationale });
        }
    }
    out
}

/// Route a parsed plan: validate the factual candidate through the parser +
/// read-only classifier, refuse mutating candidates, and mark non-factual
/// intents unsupported for this slice.
pub fn route_plan(plan: &AskPlan) -> RedDBResult<PlanRouting> {
    match plan.intent {
        AskIntent::Factual => match (&plan.query, &plan.tool) {
            (Some(_), Some(_)) => Err(RedDBError::Query(
                "ASK planner factual plan must choose exactly one of `query` or `tool`".to_string(),
            )),
            (Some(rql), None) => {
                route_query_candidate(rql, &[], &plan.rationale, QueryPhase::Final)
            }
            (
                None,
                Some(PlannerToolCall::Query {
                    rql,
                    params,
                    purpose,
                    phase,
                }),
            ) => route_query_candidate(rql, params, purpose, *phase),
            (None, Some(call)) => Ok(PlanRouting::UseTool { call: call.clone() }),
            (None, None) => Err(RedDBError::Query(
                "ASK planner classified the question as factual but produced no query or \
                     tool call"
                    .to_string(),
            )),
        },
        AskIntent::HowTo => {
            // Parser-validate every suggested statement; unparseable ones are
            // dropped. Mutating/DDL statements survive in the envelope but are
            // never executed — the suggestion is advisory.
            let suggestion = validate_suggestions(&plan.suggestion);
            Ok(PlanRouting::Suggest {
                answer: plan.answer.clone(),
                suggestion,
            })
        }
        other => Ok(PlanRouting::Unsupported { intent: other }),
    }
}

fn route_query_candidate(
    rql: &str,
    params: &[crate::json::Value],
    purpose: &str,
    phase: QueryPhase,
) -> RedDBResult<PlanRouting> {
    // Re-validate through the production parser; malformed candidates are
    // rejected here and never execute.
    let candidate = validate_candidate(rql)?;
    match candidate.disposition {
        CandidateDisposition::ReadOnly if phase == QueryPhase::Explore => {
            Ok(PlanRouting::UseTool {
                call: PlannerToolCall::Query {
                    rql: candidate.rql,
                    params: params.to_vec(),
                    purpose: purpose.to_string(),
                    phase,
                },
            })
        }
        CandidateDisposition::ReadOnly => Ok(PlanRouting::Execute {
            candidate,
            params: params.to_vec(),
            purpose: purpose.to_string(),
        }),
        CandidateDisposition::Mutating => Ok(PlanRouting::RefuseMutating {
            statement_type: candidate.statement_type,
            rql: candidate.rql,
        }),
    }
}

/// Full planner pass: build the narrowed-slice prompt, call the model,
/// parse the plan, and route it. The model is the closure-model seam.
pub fn plan_and_route<M: PlannerModel>(
    question: &str,
    slice: &NarrowedSlice,
    model: &M,
) -> RedDBResult<PlannedRoute> {
    plan_and_route_with_observations(question, slice, &[], model)
}

/// One agent-loop planner round over the narrowed schema plus prior bounded
/// metadata observations.
pub fn plan_and_route_with_observations<M: PlannerModel>(
    question: &str,
    slice: &NarrowedSlice,
    observations: &[PlannerObservation],
    model: &M,
) -> RedDBResult<PlannedRoute> {
    let prompt = build_planner_prompt_with_observations(question, slice, observations);
    let raw = model.plan(&prompt)?;
    let plan = parse_plan(&raw)?;
    let routing = route_plan(&plan)?;
    Ok(PlannedRoute {
        prompt,
        plan,
        routing,
    })
}

#[derive(Debug, Clone, PartialEq)]
pub enum AgentLoopOutcome {
    Routed {
        route: Box<PlannedRoute>,
        observations: Vec<PlannerObservation>,
    },
    BudgetExhausted(BudgetExhausted),
}

/// Run the bounded metadata-tool loop until the planner returns a terminal
/// route or the shared step budget is exhausted. A terminal query is charged
/// here too, which keeps planner rounds and executable steps linear.
pub fn run_bounded_agent_loop<M, E>(
    question: &str,
    slice: &NarrowedSlice,
    budget: &mut PlanBudget,
    model: &M,
    executor: &E,
) -> RedDBResult<AgentLoopOutcome>
where
    M: PlannerModel,
    E: Fn(&PlannerToolCall) -> RedDBResult<String>,
{
    let mut observations = Vec::new();
    loop {
        if budget.remaining() == 0 {
            return Ok(AgentLoopOutcome::BudgetExhausted(BudgetExhausted {
                attempted: PlanStep::Query,
                executed_steps: budget.executed_count(),
                max_steps: budget.max_steps(),
            }));
        }

        let prompt = build_planner_prompt_for_round(
            question,
            slice,
            &observations,
            Some(budget.remaining()),
        );
        let raw = model.plan(&prompt)?;
        let plan = parse_plan(&raw)?;
        let route = PlannedRoute {
            prompt,
            routing: route_plan(&plan)?,
            plan,
        };
        match &route.routing {
            PlanRouting::UseTool { call } => {
                // Metadata and exploratory reads must not consume the final
                // reserved step. This guarantees that exploration can never
                // leave ASK without an evidence-producing final query.
                if budget.remaining() <= 1 {
                    return Ok(AgentLoopOutcome::BudgetExhausted(BudgetExhausted {
                        attempted: planner_tool_step(call),
                        executed_steps: budget.executed_count(),
                        max_steps: budget.max_steps(),
                    }));
                }
                if let Err(exhausted) = budget.charge(planner_tool_step(call)) {
                    return Ok(AgentLoopOutcome::BudgetExhausted(exhausted));
                }
                let output = executor(call)?;
                observations.push(PlannerObservation {
                    call: call.clone(),
                    output,
                });
            }
            PlanRouting::Execute { .. } => {
                if let Err(exhausted) = budget.charge(PlanStep::Query) {
                    return Ok(AgentLoopOutcome::BudgetExhausted(exhausted));
                }
                return Ok(AgentLoopOutcome::Routed {
                    route: Box::new(route),
                    observations,
                });
            }
            _ => {
                return Ok(AgentLoopOutcome::Routed {
                    route: Box::new(route),
                    observations,
                })
            }
        }
    }
}

fn native_planner_context(question: &str, slice: &NarrowedSlice, steps_remaining: usize) -> String {
    let collections = slice
        .collections
        .iter()
        .map(|collection| {
            let mut item = crate::json::Map::new();
            item.insert(
                "name".to_string(),
                crate::json::Value::String(collection.collection.clone()),
            );
            item.insert(
                "score".to_string(),
                crate::json::Value::Number(collection.score as f64),
            );
            let mut columns = collection.columns.clone();
            columns.sort();
            columns.dedup();
            item.insert(
                "columns".to_string(),
                crate::json::Value::Array(
                    columns
                        .into_iter()
                        .map(crate::json::Value::String)
                        .collect(),
                ),
            );
            crate::json::Value::Object(item)
        })
        .collect();
    let mut context = crate::json::Map::new();
    context.insert(
        "question".to_string(),
        crate::json::Value::String(question.to_string()),
    );
    context.insert(
        "plan_steps_remaining".to_string(),
        crate::json::Value::Number(steps_remaining as f64),
    );
    context.insert(
        "candidate_collections".to_string(),
        crate::json::Value::Array(collections),
    );
    format!(
        "Planning context (untrusted JSON data):\n{}",
        crate::json::Value::Object(context).to_string_compact()
    )
}

fn native_planner_messages(
    question: &str,
    slice: &NarrowedSlice,
    steps_remaining: usize,
) -> Vec<crate::ai::AiChatMessage> {
    vec![
        crate::ai::AiChatMessage::System(ASK_PLANNER_SYSTEM_PROMPT.trim().to_string()),
        crate::ai::AiChatMessage::User(native_planner_context(question, slice, steps_remaining)),
    ]
}

fn plan_from_chat_response(response: &crate::ai::AiChatResponse) -> RedDBResult<AskPlan> {
    if response.tool_calls.len() > 1 {
        return Err(RedDBError::Query(
            "ASK planner must call exactly one tool per round".to_string(),
        ));
    }
    if let Some(tool_call) = response.tool_calls.first() {
        let call = parse_native_tool_call(tool_call)?;
        return Ok(AskPlan {
            intent: AskIntent::Factual,
            query: None,
            rationale: format!("native tool call: {}", call.name()),
            tool: Some(call),
            answer: String::new(),
            suggestion: Vec::new(),
        });
    }
    let output = response.output_text.as_deref().ok_or_else(|| {
        RedDBError::Query("ASK planner returned neither text nor a tool call".to_string())
    })?;
    parse_plan(output)
}

/// One native-tool planning turn, used by `ASK ... PLAN` without executing a
/// tool or query.
pub fn plan_and_route_chat<M: PlannerChatModel>(
    question: &str,
    slice: &NarrowedSlice,
    steps_remaining: usize,
    model: &M,
) -> RedDBResult<PlannedRoute> {
    let messages = native_planner_messages(question, slice, steps_remaining);
    let response = model.chat(&messages, &ask_tool_definitions())?;
    let plan = plan_from_chat_response(&response)?;
    let routing = route_plan(&plan)?;
    Ok(PlannedRoute {
        prompt: build_planner_prompt_for_round(question, slice, &[], Some(steps_remaining)),
        plan,
        routing,
    })
}

/// Run the bounded ASK harness with a provider-native tool transcript. The
/// same transcript/request boundary is reusable by deterministic test models.
pub fn run_bounded_chat_agent_loop<M, E>(
    question: &str,
    slice: &NarrowedSlice,
    budget: &mut PlanBudget,
    model: &M,
    executor: &E,
) -> RedDBResult<AgentLoopOutcome>
where
    M: PlannerChatModel,
    E: Fn(&PlannerToolCall) -> RedDBResult<String>,
{
    let tools = ask_tool_definitions();
    let mut messages = native_planner_messages(question, slice, budget.remaining());
    let mut observations = Vec::new();
    loop {
        if budget.remaining() == 0 {
            return Ok(AgentLoopOutcome::BudgetExhausted(BudgetExhausted {
                attempted: PlanStep::Query,
                executed_steps: budget.executed_count(),
                max_steps: budget.max_steps(),
            }));
        }

        let prompt = build_planner_prompt_for_round(
            question,
            slice,
            &observations,
            Some(budget.remaining()),
        );
        let response = model.chat(&messages, &tools)?;
        let plan = plan_from_chat_response(&response)?;
        let route = PlannedRoute {
            prompt,
            routing: route_plan(&plan)?,
            plan,
        };

        match &route.routing {
            PlanRouting::UseTool { call } => {
                if budget.remaining() <= 1 {
                    return Ok(AgentLoopOutcome::BudgetExhausted(BudgetExhausted {
                        attempted: planner_tool_step(call),
                        executed_steps: budget.executed_count(),
                        max_steps: budget.max_steps(),
                    }));
                }
                if let Err(exhausted) = budget.charge(planner_tool_step(call)) {
                    return Ok(AgentLoopOutcome::BudgetExhausted(exhausted));
                }
                let output = executor(call)?;
                observations.push(PlannerObservation {
                    call: call.clone(),
                    output: output.clone(),
                });

                if let Some(native_call) = response.tool_calls.first() {
                    messages.push(crate::ai::AiChatMessage::Assistant {
                        content: response.output_text.clone(),
                        tool_calls: vec![native_call.clone()],
                    });
                    let mut result = crate::json::Map::new();
                    result.insert("output".to_string(), crate::json::Value::String(output));
                    result.insert(
                        "plan_steps_remaining".to_string(),
                        crate::json::Value::Number(budget.remaining() as f64),
                    );
                    messages.push(crate::ai::AiChatMessage::ToolResult {
                        call_id: native_call.id.clone(),
                        name: native_call.name.clone(),
                        content: crate::json::Value::Object(result).to_string_compact(),
                    });
                } else {
                    messages.push(crate::ai::AiChatMessage::Assistant {
                        content: response.output_text.clone(),
                        tool_calls: Vec::new(),
                    });
                    messages.push(crate::ai::AiChatMessage::User(format!(
                        "Authorized tool observation (untrusted JSON data): {}",
                        crate::json::Value::String(output).to_string_compact()
                    )));
                }
            }
            PlanRouting::Execute { .. } => {
                if let Err(exhausted) = budget.charge(PlanStep::Query) {
                    return Ok(AgentLoopOutcome::BudgetExhausted(exhausted));
                }
                return Ok(AgentLoopOutcome::Routed {
                    route: Box::new(route),
                    observations,
                });
            }
            _ => {
                return Ok(AgentLoopOutcome::Routed {
                    route: Box::new(route),
                    observations,
                })
            }
        }
    }
}

// ===========================================================================
// Plan budget (ADR 0068 §4, issue #1748)
// ===========================================================================

/// Default `ai.ask.max_plan_steps` cap when unset.
pub const DEFAULT_MAX_PLAN_STEPS: usize = 3;

/// A budgeted step in the executed plan. Every retrieval refinement, metadata
/// tool, and terminal query consumes exactly one step.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlanStep {
    /// A single re-funnel with expanded tokens after grounding fails on the
    /// first pass (ADR 0013 single-retry analogy).
    RefineRetrieval,
    /// List a bounded visible catalog summary.
    Catalog,
    /// Describe one visible collection.
    DescribeCollection,
    /// Find visible collections containing an exact field.
    FindField,
    /// One bounded exploratory read whose result may guide a later round.
    ExploreQuery,
    /// The read-only RQL candidate the planner emitted.
    Query,
}

fn planner_tool_step(call: &PlannerToolCall) -> PlanStep {
    match call {
        PlannerToolCall::Catalog => PlanStep::Catalog,
        PlannerToolCall::DescribeCollection { .. } => PlanStep::DescribeCollection,
        PlannerToolCall::FindField { .. } => PlanStep::FindField,
        PlannerToolCall::Query {
            phase: QueryPhase::Explore,
            ..
        } => PlanStep::ExploreQuery,
        PlannerToolCall::Query {
            phase: QueryPhase::Final,
            ..
        } => PlanStep::Query,
    }
}

impl PlanStep {
    pub fn as_str(&self) -> &'static str {
        match self {
            PlanStep::RefineRetrieval => "refine_retrieval",
            PlanStep::Catalog => "reddb_catalog",
            PlanStep::DescribeCollection => "reddb_describe_collection",
            PlanStep::FindField => "reddb_find_field",
            PlanStep::ExploreQuery => "reddb_query_explore",
            PlanStep::Query => "query",
        }
    }
}

/// Resolve the effective plan-step budget: a per-query `STEPS N` request is
/// clamped to the configured `max_plan_steps` cap and never exceeds it; an
/// absent request falls back to the cap. The result is always at least 1.
pub fn clamp_plan_steps(requested: Option<usize>, cap: usize) -> usize {
    let cap = cap.max(1);
    match requested {
        Some(n) => n.clamp(1, cap),
        None => cap,
    }
}

/// The budget was exhausted mid-plan: a step was attempted after the cap was
/// already reached. The orchestrator turns this into a structured
/// partial-with-warning result rather than looping unbounded.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BudgetExhausted {
    pub attempted: PlanStep,
    pub executed_steps: usize,
    pub max_steps: usize,
}

/// A bounded plan-step counter. Total executed steps can never exceed
/// `max_steps` — the anti-unbounded-loop guard (ADR 0068 §4).
#[derive(Debug, Clone)]
pub struct PlanBudget {
    max_steps: usize,
    executed: Vec<PlanStep>,
}

impl PlanBudget {
    /// Build a budget from a per-query `STEPS N` request and the config cap.
    /// The request is clamped so it can never exceed the cap.
    pub fn new(requested_steps: Option<usize>, cap: usize) -> Self {
        Self {
            max_steps: clamp_plan_steps(requested_steps, cap),
            executed: Vec::new(),
        }
    }

    /// The clamped ceiling on total executed steps.
    pub fn max_steps(&self) -> usize {
        self.max_steps
    }

    /// How many steps have executed so far.
    pub fn executed_count(&self) -> usize {
        self.executed.len()
    }

    /// Steps still available before the budget is exhausted.
    pub fn remaining(&self) -> usize {
        self.max_steps.saturating_sub(self.executed.len())
    }

    /// The executed steps, in order — surfaced to the audit row.
    pub fn executed_steps(&self) -> &[PlanStep] {
        &self.executed
    }

    /// Charge one plan step. Returns `Err(BudgetExhausted)` — without
    /// recording the step — when the cap is already reached.
    pub fn charge(&mut self, step: PlanStep) -> Result<(), BudgetExhausted> {
        if self.executed.len() >= self.max_steps {
            return Err(BudgetExhausted {
                attempted: step,
                executed_steps: self.executed.len(),
                max_steps: self.max_steps,
            });
        }
        self.executed.push(step);
        Ok(())
    }
}

// ===========================================================================
// Grounding critique + single refine_retrieval retry (ADR 0068 §4)
// ===========================================================================

/// The funnel behind a closure seam so the retry/refusal logic is unit-tested
/// without a live retrieval pass. `expanded` requests the refine_retrieval
/// widening (expanded tokens, relaxed score floor) on the single retry.
pub trait RetrievalFunnel {
    fn funnel(&self, expanded: bool) -> RedDBResult<NarrowedSlice>;
}

impl<F> RetrievalFunnel for F
where
    F: Fn(bool) -> RedDBResult<NarrowedSlice>,
{
    fn funnel(&self, expanded: bool) -> RedDBResult<NarrowedSlice> {
        self(expanded)
    }
}

/// The outcome of the grounding critique: either a grounded slice (possibly
/// after the single refine_retrieval retry) or an honest "no matching
/// sources" — never an invented answer.
#[derive(Debug, Clone, PartialEq)]
pub enum GroundingOutcome {
    /// A slice that grounds. `refined` is true when the single
    /// refine_retrieval retry produced it.
    Grounded { slice: NarrowedSlice, refined: bool },
    /// Both the first funnel pass and the single refine_retrieval retry
    /// grounded nothing. ASK answers honestly instead of inventing.
    NoMatchingSources,
}

/// Fold the self-critique into grounding: run the funnel; if it grounds
/// nothing, re-funnel **exactly once** with expanded tokens (mirroring the
/// single citation retry of ADR 0013); if that still grounds nothing, report
/// `NoMatchingSources` so ASK answers honestly rather than inventing.
pub fn ground_with_refine<F: RetrievalFunnel>(funnel: &F) -> RedDBResult<GroundingOutcome> {
    let first = funnel.funnel(false)?;
    if !first.is_empty() {
        return Ok(GroundingOutcome::Grounded {
            slice: first,
            refined: false,
        });
    }
    // Exactly one refine_retrieval re-funnel with expanded tokens.
    let refined = funnel.funnel(true)?;
    if !refined.is_empty() {
        return Ok(GroundingOutcome::Grounded {
            slice: refined,
            refined: true,
        });
    }
    Ok(GroundingOutcome::NoMatchingSources)
}

/// Extract the outermost `{...}` JSON object from a model response that may
/// carry code fences or surrounding prose.
fn extract_json_object(raw: &str) -> Option<&str> {
    let start = raw.find('{')?;
    let end = raw.rfind('}')?;
    if end <= start {
        return None;
    }
    Some(&raw[start..=end])
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;
    use std::collections::VecDeque;

    use super::*;

    fn slice() -> NarrowedSlice {
        NarrowedSlice {
            collections: vec![
                ScoredCollection {
                    collection: "travelers".to_string(),
                    score: 0.91,
                    columns: vec!["passport".to_string(), "name".to_string()],
                },
                ScoredCollection {
                    collection: "trips".to_string(),
                    score: 0.44,
                    columns: vec!["passport".to_string(), "city".to_string()],
                },
            ],
        }
    }

    /// A mock planner model that returns a fixed plan JSON regardless of the
    /// prompt — stands in for the configured planner provider.
    fn mock_model(plan_json: &'static str) -> impl PlannerModel {
        move |_prompt: &str| Ok(plan_json.to_string())
    }

    struct TestChatModel {
        responses: RefCell<VecDeque<crate::ai::AiChatResponse>>,
        requests: RefCell<Vec<(Vec<crate::ai::AiChatMessage>, Vec<String>)>>,
    }

    impl TestChatModel {
        fn new(responses: Vec<crate::ai::AiChatResponse>) -> Self {
            Self {
                responses: RefCell::new(responses.into()),
                requests: RefCell::new(Vec::new()),
            }
        }
    }

    impl PlannerChatModel for TestChatModel {
        fn chat(
            &self,
            messages: &[crate::ai::AiChatMessage],
            tools: &[crate::ai::AiToolDefinition],
        ) -> RedDBResult<crate::ai::AiChatResponse> {
            self.requests.borrow_mut().push((
                messages.to_vec(),
                tools.iter().map(|tool| tool.name.clone()).collect(),
            ));
            self.responses
                .borrow_mut()
                .pop_front()
                .ok_or_else(|| RedDBError::Internal("test model script exhausted".to_string()))
        }
    }

    fn tool_response(
        id: &str,
        name: &str,
        arguments: Vec<(&str, crate::json::Value)>,
    ) -> crate::ai::AiChatResponse {
        let mut object = crate::json::Map::new();
        for (key, value) in arguments {
            object.insert(key.to_string(), value);
        }
        crate::ai::AiChatResponse {
            provider: "test",
            model: "test-model".to_string(),
            output_text: None,
            tool_calls: vec![crate::ai::AiToolCall {
                id: id.to_string(),
                name: name.to_string(),
                arguments: crate::json::Value::Object(object),
            }],
            prompt_tokens: None,
            completion_tokens: None,
            total_tokens: None,
            stop_reason: Some("tool_use".to_string()),
        }
    }

    #[test]
    fn prompt_contains_only_narrowed_slice() {
        let prompt = build_planner_prompt("who owns passport FDD-1?", &slice());
        assert!(prompt.contains("travelers"));
        assert!(prompt.contains("trips"));
        assert!(prompt.contains("passport"));
        // A collection outside the narrowed slice must never appear.
        assert!(!prompt.contains("secret_ledger"));
    }

    #[test]
    fn native_harness_keeps_tool_transcript_and_requires_final_query() {
        let model = TestChatModel::new(vec![
            tool_response(
                "call_1",
                "reddb_find_field",
                vec![("field", crate::json::Value::String("passport".to_string()))],
            ),
            tool_response(
                "call_2",
                "reddb_final_query",
                vec![
                    (
                        "sql",
                        crate::json::Value::String(
                            "SELECT name FROM travelers WHERE passport = $1".to_string(),
                        ),
                    ),
                    (
                        "params",
                        crate::json::Value::Array(vec![crate::json::Value::String(
                            "FDD-1".to_string(),
                        )]),
                    ),
                    (
                        "purpose",
                        crate::json::Value::String("answer the passport lookup".to_string()),
                    ),
                ],
            ),
        ]);
        let mut budget = PlanBudget::new(Some(3), 3);
        let executor = |call: &PlannerToolCall| {
            assert!(matches!(call, PlannerToolCall::FindField { .. }));
            Ok("[{\"collection\":\"travelers\",\"name\":\"passport\"}]".to_string())
        };

        let outcome = run_bounded_chat_agent_loop(
            "who owns passport FDD-1?",
            &slice(),
            &mut budget,
            &model,
            &executor,
        )
        .expect("native harness");
        let AgentLoopOutcome::Routed {
            route,
            observations,
        } = outcome
        else {
            panic!("expected routed outcome")
        };
        assert_eq!(observations.len(), 1);
        assert_eq!(
            budget.executed_steps(),
            &[PlanStep::FindField, PlanStep::Query]
        );
        assert!(matches!(route.routing, PlanRouting::Execute { .. }));

        let requests = model.requests.borrow();
        assert_eq!(requests.len(), 2);
        assert!(requests[0].1.iter().any(|name| name == "reddb_final_query"));
        assert!(requests[1]
            .0
            .iter()
            .any(|message| matches!(message, crate::ai::AiChatMessage::ToolResult { call_id, .. } if call_id == "call_1")));
    }

    #[test]
    fn native_query_tool_cannot_smuggle_a_final_phase() {
        let call = crate::ai::AiToolCall {
            id: "call_1".to_string(),
            name: "reddb_query".to_string(),
            arguments: {
                let mut arguments = crate::json::Map::new();
                arguments.insert(
                    "sql".to_string(),
                    crate::json::Value::String("SELECT 1".to_string()),
                );
                arguments.insert("params".to_string(), crate::json::Value::Array(Vec::new()));
                arguments.insert(
                    "purpose".to_string(),
                    crate::json::Value::String("inspect".to_string()),
                );
                arguments.insert(
                    "phase".to_string(),
                    crate::json::Value::String("final".to_string()),
                );
                crate::json::Value::Object(arguments)
            },
        };

        assert!(matches!(
            parse_native_tool_call(&call).expect("native call"),
            PlannerToolCall::Query {
                phase: QueryPhase::Explore,
                ..
            }
        ));
    }

    #[test]
    fn parse_plan_reads_intent_and_query() {
        let plan = parse_plan(
            "{\"intent\":\"factual\",\"query\":\"SELECT * FROM travelers WHERE passport = 'FDD-1'\",\"rationale\":\"lookup\"}",
        )
        .unwrap();
        assert_eq!(plan.intent, AskIntent::Factual);
        assert_eq!(
            plan.query.as_deref(),
            Some("SELECT * FROM travelers WHERE passport = 'FDD-1'")
        );
        assert_eq!(plan.rationale, "lookup");
    }

    #[test]
    fn metadata_tool_call_routes_to_a_bounded_agent_step() {
        let route = plan_and_route(
            "which collections contain passport_number?",
            &slice(),
            &mock_model(
                "{\"intent\":\"factual\",\"query\":null,\"tool\":{\"name\":\"reddb_find_field\",\"arguments\":{\"field\":\"passport_number\"}},\"rationale\":\"discover schema\"}",
            ),
        )
        .expect("metadata tool plan must route");

        assert_eq!(
            route.routing,
            PlanRouting::UseTool {
                call: PlannerToolCall::FindField {
                    field: "passport_number".to_string()
                }
            }
        );
    }

    #[test]
    fn query_tool_call_uses_the_same_read_only_validator() {
        let route = plan_and_route(
            "who owns passport FDD-1?",
            &slice(),
            &mock_model(
                "{\"intent\":\"factual\",\"query\":null,\"tool\":{\"name\":\"reddb_query\",\"arguments\":{\"sql\":\"SELECT * FROM travelers WHERE passport = $1\",\"params\":[\"FDD-1\"],\"purpose\":\"find the traveler\",\"phase\":\"final\"}}}",
            ),
        )
        .expect("read-only query tool must route");

        match route.routing {
            PlanRouting::Execute {
                params, purpose, ..
            } => {
                assert_eq!(
                    params,
                    vec![crate::json::Value::String("FDD-1".to_string())]
                );
                assert_eq!(purpose, "find the traveler");
            }
            other => panic!("expected final query, got {other:?}"),
        }
    }

    #[test]
    fn exploratory_query_cannot_bypass_the_mutation_refusal() {
        let route = plan_and_route(
            "remove FDD-1",
            &slice(),
            &mock_model(
                "{\"intent\":\"factual\",\"tool\":{\"name\":\"reddb_query\",\"arguments\":{\"sql\":\"DELETE FROM travelers WHERE passport = $1\",\"params\":[\"FDD-1\"],\"purpose\":\"remove a row\",\"phase\":\"explore\"}}}",
            ),
        )
        .expect("mutating tool calls become structured refusals");

        assert!(matches!(
            route.routing,
            PlanRouting::RefuseMutating {
                statement_type: "delete",
                ..
            }
        ));
    }

    #[test]
    fn query_tool_rejects_unbounded_or_invalid_params() {
        let not_an_array = parse_plan(
            "{\"intent\":\"factual\",\"tool\":{\"name\":\"reddb_query\",\"arguments\":{\"sql\":\"SELECT 1\",\"params\":\"secret\",\"phase\":\"final\"}}}",
        )
        .expect_err("params must be an array");
        assert!(not_an_array.to_string().contains("must be an array"));

        let params = vec![
            crate::json::Value::Null;
            crate::runtime::agent_tools::ASK_QUERY_PARAMETER_COUNT_MAX + 1
        ];
        let mut arguments = crate::json::Map::new();
        arguments.insert(
            "sql".to_string(),
            crate::json::Value::String("SELECT 1".to_string()),
        );
        arguments.insert("params".to_string(), crate::json::Value::Array(params));
        let mut tool = crate::json::Map::new();
        tool.insert(
            "name".to_string(),
            crate::json::Value::String("reddb_query".to_string()),
        );
        tool.insert(
            "arguments".to_string(),
            crate::json::Value::Object(arguments),
        );
        let mut root = crate::json::Map::new();
        root.insert(
            "intent".to_string(),
            crate::json::Value::String("factual".to_string()),
        );
        root.insert("tool".to_string(), crate::json::Value::Object(tool));
        let raw = crate::json::to_string(&crate::json::Value::Object(root)).unwrap();
        let oversized = parse_plan(&raw).expect_err("parameter count must be bounded");
        assert!(oversized.to_string().contains("exceeds 64 values"));
    }

    #[test]
    fn planner_rejects_unknown_tools_and_ambiguous_actions() {
        let unknown = parse_plan(
            "{\"intent\":\"factual\",\"tool\":{\"name\":\"reddb_vault_reveal\",\"arguments\":{}}}",
        )
        .expect_err("unknown tool must be rejected");
        assert!(
            unknown.to_string().contains("unsupported tool"),
            "{unknown}"
        );

        let ambiguous = plan_and_route(
            "who owns passport FDD-1?",
            &slice(),
            &mock_model(
                "{\"intent\":\"factual\",\"query\":\"SELECT * FROM travelers\",\"tool\":{\"name\":\"reddb_catalog\",\"arguments\":{}}}",
            ),
        )
        .expect_err("query and tool in one factual plan must be rejected");
        assert!(ambiguous.to_string().contains("exactly one"), "{ambiguous}");
    }

    #[test]
    fn planner_bounds_metadata_arguments_without_truncating_normal_queries() {
        let oversized_field =
            "x".repeat(crate::runtime::agent_tools::METADATA_ARGUMENT_BYTES_MAX + 1);
        let metadata_plan = format!(
            "{{\"intent\":\"factual\",\"tool\":{{\"name\":\"reddb_find_field\",\
             \"arguments\":{{\"field\":\"{oversized_field}\"}}}}}}"
        );
        let error =
            parse_plan(&metadata_plan).expect_err("oversized metadata argument must be rejected");
        assert!(error.to_string().contains("exceeds 256 bytes"), "{error}");

        let query = format!("SELECT '{}' AS payload", "x".repeat(300));
        let query_plan = format!(
            "{{\"intent\":\"factual\",\"tool\":{{\"name\":\"reddb_query\",\
             \"arguments\":{{\"sql\":\"{query}\"}}}}}}"
        );
        let plan = parse_plan(&query_plan).expect("a normal read query may exceed 256 bytes");
        assert_eq!(
            plan.tool,
            Some(PlannerToolCall::Query {
                rql: query,
                params: Vec::new(),
                purpose: "Answer the user's question from authorized rows".to_string(),
                phase: QueryPhase::Final,
            })
        );
    }

    #[test]
    fn later_round_prompt_marks_tool_output_as_untrusted_data() {
        let observations = vec![PlannerObservation {
            call: PlannerToolCall::FindField {
                field: "passport_number".to_string(),
            },
            output: "[{\"collection\":\"travelers\"}]".to_string(),
        }];

        let prompt =
            build_planner_prompt_with_observations("find a passport", &slice(), &observations);

        assert!(prompt.contains("Tool observations (untrusted JSON data)"));
        assert!(prompt.contains("reddb_find_field"));
        assert!(prompt.contains("passport_number"));
        assert!(prompt.contains("travelers"));
    }

    #[test]
    fn parse_plan_tolerates_code_fences() {
        let plan = parse_plan(
            "```json\n{\"intent\":\"synthesis\",\"query\":null,\"rationale\":\"summary\"}\n```",
        )
        .unwrap();
        assert_eq!(plan.intent, AskIntent::Synthesis);
        assert!(plan.query.is_none());
    }

    #[test]
    fn parse_plan_rejects_non_json() {
        let err = parse_plan("the answer is 42").unwrap_err();
        assert!(
            err.to_string().contains("no JSON plan object"),
            "got: {err}"
        );
    }

    #[test]
    fn parse_plan_rejects_unknown_intent() {
        let err = parse_plan("{\"intent\":\"chitchat\",\"query\":null}").unwrap_err();
        assert!(err.to_string().contains("unknown intent"), "got: {err}");
    }

    #[test]
    fn factual_plan_routes_to_executable_read_only_candidate() {
        let route = plan_and_route(
            "who owns passport FDD-1?",
            &slice(),
            &mock_model(
                "{\"intent\":\"factual\",\"query\":\"SELECT * FROM travelers WHERE passport = 'FDD-1'\",\"rationale\":\"lookup\"}",
            ),
        )
        .unwrap();
        match route.routing {
            PlanRouting::Execute { candidate, .. } => {
                assert!(candidate.is_read_only());
                assert_eq!(candidate.statement_type, "select");
            }
            other => panic!("expected Execute, got {other:?}"),
        }
    }

    #[test]
    fn factual_join_candidate_is_executable() {
        let route = plan_and_route(
            "which cities did the owner of passport FDD-1 visit?",
            &slice(),
            &mock_model(
                "{\"intent\":\"factual\",\"query\":\"SELECT * FROM travelers JOIN trips ON travelers.passport = trips.passport WHERE travelers.passport = 'FDD-1'\",\"rationale\":\"join\"}",
            ),
        )
        .unwrap();
        assert!(matches!(route.routing, PlanRouting::Execute { .. }));
    }

    #[test]
    fn factual_plan_with_mutating_candidate_is_refused_not_executed() {
        let route = plan_and_route(
            "delete traveler FDD-1",
            &slice(),
            &mock_model(
                "{\"intent\":\"factual\",\"query\":\"DELETE FROM travelers WHERE passport = 'FDD-1'\",\"rationale\":\"oops\"}",
            ),
        )
        .unwrap();
        match route.routing {
            PlanRouting::RefuseMutating { statement_type, .. } => {
                assert_eq!(statement_type, "delete")
            }
            other => panic!("expected RefuseMutating, got {other:?}"),
        }
    }

    #[test]
    fn factual_plan_with_malformed_candidate_is_rejected() {
        let err = plan_and_route(
            "who owns passport FDD-1?",
            &slice(),
            &mock_model(
                "{\"intent\":\"factual\",\"query\":\"this is not rql\",\"rationale\":\"x\"}",
            ),
        )
        .unwrap_err();
        assert!(
            err.to_string().contains("invalid RQL candidate"),
            "got: {err}"
        );
    }

    #[test]
    fn factual_plan_without_query_is_rejected() {
        let err = plan_and_route(
            "who owns passport FDD-1?",
            &slice(),
            &mock_model("{\"intent\":\"factual\",\"query\":null,\"rationale\":\"x\"}"),
        )
        .unwrap_err();
        assert!(
            err.to_string().contains("no query or tool call"),
            "got: {err}"
        );
    }

    #[test]
    fn synthesis_intent_routes_unsupported_in_this_slice() {
        let route = plan_and_route(
            "summarise yesterday's incidents",
            &slice(),
            &mock_model("{\"intent\":\"synthesis\",\"query\":null,\"rationale\":\"rag\"}"),
        )
        .unwrap();
        assert_eq!(
            route.routing,
            PlanRouting::Unsupported {
                intent: AskIntent::Synthesis
            }
        );
    }

    #[test]
    fn synthesis_intent_carries_the_routed_intent_for_the_rag_fallthrough() {
        // #1749: a summarise/explain question routes to the RAG path. The
        // routing carries the intent so the orchestrator can record it on the
        // downstream audit row without re-classifying.
        let route = plan_and_route(
            "summarise the incidents from yesterday",
            &slice(),
            &mock_model("{\"intent\":\"synthesis\",\"query\":null,\"rationale\":\"rag\"}"),
        )
        .unwrap();
        match route.routing {
            PlanRouting::Unsupported { intent } => {
                assert_eq!(intent, AskIntent::Synthesis);
                assert_eq!(intent.as_str(), "synthesis");
            }
            other => panic!("expected Unsupported{{synthesis}}, got {other:?}"),
        }
    }

    #[test]
    fn calculation_question_routes_factual_not_synthesis() {
        // ADR 0013 conformance boundary (#1749): a calculation-shaped question
        // must land on the *factual* intent — a read-only aggregate the engine
        // computes — never synthesis, where the LLM would be free to invent the
        // number. The planner emits `factual` + a read-only aggregate SELECT;
        // routing executes it and never falls through to RAG synthesis.
        let route = plan_and_route(
            "how many trips went to Lisbon?",
            &slice(),
            &mock_model(
                "{\"intent\":\"factual\",\"query\":\"SELECT COUNT(*) FROM trips WHERE city = 'Lisbon'\",\"rationale\":\"aggregate count\"}",
            ),
        )
        .unwrap();
        assert_ne!(
            route.plan.intent,
            AskIntent::Synthesis,
            "a calculation must never classify as synthesis (the LLM must not invent numbers)"
        );
        match route.routing {
            PlanRouting::Execute { candidate, .. } => {
                assert!(candidate.is_read_only());
                assert_eq!(candidate.statement_type, "select");
            }
            other => {
                panic!("a calculation must route to an executable factual candidate, got {other:?}")
            }
        }
    }

    #[test]
    fn how_to_intent_routes_to_a_validated_suggestion_envelope() {
        // A how-to plan carries a natural-language answer plus a suggestion of
        // parser-validated statements — a read-only SELECT, a mutating DDL
        // CREATE QUEUE, and a mutating EVENTS BACKFILL.
        let route = plan_and_route(
            "how would I capture events from orders into a queue?",
            &slice(),
            &mock_model(
                "{\"intent\":\"how_to\",\"answer\":\"Create a queue and backfill events into it.\",\
                  \"suggestion\":[\
                    {\"rql\":\"CREATE QUEUE events_q WORK\",\"rationale\":\"the sink queue\"},\
                    {\"rql\":\"EVENTS BACKFILL orders TO events_q\",\"rationale\":\"seed history\"},\
                    {\"rql\":\"SELECT * FROM travelers WHERE passport = 'FDD-1'\",\"rationale\":\"inspect\"}\
                  ],\"rationale\":\"guide\"}",
            ),
        )
        .unwrap();
        match route.routing {
            PlanRouting::Suggest { answer, suggestion } => {
                assert!(answer.contains("Create a queue"));
                assert_eq!(suggestion.len(), 3);
                // DDL / mutating statements are present and flagged mutating,
                // and are NEVER executed — this envelope is advisory only.
                assert!(suggestion[0].mutating, "CREATE QUEUE is mutating DDL");
                assert!(suggestion[1].mutating, "EVENTS BACKFILL is mutating");
                assert!(!suggestion[2].mutating, "the SELECT is read-only");
                assert_eq!(suggestion[2].statement_type, "select");
                assert_eq!(suggestion[0].rationale, "the sink queue");
            }
            other => panic!("expected Suggest, got {other:?}"),
        }
    }

    #[test]
    fn how_to_suggestion_drops_unparseable_statements_never_returns_them_raw() {
        // The middle statement is not valid RQL; it is dropped, and only the
        // two parser-valid statements survive in the envelope.
        let route = plan_and_route(
            "how do I list travelers?",
            &slice(),
            &mock_model(
                "{\"intent\":\"how_to\",\"answer\":\"Select from the collection.\",\
                  \"suggestion\":[\
                    {\"rql\":\"SELECT * FROM travelers WHERE passport = 'FDD-1'\"},\
                    {\"rql\":\"this is not rql at all\"},\
                    \"DELETE FROM travelers WHERE passport = 'FDD-1'\"\
                  ]}",
            ),
        )
        .unwrap();
        match route.routing {
            PlanRouting::Suggest { suggestion, .. } => {
                assert_eq!(suggestion.len(), 2, "the unparseable statement is dropped");
                assert_eq!(suggestion[0].statement_type, "select");
                // A bare-string suggestion item is accepted and validated too.
                assert_eq!(suggestion[1].statement_type, "delete");
                assert!(suggestion[1].mutating);
                for s in &suggestion {
                    assert!(
                        !s.rql.contains("not rql"),
                        "raw unparseable text must never survive"
                    );
                }
            }
            other => panic!("expected Suggest, got {other:?}"),
        }
    }

    #[test]
    fn routing_distinguishes_how_to_from_factual_at_the_closure_model_seam() {
        // Same slice + question, only the model's intent classification differs
        // — the closure-model seam is what routes factual vs how-to.
        let factual = plan_and_route(
            "how would I capture events into a queue?",
            &slice(),
            &mock_model(
                "{\"intent\":\"factual\",\"query\":\"SELECT * FROM travelers WHERE passport = 'FDD-1'\",\"rationale\":\"x\"}",
            ),
        )
        .unwrap();
        assert!(matches!(factual.routing, PlanRouting::Execute { .. }));

        let how_to = plan_and_route(
            "how would I capture events into a queue?",
            &slice(),
            &mock_model(
                "{\"intent\":\"how_to\",\"answer\":\"Use a queue.\",\"suggestion\":[\"CREATE QUEUE events_q WORK\"],\"rationale\":\"x\"}",
            ),
        )
        .unwrap();
        assert!(matches!(how_to.routing, PlanRouting::Suggest { .. }));
    }

    // -----------------------------------------------------------------------
    // Plan budget: STEPS clamp + bounded step counter (#1748).
    // -----------------------------------------------------------------------

    #[test]
    fn steps_clause_is_clamped_to_the_config_cap_never_exceeding_it() {
        // Above the cap → clamped down to the cap.
        assert_eq!(clamp_plan_steps(Some(9), 3), 3);
        // At/below the cap → honored verbatim.
        assert_eq!(clamp_plan_steps(Some(2), 3), 2);
        assert_eq!(clamp_plan_steps(Some(3), 3), 3);
        // Absent → falls back to the cap.
        assert_eq!(clamp_plan_steps(None, 3), 3);
        // Never below 1, even for a zero cap or a zero request.
        assert_eq!(clamp_plan_steps(Some(0), 3), 1);
        assert_eq!(clamp_plan_steps(None, 0), 1);
    }

    #[test]
    fn plan_budget_charges_until_exhausted_then_refuses_more() {
        let mut budget = PlanBudget::new(Some(2), 3);
        assert_eq!(budget.max_steps(), 2);
        assert_eq!(budget.remaining(), 2);

        assert!(budget.charge(PlanStep::RefineRetrieval).is_ok());
        assert_eq!(budget.remaining(), 1);
        assert!(budget.charge(PlanStep::Query).is_ok());
        assert_eq!(budget.remaining(), 0);
        assert_eq!(
            budget.executed_steps(),
            &[PlanStep::RefineRetrieval, PlanStep::Query]
        );

        // The third step exhausts the budget — no unbounded loop.
        let err = budget.charge(PlanStep::Query).unwrap_err();
        assert_eq!(err.max_steps, 2);
        assert_eq!(err.executed_steps, 2);
        assert_eq!(err.attempted, PlanStep::Query);
        // The refused step is not recorded.
        assert_eq!(budget.executed_count(), 2);
    }

    #[test]
    fn plan_budget_request_above_cap_cannot_buy_extra_steps() {
        // STEPS 5 with a cap of 1 → only one step ever executes.
        let mut budget = PlanBudget::new(Some(5), 1);
        assert_eq!(budget.max_steps(), 1);
        assert!(budget.charge(PlanStep::Query).is_ok());
        assert!(budget.charge(PlanStep::RefineRetrieval).is_err());
    }

    #[test]
    fn metadata_calls_and_terminal_query_share_one_step_budget() {
        let mut budget = PlanBudget::new(Some(3), 3);
        budget
            .charge(PlanStep::Catalog)
            .expect("catalog step fits budget");
        budget
            .charge(PlanStep::FindField)
            .expect("field step fits budget");
        budget
            .charge(PlanStep::Query)
            .expect("query step fits budget");

        assert_eq!(budget.remaining(), 0);
        assert_eq!(
            budget.executed_steps(),
            &[PlanStep::Catalog, PlanStep::FindField, PlanStep::Query]
        );
        assert!(budget.charge(PlanStep::DescribeCollection).is_err());
    }

    #[test]
    fn exploratory_and_final_queries_are_distinct_budgeted_actions() {
        let mut budget = PlanBudget::new(Some(2), 2);
        budget
            .charge(PlanStep::ExploreQuery)
            .expect("exploration fits budget");
        budget
            .charge(PlanStep::Query)
            .expect("final query fits budget");
        assert_eq!(
            budget.executed_steps(),
            &[PlanStep::ExploreQuery, PlanStep::Query]
        );
    }

    #[test]
    fn bounded_agent_loop_feeds_metadata_to_the_next_round_then_routes_query() {
        let responses = std::rc::Rc::new(std::cell::RefCell::new(
            std::collections::VecDeque::from([
                "{\"intent\":\"factual\",\"query\":null,\"tool\":{\"name\":\"reddb_find_field\",\"arguments\":{\"field\":\"passport_number\"}}}"
                    .to_string(),
                "{\"intent\":\"factual\",\"query\":\"SELECT name FROM travelers WHERE passport_number = 'FDD-1'\",\"tool\":null}"
                    .to_string(),
            ]),
        ));
        let prompts = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
        let responses_inner = responses.clone();
        let prompts_inner = prompts.clone();
        let model = move |prompt: &str| {
            prompts_inner.borrow_mut().push(prompt.to_string());
            Ok(responses_inner
                .borrow_mut()
                .pop_front()
                .expect("scripted planner response"))
        };

        let calls = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
        let calls_inner = calls.clone();
        let executor = move |call: &PlannerToolCall| {
            calls_inner.borrow_mut().push(call.clone());
            Ok("{\"rows\":[{\"collection\":\"travelers\"}],\"truncated\":false}".to_string())
        };
        let mut budget = PlanBudget::new(Some(2), 3);

        let outcome =
            run_bounded_agent_loop("who owns FDD-1?", &slice(), &mut budget, &model, &executor)
                .expect("bounded loop must reach the terminal query");

        assert!(matches!(
            outcome,
            AgentLoopOutcome::Routed {
                route,
                ..
            } if matches!(route.routing, PlanRouting::Execute { .. })
        ));
        assert_eq!(
            budget.executed_steps(),
            &[PlanStep::FindField, PlanStep::Query]
        );
        assert_eq!(
            calls.borrow().as_slice(),
            &[PlannerToolCall::FindField {
                field: "passport_number".to_string()
            }]
        );
        let prompts = prompts.borrow();
        assert_eq!(prompts.len(), 2);
        assert!(prompts[1].contains("Tool observations (untrusted JSON data)"));
        assert!(prompts[1].contains("travelers"));
    }

    #[test]
    fn bounded_agent_loop_executes_parameterized_exploration_before_final_query() {
        let responses = std::rc::Rc::new(std::cell::RefCell::new(
            std::collections::VecDeque::from([
                "{\"intent\":\"factual\",\"tool\":{\"name\":\"reddb_query\",\"arguments\":{\"sql\":\"SELECT id FROM travelers WHERE passport = $1 LIMIT 5\",\"params\":[\"FDD-1\"],\"purpose\":\"resolve person id\",\"phase\":\"explore\"}}}"
                    .to_string(),
                "{\"intent\":\"factual\",\"tool\":{\"name\":\"reddb_query\",\"arguments\":{\"sql\":\"SELECT name FROM travelers WHERE passport = $1\",\"params\":[\"FDD-1\"],\"purpose\":\"answer who owns the passport\",\"phase\":\"final\"}}}"
                    .to_string(),
            ]),
        ));
        let responses_inner = responses.clone();
        let model = move |_prompt: &str| {
            Ok(responses_inner
                .borrow_mut()
                .pop_front()
                .expect("scripted planner response"))
        };
        let calls = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
        let calls_inner = calls.clone();
        let executor = move |call: &PlannerToolCall| {
            calls_inner.borrow_mut().push(call.clone());
            Ok("{\"rows\":[{\"id\":\"t1\"}],\"truncated\":false}".to_string())
        };
        let mut budget = PlanBudget::new(Some(2), 2);

        let outcome =
            run_bounded_agent_loop("who owns FDD-1?", &slice(), &mut budget, &model, &executor)
                .expect("exploration must lead to a final query");

        let AgentLoopOutcome::Routed {
            route,
            observations,
        } = outcome
        else {
            panic!("expected routed outcome");
        };
        assert_eq!(observations.len(), 1);
        assert_eq!(calls.borrow().len(), 1);
        assert_eq!(
            budget.executed_steps(),
            &[PlanStep::ExploreQuery, PlanStep::Query]
        );
        match route.routing {
            PlanRouting::Execute {
                candidate,
                params,
                purpose,
            } => {
                assert_eq!(
                    candidate.rql,
                    "SELECT name FROM travelers WHERE passport = $1"
                );
                assert_eq!(
                    params,
                    vec![crate::json::Value::String("FDD-1".to_string())]
                );
                assert_eq!(purpose, "answer who owns the passport");
            }
            other => panic!("expected final query, got {other:?}"),
        }
    }

    #[test]
    fn bounded_agent_loop_does_not_call_the_model_after_the_last_tool_step() {
        let planner_calls = std::rc::Rc::new(std::cell::Cell::new(0usize));
        let planner_calls_inner = planner_calls.clone();
        let model = move |_prompt: &str| {
            planner_calls_inner.set(planner_calls_inner.get() + 1);
            Ok("{\"intent\":\"factual\",\"query\":null,\"tool\":{\"name\":\"reddb_catalog\",\"arguments\":{}}}"
                .to_string())
        };
        let tool_calls = std::rc::Rc::new(std::cell::Cell::new(0usize));
        let tool_calls_inner = tool_calls.clone();
        let executor = move |_call: &PlannerToolCall| {
            tool_calls_inner.set(tool_calls_inner.get() + 1);
            Ok("{\"rows\":[],\"truncated\":false}".to_string())
        };
        let mut budget = PlanBudget::new(Some(1), 3);

        let outcome = run_bounded_agent_loop(
            "what data is available?",
            &slice(),
            &mut budget,
            &model,
            &executor,
        )
        .expect("bounded loop must return structured exhaustion");

        assert_eq!(
            outcome,
            AgentLoopOutcome::BudgetExhausted(BudgetExhausted {
                attempted: PlanStep::Catalog,
                executed_steps: 0,
                max_steps: 1,
            })
        );
        assert_eq!(planner_calls.get(), 1, "no extra planner round");
        assert_eq!(tool_calls.get(), 0, "the final step stays reserved");
        assert!(budget.executed_steps().is_empty());
    }

    // -----------------------------------------------------------------------
    // Grounding critique + single refine_retrieval retry (#1748).
    // -----------------------------------------------------------------------

    /// A funnel closure that counts calls and returns empty/non-empty per the
    /// script `[first, refined]`.
    fn scripted_funnel(
        script: Vec<bool>,
    ) -> (impl RetrievalFunnel, std::rc::Rc<std::cell::Cell<usize>>) {
        let calls = std::rc::Rc::new(std::cell::Cell::new(0usize));
        let calls_inner = calls.clone();
        let funnel = move |_expanded: bool| -> RedDBResult<NarrowedSlice> {
            let n = calls_inner.get();
            calls_inner.set(n + 1);
            let non_empty = script.get(n).copied().unwrap_or(false);
            Ok(if non_empty {
                slice()
            } else {
                NarrowedSlice::default()
            })
        };
        (funnel, calls)
    }

    #[test]
    fn grounding_succeeds_on_first_pass_without_refining() {
        let (funnel, calls) = scripted_funnel(vec![true]);
        let outcome = ground_with_refine(&funnel).unwrap();
        assert_eq!(calls.get(), 1, "no refine when the first pass grounds");
        match outcome {
            GroundingOutcome::Grounded { refined, .. } => assert!(!refined),
            other => panic!("expected Grounded, got {other:?}"),
        }
    }

    #[test]
    fn empty_first_pass_triggers_exactly_one_refine_retrieval() {
        // First pass grounds nothing; the single refine retry grounds.
        let (funnel, calls) = scripted_funnel(vec![false, true]);
        let outcome = ground_with_refine(&funnel).unwrap();
        assert_eq!(calls.get(), 2, "exactly one refine re-funnel");
        match outcome {
            GroundingOutcome::Grounded { refined, .. } => assert!(refined),
            other => panic!("expected Grounded after refine, got {other:?}"),
        }
    }

    #[test]
    fn second_grounding_failure_returns_no_matching_sources_not_an_invented_answer() {
        // Both passes ground nothing → honest no-matching-sources; the funnel
        // is called exactly twice and never a third time.
        let (funnel, calls) = scripted_funnel(vec![false, false]);
        let outcome = ground_with_refine(&funnel).unwrap();
        assert_eq!(calls.get(), 2, "exactly one refine retry, then give up");
        assert_eq!(outcome, GroundingOutcome::NoMatchingSources);
    }

    #[test]
    fn plan_summary_includes_intent_and_query() {
        let plan = AskPlan {
            intent: AskIntent::Factual,
            query: Some("SELECT * FROM travelers WHERE passport = 'FDD-1'".to_string()),
            tool: None,
            answer: String::new(),
            suggestion: Vec::new(),
            rationale: "lookup".to_string(),
        };
        let summary = plan.summary();
        assert!(summary.contains("intent=factual"));
        assert!(summary.contains("SELECT * FROM travelers"));
    }
}