shellfirm 0.4.2

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

use std::{
    fs::{self, OpenOptions},
    io::Write,
    path::{Path, PathBuf},
};

use crate::error::{Error, Result};
use serde_derive::{Deserialize, Serialize};

use crate::checks::Severity;

/// Version of the on-disk record format written by this build.
/// - 1 (implicit, no field): original format
/// - 2: adds `schema_version`, `user`, `hostname`, `cwd`, `git_repo`,
///   `git_branch` and the optional [`AuditExtra`] fields.
/// - 3: `user` comes from the uid (not `$USER`); adds `uid`, `env_user`,
///   `escalation_reasons`, `command_bytes`, `command_sha256`,
///   `command_truncated`, `command_redacted` and the `NoTerminal` outcome.
/// - 4: adds `version` (the shellfirm version that wrote the record).
pub const AUDIT_SCHEMA_VERSION: u32 = 4;

/// Size after which `audit.log` is rotated to `audit.log.1` (10 MiB).
pub const DEFAULT_MAX_AUDIT_BYTES: u64 = 10 * 1024 * 1024;

/// Rotated generations kept by default (`audit.log.1` … `audit.log.5`).
pub const DEFAULT_MAX_AUDIT_FILES: usize = 5;

/// Longest command stored verbatim in a record (8 KiB). Longer commands are
/// truncated (at a character boundary) and hashed.
pub const MAX_STORED_COMMAND_BYTES: usize = 8 * 1024;

/// How many rotated generations are kept for the log at `audit_path`.
///
/// It is the `audit_max_files` setting of the configuration directory the
/// log lives in (`settings.yaml` next to `audit.log`), so `--config-dir DIR`
/// and `SHELLFIRM_CONFIG_DIR` rotate with DIR's own setting. The file is
/// read only when a rotation is due. The value is clamped to `1..=100`: at
/// least one rotated generation is always kept, so rotation never deletes
/// the history it just rotated (`0`, which the settings parser rejects,
/// means the default). Unset or unreadable: [`DEFAULT_MAX_AUDIT_FILES`].
#[must_use]
pub fn max_files_for(audit_path: &Path) -> usize {
    let configured = audit_path
        .parent()
        .map(|d| d.join("settings.yaml"))
        .and_then(|p| crate::config::read_regular_string(&p).ok())
        .and_then(|c| serde_yaml::from_str::<serde_yaml::Value>(&c).ok())
        .and_then(|v| v.get("audit_max_files").and_then(serde_yaml::Value::as_u64))
        .and_then(|n| usize::try_from(n).ok())
        .filter(|n| *n > 0)
        .unwrap_or(DEFAULT_MAX_AUDIT_FILES);
    clamp_max_files(configured)
}

const fn clamp_max_files(n: usize) -> usize {
    if n == 0 {
        1
    } else if n > MAX_AUDIT_FILES_LIMIT {
        MAX_AUDIT_FILES_LIMIT
    } else {
        n
    }
}

/// Largest accepted `audit_max_files`.
pub const MAX_AUDIT_FILES_LIMIT: usize = 100;

/// The number of rotated generations kept for an `audit_max_files`
/// setting (clamped like [`max_files_for`]).
#[must_use]
pub const fn max_files(setting: usize) -> usize {
    clamp_max_files(setting)
}

/// The outcome of a challenge interaction.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuditOutcome {
    Allowed,
    Denied,
    /// The check matched but was skipped because its severity was below
    /// the configured `min_severity` threshold.
    Skipped,
    /// The challenge was declined or aborted by a human (Esc, Ctrl-C, a
    /// wrong answer given up on). The record is written once, after the
    /// prompt ends; a process killed with `SIGKILL` while the prompt is
    /// shown writes no record (the command does not run either).
    Cancelled,
    /// The decision was handed back to a human (e.g. an AI-agent hook
    /// returned "ask" so the tool's own permission prompt is shown).
    Asked,
    /// Blocked without asking because no terminal was available to answer
    /// the challenge (fail closed). Distinct from `Cancelled`, which is a
    /// human declining or aborting.
    NoTerminal,
}

impl std::fmt::Display for AuditOutcome {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Allowed => write!(f, "ALLOWED"),
            Self::Denied => write!(f, "DENIED"),
            Self::Skipped => write!(f, "SKIPPED"),
            Self::Cancelled => write!(f, "CANCELLED"),
            Self::Asked => write!(f, "ASKED"),
            Self::NoTerminal => write!(f, "NO-TTY"),
        }
    }
}

/// The outcome names accepted by [`AuditOutcome::parse`] (for help texts).
pub const OUTCOME_VALUES: &str = "allowed, denied, skipped, cancelled, asked, no-terminal";

impl AuditOutcome {
    /// Parse an outcome name (case-insensitive).
    ///
    /// # Errors
    /// Returns an error listing the valid values.
    pub fn parse(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "allowed" => Ok(Self::Allowed),
            "denied" => Ok(Self::Denied),
            "skipped" => Ok(Self::Skipped),
            "cancelled" | "canceled" => Ok(Self::Cancelled),
            "asked" | "ask" => Ok(Self::Asked),
            "noterminal" | "no-terminal" | "no_terminal" | "no-tty" | "notty" => {
                Ok(Self::NoTerminal)
            }
            _ => Err(Error::Config(format!(
                "invalid outcome '{s}'. Valid values: {OUTCOME_VALUES}"
            ))),
        }
    }
}

/// A single audit log entry, as written by the interception paths.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditEvent {
    /// Unique identifier of the decision. Older versions wrote a
    /// pre-challenge record and a final record with the same id;
    /// `audit show` still collapses those pairs.
    pub event_id: String,
    pub timestamp: String,
    pub command: String,
    pub matched_ids: Vec<String>,
    pub challenge_type: String,
    pub outcome: AuditOutcome,
    pub context_labels: Vec<String>,
    /// The highest severity among the matched checks.
    pub severity: Severity,
    /// Name of the AI agent that originated this command (if any).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub agent_name: Option<String>,
    /// Session ID of the AI agent (if any).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub agent_session_id: Option<String>,
    /// Blast radius scope (e.g. "PROJECT", "MACHINE").
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub blast_radius_scope: Option<String>,
    /// Blast radius detail description.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub blast_radius_detail: Option<String>,
    /// Number of challenge attempts made before the outcome (if tracked).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub attempts: Option<u32>,
}

/// Metadata collected automatically by [`log_event`] for every record.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct AuditMeta {
    pub schema_version: u32,
    /// The shellfirm version that wrote the record (schema 4 and later).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    /// Account name of the real uid (password database), or `$USER` when
    /// the lookup fails.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub user: Option<String>,
    /// Numeric real uid (Unix).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub uid: Option<u32>,
    /// `$USER` / `$LOGNAME`, recorded only when it differs from `user`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub env_user: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub hostname: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cwd: Option<String>,
    /// Top-level directory of the git repository containing `cwd`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub git_repo: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub git_branch: Option<String>,
}

/// Optional details a caller can attach with [`log_event_with`].
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct AuditExtra {
    /// Paths of the policy files (org or `.shellfirm.yaml`) that enforce
    /// the matched checks (for a policy deny: the files that deny them).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub policy_sources: Vec<String>,
    /// The challenge actually presented (after all escalation layers), as
    /// opposed to `challenge_type` (the configured base challenge).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub effective_challenge: Option<String>,
    /// Why the command was denied: `"policy_deny"` (a policy `deny` entry),
    /// `"user_deny"` (the personal `deny_patterns_ids`), or a caller-set
    /// reason such as `"agent_auto_deny"`, `"no_tty"`, `"cancelled"` or
    /// `"fail_closed"` (see [`deny_reason`]).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub deny_reason: Option<String>,
    /// Ids of matched checks that are enforced by the project policy.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub policy_check_ids: Vec<String>,
    /// Why the challenge was raised above the base challenge (e.g.
    /// "protected branch `main`", "severity High").
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub escalation_reasons: Vec<String>,
    /// The agent's id for the tool call (Claude Code `tool_use_id`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tool_use_id: Option<String>,
}

/// The values of [`AuditExtra::deny_reason`].
pub mod deny_reason {
    use super::AuditOutcome;

    /// A matched id is in a policy `deny` list (org or `.shellfirm.yaml`).
    pub const POLICY_DENY: &str = "policy_deny";
    /// A matched id is in the personal `deny_patterns_ids`.
    pub const USER_DENY: &str = "user_deny";
    /// An AI-agent hook denied it because the severity reached
    /// `agent.auto_deny_severity`.
    pub const AGENT_AUTO_DENY: &str = "agent_auto_deny";
    /// No terminal was available to answer the challenge (fail closed).
    pub const NO_TTY: &str = "no_tty";
    /// A human cancelled the challenge (Esc, Ctrl-C, Ctrl-D / end of
    /// input). The interactive prompt keeps asking after a wrong answer,
    /// so a shell-hook challenge only ever ends unanswered this way.
    pub const CANCELLED: &str = "cancelled";
    /// The challenge was answered wrongly and given up. Written by older
    /// versions for every cancelled challenge (they could not tell a
    /// cancel from a wrong answer); read it as "not passed".
    pub const CHALLENGE_FAILED: &str = "challenge_failed";

    /// The deny reason implied by an outcome on its own (`NoTerminal` →
    /// [`NO_TTY`], `Cancelled` → [`CANCELLED`]); `None` for the others.
    #[must_use]
    pub const fn for_outcome(outcome: &AuditOutcome) -> Option<&'static str> {
        match outcome {
            AuditOutcome::NoTerminal => Some(NO_TTY),
            AuditOutcome::Cancelled => Some(CANCELLED),
            _ => None,
        }
    }
}

impl AuditExtra {
    /// Build the extra fields for one decision.
    ///
    /// - `merged`: the merged project policy of the pipeline run.
    /// - `user_deny`: the user's personal `deny_patterns_ids`.
    /// - `matched_ids`: ids of the active matches.
    /// - `denied`: whether the command was denied by a deny list.
    /// - `effective_challenge` / `escalation_reasons`: from the challenge
    ///   display.
    ///
    /// `deny_reason` is `policy_deny` when a matched id is in the policy
    /// deny list, otherwise `user_deny` when it is in the personal deny
    /// list (callers set `agent_auto_deny`, `no_tty`, … themselves).
    #[must_use]
    pub fn for_decision(
        merged: &crate::policy::MergedPolicy,
        user_deny: &[String],
        matched_ids: &[String],
        denied: bool,
        effective_challenge: Option<String>,
        escalation_reasons: Vec<String>,
    ) -> Self {
        let deny_reason = if !denied {
            None
        } else if matched_ids.iter().any(|id| merged.is_denied(id)) {
            Some(deny_reason::POLICY_DENY.to_string())
        } else if matched_ids.iter().any(|id| user_deny.contains(id)) {
            Some(deny_reason::USER_DENY.to_string())
        } else {
            Some("deny".to_string())
        };
        let policy_check_ids: Vec<String> = matched_ids
            .iter()
            .filter(|id| merged.is_enforced(id))
            .cloned()
            .collect();
        // The files that name the relevant ids: for a policy deny the
        // files that deny a matched id, otherwise those enforcing a match.
        let relevant: Vec<&String> = if deny_reason.as_deref() == Some(deny_reason::POLICY_DENY) {
            matched_ids
                .iter()
                .filter(|id| merged.is_denied(id))
                .collect()
        } else {
            policy_check_ids.iter().collect()
        };
        let mut policy_sources: Vec<String> = Vec::new();
        for id in relevant {
            for p in merged.sources_for(id) {
                let p = p.display().to_string();
                if !policy_sources.contains(&p) {
                    policy_sources.push(p);
                }
            }
        }
        Self {
            policy_sources,
            effective_challenge,
            deny_reason,
            policy_check_ids,
            escalation_reasons,
            tool_use_id: None,
        }
    }
}

