mielin-wasm 0.1.0-rc.1

WebAssembly sandboxing and execution runtime for agent cells using Wasmtime
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
//! System Host Functions for WASM Agents
//!
//! Provides system-level functionality to WASM agents:
//! - Time and date functions
//! - Random number generation
//! - Environment queries
//! - Process information

use std::sync::{Arc, RwLock};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use wasmtime::{Caller, Linker};

/// System state accessible to WASM agents
#[derive(Clone)]
pub struct SystemState {
    /// Monotonic clock offset from process start
    start_time: std::time::Instant,
    /// Random number generator state (xorshift128+)
    rng_state: Arc<RwLock<XorShift128Plus>>,
    /// Environment variables accessible to the agent
    environment: Arc<RwLock<EnvironmentStore>>,
    /// Process info
    process_info: ProcessInfo,
}

impl SystemState {
    pub fn new() -> Self {
        Self {
            start_time: std::time::Instant::now(),
            rng_state: Arc::new(RwLock::new(XorShift128Plus::new())),
            environment: Arc::new(RwLock::new(EnvironmentStore::new())),
            process_info: ProcessInfo::new(),
        }
    }

    /// Create with a specific seed for deterministic random numbers
    pub fn with_seed(seed: u64) -> Self {
        Self {
            start_time: std::time::Instant::now(),
            rng_state: Arc::new(RwLock::new(XorShift128Plus::with_seed(seed))),
            environment: Arc::new(RwLock::new(EnvironmentStore::new())),
            process_info: ProcessInfo::new(),
        }
    }

    /// Get current timestamp in nanoseconds since UNIX epoch
    pub fn timestamp_nanos(&self) -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::ZERO)
            .as_nanos() as u64
    }

    /// Get current timestamp in milliseconds since UNIX epoch
    pub fn timestamp_millis(&self) -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::ZERO)
            .as_millis() as u64
    }

    /// Get current timestamp in seconds since UNIX epoch
    pub fn timestamp_secs(&self) -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::ZERO)
            .as_secs()
    }

    /// Get monotonic time in nanoseconds since process start
    pub fn monotonic_nanos(&self) -> u64 {
        self.start_time.elapsed().as_nanos() as u64
    }

    /// Get monotonic time in milliseconds since process start
    pub fn monotonic_millis(&self) -> u64 {
        self.start_time.elapsed().as_millis() as u64
    }

    /// Generate a random u64
    pub fn random_u64(&self) -> u64 {
        self.rng_state
            .write()
            .expect("RNG state lock poisoned")
            .next_u64()
    }

    /// Generate a random f64 in range [0.0, 1.0)
    pub fn random_f64(&self) -> f64 {
        let bits = self.random_u64();
        // Use high 53 bits for full f64 precision
        (bits >> 11) as f64 * (1.0 / (1u64 << 53) as f64)
    }

    /// Fill buffer with random bytes
    pub fn random_bytes(&self, buffer: &mut [u8]) {
        let mut rng = self.rng_state.write().expect("RNG state lock poisoned");
        let mut remaining = buffer.len();
        let mut offset = 0;

        while remaining > 0 {
            let value = rng.next_u64();
            let bytes = value.to_le_bytes();
            let copy_len = remaining.min(8);
            buffer[offset..offset + copy_len].copy_from_slice(&bytes[..copy_len]);
            offset += copy_len;
            remaining -= copy_len;
        }
    }

    /// Get environment store
    pub fn environment(&self) -> &Arc<RwLock<EnvironmentStore>> {
        &self.environment
    }

    /// Get process info
    pub fn process_info(&self) -> &ProcessInfo {
        &self.process_info
    }
}

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

/// XorShift128+ random number generator
/// Fast, high-quality PRNG suitable for simulation/games
/// Period: 2^128 - 1
pub struct XorShift128Plus {
    state: [u64; 2],
}

