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
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
//! Transfer Actor implementation for ATP session management.
//!
//! Defines TransferActor and ownership topology for transfer sessions,
//! providing the actor model foundation for the data-aware transfer brain.

use crate::atp::object::ObjectId;
use crate::atp::transfer_brain::{
    ChunkId, ScheduledChunk, SystemPressure, TransferBrain, TransferBrainConfig,
};
use crate::channel::{mpsc, oneshot};
use crate::cx::Cx;
use crate::error::{Error, ErrorKind, Result};
use crate::time::{Sleep, wall_now};
use crate::types::{RegionId, TaskId, TraceId};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant, SystemTime};
#[cfg(feature = "tracing-integration")]
use tracing::{debug, error, info, warn};

// Provide no-op tracing macros when tracing is disabled
#[cfg(not(feature = "tracing-integration"))]
macro_rules! debug {
    ($($arg:tt)*) => {};
}
#[cfg(not(feature = "tracing-integration"))]
macro_rules! error {
    ($($arg:tt)*) => {};
}
#[cfg(not(feature = "tracing-integration"))]
macro_rules! info {
    ($($arg:tt)*) => {};
}
#[cfg(not(feature = "tracing-integration"))]
macro_rules! warn {
    ($($arg:tt)*) => {};
}

/// Configuration for transfer actor
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransferActorConfig {
    /// Transfer brain configuration
    pub brain_config: TransferBrainConfig,
    /// Maximum concurrent transfer sessions
    pub max_concurrent_sessions: usize,
    /// Session timeout duration
    pub session_timeout: Duration,
    /// Pressure monitoring interval
    pub pressure_monitor_interval: Duration,
    /// Resource monitoring enabled
    pub enable_resource_monitoring: bool,
}

impl Default for TransferActorConfig {
    fn default() -> Self {
        Self {
            brain_config: TransferBrainConfig::default(),
            max_concurrent_sessions: 64,
            session_timeout: Duration::from_secs(3600), // 1 hour
            pressure_monitor_interval: Duration::from_secs(1),
            enable_resource_monitoring: true,
        }
    }
}

/// Unique identifier for a transfer session
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SessionId {
    /// Object being transferred
    pub object_id: ObjectId,
    /// Session start timestamp
    pub started_at: SystemTime,
    /// Unique session counter
    pub session_counter: u64,
}

impl SessionId {
    /// Create a new session ID
    pub fn new(object_id: ObjectId, session_counter: u64) -> Self {
        Self {
            object_id,
            started_at: SystemTime::now(),
            session_counter,
        }
    }

    /// Get string representation for logging
    pub fn as_string(&self) -> String {
        format!("sess-{}-{}", self.object_id, self.session_counter)
    }
}

/// State of a transfer session
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SessionState {
    /// Session is initializing
    Initializing,
    /// Session is actively transferring data
    Active,
    /// Session is paused due to pressure or backpressure
    Paused,
    /// Session is completing final operations
    Completing,
    /// Session completed successfully
    Completed,
    /// Session failed with error
    Failed,
    /// Session was cancelled
    Cancelled,
}

/// A transfer session managed by the transfer actor
#[derive(Debug)]
pub struct TransferSession {
    /// Session identifier
    pub session_id: SessionId,
    /// Current session state
    pub state: SessionState,
    /// Object being transferred
    pub object_id: ObjectId,
    /// Session-specific transfer brain
    pub brain: TransferBrain,
    /// Region that owns this session
    pub region_id: RegionId,
    /// Task handling this session
    pub task_id: TaskId,
    /// Session start time
    pub started_at: SystemTime,
    /// Last activity timestamp
    pub last_activity: SystemTime,
    /// Total bytes transferred
    pub bytes_transferred: u64,
    /// Total chunks completed
    pub chunks_completed: usize,
    /// Current error (if any)
    pub error: Option<Error>,
    /// Session trace ID
    pub trace_id: TraceId,
}

