knowyourbusiness 0.2.2

Git-backed, searchable knowledge base and incident tracker for AI agent fleets
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
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
mod audit;
mod config;
mod embed;
mod index;
mod model;
mod store;

use anyhow::Result;
use axum::extract::{Path, Query, State};
use axum::http::StatusCode;
use axum::middleware;
use axum::routing::{get, post};
use axum::{Json, Router};
use serde::Deserialize;
use serde_json::{json, Value};
use std::collections::HashSet;
use std::net::SocketAddr;
use std::sync::Arc;
use tantivy::IndexWriter;
use tokio::sync::Mutex;

pub struct AppState {
    store: store::Store,
    index: index::SearchIndex,
    audit: audit::Audit,
    /// Vectors + model; None when no model is installed (lexical-only mode).
    semantic: Option<embed::Semantic>,
    /// One lock for ALL writes: Tantivy allows a single IndexWriter and git
    /// commits are strictly sequential anyway. Search takes no locks.
    writer: Mutex<IndexWriter>,
}

type St = State<Arc<AppState>>;
type Reply = (StatusCode, Json<Value>);

/// What gets embedded for an entry: the headline plus the head of the body,
/// which is where an entry states what it is about. Incidents also carry
/// their service and hosts — the words people will ask by.
fn embed_text(e: &model::Entry) -> String {
    let body: String = e.body.chars().take(1200).collect();
    if e.is_incident() {
        format!("{}. Incident on {} ({}). {}", e.title, e.service, e.hosts.join(", "), body)
    } else {
        format!("{}. {}", e.title, body)
    }
}

fn vector_docs(st: &AppState) -> Option<Vec<(String, String)>> {
    st.semantic.as_ref()?;
    // live tree + archived incidents/tasks: everything the default search
    // covers must be reachable semantically too
    let mut entries = match st.store.list_head() {
        Ok(v) => v,
        Err(e) => {
            eprintln!("kyb: cannot list canon for embeddings: {e:#}");
            return None;
        }
    };
    match st.store.archived_latest() {
        Ok(mut archived) => entries.append(&mut archived),
        Err(e) => eprintln!("kyb: archived entries not embedded: {e:#}"),
    }
    Some(entries.iter().map(|e| (e.key.clone(), embed_text(e))).collect())
}

async fn finish_vector_rebuild(st: &AppState, job: embed::RebuildJob) {
    let Some(sem) = st.semantic.as_ref() else { return };
    match sem.finish_rebuild(job).await {
        Ok(n) => eprintln!("kyb: embedded {n} entries"),
        Err(e) => eprintln!("kyb: embedding failed, staying lexical: {e:#}"),
    }
}

async fn rebuild_vectors(st: &AppState) {
    let Some(sem) = st.semantic.as_ref() else { return };
    let writer = st.writer.lock().await;
    let Some(docs) = vector_docs(st) else { return };
    let job = sem.begin_rebuild(docs).await;
    drop(writer);
    finish_vector_rebuild(st, job).await;
}

fn build_state(cfg: &config::Config) -> Result<Arc<AppState>> {
    let store = store::Store::open(&cfg.data_dir)?;
    let index = index::SearchIndex::open_or_create(&cfg.index_dir)?;
    let mut writer = index.writer()?;
    // the canon may have been hand-edited while the service was down —
    // always rebuild on start
    let (heads, hist) = index.reindex(&mut writer, &store)?;
    eprintln!("kyb: reindex on start — {heads} head entries, {hist} history versions");
    let audit = audit::Audit::open(&cfg.audit_path)?;
    let semantic = match embed::Semantic::load(&cfg.model_dir) {
        Ok(s) => {
            eprintln!("kyb: semantic search on ({})", cfg.model_dir.display());
            Some(s)
        }
        Err(e) => {
            eprintln!("kyb: lexical-only search ({e})");
            None
        }
    };
    Ok(Arc::new(AppState { store, index, audit, semantic, writer: Mutex::new(writer) }))
}

// No auth on purpose: we listen on 127.0.0.1 / the internal network only
fn build_app(state: Arc<AppState>) -> Router {
    Router::new()
        .route("/healthz", get(healthz))
        .route("/knowledge", post(upsert))
        .route("/knowledge/{key}", get(get_one).delete(remove))
        .route("/knowledge/{key}/history", get(history))
        .route("/incidents", post(upsert_incident).get(list_incidents))
        .route("/incidents/{key}/resolve", post(resolve_incident))
        .route("/tasks", post(upsert_task).get(list_tasks))
        .route("/tasks/{key}/resolve", post(resolve_task))
        .route("/tasks/{key}/transition", post(transition_task))
        .route("/search", get(search))
        .route("/tags", get(tags))
        .route("/reindex", post(reindex))
        .layer(middleware::from_fn_with_state(state.clone(), audit::audit_mw))
        .with_state(state)
}

#[tokio::main]
async fn main() -> Result<()> {
    let cfg = config::Config::from_env();
    let state = build_state(&cfg)?;
    rebuild_vectors(&state).await;
    let app = build_app(state);
    let listener = tokio::net::TcpListener::bind(&cfg.addr).await?;
    eprintln!("kyb: listening on http://{}", cfg.addr);
    // ConnectInfo so the audit log sees the real client IP
    axum::serve(listener, app.into_make_service_with_connect_info::<SocketAddr>()).await?;
    Ok(())
}

fn err500(e: anyhow::Error) -> Reply {
    (StatusCode::INTERNAL_SERVER_ERROR, Json(json!({"error": format!("{e:#}")})))
}

#[derive(Deserialize)]
struct UpsertReq {
    key: String,
    title: String,
    body: String,
    #[serde(default)]
    tags: Vec<String>,
    #[serde(default)]
    refs: Vec<String>,
}

/// What a route hands to [`locked_write`]: the entry to commit plus the extra
/// response fields it wants, or the reply that refuses the write outright.
type Built = Result<(model::Entry, Value), Reply>;

/// The whole read–modify–write of one entry inside a single critical section.
///
/// `build` runs with the global writer lock ALREADY held, so every stored
/// version it reads is still the current one when the commit lands: two
/// partial updates to the same key can no longer both read the same version
/// and silently overwrite each other. A terminal entry is archived in the same
/// section, so no concurrent reopen can slip between the closing commit and
/// the archival and lose its freshly recreated file to it.
///
/// The semantic update gets a generation ticket before the lock is dropped;
/// model computation happens outside the lock and can install its vector only
/// while that ticket still represents the newest committed version.
async fn locked_write(st: &Arc<AppState>, build: impl FnOnce(&AppState) -> Built) -> Reply {
    let mut w = st.writer.lock().await;
    let (entry, extra) = match build(st.as_ref()) {
        Ok(built) => built,
        Err(reply) => return reply,
    };
    let closed = entry.is_closed();
    let key = entry.key.clone();
    let (mut reply, committed) = commit_locked(st, &mut w, entry, extra);
    if closed {
        archive_locked(st, &mut w, &key, &mut reply);
    }
    let pending = match (st.semantic.as_ref(), committed) {
        (Some(sem), Some(entry)) => {
            let ticket = sem.begin_upsert(&entry.key).await;
            Some((ticket, entry))
        }
        _ => None,
    };
    drop(w);
    if let (Some(sem), Some((ticket, entry))) = (st.semantic.as_ref(), pending) {
        if let Err(err) = sem.finish_upsert(ticket, &embed_text(&entry)).await {
            eprintln!("kyb: embedding not updated for {}: {err:#}", entry.key);
        }
    }
    reply
}

/// Shared tail of every write route: validate, commit to git, update the
/// index — all under the caller's writer lock, which the `&mut IndexWriter`
/// proves is held. `extra` lets a route add response fields. Returns the entry
/// that actually landed (None when nothing was committed) so the caller can
/// refresh the vector side once the lock is gone.
fn commit_locked(
    st: &AppState,
    w: &mut IndexWriter,
    entry: model::Entry,
    extra: Value,
) -> (Reply, Option<model::Entry>) {
    if let Err(e) = entry.validate() {
        return ((StatusCode::BAD_REQUEST, Json(json!({"error": e.to_string()}))), None);
    }
    if let Err(e) = validate_task_parent_chain(st, &entry) {
        return ((StatusCode::BAD_REQUEST, Json(json!({"error": e.to_string()}))), None);
    }
    let today = chrono::Local::now().format("%Y-%m-%d").to_string();
    let (c, action) = match st.store.upsert(entry, &today) {
        Err(e) => return (err500(e), None),
        Ok(store::UpsertOutcome::Unchanged(entry)) => {
            return ((StatusCode::OK, Json(json!({"key": entry.key, "changed": false}))), None);
        }
        Ok(store::UpsertOutcome::Created(c)) => (c, "created"),
        Ok(store::UpsertOutcome::Updated(c)) => (c, "updated"),
    };
    // git is already the truth; a broken index heals via /reindex
    if let Err(e) = st
        .index
        .upsert_head(w, &c.entry, &c.sha, c.committed_at)
        .and_then(|_| st.index.commit_and_reload(w))
    {
        return (
            err500(e.context("git committed but the index was not updated — run POST /reindex")),
            None,
        );
    }
    let mut resp = json!({"key": c.entry.key, "sha": c.sha, "changed": true, "action": action});
    if let (Some(obj), Some(add)) = (resp.as_object_mut(), extra.as_object()) {
        obj.extend(add.clone());
    }
    ((StatusCode::OK, Json(resp)), Some(c.entry))
}

async fn upsert(State(st): St, Json(r): Json<UpsertReq>) -> Reply {
    locked_write(&st, move |_| {
        Ok((
            model::Entry {
                key: r.key,
                title: r.title,
                tags: r.tags,
                refs: r.refs,
                body: r.body,
                ..Default::default()
            },
            Value::Null,
        ))
    })
    .await
}

fn default_status() -> String {
    "open".to_string()
}

#[derive(Deserialize)]
struct IncidentReq {
    key: String,
    title: String,
    body: String,
    service: String,
    #[serde(default)]
    hosts: Vec<String>,
    severity: String,
    #[serde(default = "default_status")]
    status: String,
    /// Keys of knowledge entries this incident is tied to.
    #[serde(default)]
    knowledge: Vec<String>,
    /// How it ended; required when status=resolved.
    #[serde(default)]
    resolution: String,
    /// Executable "is it still happening?" check + expected healthy result.
    #[serde(default)]
    detection: String,
    /// Machine-readable poisoned windows: [{scope, from, to}].
    #[serde(default)]
    affected: Vec<model::Window>,
    #[serde(default)]
    started_at: String,
    #[serde(default)]
    detected_at: String,
    #[serde(default)]
    tags: Vec<String>,
    #[serde(default)]
    refs: Vec<String>,
}

fn now_utc() -> String {
    chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
}

/// The stored version of a key: the tree file, or — for archived
/// incidents/tasks — the latest version from history. Lets wholesale updates
/// and resolves work across the archive boundary.
fn stored_version(st: &AppState, key: &str) -> Option<model::Entry> {
    if let Ok(Some(e)) = st.store.get(key) {
        return Some(e);
    }
    st.store.latest_version(key).ok().flatten().filter(|e| e.kind != model::KIND_KNOWLEDGE)
}

/// Server-managed incident timeline. Wholesale upserts must not wipe stamps
/// the server set earlier, so empty timeline fields inherit from the stored
/// version; then status transitions get stamped if the writer left them empty.
fn stamp_timeline(st: &AppState, entry: &mut model::Entry) {
    if let Some(old) = stored_version(st, &entry.key) {
        let inherit = [
            (&mut entry.started_at, old.started_at),
            (&mut entry.detected_at, old.detected_at),
            (&mut entry.mitigated_at, old.mitigated_at),
            (&mut entry.resolved_at, old.resolved_at),
        ];
        for (field, stored) in inherit {
            if field.trim().is_empty() {
                *field = stored;
            }
        }
    }
    if entry.is_incident() && entry.detected_at.trim().is_empty() {
        entry.detected_at = now_utc();
    }
    if entry.status == "mitigated" && entry.mitigated_at.trim().is_empty() {
        entry.mitigated_at = now_utc();
    }
    // resolved incidents and done/dropped tasks both stamp the close time
    if entry.is_closed() && entry.resolved_at.trim().is_empty() {
        entry.resolved_at = now_utc();
    }
}

async fn upsert_incident(State(st): St, Json(r): Json<IncidentReq>) -> Reply {
    locked_write(&st, move |st| {
        let mut entry = model::Entry {
            key: r.key,
            title: r.title,
            kind: model::KIND_INCIDENT.into(),
            service: r.service,
            hosts: r.hosts,
            severity: r.severity,
            status: r.status,
            knowledge: r.knowledge,
            resolution: r.resolution,
            detection: r.detection,
            affected: r.affected,
            started_at: r.started_at,
            detected_at: r.detected_at,
            tags: r.tags,
            refs: r.refs,
            body: r.body,
            ..Default::default()
        };
        stamp_timeline(st, &mut entry);
        // linking to a missing entry is allowed (write the knowledge later),
        // but the writer should know the link is dangling right now
        let mut unknown = vec![];
        for k in &entry.knowledge {
            if model::is_valid_key(k) && !matches!(st.store.get(k), Ok(Some(_))) {
                unknown.push(k.clone());
            }
        }
        // Structure is a convention, not a gate: a report missing its actionable
        // parts is accepted but told exactly what a complete one carries.
        let mut hints = vec![];
        if entry.detection.trim().is_empty() {
            hints.push("no detection: add an executable 'is it still happening?' check with the expected healthy result (--detection)");
        }
        if entry.affected.is_empty() {
            hints.push("no affected windows: if data or a period got poisoned, record {scope,from,to} in --affected so backtests can exclude it programmatically");
        }
        if !entry.body.contains("- [ ]") && !entry.body.contains("- [x]") {
            hints.push("no follow-ups: track loose ends in the body as '- [ ]' checklist items so they are not lost");
        }
        if !entry.body.to_lowercase().contains("root cause") {
            hints.push("no 'Root cause' section: state it and mark the confidence — verified | suspected | unknown");
        }
        let mut extra = serde_json::Map::new();
        if !unknown.is_empty() {
            extra.insert("unknown_knowledge".into(), json!(unknown));
        }
        if !hints.is_empty() {
            extra.insert("hints".into(), json!(hints));
        }
        let extra = if extra.is_empty() { Value::Null } else { Value::Object(extra) };
        Ok((entry, extra))
    })
    .await
}

#[derive(Deserialize)]
struct ResolveReq {
    /// How it ended: what fixed it, or the accepted outcome / closing comment.
    #[serde(default)]
    resolution: String,
    /// Target status; the kind's closing status when omitted
    /// ("resolved" for incidents, "done" for tasks).
    status: Option<String>,
}

