magi-cli 0.179.0

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
//! Local named pipes survive CLI/shell tool descendants without handle inheritance.
use std::ffi::c_void;
use std::os::windows::{fs::OpenOptionsExt as _, io::IntoRawHandle as _};
use std::path::Path;
use std::sync::{
    Arc,
    atomic::{AtomicBool, Ordering},
};
use std::time::Duration;

use anyhow::{Context as _, Result, ensure};
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
use tokio::net::windows::named_pipe::{NamedPipeClient, NamedPipeServer, ServerOptions};

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

pub const CHANNEL_ENV: &str = "MAGI_CHAT_CONTROL_PIPE";
// Avoid KEY/SECRET/TOKEN: shell environment policies may strip those names.
const CAP_ENV: &str = "MAGI_CHAT_CONTROL_CAP";
const IO_TIMEOUT: Duration = Duration::from_secs(30);

#[link(name = "bcrypt")]
unsafe extern "system" {
    fn BCryptGenRandom(algorithm: *mut c_void, buffer: *mut u8, length: u32, flags: u32) -> i32;
}
#[link(name = "advapi32")]
unsafe extern "system" {
    fn ConvertStringSecurityDescriptorToSecurityDescriptorW(
        text: *const u16,
        revision: u32,
        descriptor: *mut *mut c_void,
        size: *mut u32,
    ) -> i32;
}
#[link(name = "kernel32")]
unsafe extern "system" {
    fn LocalFree(memory: *mut c_void) -> *mut c_void;
}

fn secret() -> Result<String> {
    let mut bytes = [0u8; 32];
    // SAFETY: system-preferred RNG needs no algorithm handle; buffer is writable.
    let status = unsafe { BCryptGenRandom(std::ptr::null_mut(), bytes.as_mut_ptr(), 32, 2) };
    ensure!(status >= 0, "cannot generate chat control capability");
    Ok(bytes.iter().map(|b| format!("{b:02x}")).collect())
}

fn listen(name: &str, first: bool) -> Result<NamedPipeServer> {
    #[repr(C)]
    struct SecurityAttributes {
        length: u32,
        descriptor: *mut c_void,
        inherit: i32,
    }
    // Only the owner may create additional instances. Local clients (including
    // restricted sandbox tokens) receive data I/O and synchronization rights on
    // this pipe alone; the secret authenticates them before any operation.
    // GENERIC_WRITE would also grant FILE_CREATE_PIPE_INSTANCE: do not use it.
    let sddl: Vec<u16> = "D:P(A;;GA;;;OW)(A;;0x00100003;;;WD)(A;;0x00100003;;;RC)\0"
        .encode_utf16()
        .collect();
    let mut descriptor = std::ptr::null_mut();
    // SAFETY: terminated UTF-16 input and valid output pointer.
    ensure!(
        unsafe {
            ConvertStringSecurityDescriptorToSecurityDescriptorW(
                sddl.as_ptr(),
                1,
                &mut descriptor,
                std::ptr::null_mut(),
            )
        } != 0,
        "create chat control security descriptor: {}",
        std::io::Error::last_os_error()
    );
    let mut attrs = SecurityAttributes {
        length: std::mem::size_of::<SecurityAttributes>() as u32,
        descriptor,
        inherit: 0,
    };
    // SAFETY: descriptor and attributes remain alive through CreateNamedPipeW;
    // Windows copies the descriptor and the handle is not inheritable.
    let result = unsafe {
        ServerOptions::new()
            .first_pipe_instance(first)
            .reject_remote_clients(true)
            .create_with_security_attributes_raw(
                name,
                (&mut attrs as *mut SecurityAttributes).cast(),
            )
    };
    // SAFETY: the conversion API allocated this descriptor with LocalAlloc.
    unsafe {
        LocalFree(descriptor);
    }
    Ok(result?)
}

fn connect(name: &str) -> std::io::Result<NamedPipeClient> {
    let file = std::fs::OpenOptions::new()
        // FILE_READ_DATA | FILE_WRITE_DATA | SYNCHRONIZE, without the right to
        // create a server instance under the same name.
        .access_mode(0x0010_0003)
        // OVERLAPPED, SQOS_PRESENT, IDENTIFICATION: never allow impersonation.
        .custom_flags(0x4000_0000 | 0x0010_0000 | 0x0001_0000)
        .open(name)?;
    // SAFETY: transfer a newly owned overlapped pipe handle to Tokio. Tokio
    // takes ownership of the handle even when registration fails.
    unsafe { NamedPipeClient::from_raw_handle(file.into_raw_handle()) }
}

pub(crate) struct Bridge {
    name: String,
    token: String,
    active: Arc<AtomicBool>,
    server: tokio::task::JoinHandle<()>,
}