impl TransferSession {
    /// Create a new transfer session
    pub fn new(
        session_id: SessionId,
        object_id: ObjectId,
        region_id: RegionId,
        task_id: TaskId,
        brain_config: TransferBrainConfig,
        trace_id: TraceId,
    ) -> Self {
        Self {
            session_id,
            state: SessionState::Initializing,
            object_id,
            brain: TransferBrain::new(brain_config),
            region_id,
            task_id,
            started_at: SystemTime::now(),
            last_activity: SystemTime::now(),
            bytes_transferred: 0,
            chunks_completed: 0,
            error: None,
            trace_id,
        }
    }

    /// Check if session is active
    pub fn is_active(&self) -> bool {
        matches!(
            self.state,
            SessionState::Active | SessionState::Initializing
        )
    }

    /// Check if session has timed out
    pub fn is_timed_out(&self, timeout: Duration) -> bool {
        self.last_activity.elapsed().unwrap_or(Duration::ZERO) > timeout
    }

    /// Update session activity timestamp
    pub fn update_activity(&mut self) {
        self.last_activity = SystemTime::now();
    }

    /// Transition session to new state
    pub fn transition_to(&mut self, new_state: SessionState) {
        if self.state != new_state {
            debug!(
                "Session {} transitioning from {:?} to {:?}",
                self.session_id.as_string(),
                self.state,
                new_state
            );
            self.state = new_state;
            self.update_activity();
        }
    }

    /// Set session error and transition to failed state
    pub fn fail_with_error(&mut self, error: Error) {
        self.error = Some(error);
        self.transition_to(SessionState::Failed);
    }
}

/// Message types for transfer actor communication
#[derive(Debug)]
pub enum TransferMessage {
    /// Start a new transfer session
    StartSession {
        object_id: ObjectId,
        region_id: RegionId,
        task_id: TaskId,
        trace_id: TraceId,
        response_tx: oneshot::Sender<Result<SessionId>>,
    },

    /// Schedule a chunk for transfer
    ScheduleChunk {
        session_id: SessionId,
        chunk: ScheduledChunk,
        response_tx: oneshot::Sender<Result<()>>,
    },

    /// Complete a chunk transfer
    CompleteChunk {
        session_id: SessionId,
        chunk_id: ChunkId,
        success: bool,
        bytes_transferred: u64,
        response_tx: oneshot::Sender<Result<()>>,
    },

    /// Update system pressure
    UpdatePressure { pressure: SystemPressure },

    /// Pause a transfer session
    PauseSession {
        session_id: SessionId,
        response_tx: oneshot::Sender<Result<()>>,
    },

    /// Resume a paused session
    ResumeSession {
        session_id: SessionId,
        response_tx: oneshot::Sender<Result<()>>,
    },

    /// Cancel a transfer session
    CancelSession {
        session_id: SessionId,
        response_tx: oneshot::Sender<Result<()>>,
    },

    /// Get session status
    GetSessionStatus {
        session_id: SessionId,
        response_tx: oneshot::Sender<Result<TransferSessionStatus>>,
    },

    /// Get all sessions status
    GetAllSessions {
        response_tx: oneshot::Sender<Result<Vec<TransferSessionStatus>>>,
    },

    /// Shutdown the transfer actor
    Shutdown,
}

/// Status information about a transfer session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransferSessionStatus {
    /// Session identifier
    pub session_id: SessionId,
    /// Current state
    pub state: SessionState,
    /// Object being transferred
    pub object_id: ObjectId,
    /// Session duration
    pub duration: Duration,
    /// Bytes transferred
    pub bytes_transferred: u64,
    /// Chunks completed
    pub chunks_completed: usize,
    /// Current brain state
    pub brain_state: crate::atp::transfer_brain::SchedulingState,
    /// Current metrics
    pub metrics: crate::atp::transfer_brain::TransferMetrics,
    /// Error message (if failed)
    pub error_message: Option<String>,
}

