gate4agent 0.2.10

Universal transport library for 5 CLI AI agents (Claude Code, Codex, Gemini, Cursor, OpenCode). Pipe and PTY transports. TransportSession is a thin router over PipeSession.
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
//! Multi-CLI agent session manager.
//!
//! `MultiCliManager` owns PTY and pipe sessions for Claude, Codex, and Gemini
//! simultaneously. Call `drain_events` every frame; call `snapshot(cli)` to
//! get a render-safe snapshot for the active CLI.
//!
//! ## Architecture
//!
//! Internally the manager stores instances in a `HashMap<InstanceId, AgentInstance>`.
//! Three "legacy" instances (one per `AgentCli`) are pre-created in `new()` so that
//! all existing callers using the `cli: AgentCli` API continue to work unchanged.

use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

use tokio::sync::broadcast;
use uuid::Uuid;

use crate::pty::PtySession;
use crate::pty::snapshot::{
    AgentCli, AgentRenderSnapshot, AgentSnapshotMode, ChatMessage, ChatRole, LiveStatus, TermCell, TermGrid,
};
use crate::transport::{SpawnOptions, TransportSession};
use crate::core::types::{AgentEvent, CliTool, SessionConfig};

// =============================================================================
// InstanceId
// =============================================================================

/// Opaque identifier for a per-instance agent session.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct InstanceId(pub Uuid);

impl InstanceId {
    /// Create a new random instance ID.
    pub fn new() -> Self {
        Self(Uuid::new_v4())
    }
}

impl Default for InstanceId {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// InstanceMode
// =============================================================================

/// Operating mode for an agent instance.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum InstanceMode {
    /// PTY mirror mode — spawns agent in a real pseudo-terminal.
    Pty,
    /// Chat/pipe mode — communicates via stdin/stdout with NDJSON events.
    /// Supported for all 6 CLIs via `TransportSession`.
    Chat,
}

// =============================================================================
// ManagerConfig
// =============================================================================

/// Configuration passed to `MultiCliManager::new`.
///
/// Each CLI runs in `{sessions_dir}/{cli_name}/` as cwd, so its dotfolder ends
/// up there (e.g. `.claude/` for Claude Code).
pub struct ManagerConfig {
    /// Root directory. Each CLI gets a subdirectory: `{sessions_dir}/{cli_name}/`.
    pub sessions_dir: PathBuf,
    /// Initial PTY width in columns.
    pub default_cols: u16,
    /// Initial PTY height in rows.
    pub default_rows: u16,
}

impl Default for ManagerConfig {
    fn default() -> Self {
        Self {
            sessions_dir: default_sessions_dir(),
            default_cols: 80,
            default_rows: 24,
        }
    }
}

/// Returns `%APPDATA%/zengeld/agent-sessions` on Windows, or a `data/zengeld/agent-sessions`
/// fallback on other platforms.
fn default_sessions_dir() -> PathBuf {
    #[cfg(target_os = "windows")]
    {
        if let Some(appdata) = std::env::var_os("APPDATA") {
            return PathBuf::from(appdata).join("zengeld").join("agent-sessions");
        }
    }
    PathBuf::from("data").join("zengeld").join("agent-sessions")
}

// =============================================================================
// Color helpers
// =============================================================================

fn vt100_color_to_rgb(color: vt100::Color, default_rgb: [u8; 3]) -> [u8; 3] {
    match color {
        vt100::Color::Default => default_rgb,
        vt100::Color::Rgb(r, g, b) => [r, g, b],
        vt100::Color::Idx(idx) => ansi_idx_to_rgb(idx),
    }
}

fn ansi_idx_to_rgb(idx: u8) -> [u8; 3] {
    const STANDARD_16: [[u8; 3]; 16] = [
        [0, 0, 0],
        [128, 0, 0],
        [0, 128, 0],
        [128, 128, 0],
        [0, 0, 128],
        [128, 0, 128],
        [0, 128, 128],
        [192, 192, 192],
        [128, 128, 128],
        [255, 0, 0],
        [0, 255, 0],
        [255, 255, 0],
        [0, 0, 255],
        [255, 0, 255],
        [0, 255, 255],
        [255, 255, 255],
    ];
    if (idx as usize) < STANDARD_16.len() {
        return STANDARD_16[idx as usize];
    }
    if (16..=231).contains(&idx) {
        let v = idx - 16;
        let b = v % 6;
        let g = (v / 6) % 6;
        let r = v / 36;
        let to_u8 = |x: u8| if x == 0 { 0 } else { 55 + x * 40 };
        return [to_u8(r), to_u8(g), to_u8(b)];
    }
    let gray = 8 + (idx - 232) * 10;
    [gray, gray, gray]
}

// =============================================================================
// AgentInstance (internal)
// =============================================================================

/// Internal state for a single agent instance. Not public.
struct AgentInstance {
    /// Stored for diagnostic / future introspection — the canonical id lives as the map key.
    _id: InstanceId,
    cli: AgentCli,
    mode: InstanceMode,
    workdir: PathBuf,
    pty_session: Option<PtySession>,
    /// Phase 5: unified pipe/daemon session via `TransportSession`.
    transport_session: Option<TransportSession>,
    pty_rx: Option<broadcast::Receiver<AgentEvent>>,
    pipe_rx: Option<broadcast::Receiver<AgentEvent>>,
    pty_parser: vt100::Parser,
    /// Transient live-stream buffer for the currently running session.
    /// Cleared when a past session is loaded for display.
    chat_messages: Vec<ChatMessage>,
    session_active: bool,
    pipe_session_id: Option<String>,
    /// Live status of the current turn — drives the animated spinner line in the chat view.
    live_status: LiveStatus,
}

