apoc 0.9.1

Agentic process control for LLMs
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
use std::fs::{self, File};
use std::io::{BufRead, BufReader, Read as _, Write};
use std::os::fd::{AsRawFd as _, FromRawFd as _, RawFd};
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::time::{Duration, Instant};

use base64::Engine as _;
use block2::RcBlock;
use dispatch2::{DispatchQueue, DispatchRetained};
use objc2::AllocAnyThread;
use objc2::rc::Retained;
use objc2_foundation::{NSError, NSString, NSURL};
use objc2_virtualization::{VZVirtioSocketConnection, VZVirtioSocketDevice, VZVirtualMachine};
use tokio::sync::oneshot;
use tokio_util::sync::CancellationToken;

use crate::engine::{EngineControl, EngineFuture};
use crate::model::{ExecutionEvent, IsolationBootDiagnostics, ProviderError};
use crate::registry::EventSink;

use super::config::ResolvedLaunch;
use super::pool::PoolRelease;
use super::snapshot::{
    clone_for_restore, mark_snapshot_unusable, record_snapshot, snapshot_available,
    snapshot_unusable,
};
use super::wire::SessionFrame;

struct QueueBound<T: objc2::Message>(Retained<T>);

impl<T: objc2::Message> Clone for QueueBound<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

// SAFETY: the retained Objective-C object is only dereferenced from work submitted to
// the private serial queue that owns it. Moving this wrapper only moves permission to
// enqueue that work; it does not move a VZ call to another execution context.
unsafe impl<T: objc2::Message> Send for QueueBound<T> {}
// SAFETY: see the Send invariant above. All mutation remains serialized by VZ's queue.
unsafe impl<T: objc2::Message> Sync for QueueBound<T> {}

impl<T: objc2::Message> std::ops::Deref for QueueBound<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.0
    }
}

struct VmResource {
    queue: DispatchRetained<DispatchQueue>,
    vm: QueueBound<VZVirtualMachine>,
    stopped: AtomicBool,
}

impl VmResource {
    async fn stop(&self) -> Result<(), ProviderError> {
        if self.stopped.swap(true, Ordering::SeqCst) {
            return Ok(());
        }
        stop_vm(&self.queue, &self.vm).await
    }
}

impl Drop for VmResource {
    fn drop(&mut self) {
        if self.stopped.swap(true, Ordering::SeqCst) {
            return;
        }
        let vm = self.vm.clone();
        self.queue.exec_async(move || {
            if unsafe { vm.canStop() } {
                let callback = RcBlock::new(|_error: *mut NSError| {});
                unsafe { vm.stopWithCompletionHandler(&callback) };
            }
        });
    }
}

pub struct PreparedVm {
    writer: File,
    resource: Arc<VmResource>,
    pub diagnostics: IsolationBootDiagnostics,
}

impl PreparedVm {
    pub async fn boot(
        launch: &ResolvedLaunch,
        cancellation: &CancellationToken,
    ) -> Result<Self, ProviderError> {
        if !unsafe { VZVirtualMachine::isSupported() } {
            return Err(ProviderError::new(
                "NATIVE_VZ_UNSUPPORTED",
                "Virtualization.framework is not supported on this host",
            ));
        }
        if launch.snapshot_eligible() && launch.snapshot_available()? {
            match Self::boot_from_snapshot(launch, cancellation).await {
                Ok(prepared) => return Ok(prepared),
                Err(error) => {
                    if let (Some(files), Ok(Some(fingerprint))) =
                        (&launch.snapshot_files, launch.snapshot_fingerprint(false))
                        && let Err(mark_error) =
                            mark_snapshot_unusable(files, &fingerprint, &error.message)
                    {
                        eprintln!(
                            "native-vz: cannot persist unusable snapshot state: {}",
                            mark_error.message
                        );
                    }
                    eprintln!(
                        "native-vz: snapshot restore failed ({}); falling back to cold warmup",
                        error.message
                    );
                    let mut prepared = Self::boot_cold_warmup(launch, cancellation).await?;
                    prepared.diagnostics.strategy = "snapshot_fallback_cold_warmup".to_string();
                    return Ok(prepared);
                }
            }
        }
        Self::boot_cold_warmup(launch, cancellation).await
    }