/// Transfer actor for managing transfer sessions
pub struct TransferActor {
    /// Actor configuration
    config: TransferActorConfig,
    /// Active transfer sessions
    sessions: HashMap<SessionId, TransferSession>,
    /// Session counter for unique IDs
    session_counter: u64,
    /// Current system pressure
    current_pressure: SystemPressure,
    /// Message receiver
    message_rx: mpsc::Receiver<TransferMessage>,
    /// Message sender handle for cloning
    message_tx: mpsc::Sender<TransferMessage>,
}

impl TransferActor {
    /// Create a new transfer actor
    pub fn new(config: TransferActorConfig) -> (Self, TransferActorHandle) {
        let (message_tx, message_rx) = mpsc::channel(1000);

        let actor = Self {
            config: config.clone(),
            sessions: HashMap::new(),
            session_counter: 0,
            current_pressure: SystemPressure::default(),
            message_rx,
            message_tx: message_tx.clone(),
        };

        let handle = TransferActorHandle { message_tx };

        (actor, handle)
    }

    /// Run the transfer actor event loop
    pub async fn run(mut self, cx: &Cx) -> Result<()> {
        info!("Transfer actor starting");

        let maintenance_interval = self.config.session_timeout / 10;
        let mut last_maintenance = SystemTime::now();

        loop {
            // Check for cancellation
            if cx.is_cancel_requested() {
                info!("Transfer actor cancelled, shutting down");
                break;
            }

            // Try to receive a message (non-blocking)
            match self.message_rx.try_recv() {
                Ok(msg) => {
                    let shutdown = matches!(msg, TransferMessage::Shutdown);
                    if self.handle_message(cx, msg).await.is_err() {
                        error!("Error handling transfer actor message");
                    }

                    // Check if it's shutdown
                    if shutdown {
                        break;
                    }
                }
                Err(_) => {
                    // No message available, check if maintenance is needed
                    if last_maintenance.elapsed().unwrap_or(Duration::ZERO) > maintenance_interval {
                        self.cleanup_timed_out_sessions().await;
                        last_maintenance = SystemTime::now();
                    }

                    // Small delay to avoid busy spinning
                    Sleep::after(wall_now(), Duration::from_millis(10)).await;
                }
            }
        }

        info!("Transfer actor shut down");
        Ok(())
    }

    async fn handle_message(&mut self, cx: &Cx, message: TransferMessage) -> Result<()> {
        match message {
            TransferMessage::StartSession {
                object_id,
                region_id,
                task_id,
                trace_id,
                response_tx,
            } => {
                let result = self
                    .start_session(object_id, region_id, task_id, trace_id)
                    .await;
                if response_tx.send(cx, result).is_err() {
                    debug!("Failed to send start session response");
                }
            }

            TransferMessage::ScheduleChunk {
                session_id,
                chunk,
                response_tx,
            } => {
                let result = self.schedule_chunk(session_id, chunk).await;
                let _ = response_tx.send(cx, result);
            }

            TransferMessage::CompleteChunk {
                session_id,
                chunk_id,
                success,
                bytes_transferred,
                response_tx,
            } => {
                let result = self
                    .complete_chunk(session_id, chunk_id, success, bytes_transferred)
                    .await;
                let _ = response_tx.send(cx, result);
            }

            TransferMessage::UpdatePressure { pressure } => {
                self.update_pressure(pressure).await;
            }

            TransferMessage::PauseSession {
                session_id,
                response_tx,
            } => {
                let result = self.pause_session(session_id).await;
                let _ = response_tx.send(cx, result);
            }

            TransferMessage::ResumeSession {
                session_id,
                response_tx,
            } => {
                let result = self.resume_session(session_id).await;
                let _ = response_tx.send(cx, result);
            }

            TransferMessage::CancelSession {
                session_id,
                response_tx,
            } => {
                let result = self.cancel_session(session_id).await;
                let _ = response_tx.send(cx, result);
            }

            TransferMessage::GetSessionStatus {
                session_id,
                response_tx,
            } => {
                let result = self.get_session_status(session_id).await;
                let _ = response_tx.send(cx, result);
            }

            TransferMessage::GetAllSessions { response_tx } => {
                let result = self.get_all_sessions().await;
                let _ = response_tx.send(cx, result);
            }

            TransferMessage::Shutdown => {
                info!("Transfer actor received shutdown message");
                // Gracefully shut down all sessions
                for session in self.sessions.values_mut() {
                    if session.is_active() {
                        session.transition_to(SessionState::Cancelled);
                    }
                }
            }
        }

        Ok(())
    }

