rho-coding-agent 1.45.0

A lightweight agent harness inspired by Pi
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
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
//! End-to-end Rho TUI scenarios driven through a PTY.
//!
//! These tests require a Unix PTY and a debug-built `rho` binary with the
//! fixture matrix (`RHO_TUI_TEST_MODE=matrix`).

#![cfg(unix)]

#[path = "support/claude_e2e.rs"]
mod claude_e2e;

use std::{
    fs::{self, OpenOptions},
    io::{BufRead, BufReader, Write},
    os::unix::net::UnixListener,
    path::PathBuf,
    sync::{Arc, Mutex},
    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};

use rho_tui_pty::{
    run_named, smoke_scenario_ids, IsolatedHome, Key, PtyHarness, PtySize, RhoLaunchPlan,
    ScenarioRunner, WaitTimeout,
};

fn runner() -> ScenarioRunner {
    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let artifacts = std::env::temp_dir().join("rho-pty-test-artifacts");
    ScenarioRunner::new(binary).with_artifacts(artifacts)
}

fn assert_pass(name: &str) {
    let outcome = run_named(&runner(), name).expect("scenario runner error");
    assert!(
        outcome.passed,
        "scenario {name} failed:\n{}",
        outcome.message
    );
}

fn confirm_claude_code_login(harness: &mut PtyHarness) {
    harness
        .wait_for_text(
            "Hand the terminal to Claude Code?",
            WaitTimeout::secs(10, "login confirmation"),
        )
        .unwrap();
    // Cancel is the default option, so pick Continue by its shortcut.
    harness.inject_key(&Key::Char('2')).unwrap();
}

#[test]
fn smoke_startup_stream_exit() {
    assert_pass("startup_stream_exit");
}

// Covers: first session chrome must paint without waiting on MCP, catalog, or keyring tails.
// Owner: interactive TUI
#[test]
fn startup_first_frame_paints_session_chrome() {
    assert_pass("startup_first_frame");
}

// Covers: /mcp must open and report in-flight servers while connect is still running.
// Owner: interactive TUI
#[test]
fn mcp_connecting_keeps_the_session_inspectable() {
    assert_pass("mcp_connecting");
}

// Covers: a turn held during MCP connect must start on its own once the
// servers settle, without a second Enter.
// Owner: interactive TUI
#[test]
fn mcp_connect_release_starts_the_held_turn() {
    assert_pass("mcp_connect_release");
}

// Covers: esc must return a turn held during MCP connect to the composer.
// Owner: interactive TUI
#[test]
fn mcp_hold_take_back_returns_the_prompt() {
    assert_pass("mcp_hold_take_back");
}

#[test]
fn smoke_cancel_and_resubmit() {
    assert_pass("cancel_and_resubmit");
}

#[test]
fn smoke_type_during_stream() {
    assert_pass("type_during_stream");
}

// Covers: /compact must keep the composer usable so typed keys are not dropped.
// Owner: interactive TUI
#[test]
fn type_during_compact() {
    assert_pass("type_during_compact");
}

// Covers: Enter during /compact queues a follow-up that starts after a failed compact.
// Owner: interactive TUI
#[test]
fn submit_during_compact() {
    assert_pass("submit_during_compact");
}

#[test]
fn smoke_resize_during_stream() {
    assert_pass("resize_during_stream");
}

#[test]
fn smoke_scroll_during_stream() {
    assert_pass("scroll_during_stream");
}

#[test]
fn smoke_terminal_restoration() {
    assert_pass("terminal_restoration");
}

// Covers: an interactive workflow must use its own resizable screen, show typed parallel and
// exclusive node states, and restore the terminal after a durable completion.
// Owner: interactive TUI
#[test]
fn workflow_run_uses_separate_terminal_mode() {
    assert_pass("workflow_run_interactive");
}

// Covers: terminal cancellation must reach durable resumable state, and resume must not rerun a
// node that already succeeded.
// Owner: interactive TUI
#[test]
fn workflow_cancel_then_resume_preserves_completed_nodes() {
    assert_pass("workflow_cancel_resume");
}

// Covers: pasting an absolute document path must attach extracted text instead of parsing it as a
// slash command.
// Owner: interactive TUI
#[test]
fn absolute_document_path_paste_attaches_and_submits_text() {
    assert_pass("document_attachment");
}

#[test]
fn edit_diff_streams_and_survives_cancellation() {
    assert_pass("edit_diff");
}

// Covers: enabling fast mode must update the persistent model indicator without a restart.
// Owner: interactive TUI
#[test]
fn fast_mode_appears_beside_the_active_model() {
    let home = IsolatedHome::new().unwrap();
    fs::write(
        &home.config_path,
        r#"provider = "openai-codex"
model = "gpt-5.5"
auth = "codex"
check_for_updates = false
web_search_provider = "disabled"

[behavior]
credential_store = "file"
"#,
    )
    .unwrap();
    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 28,
            cols: 100,
        },
    );
    let mut harness = PtyHarness::spawn_named(&plan, "fast_mode_statusline").unwrap();
    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();

    harness.submit_text("/fast on").unwrap();
    harness
        .wait_for_text(
            "gpt-5.5 (fast)",
            WaitTimeout::secs(10, "fast mode statusline"),
        )
        .unwrap();
    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
}

