wsx 0.15.11

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
// Startup cache — persists last known sessions + expand state.
// Loaded before first refresh_all() so the tree is populated immediately.

use std::collections::{HashMap, HashSet};
use std::path::PathBuf;

use crate::model::workspace::{
    session_display_name_from_tmux, FlatEntry, ForegroundKind, SessionInfo, WorkspaceState,
};
use serde::{Deserialize, Serialize};

/// Stable cursor identity — survives session appear/disappear and expand-state changes.
#[derive(Serialize, Deserialize, Clone)]
pub enum CursorIdentity {
    Project {
        path: String,
    },
    Worktree {
        path: String,
    },
    Session {
        worktree_path: String,
        session_name: String,
    },
}

#[derive(Serialize, Deserialize, Default)]
pub struct WorkspaceCache {
    /// worktree path → session names
    pub sessions: HashMap<String, Vec<String>>,
    /// worktree path → expanded
    pub worktree_expanded: HashMap<String, bool>,
    /// project path → expanded
    pub project_expanded: HashMap<String, bool>,
    /// last cursor position in the flat tree (raw fallback)
    pub tree_selected: usize,
    /// stable cursor identity (preferred over raw index)
    #[serde(default)]
    pub cursor_identity: Option<CursorIdentity>,
    /// session names the user has muted (no activity updates, shown as ⊘)
    #[serde(default)]
    pub muted_sessions: HashSet<String>,
    /// global send-command history (Shift+S), newest last, capped at 50
    #[serde(default)]
    pub command_history: Vec<String>,
    /// last active tab name (None = default tab)
    #[serde(default)]
    pub active_tab: Option<String>,
    /// tmux server PID at last save — used to detect server restart on next launch
    #[serde(default)]
    pub tmux_server_pid: Option<u32>,
}

impl WorkspaceCache {
    pub fn load() -> Self {
        let Ok(content) = std::fs::read_to_string(cache_path()) else {
            return Self::default();
        };
        toml::from_str(&content).unwrap_or_default()
    }

    pub fn save(&self, sync: bool) -> anyhow::Result<()> {
        let path = cache_path();
        if let Some(dir) = path.parent() {
            std::fs::create_dir_all(dir)?;
        }
        let s = toml::to_string(self)?;
        // Atomic write: temp file + rename so a crash mid-write leaves the original intact.
        let tmp = path.with_extension("toml.tmp");
        let mut f = std::fs::File::create(&tmp)?;
        std::io::Write::write_all(&mut f, s.as_bytes())?;
        if sync {
            f.sync_all()?;
        }
        drop(f);
        std::fs::rename(&tmp, &path)?;
        Ok(())
    }
}

/// Atomic write: write to a `.toml.tmp` sibling then rename over the target.
fn write_atomic(path: &std::path::Path, content: &[u8]) -> std::io::Result<()> {
    let tmp = path.with_extension("toml.tmp");
    std::fs::write(&tmp, content)?;
    std::fs::rename(&tmp, path)
}

fn cache_path() -> PathBuf {
    dirs::cache_dir()
        .unwrap_or_else(|| PathBuf::from("/tmp"))
        .join("wsx")
        .join("workspace.toml")
}

fn session_snapshot_path() -> Option<PathBuf> {
    let base = dirs::config_dir().or_else(|| dirs::home_dir().map(|h| h.join(".config")))?;
    Some(base.join("wsx").join("sessions.toml"))
}

/// Collect session names per worktree path — shared by snapshot write and restore fallback.
pub fn collect_session_names(workspace: &WorkspaceState) -> HashMap<String, Vec<String>> {
    let mut map = HashMap::new();
    for project in &workspace.projects {
        for wt in &project.worktrees {
            let names = wt.session_names();
            if !names.is_empty() {
                map.insert(wt.path.to_string_lossy().into_owned(), names);
            }
        }
    }
    map
}

/// Persist session names to Application Support — survives tmux crashes because
/// it's outside the cache and written whenever sessions change.
pub fn save_session_snapshot(workspace: &WorkspaceState) {
    let Some(path) = session_snapshot_path() else {
        return;
    };
    save_snapshot_to(workspace, &path);
}

pub(crate) fn save_snapshot_to(workspace: &WorkspaceState, path: &std::path::Path) {
    let map = collect_session_names(workspace);
    let Ok(s) = toml::to_string(&map) else { return };
    if let Some(dir) = path.parent() {
        if std::fs::create_dir_all(dir).is_err() {
            return;
        }
    }
    let _ = write_atomic(path, s.as_bytes());
}

/// Load the session snapshot written by `save_session_snapshot`.
pub fn load_session_snapshot() -> HashMap<String, Vec<String>> {
    let Some(path) = session_snapshot_path() else {
        return HashMap::new();
    };
    load_snapshot_from(&path)
}

