tmai-core 0.8.2

Core library for tmai - agent detection, state management, and monitoring
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
use anyhow::Result;
use once_cell::sync::Lazy;
use regex::Regex;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use tokio::sync::mpsc;

use crate::agents::{
    AgentStatus, AgentTeamInfo, AgentType, ApprovalType, DetectionSource, MonitoredAgent,
    TeamTaskSummaryItem,
};
use crate::api::CoreEvent;
use crate::audit::{AuditEvent, AuditLogger};
use crate::config::{ClaudeSettingsCache, Settings};
use crate::detectors::ClaudeCodeDetector;
use crate::detectors::GeminiDetector;
use crate::detectors::{get_detector, DetectionConfidence, DetectionContext, DetectionReason};
use crate::git::GitCache;
use crate::ipc::protocol::{WrapApprovalType, WrapState, WrapStatus};
use crate::ipc::server::IpcRegistry;
use crate::state::{MonitorScope, SharedState, TeamSnapshot};
use crate::teams::{self, TaskStatus};
use crate::tmux::{PaneInfo, ProcessCache, TmuxClient};

/// Tracks the last committed (actually emitted) state for an agent
struct CommittedAgentState {
    status: String,
    /// Full AgentStatus preserved for debounce override (retains activity/error text)
    full_status: AgentStatus,
    #[allow(dead_code)]
    reason: DetectionReason,
    agent_type: String,
    committed_at_ms: u64,
}

/// A pending state transition waiting to be committed after debounce period
struct PendingTransition {
    new_status: String,
    new_reason: DetectionReason,
    first_seen: Instant,
}

/// Calculate debounce threshold for a state transition
fn debounce_threshold(from: &str, to: &str) -> Duration {
    // Approval should be shown immediately
    if to == "awaiting_approval" {
        return Duration::from_millis(0);
    }
    // User action after approval is fast
    if from == "awaiting_approval" {
        return Duration::from_millis(200);
    }
    // idle<->processing oscillation is the main noise source
    if (from == "idle" && to == "processing") || (from == "processing" && to == "idle") {
        return Duration::from_millis(500);
    }
    // Default
    Duration::from_millis(300)
}

/// Message sent from poller to main loop
#[derive(Debug)]
pub enum PollMessage {
    /// Updated list of agents
    AgentsUpdated(Vec<MonitoredAgent>),
    /// Error during polling
    Error(String),
}

/// Poller for monitoring tmux panes
pub struct Poller {
    client: TmuxClient,
    process_cache: Arc<ProcessCache>,
    /// Cache for Claude Code settings (spinnerVerbs)
    claude_settings_cache: Arc<ClaudeSettingsCache>,
    settings: Settings,
    state: SharedState,
    /// IPC registry for reading wrapper states
    ipc_registry: IpcRegistry,
    /// Current session name (captured at startup, unused while scope is disabled)
    #[allow(dead_code)]
    current_session: Option<String>,
    /// Current window index (captured at startup, unused while scope is disabled)
    #[allow(dead_code)]
    current_window: Option<u32>,
    /// Audit logger for detection events
    audit_logger: AuditLogger,
    /// Receiver for audit events from external sources (UI, Web API)
    audit_event_rx: Option<tokio::sync::mpsc::UnboundedReceiver<AuditEvent>>,
    /// Previous status per agent target for change detection
    previous_statuses: HashMap<String, CommittedAgentState>,
    /// Pending state transitions waiting for debounce period to elapse
    pending_transitions: HashMap<String, PendingTransition>,
    /// Set of agent targets seen in the previous poll
    previous_agent_ids: HashSet<String>,
    /// Grace period tracker: keeps agents in Processing for up to 6 seconds after
    /// the spinner disappears, preventing Processing→Idle→Processing flicker
    /// during tool call gaps.
    grace_periods: HashMap<String, Instant>,
    /// Git branch/dirty cache for agent cwd directories
    git_cache: GitCache,
    /// Core event sender for pushing TeammateIdle/TaskCompleted events
    event_tx: Option<tokio::sync::broadcast::Sender<CoreEvent>>,
}

impl Poller {
    /// Create a new poller
    pub fn new(
        settings: Settings,
        state: SharedState,
        ipc_registry: IpcRegistry,
        audit_event_rx: Option<tokio::sync::mpsc::UnboundedReceiver<AuditEvent>>,
    ) -> Self {
        let client = TmuxClient::with_capture_lines(settings.capture_lines);

        // Capture current location at startup for scope filtering
        let (current_session, current_window) = match client.get_current_location() {
            Ok((session, window)) => (Some(session), Some(window)),
            Err(_) => (None, None),
        };

        let audit_logger = AuditLogger::new(settings.audit.enabled, settings.audit.max_size_bytes);

        Self {
            client,
            process_cache: Arc::new(ProcessCache::new()),
            claude_settings_cache: Arc::new(ClaudeSettingsCache::new()),
            settings,
            state,
            ipc_registry,
            current_session,
            current_window,
            audit_logger,
            audit_event_rx,
            previous_statuses: HashMap::new(),
            pending_transitions: HashMap::new(),
            previous_agent_ids: HashSet::new(),
            grace_periods: HashMap::new(),
            git_cache: GitCache::new(),
            event_tx: None,
        }
    }

    /// Set the core event sender for TeammateIdle/TaskCompleted notifications
    pub fn with_event_tx(mut self, tx: tokio::sync::broadcast::Sender<CoreEvent>) -> Self {
        self.event_tx = Some(tx);
        self
    }

    /// Start polling in a background task
    pub fn start(self) -> mpsc::Receiver<PollMessage> {
        let (tx, rx) = mpsc::channel(32);

        tokio::spawn(async move {
            self.run(tx).await;
        });

        rx
    }

