cellix 0.5.3

The AI companion IDE inside your agent: a Claude Code mod, Codex plugin and Gemini CLI extension
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
//! The host, on Unix: each program runs under a small process of its own, which owns the PTY
//! and a Unix socket and remembers recent output.

use std::collections::VecDeque;
use std::io::{self, Read, Write};
use std::os::unix::net::{UnixListener, UnixStream};
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use portable_pty::{CommandBuilder, MasterPty, PtySize, native_pty_system};

use super::{Job, Stopped};

/// How much output is kept for the next Cellix to show.
const KEPT: usize = 4 * 1024 * 1024;
/// How often the host looks at whether a shell is back at its prompt.
const LOOK: Duration = Duration::from_millis(500);
/// How long Cellix waits for a new host's socket.
const START_WAIT: Duration = Duration::from_secs(5);

const OUTPUT: u8 = b'o';
const EXITED: u8 = b'x';
const AT_PROMPT: u8 = b'p';
const INPUT: u8 = b'i';
const RESIZE: u8 = b'r';
const KILL: u8 = b'k';

fn send_frame(stream: &mut impl Write, kind: u8, payload: &[u8]) -> io::Result<()> {
    let mut frame = Vec::with_capacity(payload.len() + 5);
    frame.push(kind);
    frame.extend((payload.len() as u32).to_be_bytes());
    frame.extend(payload);
    stream.write_all(&frame)
}

fn read_frame(stream: &mut impl Read) -> io::Result<(u8, Vec<u8>)> {
    let mut head = [0; 5];
    stream.read_exact(&mut head)?;
    let length = u32::from_be_bytes([head[1], head[2], head[3], head[4]]) as usize;
    let mut payload = vec![0; length];
    stream.read_exact(&mut payload)?;
    Ok((head[0], payload))
}

fn pty_size(size: (u16, u16), pixels: (u16, u16)) -> PtySize {
    PtySize {
        rows: size.1,
        cols: size.0,
        pixel_width: pixels.0,
        pixel_height: pixels.1,
    }
}

fn ensure_private(folder: &Path) -> io::Result<()> {
    use std::os::unix::fs::{DirBuilderExt, PermissionsExt};
    std::fs::DirBuilder::new()
        .recursive(true)
        .mode(0o700)
        .create(folder)?;
    std::fs::set_permissions(folder, std::fs::Permissions::from_mode(0o700))
}

/// Starts a host for `job`, and connects to it. `program` is Cellix itself.
pub fn start(program: &Path, folder: &Path, job: &Job) -> io::Result<Held> {
    ensure_private(folder)?;
    let id = std::process::id();
    let stamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.subsec_nanos());
    let socket = folder.join(format!("{id}-{stamp}.sock"));
    let notes = socket.with_extension("json");
    // The program learns where it's held, so an agent's hooks can leave word of how to
    // resume it beside its socket.
    let mut job = job.clone();
    job.env
        .push(format!("{}={}", super::HELD_VAR, socket.display()));
    std::fs::write(&notes, serde_json::to_vec(&job)?)?;
    let mut command = Command::new(program);
    command
        .arg("session-host")
        .arg(&socket)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null());
    // SAFETY: setsid is async-signal-safe, and this runs in the child before it execs.
    unsafe {
        command.pre_exec(|| {
            if libc::setsid() < 0 {
                return Err(io::Error::last_os_error());
            }
            Ok(())
        });
    }
    command.spawn()?;
    let deadline = std::time::Instant::now() + START_WAIT;
    loop {
        match Held::connect(&socket) {
            Ok(held) => return Ok(held),
            Err(e) if std::time::Instant::now() > deadline => return Err(e),
            Err(_) => std::thread::sleep(Duration::from_millis(10)),
        }
    }
}

/// The sessions left running for a project, with what each is called.
pub fn left_running(folder: &Path) -> Vec<(String, PathBuf)> {
    let mut found = Vec::new();
    for entry in std::fs::read_dir(folder).into_iter().flatten().flatten() {
        let path = entry.path();
        if path.extension().is_none_or(|e| e != "sock") {
            continue;
        }
        if UnixStream::connect(&path).is_err() {
            // Its host is gone without finishing, as when the machine restarted, so what it
            // held is put aside to be offered again. A job that can't be read is forgotten.
            let _ = std::fs::remove_file(&path);
            put_aside(&path);
            continue;
        }
        let notes = std::fs::read(path.with_extension("json"));
        let job: Option<Job> = notes.ok().and_then(|n| serde_json::from_slice(&n).ok());
        if let Some(job) = job {
            found.push((job.name, path));
        }
    }
    found.sort();
    found
}