/// Size information written when a command is longer than
/// [`MAX_STORED_COMMAND_BYTES`].
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[allow(clippy::struct_field_names)]
struct CommandDigest {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    command_bytes: Option<usize>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    command_sha256: Option<String>,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    command_truncated: bool,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    command_redacted: bool,
}

#[derive(Serialize)]
struct AuditRecordOut<'a> {
    #[serde(flatten)]
    event: &'a AuditEvent,
    #[serde(flatten)]
    meta: AuditMeta,
    #[serde(flatten)]
    extra: &'a AuditExtra,
    #[serde(flatten)]
    digest: CommandDigest,
}

/// Read model for one audit record. Every field is optional so records
/// written by older versions (or by hand) still load.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct AuditEntry {
    pub event_id: String,
    pub timestamp: String,
    pub command: String,
    pub matched_ids: Vec<String>,
    pub challenge_type: String,
    pub outcome: Option<AuditOutcome>,
    pub context_labels: Vec<String>,
    pub severity: Option<Severity>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub agent_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub agent_session_id: Option<String>,
    /// The agent's id for the tool call (Claude Code `tool_use_id`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_use_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub blast_radius_scope: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub blast_radius_detail: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attempts: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub schema_version: Option<u32>,
    /// The shellfirm version that wrote the record (schema 4 and later).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uid: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub env_user: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hostname: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cwd: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub git_repo: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub git_branch: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub policy_sources: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub effective_challenge: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deny_reason: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub policy_check_ids: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub escalation_reasons: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub command_bytes: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub command_sha256: Option<String>,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub command_truncated: bool,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub command_redacted: bool,
}

// ---------------------------------------------------------------------------
// Metadata collection
// ---------------------------------------------------------------------------

fn env_user() -> Option<String> {
    ["USER", "LOGNAME", "USERNAME"]
        .iter()
        .find_map(|k| std::env::var(k).ok().filter(|v| !v.is_empty()))
}

/// Real uid of this process (Unix).
#[must_use]
pub fn current_uid() -> Option<u32> {
    #[cfg(unix)]
    {
        // SAFETY: getuid has no preconditions and cannot fail.
        Some(unsafe { libc::getuid() })
    }
    #[cfg(not(unix))]
    {
        None
    }
}

/// Account name for `uid` from the password database.
#[cfg(unix)]
fn passwd_name(uid: u32) -> Option<String> {
    let mut buf = vec![0 as libc::c_char; 4096];
    // SAFETY: an all-zero passwd struct is a valid out-parameter.
    let mut pwd: libc::passwd = unsafe { std::mem::zeroed() };
    let mut result: *mut libc::passwd = std::ptr::null_mut();
    // SAFETY: every pointer is valid for the duration of the call and the
    // buffer length matches the buffer.
    let rc = unsafe {
        libc::getpwuid_r(
            uid,
            &raw mut pwd,
            buf.as_mut_ptr(),
            buf.len(),
            &raw mut result,
        )
    };
    if rc != 0 || result.is_null() || pwd.pw_name.is_null() {
        return None;
    }
    // SAFETY: getpwuid_r succeeded, so pw_name points to a NUL-terminated
    // string inside `buf`.
    let name = unsafe { std::ffi::CStr::from_ptr(pwd.pw_name) };
    let name = name.to_string_lossy().to_string();
    (!name.is_empty()).then_some(name)
}

/// `(user, uid, env_user)`: the account of the real uid (falling back to
/// `$USER`), the uid, and `$USER` when it differs from the account name.
fn identity() -> (Option<String>, Option<u32>, Option<String>) {
    let uid = current_uid();
    #[cfg(unix)]
    let account = uid.and_then(passwd_name);
    #[cfg(not(unix))]
    let account: Option<String> = None;
    let env = env_user();
    match account {
        Some(name) => {
            let differs = env.filter(|e| *e != name);
            (Some(name), uid, differs)
        }
        None => (env, uid, None),
    }
}

fn current_hostname() -> Option<String> {
    #[cfg(unix)]
    {
        if let Ok(h) = nix::unistd::gethostname() {
            let h = h.to_string_lossy().to_string();
            if !h.is_empty() {
                return Some(h);
            }
        }
    }
    ["HOSTNAME", "COMPUTERNAME"]
        .iter()
        .find_map(|k| std::env::var(k).ok().filter(|v| !v.is_empty()))
}

/// Find the git repository root and current branch for `dir` by reading
/// `.git/HEAD` directly (no subprocess).
#[must_use]
pub fn git_info(dir: &Path) -> (Option<PathBuf>, Option<String>) {
    for d in dir.ancestors() {
        let dot_git = d.join(".git");
        let git_dir = if dot_git.is_dir() {
            Some(dot_git)
        } else if dot_git.is_file() {
            // worktree / submodule: "gitdir: <path>"
            fs::read_to_string(&dot_git).ok().and_then(|c| {
                c.lines().find_map(|l| l.strip_prefix("gitdir:")).map(|p| {
                    let p = PathBuf::from(p.trim());
                    if p.is_absolute() {
                        p
                    } else {
                        d.join(p)
                    }
                })
            })
        } else {
            None
        };
        if let Some(gd) = git_dir {
            let branch = fs::read_to_string(gd.join("HEAD")).ok().map(|head| {
                let head = head.trim();
                head.strip_prefix("ref: refs/heads/").map_or_else(
                    || format!("(detached {})", head.chars().take(12).collect::<String>()),
                    ToString::to_string,
                )
            });
            return (Some(d.to_path_buf()), branch);
        }
    }
    (None, None)
}

/// Collect the automatic metadata for a record.
#[must_use]
pub fn collect_meta() -> AuditMeta {
    collect_meta_for(None)
}

/// [`collect_meta`] for `command`.
///
/// When the command works in another
/// directory (`git -C ../infra …`, `cd ../infra && …`), `git_repo` /
/// `git_branch` are that directory's repository; otherwise the current
/// directory's. `cwd` is always the current directory.
#[must_use]
pub fn collect_meta_for(command: Option<&str>) -> AuditMeta {
    let cwd = std::env::current_dir().ok();
    let target = command.zip(cwd.as_deref()).and_then(|(c, cwd)| {
        let home = crate::env::Environment::home_dir(&crate::env::RealEnvironment);
        crate::context::command_work_dir(c, cwd, home.as_deref())
    });
    let (repo, branch) = target
        .as_deref()
        .or(cwd.as_deref())
        .map_or((None, None), git_info);
    let (user, uid, env_user) = identity();
    AuditMeta {
        schema_version: AUDIT_SCHEMA_VERSION,
        version: Some(env!("CARGO_PKG_VERSION").to_string()),
        user,
        uid,
        env_user,
        hostname: current_hostname(),
        cwd: cwd.map(|c| c.display().to_string()),
        git_repo: repo.map(|r| r.display().to_string()),
        git_branch: branch,
    }
}

// ---------------------------------------------------------------------------
// Writing
// ---------------------------------------------------------------------------

/// Append an audit event to the log file as a JSON line.
///
/// See [`log_event_with`].
///
/// # Errors
/// Returns an error if the file cannot be opened/created or JSON serialization fails.
pub fn log_event(audit_path: &Path, event: &AuditEvent) -> Result<()> {
    log_event_with(audit_path, event, &AuditExtra::default())
}

/// Append an audit event plus caller-provided details as one JSON line.
///
/// The record is written with a single `write()` on an `O_APPEND` file
/// descriptor (under an exclusive `flock` on Unix), so concurrent writers
/// never interleave. A new file is created with mode `0600`; an existing
/// file with wider permissions is tightened to `0600`. The log is rotated to
/// `<name>.1` once it exceeds [`DEFAULT_MAX_AUDIT_BYTES`].
///
/// # Errors
/// Returns an error if the file cannot be opened/created or JSON serialization fails.
pub fn log_event_with(audit_path: &Path, event: &AuditEvent, extra: &AuditExtra) -> Result<()> {
    log_event_with_limit(audit_path, event, extra, DEFAULT_MAX_AUDIT_BYTES)
}

fn open_append(path: &Path) -> std::io::Result<fs::File> {
    let mut opts = OpenOptions::new();
    opts.create(true).append(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        opts.mode(0o600);
    }
    let file = opts.open(path)?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if let Ok(meta) = file.metadata() {
            if meta.permissions().mode() & 0o077 != 0 {
                let _ = fs::set_permissions(path, fs::Permissions::from_mode(0o600));
            }
        }
    }
    Ok(file)
}

fn rotated_path_n(path: &Path, n: usize) -> PathBuf {
    let mut name = path.file_name().unwrap_or_default().to_os_string();
    name.push(format!(".{n}"));
    path.with_file_name(name)
}

#[cfg(test)]
fn rotated_path(path: &Path) -> PathBuf {
    rotated_path_n(path, 1)
}

/// Every existing log generation, oldest first (`audit.log.N` …
/// `audit.log.1`, then `audit.log`). Generations beyond the configured
/// limit that are still on disk are included too.
#[must_use]
pub fn log_files(audit_path: &Path) -> Vec<PathBuf> {
    let mut rotated: Vec<(usize, PathBuf)> = Vec::new();
    let prefix = format!(
        "{}.",
        audit_path.file_name().unwrap_or_default().to_string_lossy()
    );
    if let Some(dir) = audit_path.parent() {
        let dir = if dir.as_os_str().is_empty() {
            Path::new(".")
        } else {
            dir
        };
        for e in fs::read_dir(dir).into_iter().flatten().flatten() {
            let name = e.file_name().to_string_lossy().to_string();
            if let Some(n) = name
                .strip_prefix(&prefix)
                .and_then(|r| r.parse::<usize>().ok())
            {
                rotated.push((n, rotated_path_n(audit_path, n)));
            }
        }
    }
    rotated.sort_by_key(|r| std::cmp::Reverse(r.0));
    let mut out: Vec<PathBuf> = rotated.into_iter().map(|(_, p)| p).collect();
    if audit_path.exists() {
        out.push(audit_path.to_path_buf());
    }
    out
}

/// Shift `audit.log.k` → `audit.log.k+1` (dropping generations beyond
/// `keep`) and move `audit.log` to `audit.log.1`.
fn rotate_files(audit_path: &Path, keep: usize) -> std::io::Result<()> {
    // Never delete the generation being rotated (see `max_files_for`).
    let keep = keep.max(1);
    // Remove generations at or beyond the limit.
    for p in log_files(audit_path) {
        let n = p
            .file_name()
            .and_then(|f| f.to_str())
            .and_then(|f| f.rsplit('.').next())
            .and_then(|n| n.parse::<usize>().ok());
        if n.is_some_and(|n| n >= keep) && p != audit_path {
            let _ = fs::remove_file(&p);
        }
    }
    for n in (1..keep).rev() {
        let from = rotated_path_n(audit_path, n);
        if from.exists() {
            fs::rename(&from, rotated_path_n(audit_path, n + 1))?;
        }
    }
    fs::rename(audit_path, rotated_path_n(audit_path, 1))
}