impl XorShift128Plus {
    /// Create a new RNG seeded from system time
    pub fn new() -> Self {
        let seed = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::ZERO)
            .as_nanos() as u64;
        Self::with_seed(seed)
    }

    /// Create a new RNG with a specific seed
    pub fn with_seed(seed: u64) -> Self {
        // Use splitmix64 to initialize state from a single seed
        let mut state = [0u64; 2];
        let mut x = seed;

        for s in &mut state {
            x = x.wrapping_add(0x9e3779b97f4a7c15);
            let mut z = x;
            z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb);
            *s = z ^ (z >> 31);
        }

        // Ensure non-zero state
        if state[0] == 0 && state[1] == 0 {
            state[0] = 1;
        }

        Self { state }
    }

    /// Generate the next random u64
    pub fn next_u64(&mut self) -> u64 {
        let mut s1 = self.state[0];
        let s0 = self.state[1];
        let result = s0.wrapping_add(s1);

        self.state[0] = s0;
        s1 ^= s1 << 23;
        self.state[1] = s1 ^ s0 ^ (s1 >> 17) ^ (s0 >> 26);

        result
    }

    /// Jump forward by 2^64 steps
    /// Useful for creating independent streams
    pub fn jump(&mut self) {
        const JUMP: [u64; 2] = [0xdf900294d8f554a5, 0x170865df4b3201fc];

        let mut s0 = 0u64;
        let mut s1 = 0u64;

        for j in JUMP {
            for b in 0..64 {
                if (j >> b) & 1 != 0 {
                    s0 ^= self.state[0];
                    s1 ^= self.state[1];
                }
                self.next_u64();
            }
        }

        self.state[0] = s0;
        self.state[1] = s1;
    }
}

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

/// Environment variable store for WASM agents
pub struct EnvironmentStore {
    vars: std::collections::HashMap<String, String>,
}

impl EnvironmentStore {
    pub fn new() -> Self {
        Self {
            vars: std::collections::HashMap::new(),
        }
    }

    /// Set an environment variable
    pub fn set(&mut self, key: impl Into<String>, value: impl Into<String>) {
        self.vars.insert(key.into(), value.into());
    }

    /// Get an environment variable
    pub fn get(&self, key: &str) -> Option<&String> {
        self.vars.get(key)
    }

    /// Remove an environment variable
    pub fn remove(&mut self, key: &str) -> Option<String> {
        self.vars.remove(key)
    }

    /// Check if a key exists
    pub fn contains(&self, key: &str) -> bool {
        self.vars.contains_key(key)
    }

    /// Get all keys
    pub fn keys(&self) -> impl Iterator<Item = &String> {
        self.vars.keys()
    }

    /// Get number of variables
    pub fn len(&self) -> usize {
        self.vars.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.vars.is_empty()
    }

    /// Clear all variables
    pub fn clear(&mut self) {
        self.vars.clear();
    }
}

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

/// Process information available to WASM agents
#[derive(Clone)]
pub struct ProcessInfo {
    /// Unique identifier for this runtime instance
    pub instance_id: u64,
    /// Platform identifier
    pub platform: Platform,
    /// Architecture identifier
    pub arch: Architecture,
    /// Pointer size in bits (32 or 64)
    pub pointer_bits: u32,
    /// Number of logical CPUs available
    pub cpu_count: u32,
    /// Total memory available in bytes (if known)
    pub memory_total: Option<u64>,
}

impl ProcessInfo {
    pub fn new() -> Self {
        // Generate instance ID from time
        let instance_id = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::ZERO)
            .as_nanos() as u64;

        Self {
            instance_id,
            platform: Platform::current(),
            arch: Architecture::current(),
            pointer_bits: std::mem::size_of::<usize>() as u32 * 8,
            cpu_count: std::thread::available_parallelism()
                .map(|n| n.get() as u32)
                .unwrap_or(1),
            memory_total: None, // Would require platform-specific code
        }
    }
}

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

/// Platform identifier
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum Platform {
    Unknown = 0,
    Linux = 1,
    MacOS = 2,
    Windows = 3,
    FreeBSD = 4,
    Android = 5,
    IOS = 6,
    WASM = 7,
}

impl Platform {
    pub fn current() -> Self {
        #[cfg(target_os = "linux")]
        {
            Platform::Linux
        }
        #[cfg(target_os = "macos")]
        {
            Platform::MacOS
        }
        #[cfg(target_os = "windows")]
        {
            Platform::Windows
        }
        #[cfg(target_os = "freebsd")]
        {
            Platform::FreeBSD
        }
        #[cfg(target_os = "android")]
        {
            Platform::Android
        }
        #[cfg(target_os = "ios")]
        {
            Platform::IOS
        }
        #[cfg(all(
            not(target_os = "linux"),
            not(target_os = "macos"),
            not(target_os = "windows"),
            not(target_os = "freebsd"),
            not(target_os = "android"),
            not(target_os = "ios")
        ))]
        {
            Platform::Unknown
        }
    }
}