    /// Run the polling loop
    async fn run(mut self, tx: mpsc::Sender<PollMessage>) {
        let normal_interval = self.settings.poll_interval_ms;
        let fast_interval = self.settings.passthrough_poll_interval_ms;
        let mut backoff_ms: u64 = 0;
        let mut last_error: Option<String> = None;
        let mut last_error_at: Option<Instant> = None;
        let mut poll_count: u32 = 0;

        loop {
            // Check if we should stop and get passthrough state
            let (should_stop, is_passthrough) = {
                let state = self.state.read();
                (!state.running, state.is_passthrough_mode())
            };

            if should_stop {
                break;
            }

            // Use faster interval in passthrough mode for responsive preview updates
            let base_interval_ms = if is_passthrough {
                fast_interval
            } else {
                normal_interval
            };
            let interval_ms = base_interval_ms.saturating_add(backoff_ms);
            tokio::time::sleep(Duration::from_millis(interval_ms)).await;

            match self.poll_once().await {
                Ok((mut agents, all_panes)) => {
                    backoff_ms = 0;
                    last_error = None;
                    last_error_at = None;

                    // Periodic cache cleanup (every 10 polls)
                    poll_count = poll_count.wrapping_add(1);
                    if poll_count.is_multiple_of(10) {
                        self.process_cache.cleanup();
                        // Remove expired grace periods (> 30s old to avoid unbounded growth)
                        self.grace_periods
                            .retain(|_, ts| ts.elapsed().as_secs() < 30);
                    }

                    // Team scanning at configured interval, or re-apply cached info
                    if self.settings.teams.enabled {
                        if poll_count.is_multiple_of(self.settings.teams.scan_interval) {
                            self.scan_and_apply_teams(&mut agents, &all_panes);
                        } else {
                            self.apply_cached_team_info(&mut agents);
                        }
                    }

                    // Git branch detection (every ~10 seconds)
                    if poll_count.is_multiple_of(20) {
                        self.update_git_info(&mut agents).await;
                        self.git_cache.cleanup();
                    } else {
                        self.apply_cached_git_info(&mut agents);
                    }

                    // Audit: track state transitions
                    self.emit_audit_events(&mut agents);

                    // Drain externally-submitted audit events (from UI/Web)
                    if let Some(ref mut rx) = self.audit_event_rx {
                        while let Ok(event) = rx.try_recv() {
                            self.audit_logger.log(&event);
                        }
                    }

                    if tx.send(PollMessage::AgentsUpdated(agents)).await.is_err() {
                        break; // Receiver dropped
                    }
                }
                Err(e) => {
                    let err_str = e.to_string();
                    let should_send = match &last_error {
                        Some(prev) if prev == &err_str => last_error_at
                            .map(|t| t.elapsed() >= Duration::from_secs(2))
                            .unwrap_or(true),
                        _ => true,
                    };

                    if should_send && tx.send(PollMessage::Error(err_str.clone())).await.is_err() {
                        break;
                    }

                    last_error = Some(err_str);
                    last_error_at = Some(Instant::now());
                    backoff_ms = if backoff_ms == 0 {
                        200
                    } else {
                        (backoff_ms * 2).min(2000)
                    };
                }
            }
        }
    }

