astrid 2026.9.2

Command-line interface for Astrid secure agent runtime
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
use std::sync::{Arc, Mutex as StdMutex};

use tokio::sync::{Notify, Semaphore};
use tokio::time::Instant;

use super::*;

#[tokio::test]
async fn final_disconnect_is_retained_between_accept_iterations() {
    let state = Arc::new(GatewayState::new(
        PathBuf::from("/runtime-home"),
        astrid_core::PrincipalId::new("codex-code").expect("principal"),
        "token".into(),
    ));
    let connection = state.connection();
    drop(connection);
    timeout(
        Duration::from_millis(100),
        state.connections_drained.notified(),
    )
    .await
    .expect("final disconnect must survive without a registered waiter");
}

#[tokio::test]
async fn gateway_idle_shutdown_waits_for_the_final_connection() {
    let directory = tempfile::tempdir().expect("socket directory");
    let listener = UnixListener::bind(directory.path().join("gateway.sock")).expect("listener");
    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = Arc::new(GatewayState::new(
        directory.path().to_owned(),
        principal,
        "token".into(),
    ));
    let first = state.connection();
    let second = state.connection();
    let grace = Duration::from_millis(40);
    let mut accepting = tokio::spawn(accept_loop(listener, state, grace));
    assert!(
        timeout(grace * 3, &mut accepting).await.is_err(),
        "quiet clients keep gateway alive"
    );
    drop(first);
    assert!(
        timeout(grace * 3, &mut accepting).await.is_err(),
        "remaining client still owns lifetime"
    );
    drop(second);
    let exit = timeout(Duration::from_secs(2), accepting)
        .await
        .expect("idle gateway retires")
        .expect("task")
        .expect("accept loop");
    assert_eq!(exit, ExitCode::SUCCESS);
}

#[tokio::test]
async fn gateway_without_an_attachment_retires_after_startup_grace() {
    let directory = tempfile::tempdir().expect("socket directory");
    let listener = UnixListener::bind(directory.path().join("gateway.sock")).expect("listener");
    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = Arc::new(GatewayState::new(
        directory.path().to_owned(),
        principal,
        "token".into(),
    ));
    let exit = timeout(
        Duration::from_secs(2),
        accept_loop(listener, state, Duration::from_millis(40)),
    )
    .await
    .expect("unused gateway retires")
    .expect("accept loop");
    assert_eq!(exit, ExitCode::SUCCESS);
}

thread_local! {
    static FINISH_SLOT_PROBE: std::cell::RefCell<Option<Arc<StdMutex<Option<bool>>>>> =
        const { std::cell::RefCell::new(None) };
}

struct FinishSlotProbeGuard;

fn register_finish_slot_probe(result: Arc<StdMutex<Option<bool>>>) -> FinishSlotProbeGuard {
    FINISH_SLOT_PROBE.with(|probe| {
        assert!(
            probe.replace(Some(result)).is_none(),
            "finish-slot probe already set"
        );
    });
    FinishSlotProbeGuard
}

impl Drop for FinishSlotProbeGuard {
    fn drop(&mut self) {
        FINISH_SLOT_PROBE.with(|probe| {
            probe.borrow_mut().take();
        });
    }
}

pub(super) fn probe_finish_slot(semaphore: &Semaphore) {
    FINISH_SLOT_PROBE.with(|probe| {
        let Some(result) = probe.borrow().clone() else {
            return;
        };
        let available = semaphore.try_acquire().is_ok();
        if let Ok(mut observed) = result.lock() {
            *observed = Some(available);
        }
    });
}
use std::path::PathBuf;

use tokio::io::{AsyncWriteExt, BufReader};

use super::{
    AttachSlot, GatewayState, MAX_ATTACHES, MAX_REGISTRATION_BYTES, authenticate_registration,
    mint_hook_token, read_registration, read_registration_inner, validate_workspace,
};
use crate::commands::mcp::lifecycle::AttachRegistration;

