rust-expect 0.2.0

Next-generation Expect-style terminal automation library for Rust
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
//! Session handle for interacting with spawned processes.
//!
//! This module provides the main `Session` type that users interact with
//! to control spawned processes, send input, and expect output.

use std::sync::Arc;
#[cfg(feature = "screen")]
use std::sync::{Mutex as StdMutex, MutexGuard};
use std::time::Duration;

use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::Mutex;

#[cfg(unix)]
use crate::backend::{AsyncPty, PtyConfig, PtySpawner};
#[cfg(windows)]
use crate::backend::{PtyConfig, PtySpawner, WindowsAsyncPty};
use crate::config::SessionConfig;
use crate::dialog::{Dialog, DialogExecutor, DialogResult};
use crate::error::{ExpectError, Result};
use crate::expect::{ExpectState, MatchResult, Matcher, Pattern, PatternManager, PatternSet};
use crate::interact::InteractBuilder;
#[cfg(feature = "screen")]
use crate::screen::Screen;
use crate::types::{ControlChar, Dimensions, Match, ProcessExitStatus, SessionId, SessionState};

/// Callback invoked for every chunk of bytes read from the transport.
///
/// Taps observe the raw byte stream as it arrives, after it is appended to the
/// matcher buffer but before any pattern matching is performed. They are the
/// foundation for screen emulation, transcript recording, and other features
/// that need to see output as it happens.
pub type OutputTap = Arc<dyn Fn(&[u8]) + Send + Sync>;

/// Opaque handle identifying a registered output tap. Returned by
/// [`Session::add_output_tap`] and accepted by
/// [`Session::remove_output_tap`].
///
/// Backed by `u64`. The id space is large enough that wraparound is not
/// reachable in practice; the implementation uses a non-wrapping `+= 1`
/// so a hypothetical exhaustion would surface as a loud panic instead of
/// silently colliding with a still-registered tap.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TapId(u64);

impl std::fmt::Display for TapId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "tap#{}", self.0)
    }
}

/// Lock the screen mutex, recovering from poisoning.
///
/// A user-supplied tap (or `Screen::process` panicking on a malformed parse
/// path) can poison the screen mutex. Silently returning a default on
/// poisoning makes screen-aware expects look like they always-miss, which
/// is a confusing failure mode. Recovering via `into_inner` lets the call
/// continue against the actual screen state — the screen contents are
/// still valid; only the lock was tainted.
#[cfg(feature = "screen")]
fn lock_screen(screen: &Arc<StdMutex<Screen>>) -> MutexGuard<'_, Screen> {
    match screen.lock() {
        Ok(g) => g,
        Err(poison) => {
            tracing::warn!("screen mutex was poisoned; recovering inner state");
            poison.into_inner()
        }
    }
}

/// A session handle for interacting with a spawned process.
///
/// The session provides methods to send input, expect patterns in output,
/// and manage the lifecycle of the process.
pub struct Session<T: AsyncReadExt + AsyncWriteExt + Unpin + Send> {
    /// The underlying transport (PTY, SSH channel, etc.).
    transport: Arc<Mutex<T>>,
    /// Session configuration.
    config: SessionConfig,
    /// Pattern matcher.
    matcher: Matcher,
    /// Pattern manager for before/after patterns.
    pattern_manager: PatternManager,
    /// Current session state.
    state: SessionState,
    /// Unique session identifier.
    id: SessionId,
    /// EOF flag.
    eof: bool,
    /// Output taps invoked on every chunk of bytes read from the transport,
    /// stored as (id, callback) so they can be removed individually.
    output_taps: Vec<(TapId, OutputTap)>,
    /// Monotonic counter for assigning new `TapId`s.
    next_tap_id: u64,
    /// Attached virtual terminal screen, fed from an output tap.
    #[cfg(feature = "screen")]
    screen: Option<Arc<StdMutex<Screen>>>,
    /// Tap id used to feed the attached screen, so `detach_screen` can
    /// remove only that tap and leave user-registered taps in place.
    #[cfg(feature = "screen")]
    screen_tap_id: Option<TapId>,
    /// Poll interval used by the screen-aware expect helpers
    /// (`expect_screen_contains`, `wait_screen_not_contains`,
    /// `wait_screen_stable`). 50 ms by default.
    #[cfg(feature = "screen")]
    screen_poll_interval: Duration,
}

impl<T: AsyncReadExt + AsyncWriteExt + Unpin + Send> Session<T> {
    /// Create a new session with the given transport.
    pub fn new(transport: T, config: SessionConfig) -> Self {
        let buffer_size = config.buffer.max_size;
        let mut matcher = Matcher::new(buffer_size);
        matcher.set_default_timeout(config.timeout.default);
        Self {
            transport: Arc::new(Mutex::new(transport)),
            config,
            matcher,
            pattern_manager: PatternManager::new(),
            state: SessionState::Starting,
            id: SessionId::new(),
            eof: false,
            output_taps: Vec::new(),
            next_tap_id: 0,
            #[cfg(feature = "screen")]
            screen: None,
            #[cfg(feature = "screen")]
            screen_tap_id: None,
            #[cfg(feature = "screen")]
            screen_poll_interval: Duration::from_millis(50),
        }
    }

