tam-cli 0.3.5

Terminal agent multiplexer — manage units of work, not just processes
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
use std::io::IsTerminal;

use anyhow::Result;
use clap::Parser;

mod cli;
mod client;
mod config;
mod ledger;
mod sessions;
mod task;
mod tui;

use cli::{Cli, Commands};
use ledger::{Ledger, LedgerEvent};

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();
    let config = config::load_config()?;

    let command = match cli.command {
        Some(cmd) => cmd,
        None => return tui::run().await,
    };

    match command {
        Commands::New {
            name,
            worktree,
            source,
            no_start,
        } => {
            let mut ledger = Ledger::load()?;

            if ledger.task_exists(&name) {
                anyhow::bail!("task '{}' already exists", name);
            }

            let worktree = worktree || source.is_some();

            let task_dir = if worktree {
                // Owned context: create a worktree
                let wt_config = tam_worktree::config::load_config()?;
                let cwd = std::fs::canonicalize(".")?;
                let wt_path =
                    tam_worktree::worktree::create(&name, source.as_deref(), &wt_config, &cwd)?;

                if wt_config.auto_init {
                    tam_worktree::init::run(&wt_path)?;
                }

                ledger.append(LedgerEvent::TaskCreated {
                    name: name.clone(),
                    dir: wt_path.clone(),
                    owned: true,
                    timestamp: ledger::now(),
                })?;

                println!("Created task '{}' at {}", name, wt_path.display());
                wt_path
            } else {
                // Borrowed context: bind to cwd
                let cwd = std::fs::canonicalize(".")?;

                if let Some(existing) = ledger.find_task_by_dir(&cwd) {
                    anyhow::bail!("directory already has an active task: '{}'", existing.name);
                }

                ledger.append(LedgerEvent::TaskCreated {
                    name: name.clone(),
                    dir: cwd.clone(),
                    owned: false,
                    timestamp: ledger::now(),
                })?;

                println!("Created task '{}' in {}", name, cwd.display());
                cwd
            };

            if !no_start {
                let mut client = client::Client::connect().await?;
                let resp = client
                    .send(tam_proto::Request::Spawn {
                        provider: config.default_agent.clone(),
                        dir: task_dir,
                        id: Some(name.clone()),
                        args: vec![],
                        resume_session: None,
                        prompt: None,
                    })
                    .await?;

                match resp {
                    tam_proto::Response::Spawned { id } => {
                        ledger.append(LedgerEvent::AgentRunStarted {
                            task: name.clone(),
                            provider: config.default_agent.clone(),
                            session_id: None,
                            timestamp: ledger::now(),
                        })?;

                        // Attach immediately
                        let client = client::Client::connect().await?;
                        client.attach(&id).await?;
                    }
                    tam_proto::Response::Error { message } => {
                        eprintln!("Error: {}", message);
                        std::process::exit(1);
                    }
                    _ => {}
                }
            }
        }

        Commands::Run {
            name,
            new_session,
            agent,
            prompt,
            args,
        } => {
            let mut ledger = Ledger::load()?;
            let name = resolve_task_name(name, &ledger)?;
            let task = ledger
                .find_task(&name)
                .ok_or_else(|| anyhow::anyhow!("task '{}' not found", name))?;

            let agent = agent.unwrap_or_else(|| config.default_agent.clone());
            config::validate_provider(&agent)?;

            // Resolve session — cross-reference ledger runs with filesystem sessions
            let resume_session = if new_session || !std::io::stdin().is_terminal() {
                None
            } else {
                let runs = ledger.task_runs(&name);
                let found = sessions::list_sessions_for_task(&agent, &task.dir, &runs);
                if found.is_empty() {
                    None
                } else {
                    config::pick_session(&found)?
                }
            };

            let mut client = client::Client::connect().await?;
            let resp = client
                .send(tam_proto::Request::Spawn {
                    provider: agent.clone(),
                    dir: task.dir.clone(),
                    id: Some(name.clone()),
                    args,
                    resume_session: resume_session.clone(),
                    prompt,
                })
                .await?;

            match resp {
                tam_proto::Response::Spawned { id } => {
                    ledger.append(LedgerEvent::AgentRunStarted {
                        task: name.clone(),
                        provider: agent,
                        session_id: resume_session,
                        timestamp: ledger::now(),
                    })?;

                    // Attach immediately
                    let client = client::Client::connect().await?;
                    client.attach(&id).await?;
                }
                tam_proto::Response::Error { message } => {
                    eprintln!("Error: {}", message);
                    std::process::exit(1);
                }
                _ => {}
            }
        }

        Commands::Stop { name } => {
            let mut ledger = Ledger::load()?;
            let name = resolve_task_name(name, &ledger)?;

            let mut client = client::Client::connect().await?;
            let resp = client
                .send(tam_proto::Request::Kill { id: name.clone() })
                .await?;
            match resp {
                tam_proto::Response::Ok => {
                    ledger.append(LedgerEvent::AgentRunEnded {
                        task: name.clone(),
                        exit_code: -1,
                        timestamp: ledger::now(),
                    })?;
                    println!("Stopped agent in task '{}'", name);
                }
                tam_proto::Response::Error { message } => {
                    eprintln!("Error: {}", message);
                    std::process::exit(1);
                }
                _ => {}
            }
        }

        Commands::Attach { name } => {
            let ledger = Ledger::load()?;
            let name = resolve_task_name(name, &ledger)?;

            let client = client::Client::connect().await?;
            client.attach(&name).await?;
        }

        Commands::Drop { name, branch } => {
            let mut ledger = Ledger::load()?;
            let task = ledger
                .find_task(&name)
                .ok_or_else(|| anyhow::anyhow!("task '{}' not found", name))?;

            // Kill agent if running
            if let Ok(mut client) = client::Client::connect().await {
                let _ = client
                    .send(tam_proto::Request::Kill { id: name.clone() })
                    .await;
            }

            // Delete worktree if owned
            if task.owned {
                let wt_config = tam_worktree::config::load_config()?;
                // Find repo root from the worktree dir (or cwd as fallback)
                let cwd = if task.dir.exists() {
                    task.dir.clone()
                } else {
                    std::fs::canonicalize(".")?
                };
                if task.dir.exists() {
                    tam_worktree::worktree::delete(&name, branch, true, &wt_config, &cwd)?;
                }
                ledger.append(LedgerEvent::WorktreeDeleted {
                    task: name.clone(),
                    timestamp: ledger::now(),
                })?;
            }

            ledger.append(LedgerEvent::TaskDropped {
                task: name.clone(),
                timestamp: ledger::now(),
            })?;

            println!("Dropped task '{}'", name);
        }

        Commands::Ps { json } => {
            let ledger = Ledger::load()?;
            let snapshots = ledger.active_tasks();

            // Get running agents from daemon
            let agents = if let Ok(mut client) = client::Client::connect().await {
                match client.send(tam_proto::Request::List).await {
                    Ok(tam_proto::Response::Agents { agents }) => agents,
                    _ => vec![],
                }
            } else {
                vec![]
            };

            let mut tasks: Vec<task::Task> = snapshots
                .into_iter()
                .map(|s| {
                    let agent_info = agents.iter().find(|a| a.id == s.name).cloned();
                    task::Task::from_snapshot(s, agent_info)
                })
                .collect();

            // Populate git branch status for owned tasks without a running agent
            for t in &mut tasks {
                if t.owned && t.agent_info.is_none() {
                    t.git_branch_status = task::check_git_branch_status(&t.name, &t.dir);
                }
            }

            tasks.sort_by_key(|t| (t.status().sort_priority(), t.name.clone()));

            if json {
                let entries: Vec<serde_json::Value> = tasks
                    .iter()
                    .map(|t| {
                        serde_json::json!({
                            "name": t.name,
                            "status": t.status().to_string(),
                            "dir": t.dir,
                            "owned": t.owned,
                            "agent": t.agent_info.as_ref().map(|a| &a.provider),
                            "context_percent": t.agent_info.as_ref().and_then(|a| a.context_percent),
                            "run_count": t.run_count,
                            "last_activity": t.last_activity,
                        })
                    })
                    .collect();
                println!("{}", serde_json::to_string_pretty(&entries)?);
            } else if tasks.is_empty() {
                println!("No tasks.");
            } else {
                println!(
                    "{:<12} {:<15} {:<10} {:>5} {:>5} {:<30} {:>5}",
                    "STATUS", "TASK", "AGENT", "RUNS", "LAST", "DIR", "CTX"
                );
                for t in &tasks {
                    let dir = shorten_home(&t.dir.display().to_string());
                    let agent = t
                        .agent_info
                        .as_ref()
                        .map(|a| a.provider.as_str())
                        .unwrap_or("-");
                    let ctx = t
                        .agent_info
                        .as_ref()
                        .and_then(|a| a.context_percent)
                        .map(|p| format!("{}%", p))
                        .unwrap_or_else(|| "-".into());
                    println!(
                        "{:<12} {:<15} {:<10} {:>5} {:>5} {:<30} {:>5}",
                        t.status().indicator(),
                        t.name,
                        agent,
                        t.run_count,
                        format_age(t.last_activity),
                        dir,
                        ctx,
                    );
                }
            }
        }

        Commands::Ls {
            path,
            json,
            raw,
            porcelain,
        } => {
            let wt_config = tam_worktree::config::load_config()?;
            let root = path.unwrap_or_else(|| {
                dirs::home_dir().unwrap_or_else(|| std::path::PathBuf::from("."))
            });
            let ignore = tam_worktree::discovery::build_ignore_set(&wt_config.ignore)?;
            let paths = tam_worktree::discovery::discover(&root, &ignore, wt_config.max_depth)?;

            if json {
                let entries = tam_worktree::pretty::build_pretty_names(&paths);
                let json_entries: Vec<serde_json::Value> = entries
                    .iter()
                    .map(|e| serde_json::json!({"path": e.path, "pretty": e.display_name}))
                    .collect();
                println!("{}", serde_json::to_string_pretty(&json_entries)?);
            } else if raw {
                for p in &paths {
                    println!("{}", p.display());
                }
            } else if porcelain {
                let entries = tam_worktree::pretty::build_pretty_names(&paths);
                for entry in &entries {
                    println!("{}", entry.display_name);
                }
            } else {
                let entries = tam_worktree::pretty::build_pretty_names(&paths);
                let lines = tam_worktree::pretty::build_tree_output(&entries);
                for line in &lines {
                    println!("{}", line);
                }
            }
        }

        Commands::Pick { finder } => {
            let wt_config = tam_worktree::config::load_config()?;
            let root = dirs::home_dir().unwrap_or_else(|| std::path::PathBuf::from("."));
            let ignore = tam_worktree::discovery::build_ignore_set(&wt_config.ignore)?;
            let paths = tam_worktree::discovery::discover(&root, &ignore, wt_config.max_depth)?;
            let entries = tam_worktree::pretty::build_pretty_names(&paths);

            // Pipe through fzf or configured finder
            use std::io::Write;
            use std::process::{Command, Stdio};

            let finder = finder
                .as_deref()
                .or(config.finder.as_deref())
                .unwrap_or("fzf");
            let mut child = Command::new("sh")
                .arg("-c")
                .arg(finder)
                .stdin(Stdio::piped())
                .stdout(Stdio::piped())
                .stderr(Stdio::inherit())
                .spawn()
                .map_err(|_| anyhow::anyhow!("finder '{}' not found", finder))?;

            let mut stdin = child.stdin.take().unwrap();
            for entry in &entries {
                writeln!(stdin, "{}", entry.display_name)?;
            }
            drop(stdin);

            let output = child.wait_with_output()?;
            if !output.status.success() {
                std::process::exit(1);
            }
            let choice = String::from_utf8_lossy(&output.stdout).trim().to_string();
            if let Ok(path) = tam_worktree::pretty::resolve(&choice, &paths) {
                println!("{}", path.display());
            }
        }

        Commands::Init { agent } => {
            config::validate_provider(&agent)?;
            config::init_agent_hooks(&agent)?;
        }

        Commands::Shutdown => {
            let mut client = client::Client::connect().await?;
            let resp = client.send(tam_proto::Request::Shutdown).await?;
            match resp {
                tam_proto::Response::Ok => println!("Daemon shutting down."),
                tam_proto::Response::Error { message } => {
                    eprintln!("Error: {}", message);
                    std::process::exit(1);
                }
                _ => {}
            }
        }

        Commands::Status => match client::Client::try_connect().await? {
            Some(_) => println!("Daemon is running."),
            None => {
                println!("Daemon is not running.");
                std::process::exit(1);
            }
        },

        Commands::Daemon => {
            use tracing_subscriber::EnvFilter;
            tracing_subscriber::fmt()
                .with_env_filter(
                    EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
                )
                .init();
            #[cfg(unix)]
            {
                let _ = nix::unistd::setsid();
            }
            let socket_path = tam_proto::default_socket_path();
            let d = tam_daemon::daemon::Daemon::new(socket_path);
            return d.run().await;
        }

        Commands::HookNotify { agent, event } => {
            // Not running under tam — silently succeed so hooks don't block the agent
            let agent = match agent {
                Some(a) => a,
                None => {
                    // Also check ZINC_AGENT_ID for migration
                    match std::env::var("ZINC_AGENT_ID") {
                        Ok(a) => a,
                        Err(_) => return Ok(()),
                    }
                }
            };
            // Best-effort: don't block the agent
            if let Ok(mut client) = client::Client::connect().await {
                let _ = client
                    .send(tam_proto::Request::HookEvent {
                        agent_id: agent,
                        event,
                    })
                    .await;
            }
        }
    }

    Ok(())
}

