pensieve-server 0.1.0

HTTP + gRPC query API, auth stub, health, observability.
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
//! HTTP handlers for `POST /v1/agent/ask` (SSE), `GET /v1/agent/runs/:run_id`,
//! and the engine-management surface (`GET/PUT /v1/agent/engine`, etc.).
//!
//! The ask handler drives an ADK-Rust `Runner` and emits a stream of SSE
//! frames. Once the stream completes (naturally, on error, or on budget
//! overrun) we insert one row into `agent_runs` with the full trace as JSONB.

use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant};

use adk_rust::futures::StreamExt;
use adk_rust::identity::{SessionId, UserId};
use adk_rust::{Content, Part};
use axum::extract::{Extension, Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::Json;
use chrono::Utc;
use serde::Deserialize;
use serde_json::{json, Value};
use sqlx::types::Json as SqlxJson;
use sqlx::PgPool;
use tracing::Instrument as _;
use tracing::{debug, error, info, warn};

use super::engine::{
    build_engine, claude_cli, engine_catalogue, CredentialResolver, EngineConfig, EngineKind,
};
use super::memory_retrieve::{retrieve, RetrieveRequest};
use super::runner::{make_runner, model_id, run_oneshot, ANON_USER};
use super::sessions;
use super::state::AgentState;
use super::tools::{execute_sql, SharedToolCtx};
use super::ui_stream;
use crate::auth::{Principal, Role};

/// Hard cap on tool-call count per run. Above this, the turn is aborted
/// with `run_error{code="tool_loop"}` and persisted as `budget_exceeded`.
const MAX_TOOL_CALLS: u32 = 12;

/// Wall-clock deadline for the whole SSE exchange.
const RUN_WALL_CLOCK: Duration = Duration::from_secs(60);

/// Default number of new turns after which a session's rolling summary is
/// refreshed. Overridable via `PENSIEVE_SESSION_SUMMARY_EVERY`.
const DEFAULT_SUMMARY_EVERY: i32 = 12;

fn summary_every() -> i32 {
    std::env::var("PENSIEVE_SESSION_SUMMARY_EVERY")
        .ok()
        .and_then(|v| v.parse::<i32>().ok())
        .filter(|n| *n > 0)
        .unwrap_or(DEFAULT_SUMMARY_EVERY)
}

pub fn router(state: AgentState) -> axum::Router {
    axum::Router::new()
        .route("/ask", post(ask_handler))
        .route("/runs/:run_id", get(run_lookup_handler))
        .route("/sessions", get(list_sessions_handler))
        .route(
            "/sessions/:session_id",
            get(get_session_handler).delete(delete_session_handler),
        )
        .route(
            "/sessions/:session_id/turns",
            get(get_session_turns_handler),
        )
        .route("/engines", get(list_engines))
        .route("/engine", get(get_engine).put(put_engine))
        .route("/engine/test", post(test_engine))
        .route("/skills", get(list_skills))
        .route(
            "/skills/enabled",
            get(get_enabled_skills).put(put_enabled_skills),
        )
        .route("/memory/overview", get(super::memory::overview_handler))
        .route(
            "/memory/source-summary",
            get(super::memory::source_summary_handler),
        )
        .route("/memory/query", post(memory_query_handler))
        .route(
            "/memory/settings",
            get(get_memory_settings).put(put_memory_settings),
        )
        .route(
            "/retention/settings",
            get(get_retention_settings).put(put_retention_settings),
        )
        .route("/files/contribute", post(contribute_file_route))
        .route("/memory/export", get(export_memory_handler))
        .route("/memory/changes", get(changes_memory_handler))
        .route("/memory/import", post(import_memory_handler))
        .route(
            "/memory/dreaming/runs",
            get(super::dreaming::list_runs_handler),
        )
        .route(
            "/memory/dreaming/runs/:id",
            get(super::dreaming::get_run_handler),
        )
        .route(
            "/memory/dreaming/run",
            post(super::dreaming::trigger_run_handler),
        )
        .route("/memory/review", get(super::memory_review::list_review))
        .route(
            "/memory/review/count",
            get(super::memory_review::review_count),
        )
        .route(
            "/memory/review/bulk",
            post(super::memory_review::bulk_review),
        )
        .route(
            "/memory/review/:id/:action",
            post(super::memory_review::resolve_review),
        )
        .with_state(state)
}

#[derive(Debug, Deserialize, Default)]
struct ImportBody {
    #[serde(default)]
    memory_nodes: Vec<Value>,
    #[serde(default)]
    memory_edges: Vec<Value>,
}

/// `POST /v1/agent/memory/import` — apply memory node/edge rows (from another
/// instance's `export`/`changes`) into this store **via the MemoryWriter**, so
/// the canonical memory schema (typed `importance`, the embedding vector, …) and
/// on-demand provisioning are used — unlike generic `/v1/ingest`, which would
/// infer every column as text. Append-only / latest-wins, so re-applying is safe.
/// This is the receive side of memory sync.
async fn import_memory_handler(
    State(state): State<AgentState>,
    Extension(principal): Extension<Principal>,
    Json(body): Json<ImportBody>,
) -> Response {
    let span = tracing::info_span!(
        target: "pensieve_telemetry",
        "memory.import",
        pensieve.subject = principal.subject.as_deref().unwrap_or(""),
        pensieve.tenant = %principal.tenant,
        memory.imported = tracing::field::Empty,
    );
    async move {
        // Import is a bulk WRITE of memory nodes/edges from another instance. The
        // surrounding agent router is mounted at `Role::Read`, so gate this one
        // route at `Role::Write` in-handler — a read-only token must not be able to
        // push memory into the store.
        if principal.role < Role::Write {
            return (
                StatusCode::FORBIDDEN,
                Json(json!({ "error": "memory import requires write role" })),
            )
                .into_response();
        }
        let embed = match pensieve_memory::shared_embedding().await {
            Ok(e) => e,
            Err(e) => {
                return Json(json!({ "error": format!("embedding backend: {e}") })).into_response()
            }
        };
        let writer =
            pensieve_memory::MemoryWriter::new(state.catalog.clone(), state.format.clone(), embed);
        let mut applied_nodes = 0usize;
        let mut applied_edges = 0usize;
        let mut errors: Vec<String> = Vec::new();
        if !body.memory_nodes.is_empty() {
            match writer.append_node_rows(body.memory_nodes.clone()).await {
                Ok(()) => applied_nodes = body.memory_nodes.len(),
                Err(e) => errors.push(format!("nodes: {e}")),
            }
        }
        if !body.memory_edges.is_empty() {
            match writer.append_edge_rows(body.memory_edges.clone()).await {
                Ok(()) => applied_edges = body.memory_edges.len(),
                Err(e) => errors.push(format!("edges: {e}")),
            }
        }
        tracing::Span::current().record("memory.imported", applied_nodes + applied_edges);
        Json(json!({
            "applied_nodes": applied_nodes,
            "applied_edges": applied_edges,
            "errors": errors,
        }))
        .into_response()
    }
    .instrument(span)
    .await
}

#[derive(Debug, Deserialize)]
struct ExportParams {
    /// Restrict the export to one realm. Omit to export all.
    #[serde(default)]
    realm: Option<String>,
}

#[derive(Debug, Deserialize)]
struct ChangesParams {
    /// Return only memories changed strictly after this RFC3339 timestamp.
    /// Omit for the full set (epoch). Pass back the response's `until` next time.
    #[serde(default)]
    since: Option<String>,
    /// Restrict to one realm. Omit for all.
    #[serde(default)]
    realm: Option<String>,
}

/// `GET /v1/agent/memory/changes?since=<rfc3339>[&realm=]` — the incremental
/// pull side of memory sync: latest node versions whose `updated_at` (and edges
/// whose `created_at`) are strictly after `since`, plus an `until` watermark
/// (server clock) the caller advances to for the next pull. Same row shape as
/// export, so the caller re-applies via `POST /v1/ingest` (X-Database: memory).
async fn changes_memory_handler(
    State(state): State<AgentState>,
    axum::extract::Query(params): axum::extract::Query<ChangesParams>,
) -> Json<Value> {
    let shared = SharedToolCtx {
        realm_scope: Default::default(),
        consumer_sink: None,
        federation: Some(pensieve_federation::runtime_from(state.credentials.clone())),
        catalog: state.catalog.clone(),
        format: state.format.clone(),
        pool: state.pool.clone(),
        memory: state.memory.clone(),
        hitl: None,
        memory_settings_path: state.memory_settings_path.clone(),
    };
    let since = params
        .since
        .filter(|s| !s.trim().is_empty())
        .unwrap_or_else(|| "1970-01-01T00:00:00Z".to_string());
    let since_esc = since.replace('\'', "''");
    let realm_filter = params
        .realm
        .as_deref()
        .map(|r| format!(" AND realm = '{}'", r.replace('\'', "''")))
        .unwrap_or_default();
    let nodes_sql = format!(
        "WITH latest AS (SELECT *, row_number() OVER (PARTITION BY id ORDER BY updated_at DESC) AS __rn FROM memory_nodes) \
         SELECT id, labels, realm, memory_type, title, content, content_preview, tags, importance, status, \
                source_session_id, source_run_id, embedding, created_at, updated_at, \
                valid_at, invalid_at, superseded_by, provenance, topic_key \
         FROM latest WHERE __rn = 1 AND updated_at > '{since_esc}'{realm_filter}"
    );
    let edges_sql = format!(
        "SELECT id, src, dst, type, realm, target_namespace, props, created_at \
         FROM memory_edges WHERE created_at > '{since_esc}'"
    );
    let db = pensieve_memory::DEFAULT_DATABASE;
    let nodes = execute_sql(&shared, db, &nodes_sql, 1_000_000).await;
    let edges = execute_sql(&shared, db, &edges_sql, 1_000_000).await;
    let rows = |v: Value| v.get("rows").cloned().unwrap_or_else(|| json!([]));
    Json(json!({
        "since": since,
        "until": Utc::now().to_rfc3339(),
        "memory_nodes": rows(nodes),
        "memory_edges": rows(edges),
    }))
}

/// `GET /v1/agent/memory/export[?realm=]` — full memory snapshot (latest node
/// versions + edges, including embeddings) as JSON, for backup / portability.
/// Re-import on another instance via the idempotent `POST /v1/ingest`
/// (`X-Database: memory`, `X-Table: memory_nodes|memory_edges`, NDJSON).
async fn export_memory_handler(
    State(state): State<AgentState>,
    Extension(principal): Extension<Principal>,
    axum::extract::Query(params): axum::extract::Query<ExportParams>,
) -> Json<Value> {
    let span = tracing::info_span!(
        target: "pensieve_telemetry",
        "memory.export",
        pensieve.subject = principal.subject.as_deref().unwrap_or(""),
        pensieve.tenant = %principal.tenant,
        memory.exported = tracing::field::Empty,
    );
    async move {
        let shared = SharedToolCtx { realm_scope: Default::default(), consumer_sink: None, federation: Some(pensieve_federation::runtime_from(state.credentials.clone())),
            catalog: state.catalog.clone(),
            format: state.format.clone(),
            pool: state.pool.clone(),
            memory: state.memory.clone(),
            hitl: None,
            memory_settings_path: state.memory_settings_path.clone(),
        };
        let realm_filter = params
            .realm
            .as_deref()
            .map(|r| format!(" AND realm = '{}'", r.replace('\'', "''")))
            .unwrap_or_default();
        let nodes_sql = format!(
            "WITH latest AS (SELECT *, row_number() OVER (PARTITION BY id ORDER BY updated_at DESC) AS __rn FROM memory_nodes) \
             SELECT id, labels, realm, memory_type, title, content, content_preview, tags, importance, status, \
                    source_session_id, source_run_id, embedding, created_at, updated_at, \
                    valid_at, invalid_at, superseded_by, provenance, topic_key \
             FROM latest WHERE __rn = 1{realm_filter}"
        );
        let edges_sql =
            "SELECT id, src, dst, type, realm, target_namespace, props, created_at FROM memory_edges";
        let db = pensieve_memory::DEFAULT_DATABASE;
        let nodes = execute_sql(&shared, db, &nodes_sql, 1_000_000).await;
        let edges = execute_sql(&shared, db, edges_sql, 1_000_000).await;
        let rows = |v: Value| v.get("rows").cloned().unwrap_or_else(|| json!([]));
        let nodes_val = rows(nodes);
        let edges_val = rows(edges);
        let exported_count = nodes_val.as_array().map_or(0, Vec::len)
            + edges_val.as_array().map_or(0, Vec::len);
        tracing::Span::current().record("memory.exported", exported_count);
        Json(json!({
            "memory_nodes": nodes_val,
            "memory_edges": edges_val,
            "hint": "Re-import on another instance via POST /v1/agent/memory/import \
                     with this same {memory_nodes, memory_edges} body (writes through \
                     the MemoryWriter, preserving the canonical schema + embeddings).",
        }))
    }
    .instrument(span)
    .await
}

/// `GET /v1/agent/memory/settings` — current tunable memory settings. Reads the
/// Postgres row in server mode, or the local JSON file in local mode.
async fn get_memory_settings(State(state): State<AgentState>) -> Json<Value> {
    let s = super::memory_settings::load_for(&state).await;
    Json(serde_json::to_value(s).unwrap_or_else(|_| json!({})))
}

/// `PUT /v1/agent/memory/settings` — persist tunable memory settings. Server
/// mode writes the Postgres row; local mode writes the JSON file under
/// `${PENSIEVE_HOME}`.
async fn put_memory_settings(
    State(state): State<AgentState>,
    Json(body): Json<super::memory_settings::MemorySettings>,
) -> Response {
    if let Some(pool) = state.pool.as_ref() {
        return match super::memory_settings::save(pool, state.tenant, &body).await {
            Ok(()) => Json(json!({ "ok": true })).into_response(),
            Err(e) => (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(json!({ "error": e.to_string() })),
            )
                .into_response(),
        };
    }
    // Local mode: persist to the settings file so dreaming knobs survive restarts.
    if let Some(path) = state.memory_settings_path.as_ref() {
        return match super::memory_settings::save_local(path, &body).await {
            Ok(()) => Json(json!({ "ok": true, "persisted": true })).into_response(),
            Err(e) => (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(json!({ "error": e.to_string() })),
            )
                .into_response(),
        };
    }
    Json(json!({ "ok": true, "persisted": false })).into_response()
}

/// `POST /v1/agent/files/contribute` — persist a file's structure as candidate
/// graph nodes (used by `pensieve scrape` / `pensieve watch` and any HTTP client). Same
/// path as the `contribute_file` MCP tool.
#[derive(Debug, serde::Deserialize)]
struct ContributeFileBody {
    path: String,
    #[serde(default)]
    realm: String,
    repo: Option<String>,
    content: String,
    why_read: Option<String>,
}

async fn contribute_file_route(
    State(state): State<AgentState>,
    Json(body): Json<ContributeFileBody>,
) -> Response {
    match super::file_tools::contribute_file_impl(
        state.catalog.clone(),
        state.format.clone(),
        body.path,
        body.realm,
        body.repo,
        body.content,
        body.why_read,
    )
    .await
    {
        Ok(s) => Json(serde_json::to_value(s).unwrap_or_else(|_| json!({}))).into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({ "error": e })),
        )
            .into_response(),
    }
}