#[test]
fn attach_cap_is_bounded_per_principal_channel() {
    assert_eq!(MAX_ATTACHES, 16);
}

#[test]
fn control_tokens_have_distinct_lifetimes_and_widths() {
    let boot_token = mint_boot_token();
    let hook_token = mint_hook_token();
    assert_eq!(boot_token.len(), 32);
    assert_eq!(hook_token.len(), 64);
    assert_ne!(boot_token, hook_token);
}

#[tokio::test]
async fn every_stopper_is_counted_until_its_ack_delivery_finishes() {
    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    );
    state.begin_stop_ack();
    state.begin_stop_ack();

    let waiting = state.wait_for_stop_acks();
    tokio::pin!(waiting);
    tokio::task::yield_now().await;
    state.finish_stop_ack();
    tokio::time::timeout(std::time::Duration::from_millis(20), &mut waiting)
        .await
        .expect_err("the final stopper must not release the run loop early");

    tokio::task::yield_now().await;
    state.finish_stop_ack();
    tokio::time::timeout(std::time::Duration::from_millis(1), &mut waiting)
        .await
        .expect("final ACK delivery must be bounded");
}

#[tokio::test]
async fn attach_cap_rejects_the_seventeenth_live_session() {
    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    );
    let mut permits = Vec::with_capacity(MAX_ATTACHES);
    for _ in 0..MAX_ATTACHES {
        permits.push(state.acquire("codex-code").await.expect("attach permit"));
    }
    assert!(state.acquire("codex-code").await.is_err());
    drop(permits);
    assert!(state.acquire("codex-code").await.is_ok());
}

#[tokio::test]
async fn registration_preface_times_out_without_a_newline() {
    let (_peer, stream) = tokio::io::duplex(1);
    let mut reader = BufReader::new(stream);
    let result = tokio::time::timeout(
        std::time::Duration::from_secs(2),
        read_registration(&mut reader),
    )
    .await
    .expect("registration timeout test must complete");
    let error = result.expect_err("a preface without a newline must time out");
    assert!(
        error
            .to_string()
            .contains("timed out reading MCP attach registration"),
        "unexpected error: {error}"
    );
}

#[tokio::test]
async fn oversized_registration_preface_fails_without_waiting_for_a_newline() {
    let (mut peer, stream) = tokio::io::duplex(MAX_REGISTRATION_BYTES + 1);
    peer.write_all(&vec![b'x'; MAX_REGISTRATION_BYTES + 1])
        .await
        .expect("oversized preface must fit in the test peer");
    let mut reader = BufReader::new(stream);
    let result = tokio::time::timeout(
        std::time::Duration::from_secs(2),
        read_registration_inner(&mut reader),
    )
    .await
    .expect("registration size test must complete");
    let error = result.expect_err("an oversized preface must be rejected");
    assert!(
        error
            .to_string()
            .contains("registration is missing or too large"),
        "unexpected error: {error}"
    );
}

#[test]
fn registration_workspace_must_be_absolute() {
    let workspace = tempfile::tempdir().expect("workspace tempdir");
    assert!(validate_workspace(&workspace.path().to_string_lossy()).is_ok());
    assert!(validate_workspace("project").is_err());
    assert!(validate_workspace("").is_err());
}

#[test]
fn forged_principal_cannot_select_another_gateway_uplink() {
    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let forged = astrid_core::PrincipalId::new("other-agent").expect("principal");
    let state = GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    );
    let registration = AttachRegistration {
        version: super::ATTACH_REGISTRATION_VERSION,
        principal: forged.to_string(),
        host: "codex".into(),
        workspace_abs: "/tmp".into(),
        host_session_id: "thread-1".into(),
        hook_token: "gateway-token".into(),
    };
    let error = authenticate_registration(&registration, &state)
        .expect_err("forged principal must be rejected");
    assert!(
        error
            .to_string()
            .contains("authenticated gateway principal")
    );
}

