cellix-agent 0.5.1

Local socket and CLI adapters for Cellix
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
//! The local socket any agent can use to reach a running Cellix. One line in, one line
//! out: `comments`, `take` (comments, then clear them), `threads`, `take-threads` (those an
//! agent hasn't seen), `reply <id> <author>` then a tab and the text, or `open <path>`.
//!
//! It's a Unix socket where there are those, and elsewhere a port on 127.0.0.1 behind a token,
//! as `loopback` describes. Agents call `cellix`, so only this crate tells the two apart.

use std::env;
use std::fs;
use std::io::{self, BufRead, BufReader, ErrorKind, Write};
#[cfg(unix)]
use std::os::unix::fs::{DirBuilderExt, PermissionsExt};
#[cfg(unix)]
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::{Path, PathBuf};
use std::sync::mpsc::{Receiver, Sender, channel};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

use cellix_review::Comment;
use serde::{Deserialize, Serialize};

pub mod hook;
pub mod ide;
mod loopback;

/// How long a request waits on the app, which may be busy or gone. An agent's hook must never
/// hang on it.
const ANSWER_TIME: Duration = Duration::from_secs(2);
/// How long a client waits for a reply: longer than the app is given, so a busy app's own
/// answer arrives. A hook asks several things per prompt, so it's never much more.
const REPLY_TIME: Duration = Duration::from_secs(5);
/// How long a client waits for a Cellix to connect and prove itself.
const PROOF_TIME: Duration = Duration::from_secs(2);

/// Comments shared between the app and the socket thread.
pub type SharedComments = Arc<Mutex<Vec<Comment>>>;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum AgentState {
    Working,
    NeedsYou,
    Idle,
    Ended,
}

/// What an agent is doing, as its hooks report it.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AgentEvent {
    pub session: String,
    pub agent: String,
    pub state: AgentState,
    pub detail: String,
    pub folder: String,
}

/// A test command an agent ran.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TestRun {
    pub command: String,
    pub passed: bool,
    pub output: String,
}

/// The reviewer's answer to a plan.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Decision {
    /// With the plan's new text, if the reviewer edited it.
    Approve(Option<String>),
    SendBack(String),
}

/// Requests that need the app itself, drained on its next tick.
#[derive(Debug)]
pub enum Request {
    Open(PathBuf),
    /// The socket thread already emptied the shared list.
    CommentsTaken,
    Agent(AgentEvent),
    Test(TestRun),
    /// Input for a Cellix running as a pane, as the mod's JSON.
    Pane(String),
    /// An agent answering a comment.
    Reply {
        id: u32,
        author: String,
        text: String,
        /// Whether that comment exists, so an answer can't vanish with a success.
        found: Sender<bool>,
    },
    /// An agent suggesting a comment the reviewer didn't make. Answers with its number.
    Suggest {
        file: String,
        line: usize,
        kind: String,
        quote: String,
        text: String,
        author: String,
        made: Sender<u32>,
    },
    /// Wants the comments on the project, as JSON.
    Threads(Sender<String>),
    /// Wants the comments no agent has seen, as JSON, and marks them seen.
    TakeThreads(Sender<String>),
    /// A plan is waiting. The connection stays open until `reply` is answered or dropped.
    Plan {
        path: PathBuf,
        reply: Sender<Decision>,
    },
}

fn fnv(text: &str) -> u64 {
    text.bytes().fold(0xcbf29ce484222325, |hash, byte| {
        (hash ^ byte as u64).wrapping_mul(0x100000001b3)
    })
}

/// How agents reach a Cellix.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Transport {
    /// A Unix socket at `socket_path`.
    #[cfg(unix)]
    Unix,
    /// A port on 127.0.0.1, named with its token in a file at `socket_path`.
    Loopback,
}

impl Transport {
    /// What `start` and `request` use here.
    #[cfg(unix)]
    pub const NATIVE: Self = Self::Unix;
    #[cfg(not(unix))]
    pub const NATIVE: Self = Self::Loopback;
}