/// `GET /v1/agent/retention/settings` — current data-retention settings
/// (global / per-source / per-table / per-artifact-class day-counts).
async fn get_retention_settings(State(state): State<AgentState>) -> Response {
    let Some(pg) = state
        .catalog
        .as_ref_any()
        .downcast_ref::<pensieve_catalog::PostgresCatalog>()
    else {
        // Local mode: no Postgres, settings default to retain-forever.
        return Json(pensieve_core::retention::RetentionSettings::default()).into_response();
    };
    match pg.load_retention_settings(state.tenant).await {
        Ok(s) => Json(s).into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({ "error": e.to_string() })),
        )
            .into_response(),
    }
}

/// `PUT /v1/agent/retention/settings` — persist settings, then apply: stamp
/// `artifacts.expires_at` (so the retention worker sweeps them) and reconcile
/// explicit per-table overrides into `tables.config.retention_days`.
async fn put_retention_settings(
    State(state): State<AgentState>,
    Json(body): Json<pensieve_core::retention::RetentionSettings>,
) -> Response {
    // A day-count of 0 would mean "expire immediately" — almost always a
    // foot-gun. Retain-forever is expressed by omitting the key (or null global).
    let has_zero = body.global_default_days == Some(0)
        || body.per_source_days.values().any(|&d| d == 0)
        || body.per_table_days.values().any(|&d| d == 0)
        || body.per_artifact_class_days.values().any(|&d| d == 0);
    if has_zero {
        return (
            StatusCode::BAD_REQUEST,
            Json(json!({
                "error": "retention days must be >= 1; omit a key (or null global_default_days) to retain forever",
            })),
        )
            .into_response();
    }

    let Some(pg) = state
        .catalog
        .as_ref_any()
        .downcast_ref::<pensieve_catalog::PostgresCatalog>()
    else {
        return Json(json!({ "ok": true, "persisted": false })).into_response();
    };

    if let Err(e) = pg.save_retention_settings(state.tenant, &body).await {
        return (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({ "error": e.to_string() })),
        )
            .into_response();
    }
    // Apply immediately so the change is visible without waiting for a worker
    // tick. Both are idempotent; failures here are non-fatal (the worker
    // re-stamps artifacts on its next pass anyway).
    let stamped = pg
        .restamp_artifact_expiry(state.tenant, &body)
        .await
        .unwrap_or(0);
    if let Err(e) = pg.reconcile_table_retention(state.tenant, &body).await {
        tracing::warn!(error = %e, "reconcile_table_retention failed");
    }
    Json(json!({ "ok": true, "artifacts_restamped": stamped })).into_response()
}