    async fn start_session(
        &mut self,
        object_id: ObjectId,
        region_id: RegionId,
        task_id: TaskId,
        trace_id: TraceId,
    ) -> Result<SessionId> {
        if self.sessions.len() >= self.config.max_concurrent_sessions {
            return Err(Error::new(ErrorKind::AdmissionDenied));
        }

        self.session_counter += 1;
        let session_id = SessionId::new(object_id.clone(), self.session_counter);

        let session = TransferSession::new(
            session_id.clone(),
            object_id,
            region_id,
            task_id,
            self.config.brain_config.clone(),
            trace_id,
        );

        info!("Started transfer session {}", session_id.as_string());
        self.sessions.insert(session_id.clone(), session);

        Ok(session_id)
    }

    async fn schedule_chunk(&mut self, session_id: SessionId, chunk: ScheduledChunk) -> Result<()> {
        let session = self
            .sessions
            .get_mut(&session_id)
            .ok_or_else(|| Error::new(ErrorKind::ObjectMismatch))?;

        if !session.is_active() {
            return Err(Error::new(ErrorKind::RegionClosed));
        }

        session.brain.schedule_chunk(chunk)?;
        session.update_activity();

        if session.state == SessionState::Initializing {
            session.transition_to(SessionState::Active);
        }

        Ok(())
    }

    async fn complete_chunk(
        &mut self,
        session_id: SessionId,
        chunk_id: ChunkId,
        success: bool,
        bytes_transferred: u64,
    ) -> Result<()> {
        let pressure = self.current_pressure.clone();
        let session = self
            .sessions
            .get_mut(&session_id)
            .ok_or_else(|| Error::new(ErrorKind::ObjectMismatch))?;

        let actual_resources =
            measured_completion_resources(session, &pressure, &chunk_id, bytes_transferred);

        session
            .brain
            .complete_chunk(&chunk_id, success, actual_resources)?;
        session.update_activity();

        if success {
            session.bytes_transferred += bytes_transferred;
            session.chunks_completed += 1;
        }

        debug!(
            "Completed chunk {} in session {} (success: {}, bytes: {})",
            chunk_id.as_string(),
            session_id.as_string(),
            success,
            bytes_transferred
        );

        Ok(())
    }

    async fn update_pressure(&mut self, pressure: SystemPressure) {
        self.current_pressure = pressure.clone();

        // Update pressure in all active sessions
        for session in self.sessions.values_mut() {
            if session.is_active() {
                session.brain.update_pressure(pressure.clone());
            }
        }

        // Pause sessions if pressure is too high
        if pressure.cpu_utilization > 0.95 || pressure.disk_pressure > 0.9 {
            for session in self.sessions.values_mut() {
                if session.state == SessionState::Active {
                    session.transition_to(SessionState::Paused);
                }
            }
        }
    }

    async fn pause_session(&mut self, session_id: SessionId) -> Result<()> {
        let session = self
            .sessions
            .get_mut(&session_id)
            .ok_or_else(|| Error::new(ErrorKind::ObjectMismatch))?;

        if session.state == SessionState::Active {
            session.transition_to(SessionState::Paused);
            info!("Paused session {}", session_id.as_string());
        }

        Ok(())
    }

