arta-tui 0.4.0

Terminal workspace manager for concurrent AI coding agent sessions (tmux/zellij)
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
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Project {
    pub name: String,
    pub path: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub open_command: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub agent_command: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Thread {
    pub id: String,
    pub project: String,
    pub created: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(default)]
    pub name_locked: bool,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Workspace {
    pub projects: Vec<Project>,
    #[serde(alias = "sessions", default)]
    pub threads: Vec<Thread>,
    #[serde(
        alias = "active_session",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub active_thread: Option<String>,
    #[serde(alias = "next_session_id", default)]
    next_thread_id: u64,
    #[serde(skip)]
    file_path: PathBuf,
}

pub fn sanitize_name(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '_' || c == '-' {
                c
            } else {
                '-'
            }
        })
        .collect()
}

impl Workspace {
    pub fn new(file_path: PathBuf) -> Self {
        Workspace {
            projects: Vec::new(),
            threads: Vec::new(),
            active_thread: None,
            next_thread_id: 0,
            file_path,
        }
    }

    pub fn load(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        let data = match fs::read_to_string(&self.file_path) {
            Ok(data) => data,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
            Err(e) => return Err(e.into()),
        };
        let loaded: Workspace = serde_yaml::from_str(&data)?;
        self.projects = loaded.projects;
        self.threads = loaded.threads;
        self.active_thread = loaded.active_thread;
        self.next_thread_id = loaded.next_thread_id;
        Ok(())
    }

    pub fn save(&self) -> Result<(), Box<dyn std::error::Error>> {
        if let Some(dir) = self.file_path.parent() {
            fs::create_dir_all(dir)?;
        }
        let data = serde_yaml::to_string(self)?;
        fs::write(&self.file_path, data)?;
        Ok(())
    }

    pub fn add_project(&mut self, name: &str, path: &str, open_command: Option<&str>) {
        if self.projects.iter().any(|p| p.name == name) {
            return;
        }
        self.projects.push(Project {
            name: name.to_string(),
            path: path.to_string(),
            open_command: open_command.map(|s| s.to_string()),
            agent_command: None,
        });
        let _ = self.save();
    }

    pub fn remove_project(&mut self, name: &str) {
        self.threads.retain(|t| t.project != name);
        self.projects.retain(|p| p.name != name);
        let _ = self.save();
    }

    pub fn rename_project(&mut self, old_name: &str, new_name: &str) {
        for p in &mut self.projects {
            if p.name == old_name {
                p.name = new_name.to_string();
            }
        }
        for t in &mut self.threads {
            if t.project == old_name {
                t.project = new_name.to_string();
            }
        }
        let _ = self.save();
    }

    pub fn swap_projects(&mut self, i: usize, j: usize) {
        if i < self.projects.len() && j < self.projects.len() {
            self.projects.swap(i, j);
            let _ = self.save();
        }
    }

    pub fn create_thread(&mut self, project_name: &str) -> Option<&Thread> {
        self.next_thread_id += 1;
        let thread = Thread {
            id: format!("{}-{}", project_name, self.next_thread_id),
            project: project_name.to_string(),
            created: chrono_now(),
            name: None,
            name_locked: false,
        };
        self.threads.push(thread);
        let _ = self.save();
        self.threads.last()
    }

    pub fn remove_thread(&mut self, id: &str) {
        self.threads.retain(|t| t.id != id);
        let _ = self.save();
    }

    /// Set the user-visible display name for a thread.
    /// `lock` should be true when the user manually renames; subsequent auto-rename
    /// updates from the agent's OSC title will then be ignored.
    pub fn set_thread_display_name(&mut self, id: &str, name: &str, lock: bool) {
        let mut changed = false;
        for t in &mut self.threads {
            if t.id == id {
                let new_name = if name.is_empty() {
                    None
                } else {
                    Some(name.to_string())
                };
                if t.name != new_name || (lock && !t.name_locked) {
                    t.name = new_name;
                    if lock {
                        t.name_locked = true;
                    }
                    changed = true;
                }
                break;
            }
        }
        if changed {
            let _ = self.save();
        }
    }

    /// Returns the display name for a thread: the override `name` if set,
    /// otherwise the stable `id`. Returns `id` itself if the thread is unknown.
    pub fn display_name_for<'a>(&'a self, id: &'a str) -> &'a str {
        for t in &self.threads {
            if t.id == id {
                return t.name.as_deref().unwrap_or(&t.id);
            }
        }
        id
    }

    /// Returns the raw `name` override for a thread (without falling back to `id`).
    /// `None` means no name has ever been set.
    pub fn get_thread_name(&self, id: &str) -> Option<&str> {
        self.threads
            .iter()
            .find(|t| t.id == id)
            .and_then(|t| t.name.as_deref())
    }