/// A token no one else can guess.
pub(crate) fn new_token() -> io::Result<String> {
    let mut bytes = [0u8; 16];
    #[cfg(unix)]
    io::Read::read_exact(&mut fs::File::open("/dev/urandom")?, &mut bytes)?;
    #[cfg(not(unix))]
    getrandom::fill(&mut bytes).map_err(io::Error::other)?;
    Ok(bytes.iter().map(|b| format!("{b:02x}")).collect())
}

/// The folder the sockets live in, which only this user can enter. On Unix it's named for
/// the user in the shared temp folder, and made private.
#[cfg(unix)]
fn sockets_folder() -> PathBuf {
    // SAFETY: getuid has no preconditions and cannot fail.
    let uid = unsafe { libc::getuid() };
    env::temp_dir().join(format!("cellix-{uid}"))
}

#[cfg(not(unix))]
fn sockets_folder() -> PathBuf {
    windows_sockets_folder(|name| env::var(name).ok())
}

/// The folder the sockets live in on Windows: the user's own local app data, which only they
/// and administrators can read, wherever TEMP has been pointed.
#[cfg_attr(unix, allow(dead_code))]
fn windows_sockets_folder(var: impl Fn(&str) -> Option<String>) -> PathBuf {
    let local = var("LOCALAPPDATA")
        .filter(|v| !v.is_empty())
        .map(PathBuf::from);
    local
        .or_else(|| {
            Some(
                PathBuf::from(var("USERPROFILE")?)
                    .join("AppData")
                    .join("Local"),
            )
        })
        .unwrap_or_else(env::temp_dir)
        .join("cellix")
        .join("sockets")
}

/// One socket per workspace, in a directory only this user can enter.
pub fn socket_path(root: &Path) -> PathBuf {
    // `\\?\C:\x` and `C:\x` are one folder, so they share a socket. Unix paths are as they are.
    let root = dunce::simplified(root);
    sockets_folder().join(format!("{:016x}.sock", fnv(&root.to_string_lossy())))
}

fn answer(line: &str, comments: &SharedComments, requests: &Sender<Request>) -> io::Result<String> {
    let mut parts = line.trim().splitn(2, ' ');
    let list = |clear: bool| -> io::Result<String> {
        let mut shared = comments.lock().map_err(|_| io::Error::other("poisoned"))?;
        let json = serde_json::to_string(&*shared)?;
        if clear {
            shared.clear();
        }
        Ok(json)
    };
    match (parts.next(), parts.next()) {
        (Some("comments"), None) => list(false),
        (Some("take"), None) => {
            let json = list(true)?;
            // The hook takes on every prompt, and an empty take isn't news.
            if json != "[]" {
                let _ = requests.send(Request::CommentsTaken);
            }
            Ok(json)
        }
        (Some("open"), Some(path)) => {
            let _ = requests.send(Request::Open(PathBuf::from(path)));
            Ok("ok".into())
        }
        (Some("threads"), None) => {
            let (reply, threads) = channel();
            let _ = requests.send(Request::Threads(reply));
            Ok(threads
                .recv_timeout(ANSWER_TIME)
                .unwrap_or_else(|_| "[]".to_string()))
        }
        (Some("take-threads"), None) => {
            let (reply, threads) = channel();
            let _ = requests.send(Request::TakeThreads(reply));
            Ok(threads
                .recv_timeout(ANSWER_TIME)
                .unwrap_or_else(|_| "[]".to_string()))
        }
        (Some("reply"), Some(rest)) => {
            let Some((head, text)) = rest.split_once('\t') else {
                return Ok("error: reply needs an id, an author and text".into());
            };
            let (id, author) = head.split_once(' ').unwrap_or((head, "Agent"));
            let Ok(id) = id.parse() else {
                return Ok("error: reply needs a comment's number".into());
            };
            let (author, text) = (author.to_string(), text.replace("\\n", "\n"));
            let (found, answer) = channel();
            let _ = requests.send(Request::Reply {
                id,
                author,
                text,
                found,
            });
            Ok(match answer.recv_timeout(ANSWER_TIME) {
                Ok(true) => "ok".to_string(),
                _ => format!("error: there's no comment {id}"),
            })
        }
        (Some("suggest"), Some(json)) => {
            let value: serde_json::Value = serde_json::from_str(json)?;
            let field = |name: &str| value[name].as_str().unwrap_or_default().to_string();
            let (made, answer) = channel();
            let _ = requests.send(Request::Suggest {
                file: field("file"),
                line: value["line"].as_u64().unwrap_or(1) as usize,
                kind: field("kind"),
                quote: field("quote"),
                text: field("text"),
                author: field("author"),
                made,
            });
            Ok(match answer.recv_timeout(ANSWER_TIME) {
                Ok(id) => format!("ok {id}"),
                Err(_) => "error: Cellix didn't take the suggestion".to_string(),
            })
        }
        (Some("pane"), Some(json)) => {
            let _ = requests.send(Request::Pane(json.to_string()));
            Ok("ok".into())
        }
        (Some("event"), Some(json)) => {
            let _ = requests.send(Request::Agent(serde_json::from_str(json)?));
            Ok("ok".into())
        }
        (Some("test"), Some(json)) => {
            let _ = requests.send(Request::Test(serde_json::from_str(json)?));
            Ok("ok".into())
        }
        (Some("plan"), Some(path)) => {
            let (reply, decision) = channel();
            let path = PathBuf::from(path);
            let _ = requests.send(Request::Plan { path, reply });
            // Quitting drops the sender, and the agent goes back to asking as usual.
            Ok(match decision.recv() {
                Ok(Decision::Approve(None)) => "approve".to_string(),
                Ok(Decision::Approve(Some(text))) => {
                    format!("approve {}", serde_json::to_string(&text)?)
                }
                Ok(Decision::SendBack(text)) => {
                    format!("send-back {}", serde_json::to_string(&text)?)
                }
                Err(_) => "pass".to_string(),
            })
        }
        _ => Ok("error: unknown request".into()),
    }
}

