agentmux 0.1.0

Multi-agent coordination runtime with inter-agent messaging across CLI, MCP, tmux, and ACP.
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
//! Relay bootstrap coordination primitives for client startup.

use std::{
    fs::{self, OpenOptions},
    io,
    os::fd::AsRawFd,
    os::unix::{
        fs::PermissionsExt,
        net::{UnixListener, UnixStream},
    },
    path::{Path, PathBuf},
    thread,
    time::{Duration, Instant},
};

use super::{
    error::RuntimeError,
    paths::{
        BundleRuntimePaths, ensure_bundle_runtime_directory, ensure_existing_artifact_is_owned,
    },
};

const SOCKET_MODE_OWNER_ONLY: u32 = 0o600;
const POLL_SLEEP_INTERVAL: Duration = Duration::from_millis(50);

/// Client-side bootstrap options for relay startup behavior.
#[derive(Clone, Copy, Debug)]
pub struct BootstrapOptions {
    pub auto_start_relay: bool,
    pub startup_timeout: Duration,
}

impl Default for BootstrapOptions {
    fn default() -> Self {
        Self {
            auto_start_relay: true,
            startup_timeout: Duration::from_millis(10_000),
        }
    }
}

/// Outcome of relay bootstrap.
#[derive(Clone, Debug)]
pub struct BootstrapReport {
    pub spawned_relay: bool,
}

/// Acquires and holds the relay runtime lock.
#[derive(Debug)]
pub struct RelayRuntimeLock {
    lock_file: std::fs::File,
}

impl Drop for RelayRuntimeLock {
    fn drop(&mut self) {
        unlock(&self.lock_file);
        let _ = self.lock_file.set_len(0);
        let _ = self.lock_file.sync_all();
    }
}

#[derive(Debug)]
struct SpawnLockGuard {
    lock_file: std::fs::File,
}

impl Drop for SpawnLockGuard {
    fn drop(&mut self) {
        unlock(&self.lock_file);
    }
}

/// Tries to bootstrap relay availability for one bundle.
///
/// # Errors
///
/// Returns structured runtime errors for startup timeout, I/O failures, and
/// disabled auto-start.
pub fn bootstrap_relay<F>(
    paths: &BundleRuntimePaths,
    options: BootstrapOptions,
    spawn_relay: F,
) -> Result<BootstrapReport, RuntimeError>
where
    F: FnOnce() -> Result<(), RuntimeError>,
{
    ensure_bundle_runtime_directory(paths)?;
    if relay_socket_connectable(paths) {
        return Ok(BootstrapReport {
            spawned_relay: false,
        });
    }
    if !options.auto_start_relay {
        return Err(RuntimeError::RelayAutostartDisabled {
            relay_socket: paths.relay_socket.clone(),
        });
    }

    let mut spawn_relay = Some(spawn_relay);
    match try_acquire_spawn_lock(paths)? {
        Some(_spawn_lock_guard) => {
            if relay_socket_connectable(paths) {
                return Ok(BootstrapReport {
                    spawned_relay: false,
                });
            }
            let _ = remove_stale_relay_socket(paths)?;
            let spawn = spawn_relay.take().expect("spawn closure available");
            spawn()?;
            wait_for_relay_socket(paths, options.startup_timeout)?;
            Ok(BootstrapReport {
                spawned_relay: true,
            })
        }
        None => {
            wait_for_relay_socket(paths, options.startup_timeout)?;
            Ok(BootstrapReport {
                spawned_relay: false,
            })
        }
    }
}

/// Acquires the process lock used by the relay runtime loop.
///
/// # Errors
///
/// Returns an error if another relay already holds the runtime lock.
pub fn acquire_relay_runtime_lock(
    paths: &BundleRuntimePaths,
) -> Result<RelayRuntimeLock, RuntimeError> {
    let mut lock_file = open_lock_file(&paths.relay_lock_file)?;
    let lock_obtained = try_lock_exclusive_nonblocking(&lock_file)?;
    if !lock_obtained {
        return Err(RuntimeError::io(
            format!("relay already running for bundle {}", paths.bundle_name),
            io::Error::new(io::ErrorKind::WouldBlock, "lock held"),
        ));
    }
    write_diagnostic_pid(&mut lock_file)?;
    Ok(RelayRuntimeLock { lock_file })
}