    /// Perform a single poll
    ///
    /// Returns `(agents, all_panes)` where `all_panes` includes detached sessions
    /// for use in team scanning.
    async fn poll_once(&mut self) -> Result<(Vec<MonitoredAgent>, Vec<PaneInfo>)> {
        // Always get all panes (needed for team scanning)
        let all_panes = self.client.list_all_panes()?;

        // Use all panes (scope filtering temporarily disabled — always AllSessions)
        let panes = all_panes.clone();

        let selected_agent_id = {
            let state = self.state.read();
            state
                .agent_order
                .get(state.selection.selected_index)
                .cloned()
        };

        // Filter and convert to monitored agents
        let mut agents = Vec::new();

        for pane in panes {
            // Get cmdline from process cache for better detection
            // Try direct cmdline first, then child process cmdline (for shell -> agent)
            let direct_cmdline = self.process_cache.get_cmdline(pane.pid);
            let child_cmdline = self.process_cache.get_child_cmdline(pane.pid);

            // Try detection with child cmdline first (more specific for agents under shell)
            let agent_type = pane
                .detect_agent_type_with_cmdline(child_cmdline.as_deref())
                .or_else(|| pane.detect_agent_type_with_cmdline(direct_cmdline.as_deref()));

            if let Some(agent_type) = agent_type {
                // Try to read state from IPC registry first
                // Use pane_id (global unique ID like "5" from "%5") not pane_index (local window index)
                let wrap_state = {
                    let registry = self.ipc_registry.read();
                    registry.get(&pane.pane_id).cloned()
                };
                let is_selected = selected_agent_id.as_ref() == Some(&pane.target);

                // Optimize capture-pane based on selection and IPC state:
                // - Selected: ANSI capture for preview
                // - Non-selected + IPC: skip capture-pane entirely (state from IPC registry)
                // - Non-selected + capture-pane mode: plain capture for detection only
                let (content_ansi, mut content) = if is_selected {
                    // Selected agent: full ANSI capture for preview
                    let ansi = self.client.capture_pane(&pane.target).unwrap_or_default();
                    let plain = strip_ansi(&ansi);
                    (ansi, plain)
                } else if wrap_state.is_some() {
                    // Non-selected + IPC mode: skip capture-pane entirely
                    (String::new(), String::new())
                } else {
                    // Non-selected + capture-pane mode: plain capture for detection
                    let plain = self
                        .client
                        .capture_pane_plain(&pane.target)
                        .unwrap_or_default();
                    (String::new(), plain)
                };

                let title = self
                    .client
                    .get_pane_title(&pane.target)
                    .unwrap_or(pane.title.clone());

                // Determine status: use IPC state if available, otherwise detect from content
                let mut screen_override = false;
                let (status, context_warning, detection_reason) = if let Some(ref ws) = wrap_state {
                    // Convert WrapState to AgentStatus
                    let status = wrap_state_to_agent_status(ws);

                    // P1: IPC Approval lag correction — when IPC reports non-Approval,
                    // check screen content for High-confidence Approval patterns
                    if !matches!(status, AgentStatus::AwaitingApproval { .. }) {
                        // Reuse existing content if available (selected agent already has
                        // plain text from ANSI capture), otherwise capture for non-selected agents
                        let plain = if !content.is_empty() {
                            content.clone()
                        } else {
                            self.client
                                .capture_pane_plain(&pane.target)
                                .unwrap_or_default()
                        };
                        let detection_context = DetectionContext {
                            cwd: Some(pane.cwd.as_str()),
                            settings_cache: Some(&self.claude_settings_cache),
                        };
                        let detector = get_detector(&agent_type);
                        let result =
                            detector.detect_status_with_reason(&title, &plain, &detection_context);
                        if matches!(result.status, AgentStatus::AwaitingApproval { .. })
                            && result.reason.confidence == DetectionConfidence::High
                        {
                            // Screen override: Approval visible on screen but IPC lagging
                            let context_warning = detector.detect_context_warning(&plain);
                            content = plain;
                            screen_override = true;
                            (result.status, context_warning, Some(result.reason))
                        } else {
                            // Enrich IPC Processing with screen-detected activity
                            // (e.g., "Compacting..." from title or spinner verb)
                            let status = enrich_ipc_activity(status, &result.status, &title);
                            let matched_text =
                                if let AgentStatus::Processing { ref activity } = status {
                                    if !activity.is_empty() {
                                        Some(format!("enriched: {}", activity))
                                    } else {
                                        None
                                    }
                                } else {
                                    None
                                };
                            let reason = DetectionReason {
                                rule: "ipc_state".to_string(),
                                confidence: DetectionConfidence::High,
                                matched_text,
                            };
                            (status, None, Some(reason))
                        }
                    } else {
                        let reason = DetectionReason {
                            rule: "ipc_state".to_string(),
                            confidence: DetectionConfidence::High,
                            matched_text: None,
                        };
                        (status, None, Some(reason))
                    }
                } else {
                    // Build detection context for this pane
                    let detection_context = DetectionContext {
                        cwd: Some(pane.cwd.as_str()),
                        settings_cache: Some(&self.claude_settings_cache),
                    };

                    // Detect status using appropriate detector with reason
                    let detector = get_detector(&agent_type);
                    let result =
                        detector.detect_status_with_reason(&title, &content, &detection_context);
                    let context_warning = detector.detect_context_warning(&content);
                    (result.status, context_warning, Some(result.reason))
                };

                // Grace period: prevent Processing→Idle flicker during tool call gaps.
                // When Processing is detected, record the timestamp.
                // When Idle or fallback is detected, maintain Processing if within 6 seconds.
                // Approval and Error always bypass the grace period.
                let status = self.apply_grace_period(&pane.target, status, &detection_reason);

                let mut agent = MonitoredAgent::new(
                    pane.target.clone(),
                    agent_type,
                    title,
                    pane.cwd.clone(),
                    pane.pid,
                    pane.session.clone(),
                    pane.window_name.clone(),
                    pane.window_index,
                    pane.pane_index,
                );
                agent.status = status;
                agent.last_content = content;
                agent.last_content_ansi = content_ansi;
                agent.context_warning = context_warning;
                agent.detection_reason = detection_reason;
                agent.detection_source = if screen_override {
                    DetectionSource::CapturePane
                } else if wrap_state.is_some() {
                    DetectionSource::IpcSocket
                } else {
                    DetectionSource::CapturePane
                };

                // Detect permission mode from title/content
                match agent.agent_type {
                    AgentType::ClaudeCode => {
                        agent.mode = ClaudeCodeDetector::detect_mode(&agent.title);
                    }
                    AgentType::GeminiCli => {
                        agent.mode = GeminiDetector::detect_mode(&agent.last_content);
                    }
                    _ => {}
                }

                agents.push(agent);
            }
        }

        // Sort by session and window/pane
        agents.sort_by(|a, b| {
            a.session
                .cmp(&b.session)
                .then(a.window_index.cmp(&b.window_index))
                .then(a.pane_index.cmp(&b.pane_index))
        });

        // Build target → pane_id mapping for IPC key sending
        let target_to_pane_id: HashMap<String, String> = all_panes
            .iter()
            .map(|p| (p.target.clone(), p.pane_id.clone()))
            .collect();

        // Update in app state
        {
            let mut state = self.state.write();
            state.target_to_pane_id = target_to_pane_id;
        }

        Ok((agents, all_panes))
    }