/// Archive a closed entry: the file leaves the working tree, the latest
/// version (already committed with the final status) stays in the default
/// search. The live index doc and the vector are intentionally kept.
///
/// Runs inside the closing write's critical section — the `&mut IndexWriter`
/// is the proof the writer lock is held — because a terminal commit and its
/// archival must not be separable: a reopen landing in between would have its
/// recreated file deleted by this archive.
fn archive_locked(st: &AppState, _w: &mut IndexWriter, key: &str, reply: &mut Reply) {
    if reply.0 != StatusCode::OK {
        return;
    }
    match st.store.archive(key) {
        Err(e) => eprintln!("kyb: archive failed for {key}: {e:#}"),
        Ok(None) => {} // already archived
        Ok(Some(sha)) => {
            if let Some(obj) = reply.1.as_object_mut() {
                obj.insert("archived".into(), json!(true));
                obj.insert("archive_sha".into(), json!(sha));
            }
        }
    }
}

/// Close the loop on an incident or a task without resending the whole entry:
/// flip the status, record how it ended; every other field stays as stored.
/// A closing status also archives the entry.
///
/// The stored version is read under the global writer lock and stays under it
/// through the commit and the archival, so a close can neither overwrite a
/// concurrent update it never saw nor archive an entry someone just reopened.
async fn close_entry(st: &Arc<AppState>, key: String, want_task: bool, r: ResolveReq) -> Reply {
    if let Some(resp) = bad_key(&key) {
        return resp;
    }
    locked_write(st, move |st| {
        // archived entries can still be closed again (amended resolution) or
        // parked back to a non-closing status, which reopens the file
        let Some(mut e) = stored_version(st, &key) else {
            return Err((
                StatusCode::NOT_FOUND,
                Json(json!({"error": format!("no entry '{key}'")})),
            ));
        };
        if want_task != e.is_task() || (!want_task && !e.is_incident()) {
            let what = if want_task { "a task" } else { "an incident report" };
            return Err((
                StatusCode::BAD_REQUEST,
                Json(json!({"error": format!("'{key}' is not {what}")})),
            ));
        }
        let default_close = if want_task { "done" } else { "resolved" };
        e.status = r.status.unwrap_or_else(|| default_close.to_string());
        // the stored reason describes the block the task is leaving; carrying it
        // into another status would commit a state that contradicts itself
        if e.status != model::STATUS_BLOCKED {
            e.blocked_reason.clear();
        }
        // an empty resolution keeps whatever was recorded before, so closing
        // twice never wipes the outcome
        if !r.resolution.trim().is_empty() {
            e.resolution = r.resolution;
        }
        stamp_timeline(st, &mut e);
        // loose ends do not block a close, but they must not go silent either
        let open = e.open_followups();
        let extra = if open > 0 && e.is_closed() {
            json!({
                "open_followups": open,
                "warning": format!("{open} unfinished follow-up(s) (`- [ ]`) remain in the body — reassign or finish them"),
            })
        } else {
            Value::Null
        };
        Ok((e, extra))
    })
    .await
}

async fn resolve_incident(
    State(st): St,
    Path(key): Path<String>,
    Json(r): Json<ResolveReq>,
) -> Reply {
    close_entry(&st, key, false, r).await
}

async fn resolve_task(State(st): St, Path(key): Path<String>, Json(r): Json<ResolveReq>) -> Reply {
    close_entry(&st, key, true, r).await
}

/// A parent link may point forward — a child can be filed before the task it
/// hangs under — so a missing parent is reported back, never refused. An
/// archived (closed) parent counts as known: it is still part of the record.
fn unknown_parent(st: &AppState, entry: &model::Entry) -> Option<String> {
    let parent = entry.parent_task.trim();
    if parent.is_empty() || !model::is_valid_key(parent) {
        return None;
    }
    stored_version(st, parent).is_none().then(|| parent.to_string())
}

/// Keep task relationships a tree/forest, not merely a set of individually
/// valid edges. This runs while the global write lock is held, so two
/// concurrent updates cannot each observe the other edge as absent and commit
/// a cycle together. A missing parent remains a valid forward reference.
fn validate_task_parent_chain(st: &AppState, entry: &model::Entry) -> Result<()> {
    if !entry.is_task() || entry.parent_task.is_empty() {
        return Ok(());
    }
    let mut cursor = entry.parent_task.clone();
    let mut seen = HashSet::new();
    while !cursor.is_empty() {
        if cursor == entry.key {
            anyhow::bail!(
                "parent_task would create a cycle containing '{}'",
                entry.key
            );
        }
        if !seen.insert(cursor.clone()) {
            anyhow::bail!("parent_task points into an existing task cycle at '{cursor}'");
        }
        let Some(parent) = stored_version(st, &cursor) else {
            break;
        };
        cursor = parent.parent_task;
    }
    Ok(())
}

#[derive(Deserialize)]
struct TaskReq {
    key: String,
    title: String,
    body: String,
    #[serde(default = "default_status")]
    status: String,
    /// Optional ranking: "" | low | medium | high | critical.
    #[serde(default)]
    priority: String,
    /// What the task waits on; only valid together with status=blocked.
    #[serde(default)]
    blocked_reason: String,
    /// Who holds the task: a short, stable, non-secret label. Empty = unclaimed.
    #[serde(default)]
    assignee: String,
    /// The `task-` key this task hangs under; empty = top-level.
    #[serde(default)]
    parent_task: String,
    /// Keys of knowledge entries this task concerns.
    #[serde(default)]
    knowledge: Vec<String>,
    /// What came of it; required when closing (done/dropped).
    #[serde(default)]
    resolution: String,
    #[serde(default)]
    tags: Vec<String>,
    #[serde(default)]
    refs: Vec<String>,
}

async fn upsert_task(State(st): St, Json(r): Json<TaskReq>) -> Reply {
    locked_write(&st, move |st| {
        let mut entry = model::Entry {
            key: r.key,
            title: r.title,
            kind: model::KIND_TASK.into(),
            status: r.status,
            priority: r.priority,
            blocked_reason: r.blocked_reason,
            assignee: r.assignee.trim().to_string(),
            parent_task: r.parent_task.trim().to_string(),
            knowledge: r.knowledge,
            resolution: r.resolution,
            tags: r.tags,
            refs: r.refs,
            body: r.body,
            ..Default::default()
        };
        // tasks carry one server stamp: when they were closed
        if entry.is_closed() && entry.resolved_at.trim().is_empty() {
            if let Ok(Some(old)) = st.store.get(&entry.key) {
                entry.resolved_at = old.resolved_at;
            }
            if entry.resolved_at.trim().is_empty() {
                entry.resolved_at = now_utc();
            }
        }
        let mut unknown = vec![];
        for k in &entry.knowledge {
            if model::is_valid_key(k) && !matches!(st.store.get(k), Ok(Some(_))) {
                unknown.push(k.clone());
            }
        }
        let mut extra = serde_json::Map::new();
        if !unknown.is_empty() {
            extra.insert("unknown_knowledge".into(), json!(unknown));
        }
        if let Some(parent) = unknown_parent(st, &entry) {
            extra.insert("unknown_parent".into(), json!(parent));
        }
        let extra = if extra.is_empty() { Value::Null } else { Value::Object(extra) };
        Ok((entry, extra))
    })
    .await
}

/// A partial task update: move the task between the LIVE statuses and, when the
/// request says so, change who holds it or what it hangs under. Everything the
/// request does not mention — title, body, tags, priority, knowledge, refs —
/// stays exactly as stored, so claiming a task never means resending (or
/// paraphrasing, or losing) the task being claimed.
#[derive(Deserialize)]
struct TransitionReq {
    status: String,
    /// Present = set it ("" hands the task back to nobody); absent = keep stored.
    assignee: Option<String>,
    /// Present = set it ("" detaches the task); absent = keep stored.
    parent_task: Option<String>,
    /// Only meaningful with status=blocked; leaving blocked always clears it.
    blocked_reason: Option<String>,
}

/// Closing stays a separate act on purpose: `done`/`dropped` demand an outcome,
/// which is what POST /tasks/{key}/resolve (`kyb done`) is for.
async fn transition_task(
    State(st): St,
    Path(key): Path<String>,
    Json(r): Json<TransitionReq>,
) -> Reply {
    if let Some(resp) = bad_key(&key) {
        return resp;
    }
    let status = r.status.trim().to_string();
    if model::TASK_TERMINAL_STATUSES.contains(&status.as_str()) {
        return (
            StatusCode::BAD_REQUEST,
            Json(json!({"error": format!(
                "'{status}' closes a task and needs an outcome — use `kyb done {key}` (POST /tasks/{key}/resolve) with a resolution; a transition only moves between {}",
                model::TASK_LIVE_STATUSES.join("|")
            )})),
        );
    }
    if !model::TASK_LIVE_STATUSES.contains(&status.as_str()) {
        return (
            StatusCode::BAD_REQUEST,
            Json(json!({"error": format!(
                "status must be one of: {}", model::TASK_LIVE_STATUSES.join("|")
            )})),
        );
    }
    // The stored version is read with the writer lock already held and the
    // commit lands before it is released: two transitions on one key are a
    // strict sequence, so the second sees the first instead of overwriting it.
    locked_write(&st, move |st| {
        // an archived task can be picked back up: the stored version comes from
        // history and the write recreates the file
        let Some(mut e) = stored_version(st, &key) else {
            return Err((
                StatusCode::NOT_FOUND,
                Json(json!({"error": format!("no entry '{key}'")})),
            ));
        };
        if !e.is_task() {
            return Err((
                StatusCode::BAD_REQUEST,
                Json(json!({"error": format!("'{key}' is not a task")})),
            ));
        }
        e.status = status;
        if let Some(assignee) = r.assignee {
            e.assignee = assignee.trim().to_string();
        }
        if let Some(parent) = r.parent_task {
            e.parent_task = parent.trim().to_string();
        }
        // a reason survives only inside the block it describes
        if e.status == model::STATUS_BLOCKED {
            if let Some(reason) = r.blocked_reason {
                e.blocked_reason = reason;
            }
        } else {
            e.blocked_reason.clear();
        }
        let mut extra = serde_json::Map::new();
        extra.insert("status".into(), json!(e.status));
        extra.insert("assignee".into(), json!(e.assignee));
        if let Some(parent) = unknown_parent(st, &e) {
            extra.insert("unknown_parent".into(), json!(parent));
        }
        Ok((e, Value::Object(extra)))
    })
    .await
}

#[derive(Deserialize)]
struct IncidentsQ {
    status: Option<String>,
    service: Option<String>,
    /// Exact task priority; incidents carry none, so it never matches there.
    priority: Option<String>,
    /// Exact task ownership filters — same story: task-only fields.
    assignee: Option<String>,
    parent_task: Option<String>,
    /// followups=open keeps only reports with unfinished `- [ ]` items —
    /// the loose ends a session can pick up (archived included).
    followups: Option<String>,
    /// all=true includes archived (closed) entries; the default shows only
    /// what is live in the canon.
    all: Option<String>,
    limit: Option<usize>,
}

/// Open followups of a body: `- [ ]` checklist items (same rule as
/// Entry::open_followups, but hits carry plain text).
fn open_followups_of(body: &str) -> usize {
    body.lines().filter(|l| l.trim_start().starts_with("- [ ]")).count()
}

/// Listings come from the index, not the tree, so archived (closed) entries
/// stay part of the record — but the DEFAULT view is what is live in the
/// canon. Archived rows appear with `?all=true`, with an explicit `?status=`
/// filter, or in the loose-ends view (`?followups=open`). Open first,
/// freshest on top within a group.
fn list_kind(
    st: &AppState,
    kind: &str,
    status_order: &[&str],
    q: &IncidentsQ,
) -> Result<Vec<(index::Hit, bool)>, Reply> {
    // an empty ?status= from the CLI is "no filter", not "match nothing"
    let status = q.status.as_deref().filter(|s| !s.trim().is_empty());
    let service = q.service.as_deref().filter(|s| !s.trim().is_empty());
    let priority = q.priority.as_deref().filter(|s| !s.trim().is_empty());
    let assignee = q.assignee.as_deref().map(str::trim).filter(|s| !s.is_empty());
    let parent = q.parent_task.as_deref().map(str::trim).filter(|s| !s.is_empty());
    let only_open_followups = q.followups.as_deref() == Some("open");
    let all = matches!(q.all.as_deref(), Some("true" | "1" | "yes"));
    let opts = index::SearchOpts {
        limit: 500,
        kind: Some(kind.to_string()),
        status: status.map(String::from),
        service: service.map(String::from),
        priority: priority.map(String::from),
        assignee: assignee.map(String::from),
        parent_task: parent.map(String::from),
        ..Default::default()
    };
    let mut hits = match st.index.search("", &opts) {
        Err(e) => return Err(err500(e)),
        Ok(h) => h,
    };
    if only_open_followups {
        hits.retain(|h| open_followups_of(&h.body) > 0);
    }
    let mut rows: Vec<(index::Hit, bool)> = hits
        .into_iter()
        .map(|h| {
            let archived = is_archived(st, &h.key);
            (h, archived)
        })
        .collect();
    if !all && status.is_none() && !only_open_followups {
        rows.retain(|(_, archived)| !archived);
    }
    let rank = |s: &str| status_order.iter().position(|x| *x == s).unwrap_or(9);
    rows.sort_by(|(a, _), (b, _)| {
        rank(&a.status)
            .cmp(&rank(&b.status))
            // committed_at is exact (ISO); updated_at only has day granularity
            .then(b.committed_at.cmp(&a.committed_at))
            .then(b.updated_at.cmp(&a.updated_at))
            .then(a.key.cmp(&b.key))
    });
    rows.truncate(q.limit.unwrap_or(50).min(200));
    Ok(rows)
}

/// `archived` on a listing row: the file is gone from the tree, the entry
/// lives on in the index and in git history.
fn is_archived(st: &AppState, key: &str) -> bool {
    !matches!(st.store.get(key), Ok(Some(_)))
}

async fn list_incidents(State(st): St, Query(q): Query<IncidentsQ>) -> Reply {
    let rows = match list_kind(&st, model::KIND_INCIDENT, &model::STATUSES, &q) {
        Err(r) => return r,
        Ok(h) => h,
    };
    let rows: Vec<Value> = rows
        .iter()
        .map(|(h, archived)| {
            json!({
                "key": h.key, "title": h.title, "service": h.service, "hosts": h.hosts,
                "severity": h.severity, "status": h.status, "knowledge": h.knowledge,
                "resolution": h.resolution, "detection": h.detection, "affected": h.affected,
                "started_at": h.started_at, "detected_at": h.detected_at,
                "mitigated_at": h.mitigated_at, "resolved_at": h.resolved_at,
                "open_followups": open_followups_of(&h.body),
                "tags": h.tags, "updated_at": h.updated_at,
                "archived": archived,
            })
        })
        .collect();
    (StatusCode::OK, Json(json!({"count": rows.len(), "incidents": rows})))
}