impl Bridge {
    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 name = format!(r"\\.\pipe\magi-chat-{}", secret()?);
        let token = secret()?;
        let mut incoming = listen(&name, true)?;
        let home = home.to_owned();
        let talk = talk.to_owned();
        let active = Arc::new(AtomicBool::new(true));
        let server = tokio::spawn({
            let name = name.clone();
            let token = token.clone();
            let active = active.clone();
            async move {
                // Dropping the listener task also aborts every connected handler.
                let mut connections = tokio::task::JoinSet::new();
                loop {
                    tokio::select! {
                        result = incoming.connect(), if connections.len() < 32 => {
                            if result.is_err() { break; }
                            let next = match listen(&name, false) {
                                Ok(next) => next,
                                Err(_) => break,
                            };
                            let mut pipe = std::mem::replace(&mut incoming, next);
                            let home = home.clone();
                            let talk = talk.clone();
                            let token = token.clone();
                            let active = active.clone();
                            connections.spawn(async move {
                                let _ = tokio::time::timeout(IO_TIMEOUT, async {
                                    // Authenticate before parsing or executing an operation.
                                    let mut supplied = [0; 64];
                                    pipe.read_exact(&mut supplied).await?;
                                    ensure!(supplied.as_slice() == token.as_bytes(), "invalid control capability");
                                    let len = pipe.read_u32().await? as usize;
                                    ensure!(len <= MAX_FRAME, "control request too large");
                                    let mut body = vec![0; len];
                                    pipe.read_exact(&mut body).await?;
                                    let Request { id, operation } = serde_json::from_slice(&body)?;
                                    ensure!(active.load(Ordering::Acquire), "control capability revoked");
                                    let talks = crate::talk::Talks::at(home.join("talks"));
                                    let result = super::super::execute(
                                        &crate::queue::Queue::at(home.join("queue")),
                                        &home, &talks, &talk, operation,
                                    ).await.map_err(|e| format!("{e:#}"));
                                    let response = serde_json::to_vec(&Response { id, result })?;
                                    ensure!(response.len() <= MAX_FRAME, "control response too large");
                                    pipe.write_u32(response.len() as u32).await?;
                                    pipe.write_all(&response).await?;
                                    // Keep the server handle alive until the client reads the response.
                                    let mut done = [0];
                                    let _ = pipe.read(&mut done).await?;
                                    Ok::<_, anyhow::Error>(())
                                }).await;
                            });
                        }
                        _ = connections.join_next(), if !connections.is_empty() => {}
                    }
                }
            }
        });
        Ok(Self {
            name,
            token,
            active,
            server,
        })
    }

    pub fn attach(&self, command: &mut tokio::process::Command) {
        command
            .env(CHANNEL_ENV, &self.name)
            .env(CAP_ENV, &self.token);
    }
}

impl Drop for Bridge {
    fn drop(&mut self) {
        self.active.store(false, Ordering::Release);
        self.server.abort();
    }
}

pub(crate) fn clear_environment(command: &mut tokio::process::Command) {
    for name in [
        CHANNEL_ENV,
        CAP_ENV,
        "MAGI_CHAT_CONTROL_FD",
        "MAGI_CHAT_CONTROL_RESPONSE_FD",
        "MAGI_CHAT_CONTROL_LOCK_FD",
    ] {
        command.env_remove(name);
    }
}

pub async fn request(operation: &Operation) -> Result<String> {
    let name = std::env::var(CHANNEL_ENV)?;
    ensure!(
        name.starts_with(r"\\.\pipe\magi-chat-"),
        "invalid control pipe"
    );
    let token = std::env::var(CAP_ENV).context("missing control capability")?;
    ensure!(token.len() == 64, "invalid control capability");
    request_at(&name, &token, operation).await
}