pub(crate) fn load_snapshot_from(path: &std::path::Path) -> HashMap<String, Vec<String>> {
    let Ok(content) = std::fs::read_to_string(path) else {
        return HashMap::new();
    };
    toml::from_str(&content).unwrap_or_default()
}

/// Return type for `apply_cache`. Last two fields are for one-time tmux flag migration.
#[allow(clippy::type_complexity)]
type CacheResult = (
    usize,
    Option<CursorIdentity>,
    Vec<String>,
    Option<String>,
    Option<u32>,
    HashSet<String>,
);

/// Pre-populate workspace with cached state before first live sync.
pub fn apply_cache(workspace: &mut WorkspaceState) -> CacheResult {
    let cache = WorkspaceCache::load();
    for project in &mut workspace.projects {
        let proj_key = project.path.to_string_lossy().to_string();
        let cached = cache.project_expanded.get(&proj_key).copied();
        if let Some(expanded) = cached {
            project.expanded = expanded;
        }
        for wt in &mut project.worktrees {
            let key = wt.path.to_string_lossy().to_string();
            if let Some(&expanded) = cache.worktree_expanded.get(&key) {
                wt.expanded = expanded;
            }
            if let Some(names) = cache.sessions.get(&key) {
                wt.sessions = names
                    .iter()
                    .map(|name| {
                        let display_name = session_display_name_from_tmux(
                            name,
                            &project.name,
                            &wt.path,
                            &wt.branch,
                            wt.alias.as_deref(),
                        );
                        SessionInfo {
                            name: name.clone(),
                            display_name,
                            has_activity: false,
                            pane_capture: None,
                            last_activity: None,
                            foreground: ForegroundKind::Unknown,
                            is_running_wsx: false,
                            muted: cache.muted_sessions.contains(name),
                        }
                    })
                    .collect();
            }
        }
    }
    (
        cache.tree_selected,
        cache.cursor_identity,
        cache.command_history,
        cache.active_tab,
        cache.tmux_server_pid,
        cache.muted_sessions,
    )
}

/// Resolve a saved CursorIdentity back to a flat-tree index.
pub fn find_cursor_index(
    workspace: &WorkspaceState,
    flat: &[FlatEntry],
    id: &CursorIdentity,
) -> Option<usize> {
    match id {
        CursorIdentity::Project { path } => flat.iter().position(|e| {
            if let FlatEntry::Project { idx } = e {
                workspace.projects[*idx].path.to_string_lossy() == path.as_str()
            } else {
                false
            }
        }),
        CursorIdentity::Worktree { path } => flat.iter().position(|e| {
            if let FlatEntry::Worktree {
                project_idx: pi,
                worktree_idx: wi,
            } = e
            {
                workspace.projects[*pi].worktrees[*wi]
                    .path
                    .to_string_lossy()
                    == path.as_str()
            } else {
                false
            }
        }),
        CursorIdentity::Session {
            worktree_path,
            session_name,
        } => flat.iter().position(|e| {
            if let FlatEntry::Session {
                project_idx: pi,
                worktree_idx: wi,
                session_idx: si,
            } = e
            {
                let wt = &workspace.projects[*pi].worktrees[*wi];
                wt.path.to_string_lossy() == worktree_path.as_str()
                    && wt.sessions[*si].name == *session_name
            } else {
                false
            }
        }),
    }
}

/// Persist session names, expand states, cursor position, active_tab, and command history.
/// Returns an error string if the save fails (caller should surface it in TUI).
pub fn save_cache(
    workspace: &WorkspaceState,
    tree_selected: usize,
    flat: &[FlatEntry],
    command_history: &[String],
    active_tab: Option<&str>,
    sync: bool,
) -> Option<String> {
    let mut cache = WorkspaceCache::default();
    cache.tree_selected = tree_selected;
    cache.cursor_identity = resolve_cursor_identity(workspace, flat, tree_selected);
    cache.active_tab = active_tab.map(|s| s.to_string());
    for project in &workspace.projects {
        let proj_key = project.path.to_string_lossy().to_string();
        cache.project_expanded.insert(proj_key, project.expanded);
        for wt in &project.worktrees {
            let key = wt.path.to_string_lossy().to_string();
            cache.sessions.insert(
                key.clone(),
                wt.sessions.iter().map(|s| s.name.clone()).collect(),
            );
            cache.worktree_expanded.insert(key, wt.expanded);
            // ^ muted_sessions intentionally omitted: stored as @wsx-muted tmux user option
            // so all instances share it without cache coordination.
        }
    }
    cache.command_history = command_history.to_vec();
    cache.tmux_server_pid = crate::tmux::session::server_pid();
    cache
        .save(sync)
        .err()
        .map(|e| format!("cache save failed: {e}"))
}

/// One-time migration: write cached muted session names as tmux user options so they
/// survive future cache writes and are visible to all instances. Idempotent and non-fatal.
pub fn migrate_flags_to_tmux(muted: &HashSet<String>) {
    use crate::tmux::session::{set_session_opt, OPT_MUTED};
    for name in muted {
        set_session_opt(name, OPT_MUTED, "1");
    }
}

