magi-cli 0.240.1

Blind multi-agent implementation competition: N agents implement, M judges rank blind, deliberate, vote privately, winner survives double review + E2E gate
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
//! Turn-scoped inherited pipes plus authenticated local socket recovery.
//!
//! The launcher executes typed [`crate::chat_control::Operation`]s as the talk fixed
//! at startup. The channel cannot name a different talk or execute a command.
//! An unlinked file descriptor supplies process-associated POSIX record locks
//! so concurrent CLI tool calls cannot interleave frames on the shared pipes.
use std::io::Write as _;
use std::os::fd::{AsRawFd as _, FromRawFd as _, RawFd};
use std::path::Path;

use anyhow::{Context as _, Result, bail, ensure};
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};

use super::{MAX_FRAME, Operation, Request, Response};

pub const CHANNEL_ENV: &str = "MAGI_CHAT_CONTROL_FD";
const RESPONSE_ENV: &str = "MAGI_CHAT_CONTROL_RESPONSE_FD";
const LOCK_ENV: &str = "MAGI_CHAT_CONTROL_LOCK_FD";
mod socket;
const IDENTITY_ENV: &str = "MAGI_CHAT_CONTROL_IDENTITIES";
const MAX_STALE_RESPONSES: usize = 64;

/// Only transport preparation failures are safe to fall back from: no request
/// bytes have been sent, so local execution cannot duplicate an operation.
#[derive(Debug, thiserror::Error)]
#[error("prepare authenticated chat control transport (no operation sent)")]
pub(crate) struct Unavailable;

pub(crate) struct Bridge {
    socket: Option<socket::Socket>,
    requests: std::fs::File,
    responses: std::fs::File,
    lock: std::fs::File,
    server: tokio::task::JoinHandle<()>,
}

impl Bridge {
    /// Failure disables controls for this invocation, never the conversation.
    pub fn start(home: &Path, talk: &str, artifacts: &Path) -> Result<Self> {
        let talks = crate::talk::Talks::at(home.join("talks"));
        super::super::authorize(&talks, talk, crate::queue::CHAT_NODE)?;
        let source = crate::chat_goal::current_source();
        // Anonymous pipes need neither a path grant nor network access.
        let (requests, mut incoming) = tokio::net::unix::pipe::pipe()?;
        let (mut outgoing, responses) = tokio::net::unix::pipe::pipe()?;
        let requests = std::fs::File::from(requests.into_blocking_fd()?);
        let responses = std::fs::File::from(responses.into_blocking_fd()?);
        let path = artifacts.join(format!(".control-{}.lock", crate::rng::entropy()));
        let lock = std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .create_new(true)
            .open(&path)?;
        std::fs::remove_file(path)?;
        let serial = socket::Serial::default();
        let socket = match socket::Socket::start(home, talk, serial.clone(), source.clone()) {
            Ok(socket) => Some(socket),
            Err(error) => {
                tracing::warn!(%error, "chat control socket unavailable; inherited descriptor transport remains available");
                None
            }
        };
        let home = home.to_owned();
        let talk = talk.to_owned();
        let server = tokio::spawn(async move {
            let result: Result<()> = async {
                loop {
                    let len = incoming.read_u32().await? as usize;
                    ensure!(len <= MAX_FRAME, "control request too large");
                    let mut body = vec![0; len];
                    incoming.read_exact(&mut body).await?;
                    let Request { id, operation } = serde_json::from_slice(&body)?;
                    let _guard = serial.lock().await;
                    let result = super::super::execute_authenticated(
                        &crate::queue::Queue::at(home.join("queue")),
                        &home,
                        &talks,
                        &talk,
                        source.clone(),
                        operation,
                    )
                    .await
                    .map_err(|e| format!("{e:#}"));
                    drop(_guard);
                    let response = serde_json::to_vec(&Response { id, result })?;
                    ensure!(response.len() <= MAX_FRAME, "control response too large");
                    outgoing.write_u32(response.len() as u32).await?;
                    outgoing.write_all(&response).await?;
                }
            }
            .await;
            if let Err(error) = result {
                tracing::debug!(%error, "chat control channel closed");
            }
        });
        Ok(Self {
            socket,
            requests,
            responses,
            lock,
            server,
        })
    }

