zshrs 0.13.34

The first JIT-compiled Unix shell — bytecode VM, Cranelift JIT, worker pool, AOP intercept, Rkyv caching
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
//! Plugin-Framework-Agnostic State-Modification Recorder (PFA-SMR).
//!
//! Single-shot indexer. Spawns the user's shell init (or any script the
//! recorder bin was invoked with), captures every state mutation as it
//! flows through a state-mutating dispatcher, prints `Captured ...` to
//! stderr in real time, mirrors every line into the zshrs tracing log,
//! bundles the full set on shell exit, folds it into the canonical rkyv
//! shard (`~/.zshrs/images/{hash8}-recorder.rkyv`), prints summary stats,
//! then exits. No daemon is involved: the shell reads that shard back at
//! startup with `canonical_apply::apply_all`.
//!
//! Per docs/RECORDER.md: only the recorder can capture state at 100%
//! fidelity; the daemon never walks user config. New plugin installs
//! require the user to re-run `zshrs-recorder`.
//!
//! Module gated by `#![cfg(feature = "recorder")]` so the default
//! `zshrs` binary contains zero recorder code.

#![cfg(feature = "recorder")]

use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Mutex;
use std::time::{Instant, SystemTime, UNIX_EPOCH};

use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};

/// Global on/off switch. `bins/zshrs-recorder.rs` calls `enable()` at
/// startup before the executor runs; `bins/zshrs.rs` never touches it
/// (this module doesn't exist in that build).
static ENABLED: AtomicBool = AtomicBool::new(false);

/// Skip the end-of-run shard write. Set by `--dry-run` (alias
/// `--no-daemon`); hermetic tests (`tests/recorder_harness.rs`) use it so
/// a corpus run never replaces the user's recording.
static NO_WRITE: AtomicBool = AtomicBool::new(false);

/// Pid that installed the atexit hook. A forked subshell or `$(...)`
/// inherits the hook and the buffer; only this pid may finalize.
static OWNER_PID: AtomicU64 = AtomicU64::new(0);

/// Suppress the per-event "Captured KIND NAME ..." stderr line. Set
/// by `zshrs-recorder --quiet`. The summary footer + tracing log still
/// fire — only the live-capture firehose is muted.
static QUIET: AtomicBool = AtomicBool::new(false);

/// Emit the end-of-run summary as a single JSON line to stdout
/// instead of the multi-line human text on stderr. Set by
/// `zshrs-recorder --json`.
static JSON_SUMMARY: AtomicBool = AtomicBool::new(false);

/// Optional path to write the bundle to as a JSON file (alongside
/// the shard write, or instead of it under --dry-run). Set by
/// `zshrs-recorder -o PATH`. Lock contention is irrelevant — we set
/// it once at startup and read it once at flush time.
static OUTPUT_PATH: Lazy<Mutex<Option<String>>> = Lazy::new(|| Mutex::new(None));

/// Optional override for the bundle's `shell_id`. Lets a test (or a
/// rebrand experiment) impersonate a different shell. None = default
/// "zshrs".
static SHELL_ID_OVERRIDE: Lazy<Mutex<Option<String>>> = Lazy::new(|| Mutex::new(None));

/// Re-entrancy guard — set during emit so any builtin a recorder hook
/// itself triggers is not re-recorded.
static IN_RECORDER: AtomicBool = AtomicBool::new(false);

/// Monotonic per-record sequence number.
static ORDER_IDX: AtomicU64 = AtomicU64::new(0);

/// Recorder start time, used by the summary footer for `runs.started_at_ns`.
static START_NS: AtomicU64 = AtomicU64::new(0);

/// In-process buffer of every captured event. Folded into the shard
/// once at end-of-run.
static BUFFER: Lazy<Mutex<Vec<RecordEvent>>> = Lazy::new(|| Mutex::new(Vec::with_capacity(4096)));

/// Mirror of the SQLite `definitions.kind` discriminant.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DefKind {
    /// `Alias` variant.
    Alias,
    /// `GAlias` variant.
    GAlias,
    /// `SAlias` variant.
    SAlias,
    /// `Function` variant.
    Function,
    /// `Assign` variant.
    Assign,
    /// `Typeset` variant.
    Typeset,
    /// `Export` variant.
    Export,
    /// `PathMod` variant.
    PathMod,
    /// `HashD` variant.
    HashD,
    /// `Zstyle` variant.
    Zstyle,
    /// `Bindkey` variant.
    Bindkey,
    /// `Compdef` variant.
    Compdef,
    /// `Zmodload` variant.
    Zmodload,
    /// `Setopt` variant.
    Setopt,
    /// `Unsetopt` variant.
    Unsetopt,
    /// `Trap` variant.
    Trap,
    /// `Sched` variant.
    Sched,
    /// `Source` variant.
    Source,
    /// Removal events — RECORDER.md "Open question 4: Should we record
    /// `unalias` / `unset` / `disable` events?". Recorded so `zwhere -l`
    /// lineage can see "this name was defined at A:N, removed at B:M,
    /// redefined at C:K". Without these the override chain is invisible.
    Unalias,
    /// `Unset` variant.
    Unset,
    /// `zle -N WIDGET [FUNC]` — define a new ZLE widget. Distinct from
    /// `bindkey` (which binds a key sequence to a widget) and from
    /// `function` (which defines the underlying handler). Tracked
    /// because zinit-report lists widgets and a `zwhere` query for
    /// widgets is the natural counterpart.
    Zle,
    /// `_completion-name` file discovered in an fpath directory. zinit-
    /// report's "Completions:" section lists these per plugin; the
    /// recorder synthesises one event per `_*` file found whenever an
    /// fpath dir is added (set or appended). Distinct from `compdef`
    /// (which BINDS a completion function to a command — runtime call)
    /// and from `function` (the autoload registration that compinit
    /// will emit when it walks fpath). value field is the absolute
    /// path of the discovered file.
    Completion,
}

impl DefKind {
    /// `as_str` — see implementation.
    pub fn as_str(self) -> &'static str {
        match self {
            DefKind::Alias => "alias",
            DefKind::GAlias => "alias -g",
            DefKind::SAlias => "alias -s",
            DefKind::Function => "function",
            DefKind::Assign => "assign",
            DefKind::Typeset => "typeset",
            DefKind::Export => "export",
            DefKind::PathMod => "path_mod",
            DefKind::HashD => "hash -d",
            DefKind::Zstyle => "zstyle",
            DefKind::Bindkey => "bindkey",
            DefKind::Compdef => "compdef",
            DefKind::Zmodload => "zmodload",
            DefKind::Setopt => "setopt",
            DefKind::Unsetopt => "unsetopt",
            DefKind::Trap => "trap",
            DefKind::Sched => "sched",
            DefKind::Source => "source",
            DefKind::Unalias => "unalias",
            DefKind::Unset => "unset",
            DefKind::Zle => "zle",
            DefKind::Completion => "completion",
        }
    }
}

/// Structured parameter-attribute bitflags. Mirrors zsh's per-param
/// flag set (params.c PM_*) so the recorder records the full attribute
/// vector rather than encoding flags in the value string. Only the
/// `typeset` family populates this; other kinds set it to 0.
///
/// Wire-serialised as `u16` for compactness (zsh has ~12 user-visible
/// attribute flags; one byte would also fit but u16 leaves room for
/// PM_HASHELEM / PM_NAMEDDIR / PM_AUTOLOAD-style additions later
/// without a wire-format bump).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ParamAttrs(pub u16);

impl ParamAttrs {
    /// `NONE` constant.
    pub const NONE: Self = Self(0);
    /// `SCALAR` constant.
    pub const SCALAR: u16 = 1 << 0;
    /// `INTEGER` constant.
    pub const INTEGER: u16 = 1 << 1;
    /// `FLOAT` constant.
    pub const FLOAT: u16 = 1 << 2;
    /// `ASSOC` constant.
    pub const ASSOC: u16 = 1 << 3;
    /// `ARRAY` constant.
    pub const ARRAY: u16 = 1 << 4;
    /// `READONLY` constant.
    pub const READONLY: u16 = 1 << 5;
    /// `EXPORT` constant.
    pub const EXPORT: u16 = 1 << 6;
    /// `GLOBAL` constant.
    pub const GLOBAL: u16 = 1 << 7;
    /// `UNIQUE` constant.
    pub const UNIQUE: u16 = 1 << 8;
    /// `TIED` constant.
    pub const TIED: u16 = 1 << 9;
    /// `HIDE` constant.
    pub const HIDE: u16 = 1 << 10;
    /// `HIDE_VAL` constant.
    pub const HIDE_VAL: u16 = 1 << 11;
    /// `+=` operation marker. Distinguishes `arr=(a b)` (replace) from
    /// `arr+=(c)` (extend). Replay needs this to drive the right
    /// codepath when reconstructing array state from the bundle.
    pub const APPEND: u16 = 1 << 12;
    /// `set` — see implementation.
    pub fn set(&mut self, mask: u16) {
        self.0 |= mask;
    }
    /// `has` — see implementation.
    pub fn has(self, mask: u16) -> bool {
        self.0 & mask != 0
    }

