agcodex-tui 0.1.0

Terminal User Interface for AGCodex with mode switching support
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
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
use crate::LoginStatus;
use crate::app_event::AppEvent;
use crate::app_event_sender::AppEventSender;
use crate::chatwidget::ChatWidget;
use crate::file_search::FileSearchManager;
use crate::get_git_diff::get_git_diff;
use crate::get_login_status;
use crate::notification::NotificationSystem;
use crate::onboarding::onboarding_screen::KeyboardHandler;
use crate::onboarding::onboarding_screen::OnboardingScreen;
use crate::onboarding::onboarding_screen::OnboardingScreenArgs;
use crate::slash_command::SlashCommand;
use crate::tui;
use agcodex_core::ConversationManager;
use agcodex_core::config::Config;
use agcodex_core::config_types::TuiNotifications;
use agcodex_core::modes::ModeManager;
use agcodex_core::modes::OperatingMode;
use agcodex_core::protocol::Event;
use agcodex_core::protocol::Op;
use agcodex_persistence::session_manager::SessionManager;
use agcodex_persistence::session_manager::SessionManagerConfig;
use agcodex_persistence::types::OperatingMode as PersistenceOperatingMode;
// Temporarily disable real orchestrator until core compilation issues are fixed
// use agcodex_core::subagents::{
//     AgentOrchestrator, OrchestratorConfig, ProgressUpdate, SubagentRegistry
// };
// use agcodex_core::code_tools::ast_agent_tools::ASTAgentTools;
use color_eyre::eyre::Result;
// Use crossterm types re-exported by ratatui to avoid version conflicts
use crate::widgets::AgentPanel;
use crate::widgets::ModeIndicator;
use ratatui::crossterm::event::KeyCode;
use ratatui::crossterm::event::KeyEvent;
use ratatui::crossterm::event::KeyEventKind;
use ratatui::crossterm::terminal::supports_keyboard_enhancement;
use ratatui::layout::Constraint;
use ratatui::layout::Direction;
use ratatui::layout::Layout;
use ratatui::layout::Offset;
use ratatui::prelude::Backend;
use ratatui::text::Line;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::mpsc::Receiver;
use std::sync::mpsc::channel;
use std::thread;
use std::time::Duration;
use std::time::Instant;
use tokio::sync::Mutex;
use tokio::sync::RwLock;
use uuid::Uuid;

/// Time window for debouncing redraw requests.
const REDRAW_DEBOUNCE: Duration = Duration::from_millis(1);

/// Helper struct for auto-save functionality
struct AutoSaveApp {
    session_manager: Arc<RwLock<Option<SessionManager>>>,
    current_session_id: Arc<RwLock<Option<Uuid>>>,
    auto_save_handle: Arc<Mutex<Option<tokio::task::JoinHandle<()>>>>,
    codex_home: PathBuf,
}

impl AutoSaveApp {
    /// Start auto-save timer for active sessions
    async fn start_auto_save_timer(&self) {
        let session_manager = self.session_manager.clone();
        let current_session_id = self.current_session_id.clone();
        let auto_save_handle = self.auto_save_handle.clone();
        let codex_home = self.codex_home.clone();

        let handle = tokio::spawn(async move {
            let mut interval = tokio::time::interval(Duration::from_secs(300)); // 5 minutes
            interval.tick().await; // Skip first immediate tick

            loop {
                interval.tick().await;

                // Check if there's an active session to save
                let current_id = {
                    let guard = current_session_id.read().await;
                    *guard
                };

                if let Some(session_id) = current_id {
                    // Initialize SessionManager if needed
                    let mut session_manager_guard = session_manager.write().await;
                    if session_manager_guard.is_none() {
                        let config = SessionManagerConfig {
                            storage_path: codex_home.join("history"),
                            auto_save_interval: Duration::from_secs(300),
                            ..Default::default()
                        };

                        match SessionManager::new(config).await {
                            Ok(manager) => {
                                *session_manager_guard = Some(manager);
                            }
                            Err(e) => {
                                tracing::error!(
                                    "Auto-save: Failed to initialize SessionManager: {}",
                                    e
                                );
                                continue;
                            }
                        }
                    }

                    let session_manager = session_manager_guard.as_ref().unwrap();

                    match session_manager.save_session(session_id).await {
                        Ok(_) => {
                            tracing::debug!("Auto-saved session: {}", session_id);
                        }
                        Err(e) => {
                            tracing::warn!("Auto-save failed for session {}: {}", session_id, e);
                        }
                    }
                    drop(session_manager_guard);
                } else {
                    tracing::debug!("Auto-save: No active session to save");
                }
            }
        });

        *auto_save_handle.lock().await = Some(handle);
        tracing::info!("Auto-save timer started (5 minute intervals)");
    }
}

/// Top-level application state: which full-screen view is currently active.
#[allow(clippy::large_enum_variant)]
enum AppState<'a> {
    Onboarding {
        screen: OnboardingScreen,
    },
    /// The main chat UI is visible.
    Chat {
        /// Boxed to avoid a large enum variant and reduce the overall size of
        /// `AppState`.
        widget: Box<ChatWidget<'a>>,
    },
}

pub(crate) struct App<'a> {
    server: Arc<ConversationManager>,
    app_event_tx: AppEventSender,
    app_event_rx: Receiver<AppEvent>,
    app_state: AppState<'a>,

    /// Config is stored here so we can recreate ChatWidgets as needed.
    config: Config,

    file_search: FileSearchManager,

    pending_history_lines: Vec<Line<'static>>,

    enhanced_keys_supported: bool,

    /// Controls the animation thread that sends CommitTick events.
    commit_anim_running: Arc<AtomicBool>,

    /// Channel to schedule one-shot animation frames; coalesced by a single
    /// scheduler thread.
    frame_schedule_tx: std::sync::mpsc::Sender<Instant>,

    /// Mode manager for operating mode switching
    mode_manager: ModeManager,

    /// Agent panel for subagent management
    agent_panel: AgentPanel,

    /// Notification system for terminal bell alerts and visual feedback
    notification_system: NotificationSystem,

    /// Session manager for persistence (initialized lazily)
    session_manager: Arc<RwLock<Option<SessionManager>>>,

    /// Current session ID if any
    current_session_id: Arc<RwLock<Option<Uuid>>>,

    /// Auto-save timer handle
    auto_save_handle: Arc<Mutex<Option<tokio::task::JoinHandle<()>>>>,
    // Temporarily disabled until core compilation issues are fixed
    // /// Agent orchestrator for real execution
    // orchestrator: Arc<AgentOrchestrator>,
    //
    // /// AST-based tools for agents
    // ast_tools: Arc<ASTAgentTools>,
    //
    // /// Progress receiver for agent updates
    // progress_receiver: Arc<tokio::sync::Mutex<tokio::sync::mpsc::UnboundedReceiver<ProgressUpdate>>>,
}

/// Aggregate parameters needed to create a `ChatWidget`, as creation may be
/// deferred until after the Git warning screen is dismissed.
#[derive(Clone, Debug)]
pub(crate) struct ChatWidgetArgs {
    pub(crate) config: Config,
    initial_prompt: Option<String>,
    initial_images: Vec<PathBuf>,
    enhanced_keys_supported: bool,
}

