asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
//! ATP resource governance configuration.
//!
//! Provides configuration types for resource governance policies, profiles,
//! and CLI integration. Supports explicit profile selection, custom limits,
//! fairness policy configuration, and dry-run mode for policy validation.

use crate::atp::governance::{AtpFairnessPolicy, AtpResourceBudget};
use crate::atp::profiles::{AtpPowerProfile, AtpResourceProfile};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// Complete resource governance configuration.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct AtpGovernanceConfig {
    /// Selected power profile (can be overridden by custom limits).
    pub power_profile: AtpPowerProfile,
    /// Custom resource limits that override profile defaults.
    pub custom_limits: AtpCustomLimits,
    /// Policy for distributing resources among concurrent transfers.
    pub fairness_policy: AtpFairnessPolicy,
    /// Whether to enable dry-run mode (policy evaluation without enforcement).
    pub dry_run: bool,
    /// Additional metadata for diagnostics and debugging.
    pub metadata: AtpGovernanceMetadata,
}

/// Custom resource limits that override profile defaults.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct AtpCustomLimits {
    /// Override for maximum bandwidth (bytes per second).
    pub max_bandwidth_bytes_per_second: Option<u64>,
    /// Override for maximum in-flight bytes.
    pub max_in_flight_bytes: Option<u64>,
    /// Override for maximum repair symbols per second.
    pub max_repair_symbols_per_second: Option<u32>,
    /// Override for maximum disk write concurrency.
    pub max_disk_write_concurrency: Option<u16>,
    /// Override for maximum relay cost (microseconds per MiB).
    pub max_relay_cost_micros_per_mib: Option<u64>,
    /// Override for background priority setting.
    pub background_priority: Option<bool>,
    /// Override for metered network setting.
    pub metered_network: Option<bool>,
}

/// Metadata for governance configuration diagnostics.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AtpGovernanceMetadata {
    /// Source of this configuration (e.g., "cli", "config_file", "default").
    pub source: String,
    /// Version of the configuration schema.
    pub version: String,
    /// Timestamp when this configuration was created or last modified.
    pub timestamp: Option<String>,
    /// Additional key-value pairs for debugging and tracing.
    pub extra: BTreeMap<String, String>,
}

impl Default for AtpGovernanceMetadata {
    fn default() -> Self {
        Self {
            source: "default".to_string(),
            version: "1.0".to_string(),
            timestamp: None,
            extra: BTreeMap::new(),
        }
    }
}

impl AtpGovernanceConfig {
    /// Create a configuration from a power profile with default settings.
    #[must_use]
    pub fn from_power_profile(power_profile: AtpPowerProfile) -> Self {
        Self {
            power_profile,
            custom_limits: AtpCustomLimits::default(),
            fairness_policy: AtpFairnessPolicy::default(),
            dry_run: false,
            metadata: AtpGovernanceMetadata::default(),
        }
    }

    /// Create a configuration with custom limits.
    #[must_use]
    pub fn with_custom_limits(mut self, custom_limits: AtpCustomLimits) -> Self {
        self.custom_limits = custom_limits;
        self
    }

    /// Set the fairness policy.
    #[must_use]
    pub fn with_fairness_policy(mut self, fairness_policy: AtpFairnessPolicy) -> Self {
        self.fairness_policy = fairness_policy;
        self
    }

    /// Enable dry-run mode (policy evaluation without enforcement).
    #[must_use]
    pub fn with_dry_run(mut self, dry_run: bool) -> Self {
        self.dry_run = dry_run;
        self
    }

    /// Set metadata source.
    #[must_use]
    pub fn with_source(mut self, source: String) -> Self {
        self.metadata.source = source;
        self
    }

    /// Resolve the final resource budget by applying custom limits to the profile.
    #[must_use]
    pub fn resolve_budget(&self) -> AtpResourceBudget {
        let profile = AtpResourceProfile::for_power_profile(self.power_profile);

        AtpResourceBudget {
            max_bandwidth_bytes_per_second: self
                .custom_limits
                .max_bandwidth_bytes_per_second
                .or(profile.max_bandwidth_bytes_per_second),
            max_in_flight_bytes: self
                .custom_limits
                .max_in_flight_bytes
                .or(profile.max_in_flight_bytes),
            max_repair_symbols_per_second: self
                .custom_limits
                .max_repair_symbols_per_second
                .or(profile.max_repair_symbols_per_second),
            max_disk_write_concurrency: self
                .custom_limits
                .max_disk_write_concurrency
                .or(profile.max_disk_write_concurrency),
            max_relay_cost_micros_per_mib: self
                .custom_limits
                .max_relay_cost_micros_per_mib
                .or(profile.max_relay_cost_micros_per_mib),
            background_priority: self
                .custom_limits
                .background_priority
                .unwrap_or(profile.background_priority),
            metered_network: self
                .custom_limits
                .metered_network
                .unwrap_or(profile.metered_network),
        }
    }