/// `POST /v1/agent/memory/query` — near-realtime memory recall for external
/// callers (web UI, Claude Code hooks, coding agents). `mode: "fast"` (default)
/// runs the LLM-free graph-aware hybrid retrieval; `mode: "agentic"` adds a
/// short synthesized `brief` over the retrieved context.
#[derive(Debug, Deserialize)]
struct MemoryQueryRequest {
    #[serde(flatten)]
    retrieve: RetrieveRequest,
    #[serde(default)]
    mode: Option<String>,
}

async fn memory_query_handler(
    State(state): State<AgentState>,
    Extension(principal): Extension<Principal>,
    peer: Option<axum::extract::ConnectInfo<std::net::SocketAddr>>,
    Json(body): Json<MemoryQueryRequest>,
) -> Json<Value> {
    // Record the connecting peer (ip only — keep the recall hot path cheap; pid
    // is resolved on MCP initialize) for the live-consumers overlay.
    if let Some(axum::extract::ConnectInfo(addr)) = peer {
        crate::agent::identity::record_peer(addr, false);
    }
    let query_preview: String = body.retrieve.query.chars().take(200).collect();
    let span = tracing::info_span!(
        target: "pensieve_telemetry",
        "memory.recall",
        pensieve.subject = principal.subject.as_deref().unwrap_or(""),
        pensieve.tenant = %principal.tenant,
        pensieve.client = %crate::agent::identity::consumer_kind(),
        memory.query = %query_preview,
        memory.results = tracing::field::Empty,
        memory.took_ms = tracing::field::Empty,
    );
    async move {
        let shared = SharedToolCtx {
            // Carry the caller's realm scope so retrieve() enforces realms on
            // the REST /v1/agent/memory/query path exactly as it does over MCP.
            realm_scope: crate::auth::RealmScope::from_principal(&principal),
            consumer_sink: state.consumer_events.clone().map(|events| {
                std::sync::Arc::new(crate::agent::tools::LocalConsumerPublisher {
                    events,
                    tenant: state.tenant,
                }) as crate::agent::tools::ConsumerSink
            }),
            federation: Some(pensieve_federation::runtime_from(state.credentials.clone())),
            catalog: state.catalog.clone(),
            format: state.format.clone(),
            pool: state.pool.clone(),
            memory: state.memory.clone(),
            hitl: None,
            memory_settings_path: state.memory_settings_path.clone(),
        };
        let result = retrieve(&shared, &body.retrieve).await;
        tracing::Span::current().record("memory.results", result.memories.len());
        tracing::Span::current().record("memory.took_ms", result.took_ms);
        // Live consumer overlay: announce this recall (who, and which memory /
        // linked-resource nodes it touched) on the broadcast bus. Best-effort.
        if let Some(sink) = &shared.consumer_sink {
            let mut node_ids: Vec<String> = result.memories.iter().map(|m| m.id.clone()).collect();
            node_ids.extend(result.linked.iter().map(|l| l.node_id.clone()));
            let mut namespaces: Vec<String> =
                result.memories.iter().map(|m| m.realm.clone()).collect();
            namespaces.extend(
                result
                    .linked
                    .iter()
                    .filter_map(|l| l.target_namespace.clone()),
            );
            namespaces.sort();
            namespaces.dedup();
            let kind = crate::agent::identity::consumer_kind();
            let ip = crate::agent::identity::peer_ip();
            let pid = crate::agent::identity::peer_pid();
            let ident = principal
                .subject
                .clone()
                .or_else(|| pid.map(|p| p.to_string()))
                .or_else(|| ip.clone())
                .unwrap_or_else(|| "anon".into());
            sink.publish(pensieve_ingest_core::ConsumerActivity {
                consumer_id: format!("{kind}:{ident}"),
                label: principal.subject.clone().unwrap_or_else(|| kind.clone()),
                kind,
                subject: principal.subject.clone(),
                tenant: principal.tenant.to_string(),
                action: pensieve_ingest_core::ConsumerAction::Recall,
                node_ids,
                namespaces,
                query_preview: Some(query_preview.clone()),
                ts: chrono::Utc::now().timestamp_millis(),
                host: Some(crate::agent::identity::host_name()),
                client_version: crate::agent::identity::client_version(),
                transport: crate::agent::identity::transport(),
                ip,
                pid,
            });
        }
        let mut out = result.to_json();
        if body.mode.as_deref() == Some("agentic") && !result.context.is_empty() {
            let prompt = format!("Question: {}\n\n{}", body.retrieve.query, result.context);
            if let Ok(brief) = run_oneshot(
                &state,
                "pensieve-memory-brief",
                "Answers a question from retrieved memory.",
                "Answer the question using ONLY the provided memories. Be concise and cite memory \
                 ids. If the memories don't contain the answer, say so plainly.",
                &prompt,
            )
            .await
            {
                if let Value::Object(ref mut m) = out {
                    m.insert("brief".into(), Value::String(brief));
                }
            }
        }
        Json(out)
    }
    .instrument(span)
    .await
}

#[derive(Debug, Deserialize)]
struct AskRequest {
    question: String,
    #[serde(default)]
    #[allow(dead_code)]
    database: Option<String>,
    #[serde(default)]
    include_thinking: bool,
    /// Optional conversation session. When present, prior turns are replayed
    /// for context and this turn is appended. A new id is minted when absent.
    #[serde(default)]
    session_id: Option<String>,
    /// Marks the session source (e.g. `"claude_code"` for hook-driven asks).
    #[serde(default)]
    source: Option<String>,
}

/// One recorded logical event — we stash both the event name and the JSON
/// body so we can persist the full trace into `agent_runs` after the stream
/// completes. (The persisted format is internal and independent of the wire
/// protocol the client sees.)
#[derive(Debug, Clone)]
struct TraceFrame {
    event: &'static str,
    data: Value,
}

/// Translates the agent's logical events into the AI-SDK **UI Message Stream**
/// the client consumes, while also recording a [`TraceFrame`] of each event for
/// persistence. One `Emitter` drives one assistant message: it opens the
/// message on construction and manages text/reasoning block lifecycles (so the
/// wire always has matching `*-start`/`*-end` parts) and tool-call/result
/// pairing by id.
struct Emitter {
    ui: ui_stream::UiStream,
    trace: Vec<TraceFrame>,
    /// Open text block id, if any.
    text_id: Option<String>,
    /// Open reasoning block id, if any.
    reasoning_id: Option<String>,
    /// Monotonic counter for unique block ids within the message.
    block_seq: u64,
    /// FIFO of synthetic tool-call ids per tool name, so a `tool_result`
    /// (which carries only the tool name) can be paired with its `tool_call`.
    tool_ids: HashMap<String, VecDeque<String>>,
    tool_seq: u64,
}