async fn list_tasks(State(st): St, Query(q): Query<IncidentsQ>) -> Reply {
    let rows = match list_kind(&st, model::KIND_TASK, &model::TASK_STATUSES, &q) {
        Err(r) => return r,
        Ok(h) => h,
    };
    let rows: Vec<Value> = rows
        .iter()
        .map(|(h, archived)| {
            json!({
                "key": h.key, "title": h.title, "status": h.status,
                "priority": h.priority, "blocked_reason": h.blocked_reason,
                "assignee": h.assignee, "parent_task": h.parent_task,
                "knowledge": h.knowledge, "resolution": h.resolution,
                "resolved_at": h.resolved_at,
                "open_followups": open_followups_of(&h.body),
                "tags": h.tags, "updated_at": h.updated_at,
                "archived": archived,
            })
        })
        .collect();
    (StatusCode::OK, Json(json!({"count": rows.len(), "tasks": rows})))
}

#[derive(Deserialize)]
struct GetQ {
    at: Option<String>,
}

fn bad_key(key: &str) -> Option<Reply> {
    if model::is_valid_key(key) {
        None
    } else {
        Some((StatusCode::BAD_REQUEST, Json(json!({"error": "invalid key (slug [a-z0-9-])"}))))
    }
}

fn entry_json(e: &model::Entry, archived: bool) -> Value {
    let mut out = json!({
        "key": e.key, "title": e.title, "kind": e.kind, "tags": e.tags, "refs": e.refs,
        "updated_at": e.updated_at, "body": e.body,
    });
    let obj = out.as_object_mut().expect("out is an object");
    if e.is_incident() {
        obj.insert("service".into(), json!(e.service));
        obj.insert("hosts".into(), json!(e.hosts));
        obj.insert("severity".into(), json!(e.severity));
        obj.insert("detection".into(), json!(e.detection));
        obj.insert("affected".into(), json!(e.affected));
        obj.insert("started_at".into(), json!(e.started_at));
        obj.insert("detected_at".into(), json!(e.detected_at));
        obj.insert("mitigated_at".into(), json!(e.mitigated_at));
    }
    if e.is_task() {
        obj.insert("priority".into(), json!(e.priority));
        obj.insert("blocked_reason".into(), json!(e.blocked_reason));
        obj.insert("assignee".into(), json!(e.assignee));
        obj.insert("parent_task".into(), json!(e.parent_task));
    }
    if e.is_incident() || e.is_task() {
        obj.insert("status".into(), json!(e.status));
        obj.insert("knowledge".into(), json!(e.knowledge));
        obj.insert("resolution".into(), json!(e.resolution));
        obj.insert("resolved_at".into(), json!(e.resolved_at));
        obj.insert("open_followups".into(), json!(e.open_followups()));
    }
    if archived {
        obj.insert("archived".into(), json!(true));
    }
    out
}

async fn get_one(State(st): St, Path(key): Path<String>, Query(q): Query<GetQ>) -> Reply {
    if let Some(r) = bad_key(&key) {
        return r;
    }
    let res = match &q.at {
        Some(rev) => st.store.get_at(&key, rev),
        None => st.store.get(&key),
    };
    match res {
        Err(e) => err500(e),
        Ok(Some(e)) => (StatusCode::OK, Json(entry_json(&e, false))),
        Ok(None) if q.at.is_none() => {
            // not in the tree — an archived incident/task is still readable
            // from history; deleted knowledge stays a 404 (retraction)
            match st.store.latest_version(&key) {
                Err(e) => err500(e),
                Ok(Some(e)) if e.kind != model::KIND_KNOWLEDGE => {
                    (StatusCode::OK, Json(entry_json(&e, true)))
                }
                _ => (StatusCode::NOT_FOUND, Json(json!({"error": format!("no entry '{key}'")}))),
            }
        }
        Ok(None) => {
            (StatusCode::NOT_FOUND, Json(json!({"error": format!("no entry '{key}'")})))
        }
    }
}

async fn history(State(st): St, Path(key): Path<String>) -> Reply {
    if let Some(r) = bad_key(&key) {
        return r;
    }
    match st.store.history(&key) {
        Err(e) => err500(e),
        Ok(v) if v.is_empty() => {
            (StatusCode::NOT_FOUND, Json(json!({"error": format!("no history for '{key}'")})))
        }
        Ok(v) => (StatusCode::OK, Json(json!({"key": key, "versions": v}))),
    }
}

#[derive(Deserialize)]
struct SearchQ {
    q: Option<String>,
    tag: Option<String>,
    history: Option<bool>,
    limit: Option<usize>,
    /// sort=recent orders by commit time instead of relevance
    sort: Option<String>,
    /// semantic=false forces pure lexical search
    semantic: Option<bool>,
    /// kind=knowledge|incident; absent = both
    kind: Option<String>,
    /// incident filters (exact terms)
    status: Option<String>,
    service: Option<String>,
    /// task filters (exact terms)
    priority: Option<String>,
    assignee: Option<String>,
    parent_task: Option<String>,
}

async fn search(State(st): St, Query(p): Query<SearchQ>) -> Reply {
    let tags: Vec<String> = p
        .tag
        .map(|t| t.split(',').map(|s| s.trim().to_string()).filter(|s| !s.is_empty()).collect())
        .unwrap_or_default();
    let q = p.q.as_deref().unwrap_or("");
    let limit = p.limit.unwrap_or(10).min(100);
    // an empty ?sort= from the CLI means "unspecified"
    let sort = p.sort.as_deref().filter(|s| !s.trim().is_empty());
    // relevance is meaningless without a query: an empty q is a listing,
    // and listings read newest first
    let recent = sort == Some("recent") || (q.trim().is_empty() && sort.is_none());
    let history = p.history.unwrap_or(false);
    // Semantic retrieval covers current knowledge only: history questions are
    // "what did this say back then", which is a lexical, not a fuzzy, ask.
    let want_semantic =
        !recent && !history && !q.trim().is_empty() && st.semantic.is_some() && p.semantic != Some(false);
    // an empty ?kind= from the CLI is "no filter", not "match nothing"
    let norm = |o: &Option<String>| o.clone().filter(|s| !s.trim().is_empty());
    let opts = index::SearchOpts {
        tags: tags.clone(),
        history,
        limit: if want_semantic { limit.max(24) } else { limit },
        recent,
        kind: norm(&p.kind),
        status: norm(&p.status),
        service: norm(&p.service),
        priority: norm(&p.priority),
        assignee: norm(&p.assignee),
        parent_task: norm(&p.parent_task),
    };
    let mut hits = match st.index.search(q, &opts) {
        Err(e) => return err500(e),
        Ok(h) => h,
    };
    let mut semantic_used = false;
    if want_semantic {
        semantic_used = hybrid(&st, q, &opts, limit, &mut hits).await;
    }
    hits.truncate(limit);
    let count = hits.len();
    (StatusCode::OK, Json(json!({"count": count, "semantic": semantic_used, "hits": hits})))
}

/// Blend lexical hits with vector search over the whole base.
///
/// The vector side can contribute entries BM25 never saw — that is the point:
/// a question phrased in Russian, or in words the entry does not use, has no
/// lexical anchor at all. Failure is never fatal; the lexical order is already
/// a valid answer, so a broken model just means plain BM25.
async fn hybrid(
    st: &Arc<AppState>,
    q: &str,
    opts: &index::SearchOpts,
    limit: usize,
    hits: &mut Vec<index::Hit>,
) -> bool {
    let Some(sem) = st.semantic.as_ref() else { return false };
    let scored = match sem.search(q, limit.max(24)).await {
        Ok(s) => s,
        Err(e) => {
            eprintln!("kyb: semantic search skipped: {e:#}");
            return false;
        }
    };
    let sims: std::collections::HashMap<&str, f32> =
        scored.iter().map(|(k, s)| (k.as_str(), *s)).collect();
    let lexical_keys: Vec<String> = hits.iter().map(|h| h.key.clone()).collect();
    let mut by_key: std::collections::HashMap<String, index::Hit> =
        hits.drain(..).map(|h| (h.key.clone(), h)).collect();

    for key in embed::fuse_lists(&lexical_keys, &scored, 0.6) {
        let hit = match by_key.remove(&key) {
            Some(h) => Some(h),
            // semantic-only candidate: materialize its live doc (tree version
            // or archived latest), but only if it passes the same filters the
            // lexical side applied
            None => match st.index.get_live(&key) {
                Ok(Some(h)) => {
                    let keeps = opts
                        .tags
                        .iter()
                        .all(|t| h.tags.iter().any(|x| x.eq_ignore_ascii_case(t)))
                        && opts.kind.as_deref().is_none_or(|k| h.kind == k)
                        && opts.status.as_deref().is_none_or(|s| h.status == s)
                        && opts.priority.as_deref().is_none_or(|p| h.priority == p)
                        && opts.assignee.as_deref().is_none_or(|a| h.assignee == a)
                        && opts.parent_task.as_deref().is_none_or(|p| h.parent_task == p)
                        && opts
                            .service
                            .as_deref()
                            .is_none_or(|s| h.service.eq_ignore_ascii_case(s));
                    keeps.then_some(h)
                }
                _ => None,
            },
        };
        if let Some(mut h) = hit {
            // report similarity when we have one: it explains the placement
            if let Some(s) = sims.get(h.key.as_str()) {
                h.score = *s;
            }
            hits.push(h);
            if hits.len() >= limit {
                break;
            }
        }
    }
    true
}

/// Which topics does the base actually cover? Without this an agent can only
/// filter by a tag it already guessed. Counted from the canon, not the index.
async fn tags(State(st): St) -> Reply {
    let entries = match st.store.list_head() {
        Err(e) => return err500(e),
        Ok(v) => v,
    };
    let mut counts: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
    for e in &entries {
        for t in &e.tags {
            *counts.entry(t.to_lowercase()).or_default() += 1;
        }
    }
    let mut rows: Vec<(String, usize)> = counts.into_iter().collect();
    rows.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0)));
    let tags: Vec<Value> = rows.into_iter().map(|(t, c)| json!({"tag": t, "count": c})).collect();
    (StatusCode::OK, Json(json!({"count": tags.len(), "tags": tags})))
}

/// DELETE by kind: knowledge is retracted (drops out of the default search),
/// an incident/task is archived (its latest version stays searchable).
async fn remove(State(st): St, Path(key): Path<String>) -> Reply {
    if let Some(r) = bad_key(&key) {
        return r;
    }
    let mut w = st.writer.lock().await;
    let entry = match st.store.get(&key) {
        Err(e) => return err500(e),
        Ok(None) => {
            return (StatusCode::NOT_FOUND, Json(json!({"error": format!("no entry '{key}'")})))
        }
        Ok(Some(e)) => e,
    };
    if entry.kind == model::KIND_KNOWLEDGE {
        match st.store.delete(&key) {
            Err(e) => err500(e),
            Ok(None) => {
                (StatusCode::NOT_FOUND, Json(json!({"error": format!("no entry '{key}'")})))
            }
            Ok(Some(sha)) => {
                st.index.delete_head(&mut w, &key);
                if let Err(e) = st.index.commit_and_reload(&mut w) {
                    return err500(
                        e.context("git committed but the index was not updated — run POST /reindex"),
                    );
                }
                if let Some(sem) = st.semantic.as_ref() {
                    sem.invalidate(&key).await;
                }
                drop(w);
                (StatusCode::OK, Json(json!({"key": key, "deleted": true, "sha": sha})))
            }
        }
    } else {
        // live index doc and vector stay: archived means findable
        match st.store.archive(&key) {
            Err(e) => err500(e),
            Ok(None) => {
                (StatusCode::NOT_FOUND, Json(json!({"error": format!("no entry '{key}'")})))
            }
            Ok(Some(sha)) => {
                (StatusCode::OK, Json(json!({"key": key, "archived": true, "sha": sha})))
            }
        }
    }
}

async fn reindex(State(st): St) -> Reply {
    let mut w = st.writer.lock().await;
    let res = st.index.reindex(&mut w, &st.store);
    let vector_job = match (&res, st.semantic.as_ref()) {
        (Ok(_), Some(sem)) => match vector_docs(&st) {
            Some(docs) => Some(sem.begin_rebuild(docs).await),
            None => None,
        },
        _ => None,
    };
    drop(w);
    match res {
        Err(e) => err500(e),
        Ok((heads, hist)) => {
            if let Some(job) = vector_job {
                finish_vector_rebuild(&st, job).await;
            }
            (StatusCode::OK, Json(json!({"ok": true, "head_docs": heads, "history_docs": hist})))
        }
    }
}

async fn healthz(State(st): St) -> Reply {
    let heads = st.store.list_head().unwrap_or_default();
    let open_incidents =
        heads.iter().filter(|e| e.is_incident() && e.status == "open").count();
    // every live status counts: a blocked task is still work waiting on someone
    let open_tasks = heads.iter().filter(|e| e.is_live_task()).count();
    let index_docs = st.index.reader.searcher().num_docs();
    let last = st.store.head_info().ok().flatten();
    (
        StatusCode::OK,
        Json(json!({
            "ok": true,
            "entries": heads.len(),
            "open_incidents": open_incidents,
            "open_tasks": open_tasks,
            "index_docs": index_docs,
            "last_commit": last.map(|(sha, time)| json!({"sha": sha, "time": time})),
        })),
    )
}

#[cfg(test)]
mod api_tests {
    use super::*;
    use axum::body::Body;
    use axum::http::Request;
    use http_body_util::BodyExt;
    use rstest::rstest;
    use tower::util::ServiceExt;

    async fn call(app: &Router, method: &str, uri: &str, body: Option<Value>) -> (StatusCode, Value) {
        let b = Request::builder().method(method).uri(uri);
        let req = match body {
            Some(v) => b
                .header("content-type", "application/json")
                .body(Body::from(v.to_string()))
                .unwrap(),
            None => b.body(Body::empty()).unwrap(),
        };
        let resp = app.clone().oneshot(req).await.unwrap();
        let status = resp.status();
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        let val: Value = serde_json::from_slice(&bytes).unwrap_or(Value::Null);
        (status, val)
    }

    // The title is Russian on purpose: one API-level ru-stem check below
    // searches «стримах» and must match «стримы» from this title.
    fn upsert_body(key: &str, body: &str) -> Value {
        json!({"key": key, "title": "NATS стримы", "body": body, "tags": ["nats"]})
    }

