wsx 0.16.2

TUI workspace manager — git worktrees + tmux sessions in one tree
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
// Single source of truth for session state.
// `SessionInfo` carries raw inputs only (bell, foreground, pane_capture,
// last_activity, muted); UI and CLI consumers read the 3-state projection
// via `derive(...).app_state()` — never re-implement the logic.

use wsx_core::model::workspace::{ForegroundKind, SessionInfo};
use wsx_core::ops::IDLE_SECS;
use wsx_core::tmux::capture::{self, CaptureHint};

/// User-facing session state — the 3-state projection rendered as a tree icon.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AppSessionState {
    Idle,           // gray ○ — at rest
    Active,         // green ◉ — process is actively doing work, or running infra
    NeedsAttention, // yellow ● — bell, agent done, shell prompt, or interactive prompt waiting
}

/// Internal fine-grained classification. `derive` produces this; `app_state`
/// projects it to the 3 user-facing states. Carries the working/done
/// distinction agents need but the user-facing icon collapses away.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SessionHeuristic {
    Muted,
    Bell,
    WaitingForConfirm,
    WaitingForInput,
    AgentWorking,  // Agent foreground + recent output
    AgentDone,     // Agent foreground + no recent output (waiting for you to look)
    ServerRunning, // Runtime or InteractiveApp — green while the process exists
    ShellPrompt,   // Shell + recent output (just returned to prompt)
    ShellIdle,     // Shell + no recent output (at rest)
    PassiveViewer,
    Unknown,
}

impl SessionHeuristic {
    pub fn app_state(self) -> AppSessionState {
        match self {
            SessionHeuristic::Bell
            | SessionHeuristic::WaitingForConfirm
            | SessionHeuristic::WaitingForInput
            | SessionHeuristic::AgentDone
            | SessionHeuristic::ShellPrompt => AppSessionState::NeedsAttention,
            SessionHeuristic::AgentWorking | SessionHeuristic::ServerRunning => {
                AppSessionState::Active
            }
            SessionHeuristic::Muted
            | SessionHeuristic::ShellIdle
            | SessionHeuristic::PassiveViewer
            | SessionHeuristic::Unknown => AppSessionState::Idle,
        }
    }
}

fn is_recently_active(session: &SessionInfo) -> bool {
    session
        .last_activity
        .map(|t| t.elapsed().as_secs() < IDLE_SECS)
        .unwrap_or(false)
}

/// Classify a session. Precedence: muted > bell > capture hint > foreground.
/// For Agent and Shell, recent activity splits the heuristic further
/// (working vs done; prompt vs idle).
pub fn derive(session: &SessionInfo) -> SessionHeuristic {
    if session.muted {
        return SessionHeuristic::Muted;
    }
    if session.has_activity {
        return SessionHeuristic::Bell;
    }
    if let Some(hint) = session
        .pane_capture
        .as_deref()
        .and_then(capture::detect_capture_hint)
    {
        return match hint {
            CaptureHint::WaitingForConfirm => SessionHeuristic::WaitingForConfirm,
            CaptureHint::WaitingForInput => SessionHeuristic::WaitingForInput,
        };
    }
    let recent = is_recently_active(session);
    match session.foreground {
        ForegroundKind::Agent => {
            if recent {
                SessionHeuristic::AgentWorking
            } else {
                SessionHeuristic::AgentDone
            }
        }
        ForegroundKind::Runtime | ForegroundKind::InteractiveApp => SessionHeuristic::ServerRunning,
        ForegroundKind::Shell => {
            if recent {
                SessionHeuristic::ShellPrompt
            } else {
                SessionHeuristic::ShellIdle
            }
        }
        ForegroundKind::PassiveViewer => SessionHeuristic::PassiveViewer,
        ForegroundKind::Unknown => SessionHeuristic::Unknown,
    }
}