    async fn boot_cold_warmup(
        launch: &ResolvedLaunch,
        cancellation: &CancellationToken,
    ) -> Result<Self, ProviderError> {
        let total_started = Instant::now();
        let resource = create_vm_resource(launch, false)?;

        let boot_started = Instant::now();
        if let Err(error) = start_vm(&resource.queue, &resource.vm, cancellation).await {
            let _ = resource.stop().await;
            return Err(error);
        }
        let boot_ms = elapsed_ms(boot_started);
        let warmup_fd = match connect_with_retry(
            &resource.queue,
            &resource.vm,
            launch.guest_agent_port,
            Duration::from_millis(launch.guest_connect_timeout_ms),
            cancellation,
        )
        .await
        {
            Ok(fd) => fd,
            Err(error) => {
                let _ = resource.stop().await;
                return Err(error);
            }
        };
        let warmup_started = Instant::now();
        let warmup = tokio::task::spawn_blocking(move || {
            run_warmup_exchange(unsafe { File::from_raw_fd(warmup_fd) })
        })
        .await
        .map_err(|error| {
            ProviderError::new("VM_WARMUP_FAILED", format!("Warmup worker failed: {error}"))
        })?;
        if let Err(error) = warmup {
            let _ = resource.stop().await;
            return Err(error);
        }
        let warmup_ms = elapsed_ms(warmup_started);
        let connect_started = Instant::now();
        let fd = connect_with_retry(
            &resource.queue,
            &resource.vm,
            launch.guest_agent_port,
            Duration::from_millis(launch.guest_connect_timeout_ms),
            cancellation,
        )
        .await?;
        let reconnect_ms = elapsed_ms(connect_started);
        Ok(Self {
            writer: unsafe { File::from_raw_fd(fd) },
            resource,
            diagnostics: IsolationBootDiagnostics {
                strategy: "cold_warmup".to_string(),
                boot_ms: Some(boot_ms),
                warmup_ms: Some(warmup_ms),
                reconnect_ms: Some(reconnect_ms),
                total_ms: elapsed_ms(total_started),
            },
        })
    }

    async fn boot_from_snapshot(
        launch: &ResolvedLaunch,
        cancellation: &CancellationToken,
    ) -> Result<Self, ProviderError> {
        let files = launch.snapshot_files.as_ref().ok_or_else(|| {
            ProviderError::new("SNAPSHOT_UNAVAILABLE", "Bundle has no snapshot resources")
        })?;
        let fingerprint = launch.snapshot_fingerprint(false)?.ok_or_else(|| {
            ProviderError::new(
                "SNAPSHOT_UNAVAILABLE",
                "Bundle has no persisted machine identifier",
            )
        })?;
        if !snapshot_available(files, &fingerprint) {
            return Err(ProviderError::new(
                "SNAPSHOT_UNAVAILABLE",
                "Snapshot metadata does not match this image and host",
            ));
        }
        let total_started = Instant::now();
        let resource = create_vm_resource(launch, true)?;
        let restore_started = Instant::now();
        let restore_clone = clone_for_restore(files)?;
        if let Err(error) = restore_vm(&resource.queue, &resource.vm, restore_clone.path()).await {
            let _ = resource.stop().await;
            return Err(error);
        }
        drop(restore_clone);
        if let Err(error) = resume_vm(&resource.queue, &resource.vm).await {
            let _ = resource.stop().await;
            return Err(error);
        }
        let restore_ms = elapsed_ms(restore_started);
        let connect_started = Instant::now();
        let fd = match connect_with_retry(
            &resource.queue,
            &resource.vm,
            launch.guest_agent_port,
            Duration::from_millis(launch.guest_connect_timeout_ms),
            cancellation,
        )
        .await
        {
            Ok(fd) => fd,
            Err(error) => {
                let _ = resource.stop().await;
                return Err(error);
            }
        };
        Ok(Self {
            writer: unsafe { File::from_raw_fd(fd) },
            resource,
            diagnostics: IsolationBootDiagnostics {
                strategy: "snapshot_restore".to_string(),
                boot_ms: Some(restore_ms),
                warmup_ms: None,
                reconnect_ms: Some(elapsed_ms(connect_started)),
                total_ms: elapsed_ms(total_started),
            },
        })
    }