    /// Scan for teams and apply team info to agents
    ///
    /// Also performs cross-session scanning and creates virtual agents
    /// for team members whose panes are not found.
    fn scan_and_apply_teams(&self, agents: &mut Vec<MonitoredAgent>, all_panes: &[PaneInfo]) {
        let team_configs = match teams::scan_teams() {
            Ok(configs) => configs,
            Err(_) => return,
        };

        if team_configs.is_empty() {
            // Clear teams from state
            let mut state = self.state.write();
            state.teams.clear();
            return;
        }

        // Collect agent pids for mapping (from already-detected agents)
        let agent_pids: Vec<(String, u32)> =
            agents.iter().map(|a| (a.target.clone(), a.pid)).collect();

        // Also collect pids from all panes for broader matching
        let all_pane_pids: Vec<(String, u32)> = all_panes
            .iter()
            .map(|p| (p.target.clone(), p.pid))
            .collect();

        // Pre-build cmdline cache to avoid duplicate lookups for overlapping pids
        let mut cmdline_cache: HashMap<u32, Option<String>> = HashMap::new();
        for (_, pid) in agent_pids.iter().chain(all_pane_pids.iter()) {
            cmdline_cache.entry(*pid).or_insert_with(|| {
                self.process_cache
                    .get_child_cmdline(*pid)
                    .or_else(|| self.process_cache.get_cmdline(*pid))
            });
        }

        // Deduplicate (target, pid) pairs to avoid redundant iterations
        let mut unique_pids: HashMap<u32, String> = HashMap::new();
        for (target, pid) in agent_pids.iter().chain(all_pane_pids.iter()) {
            unique_pids.entry(*pid).or_insert_with(|| target.clone());
        }

        let mut snapshots: HashMap<String, TeamSnapshot> = HashMap::new();

        for team_config in &team_configs {
            // Scan tasks for this team
            let tasks = teams::scan_tasks(&team_config.team_name).unwrap_or_default();

            // Map members to panes using all pane pids (broader scope)
            let member_panes = teams::map_members_to_panes(team_config, &all_pane_pids);

            // Try cmdline-based matching: child process cmdline contains --agent-id
            let mut cmdline_mapping: HashMap<String, String> = HashMap::new();
            let mut matched_members: std::collections::HashSet<&str> =
                std::collections::HashSet::new();
            for (pid, target) in &unique_pids {
                if matched_members.len() == team_config.members.len() {
                    break; // All members matched
                }
                if let Some(Some(cl)) = cmdline_cache.get(pid) {
                    for member in &team_config.members {
                        if matched_members.contains(member.name.as_str()) {
                            continue; // Already matched
                        }
                        // Match --agent-id member_agent_id in cmdline
                        let marker = format!("--agent-id {}", member.agent_id);
                        if cl.contains(&marker) {
                            cmdline_mapping.insert(member.name.clone(), target.clone());
                            matched_members.insert(&member.name);
                            break;
                        }
                    }
                }
            }

            // Merge mappings (cmdline-based takes priority over heuristic)
            let mut final_mapping = member_panes;
            for (name, target) in cmdline_mapping {
                final_mapping.insert(name, target);
            }

            // Fallback: match unmapped leader by cwd (leader has no --agent-id flag)
            if let Some(leader) = team_config.members.first() {
                if !final_mapping.contains_key(&leader.name) {
                    if let Some(leader_cwd) = &leader.cwd {
                        let mapped_targets: std::collections::HashSet<&str> =
                            final_mapping.values().map(|s| s.as_str()).collect();
                        // Find an agent with matching cwd that isn't already mapped
                        if let Some(agent) = agents.iter().find(|a| {
                            a.cwd == *leader_cwd
                                && !a.is_virtual
                                && !mapped_targets.contains(a.target.as_str())
                                && a.team_info.is_none()
                        }) {
                            final_mapping.insert(leader.name.clone(), agent.target.clone());
                        }
                    }
                }
            }

            // Apply team info to matching agents and detect out-of-scope panes
            for (member_name, pane_target) in &final_mapping {
                let member_cfg = team_config.members.iter().find(|m| &m.name == member_name);
                let is_lead = team_config
                    .members
                    .first()
                    .map(|m| &m.name == member_name)
                    .unwrap_or(false);

                let team_info = build_member_team_info(
                    &team_config.team_name,
                    member_name,
                    member_cfg.and_then(|m| m.agent_type.as_deref()),
                    is_lead,
                    &tasks,
                );

                if let Some(agent) = agents.iter_mut().find(|a| &a.target == pane_target) {
                    // Agent already in list — apply team info
                    agent.team_info = Some(team_info);
                } else if let Some(pane) = all_panes.iter().find(|p| &p.target == pane_target) {
                    // Pane found but not in agents list (out of scope) — add as new agent
                    let agent_type = pane.detect_agent_type().unwrap_or(AgentType::ClaudeCode);
                    let mut new_agent = MonitoredAgent::new(
                        pane.target.clone(),
                        agent_type,
                        pane.title.clone(),
                        pane.cwd.clone(),
                        pane.pid,
                        pane.session.clone(),
                        pane.window_name.clone(),
                        pane.window_index,
                        pane.pane_index,
                    );
                    new_agent.team_info = Some(team_info);
                    agents.push(new_agent);
                }
            }

            // Determine cwd for virtual agents from any matched teammate
            let team_cwd: String = final_mapping
                .values()
                .find_map(|target| agents.iter().find(|a| &a.target == target))
                .map(|a| a.cwd.clone())
                .unwrap_or_default();

            // Create virtual agents for members without detected panes
            for member in &team_config.members {
                if !final_mapping.contains_key(&member.name) {
                    let is_lead = team_config
                        .members
                        .first()
                        .map(|m| m.name == member.name)
                        .unwrap_or(false);

                    let team_info = build_member_team_info(
                        &team_config.team_name,
                        &member.name,
                        member.agent_type.as_deref(),
                        is_lead,
                        &tasks,
                    );
                    agents.push(create_virtual_agent(
                        &team_config.team_name,
                        &member.name,
                        team_info,
                        &team_cwd,
                    ));
                }
            }

            // Collect worktree names from mapped agents
            let mut worktree_names = Vec::new();
            for pane_target in final_mapping.values() {
                if let Some(agent) = agents.iter_mut().find(|a| &a.target == pane_target) {
                    // Check if the member's cwd is within a worktree path
                    let wt_name = agent
                        .worktree_name
                        .clone()
                        .or_else(|| crate::git::extract_claude_worktree_name(&agent.cwd));

                    if let Some(ref name) = wt_name {
                        // Set worktree info on the agent if not already set
                        if agent.worktree_name.is_none() {
                            agent.worktree_name = Some(name.clone());
                        }
                        if agent.is_worktree != Some(true) {
                            agent.is_worktree = Some(true);
                        }
                        if !worktree_names.contains(name) {
                            worktree_names.push(name.clone());
                        }
                    }
                }
            }

            // Also include worktree names from team config (including unmapped members)
            for member in &team_config.members {
                if let Some(wt_name) = member.worktree_name() {
                    if !worktree_names.contains(&wt_name) {
                        worktree_names.push(wt_name);
                    }
                }
            }

            let task_done = tasks
                .iter()
                .filter(|t| t.status == TaskStatus::Completed)
                .count();
            let task_total = tasks.len();
            let task_in_progress = tasks
                .iter()
                .filter(|t| t.status == TaskStatus::InProgress)
                .count();
            let task_pending = tasks
                .iter()
                .filter(|t| t.status == TaskStatus::Pending)
                .count();

            snapshots.insert(
                team_config.team_name.clone(),
                TeamSnapshot {
                    config: team_config.clone(),
                    tasks,
                    member_panes: final_mapping,
                    last_scan: chrono::Utc::now(),
                    task_done,
                    task_total,
                    task_in_progress,
                    task_pending,
                    worktree_names,
                },
            );
        }

        // Detect newly completed tasks by comparing with previous snapshots
        {
            let prev_state = self.state.read();
            for (team_name, new_snapshot) in &snapshots {
                if let Some(prev_snapshot) = prev_state.teams.get(team_name) {
                    let prev_completed: HashSet<&str> = prev_snapshot
                        .tasks
                        .iter()
                        .filter(|t| t.status == TaskStatus::Completed)
                        .map(|t| t.id.as_str())
                        .collect();

                    for task in &new_snapshot.tasks {
                        if task.status == TaskStatus::Completed
                            && !prev_completed.contains(task.id.as_str())
                        {
                            if let Some(ref tx) = self.event_tx {
                                let _ = tx.send(CoreEvent::TaskCompleted {
                                    team_name: team_name.clone(),
                                    task_id: task.id.clone(),
                                    task_subject: task.subject.clone(),
                                });
                            }
                        }
                    }
                }
            }
        }

        // Scan agent definitions from project directories
        let project_dir = self.detect_project_dir(agents);
        let agent_defs =
            crate::teams::agents_scanner::scan_agent_definitions(project_dir.as_deref());

        // Update state with team snapshots and agent definitions
        let mut state = self.state.write();
        state.teams = snapshots;
        state.agent_definitions = agent_defs;
    }