async fn request_at(name: &str, token: &str, operation: &Operation) -> Result<String> {
    tokio::time::timeout(IO_TIMEOUT, async {
        let mut pipe = loop {
            match connect(name) {
                Ok(pipe) => break pipe,
                // ERROR_PIPE_BUSY: another connection is being accepted.
                Err(e) if e.raw_os_error() == Some(231) => {
                    tokio::time::sleep(Duration::from_millis(10)).await;
                }
                Err(e) => return Err(e.into()),
            }
        };
        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");
        pipe.write_all(token.as_bytes()).await?;
        pipe.write_u32(body.len() as u32).await?;
        pipe.write_all(&body).await?;
        let len = pipe.read_u32().await? as usize;
        ensure!(len <= MAX_FRAME, "control response too large");
        let mut body = vec![0; len];
        pipe.read_exact(&mut body).await?;
        let response: Response = serde_json::from_slice(&body)?;
        ensure!(response.id == id, "control response association lost");
        response.result.map_err(anyhow::Error::msg)
    })
    .await
    .context("chat control timed out; operation outcome is unknown; do not automatically retry")?
    .context("chat control failed; operation outcome may be unknown; do not automatically retry")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::queue::{Queue, Source, Task, TaskStatus};

    #[tokio::test]
    async fn authenticated_connections_are_isolated_scoped_and_revoked() {
        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": "talk-pipe", "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 queue = Queue::at(tmp.path().join("queue"));
        let mut tasks = Vec::new();
        for owner in [talk.id.as_str(), talk.id.as_str(), "other-talk"] {
            let mut task = Task::new(
                "work".into(),
                "work".into(),
                tmp.path().into(),
                Source::Agent {
                    run: owner.to_owned(),
                    node: crate::queue::CHAT_NODE.into(),
                },
            );
            queue.put(&mut task).unwrap();
            tasks.push(task);
        }
        let bridge = Bridge::start(tmp.path(), &talk.id, tmp.path()).unwrap();
        let mut command = tokio::process::Command::new("unused");
        bridge.attach(&mut command);
        let environment: Vec<_> = command.as_std().get_envs().collect();
        // The capability must survive automatic credential-name exclusions.
        for (name, value) in &environment {
            let name = name.to_str().unwrap().to_ascii_uppercase();
            assert!(
                !["KEY", "SECRET", "TOKEN"]
                    .iter()
                    .any(|word| name.contains(*word))
            );
            assert!(value.is_some());
        }
        assert!(environment.iter().any(|(name, value)| {
            *name == CAP_ENV && *value == Some(std::ffi::OsStr::new(&bridge.token))
        }));
        clear_environment(&mut command);
        assert!(
            command
                .as_std()
                .get_envs()
                .all(|(_, value)| value.is_none())
        );
        let hold = |index: usize| Operation::Hold {
            task: tasks[index].id.clone(),
            reason: "wait".into(),
        };
        let first = hold(0);
        let second = hold(1);
        let (a, b) = tokio::join!(
            request_at(&bridge.name, &bridge.token, &first),
            request_at(&bridge.name, &bridge.token, &second),
        );
        assert!(a.is_ok(), "{a:?}");
        assert!(b.is_ok(), "{b:?}");
        assert_eq!(queue.get(&tasks[0].id).unwrap().status, TaskStatus::Held);
        assert_eq!(queue.get(&tasks[1].id).unwrap().status, TaskStatus::Held);
        assert!(
            request_at(&bridge.name, &bridge.token, &hold(2))
                .await
                .unwrap_err()
                .to_string()
                .contains("outcome")
        );
        assert_eq!(queue.get(&tasks[2].id).unwrap().status, TaskStatus::Queued);
        let release = Operation::Release {
            task: tasks[0].id.clone(),
        };
        assert!(
            request_at(&bridge.name, &"0".repeat(64), &release)
                .await
                .is_err()
        );
        assert_eq!(queue.get(&tasks[0].id).unwrap().status, TaskStatus::Held);
        // A connection accepted before revocation may not submit work afterwards.
        let mut pending = connect(&bridge.name).unwrap();
        pending
            .write_all(&bridge.token.as_bytes()[..32])
            .await
            .unwrap();
        tokio::task::yield_now().await;
        let name = bridge.name.clone();
        let token = bridge.token.clone();
        drop(bridge);
        tokio::task::yield_now().await;
        let _ = pending.write_all(&token.as_bytes()[32..]).await;
        let body =
            serde_json::to_vec(&serde_json::json!({ "id": 99, "operation": release })).unwrap();
        let _ = pending.write_u32(body.len() as u32).await;
        let _ = pending.write_all(&body).await;
        let mut byte = [0];
        let closed = tokio::time::timeout(Duration::from_secs(2), pending.read(&mut byte))
            .await
            .unwrap();
        assert!(closed.is_err() || closed.unwrap() == 0);
        assert!(request_at(&name, &token, &release).await.is_err());
        assert_eq!(queue.get(&tasks[0].id).unwrap().status, TaskStatus::Held);
    }

    #[tokio::test]
    async fn lost_or_mismatched_responses_are_uncertain_and_never_retried() {
        for mismatch in [false, true] {
            let name = format!(r"\\.\pipe\magi-chat-{}", secret().unwrap());
            let mut server = listen(&name, true).unwrap();
            let worker = tokio::spawn(async move {
                server.connect().await.unwrap();
                let mut token = [0; 64];
                server.read_exact(&mut token).await.unwrap();
                let len = server.read_u32().await.unwrap() as usize;
                let mut body = vec![0; len];
                server.read_exact(&mut body).await.unwrap();
                if mismatch {
                    let request: Request = serde_json::from_slice(&body).unwrap();
                    let response = serde_json::to_vec(&Response {
                        id: request.id.wrapping_add(1),
                        result: Ok("wrong".into()),
                    })
                    .unwrap();
                    server.write_u32(response.len() as u32).await.unwrap();
                    server.write_all(&response).await.unwrap();
                    let mut done = [0];
                    let _ = server.read(&mut done).await;
                }
            });
            let error = request_at(
                &name,
                &"0".repeat(64),
                &Operation::Release {
                    task: "task".into(),
                },
            )
            .await
            .unwrap_err();
            assert!(error.to_string().contains("outcome"));
            worker.await.unwrap();
        }
    }
}