/// Regexes for [`redact_secrets`]: `(regex, replacement)`.
#[allow(clippy::too_many_lines)]
fn redaction_rules() -> &'static [(regex::Regex, &'static str)] {
    static RULES: std::sync::OnceLock<Vec<(regex::Regex, &'static str)>> =
        std::sync::OnceLock::new();
    RULES.get_or_init(|| {
        let v = r#"(?:"[^"]*"|'[^']*'|[^\s'";&|)]+)"#;
        // A value that is not the next option (`mongosh -p --eval …` prompts).
        let v_arg = r#"(?:"[^"]*"|'[^']*'|[^\s'";&|)\-][^\s'";&|)]*)"#;
        let rules: Vec<(String, &'static str)> = vec![
            // NAME=value assignments whose name says "secret".
            (
                format!(
                    r"(?i)\b([A-Z0-9_]*(?:PASSWORD|PASSWD|_PWD|PASS|TOKEN|SECRET|API_?KEY|ACCESS_KEY|PRIVATE_KEY|CREDENTIALS?)[A-Z0-9_]*=){v}"
                ),
                "${1}***",
            ),
            // MYSQL_PWD=… (the name is exactly the suffix).
            (format!(r"\b(MYSQL_PWD=){v}"), "${1}***"),
            // REDISCLI_AUTH=… and other `*_AUTH` variables.
            (format!(r"(?i)\b([A-Z0-9_]*_AUTH=){v}"), "${1}***"),
            // redis-cli -a PASSWORD / --pass PASSWORD
            (
                format!(
                    r"(\b(?:redis-cli|valkey-cli|keydb-cli)\b[^;&|\n]*?\s(?:-a|--pass)\s+){v_arg}"
                ),
                "${1}***",
            ),
            // mongosh -p PASSWORD / --password PASSWORD (space-separated)
            (
                format!(
                    r"(\b(?:mongosh|mongo|mongodump|mongorestore|mongoexport|mongoimport|mongofiles|mongostat|mongotop)\b[^;&|\n]*?\s(?:-p|--password)\s+){v_arg}"
                ),
                "${1}***",
            ),
            // mongosh -pPASSWORD (attached value).
            (
                format!(
                    r"(\b(?:mongosh|mongo|mongodump|mongorestore|mongoexport|mongoimport|mongofiles|mongostat|mongotop)\b[^;&|\n]*?\s-p){v}"
                ),
                "${1}***",
            ),
            // clickhouse-client --password PASSWORD (space-separated; a
            // bare `--password` prompts).
            (
                format!(
                    r"(\b(?:clickhouse-client|clickhouse)\b[^;&|\n]*?\s--password\s+){v_arg}"
                ),
                "${1}***",
            ),
            // cqlsh -p PASSWORD / --password PASSWORD (Cassandra).
            (
                format!(r"(\bcqlsh\b[^;&|\n]*?\s(?:-p|--password)\s+){v_arg}"),
                "${1}***",
            ),
            // docker login -p PASSWORD / --password PASSWORD (and podman,
            // nerdctl, buildah, skopeo, `helm registry login`).
            (
                format!(
                    r"(\b(?:docker|podman|nerdctl|buildah|skopeo|helm)\b[^;&|\n]*?\blogin\b[^;&|\n]*?\s(?:-p|--password)\s+){v_arg}"
                ),
                "${1}***",
            ),
            // aws configure set aws_secret_access_key VALUE (and
            // aws_session_token, with or without a `profile.` prefix).
            (
                format!(
                    r"(\baws\b[^;&|\n]*?\bconfigure\s+set\b[^;&|\n]*?\b(?:aws_secret_access_key|aws_session_token)\s+){v_arg}"
                ),
                "${1}***",
            ),
            // npm config set //registry/:_authToken VALUE (and `_auth`,
            // `_password`; pnpm, yarn `npmAuthToken`). The `=` spellings
            // are covered by the NAME=value rules.
            (
                format!(
                    r"(\b(?:npm|pnpm|yarn)\b[^;&|\n]*?\bconfig\s+set\s+[^\s=;&|]*?(?:_authToken|_auth|_password|npmAuthToken|npmAuthIdent|npmPassword)\s+){v_arg}"
                ),
                "${1}***",
            ),
            // --password=… / --token=… / --secret=… / --api-key=…
            (
                format!(
                    r"(?i)(--(?:password|passwd|pass|token|secret|api-?key|access-token|auth-token|client-secret)=){v}"
                ),
                "${1}***",
            ),
            // --token VALUE / --api-key VALUE (space-separated values).
            (
                format!(
                    r"(?i)(--(?:token|secret|api-?key|access-token|auth-token|client-secret)\s+){v}"
                ),
                "${1}***",
            ),
            // sqlcmd / bcp -P PASSWORD and -PPASSWORD (SQL Server; only the
            // upper-case `-P`, lower-case `-p` prints statistics).
            (
                format!(r"(\b(?:sqlcmd|bcp)\b[^;&|\n]*?\s-P\s+){v_arg}"),
                "${1}***",
            ),
            (
                format!(r"(\b(?:sqlcmd|bcp)\b[^;&|\n]*?\s-P){v}"),
                "${1}***",
            ),
            // mysql/mariadb -pSECRET (attached value only; `-p` alone prompts).
            (
                format!(
                    r"(\b(?:mysql|mysqldump|mysqladmin|mysqlimport|mysqlcheck|mariadb|mariadb-dump)\b[^;&|\n]*?\s-p){v}"
                ),
                "${1}***",
            ),
            // user:password@ in URLs.
            (
                r#"(\b[A-Za-z][A-Za-z0-9+.\-]*://[^\s:/@'"]+:)[^\s@/'"]+@"#.to_string(),
                "${1}***@",
            ),
            // :password@ with an empty user (`redis://:pw@host`).
            (
                r#"(\b[A-Za-z][A-Za-z0-9+.\-]*://:)[^\s@/'"]+@"#.to_string(),
                "${1}***@",
            ),
            // Authorization / token headers.
            (
                r#"(?i)((?:authorization|proxy-authorization|x-api-key|apikey|api-key|x-auth-token|x-access-token|private-token)\s*:\s*(?:(?:bearer|basic|token|digest)\s+)?)[^\s'"]+"#
                    .to_string(),
                "${1}***",
            ),
            // curl -u user:password / --user user:password
            (
                r#"(\b(?:curl|wget)\b[^;&|\n]*?\s(?:-u\s*|--user[=\s]+|--http-password[=\s]+)['"]?[^\s:'"]*:)[^\s'"]+"#
                    .to_string(),
                "${1}***",
            ),
            // Well-known token formats.
            (r"\b(?:AKIA|ASIA)[0-9A-Z]{16}\b".to_string(), "***"),
            (r"\bgh[pousr]_[A-Za-z0-9]{20,}\b".to_string(), "***"),
            (r"\bgithub_pat_[A-Za-z0-9_]{20,}\b".to_string(), "***"),
            (r"\bxox[abprs]-[A-Za-z0-9-]{10,}".to_string(), "***"),
            (r"\bsk-[A-Za-z0-9_\-]{20,}".to_string(), "***"),
            // Stripe secret and restricted keys.
            (
                r"\b(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{10,}".to_string(),
                "***",
            ),
            (r"\bglpat-[A-Za-z0-9_\-]{20,}".to_string(), "***"),
            // npm access tokens (`npm_` and 36 letters or digits).
            (r"\bnpm_[A-Za-z0-9]{36}\b".to_string(), "***"),
            // A JSON Web Token (header and payload are base64url JSON, so
            // both start with `eyJ`), wherever it appears.
            (
                r"\beyJ[A-Za-z0-9_\-]{5,}\.eyJ[A-Za-z0-9_\-]{5,}\.[A-Za-z0-9_\-]*".to_string(),
                "***",
            ),
        ];
        rules
            .into_iter()
            .filter_map(|(re, rep)| regex::Regex::new(&re).ok().map(|r| (r, rep)))
            .collect()
    })
}

/// Undo [`crate::context::escape_invisible`].
///
/// Each `\u{XXXX}` that stands for an invisible or control character becomes
/// that character again. Other `\u{…}` text (a printable character, an invalid code point) is
/// kept as written.
#[must_use]
pub fn unescape_invisible(s: &str) -> String {
    if !s.contains("\\u{") {
        return s.to_string();
    }
    let mut out = String::with_capacity(s.len());
    let mut rest = s;
    while let Some(i) = rest.find("\\u{") {
        out.push_str(&rest[..i]);
        let after = &rest[i + 3..];
        let decoded = after.find('}').and_then(|end| {
            let hex = &after[..end];
            let c = (!hex.is_empty() && hex.len() <= 6)
                .then(|| u32::from_str_radix(hex, 16).ok())
                .flatten()
                .and_then(char::from_u32)
                .filter(|c| {
                    crate::context::is_invisible(*c) || (c.is_control() && *c != '\t' && *c != '\n')
                })?;
            Some((c, end))
        });
        if let Some((c, end)) = decoded {
            out.push(c);
            rest = &after[end + 1..];
        } else {
            out.push_str("\\u{");
            rest = after;
        }
    }
    out.push_str(rest);
    out
}

/// Replace secrets in a command line with `***` before it is stored.
///
/// Covers `mysql -pSECRET`, `--password=…` (and `--token`, `--secret`,
/// `--api-key`), `NAME=value` assignments whose name contains `PASSWORD`,
/// `PASS`, `TOKEN`, `SECRET`, `KEY`, `CREDENTIAL` (`PGPASSWORD`,
/// `MYSQL_PWD`, `GITHUB_TOKEN`, `AWS_SECRET_ACCESS_KEY`, …), `user:pass@`
/// in URLs, `Authorization:` / `apikey:`-style headers, `curl -u
/// user:pass`, `docker login -p …`, `aws configure set
/// aws_secret_access_key …`, `npm config set …:_authToken …`, JSON Web
/// Tokens (`eyJ….eyJ….…`), and AWS (`AKIA…`), GitHub (`ghp_…`), GitLab
/// (`glpat-…`), npm (`npm_…`), Slack (`xoxb-…`), Stripe (`sk_live_…`,
/// `rk_live_…`) and `sk-…` keys.
///
/// Returns the redacted text and whether anything was replaced. This is a
/// best-effort safety net, not a guarantee: a secret passed positionally
/// (`tool login s3cr3t`) cannot be recognised.
#[must_use]
pub fn redact_secrets(command: &str) -> (String, bool) {
    let mut out = std::borrow::Cow::Borrowed(command);
    for (re, rep) in redaction_rules() {
        if re.is_match(&out) {
            out = std::borrow::Cow::Owned(re.replace_all(&out, *rep).into_owned());
        }
    }
    let changed = out != command;
    (out.into_owned(), changed)
}

/// Truncate `s` to at most `max` bytes at a character boundary.
fn truncate_at_char(s: &str, max: usize) -> &str {
    if s.len() <= max {
        return s;
    }
    let mut end = max;
    while end > 0 && !s.is_char_boundary(end) {
        end -= 1;
    }
    &s[..end]
}

/// Lowercase hex SHA-256 of `data` (small self-contained implementation so
/// the audit log needs no extra dependency).
#[must_use]
#[allow(clippy::many_single_char_names, clippy::too_many_lines)]
pub fn sha256_hex(data: &[u8]) -> String {
    const K: [u32; 64] = [
        0x428a_2f98,
        0x7137_4491,
        0xb5c0_fbcf,
        0xe9b5_dba5,
        0x3956_c25b,
        0x59f1_11f1,
        0x923f_82a4,
        0xab1c_5ed5,
        0xd807_aa98,
        0x1283_5b01,
        0x2431_85be,
        0x550c_7dc3,
        0x72be_5d74,
        0x80de_b1fe,
        0x9bdc_06a7,
        0xc19b_f174,
        0xe49b_69c1,
        0xefbe_4786,
        0x0fc1_9dc6,
        0x240c_a1cc,
        0x2de9_2c6f,
        0x4a74_84aa,
        0x5cb0_a9dc,
        0x76f9_88da,
        0x983e_5152,
        0xa831_c66d,
        0xb003_27c8,
        0xbf59_7fc7,
        0xc6e0_0bf3,
        0xd5a7_9147,
        0x06ca_6351,
        0x1429_2967,
        0x27b7_0a85,
        0x2e1b_2138,
        0x4d2c_6dfc,
        0x5338_0d13,
        0x650a_7354,
        0x766a_0abb,
        0x81c2_c92e,
        0x9272_2c85,
        0xa2bf_e8a1,
        0xa81a_664b,
        0xc24b_8b70,
        0xc76c_51a3,
        0xd192_e819,
        0xd699_0624,
        0xf40e_3585,
        0x106a_a070,
        0x19a4_c116,
        0x1e37_6c08,
        0x2748_774c,
        0x34b0_bcb5,
        0x391c_0cb3,
        0x4ed8_aa4a,
        0x5b9c_ca4f,
        0x682e_6ff3,
        0x748f_82ee,
        0x78a5_636f,
        0x84c8_7814,
        0x8cc7_0208,
        0x90be_fffa,
        0xa450_6ceb,
        0xbef9_a3f7,
        0xc671_78f2,
    ];
    let mut h: [u32; 8] = [
        0x6a09_e667,
        0xbb67_ae85,
        0x3c6e_f372,
        0xa54f_f53a,
        0x510e_527f,
        0x9b05_688c,
        0x1f83_d9ab,
        0x5be0_cd19,
    ];
    let mut msg = data.to_vec();
    let bit_len = (data.len() as u64).wrapping_mul(8);
    msg.push(0x80);
    while msg.len() % 64 != 56 {
        msg.push(0);
    }
    msg.extend_from_slice(&bit_len.to_be_bytes());
    // `msg.len()` is a multiple of 64 here: no remainder.
    for chunk in msg.as_chunks::<64>().0 {
        let mut w = [0u32; 64];
        for (i, word) in chunk.as_chunks::<4>().0.iter().enumerate() {
            w[i] = u32::from_be_bytes(*word);
        }
        for i in 16..64 {
            let s0 = w[i - 15].rotate_right(7) ^ w[i - 15].rotate_right(18) ^ (w[i - 15] >> 3);
            let s1 = w[i - 2].rotate_right(17) ^ w[i - 2].rotate_right(19) ^ (w[i - 2] >> 10);
            w[i] = w[i - 16]
                .wrapping_add(s0)
                .wrapping_add(w[i - 7])
                .wrapping_add(s1);
        }
        let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut hh] = h;
        for i in 0..64 {
            let s1 = e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25);
            let ch = (e & f) ^ (!e & g);
            let t1 = hh
                .wrapping_add(s1)
                .wrapping_add(ch)
                .wrapping_add(K[i])
                .wrapping_add(w[i]);
            let s0 = a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22);
            let maj = (a & b) ^ (a & c) ^ (b & c);
            let t2 = s0.wrapping_add(maj);
            hh = g;
            g = f;
            f = e;
            e = d.wrapping_add(t1);
            d = c;
            c = b;
            b = a;
            a = t1.wrapping_add(t2);
        }
        for (slot, v) in h.iter_mut().zip([a, b, c, d, e, f, g, hh]) {
            *slot = slot.wrapping_add(v);
        }
    }
    h.iter().fold(String::with_capacity(64), |mut acc, v| {
        use std::fmt::Write as _;
        let _ = write!(acc, "{v:08x}");
        acc
    })
}