/// The files a host keeps beside its socket, by extension.
const BESIDE: [&str; 3] = ["json", "log", "agent"];

/// Moves what a host that died left behind into the stopped folder, if its job can be read.
fn put_aside(socket: &Path) {
    let notes = std::fs::read(socket.with_extension("json"));
    let readable = notes.is_ok_and(|n| serde_json::from_slice::<Job>(&n).is_ok());
    let Some(folder) = socket.parent() else {
        return;
    };
    let aside = folder.join(super::STOPPED);
    if readable && ensure_private(&aside).is_ok() {
        for extension in BESIDE {
            let from = socket.with_extension(extension);
            if let Some(name) = from.file_name() {
                let _ = std::fs::rename(&from, aside.join(name));
            }
        }
    }
    for extension in BESIDE {
        let _ = std::fs::remove_file(socket.with_extension(extension));
    }
}

/// What stopped without finishing, as when the machine restarted, oldest first. Each has the
/// job it ran, what it last showed, and how to resume it if an agent left word.
pub fn stopped(folder: &Path) -> Vec<Stopped> {
    let aside = folder.join(super::STOPPED);
    // Anything still in the folder whose host is gone is put aside first.
    left_running(folder);
    let mut found = Vec::new();
    for entry in std::fs::read_dir(&aside).into_iter().flatten().flatten() {
        let path = entry.path();
        if path.extension().is_none_or(|e| e != "json") {
            continue;
        }
        let Some(job) = std::fs::read(&path)
            .ok()
            .and_then(|n| serde_json::from_slice::<Job>(&n).ok())
        else {
            let _ = std::fs::remove_file(&path);
            continue;
        };
        let when = entry.metadata().and_then(|m| m.modified()).ok();
        let log = path.with_extension("log");
        let ended = std::fs::metadata(&log)
            .and_then(|m| m.modified())
            .ok()
            .or(when);
        let output = std::fs::read(&log).unwrap_or_default();
        let agent = std::fs::read(path.with_extension("agent"))
            .ok()
            .and_then(|a| serde_json::from_slice(&a).ok());
        found.push((
            when,
            Stopped {
                job,
                output,
                agent,
                ended,
                notes: path,
            },
        ));
    }
    found.sort_by_key(|(when, _)| *when);
    found.into_iter().map(|(_, stopped)| stopped).collect()
}

/// A connection to a host, from Cellix's side.
pub struct Held {
    stream: UnixStream,
    pub socket: PathBuf,
    at_prompt: Arc<Mutex<bool>>,
}

/// Writes keys to a host, for the session to use like any writer.
pub struct Keys(UnixStream);

