acton-reactive 8.2.0

Acton Reactive is the main crate of the Acton framework, designed for building reactive, event-driven, and asynchronous systems. It provides intuitive abstractions to make working with distributed actors seamless and efficient.
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
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
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
/*
 * Copyright (c) 2024. Govcraft
 *
 * Licensed under either of
 *   * Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *   * MIT license: http://opensource.org/licenses/MIT
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the applicable License for the specific language governing permissions and
 * limitations under that License.
 */

//! Channel-based IPC client for connecting to an acton-reactive server.
//!
//! This module provides [`IpcClient`], a high-level client abstraction that mirrors
//! the server-side channel-based writer pattern used in the IPC listener. Instead of
//! sharing a socket writer behind `Arc<Mutex<_>>`, the client uses an `mpsc` channel
//! to send write commands to a dedicated writer task that exclusively owns the socket
//! write half.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────┐        mpsc channel         ┌──────────────┐
//! │  Caller(s)  │ ──── ClientWriteCommand ───> │  Writer Task │ ── OwnedWriteHalf
//! └─────────────┘                              └──────────────┘
//!
//!                                              ┌──────────────┐
//!                        pending_requests      │  Reader Task │ ── OwnedReadHalf
//!  oneshot::Receiver <── DashMap<corr_id> ──── │              │
//!                        active_streams        │              │
//!  mpsc::Receiver    <── DashMap<corr_id> ──── │              │
//!                                              │  push_tx ────│──> mpsc::Receiver
//!                                              └──────────────┘
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use acton_reactive::ipc::client::{IpcClient, IpcClientConfig};
//! use acton_reactive::ipc::IpcEnvelope;
//!
//! // Connect to the server
//! let client = IpcClient::connect("/tmp/my_app/ipc.sock").await?;
//!
//! // Fire-and-forget send
//! let envelope = IpcEnvelope::new("my_actor", "MyMessage", payload);
//! client.send(envelope).await?;
//!
//! // Request-response
//! let envelope = IpcEnvelope::new_request("my_actor", "MyQuery", payload);
//! let response = client.request(envelope).await?;
//!
//! // Request-stream (multiple frames per request)
//! let envelope = IpcEnvelope::new_stream_request("my_actor", "MyStreamQuery", payload);
//! let mut stream_rx = client.request_stream(envelope).await?;
//! while let Some(frame) = stream_rx.recv().await {
//!     println!("Got frame #{}: {:?}", frame.sequence, frame.payload);
//!     if frame.is_final {
//!         break;
//!     }
//! }
//!
//! // Subscribe to push notifications
//! let sub_response = client.subscribe(vec!["PriceUpdate".into()]).await?;
//! let push_rx = client.take_push_receiver().unwrap();
//! while let Some(notification) = push_rx.recv().await {
//!     println!("Got: {:?}", notification);
//! }
//! ```

use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use dashmap::DashMap;
use serde::Deserialize;
use tokio::net::UnixStream;
use tokio::sync::{mpsc, oneshot};
use tokio::task::JoinHandle;
use tracing::{debug, error, trace, warn};

use super::protocol::{
    read_frame, write_frame, Format, MAX_FRAME_SIZE, MSG_TYPE_DISCOVER, MSG_TYPE_ERROR,
    MSG_TYPE_HEARTBEAT, MSG_TYPE_PUSH, MSG_TYPE_REQUEST, MSG_TYPE_RESPONSE, MSG_TYPE_STREAM,
    MSG_TYPE_SUBSCRIBE, MSG_TYPE_UNSUBSCRIBE,
};
use super::types::{
    IpcDiscoverRequest, IpcDiscoverResponse, IpcEnvelope, IpcError, IpcPushNotification,
    IpcResponse, IpcStreamFrame, IpcSubscribeRequest, IpcSubscriptionResponse,
    IpcUnsubscribeRequest,
};

// ============================================================================
// Constants
// ============================================================================

/// Default capacity for the writer command channel.
const DEFAULT_WRITER_CHANNEL_CAPACITY: usize = 64;

/// Default capacity for the push notification channel.
const DEFAULT_PUSH_CHANNEL_CAPACITY: usize = 256;

/// Default capacity for each per-stream frame channel.
const DEFAULT_STREAM_CHANNEL_CAPACITY: usize = 64;

/// Default timeout for request-response operations.
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

// ============================================================================
// Types
// ============================================================================

/// Raw response bytes with their format, used for type-agnostic correlation matching.
type RawResponse = Result<(Format, Vec<u8>), IpcError>;

/// Map of correlation IDs to pending response channels.
type PendingRequests = DashMap<String, oneshot::Sender<RawResponse>>;

/// Map of correlation IDs to active stream frame channels.
type ActiveStreams = DashMap<String, mpsc::Sender<IpcStreamFrame>>;

/// Commands sent to the client's dedicated writer task.
///
/// Mirrors the server-side `WriteCommand` pattern from `listener.rs`, but for
/// client-to-server operations.
enum ClientWriteCommand {
    /// Send a fire-and-forget envelope (`MSG_TYPE_REQUEST`).
    /// The server responds with `MSG_TYPE_RESPONSE` which the reader drains.
    Envelope { envelope: IpcEnvelope, format: Format },

    /// Send a request that expects a correlated response.
    Request {
        envelope: IpcEnvelope,
        format: Format,
        reply_tx: oneshot::Sender<RawResponse>,
    },

    /// Send a streaming request (`MSG_TYPE_REQUEST` with `expects_stream`).
    /// The reader routes each `MSG_TYPE_STREAM` frame to the per-correlation-id
    /// channel registered in `active_streams`.
    StreamRequest { envelope: IpcEnvelope, format: Format },

    /// Send a subscribe request.
    Subscribe {
        request: IpcSubscribeRequest,
        format: Format,
        reply_tx: oneshot::Sender<RawResponse>,
    },

    /// Send an unsubscribe request.
    Unsubscribe {
        request: IpcUnsubscribeRequest,
        format: Format,
        reply_tx: oneshot::Sender<RawResponse>,
    },

    /// Send a discovery request.
    Discover {
        request: IpcDiscoverRequest,
        format: Format,
        reply_tx: oneshot::Sender<RawResponse>,
    },

    /// Gracefully shut down the writer task.
    Shutdown,
}

/// Minimal struct for peeking at a response's correlation ID without full deserialization.
///
/// All response types (`IpcResponse`, `IpcSubscriptionResponse`, `IpcDiscoverResponse`)
/// share `correlation_id` as their first field.
#[derive(Deserialize)]
struct CorrelationPeek {
    correlation_id: String,
}

// ============================================================================
// Configuration
// ============================================================================

/// Configuration for an [`IpcClient`] connection.
#[derive(Debug, Clone)]
pub struct IpcClientConfig {
    /// Wire format for serialization (JSON or `MessagePack`).
    pub format: Format,

    /// Capacity of the writer command channel.
    ///
    /// Controls how many write commands can be buffered before `send()` applies
    /// backpressure. Default: 64.
    pub writer_channel_capacity: usize,

    /// Capacity of the push notification channel.
    ///
    /// Controls how many push notifications can be buffered before the reader
    /// task drops them. Default: 256.
    pub push_channel_capacity: usize,

    /// Default timeout for request-response operations.
    ///
    /// Default: 30 seconds.
    pub default_timeout: Duration,

    /// Maximum frame size for incoming messages.
    ///
    /// Default: [`MAX_FRAME_SIZE`] (16 MiB).
    pub max_frame_size: usize,
}

impl Default for IpcClientConfig {
    fn default() -> Self {
        Self {
            format: Format::default(),
            writer_channel_capacity: DEFAULT_WRITER_CHANNEL_CAPACITY,
            push_channel_capacity: DEFAULT_PUSH_CHANNEL_CAPACITY,
            default_timeout: DEFAULT_TIMEOUT,
            max_frame_size: MAX_FRAME_SIZE,
        }
    }
}