impl App<'_> {
    pub(crate) fn new(
        config: Config,
        initial_prompt: Option<String>,
        initial_images: Vec<std::path::PathBuf>,
        show_trust_screen: bool,
    ) -> Self {
        let conversation_manager = Arc::new(ConversationManager::default());

        let (app_event_tx, app_event_rx) = channel();
        let app_event_tx = AppEventSender::new(app_event_tx);

        let enhanced_keys_supported = supports_keyboard_enhancement().unwrap_or(false);

        // Spawn a dedicated thread for reading the crossterm event loop and
        // re-publishing the events as AppEvents, as appropriate.
        {
            let app_event_tx = app_event_tx.clone();
            std::thread::spawn(move || {
                loop {
                    // This timeout is necessary to avoid holding the event lock
                    // that crossterm::event::read() acquires. In particular,
                    // reading the cursor position (crossterm::cursor::position())
                    // needs to acquire the event lock, and so will fail if it
                    // can't acquire it within 2 sec. Resizing the terminal
                    // crashes the app if the cursor position can't be read.
                    if let Ok(true) = ratatui::crossterm::event::poll(Duration::from_millis(100)) {
                        if let Ok(event) = ratatui::crossterm::event::read() {
                            match event {
                                ratatui::crossterm::event::Event::Key(key_event) => {
                                    app_event_tx.send(AppEvent::KeyEvent(key_event));
                                }
                                ratatui::crossterm::event::Event::Resize(_, _) => {
                                    app_event_tx.send(AppEvent::RequestRedraw);
                                }
                                ratatui::crossterm::event::Event::Paste(pasted) => {
                                    // Many terminals convert newlines to \r when pasting (e.g., iTerm2),
                                    // but tui-textarea expects \n. Normalize CR to LF.
                                    // [tui-textarea]: https://github.com/rhysd/tui-textarea/blob/4d18622eeac13b309e0ff6a55a46ac6706da68cf/src/textarea.rs#L782-L783
                                    // [iTerm2]: https://github.com/gnachman/iTerm2/blob/5d0c0d9f68523cbd0494dad5422998964a2ecd8d/sources/iTermPasteHelper.m#L206-L216
                                    let pasted = pasted.replace("\r", "\n");
                                    app_event_tx.send(AppEvent::Paste(pasted));
                                }
                                _ => {
                                    // Ignore any other events.
                                }
                            }
                        }
                    } else {
                        // Timeout expired, no `Event` is available
                    }
                }
            });
        }

        let login_status = get_login_status(&config);
        let should_show_onboarding =
            should_show_onboarding(login_status, &config, show_trust_screen);
        let app_state = if should_show_onboarding {
            let show_login_screen = should_show_login_screen(login_status, &config);
            let chat_widget_args = ChatWidgetArgs {
                config: config.clone(),
                initial_prompt,
                initial_images,
                enhanced_keys_supported,
            };
            AppState::Onboarding {
                screen: OnboardingScreen::new(OnboardingScreenArgs {
                    event_tx: app_event_tx.clone(),
                    codex_home: config.codex_home.clone(),
                    cwd: config.cwd.clone(),
                    show_trust_screen,
                    show_login_screen,
                    chat_widget_args,
                    login_status,
                }),
            }
        } else {
            let chat_widget = ChatWidget::new(
                config.clone(),
                conversation_manager.clone(),
                app_event_tx.clone(),
                initial_prompt,
                initial_images,
                enhanced_keys_supported,
            );
            AppState::Chat {
                widget: Box::new(chat_widget),
            }
        };

        let file_search = FileSearchManager::new(config.cwd.clone(), app_event_tx.clone());

        // Spawn a single scheduler thread that coalesces both debounced redraw
        // requests and animation frame requests, and emits a single Redraw event
        // at the earliest requested time.
        let (frame_tx, frame_rx) = channel::<Instant>();
        {
            let app_event_tx = app_event_tx.clone();
            std::thread::spawn(move || {
                use std::sync::mpsc::RecvTimeoutError;
                let mut next_deadline: Option<Instant> = None;
                loop {
                    if next_deadline.is_none() {
                        match frame_rx.recv() {
                            Ok(deadline) => next_deadline = Some(deadline),
                            Err(_) => break,
                        }
                    }

                    #[expect(clippy::expect_used)]
                    let deadline = next_deadline.expect("deadline set");
                    let now = Instant::now();
                    let timeout = if deadline > now {
                        deadline - now
                    } else {
                        Duration::from_millis(0)
                    };

                    match frame_rx.recv_timeout(timeout) {
                        Ok(new_deadline) => {
                            next_deadline =
                                Some(next_deadline.map_or(new_deadline, |d| d.min(new_deadline)));
                        }
                        Err(RecvTimeoutError::Timeout) => {
                            app_event_tx.send(AppEvent::Redraw);
                            next_deadline = None;
                        }
                        Err(RecvTimeoutError::Disconnected) => break,
                    }
                }
            });
        }
        // Temporarily disabled: Initialize agent infrastructure
        // let registry = Arc::new(SubagentRegistry::new());
        // let orchestrator_config = OrchestratorConfig::default();
        // let ast_tools = Arc::new(ASTAgentTools::new());
        //
        // // Create orchestrator
        // let orchestrator = Arc::new(AgentOrchestrator::new(
        //     registry,
        //     orchestrator_config,
        //     OperatingMode::Build, // Will be updated when mode changes
        // ));
        //
        // // Get progress receiver
        // let progress_receiver = {
        //     let orch = orchestrator.clone();
        //     tokio::spawn(async move {
        //         orch.progress_receiver().await
        //     })
        // };
        //
        // // For now, create a dummy receiver since the orchestrator method needs to be fixed
        // let (_, dummy_rx) = tokio::sync::mpsc::unbounded_channel::<ProgressUpdate>();
        // let progress_receiver = Arc::new(tokio::sync::Mutex::new(dummy_rx));

        // Create the app first, then start progress monitoring
        let notification_system = NotificationSystem::new(config.tui.notifications.clone());
        let session_manager = Arc::new(RwLock::new(None));
        let current_session_id = Arc::new(RwLock::new(None));
        let auto_save_handle = Arc::new(Mutex::new(None));

        let app = Self {
            server: conversation_manager,
            app_event_tx: app_event_tx.clone(),
            pending_history_lines: Vec::new(),
            app_event_rx,
            app_state,
            config,
            file_search,
            enhanced_keys_supported,
            commit_anim_running: Arc::new(AtomicBool::new(false)),
            frame_schedule_tx: frame_tx,
            mode_manager: ModeManager::new(OperatingMode::Build), // Default to Build mode
            agent_panel: AgentPanel::new(),
            notification_system,
            session_manager,
            current_session_id,
            auto_save_handle,
            // Temporarily disabled:
            // orchestrator: orchestrator.clone(),
            // ast_tools,
            // progress_receiver,
        };

        // TODO: Start progress monitoring when real orchestrator is available
        // app.start_progress_monitoring();

        // Start auto-save timer
        let app_clone = app.clone_for_autosave();
        tokio::spawn(async move {
            app_clone.start_auto_save_timer().await;
        });

        app
    }

    fn schedule_frame_in(&self, dur: Duration) {
        let _ = self.frame_schedule_tx.send(Instant::now() + dur);
    }

    pub(crate) fn run(&mut self, terminal: &mut tui::Tui) -> Result<()> {
        // Schedule the first render immediately.
        let _ = self.frame_schedule_tx.send(Instant::now());

        while let Ok(event) = self.app_event_rx.recv() {
            match event {
                AppEvent::InsertHistory(lines) => {
                    self.pending_history_lines.extend(lines);
                    self.app_event_tx.send(AppEvent::RequestRedraw);
                }
                AppEvent::RequestRedraw => {
                    self.schedule_frame_in(REDRAW_DEBOUNCE);
                }
                AppEvent::ScheduleFrameIn(dur) => {
                    self.schedule_frame_in(dur);
                }
                AppEvent::Redraw => {
                    // Synchronized update is not available in crossterm 0.28.1
                    // Just draw the frame directly
                    self.draw_next_frame(terminal)?;
                }
                AppEvent::StartCommitAnimation => {
                    if self
                        .commit_anim_running
                        .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
                        .is_ok()
                    {
                        let tx = self.app_event_tx.clone();
                        let running = self.commit_anim_running.clone();
                        thread::spawn(move || {
                            while running.load(Ordering::Relaxed) {
                                thread::sleep(Duration::from_millis(50));
                                tx.send(AppEvent::CommitTick);
                            }
                        });
                    }
                }
                AppEvent::StopCommitAnimation => {
                    self.commit_anim_running.store(false, Ordering::Release);
                }
                AppEvent::CommitTick => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.on_commit_tick();
                    }
                }
                AppEvent::KeyEvent(key_event) => {
                    match key_event {
                        KeyEvent {
                            code: KeyCode::Char('c'),
                            modifiers: ratatui::crossterm::event::KeyModifiers::CONTROL,
                            kind: KeyEventKind::Press,
                            ..
                        } => match &mut self.app_state {
                            AppState::Chat { widget } => {
                                widget.on_ctrl_c();
                            }
                            AppState::Onboarding { .. } => {
                                self.app_event_tx.send(AppEvent::ExitRequest);
                            }
                        },
                        KeyEvent {
                            code: KeyCode::BackTab,
                            kind: KeyEventKind::Press,
                            ..
                        } => {
                            // Shift+Tab: Cycle through operating modes
                            self.app_event_tx.send(AppEvent::CycleModes);
                        }
                        KeyEvent {
                            code: KeyCode::Char('s'),
                            modifiers: ratatui::crossterm::event::KeyModifiers::CONTROL,
                            kind: KeyEventKind::Press,
                            ..
                        } => {
                            // Ctrl+S: Open save session dialog
                            self.app_event_tx.send(AppEvent::OpenSaveDialog);
                        }
                        KeyEvent {
                            code: KeyCode::Char('o'),
                            modifiers: ratatui::crossterm::event::KeyModifiers::CONTROL,
                            kind: KeyEventKind::Press,
                            ..
                        } => {
                            // Ctrl+O: Open load session dialog
                            self.app_event_tx.send(AppEvent::OpenLoadDialog);
                        }
                        KeyEvent {
                            code: KeyCode::Char('j'),
                            modifiers: ratatui::crossterm::event::KeyModifiers::CONTROL,
                            kind: KeyEventKind::Press,
                            ..
                        } => {
                            // Ctrl+J: Open message jump popup
                            self.app_event_tx.send(AppEvent::ShowMessageJump);
                        }
                        KeyEvent {
                            code: KeyCode::Char('a'),
                            modifiers: ratatui::crossterm::event::KeyModifiers::CONTROL,
                            kind: KeyEventKind::Press,
                            ..
                        } => {
                            // Ctrl+A: Toggle agent panel
                            self.app_event_tx.send(AppEvent::ToggleAgentPanel);
                        }
                        KeyEvent {
                            code: KeyCode::Char('z'),
                            modifiers: ratatui::crossterm::event::KeyModifiers::CONTROL,
                            kind: KeyEventKind::Press,
                            ..
                        } => {
                            #[cfg(unix)]
                            {
                                self.suspend(terminal)?;
                            }
                            // No-op on non-Unix platforms.
                        }
                        KeyEvent {
                            code: KeyCode::Char('d'),
                            modifiers: ratatui::crossterm::event::KeyModifiers::CONTROL,
                            kind: KeyEventKind::Press,
                            ..
                        } => {
                            match &mut self.app_state {
                                AppState::Chat { widget } => {
                                    if widget.composer_is_empty() {
                                        self.app_event_tx.send(AppEvent::ExitRequest);
                                    } else {
                                        // Treat Ctrl+D as a normal key event when the composer
                                        // is not empty so that it doesn't quit the application
                                        // prematurely.
                                        self.dispatch_key_event(key_event);
                                    }
                                }
                                AppState::Onboarding { .. } => {
                                    self.app_event_tx.send(AppEvent::ExitRequest);
                                }
                            }
                        }
                        KeyEvent {
                            kind: KeyEventKind::Press | KeyEventKind::Repeat,
                            ..
                        } => {
                            self.dispatch_key_event(key_event);
                        }
                        _ => {
                            // Ignore Release key events.
                        }
                    };
                }
                AppEvent::Paste(text) => {
                    self.dispatch_paste_event(text);
                }
                AppEvent::CodexEvent(event) => {
                    self.dispatch_codex_event(event);
                }
                AppEvent::ExitRequest => {
                    break;
                }
                AppEvent::CodexOp(op) => match &mut self.app_state {
                    AppState::Chat { widget } => widget.submit_op(op),
                    AppState::Onboarding { .. } => {}
                },
                AppEvent::DiffResult(text) => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.add_diff_output(text);
                    }
                }
                AppEvent::DispatchCommand(command) => match command {
                    SlashCommand::New => {
                        // User accepted – switch to chat view.
                        let new_widget = Box::new(ChatWidget::new(
                            self.config.clone(),
                            self.server.clone(),
                            self.app_event_tx.clone(),
                            None,
                            Vec::new(),
                            self.enhanced_keys_supported,
                        ));
                        self.app_state = AppState::Chat { widget: new_widget };
                        self.app_event_tx.send(AppEvent::RequestRedraw);
                    }
                    SlashCommand::Init => {
                        // Guard: do not run if a task is active.
                        if let AppState::Chat { widget } = &mut self.app_state {
                            const INIT_PROMPT: &str = include_str!("../prompt_for_init_command.md");
                            widget.submit_text_message(INIT_PROMPT.to_string());
                        }
                    }
                    SlashCommand::Compact => {
                        if let AppState::Chat { widget } = &mut self.app_state {
                            widget.clear_token_usage();
                            self.app_event_tx.send(AppEvent::CodexOp(Op::Compact));
                        }
                    }
                    SlashCommand::Model => {
                        if let AppState::Chat { widget } = &mut self.app_state {
                            widget.open_model_popup();
                        }
                    }
                    SlashCommand::Approvals => {
                        if let AppState::Chat { widget } = &mut self.app_state {
                            widget.open_approvals_popup();
                        }
                    }
                    SlashCommand::Quit => {
                        break;
                    }
                    SlashCommand::Logout => {
                        if let Err(e) = agcodex_login::logout(&self.config.codex_home) {
                            tracing::error!("failed to logout: {e}");
                        }
                        break;
                    }
                    SlashCommand::Diff => {
                        if let AppState::Chat { widget } = &mut self.app_state {
                            widget.add_diff_in_progress();
                        }

                        let tx = self.app_event_tx.clone();
                        tokio::spawn(async move {
                            let text = match get_git_diff().await {
                                Ok((is_git_repo, diff_text)) => {
                                    if is_git_repo {
                                        diff_text
                                    } else {
                                        "`/diff` — _not inside a git repository_".to_string()
                                    }
                                }
                                Err(e) => format!("Failed to compute diff: {e}"),
                            };
                            tx.send(AppEvent::DiffResult(text));
                        });
                    }
                    SlashCommand::Mention => {
                        if let AppState::Chat { widget } = &mut self.app_state {
                            widget.insert_str("@");
                        }
                    }
                    SlashCommand::Status => {
                        if let AppState::Chat { widget } = &mut self.app_state {
                            widget.add_status_output();
                        }
                    }
                    SlashCommand::Mcp => {
                        if let AppState::Chat { widget } = &mut self.app_state {
                            widget.add_mcp_output();
                        }
                    }
                    #[cfg(debug_assertions)]
                    SlashCommand::TestApproval => {
                        use agcodex_core::protocol::EventMsg;
                        use std::collections::HashMap;

                        use agcodex_core::protocol::ApplyPatchApprovalRequestEvent;
                        use agcodex_core::protocol::FileChange;

                        self.app_event_tx.send(AppEvent::CodexEvent(Event {
                            id: "1".to_string(),
                            // msg: EventMsg::ExecApprovalRequest(ExecApprovalRequestEvent {
                            //     call_id: "1".to_string(),
                            //     command: vec!["git".into(), "apply".into()],
                            //     cwd: self.config.cwd.clone(),
                            //     reason: Some("test".to_string()),
                            // }),
                            msg: EventMsg::ApplyPatchApprovalRequest(
                                ApplyPatchApprovalRequestEvent {
                                    call_id: "1".to_string(),
                                    changes: HashMap::from([
                                        (
                                            PathBuf::from("/tmp/test.txt"),
                                            FileChange::Add {
                                                content: "test".to_string(),
                                            },
                                        ),
                                        (
                                            PathBuf::from("/tmp/test2.txt"),
                                            FileChange::Update {
                                                unified_diff: "+test\n-test2".to_string(),
                                                move_path: None,
                                            },
                                        ),
                                    ]),
                                    reason: None,
                                    grant_root: Some(PathBuf::from("/tmp")),
                                },
                            ),
                        }));
                    }
                },
                AppEvent::OnboardingAuthComplete(result) => {
                    if let AppState::Onboarding { screen } = &mut self.app_state {
                        screen.on_auth_complete(result);
                    }
                }
                AppEvent::OnboardingComplete(ChatWidgetArgs {
                    config,
                    enhanced_keys_supported,
                    initial_images,
                    initial_prompt,
                }) => {
                    self.app_state = AppState::Chat {
                        widget: Box::new(ChatWidget::new(
                            config,
                            self.server.clone(),
                            self.app_event_tx.clone(),
                            initial_prompt,
                            initial_images,
                            enhanced_keys_supported,
                        )),
                    }
                }
                AppEvent::StartFileSearch(query) => {
                    if !query.is_empty() {
                        self.file_search.on_user_query(query);
                    }
                }
                AppEvent::FileSearchResult { query, matches } => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.apply_file_search_result(query, matches);
                    }
                }
                AppEvent::UpdateReasoningEffort(effort) => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.set_reasoning_effort(effort);
                    }
                }
                AppEvent::UpdateModel(model) => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.set_model(model);
                    }
                }
                AppEvent::UpdateAskForApprovalPolicy(policy) => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.set_approval_policy(policy);
                    }
                }
                AppEvent::UpdateSandboxPolicy(policy) => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.set_sandbox_policy(policy);
                    }
                }
                AppEvent::CycleModes => {
                    let new_mode = self.mode_manager.cycle();
                    tracing::info!("Switched to {:?} mode", new_mode);

                    // TODO: Update orchestrator operating mode when available
                    // In a real implementation, you'd want to recreate the orchestrator
                    // or have a method to update its mode.
                    tracing::debug!(
                        "Mode switched to {:?} - will apply to future agent executions",
                        new_mode
                    );

                    // The ModeIndicator widget will display the new mode visually
                    self.app_event_tx.send(AppEvent::RequestRedraw);
                }
                AppEvent::OpenSaveDialog => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.open_save_dialog();
                    }
                }
                AppEvent::SaveSession { name, description } => {
                    let app_event_tx = self.app_event_tx.clone();
                    let session_manager = self.session_manager.clone();
                    let current_session_id = self.current_session_id.clone();
                    let current_mode = self.current_mode();
                    let codex_home = self.config.codex_home.clone();

                    tokio::spawn(async move {
                        let config = SessionManagerConfig {
                            storage_path: codex_home.join("history"),
                            auto_save_interval: Duration::from_secs(300), // 5 minutes
                            ..Default::default()
                        };

                        // Initialize SessionManager if needed
                        let mut session_manager_guard = session_manager.write().await;
                        if session_manager_guard.is_none() {
                            match SessionManager::new(config).await {
                                Ok(manager) => {
                                    *session_manager_guard = Some(manager);
                                }
                                Err(e) => {
                                    tracing::error!("Failed to initialize SessionManager: {}", e);
                                    app_event_tx.send(AppEvent::CloseSaveDialog);
                                    return;
                                }
                            }
                        }

                        let session_manager = session_manager_guard.as_ref().unwrap();

                        // Convert current operating mode to persistence format
                        let persistence_mode = match current_mode {
                            OperatingMode::Plan => PersistenceOperatingMode::Plan,
                            OperatingMode::Build => PersistenceOperatingMode::Build,
                            OperatingMode::Review => PersistenceOperatingMode::Review,
                        };

                        // TODO: Get current model from conversation manager
                        let model = "gpt-4".to_string(); // Default for now

                        match session_manager
                            .create_session(name.clone(), model, persistence_mode)
                            .await
                        {
                            Ok(session_id) => {
                                // Update current session ID
                                *current_session_id.write().await = Some(session_id);
                                tracing::info!("Session '{}' saved with ID: {}", name, session_id);

                                // Notify success
                                app_event_tx.send(AppEvent::CloseSaveDialog);
                            }
                            Err(e) => {
                                tracing::error!("Failed to save session '{}': {}", name, e);
                                app_event_tx.send(AppEvent::CloseSaveDialog);
                            }
                        }
                        drop(session_manager_guard);
                    });
                }
                AppEvent::CloseSaveDialog => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.close_save_dialog();
                    }
                }
                AppEvent::ShowMessageJump => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.show_message_jump();
                    }
                }
                AppEvent::HideMessageJump => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.hide_message_jump();
                    }
                }
                AppEvent::JumpToMessage(index) => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.jump_to_message(index);
                    }
                }
                AppEvent::MessageJumpSearch(query) => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.update_message_jump_search(query);
                    }
                }
                AppEvent::MessageJumpCycleFilter => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.cycle_message_jump_filter();
                    }
                }
                AppEvent::OpenLoadDialog => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.show_load_dialog();
                    }
                }
                AppEvent::CloseLoadDialog => {
                    // Dialog will auto-close, just request redraw
                    self.app_event_tx.send(AppEvent::RequestRedraw);
                }
                AppEvent::StartLoadSessionList => {
                    let tx = self.app_event_tx.clone();
                    let session_manager = self.session_manager.clone();
                    let codex_home = self.config.codex_home.clone();

                    tokio::spawn(async move {
                        let config = SessionManagerConfig {
                            storage_path: codex_home.join("history"),
                            auto_save_interval: Duration::from_secs(300),
                            ..Default::default()
                        };

                        // Initialize SessionManager if needed
                        let mut session_manager_guard = session_manager.write().await;
                        if session_manager_guard.is_none() {
                            match SessionManager::new(config).await {
                                Ok(manager) => {
                                    *session_manager_guard = Some(manager);
                                }
                                Err(e) => {
                                    let error_msg =
                                        format!("Failed to initialize SessionManager: {}", e);
                                    tracing::error!("{}", error_msg);
                                    tx.send(AppEvent::LoadSessionListResult(Err(error_msg)));
                                    return;
                                }
                            }
                        }

                        let session_manager = session_manager_guard.as_ref().unwrap();
                        match session_manager.list_sessions().await {
                            Ok(sessions) => {
                                tx.send(AppEvent::LoadSessionListResult(Ok(sessions)));
                            }
                            Err(e) => {
                                let error_msg = format!("Failed to list sessions: {}", e);
                                tracing::error!("{}", error_msg);
                                tx.send(AppEvent::LoadSessionListResult(Err(error_msg)));
                            }
                        }
                        drop(session_manager_guard);
                    });
                }
                AppEvent::LoadSessionListResult(result) => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.apply_load_session_list_result(result);
                    }
                }
                AppEvent::LoadSession(session_id) => {
                    let tx = self.app_event_tx.clone();
                    let session_manager = self.session_manager.clone();
                    let current_session_id = self.current_session_id.clone();
                    let codex_home = self.config.codex_home.clone();

                    tokio::spawn(async move {
                        let config = SessionManagerConfig {
                            storage_path: codex_home.join("history"),
                            auto_save_interval: Duration::from_secs(300),
                            ..Default::default()
                        };

                        // Initialize SessionManager if needed
                        let mut session_manager_guard = session_manager.write().await;
                        if session_manager_guard.is_none() {
                            match SessionManager::new(config).await {
                                Ok(manager) => {
                                    *session_manager_guard = Some(manager);
                                }
                                Err(e) => {
                                    let error_msg =
                                        format!("Failed to initialize SessionManager: {}", e);
                                    tracing::error!("{}", error_msg);
                                    tx.send(AppEvent::LoadSessionResult(Err(error_msg)));
                                    return;
                                }
                            }
                        }

                        let session_manager = session_manager_guard.as_ref().unwrap();

                        match session_manager.load_session(session_id).await {
                            Ok(_) => {
                                // Update current session ID
                                *current_session_id.write().await = Some(session_id);
                                tracing::info!("Successfully loaded session: {}", session_id);

                                // TODO: Switch conversation state to loaded session
                                // This would involve communicating with ConversationManager

                                tx.send(AppEvent::LoadSessionResult(Ok(session_id)));
                                tx.send(AppEvent::CloseLoadDialog);
                            }
                            Err(e) => {
                                let error_msg =
                                    format!("Failed to load session {}: {}", session_id, e);
                                tracing::error!("{}", error_msg);
                                tx.send(AppEvent::LoadSessionResult(Err(error_msg)));
                            }
                        }
                        drop(session_manager_guard);
                    });
                }
                AppEvent::LoadSessionResult(result) => {
                    match result {
                        Ok(session_id) => {
                            tracing::info!("Successfully loaded session: {}", session_id);
                        }
                        Err(error) => {
                            tracing::error!("Failed to load session: {}", error);
                            // Ring terminal bell for session load error
                            if let Err(e) = self.notification_system.error_occurred() {
                                tracing::warn!(
                                    "Failed to ring terminal bell for session load error: {}",
                                    e
                                );
                            }
                        }
                    }
                    self.app_event_tx.send(AppEvent::RequestRedraw);
                }
                AppEvent::UpdateLoadDialogQuery(query) => {
                    if let AppState::Chat { widget } = &mut self.app_state {
                        widget.apply_load_dialog_query_update(query);
                    }
                }
                // Session browser events (placeholder implementations)
                AppEvent::OpenSessionBrowser => {
                    // TODO: Implement session browser
                    tracing::debug!("OpenSessionBrowser event (not implemented)");
                }
                AppEvent::CloseSessionBrowser => {
                    // TODO: Implement session browser
                    tracing::debug!("CloseSessionBrowser event (not implemented)");
                }
                AppEvent::SessionBrowserNavigate(_) => {
                    // TODO: Implement session browser navigation
                    tracing::debug!("SessionBrowserNavigate event (not implemented)");
                }
                AppEvent::SessionBrowserFocusNext => {
                    // TODO: Implement session browser focus
                    tracing::debug!("SessionBrowserFocusNext event (not implemented)");
                }
                AppEvent::SessionBrowserFocusPrevious => {
                    // TODO: Implement session browser focus
                    tracing::debug!("SessionBrowserFocusPrevious event (not implemented)");
                }
                AppEvent::SessionBrowserToggleViewMode => {
                    // TODO: Implement session browser view mode
                    tracing::debug!("SessionBrowserToggleViewMode event (not implemented)");
                }
                AppEvent::SessionBrowserCycleSort => {
                    // TODO: Implement session browser sorting
                    tracing::debug!("SessionBrowserCycleSort event (not implemented)");
                }
                AppEvent::SessionBrowserUpdateSearch(_) => {
                    // TODO: Implement session browser search
                    tracing::debug!("SessionBrowserUpdateSearch event (not implemented)");
                }
                AppEvent::SessionBrowserExecuteAction => {
                    // TODO: Implement session browser action execution
                    tracing::debug!("SessionBrowserExecuteAction event (not implemented)");
                }
                AppEvent::SessionBrowserDeleteSession(_) => {
                    // TODO: Implement session deletion
                    tracing::debug!("SessionBrowserDeleteSession event (not implemented)");
                }
                AppEvent::SessionBrowserExportSession { id: _, format: _ } => {
                    // TODO: Implement session export
                    tracing::debug!("SessionBrowserExportSession event (not implemented)");
                }
                AppEvent::SessionBrowserRenameSession { id: _, new_name: _ } => {
                    // TODO: Implement session renaming
                    tracing::debug!("SessionBrowserRenameSession event (not implemented)");
                }
                AppEvent::SessionBrowserToggleFavorite(_) => {
                    // TODO: Implement favorite toggling
                    tracing::debug!("SessionBrowserToggleFavorite event (not implemented)");
                }
                AppEvent::SessionBrowserDuplicateSession(_) => {
                    // TODO: Implement session duplication
                    tracing::debug!("SessionBrowserDuplicateSession event (not implemented)");
                }
                AppEvent::SessionBrowserShowConfirmation(_) => {
                    // TODO: Implement confirmation dialog
                    tracing::debug!("SessionBrowserShowConfirmation event (not implemented)");
                }
                AppEvent::SessionBrowserConfirmAction(_) => {
                    // TODO: Implement action confirmation
                    tracing::debug!("SessionBrowserConfirmAction event (not implemented)");
                }
                AppEvent::SessionBrowserToggleFavoritesFilter => {
                    // TODO: Implement favorites filter
                    tracing::debug!("SessionBrowserToggleFavoritesFilter event (not implemented)");
                }

                // TODO: Implement remaining event handlers
                AppEvent::SessionBrowserToggleExpand
                | AppEvent::SessionBrowserSelect
                | AppEvent::SessionBrowserDelete
                | AppEvent::SessionBrowserFilter(_)
                | AppEvent::SessionBrowserSort(_)
                | AppEvent::StartHistoryGet
                | AppEvent::HistoryGetResult(_)
                | AppEvent::StartJumpToMessage(_)
                | AppEvent::StartUndo
                | AppEvent::UndoComplete
                | AppEvent::StartRedo
                | AppEvent::RedoComplete
                | AppEvent::StartFork
                | AppEvent::ForkComplete(_) => {
                    // These events are not yet implemented
                    // TODO: Add implementations as features are completed
                }

                // ===== Agent Events =====
                AppEvent::StartAgent(invocation_request) => {
                    self.handle_start_agent(invocation_request);
                }
                AppEvent::AgentProgress {
                    agent_id,
                    progress,
                    message,
                } => {
                    self.agent_panel
                        .update_progress(agent_id, progress, message);
                    self.app_event_tx.send(AppEvent::RequestRedraw);
                }
                AppEvent::AgentComplete {
                    agent_id,
                    execution,
                } => {
                    self.agent_panel.complete_agent(agent_id, execution.clone());

                    // Ring terminal bell for agent completion with enhanced feedback
                    let agent_name = execution.agent_name.clone();
                    if let Err(e) = self
                        .notification_system
                        .agent_completed_with_message(&agent_name)
                    {
                        tracing::warn!("Failed to ring terminal bell for agent completion: {}", e);
                    }

                    self.app_event_tx.send(AppEvent::RequestRedraw);
                }
                AppEvent::AgentFailed { agent_id, error } => {
                    self.agent_panel.fail_agent(agent_id, error.clone());

                    // Ring terminal bell for agent failure with enhanced feedback
                    let agent_name = format!("agent-{}", agent_id);
                    if let Err(e) = self
                        .notification_system
                        .agent_failed_with_message(&agent_name, &error)
                    {
                        tracing::warn!("Failed to ring terminal bell for agent failure: {}", e);
                    }

                    self.app_event_tx.send(AppEvent::RequestRedraw);
                }
                AppEvent::CancelAgent(agent_id) => {
                    self.handle_cancel_agent(agent_id);
                }
                AppEvent::ToggleAgentPanel => {
                    self.agent_panel.toggle_visibility();
                    self.app_event_tx.send(AppEvent::RequestRedraw);
                }
                AppEvent::AgentPanelNavigateUp => {
                    if self.agent_panel.is_visible() {
                        self.agent_panel.navigate_up();
                        self.app_event_tx.send(AppEvent::RequestRedraw);
                    }
                }
                AppEvent::AgentPanelNavigateDown => {
                    if self.agent_panel.is_visible() {
                        self.agent_panel.navigate_down();
                        self.app_event_tx.send(AppEvent::RequestRedraw);
                    }
                }
                AppEvent::AgentPanelCancel => {
                    if self.agent_panel.is_visible()
                        && let Some(agent_id) = self.agent_panel.selected_agent_id()
                    {
                        self.app_event_tx.send(AppEvent::CancelAgent(agent_id));
                    }
                }
                AppEvent::AgentOutputChunk { agent_id, chunk } => {
                    self.agent_panel.add_output_chunk(agent_id, chunk);
                    self.app_event_tx.send(AppEvent::RequestRedraw);
                }
            }
        }
        terminal.clear()?;

        Ok(())
    }

    #[cfg(unix)]
    fn suspend(&mut self, terminal: &mut tui::Tui) -> Result<()> {
        tui::restore()?;
        // SAFETY: Unix-only code path. We intentionally send SIGTSTP to the
        // current process group (pid 0) to trigger standard job-control
        // suspension semantics. This FFI does not involve any raw pointers,
        // is not called from a signal handler, and uses a constant signal.
        // Errors from kill are acceptable (e.g., if already stopped) — the
        // subsequent re-init path will still leave the terminal in a good state.
        // We considered `nix`, but didn't think it was worth pulling in for this one call.
        unsafe { libc::kill(0, libc::SIGTSTP) };
        *terminal = tui::init(&self.config)?;
        terminal.clear()?;
        self.app_event_tx.send(AppEvent::RequestRedraw);
        Ok(())
    }

    pub(crate) fn token_usage(&self) -> agcodex_core::protocol::TokenUsage {
        match &self.app_state {
            AppState::Chat { widget } => widget.token_usage().clone(),
            AppState::Onboarding { .. } => agcodex_core::protocol::TokenUsage::default(),
        }
    }

    /// Get the current operating mode
    pub(crate) const fn current_mode(&self) -> OperatingMode {
        self.mode_manager.current_mode()
    }

    /// Handle starting a new agent execution with enhanced simulation
    /// TODO: Replace with real orchestrator once core compilation issues are fixed
    fn handle_start_agent(
        &mut self,
        invocation_request: agcodex_core::subagents::InvocationRequest,
    ) {
        use agcodex_core::subagents::ExecutionPlan;

        tracing::info!(
            "Starting enhanced agent execution: {:?}",
            invocation_request.execution_plan
        );

        // Clone required data for the async task
        let app_event_tx = self.app_event_tx.clone();

        // Parse the execution plan and spawn appropriate simulated agents
        match invocation_request.execution_plan {
            ExecutionPlan::Single(invocation) => {
                self.spawn_enhanced_agent(invocation, invocation_request.context);
            }
            ExecutionPlan::Sequential(chain) => {
                // Spawn them one by one with proper chaining
                for (i, invocation) in chain.agents.into_iter().enumerate() {
                    let context = if i == 0 {
                        invocation_request.context.clone()
                    } else {
                        format!("Chained from previous agent (step {})", i)
                    };
                    // Add delay for sequential execution
                    let delay = std::time::Duration::from_millis((i as u64) * 500);
                    self.spawn_enhanced_agent_with_delay(invocation, context, delay);
                }
            }
            ExecutionPlan::Parallel(invocations) => {
                for invocation in invocations {
                    self.spawn_enhanced_agent(invocation, invocation_request.context.clone());
                }
            }
            ExecutionPlan::Conditional(conditional) => {
                // For now, just execute all agents unconditionally
                // TODO: Implement proper conditional evaluation
                tracing::debug!(
                    "Conditional execution: executing {} agents",
                    conditional.agents.len()
                );
                for invocation in conditional.agents {
                    self.spawn_enhanced_agent(invocation, invocation_request.context.clone());
                }
            }
            ExecutionPlan::Mixed(steps) => {
                // Handle mixed execution with proper coordination
                let mut step_delay = 0u64;
                for step in steps {
                    match step {
                        agcodex_core::subagents::ExecutionStep::Single(invocation) => {
                            let delay = std::time::Duration::from_millis(step_delay * 200);
                            self.spawn_enhanced_agent_with_delay(
                                invocation,
                                invocation_request.context.clone(),
                                delay,
                            );
                            step_delay += 1;
                        }
                        agcodex_core::subagents::ExecutionStep::Parallel(invocations) => {
                            for invocation in invocations {
                                let delay = std::time::Duration::from_millis(step_delay * 200);
                                self.spawn_enhanced_agent_with_delay(
                                    invocation,
                                    invocation_request.context.clone(),
                                    delay,
                                );
                            }
                            step_delay += 1;
                        }
                        agcodex_core::subagents::ExecutionStep::Conditional(conditional) => {
                            // For now, just execute all agents unconditionally
                            // TODO: Implement proper conditional evaluation
                            for invocation in conditional.agents {
                                let delay = std::time::Duration::from_millis(step_delay * 200);
                                self.spawn_enhanced_agent_with_delay(
                                    invocation,
                                    invocation_request.context.clone(),
                                    delay,
                                );
                            }
                            step_delay += 1;
                        }
                        agcodex_core::subagents::ExecutionStep::Barrier => {
                            // Implement barrier by adding extra delay
                            step_delay += 5; // Extra delay for barrier
                            tracing::debug!("Barrier step: adding extra coordination delay");
                        }
                    }
                }
            }
        }

        // Show the agent panel when agents are started
        self.agent_panel.set_visible(true);
        self.app_event_tx.send(AppEvent::RequestRedraw);
    }

    /// Spawn an enhanced agent with realistic behavior
    fn spawn_enhanced_agent(
        &mut self,
        invocation: agcodex_core::subagents::AgentInvocation,
        context: String,
    ) {
        self.spawn_enhanced_agent_with_delay(
            invocation,
            context,
            std::time::Duration::from_millis(0),
        );
    }

    /// Spawn an enhanced agent with a specified delay
    fn spawn_enhanced_agent_with_delay(
        &mut self,
        invocation: agcodex_core::subagents::AgentInvocation,
        context: String,
        delay: std::time::Duration,
    ) {
        use agcodex_core::subagents::SubagentExecution;

        let mut execution = SubagentExecution::new(invocation.agent_name.clone());
        execution.start();

        let agent_id = execution.id;
        let agent_name = invocation.agent_name.clone();
        let parameters = invocation.parameters.clone();

        // Add to the agent panel
        self.agent_panel.add_agent(execution);

        // Spawn enhanced agent execution in background
        let app_event_tx = self.app_event_tx.clone();

        tokio::spawn(async move {
            // Initial delay if specified
            if !delay.is_zero() {
                tokio::time::sleep(delay).await;
            }

            // Enhanced simulation based on agent type
            let agent_steps = Self::get_agent_steps(&agent_name);
            let total_steps = agent_steps.len();

            // Execute each step with realistic timing
            for (i, step) in agent_steps.iter().enumerate() {
                let progress = (i as f32 + 1.0) / total_steps as f32;

                app_event_tx.send(AppEvent::AgentProgress {
                    agent_id,
                    progress,
                    message: step.clone(),
                });

                // Realistic timing based on step complexity
                let step_duration = Self::get_step_duration(step);
                tokio::time::sleep(step_duration).await;
            }

            // Generate realistic output based on agent type
            let output = Self::generate_agent_output(&agent_name, &parameters, &context);
            let modified_files = Self::get_simulated_modified_files(&agent_name);

            let mut completed_execution = SubagentExecution::new(agent_name);
            completed_execution.complete(output, modified_files);

            app_event_tx.send(AppEvent::AgentComplete {
                agent_id,
                execution: completed_execution,
            });
        });
    }

    /// Get realistic steps for different agent types
    fn get_agent_steps(agent_name: &str) -> Vec<String> {
        match agent_name {
            "code-reviewer" => vec![
                "Initializing code review analysis...".to_string(),
                "Parsing AST and building symbol tables...".to_string(),
                "Analyzing code quality metrics...".to_string(),
                "Checking for security vulnerabilities...".to_string(),
                "Evaluating performance patterns...".to_string(),
                "Generating review findings...".to_string(),
                "Finalizing recommendations...".to_string(),
            ],
            "refactorer" => vec![
                "Analyzing code structure...".to_string(),
                "Identifying refactoring opportunities...".to_string(),
                "Calculating complexity metrics...".to_string(),
                "Planning structural improvements...".to_string(),
                "Generating refactoring suggestions...".to_string(),
                "Validating proposed changes...".to_string(),
            ],
            "debugger" => vec![
                "Scanning for potential bugs...".to_string(),
                "Analyzing control flow...".to_string(),
                "Checking error handling patterns...".to_string(),
                "Validating input sanitization...".to_string(),
                "Generating debug report...".to_string(),
            ],
            "test-writer" => vec![
                "Analyzing code coverage...".to_string(),
                "Identifying test gaps...".to_string(),
                "Generating test cases...".to_string(),
                "Creating mock objects...".to_string(),
                "Validating test quality...".to_string(),
            ],
            "performance" => vec![
                "Profiling execution paths...".to_string(),
                "Analyzing memory usage patterns...".to_string(),
                "Identifying bottlenecks...".to_string(),
                "Calculating algorithmic complexity...".to_string(),
                "Generating optimization recommendations...".to_string(),
            ],
            "security" => vec![
                "Scanning for OWASP Top 10 vulnerabilities...".to_string(),
                "Analyzing authentication flows...".to_string(),
                "Checking input validation...".to_string(),
                "Evaluating cryptographic usage...".to_string(),
                "Generating security assessment...".to_string(),
            ],
            "docs" => vec![
                "Analyzing code documentation...".to_string(),
                "Extracting API signatures...".to_string(),
                "Generating usage examples...".to_string(),
                "Creating documentation structure...".to_string(),
                "Finalizing documentation...".to_string(),
            ],
            _ => vec![
                "Initializing agent...".to_string(),
                "Analyzing codebase...".to_string(),
                "Processing requirements...".to_string(),
                "Generating results...".to_string(),
                "Finalizing output...".to_string(),
            ],
        }
    }

    /// Get realistic timing for different step types
    fn get_step_duration(step: &str) -> std::time::Duration {
        if step.contains("Initializing") {
            std::time::Duration::from_millis(800)
        } else if step.contains("Parsing") || step.contains("AST") {
            std::time::Duration::from_millis(1500)
        } else if step.contains("Analyzing") {
            std::time::Duration::from_millis(1200)
        } else if step.contains("Generating") {
            std::time::Duration::from_millis(1000)
        } else if step.contains("Finalizing") {
            std::time::Duration::from_millis(600)
        } else {
            std::time::Duration::from_millis(1000)
        }
    }

    /// Generate realistic output based on agent type
    fn generate_agent_output(
        agent_name: &str,
        parameters: &std::collections::HashMap<String, String>,
        context: &str,
    ) -> String {
        let param_str = if parameters.is_empty() {
            "no parameters".to_string()
        } else {
            format!("{} parameters", parameters.len())
        };

        match agent_name {
            "code-reviewer" => format!(
                "# Code Review Report\n\n\
                **Summary**: Comprehensive code review completed\n\
                **Files Analyzed**: 23\n\
                **Issues Found**: 5 medium priority, 2 low priority\n\
                **Quality Score**: 87/100\n\n\
                ## Key Findings\n\
                - Function complexity could be reduced in 3 locations\n\
                - Missing error handling in async operations\n\
                - Opportunity for performance optimization in hot paths\n\n\
                **Context**: {}\n**Parameters**: {}",
                context, param_str
            ),
            "refactorer" => format!(
                "# Refactoring Recommendations\n\n\
                **Analysis Complete**: Found 8 refactoring opportunities\n\
                **Estimated Impact**: 15% complexity reduction\n\
                **Risk Level**: Low\n\n\
                ## Suggested Changes\n\
                1. Extract common patterns into utility functions\n\
                2. Simplify conditional logic in core modules\n\
                3. Apply dependency injection pattern\n\n\
                **Context**: {}\n**Parameters**: {}",
                context, param_str
            ),
            "debugger" => format!(
                "# Debug Analysis Report\n\n\
                **Scan Complete**: No critical bugs detected\n\
                **Potential Issues**: 3 minor warnings\n\
                **Code Health**: Good\n\n\
                ## Analysis Results\n\
                - All error paths properly handled\n\
                - Memory management appears correct\n\
                - Consider adding more defensive checks\n\n\
                **Context**: {}\n**Parameters**: {}",
                context, param_str
            ),
            _ => format!(
                "# Agent Execution Report\n\n\
                **Agent**: {}\n\
                **Status**: Successfully completed\n\
                **Analysis**: Comprehensive codebase review performed\n\n\
                ## Results\n\
                - All requirements processed\n\
                - Recommendations generated\n\
                - Quality checks passed\n\n\
                **Context**: {}\n**Parameters**: {}",
                agent_name, context, param_str
            ),
        }
    }

    /// Get simulated modified files based on agent type
    fn get_simulated_modified_files(agent_name: &str) -> Vec<std::path::PathBuf> {
        match agent_name {
            "refactorer" => vec![
                std::path::PathBuf::from("src/core/refactored_module.rs"),
                std::path::PathBuf::from("src/utils/extracted_common.rs"),
            ],
            "test-writer" => vec![
                std::path::PathBuf::from("tests/integration_tests.rs"),
                std::path::PathBuf::from("tests/unit/new_test_suite.rs"),
            ],
            "docs" => vec![
                std::path::PathBuf::from("docs/api_reference.md"),
                std::path::PathBuf::from("README.md"),
            ],
            _ => vec![], // Most agents don't modify files directly
        }
    }

    /// Handle agent cancellation (enhanced simulation)
    fn handle_cancel_agent(&mut self, agent_id: Uuid) {
        tracing::info!("Cancelling agent with ID: {}", agent_id);

        // TODO: When real orchestrator is available, cancel in the orchestrator
        // self.orchestrator.cancel();

        // Update the agent panel
        self.agent_panel.cancel_agent(agent_id);
        self.app_event_tx.send(AppEvent::RequestRedraw);

        // For enhanced simulation, just log the cancellation
        tracing::debug!("Agent {} cancelled via enhanced simulation", agent_id);
    }

    /// Clone necessary components for auto-save task
    fn clone_for_autosave(&self) -> AutoSaveApp {
        AutoSaveApp {
            session_manager: self.session_manager.clone(),
            current_session_id: self.current_session_id.clone(),
            auto_save_handle: self.auto_save_handle.clone(),
            codex_home: self.config.codex_home.clone(),
        }
    }

    /// Update notification system configuration
    pub(crate) fn update_notification_config(&mut self, config: TuiNotifications) {
        self.notification_system.update_config(config);
        tracing::debug!("Updated notification configuration");
    }

    /// Get current notification configuration
    pub(crate) const fn notification_config(&self) -> &TuiNotifications {
        self.notification_system.config()
    }

    /// Initialize SessionManager lazily if not already initialized
    async fn ensure_session_manager_initialized(&self) -> Result<(), String> {
        let mut session_manager = self.session_manager.write().await;
        if session_manager.is_none() {
            let config = SessionManagerConfig {
                storage_path: self.config.codex_home.join("history"),
                auto_save_interval: Duration::from_secs(300), // 5 minutes
                ..Default::default()
            };

            match SessionManager::new(config).await {
                Ok(manager) => {
                    *session_manager = Some(manager);
                    tracing::info!("SessionManager initialized successfully");
                }
                Err(e) => {
                    let error_msg = format!("Failed to initialize SessionManager: {}", e);
                    tracing::error!("{}", error_msg);
                    return Err(error_msg);
                }
            }
        }
        Ok(())
    }

    /// Handle session save request
    async fn handle_save_session(
        &self,
        name: String,
        description: Option<String>,
    ) -> Result<Uuid, String> {
        self.ensure_session_manager_initialized().await?;

        let session_manager_guard = self.session_manager.read().await;
        let session_manager = session_manager_guard.as_ref().unwrap();

        // Convert current operating mode to persistence format
        let current_mode = match self.current_mode() {
            OperatingMode::Plan => PersistenceOperatingMode::Plan,
            OperatingMode::Build => PersistenceOperatingMode::Build,
            OperatingMode::Review => PersistenceOperatingMode::Review,
        };

        // TODO: Get current model from conversation manager
        let model = "gpt-4".to_string(); // Default for now

        let session_id = session_manager
            .create_session(name, model, current_mode)
            .await
            .map_err(|e| format!("Failed to create session: {}", e))?;

        // Update current session ID
        *self.current_session_id.write().await = Some(session_id);

        tracing::info!("Session saved with ID: {}", session_id);
        Ok(session_id)
    }

    /// Handle session load request
    async fn handle_load_session(&self, session_id: Uuid) -> Result<(), String> {
        self.ensure_session_manager_initialized().await?;

        let session_manager_guard = self.session_manager.read().await;
        let session_manager = session_manager_guard.as_ref().unwrap();

        session_manager
            .load_session(session_id)
            .await
            .map_err(|e| format!("Failed to load session: {}", e))?;

        // Update current session ID
        *self.current_session_id.write().await = Some(session_id);

        // TODO: Switch conversation state to loaded session
        // This would involve communicating with ConversationManager

        tracing::info!("Session loaded: {}", session_id);
        Ok(())
    }

    /// List all available sessions
    async fn handle_list_sessions(
        &self,
    ) -> Result<Vec<agcodex_persistence::types::SessionMetadata>, String> {
        self.ensure_session_manager_initialized().await?;

        let session_manager_guard = self.session_manager.read().await;
        let session_manager = session_manager_guard.as_ref().unwrap();

        session_manager
            .list_sessions()
            .await
            .map_err(|e| format!("Failed to list sessions: {}", e))
    }

    /// Stop auto-save timer
    async fn stop_auto_save_timer(&self) {
        let mut handle_guard = self.auto_save_handle.lock().await;
        if let Some(handle) = handle_guard.take() {
            handle.abort();
            tracing::info!("Auto-save timer stopped");
        }
    }

    /// Test notification system by triggering a test bell
    pub(crate) fn test_notification(
        &self,
        level: crate::notification::NotificationLevel,
    ) -> Result<()> {
        self.notification_system
            .notify(level)
            .map_err(|e| color_eyre::eyre::eyre!("Notification test failed: {}", e))
    }

    fn draw_next_frame(&mut self, terminal: &mut tui::Tui) -> Result<()> {
        if matches!(self.app_state, AppState::Onboarding { .. }) {
            terminal.clear()?;
        }

        let screen_size = terminal.size()?;
        let last_known_screen_size = terminal.last_known_screen_size;
        if screen_size != last_known_screen_size {
            let cursor_pos = terminal.get_cursor_position()?;
            let last_known_cursor_pos = terminal.last_known_cursor_pos;
            if cursor_pos.y != last_known_cursor_pos.y {
                // The terminal was resized. The only point of reference we have for where our viewport
                // was moved is the cursor position.
                // NB this assumes that the cursor was not wrapped as part of the resize.
                let cursor_delta = cursor_pos.y as i32 - last_known_cursor_pos.y as i32;

                let new_viewport_area = terminal.viewport_area.offset(Offset {
                    x: 0,
                    y: cursor_delta,
                });
                terminal.set_viewport_area(new_viewport_area);
                terminal.clear()?;
            }
        }

        let size = terminal.size()?;
        let desired_height = match &self.app_state {
            AppState::Chat { widget } => widget.desired_height(size.width),
            AppState::Onboarding { .. } => size.height,
        };

        let mut area = terminal.viewport_area;
        area.height = desired_height.min(size.height);
        area.width = size.width;
        if area.bottom() > size.height {
            terminal
                .backend_mut()
                .scroll_region_up(0..area.top(), area.bottom() - size.height)?;
            area.y = size.height - area.height;
        }
        if area != terminal.viewport_area {
            terminal.clear()?;
            terminal.set_viewport_area(area);
        }
        if !self.pending_history_lines.is_empty() {
            crate::insert_history::insert_history_lines(
                terminal,
                self.pending_history_lines.clone(),
            );
            self.pending_history_lines.clear();
        }
        // Extract the current mode before the mutable borrow
        let current_mode = self.current_mode();

        terminal.draw(|frame| match &mut self.app_state {
            AppState::Chat { widget } => {
                // Determine if agent panel is visible
                let agent_panel_visible = self.agent_panel.is_visible();

                // Create main layout
                let main_constraints = if agent_panel_visible {
                    vec![
                        Constraint::Length(3),  // Mode indicator height
                        Constraint::Min(0),     // Chat widget
                        Constraint::Length(15), // Agent panel height
                    ]
                } else {
                    vec![
                        Constraint::Length(3), // Mode indicator height
                        Constraint::Min(0),    // Chat widget takes the rest
                    ]
                };

                let main_layout = Layout::default()
                    .direction(Direction::Vertical)
                    .constraints(main_constraints)
                    .split(frame.area());

                // Create horizontal layout for the top area (mode indicator on the right)
                let top_layout = Layout::default()
                    .direction(Direction::Horizontal)
                    .constraints([
                        Constraint::Min(0),     // Empty space on the left
                        Constraint::Length(15), // Mode indicator width
                    ])
                    .split(main_layout[0]);

                // Render mode indicator
                let mode_indicator = ModeIndicator::new(current_mode);
                frame.render_widget(mode_indicator, top_layout[1]);

                // Render chat widget
                let chat_area = main_layout[1];
                if let Some((x, y)) = widget.cursor_pos(chat_area) {
                    frame.set_cursor_position((x, y));
                }
                frame.render_widget_ref(&**widget, chat_area);

                // Render agent panel if visible
                if agent_panel_visible && main_layout.len() > 2 {
                    frame.render_widget_ref(&self.agent_panel, main_layout[2]);
                }
            }
            AppState::Onboarding { screen } => {
                // For onboarding, still show mode indicator but simpler layout
                let layout = Layout::default()
                    .direction(Direction::Vertical)
                    .constraints([
                        Constraint::Length(3), // Mode indicator height
                        Constraint::Min(0),    // Onboarding screen takes the rest
                    ])
                    .split(frame.area());

                let top_layout = Layout::default()
                    .direction(Direction::Horizontal)
                    .constraints([
                        Constraint::Min(0),     // Empty space on the left
                        Constraint::Length(15), // Mode indicator width
                    ])
                    .split(layout[0]);

                // Render mode indicator
                let mode_indicator = ModeIndicator::new(current_mode);
                frame.render_widget(mode_indicator, top_layout[1]);

                // Render onboarding screen
                frame.render_widget_ref(&*screen, layout[1]);
            }
        })?;
        Ok(())
    }

    /// Dispatch a KeyEvent to the current view and let it decide what to do
    /// with it.
    fn dispatch_key_event(&mut self, key_event: KeyEvent) {
        // Check if agent panel is visible and should handle the key event
        if self.agent_panel.is_visible() {
            match key_event {
                KeyEvent {
                    code: KeyCode::Up,
                    kind: KeyEventKind::Press,
                    ..
                } => {
                    self.app_event_tx.send(AppEvent::AgentPanelNavigateUp);
                    return;
                }
                KeyEvent {
                    code: KeyCode::Down,
                    kind: KeyEventKind::Press,
                    ..
                } => {
                    self.app_event_tx.send(AppEvent::AgentPanelNavigateDown);
                    return;
                }
                KeyEvent {
                    code: KeyCode::Enter,
                    kind: KeyEventKind::Press,
                    ..
                } => {
                    self.app_event_tx.send(AppEvent::AgentPanelCancel);
                    return;
                }
                KeyEvent {
                    code: KeyCode::Esc,
                    kind: KeyEventKind::Press,
                    ..
                } => {
                    self.agent_panel.set_visible(false);
                    self.app_event_tx.send(AppEvent::RequestRedraw);
                    return;
                }
                KeyEvent {
                    code: KeyCode::Char('c'),
                    kind: KeyEventKind::Press,
                    ..
                } => {
                    self.agent_panel.clear_completed();
                    self.app_event_tx.send(AppEvent::RequestRedraw);
                    return;
                }
                _ => {
                    // Let other keys fall through to normal handling
                }
            }
        }

        match &mut self.app_state {
            AppState::Chat { widget } => {
                widget.handle_key_event(key_event);
            }
            AppState::Onboarding { screen } => match key_event.code {
                KeyCode::Char('q') => {
                    self.app_event_tx.send(AppEvent::ExitRequest);
                }
                _ => screen.handle_key_event(key_event),
            },
        }
    }

    fn dispatch_paste_event(&mut self, pasted: String) {
        match &mut self.app_state {
            AppState::Chat { widget } => widget.handle_paste(pasted),
            AppState::Onboarding { .. } => {}
        }
    }

    fn dispatch_codex_event(&mut self, event: Event) {
        // Enhanced notification handling for error events with specific messages
        if let agcodex_core::protocol::EventMsg::Error(error_msg) = &event.msg
            && let Err(e) = self
                .notification_system
                .error_occurred_with_message(&error_msg.message)
        {
            tracing::warn!("Failed to ring terminal bell for error event: {}", e);
        }
        // Also ring for turn aborted events
        if let agcodex_core::protocol::EventMsg::TurnAborted(_) = &event.msg
            && let Err(e) = self
                .notification_system
                .error_occurred_with_message("Turn aborted")
        {
            tracing::warn!("Failed to ring terminal bell for turn aborted: {}", e);
        }
        // Ring for approval requests (user input needed) with context
        match &event.msg {
            agcodex_core::protocol::EventMsg::ExecApprovalRequest(req) => {
                let message = format!("Approval needed for: {}", req.command.join(" "));
                if let Err(e) = self
                    .notification_system
                    .user_input_needed_with_message(&message)
                {
                    tracing::warn!(
                        "Failed to ring terminal bell for exec approval request: {}",
                        e
                    );
                }
            }
            agcodex_core::protocol::EventMsg::ApplyPatchApprovalRequest(_) => {
                if let Err(e) = self
                    .notification_system
                    .user_input_needed_with_message("Patch approval required")
                {
                    tracing::warn!(
                        "Failed to ring terminal bell for patch approval request: {}",
                        e
                    );
                }
            }
            _ => {}
        }

        match &mut self.app_state {
            AppState::Chat { widget } => widget.handle_codex_event(event),
            AppState::Onboarding { .. } => {}
        }
    }
}