impl Emitter {
    /// Open the assistant message (`start` + `start-step`) and return the emitter.
    fn new(ui: ui_stream::UiStream, message_id: &str) -> Self {
        ui.start(message_id);
        ui.start_step();
        Self {
            ui,
            trace: Vec::new(),
            text_id: None,
            reasoning_id: None,
            block_seq: 0,
            tool_ids: HashMap::new(),
            tool_seq: 0,
        }
    }

    fn record(&mut self, event: &'static str, data: Value) {
        self.trace.push(TraceFrame { event, data });
    }

    fn next_block(&mut self) -> String {
        let id = format!("blk-{}", self.block_seq);
        self.block_seq += 1;
        id
    }

    fn close_text(&mut self) {
        if let Some(id) = self.text_id.take() {
            self.ui.text_end(&id);
        }
    }
    fn close_reasoning(&mut self) {
        if let Some(id) = self.reasoning_id.take() {
            self.ui.reasoning_end(&id);
        }
    }

    /// Surface the conversation session id so the client can resume.
    fn session(&mut self, session_id: &str) {
        self.record("session", json!({ "session_id": session_id }));
        self.ui.data("session", json!({ "sessionId": session_id }));
    }

    fn run_started(&mut self, run_id: &str, model: &str, question: &str) {
        self.record(
            "run_started",
            json!({ "run_id": run_id, "model": model, "question": question }),
        );
        self.ui.data("model", json!({ "model": model }));
    }

    fn answer_delta(&mut self, text: &str) {
        self.record("answer_delta", json!({ "text": text }));
        self.close_reasoning();
        let id = match &self.text_id {
            Some(id) => id.clone(),
            None => {
                let id = self.next_block();
                self.ui.text_start(&id);
                self.text_id = Some(id.clone());
                id
            }
        };
        self.ui.text_delta(&id, text);
    }

    fn thinking_delta(&mut self, text: &str) {
        self.record("thinking_delta", json!({ "text": text }));
        self.close_text();
        let id = match &self.reasoning_id {
            Some(id) => id.clone(),
            None => {
                let id = self.next_block();
                self.ui.reasoning_start(&id);
                self.reasoning_id = Some(id.clone());
                id
            }
        };
        self.ui.reasoning_delta(&id, text);
    }

    fn tool_call(&mut self, tool: &str, args: Value, call_index: u32) {
        self.record(
            "tool_call",
            json!({ "tool": tool, "args": args, "call_index": call_index }),
        );
        self.close_text();
        self.close_reasoning();
        let id = format!("call-{}", self.tool_seq);
        self.tool_seq += 1;
        self.tool_ids
            .entry(tool.to_string())
            .or_default()
            .push_back(id.clone());
        self.ui.tool_input_available(&id, tool, args);
    }

    fn tool_result(&mut self, tool: &str, result: Value) {
        self.record("tool_result", json!({ "tool": tool, "result": result }));
        let id = self
            .tool_ids
            .get_mut(tool)
            .and_then(|q| q.pop_front())
            .unwrap_or_else(|| format!("call-{tool}"));
        self.ui.tool_output_available(&id, result);
    }

    /// Final answer bookkeeping. The text itself has already streamed via
    /// `answer_delta`, so here we only close the block and surface the SQL/KQL
    /// the run used as data parts.
    fn answer_final(&mut self, text: &str, sql_used: Option<&str>, kql_used: Option<&str>) {
        self.record(
            "answer_final",
            json!({ "text": text, "kql_used": kql_used, "sql_used": sql_used }),
        );
        self.close_text();
        self.close_reasoning();
        if let Some(sql) = sql_used {
            self.ui.data("sql", json!({ "sql": sql }));
        }
        if let Some(kql) = kql_used {
            self.ui.data("kql", json!({ "kql": kql }));
        }
    }

    fn run_error(&mut self, code: &str, message: &str) {
        self.record("run_error", json!({ "code": code, "message": message }));
        self.ui.error(message);
    }

    /// Close the message: flush open blocks, emit run metadata, then the
    /// terminal `finish-step`/`finish`/`[DONE]` parts.
    fn finish(&mut self, usage: Value) {
        self.close_text();
        self.close_reasoning();
        self.ui.data("usage", usage);
        self.ui.finish_step();
        self.ui.finish();
        self.ui.done();
    }

    /// Build the JSON trace array persisted into `agent_runs`.
    fn trace_json(&self) -> Value {
        Value::Array(
            self.trace
                .iter()
                .map(|f| json!({ "event": f.event, "data": f.data }))
                .collect(),
        )
    }
}