    /// Detect project directory from agent cwds
    ///
    /// Uses the first non-virtual agent's cwd as a heuristic for the project root.
    fn detect_project_dir(&self, agents: &[MonitoredAgent]) -> Option<std::path::PathBuf> {
        agents
            .iter()
            .find(|a| !a.is_virtual && !a.cwd.is_empty())
            .map(|a| std::path::PathBuf::from(&a.cwd))
    }

    /// Re-apply cached team info from stored snapshots on non-scan polls
    ///
    /// Since `poll_once()` creates fresh `MonitoredAgent` instances every poll,
    /// team info would be lost on polls where `scan_and_apply_teams` doesn't run.
    /// This method uses the persisted `TeamSnapshot` data to re-apply team info.
    fn apply_cached_team_info(&self, agents: &mut Vec<MonitoredAgent>) {
        let state = self.state.read();
        if state.teams.is_empty() {
            return;
        }

        for snapshot in state.teams.values() {
            for (member_name, pane_target) in &snapshot.member_panes {
                let member_cfg = snapshot
                    .config
                    .members
                    .iter()
                    .find(|m| &m.name == member_name);
                let is_lead = snapshot
                    .config
                    .members
                    .first()
                    .map(|m| &m.name == member_name)
                    .unwrap_or(false);

                let team_info = build_member_team_info(
                    &snapshot.config.team_name,
                    member_name,
                    member_cfg.and_then(|m| m.agent_type.as_deref()),
                    is_lead,
                    &snapshot.tasks,
                );

                if let Some(agent) = agents.iter_mut().find(|a| &a.target == pane_target) {
                    agent.team_info = Some(team_info);
                }
            }

            // Determine cwd for virtual agents from any matched teammate
            let team_cwd: String = snapshot
                .member_panes
                .values()
                .find_map(|target| agents.iter().find(|a| &a.target == target))
                .map(|a| a.cwd.clone())
                .unwrap_or_default();

            // Re-create virtual agents for unmapped members
            for member in &snapshot.config.members {
                if !snapshot.member_panes.contains_key(&member.name) {
                    let is_lead = snapshot
                        .config
                        .members
                        .first()
                        .map(|m| m.name == member.name)
                        .unwrap_or(false);

                    let team_info = build_member_team_info(
                        &snapshot.config.team_name,
                        &member.name,
                        member.agent_type.as_deref(),
                        is_lead,
                        &snapshot.tasks,
                    );
                    agents.push(create_virtual_agent(
                        &snapshot.config.team_name,
                        &member.name,
                        team_info,
                        &team_cwd,
                    ));
                }
            }
        }
    }

    /// Check if a pane matches the current monitor scope (temporarily unused)
    #[allow(dead_code)]
    fn matches_scope(&self, pane: &PaneInfo, scope: MonitorScope) -> bool {
        match scope {
            MonitorScope::AllSessions => true,
            MonitorScope::CurrentSession => self
                .current_session
                .as_ref()
                .map(|s| s == &pane.session)
                .unwrap_or(true),
            MonitorScope::CurrentWindow => {
                let session_match = self
                    .current_session
                    .as_ref()
                    .map(|s| s == &pane.session)
                    .unwrap_or(true);
                let window_match = self
                    .current_window
                    .map(|w| w == pane.window_index)
                    .unwrap_or(true);
                session_match && window_match
            }
        }
    }