impl AgentInstance {
    fn new(id: InstanceId, cli: AgentCli, mode: InstanceMode, workdir: PathBuf, rows: u16, cols: u16) -> Self {
        Self {
            _id: id,
            cli,
            mode,
            workdir,
            pty_session: None,
            transport_session: None,
            pty_rx: None,
            pipe_rx: None,
            pty_parser: vt100::Parser::new(rows, cols, 0),
            chat_messages: Vec::new(),
            session_active: false,
            pipe_session_id: None,
            live_status: LiveStatus::Idle,
        }
    }
}

// =============================================================================
// MultiCliManager
// =============================================================================

/// Manages agent sessions for all CLIs simultaneously.
///
/// Internally uses a `HashMap<InstanceId, AgentInstance>`. Three legacy
/// instances (one per `AgentCli`) are pre-created in `new()` so that all
/// existing callers using the `cli: AgentCli` API continue to work unchanged.
pub struct MultiCliManager {
    config: ManagerConfig,
    instances: HashMap<InstanceId, AgentInstance>,
    /// Pre-created legacy slots: index 0 = Claude, 1 = Codex, 2 = Gemini.
    legacy_instances: [InstanceId; 3],
    cols: u16,
    rows: u16,
}

impl MultiCliManager {
    /// Create a new manager with the given configuration.
    pub fn new(config: ManagerConfig) -> Self {
        let cols = config.default_cols;
        let rows = config.default_rows;

        let mut instances = HashMap::new();

        let claude_id = InstanceId::new();
        let codex_id = InstanceId::new();
        let gemini_id = InstanceId::new();

        let claude_workdir = config.sessions_dir.join(AgentCli::Claude.as_str());
        let codex_workdir = config.sessions_dir.join(AgentCli::Codex.as_str());
        let gemini_workdir = config.sessions_dir.join(AgentCli::Gemini.as_str());

        instances.insert(claude_id, AgentInstance::new(claude_id, AgentCli::Claude, InstanceMode::Pty, claude_workdir, rows, cols));
        instances.insert(codex_id, AgentInstance::new(codex_id, AgentCli::Codex, InstanceMode::Pty, codex_workdir, rows, cols));
        instances.insert(gemini_id, AgentInstance::new(gemini_id, AgentCli::Gemini, InstanceMode::Pty, gemini_workdir, rows, cols));

        Self {
            config,
            instances,
            legacy_instances: [claude_id, codex_id, gemini_id],
            cols,
            rows,
        }
    }

    // =========================================================================
    // Legacy index helpers
    // =========================================================================

    fn legacy_idx(cli: AgentCli) -> usize {
        match cli {
            AgentCli::Claude => 0,
            AgentCli::Codex => 1,
            AgentCli::Gemini => 2,
            // OpenCode does not have a legacy slot — it is managed through
            // the per-instance API. Map to slot 0 as a safe fallback;
            // callers must not pass it to legacy methods.
            AgentCli::OpenCode => 0,
        }
    }

    fn legacy_id(&self, cli: AgentCli) -> InstanceId {
        self.legacy_instances[Self::legacy_idx(cli)]
    }

    // =========================================================================
    // New per-instance API
    // =========================================================================

    /// Register a new agent instance. Does NOT spawn any process.
    ///
    /// All 6 CLIs support Chat mode via `TransportSession` (pipe/daemon).
    pub fn create_instance(
        &mut self,
        cli: AgentCli,
        mode: InstanceMode,
        workdir: PathBuf,
    ) -> Result<InstanceId, String> {
        let id = InstanceId::new();
        let inst = AgentInstance::new(id, cli, mode, workdir, self.rows, self.cols);
        self.instances.insert(id, inst);
        Ok(id)
    }

    /// Stop and remove an instance from the manager.
    pub async fn remove_instance(&mut self, id: InstanceId) {
        self.stop_instance(id).await;
        self.instances.remove(&id);
    }

    /// Start a PTY session for the given instance.
    pub async fn start_pty_instance(
        &mut self,
        id: InstanceId,
        config: SessionConfig,
    ) -> Result<(), String> {
        let rows = self.rows;
        let cols = self.cols;
        let inst = self
            .instances
            .get_mut(&id)
            .ok_or_else(|| format!("Instance {:?} not found", id))?;
        if inst.session_active {
            return Err(format!("Instance {:?} session already active", id));
        }
        match PtySession::spawn_with_size(config, rows, cols).await {
            Ok(session) => {
                inst.pty_rx = Some(session.subscribe());
                inst.pty_parser = vt100::Parser::new(rows, cols, 0);
                inst.pty_session = Some(session);
                inst.session_active = true;
                Ok(())
            }
            Err(e) => Err(format!("Failed to spawn PTY for instance {:?}: {}", id, e)),
        }
    }

    /// Write a string to the active PTY for the given instance.
    ///
    /// Lazy-spawns a PTY session on the first call.
    pub async fn write_pty_instance(&mut self, id: InstanceId, text: &str) -> Result<(), String> {
        let need_spawn = self
            .instances
            .get(&id)
            .ok_or_else(|| format!("Instance {:?} not found", id))?
            .pty_session
            .is_none();

        if need_spawn {
            let (cli, workdir) = {
                let inst = self
                    .instances
                    .get(&id)
                    .ok_or_else(|| format!("Instance {:?} not found", id))?;
                (inst.cli, inst.workdir.clone())
            };
            fs::create_dir_all(&workdir).map_err(|e| format!("mkdir error: {}", e))?;
            eprintln!(
                "[gate4agent] write_pty_instance lazy-spawn id={:?} cwd={}",
                id,
                workdir.display()
            );
            let tool = cli_to_tool(cli);
            let config = SessionConfig {
                tool,
                working_dir: workdir,
                ..SessionConfig::default()
            };
            self.start_pty_instance(id, config).await?;
        }

        let inst = self
            .instances
            .get(&id)
            .ok_or_else(|| format!("Instance {:?} not found", id))?;
        eprintln!(
            "[gate4agent] write_pty_instance id={:?} bytes={}",
            id,
            text.len()
        );
        if let Some(ref session) = inst.pty_session {
            session
                .write(text)
                .await
                .map_err(|e| format!("PTY write error (instance {:?}): {}", id, e))
        } else {
            Err(format!("No active PTY session for instance {:?}", id))
        }
    }