    pub fn attach(&self, command: &mut tokio::process::Command) {
        let requests = self.requests.as_raw_fd();
        let responses = self.responses.as_raw_fd();
        let lock = self.lock.as_raw_fd();
        if let Some(socket) = &self.socket {
            socket.attach(command);
        }
        let identities: Vec<_> = [requests, responses, lock]
            .into_iter()
            .map(identity)
            .collect::<Result<_>>()
            .expect("launcher owns control descriptors");
        command.env(
            IDENTITY_ENV,
            serde_json::to_string(&identities).expect("descriptor identities serialize"),
        );
        command
            .env(CHANNEL_ENV, requests.to_string())
            .env(RESPONSE_ENV, responses.to_string())
            .env(LOCK_ENV, lock.to_string());
        // SAFETY: fcntl is async-signal-safe. Clear CLOEXEC only after fork,
        // leaving the launcher's descriptors private to unrelated children.
        unsafe {
            command.pre_exec(move || {
                for fd in [requests, responses, lock] {
                    if libc::fcntl(fd, libc::F_SETFD, 0) == -1 {
                        return Err(std::io::Error::last_os_error());
                    }
                }
                Ok(())
            });
        }
    }
}

impl Drop for Bridge {
    fn drop(&mut self) {
        // Cancellation, fallbacks and normal completion revoke the capability.
        self.server.abort();
    }
}

/// Drop inherited authority in detached children, as well as removing its env.
/// Inspect in the parent; the child hook uses only async-signal-safe syscalls.
pub(super) fn close_inherited(command: &mut std::process::Command) {
    use std::os::unix::process::CommandExt as _;
    let Ok(encoded) = std::env::var(IDENTITY_ENV) else {
        return;
    };
    let Ok(identities) = serde_json::from_str::<Vec<[String; 4]>>(&encoded) else {
        return;
    };
    let mut descriptors = Vec::new();
    for (name, expected) in [CHANNEL_ENV, RESPONSE_ENV, LOCK_ENV]
        .into_iter()
        .zip(identities)
    {
        let Some(fd) = std::env::var(name)
            .ok()
            .and_then(|s| s.parse::<RawFd>().ok())
            .filter(|fd| *fd > 2)
        else {
            continue;
        };
        if identity(fd).ok().as_ref() != Some(&expected) {
            continue;
        }
        let mut stat: libc::stat = unsafe { std::mem::zeroed() };
        if unsafe { libc::fstat(fd, &mut stat) } == 0 {
            descriptors.push((fd, stat.st_dev, stat.st_ino, stat.st_mode));
        }
    }
    unsafe {
        command.pre_exec(move || {
            for &(fd, dev, ino, mode) in &descriptors {
                let mut stat: libc::stat = std::mem::zeroed();
                if libc::fstat(fd, &mut stat) == 0
                    && stat.st_dev == dev
                    && stat.st_ino == ino
                    && stat.st_mode == mode
                {
                    libc::close(fd);
                }
            }
            Ok(())
        });
    }
}

fn duplicate(name: &str, expected: &[String; 4]) -> Result<RawFd> {
    let fd: RawFd = std::env::var(name)?.parse()?;
    ensure!(fd > 2, "invalid control descriptor");
    // SAFETY: duplicate an existing inherited descriptor; the caller takes
    // ownership only of the new descriptor. Never close the original.
    let copy = unsafe { libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, 3) };
    ensure!(
        copy != -1,
        "control channel is unavailable: {}",
        std::io::Error::last_os_error()
    );
    // Own the duplicate immediately, including on identity failure. Never
    // read/write a reused fd merely because its number was inherited in env.
    let file = unsafe { std::fs::File::from_raw_fd(copy) };
    ensure!(
        &identity(file.as_raw_fd())? == expected,
        "control descriptor identity mismatch: {name}"
    );
    use std::os::fd::IntoRawFd as _;
    Ok(file.into_raw_fd())
}