    /// Emit audit events for state transitions with debounce
    fn emit_audit_events(&mut self, agents: &mut [MonitoredAgent]) {
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64;

        let current_ids: HashSet<String> = agents
            .iter()
            .filter(|a| !a.is_virtual)
            .map(|a| a.target.clone())
            .collect();

        // AgentDisappeared: was in previous, not in current
        for target in &self.previous_agent_ids {
            if !current_ids.contains(target) {
                let (last_status, agent_type) = self
                    .previous_statuses
                    .get(target)
                    .map(|c| (c.status.clone(), c.agent_type.clone()))
                    .unwrap_or_else(|| ("unknown".to_string(), "unknown".to_string()));
                self.audit_logger.log(&AuditEvent::AgentDisappeared {
                    ts,
                    pane_id: target.clone(),
                    agent_type,
                    last_status,
                });
                // Clean up pending transition for disappeared agent
                self.pending_transitions.remove(target);
            }
        }

        for agent in agents.iter_mut() {
            if agent.is_virtual {
                continue;
            }

            let current_status_name = status_name(&agent.status).to_string();
            let reason = agent
                .detection_reason
                .clone()
                .unwrap_or_else(|| DetectionReason {
                    rule: "unknown".to_string(),
                    confidence: DetectionConfidence::Low,
                    matched_text: None,
                });
            let source = agent.detection_source.label().to_string();

            if !self.previous_agent_ids.contains(&agent.target) {
                // AgentAppeared - no debounce needed
                self.audit_logger.log(&AuditEvent::AgentAppeared {
                    ts,
                    pane_id: agent.target.clone(),
                    agent_type: agent.agent_type.short_name().to_string(),
                    source,
                    initial_status: current_status_name.clone(),
                });
                self.previous_statuses.insert(
                    agent.target.clone(),
                    CommittedAgentState {
                        status: current_status_name,
                        full_status: agent.status.clone(),
                        reason,
                        agent_type: agent.agent_type.short_name().to_string(),
                        committed_at_ms: ts,
                    },
                );
                continue;
            }

            let committed = self.previous_statuses.get(&agent.target);
            let committed_status = committed.map(|c| c.status.as_str()).unwrap_or("unknown");

            if committed_status == current_status_name {
                // Status same as committed - cancel any pending transition (oscillation)
                self.pending_transitions.remove(&agent.target);
                continue;
            }

            // Status differs from committed - check debounce
            let threshold = debounce_threshold(committed_status, &current_status_name);

            if threshold.is_zero() {
                // Immediate commit (e.g., -> awaiting_approval)
                self.pending_transitions.remove(&agent.target);
                let prev_duration = committed.map(|c| ts.saturating_sub(c.committed_at_ms));

                let screen_context = if !agent.last_content.is_empty() {
                    let lines: Vec<&str> = agent.last_content.lines().collect();
                    let start = lines.len().saturating_sub(20);
                    let tail = lines[start..].join("\n");
                    Some(if tail.len() > 2000 {
                        tail[..tail.floor_char_boundary(2000)].to_string()
                    } else {
                        tail
                    })
                } else {
                    None
                };

                let (approval_type, approval_details) = extract_approval_info(&agent.status);
                self.audit_logger.log(&AuditEvent::StateChanged {
                    ts,
                    pane_id: agent.target.clone(),
                    agent_type: agent.agent_type.short_name().to_string(),
                    source,
                    prev_status: committed_status.to_string(),
                    new_status: current_status_name.clone(),
                    reason: reason.clone(),
                    screen_context,
                    prev_state_duration_ms: prev_duration,
                    approval_type,
                    approval_details,
                });

                // Emit TeammateIdle when a team member transitions to idle
                if current_status_name == "idle" && committed_status != "idle" {
                    self.emit_teammate_idle(agent);
                }

                self.previous_statuses.insert(
                    agent.target.clone(),
                    CommittedAgentState {
                        status: current_status_name,
                        full_status: agent.status.clone(),
                        reason,
                        agent_type: agent.agent_type.short_name().to_string(),
                        committed_at_ms: ts,
                    },
                );
            } else if let Some(pending) = self.pending_transitions.get(&agent.target) {
                if pending.new_status == current_status_name {
                    // Still in same pending transition - check if threshold elapsed
                    if pending.first_seen.elapsed() >= threshold {
                        // Commit the transition
                        let pending = self.pending_transitions.remove(&agent.target).unwrap();
                        let prev_duration = committed.map(|c| ts.saturating_sub(c.committed_at_ms));

                        let screen_context = if !agent.last_content.is_empty() {
                            let lines: Vec<&str> = agent.last_content.lines().collect();
                            let start = lines.len().saturating_sub(20);
                            let tail = lines[start..].join("\n");
                            Some(if tail.len() > 2000 {
                                tail[..tail.floor_char_boundary(2000)].to_string()
                            } else {
                                tail
                            })
                        } else {
                            None
                        };

                        let (approval_type, approval_details) =
                            extract_approval_info(&agent.status);
                        self.audit_logger.log(&AuditEvent::StateChanged {
                            ts,
                            pane_id: agent.target.clone(),
                            agent_type: agent.agent_type.short_name().to_string(),
                            source,
                            prev_status: committed_status.to_string(),
                            new_status: current_status_name.clone(),
                            reason: pending.new_reason.clone(),
                            screen_context,
                            prev_state_duration_ms: prev_duration,
                            approval_type,
                            approval_details,
                        });

                        // Emit TeammateIdle when a team member transitions to idle
                        if current_status_name == "idle" && committed_status != "idle" {
                            self.emit_teammate_idle(agent);
                        }

                        self.previous_statuses.insert(
                            agent.target.clone(),
                            CommittedAgentState {
                                status: current_status_name.clone(),
                                full_status: agent.status.clone(),
                                reason: pending.new_reason,
                                agent_type: agent.agent_type.short_name().to_string(),
                                committed_at_ms: ts,
                            },
                        );
                    } else {
                        // Still within debounce window - override agent status for UI stability
                        if let Some(committed) = self.previous_statuses.get(&agent.target) {
                            agent.status = committed.full_status.clone();
                        }
                    }
                } else {
                    // Different pending status - replace pending transition
                    self.pending_transitions.insert(
                        agent.target.clone(),
                        PendingTransition {
                            new_status: current_status_name.clone(),
                            new_reason: reason,
                            first_seen: Instant::now(),
                        },
                    );
                    // Override agent status for UI stability
                    if let Some(committed) = self.previous_statuses.get(&agent.target) {
                        agent.status = match committed.status.as_str() {
                            "idle" => AgentStatus::Idle,
                            "processing" => AgentStatus::Processing {
                                activity: String::new(),
                            },
                            "error" => AgentStatus::Error {
                                message: String::new(),
                            },
                            _ => agent.status.clone(),
                        };
                    }
                }
            } else {
                // No pending transition yet - start one
                self.pending_transitions.insert(
                    agent.target.clone(),
                    PendingTransition {
                        new_status: current_status_name.clone(),
                        new_reason: reason,
                        first_seen: Instant::now(),
                    },
                );
                // Override agent status for UI stability
                if let Some(committed) = self.previous_statuses.get(&agent.target) {
                    agent.status = match committed.status.as_str() {
                        "idle" => AgentStatus::Idle,
                        "processing" => AgentStatus::Processing {
                            activity: String::new(),
                        },
                        "error" => AgentStatus::Error {
                            message: String::new(),
                        },
                        _ => agent.status.clone(),
                    };
                }
            }
        }

        self.previous_agent_ids = current_ids;
    }

    /// Emit a TeammateIdle event if the agent belongs to a team
    fn emit_teammate_idle(&self, agent: &MonitoredAgent) {
        if let Some(ref team_info) = agent.team_info {
            if let Some(ref tx) = self.event_tx {
                let _ = tx.send(CoreEvent::TeammateIdle {
                    target: agent.target.clone(),
                    team_name: team_info.team_name.clone(),
                    member_name: team_info.member_name.clone(),
                });
            }
        }
    }