#[test]
fn claude_code_login_hands_terminal_to_fake_claude() {
    let home = IsolatedHome::new().unwrap();
    // Ensure /login claude-code does not stop at credential-store choice.
    std::fs::write(
        &home.config_path,
        r#"provider = "openai"
model = "gpt-5.5"
auth = "api-key"
check_for_updates = false
web_search_provider = "disabled"

[behavior]
credential_store = "file"
"#,
    )
    .unwrap();

    let fake = claude_e2e::install_fake_claude_login();
    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 28,
            cols: 100,
        },
    )
    .with_env("PATH", &fake.path);
    let mut harness = PtyHarness::spawn_named(&plan, "claude_code_login").unwrap();
    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();

    harness.submit_text("/login claude-code").unwrap();
    confirm_claude_code_login(&mut harness);
    harness
        .wait_for_text(
            "handing the terminal to the claude binary",
            WaitTimeout::secs(10, "handoff notice"),
        )
        .unwrap();
    // The fake claude process exits immediately, so its stdout may only appear on
    // the suspended main screen. Prefer the post-status source of truth.
    harness
        .wait_for_text(
            "signed in as fake@example.com",
            WaitTimeout::secs(10, "post-login status"),
        )
        .unwrap();
    harness
        .wait_for_text(
            "Managed by the claude binary",
            WaitTimeout::secs(10, "ownership copy"),
        )
        .unwrap();
    assert!(fake.marker.exists(), "fake claude login should have run");
    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
}

#[test]
fn login_shows_provider_picker_before_credential_store_choice() {
    let home = IsolatedHome::new().unwrap();
    // Deliberately leave behavior.credential_store unset so first normal
    // provider login must choose a store after the group picker.
    std::fs::write(
        &home.config_path,
        r#"provider = "openai"
model = "gpt-5.5"
auth = "api-key"
check_for_updates = false
web_search_provider = "disabled"
"#,
    )
    .unwrap();
    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 28,
            cols: 100,
        },
    );
    let mut harness = PtyHarness::spawn_named(&plan, "login_provider_then_store").unwrap();

    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();

    // Bare /login opens the group picker first, not the store chooser.
    harness.submit_text("/login").unwrap();
    harness
        .wait_for_text(
            "select provider to login",
            WaitTimeout::secs(10, "group picker first"),
        )
        .unwrap();
    let screen = harness.screen().contents();
    assert!(
        !screen.contains("Where should Rho store provider credentials?"),
        "store chooser must wait until a normal provider is selected:\n{screen}"
    );
    assert!(
        !screen.contains("Claude Code (delegation only)"),
        "claude-code belongs under Anthropic methods, not the top-level group picker:\n{screen}"
    );
    assert!(
        screen.contains("Anthropic"),
        "Anthropic group must remain in the bare login picker:\n{screen}"
    );

    // Filter to OpenAI so the test does not depend on picker sort order.
    harness.type_text("openai").unwrap();
    harness
        .wait_for_text("OpenAI", WaitTimeout::secs(5, "openai filtered"))
        .unwrap();
    harness.inject_key(&Key::Enter).unwrap();
    harness
        .wait_for_text(
            "select OpenAI login method",
            WaitTimeout::secs(10, "openai methods"),
        )
        .unwrap();
    // API Key is the first method.
    harness.inject_key(&Key::Enter).unwrap();
    harness
        .wait_for_text(
            "Where should Rho store provider credentials?",
            WaitTimeout::secs(10, "store after provider"),
        )
        .unwrap();
    harness
        .wait_for_text("Local file", WaitTimeout::secs(5, "file option"))
        .unwrap();
    harness.inject_key(&Key::Esc).unwrap();
    harness
        .wait_for_quiet(
            Duration::from_millis(150),
            WaitTimeout::secs(5, "after cancel"),
        )
        .unwrap();

    // Direct provider args still prompt for the store before secrets.
    harness.submit_text("/login openai").unwrap();
    harness
        .wait_for_text(
            "Where should Rho store provider credentials?",
            WaitTimeout::secs(10, "store for direct provider"),
        )
        .unwrap();
    harness.inject_key(&Key::Char('2')).unwrap();
    harness
        .wait_for_text(
            "credential store set to file",
            WaitTimeout::secs(10, "store persisted"),
        )
        .unwrap();
    harness
        .wait_for_text("enter", WaitTimeout::secs(10, "api key prompt"))
        .unwrap();
    harness.inject_key(&Key::Esc).unwrap();
    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);

    let config = std::fs::read_to_string(&home.config_path).unwrap();
    assert!(
        config.contains("credential_store = \"file\""),
        "chooser should persist file backend:\n{config}"
    );

    // Second /login must not re-prompt once config is set.
    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 28,
            cols: 100,
        },
    );
    let mut harness = PtyHarness::spawn_named(&plan, "login_provider_then_store_again").unwrap();
    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup again"))
        .unwrap();
    harness.submit_text("/login").unwrap();
    harness
        .wait_for_text(
            "select provider to login",
            WaitTimeout::secs(10, "no second chooser"),
        )
        .unwrap();
    let screen = harness.screen().contents();
    assert!(
        !screen.contains("Where should Rho store provider credentials?"),
        "chooser should not reappear after config is set:\n{screen}"
    );
    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
}

#[test]
fn login_claude_code_skips_credential_store_when_unset() {
    let home = IsolatedHome::new().unwrap();
    // Leave credential_store unset. Claude login must never ask for it.
    std::fs::write(
        &home.config_path,
        r#"provider = "openai"
model = "gpt-5.5"
auth = "api-key"
check_for_updates = false
web_search_provider = "disabled"
"#,
    )
    .unwrap();

    let fake = claude_e2e::install_fake_claude_login();
    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 28,
            cols: 100,
        },
    )
    .with_env("PATH", &fake.path);
    let mut harness = PtyHarness::spawn_named(&plan, "claude_code_login_no_store").unwrap();
    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();

    harness.submit_text("/login claude-code").unwrap();
    confirm_claude_code_login(&mut harness);
    harness
        .wait_for_text(
            "handing the terminal to the claude binary",
            WaitTimeout::secs(10, "handoff notice"),
        )
        .unwrap();
    let screen = harness.screen().contents();
    assert!(
        !screen.contains("Where should Rho store provider credentials?"),
        "claude-code must never open the Rho store chooser:\n{screen}"
    );
    harness
        .wait_for_text(
            "signed in as fake@example.com",
            WaitTimeout::secs(10, "post-login status"),
        )
        .unwrap();
    assert!(fake.marker.exists(), "fake claude login should have run");
    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);

    let config = std::fs::read_to_string(&home.config_path).unwrap();
    assert!(
        !config.contains("credential_store"),
        "claude login must not write Rho credential_store:\n{config}"
    );
}

