taskers-runtime 0.7.0

PTY runtime and OSC signal parsing for taskers.
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
use std::{
    collections::{BTreeMap, HashMap},
    env, fs,
    io::{self, BufRead, BufReader, Read, Write},
    os::unix::io::AsRawFd,
    os::unix::net::{UnixListener, UnixStream},
    path::{Path, PathBuf},
    sync::{
        Arc, Mutex,
        atomic::{AtomicU64, Ordering},
        mpsc,
    },
    thread,
};

use anyhow::{Context, Result, anyhow, bail};
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
use serde::{Deserialize, Serialize};

use crate::{CommandSpec, PtySession, SignalStreamParser};

const MAX_TRANSCRIPT_BYTES: usize = 262_144;
const OUTPUT_CHUNK_BYTES: usize = 8192;
const ATTACH_ENV_KEYS: &[&str] = &[
    "HOME",
    "PATH",
    "SHELL",
    "TERM",
    "TERMINFO",
    "TERMINFO_DIRS",
    "COLORTERM",
    "TERM_PROGRAM",
    "TASKERS_AGENT_SESSION_ID",
    "TASKERS_CTL_PATH",
    "TASKERS_DISABLE_SHELL_INTEGRATION",
    "TASKERS_EMBEDDED",
    "TASKERS_PANE_ID",
    "TASKERS_REAL_SHELL",
    "TASKERS_SHELL_INTEGRATION_DIR",
    "TASKERS_SOCKET",
    "TASKERS_SURFACE_ID",
    "TASKERS_TERMINAL_SESSION_ID",
    "TASKERS_TERMINAL_SOCKET",
    "TASKERS_USER_BASHRC",
    "TASKERS_USER_ZDOTDIR",
    "TASKERS_WORKSPACE_ID",
    "TASKERS_TTY_NAME",
    "ZDOTDIR",
];

#[derive(Debug, Clone)]
pub struct TerminalSessionClient {
    socket_path: PathBuf,
}

impl TerminalSessionClient {
    pub fn new(socket_path: PathBuf) -> Self {
        Self { socket_path }
    }

    pub fn socket_path(&self) -> &Path {
        &self.socket_path
    }

    pub fn ping(&self) -> Result<()> {
        let mut stream = self.connect()?;
        write_request(&mut stream, &SessionRequest::Ping)?;
        match read_event(&mut BufReader::new(stream))? {
            SessionEvent::Pong => Ok(()),
            other => bail!("unexpected ping response: {other:?}"),
        }
    }

    pub fn has_session(&self, session_id: &str) -> Result<bool> {
        let mut stream = self.connect()?;
        write_request(
            &mut stream,
            &SessionRequest::HasSession {
                session_id: session_id.into(),
            },
        )?;
        match read_event(&mut BufReader::new(stream))? {
            SessionEvent::Exists { exists } => Ok(exists),
            SessionEvent::Error { message } => Err(anyhow!(message)),
            other => bail!("unexpected session lookup response: {other:?}"),
        }
    }

    pub fn terminate_session(&self, session_id: &str) -> Result<()> {
        let mut stream = self.connect()?;
        write_request(
            &mut stream,
            &SessionRequest::Terminate {
                session_id: session_id.into(),
            },
        )?;
        match read_event(&mut BufReader::new(stream))? {
            SessionEvent::Ack => Ok(()),
            SessionEvent::Error { message } => Err(anyhow!(message)),
            other => bail!("unexpected terminate response: {other:?}"),
        }
    }

    pub fn list_sessions(&self) -> Result<Vec<String>> {
        let mut stream = self.connect()?;
        write_request(&mut stream, &SessionRequest::ListSessions)?;
        match read_event(&mut BufReader::new(stream))? {
            SessionEvent::SessionList { session_ids } => Ok(session_ids),
            SessionEvent::Error { message } => Err(anyhow!(message)),
            other => bail!("unexpected session list response: {other:?}"),
        }
    }