#[test]
fn missing_hook_token_is_rejected_before_uplink_selection() {
    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal.clone(),
        "gateway-token".into(),
    );
    let registration = AttachRegistration {
        version: super::ATTACH_REGISTRATION_VERSION,
        principal: principal.to_string(),
        host: "codex".into(),
        workspace_abs: "/tmp".into(),
        host_session_id: "thread-1".into(),
        hook_token: String::new(),
    };
    let error = authenticate_registration(&registration, &state)
        .expect_err("missing token must be rejected");
    assert!(error.to_string().contains("hook_token is invalid"));
}

#[test]
fn hook_token_is_minted_with_each_gateway_start() {
    let first = mint_hook_token();
    let second = mint_hook_token();
    assert_eq!(first.len(), 64);
    assert_ne!(first, second);
}

#[tokio::test]
async fn same_session_replaces_the_previous_attach() {
    use std::sync::{Arc, Mutex as StdMutex};

    use tokio::sync::Notify;
    use tokio::time::Instant;
    use tokio_util::sync::CancellationToken;

    use uuid::Uuid;

    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    );
    let permit = state.acquire("codex-code").await.expect("permit");
    let cancel = CancellationToken::new();
    let done = Arc::new(Notify::new());
    state
        .install_slot(
            "thread-1".into(),
            AttachSlot {
                id: Uuid::new_v4(),
                cancel: cancel.clone(),
                last_activity: Arc::new(StdMutex::new(Instant::now())),
                done: Arc::clone(&done),
            },
        )
        .await;
    tokio::spawn(async move {
        cancel.cancelled().await;
        drop(permit);
        done.notify_waiters();
    });
    state
        .replace_session("thread-1")
        .await
        .expect("replacement teardown must complete");
    let _permit = state
        .acquire("codex-code")
        .await
        .expect("replaced session must free the attach cap");
}

#[tokio::test]
async fn same_session_admission_serializes_reserve_acquire_install() {
    use std::sync::{Arc, Mutex as StdMutex};

    use tokio::sync::Notify;
    use tokio::time::{Duration, Instant, timeout};
    use tokio_util::sync::CancellationToken;
    use uuid::Uuid;

    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = Arc::new(GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    ));
    let first = state
        .reserve_session("thread-1", "codex-code")
        .await
        .expect("first reservation");
    let second_state = Arc::clone(&state);
    let mut second =
        tokio::spawn(async move { second_state.reserve_session("thread-1", "codex-code").await });
    tokio::task::yield_now().await;
    assert!(
        timeout(Duration::from_millis(20), &mut second)
            .await
            .is_err()
    );

    let first_id = Uuid::new_v4();
    let first_cancel = CancellationToken::new();
    let first_done = Arc::new(Notify::new());
    let first_permit = first
        .install(
            &state,
            "thread-1".into(),
            AttachSlot {
                id: first_id,
                cancel: first_cancel.clone(),
                last_activity: Arc::new(StdMutex::new(Instant::now())),
                done: Arc::clone(&first_done),
            },
        )
        .await;
    let cleanup_state = Arc::clone(&state);
    tokio::spawn(async move {
        first_cancel.cancelled().await;
        cleanup_state.take_slot_if("thread-1", first_id).await;
        drop(first_permit);
        first_done.notify_waiters();
    });

    let second = timeout(Duration::from_secs(1), &mut second)
        .await
        .expect("second reservation must complete")
        .expect("second reservation task")
        .expect("second reservation");
    let second_id = Uuid::new_v4();
    let second_done = Arc::new(Notify::new());
    let second_permit = second
        .install(
            &state,
            "thread-1".into(),
            AttachSlot {
                id: second_id,
                cancel: CancellationToken::new(),
                last_activity: Arc::new(StdMutex::new(Instant::now())),
                done: Arc::clone(&second_done),
            },
        )
        .await;
    assert_eq!(
        state.slots.lock().await.get("thread-1").map(|slot| slot.id),
        Some(second_id)
    );
    state
        .finish_slot("thread-1", second_id, second_permit, second_done)
        .await;
}

