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
//! Effect sandbox configuration and management.
use crate::effects::Effect;
use std::thread::ThreadId;
use std::time::{SystemTime, Duration};
/// Configuration for an effect sandbox.
#[derive(Debug, Clone)]
pub struct EffectSandboxConfig {
/// Maximum number of effects allowed in sandbox
pub max_effects: usize,
/// Timeout for sandbox operations
pub timeout: Duration,
/// Allowed effect types in sandbox
pub allowed_effects: Vec<Effect>,
/// Whether sandbox should auto-cleanup on completion
pub auto_cleanup: bool,
/// Resource limits for sandbox
pub resource_limits: SandboxResourceLimits,
}
/// Resource limits for a sandbox.
#[derive(Debug, Clone)]
pub struct SandboxResourceLimits {
/// Maximum memory usage (bytes)
pub max_memory: Option<usize>,
/// Maximum execution time
pub max_execution_time: Option<Duration>,
/// Maximum file operations
pub max_file_operations: Option<usize>,
/// Maximum network operations
pub max_network_operations: Option<usize>,
}
/// Handle to an effect sandbox.
#[derive(Debug)]
pub struct EffectSandboxHandle {
/// Unique sandbox ID
pub id: u64,
/// Thread this sandbox belongs to
pub thread_id: ThreadId,
/// Sandbox configuration
pub config: EffectSandboxConfig,
/// When sandbox was created
pub created_at: SystemTime,
/// Weak reference to coordinator (forward declaration to avoid circular dependency)
pub coordinator: std::sync::Weak<()>,
}
/// Statistics for a specific sandbox.
#[derive(Debug, Clone)]
pub struct SandboxStatistics {
/// Sandbox ID
pub id: u64,
/// Associated thread
pub thread_id: ThreadId,
/// How long sandbox has been active
pub uptime: Duration,
/// Number of effects executed
pub effect_count: usize,
/// Current resource usage
pub resource_usage: ResourceUsage,
}
/// Resource usage information.
#[derive(Debug, Clone, Default)]
pub struct ResourceUsage {
/// Memory usage in bytes
pub memory_bytes: usize,
/// Execution time
pub execution_time: Duration,
/// Number of file operations
pub file_operations: usize,
/// Number of network operations
pub network_operations: usize,
}
impl Default for EffectSandboxConfig {
fn default() -> Self {
Self {
max_effects: 100,
timeout: Duration::from_secs(30),
allowed_effects: vec![Effect::Pure],
auto_cleanup: true,
resource_limits: SandboxResourceLimits::default(),
}
}
}
impl Default for SandboxResourceLimits {
fn default() -> Self {
Self {
max_memory: Some(64 * 1024 * 1024), // 64MB
max_execution_time: Some(Duration::from_secs(10)),
max_file_operations: Some(100),
max_network_operations: Some(10),
}
}
}
impl EffectSandboxHandle {
/// Destroys the sandbox and cleans up resources.
pub fn destroy(self) -> Result<(), String> {
// Simplified implementation to avoid circular dependency
// Full implementation would coordinate with EffectCoordinator
Ok(())
}
/// Checks if the sandbox is still valid.
pub fn is_valid(&self) -> bool {
// TODO: Fix weak reference issue
// self.coordinator.strong_count() > 0
true
}
/// Gets sandbox statistics.
pub fn get_statistics(&self) -> SandboxStatistics {
SandboxStatistics {
id: self.id,
thread_id: self.thread_id,
uptime: SystemTime::now().duration_since(self.created_at).unwrap_or_default(),
effect_count: 0, // Would be populated in real implementation
resource_usage: ResourceUsage::default(),
}
}
}