impl Drop for App<'_> {
    fn drop(&mut self) {
        // Stop auto-save timer on drop
        let auto_save_handle = self.auto_save_handle.clone();
        tokio::spawn(async move {
            let mut handle_guard = auto_save_handle.lock().await;
            if let Some(handle) = handle_guard.take() {
                handle.abort();
                tracing::debug!("Auto-save timer stopped on App drop");
            }
        });
    }
}

fn should_show_onboarding(
    login_status: LoginStatus,
    config: &Config,
    show_trust_screen: bool,
) -> bool {
    if show_trust_screen {
        return true;
    }

    should_show_login_screen(login_status, config)
}

fn should_show_login_screen(login_status: LoginStatus, config: &Config) -> bool {
    // Only show the login screen for providers that actually require OpenAI auth
    // (OpenAI or equivalents). For OSS/other providers, skip login entirely.
    if !config.model_provider.requires_openai_auth {
        return false;
    }

    match login_status {
        LoginStatus::NotAuthenticated => true,
        LoginStatus::AuthMode(method) => method != config.preferred_auth_method,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use agcodex_core::config::ConfigOverrides;
    use agcodex_core::config::ConfigToml;
    use agcodex_login::AuthMode;

    fn make_config(preferred: AuthMode) -> Config {
        let mut cfg = Config::load_from_base_config_with_overrides(
            ConfigToml::default(),
            ConfigOverrides::default(),
            std::env::temp_dir(),
        )
        .expect("load default config");
        cfg.preferred_auth_method = preferred;
        cfg
    }

    #[test]
    fn shows_login_when_not_authenticated() {
        let cfg = make_config(AuthMode::ChatGPT);
        assert!(should_show_login_screen(
            LoginStatus::NotAuthenticated,
            &cfg
        ));
    }

    #[test]
    fn shows_login_when_api_key_but_prefers_chatgpt() {
        let cfg = make_config(AuthMode::ChatGPT);
        assert!(should_show_login_screen(
            LoginStatus::AuthMode(AuthMode::ApiKey),
            &cfg
        ))
    }

    #[test]
    fn hides_login_when_api_key_and_prefers_api_key() {
        let cfg = make_config(AuthMode::ApiKey);
        assert!(!should_show_login_screen(
            LoginStatus::AuthMode(AuthMode::ApiKey),
            &cfg
        ))
    }

    #[test]
    fn hides_login_when_chatgpt_and_prefers_chatgpt() {
        let cfg = make_config(AuthMode::ChatGPT);
        assert!(!should_show_login_screen(
            LoginStatus::AuthMode(AuthMode::ChatGPT),
            &cfg
        ))
    }
}