rmux-server 0.10.0

Tokio daemon and request dispatcher for the RMUX terminal multiplexer.
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
use super::*;

#[tokio::test]
async fn live_attach_kitty_graphics_apc_passes_through_unchanged_when_chunked() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;

    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;

    let expected = b"\x1b_Gi=7;OK\x1b\\";
    let capture = RawPaneInputProbe::start(
        &handler,
        &alpha,
        "live-attach-kitty-graphics-apc",
        expected.len(),
    )
    .await;

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b_Gi=7")
        .await
        .expect("first kitty graphics APC chunk");
    assert_eq!(pending_input, b"\x1b_Gi=7");

    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b";OK")
        .await
        .expect("second kitty graphics APC chunk");
    assert_eq!(pending_input, b"\x1b_Gi=7;OK");

    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b\\")
        .await
        .expect("closing kitty graphics APC chunk");
    assert!(pending_input.is_empty());

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, expected).await;
}

#[tokio::test]
async fn live_attach_meta_underscore_forwards_unchanged_after_escape_timeout() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;

    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;

    let expected = b"\x1b_x";
    let capture = RawPaneInputProbe::start(
        &handler,
        &alpha,
        "live-attach-meta-underscore",
        expected.len(),
    )
    .await;

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b_")
        .await
        .expect("meta underscore input");
    assert_eq!(pending_input, b"\x1b_");
    assert!(
        handler
            .flush_attached_pending_escape_input(requester_pid, &mut pending_input)
            .await
            .expect("meta underscore escape timeout"),
        "timed-out meta underscore is forwarded",
    );
    assert!(pending_input.is_empty());

    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"x")
        .await
        .expect("following literal input");
    assert!(pending_input.is_empty());

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, expected).await;
}

#[tokio::test]
async fn live_attach_meta_underscore_dispatches_root_binding_after_escape_timeout() {
    let handler = RequestHandler::new();
    let alpha = session_name("meta-underscore-binding");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;
    let rebound = handler
        .handle(Request::BindKey(Box::new(BindKeyRequest {
            table_name: "root".to_owned(),
            key: "M-_".to_owned(),
            note: Some("timed-out meta underscore".to_owned()),
            repeat: false,
            command: Some(vec![
                "send-keys".to_owned(),
                "-l".to_owned(),
                "B".to_owned(),
            ]),
        })))
        .await;
    assert!(matches!(rebound, Response::BindKey(_)), "{rebound:?}");

    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;
    let capture =
        RawPaneInputProbe::start(&handler, &alpha, "live-attach-meta-underscore-binding", 1).await;

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b_")
        .await
        .expect("ambiguous meta underscore waits for escape timeout");
    assert_eq!(pending_input, b"\x1b_");
    assert!(
        !handler
            .flush_attached_pending_escape_input(requester_pid, &mut pending_input)
            .await
            .expect("meta underscore escape timeout dispatches binding"),
        "bound meta underscore is consumed instead of forwarded",
    );
    assert!(pending_input.is_empty());

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, b"B").await;
}

