ai-model-vault 4.6.1

DEPRECATED - renamed to `ironvault`. This crate is no longer maintained; 4.6.1 is identical to 4.6.0 apart from this notice.
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
//! Telemetry and analytics module for AI Model Vault.
//!
//! Collects anonymous usage data to help improve the product.
//! **Disabled by default** — users can opt in via:
//! - Config file: `telemetry.enabled = true`
//! - CLI: `aim telemetry enable`
//!
//! To disable (if previously enabled):
//! - Config file: `telemetry.enabled = false`
//! - Environment variable: `AIM_TELEMETRY_ENABLED=false` or `AIM_TELEMETRY_DISABLED=1`
//! - Environment variable: `DO_NOT_TRACK=1`
//!
//! ## Data Collected
//!
//! Only if you opt in. Two events are emitted.
//!
//! [`TelemetryEvent::AppStart`], once per process:
//!
//! - **Environment**: version, OS, architecture, enabled feature flags
//! - **Anonymous ID**: random UUID v4 generated on first run, and a per-run
//!   session UUID. Neither is derived from anything about the machine or the
//!   user, so neither can be correlated back to an identity.
//!
//! [`TelemetryEvent::CommandRun`], once per CLI invocation:
//!
//! - **Command and subcommand name**, duration in milliseconds, and a success
//!   boolean. The binary takes both names from clap's registered command
//!   table rather than from the command line, so the field can only ever hold
//!   one of the subcommand literals declared in `args.rs` — never an argument
//!   value. The failure *reason* is not recorded, only the boolean.
//!
//! [`TelemetryEvent::ModelOperation`] on store/get/delete: operation, format
//! label, a **size bucket** (never the exact size), duration, outcome.
//!
//! [`TelemetryEvent::Conversion`] on `aim convert`: source and target format
//! labels, duration, outcome.
//!
//! [`TelemetryEvent::ApiCall`] per HTTP request: the matched **route
//! template**, method, status, duration.
//!
//! [`TelemetryEvent::Error`] when a command fails: the variant name from
//! [`crate::VaultError::kind`] and nothing else — never the message.
//!
//! [`TelemetryEvent::FeatureUsed`] for KMS: the URI scheme only.
//!
//! Every one of those labels is a `&'static str` chosen from a closed set.
//! Format labels come from [`crate::formats::ModelFormat::telemetry_name`]
//! rather than `name()`, which returns the user's own string for a custom
//! format.
//!
//! ## Where events go
//!
//! The built-in sender posts to `https://telemetry.nervosys.ai/v1/events`,
//! the project's own collector — the default value of
//! [`TelemetryConfig::endpoint`].
//! It is a compiled-in default and can be overridden with `endpoint` in
//! `config.toml`. Nothing reaches it unless telemetry is explicitly enabled;
//! `enabled` defaults to `false`.
//!
//! The OTLP path ([`OtlpSettings`]) is different: it has no default endpoint
//! and no default credential, and is configured only from the environment.
//!
//! ## Data NOT Collected
//!
//! - Model contents or file data
//! - Passphrases or encryption keys
//! - File paths or model names
//! - Personal information
//! - IP addresses (anonymized by backend)
//!
//! ## Keeping that true
//!
//! Three fields are free-form strings and are the only way the guarantees
//! above can be broken: [`TelemetryEvent::Error::context`],
//! [`TelemetryEvent::ApiCall::endpoint`] and
//! [`TelemetryEvent::FeatureUsed::detail`]. Error messages routinely contain
//! file paths, and a real request path contains the model name, so wiring any
//! of these to a formatted error or an unparameterised route would silently
//! start collecting exactly what this module promises it does not. Pass
//! constants and enum-like discriminants, never a formatted message.

use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, OnceLock};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use uuid::Uuid;

use crate::error::Result;

/// Global telemetry instance
static TELEMETRY: OnceLock<Arc<TelemetryClient>> = OnceLock::new();

/// Whether telemetry has been explicitly disabled
static TELEMETRY_DISABLED: AtomicBool = AtomicBool::new(false);

/// Telemetry configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TelemetryConfig {
    /// Whether telemetry is enabled (default: **false**).
    ///
    /// This said "default: true" while `Default::default` set it to `false`.
    /// The code was right and the comment was wrong, but it is the sort of
    /// wrong that gets quoted in a privacy review.
    pub enabled: bool,

    /// Anonymous device ID (auto-generated UUID)
    #[serde(default = "generate_device_id")]
    pub device_id: String,

    /// Telemetry endpoint URL
    #[serde(default = "default_endpoint")]
    pub endpoint: String,

    /// Batch size before sending (reduces network calls)
    #[serde(default = "default_batch_size")]
    pub batch_size: usize,

    /// Flush interval in seconds
    #[serde(default = "default_flush_interval")]
    pub flush_interval_secs: u64,
}

fn generate_device_id() -> String {
    Uuid::new_v4().to_string()
}

fn default_endpoint() -> String {
    "https://telemetry.nervosys.ai/v1/events".to_string()
}

fn default_batch_size() -> usize {
    25
}

fn default_flush_interval() -> u64 {
    300 // 5 minutes
}