impl Write for Keys {
    fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
        send_frame(&mut self.0, INPUT, bytes)?;
        Ok(bytes.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

impl Held {
    pub fn connect(socket: &Path) -> io::Result<Self> {
        Ok(Self {
            stream: UnixStream::connect(socket)?,
            socket: socket.to_path_buf(),
            at_prompt: Arc::new(Mutex::new(true)),
        })
    }

    pub fn keys(&self) -> io::Result<Keys> {
        Ok(Keys(self.stream.try_clone()?))
    }

    /// Passes what the program writes to `output`, and says on `done` when it's over.
    pub fn listen(&self, output: Sender<Vec<u8>>, done: Sender<()>) -> io::Result<()> {
        let mut stream = self.stream.try_clone()?;
        let at_prompt = self.at_prompt.clone();
        std::thread::spawn(move || {
            while let Ok((kind, payload)) = read_frame(&mut stream) {
                match kind {
                    OUTPUT => {
                        if output.send(payload).is_err() {
                            return;
                        }
                    }
                    EXITED => {
                        let _ = done.send(());
                        return;
                    }
                    AT_PROMPT => {
                        if let Ok(mut flag) = at_prompt.lock() {
                            *flag = payload == [1];
                        }
                    }
                    _ => {}
                }
            }
            // The host went away, and took the program with it.
            let _ = done.send(());
        });
        Ok(())
    }

    pub fn at_prompt(&self) -> bool {
        self.at_prompt.lock().is_ok_and(|flag| *flag)
    }

    pub fn resize(&self, size: (u16, u16), pixels: (u16, u16)) {
        let mut payload = Vec::with_capacity(8);
        for number in [size.0, size.1, pixels.0, pixels.1] {
            payload.extend(number.to_be_bytes());
        }
        let _ = send_frame(&mut &self.stream, RESIZE, &payload);
    }

    pub fn kill(&self) {
        let _ = send_frame(&mut &self.stream, KILL, &[]);
    }
}

/// What the host shares between its threads.
#[derive(Default)]
struct Shared {
    kept: VecDeque<u8>,
    /// Every Cellix showing it, as on two machines at once, by the number of its visit.
    clients: Vec<Client>,
    /// Counts clients, so each can be told from the next.
    visit: u64,
    exited: bool,
    at_prompt: bool,
    /// What it printed, on disk too, so it can be shown again after the machine restarts.
    log: Option<Log>,
    /// The size the program has, as `RESIZE` sends it.
    size: Vec<u8>,
}

struct Client {
    visit: u64,
    stream: UnixStream,
    /// The size this client wants, which it gets once it types.
    size: Option<Vec<u8>>,
}

impl Shared {
    /// Tells every client, forgetting any that have gone.
    fn tell(&mut self, kind: u8, payload: &[u8]) {
        self.clients
            .retain_mut(|client| send_frame(&mut client.stream, kind, payload).is_ok());
    }

    fn output(&mut self, bytes: &[u8]) {
        self.kept.extend(bytes);
        let extra = self.kept.len().saturating_sub(KEPT);
        self.kept.drain(..extra);
        if let Some(log) = &mut self.log {
            log.add(bytes, &self.kept);
        }
        self.tell(OUTPUT, bytes);
    }

    /// The size a client asks for. The first, or one that is typing, gets it at once; with
    /// several watching, whoever types last decides, as in tmux.
    fn wants_size(&mut self, visit: u64, size: Vec<u8>) -> Option<Vec<u8>> {
        let alone = self.clients.len() <= 1;
        if let Some(client) = self.clients.iter_mut().find(|c| c.visit == visit) {
            client.size = Some(size.clone());
        }
        (alone || self.size.is_empty()).then(|| self.take_size(size))
    }

    /// Whether a client's own size should be taken because it's typing.
    fn typing(&mut self, visit: u64) -> Option<Vec<u8>> {
        let size = self
            .clients
            .iter()
            .find(|c| c.visit == visit)
            .and_then(|c| c.size.clone())?;
        (size != self.size).then(|| self.take_size(size))
    }

    fn take_size(&mut self, size: Vec<u8>) -> Vec<u8> {
        self.size = size.clone();
        size
    }
}

/// The output on disk, kept to about `KEPT` by starting again from what's in memory when it
/// grows to twice that.
struct Log {
    file: std::fs::File,
    path: PathBuf,
    written: usize,
}

impl Log {
    fn open(path: &Path) -> Option<Self> {
        use std::os::unix::fs::OpenOptionsExt;
        let file = std::fs::OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .mode(0o600)
            .open(path)
            .ok()?;
        Some(Self {
            file,
            path: path.to_path_buf(),
            written: 0,
        })
    }

    fn add(&mut self, bytes: &[u8], kept: &VecDeque<u8>) {
        if self.written + bytes.len() > 2 * KEPT {
            if let Some(fresh) = Self::open(&self.path) {
                *self = fresh;
            }
            let (front, back) = kept.as_slices();
            let _ = self
                .file
                .write_all(front)
                .and_then(|()| self.file.write_all(back));
            self.written = kept.len();
            return;
        }
        if self.file.write_all(bytes).is_ok() {
            self.written += bytes.len();
        }
    }
}

fn resize(master: &Arc<Mutex<Box<dyn MasterPty + Send>>>, payload: &[u8]) {
    if payload.len() != 8 {
        return;
    }
    let number = |at: usize| u16::from_be_bytes([payload[at], payload[at + 1]]);
    if let Ok(master) = master.lock() {
        let size = pty_size((number(0), number(2)), (number(4), number(6)));
        let _ = master.resize(size);
    }
}

fn serve_client(
    mut stream: UnixStream,
    (shared, visit): (&Arc<Mutex<Shared>>, u64),
    writer: &Arc<Mutex<Box<dyn Write + Send>>>,
    master: &Arc<Mutex<Box<dyn MasterPty + Send>>>,
    pid: Option<u32>,
) {
    while let Ok((kind, payload)) = read_frame(&mut stream) {
        match kind {
            INPUT => {
                let size = shared.lock().ok().and_then(|mut s| s.typing(visit));
                if let Some(size) = size {
                    resize(master, &size);
                }
                if let Ok(mut writer) = writer.lock() {
                    let _ = writer.write_all(&payload);
                    let _ = writer.flush();
                }
            }
            RESIZE if payload.len() == 8 => {
                let size = shared
                    .lock()
                    .ok()
                    .and_then(|mut s| s.wants_size(visit, payload));
                if let Some(size) = size {
                    resize(master, &size);
                }
            }
            KILL => {
                if let Some(pid) = pid {
                    // SAFETY: only signals the process this host started, and its group.
                    unsafe {
                        libc::kill(-(pid as i32), libc::SIGHUP);
                        libc::kill(pid as i32, libc::SIGHUP);
                    }
                }
            }
            _ => {}
        }
    }
}

/// Runs a job, serving its socket until the program has ended and someone has been told.
pub fn serve(socket: &Path) -> io::Result<()> {
    let notes = socket.with_extension("json");
    let job: Job = serde_json::from_slice(&std::fs::read(&notes)?)?;
    let pair = native_pty_system()
        .openpty(pty_size(job.size, job.pixels))
        .map_err(io::Error::other)?;
    let (program, args) = job.argv.split_first().ok_or(io::ErrorKind::InvalidInput)?;
    let mut command = CommandBuilder::new(program);
    command.args(args);
    command.cwd(&job.folder);
    command.env("TERM", "xterm-256color");
    command.env("COLORTERM", "truecolor");
    for pair in &job.env {
        if let Some((key, value)) = pair.split_once('=') {
            command.env(key, value);
        }
    }
    let mut child = pair
        .slave
        .spawn_command(command)
        .map_err(io::Error::other)?;
    drop(pair.slave);
    let pid = child.process_id();
    let mut reader = pair.master.try_clone_reader().map_err(io::Error::other)?;
    let writer = pair.master.take_writer().map_err(io::Error::other)?;
    let writer = Arc::new(Mutex::new(writer));
    let master = Arc::new(Mutex::new(pair.master));
    // With the job read, its environment needn't sit on disk.
    let quiet = Job {
        env: Vec::new(),
        ..job
    };
    std::fs::write(&notes, serde_json::to_vec(&quiet)?)?;
    let listener = UnixListener::bind(socket)?;
    let shared = Arc::new(Mutex::new(Shared {
        log: Log::open(&socket.with_extension("log")),
        ..Shared::default()
    }));

    let from_program = shared.clone();
    std::thread::spawn(move || {
        let mut buffer = [0; 8192];
        while let Ok(count) = reader.read(&mut buffer) {
            if count == 0 {
                break;
            }
            if let Ok(mut shared) = from_program.lock() {
                shared.output(&buffer[..count]);
            }
        }
    });

    let looking = shared.clone();
    let front = master.clone();
    std::thread::spawn(move || {
        loop {
            std::thread::sleep(LOOK);
            let leader = front.lock().ok().and_then(|m| m.process_group_leader());
            let at_prompt = leader.is_some() && leader.map(|p| p as u32) == pid;
            if let Ok(mut shared) = looking.lock()
                && shared.at_prompt != at_prompt
            {
                shared.at_prompt = at_prompt;
                shared.tell(AT_PROMPT, &[u8::from(at_prompt)]);
            }
        }
    });

    let ended = shared.clone();
    let place = socket.to_path_buf();
    std::thread::spawn(move || {
        let _ = child.wait();
        // What it printed last may still be on its way.
        std::thread::sleep(Duration::from_millis(100));
        if let Ok(mut shared) = ended.lock() {
            shared.exited = true;
            // Only someone who really heard it lets the host go. Otherwise it waits to say so.
            shared.tell(EXITED, &[]);
            if !shared.clients.is_empty() {
                finish(&place);
            }
        }
    });

    for stream in listener.incoming().flatten() {
        let Ok(mut outgoing) = stream.try_clone() else {
            continue;
        };
        let mut visit = 0;
        if let Ok(mut shared) = shared.lock() {
            let (front, back) = shared.kept.as_slices();
            let sent = send_frame(&mut outgoing, OUTPUT, front)
                .and_then(|()| send_frame(&mut outgoing, OUTPUT, back))
                .and_then(|()| send_frame(&mut outgoing, AT_PROMPT, &[u8::from(shared.at_prompt)]));
            if sent.is_err() {
                continue;
            }
            shared.visit += 1;
            visit = shared.visit;
            shared.clients.push(Client {
                visit,
                stream: outgoing,
                size: None,
            });
            if shared.exited {
                shared.tell(EXITED, &[]);
                finish(socket);
            }
        }
        let (writer, master, leaving) = (writer.clone(), master.clone(), shared.clone());
        std::thread::spawn(move || {
            serve_client(stream, (&leaving, visit), &writer, &master, pid);
            if let Ok(mut shared) = leaving.lock() {
                shared.clients.retain(|c| c.visit != visit);
            }
        });
    }
    Ok(())
}

/// The program is over and someone knows: nothing is left to hold.
fn finish(socket: &Path) -> ! {
    let _ = std::fs::remove_file(socket);
    for extension in BESIDE {
        let _ = std::fs::remove_file(socket.with_extension(extension));
    }
    std::process::exit(0);
}