fn resolve_cursor_identity(
    workspace: &WorkspaceState,
    flat: &[FlatEntry],
    idx: usize,
) -> Option<CursorIdentity> {
    match flat.get(idx)? {
        FlatEntry::Project { idx: pi } => Some(CursorIdentity::Project {
            path: workspace.projects[*pi].path.to_string_lossy().to_string(),
        }),
        FlatEntry::Worktree {
            project_idx: pi,
            worktree_idx: wi,
        } => {
            let wt = &workspace.projects[*pi].worktrees[*wi];
            Some(CursorIdentity::Worktree {
                path: wt.path.to_string_lossy().to_string(),
            })
        }
        FlatEntry::Session {
            project_idx: pi,
            worktree_idx: wi,
            session_idx: si,
        } => {
            let wt = &workspace.projects[*pi].worktrees[*wi];
            Some(CursorIdentity::Session {
                worktree_path: wt.path.to_string_lossy().to_string(),
                session_name: wt.sessions[*si].name.clone(),
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::workspace::{Project, SessionInfo, WorktreeInfo};

    fn make_session(name: &str) -> SessionInfo {
        SessionInfo {
            name: name.into(),
            display_name: name.into(),
            has_activity: false,
            pane_capture: None,
            last_activity: None,
            foreground: ForegroundKind::Unknown,
            is_running_wsx: false,
            muted: false,
        }
    }

    fn make_worktree(path: &str, sessions: &[&str]) -> WorktreeInfo {
        WorktreeInfo {
            name: "main".into(),
            branch: "main".into(),
            path: std::path::PathBuf::from(path),
            is_main: true,
            alias: None,
            sessions: sessions.iter().map(|s| make_session(s)).collect(),
            expanded: true,
            git_info: None,
            fetch_failed: false,
            fetch_fail_count: 0,
            fetch_fail_reason: None,
            last_fetched: None,
            git_info_fetched_at: None,
        }
    }

    fn make_workspace(worktrees: &[(&str, &[&str])]) -> WorkspaceState {
        WorkspaceState {
            projects: vec![Project {
                name: "test".into(),
                path: std::path::PathBuf::from("/tmp/test"),
                default_branch: "main".into(),
                worktrees: worktrees
                    .iter()
                    .map(|(path, sessions)| make_worktree(path, sessions))
                    .collect(),
                config: None,
                expanded: true,
                missing: false,
            }],
        }
    }

    // ── collect_session_names (regression + new) ─────────────────────────────

    #[test]
    fn collect_session_names_maps_by_path() {
        let ws = make_workspace(&[("/tmp/proj", &["proj-main-claude", "proj-main-shell"])]);
        let map = collect_session_names(&ws);
        assert_eq!(
            map["/tmp/proj"],
            vec!["proj-main-claude", "proj-main-shell"]
        );
    }

    #[test]
    fn collect_session_names_skips_empty_worktrees() {
        let ws = make_workspace(&[("/tmp/proj-a", &["proj-a-claude"]), ("/tmp/proj-b", &[])]);
        let map = collect_session_names(&ws);
        assert!(map.contains_key("/tmp/proj-a"));
        assert!(!map.contains_key("/tmp/proj-b"));
    }

    #[test]
    fn collect_session_names_empty_workspace_returns_empty_map() {
        let ws = make_workspace(&[]);
        assert!(collect_session_names(&ws).is_empty());
    }

    // ── snapshot roundtrip ───────────────────────────────────────────────────

    #[test]
    fn snapshot_roundtrip_via_path() {
        let dir = std::env::temp_dir().join("wsx_test_snapshot_roundtrip");
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("sessions.toml");

        let ws = make_workspace(&[
            ("/tmp/proj-a", &["proj-a-claude"]),
            ("/tmp/proj-b", &["proj-b-shell", "proj-b-build"]),
        ]);

        save_snapshot_to(&ws, &path);
        let loaded = load_snapshot_from(&path);

        assert_eq!(loaded["/tmp/proj-a"], vec!["proj-a-claude"]);
        assert_eq!(loaded["/tmp/proj-b"], vec!["proj-b-shell", "proj-b-build"]);

        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn snapshot_load_missing_file_returns_empty() {
        let path = std::path::Path::new("/tmp/wsx_nonexistent_snapshot.toml");
        assert!(load_snapshot_from(path).is_empty());
    }

    #[test]
    fn snapshot_empty_workspace_writes_and_loads_empty() {
        let dir = std::env::temp_dir().join("wsx_test_snapshot_empty");
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("sessions.toml");

        let ws = make_workspace(&[]);
        save_snapshot_to(&ws, &path);
        assert!(load_snapshot_from(&path).is_empty());

        std::fs::remove_dir_all(&dir).ok();
    }
}