    /// Set the polling interval used by the screen-aware expect helpers.
    ///
    /// Affects `expect_screen_contains`, `wait_screen_not_contains`, and
    /// `wait_screen_stable`. Smaller values reduce match latency at the
    /// cost of CPU; larger values do the opposite. Default is 50 ms.
    ///
    /// Available with the `screen` feature.
    #[cfg(feature = "screen")]
    pub const fn set_screen_poll_interval(&mut self, interval: Duration) {
        self.screen_poll_interval = interval;
    }

    /// Get the current screen-poll interval. Default 50 ms.
    ///
    /// Available with the `screen` feature.
    #[cfg(feature = "screen")]
    #[must_use]
    pub const fn screen_poll_interval(&self) -> Duration {
        self.screen_poll_interval
    }

    /// Register a callback that will be invoked with every chunk of bytes
    /// read from the transport.
    ///
    /// Taps observe the raw byte stream as it arrives — they receive bytes
    /// in the same form the underlying process produced them, including any
    /// ANSI escape sequences. Taps are invoked synchronously inside the read
    /// loop after the bytes are appended to the matcher buffer; they should
    /// be cheap and non-blocking. Use a channel if expensive work is required.
    ///
    /// Multiple taps may be registered; they are invoked in registration
    /// order. Taps are dropped when the session is dropped.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use std::sync::Arc;
    /// use std::sync::Mutex;
    /// let captured: Arc<Mutex<Vec<u8>>> = Arc::new(Mutex::new(Vec::new()));
    /// let buf = captured.clone();
    /// session.add_output_tap(move |chunk| {
    ///     buf.lock().unwrap().extend_from_slice(chunk);
    /// });
    /// ```
    pub fn add_output_tap<F>(&mut self, f: F) -> TapId
    where
        F: Fn(&[u8]) + Send + Sync + 'static,
    {
        let id = TapId(self.next_tap_id);
        // Plain addition (not wrapping_add): on the astronomically unlikely
        // event of u64 exhaustion on a single session, we'd rather panic
        // loudly than silently issue a colliding id.
        self.next_tap_id += 1;
        self.output_taps.push((id, Arc::new(f)));
        id
    }

    /// Remove a previously registered output tap by its [`TapId`]. Returns
    /// `true` if a tap was removed, `false` if the id was not registered
    /// (already removed, or never existed).
    pub fn remove_output_tap(&mut self, id: TapId) -> bool {
        let len_before = self.output_taps.len();
        self.output_taps.retain(|(existing, _)| *existing != id);
        self.output_taps.len() != len_before
    }

    /// Iterate the callbacks for all currently registered output taps.
    ///
    /// Exposed for instrumentation and inspection only — the read loops in
    /// [`expect`](Self::expect) and [`interact`](Self::interact) invoke
    /// these themselves. Returns the callback `Arc`s in registration
    /// order; ids are intentionally omitted (use
    /// [`add_output_tap`](Self::add_output_tap)'s return value if you
    /// need the id).
    pub fn output_tap_callbacks(&self) -> impl Iterator<Item = &OutputTap> {
        self.output_taps.iter().map(|(_, cb)| cb)
    }

    /// Attach a virtual terminal screen to this session.
    ///
    /// Creates a [`Screen`](crate::screen::Screen) with the session's
    /// configured dimensions and registers an output tap that feeds every
    /// chunk of output into the screen's ANSI parser. The screen is then
    /// accessible via [`screen()`](Self::screen) and is automatically updated
    /// whenever output is read from the transport (i.e. inside `expect_*`,
    /// `wait`, or `wait_screen_stable`).
    ///
    /// Repeated calls replace the previous screen.
    ///
    /// Available with the `screen` feature.
    #[cfg(feature = "screen")]
    pub fn attach_screen(&mut self) {
        let (cols, rows) = self.config.dimensions;
        self.attach_screen_with_dims(rows, cols);
    }

    /// Attach a screen with custom dimensions.
    ///
    /// `rows` and `cols` are the screen size in cells. Note that this does
    /// not resize the PTY itself — use [`resize_pty`](Self::resize_pty) for
    /// that. The two should normally match, but it can be useful to set a
    /// larger virtual screen for transcript capture.
    ///
    /// Available with the `screen` feature.
    #[cfg(feature = "screen")]
    pub fn attach_screen_with_dims(&mut self, rows: u16, cols: u16) {
        // Replace any previous screen + its tap so we don't leak callbacks.
        self.detach_screen();
        let screen = Arc::new(StdMutex::new(Screen::new(rows as usize, cols as usize)));
        let screen_for_tap = screen.clone();
        let id = self.add_output_tap(move |chunk| {
            // Reuse the shared poison-recovery helper so the tap-side and
            // read-side recovery logic stays in lockstep.
            lock_screen(&screen_for_tap).process(chunk);
        });
        self.screen = Some(screen);
        self.screen_tap_id = Some(id);
    }

    /// Detach the currently attached screen, also removing its output tap.
    /// No-op if no screen is attached. Returns `true` if a screen was
    /// detached.
    ///
    /// Available with the `screen` feature.
    #[cfg(feature = "screen")]
    pub fn detach_screen(&mut self) -> bool {
        if let Some(id) = self.screen_tap_id.take() {
            self.remove_output_tap(id);
        }
        self.screen.take().is_some()
    }

    /// Get the attached virtual terminal screen, if any.
    ///
    /// Returns a shared handle protected by a [`std::sync::Mutex`]. Lock it
    /// briefly to read screen state — the lock is also taken by the output
    /// tap on every read, so holding it for long stretches blocks the read
    /// loop.
    ///
    /// Available with the `screen` feature.
    #[cfg(feature = "screen")]
    #[must_use]
    pub const fn screen(&self) -> Option<&Arc<StdMutex<Screen>>> {
        self.screen.as_ref()
    }

