tam-cli 0.5.0

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
//! The non-interactive CLI, what agents and scripts rely on: exit statuses,
//! errors as JSON with `--json`, starting agents without attaching, `tam ps
//! --json`, `tam wait` and `tam result`. The documents are in `schema.rs`.

use std::io::IsTerminal;
use std::time::Duration;

use anyhow::Result;
use serde::Serialize;
use tam_proto::{AgentInfo, ChatInfo, MemberInfo, MemberOutcome, MemberState, TaskInfo};

use crate::cli::{Commands, Until};
use crate::client::{Client, Target};
use crate::schema::{self, ErrorInfo, SCHEMA_VERSION};

/// Why a command failed, as scripts tell it: its exit status and, with
/// `--json`, the `error.kind` of its document. Errors of no kind failed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorKind {
    /// It could not do what was asked.
    Failed,
    /// `tam wait`: an agent exited or failed its turn short of the states
    /// waited for.
    Unreachable,
    /// `tam result`: a member has no response to give.
    NoResult,
    /// No such task or member, or a task name where one member is needed.
    NotFound,
    /// `tam wait` ran out of time.
    Timeout,
    /// The daemon could not be reached, or broke off.
    Daemon,
}

impl ErrorKind {
    /// The exit status (2, usage errors, is clap's).
    pub fn exit_code(self) -> i32 {
        match self {
            Self::Failed | Self::Unreachable | Self::NoResult => 1,
            Self::NotFound => 3,
            Self::Timeout => 4,
            Self::Daemon => 5,
        }
    }

    pub fn as_str(self) -> &'static str {
        match self {
            Self::Failed => "failed",
            Self::Unreachable => "unreachable",
            Self::NoResult => "no_result",
            Self::NotFound => "not_found",
            Self::Timeout => "timeout",
            Self::Daemon => "daemon",
        }
    }
}

/// An error of a known kind, anywhere in an `anyhow::Error`'s chain.
#[derive(Debug)]
pub struct CliError {
    kind: ErrorKind,
    message: String,
    /// Printed already, in the command's JSON document: exit only.
    reported: bool,
}

impl CliError {
    fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
        Self {
            kind,
            message: message.into(),
            reported: false,
        }
    }
}

impl std::fmt::Display for CliError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.message)
    }
}

impl std::error::Error for CliError {}

/// An error of this kind.
pub fn error(kind: ErrorKind, message: impl Into<String>) -> anyhow::Error {
    CliError::new(kind, message).into()
}

/// The kind of a failure (`Failed` when nothing in it tells).
pub fn kind_of(err: &anyhow::Error) -> ErrorKind {
    err.chain()
        .find_map(|e| e.downcast_ref::<CliError>())
        .map_or(ErrorKind::Failed, |e| e.kind)
}

impl ErrorInfo {
    pub fn new(err: &anyhow::Error) -> Self {
        Self {
            kind: kind_of(err).as_str(),
            message: format!("{err:#}"),
        }
    }
}

/// Whether a command was asked for JSON: its failure is reported as JSON.
pub fn json_output(command: &Commands) -> bool {
    match command {
        Commands::New { json, .. }
        | Commands::Run { json, .. }
        | Commands::Ps { json }
        | Commands::Ls { json, .. }
        | Commands::History { json, .. }
        | Commands::Peers { json, .. }
        | Commands::Wait { json, .. }
        | Commands::Result { json, .. }
        | Commands::Brief { json, .. }
        | Commands::Merge { json, .. }
        | Commands::Split { json, .. }
        | Commands::Chat { json, .. }
        | Commands::Close { json, .. }
        | Commands::Rename { json, .. }
        | Commands::Keep { json, .. } => *json,
        _ => false,
    }
}

/// Report a failed command and return its exit status: with `--json`, a
/// document on stdout (`schema_version` and `error`), else `Error: …` on
/// stderr. Nothing when the command printed it already.
pub fn fail(err: &anyhow::Error, json: bool) -> i32 {
    let found = err.chain().find_map(|e| e.downcast_ref::<CliError>());
    if !found.is_some_and(|e| e.reported) {
        if json {
            let doc = schema::Failure {
                schema_version: SCHEMA_VERSION,
                error: ErrorInfo::new(err),
            };
            if let Ok(doc) = serde_json::to_string_pretty(&doc) {
                println!("{doc}");
            }
        } else {
            eprintln!("Error: {err:?}");
        }
    }
    kind_of(err).exit_code()
}