    pub async fn stop(&self) -> Result<(), ProviderError> {
        self.resource.stop().await
    }
}

pub async fn ensure_snapshot(
    launch: &ResolvedLaunch,
    cancellation: &CancellationToken,
) -> Result<bool, ProviderError> {
    if !launch.snapshot_eligible() {
        return Err(ProviderError::new(
            "SNAPSHOT_CONFIGURATION_INVALID",
            "Snapshot build requires a snapshot-enabled launch without workspace or network devices",
        ));
    }
    let files = launch.snapshot_files.as_ref().ok_or_else(|| {
        ProviderError::new("SNAPSHOT_UNAVAILABLE", "Bundle has no snapshot resources")
    })?;
    let fingerprint = launch.snapshot_fingerprint(true)?.ok_or_else(|| {
        ProviderError::new(
            "SNAPSHOT_UNAVAILABLE",
            "Bundle has no persisted machine identifier",
        )
    })?;
    if snapshot_unusable(files, &fingerprint) {
        return Ok(false);
    }
    if snapshot_available(files, &fingerprint) {
        return Ok(false);
    }

    let _ = fs::remove_file(&files.machine_state);
    let _ = fs::remove_file(&files.metadata);
    let resource = create_vm_resource(launch, true)?;
    let built = async {
        start_vm(&resource.queue, &resource.vm, cancellation).await?;
        let fd = connect_with_retry(
            &resource.queue,
            &resource.vm,
            launch.guest_agent_port,
            Duration::from_millis(launch.guest_connect_timeout_ms),
            cancellation,
        )
        .await?;
        tokio::task::spawn_blocking(move || run_warmup_exchange(unsafe { File::from_raw_fd(fd) }))
            .await
            .map_err(|error| {
                ProviderError::new("VM_WARMUP_FAILED", format!("Warmup worker failed: {error}"))
            })??;
        pause_vm(&resource.queue, &resource.vm).await?;
        save_vm(&resource.queue, &resource.vm, &files.machine_state).await?;
        record_snapshot(files, &fingerprint)?;
        Ok::<(), ProviderError>(())
    }
    .await;
    let stopped = resource.stop().await;
    if let Err(error) = built {
        let _ = fs::remove_file(&files.machine_state);
        let _ = fs::remove_file(&files.metadata);
        return Err(error);
    }
    stopped?;
    Ok(true)
}

fn create_vm_resource(
    launch: &ResolvedLaunch,
    require_snapshot_support: bool,
) -> Result<Arc<VmResource>, ProviderError> {
    let queue = DispatchQueue::new("dev.apoc.macos-vz.session", None);
    let configuration = launch.build_configuration()?;
    if require_snapshot_support {
        unsafe { configuration.validateSaveRestoreSupportWithError() }.map_err(|error| {
            ProviderError::new(
                "SNAPSHOT_CONFIGURATION_INVALID",
                error.localizedDescription().to_string(),
            )
        })?;
    }
    let vm = QueueBound(unsafe {
        VZVirtualMachine::initWithConfiguration_queue(
            VZVirtualMachine::alloc(),
            &configuration,
            &queue,
        )
    });
    Ok(Arc::new(VmResource {
        queue,
        vm,
        stopped: AtomicBool::new(false),
    }))
}