    /// Apply spinner grace period to prevent Processing→Idle flicker.
    ///
    /// When a spinner disappears between tool calls, the detector briefly sees Idle
    /// before the next tool starts. This method holds the agent in Processing for
    /// up to 6 seconds after the last Processing detection.
    ///
    /// Approval and Error statuses always bypass the grace period.
    fn apply_grace_period(
        &mut self,
        target: &str,
        status: AgentStatus,
        detection_reason: &Option<DetectionReason>,
    ) -> AgentStatus {
        const GRACE_PERIOD_SECS: u64 = 6;

        match &status {
            AgentStatus::Processing { .. } => {
                // Update grace period timestamp
                self.grace_periods
                    .insert(target.to_string(), Instant::now());
                status
            }
            AgentStatus::AwaitingApproval { .. } | AgentStatus::Error { .. } => {
                // Always pass through immediately — these are high-priority states
                self.grace_periods.remove(target);
                status
            }
            AgentStatus::Idle | AgentStatus::Unknown => {
                // Check if grace period applies (Idle or low-confidence fallback)
                let is_low_confidence = detection_reason
                    .as_ref()
                    .map(|r| {
                        r.rule == "fallback_no_indicator"
                            || r.confidence == DetectionConfidence::Low
                    })
                    .unwrap_or(false);

                if is_low_confidence || matches!(status, AgentStatus::Idle) {
                    if let Some(last_processing) = self.grace_periods.get(target) {
                        if last_processing.elapsed().as_secs() < GRACE_PERIOD_SECS {
                            // Within grace period — maintain Processing
                            return AgentStatus::Processing {
                                activity: String::new(),
                            };
                        } else {
                            // Grace period expired — allow transition
                            self.grace_periods.remove(target);
                        }
                    }
                }
                status
            }
            _ => status,
        }
    }

    /// Fetch and apply git branch/dirty info for all agents
    async fn update_git_info(&mut self, agents: &mut [MonitoredAgent]) {
        for agent in agents.iter_mut() {
            if agent.is_virtual || agent.cwd.is_empty() {
                continue;
            }
            if let Some(info) = self.git_cache.get_info(&agent.cwd).await {
                agent.git_branch = Some(info.branch);
                agent.git_dirty = Some(info.dirty);
                agent.is_worktree = Some(info.is_worktree);
                agent.git_common_dir = info.common_dir.clone();
                agent.worktree_name = crate::git::extract_claude_worktree_name(&agent.cwd);
            }
        }
    }

    /// Apply cached git info on non-refresh polls (no git commands executed)
    fn apply_cached_git_info(&self, agents: &mut [MonitoredAgent]) {
        for agent in agents.iter_mut() {
            if agent.is_virtual || agent.cwd.is_empty() {
                continue;
            }
            if let Some(info) = self.git_cache.get_cached(&agent.cwd) {
                agent.git_branch = Some(info.branch);
                agent.git_dirty = Some(info.dirty);
                agent.is_worktree = Some(info.is_worktree);
                agent.git_common_dir = info.common_dir.clone();
                agent.worktree_name = crate::git::extract_claude_worktree_name(&agent.cwd);
            }
        }
    }

    /// Cleanup the process cache
    pub fn cleanup_cache(&self) {
        self.process_cache.cleanup();
    }
}

/// Helper to detect agent from pane info
#[allow(dead_code)]
pub fn detect_agent_from_pane(pane: &PaneInfo, client: &TmuxClient) -> Option<MonitoredAgent> {
    let agent_type = pane.detect_agent_type()?;

    let content_ansi = client.capture_pane(&pane.target).unwrap_or_default();
    let content = strip_ansi(&content_ansi);
    let title = client
        .get_pane_title(&pane.target)
        .unwrap_or(pane.title.clone());

    let detector = get_detector(&agent_type);
    let status = detector.detect_status(&title, &content);
    let context_warning = detector.detect_context_warning(&content);

    let mut agent = MonitoredAgent::new(
        pane.target.clone(),
        agent_type,
        title,
        pane.cwd.clone(),
        pane.pid,
        pane.session.clone(),
        pane.window_name.clone(),
        pane.window_index,
        pane.pane_index,
    );
    agent.status = status;
    agent.last_content = content;
    agent.last_content_ansi = content_ansi;
    agent.context_warning = context_warning;

    Some(agent)
}

fn strip_ansi(input: &str) -> String {
    // Remove OSC and CSI sequences for detection logic.
    static OSC_RE: Lazy<Regex> =
        Lazy::new(|| Regex::new(r"\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)").unwrap());
    static CSI_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\x1b\[[0-9;?]*[ -/]*[@-~]").unwrap());

    let without_osc = OSC_RE.replace_all(input, "");
    CSI_RE.replace_all(&without_osc, "").to_string()
}

/// Build `AgentTeamInfo` for a team member, finding their current in-progress task.
fn build_member_team_info(
    team_name: &str,
    member_name: &str,
    agent_type: Option<&str>,
    is_lead: bool,
    tasks: &[teams::TeamTask],
) -> AgentTeamInfo {
    let current_task = tasks
        .iter()
        .find(|t| t.owner.as_deref() == Some(member_name) && t.status == TaskStatus::InProgress)
        .map(|t| TeamTaskSummaryItem {
            id: t.id.clone(),
            subject: t.subject.clone(),
            status: t.status,
            active_form: t.active_form.clone(),
        });

    AgentTeamInfo {
        team_name: team_name.to_string(),
        member_name: member_name.to_string(),
        agent_type: agent_type.map(|s| s.to_string()),
        is_lead,
        current_task,
    }
}

/// Create a virtual `MonitoredAgent` for a team member whose pane was not found.
///
/// `cwd` is inherited from a matched teammate so virtual agents group correctly.
fn create_virtual_agent(
    team_name: &str,
    member_name: &str,
    team_info: AgentTeamInfo,
    cwd: &str,
) -> MonitoredAgent {
    let virtual_target = format!("~team:{}:{}", team_name, member_name);
    let mut agent = MonitoredAgent::new(
        virtual_target,
        AgentType::ClaudeCode,
        String::new(),
        cwd.to_string(),
        0,
        String::new(),
        String::new(),
        0,
        0,
    );
    agent.status = AgentStatus::Offline;
    agent.is_virtual = true;
    agent.team_info = Some(team_info);
    agent
}

/// Extract approval_type and approval_details from an AgentStatus for audit logging
fn extract_approval_info(status: &AgentStatus) -> (Option<String>, Option<String>) {
    if let AgentStatus::AwaitingApproval {
        approval_type,
        details,
    } = status
    {
        let type_str = match approval_type {
            ApprovalType::FileEdit => "file_edit".to_string(),
            ApprovalType::FileCreate => "file_create".to_string(),
            ApprovalType::FileDelete => "file_delete".to_string(),
            ApprovalType::ShellCommand => "shell_command".to_string(),
            ApprovalType::McpTool => "mcp_tool".to_string(),
            ApprovalType::UserQuestion { .. } => "user_question".to_string(),
            ApprovalType::Other(s) => format!("other:{}", s),
        };
        let details_opt = if details.is_empty() {
            None
        } else {
            Some(details.clone())
        };
        (Some(type_str), details_opt)
    } else {
        (None, None)
    }
}

/// Get a short name for an AgentStatus variant
fn status_name(status: &AgentStatus) -> &'static str {
    match status {
        AgentStatus::Idle => "idle",
        AgentStatus::Processing { .. } => "processing",
        AgentStatus::AwaitingApproval { .. } => "awaiting_approval",
        AgentStatus::Error { .. } => "error",
        AgentStatus::Offline => "offline",
        AgentStatus::Unknown => "unknown",
    }
}

