triaged 0.2.0

Long-running daemon that owns Triage terminal session state and serves a built-in web client and WebSocket API for PIN-paired remote attach.
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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
// Filesystem ops are only used by the Unix socket path and by tests; on Windows
// the named-pipe transport touches no filesystem entries.
#[cfg(any(unix, test))]
use std::fs;
use std::io::{BufRead, BufReader, BufWriter, ErrorKind, Write};
#[cfg(unix)]
use std::os::unix::fs::{MetadataExt, PermissionsExt};
#[cfg(unix)]
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;

use anyhow::{Context, Result, anyhow, bail};
use serde::{Deserialize, Serialize};
use triage_core::session::{
    AttachSessionRequest, AttachSessionResponse, ClientId, CompletedSession, InputLeaseRequest,
    LeaseChange, ResizeSessionRequest, RestoreSessionRequest, ServerUpdateInfo, SessionApi,
    SessionEventEnvelope, SessionEventReceiver, SessionId, SessionSnapshot, StartSessionRequest,
    StyledRowsRequest, StyledRowsResponse, SubscribeSessionEventsRequest, WriteInputRequest,
};

use crate::session::SessionManager;

const SUBSCRIPTION_HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);

/// Local IPC transport seam.
///
/// The daemon's control plane speaks a newline-delimited JSON protocol over a
/// local, single-machine socket. On Unix that socket is a filesystem AF_UNIX
/// socket (hardened to `0o600`); on Windows it is a named pipe
/// (`\\.\pipe\triage-<user>`). Only the connect/listen primitives differ — the
/// wire protocol, request handlers, and client are shared. Handover (FD passing
/// via `SCM_RIGHTS`) is Unix-only and keeps its own `UnixStream` path.
mod transport {
    use super::*;

    /// A server-side accepted local IPC stream (yielded by the listener). On
    /// Unix the accept loop uses `UnixStream` directly; this alias names the
    /// Windows `local_socket::Stream` that `handle_connection` consumes.
    #[cfg(windows)]
    pub type LocalStream = interprocess::local_socket::Stream;

    /// A client-side connected local IPC stream. On Unix this is the same
    /// `UnixStream`; on Windows we use the raw named-pipe stream rather than the
    /// `local_socket::Stream` wrapper so the connect can take a wait timeout
    /// (the cross-platform `local_socket` connect hardcodes an unbounded wait).
    #[cfg(unix)]
    pub type ClientStream = UnixStream;
    #[cfg(windows)]
    pub type ClientStream = interprocess::os::windows::named_pipe::DuplexPipeStream<
        interprocess::os::windows::named_pipe::pipe_mode::Bytes,
    >;

    /// Upper bound on how long a client waits for a daemon instance to become
    /// available. The accept loop re-arms in microseconds, so this only matters
    /// when every pipe instance is momentarily busy; without it a busy pipe
    /// (`ERROR_PIPE_BUSY`) could block the client indefinitely.
    #[cfg(windows)]
    const CONNECT_TIMEOUT: Duration = Duration::from_secs(5);

    /// Connect a client to the daemon's local IPC endpoint.
    #[cfg(unix)]
    pub fn connect(path: &Path) -> std::io::Result<ClientStream> {
        UnixStream::connect(path)
    }

    #[cfg(windows)]
    pub fn connect(path: &Path) -> std::io::Result<ClientStream> {
        use interprocess::ConnectWaitMode;
        use interprocess::os::windows::named_pipe::{DuplexPipeStream, pipe_mode::Bytes};
        // `connect_by_path` does not prepend the `\\.\pipe\` prefix, so pass the
        // fully-qualified endpoint. A missing daemon fails fast (the pipe does
        // not exist); only an all-instances-busy pipe consumes the timeout.
        let endpoint = super::display_endpoint(path);
        DuplexPipeStream::<Bytes>::connect_by_path_with_wait_mode(
            endpoint.as_str(),
            ConnectWaitMode::Timeout(CONNECT_TIMEOUT),
        )
    }

    /// Signal end-of-request to the server. On Unix we half-close the write side
    /// as a courtesy; on Windows the newline already frames the request, so this
    /// is a no-op (named pipes have no half-close).
    #[cfg(unix)]
    pub fn finish_write(stream: &ClientStream) -> std::io::Result<()> {
        stream.shutdown(std::net::Shutdown::Write)
    }

    #[cfg(windows)]
    pub fn finish_write(_stream: &ClientStream) -> std::io::Result<()> {
        Ok(())
    }

    /// Build the `interprocess` namespaced name for a Windows named pipe from the
    /// configured socket path (which on Windows carries the bare pipe name).
    /// The single legal named-pipe token for `path`. A pipe lives at
    /// `\\.\pipe\<token>`, where `<token>` must not contain a path separator. The
    /// default socket path is already a clean `triage-<user>`, but a
    /// caller-supplied or test path may be filesystem-like (`…\triage.sock`);
    /// collapse separators into a legal token that is still unique per distinct
    /// path (so parallel tests with different temp dirs don't collide). Shared by
    /// the connect/listen name builder and by user-facing endpoint display.
    #[cfg(windows)]
    pub fn windows_pipe_token(path: &Path) -> std::io::Result<String> {
        let raw = path.to_str().ok_or_else(|| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "named pipe name is not valid UTF-8",
            )
        })?;
        // Accept either a bare token (`triage-<user>`, the default) or a full pipe
        // path (`\\.\pipe\triage-<user>` / `\\?\pipe\...`); strip the well-known
        // prefix so a user-typed full path maps to the same token, then collapse
        // any remaining separators into the single legal token.
        let bare = raw
            .strip_prefix(r"\\.\pipe\")
            .or_else(|| raw.strip_prefix(r"\\?\pipe\"))
            .unwrap_or(raw);
        let collapsed: String = bare
            .chars()
            .map(|c| match c {
                '\\' | '/' | ':' => '_',
                other => other,
            })
            .collect();

        // The full pipe path `\\.\pipe\<token>` is capped by NPFS at 256 UTF-16
        // code units (the Win32 string unit), not chars — a non-BMP char is one
        // `char` but two units. A deep override/test path could exceed that;
        // collapse an over-long token to a readable prefix plus a stable hash so
        // it stays legal and still unique per distinct path.
        if collapsed.encode_utf16().count() <= MAX_PIPE_TOKEN_LEN {
            return Ok(collapsed);
        }
        use sha2::{Digest, Sha256};
        // 16 hex chars (ASCII → 16 units) + one `_` separator = 17 units.
        let hash = hex::encode(&Sha256::digest(collapsed.as_bytes())[..8]);
        let prefix = truncate_utf16_units(&collapsed, MAX_PIPE_TOKEN_LEN - 17);
        Ok(format!("{prefix}_{hash}"))
    }

    /// Maximum length, in UTF-16 code units, for a named-pipe token. NPFS caps
    /// the full `\\.\pipe\<token>` path at 256 units; this leaves margin for the
    /// 9-unit `\\.\pipe\` prefix.
    #[cfg(windows)]
    pub const MAX_PIPE_TOKEN_LEN: usize = 210;

    /// Truncate `s` to at most `max_units` UTF-16 code units, stopping on a
    /// `char` boundary so a surrogate pair is never split.
    #[cfg(windows)]
    fn truncate_utf16_units(s: &str, max_units: usize) -> String {
        let mut out = String::new();
        let mut units = 0usize;
        for c in s.chars() {
            let w = c.len_utf16();
            if units + w > max_units {
                break;
            }
            out.push(c);
            units += w;
        }
        out
    }

    #[cfg(windows)]
    pub fn windows_pipe_name(
        path: &Path,
    ) -> std::io::Result<interprocess::local_socket::Name<'static>> {
        use interprocess::local_socket::{GenericNamespaced, ToNsName};
        windows_pipe_token(path)?.to_ns_name::<GenericNamespaced>()
    }
}