    /// Get effective resource profile with custom overrides applied.
    #[must_use]
    pub fn resolve_profile(&self) -> AtpResourceProfile {
        let mut profile = AtpResourceProfile::for_power_profile(self.power_profile);

        // Apply custom overrides
        if let Some(bandwidth) = self.custom_limits.max_bandwidth_bytes_per_second {
            profile.max_bandwidth_bytes_per_second = Some(bandwidth);
        }
        if let Some(in_flight) = self.custom_limits.max_in_flight_bytes {
            profile.max_in_flight_bytes = Some(in_flight);
        }
        if let Some(repair) = self.custom_limits.max_repair_symbols_per_second {
            profile.max_repair_symbols_per_second = Some(repair);
        }
        if let Some(disk) = self.custom_limits.max_disk_write_concurrency {
            profile.max_disk_write_concurrency = Some(disk);
        }
        if let Some(relay) = self.custom_limits.max_relay_cost_micros_per_mib {
            profile.max_relay_cost_micros_per_mib = Some(relay);
        }
        if let Some(background) = self.custom_limits.background_priority {
            profile.background_priority = background;
        }
        if let Some(metered) = self.custom_limits.metered_network {
            profile.metered_network = metered;
        }

        profile
    }

    /// Check if any custom limits are configured.
    #[must_use]
    pub fn has_custom_limits(&self) -> bool {
        self.custom_limits.max_bandwidth_bytes_per_second.is_some()
            || self.custom_limits.max_in_flight_bytes.is_some()
            || self.custom_limits.max_repair_symbols_per_second.is_some()
            || self.custom_limits.max_disk_write_concurrency.is_some()
            || self.custom_limits.max_relay_cost_micros_per_mib.is_some()
            || self.custom_limits.background_priority.is_some()
            || self.custom_limits.metered_network.is_some()
    }
}

/// CLI arguments for resource governance configuration.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct AtpGovernanceCliArgs {
    /// Power profile selection.
    pub profile: Option<String>,
    /// Custom bandwidth limit (e.g., "64M", "1G").
    pub bandwidth: Option<String>,
    /// Custom in-flight bytes limit (e.g., "32M").
    pub in_flight: Option<String>,
    /// Custom repair symbols per second limit.
    pub repair_rate: Option<u32>,
    /// Custom disk write concurrency limit.
    pub disk_concurrency: Option<u16>,
    /// Custom relay cost limit in milliseconds per MiB.
    pub relay_cost_ms_per_mib: Option<u64>,
    /// Force background priority.
    pub background: bool,
    /// Treat network as metered.
    pub metered: bool,
    /// Fairness policy name.
    pub fairness: Option<String>,
    /// Enable dry-run mode.
    pub dry_run: bool,
}

impl AtpGovernanceCliArgs {
    /// Parse CLI arguments into a governance configuration.
    ///
    /// # Errors
    /// Returns error if profile name, fairness policy, or size strings are invalid.
    pub fn parse_config(&self) -> Result<AtpGovernanceConfig, String> {
        // Parse power profile
        let power_profile = if let Some(ref profile_name) = self.profile {
            parse_power_profile(profile_name)?
        } else {
            AtpPowerProfile::default()
        };

        // Parse custom limits
        let custom_limits = AtpCustomLimits {
            max_bandwidth_bytes_per_second: if let Some(ref bw) = self.bandwidth {
                Some(parse_size_string(bw)?)
            } else {
                None
            },
            max_in_flight_bytes: if let Some(ref inf) = self.in_flight {
                Some(parse_size_string(inf)?)
            } else {
                None
            },
            max_repair_symbols_per_second: self.repair_rate,
            max_disk_write_concurrency: self.disk_concurrency,
            max_relay_cost_micros_per_mib: self
                .relay_cost_ms_per_mib
                .map(parse_relay_cost_millis)
                .transpose()?,
            background_priority: if self.background { Some(true) } else { None },
            metered_network: if self.metered { Some(true) } else { None },
        };

        // Parse fairness policy
        let fairness_policy = if let Some(ref fairness_name) = self.fairness {
            parse_fairness_policy(fairness_name)?
        } else {
            AtpFairnessPolicy::default()
        };

        Ok(AtpGovernanceConfig {
            power_profile,
            custom_limits,
            fairness_policy,
            dry_run: self.dry_run,
            metadata: AtpGovernanceMetadata {
                source: "cli".to_string(),
                ..AtpGovernanceMetadata::default()
            },
        })
    }
}