    /// Parse a zsh `typeset -...` flag-letter sequence ("xrigU" etc.)
    /// into a ParamAttrs bitset. Includes letters from `typeset`,
    /// `integer`, `float`, `readonly`, `local`, `declare`, `export`.
    /// Letters not in the table are silently ignored.
    pub fn from_flag_chars(letters: &str) -> Self {
        let mut a = Self::NONE;
        for c in letters.chars() {
            match c {
                'i' => a.set(Self::INTEGER),
                'F' | 'E' => a.set(Self::FLOAT),
                'A' => a.set(Self::ASSOC),
                'a' => a.set(Self::ARRAY),
                'r' => a.set(Self::READONLY),
                'x' => a.set(Self::EXPORT),
                'g' => a.set(Self::GLOBAL),
                'U' => a.set(Self::UNIQUE),
                'T' => a.set(Self::TIED),
                'h' => a.set(Self::HIDE),
                'H' => a.set(Self::HIDE_VAL),
                _ => {}
            }
        }
        // If no concrete shape was set, mark as scalar (typeset
        // defaults to scalar string semantics).
        if a.0 & (Self::INTEGER | Self::FLOAT | Self::ASSOC | Self::ARRAY) == 0 {
            a.set(Self::SCALAR);
        }
        a
    }
}

/// One state-mutation event. Field set is the recorder's wire format
/// for the daemon `recorder_ingest` op; mirrors the SQL `definitions`
/// row in docs/RECORDER.md §Schema.
///
/// REPLAY-GRADE TYPE INFO. Every assign event carries enough structure
/// to round-trip the parameter exactly:
///
///   - `attrs`: `ParamAttrs` bitset — scalar/integer/float/assoc/array/
///     readonly/export/global/unique/tied. Populated on every emit_*
///     by inspecting the executor's current type for `name` (or the
///     declared type for typeset-family). Without this, replay can't
///     tell whether `EDITOR=vim` should restore as scalar or as a
///     previously-tied array.
///   - `value`: scalar form. Set for scalar / integer / float and as
///     a fallback for arrays/assocs (joined string).
///   - `value_array`: ORDERED element list for indexed-array events.
///     Empty for scalars/assocs. Lets replay reconstruct
///     `arr=(elem1 elem2 elem3)` exactly — the order is preserved
///     even when zsh's `typeset -U` would dedupe.
///   - `value_assoc`: ORDERED (key, value) pairs for assoc events.
///     Empty for scalars/arrays. Insertion order matters for replay
///     determinism (zsh assocs are insertion-ordered).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecordEvent {
    /// `order_idx` field.
    pub order_idx: u64,
    /// `ts_ns` field.
    pub ts_ns: u64,
    /// `kind` field.
    pub kind: DefKind,
    /// `name` field.
    pub name: String,
    /// `value` field.
    pub value: Option<String>,
    /// `file` field.
    pub file: Option<String>,
    /// `line` field.
    pub line: Option<u32>,
    /// `fn_chain` field.
    pub fn_chain: Option<String>,
    /// `attrs` field.
    #[serde(default, skip_serializing_if = "is_default_attrs")]
    pub attrs: ParamAttrs,
    /// `value_array` field.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub value_array: Option<Vec<String>>,
    /// `value_assoc` field.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub value_assoc: Option<Vec<(String, String)>>,
    /// Recording-shell identity for the federated catalog (per
    /// docs/DAEMON_AS_SERVICE.md §"Third-party shell recorders" +
    /// `docs/SHELL_IDS.md`). Distinguishes records from different
    /// shells writing to the same daemon. None = inherit from the
    /// enclosing `RecorderBundle.shell_id` (defaults to "zshrs"
    /// at ingest time when neither is set).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub shell_id: Option<String>,
}

fn is_default_attrs(a: &ParamAttrs) -> bool {
    a.0 == 0
}

/// Bundle sent to the daemon at end-of-run.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecorderBundle {
    /// `started_at_ns` field.
    pub started_at_ns: u64,
    /// `finished_at_ns` field.
    pub finished_at_ns: u64,
    /// `cmdline` field.
    pub cmdline: String,
    /// `zdotdir` field.
    pub zdotdir: Option<String>,
    /// `home` field.
    pub home: Option<String>,
    /// `events` field.
    pub events: Vec<RecordEvent>,
    /// Federated-catalog shell identity. Falls back to "zshrs" if not
    /// supplied. Per-event `shell_id` overrides this top-level value
    /// for individual records (lets one bundle carry mixed sources).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub shell_id: Option<String>,
    /// End-of-run snapshot from [`capture_end_state`]; the shard fold
    /// prefers it over the events for aliases, functions, parameters and
    /// options.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub end_state: Option<crate::daemon::recorder_shard::EndState>,
}
/// `enable` — see implementation.
#[inline]
pub fn enable() {
    ENABLED.store(true, Ordering::Relaxed);
    START_NS.store(now_ns(), Ordering::Relaxed);
    tracing::info!("recorder: enabled");
}
/// `is_enabled` — see implementation.
#[inline]
pub fn is_enabled() -> bool {
    ENABLED.load(Ordering::Relaxed)
}

/// Distinct `(kind, name)` definitions captured so far, leaving out the
/// parameter and option churn (`assign`, `typeset`, `unset`, `setopt`,
/// `unsetopt`) that any function body produces. Distinct, so a deferred
/// loader that re-arms the same `sched` entry or trap on every pass of
/// its own scheduler adds nothing. The recorder's deferred-work drain
/// stops once passes stop adding these.
pub fn definition_count() -> usize {
    BUFFER
        .lock()
        .map(|b| {
            b.iter()
                .filter(|e| {
                    !matches!(
                        e.kind,
                        DefKind::Assign
                            | DefKind::Typeset
                            | DefKind::Unset
                            | DefKind::Setopt
                            | DefKind::Unsetopt
                    )
                })
                .map(|e| (e.kind, e.name.as_str()))
                .collect::<std::collections::HashSet<_>>()
                .len()
        })
        .unwrap_or(0)
}

/// Run every `sched` entry pending NOW, whatever its due time, through
/// the ported `checksched` walk (Src/Builtins/sched.c:93). Entries the
/// run schedules itself (`sched +1 …`) carry a future time and wait for
/// the next call. Returns how many entries were pending.
///
/// The recorder never reaches a prompt, so a `sched` deadline never
/// comes due on its own — and zinit turbo (`wait''`) hangs every
/// deferred plugin off `sched`.
pub fn run_pending_sched() -> usize {
    use crate::ported::builtins::sched::schedcmd;
    let mut list = schedcmd::subsh_save();
    let mut pending = 0;
    let mut node = list.as_deref_mut();
    while let Some(sch) = node {
        sch.time = 0;
        pending += 1;
        node = sch.next.as_deref_mut();
    }
    if pending > 0 {
        schedcmd::subsh_restore(list);
        crate::ported::builtins::sched::checksched();
    }
    pending
}

/// Shell state before the first recorded file ran; [`capture_end_state`]
/// keeps only what the files changed.
struct Baseline {
    options: std::collections::HashMap<&'static str, bool>,
    params: std::collections::HashMap<String, crate::daemon::recorder_shard::TypedParam>,
    keymaps: KeymapState,
    widgets: std::collections::HashMap<String, Vec<String>>,
}

/// Set by [`mark_baseline`], consumed by [`capture_end_state`].
static BASELINE: Lazy<Mutex<Option<Baseline>>> = Lazy::new(|| Mutex::new(None));

/// Path of the terminal `zshrs-recorder` gave the recording
/// (`attach_recording_tty`); [`capture_end_state`] marks it in parameter
/// values with `TTY_PLACEHOLDER`.
static RECORDING_TTY: Lazy<Mutex<Option<String>>> = Lazy::new(|| Mutex::new(None));

/// Mark the values that belong to the recording process in every parameter
/// value — the recording terminal's path (`TTY_PLACEHOLDER`) and its `$$`
/// (`PID_PLACEHOLDER`) — so the replay can put its own there.
fn mark_process_values(params: &mut [crate::daemon::recorder_shard::TypedParam]) {
    use crate::daemon::recorder_shard::{PID_PLACEHOLDER, TTY_PLACEHOLDER};
    let tty = RECORDING_TTY.lock().ok().and_then(|t| t.clone());
    let pid = crate::ported::params::mypid.load(std::sync::atomic::Ordering::Relaxed);
    let pid = (pid > 0).then(|| pid.to_string());
    for p in params {
        for v in p.value.iter_mut().chain(p.elements.iter_mut().flatten()) {
            if let Some(tty) = &tty {
                if v.contains(tty.as_str()) {
                    *v = v.replace(tty.as_str(), TTY_PLACEHOLDER);
                }
            }
            if let Some(pid) = &pid {
                *v = replace_number(v, pid, PID_PLACEHOLDER);
            }
        }
    }
}

/// The files the recorder sources itself (the login chain, or `--file`).
static STARTUP_FILES: Lazy<Mutex<Vec<String>>> = Lazy::new(|| Mutex::new(Vec::new()));

/// Record the files the recorder sources itself; see [`STARTUP_FILES`].
pub fn set_startup_files(files: Vec<String>) {
    if let Ok(mut s) = STARTUP_FILES.lock() {
        *s = files;
    }
}