    pub fn attach_or_create(&self, session_id: &str, shell_args: &[String]) -> Result<()> {
        let _terminal_mode = TerminalModeGuard::new()?;
        let (mut cols, mut rows) = terminal_size().unwrap_or((120, 40));
        let mut stream = self.connect()?;
        let attach = SessionRequest::Attach {
            session_id: session_id.into(),
            cols,
            rows,
            cwd: env::current_dir()
                .ok()
                .map(|path| path.display().to_string()),
            shell_args: shell_args.to_vec(),
            env: collect_attach_env(),
        };
        write_request(&mut stream, &attach)?;
        stream
            .set_nonblocking(true)
            .context("failed to set terminal session socket nonblocking")?;

        let stdin = io::stdin();
        let stdin_fd = stdin.as_raw_fd();
        let socket_fd = stream.as_raw_fd();
        let mut stdin = stdin.lock();
        let mut stdout = io::stdout().lock();
        let mut input_buffer = [0u8; 4096];
        let mut socket_buffer = Vec::new();

        loop {
            if let Some((next_cols, next_rows)) = terminal_size() {
                if next_cols != cols || next_rows != rows {
                    cols = next_cols;
                    rows = next_rows;
                    write_request(&mut stream, &SessionRequest::Resize { cols, rows })?;
                }
            }

            let mut pollfds = [
                libc::pollfd {
                    fd: stdin_fd,
                    events: libc::POLLIN | libc::POLLHUP | libc::POLLERR,
                    revents: 0,
                },
                libc::pollfd {
                    fd: socket_fd,
                    events: libc::POLLIN | libc::POLLERR | libc::POLLHUP,
                    revents: 0,
                },
            ];
            let poll_result = unsafe { libc::poll(pollfds.as_mut_ptr(), pollfds.len() as _, 100) };
            if poll_result < 0 {
                let error = io::Error::last_os_error();
                if error.kind() == io::ErrorKind::Interrupted {
                    continue;
                }
                return Err(error).context("terminal session poll failed");
            }

            if (pollfds[1].revents & (libc::POLLERR | libc::POLLHUP)) != 0 {
                break;
            }
            if (pollfds[1].revents & libc::POLLIN) != 0 {
                if pump_session_events(&mut stream, &mut socket_buffer, &mut stdout)? {
                    break;
                }
            }
            if (pollfds[0].revents & (libc::POLLERR | libc::POLLHUP)) != 0 {
                let _ = write_request(&mut stream, &SessionRequest::Detach);
                break;
            }
            if (pollfds[0].revents & libc::POLLIN) != 0 {
                let bytes_read = stdin
                    .read(&mut input_buffer)
                    .context("failed to read terminal stdin")?;
                if bytes_read == 0 {
                    let _ = write_request(&mut stream, &SessionRequest::Detach);
                    break;
                }
                write_request(
                    &mut stream,
                    &SessionRequest::Input {
                        data_b64: BASE64.encode(&input_buffer[..bytes_read]),
                    },
                )?;
            }
        }

        Ok(())
    }

    fn connect(&self) -> Result<UnixStream> {
        UnixStream::connect(&self.socket_path).with_context(|| {
            format!(
                "failed to connect to terminal session socket {}",
                self.socket_path.display()
            )
        })
    }
}

#[derive(Clone)]
pub struct TerminalSessionDaemon {
    sessions: Arc<Mutex<HashMap<String, SessionState>>>,
    next_client_id: Arc<AtomicU64>,
}

struct SessionState {
    pty: Arc<Mutex<PtySession>>,
    transcript: Vec<u8>,
    needs_redraw: bool,
    clients: HashMap<u64, mpsc::Sender<SessionEvent>>,
}

impl TerminalSessionDaemon {
    pub fn new() -> Self {
        Self {
            sessions: Arc::new(Mutex::new(HashMap::new())),
            next_client_id: Arc::new(AtomicU64::new(1)),
        }
    }

    pub fn serve(&self, socket_path: &Path) -> Result<()> {
        if let Some(parent) = socket_path.parent() {
            fs::create_dir_all(parent).with_context(|| {
                format!(
                    "failed to create terminal session socket directory {}",
                    parent.display()
                )
            })?;
        }
        if socket_path.exists() {
            fs::remove_file(socket_path).with_context(|| {
                format!("failed to remove stale socket {}", socket_path.display())
            })?;
        }

        let listener = UnixListener::bind(socket_path)
            .with_context(|| format!("failed to bind {}", socket_path.display()))?;
        set_private_socket_permissions(socket_path)?;
        for stream in listener.incoming() {
            let stream = match stream {
                Ok(stream) => stream,
                Err(error) => {
                    eprintln!("terminal session accept failed: {error}");
                    continue;
                }
            };
            let daemon = self.clone();
            thread::spawn(move || {
                if let Err(error) = daemon.handle_connection(stream) {
                    eprintln!("terminal session connection failed: {error:?}");
                }
            });
        }
        Ok(())
    }