    /// Get the session ID.
    #[must_use]
    pub const fn id(&self) -> &SessionId {
        &self.id
    }

    /// Get the current session state.
    #[must_use]
    pub const fn state(&self) -> SessionState {
        self.state
    }

    /// Get the session configuration.
    #[must_use]
    pub const fn config(&self) -> &SessionConfig {
        &self.config
    }

    /// Check if EOF has been detected.
    #[must_use]
    pub const fn is_eof(&self) -> bool {
        self.eof
    }

    /// Get the current buffer contents.
    #[must_use]
    pub fn buffer(&mut self) -> String {
        self.matcher.buffer_str()
    }

    /// Clear the buffer.
    pub fn clear_buffer(&mut self) {
        self.matcher.clear();
    }

    /// Get the pattern manager for before/after patterns.
    #[must_use]
    pub const fn pattern_manager(&self) -> &PatternManager {
        &self.pattern_manager
    }

    /// Get mutable access to the pattern manager.
    pub const fn pattern_manager_mut(&mut self) -> &mut PatternManager {
        &mut self.pattern_manager
    }

    /// Set the session state.
    pub const fn set_state(&mut self, state: SessionState) {
        self.state = state;
    }

    /// Send bytes to the process.
    ///
    /// # Errors
    ///
    /// Returns an error if the write fails.
    #[allow(clippy::significant_drop_tightening)]
    pub async fn send(&mut self, data: &[u8]) -> Result<()> {
        if matches!(self.state, SessionState::Closed | SessionState::Exited(_)) {
            return Err(ExpectError::SessionClosed);
        }

        let mut transport = self.transport.lock().await;
        transport
            .write_all(data)
            .await
            .map_err(|e| ExpectError::io_context("writing to process", e))?;
        transport
            .flush()
            .await
            .map_err(|e| ExpectError::io_context("flushing process output", e))?;
        Ok(())
    }

    /// Send a string to the process.
    ///
    /// # Errors
    ///
    /// Returns an error if the write fails.
    pub async fn send_str(&mut self, s: &str) -> Result<()> {
        self.send(s.as_bytes()).await
    }

    /// Send a line to the process (appends newline based on config).
    ///
    /// # Errors
    ///
    /// Returns an error if the write fails.
    pub async fn send_line(&mut self, line: &str) -> Result<()> {
        let line_ending = self.config.line_ending.as_str();
        let data = format!("{line}{line_ending}");
        self.send(data.as_bytes()).await
    }

    /// Send a control character to the process.
    ///
    /// # Errors
    ///
    /// Returns an error if the write fails.
    pub async fn send_control(&mut self, ctrl: ControlChar) -> Result<()> {
        self.send(&[ctrl.as_byte()]).await
    }

    /// Send a Shift+Tab keystroke.
    ///
    /// Sends the xterm "back tab" sequence `\x1b[Z` (CSI Z). Most TUIs use
    /// this to cycle a focused-element ring backwards or, in Claude Code's
    /// case, to cycle permission modes. Compatible with both plain xterm
    /// and the kitty keyboard protocol's CSI-u fallback mode.
    ///
    /// # Errors
    ///
    /// Returns an error if the write fails.
    pub async fn send_shift_tab(&mut self) -> Result<()> {
        self.send(b"\x1b[Z").await
    }

    /// Send text using bracketed paste mode (DECSET 2004).
    ///
    /// Wraps the content in `\x1b[200~` and `\x1b[201~` markers. Applications
    /// that have enabled bracketed paste treat the enclosed content as
    /// pasted input rather than typed input — this suppresses autocomplete,
    /// command-history scanning, and per-character interpretation such as a
    /// leading `/` triggering a slash-command popup. Safe to call even when
    /// the receiver hasn't enabled bracketed paste: most terminals ignore
    /// the markers and deliver the inner text as-is.
    ///
    /// # Errors
    ///
    /// Returns an error if the write fails or if `text` contains the
    /// closing paste marker `\x1b[201~`, which would let the receiver drop
    /// out of paste mode mid-payload. Callers that want to send such bytes
    /// should write them through the regular [`send`](Self::send) path.
    pub async fn send_paste(&mut self, text: &str) -> Result<()> {
        if memchr::memmem::find(text.as_bytes(), b"\x1b[201~").is_some() {
            return Err(ExpectError::InvalidInput {
                api: "send_paste".to_string(),
                reason:
                    "input contains the bracketed-paste end marker (\\x1b[201~); use send() for raw bytes that include this sequence"
                        .to_string(),
            });
        }
        self.send(b"\x1b[200~").await?;
        self.send(text.as_bytes()).await?;
        self.send(b"\x1b[201~").await
    }

    /// Expect a pattern in the output.
    ///
    /// Blocks until the pattern is matched, EOF is detected, or timeout occurs.
    ///
    /// # Errors
    ///
    /// Returns an error on timeout, EOF (if not expected), or I/O error.
    pub async fn expect(&mut self, pattern: impl Into<Pattern>) -> Result<Match> {
        let patterns = PatternSet::from_patterns(vec![pattern.into()]);
        self.expect_any(&patterns).await
    }