/// Names the recorded files had a descriptor assigned to (`exec {name}>…`,
/// `zsystem flock -f name`, `sysopen -u name`), each with the order of the
/// assignment and the file running it (C's `scriptfilename`).
static DESCRIPTOR_PARAMS: Lazy<Mutex<std::collections::HashMap<String, (u64, Option<String>)>>> =
    Lazy::new(|| Mutex::new(std::collections::HashMap::new()));

/// Called where the shell stores a descriptor in a named parameter
/// (Src/exec.c:2404-2412 `addfd`'s varid arm; Src/Modules/system.c:414,
/// 765). Only these names can be descriptor-holding parameters — matching
/// values against open descriptors alone also caught every small integer
/// setting (`POWERLEVEL9K_DIR_MAX_LENGTH=40` and an open fd 40).
pub fn note_descriptor_param(name: &str) {
    if is_enabled() {
        let file = crate::ported::utils::scriptfilename_get();
        let order = ORDER_IDX.load(Ordering::Relaxed);
        if let Ok(mut s) = DESCRIPTOR_PARAMS.lock() {
            s.insert(name.to_string(), (order, file));
        }
    }
}

/// Pull out of `end.params` every parameter that holds a descriptor the
/// recorded files opened and still hold — named in [`DESCRIPTOR_PARAMS`]
/// and still open as `FDT_EXTERNAL` / `FDT_FLOCK` — and list the file that
/// last assigned each one in `end.resource_files`.
///
/// The number means nothing in another process: replayed, it named
/// whatever that shell had open at the same number, and zconvey wrote
/// `$$` into it (`echo "$$" >&${ZCONVEY_FD}`) and reused the session ID
/// it guarded. The descriptor, its lock and the ID it stands for have to
/// be taken by each shell, so the replay sources those files again.
fn take_descriptor_params(end: &mut crate::daemon::recorder_shard::EndState) {
    use crate::ported::zsh_h::{FDT_EXTERNAL, FDT_FLOCK, FDT_FLOCK_EXEC};
    let held = |fd: i32| {
        matches!(crate::ported::utils::fdtable_get(fd), k if k == FDT_EXTERNAL || k == FDT_FLOCK || k == FDT_FLOCK_EXEC)
    };
    let noted = DESCRIPTOR_PARAMS.lock().map(|s| s.clone()).unwrap_or_default();
    let (fds, kept): (Vec<_>, Vec<_>) = std::mem::take(&mut end.params).into_iter().partition(|p| {
        noted.contains_key(&p.name)
            && p.value.as_deref().and_then(|v| v.parse::<i32>().ok()).is_some_and(|fd| fd >= 10 && held(fd))
    });
    end.params = kept;
    if fds.is_empty() {
        return;
    }
    // The file that took each descriptor, in the order they were taken.
    let mut files: Vec<(u64, String)> = fds
        .iter()
        .filter_map(|p| {
            let (order, file) = noted.get(&p.name)?;
            Some((*order, file.clone()?))
        })
        .collect();
    files.sort();
    // Never a startup file: sourcing `.zshrc` again would be the whole
    // startup the replay replaces. A descriptor taken there is dropped.
    let startup = STARTUP_FILES.lock().map(|s| s.clone()).unwrap_or_default();
    for (_, file) in files {
        if startup.contains(&file) {
            continue;
        }
        if !end.resource_files.contains(&file) {
            end.resource_files.push(file);
        }
    }
    tracing::info!(
        params = ?fds.iter().map(|p| p.name.as_str()).collect::<Vec<_>>(),
        files = ?end.resource_files,
        "recorder: descriptor-holding parameters; their files are sourced again at replay"
    );
}

/// Replace every occurrence of the decimal `number` in `s` that is not
/// part of a longer run of digits — `.temp4242-x` matches 4242,
/// `142429` does not.
fn replace_number(s: &str, number: &str, with: &str) -> String {
    let bytes = s.as_bytes();
    let mut out = String::with_capacity(s.len());
    let mut last = 0;
    let mut from = 0;
    while let Some(off) = s[from..].find(number) {
        let at = from + off;
        let end = at + number.len();
        let digit_before = at > 0 && bytes[at - 1].is_ascii_digit();
        let digit_after = end < bytes.len() && bytes[end].is_ascii_digit();
        if !digit_before && !digit_after {
            out.push_str(&s[last..at]);
            out.push_str(with);
            last = end;
        }
        from = at + 1;
    }
    out.push_str(&s[last..]);
    out
}

/// Record the recording terminal's path; see [`RECORDING_TTY`].
pub fn set_recording_tty(path: Option<String>) {
    if let Ok(mut t) = RECORDING_TTY.lock() {
        *t = path;
    }
}

/// Snapshot taken by [`capture_end_state`], attached to the bundle by
/// [`flush`].
static END_STATE: Lazy<Mutex<Option<crate::daemon::recorder_shard::EndState>>> =
    Lazy::new(|| Mutex::new(None));

/// Options that describe how THIS process was started — the recorder is
/// a non-interactive script runner, the replaying shell usually an
/// interactive login one — never something a config file chose.
const UNRECORDED_OPTIONS: &[&str] = &[
    "interactive",
    "loginshell",
    "shinstdin",
    "singlecommand",
    "privileged",
    "monitor",
    "zle",
    "rcs",
    "globalrcs",
];

/// Parameters whose value belongs to the running process or to the
/// command that last ran, not to the config: replaying the recorder's
/// copy would be wrong in every shell.
const UNRECORDED_PARAMS: &[&str] = &[
    "_",
    "0",
    "argv",
    "PWD",
    "OLDPWD",
    "SHLVL",
    "SECONDS",
    "RANDOM",
    "LINENO",
    "TTY",
    "TTYIDLE",
    "pipestatus",
    "ZSH_SCRIPT",
    "ZSH_ARGZERO",
    "ZSH_EXECUTION_STRING",
    "ZSH_SUBSHELL",
    "match",
    "mbegin",
    "mend",
    "MATCH",
    "MBEGIN",
    "MEND",
    "reply",
    "REPLY",
];

/// Every canonical option and its state. `OPTNS` also lists the
/// `OPT_ALIAS` rows (`login`, `dotglob`, …); an alias is skipped because
/// its canonical row carries the same state.
fn option_states() -> std::collections::HashMap<&'static str, bool> {
    use crate::ported::options::{optlookup, opt_state_get, OPTNS};
    OPTNS
        .iter()
        .copied()
        .filter(|&n| {
            let no = optlookup(n);
            no > 0 && crate::ported::zsh_h::opt_name(no) == n
        })
        .filter_map(|n| opt_state_get(n).map(|on| (n, on)))
        .collect()
}

/// One global parameter as the replay needs it, or `None` for one that
/// cannot or must not be replayed: read-only, the special hashes
/// (`$aliases`, `$functions`, `$options`, … — views of tables the
/// snapshot carries itself), or a name in [`UNRECORDED_PARAMS`].
fn read_global_param(name: &str) -> Option<crate::daemon::recorder_shard::TypedParam> {
    use crate::ported::params::{getaparam, gethparam, getsparam, paramtab};
    if UNRECORDED_PARAMS.contains(&name) {
        return None;
    }
    // `${(t)name}` (Src/Modules/parameter.c:43 `paramtypestr`), plus the
    // tie partner and separator a `typeset -T` scalar carries (`ename`,
    // `u.tied->joinchar`, Src/zsh.h:1864, 1870-1873).
    let (ty, tie) = paramtab().read().ok().and_then(|t| {
        t.get(name).map(|pm| {
            let tie = pm.ename.clone().map(|arr| {
                let sep = pm.u_tied.as_ref().map(|t| t.joinchar).unwrap_or(b':' as i32);
                (arr, char::from_u32(sep as u32).unwrap_or(':').to_string())
            });
            (crate::ported::modules::parameter::paramtypestr(pm), tie)
        })
    })?;
    let parts: Vec<&str> = ty.split('-').collect();
    let kind = parts[0];
    let special = parts.contains(&"special");
    if parts.contains(&"readonly") || (kind == "association" && special) {
        return None;
    }
    let (value, elements) = match kind {
        "array" => (None, getaparam(name)),
        // `gethparam` is the VALUES only (Src/params.c:3118
        // `SCANPM_WANTVALS`); `gethkparam` the keys, in the same scan
        // order (c:3138).
        "association" => {
            let keys = crate::ported::params::gethkparam(name).unwrap_or_default();
            let vals = gethparam(name).unwrap_or_default();
            let flat = keys.into_iter().zip(vals).flat_map(|(k, v)| [k, v]).collect();
            (None, Some(flat))
        }
        "scalar" | "integer" | "float" => (getsparam(name), None),
        _ => return None,
    };
    // The `paramtypestr` words (c:72-90) that `typeset` sets with a
    // plain letter.
    let attrs: String = [("unique", 'U'), ("hideval", 'H'), ("hide", 'h'), ("lower", 'l'), ("upper", 'u'), ("tag", 't')]
        .iter()
        .filter(|(word, _)| parts.contains(word))
        .map(|(_, c)| *c)
        .collect();
    Some(crate::daemon::recorder_shard::TypedParam {
        name: name.to_string(),
        kind: kind.to_string(),
        export: parts.contains(&"export"),
        value,
        elements,
        attrs,
        // A special pair (`PATH`/`path`) is tied in every shell already.
        tie: tie.filter(|_| kind == "scalar" && parts.contains(&"tied") && !special),
    })
}