    /// Send a chat prompt to the pipe session for the given instance.
    ///
    /// Only valid if the instance mode is `Chat`. Lazy-spawns on first call.
    pub async fn send_chat_instance(&mut self, id: InstanceId, prompt: &str) -> Result<(), String> {
        {
            let inst = self
                .instances
                .get(&id)
                .ok_or_else(|| format!("Instance {:?} not found", id))?;
            if inst.mode != InstanceMode::Chat {
                return Err(format!(
                    "Instance {:?} is in {:?} mode, not Chat",
                    id, inst.mode
                ));
            }
        }

        let need_spawn = self
            .instances
            .get(&id)
            .ok_or_else(|| format!("Instance {:?} not found", id))?
            .transport_session
            .is_none();

        eprintln!(
            "[gate4agent] send_chat_instance id={:?} need_spawn={} prompt_len={}",
            id,
            need_spawn,
            prompt.len()
        );

        if need_spawn {
            let (cli, workdir, resume_id) = {
                let inst = self
                    .instances
                    .get(&id)
                    .ok_or_else(|| format!("Instance {:?} not found", id))?;
                (inst.cli, inst.workdir.clone(), inst.pipe_session_id.clone())
            };
            fs::create_dir_all(&workdir).map_err(|e| format!("mkdir error: {}", e))?;
            eprintln!(
                "[gate4agent] send_chat_instance lazy-spawn id={:?} cwd={}",
                id,
                workdir.display()
            );
            let tool = cli_to_tool(cli);
            {
                let inst = self
                    .instances
                    .get_mut(&id)
                    .ok_or_else(|| format!("Instance {:?} not found", id))?;
                inst.chat_messages.push(ChatMessage {
                    role: ChatRole::User,
                    content: prompt.to_string(),
                    tool_name: None,
                });
                inst.live_status = LiveStatus::Thinking;
            }
            let opts = SpawnOptions {
                resume_session_id: resume_id,
                ..SpawnOptions::default()
            };
            match TransportSession::spawn(tool, &workdir, prompt, opts).await {
                Ok(session) => {
                    let inst = self
                        .instances
                        .get_mut(&id)
                        .ok_or_else(|| format!("Instance {:?} not found", id))?;
                    inst.pipe_rx = Some(session.subscribe());
                    inst.transport_session = Some(session);
                    inst.session_active = true;
                    Ok(())
                }
                Err(e) => Err(format!("Failed to spawn pipe for instance {:?}: {}", id, e)),
            }
        } else {
            let inst = self
                .instances
                .get_mut(&id)
                .ok_or_else(|| format!("Instance {:?} not found", id))?;
            inst.chat_messages.push(ChatMessage {
                role: ChatRole::User,
                content: prompt.to_string(),
                tool_name: None,
            });
            inst.live_status = LiveStatus::Thinking;
            if let Some(ref session) = inst.transport_session {
                session
                    .send_prompt(prompt)
                    .await
                    .map_err(|e| format!("Pipe send error (instance {:?}): {}", id, e))
            } else {
                Err(format!("No active pipe session for instance {:?}", id))
            }
        }
    }

    /// Stop the session for a single instance.
    pub async fn stop_instance(&mut self, id: InstanceId) {
        if let Some(inst) = self.instances.get_mut(&id) {
            if let Some(session) = inst.pty_session.take() {
                let _ = session.kill().await;
            }
            if let Some(session) = inst.transport_session.take() {
                let _ = session.kill().await;
            }
            inst.pty_rx = None;
            inst.pipe_rx = None;
            inst.session_active = false;
        }
    }

    /// Build a render snapshot for the given instance.
    ///
    /// Returns `None` if the instance doesn't exist. Otherwise builds a PTY or
    /// Chat snapshot based on the instance's mode.
    pub fn snapshot_instance(&self, id: InstanceId) -> Option<AgentRenderSnapshot> {
        let inst = self.instances.get(&id)?;
        let snap = match inst.mode {
            InstanceMode::Pty => {
                if inst.pty_session.is_some() || self.instance_pty_has_content(inst) {
                    self.build_pty_snapshot_from_instance(inst)
                } else {
                    AgentRenderSnapshot {
                        mode: AgentSnapshotMode::Idle,
                        session_active: inst.session_active,
                        live_status: inst.live_status.clone(),
                    }
                }
            }
            InstanceMode::Chat => {
                if !inst.chat_messages.is_empty() {
                    AgentRenderSnapshot {
                        mode: AgentSnapshotMode::Chat(inst.chat_messages.clone()),
                        session_active: inst.session_active,
                        live_status: inst.live_status.clone(),
                    }
                } else {
                    AgentRenderSnapshot {
                        mode: AgentSnapshotMode::Idle,
                        session_active: inst.session_active,
                        live_status: inst.live_status.clone(),
                    }
                }
            }
        };
        Some(snap)
    }

    /// Returns `true` if the instance is active.
    pub fn is_instance_active(&self, id: InstanceId) -> bool {
        self.instances
            .get(&id)
            .map(|i| i.session_active)
            .unwrap_or(false)
    }

