zshrs 0.11.1

The first compiled Unix shell — bytecode VM, 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
//! 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, IPCs it once to `zshrs-daemon`,
//! prints summary stats, then exits. The daemon ingests the bundle and
//! rebuilds the rkyv + SQLite read caches from it.
//!
//! 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 daemon IPC. Used by hermetic tests
/// (`tests/recorder_harness.rs`) and one-off `--no-daemon` runs that
/// just want stderr capture without spawning a daemon.
static DAEMON_DISABLED: AtomicBool = AtomicBool::new(false);

/// 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 daemon ship-out, or instead of it under --no-daemon). 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. Flushed to the daemon
/// in one IPC call 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, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DefKind {
    Alias,
    GAlias,
    SAlias,
    Function,
    Assign,
    Typeset,
    Export,
    PathMod,
    HashD,
    Zstyle,
    Bindkey,
    Compdef,
    Zmodload,
    Setopt,
    Unsetopt,
    Trap,
    Sched,
    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,
    /// `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 {
    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 {
    pub const NONE: Self = Self(0);
    pub const SCALAR: u16 = 1 << 0;
    pub const INTEGER: u16 = 1 << 1;
    pub const FLOAT: u16 = 1 << 2;
    pub const ASSOC: u16 = 1 << 3;
    pub const ARRAY: u16 = 1 << 4;
    pub const READONLY: u16 = 1 << 5;
    pub const EXPORT: u16 = 1 << 6;
    pub const GLOBAL: u16 = 1 << 7;
    pub const UNIQUE: u16 = 1 << 8;
    pub const TIED: u16 = 1 << 9;
    pub const HIDE: u16 = 1 << 10;
    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;

    pub fn set(&mut self, mask: u16) {
        self.0 |= mask;
    }
    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 {
    pub order_idx: u64,
    pub ts_ns: u64,
    pub kind: DefKind,
    pub name: String,
    pub value: Option<String>,
    pub file: Option<String>,
    pub line: Option<u32>,
    pub fn_chain: Option<String>,
    #[serde(default, skip_serializing_if = "is_default_attrs")]
    pub attrs: ParamAttrs,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub value_array: Option<Vec<String>>,
    #[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 {
    pub started_at_ns: u64,
    pub finished_at_ns: u64,
    pub cmdline: String,
    pub zdotdir: Option<String>,
    pub home: Option<String>,
    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>,
}

#[inline]
pub fn enable() {
    ENABLED.store(true, Ordering::Relaxed);
    START_NS.store(now_ns(), Ordering::Relaxed);
    tracing::info!("recorder: enabled");
}

#[inline]
pub fn is_enabled() -> bool {
    ENABLED.load(Ordering::Relaxed)
}

/// Free-fn equivalent of `ShellExecutor::recorder_ctx()` — synthesizes
/// a RecordCtx from the live env vars (`$LINENO`, `$ZSH_SCRIPT`,
/// `$funcstack`) so canonical free-fn ports of C builtins (which
/// don't take a `&ShellExecutor`) can emit recorder events without
/// the executor in scope. Mirrors src/extensions/recorder.rs at
/// line 62 — same source-of-truth, different binding.
pub fn recorder_ctx_global() -> RecordCtx {
    let line = std::env::var("LINENO").ok().and_then(|s| s.parse::<u32>().ok());
    let file = std::env::var("ZSH_SCRIPT").ok()
        .or_else(|| std::env::var("ZSH_ARGZERO").ok())
        .or_else(|| std::env::var("0").ok());
    // funcstack is an array param exposed as a colon-joined env var via
    // ksh93::setsparam (the canonical free-fn bridge); split + reverse
    // to match the executor-side `funcstack > parent > grand` chain.
    let fn_chain = std::env::var("funcstack").ok().and_then(|s| {
        if s.is_empty() {
            None
        } else {
            let mut parts: Vec<&str> = s.split(':').collect();
            parts.reverse();
            Some(parts.join(" > "))
        }
    });
    RecordCtx { file, line, fn_chain }
}

#[inline]
pub fn set_daemon_disabled(v: bool) {
    DAEMON_DISABLED.store(v, Ordering::Relaxed);
}

#[inline]
fn daemon_disabled() -> bool {
    DAEMON_DISABLED.load(Ordering::Relaxed)
}

#[inline]
pub fn set_quiet(v: bool) {
    QUIET.store(v, Ordering::Relaxed);
}

#[inline]
fn quiet() -> bool {
    QUIET.load(Ordering::Relaxed)
}

#[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)
}

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())
}

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 {
    pub file: Option<String>,
    pub line: Option<u32>,
    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,
    );
}
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,
    );
}
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,
    );
}
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,
    );
}
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,
    );
}
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,
    );
}
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,
    );
}
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,
    );
}
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,
    );
}
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,
    );
}
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,
    );
}
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,
    );
}
pub fn emit_setopt(opt: &str, ctx: RecordCtx) {
    emit(
        DefKind::Setopt,
        opt,
        None,
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
pub fn emit_unsetopt(opt: &str, ctx: RecordCtx) {
    emit(
        DefKind::Unsetopt,
        opt,
        None,
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
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,
    );
}
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,
    );
}
pub fn emit_source(path: &str, ctx: RecordCtx) {
    emit(
        DefKind::Source,
        path,
        None,
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
pub fn emit_unalias(name: &str, ctx: RecordCtx) {
    emit(
        DefKind::Unalias,
        name,
        None,
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
pub fn emit_unset(name: &str, ctx: RecordCtx) {
    emit(
        DefKind::Unset,
        name,
        None,
        ctx.file,
        ctx.line,
        ctx.fn_chain,
    );
}
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,
    );
}
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, send a single IPC frame to
/// `zshrs-daemon`'s `recorder_ingest` op, and clear the buffer. Returns
/// `true` if the daemon accepted the bundle. Called from `atexit`, so
/// avoids tracing/TLS-touching helpers (use `eprintln!` only).
///
/// `--no-daemon` skips the IPC step but `-o PATH` still writes the
/// bundle to disk so post-mortem inspection works without a daemon.
#[cfg(feature = "daemon")]
pub fn flush_to_daemon() -> 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())),
    };

    // `-o PATH` writes the bundle to a JSON file alongside (or instead
    // of, under --no-daemon) shipping it to the daemon. Useful for
    // post-mortem inspection without spinning a daemon up. 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 daemon_disabled() {
        return false;
    }
    let event_count = bundle.events.len();
    let payload = match serde_json::to_value(&bundle) {
        Ok(v) => v,
        Err(e) => {
            eprintln!("recorder: bundle serialize failed: {}", e);
            return false;
        }
    };
    let t0 = Instant::now();
    // `call_once` (not `_no_spawn`) so the recorder spawns the daemon if
    // it isn't already running — first-time-setup runs must succeed even
    // on a cold machine.
    match crate::daemon::client::call_once("recorder_ingest", payload) {
        Ok(_) => {
            let took_ms = t0.elapsed().as_millis();
            eprintln!(
                "recorder: bundled {} events to daemon in {} ms",
                event_count, took_ms
            );
            true
        }
        Err(e) => {
            eprintln!("recorder: daemon ingest failed: {}", e);
            false
        }
    }
}

#[cfg(not(feature = "daemon"))]
pub fn flush_to_daemon() -> bool {
    if !is_enabled() {
        return false;
    }
    eprintln!("recorder: daemon feature off — bundle not sent");
    false
}

extern "C" fn atexit_finalize() {
    // 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.
    let _ = std::panic::catch_unwind(|| {
        print_summary();
    });
    let _ = std::panic::catch_unwind(|| {
        flush_to_daemon();
    });
}

/// Register `print_summary` + `flush_to_daemon` as a libc `atexit` hook
/// so they run even when the shell exits via `std::process::exit`.
pub fn install_atexit() {
    // SAFETY: `atexit_finalize` is a plain `extern "C"` function with the
    // libc-required signature.
    unsafe {
        libc::atexit(atexit_finalize);
    }
}