    fn handle_connection(&self, mut stream: UnixStream) -> Result<()> {
        ensure_peer_is_owner(&stream)?;
        let mut reader = BufReader::new(
            stream
                .try_clone()
                .context("failed to clone terminal session stream")?,
        );
        let request = read_request(&mut reader)?;
        match request {
            SessionRequest::Ping => {
                write_event(&mut stream, &SessionEvent::Pong)?;
            }
            SessionRequest::ListSessions => {
                let session_ids = self
                    .sessions
                    .lock()
                    .expect("session daemon mutex poisoned")
                    .keys()
                    .cloned()
                    .collect::<Vec<_>>();
                write_event(&mut stream, &SessionEvent::SessionList { session_ids })?;
            }
            SessionRequest::HasSession { session_id } => {
                let exists = self
                    .sessions
                    .lock()
                    .expect("session daemon mutex poisoned")
                    .contains_key(&session_id);
                write_event(&mut stream, &SessionEvent::Exists { exists })?;
            }
            SessionRequest::Terminate { session_id } => {
                let state = self
                    .sessions
                    .lock()
                    .expect("session daemon mutex poisoned")
                    .remove(&session_id);
                if let Some(state) = state {
                    state
                        .pty
                        .lock()
                        .expect("pty session mutex poisoned")
                        .kill()
                        .ok();
                }
                write_event(&mut stream, &SessionEvent::Ack)?;
            }
            SessionRequest::Attach {
                session_id,
                cols,
                rows,
                cwd,
                shell_args,
                env,
            } => {
                self.attach_client(stream, session_id, cols, rows, cwd, shell_args, env)?;
            }
            other => bail!("unexpected initial request: {other:?}"),
        }
        Ok(())
    }

    fn attach_client(
        &self,
        stream: UnixStream,
        session_id: String,
        cols: u16,
        rows: u16,
        cwd: Option<String>,
        shell_args: Vec<String>,
        env: BTreeMap<String, String>,
    ) -> Result<()> {
        let created = self.ensure_session(&session_id, cols, rows, cwd, shell_args, env)?;

        let client_id = self.next_client_id.fetch_add(1, Ordering::Relaxed);
        let (tx, rx) = mpsc::channel::<SessionEvent>();
        let (transcript, redraw_after_attach, pty) = {
            let mut sessions = self.sessions.lock().expect("session daemon mutex poisoned");
            let session = sessions
                .get_mut(&session_id)
                .ok_or_else(|| anyhow!("session {session_id} was not created"))?;
            let redraw_after_attach =
                !created && session.needs_redraw && session.transcript.is_empty();
            session.clients.insert(client_id, tx.clone());
            if redraw_after_attach {
                session.needs_redraw = false;
            }
            (
                session.transcript.clone(),
                redraw_after_attach,
                Arc::clone(&session.pty),
            )
        };

        tx.send(SessionEvent::Attached).ok();
        queue_output(&tx, &transcript);
        if redraw_after_attach {
            let _ = pty
                .lock()
                .expect("pty session mutex poisoned")
                .write_all(b"\x0c");
        }

        let writer_stream = stream
            .try_clone()
            .context("failed to clone client stream")?;
        let writer = thread::spawn(move || -> Result<()> {
            let mut writer = writer_stream;
            while let Ok(event) = rx.recv() {
                write_event(&mut writer, &event)?;
            }
            Ok(())
        });

        let mut reader = BufReader::new(stream);
        loop {
            match read_request(&mut reader) {
                Ok(SessionRequest::Input { data_b64 }) => {
                    let bytes = BASE64
                        .decode(data_b64)
                        .context("failed to decode session input")?;
                    let pty = {
                        let sessions = self.sessions.lock().expect("session daemon mutex poisoned");
                        sessions
                            .get(&session_id)
                            .map(|session| Arc::clone(&session.pty))
                            .ok_or_else(|| anyhow!("session {session_id} vanished"))?
                    };
                    pty.lock()
                        .expect("pty session mutex poisoned")
                        .write_all(&bytes)
                        .context("failed to write session input")?;
                }
                Ok(SessionRequest::Resize { cols, rows }) => {
                    let pty = {
                        let sessions = self.sessions.lock().expect("session daemon mutex poisoned");
                        sessions
                            .get(&session_id)
                            .map(|session| Arc::clone(&session.pty))
                            .ok_or_else(|| anyhow!("session {session_id} vanished"))?
                    };
                    pty.lock()
                        .expect("pty session mutex poisoned")
                        .resize(cols, rows)
                        .context("failed to resize session")?;
                }
                Ok(SessionRequest::Detach) => break,
                Ok(other) => bail!("unexpected attach request: {other:?}"),
                Err(error) => {
                    if is_unexpected_eof(&error) {
                        break;
                    }
                    return Err(error);
                }
            }
        }

        {
            let mut sessions = self.sessions.lock().expect("session daemon mutex poisoned");
            if let Some(session) = sessions.get_mut(&session_id) {
                session.clients.remove(&client_id);
                if session.clients.is_empty() {
                    session.transcript.clear();
                    session.needs_redraw = true;
                }
            }
        }
        drop(tx);
        writer
            .join()
            .map_err(|_| anyhow!("client writer panicked"))??;
        Ok(())
    }