// ============================================================================
// IpcClient
// ============================================================================

/// A channel-based IPC client for connecting to an acton-reactive server.
///
/// The client splits the Unix socket into a reader half (owned by a reader task)
/// and a writer half (owned by a writer task). All write operations go through
/// an `mpsc` channel, eliminating mutex contention and enabling non-blocking
/// publishes.
///
/// # Connection Lifecycle
///
/// 1. [`connect`](Self::connect) establishes the socket and spawns reader/writer tasks
/// 2. Use [`send`](Self::send) for fire-and-forget messages
/// 3. Use [`request`](Self::request) for request-response patterns
/// 4. Use [`request_stream`](Self::request_stream) for request-stream patterns
/// 5. Use [`subscribe`](Self::subscribe) + [`take_push_receiver`](Self::take_push_receiver)
///    for push notifications
/// 6. [`disconnect`](Self::disconnect) or drop to clean up
pub struct IpcClient {
    /// Sender for write commands to the writer task.
    writer_tx: mpsc::Sender<ClientWriteCommand>,

    /// Receiver for push notifications from subscriptions.
    ///
    /// Behind `std::sync::Mutex` for one-time extraction via `take_push_receiver()`.
    /// This is NOT contended during normal operation — only accessed once.
    push_rx: std::sync::Mutex<Option<mpsc::Receiver<IpcPushNotification>>>,

    /// Map of correlation IDs to active stream frame channels.
    ///
    /// Shared with the reader task, which routes incoming `MSG_TYPE_STREAM`
    /// frames to the matching channel.
    active_streams: std::sync::Arc<ActiveStreams>,

    /// Wire format for this connection.
    format: Format,

    /// Default timeout for request-response operations.
    default_timeout: Duration,

    /// Handle to the reader task (for cleanup on drop).
    reader_handle: JoinHandle<()>,

    /// Handle to the writer task (for cleanup on drop).
    ///
    /// Wrapped in `Mutex<Option<>>` so `disconnect()` can take ownership
    /// and await the task to drain pending writes before closing.
    writer_handle: std::sync::Mutex<Option<JoinHandle<()>>>,

    /// Whether the client has been shut down.
    shutdown: AtomicBool,
}

impl IpcClient {
    /// Connect to a Unix domain socket at the given path with default configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the socket connection fails.
    pub async fn connect(path: impl AsRef<Path>) -> Result<Self, IpcError> {
        Self::connect_with_config(path, IpcClientConfig::default()).await
    }

    /// Connect to a Unix domain socket with custom configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the socket connection fails.
    pub async fn connect_with_config(
        path: impl AsRef<Path>,
        config: IpcClientConfig,
    ) -> Result<Self, IpcError> {
        let path = path.as_ref();
        debug!(path = %path.display(), "IPC client connecting");

        let stream = UnixStream::connect(path)
            .await
            .map_err(|e| IpcError::IoError(format!("Failed to connect to {}: {e}", path.display())))?;

        let (reader, writer) = stream.into_split();

        // Create the writer command channel
        let (writer_tx, writer_rx) =
            mpsc::channel::<ClientWriteCommand>(config.writer_channel_capacity);

        // Create the push notification channel
        let (push_tx, push_rx) =
            mpsc::channel::<IpcPushNotification>(config.push_channel_capacity);

        // Shared pending requests map for correlation matching
        let pending_requests: std::sync::Arc<PendingRequests> =
            std::sync::Arc::new(DashMap::new());
        let pending_for_writer = std::sync::Arc::clone(&pending_requests);

        // Shared active streams map for stream frame routing
        let active_streams: std::sync::Arc<ActiveStreams> = std::sync::Arc::new(DashMap::new());
        let streams_for_writer = std::sync::Arc::clone(&active_streams);
        let streams_for_reader = std::sync::Arc::clone(&active_streams);

        // Spawn the writer task (exclusively owns the write half)
        let writer_handle = tokio::spawn(async move {
            run_client_writer_task(writer, writer_rx, pending_for_writer, streams_for_writer)
                .await;
        });

        // Spawn the reader task (exclusively owns the read half)
        let max_frame_size = config.max_frame_size;
        let reader_handle = tokio::spawn(async move {
            run_client_reader_task(
                reader,
                pending_requests,
                streams_for_reader,
                push_tx,
                max_frame_size,
            )
            .await;
        });

        debug!(path = %path.display(), "IPC client connected");

        Ok(Self {
            writer_tx,
            push_rx: std::sync::Mutex::new(Some(push_rx)),
            active_streams,
            format: config.format,
            default_timeout: config.default_timeout,
            reader_handle,
            writer_handle: std::sync::Mutex::new(Some(writer_handle)),
            shutdown: AtomicBool::new(false),
        })
    }

    /// Send a fire-and-forget envelope.
    ///
    /// The message is enqueued to the writer channel and returns immediately.
    /// The server's response (if any) will be drained by the reader task.
    ///
    /// # Errors
    ///
    /// Returns an error if the writer channel is closed (client disconnected).
    pub async fn send(&self, envelope: IpcEnvelope) -> Result<(), IpcError> {
        self.writer_tx
            .send(ClientWriteCommand::Envelope {
                envelope,
                format: self.format,
            })
            .await
            .map_err(|_| IpcError::ConnectionClosed)
    }

    /// Send a request and wait for a correlated response.
    ///
    /// Uses the client's default timeout.
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails, times out, or the connection is closed.
    pub async fn request(&self, envelope: IpcEnvelope) -> Result<IpcResponse, IpcError> {
        self.request_with_timeout(envelope, self.default_timeout)
            .await
    }

    /// Send a request and wait for a correlated response with a custom timeout.
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails, times out, or the connection is closed.
    pub async fn request_with_timeout(
        &self,
        envelope: IpcEnvelope,
        timeout_duration: Duration,
    ) -> Result<IpcResponse, IpcError> {
        let (reply_tx, reply_rx) = oneshot::channel();

        self.writer_tx
            .send(ClientWriteCommand::Request {
                envelope,
                format: self.format,
                reply_tx,
            })
            .await
            .map_err(|_| IpcError::ConnectionClosed)?;

        let (format, bytes) = tokio::time::timeout(timeout_duration, reply_rx)
            .await
            .map_err(|_| IpcError::Timeout)?
            .map_err(|_| IpcError::ConnectionClosed)??;

        format.deserialize(&bytes)
    }

    /// Send a streaming request and receive the response frames through a channel.
    ///
    /// The server replies with multiple [`IpcStreamFrame`]s for a single request.
    /// Every frame is delivered through the returned receiver, in order and
    /// without gaps. The channel closes after a frame with `is_final: true` is
    /// delivered — server errors arrive as a final frame with the `error` field
    /// set, whether the actor's stream failed mid-flight or the server rejected
    /// the request before dispatching it (shutdown drain, rate limiting).
    ///
    /// Uses the client's default timeout as the maximum time to wait **between
    /// consecutive frames** (and for the first frame). If the channel closes
    /// before an `is_final` frame arrives, the stream terminated abnormally:
    /// either the inter-frame timeout elapsed, the connection was closed, or
    /// the client was dropped.
    ///
    /// The envelope's `expects_stream` flag is set automatically, so any
    /// envelope constructor works; [`IpcEnvelope::new_stream_request`] is the
    /// idiomatic choice.
    ///
    /// # Backpressure
    ///
    /// Frames are never dropped. When the receiver's buffer fills, the client's
    /// shared connection reader blocks until the consumer catches up, applying
    /// backpressure through the connection exactly like TCP flow control. A
    /// slow stream consumer therefore stalls **everything** the reader
    /// delivers: responses to concurrent [`request`](Self::request) calls and
    /// push notifications queue behind the stalled stream. Drain the receiver
    /// promptly (or from a dedicated task) — in particular, don't await a
    /// `request()` on the same client while leaving a mid-flight stream
    /// undrained, or that request may time out waiting for a response the
    /// reader can't reach. The same applies to other concurrent streams on
    /// this client: their frames also queue behind the stalled stream, so a
    /// healthy stream can hit its inter-frame timeout and terminate without
    /// an error frame while another stream is left undrained.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection is closed before the request is enqueued.
    pub async fn request_stream(
        &self,
        envelope: IpcEnvelope,
    ) -> Result<mpsc::Receiver<IpcStreamFrame>, IpcError> {
        self.request_stream_with_timeout(envelope, self.default_timeout)
            .await
    }