    /// Expect any of the given patterns.
    ///
    /// # Errors
    ///
    /// Returns an error on timeout, EOF (if not expected), or I/O error.
    pub async fn expect_any(&mut self, patterns: &PatternSet) -> Result<Match> {
        let timeout = self.matcher.get_timeout(patterns);
        let state = ExpectState::new(patterns.clone(), timeout);

        loop {
            // Check before patterns first
            if let Some((_, action)) = self
                .pattern_manager
                .check_before(&self.matcher.buffer_str())
            {
                match action {
                    crate::expect::HandlerAction::Continue => {}
                    crate::expect::HandlerAction::Return(s) => {
                        return Ok(Match::new(0, s, String::new(), self.matcher.buffer_str()));
                    }
                    crate::expect::HandlerAction::Abort(msg) => {
                        return Err(ExpectError::PatternNotFound {
                            pattern: msg,
                            buffer: self.matcher.buffer_str(),
                        });
                    }
                    crate::expect::HandlerAction::Respond(s) => {
                        self.send_str(&s).await?;
                    }
                }
            }

            // Check for pattern match
            if let Some(result) = self.matcher.try_match_any(patterns) {
                return Ok(self.matcher.consume_match(&result));
            }

            // Check for timeout
            if state.is_timed_out() {
                return Err(ExpectError::Timeout {
                    duration: timeout,
                    pattern: patterns
                        .iter()
                        .next()
                        .map(|p| p.pattern.as_str().to_string())
                        .unwrap_or_default(),
                    buffer: self.matcher.buffer_str(),
                });
            }

            // Check for EOF
            if self.eof {
                if state.expects_eof() {
                    return Ok(Match::new(
                        0,
                        String::new(),
                        self.matcher.buffer_str(),
                        String::new(),
                    ));
                }
                return Err(ExpectError::Eof {
                    buffer: self.matcher.buffer_str(),
                });
            }

            // Read more data
            self.read_with_timeout(state.remaining_time()).await?;
        }
    }

    /// Expect with a specific timeout.
    ///
    /// # Errors
    ///
    /// Returns an error on timeout, EOF, or I/O error.
    pub async fn expect_timeout(
        &mut self,
        pattern: impl Into<Pattern>,
        timeout: Duration,
    ) -> Result<Match> {
        let pattern = pattern.into();
        let mut patterns = PatternSet::new();
        patterns.add(pattern).add(Pattern::timeout(timeout));
        self.expect_any(&patterns).await
    }

    /// Wait until the attached screen contains the given substring.
    ///
    /// Drives reads from the transport in short increments, checking the
    /// rendered screen text after each. Returns successfully as soon as
    /// `needle` appears in the screen text, or with [`ExpectError::Timeout`]
    /// when `timeout` elapses without a match. Returns [`ExpectError::Eof`]
    /// if the process exits before the substring appears.
    ///
    /// This is the screen-aware counterpart to [`expect`](Self::expect): use
    /// it when the byte stream is full of ANSI escape sequences (e.g. when
    /// driving a TUI), where literal substring matching on the byte stream
    /// would fail because of interleaved cursor positioning and SGR codes.
    ///
    /// Requires an attached screen — call [`attach_screen`](Self::attach_screen)
    /// first.
    ///
    /// # Errors
    ///
    /// Returns an error if no screen is attached, the timeout expires, EOF
    /// is reached, or an I/O error occurs.
    ///
    /// Available with the `screen` feature.
    #[cfg(feature = "screen")]
    pub async fn expect_screen_contains(&mut self, needle: &str, timeout: Duration) -> Result<()> {
        let Some(screen) = self.screen.clone() else {
            return Err(ExpectError::ScreenNotAttached);
        };

        let start = tokio::time::Instant::now();
        let poll = self.screen_poll_interval;

        loop {
            if lock_screen(&screen).query().contains(needle) {
                return Ok(());
            }
            if self.eof {
                return Err(ExpectError::Eof {
                    buffer: lock_screen(&screen).text(),
                });
            }
            let elapsed = start.elapsed();
            if elapsed >= timeout {
                return Err(ExpectError::Timeout {
                    duration: timeout,
                    pattern: needle.to_string(),
                    buffer: lock_screen(&screen).text(),
                });
            }
            let remaining = timeout.saturating_sub(elapsed);
            self.read_with_timeout(poll.min(remaining)).await?;
        }
    }

    /// Wait until the attached screen no longer contains the given substring.
    ///
    /// The inverse of [`expect_screen_contains`](Self::expect_screen_contains).
    /// Returns successfully as soon as `needle` is absent from the rendered
    /// screen, or with [`ExpectError::Timeout`] when `timeout` elapses with
    /// the substring still present. EOF is treated as "absent" (the screen
    /// state is frozen at the final paint).
    ///
    /// Useful for anchoring on the *disappearance* of an indicator —
    /// e.g. waiting for a "request in flight" status to clear, a spinner
    /// glyph to stop, or a modal to close.
    ///
    /// Requires an attached screen.
    ///
    /// # Errors
    ///
    /// Returns an error if no screen is attached, the timeout expires while
    /// the substring is still visible, or an I/O error occurs.
    ///
    /// Available with the `screen` feature.
    #[cfg(feature = "screen")]
    pub async fn wait_screen_not_contains(
        &mut self,
        needle: &str,
        timeout: Duration,
    ) -> Result<()> {
        let Some(screen) = self.screen.clone() else {
            return Err(ExpectError::ScreenNotAttached);
        };

        let start = tokio::time::Instant::now();
        let poll = self.screen_poll_interval;

        loop {
            if !lock_screen(&screen).query().contains(needle) {
                return Ok(());
            }
            if self.eof {
                return Ok(());
            }
            let elapsed = start.elapsed();
            if elapsed >= timeout {
                return Err(ExpectError::Timeout {
                    duration: timeout,
                    pattern: format!("!{needle}"),
                    buffer: lock_screen(&screen).text(),
                });
            }
            let remaining = timeout.saturating_sub(elapsed);
            self.read_with_timeout(poll.min(remaining)).await?;
        }
    }