    /// Returns all registered instance IDs.
    pub fn list_instances(&self) -> Vec<InstanceId> {
        self.instances.keys().copied().collect()
    }

    /// Returns the `AgentCli` for an instance.
    pub fn instance_cli(&self, id: InstanceId) -> Option<AgentCli> {
        self.instances.get(&id).map(|i| i.cli)
    }

    /// Returns the `InstanceMode` for an instance.
    pub fn instance_mode(&self, id: InstanceId) -> Option<InstanceMode> {
        self.instances.get(&id).map(|i| i.mode)
    }

    /// Returns the working directory for an instance.
    pub fn instance_workdir(&self, id: InstanceId) -> Option<&Path> {
        self.instances.get(&id).map(|i| i.workdir.as_path())
    }

    /// Resize the PTY for a single instance.
    pub async fn resize_instance(&mut self, id: InstanceId, cols: u16, rows: u16) {
        if let Some(inst) = self.instances.get_mut(&id) {
            inst.pty_parser.set_size(rows, cols);
            if let Some(ref session) = inst.pty_session {
                let _ = session.resize(rows, cols).await;
            }
        }
    }

    /// Load the latest history for an instance. Chat-mode only.
    pub fn load_latest_history_instance(&mut self, id: InstanceId) -> bool {
        let workdir = match self.instances.get(&id).map(|i| i.workdir.clone()) {
            Some(w) => w,
            None => return false,
        };
        let cli = match self.instances.get(&id).map(|i| i.cli) {
            Some(c) => c,
            None => return false,
        };
        let reader = crate::history::reader_for(cli);
        let latest = match reader.latest_session(&workdir) {
            Some(id_str) => id_str,
            None => return false,
        };
        let messages = reader.load_session(&workdir, &latest);
        if messages.is_empty() {
            return false;
        }
        if let Some(inst) = self.instances.get_mut(&id) {
            inst.chat_messages = messages;
            inst.pipe_session_id = Some(latest);
            inst.transport_session = None;
            inst.pipe_rx = None;
        }
        true
    }

    /// Clear chat messages for an instance, starting a fresh session (display only).
    ///
    /// Stops the running transport session (if any) and wipes the message list.
    /// The next `send_chat_instance` will begin a brand-new CLI session.
    pub fn clear_chat_instance(&mut self, id: InstanceId) {
        if let Some(inst) = self.instances.get_mut(&id) {
            inst.chat_messages.clear();
            inst.pipe_session_id = None;
            inst.transport_session = None;
            inst.pipe_rx = None;
            inst.session_active = false;
        }
    }

    /// List past sessions for an instance by its workdir (newest-first, live disk read).
    pub fn list_past_sessions_instance(&self, id: InstanceId) -> Vec<crate::history::SessionMeta> {
        let inst = match self.instances.get(&id) {
            Some(i) => i,
            None => return Vec::new(),
        };
        crate::history::reader_for(inst.cli).list_sessions(&inst.workdir)
    }

    /// Load a specific history session for an instance. Chat-mode only.
    pub fn load_history_instance(&mut self, id: InstanceId, session_id: &str) -> bool {
        let workdir = match self.instances.get(&id).map(|i| i.workdir.clone()) {
            Some(w) => w,
            None => return false,
        };
        let cli = match self.instances.get(&id).map(|i| i.cli) {
            Some(c) => c,
            None => return false,
        };
        let reader = crate::history::reader_for(cli);
        let messages = reader.load_session(&workdir, session_id);
        if messages.is_empty() {
            return false;
        }
        if let Some(inst) = self.instances.get_mut(&id) {
            inst.chat_messages = messages;
            inst.pipe_session_id = Some(session_id.to_string());
            inst.transport_session = None;
            inst.pipe_rx = None;
        }
        true
    }

    // =========================================================================
    // Legacy 3-CLI public API (shims onto per-instance API)
    // =========================================================================

    /// Returns the working directory for a given CLI.
    ///
    /// Each CLI runs isolated in `{sessions_dir}/{cli_name}/` so its dotfolder
    /// (`.claude/`, `.codex/`, `.gemini/`) lives there.
    pub fn cli_workdir(&self, cli: AgentCli) -> PathBuf {
        self.config.sessions_dir.join(cli.as_str())
    }

    /// Start a PTY session for the given CLI.
    pub async fn start_pty(&mut self, cli: AgentCli, config: SessionConfig) -> Result<(), String> {
        eprintln!(
            "[gate4agent] start_pty cli={:?} cwd={}",
            cli,
            config.working_dir.display()
        );
        let id = self.legacy_id(cli);
        let rows = self.rows;
        let cols = self.cols;
        let inst = self
            .instances
            .get_mut(&id)
            .ok_or_else(|| format!("Legacy instance for {:?} not found", cli))?;
        if inst.session_active {
            return Err(format!("{:?} session already active", cli));
        }
        match PtySession::spawn_with_size(config, rows, cols).await {
            Ok(session) => {
                inst.pty_rx = Some(session.subscribe());
                inst.pty_parser = vt100::Parser::new(rows, cols, 0);
                inst.pty_session = Some(session);
                inst.session_active = true;
                Ok(())
            }
            Err(e) => Err(format!("Failed to spawn PTY for {:?}: {}", cli, e)),
        }
    }