    /// Same app, plus the state behind it — the interleaving tests need the
    /// writer mutex itself to hold requests at a chosen point.
    fn app_with_state() -> (Router, Arc<AppState>, tempfile::TempDir, tempfile::TempDir) {
        let data = tempfile::tempdir().unwrap();
        let idx = tempfile::tempdir().unwrap();
        let cfg = config::Config {
            data_dir: data.path().to_path_buf(),
            index_dir: idx.path().to_path_buf(),
            audit_path: idx.path().join("audit.jsonl"),
            // API tests assert lexical behaviour; point at a dir with no model
            model_dir: idx.path().join("no-model"),
            addr: String::new(),
        };
        let state = build_state(&cfg).unwrap();
        (build_app(state.clone()), state, data, idx)
    }

    fn app_with_tmp() -> (Router, tempfile::TempDir, tempfile::TempDir) {
        let (app, _state, data, idx) = app_with_state();
        (app, data, idx)
    }

    #[tokio::test]
    async fn api_full_flow() {
        let (app, _data, _idx) = app_with_tmp();

        // healthz on an empty registry
        let (st, v) = call(&app, "GET", "/healthz", None).await;
        assert_eq!(st, StatusCode::OK);
        assert_eq!(v["entries"], 0);

        // create
        let (st, v) = call(&app, "POST", "/knowledge", Some(upsert_body("nats-streams", "Primary version about streams."))).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        assert_eq!(v["action"], "created");
        let sha1 = v["sha"].as_str().unwrap().to_string();
        assert_eq!(sha1.len(), 40);

        // same content -> no-op
        let (_, v) = call(&app, "POST", "/knowledge", Some(upsert_body("nats-streams", "Primary version about streams."))).await;
        assert_eq!(v["changed"], false);

        // update -> new sha
        let (_, v) = call(&app, "POST", "/knowledge", Some(upsert_body("nats-streams", "Secondary version: the rule moved."))).await;
        assert_eq!(v["action"], "updated");
        let sha2 = v["sha"].as_str().unwrap().to_string();
        assert_ne!(sha1, sha2);

        // get current
        let (st, v) = call(&app, "GET", "/knowledge/nats-streams", None).await;
        assert_eq!(st, StatusCode::OK);
        assert!(v["body"].as_str().unwrap().contains("Secondary"));

        // get old version by sha
        let (_, v) = call(&app, "GET", &format!("/knowledge/nats-streams?at={sha1}"), None).await;
        assert!(v["body"].as_str().unwrap().contains("Primary"));

        // history: 2 versions
        let (_, v) = call(&app, "GET", "/knowledge/nats-streams/history", None).await;
        assert_eq!(v["versions"].as_array().unwrap().len(), 2);

        // API-level russian stem: query «стримах» matches «стримы» in the title
        let (_, v) = call(&app, "GET", "/search?q=%D1%81%D1%82%D1%80%D0%B8%D0%BC%D0%B0%D1%85", None).await;
        assert_eq!(v["count"], 1);
        assert_eq!(v["hits"][0]["key"], "nats-streams");
        assert_eq!(v["hits"][0]["is_head"], true);

        // old text: absent in HEAD, present in history
        let q_old = "/search?q=Primary";
        let (_, v) = call(&app, "GET", q_old, None).await;
        assert_eq!(v["count"], 0);
        let (_, v) = call(&app, "GET", &format!("{q_old}&history=true"), None).await;
        assert!(v["count"].as_u64().unwrap() >= 1);
        assert_eq!(v["hits"][0]["is_head"], false);
        assert_eq!(v["hits"][0]["sha"], sha1);

        // tag filter
        let (_, v) = call(&app, "GET", "/search?q=nats&tag=nats", None).await;
        assert_eq!(v["count"], 1);
        let (_, v) = call(&app, "GET", "/search?q=nats&tag=missing", None).await;
        assert_eq!(v["count"], 0);

        // delete: head goes away, history remains searchable
        let (_, v) = call(&app, "DELETE", "/knowledge/nats-streams", None).await;
        assert_eq!(v["deleted"], true);
        let (st, _) = call(&app, "GET", "/knowledge/nats-streams", None).await;
        assert_eq!(st, StatusCode::NOT_FOUND);
        let (_, v) = call(&app, "GET", "/search?q=nats", None).await;
        assert_eq!(v["count"], 0);
        let (_, v) = call(&app, "GET", "/search?q=nats&history=true", None).await;
        assert!(v["count"].as_u64().unwrap() >= 2);

        // reindex from git restores the picture
        let (_, v) = call(&app, "POST", "/reindex", None).await;
        assert_eq!(v["ok"], true);
        assert_eq!(v["head_docs"], 0);
        assert_eq!(v["history_docs"], 2);

        // validation: bad key and secret in body -> 400
        let (st, _) = call(&app, "POST", "/knowledge", Some(json!({"key": "Bad Key", "title": "t", "body": "x"}))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST);
        let (st, v) = call(&app, "POST", "/knowledge", Some(json!({"key": "leak", "title": "t", "body": "token: ghp_abcdefghijklmnopqrstuvwxyz123456"}))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST);
        assert!(v["error"].as_str().unwrap().contains("secret"));

        // 404 on missing
        let (st, _) = call(&app, "GET", "/knowledge/nope", None).await;
        assert_eq!(st, StatusCode::NOT_FOUND);
        let (st, _) = call(&app, "GET", "/knowledge/nope/history", None).await;
        assert_eq!(st, StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn api_edge_cases() {
        let (app, _data, _idx) = app_with_tmp();

        call(&app, "POST", "/knowledge", Some(upsert_body("alpha", "body one"))).await;

        // unknown/garbage rev in ?at= -> 404, not 500
        let (st, _) = call(
            &app,
            "GET",
            "/knowledge/alpha?at=deadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
            None,
        )
        .await;
        assert_eq!(st, StatusCode::NOT_FOUND);
        let (st, _) = call(&app, "GET", "/knowledge/alpha?at=zzz", None).await;
        assert_eq!(st, StatusCode::NOT_FOUND);

        // delete -> re-add with the same key: created again, chain kept in history
        call(&app, "DELETE", "/knowledge/alpha", None).await;
        let (_, v) = call(&app, "POST", "/knowledge", Some(upsert_body("alpha", "body two"))).await;
        assert_eq!(v["action"], "created");
        let (_, v) = call(&app, "GET", "/knowledge/alpha/history", None).await;
        let changes: Vec<&str> = v["versions"]
            .as_array()
            .unwrap()
            .iter()
            .map(|x| x["change"].as_str().unwrap())
            .collect();
        assert_eq!(changes, vec!["added", "deleted", "added"]);

        // empty q = list everything current; limit works
        call(&app, "POST", "/knowledge", Some(upsert_body("beta", "another entry"))).await;
        let (_, v) = call(&app, "GET", "/search?q=", None).await;
        assert_eq!(v["count"], 2);
        let (_, v) = call(&app, "GET", "/search?q=&limit=1", None).await;
        assert_eq!(v["count"], 1);

        // broken query syntax must not fail the search (lenient parser)
        let (st, _) = call(&app, "GET", "/search?q=title%3A%28%28%28%20AND%20OR", None).await;
        assert_eq!(st, StatusCode::OK);

        // tags are case-insensitive
        call(
            &app,
            "POST",
            "/knowledge",
            Some(json!({"key": "gamma", "title": "t", "body": "tagged body", "tags": ["Infra"]})),
        )
        .await;
        let (_, v) = call(&app, "GET", "/search?q=&tag=infra", None).await;
        assert_eq!(v["count"], 1);

        // garbage payload -> 4xx, not 500
        let (st, _) = call(&app, "POST", "/knowledge", Some(json!({"key": "x"}))).await;
        assert!(st.is_client_error());

        // rm of a missing key -> 404
        let (st, _) = call(&app, "DELETE", "/knowledge/void", None).await;
        assert_eq!(st, StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn audit_log_written() {
        let (app, _data, idx) = app_with_tmp();
        call(&app, "GET", "/healthz", None).await;
        call(&app, "POST", "/knowledge", Some(upsert_body("k", "plain body"))).await;
        call(&app, "GET", "/search?q=plain", None).await;

        let content = std::fs::read_to_string(idx.path().join("audit.jsonl")).unwrap();
        let lines: Vec<&str> = content.lines().collect();
        assert_eq!(lines.len(), 2, "healthz must not be audited:\n{content}");
        let first: Value = serde_json::from_str(lines[0]).unwrap();
        assert_eq!(first["method"], "POST");
        assert_eq!(first["path"], "/knowledge");
        assert_eq!(first["status"], 200);
        assert!(first["ts"].as_str().unwrap().contains('T'));
        let second: Value = serde_json::from_str(lines[1]).unwrap();
        assert_eq!(second["path"], "/search");
        assert!(second["query"].as_str().unwrap().starts_with("q="));
    }

    // --- traversal and garbage keys in path params: 400, not a filesystem walk ---
    #[rstest]
    #[case("GET", "/knowledge/..%2F..%2Fetc")]
    #[case("GET", "/knowledge/.git")]
    #[case("GET", "/knowledge/UPPER")]
    #[case("GET", "/knowledge/a.b")]
    #[case("DELETE", "/knowledge/..%2Fx")]
    #[case("DELETE", "/knowledge/a_b")]
    #[case("GET", "/knowledge/..%2Fx/history")]
    #[case("GET", "/knowledge/%D0%BA%D0%BB%D1%8E%D1%87")]
    #[tokio::test]
    async fn traversal_and_bad_keys_rejected(#[case] method: &str, #[case] uri: &str) {
        let (app, _data, _idx) = app_with_tmp();
        let (st, _) = call(&app, method, uri, None).await;
        assert_eq!(st, StatusCode::BAD_REQUEST, "{method} {uri}");
    }

    // --- status codes per route ---
    #[rstest]
    #[case("GET", "/nope", 404)]
    #[case("POST", "/search", 405)]
    #[case("DELETE", "/search", 405)]
    #[case("GET", "/reindex", 405)]
    #[case("PUT", "/knowledge", 405)]
    #[case("DELETE", "/knowledge", 405)]
    #[case("POST", "/knowledge/some-key", 405)]
    #[case("POST", "/healthz", 405)]
    #[case("GET", "/knowledge/no-such-key", 404)]
    #[case("GET", "/knowledge/no-such-key/history", 404)]
    #[case("DELETE", "/knowledge/no-such-key", 404)]
    #[tokio::test]
    async fn route_status_matrix(#[case] method: &str, #[case] uri: &str, #[case] expect: u16) {
        let (app, _data, _idx) = app_with_tmp();
        let (st, _) = call(&app, method, uri, None).await;
        assert_eq!(st.as_u16(), expect, "{method} {uri}");
    }

    // --- upsert payload validation ---
    #[rstest]
    #[case(json!({"key": "k"}), 422)] // required fields missing
    #[case(json!({"key": "k", "title": "t"}), 422)] // no body
    #[case(json!({"key": "", "title": "t", "body": "x"}), 400)]
    #[case(json!({"key": "K", "title": "t", "body": "x"}), 400)]
    #[case(json!({"key": "../up", "title": "t", "body": "x"}), 400)]
    #[case(json!({"key": "k", "title": "", "body": "x"}), 400)]
    #[case(json!({"key": "k", "title": "   ", "body": "x"}), 400)]
    #[case(json!({"key": "k", "title": "t", "body": "password: super123secret"}), 400)]
    #[case(json!({"key": "k", "title": "t", "body": "x", "refs": ["ghp_abcdefghijklmnopqrstuvwxyz1234"]}), 400)]
    #[case(json!({"key": "k", "title": "t", "body": "clean body"}), 200)]
    #[case(json!({"key": "k2", "title": "t", "body": ""}), 200)] // empty body is allowed
    #[case(json!({"key": "k3", "title": "t", "body": "x", "tags": [], "refs": []}), 200)]
    #[tokio::test]
    async fn upsert_validation_matrix(#[case] payload: Value, #[case] expect: u16) {
        let (app, _data, _idx) = app_with_tmp();
        let (st, v) = call(&app, "POST", "/knowledge", Some(payload)).await;
        assert_eq!(st.as_u16(), expect, "response: {v}");
    }

    // 12 parallel writes to ONE key: the mutex serializes them, history is complete
    #[tokio::test]
    async fn api_concurrent_same_key() {
        let (app, _data, _idx) = app_with_tmp();
        let mut set = tokio::task::JoinSet::new();
        for i in 0..12 {
            let app = app.clone();
            set.spawn(async move {
                let body = json!({
                    "key": "same-key",
                    "title": "Shared key",
                    "body": format!("unique body {i}"),
                });
                call(&app, "POST", "/knowledge", Some(body)).await
            });
        }
        while let Some(res) = set.join_next().await {
            let (st, v) = res.unwrap();
            assert_eq!(st, StatusCode::OK, "{v}");
            assert_eq!(v["changed"], true);
        }
        // all 12 versions landed in git sequentially, none lost
        let (_, v) = call(&app, "GET", "/knowledge/same-key/history", None).await;
        assert_eq!(v["versions"].as_array().unwrap().len(), 12);
        let (_, v) = call(&app, "POST", "/reindex", None).await;
        assert_eq!(v["head_docs"], 1);
        assert_eq!(v["history_docs"], 12);
        // the current body is one of the twelve
        let (_, v) = call(&app, "GET", "/knowledge/same-key", None).await;
        assert!(v["body"].as_str().unwrap().starts_with("unique body"));
    }

    fn incident_body(key: &str, status: &str, body: &str) -> Value {
        json!({
            "key": key, "title": "orders_api OOM on host-a", "body": body,
            "service": "orders_api", "hosts": ["host-a"], "severity": "high", "status": status,
            "knowledge": ["orders-api-architecture"], "tags": ["acme"],
        })
    }

    #[tokio::test]
    async fn incident_full_flow() {
        let (app, _data, _idx) = app_with_tmp();

        // the knowledge entry the incident will link to
        call(&app, "POST", "/knowledge", Some(json!({
            "key": "orders-api-architecture", "title": "orders_api: gRPC gateway",
            "body": "The single write path into ClickHouse.", "tags": ["acme"],
        }))).await;

        // file a report; the link resolves, so no unknown_knowledge warning
        let key = "inc-2026-07-22-orders-api-oom";
        let (st, v) = call(&app, "POST", "/incidents",
            Some(incident_body(key, "open", "What happened: OOM. Workaround: restart."))).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        assert_eq!(v["action"], "created");
        assert!(v.get("unknown_knowledge").is_none(), "{v}");

        // a dangling knowledge link is allowed but reported
        let (_, v) = call(&app, "POST", "/incidents", Some(json!({
            "key": "inc-2026-07-21-landing-gap", "title": "landing gap", "body": "Data gap.",
            "service": "web_app", "severity": "medium",
            "knowledge": ["landing-architecture"],
        }))).await;
        assert_eq!(v["unknown_knowledge"], json!(["landing-architecture"]));

        // status omitted -> open
        let (_, v) = call(&app, "GET", &format!("/knowledge/inc-2026-07-21-landing-gap"), None).await;
        assert_eq!(v["status"], "open");
        assert_eq!(v["kind"], "incident");

        // GET returns the incident fields
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["service"], "orders_api");
        assert_eq!(v["severity"], "high");
        assert_eq!(v["hosts"], json!(["host-a"]));
        assert_eq!(v["knowledge"], json!(["orders-api-architecture"]));

        // a knowledge entry does not grow incident fields
        let (_, v) = call(&app, "GET", "/knowledge/orders-api-architecture", None).await;
        assert_eq!(v["kind"], "knowledge");
        assert!(v.get("status").is_none(), "{v}");

        // healthz counts open incidents
        let (_, v) = call(&app, "GET", "/healthz", None).await;
        assert_eq!(v["open_incidents"], 2);

        // list: open first, filters work; empty params (CLI style) = no filter
        let (_, v) = call(&app, "GET", "/incidents", None).await;
        assert_eq!(v["count"], 2);
        let (_, v) = call(&app, "GET", "/incidents?status=&service=&limit=50", None).await;
        assert_eq!(v["count"], 2, "{v}");
        let (_, v) = call(&app, "GET", "/incidents?service=ORDERS_API", None).await;
        assert_eq!(v["count"], 1, "service filter is case-insensitive: {v}");
        assert_eq!(v["incidents"][0]["key"], key);

        // search: kind filter separates worlds; incident fields ride on hits
        let (_, v) = call(&app, "GET", "/search?q=&kind=incident", None).await;
        assert_eq!(v["count"], 2);
        let (_, v) = call(&app, "GET", "/search?q=&kind=knowledge", None).await;
        assert_eq!(v["count"], 1);
        // empty filter params (what the CLI sends) mean "no filter", not "match nothing"
        let (_, v) = call(&app, "GET", "/search?q=&kind=&status=&service=", None).await;
        assert_eq!(v["count"], 3);
        // free text "orders api" reaches the incident via service/knowledge meta
        let (_, v) = call(&app, "GET", "/search?q=orders%20api&kind=incident&status=open&service=orders_api", None).await;
        assert_eq!(v["count"], 1, "{v}");
        assert_eq!(v["hits"][0]["key"], key);
        assert_eq!(v["hits"][0]["status"], "open");

        // closing without saying how it ended is refused
        let (st, v) = call(&app, "POST", &format!("/incidents/{key}/resolve"), Some(json!({}))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST, "{v}");
        assert!(v["error"].as_str().unwrap().contains("resolution"));

        // close it properly: status flips, the outcome is recorded, and the
        // report is archived — the file leaves the tree
        let (st, v) = call(&app, "POST", &format!("/incidents/{key}/resolve"),
            Some(json!({"resolution": "Raised the memory limit to 2G; fixed the batch flush leak."}))).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        assert_eq!(v["action"], "updated");
        assert_eq!(v["archived"], true, "{v}");
        let (_, v) = call(&app, "GET", "/incidents?status=open", None).await;
        assert_eq!(v["count"], 1);
        let (_, v) = call(&app, "GET", "/healthz", None).await;
        assert_eq!(v["open_incidents"], 1);
        // the archived report still reads in full, marked as archived
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["status"], "resolved");
        assert_eq!(v["archived"], true, "{v}");
        assert!(v["resolution"].as_str().unwrap().contains("memory limit"));
        assert_eq!(v["service"], "orders_api");
        assert_eq!(v["knowledge"], json!(["orders-api-architecture"]));
        assert!(v["body"].as_str().unwrap().contains("What happened"));
        // the default listing shows only live reports; --all adds the archive
        let (_, v) = call(&app, "GET", "/incidents", None).await;
        assert_eq!(v["count"], 1, "archived hidden by default: {v}");
        let (_, v) = call(&app, "GET", "/incidents?all=true", None).await;
        assert_eq!(v["count"], 2);
        assert_eq!(v["incidents"][1]["key"], key);
        assert_eq!(v["incidents"][1]["status"], "resolved");
        assert_eq!(v["incidents"][1]["archived"], true);
        // an explicit status filter looks into the archive on its own
        let (_, v) = call(&app, "GET", "/incidents?status=resolved", None).await;
        assert_eq!(v["count"], 1, "{v}");
        // history: filed, resolved, archived
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}/history"), None).await;
        assert_eq!(v["versions"].as_array().unwrap().len(), 3);
        assert_eq!(v["versions"][0]["change"], "deleted");
        assert!(v["versions"][0]["message"].as_str().unwrap().contains("archive"));

        // the recorded outcome is searchable in the DEFAULT search even though
        // the file is gone — "how did we fix it" lands here
        let (_, v) = call(&app, "GET", "/search?q=memory%20limit&kind=incident", None).await;
        assert_eq!(v["count"], 1, "{v}");
        assert_eq!(v["hits"][0]["resolution"].as_str().unwrap().contains("2G"), true);
        assert_eq!(v["hits"][0]["is_head"], true);

        // parking an archived report back to mitigated reopens it (the file
        // returns); the empty resolution keeps the recorded outcome
        let (_, v) = call(&app, "POST", &format!("/incidents/{key}/resolve"),
            Some(json!({"status": "mitigated"}))).await;
        assert_eq!(v["action"], "created", "reopen recreates the file: {v}");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["status"], "mitigated");
        assert!(v.get("archived").is_none(), "back in the tree: {v}");
        assert!(v["resolution"].as_str().unwrap().contains("memory limit"), "kept: {v}");
    }