    /// Send a streaming request with a custom inter-frame timeout.
    ///
    /// See [`request_stream`](Self::request_stream) for the stream and
    /// backpressure semantics. `frame_timeout` bounds the wait between
    /// consecutive frames, not the total stream duration — the server bounds
    /// the latter via the envelope's `response_timeout_ms`.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection is closed before the request is enqueued.
    pub async fn request_stream_with_timeout(
        &self,
        mut envelope: IpcEnvelope,
        frame_timeout: Duration,
    ) -> Result<mpsc::Receiver<IpcStreamFrame>, IpcError> {
        // The server only streams when the envelope asks for it.
        envelope.expects_stream = true;
        envelope.expects_reply = false;

        let correlation_id = envelope.correlation_id.clone();
        let (frame_tx, frame_rx) = mpsc::channel(DEFAULT_STREAM_CHANNEL_CAPACITY);
        let (out_tx, out_rx) = mpsc::channel(DEFAULT_STREAM_CHANNEL_CAPACITY);

        // Register the routing entry **before** writing the frame, preventing
        // a race with the reader task (mirrors `write_correlated_frame`).
        self.active_streams.insert(correlation_id.clone(), frame_tx);

        if self
            .writer_tx
            .send(ClientWriteCommand::StreamRequest {
                envelope,
                format: self.format,
            })
            .await
            .is_err()
        {
            self.active_streams.remove(&correlation_id);
            return Err(IpcError::ConnectionClosed);
        }

        // The forwarder applies the inter-frame timeout and completes the
        // stream on the final frame or on abnormal termination.
        tokio::spawn(run_stream_forwarder(
            correlation_id,
            frame_rx,
            out_tx,
            frame_timeout,
            std::sync::Arc::clone(&self.active_streams),
        ));

        Ok(out_rx)
    }

    /// Subscribe to message types.
    ///
    /// Returns the server's subscription response containing the current set
    /// of subscribed types.
    ///
    /// # Errors
    ///
    /// Returns an error if the subscription request fails or times out.
    pub async fn subscribe(
        &self,
        message_types: Vec<String>,
    ) -> Result<IpcSubscriptionResponse, IpcError> {
        let request = IpcSubscribeRequest::new(message_types);
        let (reply_tx, reply_rx) = oneshot::channel();

        self.writer_tx
            .send(ClientWriteCommand::Subscribe {
                request,
                format: self.format,
                reply_tx,
            })
            .await
            .map_err(|_| IpcError::ConnectionClosed)?;

        let (format, bytes) = tokio::time::timeout(self.default_timeout, reply_rx)
            .await
            .map_err(|_| IpcError::Timeout)?
            .map_err(|_| IpcError::ConnectionClosed)??;

        format.deserialize(&bytes)
    }

    /// Unsubscribe from message types.
    ///
    /// Pass an empty vector to unsubscribe from all types.
    ///
    /// # Errors
    ///
    /// Returns an error if the unsubscription request fails or times out.
    pub async fn unsubscribe(
        &self,
        message_types: Vec<String>,
    ) -> Result<IpcSubscriptionResponse, IpcError> {
        let request = if message_types.is_empty() {
            IpcUnsubscribeRequest::unsubscribe_all()
        } else {
            IpcUnsubscribeRequest::new(message_types)
        };
        let (reply_tx, reply_rx) = oneshot::channel();

        self.writer_tx
            .send(ClientWriteCommand::Unsubscribe {
                request,
                format: self.format,
                reply_tx,
            })
            .await
            .map_err(|_| IpcError::ConnectionClosed)?;

        let (format, bytes) = tokio::time::timeout(self.default_timeout, reply_rx)
            .await
            .map_err(|_| IpcError::Timeout)?
            .map_err(|_| IpcError::ConnectionClosed)??;

        format.deserialize(&bytes)
    }

    /// Discover available actors and message types.
    ///
    /// # Errors
    ///
    /// Returns an error if the discovery request fails or times out.
    pub async fn discover(&self) -> Result<IpcDiscoverResponse, IpcError> {
        let request = IpcDiscoverRequest::new();
        let (reply_tx, reply_rx) = oneshot::channel();

        self.writer_tx
            .send(ClientWriteCommand::Discover {
                request,
                format: self.format,
                reply_tx,
            })
            .await
            .map_err(|_| IpcError::ConnectionClosed)?;

        let (format, bytes) = tokio::time::timeout(self.default_timeout, reply_rx)
            .await
            .map_err(|_| IpcError::Timeout)?
            .map_err(|_| IpcError::ConnectionClosed)??;

        format.deserialize(&bytes)
    }

    /// Take the push notification receiver.
    ///
    /// Returns `None` if already taken. The caller owns the receiver and can
    /// poll it for incoming push notifications from subscriptions.
    ///
    /// This is designed for one-time extraction — the `Mutex` is only contended
    /// during this call, never during normal operation.
    pub fn take_push_receiver(&self) -> Option<mpsc::Receiver<IpcPushNotification>> {
        self.push_rx
            .lock()
            .ok()
            .and_then(|mut guard| guard.take())
    }

    /// Get the wire format used by this client.
    #[must_use]
    pub const fn format(&self) -> Format {
        self.format
    }

    /// Check if the client is still connected.
    ///
    /// Returns `false` if the client has been shut down or the writer task has exited.
    #[must_use]
    pub fn is_connected(&self) -> bool {
        if self.shutdown.load(Ordering::Relaxed) {
            return false;
        }
        self.writer_handle
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .as_ref()
            .is_some_and(|h| !h.is_finished())
    }

    /// Gracefully disconnect from the server.
    ///
    /// Sends an unsubscribe-all request, shuts down the writer task, and aborts
    /// the reader task.
    ///
    /// # Errors
    ///
    /// Returns an error if the shutdown sequence encounters issues.
    pub async fn disconnect(&self) -> Result<(), IpcError> {
        if self.shutdown.swap(true, Ordering::SeqCst) {
            return Ok(()); // Already shut down
        }

        // Best-effort unsubscribe-all
        let request = IpcUnsubscribeRequest::unsubscribe_all();
        let payload = self.format.serialize(&request)?;
        let _ = self
            .writer_tx
            .send(ClientWriteCommand::Envelope {
                envelope: IpcEnvelope::new(
                    "__unsubscribe__",
                    "IpcUnsubscribeRequest",
                    serde_json::Value::Null,
                ),
                format: self.format,
            })
            .await;

        // Send raw unsubscribe frame via a special envelope that the writer
        // won't try to correlate. Actually, let's just send the shutdown command
        // to cleanly exit the writer. The server will clean up subscriptions
        // when the connection drops.
        drop(payload); // Not needed — server cleans up on disconnect

        // Signal the writer task to exit and wait for it to drain pending writes
        let _ = self
            .writer_tx
            .send(ClientWriteCommand::Shutdown)
            .await;

        // Await the writer task so all pending frames (including large ones)
        // are flushed to the socket before we close the connection. Take the
        // handle out of the mutex first so the guard is dropped before awaiting.
        let writer_handle = self
            .writer_handle
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .take();
        if let Some(handle) = writer_handle {
            let _ = handle.await;
        }

        // Abort the reader task (it will exit when the socket closes)
        self.reader_handle.abort();

        debug!("IPC client disconnected");
        Ok(())
    }
}