    fn ensure_session(
        &self,
        session_id: &str,
        cols: u16,
        rows: u16,
        cwd: Option<String>,
        shell_args: Vec<String>,
        env: BTreeMap<String, String>,
    ) -> Result<bool> {
        let mut sessions = self.sessions.lock().expect("session daemon mutex poisoned");
        if sessions.contains_key(session_id) {
            return Ok(false);
        }

        let spec = build_command_spec(cols, rows, cwd, shell_args, env)?;
        let spawned = PtySession::spawn(&spec)?;
        let pty = Arc::new(Mutex::new(spawned.session));
        let reader = spawned.reader;
        sessions.insert(
            session_id.into(),
            SessionState {
                pty: Arc::clone(&pty),
                transcript: Vec::new(),
                needs_redraw: false,
                clients: HashMap::new(),
            },
        );
        drop(sessions);
        self.spawn_reader(session_id.to_string(), reader);
        Ok(true)
    }

    fn spawn_reader(&self, session_id: String, mut reader: crate::PtyReader) {
        let sessions = Arc::clone(&self.sessions);
        thread::spawn(move || {
            let mut buffer = [0u8; 4096];
            let mut signal_parser = SignalStreamParser::default();
            loop {
                let bytes_read = match reader.read_into(&mut buffer) {
                    Ok(0) => break,
                    Ok(bytes_read) => bytes_read,
                    Err(_) => break,
                };
                let chunk = &buffer[..bytes_read];
                let mut clients = Vec::new();
                {
                    let mut all_sessions = sessions.lock().expect("session daemon mutex poisoned");
                    let Some(session) = all_sessions.get_mut(&session_id) else {
                        break;
                    };
                    session.transcript.extend_from_slice(chunk);
                    trim_transcript(&mut session.transcript);
                    clients.extend(session.clients.values().cloned());
                }

                for _event in signal_parser.push_events(&String::from_utf8_lossy(chunk)) {}
                for client in clients {
                    queue_output(&client, chunk);
                }
            }

            let clients = {
                let mut all_sessions = sessions.lock().expect("session daemon mutex poisoned");
                all_sessions
                    .remove(&session_id)
                    .map(|session| session.clients.into_values().collect::<Vec<_>>())
                    .unwrap_or_default()
            };
            for client in clients {
                client.send(SessionEvent::Closed).ok();
            }
        });
    }
}

fn build_command_spec(
    cols: u16,
    rows: u16,
    cwd: Option<String>,
    mut shell_args: Vec<String>,
    mut env_map: BTreeMap<String, String>,
) -> Result<CommandSpec> {
    let integration_dir = env_map
        .get("TASKERS_SHELL_INTEGRATION_DIR")
        .cloned()
        .ok_or_else(|| anyhow!("missing TASKERS_SHELL_INTEGRATION_DIR"))?;
    env_map.insert("TASKERS_SESSION_CHILD".into(), "1".into());
    let wrapper_path = PathBuf::from(integration_dir).join("taskers-shell-wrapper.sh");
    let mut args = vec![wrapper_path.display().to_string()];
    args.append(&mut shell_args);
    env_map
        .entry("TERM".into())
        .or_insert_with(|| "xterm-256color".into());

    Ok(CommandSpec {
        program: "sh".into(),
        args,
        cwd: cwd.map(PathBuf::from),
        env: env_map,
        cols,
        rows,
    })
}