    /// Wait until the attached screen has been unchanged for `quiet_period`.
    ///
    /// Drives reads in short increments and tracks whether the rendered
    /// screen text changes between reads. Returns successfully when the
    /// screen has been quiescent for `quiet_period`, or with
    /// [`ExpectError::Timeout`] if `max_wait` elapses first.
    ///
    /// Useful as a generic "wait for the TUI to finish drawing" primitive
    /// when no specific anchor is available — for example, after submitting
    /// a prompt and before reading the response.
    ///
    /// A small `quiet_period` (e.g. 100-300 ms) catches paint completion;
    /// a larger one (1-2 s) waits out streaming responses with mid-stream
    /// pauses. Tune to the specific application.
    ///
    /// Requires an attached screen.
    ///
    /// # Errors
    ///
    /// Returns an error if no screen is attached, `max_wait` elapses, or an
    /// I/O error occurs. EOF is **not** an error — if the process exits, the
    /// final screen state is considered stable and the method returns Ok.
    ///
    /// Available with the `screen` feature.
    #[cfg(feature = "screen")]
    pub async fn wait_screen_stable(
        &mut self,
        quiet_period: Duration,
        max_wait: Duration,
    ) -> Result<()> {
        let Some(screen) = self.screen.clone() else {
            return Err(ExpectError::ScreenNotAttached);
        };

        let start = tokio::time::Instant::now();
        let poll = self.screen_poll_interval;
        let mut last_revision = lock_screen(&screen).revision();
        let mut last_change = tokio::time::Instant::now();

        loop {
            if last_change.elapsed() >= quiet_period {
                return Ok(());
            }
            if self.eof {
                return Ok(());
            }
            if start.elapsed() >= max_wait {
                return Err(ExpectError::Timeout {
                    duration: max_wait,
                    pattern: "<screen stability>".to_string(),
                    buffer: lock_screen(&screen).text(),
                });
            }
            self.read_with_timeout(poll).await?;
            let current_revision = lock_screen(&screen).revision();
            if current_revision != last_revision {
                last_revision = current_revision;
                last_change = tokio::time::Instant::now();
            }
        }
    }