impl Drop for IpcClient {
    fn drop(&mut self) {
        // Abort the reader task unconditionally (it exits when the socket closes)
        self.reader_handle.abort();

        // Only abort the writer if disconnect() didn't already drain it.
        // Take the handle out of the mutex first so the guard is dropped
        // before aborting.
        let writer_handle = self
            .writer_handle
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .take();
        if let Some(handle) = writer_handle {
            handle.abort();
        }

        // Aborted tasks never reach their post-loop cleanup, so terminate any
        // in-flight streams here. Dropping the frame senders makes each
        // forwarder observe end-of-stream and close its consumer's channel
        // promptly instead of waiting out the inter-frame timeout.
        self.active_streams.clear();
    }
}

// ============================================================================
// Writer Task
// ============================================================================

/// Serialize, register a pending reply, and write a frame to the socket.
///
/// Registers the `reply_tx` in `pending_requests` keyed by `correlation_id`
/// **before** writing the frame, preventing races with the reader task.
/// On serialization or write failure, the pending entry is cleaned up and
/// the error is sent through the oneshot channel.
///
/// Returns `None` on serialization failure (caller should `continue`),
/// or `Some(result)` with the write result.
async fn write_correlated_frame<T: serde::Serialize + Sync>(
    writer: &mut tokio::net::unix::OwnedWriteHalf,
    pending_requests: &PendingRequests,
    correlation_id: String,
    reply_tx: oneshot::Sender<RawResponse>,
    msg_type: u8,
    format: Format,
    value: &T,
) -> Option<Result<(), IpcError>> {
    pending_requests.insert(correlation_id.clone(), reply_tx);

    let payload = match format.serialize(value) {
        Ok(p) => p,
        Err(e) => {
            if let Some((_, tx)) = pending_requests.remove(&correlation_id) {
                let _ = tx.send(Err(e.clone()));
            }
            error!(error = %e, "Failed to serialize IPC frame");
            return None;
        }
    };

    let result = write_frame(writer, msg_type, format, &payload).await;
    if let Err(ref e) = result {
        if let Some((_, tx)) = pending_requests.remove(&correlation_id) {
            let _ = tx.send(Err(e.clone()));
        }
    }
    Some(result)
}

/// Dedicated writer task that exclusively owns the socket write half.
///
/// Mirrors the server-side `run_writer_task` in `listener.rs`. Receives
/// `ClientWriteCommand` messages from the mpsc channel and writes frames
/// to the socket sequentially.
///
/// For request-response commands, the pending request is registered in the
/// `DashMap` **before** writing the frame to prevent races with the reader task.
async fn run_client_writer_task(
    mut writer: tokio::net::unix::OwnedWriteHalf,
    mut receiver: mpsc::Receiver<ClientWriteCommand>,
    pending_requests: std::sync::Arc<PendingRequests>,
    active_streams: std::sync::Arc<ActiveStreams>,
) {
    trace!("IPC client writer task started");

    while let Some(cmd) = receiver.recv().await {
        let result = match cmd {
            ClientWriteCommand::Envelope { envelope, format } => {
                let payload = match format.serialize(&envelope) {
                    Ok(p) => p,
                    Err(e) => {
                        error!(error = %e, "Failed to serialize envelope");
                        continue;
                    }
                };
                Some(write_frame(&mut writer, MSG_TYPE_REQUEST, format, &payload).await)
            }

            ClientWriteCommand::Request { envelope, format, reply_tx } => {
                let cid = envelope.correlation_id.clone();
                write_correlated_frame(
                    &mut writer, &pending_requests, cid, reply_tx,
                    MSG_TYPE_REQUEST, format, &envelope,
                ).await
            }

            ClientWriteCommand::StreamRequest { envelope, format } => {
                let payload = match format.serialize(&envelope) {
                    Ok(p) => p,
                    Err(e) => {
                        error!(error = %e, "Failed to serialize stream request");
                        // Drop the routing entry so the stream terminates
                        // instead of waiting for frames that will never come.
                        active_streams.remove(&envelope.correlation_id);
                        continue;
                    }
                };
                let result = write_frame(&mut writer, MSG_TYPE_REQUEST, format, &payload).await;
                if result.is_err() {
                    active_streams.remove(&envelope.correlation_id);
                }
                Some(result)
            }

            ClientWriteCommand::Subscribe { request, format, reply_tx } => {
                let cid = request.correlation_id.clone();
                write_correlated_frame(
                    &mut writer, &pending_requests, cid, reply_tx,
                    MSG_TYPE_SUBSCRIBE, format, &request,
                ).await
            }

            ClientWriteCommand::Unsubscribe { request, format, reply_tx } => {
                let cid = request.correlation_id.clone();
                write_correlated_frame(
                    &mut writer, &pending_requests, cid, reply_tx,
                    MSG_TYPE_UNSUBSCRIBE, format, &request,
                ).await
            }

            ClientWriteCommand::Discover { request, format, reply_tx } => {
                let cid = request.correlation_id.clone();
                write_correlated_frame(
                    &mut writer, &pending_requests, cid, reply_tx,
                    MSG_TYPE_DISCOVER, format, &request,
                ).await
            }

            ClientWriteCommand::Shutdown => {
                trace!("IPC client writer received shutdown command");
                break;
            }
        };

        if let Some(Err(e)) = result {
            error!(error = %e, "IPC client writer error, closing connection");
            break;
        }
    }

    // On exit, clear all pending requests. Dropping the oneshot senders
    // causes `RecvError` on the receiver side, which callers map to `ConnectionClosed`.
    pending_requests.clear();

    // Terminate all active streams. Dropping the frame senders closes the
    // per-stream channels so consumers observe end-of-stream instead of hanging.
    active_streams.clear();

    trace!("IPC client writer task finished");
}

// ============================================================================
// Reader Task
// ============================================================================

/// Dedicated reader task that exclusively owns the socket read half.
///
/// Routes incoming frames by message type:
/// - `MSG_TYPE_RESPONSE` / `MSG_TYPE_ERROR`: Matches by correlation ID to pending
///   requests. Unclaimed responses (fire-and-forget acks) are drained.
/// - `MSG_TYPE_STREAM`: Matches by correlation ID to active streams.
/// - `MSG_TYPE_PUSH`: Forwarded through the push notification channel.
/// - `MSG_TYPE_HEARTBEAT`: Ignored.
/// - Other: Logged as warning.
async fn run_client_reader_task(
    mut reader: tokio::net::unix::OwnedReadHalf,
    pending_requests: std::sync::Arc<PendingRequests>,
    active_streams: std::sync::Arc<ActiveStreams>,
    push_tx: mpsc::Sender<IpcPushNotification>,
    max_frame_size: usize,
) {
    trace!("IPC client reader task started");

    loop {
        match read_frame(&mut reader, max_frame_size).await {
            Ok((msg_type, format, payload)) => match msg_type {
                MSG_TYPE_RESPONSE | MSG_TYPE_ERROR => {
                    handle_response_frame(&pending_requests, &active_streams, format, payload)
                        .await;
                }
                MSG_TYPE_STREAM => {
                    handle_stream_frame(&active_streams, format, &payload).await;
                }
                MSG_TYPE_PUSH => {
                    handle_push_frame(&push_tx, format, &payload);
                }
                MSG_TYPE_HEARTBEAT => {
                    trace!("IPC client received heartbeat");
                }
                _ => {
                    warn!(msg_type, "IPC client received unknown message type");
                }
            },
            Err(IpcError::ConnectionClosed) => {
                debug!("IPC client connection closed by server");
                break;
            }
            Err(e) => {
                error!(error = %e, "IPC client reader error");
                break;
            }
        }
    }

    // Fail any remaining pending requests
    for entry in pending_requests.iter() {
        let correlation_id = entry.key().clone();
        if let Some((_, tx)) = pending_requests.remove(&correlation_id) {
            let _ = tx.send(Err(IpcError::ConnectionClosed));
        }
    }

    // Terminate any streams still in flight so consumers don't hang.
    active_streams.clear();

    trace!("IPC client reader task finished");
}