/// Human-facing description of the daemon's control endpoint, for log and error
/// messages. On Unix this is the socket file path; on Windows it is the full
/// named-pipe path (`\\.\pipe\<token>`), since the stored path holds only the
/// bare pipe token (a bare token reads like a typo in an error message).
pub fn display_endpoint(path: &Path) -> String {
    #[cfg(windows)]
    {
        if let Ok(token) = transport::windows_pipe_token(path) {
            return format!(r"\\.\pipe\{token}");
        }
    }
    path.display().to_string()
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IpcConfig {
    pub socket_path: PathBuf,
    /// How long to keep retrying a bind that fails only because another daemon
    /// still holds the socket.
    ///
    /// Zero for a normal start: finding the socket genuinely owned means another
    /// daemon is running, and failing immediately is the correct, loud answer.
    ///
    /// A successor that adopted sessions through a handover is the exception. It
    /// can reach this point while its predecessor is still finishing teardown, and
    /// there it owns live PTY masters — exiting would take every adopted session
    /// down with it. Waiting out the predecessor's exit turns a lost swap into a
    /// slightly slower one. See [`IpcConfig::with_bind_grace`].
    pub bind_grace: std::time::Duration,
}

impl IpcConfig {
    pub fn new(socket_path: impl Into<PathBuf>) -> Self {
        Self {
            socket_path: socket_path.into(),
            bind_grace: std::time::Duration::ZERO,
        }
    }

    /// Tolerate a socket still held by a predecessor for up to `grace`.
    ///
    /// Only meaningful for a daemon that adopted sessions via handover; see
    /// [`IpcConfig::bind_grace`].
    pub fn with_bind_grace(mut self, grace: std::time::Duration) -> Self {
        self.bind_grace = grace;
        self
    }
}

pub struct IpcServer {
    manager: Arc<SessionManager>,
    web_cache: Arc<crate::http::WebAssetCache>,
    config: IpcConfig,
}

impl IpcServer {
    pub fn new(
        manager: Arc<SessionManager>,
        web_cache: Arc<crate::http::WebAssetCache>,
        config: IpcConfig,
    ) -> Self {
        Self {
            manager,
            web_cache,
            config,
        }
    }

    #[cfg(unix)]
    pub fn serve(self) -> Result<()> {
        let listener = bind_owner_socket(&self.config.socket_path, self.config.bind_grace)?;

        loop {
            match listener.accept() {
                Ok((stream, _addr)) => {
                    let manager = Arc::clone(&self.manager);
                    let web_cache = Arc::clone(&self.web_cache);
                    spawn_client_handler(move || handle_connection(manager, web_cache, stream));
                }
                Err(error) => {
                    tracing::warn!(error = ?error, "failed to accept Unix socket connection");
                }
            }
        }
    }

    #[cfg(windows)]
    pub fn serve(self) -> Result<()> {
        use interprocess::local_socket::ListenerOptions;
        use interprocess::local_socket::traits::ListenerExt as _;

        let pipe_name = display_endpoint(&self.config.socket_path);

        // `create_sync` sets FILE_FLAG_FIRST_PIPE_INSTANCE, so a second daemon's
        // create fails atomically — no need for a self-connect preflight (which
        // could itself block and left a phantom connection in the accept loop).
        let listener = ListenerOptions::new()
            .name(transport::windows_pipe_name(&self.config.socket_path)?)
            .create_sync()
            .with_context(|| {
                format!("creating named pipe {pipe_name} (is another triaged already running?)")
            })?;

        for incoming in listener.incoming() {
            match incoming {
                Ok(stream) => {
                    let manager = Arc::clone(&self.manager);
                    let web_cache = Arc::clone(&self.web_cache);
                    spawn_client_handler(move || handle_connection(manager, web_cache, stream));
                }
                Err(error) => {
                    tracing::warn!(error = ?error, "failed to accept named pipe connection");
                }
            }
        }
        Ok(())
    }
}

/// Spawn a detached worker thread to service one accepted IPC connection. Shared
/// by the Unix and Windows accept loops, which differ only in how they obtain
/// the stream. A clean client disconnect (`is_closed_socket_error`) is not worth
/// logging; anything else is surfaced as a warning.
fn spawn_client_handler<F>(handler: F)
where
    F: FnOnce() -> Result<()> + Send + 'static,
{
    if let Err(error) = thread::Builder::new()
        .name("triage-ipc-client".to_string())
        .spawn(move || {
            if let Err(error) = handler()
                && !is_closed_socket_error(&error)
            {
                tracing::warn!(error = ?error, "IPC client handler failed");
            }
        })
    {
        tracing::warn!(error = ?error, "failed to spawn IPC client handler");
    }
}

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

impl IpcClient {
    pub fn new(socket_path: impl Into<PathBuf>) -> Self {
        Self {
            socket_path: socket_path.into(),
        }
    }

    pub fn reload_client_assets(&self) -> Result<()> {
        match self.round_trip(WireRequest::ReloadClientAssets)? {
            WireSuccess::Unit => Ok(()),
            other => bail!("unexpected reload response: {other:?}"),
        }
    }

    fn round_trip(&self, request: WireRequest) -> Result<WireSuccess> {
        let mut stream = transport::connect(&self.socket_path)
            .with_context(|| format!("connecting to {}", display_endpoint(&self.socket_path)))?;
        write_json_line(&mut stream, &request).context("writing IPC request")?;
        transport::finish_write(&stream).context("finishing IPC request")?;

        let mut reader = BufReader::new(stream);
        let response: WireResponse = read_json_line(&mut reader)?.context("reading response")?;
        response.into_result()
    }
}

#[cfg(unix)]
pub fn default_socket_path() -> PathBuf {
    if let Some(runtime_dir) = std::env::var_os("XDG_RUNTIME_DIR") {
        return PathBuf::from(runtime_dir).join("triage/triage.sock");
    }

    std::env::temp_dir()
        .join(format!("triage-{}", fallback_user_component()))
        .join("triage.sock")
}

/// On Windows the "socket path" is the bare name of a named pipe; the transport
/// layer expands it to `\\.\pipe\triage-<user>`. Per-user in the name keeps
/// concurrent users on a shared machine from colliding.
#[cfg(windows)]
pub fn default_socket_path() -> PathBuf {
    PathBuf::from(format!("triage-{}", fallback_user_component()))
}

impl SessionApi for IpcClient {
    fn list_sessions(&self) -> Result<Vec<SessionId>> {
        match self.round_trip(WireRequest::ListSessions)? {
            WireSuccess::SessionIds(session_ids) => Ok(session_ids),
            other => bail!("unexpected list_sessions response: {other:?}"),
        }
    }

    fn start_session(&self, request: StartSessionRequest) -> Result<SessionId> {
        match self.round_trip(WireRequest::StartSession(request))? {
            WireSuccess::SessionId(session_id) => Ok(session_id),
            other => bail!("unexpected start_session response: {other:?}"),
        }
    }

    fn attach_session(&self, request: AttachSessionRequest) -> Result<AttachSessionResponse> {
        match self.round_trip(WireRequest::AttachSession(request))? {
            WireSuccess::AttachSession(response) => Ok(response),
            other => bail!("unexpected attach_session response: {other:?}"),
        }
    }

    fn subscribe_session_events(&self, session_id: SessionId) -> Result<SessionEventReceiver> {
        self.subscribe_session_events_from(SubscribeSessionEventsRequest {
            session_id,
            after_event_seq: None,
        })
    }

    fn subscribe_session_events_from(
        &self,
        request: SubscribeSessionEventsRequest,
    ) -> Result<SessionEventReceiver> {
        let mut stream = transport::connect(&self.socket_path)
            .with_context(|| format!("connecting to {}", display_endpoint(&self.socket_path)))?;
        write_json_line(
            &mut stream,
            &WireRequest::SubscribeSessionEvents {
                session_id: request.session_id,
                after_event_seq: request.after_event_seq,
            },
        )
        .context("writing IPC subscribe request")?;
        transport::finish_write(&stream).context("finishing IPC subscribe request")?;

        // The client only reads from here on, so a single handle suffices.
        let mut reader = BufReader::new(stream);
        let response: WireResponse =
            read_json_line(&mut reader)?.context("reading subscribe response")?;
        match response.into_result()? {
            WireSuccess::Subscribed => {}
            other => bail!("unexpected subscribe response: {other:?}"),
        }

        let (tx, rx) = mpsc::channel();
        thread::Builder::new()
            .name("triage-ipc-events".to_string())
            .spawn(move || {
                for line in reader.lines() {
                    let Ok(line) = line else {
                        break;
                    };
                    let Ok(response) = serde_json::from_str::<WireResponse>(&line) else {
                        break;
                    };
                    match response.into_result() {
                        Ok(WireSuccess::SessionEvent(envelope)) => {
                            if tx.send(envelope).is_err() {
                                break;
                            }
                        }
                        Ok(WireSuccess::Heartbeat) => {}
                        _ => break,
                    }
                }
            })
            .context("spawning Unix socket event reader")?;

        Ok(rx)
    }

    fn acquire_input_lease(&self, request: InputLeaseRequest) -> Result<LeaseChange> {
        match self.round_trip(WireRequest::AcquireInputLease(request))? {
            WireSuccess::LeaseChange(change) => Ok(change),
            other => bail!("unexpected acquire_input_lease response: {other:?}"),
        }
    }

    fn release_input_lease(
        &self,
        session_id: SessionId,
        client_id: ClientId,
    ) -> Result<LeaseChange> {
        match self.round_trip(WireRequest::ReleaseInputLease {
            session_id,
            client_id,
        })? {
            WireSuccess::LeaseChange(change) => Ok(change),
            other => bail!("unexpected release_input_lease response: {other:?}"),
        }
    }

    fn write_input(&self, request: WriteInputRequest) -> Result<()> {
        match self.round_trip(WireRequest::WriteInput(request))? {
            WireSuccess::Unit => Ok(()),
            other => bail!("unexpected write_input response: {other:?}"),
        }
    }

    fn resize_session(&self, request: ResizeSessionRequest) -> Result<SessionSnapshot> {
        match self.round_trip(WireRequest::ResizeSession(request))? {
            WireSuccess::SessionSnapshot(snapshot) => Ok(snapshot),
            other => bail!("unexpected resize_session response: {other:?}"),
        }
    }

    fn restore_session(&self, request: RestoreSessionRequest) -> Result<SessionSnapshot> {
        match self.round_trip(WireRequest::RestoreSession(request))? {
            WireSuccess::SessionSnapshot(snapshot) => Ok(snapshot),
            other => bail!("unexpected restore_session response: {other:?}"),
        }
    }

    fn snapshot_session(&self, session_id: SessionId) -> Result<SessionSnapshot> {
        match self.round_trip(WireRequest::SnapshotSession { session_id })? {
            WireSuccess::SessionSnapshot(snapshot) => Ok(snapshot),
            other => bail!("unexpected snapshot_session response: {other:?}"),
        }
    }

    fn styled_rows(&self, request: StyledRowsRequest) -> Result<StyledRowsResponse> {
        match self.round_trip(WireRequest::StyledRows(request))? {
            WireSuccess::StyledRows(response) => Ok(response),
            other => bail!("unexpected styled_rows response: {other:?}"),
        }
    }

    fn shutdown_session(&self, session_id: SessionId) -> Result<CompletedSession> {
        match self.round_trip(WireRequest::ShutdownSession { session_id })? {
            WireSuccess::CompletedSession(completed) => Ok(completed),
            other => bail!("unexpected shutdown_session response: {other:?}"),
        }
    }

    /// Ask the daemon for its update status (Phase 4, the TUI banner). This is a
    /// best-effort, read-only query: any IPC failure (daemon mid-restart,
    /// unexpected reply) falls back to "this build, nothing newer" so the banner
    /// simply stays hidden rather than surfacing an error.
    fn server_update_info(&self) -> ServerUpdateInfo {
        match self.round_trip(WireRequest::ServerUpdateInfo) {
            Ok(WireSuccess::ServerUpdateInfo(info)) => info,
            _ => ServerUpdateInfo {
                server_version: env!("CARGO_PKG_VERSION").to_string(),
                update_available: false,
                latest_version: None,
            },
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
enum WireRequest {
    ListSessions,
    StartSession(StartSessionRequest),
    AttachSession(AttachSessionRequest),
    SubscribeSessionEvents {
        session_id: SessionId,
        after_event_seq: Option<u64>,
    },
    AcquireInputLease(InputLeaseRequest),
    ReleaseInputLease {
        session_id: SessionId,
        client_id: ClientId,
    },
    WriteInput(WriteInputRequest),
    ResizeSession(ResizeSessionRequest),
    RestoreSession(RestoreSessionRequest),
    SnapshotSession {
        session_id: SessionId,
    },
    StyledRows(StyledRowsRequest),
    ShutdownSession {
        session_id: SessionId,
    },
    Handover,
    ReloadClientAssets,
    ServerUpdateInfo,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
enum WireResponse {
    Ok(Box<WireSuccess>),
    Err { message: String },
}

impl WireResponse {
    fn from_result(result: Result<WireSuccess>) -> Self {
        match result {
            Ok(success) => Self::Ok(Box::new(success)),
            Err(error) => Self::Err {
                message: error.to_string(),
            },
        }
    }

    fn into_result(self) -> Result<WireSuccess> {
        match self {
            Self::Ok(success) => Ok(*success),
            Self::Err { message } => Err(anyhow!(message)),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
enum WireSuccess {
    Unit,
    SessionIds(Vec<SessionId>),
    SessionId(SessionId),
    AttachSession(AttachSessionResponse),
    LeaseChange(LeaseChange),
    SessionSnapshot(SessionSnapshot),
    StyledRows(StyledRowsResponse),
    CompletedSession(CompletedSession),
    Subscribed,
    SessionEvent(SessionEventEnvelope),
    Heartbeat,
    HandoverState(crate::handover::HandoverState),
    ServerUpdateInfo(ServerUpdateInfo),
}

fn fallback_user_component() -> String {
    user_identifier()
        .filter(|value| !value.trim().is_empty())
        .map(sanitize_path_component)
        .unwrap_or_else(|| format!("pid-{}", std::process::id()))
}

#[cfg(unix)]
fn user_identifier() -> Option<String> {
    std::env::var("UID")
        .or_else(|_| {
            current_user_uid()
                .map(|uid| uid.to_string())
                .ok_or(std::env::VarError::NotPresent)
        })
        .or_else(|_| std::env::var("USER"))
        .ok()
}

#[cfg(unix)]
fn current_user_uid() -> Option<u32> {
    let home = std::env::var_os("HOME")?;
    fs::metadata(home).map(|metadata| metadata.uid()).ok()
}

#[cfg(windows)]
fn user_identifier() -> Option<String> {
    std::env::var("USERNAME").ok()
}

fn sanitize_path_component(value: String) -> String {
    value
        .chars()
        .map(|character| {
            if character.is_ascii_alphanumeric() || matches!(character, '-' | '_') {
                character
            } else {
                '_'
            }
        })
        .collect()
}

/// `(device, inode)` of the socket this daemon bound.
///
/// Recorded so teardown can tell its own socket from one a successor has since
/// bound at the same path. Without that check the choice is between never
/// cleaning up (leaving a stale file every swap, which widens the
/// unlink-then-bind race between two concurrent starters) and unlinking blindly
/// (which can delete a live successor's socket, since the commit byte releases it
/// before we exit). Comparing identity gets both: we clean up after ourselves and
/// never touch anyone else's.
#[cfg(unix)]
static OWNED_SOCKET_ID: std::sync::Mutex<Option<(u64, u64)>> = std::sync::Mutex::new(None);

/// Remove `socket_path`, but only while it still refers to the socket this
/// process bound. A no-op if a successor has already rebound the path.
#[cfg(unix)]
fn unlink_own_socket(socket_path: &Path) {
    let owned = *OWNED_SOCKET_ID
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    let Some(owned) = owned else {
        return;
    };
    if let Ok(meta) = fs::metadata(socket_path)
        && (meta.dev(), meta.ino()) == owned
    {
        let _ = fs::remove_file(socket_path);
    }
}

/// Bind the owner socket, waiting out a predecessor that still holds it for up to
/// `grace` (see [`IpcConfig::bind_grace`]). With a zero grace this fails on the
/// first attempt, exactly as an ordinary start should.
#[cfg(unix)]
fn bind_owner_socket(socket_path: &Path, grace: std::time::Duration) -> Result<UnixListener> {
    let deadline = std::time::Instant::now() + grace;
    let mut backoff = std::time::Duration::from_millis(50);
    loop {
        match try_bind_owner_socket(socket_path) {
            Ok(Some(listener)) => return Ok(listener),
            Ok(None) => {}
            // Inside the grace, retry *any* failure rather than propagating it.
            // The caller that sets a grace is holding adopted PTY masters, so
            // returning here exits the process and loses every one of them —
            // strictly worse than trying again. These races are real and benign:
            // a predecessor on an older build still unlinks on its way out, so
            // our `remove_file` can lose to it and report NotFound, and a bind can
            // lose to whoever else is starting and report EADDRINUSE. With a zero
            // grace the deadline has already passed and the error propagates
            // immediately, exactly as a fresh start requires.
            Err(error) => {
                if std::time::Instant::now() >= deadline {
                    return Err(error);
                }
                tracing::warn!(
                    socket_path = %socket_path.display(),
                    %error,
                    "bind attempt failed while a predecessor finishes teardown; retrying"
                );
            }
        }
        if std::time::Instant::now() >= deadline {
            bail!("Unix socket {} is already in use", socket_path.display());
        }
        tracing::info!(
            socket_path = %socket_path.display(),
            "socket still held by the outgoing daemon; retrying bind in {}ms",
            backoff.as_millis()
        );
        std::thread::sleep(backoff);
        backoff = (backoff * 2).min(std::time::Duration::from_millis(500));
    }
}

/// One bind attempt. `Ok(None)` means a live daemon currently owns the socket —
/// the single condition that is worth retrying; every other failure is returned
/// as an error.
#[cfg(unix)]
fn try_bind_owner_socket(socket_path: &Path) -> Result<Option<UnixListener>> {
    if let Some(parent) = socket_path.parent() {
        fs::create_dir_all(parent)
            .with_context(|| format!("creating socket directory {}", parent.display()))?;
        fs::set_permissions(parent, fs::Permissions::from_mode(0o700))
            .with_context(|| format!("securing socket directory {}", parent.display()))?;
    }

    if socket_path.exists() {
        match UnixStream::connect(socket_path) {
            // Someone is answering: retryable, since during a handover that
            // someone is a predecessor on its way out.
            Ok(_) => return Ok(None),
            Err(error)
                if matches!(
                    error.kind(),
                    ErrorKind::ConnectionRefused | ErrorKind::NotFound
                ) =>
            {
                fs::remove_file(socket_path)
                    .with_context(|| format!("removing stale socket {}", socket_path.display()))?;
            }
            Err(error) => {
                return Err(error).with_context(|| {
                    format!("checking existing socket {}", socket_path.display())
                });
            }
        }
    }

    let listener = UnixListener::bind(socket_path)
        .with_context(|| format!("binding Unix socket {}", socket_path.display()))?;
    fs::set_permissions(socket_path, fs::Permissions::from_mode(0o600))
        .with_context(|| format!("securing Unix socket {}", socket_path.display()))?;
    // Remember which socket is ours so teardown never unlinks a successor's. A
    // failure here only costs the cleanup, so it must not fail the bind.
    if let Ok(meta) = fs::metadata(socket_path) {
        *OWNED_SOCKET_ID
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner()) = Some((meta.dev(), meta.ino()));
    }
    Ok(Some(listener))
}

#[cfg(unix)]
fn handle_connection(
    manager: Arc<SessionManager>,
    web_cache: Arc<crate::http::WebAssetCache>,
    stream: UnixStream,
) -> Result<()> {
    let mut reader = BufReader::new(stream.try_clone().context("cloning Unix socket stream")?);
    // A client that connects then closes without sending a request line (e.g. a
    // liveness probe, or the Windows "already in use" preflight) yields EOF here;
    // that's a normal disconnect, not an error worth logging.
    let Some(request) = read_json_line::<WireRequest>(&mut reader)? else {
        return Ok(());
    };
    // Handover needs the raw stream for SCM_RIGHTS FD passing, so it branches
    // before the shared dispatch (which only deals with the JSON wire protocol).
    if let WireRequest::Handover = request {
        return handle_handover_server(&manager, reader.into_inner());
    }

    let mut writer = BufWriter::new(stream);
    dispatch_request(&manager, &web_cache, request, &mut writer)
}

// Windows named-pipe connection handler. The wire protocol is identical to Unix;
// the only differences are that there is no FD-passing handover, and the request
// is read then the same stream is reused for writing (the client sends exactly one
// request line before reading, so no second read handle is needed).
#[cfg(windows)]
fn handle_connection(
    manager: Arc<SessionManager>,
    web_cache: Arc<crate::http::WebAssetCache>,
    stream: transport::LocalStream,
) -> Result<()> {
    let mut reader = BufReader::new(stream);
    // A client that connects then closes without sending a request line (e.g. a
    // liveness probe, or the Windows "already in use" preflight) yields EOF here;
    // that's a normal disconnect, not an error worth logging.
    let Some(request) = read_json_line::<WireRequest>(&mut reader)? else {
        return Ok(());
    };
    if let WireRequest::Handover = request {
        bail!("Handover request not supported on Windows");
    }

    let mut writer = BufWriter::new(reader.into_inner());
    dispatch_request(&manager, &web_cache, request, &mut writer)
}

/// Service a single non-handover request: stream a subscription, or run the
/// request and write its one-shot response. Shared by both platform handlers.
fn dispatch_request(
    manager: &SessionManager,
    web_cache: &crate::http::WebAssetCache,
    request: WireRequest,
    writer: &mut impl Write,
) -> Result<()> {
    if let WireRequest::SubscribeSessionEvents {
        session_id,
        after_event_seq,
    } = request
    {
        return handle_subscription(manager, session_id, after_event_seq, writer);
    }

    let response = WireResponse::from_result(handle_request(manager, web_cache, request));
    write_json_line(writer, &response).context("writing response")?;
    writer.flush().context("flushing response")?;
    Ok(())
}

/// Descriptors duplicated for an SCM_RIGHTS send, closed on drop.
///
/// `sendmsg` installs independent descriptors in the receiver, so this process
/// must always close its own copies. Doing that through `Drop` rather than a
/// trailing loop covers the fallible steps in between — duplicating the TCP
/// listener, serializing the response — which would otherwise return with the
/// masters still open. That matters now that an aborted handover leaves this
/// daemon running and able to serve a later attempt: leaks accumulate across
/// retries instead of being reclaimed by process exit.
#[cfg(unix)]
struct StagedFds(Vec<std::os::unix::io::RawFd>);

#[cfg(unix)]
impl Drop for StagedFds {
    fn drop(&mut self) {
        for fd in self.0.drain(..) {
            // Safety: each fd is a `dup` this function owns; the receiver's copies
            // are separate descriptors installed by the kernel.
            unsafe { libc::close(fd) };
        }
    }
}

/// Serve this daemon's side of a process handover: ship session state and the
/// PTY master descriptors to the successor, wait for its adoption byte, then
/// commit to teardown and exit.
///
/// Only one handover runs at a time — the slot is claimed through
/// `SessionManager::begin_handover`, whose guard also blocks `start_session` for
/// the duration. A concurrent request is refused with
/// [`crate::handover::HANDOVER_BUSY_MESSAGE`] rather than served; see the body
/// for why serving two at once would split every session's output.
///
/// Every connection is dispatched on its own thread (`spawn_client_handler`), so
/// parking this one until the successor answers costs nothing else.
///
/// This function does not return on success: it ends in `process::exit(0)` once
/// the sessions are detached.
#[cfg(unix)]
fn handle_handover_server(manager: &SessionManager, stream: UnixStream) -> Result<()> {
    use crate::handover::{
        HANDOVER_BUSY_MESSAGE, HANDOVER_COMMIT_BYTE, HANDOVER_DONE_BYTE,
        get_active_tcp_listener_fd, send_fds,
    };
    use std::io::{Read, Write};

    // Claim before serializing anything. The guard lives on the manager so it
    // also gates session creation: while a handover is in flight, start_session
    // is refused, because a session created after this snapshot would not be in
    // the transferred fds and would be lost when this daemon detaches.
    //
    // Serving two handovers at once would dup and ship the *same* PTY masters to
    // two successors; whichever committed first would drive this daemon through
    // teardown and exit, leaving both holding live masters and splitting each
    // session's (destructive) output between them. Refusing instead is reachable
    // in normal operation — smart-start means any `triaged` launch attempts a
    // handover, including the `launchctl kickstart -k` an operator runs when a
    // swap looks stuck.
    //
    // On refusal, answer with the busy sentinel so the caller retries rather than
    // falling back to a fresh start that would fail to bind the port this daemon
    // still holds. The response is best-effort: a caller that has already gone
    // away just gets a dropped connection, exactly as before.
    let Some(_in_flight) = manager.begin_handover() else {
        let response = WireResponse::Err {
            message: HANDOVER_BUSY_MESSAGE.to_string(),
        };
        if let Ok(bytes) = serde_json::to_vec(&response) {
            let _ = send_fds(&stream, &[], &bytes);
        }
        tracing::info!("Refused a concurrent handover; a swap is already in flight.");
        return Ok(());
    };

    tracing::info!("Received handover request. Beginning process serialization...");

    let (mut state, pty_fds) = manager
        .serialize_active_sessions()
        .context("serializing active sessions for handover")?;

    // Tell the successor this daemon sends the 0x03 commit byte before detaching,
    // so it can read a pre-commit EOF as "aborted, sessions kept" and refuse
    // rather than adopt into a split-brain. An older successor ignores the field.
    state.sends_teardown_commit = true;

    // Take ownership of the PTY dups immediately. Everything between here and the
    // send can fail (the TCP dup, serializing the response), and an aborted
    // handover no longer ends the process — this daemon keeps its sessions and
    // stays available to serve a later attempt — so a descriptor leaked on those
    // paths accumulates across retries instead of vanishing with the process.
    let mut fds_to_send = StagedFds(pty_fds);

    let tcp_fd = get_active_tcp_listener_fd();
    if tcp_fd >= 0 {
        let dup_tcp = unsafe { libc::dup(tcp_fd) };
        if dup_tcp < 0 {
            bail!(
                "failed to dup TCP listener socket: {}",
                std::io::Error::last_os_error()
            );
        }
        // Front of the queue: the successor's `take_inherited_tcp_listener` claims
        // index 0, and the PTY masters must line up with the session list after it.
        fds_to_send.0.insert(0, dup_tcp);
        state.has_tcp_listener = true;
    } else {
        state.has_tcp_listener = false;
    }

    let response = WireResponse::Ok(Box::new(WireSuccess::HandoverState(state)));
    let response_bytes =
        serde_json::to_vec(&response).context("serializing handover response JSON")?;

    let send_res = send_fds(&stream, &fds_to_send.0, &response_bytes);

    // Close our copies now that the kernel has installed the receiver's: keeping
    // them open across the Phase 2/3 wait would hold every master for the whole
    // adoption window.
    drop(fds_to_send);

    send_res.context("sending handover state and FDs via SCM_RIGHTS")?;

    tracing::info!("Handover transfer completed. Waiting for client adoption sync (Phase 2)...");

    stream
        .set_read_timeout(Some(crate::handover::HANDOVER_ADOPTION_TIMEOUT))
        .context("setting read timeout on handover socket")?;
    let mut sync_byte = [0u8; 1];
    if let Err(err) = stream.try_clone()?.read_exact(&mut sync_byte) {
        bail!("Failed to receive sync byte from client: {err}");
    }
    if sync_byte[0] != crate::handover::HANDOVER_ADOPT_BYTE {
        bail!(
            "Invalid sync byte received from client: {:02x}",
            sync_byte[0]
        );
    }

    tracing::info!("Received adoption sync byte (0x01). Initiating Phase 3 (teardown)...");

    let mut out_stream = stream;

    // Announce the commit BEFORE detaching, and make the detach conditional on
    // that byte landing. This is the atomicity invariant the successor relies on:
    // it refuses to adopt on a pre-commit EOF, so we must detach *only if* the
    // 0x03 reached it. If the write fails we keep our sessions and bail — the
    // successor then refuses, and neither side drops them. (Bailing here runs the
    // guard's Drop, so this daemon can serve a later handover.)
    if let Err(error) = out_stream
        .write_all(&[HANDOVER_COMMIT_BYTE])
        .and_then(|()| out_stream.flush())
    {
        bail!(
            "failed to send teardown-commit byte (0x03); keeping sessions rather than \
             detaching, since the successor refuses to adopt without it: {error}"
        );
    }

    // Detach — do NOT kill. The successor daemon has already adopted these
    // sessions via the transferred master fds; sending each actor a shutdown
    // (which calls child.kill()) is what made handovers tear down every session.
    // We process::exit(0) below, which reaps whatever the detach left running;
    // see SessionManager::detach_all_live_sessions for what that is.
    manager.detach_all_live_sessions();

    // Past the detach there is no way back: the sessions are gone from this
    // process and the successor owns their masters. So 0x02 is a courtesy —
    // failing to deliver it must not abort the exit. Returning Err here would
    // leave a drained, session-less daemon still holding the socket and TCP
    // listener, which would then happily serve a later handover and ship an
    // empty session set, making the loss look like a clean swap.
    if let Err(error) = out_stream
        .write_all(&[HANDOVER_DONE_BYTE])
        .and_then(|()| out_stream.flush())
    {
        tracing::warn!(
            %error,
            "failed to send teardown sync byte (0x02); exiting anyway since sessions are already detached"
        );
    }

    tracing::info!("Process handover handshake completed successfully. Exiting daemon.");

    // Clean up only if the socket at this path is still the one we bound. The
    // commit byte released the successor before this point, so it may already have
    // rebound the path; unlinking blindly would delete *its* live socket, leaving
    // it serving somewhere no client can reach (`probe_daemon_socket` would report
    // Absent and the next launch would fight it for the TCP port). Skipping the
    // unlink entirely is not the answer either — a file left behind on every swap
    // widens the window where two concurrent starters both remove it and both
    // bind. Identity-checked removal avoids both.
    unlink_own_socket(&default_socket_path());

    std::process::exit(0);
}

fn handle_subscription(
    manager: &SessionManager,
    session_id: SessionId,
    after_event_seq: Option<u64>,
    writer: &mut impl Write,
) -> Result<()> {
    match manager.subscribe_session_events_from(SubscribeSessionEventsRequest {
        session_id,
        after_event_seq,
    }) {
        Ok(events) => {
            write_json_line(writer, &WireResponse::Ok(Box::new(WireSuccess::Subscribed)))
                .context("writing subscribe response")?;
            writer.flush().context("flushing subscribe response")?;

            loop {
                match events.recv_timeout(SUBSCRIPTION_HEARTBEAT_INTERVAL) {
                    Ok(event) => {
                        write_json_line(
                            writer,
                            &WireResponse::Ok(Box::new(WireSuccess::SessionEvent(event))),
                        )
                        .context("writing session event")?;
                    }
                    Err(mpsc::RecvTimeoutError::Timeout) => {
                        write_json_line(
                            writer,
                            &WireResponse::Ok(Box::new(WireSuccess::Heartbeat)),
                        )
                        .context("writing subscription heartbeat")?;
                    }
                    Err(mpsc::RecvTimeoutError::Disconnected) => break,
                }
                writer.flush().context("flushing subscription response")?;
            }
            Ok(())
        }
        Err(error) => {
            write_json_line(
                writer,
                &WireResponse::Err {
                    message: error.to_string(),
                },
            )
            .context("writing subscribe error")?;
            writer.flush().context("flushing subscribe error")?;
            Ok(())
        }
    }
}

fn handle_request(
    manager: &SessionManager,
    web_cache: &crate::http::WebAssetCache,
    request: WireRequest,
) -> Result<WireSuccess> {
    match request {
        WireRequest::ListSessions => manager.list_sessions().map(WireSuccess::SessionIds),
        WireRequest::StartSession(request) => {
            manager.start_session(request).map(WireSuccess::SessionId)
        }
        WireRequest::AttachSession(request) => manager
            .attach_session(request)
            .map(WireSuccess::AttachSession),
        WireRequest::SubscribeSessionEvents { .. } => {
            bail!("subscription requests require streaming handler")
        }
        WireRequest::AcquireInputLease(request) => manager
            .acquire_input_lease(request)
            .map(WireSuccess::LeaseChange),
        WireRequest::ReleaseInputLease {
            session_id,
            client_id,
        } => manager
            .release_input_lease(session_id, client_id)
            .map(WireSuccess::LeaseChange),
        WireRequest::WriteInput(request) => {
            manager.write_input(request).map(|()| WireSuccess::Unit)
        }
        WireRequest::ResizeSession(request) => manager
            .resize_session(request)
            .map(WireSuccess::SessionSnapshot),
        WireRequest::RestoreSession(request) => manager
            .restore_session(request)
            .map(WireSuccess::SessionSnapshot),
        WireRequest::SnapshotSession { session_id } => manager
            .snapshot_session(session_id)
            .map(WireSuccess::SessionSnapshot),
        WireRequest::StyledRows(request) => {
            manager.styled_rows(request).map(WireSuccess::StyledRows)
        }
        WireRequest::ShutdownSession { session_id } => manager
            .shutdown_session(session_id)
            .map(WireSuccess::CompletedSession),
        WireRequest::Handover => {
            bail!("handover requests require direct socket handler")
        }
        WireRequest::ReloadClientAssets => {
            web_cache.reload();
            Ok(WireSuccess::Unit)
        }
        WireRequest::ServerUpdateInfo => {
            Ok(WireSuccess::ServerUpdateInfo(manager.server_update_info()))
        }
    }
}

fn read_json_line<T: for<'de> Deserialize<'de>>(reader: &mut impl BufRead) -> Result<Option<T>> {
    let mut line = String::new();
    let read = reader.read_line(&mut line).context("reading JSON line")?;
    if read == 0 {
        return Ok(None);
    }
    serde_json::from_str(&line)
        .context("decoding JSON line")
        .map(Some)
}

fn write_json_line<T: Serialize>(writer: &mut impl Write, value: &T) -> Result<()> {
    serde_json::to_writer(&mut *writer, value).context("encoding JSON line")?;
    writer.write_all(b"\n").context("terminating JSON line")
}

fn is_closed_socket_error(error: &anyhow::Error) -> bool {
    let root_cause = error.root_cause();

    if let Some(io_error) = root_cause.downcast_ref::<std::io::Error>() {
        return is_closed_socket_error_kind(io_error.kind());
    }

    // `write_json_line` writes through `serde_json::to_writer`, which wraps the
    // underlying io error in a `serde_json::Error`. The root cause is then not
    // an `io::Error` at all, so the check above misses a client that hung up
    // mid-write and the disconnect is logged as an unexpected warning.
    root_cause
        .downcast_ref::<serde_json::Error>()
        .and_then(serde_json::Error::io_error_kind)
        .is_some_and(is_closed_socket_error_kind)
}

fn is_closed_socket_error_kind(kind: ErrorKind) -> bool {
    matches!(
        kind,
        ErrorKind::BrokenPipe | ErrorKind::ConnectionReset | ErrorKind::UnexpectedEof
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::session::SessionManagerConfig;
    use std::time::{Duration, Instant};
    use triage_core::session::{AttachMode, RestoreSessionRequest, SessionEvent, SessionSize};

    #[cfg(unix)]
    #[test]
    fn a_zero_grace_bind_fails_immediately_on_an_occupied_socket() {
        // The default for an ordinary start: another daemon owning the socket must
        // fail loudly and at once, never wait. This is what keeps a plain launch
        // from silently hanging behind a running daemon.
        let socket_path = unique_socket_path("bind-zero");
        fs::create_dir_all(socket_path.parent().expect("socket parent")).expect("socket dir");
        let _held = UnixListener::bind(&socket_path).expect("bind holder");

        let started = Instant::now();
        let error = bind_owner_socket(&socket_path, Duration::ZERO)
            .expect_err("an occupied socket must not bind");

        assert!(error.to_string().contains("already in use"));
        assert!(
            started.elapsed() < Duration::from_millis(200),
            "zero grace must not wait, took {:?}",
            started.elapsed()
        );
        let _ = fs::remove_dir_all(socket_path.parent().expect("socket parent"));
    }

    #[cfg(unix)]
    #[test]
    fn a_bind_grace_waits_for_the_predecessor_to_release_the_socket() {
        // The handover successor's case: it already owns adopted PTY masters, so
        // rather than dying while the predecessor finishes teardown it waits, then
        // reclaims the now-stale socket.
        let socket_path = unique_socket_path("bind-grace");
        fs::create_dir_all(socket_path.parent().expect("socket parent")).expect("socket dir");
        let held = UnixListener::bind(&socket_path).expect("bind holder");

        let releaser = thread::spawn(move || {
            thread::sleep(Duration::from_millis(150));
            drop(held);
        });

        let started = Instant::now();
        let listener = bind_owner_socket(&socket_path, Duration::from_secs(5))
            .expect("bind should succeed once the predecessor releases the socket");

        // It must actually have waited rather than sneaking in immediately —
        // otherwise the test would pass even if the grace did nothing.
        assert!(
            started.elapsed() >= Duration::from_millis(100),
            "expected the bind to wait for the holder, took {:?}",
            started.elapsed()
        );

        releaser.join().expect("releaser thread");
        drop(listener);
        let _ = fs::remove_dir_all(socket_path.parent().expect("socket parent"));
    }

    #[test]
    fn client_reports_server_errors() {
        let socket_path = unique_socket_path("ms");
        let log_dir = unique_dir("ms-logs");
        let manager = Arc::new(SessionManager::new(SessionManagerConfig::new(
            log_dir.clone(),
        )));
        let cache = Arc::new(crate::http::WebAssetCache::new(None));
        let server = IpcServer::new(
            Arc::clone(&manager),
            cache,
            IpcConfig::new(socket_path.clone()),
        );
        spawn_server(server);

        let client = IpcClient::new(socket_path.clone());
        let missing = SessionId::new("missing").expect("session id");
        let error = client
            .snapshot_session(missing)
            .expect_err("missing snapshot should fail");

        assert!(error.to_string().contains("not found"));
        let _ = fs::remove_file(socket_path);
        let _ = fs::remove_dir_all(log_dir);
    }

    #[test]
    fn client_fetches_server_update_info_over_socket() {
        let socket_path = unique_socket_path("upd");
        let log_dir = unique_dir("upd-logs");
        let manager = Arc::new(SessionManager::new(SessionManagerConfig::new(
            log_dir.clone(),
        )));
        // Seed a "newer release available" status so the value we read back
        // proves it crossed the wire (the client's fallback is never-available).
        manager.set_update_status_for_test(crate::update::UpdateStatus {
            current: "0.1.6".to_string(),
            latest: Some("0.1.7".to_string()),
            update_available: true,
        });
        let cache = Arc::new(crate::http::WebAssetCache::new(None));
        let server = IpcServer::new(
            Arc::clone(&manager),
            cache,
            IpcConfig::new(socket_path.clone()),
        );
        spawn_server(server);

        let client = IpcClient::new(socket_path.clone());
        let info = client.server_update_info();

        assert!(info.update_available);
        assert_eq!(info.server_version, "0.1.6");
        assert_eq!(info.latest_version.as_deref(), Some("0.1.7"));
        let _ = fs::remove_file(socket_path);
        let _ = fs::remove_dir_all(log_dir);
    }

    #[test]
    fn closed_socket_errors_are_expected_client_disconnects() {
        let error = Err::<(), _>(std::io::Error::from(ErrorKind::BrokenPipe))
            .context("flushing subscription response")
            .expect_err("broken pipe should stay an error");

        assert!(is_closed_socket_error(&error));
    }

    /// Exercises the real write path rather than a hand-built error: a client
    /// that hangs up mid-write fails inside `serde_json::to_writer`, so the
    /// root cause is a `serde_json::Error` and not an `io::Error`.
    #[test]
    fn json_closed_socket_errors_are_expected_client_disconnects() {
        struct BrokenPipeWriter;

        impl Write for BrokenPipeWriter {
            fn write(&mut self, _buffer: &[u8]) -> std::io::Result<usize> {
                Err(std::io::Error::from(ErrorKind::BrokenPipe))
            }

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

        let error = write_json_line(&mut BrokenPipeWriter, &"payload")
            .expect_err("broken pipe should stay an error");

        assert!(is_closed_socket_error(&error));
    }

    #[test]
    fn closed_socket_detection_only_matches_root_cause() {
        let error = anyhow!(
            "flushing subscription response: {}",
            std::io::Error::from(ErrorKind::BrokenPipe)
        );

        assert!(!is_closed_socket_error(&error));
    }

    #[test]
    #[cfg_attr(
        windows,
        ignore = "portable-pty ConPTY behavior needs a dedicated Windows lifecycle test"
    )]
    fn client_drives_session_over_unix_socket() {
        let socket_path = unique_socket_path("lc");
        let log_dir = unique_dir("lc-logs");
        let manager = Arc::new(SessionManager::new(SessionManagerConfig::new(
            log_dir.clone(),
        )));
        let cache = Arc::new(crate::http::WebAssetCache::new(None));
        let server = IpcServer::new(
            Arc::clone(&manager),
            cache,
            IpcConfig::new(socket_path.clone()),
        );
        spawn_server(server);

        let client = IpcClient::new(socket_path.clone());
        let client_id = ClientId::new("test-client").expect("client id");
        let mut request = StartSessionRequest::new("/bin/sh");
        request.args = vec!["-lc".to_string(), "cat".to_string()];
        request.size = SessionSize::default();
        let session_id = client.start_session(request).expect("start session");
        assert!(
            client
                .list_sessions()
                .expect("list sessions")
                .contains(&session_id)
        );
        let events = client
            .subscribe_session_events(session_id.clone())
            .expect("subscribe events");
        client
            .attach_session(AttachSessionRequest {
                session_id: session_id.clone(),
                client_id: client_id.clone(),
                mode: AttachMode::InteractiveController,
            })
            .expect("attach session");
        client
            .write_input(WriteInputRequest {
                session_id: session_id.clone(),
                client_id,
                bytes: b"socket-ready\n".to_vec(),
            })
            .expect("write input");

        let deadline = Instant::now() + Duration::from_secs(5);
        loop {
            let snapshot = client
                .snapshot_session(session_id.clone())
                .expect("snapshot session");
            if snapshot
                .visible_rows
                .iter()
                .any(|row| row.contains("socket-ready"))
            {
                break;
            }
            assert!(
                Instant::now() < deadline,
                "timed out waiting for socket output: {:?}",
                snapshot.visible_rows
            );
            std::thread::sleep(Duration::from_millis(20));
        }

        wait_for_output_event(&events);
        client
            .shutdown_session(session_id)
            .expect("shutdown session");
        let _ = fs::remove_file(socket_path);
        let _ = fs::remove_dir_all(log_dir);
    }

    #[test]
    #[cfg_attr(
        windows,
        ignore = "portable-pty ConPTY behavior needs a dedicated Windows lifecycle test"
    )]
    fn client_restores_historical_shell_over_unix_socket() {
        let socket_path = unique_socket_path("rs");
        let log_dir = unique_dir("rs-logs");
        fs::create_dir_all(&log_dir).expect("create log dir");
        let session_id = SessionId::new("session-7").expect("session id");
        let log_path = log_dir.join("session-7.log");
        fs::write(&log_path, b"socket-history\r\n").expect("write session log");
        let manifest = serde_json::json!({
            "version": 1,
            "sessions": [{
                "id": session_id,
                "command": long_running_shell_command(),
                "args": [],
                "cwd": null,
                "size": {
                    "rows": 6,
                    "cols": 40,
                    "pixel_width": 800,
                    "pixel_height": 240,
                    "dpi": 96
                },
                "log_path": log_path,
                "exited": false
            }]
        });
        fs::write(
            log_dir.join("sessions.json"),
            serde_json::to_vec(&manifest).expect("encode manifest"),
        )
        .expect("write manifest");
        let manager = Arc::new(SessionManager::new(SessionManagerConfig::new(
            log_dir.clone(),
        )));
        let cache = Arc::new(crate::http::WebAssetCache::new(None));
        let server = IpcServer::new(
            Arc::clone(&manager),
            cache,
            IpcConfig::new(socket_path.clone()),
        );
        spawn_server(server);
        let client = IpcClient::new(socket_path.clone());

        let snapshot = client
            .restore_session(RestoreSessionRequest {
                session_id: SessionId::new("session-7").expect("session id"),
                size: SessionSize {
                    rows: 6,
                    cols: 40,
                    pixel_width: 800,
                    pixel_height: 240,
                    dpi: 96,
                },
            })
            .expect("restore session over socket");

        assert!(!snapshot.exited);
        assert!(
            snapshot
                .visible_rows
                .iter()
                .any(|row| row.contains("socket-history")),
            "restored socket snapshot lost historical rows: {:?}",
            snapshot.visible_rows
        );
        manager
            .shutdown_session(SessionId::new("session-7").expect("session id"))
            .expect("shutdown restored socket session");
        let _ = fs::remove_file(socket_path);
        let _ = fs::remove_dir_all(log_dir);
    }

    fn spawn_server(server: IpcServer) {
        let socket_path = server.config.socket_path.clone();
        let (tx, rx) = mpsc::channel();
        thread::Builder::new()
            .name("triage-ipc-test-server".to_string())
            .spawn(move || {
                let result = server.serve();
                let _ = tx.send(result.map_err(|error| format!("{error:#}")));
            })
            .expect("spawn server");

        let deadline = Instant::now() + Duration::from_secs(1);
        while server_not_ready(&socket_path) {
            if let Ok(result) = rx.try_recv() {
                result.expect("test server failed");
            }
            assert!(
                Instant::now() < deadline,
                "timed out waiting for test server endpoint"
            );
            std::thread::sleep(Duration::from_millis(10));
        }
    }

    // Readiness probe for the test server. On Unix the listener is ready once the
    // socket file appears; on Windows the endpoint is a named pipe (no filesystem
    // entry), so probe by attempting to connect.
    #[cfg(unix)]
    fn server_not_ready(socket_path: &Path) -> bool {
        !socket_path.exists()
    }

    #[cfg(windows)]
    fn server_not_ready(socket_path: &Path) -> bool {
        transport::connect(socket_path).is_err()
    }

    fn unique_socket_path(name: &str) -> PathBuf {
        unique_dir(name).join("triage.sock")
    }

    fn unique_dir(name: &str) -> PathBuf {
        std::env::temp_dir().join(format!(
            "triage-ipc-{name}-{}-{:?}",
            std::process::id(),
            std::thread::current().id()
        ))
    }

    #[cfg(windows)]
    #[test]
    fn windows_pipe_token_caps_overlong_names() {
        let long = format!(r"\\.\pipe\{}", "a".repeat(400));
        let token = transport::windows_pipe_token(Path::new(&long)).expect("token");
        assert!(token.encode_utf16().count() <= transport::MAX_PIPE_TOKEN_LEN);
        // Stable across calls...
        let again = transport::windows_pipe_token(Path::new(&long)).expect("token");
        assert_eq!(token, again);
        // ...and distinct inputs yield distinct tokens.
        let other = format!(r"\\.\pipe\{}", "b".repeat(400));
        let other_token = transport::windows_pipe_token(Path::new(&other)).expect("token");
        assert_ne!(token, other_token);

        // Non-BMP chars are one `char` but two UTF-16 units, so a char-based cap
        // would undercount and overflow. The bound must hold in UTF-16 units.
        let astral = format!(r"\\.\pipe\{}", "🦀".repeat(400));
        let astral_token = transport::windows_pipe_token(Path::new(&astral)).expect("token");
        assert!(astral_token.encode_utf16().count() <= transport::MAX_PIPE_TOKEN_LEN);
    }

    // The bounded Windows connect (`ConnectWaitMode::Timeout`) must fail *fast*
    // when no daemon is listening — the pipe doesn't exist, so the connect should
    // error immediately rather than wait out the multi-second busy-pipe timeout.
    #[cfg(windows)]
    #[test]
    fn windows_connect_to_missing_daemon_fails_fast() {
        let missing = unique_socket_path("no-daemon");
        let started = Instant::now();
        let result = transport::connect(&missing);
        let elapsed = started.elapsed();
        assert!(
            result.is_err(),
            "connecting to a nonexistent pipe must error"
        );
        assert!(
            elapsed < Duration::from_secs(2),
            "missing-daemon connect should fail fast, took {elapsed:?}"
        );
    }

    #[cfg(windows)]
    fn long_running_shell_command() -> &'static str {
        "cmd.exe"
    }

    #[cfg(not(windows))]
    fn long_running_shell_command() -> &'static str {
        "/bin/sh"
    }

    fn wait_for_output_event(events: &SessionEventReceiver) {
        let deadline = Instant::now() + Duration::from_secs(5);
        loop {
            let remaining = deadline.saturating_duration_since(Instant::now());
            assert!(!remaining.is_zero(), "timed out waiting for output event");
            match events.recv_timeout(remaining.min(Duration::from_millis(100))) {
                Ok(envelope) if matches!(envelope.event, SessionEvent::Output { .. }) => return,
                Ok(_) => {}
                Err(mpsc::RecvTimeoutError::Timeout) => {}
                Err(mpsc::RecvTimeoutError::Disconnected) => {
                    panic!("event stream closed while waiting for output event");
                }
            }
        }
    }
}