/// Architecture identifier
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum Architecture {
    Unknown = 0,
    X86 = 1,
    X86_64 = 2,
    ARM = 3,
    AArch64 = 4,
    RISCV32 = 5,
    RISCV64 = 6,
    WASM32 = 7,
    WASM64 = 8,
}

impl Architecture {
    pub fn current() -> Self {
        #[cfg(target_arch = "x86")]
        {
            Architecture::X86
        }
        #[cfg(target_arch = "x86_64")]
        {
            Architecture::X86_64
        }
        #[cfg(target_arch = "arm")]
        {
            Architecture::ARM
        }
        #[cfg(target_arch = "aarch64")]
        {
            Architecture::AArch64
        }
        #[cfg(target_arch = "riscv32")]
        {
            Architecture::RISCV32
        }
        #[cfg(target_arch = "riscv64")]
        {
            Architecture::RISCV64
        }
        #[cfg(target_arch = "wasm32")]
        {
            Architecture::WASM32
        }
        #[cfg(target_arch = "wasm64")]
        {
            Architecture::WASM64
        }
        #[cfg(all(
            not(target_arch = "x86"),
            not(target_arch = "x86_64"),
            not(target_arch = "arm"),
            not(target_arch = "aarch64"),
            not(target_arch = "riscv32"),
            not(target_arch = "riscv64"),
            not(target_arch = "wasm32"),
            not(target_arch = "wasm64")
        ))]
        {
            Architecture::Unknown
        }
    }
}

/// System host functions for WASM registration
pub struct SystemHostFunctions;

impl SystemHostFunctions {
    /// Register all system host functions with the Wasmtime linker
    pub fn register<T>(linker: &mut Linker<T>) -> anyhow::Result<()>
    where
        T: HasSystemState + 'static,
    {
        // Time functions
        Self::register_time_functions(linker)?;
        // Random functions
        Self::register_random_functions(linker)?;
        // Environment functions
        Self::register_env_functions(linker)?;
        // Process functions
        Self::register_process_functions(linker)?;

        Ok(())
    }

    fn register_time_functions<T>(linker: &mut Linker<T>) -> anyhow::Result<()>
    where
        T: HasSystemState + 'static,
    {
        // Get current time in nanoseconds since UNIX epoch
        linker.func_wrap("mielin", "time_now_nanos", |caller: Caller<'_, T>| -> i64 {
            caller.data().system_state().timestamp_nanos() as i64
        })?;