// Covers: first-time /login claude-code cancel must not start the claude binary
// Owner: interactive UX
#[test]
fn login_claude_code_cancel_stays_in_rho() {
    let home = IsolatedHome::new().unwrap();
    std::fs::write(
        &home.config_path,
        r#"provider = "openai"
model = "gpt-5.5"
auth = "api-key"
check_for_updates = false
web_search_provider = "disabled"

[behavior]
credential_store = "file"
"#,
    )
    .unwrap();

    let fake = claude_e2e::install_fake_claude_login();
    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 28,
            cols: 100,
        },
    )
    .with_env("PATH", &fake.path);
    let mut harness = PtyHarness::spawn_named(&plan, "claude_code_login_cancel").unwrap();
    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();

    harness.submit_text("/login claude-code").unwrap();
    harness
        .wait_for_text(
            "Hand the terminal to Claude Code?",
            WaitTimeout::secs(10, "login confirmation"),
        )
        .unwrap();
    // A stray Enter must take the default option, and the default must be Cancel.
    harness.inject_key(&Key::Enter).unwrap();
    harness
        .wait_for_quiet(
            Duration::from_millis(150),
            WaitTimeout::secs(5, "after cancel"),
        )
        .unwrap();
    assert!(
        !fake.marker.exists(),
        "cancel must not run claude auth login"
    );
    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
}

#[test]
fn model_command_resolves_configured_alias() {
    let home = IsolatedHome::new().unwrap();
    std::fs::write(
        &home.config_path,
        r#"check_for_updates = false
web_search_provider = "disabled"

[model]
provider = "openai"
model = "gpt-5.5"
auth = "api-key"

[model.aliases]
deep = "openai-codex/gpt-5.5"
"#,
    )
    .unwrap();
    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 28,
            cols: 100,
        },
    );
    let mut harness = PtyHarness::spawn_named(&plan, "resolve_model_alias").unwrap();

    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();
    harness.submit_text("/model @deep").unwrap();
    harness
        .wait_for_text(
            "model switched to openai-codex/gpt-5.5",
            WaitTimeout::secs(10, "model switch"),
        )
        .unwrap();
    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);

    let config = std::fs::read_to_string(&home.config_path).unwrap();
    assert!(
        config.contains("model = \"@deep\""),
        "saved config:\n{config}"
    );
}

#[test]
fn first_launch_walks_the_full_screen_setup() {
    assert_pass("first_run_setup");
}

#[test]
fn first_launch_setup_can_be_skipped_into_a_session() {
    assert_pass("first_run_setup_skipped");
}

#[test]
fn signed_out_session_offers_login_from_header_statusline_and_prompt() {
    assert_pass("signed_out_setup_state");
}

#[test]
fn runtime_info_reflows_after_narrow_resize() {
    assert_pass("runtime_info");
}

// Covers: /limits must open a single-pane overlay and Esc must return to the session.
// Owner: interactive TUI
#[test]
fn limits_overlay_opens_and_dismisses() {
    assert_pass("limits_overlay");
}

// Covers: fragile interactive surfaces from issue #711.
// Owner: interactive TUI
#[test]
fn fragile_surface_scenarios_pass() {
    for id in [
        "markdown_headings",
        "streaming_markdown_stability",
        "spinner_activity_anchor",
        "spinner_activity_jump_rail",
        "help_overlay",
        "slash_command_palette",
        "file_path_autocomplete",
    ] {
        assert_pass(id);
    }
}

// Covers: advisor mode must ask for a model before it claims to be on, keep the
// chosen model across off and on, warn when a saved mode has no model, and bring
// the advisor's answer back to the executor without ending the turn on failure.
// Owner: interactive TUI
#[test]
fn advisor_mode_scenarios_pass() {
    for id in ["advisor_command", "advisor_missing_model", "advisor_review"] {
        assert_pass(id);
    }
}

#[test]
fn mermaid_flowchart_survives_narrow_and_restored_panes() {
    assert_pass("mermaid_flowchart_resize");
}

#[test]
fn bare_skill_command_starts_a_model_turn() {
    let home = IsolatedHome::new().unwrap();
    let skill_dir = home.workspace.join(".agents/skills/test-skill");
    std::fs::create_dir_all(&skill_dir).unwrap();
    std::fs::write(
        skill_dir.join("SKILL.md"),
        "---\nname: test-skill\ndescription: Test skill invocation\ndisable-model-invocation: true\n---\nFollow the unique bare skill instruction.\n",
    )
    .unwrap();
    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 28,
            cols: 100,
        },
    );
    let mut harness = PtyHarness::spawn_named(&plan, "bare_skill_command").unwrap();

    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();
    harness.submit_text("/skill:test-skill").unwrap();
    harness
        .wait_for_text(
            "skill command loaded before model response: Follow the unique bare skill instruction.",
            WaitTimeout::secs(20, "skill response"),
        )
        .unwrap();

    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
}