/// Parse power profile name string into enum variant.
fn parse_power_profile(name: &str) -> Result<AtpPowerProfile, String> {
    match name.to_lowercase().as_str() {
        "max-speed" | "max_speed" | "maxspeed" => Ok(AtpPowerProfile::MaxSpeed),
        "balanced" => Ok(AtpPowerProfile::Balanced),
        "background" => Ok(AtpPowerProfile::Background),
        "metered" => Ok(AtpPowerProfile::Metered),
        "relay-conservative" | "relay_conservative" => Ok(AtpPowerProfile::RelayConservative),
        "battery-saver" | "battery_saver" | "battery" => Ok(AtpPowerProfile::BatterySaver),
        "ci-deterministic" | "ci_deterministic" | "ci" => Ok(AtpPowerProfile::CiDeterministic),
        "custom" => Ok(AtpPowerProfile::Custom),
        _ => Err(format!(
            "Unknown power profile: {name}. Available: max-speed, balanced, background, metered, relay-conservative, battery-saver, ci-deterministic, custom"
        )),
    }
}

/// Parse fairness policy name string into enum variant.
fn parse_fairness_policy(name: &str) -> Result<AtpFairnessPolicy, String> {
    match name.to_lowercase().as_str() {
        "equal-share" | "equal_share" | "equal" => Ok(AtpFairnessPolicy::EqualShare),
        "priority-weighted" | "priority_weighted" | "priority" => {
            Ok(AtpFairnessPolicy::PriorityWeighted)
        }
        "first-come-first-served" | "first_come_first_served" | "fcfs" => {
            Ok(AtpFairnessPolicy::FirstComeFirstServed)
        }
        "size-proportional" | "size_proportional" | "size" => {
            Ok(AtpFairnessPolicy::SizeProportional)
        }
        _ => Err(format!(
            "Unknown fairness policy: {name}. Available: equal-share, priority-weighted, first-come-first-served, size-proportional"
        )),
    }
}

/// Parse size string (e.g., "64M", "1G", "512K") into bytes.
fn parse_size_string(size: &str) -> Result<u64, String> {
    let original = size.trim();
    let size = original.to_lowercase();
    let (number_part, suffix) = if let Some(pos) = size.chars().position(|c| c.is_alphabetic()) {
        (&size[..pos], &size[pos..])
    } else {
        (size.as_str(), "")
    };

    let number_part = number_part.trim();
    let suffix = suffix.trim();

    let number: u64 = number_part
        .parse()
        .map_err(|_| format!("Invalid number in size string: {number_part}"))?;

    let multiplier = match suffix {
        "" | "b" => 1,
        "k" | "kb" => 1_024,
        "m" | "mb" => 1_048_576,
        "g" | "gb" => 1_073_741_824,
        "t" | "tb" => 1_099_511_627_776,
        _ => {
            return Err(format!(
                "Unknown size suffix: {suffix}. Use B, K, M, G, or T"
            ));
        }
    };

    number
        .checked_mul(multiplier)
        .ok_or_else(|| format!("Size string overflows u64 bytes: {original}"))
}