    pub fn is_thread_name_locked(&self, id: &str) -> bool {
        self.threads
            .iter()
            .find(|t| t.id == id)
            .map(|t| t.name_locked)
            .unwrap_or(false)
    }

    pub fn swap_thread_in_project(&mut self, id: &str, direction: i32) {
        let project = match self.threads.iter().find(|t| t.id == id) {
            Some(t) => t.project.clone(),
            None => return,
        };

        let indices: Vec<usize> = self
            .threads
            .iter()
            .enumerate()
            .filter(|(_, t)| t.project == project)
            .map(|(i, _)| i)
            .collect();

        for (pos, &idx) in indices.iter().enumerate() {
            if self.threads[idx].id == id {
                let target_pos = pos as i32 + direction;
                if target_pos >= 0 && (target_pos as usize) < indices.len() {
                    let target_idx = indices[target_pos as usize];
                    self.threads.swap(idx, target_idx);
                    let _ = self.save();
                }
                return;
            }
        }
    }

    pub fn threads_for_project(&self, name: &str) -> Vec<&Thread> {
        self.threads.iter().filter(|t| t.project == name).collect()
    }

    pub fn get_project_path(&self, name: &str) -> Option<&str> {
        self.projects
            .iter()
            .find(|p| p.name == name)
            .map(|p| p.path.as_str())
    }

    pub fn get_project_open_command(&self, name: &str) -> Option<&str> {
        self.projects
            .iter()
            .find(|p| p.name == name)
            .and_then(|p| p.open_command.as_deref())
    }

    pub fn set_project_path(&mut self, name: &str, path: &str) {
        if let Some(p) = self.projects.iter_mut().find(|p| p.name == name) {
            p.path = path.to_string();
            let _ = self.save();
        }
    }

    pub fn set_active_thread(&mut self, id: Option<&str>) {
        let new = id.map(|s| s.to_string());
        if self.active_thread == new {
            return;
        }
        self.active_thread = new;
        let _ = self.save();
    }

    pub fn set_project_open_command(&mut self, name: &str, cmd: &str) {
        if let Some(p) = self.projects.iter_mut().find(|p| p.name == name) {
            p.open_command = if cmd.is_empty() {
                None
            } else {
                Some(cmd.to_string())
            };
            let _ = self.save();
        }
    }

    pub fn get_project_agent_command(&self, name: &str) -> Option<&str> {
        self.projects
            .iter()
            .find(|p| p.name == name)
            .and_then(|p| p.agent_command.as_deref())
    }

    pub fn set_project_agent_command(&mut self, name: &str, cmd: &str) {
        if let Some(p) = self.projects.iter_mut().find(|p| p.name == name) {
            p.agent_command = if cmd.is_empty() {
                None
            } else {
                Some(cmd.to_string())
            };
            let _ = self.save();
        }
    }
}

/// Migrate workspace from the legacy JSON location to the new YAML path.
///
/// If `new_path` already exists, does nothing.
/// Otherwise checks `~/.config/arta/data/workspace.json` and converts it.
pub fn migrate_workspace_if_needed(new_path: &std::path::Path) -> bool {
    if new_path.exists() {
        return false;
    }
    let legacy = dirs::config_dir()
        .map(|d| d.join("arta").join("data").join("workspace.json"));
    let Some(legacy_path) = legacy else {
        return false;
    };
    if !legacy_path.exists() {
        return false;
    }
    let Ok(data) = fs::read_to_string(&legacy_path) else {
        return false;
    };
    let Ok(loaded) = serde_json::from_str::<Workspace>(&data) else {
        return false;
    };
    if let Some(parent) = new_path.parent() {
        let _ = fs::create_dir_all(parent);
    }
    let Ok(yaml) = serde_yaml::to_string(&loaded) else {
        return false;
    };
    fs::write(new_path, yaml).is_ok()
}