/// Handle an incoming response or error frame.
///
/// Peeks at the `correlation_id` field via minimal deserialization, then routes
/// to the matching pending request. When the correlation ID instead matches an
/// active stream, the server rejected the stream request before dispatching it
/// (shutdown drain, rate limiting, or an envelope-parse failure) — the stream
/// is terminated by delivering a synthesized final [`IpcStreamFrame`] carrying
/// the response's error (or payload). Unclaimed responses (from fire-and-forget
/// sends) are silently drained.
async fn handle_response_frame(
    pending_requests: &PendingRequests,
    active_streams: &ActiveStreams,
    format: Format,
    payload: Vec<u8>,
) {
    // Peek at the correlation_id without full deserialization
    let correlation_id = match format.deserialize::<CorrelationPeek>(&payload) {
        Ok(peek) => peek.correlation_id,
        Err(e) => {
            warn!(error = %e, "Failed to peek correlation_id from response");
            return;
        }
    };

    // Look up and remove the pending request
    if let Some((_, reply_tx)) = pending_requests.remove(&correlation_id) {
        // Send raw bytes — the caller will deserialize to the expected type
        let _ = reply_tx.send(Ok((format, payload)));
        return;
    }

    // A plain response for an active stream is a pre-dispatch rejection —
    // terminate the stream instead of leaving the consumer to time out.
    // Clone the sender out of the map so no shard lock is held while sending.
    if let Some(frame_tx) = active_streams
        .get(&correlation_id)
        .map(|entry| entry.value().clone())
    {
        debug!(correlation_id, "Server rejected stream request, terminating stream");
        let frame = synthesize_stream_termination(&correlation_id, format, &payload);
        let _ = frame_tx.send(frame).await;
        active_streams.remove(&correlation_id);
        return;
    }

    // Fire-and-forget response or unknown correlation — drain silently
    trace!(correlation_id, "Draining unclaimed response");
}

/// Build the final [`IpcStreamFrame`] that terminates a stream whose request
/// the server rejected with a plain response frame.
///
/// The response's error and payload are carried over so the consumer observes
/// why the stream ended (e.g. `RATE_LIMITED`, `SHUTTING_DOWN`). If the response
/// cannot be deserialized, a generic error frame is synthesized instead.
fn synthesize_stream_termination(
    correlation_id: &str,
    format: Format,
    payload: &[u8],
) -> IpcStreamFrame {
    match format.deserialize::<IpcResponse>(payload) {
        Ok(response) if response.success => IpcStreamFrame {
            correlation_id: correlation_id.to_string(),
            sequence: 0,
            is_final: true,
            error: None,
            error_code: None,
            payload: response.payload,
        },
        Ok(response) => IpcStreamFrame {
            correlation_id: correlation_id.to_string(),
            sequence: 0,
            is_final: true,
            error: Some(response.error.unwrap_or_else(|| {
                "Server rejected the stream request".to_string()
            })),
            error_code: response.error_code,
            payload: response.payload,
        },
        Err(e) => IpcStreamFrame::error(
            correlation_id,
            0,
            format!("Server rejected the stream request with an undecodable response: {e}"),
        ),
    }
}

/// Handle an incoming stream frame.
///
/// Routes the frame to the matching active stream by correlation ID. Frames
/// for unknown (or expired) correlation IDs are warned about and dropped.
/// When the receiving side is gone, the routing entry is removed so late
/// frames don't accumulate.
///
/// The send is **awaited**: when the per-stream channel is full, the reader
/// task blocks here until the consumer drains it, applying backpressure
/// through the connection exactly like TCP flow control. Frames are never
/// silently dropped — a slow stream consumer instead stalls the shared
/// connection reader (see [`IpcClient::request_stream`]).
async fn handle_stream_frame(active_streams: &ActiveStreams, format: Format, payload: &[u8]) {
    let frame = match format.deserialize::<IpcStreamFrame>(payload) {
        Ok(frame) => frame,
        Err(e) => {
            warn!(error = %e, "Failed to deserialize stream frame");
            return;
        }
    };

    let correlation_id = frame.correlation_id.clone();
    let is_final = frame.is_final;

    // Clone the sender out of the map so no shard lock is held while sending
    let Some(frame_tx) = active_streams
        .get(&correlation_id)
        .map(|entry| entry.value().clone())
    else {
        warn!(correlation_id, "Received stream frame for unknown correlation_id, dropping");
        return;
    };

    if frame_tx.send(frame).await.is_err() {
        // The forwarder exited (receiver dropped or timed out) — clean up
        debug!(correlation_id, "Stream frame channel closed, removing stream");
        active_streams.remove(&correlation_id);
        return;
    }

    if is_final {
        // The stream is complete — release the routing entry
        active_streams.remove(&correlation_id);
    }
}

/// Per-stream forwarder that applies the inter-frame timeout.
///
/// Receives frames routed by the reader task and forwards them to the
/// consumer's channel, exiting (and closing the consumer's channel) when:
/// - a frame with `is_final: true` has been delivered (clean completion),
/// - no frame arrives within `frame_timeout` (client-side read timeout),
/// - the consumer drops its receiver (stream cancelled), or
/// - the routing entry is dropped (connection closed or client shut down).
///
/// On exit, the routing entry is removed so late frames for this correlation
/// ID are warned about and dropped rather than buffered forever.
async fn run_stream_forwarder(
    correlation_id: String,
    mut frame_rx: mpsc::Receiver<IpcStreamFrame>,
    out_tx: mpsc::Sender<IpcStreamFrame>,
    frame_timeout: Duration,
    active_streams: std::sync::Arc<ActiveStreams>,
) {
    trace!(correlation_id, "Stream forwarder started");

    loop {
        match tokio::time::timeout(frame_timeout, frame_rx.recv()).await {
            Ok(Some(frame)) => {
                let is_final = frame.is_final;
                if out_tx.send(frame).await.is_err() {
                    // Consumer dropped the receiver mid-stream
                    debug!(correlation_id, "Stream receiver dropped, cancelling stream");
                    break;
                }
                if is_final {
                    trace!(correlation_id, "Stream completed with final frame");
                    break;
                }
            }
            Ok(None) => {
                // All senders dropped: connection closed or client shut down
                debug!(correlation_id, "Stream terminated before final frame");
                break;
            }
            Err(_) => {
                warn!(
                    correlation_id,
                    timeout = ?frame_timeout,
                    "Stream timed out waiting for next frame"
                );
                break;
            }
        }
    }

    active_streams.remove(&correlation_id);
    trace!(correlation_id, "Stream forwarder finished");
}