    async fn resume_session(&mut self, session_id: SessionId) -> Result<()> {
        let session = self
            .sessions
            .get_mut(&session_id)
            .ok_or_else(|| Error::new(ErrorKind::ObjectMismatch))?;

        if session.state == SessionState::Paused {
            session.transition_to(SessionState::Active);
            info!("Resumed session {}", session_id.as_string());
        }

        Ok(())
    }

    async fn cancel_session(&mut self, session_id: SessionId) -> Result<()> {
        if let Some(mut session) = self.sessions.remove(&session_id) {
            session.transition_to(SessionState::Cancelled);
            info!("Cancelled session {}", session_id.as_string());
        }

        Ok(())
    }

    async fn get_session_status(&self, session_id: SessionId) -> Result<TransferSessionStatus> {
        let session = self
            .sessions
            .get(&session_id)
            .ok_or_else(|| Error::new(ErrorKind::ObjectMismatch))?;

        Ok(TransferSessionStatus {
            session_id: session.session_id.clone(),
            state: session.state,
            object_id: session.object_id.clone(),
            duration: session.started_at.elapsed().unwrap_or(Duration::ZERO),
            bytes_transferred: session.bytes_transferred,
            chunks_completed: session.chunks_completed,
            brain_state: session.brain.scheduling_state(),
            metrics: session.brain.metrics().clone(),
            error_message: session.error.as_ref().map(|e| format!("{:?}", e)),
        })
    }

    async fn get_all_sessions(&self) -> Result<Vec<TransferSessionStatus>> {
        let mut statuses = Vec::new();

        for session in self.sessions.values() {
            statuses.push(TransferSessionStatus {
                session_id: session.session_id.clone(),
                state: session.state,
                object_id: session.object_id.clone(),
                duration: session.started_at.elapsed().unwrap_or(Duration::ZERO),
                bytes_transferred: session.bytes_transferred,
                chunks_completed: session.chunks_completed,
                brain_state: session.brain.scheduling_state(),
                metrics: session.brain.metrics().clone(),
                error_message: session.error.as_ref().map(|e| format!("{:?}", e)),
            });
        }

        Ok(statuses)
    }

    async fn cleanup_timed_out_sessions(&mut self) {
        let timeout = self.config.session_timeout;
        let mut to_remove = Vec::new();

        for (session_id, session) in &mut self.sessions {
            if session.is_timed_out(timeout) {
                session.transition_to(SessionState::Failed);
                to_remove.push(session_id.clone());
            }
        }

        for session_id in to_remove {
            self.sessions.remove(&session_id);
            warn!("Cleaned up timed out session {}", session_id.as_string());
        }
    }

    pub async fn run_pressure_monitor(&self, cx: &Cx) -> Result<()> {
        let tx = self.message_tx.clone();
        let interval = self.config.pressure_monitor_interval;
        let mut sampler = SystemPressureSampler::new();

        loop {
            if cx.is_cancel_requested() {
                return Ok(());
            }

            Sleep::after(wall_now(), interval).await;
            let pressure = sampler.sample();
            tx.send(cx, TransferMessage::UpdatePressure { pressure })
                .await
                .map_err(|_| Error::new(ErrorKind::ChannelClosed))?;
        }
    }
}

/// Handle for communicating with the transfer actor
#[derive(Clone)]
pub struct TransferActorHandle {
    message_tx: mpsc::Sender<TransferMessage>,
}

impl TransferActorHandle {
    /// Start a new transfer session
    pub async fn start_session(
        &self,
        cx: &Cx,
        object_id: ObjectId,
        region_id: RegionId,
        task_id: TaskId,
        trace_id: TraceId,
    ) -> Result<SessionId> {
        let (response_tx, response_rx) = oneshot::channel();

        self.message_tx
            .send(
                cx,
                TransferMessage::StartSession {
                    object_id,
                    region_id,
                    task_id,
                    trace_id,
                    response_tx,
                },
            )
            .await
            .map_err(|_| Error::new(ErrorKind::ChannelClosed))?;

        let mut response_rx = response_rx;
        response_rx
            .recv(cx)
            .await
            .map_err(|_| Error::new(ErrorKind::ChannelClosed))?
    }