fn log_event_with_limit(
    audit_path: &Path,
    event: &AuditEvent,
    extra: &AuditExtra,
    max_bytes: u64,
) -> Result<()> {
    if let Some(parent) = audit_path.parent() {
        if !parent.as_os_str().is_empty() {
            fs::create_dir_all(parent)?;
        }
    }

    let meta = collect_meta_for(Some(&event.command));
    // Redact secrets, then cap the stored command so a few huge commands
    // cannot flush history. The digest is always of the original command.
    let (stored_event, digest);
    let (redacted, was_redacted) = redact_secrets(&event.command);
    let truncate = redacted.len() > MAX_STORED_COMMAND_BYTES;
    // The blast-radius description arrives escaped for display
    // (`src\u{202e}.txt`); the record keeps the real text, like `command`.
    let real_detail = event
        .blast_radius_detail
        .as_deref()
        .map(unescape_invisible)
        .filter(|d| Some(d.as_str()) != event.blast_radius_detail.as_deref());
    let event = if truncate || was_redacted || real_detail.is_some() {
        let mut e = event.clone();
        if let Some(d) = real_detail {
            e.blast_radius_detail = Some(d);
        }
        e.command = if truncate {
            format!(
                "{}…[truncated]",
                truncate_at_char(&redacted, MAX_STORED_COMMAND_BYTES)
            )
        } else {
            redacted
        };
        digest = CommandDigest {
            command_bytes: truncate.then_some(event.command.len()),
            command_sha256: Some(sha256_hex(event.command.as_bytes())),
            command_truncated: truncate,
            command_redacted: was_redacted,
        };
        stored_event = e;
        &stored_event
    } else {
        digest = CommandDigest::default();
        event
    };
    let record = AuditRecordOut {
        event,
        meta,
        extra,
        digest,
    };
    let mut line = serde_json::to_vec(&record)?;
    line.push(b'\n');

    let file = open_append(audit_path)?;

    #[cfg(unix)]
    let mut file = match nix::fcntl::Flock::lock(file, nix::fcntl::FlockArg::LockExclusive) {
        Ok(locked) => LockedOrPlain::Locked(locked),
        Err((plain, _)) => LockedOrPlain::Plain(plain),
    };
    #[cfg(not(unix))]
    let mut file = file;

    // Another writer may have rotated the file between our open() and the
    // lock: then our descriptor points at a rotated generation. Re-open the
    // current file instead (once is enough; rotation needs a full file).
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        let same = match (file_ref(&file).metadata(), fs::metadata(audit_path)) {
            (Ok(a), Ok(b)) => a.ino() == b.ino() && a.dev() == b.dev(),
            _ => false,
        };
        if !same {
            drop(file);
            let fresh = open_append(audit_path)?;
            file = match nix::fcntl::Flock::lock(fresh, nix::fcntl::FlockArg::LockExclusive) {
                Ok(locked) => LockedOrPlain::Locked(locked),
                Err((plain, _)) => LockedOrPlain::Plain(plain),
            };
        }
    }

    // Rotation (under the lock): if the file is over the limit, shift the
    // generations and continue with a fresh file.
    let len = file_ref(&file).metadata().map_or(0, |m| m.len());
    if max_bytes > 0
        && len >= max_bytes
        && rotate_files(audit_path, max_files_for(audit_path)).is_ok()
    {
        {
            drop(file);
            let fresh = open_append(audit_path)?;
            #[cfg(unix)]
            {
                file = match nix::fcntl::Flock::lock(fresh, nix::fcntl::FlockArg::LockExclusive) {
                    Ok(locked) => LockedOrPlain::Locked(locked),
                    Err((plain, _)) => LockedOrPlain::Plain(plain),
                };
            }
            #[cfg(not(unix))]
            {
                file = fresh;
            }
        }
    }

    let f = file_mut(&mut file);
    let written = f.write(&line)?;
    if written < line.len() {
        // Extremely unlikely for a regular file; finish the record.
        f.write_all(&line[written..])?;
    }
    Ok(())
}

#[cfg(unix)]
enum LockedOrPlain {
    Locked(nix::fcntl::Flock<fs::File>),
    Plain(fs::File),
}

#[cfg(unix)]
fn file_ref(f: &LockedOrPlain) -> &fs::File {
    match f {
        LockedOrPlain::Locked(l) => l,
        LockedOrPlain::Plain(p) => p,
    }
}

#[cfg(unix)]
fn file_mut(f: &mut LockedOrPlain) -> &mut fs::File {
    match f {
        LockedOrPlain::Locked(l) => l,
        LockedOrPlain::Plain(p) => p,
    }
}

#[cfg(not(unix))]
const fn file_ref(f: &fs::File) -> &fs::File {
    f
}

#[cfg(not(unix))]
fn file_mut(f: &mut fs::File) -> &mut fs::File {
    f
}

// ---------------------------------------------------------------------------
// Reading
// ---------------------------------------------------------------------------

/// Read and return all audit log lines.
///
/// # Errors
/// Returns an error if the file cannot be read.
pub fn read_log(audit_path: &Path) -> Result<String> {
    if !audit_path.exists() {
        return Ok("No audit events recorded yet.".into());
    }
    Ok(fs::read_to_string(audit_path)?)
}

/// Parsed audit records plus the number of lines that could not be parsed.
#[derive(Debug, Default)]
pub struct AuditRead {
    pub entries: Vec<AuditEntry>,
    /// Lines skipped because they are not valid UTF-8, not JSON, or lack a
    /// `timestamp` / `outcome`.
    pub malformed: usize,
    /// The files that were read, oldest first.
    pub files: Vec<PathBuf>,
}

/// Read every log generation (see [`log_files`]), oldest first.
///
/// # Errors
/// Returns an error if an existing log file cannot be read (e.g. it is a
/// directory or unreadable).
pub fn read_entries(audit_path: &Path) -> Result<AuditRead> {
    let mut out = AuditRead::default();
    for p in log_files(audit_path) {
        let bytes = fs::read(&p)
            .map_err(|e| Error::Other(format!("could not read audit log {}: {e}", p.display())))?;
        out.files.push(p);
        for raw in bytes.split(|b| *b == b'\n') {
            // A line that is not valid UTF-8 (or not a record) is counted
            // and skipped; it never makes the whole read fail.
            let Ok(line) = std::str::from_utf8(raw) else {
                out.malformed += 1;
                continue;
            };
            let line = line.trim();
            if line.is_empty() {
                continue;
            }
            match serde_json::from_str::<AuditEntry>(line) {
                Ok(e) if !e.timestamp.is_empty() && e.outcome.is_some() => out.entries.push(e),
                _ => out.malformed += 1,
            }
        }
    }
    Ok(out)
}

/// Collapse records that share an `event_id` (the pre-challenge `Cancelled`
/// record followed by the final outcome), keeping the last one.
#[must_use]
pub fn collapse_events(entries: Vec<AuditEntry>) -> Vec<AuditEntry> {
    let mut last_index: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
    for (i, e) in entries.iter().enumerate() {
        if !e.event_id.is_empty() {
            last_index.insert(e.event_id.clone(), i);
        }
    }
    entries
        .into_iter()
        .enumerate()
        .filter(|(i, e)| e.event_id.is_empty() || last_index.get(&e.event_id) == Some(i))
        .map(|(_, e)| e)
        .collect()
}

/// Filters for `audit show`.
#[derive(Debug, Clone, Default)]
pub struct AuditFilter {
    /// Only records at or after this timestamp (`YYYY-MM-DDTHH:MM:SSZ`).
    pub since: Option<String>,
    pub outcome: Option<AuditOutcome>,
    /// Only records at or above this severity.
    pub min_severity: Option<Severity>,
    /// Keep only the last N records (after the other filters).
    pub limit: Option<usize>,
}

/// Apply a filter.
#[must_use]
pub fn filter_entries(entries: Vec<AuditEntry>, f: &AuditFilter) -> Vec<AuditEntry> {
    let mut v: Vec<AuditEntry> = entries
        .into_iter()
        .filter(|e| {
            f.since
                .as_ref()
                .is_none_or(|s| e.timestamp.as_str() >= s.as_str())
        })
        .filter(|e| {
            f.outcome
                .as_ref()
                .is_none_or(|o| e.outcome.as_ref() == Some(o))
        })
        .filter(|e| {
            f.min_severity
                .is_none_or(|min| e.severity.is_some_and(|s| s >= min))
        })
        .collect();
    if let Some(n) = f.limit {
        if v.len() > n {
            v.drain(..v.len() - n);
        }
    }
    v
}