fn identity(fd: RawFd) -> Result<[String; 4]> {
    let mut stat: libc::stat = unsafe { std::mem::zeroed() };
    ensure!(
        unsafe { libc::fstat(fd, &mut stat) } == 0,
        "control descriptor closed: {}",
        std::io::Error::last_os_error()
    );
    let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
    ensure!(flags != -1, "cannot inspect control descriptor");
    Ok([
        stat.st_dev.to_string(),
        stat.st_ino.to_string(),
        (stat.st_mode & libc::S_IFMT).to_string(),
        (flags & libc::O_ACCMODE).to_string(),
    ])
}

pub fn request(operation: &Operation) -> Result<String> {
    let id = crate::rng::entropy();
    let body = serde_json::to_vec(&serde_json::json!({ "id": id, "operation": operation }))?;
    ensure!(body.len() <= MAX_FRAME, "control request exceeds 1 MiB");
    let (lock, mut requests, mut responses) = match prepare() {
        Ok(descriptors) => descriptors,
        Err(error) => {
            return socket::request(&body, id)
                .with_context(|| format!("inherited control descriptors unavailable: {error:#}"));
        }
    };
    // POSIX locks belong to each process (unlike flock's inherited open-file
    // description lock). Closing `lock` releases this command's reservation.
    let mut record: libc::flock = unsafe { std::mem::zeroed() };
    record.l_type = libc::F_WRLCK as _;
    record.l_whence = libc::SEEK_SET as _;
    loop {
        // SAFETY: record is initialized and lives through the blocking call.
        if unsafe { libc::fcntl(lock.as_raw_fd(), libc::F_SETLKW, &record) } != -1 {
            break;
        }
        let error = std::io::Error::last_os_error();
        if error.kind() != std::io::ErrorKind::Interrupted {
            bail!("lock control channel: {error}");
        }
    }
    // After transmission starts, never retry or execute locally: the launcher
    // may already have applied the operation even if the response is lost.
    (|| -> Result<Response> {
        requests.write_all(&(body.len() as u32).to_be_bytes())?;
        requests.write_all(&body)?;
        receive(&mut responses, id)
    })()
    .context(
        "chat control operation may have executed; outcome is unknown; do not automatically retry",
    )?
    .result
    .map_err(anyhow::Error::msg)
}

fn prepare() -> Result<(std::fs::File, std::fs::File, std::fs::File)> {
    let identities: Vec<[String; 4]> = serde_json::from_str(
        &std::env::var(IDENTITY_ENV).context("missing launcher descriptor identities")?,
    )?;
    ensure!(
        identities.len() == 3,
        "invalid launcher descriptor identities"
    );
    ensure!(
        identities[0][2] == libc::S_IFIFO.to_string()
            && identities[1][2] == libc::S_IFIFO.to_string()
            && identities[2][2] == libc::S_IFREG.to_string(),
        "invalid control descriptor types"
    );
    // SAFETY: duplicate() transfers ownership of fresh descriptors. Each file
    // is immediately owned so later preparation failures close earlier copies.
    let lock = unsafe { std::fs::File::from_raw_fd(duplicate(LOCK_ENV, &identities[2])?) };
    let requests = unsafe { std::fs::File::from_raw_fd(duplicate(CHANNEL_ENV, &identities[0])?) };
    let responses = unsafe { std::fs::File::from_raw_fd(duplicate(RESPONSE_ENV, &identities[1])?) };
    Ok((lock, requests, responses))
}