fn run_warmup_exchange(mut connection: File) -> Result<(), ProviderError> {
    connection
        .write_all(b"{\"control\":\"warmup\"}\n")
        .and_then(|()| connection.flush())
        .map_err(|error| {
            ProviderError::new(
                "VM_WARMUP_FAILED",
                format!("Cannot send guest warmup frame: {error}"),
            )
        })?;
    let reader = connection.try_clone().map_err(|error| {
        ProviderError::new(
            "VM_WARMUP_FAILED",
            format!("Cannot duplicate guest warmup connection: {error}"),
        )
    })?;
    let mut reader = BufReader::new(reader);
    let mut completed = false;
    for line in reader.by_ref().lines() {
        let line = line.map_err(|error| {
            ProviderError::new(
                "VM_WARMUP_FAILED",
                format!("Cannot read guest warmup response: {error}"),
            )
        })?;
        let value = serde_json::from_str::<serde_json::Value>(&line).map_err(|error| {
            ProviderError::new(
                "VM_WARMUP_FAILED",
                format!("Cannot decode guest warmup response: {error}"),
            )
        })?;
        if value.get("control").and_then(|value| value.as_str()) == Some("warmup_done") {
            completed = true;
            break;
        }
    }
    if !completed {
        return Err(ProviderError::new(
            "VM_WARMUP_FAILED",
            "Guest closed the warmup connection before warmup_done",
        ));
    }
    connection
        .write_all(b"{\"control\":\"checkpoint\"}\n")
        .and_then(|()| connection.flush())
        .map_err(|error| {
            ProviderError::new(
                "VM_WARMUP_FAILED",
                format!("Cannot send guest checkpoint frame: {error}"),
            )
        })?;
    let mut descriptor = libc::pollfd {
        fd: reader.get_ref().as_raw_fd(),
        events: libc::POLLIN | libc::POLLHUP,
        revents: 0,
    };
    let ready = unsafe { libc::poll(&mut descriptor, 1, 5_000) };
    if ready <= 0 {
        return Err(ProviderError::new(
            "VM_WARMUP_FAILED",
            if ready == 0 {
                "Guest did not close the checkpoint connection within 5000 ms".to_string()
            } else {
                format!(
                    "Cannot wait for guest checkpoint close: {}",
                    std::io::Error::last_os_error()
                )
            },
        ));
    }
    let mut trailing = Vec::new();
    reader.read_to_end(&mut trailing).map_err(|error| {
        ProviderError::new(
            "VM_WARMUP_FAILED",
            format!("Cannot observe guest checkpoint close: {error}"),
        )
    })?;
    if !trailing.is_empty() {
        return Err(ProviderError::new(
            "VM_WARMUP_FAILED",
            "Guest emitted unexpected data after checkpoint",
        ));
    }
    Ok(())
}

#[derive(Default)]
struct ActivationGate {
    active: Mutex<bool>,
    ready: Condvar,
}

impl ActivationGate {
    fn open(&self) {
        *self.active.lock().unwrap() = true;
        self.ready.notify_all();
    }

    fn wait(&self) {
        let mut active = self.active.lock().unwrap();
        while !*active {
            active = self.ready.wait(active).unwrap();
        }
    }
}

pub struct NativeSession {
    execution_id: String,
    start: SessionFrame,
    writer: Arc<Mutex<File>>,
    resource: Arc<VmResource>,
    gate: Arc<ActivationGate>,
    activated: AtomicBool,
    _release: Option<PoolRelease>,
}

impl NativeSession {
    pub fn from_prepared(
        prepared: PreparedVm,
        launch: ResolvedLaunch,
        events: EventSink,
        release: Option<PoolRelease>,
    ) -> Result<Arc<Self>, ProviderError> {
        let writer = prepared.writer;
        let reader = writer.try_clone().map_err(|error| {
            ProviderError::new(
                "VSOCK_CONNECTION_FAILED",
                format!("Cannot duplicate guest connection: {error}"),
            )
        })?;
        let gate = Arc::new(ActivationGate::default());
        let execution_id = launch.guest.request_id.clone();
        spawn_reader(reader, execution_id.clone(), events, Arc::clone(&gate));

        Ok(Arc::new(Self {
            execution_id,
            start: SessionFrame::start(&launch.guest),
            writer: Arc::new(Mutex::new(writer)),
            resource: prepared.resource,
            gate,
            activated: AtomicBool::new(false),
            _release: release,
        }))
    }

    fn write(&self, frame: &SessionFrame) -> Result<(), ProviderError> {
        let mut writer = self.writer.lock().map_err(|_| {
            ProviderError::new("VSOCK_WRITE_FAILED", "Guest connection writer is poisoned")
        })?;
        serde_json::to_writer(&mut *writer, frame).map_err(|error| {
            ProviderError::new(
                "VSOCK_WRITE_FAILED",
                format!("Cannot encode guest frame: {error}"),
            )
        })?;
        writer.write_all(b"\n").map_err(|error| {
            ProviderError::new(
                "VSOCK_WRITE_FAILED",
                format!("Cannot write guest frame: {error}"),
            )
        })?;
        writer.flush().map_err(|error| {
            ProviderError::new(
                "VSOCK_WRITE_FAILED",
                format!("Cannot flush guest frame: {error}"),
            )
        })
    }