struct SilentServer;

impl rmcp::ServerHandler for SilentServer {}

#[tokio::test]
async fn replacement_waits_for_pending_rmcp_initialization() {
    use std::sync::atomic::{AtomicBool, Ordering};

    use tokio::net::UnixStream;
    use tokio::time::{Duration, timeout};
    use tokio_util::sync::CancellationToken;
    use uuid::Uuid;

    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = Arc::new(GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    ));
    let (peer, stream) = UnixStream::pair().expect("Unix stream pair");
    let (reader, write_half) = tokio::io::split(stream);
    let host_session_id = "thread-1";
    let slot_id = Uuid::new_v4();
    let cancel = CancellationToken::new();
    let done = Arc::new(Notify::new());
    let permit = state.acquire("codex-code").await.expect("old permit");
    state
        .install_slot(
            host_session_id.into(),
            AttachSlot {
                id: slot_id,
                cancel: cancel.clone(),
                last_activity: Arc::new(StdMutex::new(Instant::now())),
                done: Arc::clone(&done),
            },
        )
        .await;

    let peers = Arc::new(Mutex::new(HashMap::new()));
    let finished = Arc::new(AtomicBool::new(false));
    let cleanup_state = Arc::clone(&state);
    let cleanup_finished = Arc::clone(&finished);
    let cleanup_done = Arc::clone(&done);
    let cleanup_cancel = cancel.clone();
    let mut old_task = tokio::spawn(async move {
        let result = run_attached_session(
            SilentServer,
            reader,
            write_half,
            peers,
            slot_id,
            cleanup_cancel,
        )
        .await;
        cleanup_finished.store(true, Ordering::Release);
        cleanup_state
            .finish_slot(host_session_id, slot_id, permit, cleanup_done)
            .await;
        result
    });

    // Keep the transport open and silent: serve_with_ct must be cancellable
    // while waiting for the initialize request itself.
    let _peer = peer;
    let reservation = timeout(
        Duration::from_secs(2),
        state.reserve_session(host_session_id, "codex-code"),
    )
    .await
    .expect("replacement admission must be bounded")
    .expect("replacement must wait for the old RMCP session to finish");
    assert!(
        finished.load(Ordering::Acquire),
        "new admission must not proceed while the old attach is pending"
    );
    assert!(
        state.slots.lock().await.get(host_session_id).is_none(),
        "old slot must be removed before replacement admission"
    );
    let mut available = Vec::with_capacity(MAX_ATTACHES - 1);
    for _ in 0..MAX_ATTACHES - 1 {
        available.push(
            state
                .acquire("codex-code")
                .await
                .expect("old permit must be released before replacement admission"),
        );
    }
    drop(available);
    drop(reservation);

    let old_result = timeout(Duration::from_secs(1), &mut old_task)
        .await
        .expect("old attach cleanup must finish")
        .expect("old attach task");
    assert!(
        old_result.is_err(),
        "cancelled initialization must fail closed"
    );
    cancel.cancel();
}

#[tokio::test]
async fn replacement_timeout_rejects_new_admission() {
    use tokio::time::{Duration, timeout};
    use tokio_util::sync::CancellationToken;
    use uuid::Uuid;

    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    );
    let permit = state.acquire("codex-code").await.expect("old permit");
    state
        .install_slot(
            "thread-1".into(),
            AttachSlot {
                id: Uuid::new_v4(),
                cancel: CancellationToken::new(),
                last_activity: Arc::new(StdMutex::new(Instant::now())),
                done: Arc::new(Notify::new()),
            },
        )
        .await;

    let result = timeout(
        Duration::from_secs(2),
        state.reserve_session("thread-1", "codex-code"),
    )
    .await
    .expect("replacement timeout must be bounded");
    let Err(error) = result else {
        panic!("new admission must fail when teardown does not finish");
    };
    assert!(
        error.to_string().contains("replacement teardown timed out"),
        "unexpected replacement error: {error}"
    );
    drop(permit);
}