/// POST /v1/agent/ask — run one agent turn and stream SSE frames.
async fn ask_handler(
    State(state): State<AgentState>,
    Extension(principal): Extension<Principal>,
    headers: axum::http::HeaderMap,
    Json(body): Json<AskRequest>,
) -> Response {
    let question = body.question.trim().to_string();
    if question.is_empty() {
        return (
            StatusCode::BAD_REQUEST,
            Json(json!({"error": "question must be non-empty"})),
        )
            .into_response();
    }
    let agent_span = tracing::info_span!(
        target: "pensieve_telemetry",
        "agent.query",
        pensieve.subject = principal.subject.as_deref().unwrap_or(""),
        pensieve.tenant = %principal.tenant,
        agent.question = %question.chars().take(200).collect::<String>(),
    );

    // Engine-kind branch: the Claude CLI engine owns its own tool loop, OAuth,
    // skills, and MCPs — adk-rust can't wrap it. Divert here before building
    // the runner.
    if let Ok(cfg) = state.engines.get().await {
        if cfg.kind == EngineKind::ClaudeCli {
            // `session_id` here is Claude's own session id (echoed back from a
            // prior turn's `data-session` part), used to `--resume`. The
            // caller's Authorization header is forwarded so Claude can reach
            // our own MCP endpoint (`mcp_url`) as the same principal.
            let auth_header = headers
                .get(axum::http::header::AUTHORIZATION)
                .and_then(|v| v.to_str().ok())
                .map(str::to_string);
            return ask_via_claude_cli(
                state.pool.clone(),
                &question,
                &cfg,
                body.session_id.clone(),
                state.mcp_url.clone(),
                auth_header,
            )
            .await;
        }
    }

    // S2.6 agent-run admission: cap concurrent agent runs (each is a heavy LLM
    // tool loop — model calls + memory recall + data-source reads). Off by
    // default (`PENSIEVE_AGENT_MAX_CONCURRENT` unset). The permit is moved into the
    // run's spawned task below, so the slot frees when the run finishes. (The
    // Claude-CLI engine path above owns its own resource model; capping it is a
    // follow-up.)
    let agent_permit = match crate::concurrency::acquire_agent_run() {
        Ok(p) => p,
        Err(retry) => {
            return (
                StatusCode::TOO_MANY_REQUESTS,
                [(axum::http::header::RETRY_AFTER, retry.to_string())],
                Json(json!({
                    "error": "agent run capacity reached; retry after the indicated delay"
                })),
            )
                .into_response();
        }
    };
    // Per-tenant agent-run budget (S2.6): one tenant's agent activity can't
    // starve another's. Off by default (`PENSIEVE_AGENT_MAX_CONCURRENT_PER_TENANT`).
    let agent_tenant_permit =
        match crate::concurrency::acquire_agent_run_for_tenant(principal.tenant) {
            Ok(p) => p,
            Err(retry) => {
                return (
                StatusCode::TOO_MANY_REQUESTS,
                [(axum::http::header::RETRY_AFTER, retry.to_string())],
                Json(json!({
                    "error": "tenant agent-run capacity reached; retry after the indicated delay"
                })),
            )
                .into_response();
            }
        };

    let run_id = uuid::Uuid::new_v4();
    let include_thinking = body.include_thinking;
    let model = model_id(&state).await;
    let started_at = Utc::now();
    let start = Instant::now();

    // Resolve (or create) the conversation session (A5). Prior turns are
    // replayed into the runner; this turn is appended below.
    let source = body.source.as_deref().unwrap_or("pensieve");
    let tenant_uuid = state.tenant.as_uuid();
    let sctx = sessions::load_or_create(
        state.pool.as_ref(),
        body.session_id.as_deref(),
        tenant_uuid,
        ANON_USER,
        source,
    )
    .await;
    let session_uuid = sctx.session_id;
    let session_id_str = session_uuid.to_string();
    let user_turn_index = sctx.next_turn_index;
    let assistant_turn_index = user_turn_index + 1;

    info!(run_id = %run_id, session_id = %session_uuid, question = %question, "agent run starting");

    // Record the user turn up-front so it survives even if the run errors.
    sessions::persist_turn(
        state.pool.as_ref(),
        session_uuid,
        tenant_uuid,
        user_turn_index,
        "user",
        &question,
        None,
    )
    .await;

    // Build runner up-front so we can surface init errors as a single-message
    // UI Message Stream (rather than an HTTP 500).
    let runner = match make_runner(
        &state,
        &session_id_str,
        &sctx.history,
        sctx.summary.as_deref(),
    )
    .await
    {
        Ok(r) => r,
        Err(e) => {
            error!(run_id = %run_id, error = %e, "failed to build agent runner");
            let (ui, rx) = ui_stream::channel();
            let mut em = Emitter::new(ui, &run_id.to_string());
            em.session(&session_id_str);
            em.run_error("init_error", &e.to_string());
            em.finish(json!({ "tool_calls": 0, "elapsed_ms": start.elapsed().as_millis() as u64 }));
            // Best-effort persistence — ignore errors here since the
            // primary surface (SSE) already told the client.
            let _ = persist_run(
                state.pool.as_ref(),
                run_id,
                &question,
                &model,
                tenant_uuid,
                Some(session_uuid),
                started_at,
                Utc::now(),
                "error",
                &Value::Null,
                &em.trace_json(),
            )
            .await;
            return ui_stream::response(rx);
        }
    };

    // Spawn the driver task. The task owns the runner and the event loop and
    // drives the UI Message Stream through an `Emitter`; the response is a
    // `tokio_stream::UnboundedReceiverStream` over the parts it produces.
    let (ui, rx) = ui_stream::channel();
    let pool = state.pool.clone();
    let summary_state = state.clone();
    let summary_every = summary_every();

    tokio::spawn(
        async move {
            // Hold the admission permits for the whole run; the slots free when this
            // task ends (success, error, or early return).
            let _agent_permit = agent_permit;
            let _agent_tenant_permit = agent_tenant_permit;
            let mut em = Emitter::new(ui, &run_id.to_string());
            let mut tool_calls: u32 = 0;
            let mut last_run_sql: Option<String> = None;
            let mut final_text: String = String::new();

            // Surface the session id first so the client can capture it even if
            // the run errors mid-stream.
            em.session(&session_id_str);
            em.run_started(&run_id.to_string(), &model, &question);

            let content = Content::new("user").with_text(&question);

            let user_id = match UserId::new(ANON_USER) {
                Ok(u) => u,
                Err(e) => {
                    em.run_error("internal", &format!("user_id: {e}"));
                    finish_and_persist(
                        pool.as_ref(),
                        &mut em,
                        run_id,
                        &question,
                        &model,
                        tenant_uuid,
                        Some(session_uuid),
                        started_at,
                        start,
                        tool_calls,
                        "error",
                    )
                    .await;
                    return;
                }
            };
            let session_id = match SessionId::new(&session_id_str) {
                Ok(s) => s,
                Err(e) => {
                    em.run_error("internal", &format!("session_id: {e}"));
                    finish_and_persist(
                        pool.as_ref(),
                        &mut em,
                        run_id,
                        &question,
                        &model,
                        tenant_uuid,
                        Some(session_uuid),
                        started_at,
                        start,
                        tool_calls,
                        "error",
                    )
                    .await;
                    return;
                }
            };

            let run_future = async {
                let mut stream = match runner.run(user_id, session_id, content).await {
                    Ok(s) => s,
                    Err(e) => {
                        return Err(format!("runner.run: {e}"));
                    }
                };

                while let Some(ev_result) = stream.next().await {
                    let ev = match ev_result {
                        Ok(e) => e,
                        Err(e) => return Err(format!("event: {e}")),
                    };

                    let partial = ev.llm_response.partial;
                    let parts_iter = ev
                        .llm_response
                        .content
                        .iter()
                        .flat_map(|c| c.parts.iter().cloned())
                        .collect::<Vec<Part>>();

                    for part in parts_iter {
                        match part {
                            Part::Text { text } => {
                                // Non-partial Text means we have the complete
                                // (possibly aggregated) turn answer — stash it for
                                // the session record. Either way it streams as a
                                // text delta.
                                if !partial {
                                    final_text.push_str(&text);
                                }
                                em.answer_delta(&text);
                            }
                            Part::Thinking { thinking, .. } => {
                                if include_thinking {
                                    em.thinking_delta(&thinking);
                                }
                            }
                            Part::FunctionCall { name, args, .. } => {
                                tool_calls += 1;
                                if name == "run_sql" {
                                    if let Some(s) = args.get("sql").and_then(|v| v.as_str()) {
                                        last_run_sql = Some(s.to_string());
                                    }
                                }
                                em.tool_call(&name, args, tool_calls);
                                if tool_calls > MAX_TOOL_CALLS {
                                    return Err(format!("tool_loop:{}", tool_calls));
                                }
                            }
                            Part::FunctionResponse {
                                function_response, ..
                            } => {
                                em.tool_result(&function_response.name, function_response.response);
                            }
                            _ => {}
                        }
                    }

                    if ev.is_final_response() {
                        // Fold in any final text even if it arrived as a
                        // non-partial full text part above. Nothing to do
                        // here — we've already pushed it to `final_text`.
                        debug!(run_id = %run_id, "agent emitted is_final_response");
                    }
                }
                Ok::<(), String>(())
            };

            let outcome = tokio::time::timeout(RUN_WALL_CLOCK, run_future).await;

            let (status, status_str): (&str, &str) = match outcome {
                Ok(Ok(())) => ("success", "success"),
                Ok(Err(msg)) if msg.starts_with("tool_loop:") => {
                    em.run_error("tool_loop", &msg);
                    ("budget_exceeded", "budget_exceeded")
                }
                Ok(Err(msg)) => {
                    em.run_error("runner_error", &msg);
                    ("error", "error")
                }
                Err(_elapsed) => {
                    warn!(run_id = %run_id, "agent run exceeded 60s wall clock");
                    em.run_error(
                        "timeout",
                        &format!(
                            "agent run exceeded {}s wall clock budget",
                            RUN_WALL_CLOCK.as_secs()
                        ),
                    );
                    ("budget_exceeded", "budget_exceeded")
                }
            };

            if status == "success" {
                em.answer_final(&final_text, last_run_sql.as_deref(), None);
                // Record the assistant turn, then refresh the rolling summary.
                sessions::persist_turn(
                    pool.as_ref(),
                    session_uuid,
                    tenant_uuid,
                    assistant_turn_index,
                    "assistant",
                    &final_text,
                    None,
                )
                .await;
                sessions::maybe_summarize_detached(summary_state, session_uuid, summary_every);
            }

            finish_and_persist(
                pool.as_ref(),
                &mut em,
                run_id,
                &question,
                &model,
                tenant_uuid,
                Some(session_uuid),
                started_at,
                start,
                tool_calls,
                status_str,
            )
            .await;
        }
        .instrument(agent_span),
    );

    ui_stream::response(rx)
}