#[test]
fn skill_command_reports_when_skill_tool_is_disabled() {
    let home = IsolatedHome::new().unwrap();
    let skill_dir = home.workspace.join(".agents/skills/test-skill");
    std::fs::create_dir_all(&skill_dir).unwrap();
    std::fs::write(
        skill_dir.join("SKILL.md"),
        "---\nname: test-skill\ndescription: Test skill invocation\n---\nFollow the skill.\n",
    )
    .unwrap();
    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 28,
            cols: 100,
        },
    )
    .with_arg("--no-tools");
    let mut harness = PtyHarness::spawn_named(&plan, "disabled_skill_command").unwrap();

    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();
    harness.submit_text("/skill:test-skill").unwrap();
    harness
        .wait_for_text(
            "skill commands are unavailable because the active agent has no skill tool",
            WaitTimeout::secs(5, "skill unavailable"),
        )
        .unwrap();

    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
}

#[test]
fn goal_waits_for_subagents_before_evaluation() {
    assert_pass("goal_waits_for_subagents");
}

#[test]
fn goal_answers_background_agent_questionnaire_while_waiting() {
    assert_pass("goal_questionnaire");
}

#[test]
fn goal_waits_for_subagents_before_retrying() {
    assert_pass("goal_waits_for_subagents_during_retry");
}

#[test]
fn background_agent_completion_is_delivered_after_turn_end() {
    assert_pass("background_agent_auto_delivery");
}

#[test]
fn subagent_rail_mouse_activation_uses_release_and_survives_refresh() {
    assert_pass("subagent_rail_mouse");
}

// Covers: /attach must open a full-screen picker of running subagents by role.
// Owner: interactive TUI
#[test]
fn attach_opens_a_picker_of_running_subagents() {
    assert_pass("attach_picker");
}

// Covers: /attach with no running subagents must still open an empty overlay.
// Owner: interactive TUI
#[test]
fn attach_opens_an_empty_picker_when_nothing_is_running() {
    assert_pass("attach_picker_empty");
}

// Covers: `rho attach` with no id must open the picker even when the directory
// has no running subagents.
// Owner: interactive TUI
#[test]
fn cli_attach_opens_an_empty_picker_when_nothing_is_running() {
    assert_pass("attach_cli_empty");
}

// Covers: a started process must stay on the activity rail after the turn ends.
// Owner: interactive TUI
#[test]
fn process_rail_stays_visible_after_turn_ends() {
    assert_pass("process_rail");
}

#[test]
fn text_selection_highlight_follows_drag_before_release() {
    assert_pass("text_selection_drag");
}

// Covers: hovering a collapsed tool card lifts its text and reverts on exit,
// and click-to-expand still works through the shared hit-test path.
// Owner: interactive TUI
#[test]
fn tool_card_hover_lifts_text_and_expands_on_click() {
    assert_pass("tool_card_hover");
}

#[test]
fn screen_text_selection_copies_composer_text() {
    assert_pass("screen_text_selection");
}

#[test]
fn background_agent_questionnaire_is_answered_in_parent_tui() {
    assert_pass("background_agent_questionnaire");
}

#[test]
fn attach_is_read_only_and_updates_live() {
    let home = IsolatedHome::new().unwrap();
    let directory = home.home.join(".rho/subagents/abc123");
    std::fs::create_dir_all(&directory).unwrap();
    std::fs::write(
        directory.join("result.json"),
        r#"{
            "state": "running",
            "agent_id": "explorer",
            "provider": "openai",
            "model": "gpt-5.5",
            "runtime": "rho",
            "started_at": 1700000000,
            "turns": 1,
            "input_tokens": 12,
            "output_tokens": 3,
            "last_activity": "assistant text"
        }"#,
    )
    .unwrap();
    let events = directory.join("events.jsonl");
    std::fs::write(
        &events,
        "{\"type\":\"prompt\",\"data\":\"delegated task\"}\n",
    )
    .unwrap();
    let socket = home.path().join("herdr.sock");
    let listener = UnixListener::bind(&socket).unwrap();
    let requests = Arc::new(Mutex::new(Vec::new()));
    let server_requests = Arc::clone(&requests);
    let server = std::thread::spawn(move || {
        for _ in 0..4 {
            let (mut stream, _) = listener.accept().unwrap();
            let mut line = String::new();
            BufReader::new(stream.try_clone().unwrap())
                .read_line(&mut line)
                .unwrap();
            server_requests
                .lock()
                .unwrap()
                .push(serde_json::from_str::<serde_json::Value>(&line).unwrap());
            stream.write_all(b"{}\n").unwrap();
        }
    });
    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(binary, &home, PtySize { rows: 24, cols: 90 })
        .with_arg("attach")
        .with_arg("abc123")
        .with_env("HERDR_ENV", "1")
        .with_env("HERDR_SOCKET_PATH", socket.display().to_string())
        .with_env("HERDR_PANE_ID", "%attach");
    let mut harness = PtyHarness::spawn_named(&plan, "attach_read_only").unwrap();

    harness
        .wait_for_text("attach abc123", WaitTimeout::secs(10, "attach startup"))
        .unwrap();
    harness
        .wait_for_text(
            "openai/gpt-5.5 · rho",
            WaitTimeout::secs(5, "provider model runtime"),
        )
        .unwrap();
    harness
        .wait_for_text("delegated task", WaitTimeout::secs(5, "delegated prompt"))
        .unwrap();
    harness.type_text("must not become a prompt").unwrap();
    harness.inject_key(&Key::Enter).unwrap();
    harness
        .wait_for_quiet(
            Duration::from_millis(200),
            WaitTimeout::secs(5, "ignored input"),
        )
        .unwrap();
    assert!(!harness.screen().contains_text("must not become a prompt"));

    let mut file = OpenOptions::new().append(true).open(&events).unwrap();
    writeln!(
        file,
        "{{\"type\":\"assistant_text_delta\",\"data\":\"watchable answer\"}}"
    )
    .unwrap();
    file.flush().unwrap();
    harness
        .wait_for_text("watchable answer", WaitTimeout::secs(5, "live event"))
        .unwrap();
    assert!(harness.screen().contains_text("read-only"));
    std::fs::write(
        directory.join("result.json"),
        r#"{
            "state": "ok",
            "agent_id": "explorer",
            "provider": "openai",
            "model": "gpt-5.5",
            "runtime": "rho",
            "started_at": 1700000000,
            "finished_at": 1700000065,
            "turns": 1,
            "input_tokens": 12,
            "output_tokens": 3,
            "last_activity": "complete",
            "result": "watchable answer"
        }"#,
    )
    .unwrap();
    harness
        .wait_for_text("complete", WaitTimeout::secs(5, "completion activity"))
        .unwrap();
    harness
        .wait_for_text("1m 05s", WaitTimeout::secs(5, "finished elapsed"))
        .unwrap();
    assert!(harness.screen().contains_text("explorer"));
    assert!(harness.screen().contains_text("ok"));

    harness.inject_key(&Key::Char('q')).unwrap();
    assert_eq!(
        harness
            .wait_for_exit(WaitTimeout::secs(5, "detach"))
            .unwrap(),
        0
    );
    assert!(String::from_utf8_lossy(harness.raw_output()).contains("?1049l"));
    server.join().unwrap();
    let requests = requests.lock().unwrap();
    assert_eq!(requests[0]["method"], "pane.report_agent");
    assert_eq!(requests[0]["params"]["state"], "working");
    assert_eq!(requests[0]["params"]["agent_session_id"], "abc123");
    assert_eq!(requests[1]["method"], "pane.report_agent");
    assert_eq!(requests[1]["params"]["state"], "working");
    assert_eq!(requests[2]["method"], "pane.report_agent");
    assert_eq!(requests[2]["params"]["state"], "idle");
    assert_eq!(requests[3]["method"], "pane.release_agent");
}