    /// Schedule a chunk for transfer
    pub async fn schedule_chunk(
        &self,
        cx: &Cx,
        session_id: SessionId,
        chunk: ScheduledChunk,
    ) -> Result<()> {
        let (response_tx, mut response_rx) = oneshot::channel();

        self.message_tx
            .send(
                cx,
                TransferMessage::ScheduleChunk {
                    session_id,
                    chunk,
                    response_tx,
                },
            )
            .await
            .map_err(|_| Error::new(ErrorKind::ChannelClosed))?;

        response_rx
            .recv(cx)
            .await
            .map_err(|_| Error::new(ErrorKind::ChannelClosed))?
    }

    /// Complete a chunk transfer
    pub async fn complete_chunk(
        &self,
        cx: &Cx,
        session_id: SessionId,
        chunk_id: ChunkId,
        success: bool,
        bytes_transferred: u64,
    ) -> Result<()> {
        let (response_tx, mut response_rx) = oneshot::channel();

        self.message_tx
            .send(
                cx,
                TransferMessage::CompleteChunk {
                    session_id,
                    chunk_id,
                    success,
                    bytes_transferred,
                    response_tx,
                },
            )
            .await
            .map_err(|_| Error::new(ErrorKind::ChannelClosed))?;

        response_rx
            .recv(cx)
            .await
            .map_err(|_| Error::new(ErrorKind::ChannelClosed))?
    }

    /// Get session status
    pub async fn get_session_status(
        &self,
        cx: &Cx,
        session_id: SessionId,
    ) -> Result<TransferSessionStatus> {
        let (response_tx, mut response_rx) = oneshot::channel();

        self.message_tx
            .send(
                cx,
                TransferMessage::GetSessionStatus {
                    session_id,
                    response_tx,
                },
            )
            .await
            .map_err(|_| Error::new(ErrorKind::ChannelClosed))?;

        response_rx
            .recv(cx)
            .await
            .map_err(|_| Error::new(ErrorKind::ChannelClosed))?
    }

    /// Shutdown the transfer actor
    pub async fn shutdown(&self, cx: &Cx) -> Result<()> {
        self.message_tx
            .send(cx, TransferMessage::Shutdown)
            .await
            .map_err(|_| Error::new(ErrorKind::ChannelClosed))?;
        Ok(())
    }
}
fn measured_completion_resources(
    session: &TransferSession,
    pressure: &SystemPressure,
    chunk_id: &ChunkId,
    bytes_transferred: u64,
) -> crate::atp::transfer_brain::ResourceUsage {
    let duration = session
        .last_activity
        .elapsed()
        .unwrap_or(Duration::ZERO)
        .max(Duration::from_millis(1));
    let duration_secs = duration.as_secs_f64().max(0.001);
    let cpu_seconds = pressure.cpu_utilization.clamp(0.0, 1.0) * duration_secs;
    let disk_bytes = chunk_id.size.max(bytes_transferred as usize) as f64
        * pressure.disk_pressure.clamp(0.0, 1.0).max(0.01);

    crate::atp::transfer_brain::ResourceUsage {
        cpu: cpu_seconds,
        disk_io: disk_bytes,
        network: bytes_transferred as f64,
        memory: chunk_id.size as f64,
        duration,
    }
}

#[derive(Debug, Clone)]
struct SystemPressureSampler {
    previous_cpu: Option<CpuSnapshot>,
    previous_disk: Option<DiskSnapshot>,
    previous_network: Option<NetworkSnapshot>,
    peak_network_bytes_per_second: f64,
}