impl Default for TelemetryConfig {
    fn default() -> Self {
        Self {
            enabled: false, // Opt-in: disabled by default for privacy
            device_id: generate_device_id(),
            endpoint: default_endpoint(),
            batch_size: default_batch_size(),
            flush_interval_secs: default_flush_interval(),
        }
    }
}

/// OTLP export settings, read from the standard OpenTelemetry environment
/// variables.
///
/// These are deliberately environment-only and are never written to the
/// telemetry config file, baked into the binary, or defaulted to a vendor
/// endpoint. Two reasons:
///
/// 1. `OTEL_EXPORTER_OTLP_HEADERS` carries a bearer token. A credential that
///    ships inside an AGPL crate published to a public registry is readable by
///    everyone who installs it, which makes it a shared secret with the world
///    rather than an authorisation.
/// 2. Configuring an exporter is an act by whoever *operates* a deployment.
///    Baking one in would export on behalf of every user who merely installed
///    the tool, which is a different person making a different decision.
///
/// Recognised variables (the standard set, so any OpenTelemetry collector or
/// vendor endpoint works without bespoke configuration):
///
/// - `OTEL_EXPORTER_OTLP_ENDPOINT`, or `OTEL_EXPORTER_OTLP_LOGS_ENDPOINT`
/// - `OTEL_EXPORTER_OTLP_PROTOCOL` — `http/protobuf` (default) or `http/json`
/// - `OTEL_EXPORTER_OTLP_HEADERS` — e.g. `Authorization=Bearer <token>`
/// - `OTEL_SERVICE_NAME`
///
/// Setting these does **not** enable telemetry. Telemetry remains opt-in; when
/// it is off, nothing is exported no matter how the exporter is configured.
#[cfg(feature = "otel")]
#[derive(Debug, Clone)]
pub struct OtlpSettings {
    /// Collector endpoint.
    pub endpoint: String,
    /// Wire protocol.
    pub protocol: OtlpProtocol,
    /// Service name reported as `service.name`.
    pub service_name: String,
}

/// OTLP wire protocol.
#[cfg(feature = "otel")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OtlpProtocol {
    /// `http/protobuf`
    HttpBinary,
    /// `http/json`
    HttpJson,
}

/// Non-`otel` builds still need to notice that OTLP was configured, so they can
/// say so instead of silently dropping it.
#[cfg(not(feature = "otel"))]
pub struct OtlpSettings;

impl OtlpSettings {
    /// The configured endpoint, if any, checking the logs-specific variable
    /// first as the specification requires.
    #[must_use]
    pub fn endpoint_from_env() -> Option<String> {
        for key in [
            "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT",
            "OTEL_EXPORTER_OTLP_ENDPOINT",
        ] {
            if let Ok(value) = std::env::var(key) {
                let trimmed = value.trim();
                if !trimmed.is_empty() {
                    return Some(trimmed.to_string());
                }
            }
        }
        None
    }
}

#[cfg(feature = "otel")]
impl OtlpSettings {
    /// Read settings from the environment, or `None` if no endpoint is set.
    #[must_use]
    pub fn from_env() -> Option<Self> {
        let endpoint = Self::endpoint_from_env()?;

        let protocol = match std::env::var("OTEL_EXPORTER_OTLP_LOGS_PROTOCOL")
            .or_else(|_| std::env::var("OTEL_EXPORTER_OTLP_PROTOCOL"))
            .unwrap_or_default()
            .trim()
            .to_ascii_lowercase()
            .as_str()
        {
            "http/json" => OtlpProtocol::HttpJson,
            // The specification's default for HTTP transport, and what an
            // unset or unrecognised value falls back to.
            _ => OtlpProtocol::HttpBinary,
        };

        let service_name = std::env::var("OTEL_SERVICE_NAME")
            .ok()
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .unwrap_or_else(|| env!("CARGO_PKG_NAME").to_string());

        Some(Self {
            endpoint,
            protocol,
            service_name,
        })
    }
}

/// Telemetry event types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum TelemetryEvent {
    /// Application started
    AppStart {
        version: String,
        os: String,
        arch: String,
        features: Vec<String>,
    },

    /// Command executed
    CommandRun {
        command: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        subcommand: Option<String>,
        duration_ms: u64,
        success: bool,
    },

    /// Model operation (store, get, delete)
    ModelOperation {
        operation: String,
        format: String,
        size_bucket: String, // "small", "medium", "large", "xlarge"
        duration_ms: u64,
        success: bool,
    },

    /// Format conversion
    Conversion {
        source_format: String,
        target_format: String,
        duration_ms: u64,
        success: bool,
    },

    /// API endpoint called
    ApiCall {
        endpoint: String,
        method: String,
        status_code: u16,
        duration_ms: u64,
    },

    /// Error occurred
    Error {
        error_type: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        context: Option<String>,
    },

    /// Feature usage
    FeatureUsed {
        feature: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        detail: Option<String>,
    },
}

/// Envelope for telemetry events
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct TelemetryEnvelope {
    pub(crate) device_id: String,
    pub(crate) session_id: String,
    pub(crate) timestamp: u64,
    pub(crate) event: TelemetryEvent,
}

/// Telemetry client for collecting and sending analytics data
pub struct TelemetryClient {
    config: TelemetryConfig,
    session_id: String,
    events: parking_lot::Mutex<Vec<TelemetryEnvelope>>,
    enabled: AtomicBool,
}