#[tokio::test]
async fn meta_underscore_then_typed_g_flushes_binding_and_unblocks_input() {
    // Alt+_ followed by a typed 'G' selects the kitty APC opener \x1b_G,
    // whose payload scan only ends on ST. If its streaming idle budget later
    // expires, the flush still resolves it as keyboard input (M-_ through the
    // key tables, body bytes rerouted) instead of swallowing every subsequent
    // keystroke — the same class as the fixed consumed-OSC M-] 5 2 ; swallow.
    let handler = RequestHandler::new();
    let alpha = session_name("meta-underscore-typed-g");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;
    let rebound = handler
        .handle(Request::BindKey(Box::new(BindKeyRequest {
            table_name: "root".to_owned(),
            key: "M-_".to_owned(),
            note: None,
            repeat: false,
            command: Some(vec![
                "set-buffer".to_owned(),
                "-b".to_owned(),
                "apc-flushed".to_owned(),
                "ok".to_owned(),
            ]),
        })))
        .await;
    assert!(matches!(rebound, Response::BindKey(_)), "{rebound:?}");
    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;
    let capture = RawPaneInputProbe::start(&handler, &alpha, "meta-underscore-typed-g", 0).await;

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b_")
        .await
        .expect("ambiguous meta underscore waits for escape timeout");
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"G")
        .await
        .expect("typed G stays retained as a possible kitty APC");
    assert_eq!(pending_input, b"\x1b_G");
    handler
        .flush_attached_pending_escape_input(requester_pid, &mut pending_input)
        .await
        .expect("timed-out M-_ input dispatches the binding");
    assert!(pending_input.is_empty());
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"x")
        .await
        .expect("input after the flush is not swallowed");

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, b"Gx").await;
    let state = handler.state.lock().await;
    let (_, contents) = state
        .buffers
        .show(Some("apc-flushed"))
        .expect("M-_ binding fired from the idle-timeout flush");
    assert_eq!(contents, b"ok");
}

#[tokio::test]
async fn live_attach_meta_underscore_timeout_respects_read_only_transition() {
    let handler = RequestHandler::new();
    let alpha = session_name("meta-underscore-read-only");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;
    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;
    let capture = RawPaneInputProbe::start(&handler, &alpha, "read-only-meta-underscore", 0).await;

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b_")
        .await
        .expect("ambiguous meta underscore waits for escape timeout");
    assert_eq!(pending_input, b"\x1b_");
    {
        let mut active_attach = handler.active_attach.lock().await;
        active_attach
            .by_pid
            .get_mut(&requester_pid)
            .expect("attach is active")
            .flags
            .insert(crate::client_flags::ClientFlags::READONLY);
    }

    assert!(
        !handler
            .flush_attached_pending_escape_input(requester_pid, &mut pending_input)
            .await
            .expect("read-only escape timeout is dropped"),
        "read-only timed-out input is not forwarded",
    );
    assert!(pending_input.is_empty());

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, b"").await;
}

#[tokio::test]
async fn live_attach_terminal_response_is_consumed_when_chunked() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;

    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;

    let capture =
        RawPaneInputProbe::start(&handler, &alpha, "live-attach-terminal-response", 0).await;

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b[?62")
        .await
        .expect("first terminal response chunk");
    assert_eq!(pending_input, b"\x1b[?62");

    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b";52;c")
        .await
        .expect("second terminal response chunk");
    assert!(pending_input.is_empty());

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, b"").await;
}

#[tokio::test]
async fn live_attach_cursor_position_response_is_forwarded_when_chunked() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;

    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;

    let expected = b"\x1b[12;34R";
    let capture = RawPaneInputProbe::start(
        &handler,
        &alpha,
        "live-attach-cursor-position-response",
        expected.len(),
    )
    .await;

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b[12;34")
        .await
        .expect("first CPR chunk");
    assert_eq!(pending_input, b"\x1b[12;34");

    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"R")
        .await
        .expect("second CPR chunk");
    assert!(pending_input.is_empty());

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, expected).await;
}

#[tokio::test]
async fn live_attach_decrpm_response_is_consumed() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;

    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;

    let capture = RawPaneInputProbe::start(&handler, &alpha, "live-attach-decrpm", 0).await;

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x1b[?2004;1$y")
        .await
        .expect("DECRPM response");

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, b"").await;
}

