lean-ctx-engine 3.12.2

LeanCTX | OSS library: the open-source runtime for the Context Gateway for AI agents behind the lean-ctx CLI, MCP server and proxy integrations.
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
// SPDX-License-Identifier: Apache-2.0
//! The protected MCP server's outside wrapper.
//!
//! Codex spawns this process; it spawns the sandboxed MCP server and then stays
//! alive as that server's supervisor. Two anonymous descriptors cross the fork
//! and never pass through Codex:
//!
//! * fd 3 — selected GitLab credentials, read only *after* Codex has spawned
//!   this wrapper, closed as soon as the handoff is acknowledged;
//! * fd 4 — the supervised execution channel. macOS refuses a nested
//!   `sandbox_apply`, so the MCP server cannot wrap its own command children in
//!   the launcher's profile; this wrapper runs outside that sandbox and is the
//!   only process that starts them.
//!
//! Both protected starts — with and without a selected GitLab source — run
//! through here, so the boundary is identical.
use crate::core::policy::runtime::{REQUIRED_POLICY_DIGEST_ENV, REQUIRED_POLICY_ROOT_ENV};
use crate::core::providers::selected_gitlab::Selection;
use std::path::{Path, PathBuf};

/// Client environment we are willing to forward. Everything else is dropped by
/// `env_clear`, including profile selectors, API keys and plugin/project config.
pub const CLIENT_ENV_PASSTHROUGH: &[&str] = &[
    "DO_NOT_TRACK",
    "PATH",
    "TERM",
    "TMPDIR",
    "LANG",
    "LC_ALL",
    "LC_CTYPE",
    "LC_MESSAGES",
];

/// Environment forwarded to the MCP server only (never to the Codex client), so
/// the server resolves the user's existing lean-ctx config, data and providers.
pub const MCP_ENV_PASSTHROUGH: &[&str] = &[
    "DO_NOT_TRACK",
    "LEAN_CTX_CACHE_DIR",
    "LEAN_CTX_CONFIG_DIR",
    "LEAN_CTX_DATA_DIR",
    "LEAN_CTX_STATE_DIR",
    "LEANCTX_ORG_POLICY",
    "XDG_CACHE_HOME",
    "XDG_CONFIG_HOME",
    "XDG_DATA_HOME",
    "XDG_STATE_HOME",
];

pub fn utf8_path(path: &Path, label: &str) -> Result<String, String> {
    path.to_str().map(str::to_string).ok_or_else(|| {
        format!("{label} is not valid UTF-8; this launcher cannot express it in Codex config")
    })
}

pub fn ensure_executable_file(path: &Path) -> Result<(), String> {
    let meta = std::fs::metadata(path)
        .map_err(|err| format!("could not stat {}: {err}", path.display()))?;
    if !meta.is_file() {
        return Err(format!("{} is not a regular file", path.display()));
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if meta.permissions().mode() & 0o111 == 0 {
            return Err(format!("{} is not executable", path.display()));
        }
    }
    Ok(())
}

#[cfg(windows)]
mod windows;

pub const WRAPPER: &str = "__protected-gitlab-mcp";
const FD_FLAG: &str = "--protected-gitlab-fd";
#[cfg(unix)]
const MAX_FRAME: usize = 32 * 1024;

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Launch {
    pub source: Selection,
    pub glab: PathBuf,
    pub config_dir: PathBuf,
}

pub fn prepare(source: Selection, glab: Option<&str>, home: &Path) -> Result<Launch, String> {
    source.validate()?;
    let glab = if let Some(path) = glab {
        PathBuf::from(path)
    } else {
        std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default())
            .map(|directory| directory.join("glab"))
            .find(|path| path.is_file())
            .ok_or("install glab or select its absolute path with --glab")?
    }
    .canonicalize()
    .map_err(|_| "cannot resolve glab executable")?;
    ensure_executable_file(&glab)?;
    let config_dir = std::env::var_os("GLAB_CONFIG_DIR")
        .map(PathBuf::from)
        .unwrap_or_else(|| home.join("Library/Application Support/glab-cli"));
    if !config_dir.is_absolute() {
        return Err("GLAB_CONFIG_DIR must be absolute".into());
    }
    utf8_path(&config_dir, "glab configuration directory")?;
    Ok(Launch {
        source,
        glab,
        config_dir,
    })
}