#[tokio::test]
async fn stale_session_cleanup_cannot_remove_a_replacement() {
    use std::sync::{Arc, Mutex as StdMutex};

    use tokio::sync::Notify;
    use tokio::time::Instant;
    use tokio_util::sync::CancellationToken;
    use uuid::Uuid;

    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    );
    let old_id = Uuid::new_v4();
    let old_cancel = CancellationToken::new();
    state
        .install_slot(
            "thread-1".into(),
            AttachSlot {
                id: old_id,
                cancel: old_cancel,
                last_activity: Arc::new(StdMutex::new(Instant::now())),
                done: Arc::new(Notify::new()),
            },
        )
        .await;
    assert!(state.take_slot_if("thread-1", old_id).await);

    let replacement_id = Uuid::new_v4();
    state
        .install_slot(
            "thread-1".into(),
            AttachSlot {
                id: replacement_id,
                cancel: CancellationToken::new(),
                last_activity: Arc::new(StdMutex::new(Instant::now())),
                done: Arc::new(Notify::new()),
            },
        )
        .await;
    assert!(!state.take_slot_if("thread-1", old_id).await);
    assert_eq!(
        state.slots.lock().await.get("thread-1").map(|slot| slot.id),
        Some(replacement_id)
    );
}

#[tokio::test]
async fn finish_slot_releases_cap_before_notifying_waiters() {
    use std::sync::{Arc, Mutex as StdMutex};

    use tokio::sync::Notify;
    use tokio::time::{Duration, timeout};
    use uuid::Uuid;

    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = Arc::new(GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    ));
    let observed = Arc::new(StdMutex::new(None));
    let _probe = register_finish_slot_probe(Arc::clone(&observed));
    let finishing = state.acquire("codex-code").await.expect("permit");
    let mut occupied = Vec::with_capacity(MAX_ATTACHES - 1);
    for _ in 0..MAX_ATTACHES - 1 {
        occupied.push(state.acquire("codex-code").await.expect("permit"));
    }
    let done = Arc::new(Notify::new());
    let notified = Arc::clone(&done).notified_owned();
    let waiter_state = Arc::clone(&state);
    let waiter = tokio::spawn(async move {
        notified.await;
        waiter_state.acquire("codex-code").await
    });
    tokio::task::yield_now().await;
    state
        .finish_slot("thread-1", Uuid::new_v4(), finishing, done)
        .await;
    assert_eq!(
        *observed.lock().expect("finish-slot probe mutex"),
        Some(true),
        "permit must be available at the notification boundary"
    );
    let permit = timeout(Duration::from_secs(1), waiter)
        .await
        .expect("waiter must wake")
        .expect("waiter task")
        .expect("released permit must be available before notification");
    drop(permit);
    drop(occupied);
}

#[tokio::test]
async fn acquire_evicts_idle_lru_then_admits() {
    use std::sync::{Arc, Mutex as StdMutex};

    use tokio::sync::Notify;
    use tokio::time::Instant;
    use tokio_util::sync::CancellationToken;

    use super::{ATTACH_IDLE_THRESHOLD, AttachSlot};
    use uuid::Uuid;

    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    );
    for index in 0..MAX_ATTACHES {
        let permit = state.acquire("codex-code").await.expect("permit");
        let cancel = CancellationToken::new();
        let done = Arc::new(Notify::new());
        let last = Instant::now()
            .checked_sub(ATTACH_IDLE_THRESHOLD + std::time::Duration::from_millis(5))
            .expect("idle timestamp");
        state
            .install_slot(
                format!("idle-{index}"),
                AttachSlot {
                    id: Uuid::new_v4(),
                    cancel: cancel.clone(),
                    last_activity: Arc::new(StdMutex::new(last)),
                    done: Arc::clone(&done),
                },
            )
            .await;
        tokio::spawn(async move {
            cancel.cancelled().await;
            drop(permit);
            done.notify_waiters();
        });
    }
    let _permit = state
        .acquire("codex-code")
        .await
        .expect("idle LRU eviction must admit a new attach");
}