/// Every global parameter the replay could restore, keyed by name.
fn global_params() -> std::collections::HashMap<String, crate::daemon::recorder_shard::TypedParam> {
    // Names first: the readers below take `paramtab` themselves.
    let names: Vec<String> = crate::ported::params::paramtab()
        .read()
        .map(|t| t.iter().map(|(n, _)| n.clone()).collect())
        .unwrap_or_default();
    names
        .into_iter()
        .filter_map(|n| read_global_param(&n).map(|p| (n, p)))
        .collect()
}

/// Every loaded module and its enabled features — what `zmodload -LF`
/// prints (`bin_zmodload_features`, Src/module.c:3022-3024 via
/// `printmodulenode` c:218-262). Replaying `zmodload -F MODULE f…` gives a
/// module exactly these features whether the files ran a bare
/// `zmodload zsh/datetime` or `zmodload -F zsh/files b:zf_rm`.
fn loaded_module_features() -> std::collections::HashMap<String, Vec<String>> {
    use crate::ported::module::{enables_module, features_module, MODULESTAB};
    use crate::ported::zsh_h::{MOD_ALIAS, MOD_INIT_B, MOD_UNLOAD};
    let mut out = std::collections::HashMap::new();
    let Ok(mut table) = MODULESTAB.lock() else {
        return out;
    };
    let table = &mut *table;
    let names: Vec<String> = table
        .modules
        .iter()
        .filter(|(_, m)| {
            let f = m.node.flags;
            f & MOD_ALIAS == 0 && f & MOD_INIT_B != 0 && f & MOD_UNLOAD == 0
        })
        .map(|(n, _)| n.clone())
        .collect();
    for name in names {
        let mut features = Vec::new();
        if features_module(table, &name, &mut features) != 0 {
            continue;
        }
        // A module with no features (`zsh/complist`) is loaded or not;
        // an empty list replays as a plain `zmodload MODULE`.
        if features.is_empty() {
            out.insert(name, Vec::new());
            continue;
        }
        let mut enables = None;
        if enables_module(table, &name, &mut enables) != 0 {
            continue;
        }
        let enables = enables.unwrap_or_default();
        let on: Vec<String> = features
            .into_iter()
            .zip(enables)
            .filter(|(_, e)| *e != 0)
            .map(|(f, _)| f)
            .collect();
        out.insert(name, on);
    }
    out
}

/// What one key sequence does in a keymap: run a widget, or send a
/// string (`bindkey -s`).
#[derive(Clone, PartialEq)]
enum KeyAction {
    Widget(String),
    Send(String),
}

/// Every keymap, as `bindkey -lL` and `bindkey -LM` would show it.
struct KeymapState {
    /// Keymap name → the name its bindings are listed under. Names that
    /// share one keymap (`main` after `bindkey -v` is `viins`) map to
    /// that keymap's primary name (Src/Zle/zle_keymap.c:78 `primary`).
    names: std::collections::HashMap<String, String>,
    /// Primary name → its bindings.
    binds: std::collections::HashMap<String, std::collections::HashMap<Vec<u8>, KeyAction>>,
}

/// Read `keymapnamtab`.
fn keymap_state() -> KeymapState {
    use crate::ported::zle::zle_keymap::keymapnamtab;
    let mut state = KeymapState {
        names: Default::default(),
        binds: Default::default(),
    };
    let Ok(tab) = keymapnamtab().lock() else {
        return state;
    };
    let named: Vec<(String, std::sync::Arc<crate::ported::zle::zle_keymap::Keymap>)> =
        tab.iter().map(|(n, kmn)| (n.clone(), kmn.keymap.clone())).collect();
    drop(tab);
    for (name, km) in &named {
        // The primary name when the keymap has one and it is linked,
        // else the alphabetically first name sharing the keymap.
        let mut sharing: Vec<&String> = named
            .iter()
            .filter(|(_, other)| std::sync::Arc::ptr_eq(km, other))
            .map(|(n, _)| n)
            .collect();
        sharing.sort();
        let primary = km
            .primary
            .as_ref()
            .filter(|p| sharing.contains(p))
            .unwrap_or(sharing[0])
            .clone();
        state.names.insert(name.clone(), primary.clone());
        if state.binds.contains_key(&primary) {
            continue;
        }
        let mut binds = std::collections::HashMap::new();
        for (byte, t) in km.first.iter().enumerate() {
            if let Some(t) = t.as_ref().filter(|t| t.nam != "undefined-key") {
                binds.insert(vec![byte as u8], KeyAction::Widget(t.nam.clone()));
            }
        }
        for (seq, kb) in &km.multi {
            // A `multi` node with neither is only a prefix of longer
            // sequences (c:85-91 `prefixct`).
            let action = match (&kb.bind, &kb.str) {
                (Some(t), _) if t.nam != "undefined-key" => KeyAction::Widget(t.nam.clone()),
                (_, Some(s)) => KeyAction::Send(s.clone()),
                _ => continue,
            };
            binds.insert(seq.clone(), action);
        }
        state.binds.insert(primary, binds);
    }
    state
}

/// A key sequence as `\NNN` octal escapes, one per byte. `bindkey`
/// decodes both its sequence and its `-s` string with
/// `GETKEYS_BINDKEY`, which includes `GETKEY_OCTAL_ESC` (Src/zsh.h:3141,
/// 3185; Src/Zle/zle_keymap.c:1022, 1038), so any byte — NUL, `^`, `\`,
/// high-bit — round-trips without quoting.
fn key_escape(seq: &[u8]) -> String {
    seq.iter().map(|b| format!("\\{b:03o}")).collect()
}

/// The `bindkey` argvs that turn keymap state `base` into `end`: new
/// keymaps (`-N`), links (`-A`), then per-keymap binds and removals
/// (`-M KEYMAP SEQ WIDGET`, `-s`, `-r`), keys as [`key_escape`] writes them.
fn keymap_diff(base: &KeymapState, end: &KeymapState) -> Vec<Vec<String>> {
    let mut created = Vec::new();
    let mut linked = Vec::new();
    let mut names: Vec<&String> = end.names.keys().collect();
    names.sort();
    for name in names {
        let primary = &end.names[name];
        if name == primary {
            if !base.names.contains_key(name) {
                created.push(vec!["-N".to_string(), name.clone()]);
            }
        } else if base.names.get(name) != Some(primary) {
            linked.push(vec!["-A".to_string(), primary.clone(), name.clone()]);
        }
    }

    let empty = std::collections::HashMap::new();
    let mut bound = Vec::new();
    let mut primaries: Vec<&String> = end.binds.keys().collect();
    primaries.sort();
    for km in primaries {
        let before = base
            .names
            .get(km)
            .and_then(|p| base.binds.get(p))
            .unwrap_or(&empty);
        let after = &end.binds[km];
        let mut seqs: Vec<&Vec<u8>> = after.keys().chain(before.keys()).collect();
        seqs.sort();
        seqs.dedup();
        for seq in seqs {
            let mut argv = vec!["-M".to_string(), km.clone()];
            match (before.get(seq), after.get(seq)) {
                (b, Some(a)) if b == Some(a) => continue,
                (_, Some(KeyAction::Widget(w))) => argv.extend([key_escape(seq), w.clone()]),
                (_, Some(KeyAction::Send(s))) => {
                    argv.extend(["-s".to_string(), key_escape(seq), key_escape(s.as_bytes())])
                }
                (Some(_), None) => argv.extend(["-r".to_string(), key_escape(seq)]),
                (None, None) => continue,
            }
            bound.push(argv);
        }
    }
    created.into_iter().chain(linked).chain(bound).collect()
}

/// User widgets in `thingytab`, name → the `zle` argv that defines it
/// (`zle -N NAME FUNC`, `zle -C NAME WIDGET FUNC`).
fn widget_state() -> std::collections::HashMap<String, Vec<String>> {
    use crate::ported::zle::zle_h::WidgetImpl;
    let Ok(tab) = crate::ported::zle::zle_thingy::thingytab().lock() else {
        return Default::default();
    };
    tab.iter()
        .filter_map(|(name, t)| {
            let argv = match &t.widget.as_ref()?.u {
                WidgetImpl::UserFunc(f) => vec!["-N".to_string(), name.clone(), f.clone()],
                WidgetImpl::Comp { wid, func, .. } => {
                    vec!["-C".to_string(), name.clone(), wid.clone(), func.clone()]
                }
                WidgetImpl::Internal(_) => return None,
            };
            Some((name.clone(), argv))
        })
        .collect()
}

/// Bring up the line editor's tables the way `zsh_main` does for an
/// interactive shell (Src/init.c, `init.rs` `zle_load_state` block), so
/// the baseline holds the keymaps and widgets the replaying shell starts
/// with. The recorder never runs `zsh_main`.
fn init_zle_tables() {
    use crate::ported::zle::zle_keymap::{createkeymapnamtab, default_bindings, keymapnamtab};
    crate::ported::zle::zle_thingy::init_thingies();
    createkeymapnamtab();
    // `default_bindings` rebuilds every keymap; only on an empty table.
    if keymapnamtab().lock().map(|t| !t.contains_key("main")).unwrap_or(false) {
        default_bindings();
    }
}

