wsx 0.15.1

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
// Workspace operation functions — pure business logic, no App state.
// These take explicit arguments rather than &mut App so they can be
// tested and reasoned about independently of the TUI state machine.

use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use anyhow::{bail, Result};

use crate::{
    config::global::GlobalConfig,
    git::{info as git_info, worktree as git_worktree},
    hooks,
    model::workspace::{
        session_display_name_from_tmux, FetchFailReason, GitInfo, Project, ProjectConfig,
        SessionInfo, WorkspaceState, WorktreeInfo,
    },
    tmux::{monitor::SessionStatus, session},
};

// (pane_capture, running_app_suppressed, muted)
type PaneSnap = HashMap<String, (Option<String>, bool, bool)>;
// session_order preserves user-defined sort across refresh
type WorktreeSnap = HashMap<PathBuf, WorktreeSnapEntry>;

struct WorktreeSnapEntry {
    git_info: Option<GitInfo>,
    git_info_fetched_at: Option<Instant>,
    expanded: bool,
    panes: PaneSnap,
    session_order: Vec<String>,
    last_fetched: Option<Instant>,
    fetch_failed: bool,
    fetch_fail_count: u32,
    fetch_fail_reason: Option<FetchFailReason>,
}

pub const IDLE_SECS: u64 = 3;

// ── Refresh helpers ───────────────────────────────────────────────────────────

fn unix_ts_to_instant(unix_ts: u64) -> Option<Instant> {
    let now_unix = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    let secs_ago = now_unix.saturating_sub(unix_ts);
    Instant::now().checked_sub(Duration::from_secs(secs_ago))
}

/// Rebuild all worktrees + sessions for every project from live data.
/// Calls `list_worktrees` per project synchronously — use for user-triggered refreshes.
pub fn refresh_workspace(
    workspace: &mut WorkspaceState,
    config: &GlobalConfig,
    sessions_with_paths: &[(String, PathBuf)],
    activity: &HashMap<String, SessionStatus>,
) {
    let worktrees: Vec<(PathBuf, Vec<git_worktree::WorktreeEntry>)> = workspace
        .projects
        .iter()
        .map(|p| {
            let entries = git_worktree::list_worktrees(&p.path).unwrap_or_default();
            (p.path.clone(), entries)
        })
        .collect();
    refresh_workspace_with_worktrees(workspace, config, sessions_with_paths, activity, worktrees);
}