    /// Read data from the transport with timeout.
    async fn read_with_timeout(&mut self, timeout: Duration) -> Result<usize> {
        let mut buf = [0u8; 4096];
        let mut transport = self.transport.lock().await;

        match tokio::time::timeout(timeout, transport.read(&mut buf)).await {
            Ok(Ok(0)) => {
                self.eof = true;
                Ok(0)
            }
            Ok(Ok(n)) => {
                self.matcher.append(&buf[..n]);
                // Run taps in catch_unwind so a panicking user callback can't
                // unwind across our await boundary or poison subsequent taps.
                // We log and continue rather than propagate — taps are
                // observers, not error sources.
                for (id, tap) in &self.output_taps {
                    let tap_clone = tap.clone();
                    let chunk = &buf[..n];
                    let result =
                        std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| tap_clone(chunk)));
                    if result.is_err() {
                        tracing::warn!(
                            %id,
                            "output tap panicked; the panic was caught and other taps continue"
                        );
                    }
                }
                Ok(n)
            }
            Ok(Err(e)) => {
                // On Linux, reading from PTY master returns EIO when the slave is closed
                // (i.e., the child process has terminated). Treat this as EOF.
                // See: https://bugs.python.org/issue5380
                if is_pty_eof_error(&e) {
                    self.eof = true;
                    Ok(0)
                } else {
                    Err(ExpectError::io_context("reading from process", e))
                }
            }
            Err(_) => {
                // Timeout, but not an error - caller will handle
                Ok(0)
            }
        }
    }

    /// Wait for the process to exit.
    ///
    /// This method blocks until EOF is detected on the session, which typically
    /// happens when the child process terminates.
    ///
    /// # Warning
    ///
    /// This method has no timeout and may block indefinitely if the process
    /// does not exit. Consider using [`wait_timeout`](Self::wait_timeout) or
    /// [`expect_eof_timeout`](Self::expect_eof_timeout) for bounded waits.
    ///
    /// # Errors
    ///
    /// Returns an error if waiting fails due to I/O error.
    pub async fn wait(&mut self) -> Result<ProcessExitStatus> {
        // Read until EOF
        while !self.eof {
            if self.read_with_timeout(Duration::from_millis(100)).await? == 0 && !self.eof {
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        }

        // Return unknown status - actual status depends on backend
        self.state = SessionState::Exited(ProcessExitStatus::Unknown);
        Ok(ProcessExitStatus::Unknown)
    }

    /// Wait for the process to exit with a timeout.
    ///
    /// Like [`wait`](Self::wait), but with a maximum duration to wait.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The timeout expires before the process exits
    /// - An I/O error occurs while waiting
    pub async fn wait_timeout(&mut self, timeout: Duration) -> Result<ProcessExitStatus> {
        let deadline = tokio::time::Instant::now() + timeout;

        while !self.eof {
            let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
            if remaining.is_zero() {
                return Err(ExpectError::timeout(
                    timeout,
                    "<EOF>",
                    self.matcher.buffer_str(),
                ));
            }

            // Use smaller of remaining time or 100ms for polling
            let poll_timeout = remaining.min(Duration::from_millis(100));
            if self.read_with_timeout(poll_timeout).await? == 0 && !self.eof {
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        }

        self.state = SessionState::Exited(ProcessExitStatus::Unknown);
        Ok(ProcessExitStatus::Unknown)
    }

    /// Check if a pattern matches immediately without blocking.
    #[must_use]
    pub fn check(&mut self, pattern: &Pattern) -> Option<MatchResult> {
        self.matcher.try_match(pattern)
    }

    /// Get the underlying transport.
    ///
    /// Use with caution as direct access bypasses session management.
    #[must_use]
    pub const fn transport(&self) -> &Arc<Mutex<T>> {
        &self.transport
    }

    /// Start an interactive session with pattern hooks.
    ///
    /// This returns a builder that allows you to configure pattern-based
    /// callbacks that fire when patterns match in the output or input.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use rust_expect::{Session, InteractAction};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), rust_expect::ExpectError> {
    ///     let mut session = Session::spawn("/bin/bash", &[]).await?;
    ///
    ///     session.interact()
    ///         .on_output("password:", |ctx| {
    ///             ctx.send("my_password\n")
    ///         })
    ///         .on_output("logout", |_| {
    ///             InteractAction::Stop
    ///         })
    ///         .start()
    ///         .await?;
    ///
    ///     Ok(())
    /// }
    /// ```
    #[must_use]
    pub fn interact(&self) -> InteractBuilder<'_, T>
    where
        T: 'static,
    {
        // Snapshot the currently registered output taps so the interact
        // read loop can fire them on every chunk. Without this, attached
        // screens and transcript recorders would silently freeze for the
        // duration of interact().
        let taps: Vec<OutputTap> = self
            .output_taps
            .iter()
            .map(|(_, tap)| tap.clone())
            .collect();
        InteractBuilder::new(&self.transport, taps)
    }

    /// Run a dialog on this session.
    ///
    /// A dialog is a predefined sequence of expect/send operations.
    /// This method executes the dialog and returns the result.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use rust_expect::{Session, Dialog, DialogStep};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), rust_expect::ExpectError> {
    ///     let mut session = Session::spawn("/bin/bash", &[]).await?;
    ///
    ///     let dialog = Dialog::named("shell_test")
    ///         .step(DialogStep::new("prompt")
    ///             .with_expect("$")
    ///             .with_send("echo hello\n"))
    ///         .step(DialogStep::new("verify")
    ///             .with_expect("hello"));
    ///
    ///     let result = session.run_dialog(&dialog).await?;
    ///     assert!(result.success);
    ///     Ok(())
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if I/O fails. Step-level timeouts are reported
    /// in the `DialogResult` rather than as errors.
    pub async fn run_dialog(&mut self, dialog: &Dialog) -> Result<DialogResult> {
        let executor = DialogExecutor::default();
        executor.execute(self, dialog).await
    }

    /// Run a dialog with a custom executor.
    ///
    /// This allows customizing the executor settings (max steps, default timeout).
    ///
    /// # Errors
    ///
    /// Returns an error if I/O fails.
    pub async fn run_dialog_with(
        &mut self,
        dialog: &Dialog,
        executor: &DialogExecutor,
    ) -> Result<DialogResult> {
        executor.execute(self, dialog).await
    }

    /// Expect end-of-file (process termination).
    ///
    /// This is a convenience method for waiting until the process terminates
    /// and closes its output stream.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use rust_expect::Session;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), rust_expect::ExpectError> {
    ///     let mut session = Session::spawn("echo", &["hello"]).await?;
    ///     session.expect("hello").await?;
    ///     session.expect_eof().await?;
    ///     Ok(())
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if the session times out before EOF or an I/O error occurs.
    pub async fn expect_eof(&mut self) -> Result<Match> {
        self.expect(Pattern::eof()).await
    }

    /// Expect end-of-file with a specific timeout.
    ///
    /// # Errors
    ///
    /// Returns an error if the session times out before EOF or an I/O error occurs.
    pub async fn expect_eof_timeout(&mut self, timeout: Duration) -> Result<Match> {
        let mut patterns = PatternSet::new();
        patterns.add(Pattern::eof()).add(Pattern::timeout(timeout));
        self.expect_any(&patterns).await
    }

    /// Run a batch of commands, waiting for the prompt after each.
    ///
    /// This is a convenience method for executing multiple shell commands
    /// in sequence. For each command, it sends the command line and waits
    /// for the prompt pattern to appear.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use rust_expect::{Session, Pattern};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), rust_expect::ExpectError> {
    ///     let mut session = Session::spawn("/bin/bash", &[]).await?;
    ///     session.expect(Pattern::shell_prompt()).await?;
    ///
    ///     // Run a batch of commands
    ///     let results = session.run_script(
    ///         &["pwd", "whoami", "date"],
    ///         Pattern::shell_prompt(),
    ///     ).await?;
    ///
    ///     for result in &results {
    ///         println!("Output: {}", result.before.trim());
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if any command times out or I/O fails.
    /// On error, partial results are lost; consider using [`Self::run_script_with_results`]
    /// if you need to capture partial results on failure.
    pub async fn run_script<I, S>(&mut self, commands: I, prompt: Pattern) -> Result<Vec<Match>>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let mut results = Vec::new();

        for cmd in commands {
            self.send_line(cmd.as_ref()).await?;
            let result = self.expect(prompt.clone()).await?;
            results.push(result);
        }

        Ok(results)
    }

    /// Run a batch of commands with a specific timeout per command.
    ///
    /// Like [`run_script`](Self::run_script), but applies the given timeout
    /// to each command individually.
    ///
    /// # Errors
    ///
    /// Returns an error if any command times out or I/O fails.
    pub async fn run_script_timeout<I, S>(
        &mut self,
        commands: I,
        prompt: Pattern,
        timeout: Duration,
    ) -> Result<Vec<Match>>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let mut results = Vec::new();

        for cmd in commands {
            self.send_line(cmd.as_ref()).await?;
            let result = self.expect_timeout(prompt.clone(), timeout).await?;
            results.push(result);
        }

        Ok(results)
    }

    /// Run a batch of commands, collecting results even on failure.
    ///
    /// Unlike [`run_script`](Self::run_script), this method continues
    /// collecting results and returns them along with any error that occurred.
    ///
    /// # Returns
    ///
    /// A tuple of `(results, error)` where:
    /// - `results` contains the matches for successfully completed commands
    /// - `error` is `Some(err)` if an error occurred, `None` if all commands succeeded
    ///
    /// # Example
    ///
    /// ```no_run
    /// use rust_expect::{Session, Pattern};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), rust_expect::ExpectError> {
    ///     let mut session = Session::spawn("/bin/bash", &[]).await?;
    ///     session.expect(Pattern::shell_prompt()).await?;
    ///
    ///     let (results, error) = session.run_script_with_results(
    ///         &["pwd", "bad_command", "date"],
    ///         Pattern::shell_prompt(),
    ///     ).await;
    ///
    ///     println!("Completed {} commands", results.len());
    ///     if let Some(e) = error {
    ///         eprintln!("Script failed at command {}: {}", results.len(), e);
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn run_script_with_results<I, S>(
        &mut self,
        commands: I,
        prompt: Pattern,
    ) -> (Vec<Match>, Option<ExpectError>)
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let mut results = Vec::new();

        for cmd in commands {
            match self.send_line(cmd.as_ref()).await {
                Ok(()) => {}
                Err(e) => return (results, Some(e)),
            }

            match self.expect(prompt.clone()).await {
                Ok(result) => results.push(result),
                Err(e) => return (results, Some(e)),
            }
        }

        (results, None)
    }
}