    async fn stop(&self) -> Result<(), ProviderError> {
        self.resource.stop().await
    }
}

impl EngineControl for NativeSession {
    fn activate(&self) -> EngineFuture<'_, Result<(), ProviderError>> {
        Box::pin(async move {
            if self.activated.swap(true, Ordering::SeqCst) {
                return Ok(());
            }
            self.write(&self.start)?;
            self.gate.open();
            Ok(())
        })
    }

    fn input<'a>(&'a self, data: Vec<u8>) -> EngineFuture<'a, Result<(), ProviderError>> {
        Box::pin(async move {
            self.write(&SessionFrame::input(
                &self.execution_id,
                base64::engine::general_purpose::STANDARD.encode(data),
            ))
        })
    }

    fn signal<'a>(&'a self, signal: String) -> EngineFuture<'a, Result<(), ProviderError>> {
        Box::pin(async move { self.write(&SessionFrame::signal(&self.execution_id, signal)) })
    }

    fn cancel<'a>(&'a self) -> EngineFuture<'a, Result<(), ProviderError>> {
        Box::pin(async move {
            let _ = self.write(&SessionFrame::close(&self.execution_id));
            self.stop().await
        })
    }
}

async fn start_vm(
    queue: &DispatchQueue,
    vm: &QueueBound<VZVirtualMachine>,
    cancellation: &CancellationToken,
) -> Result<(), ProviderError> {
    let (sender, receiver) = oneshot::channel();
    let sender = Arc::new(Mutex::new(Some(sender)));
    let vm = vm.clone();
    queue.exec_async(move || {
        let callback_sender = Arc::clone(&sender);
        let callback = RcBlock::new(move |error: *mut NSError| {
            let result = if error.is_null() {
                Ok(())
            } else {
                Err(unsafe { &*error }.localizedDescription().to_string())
            };
            if let Some(sender) = callback_sender.lock().unwrap().take() {
                let _ = sender.send(result);
            }
        });
        unsafe { vm.startWithCompletionHandler(&callback) };
    });

    tokio::select! {
        result = receiver => result
            .map_err(|_| ProviderError::new("VZ_START_FAILED", "VZ start callback was dropped"))?
            .map_err(|error| ProviderError::new("VZ_START_FAILED", error)),
        () = cancellation.cancelled() => Err(ProviderError::new("EXECUTION_CANCELED", "Execution was canceled during VM start")),
    }
}

async fn pause_vm(
    queue: &DispatchQueue,
    vm: &QueueBound<VZVirtualMachine>,
) -> Result<(), ProviderError> {
    let (sender, receiver) = oneshot::channel();
    let sender = Arc::new(Mutex::new(Some(sender)));
    let vm = vm.clone();
    queue.exec_async(move || {
        if !unsafe { vm.canPause() } {
            if let Some(sender) = sender.lock().unwrap().take() {
                let _ = sender.send(Err("VZ machine cannot pause".to_string()));
            }
            return;
        }
        let callback_sender = Arc::clone(&sender);
        let callback = RcBlock::new(move |error: *mut NSError| {
            let result = callback_result(error);
            if let Some(sender) = callback_sender.lock().unwrap().take() {
                let _ = sender.send(result);
            }
        });
        unsafe { vm.pauseWithCompletionHandler(&callback) };
    });
    receiver
        .await
        .map_err(|_| ProviderError::new("VZ_PAUSE_FAILED", "VZ pause callback was dropped"))?
        .map_err(|error| ProviderError::new("VZ_PAUSE_FAILED", error))
}

async fn resume_vm(
    queue: &DispatchQueue,
    vm: &QueueBound<VZVirtualMachine>,
) -> Result<(), ProviderError> {
    let (sender, receiver) = oneshot::channel();
    let sender = Arc::new(Mutex::new(Some(sender)));
    let vm = vm.clone();
    queue.exec_async(move || {
        if !unsafe { vm.canResume() } {
            if let Some(sender) = sender.lock().unwrap().take() {
                let _ = sender.send(Err("VZ machine cannot resume".to_string()));
            }
            return;
        }
        let callback_sender = Arc::clone(&sender);
        let callback = RcBlock::new(move |error: *mut NSError| {
            let result = callback_result(error);
            if let Some(sender) = callback_sender.lock().unwrap().take() {
                let _ = sender.send(result);
            }
        });
        unsafe { vm.resumeWithCompletionHandler(&callback) };
    });
    receiver
        .await
        .map_err(|_| ProviderError::new("VZ_RESUME_FAILED", "VZ resume callback was dropped"))?
        .map_err(|error| ProviderError::new("VZ_RESUME_FAILED", error))
}