#[tokio::test]
async fn live_attach_osc_sequences_are_consumed_at_attach_boundary() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;

    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;

    let capture = RawPaneInputProbe::start(&handler, &alpha, "live-attach-osc-response", 0).await;

    let response = b"\x1b]52;c;AAAA\x07";
    for split in 1..response.len() {
        let mut pending_input = Vec::new();
        handler
            .handle_attached_live_input(requester_pid, &mut pending_input, &response[..split])
            .await
            .expect("first fragmented OSC chunk");
        assert_eq!(pending_input, response[..split], "split at byte {split}");

        handler
            .handle_attached_live_input(requester_pid, &mut pending_input, &response[split..])
            .await
            .expect("second fragmented OSC chunk");
        assert!(pending_input.is_empty(), "split at byte {split}");
    }

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, b"").await;
}

#[tokio::test]
async fn live_attach_correlated_palette_responses_reach_the_pane_with_bel_or_st() {
    let handler = RequestHandler::new();
    let alpha = session_name("live-attach-palette-response");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;
    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;
    {
        let mut state = handler.state.lock().await;
        state
            .append_bytes_to_pane_transcript_for_test(&alpha, 0, 0, b"\x1b]4;0;?;7;?\x07")
            .expect("pane emits two palette queries");
    }

    let bel = b"\x1b]4;0;rgb:0000/1111/ffff\x07";
    let st = b"\x1b]4;7;rgb:1111/2222/3333\x1b\\";
    let mut expected = bel.to_vec();
    expected.extend_from_slice(st);
    let capture = RawPaneInputProbe::start(
        &handler,
        &alpha,
        "live-attach-palette-response",
        expected.len(),
    )
    .await;

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, &bel[..bel.len() - 1])
        .await
        .expect("fragmented BEL palette response body");
    assert_eq!(pending_input, bel[..bel.len() - 1]);
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, &bel[bel.len() - 1..])
        .await
        .expect("BEL palette response terminator");
    assert!(pending_input.is_empty());

    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, &st[..st.len() - 1])
        .await
        .expect("fragmented ST palette response body");
    assert_eq!(pending_input, st[..st.len() - 1]);
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, &st[st.len() - 1..])
        .await
        .expect("ST palette response terminator");
    assert!(pending_input.is_empty());

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, &expected).await;
}

#[tokio::test]
async fn live_attach_palette_response_rejects_unsolicited_mismatched_and_duplicate_input() {
    let handler = RequestHandler::new();
    let alpha = session_name("live-attach-palette-correlation");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;
    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;
    {
        let mut state = handler.state.lock().await;
        state
            .append_bytes_to_pane_transcript_for_test(&alpha, 0, 0, b"\x1b]4;7;?\x1b\\")
            .expect("pane emits palette query for index 7");
    }

    let expected = b"\x1b]4;7;rgb:1111/2222/3333\x07";
    let capture = RawPaneInputProbe::start(
        &handler,
        &alpha,
        "live-attach-palette-correlation",
        expected.len(),
    )
    .await;
    let mut pending_input = Vec::new();

    for rejected in [
        b"\x1b]4;0;rgb:0000/0000/0000\x07".as_slice(),
        b"\x1b]4;7;not-rgb\x07".as_slice(),
    ] {
        handler
            .handle_attached_live_input(requester_pid, &mut pending_input, rejected)
            .await
            .expect("uncorrelated palette input is safely consumed");
        assert!(pending_input.is_empty());
    }

    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, expected)
        .await
        .expect("matching palette response");
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, expected)
        .await
        .expect("duplicate palette response is safely consumed");
    assert!(pending_input.is_empty());

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, expected).await;
}