#[tokio::test]
async fn acquire_does_not_evict_active_slots() {
    use std::sync::{Arc, Mutex as StdMutex};

    use tokio::sync::Notify;
    use tokio::time::Instant;
    use tokio_util::sync::CancellationToken;

    use uuid::Uuid;

    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    );
    let mut cancels = Vec::new();
    for index in 0..MAX_ATTACHES {
        let permit = state.acquire("codex-code").await.expect("permit");
        let cancel = CancellationToken::new();
        let done = Arc::new(Notify::new());
        state
            .install_slot(
                format!("live-{index}"),
                AttachSlot {
                    id: Uuid::new_v4(),
                    cancel: cancel.clone(),
                    last_activity: Arc::new(StdMutex::new(Instant::now())),
                    done: Arc::clone(&done),
                },
            )
            .await;
        cancels.push(cancel.clone());
        tokio::spawn(async move {
            cancel.cancelled().await;
            drop(permit);
            done.notify_waiters();
        });
    }
    assert!(state.acquire("codex-code").await.is_err());
    for cancel in cancels {
        cancel.cancel();
    }
}

#[tokio::test]
async fn forged_gateway_stop_cannot_cancel_the_process() {
    use tokio::io::AsyncReadExt;

    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = Arc::new(GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    ));
    let request = GatewayControlRequest {
        version: GATEWAY_CONTROL_VERSION,
        operation: GatewayControlOperation::Stop,
        pid: std::process::id(),
        hook_token: "forged-token".into(),
    };
    let (server, mut client) = tokio::io::duplex(4096);
    let connection = state.connection();

    serve_control(request, server, Arc::clone(&state), Some(connection))
        .await
        .expect("forged control receives a bounded rejection");
    let mut response = Vec::new();
    client
        .read_to_end(&mut response)
        .await
        .expect("control ACK");
    let ack: GatewayControlAck =
        serde_json::from_slice(&response).expect("rejection is valid JSON");
    assert!(!ack.ok);
    assert!(!state.shutdown.is_cancelled());
    assert_eq!(state.active_connections.load(Ordering::Acquire), 0);
}

#[tokio::test]
async fn authenticated_gateway_stop_waits_for_final_teardown_ack() {
    use tokio::io::AsyncReadExt;
    use tokio::time::{Duration, timeout};

    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = Arc::new(GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    ));
    let request = GatewayControlRequest {
        version: GATEWAY_CONTROL_VERSION,
        operation: GatewayControlOperation::Stop,
        pid: std::process::id(),
        hook_token: "gateway-token".into(),
    };
    let (server, mut client) = tokio::io::duplex(4096);
    let serving_state = Arc::clone(&state);
    let connection = state.connection();
    let serving = tokio::spawn(async move {
        serve_control(request, server, serving_state, Some(connection)).await
    });

    timeout(Duration::from_secs(1), state.shutdown.cancelled())
        .await
        .expect("authenticated stop must cancel the gateway");
    assert_eq!(state.active_connections.load(Ordering::Acquire), 0);
    assert!(
        timeout(Duration::from_millis(20), client.read_u8())
            .await
            .is_err(),
        "no success ACK may precede final teardown"
    );

    state
        .finish_shutdown(GatewayControlAck::success(
            GatewayControlOperation::Stop,
            std::process::id(),
        ))
        .await;
    let mut response = Vec::new();
    client
        .read_to_end(&mut response)
        .await
        .expect("control ACK");
    serving
        .await
        .expect("control task")
        .expect("authenticated control");
    let ack: GatewayControlAck = serde_json::from_slice(&response).expect("valid ACK JSON");
    assert!(ack.ok);
    timeout(Duration::from_secs(1), state.shutdown_ack_sent.notified())
        .await
        .expect("ACK completion notification retains its permit");
}

