agent-float-term 0.3.4

Harness Floating Terminal: a persistent F7 shell for unwrapped AI CLIs
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
//! Exercise the gate from outside the inspected process's controlling terminal.
use std::ffi::OsString;
use std::fs::File;
use std::os::fd::{AsRawFd, FromRawFd};
use std::os::unix::ffi::OsStringExt;
use std::os::unix::process::CommandExt;
use std::process::{Child, Command, Stdio};

use super::*;

struct Job(Child);

impl Drop for Job {
    fn drop(&mut self) {
        let _ = self.0.kill();
        let _ = self.0.wait();
    }
}

fn terminal() -> (File, File, PathBuf) {
    let mut master = -1;
    let mut slave = -1;
    // SAFETY: both output descriptor pointers are valid. Null optional arguments ask
    // the OS for default terminal settings, without writing any name buffer.
    let result = unsafe {
        libc::openpty(
            &mut master,
            &mut slave,
            std::ptr::null_mut(),
            std::ptr::null_mut(),
            std::ptr::null_mut(),
        )
    };
    assert_eq!(result, 0);
    // SAFETY: successful openpty transferred two distinct owned descriptors to us.
    let (master, slave) = unsafe { (File::from_raw_fd(master), File::from_raw_fd(slave)) };
    for fd in [&master, &slave] {
        // SAFETY: these are owned live FDs; F_SETFD changes only inheritance flags.
        let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_SETFD, libc::FD_CLOEXEC) };
        assert_eq!(result, 0);
    }
    let mut name = [0u8; 1024];
    // SAFETY: slave is live and name is writable for the stated length; ttyname_r
    // does not retain the buffer. A successful call NUL-terminates the result.
    let result =
        unsafe { libc::ttyname_r(slave.as_raw_fd(), name.as_mut_ptr().cast(), name.len()) };
    assert_eq!(result, 0);
    let end = name.iter().position(|b| *b == 0).unwrap();
    let tty = PathBuf::from(OsString::from_vec(name[..end].to_vec()));
    (master, slave, tty)
}

#[test]
fn mapped_foreground_job_on_separate_pty_and_stopped_job() {
    let (_master, slave, tty) = terminal();
    let executable = Path::new("/bin/sleep").canonicalize().unwrap();
    let mut command = Command::new(&executable);
    command
        .arg("30")
        .stdin(Stdio::from(slave.try_clone().unwrap()))
        .stdout(Stdio::from(slave.try_clone().unwrap()))
        .stderr(Stdio::from(slave));
    // SAFETY: the pre-exec closure performs only async-signal-safe libc calls and
    // constructs OS errors; stdin has already been set to the child's PTY slave.
    unsafe {
        command.pre_exec(|| {
            if libc::setsid() < 0 || libc::ioctl(0, libc::TIOCSCTTY as _, 0) < 0 {
                return Err(std::io::Error::last_os_error());
            }
            Ok(())
        });
    }
    let mut job = Job(command.spawn().unwrap());
    let pid = job.0.id();
    let mappings = [HarnessMapping {
        harness: "codex".into(),
        path: executable,
    }];
    // This test explicitly opts sleep into the classifier. It tests OS plumbing,
    // not the authenticity of an AI binary or its actual keyboard ownership.
    let decision = eligible(pid, &tty, &mappings).unwrap();
    assert!(decision.eligible);
    let invocation = decision.invocation.unwrap();
    assert_eq!(invocation.liveness(), Liveness::Alive);
    assert!(!eligible(pid, &tty, &[]).unwrap().eligible);
    assert!(!foreground_nvim(pid, &tty).unwrap());
    assert!(foreground_nvim(pid, Path::new("/dev/null")).is_err());
    assert!(foreground_nvim(0, &tty).is_err());
    assert!(eligible(pid, Path::new("/dev/null"), &mappings).is_err());
    // SAFETY: pid is the live child owned by Job; SIGSTOP changes no parent memory.
    assert_eq!(unsafe { libc::kill(pid as i32, libc::SIGSTOP) }, 0);
    let deadline = Instant::now();
    while platform::identity(pid).unwrap().unwrap().runnable {
        assert!(deadline.elapsed() < Duration::from_secs(2));
        std::thread::sleep(Duration::from_millis(5));
    }
    assert!(!eligible(pid, &tty, &mappings).unwrap().eligible);
    assert!(foreground_nvim(pid, &tty).is_err());
    assert_eq!(invocation.liveness(), Liveness::Alive);
    // Leave the child unreaped so zombie metadata also counts as an ended invocation.
    job.0.kill().unwrap();
    let deadline = Instant::now();
    while invocation.liveness() != Liveness::Exited {
        assert!(deadline.elapsed() < Duration::from_secs(2));
        std::thread::sleep(Duration::from_millis(5));
    }
    job.0.wait().unwrap();
    assert_eq!(invocation.liveness(), Liveness::Exited);
    assert_eq!(confirm_missing(pid).unwrap(), None);
    assert!(foreground_nvim(pid, &tty).is_err());
}