pub fn status_label(session: &SessionInfo) -> &'static str {
    match derive(session).app_state() {
        AppSessionState::Idle => "idle",
        AppSessionState::Active => "active",
        AppSessionState::NeedsAttention => "attention",
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Instant;
    use wsx_core::model::workspace::{ForegroundKind, SessionInfo};

    fn sess(
        foreground: ForegroundKind,
        has_activity: bool,
        recent: bool,
        capture: Option<&str>,
        muted: bool,
    ) -> SessionInfo {
        SessionInfo {
            name: "s".into(),
            display_name: "s".into(),
            has_activity,
            pane_capture: capture.map(|c| c.to_string()),
            last_activity: if recent { Some(Instant::now()) } else { None },
            foreground,
            is_running_wsx: false,
            muted,
        }
    }

    // ── A. per-branch derive ─────────────────────────────────────────────────

    #[test]
    fn given_muted_session_when_derived_then_muted() {
        let s = sess(ForegroundKind::Shell, false, false, None, true);
        assert_eq!(derive(&s), SessionHeuristic::Muted);
    }

    #[test]
    fn given_bell_session_when_derived_then_bell() {
        let s = sess(ForegroundKind::Shell, true, false, None, false);
        assert_eq!(derive(&s), SessionHeuristic::Bell);
    }

    #[test]
    fn given_confirm_capture_when_derived_then_waiting_for_confirm() {
        let s = sess(
            ForegroundKind::Unknown,
            false,
            false,
            Some("Continue? [y/n]"),
            false,
        );
        assert_eq!(derive(&s), SessionHeuristic::WaitingForConfirm);
    }

    #[test]
    fn given_input_wait_capture_when_derived_then_waiting_for_input() {
        let s = sess(
            ForegroundKind::Unknown,
            false,
            false,
            Some("waiting for user"),
            false,
        );
        assert_eq!(derive(&s), SessionHeuristic::WaitingForInput);
    }

    #[test]
    fn given_non_prompt_capture_when_derived_then_capture_does_not_match() {
        let s = sess(
            ForegroundKind::Unknown,
            false,
            false,
            Some("just some output"),
            false,
        );
        assert_eq!(derive(&s), SessionHeuristic::Unknown);
    }

    #[test]
    fn given_empty_capture_string_when_derived_then_falls_through_to_foreground() {
        // Some("") is a capture that matches no hint; must fall through.
        let s = sess(ForegroundKind::Shell, false, true, Some(""), false);
        assert_eq!(derive(&s), SessionHeuristic::ShellPrompt);
    }

    #[test]
    fn given_agent_foreground_and_recent_when_derived_then_agent_working() {
        let s = sess(ForegroundKind::Agent, false, true, None, false);
        assert_eq!(derive(&s), SessionHeuristic::AgentWorking);
    }

    #[test]
    fn given_agent_foreground_and_not_recent_when_derived_then_agent_done() {
        let s = sess(ForegroundKind::Agent, false, false, None, false);
        assert_eq!(derive(&s), SessionHeuristic::AgentDone);
    }

    #[test]
    fn given_runtime_foreground_when_derived_then_server_running() {
        let s = sess(ForegroundKind::Runtime, false, false, None, false);
        assert_eq!(derive(&s), SessionHeuristic::ServerRunning);
    }

    #[test]
    fn given_interactive_app_foreground_when_derived_then_server_running() {
        let s = sess(ForegroundKind::InteractiveApp, false, false, None, false);
        assert_eq!(derive(&s), SessionHeuristic::ServerRunning);
    }

    #[test]
    fn given_shell_foreground_and_recent_when_derived_then_shell_prompt() {
        let s = sess(ForegroundKind::Shell, false, true, None, false);
        assert_eq!(derive(&s), SessionHeuristic::ShellPrompt);
    }

    #[test]
    fn given_shell_foreground_and_not_recent_when_derived_then_shell_idle() {
        let s = sess(ForegroundKind::Shell, false, false, None, false);
        assert_eq!(derive(&s), SessionHeuristic::ShellIdle);
    }

    #[test]
    fn given_passive_viewer_foreground_when_derived_then_passive_viewer() {
        let s = sess(ForegroundKind::PassiveViewer, false, false, None, false);
        assert_eq!(derive(&s), SessionHeuristic::PassiveViewer);
    }

    #[test]
    fn given_unknown_foreground_when_derived_then_unknown() {
        let s = sess(ForegroundKind::Unknown, false, false, None, false);
        assert_eq!(derive(&s), SessionHeuristic::Unknown);
    }

    // ── B. precedence interactions ───────────────────────────────────────────

    #[test]
    fn given_muted_and_bell_and_agent_when_derived_then_muted_wins() {
        let s = sess(ForegroundKind::Agent, true, true, None, true);
        assert_eq!(derive(&s), SessionHeuristic::Muted);
    }

    #[test]
    fn given_muted_and_bell_and_capture_when_derived_then_muted_wins() {
        let s = sess(
            ForegroundKind::Agent,
            true,
            true,
            Some("Continue? [y/n]"),
            true,
        );
        assert_eq!(derive(&s), SessionHeuristic::Muted);
    }

    #[test]
    fn given_bell_and_confirm_capture_and_agent_when_derived_then_bell_wins() {
        let s = sess(
            ForegroundKind::Agent,
            true,
            false,
            Some("Continue? [y/n]"),
            false,
        );
        assert_eq!(derive(&s), SessionHeuristic::Bell);
    }

    #[test]
    fn given_confirm_capture_and_agent_recent_when_derived_then_capture_wins_over_agent() {
        let s = sess(
            ForegroundKind::Agent,
            false,
            true,
            Some("Continue? [y/n]"),
            false,
        );
        assert_eq!(derive(&s), SessionHeuristic::WaitingForConfirm);
    }

    #[test]
    fn given_confirm_capture_and_shell_recent_when_derived_then_capture_wins_over_shell() {
        let s = sess(
            ForegroundKind::Shell,
            false,
            true,
            Some("Continue? [y/n]"),
            false,
        );
        assert_eq!(derive(&s), SessionHeuristic::WaitingForConfirm);
    }

    #[test]
    fn given_input_wait_capture_and_runtime_when_derived_then_capture_wins_over_runtime() {
        let s = sess(
            ForegroundKind::Runtime,
            false,
            false,
            Some("waiting for user"),
            false,
        );
        assert_eq!(derive(&s), SessionHeuristic::WaitingForInput);
    }

    #[test]
    fn given_confirm_and_input_phrases_when_derived_then_confirm_wins() {
        let s = sess(
            ForegroundKind::Unknown,
            false,
            false,
            Some("waiting for user\nContinue? [y/n]"),
            false,
        );
        assert_eq!(derive(&s), SessionHeuristic::WaitingForConfirm);
    }

    #[test]
    fn given_non_prompt_capture_and_agent_recent_when_derived_then_agent_working() {
        let s = sess(
            ForegroundKind::Agent,
            false,
            true,
            Some("just some output"),
            false,
        );
        assert_eq!(derive(&s), SessionHeuristic::AgentWorking);
    }

    // ── C. activity gating ───────────────────────────────────────────────────

    #[test]
    fn given_runtime_recent_vs_none_when_derived_then_no_gating() {
        let recent = sess(ForegroundKind::Runtime, false, true, None, false);
        let none = sess(ForegroundKind::Runtime, false, false, None, false);
        assert_eq!(derive(&recent), SessionHeuristic::ServerRunning);
        assert_eq!(derive(&none), SessionHeuristic::ServerRunning);
    }

    #[test]
    fn given_interactive_app_recent_vs_none_when_derived_then_no_gating() {
        let recent = sess(ForegroundKind::InteractiveApp, false, true, None, false);
        let none = sess(ForegroundKind::InteractiveApp, false, false, None, false);
        assert_eq!(derive(&recent), SessionHeuristic::ServerRunning);
        assert_eq!(derive(&none), SessionHeuristic::ServerRunning);
    }

    // ── D. app_state projection ─────────────────────────────────────────────

    #[test]
    fn given_heuristic_muted_when_projected_then_idle() {
        assert_eq!(SessionHeuristic::Muted.app_state(), AppSessionState::Idle);
    }

    #[test]
    fn given_heuristic_bell_when_projected_then_needs_attention() {
        assert_eq!(
            SessionHeuristic::Bell.app_state(),
            AppSessionState::NeedsAttention
        );
    }

    #[test]
    fn given_heuristic_waiting_for_confirm_when_projected_then_needs_attention() {
        assert_eq!(
            SessionHeuristic::WaitingForConfirm.app_state(),
            AppSessionState::NeedsAttention
        );
    }

    #[test]
    fn given_heuristic_waiting_for_input_when_projected_then_needs_attention() {
        assert_eq!(
            SessionHeuristic::WaitingForInput.app_state(),
            AppSessionState::NeedsAttention
        );
    }

    #[test]
    fn given_heuristic_agent_working_when_projected_then_active() {
        assert_eq!(
            SessionHeuristic::AgentWorking.app_state(),
            AppSessionState::Active
        );
    }

    #[test]
    fn given_heuristic_agent_done_when_projected_then_needs_attention() {
        assert_eq!(
            SessionHeuristic::AgentDone.app_state(),
            AppSessionState::NeedsAttention
        );
    }

    #[test]
    fn given_heuristic_server_running_when_projected_then_active() {
        assert_eq!(
            SessionHeuristic::ServerRunning.app_state(),
            AppSessionState::Active
        );
    }

    #[test]
    fn given_heuristic_shell_prompt_when_projected_then_needs_attention() {
        assert_eq!(
            SessionHeuristic::ShellPrompt.app_state(),
            AppSessionState::NeedsAttention
        );
    }

    #[test]
    fn given_heuristic_shell_idle_when_projected_then_idle() {
        assert_eq!(
            SessionHeuristic::ShellIdle.app_state(),
            AppSessionState::Idle
        );
    }

    #[test]
    fn given_heuristic_passive_viewer_when_projected_then_idle() {
        assert_eq!(
            SessionHeuristic::PassiveViewer.app_state(),
            AppSessionState::Idle
        );
    }

    #[test]
    fn given_heuristic_unknown_when_projected_then_idle() {
        assert_eq!(SessionHeuristic::Unknown.app_state(), AppSessionState::Idle);
    }

    // ── E. status_label ─────────────────────────────────────────────────────

    #[test]
    fn given_idle_state_when_labelled_then_idle_string() {
        let s = sess(ForegroundKind::PassiveViewer, false, false, None, false);
        assert_eq!(status_label(&s), "idle");
    }

    #[test]
    fn given_muted_state_when_labelled_then_idle_string() {
        let s = sess(ForegroundKind::Agent, true, true, None, true);
        assert_eq!(status_label(&s), "idle");
    }

    #[test]
    fn given_active_state_when_labelled_then_active_string() {
        let s = sess(ForegroundKind::Runtime, false, false, None, false);
        assert_eq!(status_label(&s), "active");
    }

    #[test]
    fn given_needs_attention_state_when_labelled_then_attention_string() {
        let s = sess(ForegroundKind::Shell, true, false, None, false);
        assert_eq!(status_label(&s), "attention");
    }

    // ── F. realistic-workspace distribution (the v0.15.9-catcher) ───────────

    #[test]
    fn given_realistic_workspace_when_classified_then_each_session_state_matches_spec() {
        // Regression guard for the v0.15.9 "everything green" release. The
        // earlier draft asserted aggregate counts (4/4/2), but those counts
        // are reachable by several wrong mappings. Pin each session's
        // expected outcome individually, then keep the aggregate as a
        // belt-and-suspenders consistency check.
        let cases: Vec<(SessionInfo, SessionHeuristic, AppSessionState)> = vec![
            (
                sess(ForegroundKind::Agent, false, true, None, false),
                SessionHeuristic::AgentWorking,
                AppSessionState::Active,
            ),
            (
                sess(ForegroundKind::Agent, false, false, None, false),
                SessionHeuristic::AgentDone,
                AppSessionState::NeedsAttention,
            ),
            (
                sess(ForegroundKind::Runtime, false, false, None, false),
                SessionHeuristic::ServerRunning,
                AppSessionState::Active,
            ),
            (
                sess(ForegroundKind::Runtime, false, true, None, false),
                SessionHeuristic::ServerRunning,
                AppSessionState::Active,
            ),
            (
                sess(ForegroundKind::InteractiveApp, false, false, None, false),
                SessionHeuristic::ServerRunning,
                AppSessionState::Active,
            ),
            (
                sess(ForegroundKind::Shell, false, true, None, false),
                SessionHeuristic::ShellPrompt,
                AppSessionState::NeedsAttention,
            ),
            (
                sess(ForegroundKind::Shell, false, false, None, false),
                SessionHeuristic::ShellIdle,
                AppSessionState::Idle,
            ),
            (
                sess(ForegroundKind::Shell, true, false, None, false),
                SessionHeuristic::Bell,
                AppSessionState::NeedsAttention,
            ),
            (
                sess(
                    ForegroundKind::Agent,
                    false,
                    false,
                    Some("Continue? [y/n]"),
                    false,
                ),
                SessionHeuristic::WaitingForConfirm,
                AppSessionState::NeedsAttention,
            ),
            (
                sess(ForegroundKind::Agent, false, true, None, true),
                SessionHeuristic::Muted,
                AppSessionState::Idle,
            ),
        ];
        for (i, (s, expected_heur, expected_state)) in cases.iter().enumerate() {
            let actual_heur = derive(s);
            assert_eq!(
                actual_heur, *expected_heur,
                "session #{i}: heuristic mismatch (got {:?}, want {:?})",
                actual_heur, expected_heur
            );
            assert_eq!(
                actual_heur.app_state(),
                *expected_state,
                "session #{i}: app_state mismatch",
            );
        }
        let (mut active, mut attention, mut idle) = (0, 0, 0);
        for (_, _, st) in &cases {
            match st {
                AppSessionState::Active => active += 1,
                AppSessionState::NeedsAttention => attention += 1,
                AppSessionState::Idle => idle += 1,
            }
        }
        assert_eq!((active, attention, idle), (4, 4, 2));
    }

    #[test]
    fn given_workspace_with_no_recent_activity_then_no_agent_or_shell_is_active() {
        let sessions = vec![
            sess(ForegroundKind::Agent, false, false, None, false),
            sess(ForegroundKind::Shell, false, false, None, false),
            sess(ForegroundKind::Runtime, false, false, None, false),
            sess(ForegroundKind::InteractiveApp, false, false, None, false),
            sess(ForegroundKind::PassiveViewer, false, false, None, false),
            sess(ForegroundKind::Unknown, false, false, None, false),
        ];
        for s in &sessions {
            let state = derive(s).app_state();
            if matches!(s.foreground, ForegroundKind::Agent | ForegroundKind::Shell) {
                assert_ne!(
                    state,
                    AppSessionState::Active,
                    "Agent/Shell without recent activity must not be Active (foreground={:?})",
                    s.foreground
                );
            }
        }
    }

    #[test]
    fn given_workspace_with_no_recent_activity_when_classified_then_runtime_and_app_remain_active()
    {
        let runtime = sess(ForegroundKind::Runtime, false, false, None, false);
        let app = sess(ForegroundKind::InteractiveApp, false, false, None, false);
        assert_eq!(derive(&runtime).app_state(), AppSessionState::Active);
        assert_eq!(derive(&app).app_state(), AppSessionState::Active);
    }

    #[test]
    fn given_quiet_workspace_when_classified_then_attention_count_equals_quiet_agents() {
        let sessions = vec![
            sess(ForegroundKind::Agent, false, false, None, false),
            sess(ForegroundKind::Agent, false, false, None, false),
            sess(ForegroundKind::Shell, false, false, None, false),
            sess(ForegroundKind::Runtime, false, false, None, false),
            sess(ForegroundKind::InteractiveApp, false, false, None, false),
            sess(ForegroundKind::PassiveViewer, false, false, None, false),
            sess(ForegroundKind::Unknown, false, false, None, false),
        ];
        let agent_count = sessions
            .iter()
            .filter(|s| matches!(s.foreground, ForegroundKind::Agent))
            .count();
        let attention_count = sessions
            .iter()
            .filter(|s| derive(s).app_state() == AppSessionState::NeedsAttention)
            .count();
        assert_eq!(attention_count, agent_count);
    }
}