/// End a command that printed its JSON document (with `error`): its
/// failure is only to exit with.
pub fn finish(error: Option<anyhow::Error>, json: bool) -> Result<()> {
    match error {
        None => Ok(()),
        Some(err) if json => Err(CliError {
            kind: kind_of(&err),
            message: format!("{err:#}"),
            reported: true,
        }
        .into()),
        Some(err) => Err(err),
    }
}

fn print_json<T: Serialize>(doc: &T) -> Result<()> {
    println!("{}", serde_json::to_string_pretty(doc)?);
    Ok(())
}

/// Whether `tam new`/`tam run` attach to the agent they start: unless told
/// not to (`--detach`, `--json`), or with no terminal to attach (stdin and
/// stdout both).
pub fn attaching(detach: bool) -> bool {
    !detach && std::io::stdin().is_terminal() && std::io::stdout().is_terminal()
}

/// Start the agents of a task's `members` without attaching (`start`:
/// `None` starts none), then tell what started: the JSON receipt, or a line
/// per agent. Stops at the first agent that fails to start.
pub async fn start_detached(
    client: &mut Client,
    task: &TaskInfo,
    members: &[MemberInfo],
    start: Option<crate::Start>,
    json: bool,
) -> Result<()> {
    let mut started: Vec<schema::Started> = members
        .iter()
        .map(|m| schema::Started::new(task, m))
        .collect();
    let mut failure = None;
    if let Some(start) = start {
        for (member, entry) in members.iter().zip(started.iter_mut()) {
            let run = client
                .run_agent(
                    &member.id,
                    start.session.clone(),
                    start.prompt.clone(),
                    start.args.clone(),
                )
                .await;
            match run {
                Ok((_, resumed)) => {
                    // The agent the daemon picked is that of the run it
                    // just recorded.
                    let (_, _, runs) = client.resolve_member(Some(&member.id), None).await?;
                    entry.agent = runs.last().map(|r| r.provider.clone());
                    entry.resumed_session = resumed;
                }
                Err(e) => {
                    failure = Some(e);
                    break;
                }
            }
        }
    }

    if json {
        print_json(&schema::Receipt {
            schema_version: SCHEMA_VERSION,
            of: schema::Subject::Task(task.name.clone()),
            members: started,
            error: failure.as_ref().map(ErrorInfo::new),
        })?;
    } else {
        for entry in &started {
            let Some(agent) = &entry.agent else {
                continue;
            };
            let session = match &entry.resumed_session {
                Some(id) => format!("resumed session {id}"),
                None => "new session".into(),
            };
            println!(
                "Started {agent} in '{}' ({session}). Attach with: tam attach {}",
                entry.label, entry.label
            );
        }
    }
    finish(failure, json)
}

/// `tam ps --json` (see `schema::Ps`).
pub async fn ps(
    client: &mut Client,
    infos: Vec<TaskInfo>,
    chats: Vec<ChatInfo>,
    agents: &[AgentInfo],
) -> Result<()> {
    let mut tasks = Vec::with_capacity(infos.len());
    for info in infos {
        // The runs tell the members' last exit and session. A task dropped
        // meanwhile has none.
        let runs = match client.get_task(&info.name).await {
            Ok((_, runs)) => runs,
            Err(e) if kind_of(&e) == ErrorKind::Daemon => return Err(e),
            Err(_) => Vec::new(),
        };
        tasks.push((info, runs));
    }
    let mut with_runs = Vec::with_capacity(chats.len());
    for chat in chats {
        // A chat closed meanwhile has none.
        let runs = match client.get_chat(&chat.id).await {
            Ok((_, runs)) => runs,
            Err(e) if kind_of(&e) == ErrorKind::Daemon => return Err(e),
            Err(_) => Vec::new(),
        };
        with_runs.push((chat, runs));
    }
    print_json(&schema::Ps::new(tasks, with_runs, agents))
}

/// The agents a command acts on: those of a task's members, or a chat's.
enum Agents {
    Task(TaskInfo, Vec<MemberInfo>),
    Chat(ChatInfo),
}

impl Agents {
    /// The member (or chat) ids, as the daemon takes them.
    fn ids(&self) -> Vec<String> {
        match self {
            Self::Task(_, members) => members.iter().map(|m| m.id.clone()).collect(),
            Self::Chat(chat) => vec![chat.id.clone()],
        }
    }

