tam-cli 0.6.1

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
//! Messages between task members: `tam msg`, `tam history`, and `tam
//! inbox-wait` (the inbox hook of the Claude agents tam spawns).

use std::io::{IsTerminal, Read};
use std::path::PathBuf;
use std::time::Duration;

use anyhow::{anyhow, bail, Context, Result};
use tam_proto::{
    ChatInfo, Event, MessageInfo, Request, Response, RunInfo, ServerMessage, TaskInfo,
};

use crate::client::{Client, Target};
use crate::script::{self, ErrorKind};

/// How long `tam msg` waits to see the message delivered before reporting
/// it as on its way.
const DELIVERY_WAIT: Duration = Duration::from_secs(3);

/// Longest message excerpt `tam history` shows.
const EXCERPT_LEN: usize = 72;

/// `tam msg`: send `text` (or stdin) to `target`; with `wait`, wait that many
/// seconds for the reply and print it.
pub async fn send(
    target: &str,
    text: Vec<String>,
    wait: Option<u64>,
    from: Option<String>,
) -> Result<()> {
    let text = if text.is_empty() {
        let stdin = std::io::stdin();
        if stdin.is_terminal() {
            bail!("no message: give it as arguments or on stdin");
        }
        let mut text = String::new();
        stdin
            .lock()
            .read_to_string(&mut text)
            .context("failed to read the message from stdin")?;
        text
    } else {
        text.join(" ")
    };

    let mut client = Client::connect().await?;
    client.subscribe().await?;
    let request = Request::SendMessage {
        to: target.into(),
        text,
        from,
    };
    let (message, pending) = match client.send(request).await? {
        Response::Sent { message, pending } => (*message, pending),
        other => {
            let err = unexpected("SendMessage", other);
            // The daemon looks for a task member or a chat: say when the
            // name was a shell's.
            return Err(match client.resolve_target(target).await {
                Ok(Target::Shell(shell)) => {
                    crate::shell::misnamed(&shell, "a task member or a chat")
                }
                _ => err,
            });
        }
    };
    let status = match pending {
        Some(_) if delivered_soon(&mut client, &message.id).await => None,
        pending => pending,
    };
    let status = match status {
        None => format!("Delivered to '{}' ({}).", message.to_label, message.id),
        Some(why) => format!(
            "Sent to '{}' ({}), not delivered yet: {why}.",
            message.to_label, message.id
        ),
    };

    let Some(timeout_secs) = wait else {
        println!("{status}");
        return Ok(());
    };
    // The reply alone goes to stdout.
    eprintln!("{status}");
    let mut client = Client::connect().await?;
    let request = Request::WaitReply {
        id: message.id.clone(),
        timeout_secs,
    };
    match client.send(request).await? {
        Response::Reply {
            message: reply,
            text,
        } => {
            eprintln!("{}", reply_header(&message, reply.as_deref()));
            println!("{}", text.trim_end());
            Ok(())
        }
        other => Err(unexpected("WaitReply", other)),
    }
}

/// Who replied to `sent`: with a reply message, how to answer it (a reply
/// sent with `--wait` is answered by the end of this turn too).
fn reply_header(sent: &MessageInfo, reply: Option<&MessageInfo>) -> String {
    match reply {
        Some(m) => format!(
            "Reply {} from '{}' (to answer it, run: tam msg {} \"<reply>\"):",
            m.id, m.from_label, m.from_label
        ),
        None => format!(
            "Reply from '{}' (its response to {}):",
            sent.to_label, sent.id
        ),
    }
}

/// Wait a little for the delivery of message `id` (on a subscribed client,
/// dropped after: reads it cancels are not resumable).
async fn delivered_soon(client: &mut Client, id: &str) -> bool {
    let delivered = async {
        loop {
            match client.read_message().await {
                Ok(ServerMessage::Event(Event::MessageDelivered { message }))
                    if message.id == id =>
                {
                    return true
                }
                Ok(_) => continue,
                Err(_) => return false,
            }
        }
    };
    tokio::time::timeout(DELIVERY_WAIT, delivered)
        .await
        .unwrap_or(false)
}