/// Record the option and parameter state before any file is sourced.
pub fn mark_baseline() {
    init_zle_tables();
    let base = Baseline {
        options: option_states(),
        params: global_params(),
        keymaps: keymap_state(),
        widgets: widget_state(),
    };
    if let Ok(mut b) = BASELINE.lock() {
        *b = Some(base);
    }
}

/// Snapshot what the init chain left behind — aliases, functions,
/// autoload stubs, and the global parameters and options that differ
/// from [`mark_baseline`] — for the shard fold to use instead of
/// replaying events. Read from the live tables, not the event log:
/// events miss `typeset -g x=(…)` and cannot say what `local`,
/// `localoptions`, `unalias` or `unfunction` left behind. Run after the
/// deferred-work drain, while the executor is still alive; `flush`
/// attaches it to the bundle.
pub fn capture_end_state() {
    use crate::daemon::recorder_shard::EndState;
    use crate::ported::hashtable::{aliastab_lock, shfunctab_lock, sufaliastab_lock};
    use crate::ported::zsh_h::{
        ALIAS_GLOBAL, DISABLED, MFF_STR, MFF_USERFUNC, PM_KSHSTORED, PM_LOADDIR, PM_TAGGED,
        PM_TAGGED_LOCAL, PM_UNALIASED, PM_UNDEFINED, PM_ZSHSTORED,
    };

    let mut end = EndState::default();
    if let Ok(tab) = aliastab_lock().read() {
        for (name, a) in tab.iter() {
            if a.node.flags & DISABLED != 0 {
                continue;
            }
            let bucket = if a.node.flags & ALIAS_GLOBAL != 0 {
                &mut end.global_aliases
            } else {
                &mut end.aliases
            };
            bucket.insert(name.clone(), a.text.clone());
        }
    }
    if let Ok(tab) = sufaliastab_lock().read() {
        for (name, a) in tab.iter() {
            if a.node.flags & DISABLED == 0 {
                end.suffix_aliases.insert(name.clone(), a.text.clone());
            }
        }
    }

    // A defined function's body is the source text the shell itself
    // compiles from (`shfunc.body`). The `$functions[name]` deparse is
    // the fallback only: zshrs's deparse loses quoting
    // (`'_.!~*'\''()-'` comes back as `'_.!~*'()-'`) and so does not
    // always parse again.
    let mut deparse: Vec<String> = Vec::new();
    if let Ok(tab) = shfunctab_lock().read() {
        for (name, shf) in tab.iter() {
            if shf.node.flags & DISABLED != 0 {
                continue;
            }
            let flags = shf.node.flags as u32;
            if flags & PM_UNDEFINED != 0 {
                match shf.filename.as_ref().filter(|_| flags & PM_LOADDIR != 0) {
                    // `autoload -Uz DIR/NAME` (Src/builtin.c:3288-3290 `add_autoload_function`):
                    // the function comes from DIR, not `$fpath`.
                    Some(dir) => {
                        let mut letters = String::from("-");
                        for (bit, c) in [
                            (PM_UNALIASED, 'U'),
                            (PM_ZSHSTORED, 'z'),
                            (PM_KSHSTORED, 'k'),
                            (PM_TAGGED, 't'),
                            (PM_TAGGED_LOCAL, 'T'),
                        ] {
                            if flags & bit != 0 {
                                letters.push(c);
                            }
                        }
                        let mut argv = Vec::new();
                        if letters.len() > 1 {
                            argv.push(letters);
                        }
                        argv.push(format!("{dir}/{name}"));
                        end.autoload_paths.push((name.clone(), argv));
                    }
                    None => end.autoloads.push(name.clone()),
                }
            } else if let Some(body) = shf.body.as_ref().filter(|b| !b.is_empty()) {
                end.functions.insert(name.clone(), body.clone());
            } else {
                deparse.push(name.clone());
            }
        }
    }
    end.autoloads.sort();
    end.autoload_paths.sort();

    // User math functions, as the `functions -M` argv `listusermathfunc`
    // prints for each (Src/builtin.c:3243-3277): the min/max/function
    // words appear only as far as they differ from the defaults.
    if let Ok(table) = crate::ported::module::MATHFUNCS.lock() {
        for p in table.iter().filter(|p| p.flags & MFF_USERFUNC != 0) {
            let mut showargs = if p.module.is_some() {
                3
            } else if p.maxargs != if p.minargs != 0 { p.minargs } else { -1 } {
                2
            } else if p.minargs != 0 {
                1
            } else {
                0
            };
            let mut argv = vec![
                if p.flags & MFF_STR != 0 { "-Ms" } else { "-M" }.to_string(),
                p.name.clone(),
            ];
            for word in [p.minargs.to_string(), p.maxargs.to_string(), p.module.clone().unwrap_or_default()] {
                if showargs == 0 {
                    break;
                }
                argv.push(word);
                showargs -= 1;
            }
            end.math_functions.push((p.name.clone(), argv));
        }
    }
    end.math_functions.sort();

    if let Ok(t) = crate::ported::modules::zutil::zstyletab.lock() {
        end.zstyles = t.entries();
    }

    // Src/Modules/parameter.c:388 `getfunction`, read after the table
    // guard drops (it takes the same lock).
    for name in deparse {
        let body = crate::ported::modules::parameter::getpmfunction(std::ptr::null_mut(), &name)
            .and_then(|pm| pm.u_str);
        if let Some(body) = body {
            end.functions.insert(name, body);
        }
    }

    if let Some(base) = BASELINE.lock().ok().and_then(|mut b| b.take()) {
        let now = global_params();
        // Gone since the baseline, or unset by the files outright — the
        // recorder starts from a scrubbed environment, so a variable a
        // session can carry (`unset CDPATH`) may never have been in its
        // baseline to go missing.
        let unset_by_files: Vec<String> = BUFFER
            .lock()
            .map(|b| {
                b.iter()
                    .filter(|e| e.kind == DefKind::Unset && !e.name.contains('['))
                    .map(|e| e.name.clone())
                    .collect()
            })
            .unwrap_or_default();
        let mut unset: Vec<String> = base
            .params
            .keys()
            .cloned()
            .chain(unset_by_files)
            .filter(|n| !now.contains_key(n) && !UNRECORDED_PARAMS.contains(&n.as_str()))
            .collect();
        unset.sort();
        unset.dedup();
        end.unset_params = unset;
        // Every global the files assigned, plus any other that changed. A
        // diff against the baseline alone dropped what the files set to the
        // value the recorder already inherited: zpwr exports
        // ZPWR_SEND_KEYS_PANE=-1, the recorder's parent shell had exported
        // the same, and a shell replayed from a clean environment had none
        // (`(( $ZPWR_SEND_KEYS_PANE != -1 ))`: operand expected).
        let assigned: std::collections::HashSet<String> = BUFFER
            .lock()
            .map(|b| {
                b.iter()
                    .filter(|e| matches!(e.kind, DefKind::Assign | DefKind::Typeset | DefKind::Export))
                    .map(|e| e.name.split('[').next().unwrap_or(&e.name).to_string())
                    .collect()
            })
            .unwrap_or_default();
        let mut params: Vec<_> = now
            .into_values()
            .filter(|p| assigned.contains(&p.name) || base.params.get(&p.name) != Some(p))
            .collect();
        params.sort_by(|a, b| a.name.cmp(&b.name));
        end.params = params;
        take_descriptor_params(&mut end);
        mark_process_values(&mut end.params);

        for (name, on) in option_states() {
            if UNRECORDED_OPTIONS.contains(&name) || base.options.get(name) == Some(&on) {
                continue;
            }
            if on {
                end.setopts.push(name.to_string());
            } else {
                end.unsetopts.push(name.to_string());
            }
        }
        end.setopts.sort();
        end.unsetopts.sort();

        // Every loaded module, not a diff: the recorder's baseline already
        // has modules the replaying shell may not (`zsh/complist`, whose
        // `.menu-select` a recorded `zle -C menu-select` needs).
        let mut modules: Vec<(String, Vec<String>)> = loaded_module_features().into_iter().collect();
        modules.sort();
        end.modules = modules;

        let mut widgets: Vec<(String, Vec<String>)> = widget_state()
            .into_iter()
            .filter(|(name, argv)| base.widgets.get(name) != Some(argv))
            .collect();
        widgets.sort();
        end.widgets = widgets.into_iter().map(|(_, argv)| argv).collect();
        end.bindkeys = keymap_diff(&base.keymaps, &keymap_state());
    }

    tracing::info!(
        aliases = end.aliases.len() + end.global_aliases.len() + end.suffix_aliases.len(),
        functions = end.functions.len(),
        autoloads = end.autoloads.len(),
        params = end.params.len(),
        unset_params = end.unset_params.len(),
        modules = end.modules.len(),
        autoload_paths = end.autoload_paths.len(),
        math_functions = end.math_functions.len(),
        zstyles = end.zstyles.len(),
        widgets = end.widgets.len(),
        bindkeys = end.bindkeys.len(),
        setopts = end.setopts.len(),
        unsetopts = end.unsetopts.len(),
        "recorder: end state captured"
    );
    if let Ok(mut s) = END_STATE.lock() {
        *s = Some(end);
    }
}