/// SDK hosts select metadata only; the Engine retains the same bounded,
/// in-memory credential reader and immutable provider binding as Codex.
pub fn initialize_agent_gitlab(
    source: Selection,
    glab: &Path,
    config_dir: Option<&Path>,
    project: &Path,
) -> Result<(), String> {
    #[cfg(unix)]
    {
        let home = dirs::home_dir()
            .and_then(|path| path.canonicalize().ok())
            .ok_or("credential home unavailable")?;
        let glab_path = utf8_path(glab, "glab executable")?;
        let mut launch = prepare(source, Some(&glab_path), &home)?;
        if let Some(path) = config_dir {
            launch.config_dir = path.to_path_buf();
        } else if cfg!(not(target_os = "macos")) {
            launch.config_dir = home.join(".config/glab-cli");
        }
        launch.config_dir = launch
            .config_dir
            .canonicalize()
            .map_err(|_| "credential configuration unavailable")?;
        // No selected credential material or executable can be a project input.
        if launch.glab.starts_with(project)
            || launch.config_dir.starts_with(project)
            || home.starts_with(project)
        {
            return Err("credential authority overlaps project inputs".into());
        }
        unix::disable_core_dumps()?;
        let token = unix::credential(&launch, &home)?;
        crate::core::providers::selected_gitlab::install(launch.source, token.to_string())
    }
    #[cfg(windows)]
    {
        windows::initialize(source, glab, config_dir, project)
    }
    #[cfg(not(any(unix, windows)))]
    {
        let _ = (source, glab, config_dir, project);
        Err("selected GitLab SDK credential transport unavailable on this platform".into())
    }
}

/// Runs before logging, telemetry, threads or tools can observe a descriptor.
pub fn dispatch(args: &[String]) -> Result<Option<i32>, String> {
    const EXEC_FD_FLAG: &str = "--protected-exec-fd";

    let wrapper = args.get(1).is_some_and(|arg| arg == WRAPPER);
    let receiver = args.get(1).is_some_and(|arg| arg == "mcp")
        && args.iter().any(|arg| arg == FD_FLAG || arg == EXEC_FD_FLAG);
    if !wrapper && !receiver {
        return Ok(None);
    }
    if !cfg!(target_os = "macos") {
        return Err("protected GitLab bootstrap is qualified on macOS only".into());
    }
    if std::env::var(REQUIRED_POLICY_ROOT_ENV).is_err()
        || std::env::var(REQUIRED_POLICY_DIGEST_ENV).is_err()
    {
        return Err("protected GitLab bootstrap requires the selected policy authority".into());
    }
    #[cfg(unix)]
    {
        unix::disable_core_dumps()?;
        if wrapper {
            if args.len() != 4 {
                return Err("invalid protected wrapper arguments".into());
            }
            // `null` is the ordinary protected start; a selected GitLab source
            // is the same wrapper carrying a launch configuration.
            let launch: Option<Launch> = serde_json::from_str(&args[2])
                .map_err(|_| "invalid protected launch configuration")?;
            if let Some(launch) = &launch {
                launch.source.validate()?;
            }
            return unix::run(launch.as_ref(), &args[3]).map(Some);
        }
        // `mcp [--protected-gitlab-fd N] --protected-exec-fd N`, and nothing
        // else. The credential channel is adopted first: it is read and closed
        // before the execution channel can serve anything.
        let mut index = 2;
        let mut credential: Option<i32> = None;
        let mut execution: Option<i32> = None;
        while index < args.len() {
            let fd: i32 = args
                .get(index + 1)
                .ok_or("invalid protected descriptor arguments")?
                .parse()
                .map_err(|_| "invalid protected descriptor")?;
            let slot = match args[index].as_str() {
                FD_FLAG => &mut credential,
                EXEC_FD_FLAG => &mut execution,
                _ => return Err("invalid protected descriptor arguments".into()),
            };
            if slot.is_some() {
                return Err("duplicate protected descriptor".into());
            }
            *slot = Some(fd);
            index += 2;
        }
        if let Some(fd) = credential {
            unix::receive(fd)?;
        }
        let execution =
            execution.ok_or("protected MCP start without a supervised execution channel")?;
        crate::core::protected_execution::install_client(execution)?;
        Ok(None)
    }
    #[cfg(not(unix))]
    Err("protected GitLab descriptor transport unavailable".into())
}