    // detection / affected windows / timeline: the "control panel" fields
    #[tokio::test]
    async fn incident_actionable_fields_flow() {
        let (app, _data, _idx) = app_with_tmp();
        let key = "inc-2026-07-22-symbol-mismap";
        let (st, v) = call(&app, "POST", "/incidents", Some(json!({
            "key": key, "title": "prices 50x off after symbol remap", "service": "web_app2",
            "hosts": ["host-b"], "severity": "high",
            "detection": "per (exchange,symbol): price > 50x yesterday max; healthy = 0 rows",
            "affected": [
                {"scope": "okx",    "from": "2026-07-22T08:09:40Z", "to": "2026-07-22T21:09:37Z"},
                {"scope": "gateio", "from": "2026-07-22T08:20:09Z", "to": "2026-07-22T20:04:41Z"},
            ],
            "started_at": "2026-07-22T08:09:40Z",
            "body": "Symptom: heatmap shows +38,000,000% gainers.\n\nFollow-ups:\n- [ ] guard dev NATS\n- [x] webui sanity filter\n",
        }))).await;
        assert_eq!(st, StatusCode::OK, "{v}");

        // GET: everything back; detected_at was stamped by the server
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["affected"].as_array().unwrap().len(), 2);
        assert_eq!(v["affected"][0]["scope"], "okx");
        assert_eq!(v["started_at"], "2026-07-22T08:09:40Z");
        assert!(v["detected_at"].as_str().unwrap().contains('T'), "stamped: {v}");
        assert_eq!(v["resolved_at"], "");
        assert_eq!(v["open_followups"], 1);

        // listing carries the control-panel fields and the followups filter works
        let (_, v) = call(&app, "GET", "/incidents", None).await;
        assert!(v["incidents"][0]["detection"].as_str().unwrap().contains("50x"));
        assert_eq!(v["incidents"][0]["open_followups"], 1);
        let (_, v) = call(&app, "GET", "/incidents?followups=open", None).await;
        assert_eq!(v["count"], 1);

        // search hit carries them too (backtester can read windows from a hit)
        let (_, v) = call(&app, "GET", "/search?q=mismap&kind=incident", None).await;
        assert_eq!(v["hits"][0]["affected"].as_array().unwrap().len(), 2, "{v}");
        assert!(v["hits"][0]["detection"].as_str().unwrap().contains("healthy"));

        // detection text is searchable
        let (_, v) = call(&app, "GET", "/search?q=yesterday%20max", None).await;
        assert_eq!(v["count"], 1);

        // resolve with an open follow-up: closes (and archives), but warns
        let (_, v) = call(&app, "POST", &format!("/incidents/{key}/resolve"),
            Some(json!({"resolution": "Recorders restarted; windows purged from CH."}))).await;
        assert_eq!(v["open_followups"], 1, "{v}");
        assert!(v["warning"].as_str().unwrap().contains("follow-up"));
        assert_eq!(v["archived"], true);
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert!(v["resolved_at"].as_str().unwrap().contains('T'));
        let (_, v) = call(&app, "GET", "/incidents?followups=open", None).await;
        assert_eq!(v["count"], 1, "resolved but loose ends still listed");