/// Free-fn equivalent of `ShellExecutor::recorder_ctx()` — the same
/// RecordCtx built from the shell's parameter table (`$LINENO`,
/// `$funcstack`) and C's `scriptfilename` global, so canonical free-fn
/// ports of C builtins (which don't take a `&ShellExecutor`) can emit
/// recorder events without the executor in scope. These are shell
/// parameters, not environment variables: reading them through
/// `std::env` left every event's file and line unset.
pub fn recorder_ctx_global() -> RecordCtx {
    let line = crate::ported::params::getsparam("LINENO").and_then(|s| s.parse::<u32>().ok());
    let file = crate::ported::utils::scriptfilename_get();
    let defined_at = crate::ported::params::getaparam("funcsourcetrace").and_then(|t| t.into_iter().next());
    let (file, line) = absolute_site(file, line, defined_at.as_deref());
    let fn_chain = crate::ported::params::getaparam("funcstack").and_then(|s| {
        if s.is_empty() {
            None
        } else {
            let mut parts: Vec<&str> = s.iter().map(String::as_str).collect();
            parts.reverse();
            Some(parts.join(" > "))
        }
    });
    RecordCtx {
        file,
        line,
        fn_chain,
    }
}
/// The source position of a mutation as a FILE line. Inside a function
/// `$LINENO` counts from the function body's first line (zsh prints `1` for
/// the statement right after `name() {`), so on its own it names the wrong
/// line of the file. The function's definition site (`$funcsourcetrace[1]`,
/// `file:line` of the `name() {` line) anchors it: file line = definition
/// line + `$LINENO`. At top level, or when the definition site is unknown,
/// the position is returned unchanged.
pub fn absolute_site(
    file: Option<String>,
    lineno: Option<u32>,
    defined_at: Option<&str>,
) -> (Option<String>, Option<u32>) {
    let anchored = defined_at.and_then(|d| {
        let (f, l) = d.rsplit_once(':')?;
        Some((f.to_string(), l.parse::<u32>().ok()?))
    });
    match (anchored, lineno) {
        (Some((def_file, def_line)), Some(n)) if !def_file.is_empty() => {
            (Some(def_file), Some(def_line + n))
        }
        _ => (file, lineno),
    }
}

/// Skip the end-of-run shard write (`--dry-run`).
#[inline]
pub fn set_no_write(v: bool) {
    NO_WRITE.store(v, Ordering::Relaxed);
}

#[inline]
fn no_write() -> bool {
    NO_WRITE.load(Ordering::Relaxed)
}
/// `set_quiet` — see implementation.
#[inline]
pub fn set_quiet(v: bool) {
    QUIET.store(v, Ordering::Relaxed);
}

#[inline]
fn quiet() -> bool {
    QUIET.load(Ordering::Relaxed)
}
/// `set_json_summary` — see implementation.
#[inline]
pub fn set_json_summary(v: bool) {
    JSON_SUMMARY.store(v, Ordering::Relaxed);
}

#[inline]
fn json_summary_enabled() -> bool {
    JSON_SUMMARY.load(Ordering::Relaxed)
}
/// `set_output_path` — see implementation.
pub fn set_output_path(p: Option<String>) {
    if let Ok(mut g) = OUTPUT_PATH.lock() {
        *g = p;
    }
}

fn output_path() -> Option<String> {
    OUTPUT_PATH.lock().ok().and_then(|g| g.clone())
}
/// `set_shell_id_override` — see implementation.
pub fn set_shell_id_override(s: Option<String>) {
    if let Ok(mut g) = SHELL_ID_OVERRIDE.lock() {
        *g = s;
    }
}

fn shell_id_override() -> Option<String> {
    SHELL_ID_OVERRIDE.lock().ok().and_then(|g| g.clone())
}

fn now_ns() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_nanos() as u64)
        .unwrap_or(0)
}

/// Per-call site context derived from the executor (current `$LINENO`,
/// current source file, current `$funcstack`).
#[derive(Debug, Clone, Default)]
pub struct RecordCtx {
    /// `file` field.
    pub file: Option<String>,
    /// `line` field.
    pub line: Option<u32>,
    /// `fn_chain` field.
    pub fn_chain: Option<String>,
}

fn loc_str(file: &Option<String>, line: Option<u32>) -> String {
    match (file.as_deref(), line) {
        (Some(f), Some(l)) => format!("{}:{}", f, l),
        (Some(f), None) => f.to_string(),
        _ => "<unknown>".to_string(),
    }
}

fn fn_chain_suffix(chain: &Option<String>) -> String {
    match chain.as_deref() {
        Some(c) if !c.is_empty() => format!(" ({})", c),
        _ => String::new(),
    }
}

/// Push a record. Real-time stderr line + tracing log line + push to
/// in-process buffer. Re-entrancy-guarded.
pub fn emit(
    kind: DefKind,
    name: impl Into<String>,
    value: Option<String>,
    file: Option<String>,
    line: Option<u32>,
    fn_chain: Option<String>,
) {
    emit_with_attrs(kind, name, value, file, line, fn_chain, ParamAttrs::NONE)
}

/// Push a record with explicit ParamAttrs. Used by typeset-family
/// dispatchers so the structured attrs ride alongside the record.
pub fn emit_with_attrs(
    kind: DefKind,
    name: impl Into<String>,
    value: Option<String>,
    file: Option<String>,
    line: Option<u32>,
    fn_chain: Option<String>,
    attrs: ParamAttrs,
) {
    emit_full(kind, name, value, file, line, fn_chain, attrs, None, None)
}

/// Full emit with replay-grade structured payload. Array and assoc
/// hooks call this directly so element ordering / key-value pairs
/// survive the wire trip to the daemon for exact reconstruction.
#[allow(clippy::too_many_arguments)]
pub fn emit_full(
    kind: DefKind,
    name: impl Into<String>,
    value: Option<String>,
    file: Option<String>,
    line: Option<u32>,
    fn_chain: Option<String>,
    attrs: ParamAttrs,
    value_array: Option<Vec<String>>,
    value_assoc: Option<Vec<(String, String)>>,
) {
    if !is_enabled() {
        return;
    }
    if IN_RECORDER.swap(true, Ordering::Acquire) {
        return;
    }
    let name = name.into();

    // Realtime "Captured ..." line, format per docs/RECORDER.md user spec.
    let value_part = match value.as_deref() {
        Some(v) => format!("={}", short_value(v)),
        None => String::new(),
    };
    let loc = loc_str(&file, line);
    let chain = fn_chain_suffix(&fn_chain);
    let kind_str = kind.as_str();
    let attrs_part = if attrs.0 == 0 {
        String::new()
    } else {
        format!(" [{}]", attrs_to_str(attrs))
    };
    if !quiet() {
        eprintln!(
            "Captured {} {}{}{}, file: {}{}",
            kind_str, name, attrs_part, value_part, loc, chain
        );
    }
    tracing::info!(
        kind = kind_str,
        %name,
        value = value.as_deref().unwrap_or(""),
        attrs = attrs.0,
        file = file.as_deref().unwrap_or(""),
        line = line.unwrap_or(0),
        fn_chain = fn_chain.as_deref().unwrap_or(""),
        "recorder: captured"
    );

    let ev = RecordEvent {
        order_idx: ORDER_IDX.fetch_add(1, Ordering::Relaxed),
        ts_ns: now_ns(),
        kind,
        name,
        value,
        file,
        line,
        fn_chain,
        attrs,
        value_array,
        value_assoc,
        // Per-event shell_id stays None — the bundle's top-level
        // shell_id at flush time tells the daemon which shell these
        // events came from. Shaving N bytes per event matters at
        // recorder scale (zpwr ingest = ~20k events).
        shell_id: None,
    };
    if let Ok(mut buf) = BUFFER.lock() {
        buf.push(ev);
    }
    IN_RECORDER.store(false, Ordering::Release);
}

/// Format ParamAttrs as a comma-joined human label for the realtime
/// stderr line (`[scalar,export,readonly]` etc.). Wire format stays
/// the raw u16.
fn attrs_to_str(a: ParamAttrs) -> String {
    let mut parts: Vec<&'static str> = Vec::new();
    if a.has(ParamAttrs::INTEGER) {
        parts.push("integer");
    }
    if a.has(ParamAttrs::FLOAT) {
        parts.push("float");
    }
    if a.has(ParamAttrs::ASSOC) {
        parts.push("assoc");
    }
    if a.has(ParamAttrs::ARRAY) {
        parts.push("array");
    }
    if a.has(ParamAttrs::SCALAR) && parts.is_empty() {
        parts.push("scalar");
    }
    if a.has(ParamAttrs::READONLY) {
        parts.push("readonly");
    }
    if a.has(ParamAttrs::EXPORT) {
        parts.push("export");
    }
    if a.has(ParamAttrs::GLOBAL) {
        parts.push("global");
    }
    if a.has(ParamAttrs::UNIQUE) {
        parts.push("unique");
    }
    if a.has(ParamAttrs::TIED) {
        parts.push("tied");
    }
    if a.has(ParamAttrs::HIDE) {
        parts.push("hide");
    }
    if a.has(ParamAttrs::HIDE_VAL) {
        parts.push("hideval");
    }
    if a.has(ParamAttrs::APPEND) {
        parts.push("append");
    }
    parts.join(",")
}

