bmux_pane_runtime_plugin 0.0.1-alpha.3

Shipped pane-runtime plugin for bmux (declares capabilities + typed services)
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
//! Typed handlers for the `attach-runtime-state` interface.
//!
//! State queries dispatch through the registered
//! `SessionRuntimeManagerHandle`. JSON-encoded payloads (layout,
//! snapshot, pane-images) serialize the relevant IPC structs so
//! consumers can decode them without the plugin having to invent a
//! parallel BPDL representation of every field.

use bmux_attach_layout_protocol::{
    AttachPaneChunk, AttachPaneInputMode, AttachPaneMouseProtocol, AttachScene, PaneLayoutNode,
    PaneSummary,
};
use bmux_pane_runtime_plugin_api::attach_runtime_state::{
    AttachLayout as AttachLayoutRecord, AttachPaneGridDelta, AttachPaneGridSnapshot,
    AttachPaneGridWindow, AttachPaneImages, AttachPaneOutputBatch,
    AttachPaneSnapshot as AttachPaneSnapshotRecord, AttachSnapshot as AttachSnapshotRecord,
    AttachStateError, PaneChunk, PaneGridDelta, PaneGridSnapshot, PaneGridWindow,
    PaneGridWindowRequest, PaneInputMode, PaneMouseProtocol, PaneOutputCursorRead,
    PaneScrollbackPin, PaneScrollbackUnpinAck,
};
use bmux_plugin_sdk::NativeServiceContext;
use bmux_session_models::{ClientId, SessionId};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistorySliceArgs {
    pub session_id: Uuid,
    pub pane_id: Uuid,
    pub pin_id: u64,
    pub capture_id: Uuid,
    pub line_index: u32,
    pub cell_offset: u32,
    pub max_columns: u32,
    pub max_text_bytes: u32,
}

pub fn history_slice(
    req: &HistorySliceArgs,
    ctx: &NativeServiceContext,
) -> Result<
    bmux_pane_runtime_plugin_api::attach_runtime_state::HistorySliceV1,
    bmux_pane_runtime_plugin_api::attach_runtime_state::HistoryFetchError,
> {
    use bmux_pane_runtime_plugin_api::attach_runtime_state::{HistoryEnd, HistoryFetchError};
    let handle = super::session_runtime_handle().ok_or_else(|| HistoryFetchError::Runtime {
        error: failed("runtime unavailable"),
    })?;
    let (encoded, next_cell_offset, end_kind) = handle
        .0
        .attach_history_slice(
            SessionId(req.session_id),
            caller_client_id(ctx),
            req.pane_id,
            (req.pin_id, req.capture_id),
            (req.line_index as usize, req.cell_offset as usize),
            (req.max_columns as usize, req.max_text_bytes as usize),
        )
        .map_err(|error| match error {
            bmux_pane_runtime_state::HistoryFetchError::StaleCapture => {
                HistoryFetchError::StaleCapture
            }
            bmux_pane_runtime_state::HistoryFetchError::Unavailable => {
                HistoryFetchError::Unavailable
            }
            bmux_pane_runtime_state::HistoryFetchError::InvalidOffset => {
                HistoryFetchError::InvalidOffset
            }
            bmux_pane_runtime_state::HistoryFetchError::BudgetExhausted => {
                HistoryFetchError::BudgetExhausted
            }
            bmux_pane_runtime_state::HistoryFetchError::Runtime(error) => {
                HistoryFetchError::Runtime {
                    error: attach_runtime_error(error),
                }
            }
        })?;
    let end_kind = match end_kind {
        bmux_pane_runtime_state::HistoryEnd::Continue => HistoryEnd::Continue,
        bmux_pane_runtime_state::HistoryEnd::HardBreak => HistoryEnd::HardBreak,
        bmux_pane_runtime_state::HistoryEnd::Open => HistoryEnd::Open,
    };
    Ok(
        bmux_pane_runtime_plugin_api::attach_runtime_state::HistorySliceV1 {
            encoded,
            next_cell_offset: next_cell_offset as u64,
            end_kind,
        },
    )
}

pub fn main_row_slice(
    req: &HistorySliceArgs,
    ctx: &NativeServiceContext,
) -> Result<
    bmux_pane_runtime_plugin_api::attach_runtime_state::HistorySliceV1,
    bmux_pane_runtime_plugin_api::attach_runtime_state::HistoryFetchError,