    /// Start a Pipe/Chat session for the given CLI.
    ///
    /// Deprecated: prefer `send_chat` which lazy-spawns on the first message.
    pub async fn start_pipe(
        &mut self,
        cli: AgentCli,
        config: SessionConfig,
        prompt: &str,
    ) -> Result<(), String> {
        let id = self.legacy_id(cli);
        let inst = self
            .instances
            .get_mut(&id)
            .ok_or_else(|| format!("Legacy instance for {:?} not found", cli))?;
        if inst.session_active {
            return Err(format!("{:?} session already active", cli));
        }
        let msg = ChatMessage {
            role: ChatRole::User,
            content: prompt.to_string(),
            tool_name: None,
        };
        inst.chat_messages.push(msg);
        let workdir = config.working_dir.clone();
        let tool = config.tool;
        match TransportSession::spawn(tool, &workdir, prompt, SpawnOptions::default()).await {
            Ok(session) => {
                let inst = self
                    .instances
                    .get_mut(&id)
                    .ok_or_else(|| format!("Legacy instance for {:?} not found", cli))?;
                inst.pipe_rx = Some(session.subscribe());
                inst.transport_session = Some(session);
                inst.session_active = true;
                Ok(())
            }
            Err(e) => Err(format!("Failed to spawn pipe for {:?}: {}", cli, e)),
        }
    }

    /// Stop the session for a single CLI.
    pub async fn stop(&mut self, cli: AgentCli) {
        let id = self.legacy_id(cli);
        self.stop_instance(id).await;
    }

    /// Stop all active sessions (called on app shutdown).
    pub async fn stop_all(&mut self) {
        let all_ids: Vec<InstanceId> = self.instances.keys().copied().collect();
        for id in all_ids {
            self.stop_instance(id).await;
        }
    }

    /// Write a string to the active PTY for the given CLI.
    ///
    /// Lazy-spawns a PTY session on the first call for this CLI.
    pub async fn write_pty(&mut self, cli: AgentCli, text: &str) -> Result<(), String> {
        let need_spawn = {
            let id = self.legacy_id(cli);
            self.instances
                .get(&id)
                .map(|i| i.pty_session.is_none())
                .unwrap_or(false)
        };
        if need_spawn {
            let workdir = self.cli_workdir(cli);
            fs::create_dir_all(&workdir).map_err(|e| format!("mkdir error: {}", e))?;
            eprintln!(
                "[gate4agent] write_pty lazy-spawn cli={:?} cwd={}",
                cli,
                workdir.display()
            );
            let tool = cli_to_tool(cli);
            let config = SessionConfig {
                tool,
                working_dir: workdir,
                ..SessionConfig::default()
            };
            self.start_pty(cli, config).await?;
        }
        eprintln!(
            "[gate4agent] write_pty cli={:?} bytes={}",
            cli,
            text.len()
        );
        let id = self.legacy_id(cli);
        let inst = self
            .instances
            .get(&id)
            .ok_or_else(|| format!("Legacy instance for {:?} not found", cli))?;
        if let Some(ref session) = inst.pty_session {
            session
                .write(text)
                .await
                .map_err(|e| format!("PTY write error ({:?}): {}", cli, e))
        } else {
            Err(format!("No active PTY session for {:?}", cli))
        }
    }

    /// Send a chat prompt to the pipe session for the given CLI.
    ///
    /// Lazy-spawns a pipe session on the first call for this CLI.
    ///
    /// Routes all 6 CLIs through `TransportSession`. Legacy Claude-only restriction
    /// is lifted: all CLIs with a legacy slot can now use Chat/pipe mode.
    pub async fn send_chat(&mut self, cli: AgentCli, prompt: &str) -> Result<(), String> {
        let id = self.legacy_id(cli);
        let need_spawn = self
            .instances
            .get(&id)
            .map(|i| i.transport_session.is_none())
            .unwrap_or(false);

        eprintln!(
            "[gate4agent] send_chat cli={:?} need_spawn={} prompt_len={}",
            cli,
            need_spawn,
            prompt.len()
        );

        if need_spawn {
            let workdir = self.cli_workdir(cli);
            fs::create_dir_all(&workdir).map_err(|e| format!("mkdir error: {}", e))?;
            eprintln!(
                "[gate4agent] send_chat lazy-spawn cli={:?} cwd={}",
                cli,
                workdir.display()
            );
            let tool = cli_to_tool(cli);
            // Push the user message into the transient buffer immediately so UI shows it.
            {
                let inst = self
                    .instances
                    .get_mut(&id)
                    .ok_or_else(|| format!("Legacy instance for {:?} not found", cli))?;
                inst.chat_messages.push(ChatMessage {
                    role: ChatRole::User,
                    content: prompt.to_string(),
                    tool_name: None,
                });
                // Switch to Thinking state so the animated spinner shows immediately.
                inst.live_status = LiveStatus::Thinking;
            }
            // Resume the previous session if we have its id captured.
            let resume_id = self
                .instances
                .get(&id)
                .and_then(|i| i.pipe_session_id.clone());
            let opts = SpawnOptions {
                resume_session_id: resume_id,
                ..SpawnOptions::default()
            };
            match TransportSession::spawn(tool, &workdir, prompt, opts).await {
                Ok(session) => {
                    let inst = self
                        .instances
                        .get_mut(&id)
                        .ok_or_else(|| format!("Legacy instance for {:?} not found", cli))?;
                    inst.pipe_rx = Some(session.subscribe());
                    inst.transport_session = Some(session);
                    inst.session_active = true;
                    Ok(())
                }
                Err(e) => Err(format!("Failed to spawn pipe for {:?}: {}", cli, e)),
            }
        } else {
            // Existing session — push message and forward.
            {
                let inst = self
                    .instances
                    .get_mut(&id)
                    .ok_or_else(|| format!("Legacy instance for {:?} not found", cli))?;
                inst.chat_messages.push(ChatMessage {
                    role: ChatRole::User,
                    content: prompt.to_string(),
                    tool_name: None,
                });
                inst.live_status = LiveStatus::Thinking;
            }
            let inst = self
                .instances
                .get(&id)
                .ok_or_else(|| format!("Legacy instance for {:?} not found", cli))?;
            if let Some(ref session) = inst.transport_session {
                session
                    .send_prompt(prompt)
                    .await
                    .map_err(|e| format!("Pipe send error ({:?}): {}", cli, e))
            } else {
                Err(format!("No active pipe session for {:?}", cli))
            }
        }
    }