fn serve(
    mut reader: impl BufRead,
    mut stream: impl Write,
    comments: &SharedComments,
    requests: &Sender<Request>,
) -> io::Result<()> {
    let mut line = String::new();
    reader.read_line(&mut line)?;
    writeln!(stream, "{}", answer(&line, comments, requests)?)
}

/// The running server. Dropping it removes the socket file, and the plans kept beside it.
pub struct Server {
    path: PathBuf,
    owner: Owner,
    pub requests: Receiver<Request>,
}

/// What tells this server's socket file from one a later Cellix put in its place.
enum Owner {
    /// The Unix socket's inode.
    #[cfg(unix)]
    Socket(u64),
    Note(loopback::Note),
}

impl Server {
    /// Whether the socket file is still this server's own.
    fn owns_its_file(&self) -> bool {
        match &self.owner {
            #[cfg(unix)]
            Owner::Socket(inode) => {
                use std::os::unix::fs::MetadataExt;
                fs::symlink_metadata(&self.path).is_ok_and(|m| m.ino() == *inode)
            }
            Owner::Note(note) => note.is_at(&self.path),
        }
    }
}

impl Drop for Server {
    fn drop(&mut self) {
        // Two starting at once can both find no one here, and the second's file replaces the
        // first's. The first mustn't take it with it.
        if self.owns_its_file() {
            let _ = fs::remove_file(&self.path);
            let _ = fs::remove_dir_all(self.path.with_extension(""));
        }
    }
}

/// Makes the folder the sockets live in, which only this user can enter.
fn private_folder(dir: &Path) -> io::Result<()> {
    #[cfg(unix)]
    {
        fs::DirBuilder::new()
            .recursive(true)
            .mode(0o700)
            .create(dir)?;
        fs::set_permissions(dir, fs::Permissions::from_mode(0o700))
    }
    // Inside the user's own local app data, which only they and administrators can read.
    #[cfg(not(unix))]
    fs::DirBuilder::new().recursive(true).create(dir)
}

/// Binds the workspace socket. Fails if another Cellix already serves this workspace.
pub fn start(root: &Path, comments: SharedComments) -> io::Result<Server> {
    start_with(root, comments, Transport::NATIVE)
}