        // Get current time in milliseconds since UNIX epoch
        linker.func_wrap(
            "mielin",
            "time_now_millis",
            |caller: Caller<'_, T>| -> i64 {
                caller.data().system_state().timestamp_millis() as i64
            },
        )?;

        // Get current time in seconds since UNIX epoch
        linker.func_wrap("mielin", "time_now_secs", |caller: Caller<'_, T>| -> i64 {
            caller.data().system_state().timestamp_secs() as i64
        })?;

        // Get monotonic time in nanoseconds (for elapsed time measurement)
        linker.func_wrap(
            "mielin",
            "time_monotonic_nanos",
            |caller: Caller<'_, T>| -> i64 {
                caller.data().system_state().monotonic_nanos() as i64
            },
        )?;

        // Get monotonic time in milliseconds
        linker.func_wrap(
            "mielin",
            "time_monotonic_millis",
            |caller: Caller<'_, T>| -> i64 {
                caller.data().system_state().monotonic_millis() as i64
            },
        )?;

        Ok(())
    }

    fn register_random_functions<T>(linker: &mut Linker<T>) -> anyhow::Result<()>
    where
        T: HasSystemState + 'static,
    {
        // Generate random u32
        linker.func_wrap("mielin", "random_u32", |caller: Caller<'_, T>| -> i32 {
            (caller.data().system_state().random_u64() & 0xFFFFFFFF) as i32
        })?;

        // Generate random u64 (returns as two i32s: low, high)
        linker.func_wrap("mielin", "random_u64_low", |caller: Caller<'_, T>| -> i32 {
            // Store the full value and return low bits
            (caller.data().system_state().random_u64() & 0xFFFFFFFF) as i32
        })?;

        linker.func_wrap(
            "mielin",
            "random_u64_high",
            |caller: Caller<'_, T>| -> i32 {
                (caller.data().system_state().random_u64() >> 32) as i32
            },
        )?;

        // Generate random f32 in [0.0, 1.0)
        linker.func_wrap("mielin", "random_f32", |caller: Caller<'_, T>| -> f32 {
            caller.data().system_state().random_f64() as f32
        })?;

        // Generate random f64 in [0.0, 1.0)
        linker.func_wrap("mielin", "random_f64", |caller: Caller<'_, T>| -> f64 {
            caller.data().system_state().random_f64()
        })?;

        // Fill memory with random bytes
        // Returns number of bytes written, or 0 on error
        linker.func_wrap(
            "mielin",
            "random_bytes",
            |mut caller: Caller<'_, T>, ptr: i32, len: i32| -> i32 {
                if len <= 0 {
                    return 0;
                }

                let offset = ptr as usize;
                let length = len as usize;

                // First generate the random bytes
                let random_data: Vec<u8> = {
                    let mut rng_state = caller
                        .data()
                        .system_state()
                        .rng_state
                        .write()
                        .expect("RNG state lock poisoned");

                    let mut buffer = vec![0u8; length];
                    let mut remaining = length;
                    let mut current = 0;

                    while remaining > 0 {
                        let value = rng_state.next_u64();
                        let bytes = value.to_le_bytes();
                        let copy_len = remaining.min(8);
                        buffer[current..current + copy_len].copy_from_slice(&bytes[..copy_len]);
                        current += copy_len;
                        remaining -= copy_len;
                    }
                    buffer
                };

                // Then copy to WASM memory
                let memory = match caller.get_export("memory") {
                    Some(wasmtime::Extern::Memory(mem)) => mem,
                    _ => return 0,
                };

                let data = memory.data_mut(&mut caller);
                if offset + length > data.len() {
                    return 0;
                }

                data[offset..offset + length].copy_from_slice(&random_data);
                len
            },
        )?;

        Ok(())
    }

    fn register_env_functions<T>(linker: &mut Linker<T>) -> anyhow::Result<()>
    where
        T: HasSystemState + 'static,
    {
        // Get environment variable length
        // Returns -1 if not found, otherwise length of value
        linker.func_wrap(
            "mielin",
            "env_get_len",
            |mut caller: Caller<'_, T>, key_ptr: i32, key_len: i32| -> i32 {
                let memory = match caller.get_export("memory") {
                    Some(wasmtime::Extern::Memory(mem)) => mem,
                    _ => return -1,
                };

                let key_offset = key_ptr as usize;
                let key_length = key_len as usize;

                let data = memory.data(&caller);
                if key_offset + key_length > data.len() {
                    return -1;
                }

                let key = match std::str::from_utf8(&data[key_offset..key_offset + key_length]) {
                    Ok(k) => k,
                    Err(_) => return -1,
                };

                let env = caller
                    .data()
                    .system_state()
                    .environment()
                    .read()
                    .expect("Environment lock poisoned");
                match env.get(key) {
                    Some(value) => value.len() as i32,
                    None => -1,
                }
            },
        )?;

        // Get environment variable value
        // Returns bytes written, or -1 on error
        linker.func_wrap(
            "mielin",
            "env_get",
            |mut caller: Caller<'_, T>,
             key_ptr: i32,
             key_len: i32,
             value_ptr: i32,
             value_max_len: i32|
             -> i32 {
                let memory = match caller.get_export("memory") {
                    Some(wasmtime::Extern::Memory(mem)) => mem,
                    _ => return -1,
                };

                let key_offset = key_ptr as usize;
                let key_length = key_len as usize;

                let data = memory.data(&caller);
                if key_offset + key_length > data.len() {
                    return -1;
                }

                let key = match std::str::from_utf8(&data[key_offset..key_offset + key_length]) {
                    Ok(k) => k.to_string(),
                    Err(_) => return -1,
                };

                let value = {
                    let env = caller
                        .data()
                        .system_state()
                        .environment()
                        .read()
                        .expect("Environment lock poisoned");
                    match env.get(&key) {
                        Some(v) => v.clone(),
                        None => return -1,
                    }
                };

                let value_offset = value_ptr as usize;
                let value_max = value_max_len as usize;

                let data = memory.data_mut(&mut caller);
                if value_offset + value_max > data.len() {
                    return -1;
                }

                let copy_len = value.len().min(value_max);
                data[value_offset..value_offset + copy_len]
                    .copy_from_slice(&value.as_bytes()[..copy_len]);

                copy_len as i32
            },
        )?;

        // Check if environment variable exists
        linker.func_wrap(
            "mielin",
            "env_contains",
            |mut caller: Caller<'_, T>, key_ptr: i32, key_len: i32| -> i32 {
                let memory = match caller.get_export("memory") {
                    Some(wasmtime::Extern::Memory(mem)) => mem,
                    _ => return 0,
                };

                let key_offset = key_ptr as usize;
                let key_length = key_len as usize;

                let data = memory.data(&caller);
                if key_offset + key_length > data.len() {
                    return 0;
                }

                let key = match std::str::from_utf8(&data[key_offset..key_offset + key_length]) {
                    Ok(k) => k,
                    Err(_) => return 0,
                };

                let env = caller
                    .data()
                    .system_state()
                    .environment()
                    .read()
                    .expect("Environment lock poisoned");
                env.contains(key) as i32
            },
        )?;

        // Get number of environment variables
        linker.func_wrap("mielin", "env_count", |caller: Caller<'_, T>| -> i32 {
            let env = caller
                .data()
                .system_state()
                .environment()
                .read()
                .expect("Environment lock poisoned");
            env.len() as i32
        })?;

        Ok(())
    }

    fn register_process_functions<T>(linker: &mut Linker<T>) -> anyhow::Result<()>
    where
        T: HasSystemState + 'static,
    {
        // Get runtime instance ID (low 32 bits)
        linker.func_wrap(
            "mielin",
            "process_instance_id_low",
            |caller: Caller<'_, T>| -> i32 {
                (caller.data().system_state().process_info().instance_id & 0xFFFFFFFF) as i32
            },
        )?;

        // Get runtime instance ID (high 32 bits)
        linker.func_wrap(
            "mielin",
            "process_instance_id_high",
            |caller: Caller<'_, T>| -> i32 {
                (caller.data().system_state().process_info().instance_id >> 32) as i32
            },
        )?;

        // Get platform identifier
        linker.func_wrap(
            "mielin",
            "process_platform",
            |caller: Caller<'_, T>| -> i32 {
                caller.data().system_state().process_info().platform as i32
            },
        )?;

        // Get architecture identifier
        linker.func_wrap("mielin", "process_arch", |caller: Caller<'_, T>| -> i32 {
            caller.data().system_state().process_info().arch as i32
        })?;

        // Get pointer size in bits (32 or 64)
        linker.func_wrap(
            "mielin",
            "process_pointer_bits",
            |caller: Caller<'_, T>| -> i32 {
                caller.data().system_state().process_info().pointer_bits as i32
            },
        )?;

        // Get number of logical CPUs
        linker.func_wrap(
            "mielin",
            "process_cpu_count",
            |caller: Caller<'_, T>| -> i32 {
                caller.data().system_state().process_info().cpu_count as i32
            },
        )?;

        // Get total memory in MB (or -1 if unknown)
        linker.func_wrap(
            "mielin",
            "process_memory_total_mb",
            |caller: Caller<'_, T>| -> i32 {
                match caller.data().system_state().process_info().memory_total {
                    Some(bytes) => (bytes / (1024 * 1024)) as i32,
                    None => -1,
                }
            },
        )?;

        Ok(())
    }
}