fn collect_attach_env() -> BTreeMap<String, String> {
    let mut env_map = BTreeMap::new();
    for key in ATTACH_ENV_KEYS {
        if let Ok(value) = env::var(key) {
            env_map.insert((*key).into(), value);
        }
    }
    env_map
}

fn trim_transcript(transcript: &mut Vec<u8>) {
    if transcript.len() <= MAX_TRANSCRIPT_BYTES {
        return;
    }
    let drop_len = transcript.len() - MAX_TRANSCRIPT_BYTES;
    transcript.drain(..drop_len);
}

fn queue_output(sender: &mpsc::Sender<SessionEvent>, bytes: &[u8]) {
    for chunk in bytes.chunks(OUTPUT_CHUNK_BYTES) {
        sender
            .send(SessionEvent::Output {
                data_b64: BASE64.encode(chunk),
            })
            .ok();
    }
}

fn write_request(stream: &mut UnixStream, request: &SessionRequest) -> Result<()> {
    let data = serde_json::to_vec(request).context("failed to serialize session request")?;
    stream
        .write_all(&data)
        .context("failed to write session request")?;
    stream
        .write_all(b"\n")
        .context("failed to terminate session request")?;
    stream.flush().ok();
    Ok(())
}

fn write_event(stream: &mut UnixStream, event: &SessionEvent) -> Result<()> {
    let data = serde_json::to_vec(event).context("failed to serialize session event")?;
    stream
        .write_all(&data)
        .context("failed to write session event")?;
    stream
        .write_all(b"\n")
        .context("failed to terminate session event")?;
    stream.flush().ok();
    Ok(())
}

fn read_request(reader: &mut BufReader<UnixStream>) -> Result<SessionRequest> {
    let mut line = String::new();
    let bytes = reader
        .read_line(&mut line)
        .context("failed to read session request")?;
    if bytes == 0 {
        bail!("unexpected EOF while reading session request");
    }
    serde_json::from_str(line.trim_end()).context("failed to parse session request")
}

fn read_event(reader: &mut BufReader<UnixStream>) -> Result<SessionEvent> {
    let mut line = String::new();
    let bytes = reader
        .read_line(&mut line)
        .context("failed to read session event")?;
    if bytes == 0 {
        bail!("unexpected EOF while reading session event");
    }
    serde_json::from_str(line.trim_end()).context("failed to parse session event")
}

fn is_unexpected_eof(error: &anyhow::Error) -> bool {
    error
        .to_string()
        .contains("unexpected EOF while reading session request")
}

fn set_private_socket_permissions(socket_path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;

    let permissions = fs::Permissions::from_mode(0o600);
    fs::set_permissions(socket_path, permissions).with_context(|| {
        format!(
            "failed to set private permissions on terminal socket {}",
            socket_path.display()
        )
    })
}

fn ensure_peer_is_owner(stream: &UnixStream) -> Result<()> {
    #[cfg(not(target_os = "linux"))]
    {
        let _ = stream;
        return Ok(());
    }

    #[cfg(target_os = "linux")]
    {
        let expected_uid = unsafe { libc::geteuid() };
        let mut credentials = libc::ucred {
            pid: 0,
            uid: 0,
            gid: 0,
        };
        let mut len = std::mem::size_of::<libc::ucred>() as libc::socklen_t;
        let result = unsafe {
            libc::getsockopt(
                stream.as_raw_fd(),
                libc::SOL_SOCKET,
                libc::SO_PEERCRED,
                (&mut credentials as *mut libc::ucred).cast(),
                &mut len,
            )
        };
        if result != 0 {
            return Err(io::Error::last_os_error()).context("failed to read peer credentials");
        }
        if credentials.uid != expected_uid {
            bail!(
                "rejecting terminal session client from uid {} (expected {})",
                credentials.uid,
                expected_uid
            );
        }
        Ok(())
    }
}