fn receive(responses: &mut impl std::io::Read, id: u64) -> Result<Response> {
    // A killed requester releases its process lock but can leave its complete
    // response in the pipe. Drain those frames under the same lock, including
    // error responses, without sending the operation again. Bound the number
    // drained, not the duration of a legitimate long-running operation.
    for stale in 0..=MAX_STALE_RESPONSES {
        let mut length = [0; 4];
        responses
            .read_exact(&mut length)
            .context("chat control launcher disconnected")?;
        let length = u32::from_be_bytes(length) as usize;
        ensure!(length <= MAX_FRAME, "control response too large");
        let mut response = vec![0; length];
        responses.read_exact(&mut response)?;
        let response: Response = serde_json::from_slice(&response)?;
        if response.id == id {
            return Ok(response);
        }
        ensure!(
            stale < MAX_STALE_RESPONSES,
            "too many stale control responses"
        );
    }
    unreachable!("response drain is bounded")
}

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

    #[test]
    #[ignore = "subprocess helper for detached control cleanup"]
    fn detached_control_cleanup_child() {
        if std::env::var_os("MAGI_TEST_CONTROL_CLEANUP").is_none() {
            return;
        }
        let descriptors: Vec<_> = [CHANNEL_ENV, RESPONSE_ENV, LOCK_ENV]
            .into_iter()
            .map(|name| std::env::var(name).unwrap())
            .collect();
        let mut command = std::process::Command::new("python3");
        command
            .args([
                "-c",
                r#"import os, sys
assert not any(n.startswith('MAGI_CHAT_CONTROL_') for n in os.environ)
for fd in map(int, sys.argv[1:]):
    try:
        os.fstat(fd)
    except OSError:
        pass
    else:
        raise AssertionError('detached child retained control descriptor')
"#,
            ])
            .args(descriptors);
        super::super::clear_std_environment(&mut command);
        assert!(command.status().unwrap().success());
    }

    #[tokio::test]
    async fn detached_children_lose_control_environment_and_descriptors() {
        let tmp = tempfile::tempdir().unwrap();
        let talks = crate::talk::Talks::at(tmp.path().join("talks"));
        let mut talk: crate::talk::Talk = serde_json::from_value(serde_json::json!({
            "schema": 1, "id": "cleanup-chat", "repo": ".", "agent": "mock",
            "status": "open", "turns": [], "created_at": jiff::Timestamp::now(),
            "updated_at": jiff::Timestamp::now(),
            "seat": crate::agent::SeatState::new("talk", "mock", 1)
        }))
        .unwrap();
        talks.put(&mut talk).unwrap();
        let bridge = Bridge::start(tmp.path(), &talk.id, tmp.path()).unwrap();
        let mut command = tokio::process::Command::new(std::env::current_exe().unwrap());
        command
            .args([
                "--ignored",
                "--exact",
                "chat_control::bridge::unix::tests::detached_control_cleanup_child",
            ])
            .env("MAGI_TEST_CONTROL_CLEANUP", "1");
        bridge.attach(&mut command);
        let output = command.output().await.unwrap();
        assert!(
            output.status.success(),
            "{} {}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
    }

    fn frame(id: u64, result: std::result::Result<String, String>) -> Vec<u8> {
        let body = serde_json::to_vec(&Response { id, result }).unwrap();
        let mut bytes = (body.len() as u32).to_be_bytes().to_vec();
        bytes.extend(body);
        bytes
    }

    #[test]
    fn drains_stale_successes_and_errors_up_to_the_limit() {
        let mut bytes = Vec::new();
        for id in 0..MAX_STALE_RESPONSES as u64 {
            bytes.extend(frame(
                id,
                if id % 2 == 0 {
                    Ok("old".into())
                } else {
                    Err("old error".into())
                },
            ));
        }
        bytes.extend(frame(100, Ok("mine".into())));
        assert_eq!(
            receive(&mut bytes.as_slice(), 100).unwrap().result.unwrap(),
            "mine"
        );
        assert_eq!(
            receive(&mut frame(100, Err("mine failed".into())).as_slice(), 100)
                .unwrap()
                .result
                .unwrap_err(),
            "mine failed"
        );
    }

    #[test]
    fn rejects_excess_stale_responses_and_invalid_frames() {
        let mut bytes = Vec::new();
        for _ in 0..=MAX_STALE_RESPONSES {
            bytes.extend(frame(1, Ok("old".into())));
        }
        bytes.extend(frame(2, Ok("mine".into())));
        let mut remaining = bytes.as_slice();
        assert!(
            receive(&mut remaining, 2)
                .err()
                .unwrap()
                .to_string()
                .contains("too many stale")
        );
        assert_eq!(receive(&mut remaining, 2).unwrap().result.unwrap(), "mine");
        for bytes in [
            vec![],
            vec![0, 0],
            vec![0, 0, 0, 2, b'{'],
            vec![0, 0, 0, 1, b'!'],
            ((MAX_FRAME + 1) as u32).to_be_bytes().to_vec(),
        ] {
            assert!(receive(&mut bytes.as_slice(), 2).is_err());
        }
    }
}