    fn outcomes(&self, outcomes: &[MemberOutcome], error: Option<ErrorInfo>) -> schema::Outcomes {
        match self {
            Self::Task(task, _) => schema::Outcomes::new(task, outcomes, error),
            Self::Chat(chat) => schema::Outcomes::chat(chat, outcomes, error),
        }
    }
}

/// The agents a command acts on: all of a task's members, the one named as
/// TASK/MEMBER or by id, a chat (by id, or by a name no task has), or the
/// member whose worktree holds the current directory.
async fn agents_of(client: &mut Client, name: Option<&str>) -> Result<Agents> {
    if let Some(name) = name {
        if let Ok((task, _)) = client.get_task(name).await {
            let members = task.members.clone();
            return Ok(Agents::Task(task, members));
        }
    }
    Ok(match crate::resolve_target(client, name).await? {
        Target::Member(task, member) => Agents::Task(task, vec![member]),
        Target::Chat(chat, _) => Agents::Chat(chat),
    })
}

fn member_state(until: Until) -> MemberState {
    match until {
        Until::Working => MemberState::Working,
        Until::Blocked => MemberState::Blocked,
        Until::Ready => MemberState::Ready,
        Until::Idle => MemberState::Idle,
        Until::Question => MemberState::Question,
        Until::Error => MemberState::Error,
        Until::Exit => MemberState::Exit,
    }
}

/// `tam wait`: wait until the agents settle in one of `until` (default:
/// any state but working), then print their states.
pub async fn wait(
    name: Option<String>,
    until: Vec<Until>,
    timeout: Option<Duration>,
    json: bool,
) -> Result<()> {
    let mut client = Client::connect().await?;
    let agents = agents_of(&mut client, name.as_deref()).await?;
    let until: Vec<MemberState> = if until.is_empty() {
        MemberState::SETTLED.to_vec()
    } else {
        until.into_iter().map(member_state).collect()
    };
    let ids = agents.ids();
    let timeout_secs = timeout.map(|t| t.as_secs());
    let (outcomes, timed_out) = client.wait_agents(ids, until.clone(), timeout_secs).await?;

    let error = if timed_out {
        let waiting: Vec<String> = outcomes
            .iter()
            .filter(|o| !o.state.satisfies(&until))
            .map(|o| format!("'{}' is {}", o.label, o.state))
            .collect();
        Some(error(
            ErrorKind::Timeout,
            format!(
                "timed out after {}s: {}",
                timeout_secs.unwrap_or_default(),
                waiting.join(", ")
            ),
        ))
    } else {
        // Given up: on the agent that exited or failed, not on one still
        // going (another member's).
        let stuck = |o: &&MemberOutcome| {
            matches!(o.state, MemberState::Exit | MemberState::Error) && !o.state.satisfies(&until)
        };
        outcomes
            .iter()
            .find(stuck)
            .or_else(|| outcomes.iter().find(|o| !o.state.satisfies(&until)))
            .map(|o| error(ErrorKind::Unreachable, gave_up(o, &until)))
    };
    if json {
        let info = error.as_ref().map(ErrorInfo::new);
        print_json(&agents.outcomes(&outcomes, info))?;
    } else {
        for o in &outcomes {
            println!("{}", state_line(o));
        }
    }
    finish(error, json)
}

/// `tam result`: print the responses that ended the agents' last turns.
pub async fn result(name: Option<String>, json: bool) -> Result<()> {
    let mut client = Client::connect().await?;
    let agents = agents_of(&mut client, name.as_deref()).await?;
    let outcomes = client.outcomes(agents.ids()).await?;

    let missing: Vec<String> = outcomes
        .iter()
        .filter(|o| o.response.is_none())
        .map(no_response)
        .collect();
    let error = (!missing.is_empty()).then(|| error(ErrorKind::NoResult, missing.join("; ")));
    if json {
        let info = error.as_ref().map(ErrorInfo::new);
        print_json(&agents.outcomes(&outcomes, info))?;
    } else {
        // Several members: each response under its member's name.
        let several = outcomes.len() > 1;
        let mut first = true;
        for o in &outcomes {
            let Some(response) = &o.response else {
                continue;
            };
            if several {
                if !first {
                    println!();
                }
                println!("── {} ──", o.label);
            }
            first = false;
            println!("{}", response.text.trim_end());
        }
    }
    finish(error, json)
}