impl<T: AsyncReadExt + AsyncWriteExt + Unpin + Send> std::fmt::Debug for Session<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Session")
            .field("id", &self.id)
            .field("state", &self.state)
            .field("eof", &self.eof)
            .finish_non_exhaustive()
    }
}

// Unix-specific spawn implementation
#[cfg(unix)]
impl Session<AsyncPty> {
    /// Spawn a new process with the given command.
    ///
    /// This creates a new PTY, forks a child process, and returns a Session
    /// connected to the child's terminal.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use rust_expect::Session;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), rust_expect::ExpectError> {
    ///     let mut session = Session::spawn("/bin/bash", &[]).await?;
    ///     session.expect("$").await?;
    ///     session.send_line("echo hello").await?;
    ///     session.expect("hello").await?;
    ///     Ok(())
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The command contains null bytes
    /// - PTY allocation fails
    /// - Fork fails
    /// - The command cannot be executed
    pub async fn spawn(command: &str, args: &[&str]) -> Result<Self> {
        Self::spawn_with_config(command, args, SessionConfig::default()).await
    }

    /// Spawn a new process with custom configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if spawning fails.
    pub async fn spawn_with_config(
        command: &str,
        args: &[&str],
        config: SessionConfig,
    ) -> Result<Self> {
        let pty_config = PtyConfig::from(&config);
        let spawner = PtySpawner::with_config(pty_config);

        // Convert &[&str] to Vec<String> for the spawner
        let args_owned: Vec<String> = args.iter().map(|s| (*s).to_string()).collect();

        // Spawn the process
        let handle = spawner.spawn(command, &args_owned).await?;

        // Wrap in AsyncPty for async I/O
        let async_pty = AsyncPty::from_handle(handle)
            .map_err(|e| ExpectError::io_context("creating async PTY wrapper", e))?;

        // Create the session
        let mut session = Self::new(async_pty, config);
        session.state = SessionState::Running;

        Ok(session)
    }

    /// Get the child process ID.
    #[must_use]
    pub fn pid(&self) -> u32 {
        // We need to access the inner transport's pid
        // For now, use the blocking lock since we know it's not contended
        // during a sync call like this
        if let Ok(transport) = self.transport.try_lock() {
            transport.pid()
        } else {
            0
        }
    }

    /// Resize the terminal.
    ///
    /// Also resizes the attached screen (if any) so it stays consistent
    /// with the PTY. Without this, screen-aware assertions would drift
    /// after a resize.
    ///
    /// # Errors
    ///
    /// Returns an error if the resize ioctl fails.
    pub async fn resize_pty(&mut self, cols: u16, rows: u16) -> Result<()> {
        {
            let mut transport = self.transport.lock().await;
            transport.resize(cols, rows)?;
        }
        self.config.dimensions = (cols, rows);
        #[cfg(feature = "screen")]
        if let Some(screen) = self.screen.as_ref()
            && let Ok(mut s) = screen.lock()
        {
            s.resize(rows as usize, cols as usize);
        }
        Ok(())
    }