#[cfg(target_arch = "aarch64")]
async fn restore_vm(
    queue: &DispatchQueue,
    vm: &QueueBound<VZVirtualMachine>,
    path: &Path,
) -> Result<(), ProviderError> {
    let (sender, receiver) = oneshot::channel();
    let sender = Arc::new(Mutex::new(Some(sender)));
    let vm = vm.clone();
    let url = file_url(path);
    queue.exec_async(move || {
        let callback_sender = Arc::clone(&sender);
        let callback = RcBlock::new(move |error: *mut NSError| {
            let result = callback_result(error);
            if let Some(sender) = callback_sender.lock().unwrap().take() {
                let _ = sender.send(result);
            }
        });
        unsafe { vm.restoreMachineStateFromURL_completionHandler(&url, &callback) };
    });
    receiver
        .await
        .map_err(|_| {
            ProviderError::new("SNAPSHOT_RESTORE_FAILED", "VZ restore callback was dropped")
        })?
        .map_err(|error| ProviderError::new("SNAPSHOT_RESTORE_FAILED", error))
}

#[cfg(not(target_arch = "aarch64"))]
async fn restore_vm(
    _queue: &DispatchQueue,
    _vm: &QueueBound<VZVirtualMachine>,
    _path: &Path,
) -> Result<(), ProviderError> {
    Err(ProviderError::new(
        "SNAPSHOT_UNSUPPORTED",
        "VZ snapshot restore requires an arm64 macOS host",
    ))
}

#[cfg(target_arch = "aarch64")]
async fn save_vm(
    queue: &DispatchQueue,
    vm: &QueueBound<VZVirtualMachine>,
    path: &Path,
) -> Result<(), ProviderError> {
    let _ = fs::remove_file(path);
    let (sender, receiver) = oneshot::channel();
    let sender = Arc::new(Mutex::new(Some(sender)));
    let vm = vm.clone();
    let url = file_url(path);
    queue.exec_async(move || {
        let callback_sender = Arc::clone(&sender);
        let callback = RcBlock::new(move |error: *mut NSError| {
            let result = callback_result(error);
            if let Some(sender) = callback_sender.lock().unwrap().take() {
                let _ = sender.send(result);
            }
        });
        unsafe { vm.saveMachineStateToURL_completionHandler(&url, &callback) };
    });
    let saved = receiver
        .await
        .map_err(|_| ProviderError::new("SNAPSHOT_SAVE_FAILED", "VZ save callback was dropped"))?
        .map_err(|error| ProviderError::new("SNAPSHOT_SAVE_FAILED", error));
    if let Err(error) = saved {
        let _ = fs::remove_file(path);
        return Err(error);
    }
    File::open(path)
        .and_then(|file| file.sync_all())
        .map_err(|error| {
            ProviderError::new(
                "SNAPSHOT_SAVE_FAILED",
                format!("Cannot sync saved machine state: {error}"),
            )
        })?;
    Ok(())
}

#[cfg(not(target_arch = "aarch64"))]
async fn save_vm(
    _queue: &DispatchQueue,
    _vm: &QueueBound<VZVirtualMachine>,
    _path: &Path,
) -> Result<(), ProviderError> {
    Err(ProviderError::new(
        "SNAPSHOT_UNSUPPORTED",
        "VZ snapshot save requires an arm64 macOS host",
    ))
}

fn callback_result(error: *mut NSError) -> Result<(), String> {
    if error.is_null() {
        Ok(())
    } else {
        let error = unsafe { &*error };
        let reason = error
            .localizedFailureReason()
            .map(|value| value.to_string())
            .unwrap_or_else(|| "none".to_string());
        let recovery = error
            .localizedRecoverySuggestion()
            .map(|value| value.to_string())
            .unwrap_or_else(|| "none".to_string());
        Err(format!(
            "{} (domain={} code={} reason={} recovery={})",
            error.localizedDescription(),
            error.domain(),
            error.code(),
            reason,
            recovery
        ))
    }
}