impl TelemetryClient {
    /// Create a new telemetry client
    pub fn new(config: TelemetryConfig) -> Self {
        let enabled = config.enabled && !Self::is_disabled_by_env();

        Self {
            enabled: AtomicBool::new(enabled),
            session_id: Uuid::new_v4().to_string(),
            events: parking_lot::Mutex::new(Vec::new()),
            config,
        }
    }

    /// Check if telemetry is disabled via environment variable
    fn is_disabled_by_env() -> bool {
        std::env::var("AIM_TELEMETRY_ENABLED")
            .map(|v| v.to_lowercase() == "false" || v == "0")
            .unwrap_or(false)
            || std::env::var("AIM_TELEMETRY_DISABLED")
                .map(|v| v == "1" || v.to_lowercase() == "true")
                .unwrap_or(false)
            || std::env::var("DO_NOT_TRACK")
                .map(|v| v == "1" || v.to_lowercase() == "true")
                .unwrap_or(false)
    }

    /// Disable telemetry
    pub fn disable(&self) {
        self.enabled.store(false, Ordering::SeqCst);
    }

    /// Enable telemetry
    pub fn enable(&self) {
        self.enabled.store(true, Ordering::SeqCst);
    }

    /// Check if telemetry is enabled
    pub fn is_enabled(&self) -> bool {
        self.enabled.load(Ordering::SeqCst)
    }

    /// Track an event
    pub fn track(&self, event: TelemetryEvent) {
        if !self.is_enabled() {
            return;
        }

        let envelope = TelemetryEnvelope {
            device_id: self.config.device_id.clone(),
            session_id: self.session_id.clone(),
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
            event,
        };

        let mut events = self.events.lock();
        events.push(envelope);

        // Flush if we've reached batch size
        if events.len() >= self.config.batch_size {
            let batch = std::mem::take(&mut *events);
            drop(events);
            self.send_batch(batch);
        }
    }

    /// Flush all pending events
    pub fn flush(&self) {
        if !self.is_enabled() {
            return;
        }

        let batch = std::mem::take(&mut *self.events.lock());
        if !batch.is_empty() {
            self.send_batch(batch);
        }
    }

    /// Send a batch of events to the telemetry endpoint
    fn send_batch(&self, events: Vec<TelemetryEnvelope>) {
        let endpoint = self.config.endpoint.clone();

        std::thread::spawn(move || {
            // Try to send to remote server with retries
            let mut last_error = None;
            for attempt in 0..3 {
                if attempt > 0 {
                    // Exponential backoff: 100ms, 400ms
                    std::thread::sleep(Duration::from_millis(100 * (1 << attempt)));
                }

                // OTLP takes precedence when configured; the JSON endpoint is
                // the fallback for deployments with no collector.
                let result = match Self::try_send_otlp(&events) {
                    Some(otlp_result) => otlp_result,
                    None => Self::send_http_batch(&endpoint, &events),
                };

                match result {
                    Ok(()) => {
                        // Also try to send any previously queued events
                        let _ = Self::flush_local_queue(&endpoint);
                        return;
                    }
                    Err(e) => {
                        last_error = Some(e);
                    }
                }
            }

            // All retries failed - save to local queue for later
            if let Some(_err) = last_error {
                if let Ok(body) = serde_json::to_string(&events) {
                    let _ = Self::write_to_local_queue(&body);
                }
            }
        });
    }

    /// Send events via OTLP, if an OTLP endpoint is configured.
    ///
    /// Returns `None` when OTLP is not configured, so the caller falls back to
    /// the plain JSON sender.
    #[cfg(feature = "otel")]
    fn try_send_otlp(events: &[TelemetryEnvelope]) -> Option<std::result::Result<(), String>> {
        let settings = OtlpSettings::from_env()?;
        Some(crate::telemetry_otlp::export(events, &settings))
    }

    #[cfg(not(feature = "otel"))]
    #[allow(clippy::unnecessary_wraps)]
    fn try_send_otlp(_events: &[TelemetryEnvelope]) -> Option<std::result::Result<(), String>> {
        // Warn rather than fail silently: an operator who configured OTLP and
        // is running a binary without the feature would otherwise see nothing
        // arrive and have no way to tell why.
        if OtlpSettings::endpoint_from_env().is_some() {
            eprintln!(
                "warning: OTEL_EXPORTER_OTLP_ENDPOINT is set, but this binary was \
                 built without the `otel` feature. Telemetry will not be exported \
                 over OTLP."
            );
        }
        None
    }

    /// Send events via HTTP POST
    fn send_http_batch(
        endpoint: &str,
        events: &[TelemetryEnvelope],
    ) -> std::result::Result<(), String> {
        use reqwest::blocking::Client;

        let client = Client::builder()
            .timeout(Duration::from_secs(10))
            .build()
            .map_err(|e| format!("Failed to create HTTP client: {}", e))?;

        let response = client
            .post(endpoint)
            .header("Content-Type", "application/json")
            .header("User-Agent", format!("aim/{}", env!("CARGO_PKG_VERSION")))
            .json(events)
            .send()
            .map_err(|e| format!("HTTP request failed: {}", e))?;

        if response.status().is_success() {
            Ok(())
        } else {
            Err(format!("Server returned error: {}", response.status()))
        }
    }