#[test]
fn foreground_editor_uses_executable_not_argv_and_rejects_stopped_identity() {
    let root = tempfile::tempdir().unwrap();
    let executable = root.path().join("nvim");
    // A named fixture tests executable/TTY identity, not real editor navigation.
    let source = root.path().join("editor.c");
    std::fs::write(
        &source,
        "#include <unistd.h>\nint main(void) { sleep(30); return 0; }\n",
    )
    .unwrap();
    assert!(Command::new("cc")
        .arg(&source)
        .arg("-o")
        .arg(&executable)
        .status()
        .unwrap()
        .success());
    let (_master, slave, tty) = terminal();
    let mut command = Command::new(executable);
    command
        .arg0("not-an-editor")
        .arg("30")
        .stdin(Stdio::from(slave.try_clone().unwrap()))
        .stdout(Stdio::from(slave.try_clone().unwrap()))
        .stderr(Stdio::from(slave));
    // SAFETY: only async-signal-safe calls run after fork, on this child's PTY.
    unsafe {
        command.pre_exec(|| {
            if libc::setsid() < 0 || libc::ioctl(0, libc::TIOCSCTTY as _, 0) < 0 {
                return Err(std::io::Error::last_os_error());
            }
            Ok(())
        });
    }
    let job = Job(command.spawn().unwrap());
    let pid = job.0.id();
    assert!(foreground_nvim(pid, &tty).unwrap());
    let (_other_master, _other_slave, other_tty) = terminal();
    assert!(foreground_nvim(pid, &other_tty).is_err());
    // SAFETY: only the positive PID of our owned fixture child is signaled.
    assert_eq!(unsafe { libc::kill(pid as i32, libc::SIGSTOP) }, 0);
    let start = Instant::now();
    while platform::identity(pid).unwrap().unwrap().runnable {
        assert!(start.elapsed() < Duration::from_secs(2));
        std::thread::sleep(Duration::from_millis(5));
    }
    assert!(foreground_nvim(pid, &tty).is_err());
}

struct FixtureJob(Child);

impl Drop for FixtureJob {
    fn drop(&mut self) {
        // SAFETY: this PID belongs to our isolated fixture. Its handler reaps its
        // children before exiting; no live user process or process group is signaled.
        unsafe { libc::kill(self.0.id() as i32, libc::SIGTERM) };
        let start = Instant::now();
        while start.elapsed() < Duration::from_secs(3) {
            if let Some(status) = self.0.try_wait().ok().flatten() {
                if !std::thread::panicking() {
                    assert!(
                        status.success(),
                        "fixture detected consumed protocol input or failed cleanup"
                    );
                }
                return;
            }
            std::thread::sleep(Duration::from_millis(5));
        }
        // SAFETY: setsid made this fixture's PID its private process group ID.
        unsafe { libc::kill(-(self.0.id() as i32), libc::SIGKILL) };
        let _ = self.0.wait();
    }
}

// A standalone C executable avoids Rust test-harness threads after fork. The
// helpers deliberately exec a binary named node with an overwritten npm-style
// argv and nonexistent argv[1]; inspecting their argv would incorrectly fail.
const WORKERS: &str = r#"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