    /// Send a signal to the child process.
    ///
    /// # Errors
    ///
    /// Returns an error if sending the signal fails.
    pub fn signal(&self, signal: i32) -> Result<()> {
        if let Ok(transport) = self.transport.try_lock() {
            transport.signal(signal)
        } else {
            Err(ExpectError::io_context(
                "sending signal to process",
                std::io::Error::new(std::io::ErrorKind::WouldBlock, "transport is locked"),
            ))
        }
    }

    /// Kill the child process.
    ///
    /// # Errors
    ///
    /// Returns an error if killing the process fails.
    pub fn kill(&self) -> Result<()> {
        if let Ok(transport) = self.transport.try_lock() {
            transport.kill()
        } else {
            Err(ExpectError::io_context(
                "killing process",
                std::io::Error::new(std::io::ErrorKind::WouldBlock, "transport is locked"),
            ))
        }
    }
}

// Windows-specific spawn implementation
#[cfg(windows)]
impl Session<WindowsAsyncPty> {
    /// Spawn a new process with the given command.
    ///
    /// This creates a new PTY using Windows ConPTY, spawns a child process,
    /// and returns a Session connected to the child's terminal.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use rust_expect::Session;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), rust_expect::ExpectError> {
    ///     let mut session = Session::spawn("cmd.exe", &[]).await?;
    ///     session.expect(">").await?;
    ///     session.send_line("echo hello").await?;
    ///     session.expect("hello").await?;
    ///     Ok(())
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - ConPTY is not available (Windows version too old)
    /// - PTY allocation fails
    /// - The command cannot be executed
    pub async fn spawn(command: &str, args: &[&str]) -> Result<Self> {
        Self::spawn_with_config(command, args, SessionConfig::default()).await
    }

    /// Spawn a new process with custom configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if spawning fails.
    pub async fn spawn_with_config(
        command: &str,
        args: &[&str],
        config: SessionConfig,
    ) -> Result<Self> {
        let pty_config = PtyConfig::from(&config);
        let spawner = PtySpawner::with_config(pty_config);

        // Convert &[&str] to Vec<String> for the spawner
        let args_owned: Vec<String> = args.iter().map(|s| s.to_string()).collect();

        // Spawn the process
        let handle = spawner.spawn(command, &args_owned).await?;

        // Wrap in WindowsAsyncPty for async I/O
        let async_pty = WindowsAsyncPty::from_handle(handle);

        // Create the session
        let mut session = Session::new(async_pty, config);
        session.state = SessionState::Running;

        Ok(session)
    }

    /// Get the child process ID.
    #[must_use]
    pub fn pid(&self) -> u32 {
        if let Ok(transport) = self.transport.try_lock() {
            transport.pid()
        } else {
            0
        }
    }

    /// Resize the terminal.
    ///
    /// # Errors
    ///
    /// Returns an error if the resize operation fails.
    pub async fn resize_pty(&mut self, cols: u16, rows: u16) -> Result<()> {
        let mut transport = self.transport.lock().await;
        transport.resize(cols, rows)
    }

    /// Check if the child process is still running.
    #[must_use]
    pub fn is_running(&self) -> bool {
        if let Ok(transport) = self.transport.try_lock() {
            transport.is_running()
        } else {
            true // Assume running if we can't check
        }
    }

    /// Kill the child process.
    ///
    /// # Errors
    ///
    /// Returns an error if killing the process fails.
    pub fn kill(&self) -> Result<()> {
        if let Ok(mut transport) = self.transport.try_lock() {
            transport.kill()
        } else {
            Err(ExpectError::io_context(
                "killing process",
                std::io::Error::new(std::io::ErrorKind::WouldBlock, "transport is locked"),
            ))
        }
    }
}

/// Extension trait for session operations.
pub trait SessionExt {
    /// Send and expect in one call.
    fn send_expect(
        &mut self,
        send: &str,
        expect: impl Into<Pattern>,
    ) -> impl std::future::Future<Output = Result<Match>> + Send;

    /// Resize the terminal.
    fn resize(
        &mut self,
        dimensions: Dimensions,
    ) -> impl std::future::Future<Output = Result<()>> + Send;
}

/// Check if an I/O error indicates PTY EOF.
///
/// On Linux, reading from the PTY master returns EIO when the slave side
/// has been closed (i.e., the child process has terminated). This is different
/// from the standard EOF behavior where `read()` returns 0 bytes.
///
/// This function returns true for errors that should be treated as EOF:
/// - EIO (errno 5) on Unix systems
/// - `BrokenPipe` on any platform
fn is_pty_eof_error(e: &std::io::Error) -> bool {
    use std::io::ErrorKind;

    // BrokenPipe indicates the other end has closed
    if e.kind() == ErrorKind::BrokenPipe {
        return true;
    }

    // On Unix, check for EIO which indicates slave PTY closed
    #[cfg(unix)]
    {
        if let Some(errno) = e.raw_os_error() {
            // EIO is 5 on Linux/macOS/BSD
            if errno == libc::EIO {
                return true;
            }
        }
    }

    false
}