#[test]
fn attach_live_elapsed_advances_without_status_change() {
    let home = IsolatedHome::new().unwrap();
    let directory = home.home.join(".rho/subagents/e1a95e");
    std::fs::create_dir_all(&directory).unwrap();
    let started_at = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs()
        .saturating_sub(2);
    std::fs::write(
        directory.join("result.json"),
        format!(
            r#"{{
            "state": "running",
            "agent_id": "explorer",
            "provider": "openai",
            "model": "gpt-5.5",
            "runtime": "rho",
            "started_at": {started_at},
            "turns": 1,
            "last_activity": "assistant text"
        }}"#
        ),
    )
    .unwrap();
    std::fs::write(
        directory.join("events.jsonl"),
        "{\"type\":\"prompt\",\"data\":\"elapsed clock\"}\n",
    )
    .unwrap();

    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(binary, &home, PtySize { rows: 24, cols: 90 })
        .with_arg("attach")
        .with_arg("e1a95e");
    let mut harness = PtyHarness::spawn_named(&plan, "attach_live_elapsed").unwrap();

    harness
        .wait_for_text("attach e1a95e", WaitTimeout::secs(10, "attach startup"))
        .unwrap();
    harness
        .wait_for_text("elapsed clock", WaitTimeout::secs(5, "prompt"))
        .unwrap();

    // Capture an initial whole-second elapsed label from the identity row.
    let first = wait_for_turn_elapsed_secs(&mut harness, WaitTimeout::secs(5, "first elapsed"));
    // result.json stays unchanged; elapsed must still tick forward.
    let later = wait_for_turn_elapsed_secs_at_least(
        &mut harness,
        first + 2,
        WaitTimeout::secs(8, "elapsed advanced"),
    );
    assert!(
        later >= first + 2,
        "elapsed should advance without status I/O (first={first}s later={later}s)"
    );

    harness.inject_key(&Key::Char('q')).unwrap();
    assert_eq!(
        harness
            .wait_for_exit(WaitTimeout::secs(5, "detach"))
            .unwrap(),
        0
    );
}

/// Parse `turn N · Xs` whole-second elapsed from the attach identity line.
fn turn_elapsed_secs(screen: &str) -> Option<u64> {
    let marker = "turn ";
    let rest = screen.split(marker).nth(1)?;
    let after_turn = rest.split_once('·')?.1.trim_start();
    let token = after_turn.split_whitespace().next()?;
    token.strip_suffix('s')?.parse().ok()
}

fn wait_for_turn_elapsed_secs(harness: &mut PtyHarness, timeout: WaitTimeout) -> u64 {
    let started = Instant::now();
    let deadline = started + timeout.duration;
    loop {
        harness.poll(Duration::from_millis(50));
        if let Some(secs) = turn_elapsed_secs(&harness.screen().contents()) {
            return secs;
        }
        if Instant::now() >= deadline {
            panic!(
                "timeout waiting for live elapsed seconds during {}: screen=\n{}",
                timeout.label,
                harness.screen().contents()
            );
        }
    }
}

fn wait_for_turn_elapsed_secs_at_least(
    harness: &mut PtyHarness,
    minimum: u64,
    timeout: WaitTimeout,
) -> u64 {
    let started = Instant::now();
    let deadline = started + timeout.duration;
    loop {
        harness.poll(Duration::from_millis(50));
        if let Some(secs) = turn_elapsed_secs(&harness.screen().contents()) {
            if secs >= minimum {
                return secs;
            }
        }
        if Instant::now() >= deadline {
            panic!(
                "timeout waiting for elapsed >= {minimum}s during {}: screen=\n{}",
                timeout.label,
                harness.screen().contents()
            );
        }
    }
}