> {
    use bmux_pane_runtime_plugin_api::attach_runtime_state::{HistoryEnd, HistoryFetchError};
    let handle = super::session_runtime_handle().ok_or_else(|| HistoryFetchError::Runtime {
        error: failed("runtime unavailable"),
    })?;
    let (encoded, next_cell_offset, end_kind) = handle
        .0
        .attach_main_row_slice(
            SessionId(req.session_id),
            caller_client_id(ctx),
            req.pane_id,
            (req.pin_id, req.capture_id),
            (req.line_index as usize, req.cell_offset as usize),
            (req.max_columns as usize, req.max_text_bytes as usize),
        )
        .map_err(|error| match error {
            bmux_pane_runtime_state::HistoryFetchError::StaleCapture => {
                HistoryFetchError::StaleCapture
            }
            bmux_pane_runtime_state::HistoryFetchError::Unavailable => {
                HistoryFetchError::Unavailable
            }
            bmux_pane_runtime_state::HistoryFetchError::InvalidOffset => {
                HistoryFetchError::InvalidOffset
            }
            bmux_pane_runtime_state::HistoryFetchError::BudgetExhausted => {
                HistoryFetchError::BudgetExhausted
            }
            bmux_pane_runtime_state::HistoryFetchError::Runtime(error) => {
                HistoryFetchError::Runtime {
                    error: attach_runtime_error(error),
                }
            }
        })?;
    let end_kind = match end_kind {
        bmux_pane_runtime_state::HistoryEnd::Continue => HistoryEnd::Continue,
        bmux_pane_runtime_state::HistoryEnd::HardBreak => HistoryEnd::HardBreak,
        bmux_pane_runtime_state::HistoryEnd::Open => HistoryEnd::Open,
    };
    Ok(
        bmux_pane_runtime_plugin_api::attach_runtime_state::HistorySliceV1 {
            encoded,
            next_cell_offset: next_cell_offset as u64,
            end_kind,
        },
    )
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttachLayoutArgs {
    pub session_id: Uuid,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttachSnapshotArgs {
    pub session_id: Uuid,
    pub max_bytes_per_pane: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttachPaneSnapshotArgs {
    pub session_id: Uuid,
    pub pane_ids: Vec<Uuid>,
    pub max_bytes_per_pane: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttachPaneOutputBatchArgs {
    pub session_id: Uuid,
    pub pane_ids: Vec<Uuid>,
    pub max_bytes: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaneOutputCursorArgs {
    pub session_id: Uuid,
    pub pane_id: Uuid,
    pub cursor: u64,
    pub max_bytes: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaneGridSnapshotArgs {
    pub session_id: Uuid,
    pub pane_id: Uuid,
    pub max_rows: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttachPaneGridSnapshotArgs {
    pub session_id: Uuid,
    pub pane_ids: Vec<Uuid>,
    pub max_rows_per_pane: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttachPaneGridWindowArgs {
    pub session_id: Uuid,
    pub windows: Vec<PaneGridWindowRequest>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttachPaneScrollbackPinArgs {
    pub session_id: Uuid,
    pub pane_id: Uuid,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(
    clippy::struct_field_names,
    reason = "fields mirror the generated BPDL request contract"
)]
pub struct AttachPaneScrollbackUnpinArgs {
    pub session_id: Uuid,
    pub pane_id: Uuid,
    pub pin_id: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttachPaneGridDeltaArgs {
    pub session_id: Uuid,
    pub pane_ids: Vec<Uuid>,
    pub base_revisions: Vec<u64>,
    pub max_batches_per_pane: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttachPaneImagesArgs {
    pub session_id: Uuid,
    pub pane_ids: Vec<Uuid>,
    pub since_sequences: Vec<u64>,
}

fn failed(reason: impl Into<String>) -> AttachStateError {
    AttachStateError::Failed {
        reason: reason.into(),
    }
}

fn attach_runtime_error(error: bmux_pane_runtime_state::SessionRuntimeError) -> AttachStateError {
    match error {
        bmux_pane_runtime_state::SessionRuntimeError::NotAttached => AttachStateError::NotAttached,
        // Runtime lookup failures can refer to a pane or a session. Do not claim
        // that the session is missing when the runtime cannot distinguish them.
        error => failed(error.to_string()),
    }
}

#[cfg(test)]
mod tests {
    use super::attach_runtime_error;
    use bmux_pane_runtime_plugin_api::attach_runtime_state::AttachStateError;
    use bmux_pane_runtime_state::SessionRuntimeError;

    #[test]
    fn structured_runtime_errors_preserve_attachment_and_failure_distinction() {
        assert!(matches!(
            attach_runtime_error(SessionRuntimeError::NotAttached),
            AttachStateError::NotAttached
        ));
        for error in [
            SessionRuntimeError::NotFound,
            SessionRuntimeError::Closed,
            SessionRuntimeError::ResponseBudgetExceeded,
        ] {
            let AttachStateError::Failed { reason } = attach_runtime_error(error) else {
                panic!("runtime failure must not be reported as an attachment failure");
            };
            assert_eq!(reason, error.to_string());
        }
    }
}

fn caller_client_id(ctx: &NativeServiceContext) -> ClientId {
    ctx.caller_client_id
        .map_or_else(|| ClientId(Uuid::nil()), ClientId)
}

#[derive(Serialize)]
struct LayoutPayload {
    panes: Vec<PaneSummary>,
    layout_root: PaneLayoutNode,
    scene: AttachScene,
    zoomed: bool,
}

pub fn attach_layout_state(
    req: &AttachLayoutArgs,
    ctx: &NativeServiceContext,
) -> Result<AttachLayoutRecord, AttachStateError> {
    let handle = super::session_runtime_handle()
        .ok_or_else(|| failed("pane-runtime manager handle not registered"))?;
    let state = handle
        .0
        .attach_layout_state(SessionId(req.session_id), caller_client_id(ctx))
        .map_err(|_| AttachStateError::NotAttached)?;
    let payload = LayoutPayload {
        panes: state.panes,
        layout_root: state.layout_root,
        scene: state.scene,
        zoomed: state.zoomed,
    };
    let encoded =
        serde_json::to_vec(&payload).map_err(|e| failed(format!("encode layout payload: {e}")))?;
    Ok(AttachLayoutRecord {
        session_id: req.session_id,
        context_id: None,
        focused_pane_id: state.focused_pane_id,
        encoded,
    })
}

#[derive(Serialize)]
struct SnapshotLayoutPayload {
    panes: Vec<PaneSummary>,
    layout_root: PaneLayoutNode,
    scene: AttachScene,
}

pub fn attach_snapshot_state(
    req: &AttachSnapshotArgs,
    ctx: &NativeServiceContext,
) -> Result<AttachSnapshotRecord, AttachStateError> {
    let handle = super::session_runtime_handle()
        .ok_or_else(|| failed("pane-runtime manager handle not registered"))?;
    let state = handle
        .0
        .attach_snapshot_state(
            SessionId(req.session_id),
            caller_client_id(ctx),
            req.max_bytes_per_pane as usize,
        )
        .map_err(|_| AttachStateError::NotAttached)?;
    let layout_payload = SnapshotLayoutPayload {
        panes: state.panes,
        layout_root: state.layout_root,
        scene: state.scene,
    };
    let layout_encoded = serde_json::to_vec(&layout_payload)
        .map_err(|e| failed(format!("encode layout payload: {e}")))?;
    Ok(AttachSnapshotRecord {
        session_id: req.session_id,
        context_id: None,
        focused_pane_id: state.focused_pane_id,
        zoomed: state.zoomed,
        layout_encoded,
        chunks: state.chunks.into_iter().map(chunk_to_record).collect(),
        pane_mouse_protocols: state
            .pane_mouse_protocols
            .into_iter()
            .map(mouse_to_record)
            .collect(),
        pane_input_modes: state
            .pane_input_modes
            .into_iter()
            .map(input_mode_to_record)
            .collect(),
    })
}

pub fn attach_pane_snapshot_state(
    req: &AttachPaneSnapshotArgs,
    ctx: &NativeServiceContext,
) -> Result<AttachPaneSnapshotRecord, AttachStateError> {
    let handle = super::session_runtime_handle()
        .ok_or_else(|| failed("pane-runtime manager handle not registered"))?;
    let state = handle
        .0
        .attach_pane_snapshot_state(
            SessionId(req.session_id),
            caller_client_id(ctx),
            &req.pane_ids,
            req.max_bytes_per_pane as usize,
        )
        .map_err(|_| AttachStateError::NotAttached)?;
    Ok(AttachPaneSnapshotRecord {
        chunks: state.chunks.into_iter().map(chunk_to_record).collect(),
        pane_mouse_protocols: state
            .pane_mouse_protocols
            .into_iter()
            .map(mouse_to_record)
            .collect(),
        pane_input_modes: state
            .pane_input_modes
            .into_iter()
            .map(input_mode_to_record)
            .collect(),
    })
}

pub fn attach_pane_output_batch(
    req: &AttachPaneOutputBatchArgs,
    ctx: &NativeServiceContext,
) -> Result<AttachPaneOutputBatch, AttachStateError> {
    let handle = super::session_runtime_handle()
        .ok_or_else(|| failed("pane-runtime manager handle not registered"))?;
    let (chunks, output_still_pending) = handle.0.attach_pane_output_batch_with_dirty_check(
        SessionId(req.session_id),
        caller_client_id(ctx),
        &req.pane_ids,
        req.max_bytes as usize,
    );
    let chunks = chunks.map_err(|_| AttachStateError::NotAttached)?;
    Ok(AttachPaneOutputBatch {
        chunks: chunks.into_iter().map(chunk_to_record).collect(),
        output_still_pending,
    })
}

pub fn pane_output_cursor_state(
    req: &PaneOutputCursorArgs,
) -> Result<PaneOutputCursorRead, AttachStateError> {
    let handle = super::session_runtime_handle()
        .ok_or_else(|| failed("pane-runtime manager handle not registered"))?;
    let read = handle
        .0
        .read_pane_output_at(
            SessionId(req.session_id),
            req.pane_id,
            req.cursor,
            usize::try_from(req.max_bytes).unwrap_or(usize::MAX),
        )
        .map_err(|error| failed(error.to_string()))?;
    Ok(PaneOutputCursorRead {
        data: read.bytes,
        retained_start: read.retained_start,
        stream_start: read.stream_start,
        stream_end: read.stream_end,
        source_end: read.source_end,
        stream_gap: read.stream_gap,
    })
}

pub fn pane_grid_snapshot_state(
    req: &PaneGridSnapshotArgs,
) -> Result<PaneGridSnapshot, AttachStateError> {
    let handle = super::session_runtime_handle()
        .ok_or_else(|| failed("pane-runtime manager handle not registered"))?;
    let state = handle
        .0
        .attach_grid_snapshot_state(
            SessionId(req.session_id),
            ClientId(Uuid::nil()),
            &[req.pane_id],
            usize::try_from(req.max_rows).unwrap_or(usize::MAX),
        )
        .map_err(|error| failed(error.to_string()))?;
    state
        .snapshots
        .into_iter()
        .find(|snapshot| snapshot.pane_id == req.pane_id)
        .map(|snapshot| PaneGridSnapshot {
            pane_id: snapshot.pane_id,
            stream_end: snapshot.stream_end,
            encoded: snapshot.encoded,
        })
        .ok_or_else(|| failed("pane snapshot is missing"))
}

pub fn attach_pane_grid_snapshot_state(
    req: &AttachPaneGridSnapshotArgs,
    ctx: &NativeServiceContext,
) -> Result<AttachPaneGridSnapshot, AttachStateError> {
    let handle = super::session_runtime_handle()
        .ok_or_else(|| failed("pane-runtime manager handle not registered"))?;
    let state = handle
        .0
        .attach_grid_snapshot_state(
            SessionId(req.session_id),
            caller_client_id(ctx),
            &req.pane_ids,
            req.max_rows_per_pane as usize,
        )
        .map_err(attach_runtime_error)?;
    Ok(AttachPaneGridSnapshot {
        snapshots: state
            .snapshots
            .into_iter()
            .map(|snapshot| PaneGridSnapshot {
                pane_id: snapshot.pane_id,
                stream_end: snapshot.stream_end,
                encoded: snapshot.encoded,
            })
            .collect(),
    })
}

pub fn attach_pane_grid_window_state(
    req: &AttachPaneGridWindowArgs,
    ctx: &NativeServiceContext,
) -> Result<AttachPaneGridWindow, AttachStateError> {
    let handle = super::session_runtime_handle()
        .ok_or_else(|| failed("pane-runtime manager handle not registered"))?;
    let windows = req
        .windows
        .iter()
        .map(
            |window| bmux_pane_runtime_state::AttachPaneGridWindowRequest {
                pane_id: window.pane_id,
                scrollback_offset: window.scrollback_offset as usize,
                rows: window.rows as usize,
                anchor_total_scrolled_rows: window.anchor_total_scrolled_rows,
                pin_id: window.pin_id,
            },
        )
        .collect::<Vec<_>>();
    let state = handle
        .0
        .attach_grid_window_state(SessionId(req.session_id), caller_client_id(ctx), &windows)
        .map_err(attach_runtime_error)?;
    Ok(AttachPaneGridWindow {
        windows: state
            .windows
            .into_iter()
            .map(|window| PaneGridWindow {
                pane_id: window.pane_id,
                scrollback_offset: u32::try_from(window.scrollback_offset).unwrap_or(u32::MAX),
                max_scrollback_offset: u32::try_from(window.max_scrollback_offset)
                    .unwrap_or(u32::MAX),
                total_scrolled_rows: window.total_scrolled_rows,
                anchor_delta_rows: u32::try_from(window.anchor_delta_rows).unwrap_or(u32::MAX),
                anchor_clamped: window.anchor_clamped,
                stream_end: window.stream_end,
                encoded: window.encoded,
            })
            .collect(),
    })
}

pub fn attach_pane_scrollback_pin(
    req: &AttachPaneScrollbackPinArgs,
    ctx: &NativeServiceContext,
) -> Result<PaneScrollbackPin, AttachStateError> {
    let handle = super::session_runtime_handle()
        .ok_or_else(|| failed("pane-runtime manager handle not registered"))?;
    let pin = handle
        .0
        .attach_scrollback_pin(
            SessionId(req.session_id),
            caller_client_id(ctx),
            req.pane_id,
        )
        .map_err(attach_runtime_error)?;
    Ok(PaneScrollbackPin {
        pane_id: pin.pane_id,
        pin_id: pin.pin_id,
        total_scrolled_rows: pin.total_scrolled_rows,
        max_scrollback_offset: u32::try_from(pin.max_scrollback_offset).unwrap_or(u32::MAX),
        stream_end: pin.stream_end,
    })
}

pub fn history_capture(
    req: &AttachPaneScrollbackPinArgs,
    ctx: &NativeServiceContext,
) -> Result<bmux_pane_runtime_plugin_api::attach_runtime_state::HistoryCaptureV1, AttachStateError>
{
    let handle = super::session_runtime_handle().ok_or_else(|| failed("runtime unavailable"))?;
    let captured = handle
        .0
        .attach_scrollback_pin(
            SessionId(req.session_id),
            caller_client_id(ctx),
            req.pane_id,
        )
        .map_err(attach_runtime_error)?;
    Ok(
        bmux_pane_runtime_plugin_api::attach_runtime_state::HistoryCaptureV1 {
            width: captured.width,
            height: captured.height,
            history_truncated: captured.history_truncated,
            history_line_count: captured.history_line_count as u64,
            capture_id: captured.capture_id,
            pin: PaneScrollbackPin {
                pane_id: captured.pane_id,
                pin_id: captured.pin_id,
                total_scrolled_rows: captured.total_scrolled_rows,
                max_scrollback_offset: u32::try_from(captured.max_scrollback_offset)
                    .unwrap_or(u32::MAX),
                stream_end: captured.stream_end,
            },
        },
    )
}

pub fn attach_pane_scrollback_unpin(
    req: &AttachPaneScrollbackUnpinArgs,
    ctx: &NativeServiceContext,
) -> Result<PaneScrollbackUnpinAck, AttachStateError> {
    let handle = super::session_runtime_handle()
        .ok_or_else(|| failed("pane-runtime manager handle not registered"))?;
    let ack = handle
        .0
        .attach_scrollback_unpin(
            SessionId(req.session_id),
            caller_client_id(ctx),
            req.pane_id,
            req.pin_id,
        )
        .map_err(attach_runtime_error)?;
    Ok(PaneScrollbackUnpinAck {
        pane_id: ack.pane_id,
        pin_id: ack.pin_id,
        released: ack.released,
    })
}

/// `attach-pane-grid-delta-state` delivers revisioned structured grid
/// deltas serialized as JSON (`Vec<GridDeltaBatch>`) per pane.
pub fn attach_pane_grid_delta_state(
    req: &AttachPaneGridDeltaArgs,
    ctx: &NativeServiceContext,
) -> Result<AttachPaneGridDelta, AttachStateError> {
    let handle = super::session_runtime_handle()
        .ok_or_else(|| failed("pane-runtime manager handle not registered"))?;
    let state = handle
        .0
        .attach_grid_delta_state(
            SessionId(req.session_id),
            caller_client_id(ctx),
            &req.pane_ids,
            &req.base_revisions,
            req.max_batches_per_pane as usize,
        )
        .map_err(attach_runtime_error)?;
    Ok(AttachPaneGridDelta {
        deltas: state
            .deltas
            .into_iter()
            .map(|delta| PaneGridDelta {
                pane_id: delta.pane_id,
                base_revision: delta.base_revision,
                revision: delta.revision,
                desynced: delta.desynced,
                encoded: delta.encoded,
            })
            .collect(),
    })
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryImagesArgs {
    pub session_id: Uuid,
    pub pane_id: Uuid,
    pub pin_id: u64,
    pub capture_id: Uuid,
    pub width: u16,
    pub scrollback_offset: u32,
    pub rows: u16,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnchoredHistoryImagesArgs {
    pub session_id: Uuid,
    pub pane_id: Uuid,
    pub pin_id: u64,
    pub capture_id: Uuid,
    pub width: u16,
    pub line_index: u32,
    pub column: u32,
    pub rows: u16,
}

pub fn history_images_anchored(
    req: &AnchoredHistoryImagesArgs,
    ctx: &NativeServiceContext,
) -> Result<bmux_pane_runtime_plugin_api::attach_runtime_state::HistoryImagesV1, AttachStateError> {
    let encoded = crate::runtime::captured_image_window_anchored(req, caller_client_id(ctx))
        .map_err(|error| failed(error.to_string()))?;
    Ok(
        bmux_pane_runtime_plugin_api::attach_runtime_state::HistoryImagesV1 {
            capture_id: req.capture_id,
            encoded,
        },
    )
}

pub fn history_images(
    req: &HistoryImagesArgs,
    ctx: &NativeServiceContext,
) -> Result<bmux_pane_runtime_plugin_api::attach_runtime_state::HistoryImagesV1, AttachStateError> {
    let encoded = crate::runtime::captured_image_window(req, caller_client_id(ctx))
        .map_err(|error| failed(error.to_string()))?;
    Ok(
        bmux_pane_runtime_plugin_api::attach_runtime_state::HistoryImagesV1 {
            capture_id: req.capture_id,
            encoded,
        },
    )
}

pub fn history_images_reflowed(
    req: &HistoryImagesArgs,
    ctx: &NativeServiceContext,
) -> Result<bmux_pane_runtime_plugin_api::attach_runtime_state::HistoryImagesV1, AttachStateError> {
    let encoded = crate::runtime::captured_image_window_reflowed(req, caller_client_id(ctx))
        .map_err(|error| failed(error.to_string()))?;
    Ok(
        bmux_pane_runtime_plugin_api::attach_runtime_state::HistoryImagesV1 {
            capture_id: req.capture_id,
            encoded,
        },
    )
}

pub fn attach_pane_images(
    req: &AttachPaneImagesArgs,
    _ctx: &NativeServiceContext,
) -> Result<AttachPaneImages, AttachStateError> {
    let handle = super::session_runtime_handle()
        .ok_or_else(|| failed("pane-runtime manager handle not registered"))?;
    let deltas = handle.0.attach_pane_image_deltas(
        SessionId(req.session_id),
        &req.pane_ids,
        &req.since_sequences,
        None,
    );
    let encoded = serde_json::to_vec(&deltas)
        .map_err(|e| failed(format!("encode pane-images deltas: {e}")))?;
    Ok(AttachPaneImages { encoded })
}

fn chunk_to_record(chunk: AttachPaneChunk) -> PaneChunk {
    PaneChunk {
        pane_id: chunk.pane_id,
        data: chunk.data,
        stream_start: chunk.stream_start,
        stream_end: chunk.stream_end,
        stream_gap: chunk.stream_gap,
        sync_update_active: chunk.sync_update_active,
    }
}

fn mouse_to_record(mouse: AttachPaneMouseProtocol) -> PaneMouseProtocol {
    let encoded = serde_json::to_vec(&mouse.protocol).unwrap_or_default();
    PaneMouseProtocol {
        pane_id: mouse.pane_id,
        encoded,
    }
}

fn input_mode_to_record(mode: AttachPaneInputMode) -> PaneInputMode {
    let encoded = serde_json::to_vec(&mode.mode).unwrap_or_default();
    PaneInputMode {
        pane_id: mode.pane_id,
        encoded,
    }
}