    // =========================================================================
    // Resize
    // =========================================================================

    /// Resize all active PTY sessions to the new dimensions.
    pub async fn resize(&mut self, cols: u16, rows: u16) {
        if self.cols == cols && self.rows == rows {
            return;
        }
        self.cols = cols;
        self.rows = rows;
        for inst in self.instances.values_mut() {
            inst.pty_parser.set_size(rows, cols);
            if let Some(ref session) = inst.pty_session {
                let _ = session.resize(rows, cols).await;
            }
        }
    }

    // =========================================================================
    // Drain events (call every frame)
    // =========================================================================

    /// Drain events for all instances. Returns `true` if any events were processed.
    pub fn drain_events(&mut self) -> bool {
        let mut had_events = false;
        for inst in self.instances.values_mut() {
            had_events |= Self::drain_one(inst);
        }
        had_events
    }

    fn drain_one(inst: &mut AgentInstance) -> bool {
        let mut had_events = false;

        // Drain PTY events
        if let Some(ref mut rx) = inst.pty_rx {
            loop {
                match rx.try_recv() {
                    Ok(event) => {
                        had_events = true;
                        match event {
                            AgentEvent::PtyRaw { data } => {
                                inst.pty_parser.process(&data);
                            }
                            AgentEvent::Exited { .. } => {
                                inst.session_active = false;
                            }
                            _ => {}
                        }
                    }
                    Err(broadcast::error::TryRecvError::Empty) => break,
                    Err(broadcast::error::TryRecvError::Closed) => {
                        inst.session_active = false;
                        break;
                    }
                    Err(broadcast::error::TryRecvError::Lagged(_)) => continue,
                }
            }
        }

        // Drain Pipe events
        if let Some(ref mut rx) = inst.pipe_rx {
            loop {
                match rx.try_recv() {
                    Ok(event) => {
                        had_events = true;
                        match event {
                            AgentEvent::SessionStart { session_id, .. } => {
                                // Capture id for --resume on the next prompt.
                                // Do NOT push a ChatMessage — that was the source
                                // of "[tool] unknown · session XXXX" spam.
                                inst.pipe_session_id = Some(session_id);
                            }
                            AgentEvent::Text { text, is_delta: _ } => {
                                // Finalize any RunningTool status: push a "done" history entry.
                                if let LiveStatus::RunningTool { ref name, done } =
                                    inst.live_status.clone()
                                {
                                    inst.chat_messages.push(ChatMessage {
                                        role: ChatRole::Tool,
                                        content: format!("{} · {} done", name, done),
                                        tool_name: Some(name.clone()),
                                    });
                                }
                                inst.live_status = LiveStatus::Idle;

                                if let Some(last) = inst.chat_messages.last_mut() {
                                    if last.role == ChatRole::Assistant {
                                        last.content.push_str(&text);
                                        continue;
                                    }
                                }
                                inst.chat_messages.push(ChatMessage {
                                    role: ChatRole::Assistant,
                                    content: text,
                                    tool_name: None,
                                });
                            }
                            AgentEvent::ToolStart { name, .. } => {
                                // Finalize previous tool if any.
                                if let LiveStatus::RunningTool {
                                    name: ref prev,
                                    done,
                                } = inst.live_status.clone()
                                {
                                    inst.chat_messages.push(ChatMessage {
                                        role: ChatRole::Tool,
                                        content: format!("{} · {} done", prev, done),
                                        tool_name: Some(prev.clone()),
                                    });
                                }
                                // Start tracking the new tool.
                                inst.live_status = LiveStatus::RunningTool { name, done: 0 };
                            }
                            AgentEvent::ToolResult {
                                id: _,
                                output: _,
                                is_error: _,
                                ..
                            } => {
                                if let LiveStatus::RunningTool { done, .. } = &mut inst.live_status
                                {
                                    *done = done.saturating_add(1);
                                }
                            }
                            AgentEvent::Thinking { text: _ } => {
                                // Keep Thinking status as-is; suppress bubble noise.
                            }
                            AgentEvent::Error { message } => {
                                inst.live_status = LiveStatus::Idle;
                                inst.chat_messages.push(ChatMessage {
                                    role: ChatRole::Error,
                                    content: message,
                                    tool_name: None,
                                });
                            }
                            AgentEvent::TurnComplete { .. } => {
                                // Finalize any in-flight tool and clear live status.
                                if let LiveStatus::RunningTool { ref name, done } =
                                    inst.live_status.clone()
                                {
                                    inst.chat_messages.push(ChatMessage {
                                        role: ChatRole::Tool,
                                        content: format!("{} · {} done", name, done),
                                        tool_name: Some(name.clone()),
                                    });
                                }
                                inst.live_status = LiveStatus::Idle;
                            }
                            AgentEvent::SessionEnd {
                                result, is_error, ..
                            } => {
                                if is_error {
                                    inst.chat_messages.push(ChatMessage {
                                        role: ChatRole::Error,
                                        content: format!("Session error · {}", result),
                                        tool_name: None,
                                    });
                                }
                                // Finalize any in-flight tool.
                                if let LiveStatus::RunningTool { ref name, done } =
                                    inst.live_status.clone()
                                {
                                    inst.chat_messages.push(ChatMessage {
                                        role: ChatRole::Tool,
                                        content: format!("{} · {} done", name, done),
                                        tool_name: Some(name.clone()),
                                    });
                                }
                                inst.live_status = LiveStatus::Idle;
                            }
                            AgentEvent::Exited { .. } => {
                                inst.session_active = false;
                                inst.live_status = LiveStatus::Idle;
                            }
                            _ => {}
                        }
                    }
                    Err(broadcast::error::TryRecvError::Empty) => break,
                    Err(broadcast::error::TryRecvError::Closed) => {
                        inst.session_active = false;
                        break;
                    }
                    Err(broadcast::error::TryRecvError::Lagged(_)) => continue,
                }
            }
        }

        had_events
    }