static volatile sig_atomic_t running = 1;
static void stop(int sig) { (void)sig; running = 0; }
static void check(int ok) { if (!ok) _exit(90); }
static void reap(pid_t child) {
    kill(child, SIGTERM);
    int status = 0;
    pid_t result;
    do { result = waitpid(child, &status, 0); } while (result < 0 && errno == EINTR);
    check(result == child && WIFEXITED(status) && WEXITSTATUS(status) == 0);
}
static void input(const char *mode) {
    if (!strcmp(mode, "tty-input") || !strcmp(mode, "editor-chain")) return;
    int pair[2];
    if (!strcmp(mode, "socket")) {
        check(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0);
    } else {
        check(pipe(pair) == 0);
    }
    check(write(pair[1], "protocol bytes must remain unread", 32) == 32);
    check(dup2(pair[0], 0) == 0);
    close(pair[0]);
    close(pair[1]);
    if (!strcmp(mode, "null") || !strcmp(mode, "regular-input")) {
        int fd = open(!strcmp(mode, "null") ? "/dev/null" : "/bin/sh", O_RDONLY);
        check(fd >= 0 && dup2(fd, 0) == 0);
        close(fd);
    }
    if (!strcmp(mode, "closed-input")) close(0);
}
static void worker(const char *mode, int ready, int depth) {
    signal(SIGTERM, stop);
    pid_t child = -1;
    if (depth) {
        child = fork();
        check(child >= 0);
        if (!child) {
            input("pipe");
            worker("pipe", ready, 0);
            _exit(0);
        }
    }
    const char *extra = NULL;
    if (!strcmp(mode, "extra-tty")) extra = "/dev/tty";
    if (!strcmp(mode, "extra-unknown")) extra = "/dev/zero";
    if (!strcmp(mode, "extra-null")) extra = "/dev/null";
    if (!strcmp(mode, "extra-random")) extra = "/dev/random";
    if (!strcmp(mode, "extra-urandom")) extra = "/dev/urandom";
    if (extra) check(open(extra, O_RDONLY | O_NONBLOCK) >= 3);
    if (!strcmp(mode, "extra-pty")) check(dup(2) >= 3);
    check(isatty(2)); /* gopls-style terminal stderr is intentional. */
    check(write(ready, "R", 1) == 1);
    close(ready);
    while (running) {
        if (!strcmp(mode, "churn")) {
            int pair[2];
            check(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0);
            check(write(pair[1], "volatile", 8) == 8);
            int fd = open("/dev/null", O_RDONLY);
            check(fd >= 0);
            close(fd);
            close(pair[0]);
            close(pair[1]);
        } else {
            usleep(1000);
        }
    }
    if (child > 0) reap(child);
    if (strcmp(mode, "tty-input") && strcmp(mode, "editor-chain") &&
        strcmp(mode, "null") && strcmp(mode, "regular-input") && strcmp(mode, "closed-input")) {
        char bytes[32];
        check(read(0, bytes, sizeof(bytes)) == 32);
        check(!memcmp(bytes, "protocol bytes must remain unread", 32));
    }
}
int main(int argc, char **argv) {
    if (argc == 6 && !strcmp(argv[5], "worker")) {
        worker(argv[2], atoi(argv[3]), atoi(argv[4]));
        return 0;
    }
    check(argc == 2);
    /* Other parallel test threads can be between openpty and CLOEXEC. Isolate
       the frontend before creating its intentional worker descriptors. */
    long max_fd = sysconf(_SC_OPEN_MAX);
    check(max_fd > 0 && max_fd <= INT_MAX);
    for (int fd = 3; fd < max_fd; fd++) close(fd);
    signal(SIGTERM, stop);
    char helper[4096];
    check(strlen(argv[0]) + 8 < sizeof(helper));
    strcpy(helper, argv[0]);
    char *base = strrchr(helper, '/');
    check(base != NULL);
    strcpy(base + 1, "node");
    int ready[2];
    check(pipe(ready) == 0);
    int depth = !strcmp(argv[1], "chain") || !strcmp(argv[1], "editor-chain");
    pid_t children[3];
    for (int i = 0; i < 3; i++) {
        children[i] = fork();
        check(children[i] >= 0);
        if (!children[i]) {
            close(ready[0]);
            const char *mode = i == 0 ? argv[1] : (i == 1 ? "socket" : "null");
            input(mode);
            char fd[24];
            snprintf(fd, sizeof(fd), "%d", ready[1]);
            char *args[] = { "npm exec renamed MCP helper", "/nonexistent/renamed-entrypoint",
                (char *)mode, fd, (i == 0 && depth) ? "1" : "0", "worker", NULL };
            execv(helper, args);
            _exit(91);
        }
    }
    close(ready[1]);
    for (int i = 0; i < 3 + depth; i++) {
        char byte;
        check(read(ready[0], &byte, 1) == 1 && byte == 'R');
    }
    close(ready[0]);
    check(write(1, "READY\n", 6) == 6);
    while (running) usleep(1000);
    for (int i = 0; i < 3; i++) reap(children[i]);
    return 0;
}
"#;