/// Resolve a task name from explicit argument or from cwd.
fn resolve_task_name(name: Option<String>, ledger: &Ledger) -> Result<String> {
    if let Some(name) = name {
        return Ok(name);
    }
    let cwd = std::fs::canonicalize(".")?;
    match ledger.find_task_by_dir(&cwd) {
        Some(task) => Ok(task.name),
        None => anyhow::bail!("no task in current directory"),
    }
}

fn format_age(timestamp: Option<u64>) -> String {
    let Some(ts) = timestamp else {
        return "-".into();
    };
    let now = ledger::now();
    let elapsed = now.saturating_sub(ts);
    if elapsed < 60 {
        "now".into()
    } else if elapsed < 3600 {
        format!("{}m", elapsed / 60)
    } else if elapsed < 86400 {
        format!("{}h", elapsed / 3600)
    } else {
        format!("{}d", elapsed / 86400)
    }
}

fn shorten_home(path: &str) -> String {
    if let Ok(home) = std::env::var("HOME") {
        if let Some(rest) = path.strip_prefix(&home) {
            return format!("~{}", rest);
        }
    }
    path.to_string()
}

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

    #[test]
    fn test_shorten_home() {
        assert_eq!(shorten_home("/other/path"), "/other/path");
        if let Ok(home) = std::env::var("HOME") {
            let input = format!("{}/projects/foo", home);
            assert_eq!(shorten_home(&input), "~/projects/foo");
        }
    }

    #[test]
    fn test_format_age_none() {
        assert_eq!(format_age(None), "-");
    }

    #[test]
    fn test_format_age_recent() {
        let ts = ledger::now();
        assert_eq!(format_age(Some(ts)), "now");
    }

    #[test]
    fn test_format_age_minutes() {
        let ts = ledger::now() - 300; // 5 minutes ago
        assert_eq!(format_age(Some(ts)), "5m");
    }

    #[test]
    fn test_format_age_hours() {
        let ts = ledger::now() - 7200; // 2 hours ago
        assert_eq!(format_age(Some(ts)), "2h");
    }

    #[test]
    fn test_format_age_days() {
        let ts = ledger::now() - 172800; // 2 days ago
        assert_eq!(format_age(Some(ts)), "2d");
    }

    #[test]
    fn test_resolve_task_name_explicit() {
        let tmp = tempfile::TempDir::new().unwrap();
        let ledger = Ledger::load_from(tmp.path().join("ledger.jsonl")).unwrap();
        let name = resolve_task_name(Some("my-task".into()), &ledger).unwrap();
        assert_eq!(name, "my-task");
    }

    #[test]
    fn test_resolve_task_name_from_cwd() {
        let tmp = tempfile::TempDir::new().unwrap();
        let mut ledger = Ledger::load_from(tmp.path().join("ledger.jsonl")).unwrap();

        let cwd = std::fs::canonicalize(".").unwrap();
        ledger
            .append(LedgerEvent::TaskCreated {
                name: "cwd-task".into(),
                dir: cwd,
                owned: false,
                timestamp: ledger::now(),
            })
            .unwrap();

        let name = resolve_task_name(None, &ledger).unwrap();
        assert_eq!(name, "cwd-task");
    }

    #[test]
    fn test_resolve_task_name_no_match() {
        let tmp = tempfile::TempDir::new().unwrap();
        let ledger = Ledger::load_from(tmp.path().join("ledger.jsonl")).unwrap();
        let result = resolve_task_name(None, &ledger);
        assert!(result.is_err());
    }

    /// Helper: create a git repo with an initial commit.
    fn init_git_repo(path: &Path) {
        std::fs::create_dir_all(path).unwrap();
        let git = |args: &[&str]| {
            std::process::Command::new("git")
                .args(args)
                .current_dir(path)
                .output()
                .unwrap()
        };
        git(&["init"]);
        git(&["config", "user.email", "test@test.com"]);
        git(&["config", "user.name", "Test"]);
        std::fs::write(path.join("README.md"), "# test").unwrap();
        git(&["add", "."]);
        git(&["commit", "-m", "init"]);
    }

    #[test]
    fn test_worktree_flow() {
        let tmp = tempfile::TempDir::new().unwrap();
        let repo = tmp.path().join("myrepo");
        init_git_repo(&repo);

        // Create a worktree via tam-worktree
        let wt_config = tam_worktree::config::Config {
            max_depth: 3,
            ignore: vec![],
            worktree_root: tmp.path().to_path_buf(),
            auto_init: false,
        };
        let wt_path = tam_worktree::worktree::create("test-feat", None, &wt_config, &repo).unwrap();
        assert!(wt_path.exists());

        // Track in ledger
        let mut ledger = Ledger::load_from(tmp.path().join("ledger.jsonl")).unwrap();
        ledger
            .append(LedgerEvent::TaskCreated {
                name: "test-feat".into(),
                dir: wt_path.clone(),
                owned: true,
                timestamp: ledger::now(),
            })
            .unwrap();

        let tasks = ledger.active_tasks();
        assert_eq!(tasks.len(), 1);
        assert!(tasks[0].owned);

        // Delete worktree
        tam_worktree::worktree::delete("test-feat", false, true, &wt_config, &wt_path).unwrap();
        assert!(!wt_path.exists());

        // Drop from ledger
        ledger
            .append(LedgerEvent::TaskDropped {
                task: "test-feat".into(),
                timestamp: ledger::now(),
            })
            .unwrap();
        assert!(ledger.active_tasks().is_empty());
    }

    #[test]
    fn test_task_status_with_git() {
        let tmp = tempfile::TempDir::new().unwrap();
        let repo = tmp.path().join("myrepo");
        init_git_repo(&repo);

        let git = |args: &[&str]| {
            std::process::Command::new("git")
                .args(args)
                .current_dir(&repo)
                .output()
                .unwrap()
        };

        // Create a worktree so we have a real owned task dir
        let wt_config = tam_worktree::config::Config {
            max_depth: 3,
            ignore: vec![],
            worktree_root: tmp.path().to_path_buf(),
            auto_init: false,
        };
        let wt_path =
            tam_worktree::worktree::create("test-branch", None, &wt_config, &repo).unwrap();

        // check_git_branch_status uses repo_root which resolves from the worktree.
        let root = tam_worktree::git::repo_root(&wt_path).unwrap();
        assert!(
            root.join(".git").exists(),
            "repo_root should find main repo"
        );

        // Fresh branch should be Active
        assert!(tam_worktree::git::local_branch_exists(&root, "test-branch").unwrap());
        let status = task::check_git_branch_status("test-branch", &wt_path);
        assert_eq!(
            status,
            task::GitBranchStatus::Active,
            "new branch should be active"
        );

        // Make a commit on the worktree branch — still active
        std::fs::write(wt_path.join("new.txt"), "content").unwrap();
        let git_wt = |args: &[&str]| {
            std::process::Command::new("git")
                .args(args)
                .current_dir(&wt_path)
                .output()
                .unwrap()
        };
        git_wt(&["add", "."]);
        git_wt(&["commit", "-m", "diverge"]);

        let status = task::check_git_branch_status("test-branch", &wt_path);
        assert_eq!(status, task::GitBranchStatus::Active);

        // Merge it back — branch still exists, still Active
        git(&["merge", "--no-ff", "test-branch", "-m", "Merge test-branch"]);
        let status = task::check_git_branch_status("test-branch", &wt_path);
        assert_eq!(status, task::GitBranchStatus::Active);

        // Remove worktree and delete branch → Gone
        tam_worktree::worktree::delete("test-branch", false, true, &wt_config, &wt_path).unwrap();
        git(&["branch", "-d", "test-branch"]);
        let status = task::check_git_branch_status("test-branch", &repo);
        assert_eq!(status, task::GitBranchStatus::Gone);
    }
}