    /// Flush any events stored in the local queue
    fn flush_local_queue(endpoint: &str) -> std::result::Result<(), String> {
        let queue_file = Self::get_queue_file_path();

        if !queue_file.exists() {
            return Ok(());
        }

        // Read all queued events
        let contents =
            fs::read_to_string(&queue_file).map_err(|e| format!("Failed to read queue: {}", e))?;

        let mut all_sent = true;
        let mut remaining_lines = Vec::new();

        for line in contents.lines() {
            if line.trim().is_empty() {
                continue;
            }

            // Parse the queued batch
            if let Ok(events) = serde_json::from_str::<Vec<TelemetryEnvelope>>(line) {
                if Self::send_http_batch(endpoint, &events).is_err() {
                    all_sent = false;
                    remaining_lines.push(line.to_string());
                }
            }
        }

        // Update queue file - remove sent events
        if all_sent {
            let _ = fs::remove_file(&queue_file);
        } else if !remaining_lines.is_empty() {
            use std::io::Write;
            if let Ok(mut file) = fs::File::create(&queue_file) {
                for line in remaining_lines {
                    let _ = writeln!(file, "{}", line);
                }
            }
        }

        Ok(())
    }

    /// Get the queue file path
    fn get_queue_file_path() -> PathBuf {
        directories::BaseDirs::new()
            .map(|d| {
                d.cache_dir()
                    .join("ai")
                    .join("telemetry")
                    .join("events.jsonl")
            })
            .unwrap_or_else(|| {
                PathBuf::from(".")
                    .join(".cache")
                    .join("ai")
                    .join("telemetry")
                    .join("events.jsonl")
            })
    }

    /// Write events to local queue file (for offline/batched collection)
    fn write_to_local_queue(body: &str) -> std::io::Result<()> {
        use std::io::Write;

        let queue_file = Self::get_queue_file_path();
        if let Some(queue_dir) = queue_file.parent() {
            fs::create_dir_all(queue_dir)?;
            let _ = crate::permissions::restrict_dir(queue_dir);
        }

        let mut options = fs::OpenOptions::new();
        options.create(true).append(true);
        crate::permissions::set_create_mode(&mut options);

        let mut file = options.open(queue_file)?;
        writeln!(file, "{}", body)?;
        Ok(())
    }

    /// Get the device ID
    pub fn device_id(&self) -> &str {
        &self.config.device_id
    }
}

impl Drop for TelemetryClient {
    fn drop(&mut self) {
        self.flush();
    }
}

// === Global telemetry functions ===

/// Initialize the global telemetry client
pub fn init(config: TelemetryConfig) {
    let _ = TELEMETRY.set(Arc::new(TelemetryClient::new(config)));
}

/// Initialize telemetry with default config, loading from disk if available
pub fn init_default(config_dir: Option<&PathBuf>) -> Result<()> {
    let config = load_or_create_config(config_dir)?;
    init(config);
    Ok(())
}

/// Load telemetry config from disk or create default
fn load_or_create_config(config_dir: Option<&PathBuf>) -> Result<TelemetryConfig> {
    let config_path = config_dir
        .cloned()
        .or_else(|| directories::BaseDirs::new().map(|d| d.config_dir().join("ai").join("models")))
        .map(|d| d.join("telemetry.yaml"));

    if let Some(path) = &config_path {
        if path.exists() {
            let contents = fs::read_to_string(path)?;
            let config: TelemetryConfig = serde_yaml_ng::from_str(&contents)?;
            return Ok(config);
        }
    }

    // Create default config and save it
    let config = TelemetryConfig::default();

    if let Some(path) = config_path {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }
        let contents = serde_yaml_ng::to_string(&config)?;
        fs::write(&path, contents)?;
    }

    Ok(config)
}

/// Disable telemetry globally
pub fn disable() {
    TELEMETRY_DISABLED.store(true, Ordering::SeqCst);
    if let Some(client) = TELEMETRY.get() {
        client.disable();
    }
}

/// Check if telemetry is enabled
pub fn is_enabled() -> bool {
    !TELEMETRY_DISABLED.load(Ordering::SeqCst)
        && TELEMETRY.get().map(|c| c.is_enabled()).unwrap_or(false)
}

/// Track an event
pub fn track(event: TelemetryEvent) {
    if let Some(client) = TELEMETRY.get() {
        client.track(event);
    }
}

/// Flush pending events
pub fn flush() {
    if let Some(client) = TELEMETRY.get() {
        client.flush();
    }
}

// === Convenience tracking functions ===

/// Track application start
pub fn track_app_start() {
    let features = collect_enabled_features();

    track(TelemetryEvent::AppStart {
        version: env!("CARGO_PKG_VERSION").to_string(),
        os: std::env::consts::OS.to_string(),
        arch: std::env::consts::ARCH.to_string(),
        features,
    });
}

/// Track a command execution
pub fn track_command(command: &str, subcommand: Option<&str>, duration: Duration, success: bool) {
    track(TelemetryEvent::CommandRun {
        command: command.to_string(),
        subcommand: subcommand.map(|s| s.to_string()),
        duration_ms: duration.as_millis() as u64,
        success,
    });
}