#[test]
fn same_group_helpers_use_fd_metadata_not_names_or_arguments() {
    use std::io::Read;
    use std::os::unix::fs::MetadataExt;
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("workers.c");
    let executable = directory.path().join("frontend");
    std::fs::write(&source, WORKERS).unwrap();
    assert!(Command::new("cc")
        .args([
            "-std=c11",
            "-D_DEFAULT_SOURCE",
            "-Wall",
            "-Wextra",
            "-Werror"
        ])
        .arg(&source)
        .arg("-o")
        .arg(&executable)
        .status()
        .unwrap()
        .success());
    std::fs::copy(&executable, directory.path().join("node")).unwrap();
    let mappings = [HarnessMapping {
        harness: "claude".into(),
        path: executable.canonicalize().unwrap(),
    }];
    for (mode, expected) in [
        ("pipe", true),
        ("socket", true),
        ("null", true),
        ("chain", true),
        ("extra-null", true),
        ("extra-random", true),
        ("extra-urandom", true),
        ("churn", true),
        ("tty-input", false),
        ("editor-chain", false),
        ("extra-tty", false),
        ("extra-pty", false),
        ("extra-unknown", false),
        ("regular-input", false),
        ("closed-input", false),
    ] {
        let (mut master, slave, tty) = terminal();
        let mut command = Command::new(&executable);
        command
            .arg(mode)
            .stdin(Stdio::from(slave.try_clone().unwrap()))
            .stdout(Stdio::from(slave.try_clone().unwrap()))
            .stderr(Stdio::from(slave));
        // SAFETY: only async-signal-safe libc calls run between fork and exec;
        // stdin is the fixture's owned PTY slave, not a user pane.
        unsafe {
            command.pre_exec(|| {
                if libc::setsid() < 0 || libc::ioctl(0, libc::TIOCSCTTY as _, 0) < 0 {
                    return Err(std::io::Error::last_os_error());
                }
                Ok(())
            });
        }
        let job = FixtureJob(command.spawn().unwrap());
        // SAFETY: master is an owned live PTY FD; this enables bounded readiness polling.
        let result = unsafe { libc::fcntl(master.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK) };
        assert_eq!(result, 0);
        let start = Instant::now();
        let mut output = Vec::new();
        while !output.windows(5).any(|w| w == b"READY") {
            assert!(
                start.elapsed() < Duration::from_secs(5),
                "fixture readiness: {mode}"
            );
            let mut bytes = [0; 128];
            match master.read(&mut bytes) {
                Ok(count) => output.extend_from_slice(&bytes[..count]),
                Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {}
                Err(error) => panic!("fixture readiness {mode}: {error}"),
            }
            std::thread::sleep(Duration::from_millis(5));
        }
        let mut invocation = None;
        for _ in 0..if mode == "churn" { 10 } else { 1 } {
            let decision = eligible(job.0.id(), &tty, &mappings)
                .unwrap_or_else(|error| panic!("{mode}: {error:#}"));
            assert_eq!(decision.eligible, expected, "{mode}: {}", decision.reason);
            assert_eq!(decision.invocation.is_some(), expected);
            if let Some(current) = decision.invocation {
                assert_eq!(current.liveness(), Liveness::Alive);
                if let Some(previous) = invocation {
                    assert_eq!(current, previous);
                }
                invocation = Some(current);
            }
        }
        let snapshot = snapshot(
            job.0.id(),
            platform::device(std::fs::metadata(&tty).unwrap().rdev()),
            job.0.id(),
            &mappings,
            Instant::now(),
        )
        .unwrap();
        assert!(snapshot.members.len() >= 4);
        for helper in snapshot
            .members
            .iter()
            .filter(|p| p.identity.pid != job.0.id())
        {
            assert!(helper.argv.is_empty(), "helper argv must never be loaded");
        }
    }
}