/// Extract activity text from a tmux pane title
///
/// Strips Braille spinner characters (`⠂`, `⠐`) and mode icons, returning the
/// remaining text as activity.  Returns empty string when an idle indicator (`✳`)
/// is present or when no meaningful text remains.
fn extract_activity_from_title(title: &str) -> String {
    // Idle indicator means no processing activity
    if title.contains('') {
        return String::new();
    }
    let cleaned: String = title
        .chars()
        .filter(|&c| !matches!(c, '' | '' | '' | '' | ''))
        .collect();
    let trimmed = cleaned.trim();
    if trimmed.is_empty() {
        return String::new();
    }
    trimmed.to_string()
}

/// Enrich IPC Processing status with screen-detected activity
///
/// When IPC reports Processing with empty activity, first try the screen-detected
/// activity, then fall back to extracting activity directly from the pane title.
fn enrich_ipc_activity(
    ipc_status: AgentStatus,
    screen_status: &AgentStatus,
    title: &str,
) -> AgentStatus {
    if let AgentStatus::Processing { ref activity } = ipc_status {
        if activity.is_empty() {
            // 1. Try screen-detected activity
            if let AgentStatus::Processing {
                activity: ref screen_activity,
            } = screen_status
            {
                if !screen_activity.is_empty() {
                    return AgentStatus::Processing {
                        activity: screen_activity.clone(),
                    };
                }
            }
            // 2. Fall back to title-based extraction
            let title_activity = extract_activity_from_title(title);
            if !title_activity.is_empty() {
                return AgentStatus::Processing {
                    activity: title_activity,
                };
            }
        }
    }
    ipc_status
}

/// Convert WrapState from state file to AgentStatus
fn wrap_state_to_agent_status(ws: &WrapState) -> AgentStatus {
    match ws.status {
        WrapStatus::Processing => AgentStatus::Processing {
            activity: String::new(),
        },
        WrapStatus::Idle => AgentStatus::Idle,
        WrapStatus::AwaitingApproval => {
            let approval_type = match ws.approval_type {
                Some(WrapApprovalType::UserQuestion) => ApprovalType::UserQuestion {
                    choices: ws.choices.clone(),
                    multi_select: ws.multi_select,
                    cursor_position: ws.cursor_position,
                },
                Some(WrapApprovalType::FileEdit) => ApprovalType::FileEdit,
                Some(WrapApprovalType::ShellCommand) => ApprovalType::ShellCommand,
                Some(WrapApprovalType::McpTool) => ApprovalType::McpTool,
                Some(WrapApprovalType::YesNo) => ApprovalType::Other("Yes/No".to_string()),
                Some(WrapApprovalType::Other) => ApprovalType::Other("Approval".to_string()),
                None => ApprovalType::Other("Approval".to_string()),
            };
            let details = ws.details.clone().unwrap_or_default();
            AgentStatus::AwaitingApproval {
                approval_type,
                details,
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::state::AppState;

    #[test]
    fn test_poller_creation() {
        let settings = Settings::default();
        let state = AppState::shared();
        let ipc_registry = Arc::new(parking_lot::RwLock::new(std::collections::HashMap::new()));
        let _poller = Poller::new(settings, state, ipc_registry, None);
    }

    #[test]
    fn test_extract_activity_from_title() {
        // Braille spinner + text
        assert_eq!(extract_activity_from_title("⠐ Compacting"), "Compacting");
        assert_eq!(extract_activity_from_title("⠂ Levitating"), "Levitating");
        // Idle indicator → empty
        assert_eq!(extract_activity_from_title(""), "");
        assert_eq!(extract_activity_from_title("✳ idle text"), "");
        // Empty / whitespace
        assert_eq!(extract_activity_from_title(""), "");
        assert_eq!(extract_activity_from_title("   "), "");
        // Pure spinner chars
        assert_eq!(extract_activity_from_title(""), "");
        // Mode icons stripped
        assert_eq!(extract_activity_from_title("⏸ ⠐ Planning"), "Planning");
    }

    #[test]
    fn test_enrich_ipc_activity_screen_priority() {
        let ipc = AgentStatus::Processing {
            activity: String::new(),
        };
        let screen = AgentStatus::Processing {
            activity: "✶ Compacting…".to_string(),
        };
        let result = enrich_ipc_activity(ipc, &screen, "⠐ Compacting");
        assert!(
            matches!(result, AgentStatus::Processing { ref activity } if activity == "✶ Compacting…")
        );
    }

    #[test]
    fn test_enrich_ipc_activity_title_fallback() {
        let ipc = AgentStatus::Processing {
            activity: String::new(),
        };
        // Screen returns Idle (e.g., ✳ in title caused screen detector to return Idle)
        let screen = AgentStatus::Idle;
        let result = enrich_ipc_activity(ipc, &screen, "⠐ Compacting");
        assert!(
            matches!(result, AgentStatus::Processing { ref activity } if activity == "Compacting")
        );
    }

    #[test]
    fn test_enrich_ipc_activity_no_enrichment_when_filled() {
        let ipc = AgentStatus::Processing {
            activity: "Already set".to_string(),
        };
        let screen = AgentStatus::Processing {
            activity: "Other".to_string(),
        };
        let result = enrich_ipc_activity(ipc, &screen, "⠐ Compacting");
        assert!(
            matches!(result, AgentStatus::Processing { ref activity } if activity == "Already set")
        );
    }
}