    // =========================================================================
    // Snapshot + query (legacy CLI-based)
    // =========================================================================

    /// Build a render snapshot for the given CLI, honoring the UI's requested mode.
    ///
    /// `want_pty=true` → return PTY grid if a PTY parser/session exists (even if exited),
    /// otherwise Idle. `want_pty=false` → return chat messages if any, otherwise Idle.
    /// This decouples snapshot mode from session liveness so mode switches don't
    /// destroy the other view.
    pub fn snapshot_mode(&self, cli: AgentCli, want_pty: bool) -> AgentRenderSnapshot {
        let id = self.legacy_id(cli);
        let inst = match self.instances.get(&id) {
            Some(i) => i,
            None => {
                return AgentRenderSnapshot {
                    mode: AgentSnapshotMode::Idle,
                    session_active: false,
                    live_status: LiveStatus::Idle,
                }
            }
        };
        if want_pty {
            // Render PTY grid as long as we ever spawned one OR the parser has content.
            if inst.pty_session.is_some() || self.instance_pty_has_content(inst) {
                return self.build_pty_snapshot_from_instance(inst);
            }
            return AgentRenderSnapshot {
                mode: AgentSnapshotMode::Idle,
                session_active: inst.session_active,
                live_status: inst.live_status.clone(),
            };
        }
        // Chat mode requested
        if !inst.chat_messages.is_empty() {
            return AgentRenderSnapshot {
                mode: AgentSnapshotMode::Chat(inst.chat_messages.clone()),
                session_active: inst.session_active,
                live_status: inst.live_status.clone(),
            };
        }
        AgentRenderSnapshot {
            mode: AgentSnapshotMode::Idle,
            session_active: inst.session_active,
            live_status: inst.live_status.clone(),
        }
    }

    fn instance_pty_has_content(&self, inst: &AgentInstance) -> bool {
        let screen = inst.pty_parser.screen();
        for row in 0..self.rows {
            for col in 0..self.cols {
                if let Some(cell) = screen.cell(row, col) {
                    if !cell.contents().is_empty() {
                        return true;
                    }
                }
            }
        }
        false
    }

    fn build_pty_snapshot_from_instance(&self, inst: &AgentInstance) -> AgentRenderSnapshot {
        let screen = inst.pty_parser.screen();
        let mut grid = TermGrid::empty(self.cols, self.rows);
        for row in 0..self.rows {
            for col in 0..self.cols {
                if let Some(cell) = screen.cell(row, col) {
                    let fg = vt100_color_to_rgb(cell.fgcolor(), [204, 204, 204]);
                    let bg = vt100_color_to_rgb(cell.bgcolor(), [0, 0, 0]);
                    let contents = cell.contents();
                    grid.cells[row as usize][col as usize] = TermCell {
                        ch: if contents.is_empty() {
                            " ".to_string()
                        } else {
                            contents
                        },
                        fg,
                        bg,
                        bold: cell.bold(),
                    };
                }
            }
        }
        let (cur_row, cur_col) = screen.cursor_position();
        grid.cursor_row = cur_row;
        grid.cursor_col = cur_col;
        // Respect DECTCEM. Empirically verified with
        // `cargo run --example pty_cursor_probe -p gate4agent`: Ink-based
        // TUIs like Claude Code
        //   - hide the terminal cursor in steady state and only unhide it
        //     briefly while echoing raw input;
        //   - park the vt100 cursor on whichever cell they are currently
        //     repainting — for Claude that is typically the top-right
        //     buddy ASCII-art animation, NOT the edit caret;
        //   - draw their own fake caret in the framebuffer at the real
        //     input position (reverse video / block glyph).
        // Drawing our own white block at the raw vt100 position therefore
        // causes two visible bugs: (1) the caret drifts into the buddy
        // area when idle, and (2) it sits one cell past the real input
        // caret while typing. Respecting `hide_cursor()` means the visible
        // caret comes entirely from the framebuffer (what Ink drew), and
        // both symptoms disappear.
        grid.cursor_visible = !screen.hide_cursor();
        AgentRenderSnapshot {
            mode: AgentSnapshotMode::Pty(grid),
            session_active: inst.session_active,
            live_status: inst.live_status.clone(),
        }
    }