/// Parse a `--since` value into a comparable UTC timestamp.
///
/// Accepts a relative duration (`30m`, `12h`, `7d`, `2w`), a date
/// (`2026-01-31`) or an RFC 3339 timestamp (`2026-01-31T10:00:00Z`, with
/// fractional seconds or an offset: `2026-01-31T12:00:00.5+02:00`, also
/// written `+0200`). A
/// timestamp without an offset (`2026-01-31T10:00:00`) is taken as UTC.
///
/// # Errors
/// Returns an error describing the accepted formats.
pub fn parse_since(value: &str) -> Result<String> {
    let v = value.trim();
    let err = || {
        Error::Config(format!(
            "invalid --since value '{value}'. Use a duration like 30m, 12h, 7d, 2w, \
             a date like 2026-01-31, or a timestamp like 2026-01-31T10:00:00Z \
             or 2026-01-31T10:00:00+02:00 (no offset means UTC)"
        ))
    };
    let is_duration =
        v.is_ascii() && v.len() >= 2 && v[..v.len() - 1].chars().all(|c| c.is_ascii_digit());
    if let Some(unit) = v
        .chars()
        .last()
        .filter(|c| is_duration && c.is_ascii_alphabetic())
    {
        let num: u64 = v[..v.len() - 1].parse().map_err(|_| err())?;
        let secs = match unit {
            's' => num,
            'm' => num * 60,
            'h' => num * 3600,
            'd' => num * 86_400,
            'w' => num * 7 * 86_400,
            _ => return Err(err()),
        };
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        return Ok(timestamp_from_epoch(now.saturating_sub(secs)));
    }
    let bytes = v.as_bytes();
    let is_date = v.len() == 10
        && bytes[4] == b'-'
        && bytes[7] == b'-'
        && v.chars()
            .enumerate()
            .all(|(i, c)| i == 4 || i == 7 || c.is_ascii_digit());
    if is_date {
        return if valid_date(&v[..10]) {
            Ok(format!("{v}T00:00:00Z"))
        } else {
            Err(err())
        };
    }
    // A timestamp without an offset is taken as UTC.
    parse_datetime(v)
        .map(|(secs, _)| timestamp_from_epoch(secs))
        .ok_or_else(err)
}

/// An RFC 3339 timestamp (`2026-01-31T10:00:00Z`, `…T10:00:00.123Z`,
/// `…T12:00:00+02:00`) as `YYYY-MM-DDTHH:MM:SSZ` in UTC (fractions of a
/// second dropped). A timestamp without an offset gives `None`.
#[must_use]
pub fn parse_timestamp(s: &str) -> Option<String> {
    parse_datetime(s)
        .filter(|&(_, offset)| offset)
        .map(|(secs, _)| timestamp_from_epoch(secs))
}

/// `YYYY-MM-DD[T ]HH:MM:SS[.fraction][Z|±HH:MM|±HHMM]` as seconds since the Unix
/// epoch, and whether it had an offset (`Z` included).
fn parse_datetime(value: &str) -> Option<(u64, bool)> {
    let text = value.trim();
    if !text.is_ascii() || text.len() < 19 {
        return None;
    }
    let bytes = text.as_bytes();
    let num = |r: std::ops::Range<usize>| -> Option<i64> {
        bytes[r.clone()]
            .iter()
            .all(u8::is_ascii_digit)
            .then(|| text[r].parse().ok())
            .flatten()
    };
    if !valid_date(&text[..10])
        || !matches!(bytes[10], b'T' | b't' | b' ')
        || bytes[13] != b':'
        || bytes[16] != b':'
    {
        return None;
    }
    let (hour, min, sec) = (num(11..13)?, num(14..16)?, num(17..19)?);
    if hour > 23 || min > 59 || sec > 60 {
        return None;
    }
    let mut rest = &text[19..];
    if let Some(frac) = rest.strip_prefix('.') {
        let digits = frac.bytes().take_while(u8::is_ascii_digit).count();
        if digits == 0 {
            return None;
        }
        rest = &frac[digits..];
    }
    let (offset, has_offset) = match rest.as_bytes() {
        [] => (0, false),
        [b'Z' | b'z'] => (0, true),
        [sign @ (b'+' | b'-'), h1, h2, b':', m1, m2] | [sign @ (b'+' | b'-'), h1, h2, m1, m2] => {
            let digits = [*h1, *h2, *m1, *m2];
            if !digits.iter().all(u8::is_ascii_digit) {
                return None;
            }
            let d = |i: usize| i64::from(digits[i] - b'0');
            let (oh, om) = (d(0) * 10 + d(1), d(2) * 10 + d(3));
            if oh > 23 || om > 59 {
                return None;
            }
            let o = oh * 3600 + om * 60;
            (if *sign == b'-' { -o } else { o }, true)
        }
        _ => return None,
    };
    let days = days_from_civil(num(0..4)?, num(5..7)?, num(8..10)?);
    let secs = days * 86_400 + hour * 3600 + min * 60 + sec - offset;
    u64::try_from(secs).ok().map(|secs| (secs, has_offset))
}

/// Days since 1970-01-01 of a (valid) date (Howard Hinnant's
/// `days_from_civil`).
const fn days_from_civil(y: i64, m: i64, d: i64) -> i64 {
    let y = if m <= 2 { y - 1 } else { y };
    let era = y.div_euclid(400);
    let yoe = y - era * 400;
    let mp = if m > 2 { m - 3 } else { m + 9 };
    let doy = (153 * mp + 2) / 5 + d - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    era * 146_097 + doe - 719_468
}

/// `YYYY-MM-DD` with a real month and day.
fn valid_date(d: &str) -> bool {
    let b = d.as_bytes();
    if d.len() != 10 || !d.is_ascii() || b[4] != b'-' || b[7] != b'-' {
        return false;
    }
    let (Ok(y), Ok(m), Ok(day)) = (
        d[..4].parse::<u32>(),
        d[5..7].parse::<u32>(),
        d[8..10].parse::<u32>(),
    ) else {
        return false;
    };
    let leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
    let days = match m {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 if leap => 29,
        2 => 28,
        _ => return false,
    };
    (1..=days).contains(&day)
}

/// Render records as a human-readable table.
#[must_use]
pub fn format_table(entries: &[AuditEntry]) -> String {
    use std::fmt::Write as _;
    let mut out = String::new();
    let _ = writeln!(
        out,
        "{:<20}  {:<9}  {:<8}  {:<28}  COMMAND",
        "TIME (UTC)", "OUTCOME", "SEVERITY", "CHECKS"
    );
    for e in entries {
        let outcome = e
            .outcome
            .as_ref()
            .map_or_else(|| "?".to_string(), ToString::to_string);
        let sev = e
            .severity
            .map_or_else(|| "?".to_string(), |s| s.to_string());
        let mut checks = e.matched_ids.join(",");
        if checks.chars().count() > 28 {
            checks = format!("{}…", checks.chars().take(27).collect::<String>());
        }
        // Invisible and bidi characters are shown escaped (`\u{202e}`): the
        // JSON formats keep the real text.
        let mut cmd = crate::context::escape_invisible(&e.command.replace('\n', " "));
        if cmd.chars().count() > 80 {
            cmd = format!("{}…", cmd.chars().take(79).collect::<String>());
        }
        let mut extras = Vec::new();
        if let Some(a) = &e.agent_name {
            extras.push(format!("agent={}", crate::context::escape_invisible(a)));
        }
        if let Some(t) = &e.tool_use_id {
            extras.push(format!(
                "tool_use_id={}",
                crate::context::escape_invisible(t)
            ));
        }
        if let Some(r) = &e.deny_reason {
            extras.push(format!("reason={r}"));
        }
        // A kubectl / helm row names the cluster context it ran against;
        // the git branch is only shown for the other rows (and git checks).
        let k8s: Vec<&String> = e
            .context_labels
            .iter()
            .filter(|l| l.starts_with("k8s="))
            .collect();
        for l in &k8s {
            extras.push(crate::context::escape_invisible(l));
        }
        if let Some(b) = e
            .git_branch
            .as_ref()
            .filter(|_| k8s.is_empty() || e.matched_ids.iter().any(|id| id.starts_with("git")))
        {
            extras.push(format!("branch={}", crate::context::escape_invisible(b)));
        }
        let extras = if extras.is_empty() {
            String::new()
        } else {
            format!("  [{}]", extras.join(" "))
        };
        let _ = writeln!(
            out,
            "{:<20}  {outcome:<9}  {sev:<8}  {checks:<28}  {cmd}{extras}",
            e.timestamp
        );
    }
    out
}

/// Clear the audit log (and every rotated generation).
///
/// # Errors
/// Returns an error if the file cannot be removed.
pub fn clear_log(audit_path: &Path) -> Result<()> {
    for p in log_files(audit_path) {
        if p.exists() {
            fs::remove_file(&p).map_err(|e| {
                Error::Other(format!("could not remove audit log {}: {e}", p.display()))
            })?;
        }
    }
    Ok(())
}

/// Get the current timestamp in ISO 8601 format.
#[must_use]
pub fn now_timestamp() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    timestamp_from_epoch(secs)
}

/// Format seconds since the Unix epoch as `YYYY-MM-DDTHH:MM:SSZ` (UTC).
#[must_use]
pub fn timestamp_from_epoch(secs: u64) -> String {
    let days = secs / 86400;
    let remaining = secs % 86400;
    let hours = remaining / 3600;
    let minutes = (remaining % 3600) / 60;
    let seconds = remaining % 60;
    let (year, month, day) = epoch_days_to_date(days);
    format!("{year:04}-{month:02}-{day:02}T{hours:02}:{minutes:02}:{seconds:02}Z")
}