fn file_url(path: &Path) -> Retained<NSURL> {
    NSURL::fileURLWithPath(&NSString::from_str(&path.to_string_lossy()))
}

async fn connect_with_retry(
    queue: &DispatchQueue,
    vm: &QueueBound<VZVirtualMachine>,
    port: u32,
    timeout: Duration,
    cancellation: &CancellationToken,
) -> Result<RawFd, ProviderError> {
    let deadline = Instant::now() + timeout;
    loop {
        let last_error = match connect_once(queue, vm, port).await {
            Ok(fd) => return Ok(fd),
            Err(error) => error,
        };
        if Instant::now() >= deadline {
            return Err(ProviderError::new(
                "VSOCK_CONNECTION_TIMEOUT",
                format!("Timed out connecting to guest agent on port {port}: {last_error}"),
            ));
        }
        tokio::select! {
            () = cancellation.cancelled() => {
                return Err(ProviderError::new(
                    "EXECUTION_CANCELED",
                    "Execution was canceled while connecting to the guest agent",
                ));
            }
            () = tokio::time::sleep(Duration::from_millis(50)) => {}
        }
    }
}

async fn connect_once(
    queue: &DispatchQueue,
    vm: &QueueBound<VZVirtualMachine>,
    port: u32,
) -> Result<RawFd, String> {
    let (sender, receiver) = oneshot::channel();
    let sender = Arc::new(Mutex::new(Some(sender)));
    let vm = vm.clone();
    queue.exec_async(move || unsafe {
        let devices = vm.socketDevices();
        let Some(device) = devices.firstObject() else {
            if let Some(sender) = sender.lock().unwrap().take() {
                let _ = sender.send(Err("VZ exposed no socket device".to_string()));
            }
            return;
        };
        let device: Retained<VZVirtioSocketDevice> = Retained::cast_unchecked(device);
        let callback_sender = Arc::clone(&sender);
        let callback = RcBlock::new(
            move |connection: *mut VZVirtioSocketConnection, error: *mut NSError| {
                let result = if !error.is_null() {
                    Err((&*error).localizedDescription().to_string())
                } else if connection.is_null() {
                    Err("VZ returned an empty socket connection".to_string())
                } else {
                    let fd = (&*connection).fileDescriptor();
                    let duplicated = libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, 0);
                    if duplicated < 0 {
                        Err(std::io::Error::last_os_error().to_string())
                    } else {
                        Ok(duplicated)
                    }
                };
                if let Some(sender) = callback_sender.lock().unwrap().take() {
                    let _ = sender.send(result);
                }
            },
        );
        device.connectToPort_completionHandler(port, &callback);
    });
    receiver
        .await
        .map_err(|_| "VZ connection callback was dropped".to_string())?
}

async fn stop_vm(
    queue: &DispatchQueue,
    vm: &QueueBound<VZVirtualMachine>,
) -> Result<(), ProviderError> {
    let (sender, receiver) = oneshot::channel();
    let sender = Arc::new(Mutex::new(Some(sender)));
    let vm = vm.clone();
    queue.exec_async(move || {
        if !unsafe { vm.canStop() } {
            if let Some(sender) = sender.lock().unwrap().take() {
                let _ = sender.send(Ok(()));
            }
            return;
        }
        let callback_sender = Arc::clone(&sender);
        let callback = RcBlock::new(move |error: *mut NSError| {
            let result = if error.is_null() {
                Ok(())
            } else {
                Err(unsafe { &*error }.localizedDescription().to_string())
            };
            if let Some(sender) = callback_sender.lock().unwrap().take() {
                let _ = sender.send(result);
            }
        });
        unsafe { vm.stopWithCompletionHandler(&callback) };
    });
    receiver
        .await
        .map_err(|_| ProviderError::new("VZ_STOP_FAILED", "VZ stop callback was dropped"))?
        .map_err(|error| ProviderError::new("VZ_STOP_FAILED", error))
}

