1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
//! Cloud-agent + Claude-agents dashboard methods on `App` (A-1 of
//! the file-split refactor — 2026-06-28). Owns the spawn_managed_agents
//! worker, the new-cloud-run + new-cloud-agent wizards, the cloud-run
//! pane drains, the claude-agents dashboard pane (refresh, filters,
//! kill confirmation, escalation, action dispatch, file/row context
//! menus), AI spend stats, AI session search.
//!
//! Extracted from `src/app/mod.rs`. Pure non-destructive move.
use super::*;
impl App {
/// Spawn an `ant beta:worker poll` Pty pane for a self-hosted
/// sandbox environment. User must have already created the
/// environment in the Console and exported
/// `ANTHROPIC_ENVIRONMENT_KEY` + `ANTHROPIC_ENVIRONMENT_ID`.
/// Detects missing `ant` binary and toasts a manual install hint.
pub fn spawn_managed_agents_worker(&mut self) {
if !binary_on_path("ant") {
self.toast(
{
let hint = match std::env::consts::OS {
"macos" => "run: brew install anthropics/tap/ant".to_string(),
_ => "see install docs (no brew tap on this platform)".to_string(),
};
format!(
"ant CLI not installed — see https://platform.claude.com/docs/en/managed-agents/self-hosted-sandboxes#install-the-ant-cli or {hint}"
)
},
);
return;
}
let workspace = self.workspace.clone();
let profile = crate::pty_pane::BinaryProfile {
label: "ant worker".to_string(),
exe: "ant".to_string(),
args: vec![
"beta:worker".to_string(),
"poll".to_string(),
"--workdir".to_string(),
workspace.display().to_string(),
],
cwd: Some(workspace),
env: Vec::new(),
session_id: None,
integration_id: None,
};
self.open_pty(profile);
self.toast(
"ant beta:worker poll spawned — needs ANTHROPIC_ENVIRONMENT_KEY + ANTHROPIC_ENVIRONMENT_ID in env",
);
}
/// Cycle the auto-refresh interval on the active CloudAgentRun
/// pane: off → 10s → 30s → 60s → 5m → off. Resets
/// `last_auto_refresh` so the new interval starts counting now.
pub fn cloud_agent_run_cycle_auto(&mut self) {
let Some(id) = self.active else { return };
let Some(Pane::CloudAgentRun(p)) = self.panes.get_mut(id) else {
return;
};
p.auto_refresh_secs = match p.auto_refresh_secs {
0 => 10,
10 => 30,
30 => 60,
60 => 300,
_ => 0,
};
p.last_auto_refresh = Some(std::time::Instant::now());
let label = if p.auto_refresh_secs == 0 {
"auto-refresh off".to_string()
} else if p.auto_refresh_secs < 60 {
format!("auto-refresh every {}s", p.auto_refresh_secs)
} else {
format!("auto-refresh every {}m", p.auto_refresh_secs / 60)
};
self.toast(label);
}
/// For every CloudAgentRun pane with auto-refresh enabled and
/// an elapsed interval ≥ its cadence, re-spawn the log +
/// artifact workers. Called once per `tick()` — bails early
/// when nothing is due.
pub(crate) fn tick_cloud_agent_run_auto(&mut self) {
let now = std::time::Instant::now();
let due: Vec<usize> = self
.panes
.iter()
.enumerate()
.filter_map(|(i, p)| {
if let Pane::CloudAgentRun(c) = p
&& c.auto_refresh_secs > 0
&& c.last_auto_refresh
.map(|t| now.duration_since(t).as_secs() >= c.auto_refresh_secs)
.unwrap_or(true)
{
Some(i)
} else {
None
}
})
.collect();
for i in due {
// Save current active so we can swap, refresh, restore.
let saved = self.active;
self.active = Some(i);
self.cloud_agent_run_refresh();
if let Some(Pane::CloudAgentRun(p)) = self.panes.get_mut(i) {
p.last_auto_refresh = Some(now);
}
self.active = saved;
}
}
/// Re-spawn the log + artifact workers on the active
/// CloudAgentRun pane. Wired to the `[↻ Refresh]` chip on
/// the detail pane's sub-header. Useful when the run finished
/// after the pane was opened (artifacts uploaded in the
/// meantime) or when the user wants a fresh tail.
pub fn cloud_agent_run_refresh(&mut self) {
let Some(id) = self.active else { return };
let Some(Pane::CloudAgentRun(p)) = self.panes.get_mut(id) else {
return;
};
// Skip for managed-agent panes — they use the SSE event
// stream (session_event_rx), not log_rx / artifacts_rx.
// Restart the SSE stream so the user gets fresh events.
if matches!(
p.source,
crate::cloud_agent_run::CloudRunSource::AnthropicManaged
) {
let session_id = p.run_id.clone();
p.logs.clear();
p.logs_loading = true;
p.logs_err = None;
p.log_follow = true;
p.log_scroll = 0;
p.session_event_rx = Some(crate::anthropic_api::spawn_session_event_stream(session_id));
self.toast("restarting session stream…");
return;
}
// ECS runner: rebuild log + artifact workers from scratch.
let run_id = p.run_id.clone();
let state = p.state.clone();
let s3_prefix = p.s3_artifact_prefix.clone();
let log_group = self.config.cloud_agents.log_group.clone();
let Some(Pane::CloudAgentRun(p)) = self.panes.get_mut(id) else {
return;
};
p.logs.clear();
p.logs_loading = true;
p.logs_err = None;
p.log_follow = true;
p.log_scroll = 0;
p.artifacts.clear();
p.artifacts_loading = true;
p.artifacts_err = None;
p.log_rx = Some(crate::cloud_agent_run::spawn_log_fetcher(
run_id, state, log_group,
));
p.artifacts_rx = Some(crate::cloud_agent_run::spawn_artifacts_fetcher(s3_prefix));
self.toast("refreshing logs + artifacts…");
}
/// Daily-driver path: fire a Managed Agents session against
/// the user's saved defaults, using whatever's in
/// `cloud_run_prompt_input` as the user message. No wizard.
/// Called on Enter from the panel's quick-fire input row.
pub fn cloud_run_quick_send(&mut self) {
let defaults = self.config.cloud_run.defaults.clone();
if defaults.agent_id.is_empty() || defaults.env_id.is_empty() {
self.toast("no defaults saved — open the wizard to set up an agent + env");
self.open_new_cloud_run_wizard();
return;
}
let prompt = self.cloud_run_prompt_input.trim().to_string();
if prompt.is_empty() {
self.toast("type a prompt first");
return;
}
self.cloud_run_prompt_input.clear();
self.cloud_run_prompt_focused = false;
let agent_id = defaults.agent_id;
let env_id = defaults.env_id;
let tx = self.cloud_run_msg_tx.clone();
std::thread::spawn(move || {
macro_rules! emit { ($($t:tt)*) => { let _ = tx.send(format!($($t)*)); }; }
let backend = match crate::anthropic_api::detect_backend() {
Ok(b) => b,
Err(e) => {
emit!("cloud-run · backend: {e}");
return;
}
};
let session = match crate::anthropic_api::create_session(
&backend,
&agent_id,
&env_id,
"mnml quick send",
) {
Ok(s) => s,
Err(e) => {
emit!("cloud-run · create_session: {e}");
return;
}
};
if let Err(e) = crate::anthropic_api::send_user_message(&backend, &session.id, &prompt)
{
emit!("cloud-run · send_user_message: {e}");
return;
}
emit!("session running · {}", &session.id);
});
self.toast("firing Managed Agents session…");
}
/// Open the Cloud Agents wizard (runner picker — Managed Agents
/// or ECS runner). Distinct from `open_new_cloud_agent_wizard`
/// which opens the local-agents PR wizard.
pub fn open_new_cloud_run_wizard(&mut self) {
if let Some(id) = self
.panes
.iter()
.position(|p| matches!(p, Pane::NewCloudRunWizard(_)))
{
self.reveal_pane(id);
return;
}
let pane =
Pane::NewCloudRunWizard(crate::new_cloud_run_wizard::NewCloudRunWizardPane::new());
match self.active {
Some(cur) => {
let new_id = self.split_leaf_with(cur, crate::layout::SplitDir::Vertical, pane);
self.active = Some(new_id);
}
None => {
self.panes.push(pane);
let id = self.panes.len() - 1;
*self.layout_mut() = Layout::leaf(id);
self.active = Some(id);
}
}
self.focus = Focus::Pane;
}
pub fn new_cloud_run_wizard_move(&mut self, delta: isize) {
let Some(id) = self.active else { return };
let Some(Pane::NewCloudRunWizard(w)) = self.panes.get_mut(id) else {
return;
};
use crate::new_cloud_run_wizard::{CloudRunStep, CloudRunner, SandboxLocation};
let max = match w.step {
CloudRunStep::Runner => CloudRunner::all().len(),
CloudRunStep::ManagedAgent => 2,
CloudRunStep::ManagedSandbox => SandboxLocation::all().len(),
_ => 0,
};
if max == 0 {
return;
}
let cur = w.focus_row as isize;
let new = (cur + delta).rem_euclid(max as isize) as usize;
w.focus_row = new;
match w.step {
CloudRunStep::Runner => w.runner = CloudRunner::all()[new],
CloudRunStep::ManagedAgent => w.managed_agent_create_new = new == 0,
CloudRunStep::ManagedSandbox => w.sandbox = SandboxLocation::all()[new],
_ => {}
}
}
pub fn new_cloud_run_wizard_next(&mut self) {
let Some(id) = self.active else { return };
let Some(Pane::NewCloudRunWizard(w)) = self.panes.get_mut(id) else {
return;
};
if !w.next_step() {
self.new_cloud_run_wizard_submit();
}
}
pub fn new_cloud_run_wizard_back(&mut self) {
let Some(id) = self.active else { return };
let Some(Pane::NewCloudRunWizard(w)) = self.panes.get_mut(id) else {
return;
};
w.prev_step();
}
pub fn new_cloud_run_wizard_type(&mut self, ch: char) {
let Some(id) = self.active else { return };
let Some(Pane::NewCloudRunWizard(w)) = self.panes.get_mut(id) else {
return;
};
use crate::new_cloud_run_wizard::CloudRunStep;
match w.step {
CloudRunStep::ManagedAgent => {
if w.managed_agent_create_new {
w.managed_agent_new_name.push(ch);
} else {
w.managed_agent_id.push(ch);
}
}
CloudRunStep::QweTicket => w.qwe_ticket.push(ch),
CloudRunStep::Prompt => w.prompt.push(ch),
_ => {}
}
}
pub fn new_cloud_run_wizard_backspace(&mut self) {
let Some(id) = self.active else { return };
let Some(Pane::NewCloudRunWizard(w)) = self.panes.get_mut(id) else {
return;
};
use crate::new_cloud_run_wizard::CloudRunStep;
match w.step {
CloudRunStep::ManagedAgent => {
if w.managed_agent_create_new {
w.managed_agent_new_name.pop();
} else {
w.managed_agent_id.pop();
}
}
CloudRunStep::QweTicket => {
w.qwe_ticket.pop();
}
CloudRunStep::Prompt => {
w.prompt.pop();
}
_ => {}
}
}
pub fn new_cloud_run_wizard_close(&mut self) {
if let Some(id) = self.active
&& matches!(self.panes.get(id), Some(Pane::NewCloudRunWizard(_)))
{
self.close_pane(id);
}
}
/// Submit. QWE path routes to existing fire_cloud_run.
/// Managed Agents path spawns a worker thread that calls the
/// Anthropic API (create agent if needed → create env if
/// needed → create session). Result toasts back.
pub fn new_cloud_run_wizard_submit(&mut self) {
let Some(id) = self.active else { return };
use crate::new_cloud_run_wizard::{CloudRunner, SandboxLocation};
let cfg = match self.panes.get(id) {
Some(Pane::NewCloudRunWizard(p)) => (
p.runner,
p.qwe_ticket.clone(),
p.managed_agent_create_new,
p.managed_agent_new_name.clone(),
p.managed_agent_id.clone(),
p.sandbox,
p.managed_env_id.clone(),
p.prompt.clone(),
),
_ => return,
};
match cfg.0 {
CloudRunner::Ecs => {
let t = cfg.1.trim().to_string();
if t.is_empty() {
self.toast("ticket is empty");
return;
}
self.fire_cloud_run(&t);
self.toast(format!("fired ECS run for {t}"));
self.new_cloud_run_wizard_close();
}
CloudRunner::ManagedAgents => {
if cfg.7.trim().is_empty() {
self.toast("prompt is empty");
return;
}
// Mark submitting + spawn worker. Result drained
// by the wizard's tick handler.
if let Some(Pane::NewCloudRunWizard(w)) = self.panes.get_mut(id) {
w.submitting = true;
w.last_message = Some("Submitting to Anthropic API…".to_string());
}
let create_new = cfg.2;
let agent_name = cfg.3;
let agent_id_existing = cfg.4;
let sandbox = cfg.5;
let env_id_existing = cfg.6;
let prompt = cfg.7;
let tx = self.cloud_run_msg_tx.clone();
std::thread::spawn(move || {
macro_rules! emit { ($($t:tt)*) => { let _ = tx.send(format!($($t)*)); }; }
let backend = match crate::anthropic_api::detect_backend() {
Ok(b) => b,
Err(e) => {
emit!("cloud-run · backend: {e}");
return;
}
};
let agent_id = if create_new {
match crate::anthropic_api::create_agent(
&backend,
&agent_name,
"claude-opus-4-8",
"You are a helpful coding agent.",
) {
Ok(a) => a.id,
Err(e) => {
emit!("cloud-run · create_agent: {e}");
return;
}
}
} else {
agent_id_existing
};
let env_id = if env_id_existing.is_empty() {
let kind = match sandbox {
SandboxLocation::AnthropicCloud => "cloud",
SandboxLocation::SelfHostedLocal
| SandboxLocation::SelfHostedRemote => "self_hosted",
};
match crate::anthropic_api::create_environment(&backend, "ide-env", kind) {
Ok(e) => e.id,
Err(e) => {
emit!("cloud-run · create_environment: {e}");
return;
}
}
} else {
env_id_existing
};
let session = match crate::anthropic_api::create_session(
&backend,
&agent_id,
&env_id,
"mnml session",
) {
Ok(s) => s,
Err(e) => {
emit!("cloud-run · create_session: {e}");
return;
}
};
// Critical step: a freshly-created session is
// idle until you POST a user.message to its
// /events endpoint. Without this, the session
// sits there forever showing "No events yet".
if let Err(e) =
crate::anthropic_api::send_user_message(&backend, &session.id, &prompt)
{
emit!("cloud-run · send_user_message: {e}");
return;
}
// Save defaults so the next run can be a
// one-line quick-fire from the panel input
// instead of the full wizard. The drainer
// parses this sentinel format and writes to
// ~/.config/mnml/config.toml.
let sandbox_kind = match sandbox {
SandboxLocation::AnthropicCloud => "cloud",
SandboxLocation::SelfHostedLocal | SandboxLocation::SelfHostedRemote => {
"self_hosted"
}
};
emit!(
"__persist_defaults__|{}|{}|{}|claude-opus-4-8",
agent_id,
env_id,
sandbox_kind
);
emit!("session running · {}", &session.id);
});
self.toast("submitting Managed Agents run…");
self.new_cloud_run_wizard_close();
}
}
}
/// Open the multi-step wizard for creating a new cloud agent
/// run. Routes to either the ECS runner or Anthropic managed
/// agents depending on the user's step-1 pick. The pane lives
/// at the active leaf; close with Esc.
pub fn open_new_cloud_agent_wizard(&mut self) {
// Reuse an existing wizard pane if present — no point
// letting the user pile multiple half-filled wizards.
if let Some(id) = self
.panes
.iter()
.position(|p| matches!(p, Pane::NewCloudAgentWizard(_)))
{
self.reveal_pane(id);
return;
}
let pane = Pane::NewCloudAgentWizard(
crate::new_cloud_agent_wizard::NewCloudAgentWizardPane::new(),
);
match self.active {
Some(cur) => {
let new_id = self.split_leaf_with(cur, crate::layout::SplitDir::Vertical, pane);
self.active = Some(new_id);
}
None => {
self.panes.push(pane);
let id = self.panes.len() - 1;
*self.layout_mut() = Layout::leaf(id);
self.active = Some(id);
}
}
self.focus = Focus::Pane;
}
/// Move the wizard's focus row up/down. No-op when the active
/// pane isn't a wizard.
pub fn new_cloud_agent_wizard_move(&mut self, delta: isize) {
let Some(id) = self.active else {
return;
};
let Some(Pane::NewCloudAgentWizard(w)) = self.panes.get_mut(id) else {
return;
};
use crate::new_cloud_agent_wizard::{Action, Source, WizardStep};
let max = match w.step {
WizardStep::Source => Source::all().len(),
WizardStep::PrList => w.pr_rows.len(),
WizardStep::Action => Action::all().len(),
_ => 0,
};
if max == 0 {
return;
}
let cur = w.focus_row as isize;
let new = (cur + delta).rem_euclid(max as isize) as usize;
w.focus_row = new;
// Radio selections apply immediately so the user sees the
// chosen option highlighted; PrList does NOT (Space toggles).
match w.step {
WizardStep::Source => w.source = Source::all()[new],
WizardStep::Action => w.action = Action::all()[new],
_ => {}
}
}
/// Toggle the selected state of the focused PR row. No-op on
/// other steps.
pub fn new_cloud_agent_wizard_toggle(&mut self) {
let Some(id) = self.active else {
return;
};
let Some(Pane::NewCloudAgentWizard(w)) = self.panes.get_mut(id) else {
return;
};
if !matches!(w.step, crate::new_cloud_agent_wizard::WizardStep::PrList) {
return;
}
let idx = w.focus_row;
if let Some(r) = w.pr_rows.get_mut(idx) {
r.selected = !r.selected;
}
}
/// Select / deselect all PRs (only on PrList step).
pub fn new_cloud_agent_wizard_select_all(&mut self) {
let Some(id) = self.active else {
return;
};
let Some(Pane::NewCloudAgentWizard(w)) = self.panes.get_mut(id) else {
return;
};
if !matches!(w.step, crate::new_cloud_agent_wizard::WizardStep::PrList) {
return;
}
let any_selected = w.pr_rows.iter().any(|r| r.selected);
for r in w.pr_rows.iter_mut() {
r.selected = !any_selected;
}
}
/// Advance the wizard to the next step OR submit if we're on
/// Review. No-op when the active pane isn't a wizard. When
/// stepping INTO PrList, kick off the PR-list fetcher worker
/// for the picked source.
pub fn new_cloud_agent_wizard_next(&mut self) {
let Some(id) = self.active else {
return;
};
let Some(Pane::NewCloudAgentWizard(w)) = self.panes.get_mut(id) else {
return;
};
let advanced = w.next_step();
if !advanced {
self.new_cloud_agent_wizard_submit();
return;
}
// Side effect on step transition: load the PR list.
if matches!(
self.panes.get(id),
Some(Pane::NewCloudAgentWizard(p)) if matches!(p.step, crate::new_cloud_agent_wizard::WizardStep::PrList)
) {
self.kick_off_pr_list_load(id);
}
}
fn kick_off_pr_list_load(&mut self, pane_id: usize) {
let source = match self.panes.get(pane_id) {
Some(Pane::NewCloudAgentWizard(p)) => p.source,
_ => return,
};
let repo_path = self.active_repo_path().to_path_buf();
if let Some(Pane::NewCloudAgentWizard(w)) = self.panes.get_mut(pane_id) {
w.pr_rows.clear();
w.pr_err = None;
w.pr_loading = true;
use crate::new_cloud_agent_wizard::Source;
let rx = match source {
Source::GitHubPr => Some(crate::new_cloud_agent_wizard::spawn_gh_pr_fetcher(
repo_path,
)),
Source::BitbucketPr => {
let slug = derive_bitbucket_slug(&repo_path)
.unwrap_or_else(|| "<workspace>/<repo>".to_string());
Some(crate::new_cloud_agent_wizard::spawn_bitbucket_pr_fetcher(
slug,
))
}
Source::ManualPrompt => None,
};
w.pr_rx = rx;
if w.pr_rx.is_none() {
w.pr_loading = false;
}
}
}
pub fn new_cloud_agent_wizard_back(&mut self) {
let Some(id) = self.active else {
return;
};
let Some(Pane::NewCloudAgentWizard(w)) = self.panes.get_mut(id) else {
return;
};
w.prev_step();
}
/// Append a char to the focused text input on the CustomPrompt step.
pub fn new_cloud_agent_wizard_type(&mut self, ch: char) {
let Some(id) = self.active else {
return;
};
let Some(Pane::NewCloudAgentWizard(w)) = self.panes.get_mut(id) else {
return;
};
if matches!(
w.step,
crate::new_cloud_agent_wizard::WizardStep::CustomPrompt
) {
w.custom_prompt.push(ch);
}
}
pub fn new_cloud_agent_wizard_backspace(&mut self) {
let Some(id) = self.active else {
return;
};
let Some(Pane::NewCloudAgentWizard(w)) = self.panes.get_mut(id) else {
return;
};
if matches!(
w.step,
crate::new_cloud_agent_wizard::WizardStep::CustomPrompt
) {
w.custom_prompt.pop();
}
}
/// Close the wizard pane (Esc).
pub fn new_cloud_agent_wizard_close(&mut self) {
if let Some(id) = self.active
&& matches!(self.panes.get(id), Some(Pane::NewCloudAgentWizard(_)))
{
self.close_pane(id);
}
}
/// Submit the wizard. Resolves the final prompt per the chosen
/// Action template (or the user's CustomPrompt), then for each
/// selected PR row spawns a Claude session in a Pty pane.
pub fn new_cloud_agent_wizard_submit(&mut self) {
let Some(id) = self.active else {
return;
};
use crate::new_cloud_agent_wizard::{Action, Source};
let cfg = match self.panes.get(id) {
Some(Pane::NewCloudAgentWizard(w)) => (
w.source,
w.action,
w.custom_prompt.clone(),
w.pr_rows
.iter()
.filter(|r| r.selected)
.map(|r| (r.number, r.title.clone()))
.collect::<Vec<_>>(),
),
_ => return,
};
// #997 (2026-08-19) — read the per-user override from
// `[ai.review_templates]` when present, else fall back to
// the shipped default. Custom action still returns
// `<custom>` as before (that path uses `cfg.2` verbatim).
let template = cfg.1.prompt_template_from(&self.config);
let make_prompt = |pr_num: u32| -> String {
if matches!(cfg.1, Action::Custom) {
format!("{}\n\n(context: PR #{})", cfg.2, pr_num)
} else {
template.replace("<num>", &pr_num.to_string())
}
};
match cfg.0 {
Source::ManualPrompt => {
let prompt = if matches!(cfg.1, Action::Custom) {
cfg.2.clone()
} else {
template.replace("<num>", "(no PR)")
};
if prompt.trim().is_empty() {
self.toast("prompt is empty");
return;
}
self.spawn_claude_session_pty(prompt, None, "manual");
self.new_cloud_agent_wizard_close();
}
Source::GitHubPr | Source::BitbucketPr => {
if cfg.3.is_empty() {
self.toast("no PRs selected — go back to step 2");
return;
}
let mut count = 0;
for (num, title) in &cfg.3 {
let prompt = make_prompt(*num);
let label = format!("claude PR#{num}");
self.spawn_claude_session_pty(prompt, Some(*num), &label);
count += 1;
let _ = title;
}
self.toast(format!("fired {count} Claude session(s)"));
self.new_cloud_agent_wizard_close();
}
}
}
/// Spawn a `claude --print <prompt>` Pty pane. When `pr_number`
/// is Some, runs `gh pr checkout <num>` first so Claude works
/// against the PR's branch. Worker runs in the active repo's
/// directory.
fn spawn_claude_session_pty(&mut self, prompt: String, pr_number: Option<u32>, label: &str) {
let cwd = self.active_repo_path().to_path_buf();
// Build a one-shot shell script: checkout (if PR) then claude.
// The script runs in bash with `set -e` so a failing checkout
// doesn't silently drop into the wrong branch.
let escaped_prompt = prompt.replace('\'', "'\\''");
let script = match pr_number {
Some(n) => format!(
"set -e\necho '→ gh pr checkout {n}'\ngh pr checkout {n}\necho '→ claude --print'\nclaude --print '{escaped_prompt}'\n"
),
None => format!("set -e\necho '→ claude --print'\nclaude --print '{escaped_prompt}'\n"),
};
let profile = crate::pty_pane::BinaryProfile {
label: label.to_string(),
exe: "bash".to_string(),
args: vec!["-c".to_string(), script],
cwd: Some(cwd),
env: Vec::new(),
session_id: None,
integration_id: None,
};
self.open_pty(profile);
}
/// Drain the PR list fetcher on every wizard pane each tick.
pub(crate) fn drain_new_cloud_agent_wizards(&mut self) {
for pane in self.panes.iter_mut() {
if let Pane::NewCloudAgentWizard(w) = pane {
let _ = w.drain();
}
}
}
/// Open a comprehensive cloud-agent run detail pane for the
/// row at `idx` in `cloud_agents_rows`. Aggregates summary,
/// web links, S3 artifacts, and CloudWatch logs into one pane.
/// Spawns the log + artifact fetcher workers immediately so
/// data starts streaming in by the first frame. Used by the
/// right-click "View run details" menu entry and the
/// `:cloud_agents.open_run` palette command.
pub fn open_cloud_agent_run(&mut self, idx: usize) {
let Some(row) = self.cloud_agents_rows.get(idx).cloned() else {
self.toast("cloud agent row not found");
return;
};
// Branch on source — managed agents have no AWS bits.
if matches!(
row.source,
crate::claude_agents::AgentSource::AnthropicManaged
) {
let mut pane = crate::cloud_agent_run::CloudAgentRunPane::new_managed(
row.session_id.clone(),
row.last_assistant_msg.clone().unwrap_or_default(),
row.state.badge().to_string(),
None,
Some(row.workspace.clone()),
);
// Live event stream from /v1/sessions/{id}/stream.
// Worker shells out to `curl -N` (http::send is
// sync); drained by the existing tick loop.
pane.session_event_rx = Some(crate::anthropic_api::spawn_session_event_stream(
row.session_id.clone(),
));
// Open as a tab in the focused leaf, not as a split —
// matches VS Code semantics. Push to panes vec, then
// reveal_pane() adds it to the active leaf's tabs.
self.panes.push(Pane::CloudAgentRun(pane));
let new_id = self.panes.len() - 1;
self.reveal_pane(new_id);
self.focus = Focus::Pane;
return;
}
let meta = self.cloud_agents_meta.get(&row.session_id).cloned();
let ticket = meta
.as_ref()
.map(|m| m.ticket.clone())
.unwrap_or_else(|| row.workspace.clone());
let flow = meta.as_ref().map(|m| m.flow.clone()).unwrap_or_default();
let state = meta
.as_ref()
.map(|m| m.state.clone())
.unwrap_or_else(|| "—".to_string());
let pr_url = meta.as_ref().and_then(|m| m.pr_url.clone());
// Prefer DynamoDB's recorded prefix; fall back to the
// configured `[cloud_agents] s3_artifacts_bucket` when
// missing (path convention: `artifacts/{flow}/{run_id}/`).
// Skipped entirely when no bucket is configured.
let s3_bucket = self.config.cloud_agents.s3_artifacts_bucket.clone();
let s3_prefix = meta
.as_ref()
.and_then(|m| m.s3_artifact_prefix.clone())
.or_else(|| {
if flow.is_empty() || s3_bucket.is_empty() {
None
} else {
Some(format!(
"s3://{s3_bucket}/artifacts/{flow}/{}/",
row.session_id
))
}
});
let s3_console = s3_prefix
.as_deref()
.and_then(crate::cloud_agent_run::s3_console_url_for);
let jira_url = crate::cloud_agent_run::jira_url_for(
&ticket,
self.config.jira.effective_domain().as_deref(),
);
let cloudwatch_url = meta
.as_ref()
.map(|m| m.cloudwatch_url(&row.session_id))
.unwrap_or_default();
let mut pane = crate::cloud_agent_run::CloudAgentRunPane::new(
row.session_id.clone(),
ticket,
flow,
state.clone(),
row.workspace.clone(),
row.last_activity,
jira_url,
pr_url,
cloudwatch_url,
s3_prefix.clone(),
s3_console,
);
pane.log_rx = Some(crate::cloud_agent_run::spawn_log_fetcher(
row.session_id.clone(),
state,
self.config.cloud_agents.log_group.clone(),
));
pane.artifacts_rx = Some(crate::cloud_agent_run::spawn_artifacts_fetcher(s3_prefix));
self.panes.push(Pane::CloudAgentRun(pane));
let new_id = self.panes.len() - 1;
self.reveal_pane(new_id);
self.focus = Focus::Pane;
}
/// Drain log + artifact channels on every CloudAgentRun pane.
/// Called from `App::tick`.
/// Pull any queued messages from cloud-run worker threads
/// and route them through the toast queue. Workers send
/// status / error strings; we surface them as toasts so the
/// user sees what happened without `eprintln!` corrupting
/// the ratatui frame.
pub(crate) fn drain_cloud_run_msgs(&mut self) {
let Some(rx) = self.cloud_run_msg_rx.as_ref() else {
return;
};
let msgs: Vec<String> = rx.try_iter().collect();
for msg in msgs {
// Sentinel: `__persist_defaults__|agent_id|env_id|sandbox|model`
// The wizard sends this after a successful submit so
// the Cloud Agents panel's quick-fire input gets a
// saved target. Don't toast it; route to config.
if let Some(rest) = msg.strip_prefix("__persist_defaults__|") {
let parts: Vec<&str> = rest.splitn(4, '|').collect();
if parts.len() == 4 {
self.config.cloud_run.defaults.agent_id = parts[0].to_string();
self.config.cloud_run.defaults.env_id = parts[1].to_string();
self.config.cloud_run.defaults.sandbox = parts[2].to_string();
self.config.cloud_run.defaults.model = parts[3].to_string();
match crate::config::persist_cloud_run_defaults(&self.config.cloud_run.defaults)
{
Ok(_) => self.toast("saved cloud-run defaults"),
Err(e) => self.toast(format!("save defaults: {e}")),
}
}
continue;
}
self.toast(msg);
}
}
pub(crate) fn drain_cloud_agent_run_panes(&mut self) {
for pane in self.panes.iter_mut() {
if let Pane::CloudAgentRun(p) = pane {
let _ = p.drain();
}
}
}
pub fn open_claude_agents_pane(&mut self) {
let anchor = self.workspace.clone();
// If the pane is already open, just focus it — don't
// rebuild. Rebuilding clobbers the user's sort key,
// filter state, multi-select, etc. They can press `r`
// inside the pane for a manual refresh.
if let Some(id) = self
.panes
.iter()
.position(|p| matches!(p, Pane::ClaudeAgents(_)))
{
self.reveal_pane(id);
return;
}
// #25 — use the background prefetch cache if the worker has
// finished; falls back to a synchronous build otherwise
// (fresh-launch case where the pane opens before the
// prefetch thread lands its rows).
let cached_rows = self
.claude_agents_prefetch
.lock()
.ok()
.and_then(|mut g| g.take());
let mut built = match cached_rows {
Some(rows) => {
crate::claude_agents::ClaudeAgentsPane::build_anchored_from_rows(anchor, rows)
}
None => crate::claude_agents::ClaudeAgentsPane::build_anchored(anchor),
};
// #25 v4 — apply the persisted age filter so it survives
// pane rebuilds + relaunches.
built.age_filter = self.claude_agents_last_age_filter;
let pane = Pane::ClaudeAgents(built);
match self.active {
Some(cur) => {
let new_id = self.split_leaf_with(cur, crate::layout::SplitDir::Vertical, pane);
self.active = Some(new_id);
}
None => {
self.panes.push(pane);
let id = self.panes.len() - 1;
*self.layout_mut() = Layout::leaf(id);
self.active = Some(id);
}
}
self.focus = Focus::Pane;
}
/// Refresh the active Claude Agents pane in place. Same effect
/// as `r` key in the pane. Preserves filter/group state.
pub fn refresh_claude_agents_pane(&mut self) {
let Some(i) = self.active else { return };
if matches!(self.panes.get(i), Some(Pane::ClaudeAgents(_)))
&& let Some(Pane::ClaudeAgents(c)) = self.panes.get_mut(i)
{
c.refresh_in_place();
}
}
pub fn claude_agents_toggle_workspace_only(&mut self) {
let Some(i) = self.active else { return };
let on = if let Some(Pane::ClaudeAgents(p)) = self.panes.get_mut(i) {
p.workspace_only = !p.workspace_only;
p.selected = 0;
Some(p.workspace_only)
} else {
None
};
if let Some(on) = on {
self.toast(if on {
"showing this workspace only"
} else {
"showing all workspaces"
});
}
}
/// #25 v4 — cycle the age filter on the active Claude
/// Agents pane. Order: Today → 7d → 30d → All → Today.
pub fn claude_agents_cycle_age(&mut self) {
let Some(i) = self.active else { return };
let new_filter = if let Some(Pane::ClaudeAgents(p)) = self.panes.get_mut(i) {
p.age_filter = p.age_filter.cycle();
p.selected = 0;
Some(p.age_filter)
} else {
None
};
if let Some(f) = new_filter {
// Persist across pane rebuilds + sessions.
self.claude_agents_last_age_filter = f;
self.toast(format!("age filter: {}", f.label()));
}
}
pub fn claude_agents_clear_filters(&mut self) {
let Some(i) = self.active else { return };
if let Some(Pane::ClaudeAgents(p)) = self.panes.get_mut(i) {
p.clear_filters();
}
self.toast("filters cleared");
}
/// `:ai.spend_today` — open the SpendReport pane: sortable
/// per-workspace breakdown of token + cost spend across every
/// Claude + Codex session touched in the last 24 hours.
///
/// 2026-06-21 — was a Markdown scratch (table) that the
/// editor rendered with full syntax-highlighting + cursor /
/// find etc., none of which makes sense for a read-only
/// financial report. Now a dedicated pane with clickable
/// column headers, sort cycling (`s` chord), and the
/// renderer is workspace / total counts in its title bar.
pub fn ai_spend_today(&mut self) {
// Re-use an existing SpendReport pane if one is open, else
// open fresh. Avoids accumulating 5 spend panes after a
// few uses.
if let Some(pid) = self
.panes
.iter()
.position(|p| matches!(p, Pane::SpendReport(_)))
{
if let Some(Pane::SpendReport(sr)) = self.panes.get_mut(pid) {
sr.refresh();
}
self.active = Some(pid);
self.focus_pane();
} else {
let pane = Pane::SpendReport(crate::pane::SpendReportPane::fresh());
match self.active {
Some(cur) => {
let new_id =
self.split_leaf_with(cur, crate::layout::SplitDir::Horizontal, pane);
self.active = Some(new_id);
}
None => {
self.panes.push(pane);
let id = self.panes.len() - 1;
*self.layout_mut() = crate::layout::Layout::leaf(id);
self.active = Some(id);
}
}
self.focus_pane();
}
// claude-agents 3rd 2026-06-29 SEV-3: the else branch
// here was always dead — the worker was just spawned and
// loading=true. The totals toast now fires from
// App::tick when poll_pending() drains the channel.
// We still toast the loading placeholder so the user
// knows the action registered.
self.toast("computing spend… (background)".to_string());
}
/// `:ai.session_search` — open a prompt for a search term.
/// Accept runs `claude_agents::search_all_transcripts` (greps
/// every .jsonl under ~/.claude/projects/) and dumps the hits
/// into a `[session-search]` scratch.
pub fn ai_session_search_prompt(&mut self) {
self.prompt = Some(crate::prompt::Prompt::new(
crate::prompt::PromptKind::ClaudeSessionSearch,
"search all Claude transcripts:".to_string(),
));
}
/// Accept handler — runs the search synchronously (grep is fast
/// enough for a few hundred MB; if it ever gets too slow we can
/// move to a worker).
pub fn ai_session_search_run(&mut self, query: String) {
// claude-agents SEV-2 2026-07-11: spawn the search on a
// background thread — a synchronous walk over 3000+ jsonl
// transcripts (3-4 GB) blocked the UI for 1.4-1.8s. `App::tick`
// drains the result via `ai_session_search_rx`.
if self.ai_session_search_rx.is_some() {
self.toast("session-search: already running…");
return;
}
let (tx, rx) = std::sync::mpsc::channel();
let q = query;
std::thread::spawn(move || {
let hits = crate::claude_agents::search_all_transcripts(&q);
let _ = tx.send((q, hits));
});
self.ai_session_search_rx = Some(rx);
self.toast("session-search: scanning transcripts…");
}
/// Drain the in-flight session-search result. Called from `App::tick`.
pub fn drain_ai_session_search(&mut self) {
let Some(rx) = self.ai_session_search_rx.as_ref() else {
return;
};
match rx.try_recv() {
Ok((query, hits)) => {
self.ai_session_search_rx = None;
if hits.is_empty() {
self.toast(format!("no matches for {query:?}"));
return;
}
let mut body = String::new();
body.push_str(&format!(
"# {} hits for {:?} across ~/.claude/projects/\n\n",
hits.len(),
query
));
use std::collections::BTreeMap;
let mut grouped: BTreeMap<String, Vec<&crate::claude_agents::SearchHit>> =
BTreeMap::new();
for h in &hits {
grouped.entry(h.workspace.clone()).or_default().push(h);
}
for (ws, hs) in grouped {
body.push_str(&format!("\n## {ws}\n\n"));
for h in hs {
let sid_short: String = h.session_id.chars().take(8).collect();
body.push_str(&format!(
"- [{}] {sid_short} · {}\n {}\n {}\n",
h.role.glyph(),
h.transcript_path.display(),
h.snippet,
"",
));
}
}
self.open_scratch_with_text("[session-search]".to_string(), body);
self.toast(format!("{} hits → [session-search]", hits.len()));
}
Err(std::sync::mpsc::TryRecvError::Empty) => {}
Err(std::sync::mpsc::TryRecvError::Disconnected) => {
self.ai_session_search_rx = None;
self.toast("session-search: worker died");
}
}
}
/// Tick hook — two refresh rates:
/// - every ~3s rebuild the full row set (newly-active
/// sessions, state transitions) via `refresh_in_place`.
/// - every tick re-tail JUST the selected row's transcript
/// when it's a live Claude session (`live_tail_selected`),
/// so the drill-down (todos, recent files, bash, cost,
/// tokens) updates ~10×/sec without waiting on the global
/// rebuild. Cursor stays put.
///
/// Both are paused by `paused_by_user` (toggle with `p`) and
/// the transient `paused` (filter-mode input active).
pub fn maybe_auto_refresh_claude_agents(&mut self) {
const REFRESH_EVERY_SECS: u64 = 3;
let now = std::time::SystemTime::now();
for i in 0..self.panes.len() {
let (do_full, do_tail) = match self.panes.get(i) {
Some(Pane::ClaudeAgents(p)) if !p.paused && !p.paused_by_user => {
let full = now
.duration_since(p.built_at)
.map(|d| d.as_secs() >= REFRESH_EVERY_SECS)
.unwrap_or(false);
let tail = now
.duration_since(p.last_live_tail)
.map(|d| d.as_millis() >= 500)
.unwrap_or(true);
(full, tail)
}
_ => (false, false),
};
let transitions = if do_full && let Some(Pane::ClaudeAgents(p)) = self.panes.get_mut(i)
{
p.refresh_in_place();
Some(p.compute_transitions())
} else {
None
};
if let Some(msgs) = transitions {
for msg in msgs.into_iter().take(3) {
self.toast(msg);
}
}
if do_tail && let Some(Pane::ClaudeAgents(p)) = self.panes.get_mut(i) {
p.live_tail_selected();
}
}
}
/// 2026-06-21 — right-click on a Files drill-down row in the
/// dashboard. Surfaces 4 actions for the clicked file:
/// Open (single-pane), Reveal in tree, Yank workspace-relative
/// path, Copy file contents to a scratch buffer.
pub fn open_dashboard_file_context_menu(&mut self, path: String, anchor: (u16, u16)) {
use crate::context_menu::{ContextMenu, MenuAction, MenuItem};
let pb = std::path::PathBuf::from(&path);
let rel = crate::app::rel_path(&self.workspace, &pb);
let title = pb
.file_name()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or_else(|| path.clone());
let items = vec![
MenuItem::new("Open", MenuAction::OpenPath(pb.clone())),
MenuItem::new("Reveal in tree", MenuAction::RevealInFinder(pb.clone())),
MenuItem::new("Yank path", MenuAction::CopyPath(rel)),
MenuItem::new("Open externally", MenuAction::OpenExternally(pb)),
];
self.context_menu = Some(ContextMenu::new(Some(title), anchor, items));
}
/// 2026-06-21 vscode-mouse SEV-2: right-click on a dashboard
/// row opens a context menu with the 7 row actions surfaced.
/// Row is selected at click time so the menu acts on the row
/// the user actually clicked.
pub fn open_dashboard_row_context_menu(
&mut self,
pid: usize,
row_idx: usize,
anchor: (u16, u16),
) {
use crate::context_menu::{ContextMenu, MenuAction, MenuItem};
let Some(Pane::ClaudeAgents(p)) = self.panes.get_mut(pid) else {
return;
};
p.selected = row_idx.min(p.visible_indices().len().saturating_sub(1));
let title = p
.selected_row()
.map(|r| {
let sid_short = &r.session_id[..8.min(r.session_id.len())];
format!("{} · {}", r.workspace, sid_short)
})
.unwrap_or_else(|| "session".to_string());
let items = vec![
MenuItem::new(
"Open transcript",
MenuAction::Command("ai.dashboard.open_transcript"),
),
MenuItem::new(
"Resume in mnml pty",
MenuAction::Command("ai.dashboard.resume_in_pty"),
),
MenuItem::new(
"Yank session id",
MenuAction::Command("ai.dashboard.yank_session_id"),
),
MenuItem::new("Yank cwd", MenuAction::Command("ai.dashboard.yank_cwd")),
MenuItem::new(
"Export as markdown",
MenuAction::Command("ai.dashboard.export_markdown"),
),
MenuItem::new("Kill session…", MenuAction::Command("ai.dashboard.kill")),
];
self.active = Some(pid);
self.context_menu = Some(ContextMenu::new(Some(title), anchor, items));
}
/// Yank the selected row's session id (or open transcript) — `y` / `t`.
pub fn claude_agents_action(&mut self, action: crate::claude_agents::ClaudeAgentsAction) {
use crate::claude_agents::ClaudeAgentsAction;
let Some(i) = self.active else { return };
let Some(Pane::ClaudeAgents(p)) = self.panes.get(i) else {
return;
};
let Some(row) = p.selected_row() else { return };
match action {
ClaudeAgentsAction::YankSessionId => {
let sid = row.session_id.clone();
self.clipboard.set(sid.clone(), false);
self.toast(format!("yanked session id {sid}"));
}
ClaudeAgentsAction::OpenTranscript => {
let path = row.transcript_path.clone();
if path.as_os_str().is_empty() {
self.toast("no transcript on disk for this session");
return;
}
self.open_path(&path);
}
ClaudeAgentsAction::YankCwd => {
if let Some(cwd) = row.cwd.clone() {
self.clipboard.set(cwd.clone(), false);
self.toast(format!("yanked cwd {cwd}"));
} else {
self.toast("no cwd recorded for that session");
}
}
ClaudeAgentsAction::KillPrompt => {
// Batch mode: if any rows are multi-selected, kill
// them all (after one confirm prompt).
// claude-agents 2026-06-28 findings 2+3: count the
// multi-selected set's TOTAL size (live + ended)
// even though we only kill the live ones. The
// confirm prompt now reads "kill N of M selected"
// so the user understands ended sessions are
// skipped, instead of seeing the chip say ☑5 then
// the prompt say "SIGTERM 2 sessions" (silent loss
// of 3 from the count).
let (batch, total_selected): (Vec<(String, u32)>, usize) =
if let Some(Pane::ClaudeAgents(pane)) = self.panes.get(i) {
(pane.multi_selected_pids(), pane.multi_selected.len())
} else {
(Vec::new(), 0)
};
// Finding #2: when the user multi-selected sessions
// but they're ALL ended (batch empty, total > 0),
// tell them — don't silently fall through to the
// single-row "no PID" toast.
if batch.is_empty() && total_selected > 0 {
self.toast(format!(
"all {total_selected} selected sessions already ended — nothing to kill"
));
return;
}
if !batch.is_empty() {
self.pending_kill_batch = batch.clone();
self.pending_kill_pid = None;
let n = batch.len();
let prompt_text = if n == total_selected {
format!("SIGTERM {n} selected session(s)?")
} else {
let skipped = total_selected - n;
format!(
"SIGTERM {n} of {total_selected} selected? ({skipped} already ended, skipped)"
)
};
let mut p = crate::prompt::Prompt::new(
crate::prompt::PromptKind::ClaudeKillConfirm,
prompt_text,
);
p.cursor = 1;
self.prompt = Some(p);
return;
}
let Some(pid) = row.pid else {
self.toast("no PID — session already ended");
return;
};
let sid = row.session_id.clone();
let workspace = row.workspace.clone();
self.pending_kill_pid = Some(pid);
let mut p = crate::prompt::Prompt::new(
crate::prompt::PromptKind::ClaudeKillConfirm,
format!(
"SIGTERM PID {pid}? ({workspace} · {})",
sid.chars().take(8).collect::<String>()
),
);
p.cursor = 1;
self.prompt = Some(p);
}
ClaudeAgentsAction::ExportMarkdown => {
match crate::claude_agents::export_transcript_as_markdown(row) {
Ok((stem, md)) => {
let dir = self.workspace.join(".mnml").join("claude-exports");
if let Err(e) = std::fs::create_dir_all(&dir) {
self.toast(format!("export: mkdir {}: {e}", dir.display()));
return;
}
let path = dir.join(format!("{stem}.md"));
match std::fs::write(&path, &md) {
Ok(()) => {
self.toast(format!("exported → {}", path.display()));
self.open_path(&path);
}
Err(e) => self.toast(format!("export write: {e}")),
}
}
Err(e) => self.toast(format!("export: {e}")),
}
}
ClaudeAgentsAction::ResumeSession => {
use crate::claude_agents::AgentSource;
let sid = row.session_id.clone();
let cwd = row
.cwd
.as_deref()
.map(std::path::PathBuf::from)
.unwrap_or_else(|| self.workspace.clone());
match row.source {
AgentSource::Claude => {
let profile =
crate::pty_pane::BinaryProfile::claude_code_resume(cwd, sid.clone());
self.open_pty(profile);
self.toast(format!(
"resuming claude session {}…",
sid.chars().take(8).collect::<String>()
));
}
AgentSource::Codex => {
// Codex CLI is stateless — open a fresh codex
// pty in the row's cwd. (If a future codex
// gains --resume, swap this for the resume
// profile.)
let profile = crate::pty_pane::BinaryProfile::codex(cwd);
self.open_pty(profile);
self.toast("opened fresh codex (CLI is stateless)");
}
AgentSource::Ecs => {
// Cloud row — no local resume. Surface
// the runId / state in a toast so the user
// can copy it / open CloudWatch logs.
self.toast(format!("ecs run {sid} — cloud row, no local resume"));
}
AgentSource::AnthropicManaged => {
// Managed Agents session lives on
// Anthropic's side — surface the
// session id so user can open it in the
// console.
self.toast(format!(
"managed-agent {sid} — open at https://platform.claude.com/sessions/{sid}"
));
}
}
}
}
}
/// Fire SIGTERM at the pending kill targets. Used by the
/// `PromptKind::ClaudeKillConfirm` accept path. Handles both
/// single-PID (`pending_kill_pid`) and batch
/// (`pending_kill_batch`) targets. PIDs that survive 2s of
/// TERM are escalated to SIGKILL by
/// `maybe_escalate_claude_kills` (tick hook).
pub fn claude_agents_kill_confirmed(&mut self) {
let now = std::time::SystemTime::now();
if !self.pending_kill_batch.is_empty() {
let batch = std::mem::take(&mut self.pending_kill_batch);
let mut killed = 0usize;
let mut failed = 0usize;
let mut killed_sids: Vec<String> = Vec::new();
let mut escalation: Vec<(u32, std::time::SystemTime)> = Vec::new();
for (sid, pid) in &batch {
let out = std::process::Command::new("kill")
.arg("-TERM")
.arg(pid.to_string())
.output();
match out {
Ok(o) if o.status.success() => {
killed += 1;
killed_sids.push(sid.clone());
escalation.push((*pid, now));
}
_ => failed += 1,
}
}
if let Some(i) = self.active
&& let Some(Pane::ClaudeAgents(p)) = self.panes.get_mut(i)
{
for (pid, ts) in escalation {
p.kill_escalation.insert(pid, ts);
}
}
// Drop the killed sids from the multi-select set.
if let Some(i) = self.active
&& let Some(Pane::ClaudeAgents(p)) = self.panes.get_mut(i)
{
for sid in &killed_sids {
p.multi_selected.remove(sid);
}
}
self.toast(format!("SIGTERM → {killed} killed, {failed} failed"));
self.refresh_claude_agents_pane();
return;
}
let Some(pid) = self.pending_kill_pid.take() else {
return;
};
let out = std::process::Command::new("kill")
.arg("-TERM")
.arg(pid.to_string())
.output();
let mut sent_term = false;
match out {
Ok(o) if o.status.success() => {
self.toast(format!("SIGTERM → {pid}"));
sent_term = true;
}
Ok(o) => {
let err = String::from_utf8_lossy(&o.stderr);
self.toast(format!("kill {pid} failed: {err}"));
}
Err(e) => self.toast(format!("kill {pid}: {e}")),
}
if sent_term
&& let Some(i) = self.active
&& let Some(Pane::ClaudeAgents(p)) = self.panes.get_mut(i)
{
p.kill_escalation.insert(pid, now);
}
self.refresh_claude_agents_pane();
}
/// Tick hook — for every PID we SIGTERM'd more than 2s ago, if
/// it's still alive escalate to SIGKILL. Drops the entry on
/// successful escalation OR PID disappearance.
pub fn maybe_escalate_claude_kills(&mut self) {
const ESCALATE_AFTER_SECS: u64 = 2;
let now = std::time::SystemTime::now();
let mut to_kill: Vec<u32> = Vec::new();
let mut to_drop: Vec<u32> = Vec::new();
for i in 0..self.panes.len() {
let Some(Pane::ClaudeAgents(p)) = self.panes.get(i) else {
continue;
};
for (pid, ts) in &p.kill_escalation {
let age = now.duration_since(*ts).map(|d| d.as_secs()).unwrap_or(0);
if age < ESCALATE_AFTER_SECS {
continue;
}
// `kill -0 PID` returns 0 if the process is still
// alive (without sending any signal).
let alive = std::process::Command::new("kill")
.args(["-0", &pid.to_string()])
.output()
.map(|o| o.status.success())
.unwrap_or(false);
if alive {
to_kill.push(*pid);
} else {
to_drop.push(*pid);
}
}
}
for pid in to_kill {
let out = std::process::Command::new("kill")
.args(["-KILL", &pid.to_string()])
.output();
if let Ok(o) = out
&& o.status.success()
{
self.toast(format!("SIGKILL → {pid} (TERM ignored)"));
to_drop.push(pid);
}
}
for i in 0..self.panes.len() {
if let Some(Pane::ClaudeAgents(p)) = self.panes.get_mut(i) {
for pid in &to_drop {
p.kill_escalation.remove(pid);
}
}
}
}
}