/// Like `refresh_workspace` but with pre-computed worktree entries — avoids subprocess calls
/// on the caller's thread. Used by the periodic background refresh path.
pub fn refresh_workspace_with_worktrees(
    workspace: &mut WorkspaceState,
    config: &GlobalConfig,
    sessions_with_paths: &[(String, PathBuf)],
    activity: &HashMap<String, SessionStatus>,
    worktrees: Vec<(PathBuf, Vec<git_worktree::WorktreeEntry>)>,
) {
    // Pre-index sessions by worktree path for O(1) lookup per worktree
    let mut sessions_by_path: HashMap<&PathBuf, Vec<&str>> = HashMap::new();
    for (name, path) in sessions_with_paths {
        sessions_by_path.entry(path).or_default().push(name.as_str());
    }

    let aliases_by_path: Vec<(PathBuf, HashMap<String, String>)> = config
        .projects
        .iter()
        .map(|e| (e.path.clone(), e.aliases.clone()))
        .collect();

    let mut worktrees_map: HashMap<PathBuf, Vec<git_worktree::WorktreeEntry>> =
        worktrees.into_iter().collect();

    for i in 0..workspace.projects.len() {
        let path = workspace.projects[i].path.clone();
        let proj_name = workspace.projects[i].name.clone();
        let aliases = aliases_by_path
            .iter()
            .find(|(p, _)| p == &path)
            .map(|(_, a)| a.clone())
            .unwrap_or_default();

        let snapshot: WorktreeSnap = workspace.projects[i]
            .worktrees
            .iter()
            .map(|w| {
                let panes = w
                    .sessions
                    .iter()
                    .map(|s| {
                        (
                            s.name.clone(),
                            (s.pane_capture.clone(), s.running_app_suppressed, s.muted),
                        )
                    })
                    .collect();
                let order = w.sessions.iter().map(|s| s.name.clone()).collect();
                (
                    w.path.clone(),
                    WorktreeSnapEntry {
                        git_info: w.git_info.clone(),
                        git_info_fetched_at: w.git_info_fetched_at,
                        expanded: w.expanded,
                        panes,
                        session_order: order,
                        last_fetched: w.last_fetched,
                        fetch_failed: w.fetch_failed,
                        fetch_fail_count: w.fetch_fail_count,
                        fetch_fail_reason: w.fetch_fail_reason.clone(),
                    },
                )
            })
            .collect();

        let entries = worktrees_map.remove(&path).unwrap_or_default();
        let mut new_worktrees = Vec::new();
        for entry in entries
            .into_iter()
            .filter(|e| !config.is_worktree_excluded(&e.path))
        {
            let alias = aliases.get(&entry.branch).cloned();
            let wt_path = entry.path.clone();
            let prev = snapshot.get(&entry.path);

            let prev_order: &[String] = prev
                .map(|snap| snap.session_order.as_slice())
                .unwrap_or(&[]);
            // Index prev_order for O(1) sort-key lookup
            let order_index: HashMap<&str, usize> = prev_order
                .iter()
                .enumerate()
                .map(|(i, n)| (n.as_str(), i))
                .collect();

            let empty_names: Vec<&str> = Vec::new();
            let session_names = sessions_by_path.get(&wt_path).unwrap_or(&empty_names);
            let mut sessions: Vec<SessionInfo> = session_names
                .iter()
                .map(|&name| {
                    let display_name = session_display_name_from_tmux(
                        name,
                        &proj_name,
                        &wt_path,
                        &entry.branch,
                        alias.as_deref(),
                    );
                    let prev_pane = prev.and_then(|snap| snap.panes.get(name));
                    let (pane_capture, prev_suppressed, prev_muted) = prev_pane
                        .map(|(p, s, m)| (p.clone(), *s, *m))
                        .unwrap_or((None, false, false));
                    let status = activity.get(name);
                    // Prefer tmux-sourced muted/suppressed flags over snapshot so all instances agree.
                    // ^ @wsx-muted and @wsx-suppressed are written to tmux on toggle; snapshot is fallback.
                    let muted = status.map(|s| s.wsx_muted).unwrap_or(prev_muted);
                    let tmux_suppressed = status.map(|s| s.wsx_suppressed).unwrap_or(false);
                    // Muted sessions skip all activity tracking.
                    let (has_activity, has_running_app, last_activity, running_app_suppressed) =
                        if muted {
                            (false, false, None, false)
                        } else {
                            let has_activity = status.map(|s| s.has_bell).unwrap_or(false);
                            let has_running_app =
                                status.map(|s| s.has_running_app).unwrap_or(false);
                            let last_activity = status
                                .filter(|s| s.last_activity_ts > 0)
                                .and_then(|s| unix_ts_to_instant(s.last_activity_ts));
                            let currently_active = last_activity
                                .map(|t| t.elapsed().as_secs() < IDLE_SECS)
                                .unwrap_or(false);
                            // Reset suppressed when new activity arrives; prefer tmux value.
                            let running_app_suppressed = if currently_active {
                                false
                            } else {
                                tmux_suppressed || prev_suppressed
                            };
                            (
                                has_activity,
                                has_running_app,
                                last_activity,
                                running_app_suppressed,
                            )
                        };
                    SessionInfo {
                        name: name.to_string(),
                        display_name,
                        has_activity,
                        pane_capture,
                        last_activity,
                        has_running_app,
                        is_running_wsx: status.map(|s| s.is_running_wsx).unwrap_or(false),
                        running_app_suppressed,
                        muted,
                    }
                })
                .collect();
            sessions.sort_by_key(|s| *order_index.get(s.name.as_str()).unwrap_or(&usize::MAX));

            let (git_info, git_info_fetched_at, expanded, last_fetched, fetch_failed, fetch_fail_count, fetch_fail_reason) = prev
                .map(|snap| {
                    (
                        snap.git_info.clone(),
                        snap.git_info_fetched_at,
                        snap.expanded,
                        snap.last_fetched,
                        snap.fetch_failed,
                        snap.fetch_fail_count,
                        snap.fetch_fail_reason.clone(),
                    )
                })
                .unwrap_or((None, None, true, None, false, 0, None));

            new_worktrees.push(WorktreeInfo {
                name: entry.name,
                branch: entry.branch,
                path: entry.path,
                is_main: entry.is_main,
                alias,
                sessions,
                expanded,
                git_info,
                git_info_fetched_at,
                fetch_failed,
                fetch_fail_count,
                fetch_fail_reason,
                last_fetched,
            });
        }
        workspace.projects[i].worktrees = new_worktrees;
    }
}