/// What the agent waits on, or why its turn failed.
fn state_detail(o: &MemberOutcome) -> Option<String> {
    match o.state {
        MemberState::Blocked | MemberState::Question => o.pending.as_ref().map(|p| match &p.tool {
            Some(tool) => format!("{tool}: {}", p.summary),
            None => p.summary.clone(),
        }),
        MemberState::Error => o.error.as_ref().map(|e| match &e.message {
            Some(message) => format!("{}: {message}", e.kind),
            None => e.kind.clone(),
        }),
        MemberState::Exit => Some(match (&o.provider, o.exit_code) {
            (None, _) => "no agent ran".into(),
            (Some(_), Some(-1)) => "stopped".into(),
            (Some(_), Some(code)) => format!("exit {code}"),
            (Some(_), None) => "not running".into(),
        }),
        _ => None,
    }
}

/// `feat/api: question (AskUserQuestion: Tea or coffee?)`.
fn state_line(o: &MemberOutcome) -> String {
    match state_detail(o) {
        Some(detail) => format!("{}: {} ({detail})", o.label, o.state),
        None => format!("{}: {}", o.label, o.state),
    }
}

/// Why a wait gave up on a member short of `until`.
fn gave_up(o: &MemberOutcome, until: &[MemberState]) -> String {
    let wanted: Vec<String> = until.iter().map(ToString::to_string).collect();
    let why = match (o.state, &o.provider, o.exit_code) {
        (MemberState::Error, ..) => match state_detail(o) {
            Some(detail) => format!("its turn failed ({detail})"),
            None => "its turn failed".into(),
        },
        (_, None, _) => "no agent ran in it".into(),
        (_, Some(_), Some(-1)) => "its agent was stopped".into(),
        (_, Some(_), Some(code)) => format!("its agent exited (exit {code})"),
        (_, Some(_), None) => "its agent is not running".into(),
    };
    format!(
        "'{}' will not get to {} without someone acting: {why}",
        o.label,
        wanted.join(" or ")
    )
}