impl SystemPressureSampler {
    fn new() -> Self {
        Self {
            previous_cpu: read_cpu_snapshot(),
            previous_disk: read_disk_snapshot(),
            previous_network: read_network_snapshot(),
            peak_network_bytes_per_second: 0.0,
        }
    }

    fn sample(&mut self) -> SystemPressure {
        let cpu_snapshot = read_cpu_snapshot();
        let cpu_utilization = cpu_snapshot
            .as_ref()
            .and_then(|current| {
                self.previous_cpu
                    .as_ref()
                    .map(|previous| current.utilization_since(previous))
            })
            .unwrap_or(0.0);
        self.previous_cpu = cpu_snapshot;

        let disk_snapshot = read_disk_snapshot();
        let disk_pressure = disk_snapshot
            .as_ref()
            .and_then(|current| {
                self.previous_disk
                    .as_ref()
                    .map(|previous| current.pressure_since(previous))
            })
            .unwrap_or(0.0);
        self.previous_disk = disk_snapshot;

        let network_snapshot = read_network_snapshot();
        let network_pressure = network_snapshot
            .as_ref()
            .and_then(|current| {
                self.previous_network.as_ref().map(|previous| {
                    current.pressure_since(previous, &mut self.peak_network_bytes_per_second)
                })
            })
            .unwrap_or(0.0);
        self.previous_network = network_snapshot;

        SystemPressure {
            cpu_utilization,
            disk_pressure,
            network_pressure,
            memory_pressure: read_memory_pressure().unwrap_or(0.0),
            measured_at: SystemTime::now(),
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct CpuSnapshot {
    idle: u64,
    total: u64,
}

impl CpuSnapshot {
    fn utilization_since(self, previous: &Self) -> f64 {
        let total_delta = self.total.saturating_sub(previous.total);
        if total_delta == 0 {
            return 0.0;
        }

        let idle_delta = self.idle.saturating_sub(previous.idle);
        ((total_delta.saturating_sub(idle_delta)) as f64 / total_delta as f64).clamp(0.0, 1.0)
    }
}

#[derive(Debug, Clone, Copy)]
struct DiskSnapshot {
    weighted_io_millis: u64,
    sampled_at: Instant,
}

impl DiskSnapshot {
    fn pressure_since(self, previous: &Self) -> f64 {
        let elapsed_millis = self
            .sampled_at
            .saturating_duration_since(previous.sampled_at)
            .as_millis()
            .max(1) as f64;
        let io_delta = self
            .weighted_io_millis
            .saturating_sub(previous.weighted_io_millis) as f64;

        (io_delta / elapsed_millis).clamp(0.0, 1.0)
    }
}

#[derive(Debug, Clone, Copy)]
struct NetworkSnapshot {
    bytes: u64,
    sampled_at: Instant,
}

impl NetworkSnapshot {
    fn pressure_since(self, previous: &Self, peak_bytes_per_second: &mut f64) -> f64 {
        let elapsed_secs = self
            .sampled_at
            .saturating_duration_since(previous.sampled_at)
            .as_secs_f64()
            .max(0.001);
        let byte_delta = self.bytes.saturating_sub(previous.bytes) as f64;
        let bytes_per_second = byte_delta / elapsed_secs;
        *peak_bytes_per_second = (*peak_bytes_per_second).max(bytes_per_second);

        if *peak_bytes_per_second <= f64::EPSILON {
            0.0
        } else {
            (bytes_per_second / *peak_bytes_per_second).clamp(0.0, 1.0)
        }
    }
}

fn read_cpu_snapshot() -> Option<CpuSnapshot> {
    let stat = std::fs::read_to_string("/proc/stat").ok()?;
    let cpu_line = stat.lines().find(|line| line.starts_with("cpu "))?;
    let mut values = cpu_line
        .split_whitespace()
        .skip(1)
        .filter_map(|field| field.parse::<u64>().ok());
    let user = values.next()?;
    let nice = values.next()?;
    let system = values.next()?;
    let idle = values.next()?;
    let iowait = values.next().unwrap_or(0);
    let irq = values.next().unwrap_or(0);
    let softirq = values.next().unwrap_or(0);
    let steal = values.next().unwrap_or(0);
    let idle_all = idle.saturating_add(iowait);
    let total = user
        .saturating_add(nice)
        .saturating_add(system)
        .saturating_add(idle)
        .saturating_add(iowait)
        .saturating_add(irq)
        .saturating_add(softirq)
        .saturating_add(steal);

    Some(CpuSnapshot {
        idle: idle_all,
        total,
    })
}

fn read_memory_pressure() -> Option<f64> {
    let meminfo = std::fs::read_to_string("/proc/meminfo").ok()?;
    let mut total_kib = None;
    let mut available_kib = None;

    for line in meminfo.lines() {
        if let Some(rest) = line.strip_prefix("MemTotal:") {
            total_kib = rest
                .split_whitespace()
                .next()
                .and_then(|value| value.parse::<u64>().ok());
        } else if let Some(rest) = line.strip_prefix("MemAvailable:") {
            available_kib = rest
                .split_whitespace()
                .next()
                .and_then(|value| value.parse::<u64>().ok());
        }
    }

    let total_kib = total_kib?;
    if total_kib == 0 {
        return None;
    }
    let available_kib = available_kib?;
    Some((1.0 - (available_kib as f64 / total_kib as f64)).clamp(0.0, 1.0))
}

fn read_disk_snapshot() -> Option<DiskSnapshot> {
    let diskstats = std::fs::read_to_string("/proc/diskstats").ok()?;
    let weighted_io_millis = diskstats
        .lines()
        .filter_map(|line| {
            let fields = line.split_whitespace().collect::<Vec<_>>();
            let device = *fields.get(2)?;
            if device.starts_with("loop") || device.starts_with("ram") {
                return None;
            }
            fields.get(13)?.parse::<u64>().ok()
        })
        .fold(0_u64, u64::saturating_add);

    Some(DiskSnapshot {
        weighted_io_millis,
        sampled_at: Instant::now(),
    })
}

fn read_network_snapshot() -> Option<NetworkSnapshot> {
    let netdev = std::fs::read_to_string("/proc/net/dev").ok()?;
    let bytes = netdev
        .lines()
        .skip(2)
        .filter_map(|line| {
            let (interface, counters) = line.split_once(':')?;
            if interface.trim() == "lo" {
                return None;
            }
            let mut fields = counters.split_whitespace();
            let rx_bytes = fields.next()?.parse::<u64>().ok()?;
            let tx_bytes = fields.nth(7)?.parse::<u64>().ok()?;
            Some(rx_bytes.saturating_add(tx_bytes))
        })
        .fold(0_u64, u64::saturating_add);

    Some(NetworkSnapshot {
        bytes,
        sampled_at: Instant::now(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::atp::object::ContentId;

    #[test]
    fn test_transfer_actor_creation() {
        let config = TransferActorConfig::default();
        let (actor, _handle) = TransferActor::new(config);

        assert_eq!(actor.session_counter, 0);
        assert!(actor.sessions.is_empty());
    }

    #[test]
    fn test_session_state_transition() {
        let object_id = ObjectId::content(ContentId::from_bytes(b"test-object"));
        let session_id = SessionId::new(object_id.clone(), 1);
        let mut session = TransferSession::new(
            session_id,
            object_id,
            RegionId::new_for_test(1, 0),
            TaskId::new_for_test(2, 0),
            TransferBrainConfig::default(),
            TraceId::from_raw(3),
        );

        assert_eq!(session.state, SessionState::Initializing);
        session.transition_to(SessionState::Active);
        assert_eq!(session.state, SessionState::Active);
    }
}