/// Binds the relay socket listener for the active bundle.
///
/// # Errors
///
/// Returns an error when socket bind or permission assignment fails.
pub fn bind_relay_listener(paths: &BundleRuntimePaths) -> Result<UnixListener, RuntimeError> {
    ensure_existing_artifact_is_owned(&paths.relay_socket)?;
    if paths.relay_socket.exists() {
        fs::remove_file(&paths.relay_socket).map_err(|source| {
            RuntimeError::io(
                format!("remove stale relay socket {}", paths.relay_socket.display()),
                source,
            )
        })?;
    }
    let listener = UnixListener::bind(&paths.relay_socket).map_err(|source| {
        RuntimeError::io(
            format!("bind relay socket {}", paths.relay_socket.display()),
            source,
        )
    })?;
    fs::set_permissions(
        &paths.relay_socket,
        fs::Permissions::from_mode(SOCKET_MODE_OWNER_ONLY),
    )
    .map_err(|source| {
        RuntimeError::io(
            format!(
                "set mode 0600 on relay socket {}",
                paths.relay_socket.display()
            ),
            source,
        )
    })?;
    Ok(listener)
}

/// Resolves a best-effort path for the unified `agentmux` executable.
pub fn resolve_relay_program() -> Result<PathBuf, RuntimeError> {
    if let Ok(command_path) = std::env::var("AGENTMUX_COMMAND") {
        let trimmed = command_path.trim();
        if !trimmed.is_empty() {
            return Ok(PathBuf::from(trimmed));
        }
    }
    if let Ok(command_path) = std::env::var("AGENTMUX_RELAY_COMMAND") {
        let trimmed = command_path.trim();
        if !trimmed.is_empty() {
            return Ok(PathBuf::from(trimmed));
        }
    }
    let mut sibling = std::env::current_exe()
        .map_err(|source| RuntimeError::io("resolve current executable path", source))?;
    sibling.set_file_name(format!("agentmux{}", std::env::consts::EXE_SUFFIX));
    Ok(sibling)
}

fn relay_socket_connectable(paths: &BundleRuntimePaths) -> bool {
    ensure_existing_artifact_is_owned(&paths.relay_socket).is_ok()
        && UnixStream::connect(&paths.relay_socket).is_ok()
}

fn wait_for_relay_socket(
    paths: &BundleRuntimePaths,
    startup_timeout: Duration,
) -> Result<(), RuntimeError> {
    let deadline = Instant::now() + startup_timeout;
    loop {
        if relay_socket_connectable(paths) {
            return Ok(());
        }
        if Instant::now() >= deadline {
            return Err(RuntimeError::RelayStartupTimeout {
                relay_socket: paths.relay_socket.clone(),
                startup_timeout,
            });
        }
        thread::sleep(POLL_SLEEP_INTERVAL);
    }
}

fn try_acquire_spawn_lock(
    paths: &BundleRuntimePaths,
) -> Result<Option<SpawnLockGuard>, RuntimeError> {
    let lock_file = open_lock_file(&paths.relay_spawn_lock_file)?;
    let lock_obtained = try_lock_exclusive_nonblocking(&lock_file)?;
    if lock_obtained {
        return Ok(Some(SpawnLockGuard { lock_file }));
    }
    Ok(None)
}

fn remove_stale_relay_socket(paths: &BundleRuntimePaths) -> Result<bool, RuntimeError> {
    if !paths.relay_socket.exists() {
        return Ok(false);
    }
    if relay_runtime_lock_is_held(paths)? {
        return Ok(false);
    }
    ensure_existing_artifact_is_owned(&paths.relay_socket)?;
    fs::remove_file(&paths.relay_socket).map_err(|source| {
        RuntimeError::io(
            format!("remove stale relay socket {}", paths.relay_socket.display()),
            source,
        )
    })?;
    Ok(true)
}

/// Checks whether the relay runtime lock is currently held for one bundle.
///
/// # Errors
///
/// Returns `RuntimeError` when lock-file access fails.
pub fn relay_runtime_lock_is_held(paths: &BundleRuntimePaths) -> Result<bool, RuntimeError> {
    let lock_file = open_lock_file(&paths.relay_lock_file)?;
    let lock_obtained = try_lock_exclusive_nonblocking(&lock_file)?;
    if lock_obtained {
        unlock(&lock_file);
        return Ok(false);
    }
    Ok(true)
}

fn open_lock_file(path: &Path) -> Result<std::fs::File, RuntimeError> {
    ensure_existing_artifact_is_owned(path)?;
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).map_err(|source| {
            RuntimeError::io(
                format!("create lock directory {}", parent.display()),
                source,
            )
        })?;
    }
    OpenOptions::new()
        .create(true)
        .truncate(false)
        .read(true)
        .write(true)
        .open(path)
        .map_err(|source| RuntimeError::io(format!("open lock file {}", path.display()), source))
}