#[tokio::test]
async fn shutdown_waits_for_precounted_connections() {
    use tokio::time::{Duration, timeout};

    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = Arc::new(GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    ));
    let connection = state.connection();
    let waiting_state = Arc::clone(&state);
    let mut waiting = tokio::spawn(async move { waiting_state.wait_for_connections().await });

    assert!(
        timeout(Duration::from_millis(20), &mut waiting)
            .await
            .is_err(),
        "shutdown must not race past an accepted, not-yet-polled connection"
    );
    drop(connection);
    timeout(Duration::from_secs(1), waiting)
        .await
        .expect("connection drain")
        .expect("connection waiter");
}

#[tokio::test]
async fn concurrent_accepted_stoppers_receive_the_same_final_ack() {
    use tokio::io::AsyncWriteExt;
    use tokio::net::UnixListener;
    use tokio::time::{Duration, timeout};

    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = Arc::new(GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    ));
    let socket = std::env::temp_dir().join(format!("astrid-1809-stop-{}.sock", std::process::id()));
    let _ = std::fs::remove_file(&socket);
    let listener = UnixListener::bind(&socket).expect("short-path gateway listener");
    let accepting_state = Arc::clone(&state);
    let accepting = tokio::spawn(async move {
        accept_loop(listener, accepting_state, Duration::from_secs(30)).await
    });

    let request = serde_json::to_vec(&GatewayControlRequest {
        version: GATEWAY_CONTROL_VERSION,
        operation: GatewayControlOperation::Stop,
        pid: std::process::id(),
        hook_token: "gateway-token".into(),
    })
    .expect("control request");
    let mut clients = Vec::new();
    for _ in 0..2 {
        let mut client = tokio::net::UnixStream::connect(&socket)
            .await
            .expect("connect stopper");
        client
            .write_all(&request)
            .await
            .expect("queue stop request");
        client.write_all(b"\n").await.expect("terminate request");
        clients.push(client);
    }
    tokio::task::yield_now().await;
    state.shutdown.cancel();

    state
        .finish_shutdown(GatewayControlAck::success(
            GatewayControlOperation::Stop,
            std::process::id(),
        ))
        .await;
    timeout(Duration::from_secs(1), accepting)
        .await
        .expect("accept loop drains all stoppers")
        .expect("accept loop task")
        .expect("normal accept exit");

    let mut acks = Vec::new();
    for mut client in clients {
        let mut response = Vec::new();
        tokio::io::AsyncReadExt::read_to_end(&mut client, &mut response)
            .await
            .expect("control ACK");
        acks.push(serde_json::from_slice::<GatewayControlAck>(&response).expect("valid ACK"));
    }
    assert!(acks.iter().all(|ack| ack.ok));
    let _ = std::fs::remove_file(&socket);
}

#[tokio::test]
async fn shutdown_rejects_late_attach_admission() {
    let principal = astrid_core::PrincipalId::new("codex-code").expect("principal");
    let state = GatewayState::new(
        PathBuf::from("/runtime-home"),
        principal,
        "gateway-token".into(),
    );
    state.shutdown.cancel();

    let Err(error) = state.reserve_session("thread-late", "codex-code").await else {
        panic!("stop must close attach admission");
    };
    assert!(error.to_string().contains("shutting down"));
}

#[test]
fn gateway_cleanup_failure_does_not_mask_accept_failure() {
    let error = combine_gateway_results(
        Err(anyhow::anyhow!("gateway.accept")),
        Err(anyhow::anyhow!("gateway.listener_cleanup")),
    )
    .expect_err("both failures must be returned");
    let message = format!("{error:#}");
    assert!(message.starts_with("gateway.accept"));
    assert!(message.contains("additional gateway cleanup failure"));
    assert!(message.contains("gateway.listener_cleanup"));
}