/// `tam history`: the agent runs and messages of a task (or of a chat),
/// oldest first.
pub async fn history(task: Option<String>, json: bool) -> Result<()> {
    let mut client = Client::connect().await?;
    let of = match task {
        Some(name) => match client.get_task(&name).await {
            Ok((task, runs)) => History::Task(task, runs),
            Err(e) if script::kind_of(&e) == ErrorKind::Daemon => return Err(e),
            // A member's task, or a chat.
            Err(_) => match client.resolve_target(&name).await? {
                Target::Member(task, ..) => {
                    let (task, runs) = client.get_task(&task.name).await?;
                    History::Task(task, runs)
                }
                Target::Chat(chat, runs) => History::Chat(chat, runs),
                Target::Shell(shell) => {
                    return Err(crate::shell::misnamed(&shell, "a task or a chat"))
                }
            },
        },
        None => {
            let cwd = std::fs::canonicalize(".")?;
            let name = client
                .resolve_member(None, Some(&cwd))
                .await
                .map_err(|_| anyhow!("no task in current directory"))?
                .0
                .name;
            let (task, runs) = client.get_task(&name).await?;
            History::Task(task, runs)
        }
    };
    let (key, name, runs) = match &of {
        History::Task(task, runs) => ("task", task.name.clone(), runs),
        History::Chat(chat, runs) => ("chat", chat.id.clone(), runs),
    };
    let messages = match client
        .send(Request::ListMessages {
            task: Some(name.clone()),
        })
        .await?
    {
        Response::Messages { messages } => messages,
        other => return Err(unexpected("ListMessages", other)),
    };

    if json {
        let out = serde_json::json!({
            key: name,
            "runs": runs,
            "messages": messages,
        });
        println!("{}", serde_json::to_string_pretty(&out)?);
        return Ok(());
    }
    let label = |id: &str| match &of {
        History::Task(task, _) => match task.member(id) {
            Some(m) => task.member_label(m),
            None => id.to_string(),
        },
        History::Chat(chat, _) if chat.id == id => chat.label(),
        History::Chat(..) => id.to_string(),
    };
    let lines = history_lines(&label, runs, &messages);
    if lines.is_empty() {
        match &of {
            History::Task(task, _) => println!("No runs or messages in task '{}'.", task.name),
            History::Chat(chat, _) => {
                println!(
                    "No runs or messages in chat {}.",
                    crate::chat::describe(chat)
                )
            }
        }
    }
    for (at, line) in lines {
        println!("{:>5}  {line}", crate::format_age(Some(at)));
    }
    Ok(())
}

/// Whose history `tam history` shows, with its runs.
enum History {
    Task(TaskInfo, Vec<RunInfo>),
    Chat(ChatInfo, Vec<RunInfo>),
}

/// One line per run start, run end and message, with its timestamp, in
/// time order. `label` names a run's member (or chat).
fn history_lines(
    label: &dyn Fn(&str) -> String,
    runs: &[RunInfo],
    messages: &[MessageInfo],
) -> Vec<(u64, String)> {
    let member = label;
    let mut lines = Vec::new();
    for run in runs {
        let session = run
            .session_id
            .as_deref()
            .map(|s| format!(", session {s}"))
            .unwrap_or_default();
        lines.push((
            run.started_at,
            format!("{}  {} started{session}", member(&run.member), run.provider),
        ));
        if let Some(ended) = run.ended_at {
            let exit = run
                .exit_code
                .map(|c| format!(" (exit {c})"))
                .unwrap_or_default();
            lines.push((
                ended,
                format!("{}  {} ended{exit}", member(&run.member), run.provider),
            ));
        }
    }
    for m in messages {
        let state = if m.delivered_at.is_some() {
            "delivered"
        } else {
            "queued"
        };
        lines.push((
            m.sent_at,
            format!(
                "{} → {}  {}  {state}  {}",
                m.from_label,
                m.to_label,
                m.id,
                excerpt(&m.text)
            ),
        ));
    }
    lines.sort_by_key(|(at, _)| *at);
    lines
}

/// The message on one line, cut to [`EXCERPT_LEN`] characters.
fn excerpt(text: &str) -> String {
    let line = text.split_whitespace().collect::<Vec<_>>().join(" ");
    match line.char_indices().nth(EXCERPT_LEN) {
        Some((cut, _)) => format!("{}…", &line[..cut]),
        None => line,
    }
}