fn try_lock_exclusive_nonblocking(lock_file: &std::fs::File) -> Result<bool, RuntimeError> {
    let code = unsafe { libc::flock(lock_file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
    if code == 0 {
        return Ok(true);
    }
    let source = io::Error::last_os_error();
    if source.kind() == io::ErrorKind::WouldBlock {
        return Ok(false);
    }
    Err(RuntimeError::io("lock file with flock", source))
}

fn unlock(lock_file: &std::fs::File) {
    let _ = unsafe { libc::flock(lock_file.as_raw_fd(), libc::LOCK_UN) };
}

fn write_diagnostic_pid(lock_file: &mut std::fs::File) -> Result<(), RuntimeError> {
    use std::io::{Seek, SeekFrom, Write};

    lock_file
        .set_len(0)
        .map_err(|source| RuntimeError::io("truncate relay lock file", source))?;
    lock_file
        .seek(SeekFrom::Start(0))
        .map_err(|source| RuntimeError::io("seek relay lock file", source))?;
    writeln!(lock_file, "{}", std::process::id())
        .map_err(|source| RuntimeError::io("write relay lock pid", source))?;
    lock_file
        .flush()
        .map_err(|source| RuntimeError::io("flush relay lock pid", source))
}

#[cfg(test)]
mod tests {
    use std::{
        fs::File,
        io::Write,
        os::unix::net::UnixListener,
        sync::{Arc, Mutex},
        thread,
        time::Duration,
    };

    use tempfile::TempDir;

    use super::{BootstrapOptions, bootstrap_relay};
    use crate::runtime::paths::{BundleRuntimePaths, ensure_bundle_runtime_directory};

    fn test_paths() -> (TempDir, BundleRuntimePaths) {
        let temporary = TempDir::new().expect("temporary");
        let paths = BundleRuntimePaths::resolve(temporary.path(), "party-alpha").expect("paths");
        ensure_bundle_runtime_directory(&paths).expect("directory");
        (temporary, paths)
    }

    #[test]
    fn bootstrap_fails_when_autostart_disabled() {
        let (_temporary, paths) = test_paths();
        let options = BootstrapOptions {
            auto_start_relay: false,
            startup_timeout: Duration::from_millis(100),
        };
        let err = bootstrap_relay(&paths, options, || Ok(())).expect_err("bootstrap should fail");
        assert!(
            err.to_string()
                .contains("start agentmux host relay with matching --state-directory"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn bootstrap_uses_existing_relay_without_spawning() {
        let (_temporary, paths) = test_paths();
        let _listener = UnixListener::bind(&paths.relay_socket).expect("bind listener");
        let spawn_called = Arc::new(Mutex::new(false));
        let spawn_called_inner = Arc::clone(&spawn_called);
        let options = BootstrapOptions::default();
        let report = bootstrap_relay(&paths, options, || {
            *spawn_called_inner.lock().expect("mutex") = true;
            Ok(())
        })
        .expect("bootstrap");
        assert!(!report.spawned_relay);
        assert!(!*spawn_called.lock().expect("mutex"));
    }

    #[test]
    fn bootstrap_spawns_relay_when_socket_missing() {
        let (_temporary, paths) = test_paths();
        let listener_handle = Arc::new(Mutex::new(None));
        let listener_handle_inner = Arc::clone(&listener_handle);
        let options = BootstrapOptions {
            auto_start_relay: true,
            startup_timeout: Duration::from_secs(1),
        };
        let report = bootstrap_relay(&paths, options, || {
            let relay_socket = paths.relay_socket.clone();
            let handle = thread::spawn(move || {
                let listener = UnixListener::bind(&relay_socket).expect("bind");
                thread::sleep(Duration::from_millis(250));
                drop(listener);
            });
            *listener_handle_inner.lock().expect("mutex") = Some(handle);
            Ok(())
        })
        .expect("bootstrap");
        assert!(report.spawned_relay);
        if let Some(handle) = listener_handle.lock().expect("mutex").take() {
            handle.join().expect("listener thread");
        }
    }

    #[test]
    fn bootstrap_removes_stale_socket_before_spawning() {
        let (_temporary, paths) = test_paths();
        let mut stale = File::create(&paths.relay_socket).expect("stale file");
        writeln!(stale, "stale").expect("write stale");
        let listener_handle = Arc::new(Mutex::new(None));
        let listener_handle_inner = Arc::clone(&listener_handle);

        let options = BootstrapOptions {
            auto_start_relay: true,
            startup_timeout: Duration::from_secs(1),
        };
        let report = bootstrap_relay(&paths, options, || {
            assert!(!paths.relay_socket.exists(), "stale file should be removed");
            let relay_socket = paths.relay_socket.clone();
            let handle = thread::spawn(move || {
                let listener = UnixListener::bind(&relay_socket).expect("bind");
                thread::sleep(Duration::from_millis(250));
                drop(listener);
            });
            *listener_handle_inner.lock().expect("mutex") = Some(handle);
            Ok(())
        })
        .expect("bootstrap");
        assert!(report.spawned_relay);
        if let Some(handle) = listener_handle.lock().expect("mutex").take() {
            handle.join().expect("listener thread");
        }
    }
}