fn spawn_reader(reader: File, execution_id: String, events: EventSink, gate: Arc<ActivationGate>) {
    let runtime = tokio::runtime::Handle::current();
    std::thread::spawn(move || {
        gate.wait();
        let mut terminal = false;
        for line in BufReader::new(reader).lines() {
            let line = match line {
                Ok(line) => line,
                Err(error) => {
                    runtime.block_on(events.append(ExecutionEvent::error(
                        &execution_id,
                        "VSOCK_READ_FAILED",
                        error.to_string(),
                    )));
                    return;
                }
            };
            let frame: SessionFrame = match serde_json::from_str(&line) {
                Ok(frame) => frame,
                Err(error) => {
                    runtime.block_on(events.append(ExecutionEvent::error(
                        &execution_id,
                        "VSOCK_PROTOCOL_ERROR",
                        error.to_string(),
                    )));
                    return;
                }
            };
            if frame.exec_id.as_deref() != Some(execution_id.as_str()) {
                continue;
            }
            let event = translate_event(&execution_id, frame);
            terminal = event.terminal();
            runtime.block_on(events.append(event));
            if terminal {
                return;
            }
        }
        if !terminal {
            runtime.block_on(events.append(ExecutionEvent::error(
                &execution_id,
                "VSOCK_CONNECTION_CLOSED",
                "Guest connection closed before a terminal event",
            )));
        }
    });
}

fn translate_event(execution_id: &str, frame: SessionFrame) -> ExecutionEvent {
    let (data, data_base64) = match frame.kind.as_str() {
        "stdout" | "stderr" => (None, frame.data),
        "error" => {
            let decoded = frame.data.as_deref().and_then(|encoded| {
                base64::engine::general_purpose::STANDARD
                    .decode(encoded)
                    .ok()
                    .map(|bytes| String::from_utf8_lossy(&bytes).into_owned())
            });
            (decoded.or(frame.data), None)
        }
        _ => (frame.data, None),
    };
    ExecutionEvent {
        kind: frame.kind,
        request_id: Some(execution_id.to_string()),
        data,
        data_base64,
        exit_code: frame.exit_code,
        error_code: frame.error_code,
        export_index: frame.export_index,
    }
}

fn elapsed_ms(started: Instant) -> u64 {
    u64::try_from(started.elapsed().as_millis()).unwrap_or(u64::MAX)
}

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

    #[test]
    fn translates_binary_output_without_decoding_it() {
        let frame = SessionFrame {
            kind: "stdout".to_string(),
            protocol_version: 1,
            request_id: None,
            exec_id: Some("native-1".to_string()),
            argv: None,
            timeout_ms: None,
            memory_mb: None,
            pids_max: None,
            env: None,
            cwd: None,
            workspace: None,
            exports: None,
            tty: None,
            data: Some("AP8=".to_string()),
            signal: None,
            exit_code: None,
            error_code: None,
            export_index: None,
        };
        let event = translate_event("native-1", frame);
        assert_eq!(event.data_base64.as_deref(), Some("AP8="));
        assert!(event.data.is_none());
    }

    #[test]
    fn warmup_exchange_waits_for_done_then_sends_checkpoint() {
        let mut descriptors = [0; 2];
        assert_eq!(
            unsafe {
                libc::socketpair(
                    libc::AF_UNIX,
                    libc::SOCK_STREAM,
                    0,
                    descriptors.as_mut_ptr(),
                )
            },
            0
        );
        let host = unsafe { File::from_raw_fd(descriptors[0]) };
        let guest = unsafe { File::from_raw_fd(descriptors[1]) };
        let peer = std::thread::spawn(move || {
            let mut writer = guest.try_clone().unwrap();
            let mut reader = BufReader::new(guest);
            let mut line = String::new();
            reader.read_line(&mut line).unwrap();
            assert_eq!(line, "{\"control\":\"warmup\"}\n");
            writer
                .write_all(b"{\"control\":\"warmup_done\",\"read_bytes\":42}\n")
                .unwrap();
            writer.flush().unwrap();
            line.clear();
            reader.read_line(&mut line).unwrap();
            assert_eq!(line, "{\"control\":\"checkpoint\"}\n");
        });

        run_warmup_exchange(host).unwrap();
        peer.join().unwrap();
    }
}