/// Track a model operation
pub fn track_model_op(
    operation: &str,
    format: &str,
    size_bytes: u64,
    duration: Duration,
    success: bool,
) {
    let size_bucket = match size_bytes {
        0..=10_000_000 => "small",              // < 10MB
        10_000_001..=100_000_000 => "medium",   // 10MB - 100MB
        100_000_001..=1_000_000_000 => "large", // 100MB - 1GB
        _ => "xlarge",                          // > 1GB
    };

    track(TelemetryEvent::ModelOperation {
        operation: operation.to_string(),
        format: format.to_string(),
        size_bucket: size_bucket.to_string(),
        duration_ms: duration.as_millis() as u64,
        success,
    });
}

/// Track format conversion
pub fn track_conversion(
    source_format: &str,
    target_format: &str,
    duration: Duration,
    success: bool,
) {
    track(TelemetryEvent::Conversion {
        source_format: source_format.to_string(),
        target_format: target_format.to_string(),
        duration_ms: duration.as_millis() as u64,
        success,
    });
}

/// Track an API call.
///
/// `endpoint` must be the *route template* (`/models/:name`), never the
/// resolved path — a resolved path contains the model name, which this module
/// documents as not collected.
pub fn track_api_call(endpoint: &str, method: &str, status_code: u16, duration: Duration) {
    track(TelemetryEvent::ApiCall {
        endpoint: endpoint.to_string(),
        method: method.to_string(),
        status_code,
        duration_ms: duration.as_millis() as u64,
    });
}

/// Track an error.
///
/// `error_type` is a discriminant (`"integrity"`, `"auth"`), and `context`
/// must be a constant. Never pass a formatted [`crate::VaultError`]: its
/// messages embed file paths and model names, which this module documents as
/// not collected.
pub fn track_error(error_type: &str, context: Option<&str>) {
    track(TelemetryEvent::Error {
        error_type: error_type.to_string(),
        context: context.map(|s| s.to_string()),
    });
}

/// Track feature usage.
///
/// `detail` must be a constant, not user data — see the module-level note on
/// keeping the "not collected" guarantees true.
pub fn track_feature(feature: &str, detail: Option<&str>) {
    track(TelemetryEvent::FeatureUsed {
        feature: feature.to_string(),
        detail: detail.map(|s| s.to_string()),
    });
}

/// Collect enabled feature flags
#[allow(unused_mut, clippy::vec_init_then_push)]
fn collect_enabled_features() -> Vec<String> {
    let mut features: Vec<String> = vec![];

    #[cfg(feature = "api")]
    features.push("api".to_string());

    #[cfg(feature = "python")]
    features.push("python".to_string());

    #[cfg(feature = "cloud")]
    features.push("cloud".to_string());

    features
}

/// Timer guard for automatic duration tracking
pub struct TrackingTimer {
    start: Instant,
    command: String,
    subcommand: Option<String>,
}

impl TrackingTimer {
    pub fn new(command: &str, subcommand: Option<&str>) -> Self {
        Self {
            start: Instant::now(),
            command: command.to_string(),
            subcommand: subcommand.map(|s| s.to_string()),
        }
    }