        // wholesale re-add without timestamps must NOT wipe server stamps,
        // even across the archive boundary
        let (_, v) = call(&app, "POST", "/incidents", Some(json!({
            "key": key, "title": "prices 50x off after symbol remap", "service": "web_app2",
            "hosts": ["host-b"], "severity": "high", "status": "resolved",
            "resolution": "Recorders restarted; windows purged from CH.",
            "body": "Everything from before, follow-ups all done:\n- [x] guard dev NATS\n",
        }))).await;
        assert_eq!(v["action"], "created", "re-filing an archived report recreates the file: {v}");
        assert_eq!(v["archived"], true, "and a closed status archives it again: {v}");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["started_at"], "2026-07-22T08:09:40Z", "inherited: {v}");
        assert!(v["resolved_at"].as_str().unwrap().contains('T'), "inherited: {v}");
        assert_eq!(v["open_followups"], 0);
        let (_, v) = call(&app, "GET", "/incidents?followups=open", None).await;
        assert_eq!(v["count"], 0);
    }

    // a mitigated transition stamps mitigated_at
    #[tokio::test]
    async fn mitigated_timestamp_stamped() {
        let (app, _data, _idx) = app_with_tmp();
        let key = "inc-2026-07-23-nats-lag";
        call(&app, "POST", "/incidents", Some(json!({
            "key": key, "title": "lag", "body": "x", "service": "nats", "severity": "low",
        }))).await;
        call(&app, "POST", &format!("/incidents/{key}/resolve"),
            Some(json!({"status": "mitigated", "resolution": "hourly restart cron while the fix bakes"}))).await;
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["status"], "mitigated");
        assert!(v["mitigated_at"].as_str().unwrap().contains('T'), "{v}");
        assert_eq!(v["resolved_at"], "");
    }

    #[rstest]
    #[case::window_missing_to(json!({"key": "inc-w", "title": "t", "body": "x", "service": "s", "severity": "low",
        "affected": [{"scope": "okx", "from": "2026-07-22T08:00:00Z", "to": ""}]}), 400)]
    #[case::window_ok(json!({"key": "inc-w", "title": "t", "body": "x", "service": "s", "severity": "low",
        "affected": [{"scope": "okx", "from": "2026-07-22T08:00:00Z", "to": "2026-07-22T09:00:00Z"}]}), 200)]
    #[tokio::test]
    async fn affected_window_validation(#[case] payload: Value, #[case] expect: u16) {
        let (app, _data, _idx) = app_with_tmp();
        let (st, v) = call(&app, "POST", "/incidents", Some(payload)).await;
        assert_eq!(st.as_u16(), expect, "response: {v}");
    }

    // a bare report is accepted but told what a complete one carries;
    // a structured one gets no hints
    #[tokio::test]
    async fn structure_hints() {
        let (app, _data, _idx) = app_with_tmp();
        let (st, v) = call(&app, "POST", "/incidents", Some(json!({
            "key": "inc-2026-07-23-bare", "title": "t", "body": "something broke",
            "service": "s", "severity": "low",
        }))).await;
        assert_eq!(st, StatusCode::OK);
        let hints = v["hints"].as_array().unwrap();
        assert_eq!(hints.len(), 4, "{v}");
        assert!(hints.iter().any(|h| h.as_str().unwrap().contains("detection")));

        let (_, v) = call(&app, "POST", "/incidents", Some(json!({
            "key": "inc-2026-07-23-full", "title": "t",
            "body": "Symptom: x.\nRoot cause (verified): y.\nFollow-ups:\n- [ ] z\n",
            "service": "s", "severity": "low",
            "detection": "check: 0 rows when healthy",
            "affected": [{"scope": "a", "from": "b", "to": "c"}],
        }))).await;
        assert!(v.get("hints").is_none(), "{v}");
    }

    // resolve endpoint edge cases: missing key, not an incident
    #[tokio::test]
    async fn resolve_edge_cases() {
        let (app, _data, _idx) = app_with_tmp();
        let (st, _) = call(&app, "POST", "/incidents/inc-nope/resolve",
            Some(json!({"resolution": "x"}))).await;
        assert_eq!(st, StatusCode::NOT_FOUND);
        call(&app, "POST", "/knowledge", Some(upsert_body("plain", "a fact"))).await;
        let (st, v) = call(&app, "POST", "/knowledge/../resolve", Some(json!({"resolution": "x"}))).await;
        assert!(st.is_client_error(), "{v}");
        let (st, v) = call(&app, "POST", "/incidents/plain/resolve",
            Some(json!({"resolution": "x"}))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST, "{v}");
        assert!(v["error"].as_str().unwrap().contains("not an incident"));
    }

    // --- incident payload validation ---
    #[rstest]
    #[case::key_without_prefix(json!({"key": "orders-api-oom", "title": "t", "body": "x", "service": "s", "severity": "high"}), 400)]
    #[case::missing_service(json!({"key": "inc-a", "title": "t", "body": "x", "severity": "high"}), 422)]
    #[case::empty_service(json!({"key": "inc-a", "title": "t", "body": "x", "service": " ", "severity": "high"}), 400)]
    #[case::bad_severity(json!({"key": "inc-a", "title": "t", "body": "x", "service": "s", "severity": "huge"}), 400)]
    #[case::bad_status(json!({"key": "inc-a", "title": "t", "body": "x", "service": "s", "severity": "low", "status": "wip"}), 400)]
    #[case::bad_knowledge(json!({"key": "inc-a", "title": "t", "body": "x", "service": "s", "severity": "low", "knowledge": ["Bad Key"]}), 400)]
    #[case::secret_in_body(json!({"key": "inc-a", "title": "t", "body": "password: super123secret", "service": "s", "severity": "low"}), 400)]
    #[case::resolved_needs_resolution(json!({"key": "inc-a", "title": "t", "body": "x", "service": "s", "severity": "low", "status": "resolved"}), 400)]
    #[case::resolved_with_resolution(json!({"key": "inc-a", "title": "t", "body": "x", "service": "s", "severity": "low", "status": "resolved", "resolution": "fixed by restart"}), 200)]
    #[case::minimal_ok(json!({"key": "inc-a", "title": "t", "body": "x", "service": "s", "severity": "low"}), 200)]
    #[tokio::test]
    async fn incident_validation_matrix(#[case] payload: Value, #[case] expect: u16) {
        let (app, _data, _idx) = app_with_tmp();
        let (st, v) = call(&app, "POST", "/incidents", Some(payload)).await;
        assert_eq!(st.as_u16(), expect, "response: {v}");
    }

    // the inc- namespace is fenced off from plain knowledge writes
    #[tokio::test]
    async fn knowledge_cannot_take_inc_keys() {
        let (app, _data, _idx) = app_with_tmp();
        let (st, v) = call(&app, "POST", "/knowledge",
            Some(json!({"key": "inc-2026-07-22-fake", "title": "t", "body": "x"}))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST, "{v}");
        assert!(v["error"].as_str().unwrap().contains("reserved"));
    }

    // filing a report that is already resolved archives it immediately;
    // archived reports stay listed, searchable and readable
    #[tokio::test]
    async fn incident_delete_and_history_search() {
        let (app, _data, _idx) = app_with_tmp();
        let key = "inc-2026-07-20-nats-lag";
        let (_, v) = call(&app, "POST", "/incidents", Some(json!({
            "key": key, "title": "NATS consumer lag", "body": "Consumers fell behind.",
            "service": "nats", "severity": "low", "status": "resolved",
            "resolution": "Consumers caught up after the stream limit was raised.",
        }))).await;
        assert_eq!(v["archived"], true, "{v}");
        // gone from the tree: a second DELETE has nothing to remove
        let (st, _) = call(&app, "DELETE", &format!("/knowledge/{key}"), None).await;
        assert_eq!(st, StatusCode::NOT_FOUND);
        // still part of the record: listed with --all, found by default search
        let (_, v) = call(&app, "GET", "/incidents", None).await;
        assert_eq!(v["count"], 0, "archived hidden from the default listing: {v}");
        let (_, v) = call(&app, "GET", "/incidents?all=true", None).await;
        assert_eq!(v["count"], 1);
        assert_eq!(v["incidents"][0]["archived"], true);
        let (_, v) = call(&app, "GET", "/search?q=consumer&kind=incident", None).await;
        assert_eq!(v["count"], 1, "{v}");
        assert_eq!(v["hits"][0]["is_head"], true);
        assert!(v["hits"][0]["resolution"].as_str().unwrap().contains("stream limit"));
        let (_, v) = call(&app, "GET", "/search?q=consumer&history=true&kind=incident", None).await;
        assert_eq!(v["count"], 1, "{v}");
        assert_eq!(v["hits"][0]["is_head"], false);
        // an OPEN incident deletes as an archive, not a retraction
        let key2 = "inc-2026-07-21-open-one";
        call(&app, "POST", "/incidents", Some(json!({
            "key": key2, "title": "open one", "body": "x", "service": "nats", "severity": "low",
        }))).await;
        let (_, v) = call(&app, "DELETE", &format!("/knowledge/{key2}"), None).await;
        assert_eq!(v["archived"], true, "{v}");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key2}"), None).await;
        assert_eq!(v["archived"], true, "readable after archive: {v}");
    }

    // tasks: the third kind — lightweight notes/ideas with a resolution loop
    #[tokio::test]
    async fn task_full_flow() {
        let (app, _data, _idx) = app_with_tmp();

        // the task- namespace is fenced off from plain knowledge
        let (st, v) = call(&app, "POST", "/knowledge",
            Some(json!({"key": "task-fake", "title": "t", "body": "x"}))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST, "{v}");
        assert!(v["error"].as_str().unwrap().contains("reserved"));
        // and task keys must carry the prefix
        let (st, v) = call(&app, "POST", "/tasks",
            Some(json!({"key": "fix-logs", "title": "t", "body": "x"}))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST, "{v}");

        // file a task; an idea is just a task tagged accordingly
        let key = "task-raise-log-retention";
        let (st, v) = call(&app, "POST", "/tasks", Some(json!({
            "key": key, "title": "Raise container log retention to 72h",
            "body": "Short retention loses evidence.\n\n- [ ] measure log volume first\n",
            "tags": ["idea", "observability"],
            "knowledge": ["web-app-architecture"],
        }))).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        assert_eq!(v["action"], "created");
        assert_eq!(v["unknown_knowledge"], json!(["web-app-architecture"]));

        let (_, v) = call(&app, "GET", "/tasks", None).await;
        assert_eq!(v["count"], 1);
        assert_eq!(v["tasks"][0]["status"], "open");
        assert_eq!(v["tasks"][0]["archived"], false);
        assert_eq!(v["tasks"][0]["open_followups"], 1);
        let (_, v) = call(&app, "GET", "/healthz", None).await;
        assert_eq!(v["open_tasks"], 1);
        let (_, v) = call(&app, "GET", "/search?q=&kind=task", None).await;
        assert_eq!(v["count"], 1);

        // closing without saying what came of it is refused
        let (st, v) = call(&app, "POST", &format!("/tasks/{key}/resolve"), Some(json!({}))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST, "{v}");
        assert!(v["error"].as_str().unwrap().contains("resolution"));

        // done: closed, archived — out of the default listing, kept in --all
        let (st, v) = call(&app, "POST", &format!("/tasks/{key}/resolve"),
            Some(json!({"resolution": "Retention raised to 72h with a 2G disk budget."}))).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        assert_eq!(v["archived"], true, "{v}");
        let (_, v) = call(&app, "GET", "/tasks", None).await;
        assert_eq!(v["count"], 0, "archived hidden by default: {v}");
        let (_, v) = call(&app, "GET", "/tasks?all=true", None).await;
        assert_eq!(v["count"], 1);
        assert_eq!(v["tasks"][0]["status"], "done");
        assert_eq!(v["tasks"][0]["archived"], true);
        let (_, v) = call(&app, "GET", "/healthz", None).await;
        assert_eq!(v["open_tasks"], 0);
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["kind"], "task");
        assert_eq!(v["archived"], true);
        assert!(v["resolved_at"].as_str().unwrap().contains('T'), "stamped: {v}");
        let (_, v) = call(&app, "GET", "/search?q=retention%20disk&kind=task", None).await;
        assert_eq!(v["count"], 1, "{v}");
        assert_eq!(v["hits"][0]["is_head"], true);

        // dropped needs a reason too, and ranks below done in the --all listing
        call(&app, "POST", "/tasks", Some(json!({
            "key": "task-try-foo", "title": "Try foo", "body": "x",
        }))).await;
        let (_, v) = call(&app, "POST", "/tasks/task-try-foo/resolve",
            Some(json!({"status": "dropped", "resolution": "Obsolete after the bar rewrite."}))).await;
        assert_eq!(v["archived"], true, "{v}");
        let (_, v) = call(&app, "GET", "/tasks?all=true", None).await;
        assert_eq!(v["count"], 2);
        assert_eq!(v["tasks"][0]["status"], "done");
        assert_eq!(v["tasks"][1]["status"], "dropped");

        // the incident endpoint refuses tasks
        let (st, v) = call(&app, "POST", &format!("/incidents/{key}/resolve"),
            Some(json!({"resolution": "x"}))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST, "{v}");
        assert!(v["error"].as_str().unwrap().contains("not an incident"));
    }

    // priority + the widened lifecycle: create, filter, transition, archive
    #[tokio::test]
    async fn task_priority_and_status_flow() {
        let (app, _data, _idx) = app_with_tmp();

        // priority rides through create -> GET -> listing -> search
        let key = "task-raise-log-retention";
        let (st, v) = call(&app, "POST", "/tasks", Some(json!({
            "key": key, "title": "Raise container log retention to 72h",
            "body": "Short retention loses evidence.", "priority": "high",
        }))).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["priority"], "high");
        assert_eq!(v["blocked_reason"], "");
        assert_eq!(v["status"], "open");

        // an unranked task is the default and stays empty, never guessed
        call(&app, "POST", "/tasks", Some(json!({
            "key": "task-try-foo", "title": "Try foo", "body": "an idea",
        }))).await;
        let (_, v) = call(&app, "GET", "/knowledge/task-try-foo", None).await;
        assert_eq!(v["priority"], "", "{v}");

        // a second ranked task, plus one that is in flight
        call(&app, "POST", "/tasks", Some(json!({
            "key": "task-swap-disk", "title": "Swap the failing disk", "body": "x",
            "priority": "critical", "status": "in_progress",
        }))).await;

        // the exact priority filter on the listing
        let (_, v) = call(&app, "GET", "/tasks?priority=high", None).await;
        assert_eq!(v["count"], 1, "{v}");
        assert_eq!(v["tasks"][0]["key"], key);
        assert_eq!(v["tasks"][0]["priority"], "high");
        let (_, v) = call(&app, "GET", "/tasks?priority=low", None).await;
        assert_eq!(v["count"], 0);
        // an empty ?priority= (what the CLI sends) is "no filter"
        let (_, v) = call(&app, "GET", "/tasks?status=&priority=&limit=50", None).await;
        assert_eq!(v["count"], 3, "{v}");
        // it combines with the status filter
        let (_, v) = call(&app, "GET", "/tasks?status=in_progress&priority=critical", None).await;
        assert_eq!(v["count"], 1, "{v}");
        assert_eq!(v["tasks"][0]["key"], "task-swap-disk");
        // and search takes the same exact filter
        let (_, v) = call(&app, "GET", "/search?q=&kind=task&priority=critical", None).await;
        assert_eq!(v["count"], 1, "{v}");
        assert_eq!(v["hits"][0]["key"], "task-swap-disk");
        assert_eq!(v["hits"][0]["priority"], "critical");

        // block it: the reason is recorded and comes back everywhere
        let (st, v) = call(&app, "POST", "/tasks", Some(json!({
            "key": "task-swap-disk", "title": "Swap the failing disk", "body": "x",
            "priority": "critical", "status": "blocked",
            "blocked_reason": "waiting on the replacement disk to arrive",
        }))).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        assert!(v.get("archived").is_none(), "blocked is not an ending: {v}");
        let (_, v) = call(&app, "GET", "/knowledge/task-swap-disk", None).await;
        assert_eq!(v["status"], "blocked");
        assert_eq!(v["blocked_reason"], "waiting on the replacement disk to arrive");
        assert!(v.get("archived").is_none(), "still live in the canon: {v}");
        let (_, v) = call(&app, "GET", "/tasks?status=blocked", None).await;
        assert_eq!(v["tasks"][0]["blocked_reason"], "waiting on the replacement disk to arrive");
        let (_, v) = call(&app, "GET", "/search?q=&kind=task&status=blocked", None).await;
        assert_eq!(v["hits"][0]["blocked_reason"], "waiting on the replacement disk to arrive");

        // all three live statuses are live: listed by default and counted
        let (_, v) = call(&app, "GET", "/tasks", None).await;
        assert_eq!(v["count"], 3, "open + in_progress(now blocked) + open: {v}");
        let (_, v) = call(&app, "GET", "/healthz", None).await;
        assert_eq!(v["open_tasks"], 3, "blocked and in_progress count as work: {v}");

        // moving off blocked clears the stale reason instead of committing a
        // state that contradicts itself
        let (_, v) = call(&app, "POST", "/tasks/task-swap-disk/resolve",
            Some(json!({"status": "in_progress"}))).await;
        assert_eq!(v["changed"], true, "{v}");
        let (_, v) = call(&app, "GET", "/knowledge/task-swap-disk", None).await;
        assert_eq!(v["status"], "in_progress");
        assert_eq!(v["blocked_reason"], "", "stale reason dropped: {v}");
        assert_eq!(v["priority"], "critical", "priority survives the transition: {v}");

        // only the terminal statuses close and archive
        let (_, v) = call(&app, "POST", "/tasks/task-swap-disk/resolve",
            Some(json!({"resolution": "New disk installed and the array rebuilt."}))).await;
        assert_eq!(v["archived"], true, "{v}");
        let (_, v) = call(&app, "GET", "/healthz", None).await;
        assert_eq!(v["open_tasks"], 2);
        let (_, v) = call(&app, "GET", "/tasks", None).await;
        assert_eq!(v["count"], 2, "archived hidden by default: {v}");
        // the archived task keeps its priority in the record
        let (_, v) = call(&app, "GET", "/knowledge/task-swap-disk", None).await;
        assert_eq!(v["archived"], true);
        assert_eq!(v["priority"], "critical", "{v}");
        assert_eq!(v["status"], "done");
        let (_, v) = call(&app, "GET", "/tasks?all=true&priority=critical", None).await;
        assert_eq!(v["count"], 1, "the archive answers the priority filter too: {v}");
        assert_eq!(v["tasks"][0]["archived"], true);

        // closing a blocked task directly: the reason is dropped, not carried
        call(&app, "POST", "/tasks", Some(json!({
            "key": "task-try-foo", "title": "Try foo", "body": "an idea",
            "status": "blocked", "blocked_reason": "waiting on the bar rewrite",
        }))).await;
        let (_, v) = call(&app, "POST", "/tasks/task-try-foo/resolve",
            Some(json!({"status": "dropped", "resolution": "Obsolete after the bar rewrite."}))).await;
        assert_eq!(v["archived"], true, "{v}");
        let (_, v) = call(&app, "GET", "/knowledge/task-try-foo", None).await;
        assert_eq!(v["status"], "dropped");
        assert_eq!(v["blocked_reason"], "", "{v}");
    }

    // ownership and the parent link: create -> GET -> listing -> search
    #[tokio::test]
    async fn task_ownership_and_parent_flow() {
        let (app, _data, _idx) = app_with_tmp();

        // an umbrella task, claimed under a stable public label
        let parent = "task-migrate-logs";
        let (st, v) = call(&app, "POST", "/tasks", Some(json!({
            "key": parent, "title": "Migrate the log pipeline", "body": "The umbrella task.",
            "status": "in_progress", "priority": "high", "assignee": "agent-a",
        }))).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        assert!(v.get("unknown_parent").is_none(), "a top-level task has no parent: {v}");

        // a child hangs under it
        let child = "task-migrate-logs-step-one";
        let (st, v) = call(&app, "POST", "/tasks", Some(json!({
            "key": child, "title": "Step one: measure the volume", "body": "x",
            "assignee": "agent-b", "parent_task": parent,
        }))).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        assert!(v.get("unknown_parent").is_none(), "the parent exists: {v}");

        // a forward reference is reported, not refused — the parent may be
        // filed after the child
        let (st, v) = call(&app, "POST", "/tasks", Some(json!({
            "key": "task-orphan", "title": "Filed before its parent", "body": "x",
            "parent_task": "task-not-written-yet",
        }))).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        assert_eq!(v["unknown_parent"], "task-not-written-yet");

        // GET carries both fields; an unclaimed task carries them empty
        let (_, v) = call(&app, "GET", &format!("/knowledge/{child}"), None).await;
        assert_eq!(v["assignee"], "agent-b");
        assert_eq!(v["parent_task"], parent);
        let (_, v) = call(&app, "GET", "/knowledge/task-orphan", None).await;
        assert_eq!(v["assignee"], "", "{v}");
        assert_eq!(v["parent_task"], "task-not-written-yet");

        // the listing filters exactly
        let (_, v) = call(&app, "GET", "/tasks?assignee=agent-a", None).await;
        assert_eq!(v["count"], 1, "{v}");
        assert_eq!(v["tasks"][0]["key"], parent);
        assert_eq!(v["tasks"][0]["assignee"], "agent-a");
        let (_, v) = call(&app, "GET", &format!("/tasks?parent_task={parent}"), None).await;
        assert_eq!(v["count"], 1, "{v}");
        assert_eq!(v["tasks"][0]["key"], child);
        assert_eq!(v["tasks"][0]["parent_task"], parent);
        let (_, v) = call(&app, "GET", "/tasks?assignee=agent-nobody", None).await;
        assert_eq!(v["count"], 0);
        // empty params (what the CLI sends) are "no filter", not "match nothing"
        let (_, v) = call(&app, "GET", "/tasks?assignee=&parent_task=&limit=50", None).await;
        assert_eq!(v["count"], 3, "{v}");

        // and search takes the same exact filters
        let (_, v) = call(&app, "GET", "/search?q=&kind=task&assignee=agent-b", None).await;
        assert_eq!(v["count"], 1, "{v}");
        assert_eq!(v["hits"][0]["key"], child);
        assert_eq!(v["hits"][0]["assignee"], "agent-b");
        assert_eq!(v["hits"][0]["parent_task"], parent);
        let (_, v) =
            call(&app, "GET", &format!("/search?q=&kind=task&parent_task={parent}"), None).await;
        assert_eq!(v["count"], 1, "{v}");
        let (_, v) = call(&app, "GET", "/search?q=&kind=task&assignee=&parent_task=", None).await;
        assert_eq!(v["count"], 3, "{v}");

        // an archived parent is still a known parent: the record outlives the file
        call(&app, "POST", &format!("/tasks/{parent}/resolve"),
            Some(json!({"resolution": "Pipeline migrated."}))).await;
        let (_, v) = call(&app, "POST", "/tasks", Some(json!({
            "key": "task-migrate-logs-step-two", "title": "Step two", "body": "x",
            "parent_task": parent,
        }))).await;
        assert!(v.get("unknown_parent").is_none(), "an archived parent still exists: {v}");
    }

    // the partial transition: move a task without resending what it says
    #[tokio::test]
    async fn task_transition_partial_updates() {
        let (app, _data, _idx) = app_with_tmp();
        let key = "task-raise-log-retention";
        call(&app, "POST", "/tasks", Some(json!({
            "key": key, "title": "Raise container log retention to 72h",
            "body": "Short retention loses evidence.\n\n- [ ] measure log volume first\n",
            "priority": "high", "tags": ["observability"],
            "knowledge": ["web-app-architecture"], "refs": ["see the compose file on the log host"],
        }))).await;

        // pick it up: status plus an owner label, nothing else resent
        let (st, v) = call(&app, "POST", &format!("/tasks/{key}/transition"),
            Some(json!({"status": "in_progress", "assignee": "agent-a"}))).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        assert_eq!(v["changed"], true);
        assert_eq!(v["action"], "updated");
        assert_eq!(v["status"], "in_progress");
        assert_eq!(v["assignee"], "agent-a");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["status"], "in_progress");
        assert_eq!(v["assignee"], "agent-a");
        assert_eq!(v["title"], "Raise container log retention to 72h", "title kept: {v}");
        assert_eq!(v["priority"], "high", "omitted metadata survives: {v}");
        assert_eq!(v["tags"], json!(["observability"]));
        assert_eq!(v["refs"], json!(["see the compose file on the log host"]));
        assert_eq!(v["knowledge"], json!(["web-app-architecture"]));
        assert!(v["body"].as_str().unwrap().contains("measure log volume"), "body kept: {v}");
        assert_eq!(v["open_followups"], 1);
        assert!(v.get("archived").is_none(), "a live transition never archives: {v}");
        let (_, v) = call(&app, "GET", "/healthz", None).await;
        assert_eq!(v["open_tasks"], 1, "still work in flight: {v}");

        // block it: the reason lands, everything else stays
        let (_, v) = call(&app, "POST", &format!("/tasks/{key}/transition"),
            Some(json!({"status": "blocked", "blocked_reason": "waiting on the disk budget approval"}))).await;
        assert_eq!(v["status"], "blocked", "{v}");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["blocked_reason"], "waiting on the disk budget approval");
        assert_eq!(v["assignee"], "agent-a", "an omitted assignee is kept: {v}");
        assert_eq!(v["priority"], "high");

        // leaving blocked clears the reason even though the request never named it
        let (_, v) = call(&app, "POST", &format!("/tasks/{key}/transition"),
            Some(json!({"status": "in_progress"}))).await;
        assert_eq!(v["changed"], true, "{v}");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["blocked_reason"], "", "stale reason dropped: {v}");
        assert_eq!(v["status"], "in_progress");

        // hand it back: an explicit empty assignee unclaims it
        let (_, v) = call(&app, "POST", &format!("/tasks/{key}/transition"),
            Some(json!({"status": "open", "assignee": ""}))).await;
        assert_eq!(v["assignee"], "", "{v}");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["assignee"], "");
        assert_eq!(v["status"], "open");

        // relate it to a parent without touching anything else
        let (_, v) = call(&app, "POST", &format!("/tasks/{key}/transition"),
            Some(json!({"status": "in_progress", "parent_task": "task-migrate-logs"}))).await;
        assert_eq!(v["unknown_parent"], "task-migrate-logs", "forward reference reported: {v}");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["parent_task"], "task-migrate-logs");
        assert_eq!(v["priority"], "high", "{v}");

        // every transition is a git version: history + get --at reconstruct who
        // held the task, and in which status, at any point
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}/history"), None).await;
        let versions = v["versions"].as_array().unwrap().clone();
        assert_eq!(versions.len(), 6, "create + 5 transitions: {versions:?}");
        let claimed = versions[versions.len() - 2]["sha"].as_str().unwrap().to_string();
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}?at={claimed}"), None).await;
        assert_eq!(v["assignee"], "agent-a", "the earlier owner is reconstructable: {v}");
        assert_eq!(v["status"], "in_progress");

        // closing still goes through resolve, and the record keeps the owner
        let (_, v) = call(&app, "POST", &format!("/tasks/{key}/resolve"),
            Some(json!({"resolution": "Raised to 72h with a 2G disk budget."}))).await;
        assert_eq!(v["archived"], true, "{v}");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["status"], "done");
        assert_eq!(v["parent_task"], "task-migrate-logs", "{v}");

        // an archived task can be picked back up: the file returns
        let (_, v) = call(&app, "POST", &format!("/tasks/{key}/transition"),
            Some(json!({"status": "in_progress", "assignee": "agent-b"}))).await;
        assert_eq!(v["action"], "created", "reopening recreates the file: {v}");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert!(v.get("archived").is_none(), "back in the canon: {v}");
        assert_eq!(v["assignee"], "agent-b");
    }

    // Parent links form a chain, never a ring — for both wholesale writes and
    // partial transitions, including cycles longer than self-parenting.
    #[tokio::test]
    async fn task_parent_cycles_rejected() {
        let (app, _data, _idx) = app_with_tmp();
        for (key, parent) in [
            ("task-project", ""),
            ("task-project-step-one", "task-project"),
            ("task-project-step-two", "task-project-step-one"),
        ] {
            let (st, v) = call(&app, "POST", "/tasks", Some(json!({
                "key": key, "title": key, "body": "x", "parent_task": parent,
            }))).await;
            assert_eq!(st, StatusCode::OK, "{v}");
        }

        // project -> step two -> step one -> project would be a three-node cycle
        let (st, v) = call(&app, "POST", "/tasks", Some(json!({
            "key": "task-project", "title": "task-project", "body": "x",
            "parent_task": "task-project-step-two",
        }))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST, "{v}");
        assert!(v["error"].as_str().unwrap().contains("cycle"), "{v}");

        let (st, v) = call(&app, "POST", "/tasks/task-project/transition", Some(json!({
            "status": "in_progress", "parent_task": "task-project-step-two",
        }))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST, "{v}");
        assert!(v["error"].as_str().unwrap().contains("cycle"), "{v}");

        // Neither rejected write changed the root.
        let (_, v) = call(&app, "GET", "/knowledge/task-project", None).await;
        assert_eq!(v["status"], "open", "{v}");
        assert_eq!(v["parent_task"], "", "{v}");
    }

    // the transition endpoint is not a closing endpoint, and not a way past
    // validation either
    #[tokio::test]
    async fn task_transition_rejections() {
        let (app, _data, _idx) = app_with_tmp();
        let key = "task-swap-disk";
        call(&app, "POST", "/tasks", Some(json!({
            "key": key, "title": "Swap the failing disk", "body": "x", "priority": "critical",
        }))).await;
        call(&app, "POST", "/knowledge", Some(upsert_body("plain-note", "a fact"))).await;

        // the terminal statuses need an outcome — and say where to give one
        for status in ["done", "dropped"] {
            let (st, v) = call(&app, "POST", &format!("/tasks/{key}/transition"),
                Some(json!({"status": status}))).await;
            assert_eq!(st, StatusCode::BAD_REQUEST, "{status}: {v}");
            let err = v["error"].as_str().unwrap();
            assert!(err.contains("kyb done"), "{status}: {err}");
            assert!(err.contains("/resolve"), "{status}: {err}");
        }

        let bad = [
            json!({"status": "wip"}),
            json!({"status": ""}),
            json!({"status": "mitigated"}),
            json!({"status": "in_progress", "assignee": "token: ghp_abcdefghijklmnopqrstuvwxyz123456"}),
            json!({"status": "in_progress", "assignee": "x".repeat(model::ASSIGNEE_MAX_LEN + 1)}),
            json!({"status": "in_progress", "parent_task": "not-a-task-key"}),
            json!({"status": "in_progress", "parent_task": "inc-2026-08-15-oom"}),
        ];
        for payload in bad {
            let (st, v) =
                call(&app, "POST", &format!("/tasks/{key}/transition"), Some(payload.clone())).await;
            assert_eq!(st, StatusCode::BAD_REQUEST, "payload {payload}: {v}");
        }
        // a task cannot hang under itself — the smallest broken tree
        let (st, v) = call(&app, "POST", &format!("/tasks/{key}/transition"),
            Some(json!({"status": "in_progress", "parent_task": key}))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST, "self-parenting: {v}");
        assert!(v["error"].as_str().unwrap().contains("own parent"), "{v}");

        // wrong kind, missing key, garbage key
        let (st, v) = call(&app, "POST", "/tasks/plain-note/transition",
            Some(json!({"status": "in_progress"}))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST, "{v}");
        assert!(v["error"].as_str().unwrap().contains("not a task"), "{v}");
        let (st, _) = call(&app, "POST", "/tasks/task-nope/transition",
            Some(json!({"status": "in_progress"}))).await;
        assert_eq!(st, StatusCode::NOT_FOUND);
        let (st, _) = call(&app, "POST", "/tasks/Bad%20Key/transition",
            Some(json!({"status": "in_progress"}))).await;
        assert_eq!(st, StatusCode::BAD_REQUEST);
        let (st, _) = call(&app, "POST", &format!("/tasks/{key}/transition"), Some(json!({}))).await;
        assert!(st.is_client_error(), "status is required");

        // through all of that the task never moved
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["status"], "open", "{v}");
        assert_eq!(v["assignee"], "");
        assert_eq!(v["parent_task"], "");
        assert_eq!(v["priority"], "critical");
    }

    // tasks written before this feature carry neither field and keep working:
    // they list, they filter, and they can be claimed with one transition
    #[tokio::test]
    async fn legacy_task_without_ownership_still_works() {
        let (app, _data, _idx) = app_with_tmp();
        let key = "task-old-note";
        let (st, v) = call(&app, "POST", "/tasks", Some(json!({
            "key": key, "title": "An old note", "body": "written before ownership existed",
        }))).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["assignee"], "", "{v}");
        assert_eq!(v["parent_task"], "");
        assert_eq!(v["status"], "open");
        let (_, v) = call(&app, "GET", "/tasks", None).await;
        assert_eq!(v["count"], 1);
        assert_eq!(v["tasks"][0]["assignee"], "");
        assert_eq!(v["tasks"][0]["parent_task"], "");

        // claiming it needs neither the title nor the body
        let (st, v) = call(&app, "POST", &format!("/tasks/{key}/transition"),
            Some(json!({"status": "in_progress", "assignee": "agent-a"}))).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["assignee"], "agent-a");
        assert!(v["body"].as_str().unwrap().contains("before ownership"), "{v}");
    }

    // --- task payload validation: the new fields ---
    #[rstest]
    #[case::priority_high(json!({"key": "task-a", "title": "t", "body": "x", "priority": "high"}), 200)]
    #[case::priority_absent(json!({"key": "task-a", "title": "t", "body": "x"}), 200)]
    #[case::priority_empty(json!({"key": "task-a", "title": "t", "body": "x", "priority": ""}), 200)]
    #[case::priority_unknown(json!({"key": "task-a", "title": "t", "body": "x", "priority": "urgent"}), 400)]
    #[case::priority_wrong_case(json!({"key": "task-a", "title": "t", "body": "x", "priority": "HIGH"}), 400)]
    #[case::status_in_progress(json!({"key": "task-a", "title": "t", "body": "x", "status": "in_progress"}), 200)]
    #[case::status_blocked_bare(json!({"key": "task-a", "title": "t", "body": "x", "status": "blocked"}), 200)]
    #[case::status_blocked_with_reason(json!({"key": "task-a", "title": "t", "body": "x", "status": "blocked", "blocked_reason": "waiting on vendor"}), 200)]
    #[case::status_unknown(json!({"key": "task-a", "title": "t", "body": "x", "status": "wip"}), 400)]
    #[case::stale_reason_on_open(json!({"key": "task-a", "title": "t", "body": "x", "blocked_reason": "waiting on vendor"}), 400)]
    #[case::stale_reason_on_in_progress(json!({"key": "task-a", "title": "t", "body": "x", "status": "in_progress", "blocked_reason": "waiting on vendor"}), 400)]
    #[case::stale_reason_on_done(json!({"key": "task-a", "title": "t", "body": "x", "status": "done", "resolution": "shipped", "blocked_reason": "waiting on vendor"}), 400)]
    #[case::secret_in_reason(json!({"key": "task-a", "title": "t", "body": "x", "status": "blocked", "blocked_reason": "password: super123secret"}), 400)]
    #[case::in_progress_needs_no_resolution(json!({"key": "task-a", "title": "t", "body": "x", "status": "in_progress"}), 200)]
    #[case::done_still_needs_resolution(json!({"key": "task-a", "title": "t", "body": "x", "status": "done"}), 400)]
    #[case::assignee_absent(json!({"key": "task-a", "title": "t", "body": "x"}), 200)]
    #[case::assignee_label(json!({"key": "task-a", "title": "t", "body": "x", "assignee": "agent-a"}), 200)]
    #[case::assignee_blank_is_unclaimed(json!({"key": "task-a", "title": "t", "body": "x", "assignee": "   "}), 200)]
    #[case::assignee_secret(json!({"key": "task-a", "title": "t", "body": "x", "assignee": "password: super123secret"}), 400)]
    #[case::parent_absent(json!({"key": "task-a", "title": "t", "body": "x"}), 200)]
    #[case::parent_task_key(json!({"key": "task-a", "title": "t", "body": "x", "parent_task": "task-parent"}), 200)]
    #[case::parent_not_a_task(json!({"key": "task-a", "title": "t", "body": "x", "parent_task": "nats-streams"}), 400)]
    #[case::parent_malformed(json!({"key": "task-a", "title": "t", "body": "x", "parent_task": "task-Bad Key"}), 400)]
    #[case::parent_traversal(json!({"key": "task-a", "title": "t", "body": "x", "parent_task": "../task-x"}), 400)]
    #[case::parent_self(json!({"key": "task-a", "title": "t", "body": "x", "parent_task": "task-a"}), 400)]
    #[tokio::test]
    async fn task_validation_matrix(#[case] payload: Value, #[case] expect: u16) {
        let (app, _data, _idx) = app_with_tmp();
        let (st, v) = call(&app, "POST", "/tasks", Some(payload)).await;
        assert_eq!(st.as_u16(), expect, "response: {v}");
    }

    // the task-only fields must not leak onto the other kinds
    #[rstest]
    #[case::knowledge_priority("/knowledge", json!({"key": "k", "title": "t", "body": "x", "priority": "high"}))]
    #[case::knowledge_assignee("/knowledge", json!({"key": "k", "title": "t", "body": "x", "assignee": "agent-a"}))]
    #[case::knowledge_parent("/knowledge", json!({"key": "k", "title": "t", "body": "x", "parent_task": "task-x"}))]
    #[case::incident_priority("/incidents", json!({"key": "inc-a", "title": "t", "body": "x", "service": "s", "severity": "low", "priority": "high"}))]
    #[case::incident_assignee("/incidents", json!({"key": "inc-a", "title": "t", "body": "x", "service": "s", "severity": "low", "assignee": "agent-a"}))]
    #[case::incident_parent("/incidents", json!({"key": "inc-a", "title": "t", "body": "x", "service": "s", "severity": "low", "parent_task": "task-x"}))]
    #[tokio::test]
    async fn task_only_fields_ignored_on_other_kinds(#[case] path: &str, #[case] payload: Value) {
        let (app, _data, _idx) = app_with_tmp();
        // unknown fields are dropped by the request types, so the write lands
        // clean rather than smuggling a task field into another kind
        let (st, v) = call(&app, "POST", path, Some(payload)).await;
        assert_eq!(st, StatusCode::OK, "{v}");
        let key = v["key"].as_str().unwrap().to_string();
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert!(v.get("priority").is_none(), "priority is task-only: {v}");
        assert!(v.get("blocked_reason").is_none(), "blocked_reason is task-only: {v}");
        assert!(v.get("assignee").is_none(), "assignee is task-only: {v}");
        assert!(v.get("parent_task").is_none(), "parent_task is task-only: {v}");
    }

    #[tokio::test]
    async fn tags_endpoint_and_tag_search() {
        let (app, _data, _idx) = app_with_tmp();
        let post = |k: &str, tags: Value| {
            json!({"key": k, "title": "t", "body": "apples and bicycles", "tags": tags})
        };
        call(&app, "POST", "/knowledge", Some(post("a", json!(["Infra", "nats"])))).await;
        call(&app, "POST", "/knowledge", Some(post("b", json!(["infra"])))).await;
        call(&app, "POST", "/knowledge", Some(post("c", json!(["kubernetes"])))).await;

        // the base can report which topics it covers, most used first
        let (st, v) = call(&app, "GET", "/tags", None).await;
        assert_eq!(st, StatusCode::OK);
        assert_eq!(v["count"], 3);
        assert_eq!(v["tags"][0]["tag"], "infra");
        assert_eq!(v["tags"][0]["count"], 2, "case-folded and counted: {v}");

        // a tag is findable by free text even though no body mentions it
        let (_, v) = call(&app, "GET", "/search?q=kubernetes", None).await;
        assert_eq!(v["count"], 1);
        assert_eq!(v["hits"][0]["key"], "c");

        // deleting an entry drops its tag from the listing
        call(&app, "DELETE", "/knowledge/c", None).await;
        let (_, v) = call(&app, "GET", "/tags", None).await;
        assert_eq!(v["count"], 2);
    }

    #[tokio::test]
    async fn search_sort_recent() {
        let (app, _data, _idx) = app_with_tmp();
        call(&app, "POST", "/knowledge", Some(upsert_body("older", "shared shared shared"))).await;
        call(&app, "POST", "/knowledge", Some(upsert_body("newer", "shared once"))).await;

        let (_, v) = call(&app, "GET", "/search?q=shared", None).await;
        assert_eq!(v["hits"][0]["key"], "older", "relevance favours the denser match");
        let (_, v) = call(&app, "GET", "/search?q=shared&sort=recent", None).await;
        assert_eq!(v["hits"][0]["key"], "newer", "recent sort puts the latest first");
        // an empty query is a listing, and listings read newest first
        let (_, v) = call(&app, "GET", "/search?q=&sort=", None).await;
        assert_eq!(v["hits"][0]["key"], "newer", "empty q defaults to recent: {v}");
    }

    // reindex is idempotent: N runs — same numbers
    #[tokio::test]
    async fn reindex_idempotent() {
        let (app, _data, _idx) = app_with_tmp();
        call(&app, "POST", "/knowledge", Some(upsert_body("a", "one"))).await;
        call(&app, "POST", "/knowledge", Some(upsert_body("a", "two"))).await;
        call(&app, "POST", "/knowledge", Some(upsert_body("b", "three"))).await;
        for _ in 0..3 {
            let (_, v) = call(&app, "POST", "/reindex", None).await;
            assert_eq!(v["head_docs"], 2);
            assert_eq!(v["history_docs"], 3);
        }
        let (_, v) = call(&app, "GET", "/search?q=two", None).await;
        assert_eq!(v["count"], 1, "search must be alive after reindex");
    }

    // 10 parallel writes to distinct keys: the single writer mutex must
    // serialize them with no losses and no broken commits
    #[tokio::test]
    async fn api_concurrent_upserts() {
        let (app, _data, _idx) = app_with_tmp();
        let mut set = tokio::task::JoinSet::new();
        for i in 0..10 {
            let app = app.clone();
            set.spawn(async move {
                let body = json!({
                    "key": format!("key-{i}"),
                    "title": format!("Entry {i}"),
                    "body": format!("body {i}"),
                });
                call(&app, "POST", "/knowledge", Some(body)).await
            });
        }
        while let Some(res) = set.join_next().await {
            let (st, v) = res.unwrap();
            assert_eq!(st, StatusCode::OK, "{v}");
            assert_eq!(v["changed"], true);
        }
        let (_, v) = call(&app, "GET", "/healthz", None).await;
        assert_eq!(v["entries"], 10);
        let (_, v) = call(&app, "GET", "/search?q=&limit=50", None).await;
        assert_eq!(v["count"], 10);
        // history intact: exactly one version per key
        let (_, v) = call(&app, "POST", "/reindex", None).await;
        assert_eq!(v["head_docs"], 10);
        assert_eq!(v["history_docs"], 10);
    }

    // --- same-key interleavings, replayed exactly ---
    //
    // These do not stress the server and hope: the test holds the global writer
    // lock, parks the competing requests on it one after another, and then lets
    // them go. The order is the queue order, so every run reproduces the same
    // interleaving — the one that used to lose a write.

    /// Park a POST on the writer lock the test is holding.
    ///
    /// The test runtime is single-threaded and the request has no other
    /// suspension point on its way to that lock, so by the time the spawned
    /// task has signalled and the scheduler has drained, it is queued on the
    /// mutex — behind whoever was parked before it — with nothing read yet.
    async fn park_on_writer_lock(
        app: &Router,
        uri: String,
        body: Value,
    ) -> tokio::task::JoinHandle<(StatusCode, Value)> {
        let app = app.clone();
        let (started, ready) = tokio::sync::oneshot::channel();
        let task = tokio::spawn(async move {
            let _ = started.send(());
            call(&app, "POST", &uri, Some(body)).await
        });
        ready.await.expect("the request task must start");
        // drain anything already runnable, so the request is parked on the lock
        // before the caller queues the next one behind it
        for _ in 0..8 {
            tokio::task::yield_now().await;
        }
        task
    }

    fn open_task(key: &str) -> Value {
        json!({
            "key": key, "title": "Rotate the internal TLS certificates",
            "body": "They expire in a week.\n\n- [ ] schedule the restart window\n",
            "priority": "high", "tags": ["security"],
        })
    }

    // Two partial transitions on ONE key: the second must build on the version
    // the first committed, not on the one it replaced.
    #[tokio::test]
    async fn concurrent_transitions_keep_the_earlier_update() {
        let (app, state, _data, _idx) = app_with_state();
        let key = "task-rotate-tls-certs";
        let (st, v) = call(&app, "POST", "/tasks", Some(open_task(key))).await;
        assert_eq!(st, StatusCode::OK, "{v}");

        let guard = state.writer.lock().await;
        let claim = park_on_writer_lock(
            &app,
            format!("/tasks/{key}/transition"),
            json!({"status": "in_progress", "assignee": "agent-a"}),
        )
        .await;
        let block = park_on_writer_lock(
            &app,
            format!("/tasks/{key}/transition"),
            json!({"status": "blocked", "blocked_reason": "waiting on the CA"}),
        )
        .await;
        drop(guard); // the claim parked first, so the claim runs first

        let (st, claimed) = claim.await.unwrap();
        assert_eq!(st, StatusCode::OK, "{claimed}");
        let (st, blocked) = block.await.unwrap();
        assert_eq!(st, StatusCode::OK, "{blocked}");
        assert_eq!(claimed["assignee"], "agent-a");
        assert_eq!(blocked["status"], "blocked");
        assert_eq!(
            blocked["assignee"], "agent-a",
            "the second transition read the first one's commit: {blocked}"
        );

        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["status"], "blocked");
        assert_eq!(v["blocked_reason"], "waiting on the CA");
        assert_eq!(v["assignee"], "agent-a", "the claim was not overwritten: {v}");
        assert_eq!(v["priority"], "high", "untouched metadata survives: {v}");
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}/history"), None).await;
        assert_eq!(
            v["versions"].as_array().unwrap().len(),
            3,
            "create plus both transitions, none dropped: {v}"
        );
    }

    // A close archives the entry it committed — never one that has been
    // reopened behind its back.
    #[tokio::test]
    async fn a_close_cannot_archive_a_task_reopened_behind_it() {
        let (app, state, _data, _idx) = app_with_state();
        let key = "task-rotate-tls-certs";
        call(&app, "POST", "/tasks", Some(open_task(key))).await;

        let guard = state.writer.lock().await;
        let close = park_on_writer_lock(
            &app,
            format!("/tasks/{key}/resolve"),
            json!({"resolution": "Rotated by hand; automation filed separately."}),
        )
        .await;
        let reopen = park_on_writer_lock(
            &app,
            format!("/tasks/{key}/transition"),
            json!({"status": "in_progress", "assignee": "agent-b"}),
        )
        .await;
        drop(guard); // close first, reopen second

        let (st, closed) = close.await.unwrap();
        assert_eq!(st, StatusCode::OK, "{closed}");
        assert_eq!(closed["archived"], true, "the close archived what it closed: {closed}");
        let (st, reopened) = reopen.await.unwrap();
        assert_eq!(st, StatusCode::OK, "{reopened}");
        assert_eq!(reopened["status"], "in_progress");

        // the reopen came after the archival and stays: a live task must never
        // be left with its file deleted under it
        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["status"], "in_progress");
        assert_eq!(v["assignee"], "agent-b");
        assert!(v.get("archived").is_none(), "the reopened task is back in the canon: {v}");
        assert!(
            v["resolution"].as_str().unwrap().contains("Rotated by hand"),
            "the recorded outcome survives the reopen: {v}"
        );
        let (_, v) = call(&app, "GET", "/healthz", None).await;
        assert_eq!(v["open_tasks"], 1, "work in flight is still counted: {v}");
        let (_, v) = call(&app, "GET", "/tasks", None).await;
        assert_eq!(v["count"], 1, "and it still lists: {v}");
        assert_eq!(v["tasks"][0]["archived"], false);
    }

    // The mirror image: the reopen wins the lock first, so the close must carry
    // what the reopen wrote instead of the version it read before it.
    #[tokio::test]
    async fn a_close_after_a_reopen_carries_the_reopened_state() {
        let (app, state, _data, _idx) = app_with_state();
        let key = "task-rotate-tls-certs";
        call(&app, "POST", "/tasks", Some(open_task(key))).await;

        let guard = state.writer.lock().await;
        let reopen = park_on_writer_lock(
            &app,
            format!("/tasks/{key}/transition"),
            json!({"status": "in_progress", "assignee": "agent-b"}),
        )
        .await;
        let close = park_on_writer_lock(
            &app,
            format!("/tasks/{key}/resolve"),
            json!({"resolution": "Rotated by hand; automation filed separately."}),
        )
        .await;
        drop(guard); // reopen first, close second

        let (st, reopened) = reopen.await.unwrap();
        assert_eq!(st, StatusCode::OK, "{reopened}");
        let (st, closed) = close.await.unwrap();
        assert_eq!(st, StatusCode::OK, "{closed}");
        assert_eq!(closed["archived"], true, "{closed}");

        let (_, v) = call(&app, "GET", &format!("/knowledge/{key}"), None).await;
        assert_eq!(v["status"], "done");
        assert_eq!(v["archived"], true, "a closed task leaves the tree: {v}");
        assert_eq!(
            v["assignee"], "agent-b",
            "the close committed the claim it never resent: {v}"
        );
        assert!(v["resolution"].as_str().unwrap().contains("Rotated by hand"), "{v}");
        let (_, v) = call(&app, "GET", "/healthz", None).await;
        assert_eq!(v["open_tasks"], 0, "{v}");
        let (_, v) = call(&app, "GET", "/tasks", None).await;
        assert_eq!(v["count"], 0, "closed tasks leave the live listing: {v}");
        let (_, v) = call(&app, "GET", "/tasks?all=true", None).await;
        assert_eq!(v["count"], 1, "and stay in the record: {v}");
        assert_eq!(v["tasks"][0]["assignee"], "agent-b");
    }
}