    /// Legacy snapshot — inferred from state. Prefer `snapshot_mode`.
    pub fn snapshot(&self, cli: AgentCli) -> AgentRenderSnapshot {
        let id = self.legacy_id(cli);
        let inst = match self.instances.get(&id) {
            Some(i) => i,
            None => {
                return AgentRenderSnapshot {
                    mode: AgentSnapshotMode::Idle,
                    session_active: false,
                    live_status: LiveStatus::Idle,
                }
            }
        };

        if !inst.session_active {
            if !inst.chat_messages.is_empty() {
                return AgentRenderSnapshot {
                    mode: AgentSnapshotMode::Chat(inst.chat_messages.clone()),
                    session_active: false,
                    live_status: inst.live_status.clone(),
                };
            }
            return AgentRenderSnapshot {
                mode: AgentSnapshotMode::Idle,
                session_active: false,
                live_status: inst.live_status.clone(),
            };
        }

        if inst.pty_session.is_some() {
            let screen = inst.pty_parser.screen();
            let mut grid = TermGrid::empty(self.cols, self.rows);
            for row in 0..self.rows {
                for col in 0..self.cols {
                    if let Some(cell) = screen.cell(row, col) {
                        let fg = vt100_color_to_rgb(cell.fgcolor(), [204, 204, 204]);
                        let bg = vt100_color_to_rgb(cell.bgcolor(), [0, 0, 0]);
                        let contents = cell.contents();
                        grid.cells[row as usize][col as usize] = TermCell {
                            ch: if contents.is_empty() {
                                " ".to_string()
                            } else {
                                contents
                            },
                            fg,
                            bg,
                            bold: cell.bold(),
                        };
                    }
                }
            }
            let (cur_row, cur_col) = screen.cursor_position();
            grid.cursor_row = cur_row;
            grid.cursor_col = cur_col;
            // See build_pty_snapshot_from_instance() for the rationale: Ink-based TUIs
            // draw their own fake caret in the framebuffer and park the
            // real vt100 cursor on animation cells, so we must respect
            // DECTCEM to avoid a drifting ghost caret.
            grid.cursor_visible = !screen.hide_cursor();
            // Buddy extraction disabled — heuristic doesn't reliably catch the
            // companion ASCII art yet. Kept in snapshot.rs for future experiments.
            // grid.detect_and_extract_buddy();
            AgentRenderSnapshot {
                mode: AgentSnapshotMode::Pty(grid),
                session_active: true,
                live_status: inst.live_status.clone(),
            }
        } else {
            AgentRenderSnapshot {
                mode: AgentSnapshotMode::Chat(inst.chat_messages.clone()),
                session_active: true,
                live_status: inst.live_status.clone(),
            }
        }
    }

    /// Returns `true` if either PTY or pipe session is alive for this CLI.
    pub fn is_active(&self, cli: AgentCli) -> bool {
        let id = self.legacy_id(cli);
        self.instances
            .get(&id)
            .map(|i| i.session_active)
            .unwrap_or(false)
    }

    /// Returns `true` if any instance has an active session.
    pub fn any_active(&self) -> bool {
        self.instances.values().any(|i| i.session_active)
    }

    /// Number of past sessions for this CLI (live disk read).
    pub fn past_session_count(&self, cli: AgentCli) -> usize {
        let workdir = self.cli_workdir(cli);
        crate::history::reader_for(cli).list_sessions(&workdir).len()
    }

    /// List past sessions (newest first, live disk read).
    pub fn list_past_sessions(&self, cli: AgentCli) -> Vec<crate::history::SessionMeta> {
        let workdir = self.cli_workdir(cli);
        crate::history::reader_for(cli).list_sessions(&workdir)
    }

    /// Load the latest past session into the chat view (display only — does NOT resume the CLI process).
    ///
    /// Returns `true` if anything was loaded.
    pub fn load_latest_history(&mut self, cli: AgentCli) -> bool {
        let workdir = self.cli_workdir(cli);
        let reader = crate::history::reader_for(cli);
        eprintln!(
            "[gate4agent] load_latest_history cli={:?} workdir={}",
            cli,
            workdir.display()
        );
        let latest = match reader.latest_session(&workdir) {
            Some(id) => {
                eprintln!("[gate4agent]   latest session id={}", id);
                id
            }
            None => {
                eprintln!("[gate4agent]   no past sessions found");
                return false;
            }
        };
        let messages = reader.load_session(&workdir, &latest);
        eprintln!("[gate4agent]   loaded {} messages", messages.len());
        if messages.is_empty() {
            return false;
        }
        let id = self.legacy_id(cli);
        if let Some(inst) = self.instances.get_mut(&id) {
            inst.chat_messages = messages;
            inst.pipe_session_id = Some(latest.clone());
            inst.transport_session = None;
            inst.pipe_rx = None;
        }
        true
    }

    /// Load a specific past session into the chat view AND mark it as the
    /// resume target — the next `send_chat` will respawn the pipe with
    /// `--resume <session_id>`, so the user keeps writing into the chosen
    /// thread instead of accidentally starting a fresh session.
    pub fn load_history(&mut self, cli: AgentCli, session_id: &str) -> bool {
        let workdir = self.cli_workdir(cli);
        let reader = crate::history::reader_for(cli);
        let messages = reader.load_session(&workdir, session_id);
        if messages.is_empty() {
            return false;
        }
        let id = self.legacy_id(cli);
        if let Some(inst) = self.instances.get_mut(&id) {
            inst.chat_messages = messages;
            inst.pipe_session_id = Some(session_id.to_string());
            // Drop any stale live transport handle so the next send_chat goes through
            // the spawn branch and picks up the resume id.
            inst.transport_session = None;
            inst.pipe_rx = None;
        }
        true
    }

    /// Backwards-compatible wrapper — loads the latest past session.
    ///
    /// Chart-app callers using `load_next_past_session` continue to work unchanged.
    pub fn load_next_past_session(&mut self, cli: AgentCli) -> bool {
        self.load_latest_history(cli)
    }
}

impl Default for MultiCliManager {
    fn default() -> Self {
        Self::new(ManagerConfig::default())
    }
}

// =============================================================================
// Helpers
// =============================================================================

fn cli_to_tool(cli: AgentCli) -> CliTool {
    match cli {
        AgentCli::Claude => CliTool::ClaudeCode,
        AgentCli::Codex => CliTool::Codex,
        AgentCli::Gemini => CliTool::Gemini,
        AgentCli::OpenCode => CliTool::OpenCode,
    }
}