#[cfg(unix)]
mod unix {
    use super::{CLIENT_ENV_PASSTHROUGH, MCP_ENV_PASSTHROUGH};
    use super::{
        FD_FLAG, Launch, MAX_FRAME, REQUIRED_POLICY_DIGEST_ENV, REQUIRED_POLICY_ROOT_ENV, Selection,
    };
    use crate::core::protected_store;
    use std::io::{Read, Write};
    use std::os::fd::{AsRawFd, FromRawFd};
    use std::os::unix::net::UnixStream;
    use std::os::unix::process::CommandExt;
    use std::process::{Command, Stdio};
    use std::time::{Duration, Instant};
    use zeroize::Zeroizing;

    const DEADLINE: Duration = Duration::from_secs(10);
    /// Credential channel in the MCP child. Read and closed during startup.
    const CHILD_FD: i32 = 3;
    /// Supervised execution channel in the MCP child; lives for the session.
    const EXEC_FD: i32 = crate::core::protected_execution::CHILD_EXEC_FD;

    pub(super) fn disable_core_dumps() -> Result<(), String> {
        let limit = libc::rlimit {
            rlim_cur: 0,
            rlim_max: 0,
        };
        // No credential-bearing process is permitted to write a core file.
        // SAFETY: limit is initialized and lives throughout this synchronous syscall.
        if unsafe { libc::setrlimit(libc::RLIMIT_CORE, &raw const limit) } != 0 {
            return Err("cannot disable credential-process core dumps".into());
        }
        Ok(())
    }

