synheart-sensor-agent 0.2.2

Privacy-first PC background sensor for behavioral research
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
//! HSI (Human State Interface) 1.0 compliant snapshot builder.
//!
//! This module creates JSON snapshots according to the HSI 1.0 specification.
//! Each snapshot represents a single time window of behavioral data.

use crate::core::features::WindowFeatures;
use crate::core::windowing::EventWindow;
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

/// The current HSI format version.
pub const HSI_VERSION: &str = "1.0";

/// The name of this producer.
pub const PRODUCER_NAME: &str = "synheart-sensor-agent";

// ============================================================================
// HSI 1.0 Compliant Types
// ============================================================================

/// HSI 1.0 axis reading direction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HsiDirection {
    /// A higher score indicates *more* of the measured quality (e.g., focus).
    HigherIsMore,
    /// A higher score indicates *less* of the measured quality (e.g., stress).
    HigherIsLess,
    /// The score is meaningful in both directions (e.g., HRV deviation).
    Bidirectional,
}

/// HSI 1.0 source type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HsiSourceType {
    /// Hardware sensor (e.g., keyboard/mouse hooks).
    Sensor,
    /// Software application.
    App,
    /// User self-report.
    SelfReport,
    /// External observer.
    Observer,
    /// Computed / derived from other sources.
    Derived,
    /// Unclassified source type.
    Other,
}

/// HSI 1.0 producer metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HsiProducer {
    /// Name of the producing software
    pub name: String,
    /// Version of the producing software
    pub version: String,
    /// Unique instance identifier (UUID)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instance_id: Option<String>,
}

/// HSI 1.0 window definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HsiWindow {
    /// Window start time (RFC3339)
    pub start: String,
    /// Window end time (RFC3339)
    pub end: String,
    /// Optional label for the window
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
}

/// HSI 1.0 axis reading
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HsiAxisReading {
    /// Axis name (lower_snake_case)
    pub axis: String,
    /// Score value (0-1) or null if unavailable
    pub score: Option<f64>,
    /// Confidence in the score (0-1)
    pub confidence: f64,
    /// Window ID this reading belongs to
    pub window_id: String,
    /// Direction semantics
    #[serde(skip_serializing_if = "Option::is_none")]
    pub direction: Option<HsiDirection>,
    /// Unit of measurement
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unit: Option<String>,
    /// Source IDs that contributed to this reading
    #[serde(skip_serializing_if = "Option::is_none")]
    pub evidence_source_ids: Option<Vec<String>>,
    /// Notes about this reading
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,
}

/// HSI 1.0 axes domain (contains readings array)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HsiAxesDomain {
    /// Axis readings
    pub readings: Vec<HsiAxisReading>,
}

/// HSI 1.0 axes container
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct HsiAxes {
    /// Affect domain readings
    #[serde(skip_serializing_if = "Option::is_none")]
    pub affect: Option<HsiAxesDomain>,
    /// Engagement domain readings
    #[serde(skip_serializing_if = "Option::is_none")]
    pub engagement: Option<HsiAxesDomain>,
    /// Behavior domain readings
    #[serde(skip_serializing_if = "Option::is_none")]
    pub behavior: Option<HsiAxesDomain>,
}

/// HSI 1.0 source definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HsiSource {
    /// Source type
    #[serde(rename = "type")]
    pub source_type: HsiSourceType,
    /// Quality of the source (0-1)
    pub quality: f64,
    /// Whether the source is degraded
    pub degraded: bool,
    /// Optional notes
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,
}

/// HSI 1.0 privacy declaration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HsiPrivacy {
    /// Must be false - HSI payloads must not contain PII
    pub contains_pii: bool,
    /// Whether raw biosignals are allowed
    pub raw_biosignals_allowed: bool,
    /// Whether derived metrics are allowed
    pub derived_metrics_allowed: bool,
    /// Notes about privacy
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,
}

impl Default for HsiPrivacy {
    fn default() -> Self {
        Self {
            contains_pii: false,
            raw_biosignals_allowed: false,
            derived_metrics_allowed: true,
            notes: Some(
                "No key content or coordinates captured - timing and magnitude only".to_string(),
            ),
        }
    }
}