#[test]
fn attach_replays_finished_claude_run_from_fixtures() {
    let home = IsolatedHome::new().unwrap();
    let directory = home.home.join(".rho/subagents/c1a0de");
    std::fs::create_dir_all(&directory).unwrap();
    let fixtures = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/claude_attach");
    std::fs::copy(fixtures.join("result.json"), directory.join("result.json")).unwrap();
    std::fs::copy(
        fixtures.join("events.jsonl"),
        directory.join("events.jsonl"),
    )
    .unwrap();

    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 24,
            cols: 100,
        },
    )
    .with_arg("attach")
    .with_arg("c1a0de");
    let mut harness = PtyHarness::spawn_named(&plan, "claude_attach_replay").unwrap();

    harness
        .wait_for_text("attach c1a0de", WaitTimeout::secs(10, "attach startup"))
        .unwrap();
    harness
        .wait_for_text(
            "Say hello in one short sentence.",
            WaitTimeout::secs(5, "prompt"),
        )
        .unwrap();
    harness
        .wait_for_text("Hello from Claude.", WaitTimeout::secs(5, "assistant text"))
        .unwrap();
    harness
        .wait_for_text(
            "claude-code/claude-opus-demo · claude-cli · turn 1 · 42s",
            WaitTimeout::secs(5, "provider model runtime elapsed"),
        )
        .unwrap();
    harness
        .wait_for_text(
            "claude sess-success-001",
            WaitTimeout::secs(5, "session id"),
        )
        .unwrap();
    harness
        .wait_for_text("claude-planner", WaitTimeout::secs(5, "agent id"))
        .unwrap();
    assert!(harness.screen().contains_text("read-only"));
    assert!(harness.screen().contains_text("ok"));

    harness.inject_key(&Key::Char('q')).unwrap();
    assert_eq!(
        harness
            .wait_for_exit(WaitTimeout::secs(5, "detach"))
            .unwrap(),
        0
    );
}

/// Full fake-Claude runtime path: matrix parent -> agent tool -> binder/executor
/// -> `claude -p` spawn -> stream-json -> result/events persistence -> parent
/// completion UI -> `rho attach` replay. Never touches a real Claude binary or
/// the network.
#[test]
fn fake_claude_runtime_end_to_end_success() {
    let home = IsolatedHome::new().unwrap();
    claude_e2e::install_claude_planner_agent(&home.home);

    let fake_root = home.path().join("fake-claude");
    let fake = claude_e2e::install_fake_claude(&fake_root, claude_e2e::FakeClaudeMode::Success);
    let path = claude_e2e::path_with_fake(&fake.bin_dir);

    // Prove the isolated PATH cannot resolve a host Claude: only our stub.
    assert!(fake.claude.is_file());
    assert_eq!(
        which_on_path("claude", &path).as_deref(),
        Some(fake.claude.as_path()),
        "PATH must resolve the fake claude first"
    );

    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 32,
            cols: 120,
        },
    )
    .with_env("PATH", path);
    let mut harness = PtyHarness::spawn_named(&plan, "fake_claude_runtime_e2e").unwrap();

    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();

    // Real agent-tool path via matrix fixture prompt (foreground delegation).
    harness.submit_text("fixture claude agent").unwrap();
    harness
        .wait_for_text(
            "claude-planner",
            WaitTimeout::secs(15, "agent tool started"),
        )
        .unwrap();

    claude_e2e::wait_for_spawn(&fake, Duration::from_secs(15));
    let record = fake.read_spawn_record();
    claude_e2e::assert_success_spawn(&record, &home.workspace);

    // Parent UI shows final text and Claude session id from the live fixture.
    harness
        .wait_for_text(
            "rho-claude-e2e-ok",
            WaitTimeout::secs(20, "final assistant text"),
        )
        .unwrap();
    harness
        .wait_for_text(
            "11111111-2222-4333-8444-555555555555",
            WaitTimeout::secs(10, "claude session id"),
        )
        .unwrap();
    harness
        .wait_for_text(
            "claude agent tool finished:",
            WaitTimeout::secs(15, "parent turn closed"),
        )
        .unwrap();

    let run_dir = claude_e2e::wait_for_single_run_dir(&home.home, Duration::from_secs(10));
    let run_id = run_dir
        .file_name()
        .and_then(|name| name.to_str())
        .expect("run id")
        .to_string();
    let status = claude_e2e::wait_for_terminal_result(&run_dir, Duration::from_secs(10));
    claude_e2e::assert_success_result(&status, &run_dir);
    assert!(
        run_dir.starts_with(home.home.join(".rho/sessions")),
        "interactive run was not nested under its session: {}",
        run_dir.display()
    );
    assert!(
        !home.home.join(".rho/subagents").join(&run_id).exists(),
        "interactive run also appeared in the global pool"
    );

    // Offline proof: only the fake binary ran; spawn marker is under the temp root.
    assert!(
        fake.spawn_marker.starts_with(home.path()),
        "spawn marker escaped isolated root: {}",
        fake.spawn_marker.display()
    );
    assert!(
        record.args.iter().all(|arg| !arg.contains("anthropic.com")),
        "spawn argv must not reference network endpoints: {:?}",
        record.args
    );

    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);

    // Replay the real on-disk artifacts through `rho attach`.
    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let mut plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 28,
            cols: 120,
        },
    )
    .with_arg("attach")
    .with_arg(&run_id);
    plan.cwd = home.path().join("attach-workspace");
    fs::create_dir_all(&plan.cwd).unwrap();
    let mut attach = PtyHarness::spawn_named(&plan, "fake_claude_runtime_e2e_attach").unwrap();
    attach
        .wait_for_text(
            &format!("attach {run_id}"),
            WaitTimeout::secs(10, "attach startup"),
        )
        .unwrap();
    attach
        .wait_for_text(
            "Say hello in one short sentence.",
            WaitTimeout::secs(5, "attach prompt"),
        )
        .unwrap();
    // Partial deltas are stored separately; the live fixture splits the final
    // phrase across two assistant_text_delta events ("r" + "ho-claude-e2e-ok").
    attach
        .wait_for_text(
            "ho-claude-e2e-ok",
            WaitTimeout::secs(5, "attach final text tail"),
        )
        .unwrap();
    attach
        .wait_for_text(
            // Resolved Claude models lengthen the identity line, so the full
            // session UUID ellipsizes on a 120-col attach header. The stable
            // prefix still proves the session id landed.
            "claude 11111111",
            WaitTimeout::secs(5, "attach session id"),
        )
        .unwrap();
    attach
        .wait_for_text(
            "ran as claude-sonnet-5",
            WaitTimeout::secs(5, "attach resolved model"),
        )
        .unwrap();
    attach
        .wait_for_text("claude-planner", WaitTimeout::secs(5, "attach agent id"))
        .unwrap();
    assert!(attach.screen().contains_text("read-only"));
    assert!(attach.screen().contains_text("ok") || attach.screen().contains_text("complete"));
    // Joined final text lives in result.json (asserted earlier); attach stream
    // fidelity keeps the partial pieces visible.
    let attach_screen = attach.screen().contents();
    assert!(
        attach_screen.contains('r') && attach_screen.contains("ho-claude-e2e-ok"),
        "attach should replay streamed halves:\n{attach_screen}"
    );
    attach.inject_key(&Key::Char('q')).unwrap();
    assert_eq!(
        attach
            .wait_for_exit(WaitTimeout::secs(5, "detach"))
            .unwrap(),
        0
    );
}