/// Handle an incoming push notification frame.
fn handle_push_frame(
    push_tx: &mpsc::Sender<IpcPushNotification>,
    format: Format,
    payload: &[u8],
) {
    match format.deserialize::<IpcPushNotification>(payload) {
        Ok(notification) => {
            if let Err(e) = push_tx.try_send(notification) {
                match e {
                    mpsc::error::TrySendError::Full(_) => {
                        warn!("Push notification channel full, dropping notification");
                    }
                    mpsc::error::TrySendError::Closed(_) => {
                        debug!("Push notification channel closed");
                    }
                }
            }
        }
        Err(e) => {
            warn!(error = %e, "Failed to deserialize push notification");
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    /// Test that `IpcClientConfig` has sensible defaults.
    #[test]
    fn config_defaults() {
        let config = IpcClientConfig::default();
        assert_eq!(config.writer_channel_capacity, 64);
        assert_eq!(config.push_channel_capacity, 256);
        assert_eq!(config.default_timeout, Duration::from_secs(30));
        assert_eq!(config.max_frame_size, MAX_FRAME_SIZE);
    }

    /// Test that the client can connect to a Unix socket.
    #[tokio::test]
    async fn connect_to_socket() {
        let dir = tempfile::tempdir().expect("failed to create temp dir");
        let socket_path = dir.path().join("test.sock");

        // Start a listener that accepts one connection
        let listener = tokio::net::UnixListener::bind(&socket_path)
            .expect("failed to bind socket");

        let accept_handle = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("failed to accept");
            stream
        });

        // Connect with the client
        let client = IpcClient::connect(&socket_path).await;
        assert!(client.is_ok());

        let client = client.expect("client should connect");
        assert!(client.is_connected());

        // Clean up
        let _server_stream = accept_handle.await.expect("accept failed");
        drop(client);
    }

    /// Test that connect fails for a non-existent socket.
    #[tokio::test]
    async fn connect_fails_for_missing_socket() {
        let result = IpcClient::connect("/tmp/nonexistent_test_socket_12345.sock").await;
        assert!(result.is_err());
    }

    /// Test fire-and-forget send enqueues without blocking.
    #[tokio::test]
    async fn send_fire_and_forget() {
        let dir = tempfile::tempdir().expect("failed to create temp dir");
        let socket_path = dir.path().join("test.sock");

        let listener = tokio::net::UnixListener::bind(&socket_path)
            .expect("failed to bind socket");

        let accept_handle = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("failed to accept");
            let (mut reader, mut writer) = stream.into_split();

            // Read the frame the client sends
            let result = read_frame(&mut reader, MAX_FRAME_SIZE).await;
            assert!(result.is_ok());
            let (msg_type, _, payload) = result.expect("should read frame");
            assert_eq!(msg_type, MSG_TYPE_REQUEST);

            // Verify it's an IpcEnvelope
            let envelope: IpcEnvelope =
                serde_json::from_slice(&payload).expect("should deserialize");
            assert_eq!(envelope.target, "test_actor");

            // Send a response back (server always responds to requests)
            let response = IpcResponse::success(&envelope.correlation_id, None);
            let resp_payload =
                serde_json::to_vec(&response).expect("should serialize response");
            write_frame(
                &mut writer,
                MSG_TYPE_RESPONSE,
                Format::Json,
                &resp_payload,
            )
            .await
            .expect("should write response");

            // Keep connection alive briefly
            tokio::time::sleep(Duration::from_millis(100)).await;
        });

        let client = IpcClient::connect(&socket_path)
            .await
            .expect("should connect");

        let envelope = IpcEnvelope::new(
            "test_actor",
            "TestMessage",
            serde_json::json!({"key": "value"}),
        );
        let result = client.send(envelope).await;
        assert!(result.is_ok());

        accept_handle.await.expect("server task failed");
    }

    /// Test request-response with correlation matching.
    #[tokio::test]
    async fn request_response_correlation() {
        let dir = tempfile::tempdir().expect("failed to create temp dir");
        let socket_path = dir.path().join("test.sock");

        let listener = tokio::net::UnixListener::bind(&socket_path)
            .expect("failed to bind socket");

        let accept_handle = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("failed to accept");
            let (mut reader, mut writer) = stream.into_split();

            // Read the request
            let (msg_type, _, payload) = read_frame(&mut reader, MAX_FRAME_SIZE)
                .await
                .expect("should read frame");
            assert_eq!(msg_type, MSG_TYPE_REQUEST);

            let envelope: IpcEnvelope =
                serde_json::from_slice(&payload).expect("should deserialize");

            // Send correlated response
            let response = IpcResponse::success(
                &envelope.correlation_id,
                Some(serde_json::json!({"result": 42})),
            );
            let resp_payload =
                serde_json::to_vec(&response).expect("should serialize response");
            write_frame(
                &mut writer,
                MSG_TYPE_RESPONSE,
                Format::Json,
                &resp_payload,
            )
            .await
            .expect("should write response");

            // Keep connection alive
            tokio::time::sleep(Duration::from_millis(100)).await;
        });

        let client = IpcClient::connect(&socket_path)
            .await
            .expect("should connect");

        let envelope = IpcEnvelope::new_request(
            "test_actor",
            "TestQuery",
            serde_json::json!({"query": "test"}),
        );
        let response = client.request(envelope).await;
        assert!(response.is_ok());

        let response = response.expect("should get response");
        assert!(response.success);
        assert_eq!(
            response.payload,
            Some(serde_json::json!({"result": 42}))
        );

        accept_handle.await.expect("server task failed");
    }

    /// Test subscribe and receive push notifications.
    #[tokio::test]
    async fn subscribe_and_receive_push() {
        let dir = tempfile::tempdir().expect("failed to create temp dir");
        let socket_path = dir.path().join("test.sock");

        let listener = tokio::net::UnixListener::bind(&socket_path)
            .expect("failed to bind socket");

        let accept_handle = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("failed to accept");
            let (mut reader, mut writer) = stream.into_split();

            // Read subscribe request
            let (msg_type, _, payload) = read_frame(&mut reader, MAX_FRAME_SIZE)
                .await
                .expect("should read frame");
            assert_eq!(msg_type, MSG_TYPE_SUBSCRIBE);

            let sub_request: IpcSubscribeRequest =
                serde_json::from_slice(&payload).expect("should deserialize");

            // Send subscription response
            let response = IpcSubscriptionResponse::success(
                &sub_request.correlation_id,
                sub_request.message_types.clone(),
            );
            let resp_payload =
                serde_json::to_vec(&response).expect("should serialize");
            write_frame(
                &mut writer,
                MSG_TYPE_RESPONSE,
                Format::Json,
                &resp_payload,
            )
            .await
            .expect("should write response");

            // Send a push notification
            let notification = IpcPushNotification::new(
                "TestEvent",
                Some("test_actor".to_string()),
                serde_json::json!({"data": "hello"}),
            );
            let push_payload =
                serde_json::to_vec(&notification).expect("should serialize");
            write_frame(
                &mut writer,
                MSG_TYPE_PUSH,
                Format::Json,
                &push_payload,
            )
            .await
            .expect("should write push");

            // Keep connection alive
            tokio::time::sleep(Duration::from_millis(200)).await;
        });

        let client = IpcClient::connect(&socket_path)
            .await
            .expect("should connect");

        // Subscribe
        let sub_response = client
            .subscribe(vec!["TestEvent".to_string()])
            .await
            .expect("should subscribe");
        assert!(sub_response.success);

        // Take push receiver and wait for notification
        let mut push_rx = client
            .take_push_receiver()
            .expect("should take push receiver");

        let notification = tokio::time::timeout(Duration::from_secs(2), push_rx.recv())
            .await
            .expect("should not timeout")
            .expect("should receive notification");

        assert_eq!(notification.message_type, "TestEvent");
        assert_eq!(
            notification.payload,
            serde_json::json!({"data": "hello"})
        );

        accept_handle.await.expect("server task failed");
    }

    /// Test that stream frames are routed to the `request_stream` receiver and
    /// the channel closes cleanly after the final frame.
    #[tokio::test]
    async fn request_stream_receives_frames_until_final() {
        let dir = tempfile::tempdir().expect("failed to create temp dir");
        let socket_path = dir.path().join("test.sock");

        let listener = tokio::net::UnixListener::bind(&socket_path)
            .expect("failed to bind socket");

        let accept_handle = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("failed to accept");
            let (mut reader, mut writer) = stream.into_split();

            // Read the stream request
            let (msg_type, _, payload) = read_frame(&mut reader, MAX_FRAME_SIZE)
                .await
                .expect("should read frame");
            assert_eq!(msg_type, MSG_TYPE_REQUEST);

            let envelope: IpcEnvelope =
                serde_json::from_slice(&payload).expect("should deserialize");
            assert!(envelope.expects_stream);

            // Send three data frames followed by a final frame
            for sequence in 0..3_u32 {
                let frame = IpcStreamFrame::data(
                    &envelope.correlation_id,
                    sequence,
                    serde_json::json!({ "n": sequence }),
                );
                let frame_payload =
                    serde_json::to_vec(&frame).expect("should serialize frame");
                write_frame(&mut writer, MSG_TYPE_STREAM, Format::Json, &frame_payload)
                    .await
                    .expect("should write frame");
            }

            let final_frame = IpcStreamFrame::final_frame(&envelope.correlation_id, 3, None);
            let final_payload =
                serde_json::to_vec(&final_frame).expect("should serialize final frame");
            write_frame(&mut writer, MSG_TYPE_STREAM, Format::Json, &final_payload)
                .await
                .expect("should write final frame");

            // Keep connection alive
            tokio::time::sleep(Duration::from_millis(200)).await;
        });

        let client = IpcClient::connect(&socket_path)
            .await
            .expect("should connect");

        let envelope = IpcEnvelope::new_stream_request(
            "test_actor",
            "TestStream",
            serde_json::json!({}),
        );
        let mut stream_rx = client
            .request_stream(envelope)
            .await
            .expect("should start stream");

        let mut frames = Vec::new();
        while let Some(frame) =
            tokio::time::timeout(Duration::from_secs(2), stream_rx.recv())
                .await
                .expect("should not timeout")
        {
            frames.push(frame);
        }

        assert_eq!(frames.len(), 4);
        for (expected_sequence, frame) in (0_u32..).zip(frames.iter()) {
            assert_eq!(frame.sequence, expected_sequence);
        }
        assert!(frames.last().expect("frames should not be empty").is_final);
        assert!(frames[..3].iter().all(|f| !f.is_final));

        // The routing entry is released after the final frame
        tokio::time::sleep(Duration::from_millis(50)).await;
        assert!(client.active_streams.is_empty());

        accept_handle.await.expect("server task failed");
    }

    /// Test that the stream terminates when no frame arrives within the timeout.
    #[tokio::test]
    async fn request_stream_times_out_without_frames() {
        let dir = tempfile::tempdir().expect("failed to create temp dir");
        let socket_path = dir.path().join("test.sock");

        let listener = tokio::net::UnixListener::bind(&socket_path)
            .expect("failed to bind socket");

        let accept_handle = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("failed to accept");
            let (mut reader, _writer) = stream.into_split();

            // Read the request but never respond
            let _ = read_frame(&mut reader, MAX_FRAME_SIZE).await;
            tokio::time::sleep(Duration::from_millis(500)).await;
        });

        let client = IpcClient::connect(&socket_path)
            .await
            .expect("should connect");

        let envelope = IpcEnvelope::new_stream_request(
            "test_actor",
            "TestStream",
            serde_json::json!({}),
        );
        let mut stream_rx = client
            .request_stream_with_timeout(envelope, Duration::from_millis(100))
            .await
            .expect("should start stream");

        // The channel closes without an `is_final` frame once the timeout elapses
        let result = tokio::time::timeout(Duration::from_secs(2), stream_rx.recv())
            .await
            .expect("stream should terminate before the outer timeout");
        assert!(result.is_none());

        // The routing entry is cleaned up
        assert!(client.active_streams.is_empty());

        accept_handle.await.expect("server task failed");
    }

    /// Test that a stream terminates (rather than hanging) when the connection
    /// closes mid-stream.
    #[tokio::test]
    async fn request_stream_terminates_on_connection_close() {
        let dir = tempfile::tempdir().expect("failed to create temp dir");
        let socket_path = dir.path().join("test.sock");

        let listener = tokio::net::UnixListener::bind(&socket_path)
            .expect("failed to bind socket");

        let accept_handle = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("failed to accept");
            let (mut reader, mut writer) = stream.into_split();

            let (_, _, payload) = read_frame(&mut reader, MAX_FRAME_SIZE)
                .await
                .expect("should read frame");
            let envelope: IpcEnvelope =
                serde_json::from_slice(&payload).expect("should deserialize");

            // Send two data frames, then drop the connection without a final frame
            for sequence in 0..2_u32 {
                let frame = IpcStreamFrame::data(
                    &envelope.correlation_id,
                    sequence,
                    serde_json::json!({ "n": sequence }),
                );
                let frame_payload =
                    serde_json::to_vec(&frame).expect("should serialize frame");
                write_frame(&mut writer, MSG_TYPE_STREAM, Format::Json, &frame_payload)
                    .await
                    .expect("should write frame");
            }
        });

        let client = IpcClient::connect(&socket_path)
            .await
            .expect("should connect");

        let envelope = IpcEnvelope::new_stream_request(
            "test_actor",
            "TestStream",
            serde_json::json!({}),
        );
        let mut stream_rx = client
            .request_stream(envelope)
            .await
            .expect("should start stream");

        let mut frames = Vec::new();
        while let Some(frame) =
            tokio::time::timeout(Duration::from_secs(2), stream_rx.recv())
                .await
                .expect("stream should terminate, not hang")
        {
            frames.push(frame);
        }

        // Both data frames arrived, then the channel closed without `is_final`
        assert_eq!(frames.len(), 2);
        assert!(frames.iter().all(|f| !f.is_final));

        accept_handle.await.expect("server task failed");
    }

    /// Test that dropping the stream receiver cleans up the routing entry.
    #[tokio::test]
    async fn request_stream_receiver_drop_cleans_up() {
        let dir = tempfile::tempdir().expect("failed to create temp dir");
        let socket_path = dir.path().join("test.sock");

        let listener = tokio::net::UnixListener::bind(&socket_path)
            .expect("failed to bind socket");

        let accept_handle = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("failed to accept");
            let (mut reader, mut writer) = stream.into_split();

            let (_, _, payload) = read_frame(&mut reader, MAX_FRAME_SIZE)
                .await
                .expect("should read frame");
            let envelope: IpcEnvelope =
                serde_json::from_slice(&payload).expect("should deserialize");

            // Send a frame after the client has dropped its receiver
            tokio::time::sleep(Duration::from_millis(100)).await;
            let frame = IpcStreamFrame::data(
                &envelope.correlation_id,
                0,
                serde_json::json!({ "n": 0 }),
            );
            let frame_payload =
                serde_json::to_vec(&frame).expect("should serialize frame");
            write_frame(&mut writer, MSG_TYPE_STREAM, Format::Json, &frame_payload)
                .await
                .expect("should write frame");

            tokio::time::sleep(Duration::from_millis(200)).await;
        });

        let client = IpcClient::connect(&socket_path)
            .await
            .expect("should connect");

        let envelope = IpcEnvelope::new_stream_request(
            "test_actor",
            "TestStream",
            serde_json::json!({}),
        );
        let stream_rx = client
            .request_stream(envelope)
            .await
            .expect("should start stream");
        assert_eq!(client.active_streams.len(), 1);

        // Drop the receiver mid-stream
        drop(stream_rx);

        // Once the forwarder notices the dropped receiver, the entry is removed
        tokio::time::sleep(Duration::from_millis(300)).await;
        assert!(client.active_streams.is_empty());

        accept_handle.await.expect("server task failed");
    }

    /// Test that `take_push_receiver` returns `None` on second call.
    #[tokio::test]
    async fn take_push_receiver_once() {
        let dir = tempfile::tempdir().expect("failed to create temp dir");
        let socket_path = dir.path().join("test.sock");

        let listener = tokio::net::UnixListener::bind(&socket_path)
            .expect("failed to bind socket");

        let accept_handle = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("failed to accept");
            tokio::time::sleep(Duration::from_millis(200)).await;
            drop(stream);
        });

        let client = IpcClient::connect(&socket_path)
            .await
            .expect("should connect");

        assert!(client.take_push_receiver().is_some());
        assert!(client.take_push_receiver().is_none());

        accept_handle.await.expect("server task failed");
    }

    /// Test that a slow consumer receives every frame, in order and without
    /// gaps, via reader backpressure rather than frame drops.
    #[tokio::test]
    async fn request_stream_slow_consumer_receives_all_frames() {
        // Well beyond the combined per-stream channel capacities, so the
        // reader task must block on the full channel while the consumer lags.
        const FRAME_COUNT: u32 = 300;

        let dir = tempfile::tempdir().expect("failed to create temp dir");
        let socket_path = dir.path().join("test.sock");

        let listener = tokio::net::UnixListener::bind(&socket_path)
            .expect("failed to bind socket");

        let accept_handle = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("failed to accept");
            let (mut reader, mut writer) = stream.into_split();

            let (_, _, payload) = read_frame(&mut reader, MAX_FRAME_SIZE)
                .await
                .expect("should read frame");
            let envelope: IpcEnvelope =
                serde_json::from_slice(&payload).expect("should deserialize");

            // Flood the client with frames as fast as the socket accepts them
            for sequence in 0..FRAME_COUNT - 1 {
                let frame = IpcStreamFrame::data(
                    &envelope.correlation_id,
                    sequence,
                    serde_json::json!({ "n": sequence }),
                );
                let frame_payload =
                    serde_json::to_vec(&frame).expect("should serialize frame");
                write_frame(&mut writer, MSG_TYPE_STREAM, Format::Json, &frame_payload)
                    .await
                    .expect("should write frame");
            }

            let final_frame =
                IpcStreamFrame::final_frame(&envelope.correlation_id, FRAME_COUNT - 1, None);
            let final_payload =
                serde_json::to_vec(&final_frame).expect("should serialize final frame");
            write_frame(&mut writer, MSG_TYPE_STREAM, Format::Json, &final_payload)
                .await
                .expect("should write final frame");

            // Keep connection alive until the consumer has drained everything
            tokio::time::sleep(Duration::from_secs(2)).await;
        });

        let client = IpcClient::connect(&socket_path)
            .await
            .expect("should connect");

        let envelope = IpcEnvelope::new_stream_request(
            "test_actor",
            "TestStream",
            serde_json::json!({}),
        );
        let mut stream_rx = client
            .request_stream(envelope)
            .await
            .expect("should start stream");

        // Lag behind the server long enough for every buffer to fill
        tokio::time::sleep(Duration::from_millis(300)).await;

        let mut frames = Vec::new();
        while let Some(frame) =
            tokio::time::timeout(Duration::from_secs(5), stream_rx.recv())
                .await
                .expect("stream should terminate, not hang")
        {
            frames.push(frame);
        }

        // Every frame arrived, in order, with no sequence gaps
        assert_eq!(frames.len(), FRAME_COUNT as usize);
        for (expected_sequence, frame) in (0_u32..).zip(frames.iter()) {
            assert_eq!(frame.sequence, expected_sequence);
        }
        assert!(frames.last().expect("frames should not be empty").is_final);

        accept_handle.await.expect("server task failed");
    }

    /// Test that a pre-dispatch server rejection (e.g. rate limiting) reaches
    /// the stream consumer as a final error frame instead of stalling the
    /// stream until the inter-frame timeout.
    #[tokio::test]
    async fn request_stream_terminated_by_rate_limit_rejection() {
        let dir = tempfile::tempdir().expect("failed to create temp dir");
        let socket_path = dir.path().join("test.sock");

        let listener = tokio::net::UnixListener::bind(&socket_path)
            .expect("failed to bind socket");

        let accept_handle = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("failed to accept");
            let (mut reader, mut writer) = stream.into_split();

            let (_, _, payload) = read_frame(&mut reader, MAX_FRAME_SIZE)
                .await
                .expect("should read frame");
            let envelope: IpcEnvelope =
                serde_json::from_slice(&payload).expect("should deserialize");

            // Reject the stream request exactly like the listener's rate
            // limiter does: a plain error response on the same correlation ID.
            let response = IpcResponse::error(
                &envelope.correlation_id,
                &IpcError::RateLimited { retry_after_ms: 100 },
            );
            let resp_payload =
                serde_json::to_vec(&response).expect("should serialize response");
            write_frame(&mut writer, MSG_TYPE_ERROR, Format::Json, &resp_payload)
                .await
                .expect("should write response");

            // Keep connection alive
            tokio::time::sleep(Duration::from_millis(200)).await;
        });

        let client = IpcClient::connect(&socket_path)
            .await
            .expect("should connect");

        let envelope = IpcEnvelope::new_stream_request(
            "test_actor",
            "TestStream",
            serde_json::json!({}),
        );
        let mut stream_rx = client
            .request_stream(envelope)
            .await
            .expect("should start stream");

        // The rejection arrives as a synthesized final error frame
        let frame = tokio::time::timeout(Duration::from_secs(2), stream_rx.recv())
            .await
            .expect("stream should terminate, not hang")
            .expect("should receive the rejection frame");
        assert!(frame.is_final);
        assert_eq!(frame.error_code.as_deref(), Some("RATE_LIMITED"));
        assert!(frame.error.is_some());

        // The channel closes and the routing entry is released
        let next = tokio::time::timeout(Duration::from_secs(2), stream_rx.recv())
            .await
            .expect("stream should terminate, not hang");
        assert!(next.is_none());
        assert!(client.active_streams.is_empty());

        accept_handle.await.expect("server task failed");
    }

    /// Test that dropping the client terminates in-flight streams promptly
    /// instead of leaving consumers to wait out the inter-frame timeout.
    #[tokio::test]
    async fn drop_client_closes_stream_channel() {
        let dir = tempfile::tempdir().expect("failed to create temp dir");
        let socket_path = dir.path().join("test.sock");

        let listener = tokio::net::UnixListener::bind(&socket_path)
            .expect("failed to bind socket");

        let accept_handle = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("failed to accept");
            let (mut reader, _writer) = stream.into_split();

            // Read the request but never send any frames
            let _ = read_frame(&mut reader, MAX_FRAME_SIZE).await;
            tokio::time::sleep(Duration::from_millis(500)).await;
        });

        let client = IpcClient::connect(&socket_path)
            .await
            .expect("should connect");

        let envelope = IpcEnvelope::new_stream_request(
            "test_actor",
            "TestStream",
            serde_json::json!({}),
        );
        let mut stream_rx = client
            .request_stream(envelope)
            .await
            .expect("should start stream");

        // Drop the client with the stream still in flight. The default
        // inter-frame timeout is 30s, so a prompt close proves the Drop
        // cleanup (not the timeout) terminated the stream.
        drop(client);

        let result = tokio::time::timeout(Duration::from_secs(2), stream_rx.recv())
            .await
            .expect("stream should close promptly after drop, not hang");
        assert!(result.is_none());

        accept_handle.await.expect("server task failed");
    }

    /// Test graceful disconnect.
    #[tokio::test]
    async fn graceful_disconnect() {
        let dir = tempfile::tempdir().expect("failed to create temp dir");
        let socket_path = dir.path().join("test.sock");

        let listener = tokio::net::UnixListener::bind(&socket_path)
            .expect("failed to bind socket");

        let accept_handle = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("failed to accept");
            tokio::time::sleep(Duration::from_millis(500)).await;
            drop(stream);
        });

        let client = IpcClient::connect(&socket_path)
            .await
            .expect("should connect");

        assert!(client.is_connected());

        let result = client.disconnect().await;
        assert!(result.is_ok());

        // After disconnect, is_connected should return false
        // (may take a moment for the writer task to exit)
        tokio::time::sleep(Duration::from_millis(50)).await;
        assert!(!client.is_connected());

        // Second disconnect should be a no-op
        let result = client.disconnect().await;
        assert!(result.is_ok());

        accept_handle.await.expect("server task failed");
    }
}