/// HSI 1.0 compliant snapshot
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HsiSnapshot {
    /// HSI schema version (must be "1.0")
    pub hsi_version: String,
    /// When the human state was observed (RFC3339)
    pub observed_at_utc: String,
    /// When this payload was computed (RFC3339)
    pub computed_at_utc: String,
    /// Producer metadata
    pub producer: HsiProducer,
    /// Window identifiers
    pub window_ids: Vec<String>,
    /// Window definitions keyed by ID
    pub windows: HashMap<String, HsiWindow>,
    /// Source identifiers
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_ids: Option<Vec<String>>,
    /// Source definitions keyed by ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sources: Option<HashMap<String, HsiSource>>,
    /// Axis readings by domain
    #[serde(skip_serializing_if = "Option::is_none")]
    pub axes: Option<HsiAxes>,
    /// Privacy declaration
    pub privacy: HsiPrivacy,
    /// Additional metadata
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<HashMap<String, serde_json::Value>>,
}

/// Builder for creating HSI 1.0 compliant snapshots.
pub struct HsiBuilder {
    instance_id: Uuid,
    session_id: Option<String>,
}

impl HsiBuilder {
    /// Create a new HSI builder with a unique instance ID.
    pub fn new() -> Self {
        Self {
            instance_id: Uuid::new_v4(),
            session_id: None,
        }
    }

    /// Set the session ID for generated snapshots.
    pub fn with_session_id(mut self, session_id: String) -> Self {
        self.session_id = Some(session_id);
        self
    }

    /// Get the instance ID.
    pub fn instance_id(&self) -> Uuid {
        self.instance_id
    }