#[tokio::test]
async fn live_attach_palette_response_is_correlated_to_the_current_pane() {
    let handler = RequestHandler::new();
    let alpha = session_name("live-attach-palette-pane-correlation");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;
    let split = handler
        .handle(Request::SplitWindow(SplitWindowRequest {
            target: SplitWindowTarget::Session(alpha.clone()),
            direction: SplitDirection::Horizontal,
            before: false,
            environment: None,
        }))
        .await;
    assert!(matches!(split, Response::SplitWindow(_)), "{split:?}");

    let first = PaneTarget::with_window(alpha.clone(), 0, 0);
    let second = PaneTarget::with_window(alpha.clone(), 0, 1);
    let selected = handler
        .handle(Request::SelectPane(Box::new(SelectPaneRequest {
            target: first.clone(),
            title: None,
            style: None,
            input_disabled: None,
            preserve_zoom: false,
        })))
        .await;
    assert!(matches!(selected, Response::SelectPane(_)), "{selected:?}");
    {
        let mut state = handler.state.lock().await;
        state
            .append_bytes_to_pane_transcript_for_test(&alpha, 0, 0, b"\x1b]4;7;?\x07")
            .expect("first pane emits palette query");
        state.start_pane_input_capture_for_test(&first);
        state.start_pane_input_capture_for_test(&second);
    }

    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;
    let response = b"\x1b]4;7;rgb:1111/2222/3333\x1b\\";
    let selected = handler
        .handle(Request::SelectPane(Box::new(SelectPaneRequest {
            target: second.clone(),
            title: None,
            style: None,
            input_disabled: None,
            preserve_zoom: false,
        })))
        .await;
    assert!(matches!(selected, Response::SelectPane(_)), "{selected:?}");
    handler
        .handle_attached_live_input_for_test(requester_pid, response)
        .await
        .expect("response on wrong pane is safely consumed");

    let selected = handler
        .handle(Request::SelectPane(Box::new(SelectPaneRequest {
            target: first.clone(),
            title: None,
            style: None,
            input_disabled: None,
            preserve_zoom: false,
        })))
        .await;
    assert!(matches!(selected, Response::SelectPane(_)), "{selected:?}");
    handler
        .handle_attached_live_input_for_test(requester_pid, response)
        .await
        .expect("response reaches its queried pane");

    let state = handler.state.lock().await;
    assert_eq!(
        state.pane_input_capture_for_test(&first),
        Some(response.to_vec())
    );
    assert_eq!(state.pane_input_capture_for_test(&second), Some(Vec::new()));
}

#[tokio::test]
async fn concurrent_palette_responses_consume_exactly_one_query_slot() {
    let handler = RequestHandler::new();
    let alpha = session_name("live-attach-palette-concurrency");
    let first_pid = 91_001;
    let second_pid = 91_002;

    create_send_keys_test_session(&handler, &alpha).await;
    let mut control_receivers = Vec::new();
    for requester_pid in [first_pid, second_pid] {
        let (control_tx, control_rx) = mpsc::unbounded_channel();
        let _attach_id = handler
            .register_attach(requester_pid, alpha.clone(), control_tx)
            .await;
        control_receivers.push(control_rx);
    }
    let target = PaneTarget::new(alpha.clone(), 0);
    {
        let mut state = handler.state.lock().await;
        state
            .append_bytes_to_pane_transcript_for_test(&alpha, 0, 0, b"\x1b]4;7;?\x1b\\")
            .expect("pane emits one palette query");
        state.start_pane_input_capture_for_test(&target);
    }

    let response = b"\x1b]4;7;rgb:1111/2222/3333\x07";
    let mut first_pending = Vec::new();
    let mut second_pending = Vec::new();
    let (first_result, second_result) = tokio::join!(
        handler.handle_attached_live_input(first_pid, &mut first_pending, response),
        handler.handle_attached_live_input(second_pid, &mut second_pending, response),
    );
    first_result.expect("first concurrent response is handled");
    second_result.expect("second concurrent response is handled");
    assert!(first_pending.is_empty());
    assert!(second_pending.is_empty());

    let state = handler.state.lock().await;
    assert_eq!(
        state.pane_input_capture_for_test(&target),
        Some(response.to_vec()),
        "one query slot authorizes exactly one concurrent response"
    );
    drop(control_receivers);
}