fn pump_session_events(
    stream: &mut UnixStream,
    pending: &mut Vec<u8>,
    stdout: &mut impl Write,
) -> Result<bool> {
    let mut buffer = [0u8; 4096];
    loop {
        match stream.read(&mut buffer) {
            Ok(0) => return Ok(true),
            Ok(bytes_read) => pending.extend_from_slice(&buffer[..bytes_read]),
            Err(error) if error.kind() == io::ErrorKind::WouldBlock => break,
            Err(error) => return Err(error).context("failed to read terminal session socket"),
        }
    }

    while let Some(newline) = pending.iter().position(|byte| *byte == b'\n') {
        let line = pending.drain(..=newline).collect::<Vec<_>>();
        let event = serde_json::from_slice::<SessionEvent>(&line[..line.len() - 1])
            .context("failed to parse session event")?;
        match event {
            SessionEvent::Attached => {}
            SessionEvent::Output { data_b64 } => {
                let bytes = BASE64
                    .decode(data_b64)
                    .context("failed to decode sidecar output")?;
                stdout
                    .write_all(&bytes)
                    .context("failed to write sidecar output")?;
                stdout.flush().ok();
            }
            SessionEvent::Closed => return Ok(true),
            SessionEvent::Error { message } => return Err(anyhow!(message)),
            other => bail!("unexpected attach event: {other:?}"),
        }
    }

    Ok(false)
}

struct TerminalModeGuard {
    fd: i32,
    original: libc::termios,
}

impl TerminalModeGuard {
    fn new() -> Result<Option<Self>> {
        let stdin = io::stdin();
        let fd = stdin.as_raw_fd();
        let is_tty = unsafe { libc::isatty(fd) } == 1;
        if !is_tty {
            return Ok(None);
        }

        let mut termios = unsafe { std::mem::zeroed::<libc::termios>() };
        let get_result = unsafe { libc::tcgetattr(fd, &mut termios) };
        if get_result != 0 {
            bail!("failed to read terminal mode");
        }
        let original = termios;
        unsafe {
            libc::cfmakeraw(&mut termios);
        }
        let set_result = unsafe { libc::tcsetattr(fd, libc::TCSANOW, &termios) };
        if set_result != 0 {
            bail!("failed to set terminal raw mode");
        }

        Ok(Some(Self { fd, original }))
    }
}

impl Drop for TerminalModeGuard {
    fn drop(&mut self) {
        unsafe {
            libc::tcsetattr(self.fd, libc::TCSANOW, &self.original);
        }
    }
}

fn terminal_size() -> Option<(u16, u16)> {
    let stdout = io::stdout();
    let fd = stdout.as_raw_fd();
    let mut winsize = libc::winsize {
        ws_row: 0,
        ws_col: 0,
        ws_xpixel: 0,
        ws_ypixel: 0,
    };
    let result = unsafe { libc::ioctl(fd, libc::TIOCGWINSZ, &mut winsize) };
    if result != 0 || winsize.ws_col == 0 || winsize.ws_row == 0 {
        return None;
    }
    Some((winsize.ws_col, winsize.ws_row))
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum SessionRequest {
    Ping,
    ListSessions,
    HasSession {
        session_id: String,
    },
    Terminate {
        session_id: String,
    },
    Attach {
        session_id: String,
        cols: u16,
        rows: u16,
        cwd: Option<String>,
        shell_args: Vec<String>,
        env: BTreeMap<String, String>,
    },
    Input {
        data_b64: String,
    },
    Resize {
        cols: u16,
        rows: u16,
    },
    Detach,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum SessionEvent {
    Pong,
    SessionList { session_ids: Vec<String> },
    Exists { exists: bool },
    Ack,
    Attached,
    Output { data_b64: String },
    Closed,
    Error { message: String },
}

#[cfg(test)]
mod tests {
    use super::{collect_attach_env, terminal_size};

    #[test]
    fn collect_attach_env_preserves_terminal_session_identity() {
        unsafe {
            std::env::set_var("TASKERS_TERMINAL_SESSION_ID", "session-123");
        }
        let env = collect_attach_env();
        assert_eq!(
            env.get("TASKERS_TERMINAL_SESSION_ID").map(String::as_str),
            Some("session-123")
        );
        unsafe {
            std::env::remove_var("TASKERS_TERMINAL_SESSION_ID");
        }
    }

    #[test]
    fn terminal_size_helper_returns_none_or_positive_dimensions() {
        match terminal_size() {
            Some((cols, rows)) => {
                assert!(cols > 0);
                assert!(rows > 0);
            }
            None => {}
        }
    }
}