    /// Build an HSI 1.0 compliant snapshot from a window and its computed features.
    pub fn build(&self, window: &EventWindow, features: &WindowFeatures) -> HsiSnapshot {
        let computed_at = Utc::now();

        // Generate window ID
        let window_id = format!("w_{}", computed_at.timestamp_millis());

        // Build windows map
        let mut windows = HashMap::new();
        windows.insert(
            window_id.clone(),
            HsiWindow {
                start: window.start.to_rfc3339(),
                end: window.end.to_rfc3339(),
                label: if window.is_session_start {
                    Some("session_start".to_string())
                } else {
                    None
                },
            },
        );

        // Build source
        let source_id = format!("s_keyboard_mouse_{}", self.instance_id);
        let mut sources = HashMap::new();

        // Calculate quality based on event count
        let event_count = window.event_count();
        let quality = if event_count == 0 {
            0.0
        } else if event_count < 10 {
            0.5
        } else if event_count < 50 {
            0.75
        } else {
            0.95
        };

        sources.insert(
            source_id.clone(),
            HsiSource {
                source_type: HsiSourceType::Sensor,
                quality,
                degraded: event_count < 10,
                notes: if event_count < 10 {
                    Some("Low event count in window".to_string())
                } else {
                    None
                },
            },
        );

        // Calculate confidence based on data availability
        let confidence = quality * 0.9; // Slightly lower than quality

        // Build behavioral axis readings
        let behavior_readings = vec![
            // Typing rate (normalized to 0-1 by clamping to max 10 keys/sec)
            HsiAxisReading {
                axis: "typing_rate".to_string(),
                score: Some((features.keyboard.typing_rate / 10.0).min(1.0)),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsMore),
                unit: Some("keys_per_sec_normalized".to_string()),
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: None,
            },
            // Burst index (already 0-1)
            HsiAxisReading {
                axis: "typing_burstiness".to_string(),
                score: Some(features.keyboard.burst_index),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::Bidirectional),
                unit: None,
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: Some("Clustering of keystrokes".to_string()),
            },
            // Session continuity (already 0-1)
            HsiAxisReading {
                axis: "session_continuity".to_string(),
                score: Some(features.keyboard.session_continuity),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsMore),
                unit: None,
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: None,
            },
            // Idle ratio (already 0-1)
            HsiAxisReading {
                axis: "idle_ratio".to_string(),
                score: Some(features.mouse.idle_ratio),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsLess),
                unit: Some("ratio".to_string()),
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: None,
            },
            // Focus continuity proxy (already 0-1)
            HsiAxisReading {
                axis: "focus_continuity".to_string(),
                score: Some(features.behavioral.focus_continuity_proxy),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsMore),
                unit: None,
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: Some("Derived from typing and mouse patterns".to_string()),
            },
            // Interaction rhythm (already 0-1)
            HsiAxisReading {
                axis: "interaction_rhythm".to_string(),
                score: Some(features.behavioral.interaction_rhythm),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsMore),
                unit: None,
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: None,
            },
            // Motor stability (already 0-1)
            HsiAxisReading {
                axis: "motor_stability".to_string(),
                score: Some(features.behavioral.motor_stability),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsMore),
                unit: None,
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: None,
            },
            // Friction (already 0-1)
            HsiAxisReading {
                axis: "friction".to_string(),
                score: Some(features.behavioral.friction),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsMore),
                unit: None,
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: Some("Micro-adjustments and hesitation".to_string()),
            },
            // Typing cadence stability (already 0-1)
            HsiAxisReading {
                axis: "typing_cadence_stability".to_string(),
                score: Some(features.keyboard.typing_cadence_stability),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsMore),
                unit: None,
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: Some("Rhythmic consistency of typing".to_string()),
            },
            // Typing gap ratio (already 0-1)
            HsiAxisReading {
                axis: "typing_gap_ratio".to_string(),
                score: Some(features.keyboard.typing_gap_ratio),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsLess),
                unit: Some("ratio".to_string()),
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: Some("Proportion of inter-tap intervals classified as gaps".to_string()),
            },
            // Typing interaction intensity (already 0-1)
            HsiAxisReading {
                axis: "typing_interaction_intensity".to_string(),
                score: Some(features.keyboard.typing_interaction_intensity),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsMore),
                unit: None,
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: Some("Composite of speed, cadence stability, and gap behavior".to_string()),
            },
            // Keyboard scroll rate (normalized to 0-1, capped at 5 keys/sec)
            HsiAxisReading {
                axis: "keyboard_scroll_rate".to_string(),
                score: Some((features.keyboard.keyboard_scroll_rate / 5.0).min(1.0)),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsMore),
                unit: Some("nav_keys_per_sec_normalized".to_string()),
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: Some(
                    "Navigation keys (arrows, page up/down) - separate from mouse scroll"
                        .to_string(),
                ),
            },
            // Burstiness (already 0-1)
            HsiAxisReading {
                axis: "burstiness".to_string(),
                score: Some(features.behavioral.burstiness),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::Bidirectional),
                unit: None,
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: Some(
                    "Whether interactions occur in clusters (high) or evenly (low)".to_string(),
                ),
            },
            // Correction rate (0-1+, clamped to 1.0)
            HsiAxisReading {
                axis: "correction_rate".to_string(),
                score: Some(features.keyboard.correction_rate.min(1.0)),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsLess),
                unit: Some("ratio".to_string()),
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: Some("Ratio of correction keys to typing keys".to_string()),
            },
            // Typing efficiency (already 0-1)
            HsiAxisReading {
                axis: "typing_efficiency".to_string(),
                score: Some(features.keyboard.typing_efficiency),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsMore),
                unit: None,
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: Some("1.0 - correction_rate, clamped to 0-1".to_string()),
            },
            // Shortcut intensity (normalized to 0-1, capped at 2 shortcuts/sec)
            HsiAxisReading {
                axis: "shortcut_intensity".to_string(),
                score: Some((features.keyboard.shortcut_rate / 2.0).min(1.0)),
                confidence,
                window_id: window_id.clone(),
                direction: Some(HsiDirection::HigherIsMore),
                unit: Some("shortcuts_per_sec_normalized".to_string()),
                evidence_source_ids: Some(vec![source_id.clone()]),
                notes: Some("Rate of keyboard shortcuts (copy, paste, etc.)".to_string()),
            },
        ];

        // Build axes
        let axes = HsiAxes {
            affect: None,
            engagement: None,
            behavior: Some(HsiAxesDomain {
                readings: behavior_readings,
            }),
        };

        // Build metadata
        let mut meta = HashMap::new();
        meta.insert(
            "keyboard_events".to_string(),
            serde_json::Value::Number(serde_json::Number::from(window.keyboard_events.len())),
        );
        meta.insert(
            "mouse_events".to_string(),
            serde_json::Value::Number(serde_json::Number::from(window.mouse_events.len())),
        );
        meta.insert(
            "duration_secs".to_string(),
            serde_json::Value::Number(
                serde_json::Number::from_f64(window.duration_secs())
                    .unwrap_or(serde_json::Number::from(0)),
            ),
        );
        meta.insert(
            "is_session_start".to_string(),
            serde_json::Value::Bool(window.is_session_start),
        );
        if let Some(ref session_id) = self.session_id {
            meta.insert(
                "session_id".to_string(),
                serde_json::Value::String(session_id.clone()),
            );
        }
        if let Some(ref app_id) = window.app_id {
            meta.insert(
                "app_id".to_string(),
                serde_json::Value::String(app_id.clone()),
            );
        }
        // Include raw feature values in meta for transparency
        meta.insert(
            "raw_typing_rate".to_string(),
            serde_json::Value::Number(
                serde_json::Number::from_f64(features.keyboard.typing_rate)
                    .unwrap_or(serde_json::Number::from(0)),
            ),
        );
        meta.insert(
            "raw_mean_velocity".to_string(),
            serde_json::Value::Number(
                serde_json::Number::from_f64(features.mouse.mean_velocity)
                    .unwrap_or(serde_json::Number::from(0)),
            ),
        );
        meta.insert(
            "raw_click_rate".to_string(),
            serde_json::Value::Number(
                serde_json::Number::from_f64(features.mouse.click_rate)
                    .unwrap_or(serde_json::Number::from(0)),
            ),
        );
        meta.insert(
            "typing_tap_count".to_string(),
            serde_json::Value::Number(serde_json::Number::from(features.keyboard.typing_tap_count)),
        );
        meta.insert(
            "navigation_key_count".to_string(),
            serde_json::Value::Number(serde_json::Number::from(
                features.keyboard.navigation_key_count,
            )),
        );
        meta.insert(
            "keyboard_scroll_rate".to_string(),
            serde_json::Value::Number(
                serde_json::Number::from_f64(features.keyboard.keyboard_scroll_rate)
                    .unwrap_or(serde_json::Number::from(0)),
            ),
        );
        meta.insert(
            "idle_time_ms".to_string(),
            serde_json::Value::Number(serde_json::Number::from(features.mouse.idle_time_ms)),
        );
        meta.insert(
            "deep_focus_block".to_string(),
            serde_json::Value::Bool(features.behavioral.deep_focus_block),
        );
        meta.insert(
            "burstiness".to_string(),
            serde_json::Value::Number(
                serde_json::Number::from_f64(features.behavioral.burstiness)
                    .unwrap_or(serde_json::Number::from(0)),
            ),
        );
        meta.insert(
            "backspace_count".to_string(),
            serde_json::Value::Number(serde_json::Number::from(features.keyboard.backspace_count)),
        );
        meta.insert(
            "delete_count".to_string(),
            serde_json::Value::Number(serde_json::Number::from(features.keyboard.delete_count)),
        );
        meta.insert(
            "correction_rate".to_string(),
            serde_json::Value::Number(
                serde_json::Number::from_f64(features.keyboard.correction_rate)
                    .unwrap_or(serde_json::Number::from(0)),
            ),
        );
        meta.insert(
            "enter_count".to_string(),
            serde_json::Value::Number(serde_json::Number::from(features.keyboard.enter_count)),
        );
        meta.insert(
            "tab_count".to_string(),
            serde_json::Value::Number(serde_json::Number::from(features.keyboard.tab_count)),
        );
        meta.insert(
            "escape_count".to_string(),
            serde_json::Value::Number(serde_json::Number::from(features.keyboard.escape_count)),
        );
        meta.insert(
            "shortcut_count".to_string(),
            serde_json::Value::Number(serde_json::Number::from(features.keyboard.shortcut_count)),
        );
        meta.insert(
            "shortcut_rate".to_string(),
            serde_json::Value::Number(
                serde_json::Number::from_f64(features.keyboard.shortcut_rate)
                    .unwrap_or(serde_json::Number::from(0)),
            ),
        );

        HsiSnapshot {
            hsi_version: HSI_VERSION.to_string(),
            observed_at_utc: window.end.to_rfc3339(),
            computed_at_utc: computed_at.to_rfc3339(),
            producer: HsiProducer {
                name: PRODUCER_NAME.to_string(),
                version: env!("CARGO_PKG_VERSION").to_string(),
                instance_id: Some(self.instance_id.to_string()),
            },
            window_ids: vec![window_id],
            windows,
            source_ids: Some(vec![source_id]),
            sources: Some(sources),
            axes: Some(axes),
            privacy: HsiPrivacy::default(),
            meta: Some(meta),
        }
    }

    /// Build and serialize an HSI snapshot to JSON.
    pub fn build_json(&self, window: &EventWindow, features: &WindowFeatures) -> String {
        let snapshot = self.build(window, features);
        serde_json::to_string_pretty(&snapshot).unwrap_or_else(|_| "{}".to_string())
    }
}