#[allow(clippy::too_many_arguments)]
async fn finish_and_persist(
    pool: Option<&PgPool>,
    em: &mut Emitter,
    run_id: uuid::Uuid,
    question: &str,
    model: &str,
    tenant: uuid::Uuid,
    session_id: Option<uuid::Uuid>,
    started_at: chrono::DateTime<Utc>,
    start: Instant,
    tool_calls: u32,
    status: &str,
) {
    let elapsed_ms = start.elapsed().as_millis() as u64;
    let usage_json = json!({
        "run_id": run_id.to_string(),
        "tool_calls": tool_calls,
        "elapsed_ms": elapsed_ms,
    });
    // Closes any open blocks and emits the terminal finish/[DONE] parts.
    em.finish(usage_json.clone());

    let trace_json = em.trace_json();

    if let Err(e) = persist_run(
        pool,
        run_id,
        question,
        model,
        tenant,
        session_id,
        started_at,
        Utc::now(),
        status,
        &usage_json,
        &trace_json,
    )
    .await
    {
        error!(run_id = %run_id, error = %e, "failed to persist agent_runs row");
    }
}

#[allow(clippy::too_many_arguments)]
pub(crate) async fn persist_run(
    pool: Option<&PgPool>,
    run_id: uuid::Uuid,
    question: &str,
    model_id: &str,
    tenant: uuid::Uuid,
    session_id: Option<uuid::Uuid>,
    started_at: chrono::DateTime<Utc>,
    finished_at: chrono::DateTime<Utc>,
    status: &str,
    usage_json: &Value,
    trace_json: &Value,
) -> sqlx::Result<()> {
    let Some(pool) = pool else { return Ok(()) }; // local mode: no run persistence
    sqlx::query(
        r#"
        INSERT INTO agent_runs (
            run_id, tenant_id, question, model_id, auth_subject,
            session_id, started_at, finished_at, status,
            usage_json, trace_json, replay_cache_hit
        ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
        "#,
    )
    .bind(run_id)
    .bind(tenant)
    .bind(question)
    .bind(model_id)
    .bind(ANON_USER)
    .bind(session_id)
    .bind(started_at)
    .bind(finished_at)
    .bind(status)
    .bind(SqlxJson(usage_json.clone()))
    .bind(SqlxJson(trace_json.clone()))
    .bind(false)
    .execute(pool)
    .await
    .map(|_| ())
}

// ---------------------------------------------------------------------------
// GET/DELETE /v1/agent/sessions* — multi-turn session surface (A5)
// ---------------------------------------------------------------------------

fn parse_session_id(raw: &str) -> Result<uuid::Uuid, Response> {
    uuid::Uuid::parse_str(raw).map_err(|_| {
        (
            StatusCode::BAD_REQUEST,
            Json(json!({"error": "invalid session_id (expected uuid)"})),
        )
            .into_response()
    })
}

async fn list_sessions_handler(State(state): State<AgentState>) -> Response {
    let Some(pool) = state.pool.as_ref() else {
        return Json(json!({ "sessions": [] })).into_response(); // local: no session history
    };
    let rows: Vec<(
        uuid::Uuid,
        Option<String>,
        chrono::DateTime<Utc>,
        chrono::DateTime<Utc>,
        String,
        i64,
    )> = match sqlx::query_as(
        r#"
        SELECT s.session_id, s.title, s.created_at, s.last_active, s.source,
               COUNT(t.turn_index) AS turn_count
        FROM agent_sessions s
        LEFT JOIN agent_session_turns t ON t.session_id = s.session_id
        GROUP BY s.session_id
        ORDER BY s.last_active DESC
        LIMIT 200
        "#,
    )
    .fetch_all(pool)
    .await
    {
        Ok(r) => r,
        Err(e) => {
            error!(error = %e, "list sessions failed");
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(json!({"error": e.to_string()})),
            )
                .into_response();
        }
    };
    let sessions: Vec<Value> = rows
        .into_iter()
        .map(|(id, title, created, last_active, source, turn_count)| {
            json!({
                "session_id": id.to_string(),
                "title": title,
                "created_at": created,
                "last_active": last_active,
                "source": source,
                "turn_count": turn_count,
            })
        })
        .collect();
    Json(json!({ "sessions": sessions })).into_response()
}

async fn get_session_handler(
    State(state): State<AgentState>,
    Path(session_id): Path<String>,
) -> Response {
    let sid = match parse_session_id(&session_id) {
        Ok(u) => u,
        Err(resp) => return resp,
    };
    let Some(pool) = state.pool.as_ref() else {
        return (
            StatusCode::NOT_FOUND,
            Json(json!({"error": "session not found"})),
        )
            .into_response();
    };
    let row: Option<(
        Option<String>,
        Option<String>,
        chrono::DateTime<Utc>,
        chrono::DateTime<Utc>,
        String,
        i32,
    )> = sqlx::query_as(
        "SELECT title, rolling_summary, created_at, last_active, source, summary_turn_index \
         FROM agent_sessions WHERE session_id = $1",
    )
    .bind(sid)
    .fetch_optional(pool)
    .await
    .unwrap_or(None);
    match row {
        Some((title, summary, created, last_active, source, summary_idx)) => Json(json!({
            "session_id": sid.to_string(),
            "title": title,
            "rolling_summary": summary,
            "created_at": created,
            "last_active": last_active,
            "source": source,
            "summary_turn_index": summary_idx,
        }))
        .into_response(),
        None => (
            StatusCode::NOT_FOUND,
            Json(json!({"error": "session not found"})),
        )
            .into_response(),
    }
}

async fn get_session_turns_handler(
    State(state): State<AgentState>,
    Path(session_id): Path<String>,
) -> Response {
    let sid = match parse_session_id(&session_id) {
        Ok(u) => u,
        Err(resp) => return resp,
    };
    let Some(pool) = state.pool.as_ref() else {
        return Json(json!({ "session_id": sid.to_string(), "turns": [] })).into_response();
    };
    let rows: Vec<(
        i32,
        String,
        SqlxJson<Value>,
        Option<uuid::Uuid>,
        chrono::DateTime<Utc>,
    )> = sqlx::query_as(
        "SELECT turn_index, role, content_json, run_id, created_at \
         FROM agent_session_turns WHERE session_id = $1 ORDER BY turn_index ASC",
    )
    .bind(sid)
    .fetch_all(pool)
    .await
    .unwrap_or_default();
    let turns: Vec<Value> = rows
        .into_iter()
        .map(|(idx, role, content, run_id, created)| {
            json!({
                "turn_index": idx,
                "role": role,
                "content": content.0,
                "run_id": run_id.map(|r| r.to_string()),
                "created_at": created,
            })
        })
        .collect();
    Json(json!({ "session_id": sid.to_string(), "turns": turns })).into_response()
}