/// Truncate values for the realtime stderr line. SQL/IPC store the full
/// value; only the human readout is trimmed.
fn short_value(s: &str) -> String {
    const MAX: usize = 120;
    let single = s.replace('\n', "\\n");
    if single.chars().count() <= MAX {
        format!("\"{}\"", single)
    } else {
        let mut clipped: String = single.chars().take(MAX).collect();
        clipped.push('…');
        format!("\"{}\"", clipped)
    }
}

/// Per-builtin convenience wrappers — one per state-mutating dispatcher.
pub fn emit_alias(name: &str, value: Option<&str>, ctx: RecordCtx) {
    emit(
        DefKind::Alias,
        name,
        value.map(str::to_string),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_galias` — see implementation.
pub fn emit_galias(name: &str, value: Option<&str>, ctx: RecordCtx) {
    emit(
        DefKind::GAlias,
        name,
        value.map(str::to_string),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_salias` — see implementation.
pub fn emit_salias(name: &str, value: Option<&str>, ctx: RecordCtx) {
    emit(
        DefKind::SAlias,
        name,
        value.map(str::to_string),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_function` — see implementation.
pub fn emit_function(name: &str, body: Option<&str>, ctx: RecordCtx) {
    emit(
        DefKind::Function,
        name,
        body.map(str::to_string),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_assign` — see implementation.
pub fn emit_assign(name: &str, value: &str, ctx: RecordCtx) {
    emit(
        DefKind::Assign,
        name,
        Some(value.to_string()),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}

/// Scalar-assign with structured attrs. Call sites that have already
/// inspected the executor for the parameter's declared type
/// (`var_attrs.kind`, `readonly`, `export`) pass a populated
/// `ParamAttrs` here so the recorded event tells replay exactly how
/// to declare the variable. Without populated attrs, replay would
/// have to guess scalar vs array vs integer from the value string —
/// guesses break when `EDITOR=vim` was previously declared `typeset
/// -gx EDITOR`, since plain replay loses the export/global bits.
pub fn emit_assign_typed(name: &str, value: &str, attrs: ParamAttrs, ctx: RecordCtx) {
    emit_with_attrs(
        DefKind::Assign,
        name,
        Some(value.to_string()),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
        attrs,
    );
}

/// Indexed-array assign with the ordered element list preserved.
/// `is_append` controls a SET vs APPEND attribute bit so replay knows
/// whether to start the array fresh or extend an existing one.
pub fn emit_array_assign(
    name: &str,
    elements: Vec<String>,
    mut attrs: ParamAttrs,
    is_append: bool,
    ctx: RecordCtx,
) {
    attrs.set(ParamAttrs::ARRAY);
    if is_append {
        attrs.set(ParamAttrs::APPEND);
    }
    let joined = elements.join(" ");
    emit_full(
        DefKind::Assign,
        name,
        Some(joined),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
        attrs,
        Some(elements),
        None,
    );
}

/// Assoc-array assign with insertion-ordered (key, value) pairs.
/// `is_append` distinguishes `h=(...)` (replace) from `h+=(...)`
/// / `h[k]=v` element-add semantics.
pub fn emit_assoc_assign(
    name: &str,
    pairs: Vec<(String, String)>,
    mut attrs: ParamAttrs,
    is_append: bool,
    ctx: RecordCtx,
) {
    attrs.set(ParamAttrs::ASSOC);
    if is_append {
        attrs.set(ParamAttrs::APPEND);
    }
    // Joined key/value pairs as the scalar fallback for clients that
    // only read `value`.
    let joined = pairs
        .iter()
        .flat_map(|(k, v)| [k.as_str(), v.as_str()])
        .collect::<Vec<_>>()
        .join(" ");
    emit_full(
        DefKind::Assign,
        name,
        Some(joined),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
        attrs,
        None,
        Some(pairs),
    );
}
/// Backwards-compat: legacy emit_typeset used by sites that haven't
/// migrated to structured attrs yet. Parses the leading flags out of
/// `flags_value` (everything starting with `-`) and routes to the
/// attrs-aware emitter. New call sites should use `emit_typeset_attrs`.
pub fn emit_typeset(name: &str, flags_value: &str, ctx: RecordCtx) {
    let mut letters = String::new();
    let mut value_part = String::new();
    let mut iter = flags_value.split_whitespace();
    while let Some(tok) = iter.next() {
        if let Some(rest) = tok.strip_prefix('-') {
            letters.push_str(rest);
        } else if let Some(rest) = tok.strip_prefix('+') {
            // +F means "unset float attribute"; for the recorder we
            // don't currently distinguish set/unset of attrs — record
            // the letters as-is.
            letters.push_str(rest);
        } else {
            if !value_part.is_empty() {
                value_part.push(' ');
            }
            value_part.push_str(tok);
        }
    }
    let attrs = ParamAttrs::from_flag_chars(&letters);
    let value_opt = if value_part.is_empty() {
        None
    } else {
        Some(value_part)
    };
    emit_with_attrs(
        DefKind::Typeset,
        name,
        value_opt,
        ctx.file,
        ctx.line,
        ctx.fn_chain,
        attrs,
    );
}

/// Typed typeset emitter — call sites that already have attrs in hand
/// (e.g. `builtin_integer` knows it's emitting an integer) skip the
/// flag-string round-trip.
pub fn emit_typeset_attrs(name: &str, value: Option<&str>, attrs: ParamAttrs, ctx: RecordCtx) {
    emit_with_attrs(
        DefKind::Typeset,
        name,
        value.map(str::to_string),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
        attrs,
    );
}
/// `emit_export` — see implementation.
pub fn emit_export(name: &str, value: Option<&str>, ctx: RecordCtx) {
    emit(
        DefKind::Export,
        name,
        value.map(str::to_string),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_path_mod` — see implementation.
pub fn emit_path_mod(name: &str, op: &str, ctx: RecordCtx) {
    emit(
        DefKind::PathMod,
        name,
        Some(op.to_string()),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_hash_d` — see implementation.
pub fn emit_hash_d(name: &str, path: &str, ctx: RecordCtx) {
    emit(
        DefKind::HashD,
        name,
        Some(path.to_string()),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_zstyle` — see implementation.
pub fn emit_zstyle(pattern: &str, rest: &str, ctx: RecordCtx) {
    emit(
        DefKind::Zstyle,
        pattern,
        Some(rest.to_string()),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_bindkey` — see implementation.
pub fn emit_bindkey(seq: &str, widget: &str, ctx: RecordCtx) {
    emit(
        DefKind::Bindkey,
        seq,
        Some(widget.to_string()),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_compdef` — see implementation.
pub fn emit_compdef(func: &str, cmds: &str, ctx: RecordCtx) {
    emit(
        DefKind::Compdef,
        func,
        Some(cmds.to_string()),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_zmodload` — see implementation.
pub fn emit_zmodload(module: &str, flags: &str, ctx: RecordCtx) {
    emit(
        DefKind::Zmodload,
        module,
        Some(flags.to_string()),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_setopt` — see implementation.
pub fn emit_setopt(opt: &str, ctx: RecordCtx) {
    emit(DefKind::Setopt, opt, None, ctx.file, ctx.line, ctx.fn_chain);
}
/// `emit_unsetopt` — see implementation.
pub fn emit_unsetopt(opt: &str, ctx: RecordCtx) {
    emit(
        DefKind::Unsetopt,
        opt,
        None,
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_trap` — see implementation.
pub fn emit_trap(sig: &str, handler: &str, ctx: RecordCtx) {
    emit(
        DefKind::Trap,
        sig,
        Some(handler.to_string()),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_sched` — see implementation.
pub fn emit_sched(when: &str, cmd: &str, ctx: RecordCtx) {
    emit(
        DefKind::Sched,
        when,
        Some(cmd.to_string()),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_source` — see implementation.
pub fn emit_source(path: &str, ctx: RecordCtx) {
    emit(
        DefKind::Source,
        path,
        None,
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_unalias` — see implementation.
pub fn emit_unalias(name: &str, ctx: RecordCtx) {
    emit(
        DefKind::Unalias,
        name,
        None,
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_unset` — see implementation.
pub fn emit_unset(name: &str, ctx: RecordCtx) {
    emit(DefKind::Unset, name, None, ctx.file, ctx.line, ctx.fn_chain);
}
/// `emit_zle` — see implementation.
pub fn emit_zle(widget: &str, func: Option<&str>, ctx: RecordCtx) {
    emit(
        DefKind::Zle,
        widget,
        func.map(str::to_string),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
/// `emit_completion` — see implementation.
pub fn emit_completion(name: &str, abs_path: &str, ctx: RecordCtx) {
    emit(
        DefKind::Completion,
        name,
        Some(abs_path.to_string()),
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}

/// Walk `dir` and emit one `Completion` event per `_*` file found —
/// matches what zinit-report surfaces in its "Completions:" section.
/// Called from the path/fpath array hook whenever an fpath dir is
/// added (set or appended). Filesystem I/O — bounded to the directory
/// the user just registered, so the cost is paid once per fpath edit
/// (typically tens of files per plugin, hundreds for big completion
/// trees like zsh-more-completions).
///
/// Filename rule (matches compinit/compaudit at zsh/Src/Zle/comp1.c):
/// every entry starting with `_` and not a directory is a candidate.
/// Symlinks are dereferenced. Hidden subdirs and `.zwc` files are
/// skipped. Errors (unreadable dir, EACCES) are tracing-warn'd and
/// otherwise silent — this is a best-effort surfacing layer.
pub fn discover_completions_in_fpath_dir(dir: &str, ctx: &RecordCtx) {
    if !is_enabled() {
        return;
    }
    let entries = match std::fs::read_dir(dir) {
        Ok(rd) => rd,
        Err(e) => {
            tracing::warn!(?e, dir, "recorder: completion discovery skipped");
            return;
        }
    };
    for entry in entries.flatten() {
        let name = match entry.file_name().into_string() {
            Ok(n) => n,
            Err(_) => continue,
        };
        if !name.starts_with('_') {
            continue;
        }
        if name.ends_with(".zwc") {
            continue;
        }
        let path = entry.path();
        // file_type may need to follow a symlink to know if it's a dir.
        let is_file = match entry.file_type() {
            Ok(ft) if ft.is_symlink() => std::fs::metadata(&path)
                .map(|m| m.is_file())
                .unwrap_or(false),
            Ok(ft) => ft.is_file(),
            Err(_) => false,
        };
        if !is_file {
            continue;
        }
        let abs = path.to_string_lossy().to_string();
        emit_completion(&name, &abs, ctx.clone());
    }
}

/// End-of-run summary printed to stderr right before the IPC bundle
/// is sent. Counts per `kind` plus totals. Called from `atexit`, so
/// must avoid touching anything backed by thread-locals (tracing's
/// dispatch + once_cell::Lazy can both raise AccessError once Rust
/// starts tearing down TLS).
pub fn print_summary() {
    if !is_enabled() {
        return;
    }
    let buf = match BUFFER.try_lock() {
        Ok(b) => b,
        Err(_) => return,
    };
    let total = buf.len();
    let mut counts: std::collections::BTreeMap<&'static str, usize> =
        std::collections::BTreeMap::new();
    for ev in buf.iter() {
        *counts.entry(ev.kind.as_str()).or_insert(0) += 1;
    }
    let started = START_NS.load(Ordering::Relaxed);
    let elapsed_ms = if started > 0 {
        (now_ns().saturating_sub(started)) / 1_000_000
    } else {
        0
    };
    if json_summary_enabled() {
        // Single JSON line to stdout — pipes cleanly into jq / scripts.
        let mut counts_pairs = Vec::with_capacity(counts.len());
        for (k, v) in &counts {
            counts_pairs.push(format!("\"{}\":{}", k, v));
        }
        println!(
            "{{\"total_events\":{},\"elapsed_ms\":{},\"counts\":{{{}}}}}",
            total,
            elapsed_ms,
            counts_pairs.join(",")
        );
    } else {
        eprintln!();
        eprintln!("--- zshrs-recorder summary ---");
        eprintln!("  total events: {}", total);
        for (k, v) in &counts {
            eprintln!("  {:<10} {}", k, v);
        }
        eprintln!("  elapsed:      {} ms", elapsed_ms);
    }
}

/// Bundle every captured event, fold it into the canonical rkyv shard
/// and write `~/.zshrs/images/{hash8}-recorder.rkyv`, then clear the
/// buffer. Returns `true` when the shard was written. No daemon: the
/// recorder owns this write so a recording lands on a machine where
/// `zshrs-daemon` was never started. Called from `atexit`, so avoids
/// tracing/TLS-touching helpers (use `eprintln!` only).
///
/// `--dry-run` skips the shard write; `-o PATH` still writes the
/// bundle JSON so post-mortem inspection works either way.
#[cfg(feature = "daemon")]
pub fn flush() -> bool {
    if !is_enabled() {
        return false;
    }
    let events = match BUFFER.try_lock() {
        Ok(mut b) => std::mem::take(&mut *b),
        Err(_) => return false,
    };
    let events_empty = events.is_empty();
    let bundle = RecorderBundle {
        started_at_ns: START_NS.load(Ordering::Relaxed),
        finished_at_ns: now_ns(),
        cmdline: std::env::args().collect::<Vec<_>>().join(" "),
        zdotdir: std::env::var("ZDOTDIR").ok(),
        home: std::env::var("HOME").ok(),
        events,
        // `--shell-id ID` overrides this so a recorder run can
        // impersonate a non-zshrs source (federation testing,
        // rebrand experiments). Default = "zshrs" since this code
        // path is exclusive to the AOP-instrumented zshrs-recorder.
        shell_id: Some(shell_id_override().unwrap_or_else(|| "zshrs".to_string())),
        end_state: END_STATE.lock().ok().and_then(|mut s| s.take()),
    };

    // `-o PATH` writes the bundle to a JSON file alongside (or instead
    // of, under --dry-run) the shard. Empty bundles still write —
    // caller asked for the file, give them the file (even if it just
    // confirms "recorder ran, captured nothing" — that itself is a
    // useful diagnostic when a parse error caused zero events).
    if let Some(path) = output_path() {
        match serde_json::to_string(&bundle) {
            Ok(s) => {
                if let Err(e) = std::fs::write(&path, s.as_bytes()) {
                    eprintln!("recorder: write {path}: {e}");
                } else {
                    eprintln!("recorder: bundle written to {path}");
                }
            }
            Err(e) => eprintln!("recorder: bundle serialize for output failed: {e}"),
        }
    }

    if events_empty {
        eprintln!("recorder: no events to flush");
        return false;
    }
    if no_write() {
        return false;
    }

    let t0 = Instant::now();
    // `RecordEvent` and the shard fold's `BundleEvent` share one wire
    // format; round-tripping through serde keeps the two crates'
    // types independent.
    let folded_input = serde_json::to_value(&bundle).and_then(serde_json::from_value::<
        crate::daemon::recorder_shard::Bundle,
    >);
    let shard_bundle = match folded_input {
        Ok(b) => b,
        Err(e) => {
            eprintln!("recorder: bundle conversion failed: {e}");
            return false;
        }
    };
    let paths = match crate::daemon::paths::CachePaths::resolve() {
        Ok(p) => p,
        Err(e) => {
            eprintln!("recorder: cache paths unresolved: {e}");
            return false;
        }
    };
    if let Err(e) = paths.ensure_dirs() {
        eprintln!("recorder: {}: {e}", paths.images.display());
        return false;
    }
    match crate::daemon::recorder_shard::write_bundle_shard(&paths, &shard_bundle) {
        Ok(path) => {
            eprintln!(
                "recorder: {} events written to {} in {} ms",
                shard_bundle.events.len(),
                path.display(),
                t0.elapsed().as_millis()
            );
            true
        }
        Err(e) => {
            eprintln!("recorder: shard write failed: {e}");
            false
        }
    }
}
/// Non-daemon builds carry no shard writer.
#[cfg(not(feature = "daemon"))]
pub fn flush() -> bool {
    if !is_enabled() {
        return false;
    }
    eprintln!("recorder: daemon feature off — shard not written");
    false
}

/// Set once the summary + shard write ran, so the natural end of
/// `main` and the atexit hook never both fire.
static FINALIZED: AtomicBool = AtomicBool::new(false);

/// Print the summary and write the shard. `bins/zshrs-recorder.rs`
/// calls this at the end of `main`, while thread-locals are still
/// alive — `write_canonical_shard` logs through `tracing`, which
/// panics with `AccessError` once `main` has returned. The atexit
/// hook covers the paths that never get back to `main` (`exit` inside
/// the sourced files).
pub fn finalize() {
    if FINALIZED.swap(true, Ordering::SeqCst) {
        return;
    }
    print_summary();
    flush();
}

extern "C" fn atexit_finalize() {
    // A forked subshell or `$(...)` inherits this hook AND a copy of the
    // buffer holding every event captured so far. Finalizing there wrote
    // a partial shard (and a partial summary) per child, and whichever
    // child exited last won — the parent's full recording was lost.
    if std::process::id() as u64 != OWNER_PID.load(Ordering::Relaxed) {
        return;
    }
    if FINALIZED.swap(true, Ordering::SeqCst) {
        return;
    }
    // libc atexit runs AFTER the Rust runtime starts tearing down
    // thread-locals. `tracing::*` and `once_cell::Lazy` both touch TLS,
    // so we must catch the AccessError they raise during destruction —
    // an unwinding panic from an `extern "C"` function aborts the
    // process and swallows the very output the user is here to see.
    // The mark lets the log sites below the call skip `tracing` entirely
    // rather than panic and be caught; see `atexit_teardown`.
    crate::atexit_teardown::mark();
    let _ = std::panic::catch_unwind(|| {
        print_summary();
    });
    let _ = std::panic::catch_unwind(|| {
        flush();
    });
}

/// Register `print_summary` + `flush` as a libc `atexit` hook so they
/// run even when the shell exits via `std::process::exit`. Only the
/// calling process finalizes; forked children skip the hook.
pub fn install_atexit() {
    OWNER_PID.store(std::process::id() as u64, Ordering::Relaxed);
    // SAFETY: `atexit_finalize` is a plain `extern "C"` function with the
    // libc-required signature.
    unsafe {
        libc::atexit(atexit_finalize);
    }
}