/// Background Claude completion path: terminal `total_cost_usd` must fold into
/// the parent session total shown by `/info` after automatic delivery.
#[test]
fn fake_claude_background_cost_appears_in_info() {
    let home = IsolatedHome::new().unwrap();
    claude_e2e::install_claude_planner_agent(&home.home);

    let fake_root = home.path().join("fake-claude");
    let fake = claude_e2e::install_fake_claude(&fake_root, claude_e2e::FakeClaudeMode::Success);
    let path = claude_e2e::path_with_fake(&fake.bin_dir);

    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 36,
            cols: 120,
        },
    )
    .with_env("PATH", path);
    let mut harness = PtyHarness::spawn_named(&plan, "fake_claude_background_cost_info").unwrap();

    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();

    harness
        .submit_text("fixture background claude agent")
        .unwrap();
    harness
        .wait_for_text(
            "background claude agent dispatched:",
            WaitTimeout::secs(15, "background dispatch"),
        )
        .unwrap();

    claude_e2e::wait_for_spawn(&fake, Duration::from_secs(15));
    let run_dir = claude_e2e::wait_for_single_run_dir(&home.home, Duration::from_secs(10));
    let status = claude_e2e::wait_for_terminal_result(&run_dir, Duration::from_secs(10));
    claude_e2e::assert_success_result(&status, &run_dir);

    harness
        .wait_for_text(
            "background claude agent completion received with delegated result (delivery 1)",
            WaitTimeout::secs(20, "completion delivery"),
        )
        .unwrap();

    // Delivery should include the fixture cost on the statusline total.
    harness
        .wait_for_text("$0.034", WaitTimeout::secs(10, "statusline subagent cost"))
        .unwrap();

    harness.submit_text("/info").unwrap();
    harness
        .wait_for_text("Session usage", WaitTimeout::secs(10, "info opened"))
        .unwrap();
    harness
        .wait_for_text(
            "Subagent cost",
            WaitTimeout::secs(10, "subagent cost label"),
        )
        .unwrap();
    harness
        .wait_for_text("$0.034", WaitTimeout::secs(10, "subagent cost value"))
        .unwrap();

    let screen = harness.screen().contents();
    assert!(
        screen.contains("Subagent cost") && screen.contains("$0.034"),
        "expected /info to show delivered subagent cost:\n{screen}"
    );
    assert!(
        !screen.contains("No token usage recorded yet."),
        "subagent cost should replace the empty-usage note:\n{screen}"
    );

    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
}

/// Sibling error path: fake Claude emits a terminal error stream and nonzero
/// exit; the parent surfaces a failed delegated run without network access.
#[test]
fn fake_claude_runtime_end_to_end_error() {
    let home = IsolatedHome::new().unwrap();
    claude_e2e::install_claude_planner_agent(&home.home);

    let fake_root = home.path().join("fake-claude");
    let fake = claude_e2e::install_fake_claude(&fake_root, claude_e2e::FakeClaudeMode::Error);
    let path = claude_e2e::path_with_fake(&fake.bin_dir);

    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 32,
            cols: 120,
        },
    )
    .with_env("PATH", path);
    let mut harness = PtyHarness::spawn_named(&plan, "fake_claude_runtime_e2e_error").unwrap();

    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();
    harness.submit_text("fixture claude agent error").unwrap();
    harness
        .wait_for_text(
            "claude-planner",
            WaitTimeout::secs(15, "agent tool started"),
        )
        .unwrap();

    claude_e2e::wait_for_spawn(&fake, Duration::from_secs(15));

    // Failed foreground agent should show failed presentation and/or error text.
    harness
        .wait_for_text("failed", WaitTimeout::secs(20, "failed agent state"))
        .unwrap();

    let run_dir = claude_e2e::wait_for_single_run_dir(&home.home, Duration::from_secs(10));
    let status = claude_e2e::wait_for_terminal_result(&run_dir, Duration::from_secs(10));
    claude_e2e::assert_error_result(&status);
    assert!(
        fake.spawn_marker.exists(),
        "error path must still have spawned the fake binary"
    );

    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
}