/// Update session activity state from live tmux data. Returns true if any field changed.
pub fn update_activity(
    workspace: &mut WorkspaceState,
    activity: &HashMap<String, SessionStatus>,
) -> bool {
    let mut changed = false;
    for project in &mut workspace.projects {
        for wt in &mut project.worktrees {
            for sess in &mut wt.sessions {
                if sess.muted {
                    continue;
                }
                let old_bell = sess.has_activity;
                let old_running = sess.has_running_app;
                if let Some(status) = activity.get(&sess.name) {
                    sess.has_activity = status.has_bell;
                    sess.has_running_app = status.has_running_app;
                    sess.is_running_wsx = status.is_running_wsx;
                    sess.last_activity = Some(status.last_activity_ts)
                        .filter(|&ts| ts > 0)
                        .and_then(|ts| unix_ts_to_instant(ts));
                    let currently_active = sess
                        .last_activity
                        .map(|t| t.elapsed().as_secs() < IDLE_SECS)
                        .unwrap_or(false);
                    if currently_active {
                        sess.running_app_suppressed = false;
                    }
                } else {
                    sess.has_activity = false;
                    sess.has_running_app = false;
                    sess.is_running_wsx = false;
                }
                if sess.has_activity != old_bell || sess.has_running_app != old_running {
                    changed = true;
                }
            }
        }
    }
    changed
}

// ── Workspace loading ─────────────────────────────────────────────────────────

pub fn load_workspace(config: &GlobalConfig) -> WorkspaceState {
    if config.projects.is_empty() {
        return WorkspaceState::empty();
    }

    let projects = config
        .projects
        .iter()
        .filter_map(|entry| {
            let path = &entry.path;
            if !path.exists() || !path.join(".git").exists() {
                return None;
            }

            let default_branch = detect_default_branch(path);
            let proj_config = crate::config::project::load_project_config(path);
            let entries = git_worktree::list_worktrees(path).unwrap_or_default();
            let entries = entries
                .into_iter()
                .filter(|e| !config.is_worktree_excluded(&e.path))
                .collect();
            let worktrees = git_worktree::to_worktree_infos(entries, &entry.aliases);

            Some(Project {
                name: entry.name.clone(),
                path: path.clone(),
                default_branch,
                worktrees,
                config: Some(proj_config),
                expanded: true,
            })
        })
        .collect();

    WorkspaceState { projects }
}

pub fn expand_path(s: &str) -> PathBuf {
    if s.starts_with("~/") {
        if let Some(home) = dirs::home_dir() {
            return home.join(&s[2..]);
        }
    }
    PathBuf::from(s)
}

pub fn detect_default_branch(path: &std::path::Path) -> String {
    git_info::current_branch(path).unwrap_or_else(|| "main".into())
}

// ── Project registration ──────────────────────────────────────────────────────

/// Register a new project at `path`. Returns the constructed `Project` and
/// mutates `config` (caller must call `config.save()`).
pub fn register_project(path: PathBuf, config: &mut GlobalConfig) -> Result<Project> {
    if path.as_os_str().is_empty() {
        bail!("empty path");
    }
    if !path.exists() {
        bail!("path does not exist: {}", path.display());
    }
    if !path.join(".git").exists() {
        bail!("not a git repository: {}", path.display());
    }

    let name = path
        .file_name()
        .map(|n| n.to_string_lossy().to_string())
        .unwrap_or_else(|| "unknown".to_string());

    let default_branch = detect_default_branch(&path);
    let proj_config = crate::config::project::load_project_config(&path);
    let entries = git_worktree::list_worktrees(&path).unwrap_or_default();
    let aliases = config
        .projects
        .iter()
        .find(|e| e.path == path)
        .map(|e| e.aliases.clone())
        .unwrap_or_default();
    let worktrees = git_worktree::to_worktree_infos(entries, &aliases);

    config.add_project(name.clone(), path.clone());

    Ok(Project {
        name,
        path,
        default_branch,
        worktrees,
        config: Some(proj_config),
        expanded: true,
    })
}

/// Remove a project by path from config. Caller must call `config.save()`.
pub fn unregister_project(path: &PathBuf, config: &mut GlobalConfig) {
    config.remove_project(path);
}

// ── Worktree operations ───────────────────────────────────────────────────────