fn parse_relay_cost_millis(ms: u64) -> Result<u64, String> {
    ms.checked_mul(1_000)
        .ok_or_else(|| format!("Relay cost is too large: {ms} ms/MiB"))
}

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

    #[test]
    fn governance_config_defaults_are_reasonable() {
        let config = AtpGovernanceConfig::default();
        assert_eq!(config.power_profile, AtpPowerProfile::Balanced);
        assert_eq!(config.fairness_policy, AtpFairnessPolicy::EqualShare);
        assert!(!config.dry_run);
        assert!(!config.has_custom_limits());
        assert_eq!(config.metadata.source, "default");
    }

    #[test]
    fn resolve_budget_applies_custom_limits() {
        let config = AtpGovernanceConfig::from_power_profile(AtpPowerProfile::Balanced)
            .with_custom_limits(AtpCustomLimits {
                max_bandwidth_bytes_per_second: Some(1_048_576), // 1 MiB/s
                background_priority: Some(true),
                ..AtpCustomLimits::default()
            });

        let budget = config.resolve_budget();
        assert_eq!(budget.max_bandwidth_bytes_per_second, Some(1_048_576));
        assert!(budget.background_priority);
        // Other limits should come from the balanced profile.
        assert_eq!(budget.max_in_flight_bytes, Some(128 * 1_048_576));
    }

    #[test]
    fn parse_power_profile_handles_variations() {
        assert_eq!(
            parse_power_profile("max-speed").unwrap(),
            AtpPowerProfile::MaxSpeed
        );
        assert_eq!(
            parse_power_profile("Max_Speed").unwrap(),
            AtpPowerProfile::MaxSpeed
        );
        assert_eq!(
            parse_power_profile("BALANCED").unwrap(),
            AtpPowerProfile::Balanced
        );
        assert_eq!(
            parse_power_profile("battery").unwrap(),
            AtpPowerProfile::BatterySaver
        );

        assert!(parse_power_profile("invalid").is_err());
    }

    #[test]
    fn parse_fairness_policy_handles_variations() {
        assert_eq!(
            parse_fairness_policy("equal").unwrap(),
            AtpFairnessPolicy::EqualShare
        );
        assert_eq!(
            parse_fairness_policy("Priority_Weighted").unwrap(),
            AtpFairnessPolicy::PriorityWeighted
        );
        assert_eq!(
            parse_fairness_policy("fcfs").unwrap(),
            AtpFairnessPolicy::FirstComeFirstServed
        );

        assert!(parse_fairness_policy("invalid").is_err());
    }

    #[test]
    fn parse_size_string_handles_units() {
        assert_eq!(parse_size_string("1024").unwrap(), 1_024);
        assert_eq!(parse_size_string("1K").unwrap(), 1_024);
        assert_eq!(parse_size_string("64m").unwrap(), 64 * 1_048_576);
        assert_eq!(parse_size_string("2G").unwrap(), 2 * 1_073_741_824);
        assert_eq!(parse_size_string("1 T").unwrap(), 1_099_511_627_776);

        assert!(parse_size_string("invalid").is_err());
        assert!(parse_size_string("64X").is_err());
    }

    #[test]
    fn parse_size_string_rejects_byte_overflow() {
        let too_large_kib = format!("{}K", u64::MAX / 1_024 + 1);
        let too_large_tib = format!("{}T", u64::MAX / 1_099_511_627_776 + 1);

        assert!(parse_size_string(&too_large_kib).is_err());
        assert!(parse_size_string(&too_large_tib).is_err());
        assert_eq!(
            parse_size_string(&format!("{}B", u64::MAX)).unwrap(),
            u64::MAX
        );
    }

    #[test]
    fn cli_args_parse_config_rejects_relay_cost_overflow() {
        let args = AtpGovernanceCliArgs {
            relay_cost_ms_per_mib: Some(u64::MAX),
            ..AtpGovernanceCliArgs::default()
        };

        assert!(args.parse_config().is_err());
        assert_eq!(
            parse_relay_cost_millis(u64::MAX / 1_000).unwrap(),
            (u64::MAX / 1_000) * 1_000
        );
    }

    #[test]
    fn cli_args_parse_config_works() {
        let args = AtpGovernanceCliArgs {
            profile: Some("battery-saver".to_string()),
            bandwidth: Some("32M".to_string()),
            fairness: Some("fcfs".to_string()),
            background: true,
            dry_run: true,
            ..AtpGovernanceCliArgs::default()
        };

        let config = args.parse_config().unwrap();
        assert_eq!(config.power_profile, AtpPowerProfile::BatterySaver);
        assert_eq!(
            config.custom_limits.max_bandwidth_bytes_per_second,
            Some(32 * 1_048_576)
        );
        assert_eq!(
            config.fairness_policy,
            AtpFairnessPolicy::FirstComeFirstServed
        );
        assert_eq!(config.custom_limits.background_priority, Some(true));
        assert!(config.dry_run);
        assert_eq!(config.metadata.source, "cli");
    }
}