/// `start`, over the transport given.
pub fn start_with(
    root: &Path,
    comments: SharedComments,
    transport: Transport,
) -> io::Result<Server> {
    let path = socket_path(root);
    let dir = path.parent().ok_or_else(|| io::Error::other("no parent"))?;
    private_folder(dir)?;
    let (sender, requests) = channel();
    let owner = match transport {
        #[cfg(unix)]
        Transport::Unix => {
            if UnixStream::connect(&path).is_ok() {
                return Err(io::Error::new(ErrorKind::AddrInUse, "already running"));
            }
            let _ = fs::remove_file(&path);
            let listener = UnixListener::bind(&path)?;
            let inode = std::os::unix::fs::MetadataExt::ino(&fs::symlink_metadata(&path)?);
            thread::spawn(move || {
                for stream in listener.incoming().flatten() {
                    // One thread each, because a plan holds its connection open for as long as
                    // the reviewer takes.
                    let (comments, sender) = (comments.clone(), sender.clone());
                    thread::spawn(move || {
                        let reader = BufReader::new(stream.try_clone()?);
                        serve(reader, stream, &comments, &sender)
                    });
                }
            });
            Owner::Socket(inode)
        }
        Transport::Loopback => {
            if loopback::alive(&path) {
                return Err(io::Error::new(ErrorKind::AddrInUse, "already running"));
            }
            let _ = fs::remove_file(&path);
            let listener = Arc::new(loopback::bind(&path)?);
            let note = listener.note.clone();
            thread::spawn(move || {
                for stream in listener.incoming() {
                    let (comments, sender) = (comments.clone(), sender.clone());
                    let listener = listener.clone();
                    // Admitted on its own thread, so a stranger that never sends a token
                    // holds up no one else.
                    thread::spawn(move || {
                        let (reader, stream) = listener.admit(stream)?;
                        serve(reader, stream, &comments, &sender)
                    });
                }
            });
            Owner::Note(note)
        }
    };
    Ok(Server {
        path,
        owner,
        requests,
    })
}

/// Sends one request to the Cellix serving `root`.
pub fn request(root: &Path, line: &str) -> io::Result<String> {
    request_with(root, line, Transport::NATIVE)
}

/// How long to wait for the reply to `line`. A plan waits for the reviewer, however long they
/// take; anything else is answered at once or not at all, and a hook mustn't hang on it.
fn patience(line: &str) -> Option<Duration> {
    (!line.starts_with("plan ")).then_some(REPLY_TIME)
}