/// Create a new git worktree under `repo_path` for `branch`.
/// Runs hooks (env copy, post_create) and returns the new worktree path.
/// Returns a warning string if a hook failed (non-fatal).
pub fn create_worktree(
    repo_path: &PathBuf,
    default_branch: &str,
    proj_config: &ProjectConfig,
    branch: &str,
) -> Result<(PathBuf, Option<String>)> {
    let wt_path = git_worktree::create_worktree(repo_path, branch, default_branch)?;

    let mut warning: Option<String> = None;

    if let Err(e) = hooks::copy_env_files(repo_path, &wt_path, proj_config) {
        warning = Some(format!("Warning: .env copy: {}", e));
    }
    if let Some(ref cmd) = proj_config.post_create {
        if let Err(e) = hooks::run_post_create(&wt_path, cmd) {
            warning = Some(format!("Warning: postCreate: {}", e));
        }
    }

    Ok((wt_path, warning))
}

/// Remove a git worktree and kill any associated tmux sessions.
pub fn delete_worktree(
    repo_path: &PathBuf,
    wt_path: &PathBuf,
    branch: &str,
    session_names: &[String],
) -> Result<()> {
    git_worktree::remove_worktree(repo_path, wt_path, branch)?;
    for sess in session_names {
        let _ = session::kill_session(sess);
    }
    Ok(())
}

// ── Session operations ────────────────────────────────────────────────────────

/// Create a named tmux session at `wt_path` and optionally send an initial command.
/// Returns (tmux_name, display_name). Tmux name is prefixed with `{proj_name}-{wt_slug}-`;
/// display_name is the user-visible part (what the user typed).
pub fn create_session(
    proj_name: &str,
    wt_slug: &str,
    wt_path: &PathBuf,
    session_name: Option<String>,
    command: Option<String>,
) -> Result<(String, String)> {
    // display name priority: explicit > command first word > proj_name
    let base_display = match &session_name {
        Some(n) if !n.is_empty() => n.clone(),
        _ => match &command {
            Some(cmd) => cmd
                .split_whitespace()
                .next()
                .unwrap_or(proj_name)
                .to_string(),
            None => proj_name.to_string(),
        },
    };
    let base_tmux = format!("{}-{}-{}", proj_name, wt_slug, base_display);
    let tmux_name = session::unique_session_name(&base_tmux);
    // strip "{proj_name}-{wt_slug}-" prefix to get display name
    let prefix_len = proj_name.len() + 1 + wt_slug.len() + 1;
    let display_name = tmux_name[prefix_len..].to_string();
    session::create_session(&tmux_name, wt_path)?;
    if let Some(cmd) = command {
        session::send_keys(&tmux_name, &cmd)?;
    }
    Ok((tmux_name, display_name))
}

/// Rename a tmux session from `old_name` to `new_name`.
pub fn rename_session(old_name: &str, new_name: &str) -> Result<()> {
    session::rename_session(old_name, new_name)
}

/// Recreate tmux sessions after a server restart (reboot/crash).
///
/// ! Only restores when the tmux server PID has changed. If the same server is
/// ! still running, missing sessions were intentionally killed — skip restore.
///
/// Uses the session snapshot (written periodically to Application Support) as the
/// source of truth — more recent than the cache, which may lag behind live state.
///
/// Returns the number of sessions recreated.
pub fn restore_cached_sessions(workspace: &WorkspaceState, cached_pid: Option<u32>) -> usize {
    let current_pid = session::server_pid();
    if cached_pid.is_some() && cached_pid == current_pid {
        return 0;
    }

    let live: HashSet<String> = session::list_sessions_with_paths()
        .into_iter()
        .map(|(name, _)| name)
        .collect();

    // Prefer snapshot (written on every cache flush) over workspace sessions,
    // which come from the cache and may not reflect the last live state.
    let snapshot = crate::cache::load_session_snapshot();
    let source = if !snapshot.is_empty() {
        snapshot
    } else {
        crate::cache::collect_session_names(workspace)
    };

    let mut restored = 0usize;
    for (path_str, names) in &source {
        let path = std::path::Path::new(path_str.as_str());
        for name in names {
            if !live.contains(name) {
                if session::create_session(name, path).is_ok() {
                    restored += 1;
                }
            }
        }
    }
    restored
}

// ── Alias operations ──────────────────────────────────────────────────────────

/// Persist an alias for a branch in the global config. Caller must call `config.save()`.
pub fn set_alias(config: &mut GlobalConfig, proj_path: &PathBuf, branch: &str, alias: &str) {
    config.set_alias(proj_path, branch, alias);
}