/// Trait for types that contain a SystemState
pub trait HasSystemState {
    fn system_state(&self) -> &SystemState;
}

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

    #[test]
    fn test_xorshift_deterministic() {
        let mut rng1 = XorShift128Plus::with_seed(12345);
        let mut rng2 = XorShift128Plus::with_seed(12345);

        for _ in 0..100 {
            assert_eq!(rng1.next_u64(), rng2.next_u64());
        }
    }

    #[test]
    fn test_xorshift_different_seeds() {
        let mut rng1 = XorShift128Plus::with_seed(1);
        let mut rng2 = XorShift128Plus::with_seed(2);

        // Should produce different sequences
        let seq1: Vec<u64> = (0..10).map(|_| rng1.next_u64()).collect();
        let seq2: Vec<u64> = (0..10).map(|_| rng2.next_u64()).collect();

        assert_ne!(seq1, seq2);
    }

    #[test]
    fn test_xorshift_non_zero() {
        let mut rng = XorShift128Plus::with_seed(0);

        // Even with seed 0, should produce non-zero values
        let mut has_nonzero = false;
        for _ in 0..100 {
            if rng.next_u64() != 0 {
                has_nonzero = true;
                break;
            }
        }
        assert!(has_nonzero);
    }

    #[test]
    fn test_xorshift_jump() {
        let mut rng1 = XorShift128Plus::with_seed(42);
        let mut rng2 = XorShift128Plus::with_seed(42);

        // Advance rng1 by 2^64 steps
        rng1.jump();

        // rng1 and rng2 should produce different sequences now
        assert_ne!(rng1.next_u64(), rng2.next_u64());
    }

    #[test]
    fn test_system_state_timestamp() {
        let state = SystemState::new();

        let ts1 = state.timestamp_millis();
        std::thread::sleep(std::time::Duration::from_millis(10));
        let ts2 = state.timestamp_millis();

        assert!(ts2 > ts1);
    }

    #[test]
    fn test_system_state_monotonic() {
        let state = SystemState::new();

        let m1 = state.monotonic_nanos();
        std::thread::sleep(std::time::Duration::from_millis(1));
        let m2 = state.monotonic_nanos();

        assert!(m2 > m1);
    }

    #[test]
    fn test_system_state_random_f64_range() {
        let state = SystemState::with_seed(999);

        for _ in 0..1000 {
            let val = state.random_f64();
            assert!(val >= 0.0, "Value {} should be >= 0.0", val);
            assert!(val < 1.0, "Value {} should be < 1.0", val);
        }
    }

    #[test]
    fn test_system_state_random_bytes() {
        let state = SystemState::with_seed(777);

        let mut buffer = [0u8; 32];
        state.random_bytes(&mut buffer);

        // Should not be all zeros
        assert!(buffer.iter().any(|&b| b != 0));
    }

    #[test]
    fn test_environment_store() {
        let mut env = EnvironmentStore::new();

        assert!(env.is_empty());

        env.set("KEY1", "value1");
        env.set("KEY2", "value2");

        assert_eq!(env.len(), 2);
        assert!(env.contains("KEY1"));
        assert_eq!(env.get("KEY1"), Some(&"value1".to_string()));

        let removed = env.remove("KEY1");
        assert_eq!(removed, Some("value1".to_string()));
        assert!(!env.contains("KEY1"));
    }

    #[test]
    fn test_platform_detection() {
        let platform = Platform::current();

        // Should be one of the known platforms
        match platform {
            Platform::Linux
            | Platform::MacOS
            | Platform::Windows
            | Platform::FreeBSD
            | Platform::Android
            | Platform::IOS
            | Platform::WASM
            | Platform::Unknown => {}
        }
    }

    #[test]
    fn test_architecture_detection() {
        let arch = Architecture::current();

        // Should be one of the known architectures
        match arch {
            Architecture::X86
            | Architecture::X86_64
            | Architecture::ARM
            | Architecture::AArch64
            | Architecture::RISCV32
            | Architecture::RISCV64
            | Architecture::WASM32
            | Architecture::WASM64
            | Architecture::Unknown => {}
        }
    }

    #[test]
    fn test_process_info() {
        let info = ProcessInfo::new();

        assert!(info.cpu_count >= 1);
        assert!(info.pointer_bits == 32 || info.pointer_bits == 64);
    }

    #[test]
    fn test_system_state_with_seed() {
        let state1 = SystemState::with_seed(12345);
        let state2 = SystemState::with_seed(12345);

        // Same seed should produce same random sequence
        assert_eq!(state1.random_u64(), state2.random_u64());
    }

    #[test]
    fn test_random_u64_distribution() {
        let state = SystemState::with_seed(54321);

        let mut low_count = 0;
        let mut high_count = 0;
        let midpoint = u64::MAX / 2;

        for _ in 0..1000 {
            let val = state.random_u64();
            if val < midpoint {
                low_count += 1;
            } else {
                high_count += 1;
            }
        }

        // Should be roughly evenly distributed (within 30%)
        let ratio = low_count as f64 / high_count as f64;
        assert!(
            ratio > 0.7 && ratio < 1.3,
            "Distribution ratio {} seems skewed",
            ratio
        );
    }

    #[test]
    fn test_environment_clear() {
        let mut env = EnvironmentStore::new();
        env.set("A", "1");
        env.set("B", "2");

        assert_eq!(env.len(), 2);
        env.clear();
        assert!(env.is_empty());
    }

    #[test]
    fn test_environment_keys() {
        let mut env = EnvironmentStore::new();
        env.set("KEY_A", "val_a");
        env.set("KEY_B", "val_b");

        let keys: Vec<&String> = env.keys().collect();
        assert_eq!(keys.len(), 2);
        assert!(keys.iter().any(|k| *k == "KEY_A"));
        assert!(keys.iter().any(|k| *k == "KEY_B"));
    }
}