/// Resolve `program` on a PATH string the same way a shell would (first hit).
fn which_on_path(program: &str, path_var: &str) -> Option<PathBuf> {
    for dir in path_var.split(':').filter(|dir| !dir.is_empty()) {
        let candidate = PathBuf::from(dir).join(program);
        if candidate.is_file() {
            return Some(candidate);
        }
    }
    None
}

#[test]
fn smoke_subset_is_registered() {
    let smoke = smoke_scenario_ids();
    // Core lifecycle gates kept in CI.
    for id in [
        "startup_stream_exit",
        "cancel_and_resubmit",
        "type_during_stream",
        "resize_during_stream",
        "scroll_during_stream",
        "terminal_restoration",
    ] {
        assert!(smoke.contains(&id), "missing core smoke scenario {id}");
    }
    // Fragility champions from issue #711.
    assert!(
        smoke.contains(&"streaming_markdown_stability"),
        "missing markdown stability smoke scenario"
    );
    assert!(
        smoke.contains(&"spinner_activity_anchor"),
        "missing activity-rail smoke scenario"
    );
}

/// Full Claude Code advisor path: picker selection -> config -> advisor tool
/// call -> `claude -p` spawn with no tools -> stream-json -> advice in the card.
/// Never touches a real Claude binary or the network.
#[test]
fn fake_claude_advisor_reviews_the_session() {
    let home = IsolatedHome::new().unwrap();
    std::fs::write(
        &home.config_path,
        r#"provider = "openai"
model = "gpt-5.5"
auth = "api-key"
check_for_updates = false
web_search_provider = "disabled"

[behavior]
credential_store = "file"
advisor_mode = true
"#,
    )
    .unwrap();

    let fake_root = home.path().join("fake-claude");
    let fake = claude_e2e::install_fake_claude(&fake_root, claude_e2e::FakeClaudeMode::Success);
    let path = claude_e2e::path_with_fake(&fake.bin_dir);
    assert_eq!(
        which_on_path("claude", &path).as_deref(),
        Some(fake.claude.as_path()),
        "PATH must resolve the fake claude first"
    );

    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 32,
            cols: 120,
        },
    )
    .with_env("PATH", path);
    let mut harness = PtyHarness::spawn_named(&plan, "fake_claude_advisor").unwrap();
    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();

    // Picking a Claude Code row is the only step: it selects the runtime too.
    harness.submit_text("/advisor on").unwrap();
    harness
        .wait_for_text(
            "select model for advisor",
            WaitTimeout::secs(10, "advisor model picker"),
        )
        .unwrap();
    harness.type_text("claude-code/opus").unwrap();
    harness
        .wait_for_text("(1/1)", WaitTimeout::secs(10, "claude code row filtered"))
        .unwrap();
    harness.inject_key(&Key::Enter).unwrap();
    harness
        .wait_for_text(
            "advisor mode is on: claude-code/opus reviews the session",
            WaitTimeout::secs(10, "advisor turned on"),
        )
        .unwrap();
    harness
        .wait_for_text(
            "advisor: claude-code/opus",
            WaitTimeout::secs(10, "advisor divider"),
        )
        .unwrap();

    harness.submit_text("fixture advisor").unwrap();
    claude_e2e::wait_for_spawn(&fake, Duration::from_secs(20));
    let record = fake.read_spawn_record();

    // Parity with the Rho advisor: one turn, no tools, no workspace access.
    // The recorder joins argv on NUL and drops empty chunks, so the empty
    // `--tools` value shows up as the flag with nothing of its own after it.
    assert!(
        value_after(&record.args, "--tools").is_none_or(|value| value.starts_with("--")),
        "the advisor must run with no tools: {:?}",
        record.args
    );
    assert!(
        record.args.iter().any(|arg| arg == "--tools"),
        "--tools must always be set so ambient tools are not inherited: {:?}",
        record.args
    );
    assert!(
        !record.args.iter().any(|arg| arg == "--allowedTools"),
        "the advisor must allow no tools: {:?}",
        record.args
    );
    for pair in [
        ["--model", "opus"],
        ["--max-turns", "1"],
        ["--permission-mode", "dontAsk"],
    ] {
        assert!(
            record.args.windows(2).any(|window| window == pair),
            "missing {pair:?}: {:?}",
            record.args
        );
    }
    assert!(
        record
            .args
            .iter()
            .any(|arg| arg == "--no-session-persistence"),
        "a one-shot advisor call must leave no resumable session: {:?}",
        record.args
    );
    let prompt =
        value_after(&record.args, "--system-prompt").expect("advisor system prompt on argv");
    assert!(
        !prompt.is_empty(),
        "advisor system prompt must not be empty"
    );
    assert!(
        record.stdin.contains("fixture advisor"),
        "the advisor must receive the session transcript on stdin: {}",
        record.stdin
    );

    // The fixture's result text is the advice the executor gets back.
    harness
        .wait_for_text(
            "rho-claude-e2e-ok",
            WaitTimeout::secs(20, "advice in the advisor card"),
        )
        .unwrap();

    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
}

/// First value following `flag` in a recorded argv.
fn value_after<'a>(args: &'a [String], flag: &str) -> Option<&'a str> {
    args.windows(2)
        .find(|pair| pair[0] == flag)
        .map(|pair| pair[1].as_str())
}