    fn read_bounded(stream: &mut UnixStream, max: usize) -> Result<Zeroizing<Vec<u8>>, String> {
        stream
            .set_nonblocking(true)
            .map_err(|_| "cannot bound credential channel")?;
        let end = Instant::now() + DEADLINE;
        let mut output = Zeroizing::new(Vec::new());
        let mut buffer = Zeroizing::new([0_u8; 4096]);
        loop {
            if Instant::now() >= end {
                return Err("credential channel timed out".into());
            }
            match stream.read(buffer.as_mut()) {
                Ok(0) => return Ok(output),
                Ok(count) => {
                    if output.len() + count > max {
                        return Err("credential channel exceeded size limit".into());
                    }
                    output.extend_from_slice(&buffer[..count]);
                }
                Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {}
                Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
                    std::thread::sleep(Duration::from_millis(5));
                }
                Err(_) => return Err("credential channel failed".into()),
            }
        }
    }

    /// Unlike process_capture this never spools stdout or stderr to disk.
    pub(super) fn credential(
        launch: &Launch,
        home: &std::path::Path,
    ) -> Result<Zeroizing<String>, String> {
        let (mut read, write) =
            UnixStream::pair().map_err(|_| "cannot create credential channel")?;
        let mut reader = Command::new(&launch.glab);
        reader
            .args([
                "config",
                "get",
                "token",
                "--global",
                "--host",
                &launch.source.host,
            ])
            .env_clear()
            .env("HOME", home)
            .env("GLAB_CONFIG_DIR", &launch.config_dir)
            .env("GLAB_SEND_TELEMETRY", "false")
            .env("GLAB_CHECK_UPDATE", "false")
            .current_dir("/")
            .stdin(Stdio::null())
            .stdout(Stdio::from(std::os::fd::OwnedFd::from(write)))
            .stderr(Stdio::null())
            .process_group(0);
        // Q13: the reader leads its own process group and has a guard on it,
        // which ends the group when this read is over or this process is
        // gone. The reader's program is executed only once the guard is
        // ready; a reader that cannot get a guard never started. `None` for
        // the guard: this process is not the `lean-ctx` binary. The guard
        // holds neither end of the credential channel (both are close-on-exec)
        // and writes nothing to disk.
        let (mut child, guard) = match crate::shell::containment::start_guarded_child(&mut reader) {
            Ok(started) => started,
            Err(crate::shell::containment::Refusal::NotStarted(_)) => {
                return Err("cannot start selected glab credential reader".into());
            }
            Err(_) => return Err("cannot contain selected glab credential reader".into()),
        };
        // The command holds the reader's end of the channel; with it open the
        // end of the reader's output could not be seen.
        drop(reader);
        let end = Instant::now() + DEADLINE;
        let result = read_bounded(&mut read, 16 * 1024);
        let status = loop {
            match child.try_wait() {
                Ok(Some(status)) => break Some(status),
                Ok(None) if Instant::now() < end && result.is_ok() => {
                    std::thread::sleep(Duration::from_millis(5));
                }
                _ => break None,
            }
        };
        // A successful try_wait has already reaped the leader. Never signal
        // that recycled PID; only kill a group whose leader is still ours.
        // SAFETY: the child was spawned with its own process group; the negative
        // PID addresses that owned group and no pointers cross the syscall.
        if status.is_none() {
            // SAFETY: only the unreaped leader's own process group is addressed.
            unsafe {
                libc::kill(-(child.id() as i32), libc::SIGKILL);
            }
        }
        let _ = child.wait();
        if let Some(guard) = guard {
            let _ = guard.finish();
        }
        if !status.is_some_and(|status| status.success()) {
            return Err("selected glab credential could not be read".into());
        }
        let bytes = result?;
        let text = std::str::from_utf8(&bytes).map_err(|_| "invalid selected GitLab credential")?;
        let token = text.trim_end_matches(['\r', '\n']);
        if token.is_empty() || !token.bytes().all(|b| (33..=126).contains(&b)) {
            return Err("invalid selected GitLab credential".into());
        }
        Ok(Zeroizing::new(token.to_owned()))
    }

    /// The credential handoff frame, built before the MCP server starts and
    /// dropped as soon as it is acknowledged. Only the selected-GitLab start
    /// produces one; every other step below is shared.
    fn handoff(launch: &Launch) -> Result<Zeroizing<Vec<u8>>, String> {
        let home = std::env::var_os("HOME").ok_or("credential home unavailable")?;
        let token = credential(launch, std::path::Path::new(&home))?;
        #[derive(serde::Serialize)]
        struct Frame<'a> {
            version: u8,
            source: &'a Selection,
            token: &'a str,
        }
        let frame = Zeroizing::new(
            serde_json::to_vec(&Frame {
                version: 1,
                source: &launch.source,
                token: &token,
            })
            .map_err(|_| "cannot encode credential handoff")?,
        );
        drop(token);
        if frame.len() > MAX_FRAME {
            return Err("credential handoff exceeded size limit".into());
        }
        Ok(frame)
    }

    /// Start the sandboxed MCP server and supervise it. Returns the server's
    /// exit code; the wrapper lives exactly as long as the session does.
    pub(super) fn run(launch: Option<&Launch>, profile: &str) -> Result<i32, String> {
        // The immutable boundary every command child of this session runs
        // under. Built here, outside the MCP sandbox, from the rules the
        // launcher pinned — never from a request or a writable file.
        let boundary = crate::core::protected_execution::LaunchProfile::from_pinned(profile)?;
        protected_store::validate(boundary.shell_profile())?;

        let handoff_frame = match launch {
            Some(launch) => {
                if !launch.glab.is_absolute() || !launch.config_dir.is_absolute() {
                    return Err("selected credential paths must be absolute".into());
                }
                Some(handoff(launch)?)
            }
            None => None,
        };

        let (supervisor, worker) =
            UnixStream::pair().map_err(|_| "cannot create the supervised execution channel")?;
        let (mut send, receive) = match handoff_frame {
            Some(_) => {
                let (send, receive) =
                    UnixStream::pair().map_err(|_| "cannot create MCP credential channel")?;
                send.set_write_timeout(Some(DEADLINE))
                    .map_err(|_| "cannot bound MCP credential channel")?;
                send.set_read_timeout(Some(DEADLINE))
                    .map_err(|_| "cannot bound MCP acknowledgement")?;
                (Some(send), Some(receive))
            }
            None => (None, None),
        };

        let credential_fd = receive.as_ref().map_or(-1, AsRawFd::as_raw_fd);
        let worker_fd = worker.as_raw_fd();
        let binary = std::env::current_exe().map_err(|_| "MCP executable unavailable")?;
        let mut arguments = vec!["mcp".to_string()];
        if receive.is_some() {
            arguments.push(FD_FLAG.to_string());
            arguments.push(CHILD_FD.to_string());
        }
        arguments.push(crate::core::protected_execution::EXEC_FD_FLAG.to_string());
        arguments.push(crate::core::protected_execution::CHILD_EXEC_FD.to_string());
        let mut command = Command::new(protected_store::SANDBOX_EXEC);
        command
            .args(["-p", profile])
            .arg(binary)
            .args(&arguments)
            .env_clear();
        // Forward exactly the frozen nonsecret MCP authority, never ambient
        // tokens, proxies, injection options or glab configuration selectors.
        for name in [
            "HOME",
            "LEAN_CTX_PROJECT_ROOT",
            REQUIRED_POLICY_ROOT_ENV,
            REQUIRED_POLICY_DIGEST_ENV,
            protected_store::CHILD_PROFILE_ENV,
        ]
        .iter()
        .chain(MCP_ENV_PASSTHROUGH.iter())
        .chain(CLIENT_ENV_PASSTHROUGH.iter())
        {
            if let Some(value) = std::env::var_os(name) {
                command.env(name, value);
            }
        }
        // SAFETY: both ends remain owned and open through spawn; pre_exec
        // invokes only async-signal-safe descriptor syscalls, with no
        // allocation or locks. Each descriptor is first duplicated above the
        // fixed range, so placing one cannot clobber the other's source.
        unsafe {
            command.pre_exec(move || {
                let credential_hold = if credential_fd >= 0 {
                    libc::fcntl(credential_fd, libc::F_DUPFD, 10)
                } else {
                    -1
                };
                if credential_fd >= 0 && credential_hold < 0 {
                    return Err(std::io::Error::last_os_error());
                }
                let worker_hold = libc::fcntl(worker_fd, libc::F_DUPFD, 10);
                if worker_hold < 0 {
                    return Err(std::io::Error::last_os_error());
                }
                if credential_hold >= 0
                    && (libc::dup2(credential_hold, CHILD_FD) < 0
                        || libc::fcntl(CHILD_FD, libc::F_SETFD, 0) < 0)
                {
                    return Err(std::io::Error::last_os_error());
                }
                if libc::dup2(worker_hold, EXEC_FD) < 0
                    || libc::fcntl(EXEC_FD, libc::F_SETFD, 0) < 0
                {
                    return Err(std::io::Error::last_os_error());
                }
                // The holds are only staging; the child must inherit exactly
                // the two fixed descriptors and nothing else.
                if credential_hold >= 0 {
                    libc::close(credential_hold);
                }
                libc::close(worker_hold);
                Ok(())
            });
        }
        let mut child = command.spawn().map_err(|_| "cannot start protected MCP")?;
        drop(receive);
        // The wrapper keeps only the supervisor end; the worker end now lives
        // in the MCP server, which seals it before any helper can observe it.
        drop(worker);

        if let (Some(mut send), Some(frame)) = (send.take(), handoff_frame) {
            // Keep the peer alive until admission is acknowledged: on macOS a
            // full peer close makes getpeername fail even while unread bytes
            // remain.
            let sent = send
                .write_all(&frame)
                .and_then(|()| send.shutdown(std::net::Shutdown::Write))
                .and_then(|()| {
                    let mut acknowledgement = [0_u8; 1];
                    send.read_exact(&mut acknowledgement)?;
                    if acknowledgement != [1] {
                        return Err(std::io::Error::other("invalid MCP acknowledgement"));
                    }
                    Ok(())
                });
            drop(send);
            drop(frame);
            if sent.is_err() {
                let _ = child.kill();
                let _ = child.wait();
                return Err("MCP credential handoff failed".into());
            }
        }

        // Supervise this session's command children until the MCP server
        // disconnects. A supervisor that cannot serve takes the session with
        // it: there is no unsupervised second half of a protected session.
        if let Err(error) = crate::core::protected_execution::serve(supervisor, boundary) {
            let _ = child.kill();
            let _ = child.wait();
            return Err(error);
        }
        finish_mcp(&mut child, Duration::from_secs(5))
    }

    fn finish_mcp(child: &mut std::process::Child, grace: Duration) -> Result<i32, String> {
        let deadline = Instant::now() + grace;
        loop {
            match child.try_wait() {
                Ok(Some(status)) => return Ok(status.code().unwrap_or(1)),
                Ok(None) if Instant::now() < deadline => {
                    std::thread::sleep(Duration::from_millis(10));
                }
                _ => {
                    let _ = child.kill();
                    let _ = child.wait();
                    return Err("protected execution channel ended before MCP shutdown".into());
                }
            }
        }
    }

    #[test]
    fn a_closed_execution_channel_cannot_leave_a_live_mcp_child() {
        let mut child = Command::new("/bin/sleep").arg("120").spawn().unwrap();
        let result = finish_mcp(&mut child, Duration::from_millis(20));
        assert!(result.is_err());
        assert!(child.try_wait().unwrap().is_some());
    }

    pub(super) fn receive(fd: i32) -> Result<(), String> {
        if fd <= 2 {
            return Err("invalid protected GitLab descriptor".into());
        }
        // Validate socket type without taking ownership of arbitrary descriptors.
        let mut kind: libc::c_int = 0;
        let mut len = std::mem::size_of_val(&kind) as libc::socklen_t;
        // SAFETY: kind/len are initialized writable values of the sizes supplied.
        if unsafe {
            libc::getsockopt(
                fd,
                libc::SOL_SOCKET,
                libc::SO_TYPE,
                (&raw mut kind).cast(),
                &raw mut len,
            )
        } != 0
            || kind != libc::SOCK_STREAM
        {
            return Err("GitLab bootstrap requires an anonymous stream socket".into());
        }
        // SAFETY: the early CLI receiver owns the inherited descriptor exactly
        // once, before other code can use it; getsockopt verified it is open.
        let mut stream = unsafe { UnixStream::from_raw_fd(fd) };
        if !stream
            .local_addr()
            .is_ok_and(|address| address.is_unnamed())
            || !stream.peer_addr().is_ok_and(|address| address.is_unnamed())
        {
            return Err("GitLab bootstrap requires an anonymous connected socket pair".into());
        }
        // SAFETY: stream owns the open descriptor and F_SETFD takes integer flags.
        if unsafe { libc::fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC) } < 0 {
            return Err("cannot seal credential descriptor".into());
        }
        let bytes = read_bounded(&mut stream, MAX_FRAME)?;
        #[derive(serde::Deserialize)]
        #[serde(deny_unknown_fields)]
        struct Frame {
            version: u8,
            source: Selection,
            #[serde(deserialize_with = "secret_string")]
            token: Zeroizing<String>,
        }
        fn secret_string<'de, D: serde::Deserializer<'de>>(
            deserializer: D,
        ) -> Result<Zeroizing<String>, D::Error> {
            serde::Deserialize::deserialize(deserializer).map(Zeroizing::new)
        }
        let frame: Frame =
            serde_json::from_slice(&bytes).map_err(|_| "invalid GitLab bootstrap frame")?;
        if frame.version != 1 {
            return Err("unsupported GitLab bootstrap version".into());
        }
        crate::core::providers::selected_gitlab::install(frame.source, frame.token.to_string())?;
        stream
            .set_nonblocking(false)
            .map_err(|_| "cannot acknowledge credential handoff")?;
        stream
            .set_write_timeout(Some(DEADLINE))
            .map_err(|_| "cannot bound credential acknowledgement")?;
        stream
            .write_all(&[1])
            .map_err(|_| "credential acknowledgement failed")?;
        drop(stream);
        Ok(())
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        #[test]
        fn memory_channel_bounds_and_eof() {
            let (mut read, mut write) = UnixStream::pair().unwrap();
            write.write_all(b"synthetic").unwrap();
            drop(write);
            assert_eq!(read_bounded(&mut read, 9).unwrap().as_slice(), b"synthetic");
            let (mut read, mut write) = UnixStream::pair().unwrap();
            write.write_all(b"too long").unwrap();
            drop(write);
            assert!(read_bounded(&mut read, 2).is_err());
        }
        /// Q13: the credential reader runs with a process guard, and what it
        /// starts in its group does not outlive the read. The stand-in `glab`
        /// lives in a temp dir, touches no network and prints a fixed value
        /// that is not shaped like a credential.
        #[test]
        fn the_credential_reader_leaves_nothing_behind() {
            crate::shell::containment::tests::run_external_fixture(
                module_path!(),
                "credential_reader_fixture",
            );
        }

        /// Body of the test above; runs in a fixture process group of its own
        /// that is ended when this returns or an assertion fails. The
        /// reader's background process is in the reader's group, not the
        /// fixture's: if a regression leaves it, it expires by itself (20 s).
        #[test]
        #[ignore = "fixture body of the_credential_reader_leaves_nothing_behind"]
        fn credential_reader_fixture() {
            use std::os::unix::fs::PermissionsExt as _;
            let fixture = crate::shell::containment::tests::external_fixture();
            let directory = tempfile::tempdir().unwrap();
            let glab = directory.path().join("glab");
            let left = directory.path().join("left.pid");
            std::fs::write(
                &glab,
                format!(
                    "#!/bin/sh\n/bin/sleep 20 >/dev/null 2>&1 &\necho $! > '{}'\necho not-a-credential\n",
                    left.display()
                ),
            )
            .unwrap();
            std::fs::set_permissions(&glab, std::fs::Permissions::from_mode(0o700)).unwrap();
            let launch = Launch {
                source: Selection {
                    host: "gitlab.example.test".into(),
                    project: 1,
                    namespace: "group".into(),
                },
                glab,
                config_dir: directory.path().to_path_buf(),
            };
            let value = credential(&launch, directory.path()).unwrap();
            assert_eq!(value.as_str(), "not-a-credential");
            let pid: libc::pid_t = std::fs::read_to_string(&left)
                .unwrap()
                .trim()
                .parse()
                .unwrap();
            let deadline = Instant::now() + Duration::from_secs(5);
            // SAFETY: signal 0 only probes for existence.
            while unsafe { libc::kill(pid, 0) } == 0 {
                assert!(
                    Instant::now() < deadline,
                    "the reader's background process outlived the read"
                );
                std::thread::sleep(Duration::from_millis(20));
            }
            // The temp dir is removed here; the group kill that follows
            // would skip its destructor.
            drop(directory);
            fixture.complete();
        }

        /// Q13: the credential reader is not executed before its guard is
        /// ready. A caller killed in between leaves nothing running, and the
        /// reader's program never ran.
        #[test]
        fn the_credential_reader_never_runs_before_its_guard_is_ready() {
            crate::shell::containment::tests::run_external_fixture(
                module_path!(),
                "credential_caller_killed_fixture",
            );
        }

        /// Body of the test above: the stand-in below is killed between "the
        /// reader's process exists" and "attach its guard".
        #[test]
        #[ignore = "fixture body of the_credential_reader_never_runs_before_its_guard_is_ready"]
        fn credential_caller_killed_fixture() {
            let fixture = crate::shell::containment::tests::external_fixture();
            crate::shell::containment::tests::caller_killed_before_its_guard(
                module_path!(),
                "credential_caller_standin",
            );
            fixture.complete();
        }

        /// Stands in for a launcher in the middle of the credential read. Its
        /// `glab` lives beside the mark, touches no network, and would leave
        /// the mark and keep running.
        #[test]
        #[ignore = "stand-in caller of credential_caller_killed_fixture"]
        fn credential_caller_standin() {
            use std::os::unix::fs::PermissionsExt as _;
            let marker = crate::shell::containment::tests::caller_standin_marker();
            let directory = marker.parent().unwrap();
            let glab = directory.join("glab");
            std::fs::write(
                &glab,
                format!(
                    "#!/bin/sh\necho ran > '{}'\n/bin/sleep 60\n",
                    marker.display()
                ),
            )
            .unwrap();
            std::fs::set_permissions(&glab, std::fs::Permissions::from_mode(0o700)).unwrap();
            let launch = Launch {
                source: Selection {
                    host: "gitlab.example.test".into(),
                    project: 1,
                    namespace: "group".into(),
                },
                glab,
                config_dir: directory.to_path_buf(),
            };
            let _ = credential(&launch, directory);
        }
        #[test]
        fn standard_streams_and_closed_descriptors_are_rejected() {
            for fd in [-1, 0, 1, 2, i32::MAX] {
                assert!(receive(fd).is_err());
            }
        }
    }
}