    pub fn finish(self, success: bool) {
        track_command(
            &self.command,
            self.subcommand.as_deref(),
            self.start.elapsed(),
            success,
        );
    }
}

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

    #[test]
    fn test_telemetry_config_default() {
        let config = TelemetryConfig::default();
        assert!(!config.enabled);
        assert!(!config.device_id.is_empty());
        assert!(config.endpoint.contains("telemetry"));
        assert_eq!(config.batch_size, 25);
        assert_eq!(config.flush_interval_secs, 300);
    }

    #[test]
    fn test_telemetry_client_disable() {
        let config = TelemetryConfig {
            enabled: true,
            ..TelemetryConfig::default()
        };
        let client = TelemetryClient::new(config);

        assert!(client.is_enabled());
        client.disable();
        assert!(!client.is_enabled());
    }

    #[test]
    fn test_telemetry_client_enable() {
        let config = TelemetryConfig {
            enabled: false,
            ..TelemetryConfig::default()
        };
        let client = TelemetryClient::new(config);
        assert!(!client.is_enabled());
        client.enable();
        assert!(client.is_enabled());
    }

    #[test]
    fn test_telemetry_client_device_id() {
        let config = TelemetryConfig::default();
        let expected_id = config.device_id.clone();
        let client = TelemetryClient::new(config);
        assert_eq!(client.device_id(), expected_id);
    }

    #[test]
    fn test_telemetry_client_track_when_disabled() {
        let config = TelemetryConfig {
            enabled: false,
            ..TelemetryConfig::default()
        };
        let client = TelemetryClient::new(config);

        // Should be a no-op when disabled
        client.track(TelemetryEvent::CommandRun {
            command: "test".to_string(),
            subcommand: None,
            duration_ms: 0,
            success: true,
        });

        // Events list should be empty
        let events = client.events.lock();
        assert!(events.is_empty());
    }

    #[test]
    fn test_telemetry_client_track_when_enabled() {
        let config = TelemetryConfig {
            enabled: true,
            batch_size: 100, // high batch size to avoid auto-flush
            ..TelemetryConfig::default()
        };
        let client = TelemetryClient::new(config);

        client.track(TelemetryEvent::CommandRun {
            command: "store".to_string(),
            subcommand: Some("model".to_string()),
            duration_ms: 150,
            success: true,
        });

        let events = client.events.lock();
        assert_eq!(events.len(), 1);
        assert!(!events[0].device_id.is_empty());
        assert!(!events[0].session_id.is_empty());
        assert!(events[0].timestamp > 0);
    }

    #[test]
    fn test_telemetry_client_flush_when_disabled() {
        let config = TelemetryConfig {
            enabled: false,
            ..TelemetryConfig::default()
        };
        let client = TelemetryClient::new(config);
        // Should not panic
        client.flush();
    }

    #[test]
    fn test_size_bucket() {
        // Test the size bucketing logic
        let check_bucket = |size: u64| -> &'static str {
            match size {
                0..=10_000_000 => "small",
                10_000_001..=100_000_000 => "medium",
                100_000_001..=1_000_000_000 => "large",
                _ => "xlarge",
            }
        };

        assert_eq!(check_bucket(1_000), "small");
        assert_eq!(check_bucket(50_000_000), "medium");
        assert_eq!(check_bucket(500_000_000), "large");
        assert_eq!(check_bucket(2_000_000_000), "xlarge");
    }

    #[test]
    fn test_event_serialization() {
        let event = TelemetryEvent::CommandRun {
            command: "store".to_string(),
            subcommand: None,
            duration_ms: 150,
            success: true,
        };

        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("command_run"));
        assert!(json.contains("store"));
    }

    #[test]
    fn test_event_app_start_serialization() {
        let event = TelemetryEvent::AppStart {
            version: "1.3.0".to_string(),
            os: "windows".to_string(),
            arch: "x86_64".to_string(),
            features: vec!["api".to_string(), "cloud".to_string()],
        };
        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("app_start"));
        assert!(json.contains("1.3.0"));
    }

    #[test]
    fn test_event_model_operation_serialization() {
        let event = TelemetryEvent::ModelOperation {
            operation: "store".to_string(),
            format: "safetensors".to_string(),
            size_bucket: "medium".to_string(),
            duration_ms: 3000,
            success: true,
        };
        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("model_operation"));
    }

    #[test]
    fn test_event_conversion_serialization() {
        let event = TelemetryEvent::Conversion {
            source_format: "pytorch".to_string(),
            target_format: "onnx".to_string(),
            duration_ms: 5000,
            success: false,
        };
        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("conversion"));
        assert!(json.contains("false"));
    }

    #[test]
    fn test_event_api_call_serialization() {
        let event = TelemetryEvent::ApiCall {
            endpoint: "/api/v1/models".to_string(),
            method: "GET".to_string(),
            status_code: 200,
            duration_ms: 50,
        };
        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("api_call"));
        assert!(json.contains("200"));
    }

    #[test]
    fn test_event_error_serialization() {
        let event = TelemetryEvent::Error {
            error_type: "CryptoError".to_string(),
            context: Some("decryption failed".to_string()),
        };
        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("error"));
        assert!(json.contains("CryptoError"));

        // Without context
        let event2 = TelemetryEvent::Error {
            error_type: "IoError".to_string(),
            context: None,
        };
        let json2 = serde_json::to_string(&event2).unwrap();
        assert!(!json2.contains("context"));
    }

    #[test]
    fn test_event_feature_used_serialization() {
        let event = TelemetryEvent::FeatureUsed {
            feature: "cloud_push".to_string(),
            detail: Some("s3".to_string()),
        };
        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("feature_used"));

        let event2 = TelemetryEvent::FeatureUsed {
            feature: "rag".to_string(),
            detail: None,
        };
        let json2 = serde_json::to_string(&event2).unwrap();
        assert!(!json2.contains("detail"));
    }

    #[test]
    fn test_telemetry_config_serialization_roundtrip() {
        let config = TelemetryConfig::default();
        let yaml = serde_yaml_ng::to_string(&config).unwrap();
        let deserialized: TelemetryConfig = serde_yaml_ng::from_str(&yaml).unwrap();
        assert_eq!(deserialized.batch_size, config.batch_size);
        assert_eq!(deserialized.flush_interval_secs, config.flush_interval_secs);
    }

    #[test]
    fn test_get_queue_file_path() {
        let path = TelemetryClient::get_queue_file_path();
        assert!(path.to_string_lossy().contains("telemetry"));
        assert!(path.to_string_lossy().contains("events.jsonl"));
    }

    #[test]
    fn test_tracking_timer() {
        let timer = TrackingTimer::new("test-cmd", Some("sub"));
        assert_eq!(timer.command, "test-cmd");
        assert_eq!(timer.subcommand.as_deref(), Some("sub"));
        // Don't call finish() since global telemetry isn't initialized
    }

    #[test]
    fn test_tracking_timer_no_subcommand() {
        let timer = TrackingTimer::new("list", None);
        assert!(timer.subcommand.is_none());
    }

    #[test]
    fn test_collect_enabled_features() {
        let features = collect_enabled_features();
        // Result depends on compile features, but should not panic
        assert!(features.len() <= 10);
    }

    #[test]
    fn test_global_is_enabled_without_init() {
        // Without initialization, should return false
        assert!(!is_enabled());
    }

    #[test]
    fn test_is_disabled_by_env_default() {
        // Without any env vars set, should return false
        // (env vars may or may not be set in CI, so just ensure no panic)
        let _ = TelemetryClient::is_disabled_by_env();
    }

    /// Configuring an exporter must never be what turns collection on.
    ///
    /// The two decisions are made by different people: an operator points the
    /// build at a collector, but whether this deployment reports at all is the
    /// opt-in. If setting `OTEL_EXPORTER_OTLP_ENDPOINT` silently enabled
    /// telemetry, every deployment that configured a collector for its own
    /// traces would start reporting here too.
    #[test]
    fn test_otlp_endpoint_alone_does_not_enable_telemetry() {
        let config = TelemetryConfig::default();
        assert!(!config.enabled, "precondition: default is opt-in");

        let client = TelemetryClient::new(config);
        assert!(
            !client.is_enabled(),
            "an OTLP endpoint configures where events go, not whether they are \
             collected"
        );
    }

    /// `DO_NOT_TRACK` and friends must still win when OTLP is configured.
    #[test]
    fn test_explicit_disable_still_wins_over_otlp_config() {
        let config = TelemetryConfig {
            enabled: true,
            ..Default::default()
        };

        // Simulate the operator's kill switch being set.
        let client = TelemetryClient::new(config);
        client.disable();

        assert!(
            !client.is_enabled(),
            "an explicit disable must not be overridden by exporter settings"
        );
    }

    #[test]
    fn test_telemetry_client_track_multiple_events() {
        let config = TelemetryConfig {
            enabled: true,
            batch_size: 100,
            ..TelemetryConfig::default()
        };
        let client = TelemetryClient::new(config);

        // Track several different event types
        client.track(TelemetryEvent::AppStart {
            version: "1.3.0".to_string(),
            os: "windows".to_string(),
            arch: "x86_64".to_string(),
            features: vec!["api".to_string()],
        });
        client.track(TelemetryEvent::CommandRun {
            command: "store".to_string(),
            subcommand: None,
            duration_ms: 100,
            success: true,
        });
        client.track(TelemetryEvent::Error {
            error_type: "IoError".to_string(),
            context: Some("test context".to_string()),
        });

        let events = client.events.lock();
        assert_eq!(events.len(), 3);
    }

    #[test]
    fn test_telemetry_client_flush_clears_events() {
        let config = TelemetryConfig {
            enabled: true,
            batch_size: 100,
            ..TelemetryConfig::default()
        };
        let client = TelemetryClient::new(config);

        client.track(TelemetryEvent::FeatureUsed {
            feature: "rag".to_string(),
            detail: None,
        });
        assert_eq!(client.events.lock().len(), 1);

        // Flush will try to send (and fail since no server), but events should be drained
        client.flush();
        // After flush, internal batch was taken; wait a moment for the spawned thread
        std::thread::sleep(std::time::Duration::from_millis(50));
        assert_eq!(client.events.lock().len(), 0);
    }

    #[test]
    fn test_telemetry_config_custom_values() {
        let config = TelemetryConfig {
            enabled: true,
            device_id: "custom-id".to_string(),
            endpoint: "https://custom.endpoint/v1".to_string(),
            batch_size: 10,
            flush_interval_secs: 60,
        };
        assert!(config.enabled);
        assert_eq!(config.device_id, "custom-id");
        assert_eq!(config.batch_size, 10);
        assert_eq!(config.flush_interval_secs, 60);
    }

    #[test]
    fn test_telemetry_envelope_fields() {
        let config = TelemetryConfig {
            enabled: true,
            batch_size: 100,
            ..TelemetryConfig::default()
        };
        let client = TelemetryClient::new(config);

        client.track(TelemetryEvent::Conversion {
            source_format: "pytorch".to_string(),
            target_format: "onnx".to_string(),
            duration_ms: 2000,
            success: true,
        });

        let events = client.events.lock();
        let env = &events[0];
        assert!(!env.device_id.is_empty());
        assert!(!env.session_id.is_empty());
        assert!(env.timestamp > 0);

        // Verify the event content via serialization
        let json = serde_json::to_string(&env.event).unwrap();
        assert!(json.contains("conversion"));
        assert!(json.contains("pytorch"));
    }

    #[test]
    fn test_tracking_timer_finish_does_not_panic() {
        // TrackingTimer::finish calls the global track(), which is a no-op
        // if no global client is initialized
        let timer = TrackingTimer::new("bench-cmd", Some("sub-a"));
        std::thread::sleep(std::time::Duration::from_millis(5));
        timer.finish(true);
    }

    #[test]
    fn test_tracking_timer_finish_failure() {
        let timer = TrackingTimer::new("failing-cmd", None);
        timer.finish(false);
    }

    #[test]
    fn test_global_track_noop_without_init() {
        // Global track() should silently no-op when TELEMETRY not initialized
        track(TelemetryEvent::FeatureUsed {
            feature: "test".to_string(),
            detail: None,
        });
    }

    #[test]
    fn test_global_flush_noop_without_init() {
        flush();
    }

    #[test]
    fn test_global_disable_noop_without_init() {
        disable();
    }

    #[test]
    fn test_convenience_track_functions_noop() {
        // All convenience functions should be no-ops when global not initialized
        track_app_start();
        track_command("test", Some("sub"), Duration::from_millis(10), true);
        track_command("test", None, Duration::from_millis(5), false);
        track_model_op(
            "store",
            "safetensors",
            1_000,
            Duration::from_millis(50),
            true,
        );
        track_model_op("get", "gguf", 500_000_000, Duration::from_millis(100), true);
        track_model_op(
            "store",
            "onnx",
            50_000_000,
            Duration::from_millis(75),
            false,
        );
        track_model_op(
            "store",
            "pytorch",
            2_000_000_000,
            Duration::from_millis(200),
            true,
        );
        track_conversion("pytorch", "onnx", Duration::from_secs(5), true);
        track_api_call("/models", "GET", 200, Duration::from_millis(30));
        track_error("TestError", Some("test context"));
        track_error("TestError", None);
        track_feature("rag", Some("search"));
        track_feature("cloud", None);
    }

    #[test]
    fn test_size_bucket_boundaries() {
        // Test exact boundary values for size bucketing
        let check_bucket = |size: u64| -> &'static str {
            match size {
                0..=10_000_000 => "small",
                10_000_001..=100_000_000 => "medium",
                100_000_001..=1_000_000_000 => "large",
                _ => "xlarge",
            }
        };

        assert_eq!(check_bucket(0), "small");
        assert_eq!(check_bucket(10_000_000), "small"); // upper boundary
        assert_eq!(check_bucket(10_000_001), "medium"); // lower boundary
        assert_eq!(check_bucket(100_000_000), "medium"); // upper boundary
        assert_eq!(check_bucket(100_000_001), "large"); // lower boundary
        assert_eq!(check_bucket(1_000_000_000), "large"); // upper boundary
        assert_eq!(check_bucket(1_000_000_001), "xlarge"); // lower boundary
    }

    #[test]
    fn test_telemetry_envelope_serialization_roundtrip() {
        let envelope = TelemetryEnvelope {
            device_id: "dev-1".to_string(),
            session_id: "sess-1".to_string(),
            timestamp: 1700000000,
            event: TelemetryEvent::ApiCall {
                endpoint: "/health".to_string(),
                method: "GET".to_string(),
                status_code: 200,
                duration_ms: 5,
            },
        };
        let json = serde_json::to_string(&envelope).unwrap();
        let d: TelemetryEnvelope = serde_json::from_str(&json).unwrap();
        assert_eq!(d.device_id, "dev-1");
        assert_eq!(d.session_id, "sess-1");
        assert_eq!(d.timestamp, 1700000000);
    }

    #[test]
    fn test_write_to_local_queue_and_read() {
        let dir = tempfile::tempdir().unwrap();
        let queue_path = dir.path().join("events.jsonl");

        // Manually test the write logic (without relying on the static path)
        use std::io::Write;
        let body = r#"[{"device_id":"d","session_id":"s","timestamp":1,"event":{"type":"feature_used","feature":"test"}}]"#;
        let mut f = std::fs::File::create(&queue_path).unwrap();
        writeln!(f, "{}", body).unwrap();
        drop(f);

        let contents = std::fs::read_to_string(&queue_path).unwrap();
        assert!(contents.contains("feature_used"));

        // Verify it parses as a batch
        for line in contents.lines() {
            if !line.trim().is_empty() {
                let batch: Vec<TelemetryEnvelope> = serde_json::from_str(line).unwrap();
                assert_eq!(batch.len(), 1);
            }
        }
    }

    #[test]
    fn test_load_or_create_config_new() {
        let dir = tempfile::tempdir().unwrap();
        let config_dir = dir.path().to_path_buf();
        let config = load_or_create_config(Some(&config_dir)).unwrap();
        assert!(!config.enabled); // default is opt-in disabled
        assert!(!config.device_id.is_empty());

        // Config file should have been created
        let config_path = config_dir.join("telemetry.yaml");
        assert!(config_path.exists());
    }

    #[test]
    fn test_load_or_create_config_existing() {
        let dir = tempfile::tempdir().unwrap();
        let config_dir = dir.path().to_path_buf();
        let config_path = config_dir.join("telemetry.yaml");

        // Write a custom config
        let custom = TelemetryConfig {
            enabled: true,
            device_id: "existing-device".to_string(),
            batch_size: 50,
            ..TelemetryConfig::default()
        };
        let yaml = serde_yaml_ng::to_string(&custom).unwrap();
        std::fs::write(&config_path, &yaml).unwrap();

        // Load it
        let loaded = load_or_create_config(Some(&config_dir)).unwrap();
        assert!(loaded.enabled);
        assert_eq!(loaded.device_id, "existing-device");
        assert_eq!(loaded.batch_size, 50);
    }

    #[test]
    fn test_init_and_track() {
        // Use a unique config to avoid interfering with global state
        // Note: TELEMETRY is a OnceLock so init() only works once per process.
        // This test verifies init doesn't panic when called (may be no-op if already set).
        let config = TelemetryConfig {
            enabled: false,
            ..TelemetryConfig::default()
        };
        init(config);
        // Global is_enabled reflects the client state
        // (but won't be true since we disabled it, and TELEMETRY_DISABLED may also be set)
    }

    #[test]
    fn test_default_helper_functions() {
        // Cover the default value functions
        assert!(!generate_device_id().is_empty());
        assert!(default_endpoint().contains("telemetry"));
        assert_eq!(default_batch_size(), 25);
        assert_eq!(default_flush_interval(), 300);
    }
}