/// `tam inbox-wait`, run in the background by the inbox hooks tam injects
/// into Claude agents: wait for the agent's messages and print them on
/// stderr, exiting 2 so that Claude shows them to the model (waking it when
/// idle). Exits 0 quietly when there is nothing to deliver: no agent id, no
/// daemon (never starts one), or another inbox hook took over.
pub async fn inbox_wait(agent: Option<String>, socket: Option<PathBuf>) -> i32 {
    // The hook's JSON input is not needed: drain it on the side.
    let stdin = std::io::stdin();
    if !stdin.is_terminal() {
        std::thread::spawn(move || std::io::copy(&mut stdin.lock(), &mut std::io::sink()));
    }
    let Some(agent_id) = agent else {
        return 0;
    };
    let socket = socket.unwrap_or_else(tam_proto::default_socket_path);
    let Ok(Some(mut client)) = Client::try_connect_at(&socket).await else {
        return 0;
    };
    match client.send(Request::WaitInbox { agent_id }).await {
        Ok(Response::Inbox { text, .. }) => {
            eprintln!("{text}");
            2
        }
        _ => 0,
    }
}

fn unexpected(request: &str, response: Response) -> anyhow::Error {
    match response {
        Response::Error { message } => anyhow!("{message}"),
        other => anyhow!("unexpected response to {request}: {other:?}"),
    }
}

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

    fn task() -> TaskInfo {
        let member = |id: &str, name: &str| MemberInfo {
            id: id.into(),
            name: name.into(),
            repo: name.into(),
            repo_root: None,
            branch: "feat".into(),
            dir: PathBuf::from("/w").join(name),
            health: None,
        };
        TaskInfo {
            name: "feat".into(),
            members: vec![member("m-1", "api"), member("m-2", "web")],
            created_at: 0,
            run_count: 1,
            last_activity: None,
        }
    }

    #[test]
    fn history_interleaves_runs_and_messages() {
        let runs = vec![RunInfo {
            member: "m-1".into(),
            provider: "claude".into(),
            session_id: Some("s1".into()),
            started_at: 10,
            ended_at: Some(40),
            exit_code: Some(0),
        }];
        let messages = vec![MessageInfo {
            id: "msg-1".into(),
            from: None,
            from_label: "user".into(),
            to: "m-1".into(),
            to_label: "feat/api".into(),
            text: "please\nrun the tests".into(),
            hop: 0,
            reply_to: None,
            sent_at: 20,
            delivered_at: Some(21),
        }];
        let task = task();
        let label = |id: &str| {
            task.member(id)
                .map_or(id.to_string(), |m| task.member_label(m))
        };
        let lines = history_lines(&label, &runs, &messages);
        let lines: Vec<&str> = lines.iter().map(|(_, l)| l.as_str()).collect();
        assert_eq!(
            lines,
            [
                "feat/api  claude started, session s1",
                "user → feat/api  msg-1  delivered  please run the tests",
                "feat/api  claude ended (exit 0)",
            ]
        );
    }

    #[test]
    fn reply_headers_name_who_replied() {
        let sent = MessageInfo {
            id: "msg-1".into(),
            from: None,
            from_label: "user".into(),
            to: "m-2".into(),
            to_label: "feat/web".into(),
            text: "?".into(),
            hop: 0,
            reply_to: None,
            sent_at: 0,
            delivered_at: None,
        };
        assert_eq!(
            reply_header(&sent, None),
            "Reply from 'feat/web' (its response to msg-1):"
        );
        let reply = MessageInfo {
            id: "msg-2".into(),
            from: Some("m-2".into()),
            from_label: "feat/web".into(),
            reply_to: Some("msg-1".into()),
            ..sent.clone()
        };
        assert_eq!(
            reply_header(&sent, Some(&reply)),
            "Reply msg-2 from 'feat/web' (to answer it, run: tam msg feat/web \"<reply>\"):"
        );
    }

    #[test]
    fn excerpts_are_one_capped_line() {
        assert_eq!(excerpt("a\n  b"), "a b");
        let long = excerpt(&"é".repeat(100));
        assert_eq!(long.chars().count(), EXCERPT_LEN + 1);
        assert!(long.ends_with('…'));
    }
}