async fn delete_session_handler(
    State(state): State<AgentState>,
    Path(session_id): Path<String>,
) -> Response {
    let sid = match parse_session_id(&session_id) {
        Ok(u) => u,
        Err(resp) => return resp,
    };
    let Some(pool) = state.pool.as_ref() else {
        return (
            StatusCode::NOT_FOUND,
            Json(json!({"error": "session not found"})),
        )
            .into_response();
    };
    // Turns cascade via FK ON DELETE CASCADE. `agent_runs.session_id` is a plain
    // (non-FK) column, so detach it to avoid dangling references.
    let _ = sqlx::query("UPDATE agent_runs SET session_id = NULL WHERE session_id = $1")
        .bind(sid)
        .execute(pool)
        .await;
    match sqlx::query("DELETE FROM agent_sessions WHERE session_id = $1")
        .bind(sid)
        .execute(pool)
        .await
    {
        Ok(r) if r.rows_affected() > 0 => {
            Json(json!({"deleted": true, "session_id": sid.to_string()})).into_response()
        }
        Ok(_) => (
            StatusCode::NOT_FOUND,
            Json(json!({"error": "session not found"})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": e.to_string()})),
        )
            .into_response(),
    }
}

// ---------------------------------------------------------------------------
// GET /v1/agent/runs/:run_id
// ---------------------------------------------------------------------------

async fn run_lookup_handler(
    State(state): State<AgentState>,
    Path(run_id): Path<String>,
) -> Response {
    let uid = match uuid::Uuid::parse_str(&run_id) {
        Ok(u) => u,
        Err(_) => {
            return (
                StatusCode::BAD_REQUEST,
                Json(json!({"error": "invalid run_id (expected uuid)"})),
            )
                .into_response();
        }
    };
    // Local degraded mode: serve the dreaming agent-run trace from the store.
    // The conversation drilldown (`/v1/agent/runs/:agent_run_id`) reads the
    // trace recorded inline by the dreaming run.
    if let Some(store) = state.local_dreaming.as_ref() {
        return match store.get_agent_run(uid) {
            Some((run, trace)) => {
                let started = run.get("started_at").cloned().unwrap_or(Value::Null);
                let finished = run.get("finished_at").cloned().unwrap_or(Value::Null);
                let model = run.get("model").cloned().unwrap_or(Value::Null);
                let status = run.get("status").cloned().unwrap_or(Value::Null);
                Json(json!({
                    "run_id": run_id,
                    "question": run.get("mode").and_then(|m| m.as_str())
                        .map(|m| format!("Dreaming run ({m})")).unwrap_or_else(|| "Dreaming run".into()),
                    "model_id": model,
                    "status": status,
                    "started_at": started,
                    "finished_at": finished,
                    "usage": run.get("stats").cloned().unwrap_or(Value::Null),
                    "trace": trace,
                }))
                .into_response()
            }
            None => (
                StatusCode::NOT_FOUND,
                Json(json!({"error": "run not found", "run_id": run_id})),
            )
                .into_response(),
        };
    }
    let Some(pool) = state.pool.as_ref() else {
        return (
            StatusCode::NOT_FOUND,
            Json(json!({"error": "run not found"})),
        )
            .into_response();
    };

    let row: Option<(
        String,
        String,
        String,
        chrono::DateTime<Utc>,
        chrono::DateTime<Utc>,
        SqlxJson<Value>,
        SqlxJson<Value>,
    )> = match sqlx::query_as(
        r#"
        SELECT question, model_id, status, started_at, finished_at,
               usage_json, trace_json
        FROM agent_runs
        WHERE run_id = $1
        "#,
    )
    .bind(uid)
    .fetch_optional(pool)
    .await
    {
        Ok(r) => r,
        Err(e) => {
            error!(run_id = %run_id, error = %e, "agent_runs lookup failed");
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(json!({"error": e.to_string()})),
            )
                .into_response();
        }
    };
    let Some((question, model, status, started_at, finished_at, usage, trace)) = row else {
        return (
            StatusCode::NOT_FOUND,
            Json(json!({"error": "run not found", "run_id": run_id})),
        )
            .into_response();
    };
    Json(json!({
        "run_id": run_id,
        "question": question,
        "model_id": model,
        "status": status,
        "started_at": started_at,
        "finished_at": finished_at,
        "usage": usage.0,
        "trace": trace.0,
    }))
    .into_response()
}

// ---------------------------------------------------------------------------
// Engine management routes
// ---------------------------------------------------------------------------

/// `GET /engines` — list available providers + their default models + the active config.
async fn list_engines(
    State(state): State<AgentState>,
) -> Result<axum::Json<serde_json::Value>, (axum::http::StatusCode, String)> {
    // Nothing persisted yet (fresh install) is a normal state, not an error —
    // return the provider catalogue with a default-shaped `active` so the
    // settings UI can render the form and save a first config.
    let (active, configured) = match state.engines.get().await {
        Ok(a) => (a, true),
        Err(_) => (
            EngineConfig {
                kind: EngineKind::Ollama,
                model: String::new(),
                credential_id: None,
                host: None,
                extras: serde_json::Value::Null,
            },
            false,
        ),
    };
    // Pass the active Ollama host (if configured) so the catalogue's live
    // `/api/tags` fetch hits the user's actual instance, not the default.
    let ollama_hint = active.host.as_deref();
    let catalogue = engine_catalogue(ollama_hint).await;
    Ok(axum::Json(serde_json::json!({
        "available": catalogue,
        "active": active,
        "configured": configured,
    })))
}