#[tokio::test]
async fn ambiguous_alt_right_bracket_is_forwarded_after_escape_timeout() {
    let handler = RequestHandler::new();
    let alpha = session_name("live-alt-right-bracket");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;
    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;
    let capture = RawPaneInputProbe::start(&handler, &alpha, "live-alt-right-bracket", 0).await;

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b]")
        .await
        .expect("ambiguous OSC prefix waits for escape timeout");
    assert_eq!(pending_input, b"\x1b]");
    handler
        .flush_attached_pending_escape_input(requester_pid, &mut pending_input)
        .await
        .expect("timed-out Alt-] input is forwarded");
    assert!(pending_input.is_empty());

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, b"\x1b]").await;
}

#[tokio::test]
async fn alt_right_bracket_binding_fires_after_escape_timeout_and_unblocks_input() {
    let handler = RequestHandler::new();
    let alpha = session_name("live-alt-right-bracket-binding");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;
    let bound = handler
        .handle(Request::BindKey(Box::new(BindKeyRequest {
            table_name: "root".to_owned(),
            key: "M-]".to_owned(),
            note: None,
            repeat: false,
            command: Some(vec![
                "set-buffer".to_owned(),
                "-b".to_owned(),
                "flushed".to_owned(),
                "ok".to_owned(),
            ]),
        })))
        .await;
    assert!(matches!(bound, Response::BindKey(_)), "{bound:?}");
    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;
    let capture =
        RawPaneInputProbe::start(&handler, &alpha, "live-alt-right-bracket-binding", 0).await;

    // Alt+] followed by typed body bytes accumulates a full consumed-OSC
    // prefix with no terminator. If its streaming idle budget expires, the
    // flush must resolve it as keyboard input: dispatch M-] through the key
    // tables and reroute the body bytes, instead of opening an unterminated
    // OSC in the pane and swallowing every subsequent keystroke.
    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b]")
        .await
        .expect("ambiguous OSC prefix waits for escape timeout");
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"52;")
        .await
        .expect("typed body bytes stay retained");
    assert_eq!(pending_input, b"\x1b]52;");
    handler
        .flush_attached_pending_escape_input(requester_pid, &mut pending_input)
        .await
        .expect("timed-out M-] input dispatches the binding");
    assert!(pending_input.is_empty());
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"x")
        .await
        .expect("input after the flush is not swallowed");

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, b"52;x").await;
    let state = handler.state.lock().await;
    let (_, contents) = state
        .buffers
        .show(Some("flushed"))
        .expect("M-] binding fired from the idle-timeout flush");
    assert_eq!(contents, b"ok");
}

#[tokio::test]
async fn ambiguous_alt_right_bracket_dispatches_root_binding_after_escape_timeout() {
    let handler = RequestHandler::new();
    let alpha = session_name("live-alt-right-bracket-root-binding");
    let requester_pid = std::process::id();

    create_send_keys_test_session(&handler, &alpha).await;
    let rebound = handler
        .handle(Request::BindKey(Box::new(BindKeyRequest {
            table_name: "root".to_owned(),
            key: "M-]".to_owned(),
            note: Some("timed-out meta right bracket".to_owned()),
            repeat: false,
            command: Some(vec![
                "send-keys".to_owned(),
                "-l".to_owned(),
                "R".to_owned(),
            ]),
        })))
        .await;
    assert!(matches!(rebound, Response::BindKey(_)), "{rebound:?}");

    let (control_tx, _control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha.clone(), control_tx)
        .await;
    let capture =
        RawPaneInputProbe::start(&handler, &alpha, "live-alt-right-bracket-root-binding", 1).await;

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b]")
        .await
        .expect("ambiguous Alt-] waits for escape timeout");
    assert_eq!(pending_input, b"\x1b]");
    assert!(
        !handler
            .flush_attached_pending_escape_input(requester_pid, &mut pending_input)
            .await
            .expect("Alt-] escape timeout dispatches binding"),
        "bound Alt-] is consumed instead of forwarded",
    );
    assert!(pending_input.is_empty());

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, b"R").await;
}