impl Default for HsiBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::features::compute_features;
    use chrono::Duration;

    #[test]
    fn test_hsi_builder_instance_id() {
        let builder1 = HsiBuilder::new();
        let builder2 = HsiBuilder::new();
        assert_ne!(builder1.instance_id(), builder2.instance_id());
    }

    #[test]
    fn test_hsi_snapshot_creation() {
        let builder = HsiBuilder::new();
        let window = EventWindow::new(Utc::now(), Duration::seconds(10));
        let features = compute_features(&window);

        let snapshot = builder.build(&window, &features);

        assert_eq!(snapshot.hsi_version, HSI_VERSION);
        assert_eq!(snapshot.producer.name, PRODUCER_NAME);
        assert!(!snapshot.privacy.contains_pii);
        assert!(snapshot.privacy.derived_metrics_allowed);
    }

    #[test]
    fn test_hsi_1_0_compliance() {
        let builder = HsiBuilder::new();
        let window = EventWindow::new(Utc::now(), Duration::seconds(10));
        let features = compute_features(&window);

        let snapshot = builder.build(&window, &features);

        // Check required top-level fields
        assert_eq!(snapshot.hsi_version, "1.0");
        assert!(!snapshot.observed_at_utc.is_empty());
        assert!(!snapshot.computed_at_utc.is_empty());
        assert!(!snapshot.window_ids.is_empty());
        assert!(!snapshot.windows.is_empty());

        // Check window_ids match windows keys
        for id in &snapshot.window_ids {
            assert!(snapshot.windows.contains_key(id));
        }

        // Check privacy constraints
        assert!(!snapshot.privacy.contains_pii);

        // Check axes structure
        let axes = snapshot.axes.as_ref().unwrap();
        let behavior = axes.behavior.as_ref().unwrap();
        assert!(!behavior.readings.is_empty());

        // Check each reading has required fields
        for reading in &behavior.readings {
            assert!(!reading.axis.is_empty());
            assert!(reading.confidence >= 0.0 && reading.confidence <= 1.0);
            assert!(!reading.window_id.is_empty());
            if let Some(score) = reading.score {
                assert!((0.0..=1.0).contains(&score), "score out of range: {score}");
            }
        }
    }

    #[test]
    fn test_hsi_json_serialization() {
        let builder = HsiBuilder::new();
        let window = EventWindow::new(Utc::now(), Duration::seconds(10));
        let features = compute_features(&window);

        let json = builder.build_json(&window, &features);

        // Verify JSON contains required fields
        assert!(json.contains("hsi_version"));
        assert!(json.contains("observed_at_utc"));
        assert!(json.contains("computed_at_utc"));
        assert!(json.contains("producer"));
        assert!(json.contains("window_ids"));
        assert!(json.contains("windows"));
        assert!(json.contains("privacy"));
        assert!(json.contains("contains_pii"));
    }

    #[test]
    fn test_source_quality_calculation() {
        let builder = HsiBuilder::new();
        let window = EventWindow::new(Utc::now(), Duration::seconds(10));
        let features = compute_features(&window);

        let snapshot = builder.build(&window, &features);

        let sources = snapshot.sources.as_ref().unwrap();
        let source = sources.values().next().unwrap();

        // Empty window should have low quality and be degraded
        assert!(source.quality < 0.5);
        assert!(source.degraded);
    }

    #[test]
    fn test_hsi_meta_includes_app_id_when_present() {
        let builder = HsiBuilder::new();
        let mut window = EventWindow::new(Utc::now(), Duration::seconds(10));
        window.app_id = Some("com.test.App".to_string());
        let features = compute_features(&window);

        let snapshot = builder.build(&window, &features);
        let meta = snapshot.meta.as_ref().unwrap();

        assert_eq!(
            meta.get("app_id"),
            Some(&serde_json::Value::String("com.test.App".to_string()))
        );
    }

    #[test]
    fn test_hsi_meta_excludes_app_id_when_none() {
        let builder = HsiBuilder::new();
        let window = EventWindow::new(Utc::now(), Duration::seconds(10));
        let features = compute_features(&window);

        let snapshot = builder.build(&window, &features);
        let meta = snapshot.meta.as_ref().unwrap();

        assert!(!meta.contains_key("app_id"));
    }
}