/// `GET /engine` — the persisted EngineConfig (one global row, v1).
async fn get_engine(
    State(state): State<AgentState>,
) -> Result<axum::Json<EngineConfig>, (axum::http::StatusCode, String)> {
    state
        .engines
        .get()
        .await
        .map(axum::Json)
        .map_err(|e| (axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
}

/// `PUT /engine` — save a new EngineConfig.
async fn put_engine(
    State(state): State<AgentState>,
    axum::Json(cfg): axum::Json<EngineConfig>,
) -> Result<axum::Json<EngineConfig>, (axum::http::StatusCode, String)> {
    state
        .engines
        .put(&cfg)
        .await
        .map_err(|e| (axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
    Ok(axum::Json(cfg))
}

/// `POST /engine/test` — dry-run probe against a candidate config without
/// persisting it. Confirms creds resolve + the provider responds.
async fn test_engine(
    State(state): State<AgentState>,
    axum::Json(cfg): axum::Json<EngineConfig>,
) -> Result<axum::Json<serde_json::Value>, (axum::http::StatusCode, String)> {
    use adk_rust::futures::StreamExt;
    use adk_rust::{GenerateContentConfig, LlmRequest};

    // Claude CLI engine: spawn the binary with a 1-token prompt and confirm
    // it streams *any* text and completes without error. Bypasses build_engine
    // entirely because the CLI doesn't go through adk-rust.
    if cfg.kind == EngineKind::ClaudeCli {
        let probe = tokio::time::timeout(std::time::Duration::from_secs(30), async {
            let mut rx = claude_cli::run_stream("ping", Some(&cfg.model), None, None, None)
                .map_err(|e| format!("spawn: {e}"))?;
            let mut got_output = false;
            let mut err: Option<String> = None;
            while let Some(ev) = rx.recv().await {
                match ev {
                    claude_cli::ClaudeEvent::TextStart { .. }
                    | claude_cli::ClaudeEvent::TextDelta { .. } => got_output = true,
                    claude_cli::ClaudeEvent::Error { message } => err = Some(message),
                    claude_cli::ClaudeEvent::Result { is_error: true, .. } => {
                        err.get_or_insert_with(|| "claude reported an error".to_string());
                    }
                    _ => {}
                }
            }
            if let Some(e) = err {
                return Err(e);
            }
            if !got_output {
                return Err("claude produced no output".to_string());
            }
            Ok::<(), String>(())
        })
        .await;

        return match probe {
            Ok(Ok(())) => Ok(axum::Json(serde_json::json!({
                "ok": true,
                "kind": cfg.kind,
                "model": cfg.model,
            }))),
            Ok(Err(msg)) => Err((axum::http::StatusCode::BAD_GATEWAY, msg)),
            Err(_elapsed) => Err((
                axum::http::StatusCode::GATEWAY_TIMEOUT,
                "claude_cli probe timed out after 30s".into(),
            )),
        };
    }

    // adk-rust path for Anthropic / OpenAI / Ollama.
    let resolver = CredentialResolver::new(state.credentials.clone(), state.tenant);
    let key = resolver.resolve(&cfg).await.map_err(|e| {
        (
            axum::http::StatusCode::BAD_REQUEST,
            format!("credential: {e}"),
        )
    })?;
    let llm = build_engine(&cfg, key)
        .map_err(|e| (axum::http::StatusCode::BAD_REQUEST, format!("init: {e}")))?;

    let req = LlmRequest {
        model: cfg.model.clone(),
        contents: vec![Content::new("user").with_text("ping")],
        config: Some(GenerateContentConfig {
            max_output_tokens: Some(1),
            ..Default::default()
        }),
        tools: Default::default(),
    };

    let probe = tokio::time::timeout(std::time::Duration::from_secs(30), async {
        let mut stream = llm
            .generate_content(req, false)
            .await
            .map_err(|e| format!("provider: {e:?}"))?;
        // Probe only the first streamed item (provider reachability/auth check).
        if let Some(item) = stream.next().await {
            item.map_err(|e| format!("stream: {e:?}"))?;
        }
        Ok::<(), String>(())
    })
    .await;

    match probe {
        Ok(Ok(())) => Ok(axum::Json(serde_json::json!({
            "ok": true,
            "kind": cfg.kind,
            "model": cfg.model,
        }))),
        Ok(Err(msg)) => Err((axum::http::StatusCode::BAD_GATEWAY, msg)),
        Err(_elapsed) => Err((
            axum::http::StatusCode::GATEWAY_TIMEOUT,
            "probe timed out after 30s".into(),
        )),
    }
}

// ---------------------------------------------------------------------------
// Skill management routes
// ---------------------------------------------------------------------------

#[derive(Debug, serde::Serialize)]
struct SkillRow {
    name: String,
    description: String,
    source: super::skills::SkillSource,
    path: String,
    enabled: bool,
    /// Truncated body preview for the UI tooltip.
    preview: String,
}

/// `GET /skills` — list every discovered skill on the host, plus its enabled
/// state. Discovery is cheap (filesystem walk), so we run it on every request
/// rather than caching.
async fn list_skills(
    State(state): State<AgentState>,
) -> Result<axum::Json<Vec<SkillRow>>, (axum::http::StatusCode, String)> {
    let enabled = state
        .skills
        .get()
        .await
        .map_err(|e| (axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
    let enabled_set: std::collections::HashSet<&str> = enabled.iter().map(String::as_str).collect();

    let mut rows: Vec<SkillRow> = super::skills::discover_all()
        .into_iter()
        .map(|s| {
            let preview = preview_body(&s.body);
            SkillRow {
                enabled: enabled_set.contains(s.name.as_str()),
                name: s.name,
                description: s.description,
                source: s.source,
                path: s.path,
                preview,
            }
        })
        .collect();
    rows.sort_by(|a, b| a.name.cmp(&b.name));
    Ok(axum::Json(rows))
}

/// `GET /skills/enabled` — just the toggled set, for callers that don't need
/// the full discovery payload.
async fn get_enabled_skills(
    State(state): State<AgentState>,
) -> Result<axum::Json<Vec<String>>, (axum::http::StatusCode, String)> {
    state
        .skills
        .get()
        .await
        .map(axum::Json)
        .map_err(|e| (axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
}

#[derive(Debug, serde::Deserialize)]
struct PutEnabledSkillsBody {
    skills: Vec<String>,
}

/// `PUT /skills/enabled` — replace the toggled set wholesale.
async fn put_enabled_skills(
    State(state): State<AgentState>,
    axum::Json(body): axum::Json<PutEnabledSkillsBody>,
) -> Result<axum::Json<Vec<String>>, (axum::http::StatusCode, String)> {
    state
        .skills
        .put(&body.skills)
        .await
        .map_err(|e| (axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
    Ok(axum::Json(body.skills))
}

fn preview_body(body: &str) -> String {
    let trimmed = body.trim();
    if trimmed.chars().count() <= 200 {
        return trimmed.to_string();
    }
    let mut out: String = trimmed.chars().take(200).collect();
    out.push('…');
    out
}

// ---------------------------------------------------------------------------
// Claude CLI engine — drives the Claude Code agent loop via the
// claude-code-agent-sdk and translates its events into the UI Message Stream.
// Bypasses adk-rust entirely (Claude Code owns its own tool loop). Multi-turn
// context is preserved by resuming Claude's own session (`--resume`).
// ---------------------------------------------------------------------------

async fn ask_via_claude_cli(
    pool: Option<PgPool>,
    question: &str,
    cfg: &EngineConfig,
    resume_session_id: Option<String>,
    mcp_url: Option<String>,
    auth_header: Option<String>,
) -> Response {
    let run_id = uuid::Uuid::new_v4();
    let started_at = Utc::now();
    let start = Instant::now();
    let model = format!("claude_cli/{}", cfg.model);

    info!(run_id = %run_id, model = %model, resume = ?resume_session_id, mcp = mcp_url.is_some(), "claude_cli ask starting");

    let (ui, rx) = ui_stream::channel();
    ui.start(&run_id.to_string());
    ui.start_step();
    ui.data("model", json!({ "model": model }));

    let question_owned = question.to_string();
    let model_label = cfg.model.clone();
    tokio::spawn(async move {
        let mut answer = String::new();
        let mut errored: Option<String> = None;
        // Claude's own session id, echoed to the client so the next turn can
        // resume this conversation.
        let mut claude_session = String::new();
        let mut total_cost_usd: Option<f64> = None;
        let mut num_turns: u32 = 0;

        // Point the agent at our own MCP server so it can query the user's data.
        let mcp = mcp_url.map(|url| claude_cli::McpConfig {
            url,
            auth_header,
            strict: false,
        });

        let mut events = match claude_cli::run_stream(
            &question_owned,
            Some(&model_label),
            resume_session_id.as_deref(),
            None,
            mcp.as_ref(),
        ) {
            Ok(rx) => rx,
            Err(e) => {
                ui.error(&e.to_string());
                ui.data("usage", json!({ "run_id": run_id.to_string(), "elapsed_ms": start.elapsed().as_millis() as u64 }));
                ui.finish_step();
                ui.finish();
                ui.done();
                let _ = persist_run(
                    pool.as_ref(),
                    run_id,
                    &question_owned,
                    &model,
                    pensieve_core::tenant::DEFAULT_TENANT.as_uuid(),
                    None,
                    started_at,
                    Utc::now(),
                    "error",
                    &Value::Null,
                    &json!([{ "event": "run_error", "data": { "message": e.to_string() } }]),
                )
                .await;
                return;
            }
        };

        while let Some(ev) = events.recv().await {
            match ev {
                claude_cli::ClaudeEvent::Init { session_id } => {
                    claude_session = session_id.clone();
                    ui.data("session", json!({ "sessionId": session_id }));
                }
                claude_cli::ClaudeEvent::TextStart { block_id } => ui.text_start(&block_id),
                claude_cli::ClaudeEvent::TextDelta { block_id, text } => {
                    answer.push_str(&text);
                    ui.text_delta(&block_id, &text);
                }
                claude_cli::ClaudeEvent::TextEnd { block_id } => ui.text_end(&block_id),
                claude_cli::ClaudeEvent::ThinkingStart { block_id } => {
                    ui.reasoning_start(&block_id)
                }
                claude_cli::ClaudeEvent::ThinkingDelta { block_id, text } => {
                    ui.reasoning_delta(&block_id, &text)
                }
                claude_cli::ClaudeEvent::ThinkingEnd { block_id } => ui.reasoning_end(&block_id),
                claude_cli::ClaudeEvent::ToolUse { id, name, input } => {
                    ui.tool_input_available(&id, &name, input)
                }
                claude_cli::ClaudeEvent::ToolResult {
                    id,
                    output,
                    is_error,
                } => {
                    if is_error {
                        let txt = output
                            .as_str()
                            .map(str::to_string)
                            .unwrap_or_else(|| output.to_string());
                        ui.tool_output_error(&id, &txt);
                    } else {
                        ui.tool_output_available(&id, output);
                    }
                }
                claude_cli::ClaudeEvent::Result {
                    session_id,
                    total_cost_usd: cost,
                    num_turns: turns,
                    is_error,
                    ..
                } => {
                    if claude_session.is_empty() {
                        claude_session = session_id;
                    }
                    total_cost_usd = cost;
                    num_turns = turns;
                    if is_error {
                        errored.get_or_insert_with(|| "claude reported an error".to_string());
                    }
                }
                claude_cli::ClaudeEvent::Error { message } => {
                    warn!(run_id = %run_id, message = %message, "claude_cli error");
                    ui.error(&message);
                    errored = Some(message);
                }
            }
        }

        ui.data(
            "usage",
            json!({
                "run_id": run_id.to_string(),
                "elapsed_ms": start.elapsed().as_millis() as u64,
                "total_cost_usd": total_cost_usd,
                "num_turns": num_turns,
                "session_id": claude_session,
            }),
        );
        ui.finish_step();
        ui.finish();
        ui.done();

        let _ = persist_run(
            pool.as_ref(),
            run_id,
            &question_owned,
            &model,
            pensieve_core::tenant::DEFAULT_TENANT.as_uuid(),
            None,
            started_at,
            Utc::now(),
            if errored.is_some() {
                "error"
            } else {
                "success"
            },
            &Value::String(answer.clone()),
            &json!([{ "event": "answer_final", "data": { "text": answer } }]),
        )
        .await;
    });

    ui_stream::response(rx)
}