/// Why a member has no response.
fn no_response(o: &MemberOutcome) -> String {
    let why = match o.state {
        MemberState::Working => "its agent is in the middle of a turn (tam wait first)".into(),
        MemberState::Blocked | MemberState::Question | MemberState::Error => {
            let what = match o.state {
                MemberState::Error => "its last turn failed",
                MemberState::Blocked => "its agent waits on a permission",
                _ => "its agent asked a question",
            };
            match state_detail(o) {
                Some(detail) => format!("{what} ({detail})"),
                None => what.into(),
            }
        }
        MemberState::Exit if o.provider.is_none() => "no agent ran in it".into(),
        _ => "no turn of its latest run ended with one (not run yet, interrupted, or an agent \
              that reports none: only Claude does)"
            .into(),
    };
    format!("no response from '{}': {why}", o.label)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tam_proto::{AgentResponse, Pending};

    fn outcome(state: MemberState) -> MemberOutcome {
        MemberOutcome {
            member: "m-1".into(),
            task: "feat".into(),
            label: "feat".into(),
            state,
            provider: Some("claude".into()),
            session_id: None,
            exit_code: None,
            pending: None,
            error: None,
            response: None,
        }
    }

    #[test]
    fn exit_statuses_tell_failures_apart() {
        let codes: Vec<(&str, i32)> = [
            ErrorKind::Failed,
            ErrorKind::Unreachable,
            ErrorKind::NoResult,
            ErrorKind::NotFound,
            ErrorKind::Timeout,
            ErrorKind::Daemon,
        ]
        .iter()
        .map(|k| (k.as_str(), k.exit_code()))
        .collect();
        assert_eq!(
            codes,
            [
                ("failed", 1),
                ("unreachable", 1),
                ("no_result", 1),
                ("not_found", 3),
                ("timeout", 4),
                ("daemon", 5)
            ]
        );
    }

    #[test]
    fn the_kind_is_found_under_context() {
        use anyhow::Context;
        let err = Err::<(), _>(error(ErrorKind::NotFound, "task 'x' not found"))
            .context("cannot run")
            .unwrap_err();
        assert_eq!(kind_of(&err), ErrorKind::NotFound);
        assert_eq!(kind_of(&anyhow::anyhow!("plain")), ErrorKind::Failed);
        let info = ErrorInfo::new(&err);
        assert_eq!(info.kind, "not_found");
        assert_eq!(info.message, "cannot run: task 'x' not found");
    }

    #[test]
    fn a_reported_failure_is_not_printed_again() {
        let err = finish(Some(error(ErrorKind::Timeout, "late")), true).unwrap_err();
        assert!(err.downcast_ref::<CliError>().unwrap().reported);
        assert_eq!(fail(&err, true), 4);
        let err = finish(Some(error(ErrorKind::Timeout, "late")), false).unwrap_err();
        assert!(!err.downcast_ref::<CliError>().unwrap().reported);
        assert!(finish(None, true).is_ok());
    }

    #[test]
    fn state_lines_say_what_the_agent_waits_on() {
        let mut o = outcome(MemberState::Question);
        o.pending = Some(Pending {
            tool: Some("AskUserQuestion".into()),
            summary: "Tea or coffee?".into(),
        });
        assert_eq!(
            state_line(&o),
            "feat: question (AskUserQuestion: Tea or coffee?)"
        );
        let mut o = outcome(MemberState::Exit);
        o.exit_code = Some(-1);
        assert_eq!(state_line(&o), "feat: exit (stopped)");
        o.exit_code = Some(2);
        assert_eq!(
            gave_up(&o, &[MemberState::Ready, MemberState::Question]),
            "'feat' will not get to ready or question without someone acting: its agent exited (exit 2)"
        );
        o.provider = None;
        assert_eq!(state_line(&o), "feat: exit (no agent ran)");
        assert_eq!(state_line(&outcome(MemberState::Ready)), "feat: ready");
        assert_eq!(
            gave_up(&o, &[MemberState::Ready]),
            "'feat' will not get to ready without someone acting: no agent ran in it"
        );
    }

    #[test]
    fn no_response_says_why() {
        assert!(no_response(&outcome(MemberState::Working)).contains("tam wait first"));
        let mut o = outcome(MemberState::Exit);
        o.provider = None;
        assert_eq!(
            no_response(&o),
            "no response from 'feat': no agent ran in it"
        );
        let o = outcome(MemberState::Ready);
        assert!(no_response(&o).contains("only Claude"));
        // With one, the outcome renders it whole.
        let mut o = outcome(MemberState::Ready);
        o.response = Some(AgentResponse {
            text: "done".into(),
            at: 3,
        });
        let task = TaskInfo {
            name: "feat".into(),
            members: vec![],
            created_at: 0,
            run_count: 0,
            last_activity: None,
        };
        let doc = serde_json::to_value(schema::Outcomes::new(&task, &[o], None)).unwrap();
        assert_eq!(doc["members"][0]["response"], "done");
        assert_eq!(doc["members"][0]["response_at"], 3);
        assert!(doc["error"].is_null());
    }

    #[test]
    fn wait_takes_states_and_durations() {
        use crate::cli::Cli;
        use clap::Parser;
        let parse = |args: &[&str]| Cli::try_parse_from(["tam"].iter().chain(args));
        match parse(&[
            "wait",
            "feat",
            "--until",
            "input,exit",
            "-t",
            "15m",
            "--json",
        ])
        .unwrap()
        .command
        {
            Some(Commands::Wait {
                name,
                until,
                timeout,
                json,
            }) => {
                assert_eq!(name.as_deref(), Some("feat"));
                assert_eq!(until, [Until::Ready, Until::Exit]);
                assert_eq!(timeout, Some(Duration::from_secs(900)));
                assert!(json);
            }
            _ => panic!("not a wait"),
        }
        for (arg, secs) in [("90", 90), ("90s", 90), ("2h", 7200), ("0", 0)] {
            match parse(&["wait", "--timeout", arg]).unwrap().command {
                Some(Commands::Wait { timeout, .. }) => {
                    assert_eq!(timeout, Some(Duration::from_secs(secs)), "{arg}")
                }
                _ => panic!("not a wait"),
            }
        }
        for bad in ["", "m", "1d", "-5", "1.5h", "99999999999999999999h"] {
            assert!(parse(&["wait", "--timeout", bad]).is_err(), "{bad}");
        }
        assert!(parse(&["wait", "--until", "done"]).is_err());

        // One prompt, given either way; none with --no-start.
        assert!(parse(&["run", "feat", "--prompt", "a", "b"]).is_err());
        assert!(parse(&["run", "--prompt", "a", "-d"]).is_ok());
        assert!(parse(&["new", "feat", "--prompt", "a", "--no-start"]).is_err());
    }
}