fn chrono_now() -> String {
    use std::time::SystemTime;
    let duration = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default();
    // Simple ISO-8601 approximation without chrono dependency
    let secs = duration.as_secs();
    let days = secs / 86400;
    let time_of_day = secs % 86400;
    let hours = time_of_day / 3600;
    let minutes = (time_of_day % 3600) / 60;
    let seconds = time_of_day % 60;

    // Days since epoch to y/m/d (simplified)
    let mut y = 1970i64;
    let mut remaining = days as i64;
    loop {
        let days_in_year = if y % 4 == 0 && (y % 100 != 0 || y % 400 == 0) {
            366
        } else {
            365
        };
        if remaining < days_in_year {
            break;
        }
        remaining -= days_in_year;
        y += 1;
    }
    let leap = y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
    let month_days = [
        31,
        if leap { 29 } else { 28 },
        31,
        30,
        31,
        30,
        31,
        31,
        30,
        31,
        30,
        31,
    ];
    let mut m = 0;
    for (i, &md) in month_days.iter().enumerate() {
        if remaining < md as i64 {
            m = i + 1;
            break;
        }
        remaining -= md as i64;
    }
    let d = remaining + 1;

    format!(
        "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
        y, m, d, hours, minutes, seconds
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    use std::sync::atomic::{AtomicU32, Ordering};
    static COUNTER: AtomicU32 = AtomicU32::new(0);

    fn temp_workspace() -> Workspace {
        let id = COUNTER.fetch_add(1, Ordering::SeqCst);
        let dir = std::env::temp_dir().join(format!("arta-test-{}-{}", std::process::id(), id));
        let _ = fs::create_dir_all(&dir);
        Workspace::new(dir.join("workspace.yaml"))
    }

    #[test]
    fn test_sanitize_name() {
        assert_eq!(sanitize_name("hello world"), "hello-world");
        assert_eq!(sanitize_name("my-project_1"), "my-project_1");
        assert_eq!(sanitize_name("a/b.c"), "a-b-c");
    }

    #[test]
    fn test_add_remove_project() {
        let mut ws = temp_workspace();
        ws.add_project("test", "/tmp/test", None);
        assert_eq!(ws.projects.len(), 1);
        assert_eq!(ws.projects[0].name, "test");

        // Duplicate should be ignored
        ws.add_project("test", "/tmp/test2", None);
        assert_eq!(ws.projects.len(), 1);

        ws.remove_project("test");
        assert_eq!(ws.projects.len(), 0);
    }

    #[test]
    fn test_create_thread() {
        let mut ws = temp_workspace();
        ws.add_project("proj", "/tmp/proj", None);
        ws.create_thread("proj");
        ws.create_thread("proj");
        assert_eq!(ws.threads.len(), 2);
        assert_eq!(ws.threads[0].id, "proj-1");
        assert_eq!(ws.threads[1].id, "proj-2");
        assert!(ws.threads[0].name.is_none());
        assert!(!ws.threads[0].name_locked);
    }

    #[test]
    fn test_rename_project() {
        let mut ws = temp_workspace();
        ws.add_project("old", "/tmp/old", None);
        ws.create_thread("old");
        ws.rename_project("old", "new");
        assert_eq!(ws.projects[0].name, "new");
        assert_eq!(ws.threads[0].project, "new");
    }

    #[test]
    fn test_yaml_roundtrip() {
        let mut ws = temp_workspace();
        ws.add_project("proj", "/tmp/proj", None);
        ws.create_thread("proj");
        ws.set_thread_display_name("proj-1", "My work", true);
        ws.save().unwrap();

        let mut ws2 = Workspace::new(ws.file_path.clone());
        ws2.load().unwrap();
        assert_eq!(ws2.projects.len(), 1);
        assert_eq!(ws2.threads.len(), 1);
        assert_eq!(ws2.projects[0].name, "proj");
        assert_eq!(ws2.threads[0].id, "proj-1");
        assert_eq!(ws2.threads[0].name.as_deref(), Some("My work"));
        assert!(ws2.threads[0].name_locked);

        let _ = fs::remove_dir_all(ws.file_path.parent().unwrap());
    }

    #[test]
    fn test_project_agent_command() {
        let mut ws = temp_workspace();
        ws.add_project("p", "/tmp/p", None);

        // Defaults to None
        assert!(ws.get_project_agent_command("p").is_none());

        // Set
        ws.set_project_agent_command("p", "aider");
        assert_eq!(ws.get_project_agent_command("p"), Some("aider"));

        // Clear via empty string
        ws.set_project_agent_command("p", "");
        assert!(ws.get_project_agent_command("p").is_none());

        // Unknown project: getter returns None, setter is a no-op (no panic)
        assert!(ws.get_project_agent_command("nope").is_none());
        ws.set_project_agent_command("nope", "codex");

        // YAML roundtrip preserves the override
        ws.set_project_agent_command("p", "codex");
        ws.save().unwrap();
        let mut ws2 = Workspace::new(ws.file_path.clone());
        ws2.load().unwrap();
        assert_eq!(ws2.get_project_agent_command("p"), Some("codex"));

        let _ = fs::remove_dir_all(ws.file_path.parent().unwrap());
    }

    #[test]
    fn test_swap_projects() {
        let mut ws = temp_workspace();
        ws.add_project("a", "/tmp/a", None);
        ws.add_project("b", "/tmp/b", None);
        ws.swap_projects(0, 1);
        assert_eq!(ws.projects[0].name, "b");
        assert_eq!(ws.projects[1].name, "a");
    }

    #[test]
    fn test_set_thread_display_name() {
        let mut ws = temp_workspace();
        ws.add_project("p", "/tmp/p", None);
        ws.create_thread("p");

        // Auto-set (no lock)
        ws.set_thread_display_name("p-1", "Refactoring auth", false);
        assert_eq!(ws.threads[0].name.as_deref(), Some("Refactoring auth"));
        assert!(!ws.threads[0].name_locked);
        assert_eq!(ws.display_name_for("p-1"), "Refactoring auth");

        // Auto-update again (still not locked)
        ws.set_thread_display_name("p-1", "Other work", false);
        assert_eq!(ws.threads[0].name.as_deref(), Some("Other work"));
        assert!(!ws.threads[0].name_locked);

        // User rename (locks)
        ws.set_thread_display_name("p-1", "User chosen", true);
        assert!(ws.threads[0].name_locked);
        assert_eq!(ws.display_name_for("p-1"), "User chosen");

        // Lock stays sticky on subsequent calls
        ws.set_thread_display_name("p-1", "Other again", false);
        // The stored value still updates (caller is responsible for skipping if locked)
        // — the lock is informational, enforced by the caller via is_thread_name_locked.
        // But test that lock flag remains true.
        assert!(ws.threads[0].name_locked);
    }

    #[test]
    fn test_display_name_for_falls_back_to_id() {
        let mut ws = temp_workspace();
        ws.add_project("p", "/tmp/p", None);
        ws.create_thread("p");
        assert_eq!(ws.display_name_for("p-1"), "p-1");
        assert_eq!(ws.display_name_for("nonexistent"), "nonexistent");
    }

    #[test]
    fn test_is_thread_name_locked() {
        let mut ws = temp_workspace();
        ws.add_project("p", "/tmp/p", None);
        ws.create_thread("p");
        assert!(!ws.is_thread_name_locked("p-1"));
        ws.set_thread_display_name("p-1", "x", true);
        assert!(ws.is_thread_name_locked("p-1"));
        assert!(!ws.is_thread_name_locked("nonexistent"));
    }

    #[test]
    fn test_load_legacy_session_keys() {
        // YAML using the pre-rename keys: sessions / active_session / next_session_id.
        // The serde aliases should let this load without modification.
        let id = COUNTER.fetch_add(1, Ordering::SeqCst);
        let dir = std::env::temp_dir().join(format!("arta-legacy-{}-{}", std::process::id(), id));
        let _ = fs::create_dir_all(&dir);
        let path = dir.join("workspace.yaml");
        let yaml = "projects:\n  - name: p\n    path: /tmp/p\nsessions:\n  - id: p-1\n    project: p\n    created: 2026-01-01T00:00:00Z\nactive_session: p-1\nnext_session_id: 1\n";
        fs::write(&path, yaml).unwrap();

        let mut ws = Workspace::new(path);
        ws.load().unwrap();
        assert_eq!(ws.projects.len(), 1);
        assert_eq!(ws.threads.len(), 1);
        assert_eq!(ws.threads[0].id, "p-1");
        assert_eq!(ws.active_thread.as_deref(), Some("p-1"));

        let _ = fs::remove_dir_all(dir);
    }

    #[test]
    fn test_migrate_from_json() {
        let id = COUNTER.fetch_add(1, Ordering::SeqCst);
        let base = std::env::temp_dir().join(format!("arta-migrate-{}-{}", std::process::id(), id));

        // Create a legacy JSON file using the old session-keyed schema.
        let legacy_dir = base.join("legacy");
        let _ = fs::create_dir_all(&legacy_dir);
        let json = r#"{
  "projects": [{"name": "test", "path": "/tmp/test"}],
  "sessions": [{"id": "test-1", "project": "test", "created": "2026-01-01T00:00:00Z"}],
  "active_session": "test-1",
  "next_session_id": 1
}"#;
        fs::write(legacy_dir.join("workspace.json"), json).unwrap();

        // New YAML path
        let new_dir = base.join("new");
        let _ = fs::create_dir_all(&new_dir);
        let new_path = new_dir.join("workspace.yaml");

        // Cannot use migrate_workspace_if_needed directly since it checks
        // dirs::config_dir(). Test the JSON-to-YAML conversion logic instead,
        // exercising the same serde aliases.
        let loaded: Workspace =
            serde_json::from_str(json).unwrap();
        let yaml = serde_yaml::to_string(&loaded).unwrap();
        fs::write(&new_path, &yaml).unwrap();

        let mut ws = Workspace::new(new_path.clone());
        ws.load().unwrap();
        assert_eq!(ws.projects.len(), 1);
        assert_eq!(ws.projects[0].name, "test");
        assert_eq!(ws.threads[0].id, "test-1");

        let _ = fs::remove_dir_all(&base);
    }
}