/// Convert epoch days to (year, month, day). Simplified algorithm.
const fn epoch_days_to_date(days: u64) -> (u64, u64, u64) {
    // Algorithm based on Howard Hinnant's civil_from_days
    let z = days + 719_468;
    let era = z / 146_097;
    let doe = z - era * 146_097;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = if m <= 2 { y + 1 } else { y };
    (y, m, d)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    fn event(id: &str, outcome: AuditOutcome, severity: Severity, ts: &str) -> AuditEvent {
        AuditEvent {
            event_id: id.into(),
            timestamp: ts.into(),
            command: "git push -f".into(),
            matched_ids: vec!["git:force_push".into()],
            challenge_type: "Math".into(),
            outcome,
            context_labels: vec!["branch=main".into()],
            severity,
            agent_name: None,
            agent_session_id: None,
            blast_radius_scope: None,
            blast_radius_detail: None,
            attempts: None,
        }
    }

    #[test]
    fn test_log_and_read() {
        let temp = tree_fs::TreeBuilder::default()
            .create()
            .expect("create tree");
        let path = temp.root.join("audit.log");

        let ev = event(
            "test-event-1",
            AuditOutcome::Allowed,
            Severity::High,
            "2026-02-15T10:00:00Z",
        );
        log_event(&path, &ev).unwrap();
        let content = read_log(&path).unwrap();
        // JSON lines format: each line is a valid JSON object, readable as
        // the original AuditEvent (backward compatible).
        let parsed: AuditEvent = serde_json::from_str(content.trim()).unwrap();
        assert_eq!(parsed.event_id, "test-event-1");
        assert_eq!(parsed.command, "git push -f");
        assert_eq!(parsed.outcome, AuditOutcome::Allowed);
        assert_eq!(parsed.matched_ids, vec!["git:force_push"]);
        assert_eq!(parsed.context_labels, vec!["branch=main"]);
        assert_eq!(parsed.severity, Severity::High);

        let entry: AuditEntry = serde_json::from_str(content.trim()).unwrap();
        assert_eq!(entry.schema_version, Some(AUDIT_SCHEMA_VERSION));
        assert!(entry.cwd.is_some());
    }

    #[test]
    fn extra_fields_are_written() {
        let temp = tree_fs::TreeBuilder::default().create().unwrap();
        let path = temp.root.join("audit.log");
        let extra = AuditExtra {
            policy_sources: vec!["/repo/.shellfirm.yaml".into()],
            effective_challenge: Some("Yes".into()),
            deny_reason: Some("policy_deny".into()),
            policy_check_ids: vec!["git:force_push".into()],
            escalation_reasons: vec!["protected branch `main`".into()],
            tool_use_id: Some("toolu_1".into()),
        };
        log_event_with(
            &path,
            &event(
                "x",
                AuditOutcome::Denied,
                Severity::High,
                "2026-02-15T10:00:00Z",
            ),
            &extra,
        )
        .unwrap();
        let read = read_entries(&path).unwrap();
        assert_eq!(read.entries.len(), 1);
        let e = &read.entries[0];
        assert_eq!(e.deny_reason.as_deref(), Some("policy_deny"));
        assert_eq!(e.effective_challenge.as_deref(), Some("Yes"));
        assert_eq!(e.policy_sources, vec!["/repo/.shellfirm.yaml".to_string()]);
        // `audit show --format json|jsonl` keeps the tool_use_id.
        assert_eq!(e.tool_use_id.as_deref(), Some("toolu_1"));
        let json = serde_json::to_value(e).unwrap();
        assert_eq!(json["tool_use_id"], "toolu_1");
        assert!(format_table(&read.entries).contains("tool_use_id=toolu_1"));
    }

    #[test]
    fn table_shows_the_k8s_context_for_kubectl_rows() {
        let kube = AuditEntry {
            command: "kubectl delete deploy api".into(),
            matched_ids: vec!["kubernetes-strict:delete_resource".into()],
            context_labels: vec!["k8s=prod-eu".into()],
            git_branch: Some("main".into()),
            ..AuditEntry::default()
        };
        let git = AuditEntry {
            command: "git push -f".into(),
            matched_ids: vec!["git:force_push".into()],
            git_branch: Some("main".into()),
            ..AuditEntry::default()
        };
        let table = format_table(&[kube, git]);
        let lines: Vec<&str> = table.lines().collect();
        assert!(lines[1].contains("[k8s=prod-eu]"), "{table}");
        assert!(!lines[1].contains("branch="), "{table}");
        assert!(lines[2].contains("[branch=main]"), "{table}");
    }

    #[test]
    fn sqlcmd_and_bcp_passwords_are_redacted() {
        for (cmd, want) in [
            (
                "sqlcmd -S db -U sa -P S3cret! -Q 'select 1'",
                "sqlcmd -S db -U sa -P *** -Q 'select 1'",
            ),
            (
                "sqlcmd -S db -U sa -PS3cret -i x.sql",
                "sqlcmd -S db -U sa -P*** -i x.sql",
            ),
            (
                "bcp dbo.t out t.dat -S db -U sa -P 'a b' -c",
                "bcp dbo.t out t.dat -S db -U sa -P *** -c",
            ),
            (
                "bcp dbo.t out t.dat -Uu -Ppw",
                "bcp dbo.t out t.dat -Uu -P***",
            ),
        ] {
            assert_eq!(redact_secrets(cmd).0, want, "{cmd}");
        }
        // `-p` (statistics) is not a password.
        assert!(!redact_secrets("sqlcmd -S db -p -Q 'select 1'").1);
    }

    #[test]
    fn table_escapes_invisible_characters_but_json_keeps_them() {
        let e = AuditEntry {
            timestamp: "2026-02-15T10:00:00Z".into(),
            command: "rm -rf src\u{202e}.txt # \u{200b}x".into(),
            git_branch: Some("main\u{200b}".into()),
            ..AuditEntry::default()
        };
        let table = format_table(std::slice::from_ref(&e));
        assert!(table.contains("src\\u{202e}.txt"), "{table}");
        assert!(table.contains("branch=main\\u{200b}"), "{table}");
        assert!(!table.contains('\u{202e}'));
        let json = serde_json::to_value(&e).unwrap();
        assert_eq!(json["command"], "rm -rf src\u{202e}.txt # \u{200b}x");
    }

    #[test]
    fn old_records_still_parse() {
        let temp = tree_fs::TreeBuilder::default().create().unwrap();
        let path = temp.root.join("audit.log");
        fs::write(
            &path,
            "{\"event_id\":\"a\",\"timestamp\":\"2026-01-01T00:00:00Z\",\"command\":\"rm -rf /\",\"matched_ids\":[\"fs:x\"],\"challenge_type\":\"Math\",\"outcome\":\"Allowed\",\"context_labels\":[],\"severity\":\"Critical\"}\nnot json\n",
        )
        .unwrap();
        let read = read_entries(&path).unwrap();
        assert_eq!(read.entries.len(), 1);
        assert_eq!(read.malformed, 1);
        assert_eq!(read.entries[0].schema_version, None);
    }

    #[cfg(unix)]
    #[test]
    fn audit_file_is_private() {
        use std::os::unix::fs::PermissionsExt;
        let temp = tree_fs::TreeBuilder::default().create().unwrap();
        let path = temp.root.join("audit.log");
        fs::write(&path, "").unwrap();
        fs::set_permissions(&path, fs::Permissions::from_mode(0o644)).unwrap();
        log_event(
            &path,
            &event(
                "p",
                AuditOutcome::Allowed,
                Severity::Low,
                "2026-01-01T00:00:00Z",
            ),
        )
        .unwrap();
        let mode = fs::metadata(&path).unwrap().permissions().mode() & 0o777;
        assert_eq!(mode, 0o600);
    }

    #[test]
    fn concurrent_writers_do_not_interleave() {
        let temp = tree_fs::TreeBuilder::default().create().unwrap();
        let path = temp.root.join("audit.log");
        let big = "x".repeat(8 * 1024);
        let handles: Vec<_> = (0..16)
            .map(|t| {
                let path = path.clone();
                let big = big.clone();
                std::thread::spawn(move || {
                    for i in 0..10 {
                        let mut ev = event(
                            &format!("{t}-{i}"),
                            AuditOutcome::Allowed,
                            Severity::Low,
                            "2026-01-01T00:00:00Z",
                        );
                        ev.command.clone_from(&big);
                        log_event(&path, &ev).unwrap();
                    }
                })
            })
            .collect();
        for h in handles {
            h.join().unwrap();
        }
        let read = read_entries(&path).unwrap();
        assert_eq!(read.malformed, 0);
        assert_eq!(read.entries.len(), 160);
    }

    #[test]
    fn rotation_moves_old_log_aside() {
        let temp = tree_fs::TreeBuilder::default().create().unwrap();
        let path = temp.root.join("audit.log");
        let ev = event(
            "r",
            AuditOutcome::Allowed,
            Severity::Low,
            "2026-01-01T00:00:00Z",
        );
        log_event_with_limit(&path, &ev, &AuditExtra::default(), 10).unwrap();
        log_event_with_limit(&path, &ev, &AuditExtra::default(), 10).unwrap();
        assert!(rotated_path(&path).exists());
        let read = read_entries(&path).unwrap();
        assert_eq!(read.entries.len(), 2);
        clear_log(&path).unwrap();
        assert!(!path.exists());
        assert!(!rotated_path(&path).exists());
    }

    #[test]
    fn collapse_and_filter() {
        let mk = |id: &str, o: AuditOutcome, s: Severity, ts: &str| -> AuditEntry {
            serde_json::from_str(&serde_json::to_string(&event(id, o, s, ts)).unwrap()).unwrap()
        };
        let entries = vec![
            mk(
                "a",
                AuditOutcome::Cancelled,
                Severity::High,
                "2026-01-01T00:00:00Z",
            ),
            mk(
                "a",
                AuditOutcome::Allowed,
                Severity::High,
                "2026-01-01T00:00:01Z",
            ),
            mk(
                "b",
                AuditOutcome::Denied,
                Severity::Critical,
                "2026-02-01T00:00:00Z",
            ),
            mk(
                "c",
                AuditOutcome::Skipped,
                Severity::Low,
                "2026-03-01T00:00:00Z",
            ),
        ];
        let collapsed = collapse_events(entries);
        assert_eq!(collapsed.len(), 3);
        assert_eq!(collapsed[0].outcome, Some(AuditOutcome::Allowed));

        let f = AuditFilter {
            outcome: Some(AuditOutcome::Denied),
            ..Default::default()
        };
        assert_eq!(filter_entries(collapsed.clone(), &f).len(), 1);
        let f = AuditFilter {
            min_severity: Some(Severity::High),
            ..Default::default()
        };
        assert_eq!(filter_entries(collapsed.clone(), &f).len(), 2);
        let f = AuditFilter {
            since: Some("2026-02-01T00:00:00Z".into()),
            ..Default::default()
        };
        assert_eq!(filter_entries(collapsed.clone(), &f).len(), 2);
        let f = AuditFilter {
            limit: Some(1),
            ..Default::default()
        };
        let last = filter_entries(collapsed, &f);
        assert_eq!(last.len(), 1);
        assert_eq!(last[0].event_id, "c");
        assert!(format_table(&last).contains("SKIPPED"));
    }

    #[test]
    fn parse_since_formats() {
        assert_eq!(parse_since("2026-01-31").unwrap(), "2026-01-31T00:00:00Z");
        assert_eq!(
            parse_since("2026-01-31T10:00:00Z").unwrap(),
            "2026-01-31T10:00:00Z"
        );
        assert_eq!(parse_since("7d").unwrap().len(), 20);
        assert!(parse_since("yesterday").is_err());
        assert!(parse_since("7y").is_err());
        let msg = parse_since("yesterday").unwrap_err().to_string();
        assert!(msg.contains("2026-01-31T10:00:00+02:00"), "{msg}");
    }

    #[test]
    fn parse_since_takes_offsets_fractions_and_naive_times() {
        for (input, want) in [
            ("2026-11-19T00:00:00+05:00", "2026-11-18T19:00:00Z"),
            ("2026-11-19T22:30:00-02:30", "2026-11-20T01:00:00Z"),
            ("2026-11-19T10:00:00.123Z", "2026-11-19T10:00:00Z"),
            ("2026-11-19T10:00:00.5+00:00", "2026-11-19T10:00:00Z"),
            ("2026-11-19T10:00:00", "2026-11-19T10:00:00Z"),
            ("2026-11-19 10:00:00", "2026-11-19T10:00:00Z"),
            ("2024-03-01T00:30:00+01:00", "2024-02-29T23:30:00Z"),
            ("2026-11-19T10:00:00+0200", "2026-11-19T08:00:00Z"),
            ("2026-11-19T00:00:00-0500", "2026-11-19T05:00:00Z"),
            ("1970-01-01T00:00:00Z", "1970-01-01T00:00:00Z"),
        ] {
            assert_eq!(parse_since(input).unwrap(), want, "{input}");
        }
        for bad in [
            "2026-11-19T10:00:00.Z",
            "2026-11-19T10:00:00+5",
            "2026-11-19T10:00:00+24:00",
            "2026-11-19T10:00:00+020",
            "2026-11-19T10:00:00+2400",
            "2026-11-19T24:00:00Z",
            "2026-02-30T10:00:00Z",
            "2026-11-19T10:00Z",
            "2026-11-19T10:00:00ZZ",
            "1970-01-01T00:00:00+01:00",
        ] {
            assert!(parse_since(bad).is_err(), "{bad}");
        }
        assert_eq!(parse_timestamp("2026-11-19T10:00:00"), None);
        assert_eq!(
            parse_timestamp("2026-11-19T12:00:00.999+02:00").as_deref(),
            Some("2026-11-19T10:00:00Z")
        );
    }

    #[test]
    fn outcome_parse() {
        assert_eq!(AuditOutcome::parse("DENIED").unwrap(), AuditOutcome::Denied);
        assert!(AuditOutcome::parse("nope").is_err());
    }

    #[test]
    fn git_info_reads_head() {
        let temp = tree_fs::TreeBuilder::default()
            .add(".git/HEAD", "ref: refs/heads/feature/x\n")
            .add("sub/file", "")
            .create()
            .unwrap();
        let (repo, branch) = git_info(&temp.root.join("sub"));
        assert_eq!(repo.as_deref(), Some(temp.root.as_path()));
        assert_eq!(branch.as_deref(), Some("feature/x"));
    }

    #[test]
    fn test_read_nonexistent_log() {
        let path = PathBuf::from("/tmp/nonexistent-audit-test.log");
        let result = read_log(&path).unwrap();
        assert!(result.contains("No audit events"));
    }

    #[test]
    fn test_cancelled_outcome_serialization() {
        let temp = tree_fs::TreeBuilder::default()
            .create()
            .expect("create tree");
        let path = temp.root.join("audit.log");
        log_event(
            &path,
            &event(
                "cancel-test-1",
                AuditOutcome::Cancelled,
                Severity::Critical,
                "2026-02-15T10:00:00Z",
            ),
        )
        .unwrap();
        let content = read_log(&path).unwrap();
        let parsed: AuditEvent = serde_json::from_str(content.trim()).unwrap();
        assert_eq!(parsed.outcome, AuditOutcome::Cancelled);
        assert_eq!(format!("{}", parsed.outcome), "CANCELLED");
    }

    #[test]
    fn git_meta_follows_the_target_repository() {
        let temp = tree_fs::TreeBuilder::default()
            .add("infra/.git/HEAD", "ref: refs/heads/infra-main\n")
            .add("infra/x", "")
            .create()
            .unwrap();
        // `/`-separated, as written in a shell command (on Windows
        // `C:/Users/…`; a `\` in a shell word is an escape).
        let infra = temp
            .root
            .join("infra")
            .display()
            .to_string()
            .replace('\\', "/");
        for command in [
            format!("git -C {infra} push -f"),
            format!("cd {infra} && git reset --hard"),
            format!("cd {infra} && rm -rf x"),
        ] {
            let meta = collect_meta_for(Some(&command));
            assert_eq!(meta.git_repo.as_deref(), Some(infra.as_str()), "{command}");
            assert_eq!(meta.git_branch.as_deref(), Some("infra-main"), "{command}");
            assert_eq!(
                meta.cwd,
                std::env::current_dir()
                    .ok()
                    .map(|c| c.display().to_string())
            );
        }
        // No other directory: the current one's repository.
        let here = collect_meta_for(Some("git push -f"));
        assert_eq!(here.git_repo, collect_meta().git_repo);
    }

    #[test]
    fn test_now_timestamp_format() {
        let ts = now_timestamp();
        assert!(ts.contains('T'));
        assert!(ts.ends_with('Z'));
        assert_eq!(ts.len(), 20);
    }

    #[test]
    fn sha256_matches_known_vectors() {
        assert_eq!(
            sha256_hex(b""),
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
        );
        assert_eq!(
            sha256_hex(b"abc"),
            "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
        );
        assert_eq!(
            sha256_hex(&[b'a'; 1000]),
            "41edece42d63e8d9bf515a9ba6932e1c20cbc9f5a5d134645adb5db1b9737ea3"
        );
    }

    #[test]
    fn huge_commands_are_truncated_and_hashed() {
        let temp = tree_fs::TreeBuilder::default().create().unwrap();
        let path = temp.root.join("audit.log");
        let mut ev = event(
            "big",
            AuditOutcome::Asked,
            Severity::High,
            "2026-01-01T00:00:00Z",
        );
        ev.command = format!("echo {} && rm -rf ./src", "é".repeat(3 * 1024 * 1024));
        log_event(&path, &ev).unwrap();
        assert!(fs::metadata(&path).unwrap().len() < 16 * 1024);
        let e = &read_entries(&path).unwrap().entries[0];
        assert!(e.command_truncated);
        assert_eq!(e.command_bytes, Some(ev.command.len()));
        assert_eq!(
            e.command_sha256.as_deref(),
            Some(sha256_hex(ev.command.as_bytes()).as_str())
        );
    }

    #[test]
    fn rotation_keeps_n_generations_and_show_reads_them() {
        let temp = tree_fs::TreeBuilder::default().create().unwrap();
        let path = temp.root.join("audit.log");
        for i in 0..5 {
            fs::write(
                &path,
                format!(
                    "{}\n",
                    serde_json::to_string(&event(
                        &format!("e{i}"),
                        AuditOutcome::Allowed,
                        Severity::Low,
                        "2026-01-01T00:00:00Z"
                    ))
                    .unwrap()
                ),
            )
            .unwrap();
            rotate_files(&path, 3).unwrap();
        }
        let files = log_files(&path);
        assert_eq!(files.len(), 3, "{files:?}");
        assert!(!rotated_path_n(&path, 4).exists());
        let ids: Vec<String> = read_entries(&path)
            .unwrap()
            .entries
            .into_iter()
            .map(|e| e.event_id)
            .collect();
        assert_eq!(ids, vec!["e2", "e3", "e4"]);
        clear_log(&path).unwrap();
        assert!(log_files(&path).is_empty());
    }

    #[test]
    fn invalid_utf8_and_incomplete_lines_are_skipped() {
        let temp = tree_fs::TreeBuilder::default().create().unwrap();
        let path = temp.root.join("audit.log");
        let good = serde_json::to_string(&event(
            "g",
            AuditOutcome::Denied,
            Severity::High,
            "2026-01-01T00:00:00Z",
        ))
        .unwrap();
        let mut bytes = b"\xff\xfe\n{\"foo\":1}\n".to_vec();
        bytes.extend_from_slice(good.as_bytes());
        bytes.push(b'\n');
        fs::write(&path, bytes).unwrap();
        let r = read_entries(&path).unwrap();
        assert_eq!(r.entries.len(), 1);
        assert_eq!(r.malformed, 2);
    }

    #[test]
    fn parse_since_rejects_impossible_dates() {
        assert!(parse_since("2026-13-01").is_err());
        assert!(parse_since("2026-02-30").is_err());
        assert!(parse_since("2024-02-29").is_ok());
        assert!(parse_since("2026-01-31T25:00:00Z").is_err());
        assert!(parse_since("2026-01-31TAB:00:00Z").is_err());
    }

    #[test]
    fn no_terminal_outcome_round_trips() {
        assert_eq!(
            AuditOutcome::parse("no-terminal").unwrap(),
            AuditOutcome::NoTerminal
        );
        assert_eq!(
            serde_json::to_string(&AuditOutcome::NoTerminal).unwrap(),
            "\"NoTerminal\""
        );
    }

    #[test]
    fn identity_comes_from_the_uid() {
        let meta = collect_meta();
        #[cfg(unix)]
        assert_eq!(meta.uid, current_uid());
        assert_eq!(meta.schema_version, AUDIT_SCHEMA_VERSION);
    }

    #[test]
    fn extra_for_policy_deny() {
        let merged = crate::policy::MergedPolicy {
            extra_deny: vec!["git:force_push".into()],
            enforced_ids: ["git:force_push".to_string()].into_iter().collect(),
            sources: vec![PathBuf::from("/repo/.shellfirm.yaml")],
            ..Default::default()
        };
        let x = AuditExtra::for_decision(
            &merged,
            &[],
            &["git:force_push".into()],
            true,
            Some("Deny".into()),
            vec![],
        );
        assert_eq!(x.deny_reason.as_deref(), Some("policy_deny"));
        assert_eq!(x.policy_sources, vec!["/repo/.shellfirm.yaml".to_string()]);
        let y = AuditExtra::for_decision(
            &crate::policy::MergedPolicy::default(),
            &["git:reset".into()],
            &["git:reset".into()],
            true,
            None,
            vec![],
        );
        assert_eq!(y.deny_reason.as_deref(), Some("user_deny"));
        assert!(y.policy_sources.is_empty());
    }

    #[test]
    fn extra_policy_sources_are_the_files_that_name_the_id() {
        let mut id_sources = std::collections::BTreeMap::new();
        id_sources.insert(
            "kubernetes:delete_namespace".to_string(),
            vec![PathBuf::from("/etc/shellfirm/policy.yaml")],
        );
        id_sources.insert(
            "git:reset".to_string(),
            vec![PathBuf::from("/repo/.shellfirm.yaml")],
        );
        let merged = crate::policy::MergedPolicy {
            extra_deny: vec!["kubernetes:delete_namespace".into()],
            enforced_ids: [
                "kubernetes:delete_namespace".to_string(),
                "git:reset".to_string(),
            ]
            .into_iter()
            .collect(),
            sources: vec![
                PathBuf::from("/etc/shellfirm/policy.yaml"),
                PathBuf::from("/repo/.shellfirm.yaml"),
            ],
            org_sources: vec![PathBuf::from("/etc/shellfirm/policy.yaml")],
            id_sources,
            ..Default::default()
        };
        let x = AuditExtra::for_decision(
            &merged,
            &[],
            &["kubernetes:delete_namespace".into()],
            true,
            Some("Deny".into()),
            vec![],
        );
        assert_eq!(x.deny_reason.as_deref(), Some("policy_deny"));
        assert_eq!(
            x.policy_sources,
            vec!["/etc/shellfirm/policy.yaml".to_string()]
        );
        // A personal deny of an id the policy only escalates is a user deny,
        // but the policy enforcement is still attributed.
        let y = AuditExtra::for_decision(
            &merged,
            &["git:reset".into()],
            &["git:reset".into()],
            true,
            None,
            vec![],
        );
        assert_eq!(y.deny_reason.as_deref(), Some("user_deny"));
        assert_eq!(y.policy_check_ids, vec!["git:reset".to_string()]);
        assert_eq!(y.policy_sources, vec!["/repo/.shellfirm.yaml".to_string()]);
        // Not denied: no reason.
        let z = AuditExtra::for_decision(&merged, &[], &["git:reset".into()], false, None, vec![]);
        assert!(z.deny_reason.is_none());
    }

    #[test]
    fn max_files_is_never_zero() {
        assert_eq!(clamp_max_files(0), 1);
        assert_eq!(clamp_max_files(3), 3);
        assert_eq!(clamp_max_files(1000), MAX_AUDIT_FILES_LIMIT);
    }

    #[test]
    fn rotation_with_keep_zero_still_keeps_one_generation() {
        let temp = tree_fs::TreeBuilder::default().create().unwrap();
        let path = temp.root.join("audit.log");
        fs::write(&path, "old\n").unwrap();
        rotate_files(&path, 0).unwrap();
        assert_eq!(fs::read_to_string(rotated_path(&path)).unwrap(), "old\n");
    }

    /// Test inputs are assembled from pieces so this source file itself
    /// does not read like a risky command to shell hooks scanning it.
    fn j(parts: &[&str]) -> String {
        parts.concat()
    }

    #[test]
    fn unescape_invisible_restores_only_invisible_characters() {
        let shown = crate::context::escape_invisible("src\u{202e}.txt and b\u{200b}");
        assert_eq!(shown, "src\\u{202e}.txt and b\\u{200b}");
        assert_eq!(unescape_invisible(&shown), "src\u{202e}.txt and b\u{200b}");
        // Printable or invalid code points stay as written.
        assert_eq!(unescape_invisible("a\\u{41}b"), "a\\u{41}b");
        assert_eq!(unescape_invisible("a\\u{zz}\\u{"), "a\\u{zz}\\u{");
        assert_eq!(unescape_invisible("plain"), "plain");
    }

    #[test]
    fn deny_reason_for_outcome_tells_cancel_from_no_tty() {
        assert_eq!(
            deny_reason::for_outcome(&AuditOutcome::Cancelled),
            Some(deny_reason::CANCELLED)
        );
        assert_eq!(
            deny_reason::for_outcome(&AuditOutcome::NoTerminal),
            Some(deny_reason::NO_TTY)
        );
        assert_eq!(deny_reason::for_outcome(&AuditOutcome::Allowed), None);
        assert_ne!(deny_reason::CANCELLED, deny_reason::CHALLENGE_FAILED);
    }

    #[test]
    fn stored_blast_radius_detail_is_the_real_text() {
        let tmp = tree_fs::TreeBuilder::default().create().unwrap();
        let path = tmp.root.join("audit.log");
        let mut ev = event(
            "e1",
            AuditOutcome::Cancelled,
            Severity::High,
            "2026-02-15T10:00:00Z",
        );
        ev.blast_radius_detail = Some(crate::context::escape_invisible(
            "Deletes 2 paths (src\u{202e}.txt, b)",
        ));
        log_event(&path, &ev).unwrap();
        let line = std::fs::read_to_string(&path).unwrap();
        let v: serde_json::Value = serde_json::from_str(line.trim()).unwrap();
        assert_eq!(
            v["blast_radius_detail"].as_str(),
            Some("Deletes 2 paths (src\u{202e}.txt, b)")
        );
    }

    #[test]
    fn secrets_are_redacted() {
        let cases: Vec<(String, String)> = vec![
            (
                j(&["mysql -u root -pS3cret! db"]),
                j(&["mysql -u root -p*** db"]),
            ),
            (
                j(&["mysqldump --password=S3cret db"]),
                j(&["mysqldump --password=*** db"]),
            ),
            (
                j(&["PGPASSWORD=hunter2 psql -h db"]),
                j(&["PGPASSWORD=*** psql -h db"]),
            ),
            (j(&["MYSQL_PWD='a b' mysql"]), j(&["MYSQL_PWD=*** mysql"])),
            (
                j(&["export GITHUB_TOKEN=abc123; gh repo list"]),
                j(&["export GITHUB_TOKEN=***; gh repo list"]),
            ),
            (
                j(&["AWS_SECRET_ACCESS_KEY=xyz aws s3 ls"]),
                j(&["AWS_SECRET_ACCESS_KEY=*** aws s3 ls"]),
            ),
            (
                j(&["psql postgres://admin:pa55@db.prod:5432/app -c 'select 1'"]),
                j(&["psql postgres://admin:***@db.prod:5432/app -c 'select 1'"]),
            ),
            (
                j(&[r#"curl -H "Authorization: Bearer eyJhbGci.x.y" https://api/x"#]),
                j(&[r#"curl -H "Authorization: Bearer ***" https://api/x"#]),
            ),
            (
                j(&["curl -u admin:secret https://h/x"]),
                j(&["curl -u admin:*** https://h/x"]),
            ),
            (
                j(&["aws --profile x s3 ls # AKIA", "IOSFODNN7EXAMPLE"]),
                j(&["aws --profile x s3 ls # ***"]),
            ),
            (
                j(&["vault login --token s.abcdef"]),
                j(&["vault login --token ***"]),
            ),
            (
                j(&["redis-cli -a R3disPw PING"]),
                j(&["redis-cli -a *** PING"]),
            ),
            (
                j(&["redis-cli -h prod --pass RdPw33 PING"]),
                j(&["redis-cli -h prod --pass *** PING"]),
            ),
            (
                j(&["REDISCLI_AUTH=RcAuth5 redis-cli PING"]),
                j(&["REDISCLI_AUTH=*** redis-cli PING"]),
            ),
            (
                j(&["redis-cli -u redis://:UrlRedisPw@prod:6379 PING"]),
                j(&["redis-cli -u redis://:***@prod:6379 PING"]),
            ),
            (
                j(&["mongosh -u admin -p MgPw11 --eval 'db.x.find()'"]),
                j(&["mongosh -u admin -p *** --eval 'db.x.find()'"]),
            ),
            (
                j(&["mongosh --username admin --password MgPw22 --eval x"]),
                j(&["mongosh --username admin --password *** --eval x"]),
            ),
            (
                j(&[
                    "clickhouse-client --password ChPw10 -q 'DROP DATA",
                    "BASE prod'",
                ]),
                j(&[
                    "clickhouse-client --password *** -q 'DROP DATA",
                    "BASE prod'",
                ]),
            ),
            (
                j(&["clickhouse-client --password=ChPw11 -q x"]),
                j(&["clickhouse-client --password=*** -q x"]),
            ),
            (
                j(&["cqlsh -u cassandra -p CqPw12 -e 'DROP KEY", "SPACE prod'"]),
                j(&["cqlsh -u cassandra -p *** -e 'DROP KEY", "SPACE prod'"]),
            ),
            (
                j(&["cqlsh --password CqPw13 db1"]),
                j(&["cqlsh --password *** db1"]),
            ),
            (
                j(&["mongosh admin -u root -pMgPw15 --eval x"]),
                j(&["mongosh admin -u root -p*** --eval x"]),
            ),
            (j(&["mongo -pMgPw16 db"]), j(&["mongo -p*** db"])),
        ];
        for (input, want) in &cases {
            let (got, changed) = redact_secrets(input);
            assert!(changed, "{input}");
            assert_eq!(&got, want, "{input}");
        }
        for safe in [
            "git push -f origin main",
            "mkdir -p build && ls build",
            "ssh -p22 host",
            "docker run -p8080:80 img",
            "mysql -p db",
            "ls ./node_modules",
            "mongosh -u admin -p --eval x",
            "redis-cli -h prod PING",
            "clickhouse-client --password -q x",
            "cqlsh -p --help",
            "mongosh --port 27017 db",
            "mysql --password mydb",
        ] {
            let (got, changed) = redact_secrets(safe);
            assert!(!changed, "{safe} -> {got}");
            assert_eq!(got, safe);
        }
    }

    #[test]
    fn registry_logins_api_key_headers_jwts_and_stripe_keys_are_redacted() {
        let jwt =
            "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoic2VydmljZV9yb2xlIn0.SIGSIGSIGSIG";
        // Built from parts so the source holds no key-shaped literal.
        let sk = ["sk", "live", "51HstripeKEYabcdefghijklmnop"].join("_");
        let rk = ["rk", "live", "51HabcdefghijKLMN"].join("_");
        let cases = [
            (
                "docker login -p DockerPw321 registry.example.com && rm -rf ~/projects",
                "docker login -p *** registry.example.com && rm -rf ~/projects",
            ),
            (
                "docker login -u bot --password DockerPw321 ghcr.io",
                "docker login -u bot --password *** ghcr.io",
            ),
            (
                "podman login --password=PodPw1 quay.io",
                "podman login --password=*** quay.io",
            ),
            (
                "helm registry login reg.io -u u -p HelmPw1",
                "helm registry login reg.io -u u -p ***",
            ),
            (
                &format!("curl -X DELETE https://api.x/v1/p -H 'apikey: {jwt}'"),
                "curl -X DELETE https://api.x/v1/p -H 'apikey: ***'",
            ),
            (
                &format!("curl -H \"api-key: abc123\" -d '{{\"t\":\"{jwt}\"}}' https://x"),
                "curl -H \"api-key: ***\" -d '{\"t\":\"***\"}' https://x",
            ),
            (
                &format!("echo {sk} | vercel env add STRIPE production"),
                "echo *** | vercel env add STRIPE production",
            ),
            (
                &format!("stripe charges list --api-key {rk}"),
                "stripe charges list --api-key ***",
            ),
            (&format!("echo {rk}"), "echo ***"),
            (
                "git clone https://x/r.git && echo glpat-abcdefghijklmnopqrstu",
                "git clone https://x/r.git && echo ***",
            ),
        ];
        for (input, want) in &cases {
            let (got, changed) = redact_secrets(input);
            assert!(changed, "{input}");
            assert_eq!(&got, want, "{input}");
        }
        for safe in [
            "docker run -p 8080:80 img",
            "docker login ghcr.io",
            "echo $T | docker login --password-stdin -u bot ghcr.io",
            "docker login registry && docker run -p 8080:80 img",
            "grep -r 'apikey' src/",
            "echo eyJ is how a JWT starts",
            "ls sk_live_docs/ && cat task_list_test",
            "kubectl -n monkey: get pods",
        ] {
            let (got, changed) = redact_secrets(safe);
            assert!(!changed, "{safe} -> {got}");
        }
    }

    #[test]
    fn aws_configure_npm_config_and_npm_tokens_are_redacted() {
        let npm_token = "npm_abcdefghijklmnopqrstuvwxyz0123456789";
        let cases = [
            (
                "aws configure set aws_secret_access_key wJalrXUtnFEMIK7MDENGbPxRfiCYKEY && aws s3 rb s3://p --force".to_string(),
                "aws configure set aws_secret_access_key *** && aws s3 rb s3://p --force".to_string(),
            ),
            (
                "aws configure set aws_session_token FwoGZXIvYXdzEXAMPLE --profile prod".to_string(),
                "aws configure set aws_session_token *** --profile prod".to_string(),
            ),
            (
                "aws --profile p configure set default.aws_secret_access_key 'wJalr XUtn'".to_string(),
                "aws --profile p configure set default.aws_secret_access_key ***".to_string(),
            ),
            (
                "npm config set //registry.npmjs.org/:_authToken npm_ABCDEFsecretNPMtoken1234 && rm -rf ~/projects".to_string(),
                "npm config set //registry.npmjs.org/:_authToken *** && rm -rf ~/projects".to_string(),
            ),
            (
                "pnpm config set //npm.pkg.github.com/:_password c2VjcmV0".to_string(),
                "pnpm config set //npm.pkg.github.com/:_password ***".to_string(),
            ),
            (
                "npm config set //r.example.com/:_auth dXNlcjpwYXNz".to_string(),
                "npm config set //r.example.com/:_auth ***".to_string(),
            ),
            (
                "yarn config set npmAuthToken abc123secret".to_string(),
                "yarn config set npmAuthToken ***".to_string(),
            ),
            (
                "npm config set //registry.npmjs.org/:_authToken=abc123secret".to_string(),
                "npm config set //registry.npmjs.org/:_authToken=***".to_string(),
            ),
            (format!("echo {npm_token} | gh secret set NPM"), "echo *** | gh secret set NPM".to_string()),
        ];
        for (input, want) in &cases {
            let (got, changed) = redact_secrets(input);
            assert!(changed, "{input}");
            assert_eq!(&got, want, "{input}");
        }
        for safe in [
            "aws configure set region us-east-1",
            "aws configure get aws_secret_access_key",
            "aws configure list",
            "npm config set registry https://registry.npmjs.org/",
            "npm config get //registry.npmjs.org/:_authToken",
            "npm config delete //registry.npmjs.org/:_authToken",
            "echo $npm_config_cache && echo $npm_package_name",
            "npm_lifecycle_event=test npm run build",
            "npm install && npm test",
        ] {
            let (got, changed) = redact_secrets(safe);
            assert!(!changed, "{safe} -> {got}");
        }
    }

    #[test]
    fn redacted_record_keeps_the_original_digest() {
        let temp = tree_fs::TreeBuilder::default().create().unwrap();
        let path = temp.root.join("audit.log");
        let mut ev = event(
            "r",
            AuditOutcome::Denied,
            Severity::High,
            "2026-02-15T10:00:00Z",
        );
        ev.command = j(&["mysql -uroot -phunter2 -e 'select 1'"]);
        log_event(&path, &ev).unwrap();
        let line = read_log(&path).unwrap();
        assert!(!line.contains("hunter2"), "{line}");
        let entry: AuditEntry = serde_json::from_str(line.trim()).unwrap();
        assert_eq!(entry.command, "mysql -uroot -p*** -e 'select 1'");
        assert!(entry.command_redacted);
        assert!(!entry.command_truncated);
        assert_eq!(
            entry.command_sha256.as_deref(),
            Some(sha256_hex(ev.command.as_bytes()).as_str())
        );
        // A command without secrets is stored verbatim, with no digest.
        let temp2 = tree_fs::TreeBuilder::default().create().unwrap();
        let path2 = temp2.root.join("audit.log");
        let ev2 = event(
            "s",
            AuditOutcome::Denied,
            Severity::High,
            "2026-02-15T10:00:00Z",
        );
        log_event(&path2, &ev2).unwrap();
        let entry2: AuditEntry = serde_json::from_str(read_log(&path2).unwrap().trim()).unwrap();
        assert!(!entry2.command_redacted);
        assert!(entry2.command_sha256.is_none());
    }
}