/// `request`, over the transport given.
pub fn request_with(root: &Path, line: &str, transport: Transport) -> io::Result<String> {
    let mut reply = String::new();
    match transport {
        #[cfg(unix)]
        Transport::Unix => {
            let mut stream = UnixStream::connect(socket_path(root))?;
            stream.set_read_timeout(patience(line))?;
            writeln!(stream, "{line}")?;
            BufReader::new(stream).read_line(&mut reply)?;
        }
        Transport::Loopback => {
            let mut stream = loopback::connect(&socket_path(root), PROOF_TIME)?;
            stream.set_read_timeout(patience(line))?;
            writeln!(stream, "{line}")?;
            BufReader::new(stream).read_line(&mut reply)?;
        }
    }
    // A reply cut short isn't one.
    if !reply.ends_with('\n') {
        return Err(io::Error::new(ErrorKind::UnexpectedEof, "no reply"));
    }
    Ok(reply.trim_end().to_string())
}

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

    fn comment() -> Comment {
        Comment {
            file: "a.rs".into(),
            line: 3,
            text: "why?".into(),
            ..Comment::default()
        }
    }

    #[test]
    fn comments_can_be_read_and_taken() {
        let dir = tempfile::tempdir().unwrap();
        let shared: SharedComments = Arc::new(Mutex::new(vec![comment()]));
        let server = start(dir.path(), shared.clone()).unwrap();
        let json = request(dir.path(), "comments").unwrap();
        let parsed: Vec<Comment> = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, vec![comment()]);
        assert_eq!(shared.lock().unwrap().len(), 1);
        request(dir.path(), "take").unwrap();
        assert!(shared.lock().unwrap().is_empty());
        assert!(matches!(server.requests.recv(), Ok(Request::CommentsTaken)));
    }

    #[test]
    fn open_reaches_the_app() {
        let dir = tempfile::tempdir().unwrap();
        let server = start(dir.path(), Arc::default()).unwrap();
        assert_eq!(request(dir.path(), "open /tmp/x.rs").unwrap(), "ok");
        assert!(matches!(
            server.requests.recv(),
            Ok(Request::Open(path)) if path == Path::new("/tmp/x.rs")
        ));
    }

    #[test]
    fn a_second_server_is_refused_and_stale_sockets_are_replaced() {
        let dir = tempfile::tempdir().unwrap();
        let server = start(dir.path(), Arc::default()).unwrap();
        assert!(start(dir.path(), Arc::default()).is_err());
        let path = server.path.clone();
        std::mem::forget(server);
        fs::remove_file(&path).unwrap();
        fs::write(&path, "stale").unwrap();
        assert!(start(dir.path(), Arc::default()).is_ok());
    }

    #[test]
    fn a_closed_server_takes_its_plans_with_it() {
        let dir = tempfile::tempdir().unwrap();
        let server = start(dir.path(), Arc::default()).unwrap();
        let plans = server.path.with_extension("").join("s1");
        fs::create_dir_all(&plans).unwrap();
        fs::write(plans.join("Plan.md"), "plan").unwrap();
        let path = server.path.clone();
        drop(server);
        assert!(!path.exists());
        assert!(!path.with_extension("").exists());
    }

    /// What Windows uses, proved on every platform.
    #[test]
    fn agents_reach_cellix_over_loopback_with_its_token() {
        let dir = tempfile::tempdir().unwrap();
        let shared: SharedComments = Arc::new(Mutex::new(vec![comment()]));
        let over = Transport::Loopback;
        let server = start_with(dir.path(), shared.clone(), over).unwrap();
        // Where the socket would be, a note of the port, a token each way, and the process.
        let note = fs::read_to_string(&server.path).unwrap();
        let fields: Vec<&str> = note.split_whitespace().collect();
        let port: u16 = fields[0].parse().unwrap();
        assert_eq!((fields[1].len(), fields[2].len()), (32, 32));
        assert_ne!(fields[1], fields[2]);
        assert_eq!(fields[3], std::process::id().to_string());
        #[cfg(unix)]
        assert_eq!(
            fs::metadata(&server.path).unwrap().permissions().mode() & 0o777,
            0o600
        );

        let json = request_with(dir.path(), "comments", over).unwrap();
        let parsed: Vec<Comment> = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, vec![comment()]);
        assert_eq!(
            request_with(dir.path(), "open /tmp/x.rs", over).unwrap(),
            "ok"
        );
        assert!(matches!(
            server.requests.recv(),
            Ok(Request::Open(path)) if path == Path::new("/tmp/x.rs")
        ));
        request_with(dir.path(), "take", over).unwrap();
        assert!(shared.lock().unwrap().is_empty());
        assert!(matches!(server.requests.recv(), Ok(Request::CommentsTaken)));

        // Anything else that finds the port gets nothing without the token, even with a
        // guess as long as it.
        for guess in ["comments", &"0".repeat(32)] {
            let mut stranger = std::net::TcpStream::connect(("127.0.0.1", port)).unwrap();
            writeln!(stranger, "{guess}\ncomments").unwrap();
            let mut reply = String::new();
            let _ = BufReader::new(stranger).read_line(&mut reply);
            assert_eq!(reply, "");
        }
        shared.lock().unwrap().push(comment());
        assert_ne!(request_with(dir.path(), "comments", over).unwrap(), "[]");

        // A second Cellix here is refused while this one answers.
        assert!(start_with(dir.path(), Arc::default(), over).is_err());
        let path = server.path.clone();
        drop(server);
        assert!(!path.exists());
    }

    /// A note as a Cellix writes it, for a port and process of the test's choosing.
    fn note_for(port: u16, pid: u32) -> String {
        let tokens = (new_token().unwrap(), new_token().unwrap());
        format!("{port} {} {} {pid}\n", tokens.0, tokens.1)
    }

    /// A port that refuses connections, as one whose Cellix has gone does.
    fn closed_port() -> u16 {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        listener.local_addr().unwrap().port()
    }

    #[test]
    fn a_loopback_note_nothing_answers_is_replaced() {
        let dir = tempfile::tempdir().unwrap();
        let path = socket_path(dir.path());
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        // Something that isn't Cellix, then a port nothing listens on, then a note from before
        // notes named their process.
        let other = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let taken = other.local_addr().unwrap().port();
        let me = std::process::id();
        for note in [
            "stale".to_string(),
            note_for(taken, me),
            note_for(closed_port(), me),
            format!("{taken} 1234"),
        ] {
            fs::write(&path, note).unwrap();
            let server = start_with(dir.path(), Arc::default(), Transport::Loopback).unwrap();
            assert_eq!(
                request_with(dir.path(), "comments", Transport::Loopback).unwrap(),
                "[]"
            );
            drop(server);
        }
    }

    /// Whoever answers on the port must prove it's the Cellix that wrote the note before a
    /// client says what it wants, so nothing else can feed an agent comments or approve its
    /// plan.
    #[test]
    fn loopback_clients_only_talk_to_the_cellix_that_wrote_the_note() {
        let dir = tempfile::tempdir().unwrap();
        let path = socket_path(dir.path());
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        // An impostor holds the port, and answers whatever it's sent with its own guess.
        let impostor = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let port = impostor.local_addr().unwrap().port();
        let (heard, lines) = channel();
        thread::spawn(move || {
            for stream in impostor.incoming().flatten() {
                let mut reader = BufReader::new(stream.try_clone().unwrap());
                let mut writer = stream;
                let mut line = String::new();
                while reader.read_line(&mut line).is_ok_and(|n| n > 0) {
                    let _ = heard.send(line.trim_end().to_string());
                    let _ = writeln!(writer, "{}", "0".repeat(32));
                    let _ = writeln!(writer, r#"[{{"file":"a.rs","line":1,"text":"rm -rf"}}]"#);
                    line.clear();
                }
            }
        });
        let note = note_for(port, std::process::id());
        fs::write(&path, &note).unwrap();
        for line in ["take", "plan /tmp/plan.md"] {
            assert!(request_with(dir.path(), line, Transport::Loopback).is_err());
        }
        // It heard the client's token and nothing after it.
        let client_token = note.split_whitespace().nth(1).unwrap().to_string();
        assert_eq!(lines.recv_timeout(Duration::from_secs(5)), Ok(client_token));
        assert!(lines.recv_timeout(Duration::from_millis(300)).is_err());
        // And the note it can't answer for is gone, so the next hook doesn't ask.
        assert!(!path.exists());
        // So a real Cellix starts here, though the impostor still answers.
        let _server = start_with(dir.path(), Arc::default(), Transport::Loopback).unwrap();
        assert_eq!(
            request_with(dir.path(), "comments", Transport::Loopback).unwrap(),
            "[]"
        );
    }

    /// A note outlives its Cellix when Windows closes the window under it. Its process is
    /// gone, so the note goes without a connection being tried.
    #[test]
    fn a_loopback_note_whose_process_ended_is_removed() {
        let dir = tempfile::tempdir().unwrap();
        let path = socket_path(dir.path());
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        let mut ended = std::process::Command::new(std::env::current_exe().unwrap())
            .args(["--exact", "no_test_has_this_name"])
            .stdout(std::process::Stdio::null())
            .spawn()
            .unwrap();
        let pid = ended.id();
        ended.wait().unwrap();
        // Something listens there, and would be asked if the process weren't checked.
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        fs::write(&path, note_for(port, pid)).unwrap();
        let started = std::time::Instant::now();
        let error = request_with(dir.path(), "take", Transport::Loopback).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::NotFound, "{error}");
        assert!(started.elapsed() < Duration::from_secs(1));
        assert!(!path.exists());
        listener.set_nonblocking(true).unwrap();
        assert!(listener.accept().is_err(), "it connected anyway");

        // A refused port removes its note too, and quickly.
        fs::write(&path, note_for(closed_port(), std::process::id())).unwrap();
        let started = std::time::Instant::now();
        assert!(request_with(dir.path(), "take", Transport::Loopback).is_err());
        assert!(started.elapsed() < PROOF_TIME + Duration::from_secs(1));
        assert!(!path.exists());
    }

    /// A Cellix that takes a connection and never answers holds a hook up for a few seconds
    /// at most, whichever way it's reached. Only a plan waits on.
    #[test]
    fn a_silent_cellix_holds_no_hook_up() {
        assert_eq!(patience("take"), Some(REPLY_TIME));
        assert_eq!(patience("event {}"), Some(REPLY_TIME));
        assert_eq!(patience("plan /tmp/p.md"), None);
        let dir = tempfile::tempdir().unwrap();
        let path = socket_path(dir.path());
        fs::create_dir_all(path.parent().unwrap()).unwrap();

        // Silent before proving itself.
        let mute = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        fs::write(
            &path,
            note_for(mute.local_addr().unwrap().port(), std::process::id()),
        )
        .unwrap();
        let started = std::time::Instant::now();
        assert!(request_with(dir.path(), "take", Transport::Loopback).is_err());
        let took = started.elapsed();
        assert!(took < PROOF_TIME + Duration::from_secs(2), "{took:?}");
        // A Cellix too slow to prove itself may only be busy, so its note stays.
        assert!(path.exists());

        // Proving itself, then silent: the reply time bounds it.
        let listener = loopback::bind(&path).unwrap();
        thread::spawn(move || {
            for stream in listener.incoming() {
                let admitted = listener.admit(stream);
                thread::sleep(Duration::from_secs(30));
                drop(admitted);
            }
        });
        let started = std::time::Instant::now();
        assert!(request_with(dir.path(), "take", Transport::Loopback).is_err());
        let took = started.elapsed();
        assert!(took >= REPLY_TIME / 2, "{took:?}");
        assert!(took < REPLY_TIME + Duration::from_secs(2), "{took:?}");

        #[cfg(unix)]
        {
            let _ = fs::remove_file(&path);
            let socket = UnixListener::bind(&path).unwrap();
            thread::spawn(move || {
                let held: Vec<_> = socket.incoming().collect();
                drop(held);
            });
            let started = std::time::Instant::now();
            assert!(request_with(dir.path(), "take", Transport::Unix).is_err());
            assert!(started.elapsed() < REPLY_TIME + Duration::from_secs(2));
        }
    }

    /// Two starting at once can both find the place empty, and the second's file replaces the
    /// first's. Closing the first leaves the second's alone.
    #[test]
    fn a_cellix_only_removes_its_own_socket_file() {
        for transport in [Transport::NATIVE, Transport::Loopback] {
            let dir = tempfile::tempdir().unwrap();
            let first = start_with(dir.path(), Arc::default(), transport).unwrap();
            let path = first.path.clone();
            fs::remove_file(&path).unwrap();
            let second = start_with(dir.path(), Arc::default(), transport).unwrap();
            drop(first);
            assert!(path.exists(), "{transport:?}");
            assert_eq!(
                request_with(dir.path(), "comments", transport).unwrap(),
                "[]"
            );
            drop(second);
            assert!(!path.exists(), "{transport:?}");
        }
    }

    /// Windows keeps sockets in the user's local app data, not wherever TEMP points, and
    /// doesn't name the folder for the user at all.
    #[test]
    fn windows_keeps_sockets_in_local_app_data() {
        let var = |name: &str| match name {
            "LOCALAPPDATA" => Some(r"C:\Users\Zoë\AppData\Local".to_string()),
            "TEMP" => Some(r"D:\shared".to_string()),
            _ => None,
        };
        let local = PathBuf::from(r"C:\Users\Zoë\AppData\Local");
        assert_eq!(
            windows_sockets_folder(var),
            local.join("cellix").join("sockets")
        );
        let profile = |name: &str| (name == "USERPROFILE").then(|| r"C:\Users\Zoë".to_string());
        let local = PathBuf::from(r"C:\Users\Zoë").join("AppData").join("Local");
        assert_eq!(
            windows_sockets_folder(profile),
            local.join("cellix").join("sockets")
        );
        #[cfg(not(unix))]
        assert_eq!(
            socket_path(Path::new(r"C:\x")).parent(),
            Some(windows_sockets_folder(|n| env::var(n).ok()).as_path())
        );
    }

    #[test]
    fn unknown_requests_get_an_error() {
        let dir = tempfile::tempdir().unwrap();
        let _server = start(dir.path(), Arc::default()).unwrap();
        assert!(
            request(dir.path(), "nonsense")
                .unwrap()
                .starts_with("error")
        );
    }
}