tam-cli 0.6.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
use std::path::PathBuf;

use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(
    name = "tam",
    about = "Terminal agent multiplexer — manage units of work, not just processes"
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Commands>,
}

#[derive(Subcommand)]
pub enum Commands {
    /// Create a new task: a worktree on branch NAME in the current repository,
    /// or in each --repo, and start an agent in each
    ///
    /// All or nothing: if one worktree cannot be created, those created
    /// before it are removed again. With several repositories, the agent of
    /// the first one is attached; name the others as NAME/<repo>.
    New {
        /// Task name, also the name of its branch
        name: String,

        /// Branch from a specific ref (default: the repository's default branch)
        #[arg(short, long)]
        source: Option<String>,

        /// Don't start an agent after creating the task
        #[arg(long)]
        no_start: bool,

        /// Start the agent on this prompt (each agent, with several
        /// repositories)
        #[arg(short, long, value_name = "TEXT", conflicts_with = "no_start")]
        prompt: Option<String>,

        /// Don't attach to the agent (never attaches without a terminal)
        #[arg(short, long)]
        detach: bool,

        /// Print a JSON receipt instead of attaching (see README, Scripting)
        #[arg(long)]
        json: bool,

        /// Repository to create a worktree in (repeatable; default: the
        /// current one): a path to it, or its name as `tam ls` shows it
        #[arg(short, long = "repo", value_name = "REPO")]
        repo: Vec<String>,

        /// Add the worktrees to the existing task NAME instead
        #[arg(long)]
        join: bool,

        /// What the task's members work from: a URL, a ticket id or a path
        /// (expanded by [[brief.patterns]] in the config; default: the file
        /// brief.md in the task's directory, see tam brief)
        #[arg(long, value_name = "REF", conflicts_with = "join")]
        brief: Option<String>,
    },

    /// Start an agent in a task (or a chat), resuming its current session
    ///
    /// The current session is the last one the agent had a turn in. A chat
    /// always resumes its own: --new-session and --session are for tasks.
    Run {
        /// Task name, TASK/MEMBER, or a chat's id or name (resolved from
        /// current directory if omitted)
        name: Option<String>,

        /// Start a new session (it becomes current once you use it; not for
        /// a chat)
        #[arg(long, conflicts_with = "session")]
        new_session: bool,

        /// Resume this agent session instead of the current one (not for a
        /// chat: tam chat --session makes another chat)
        #[arg(long, value_name = "ID")]
        session: Option<String>,

        /// Initial prompt text (same as --prompt)
        prompt: Option<String>,

        /// Extra arguments passed to the agent command
        #[arg(last = true)]
        args: Vec<String>,

        /// Start the agent on this prompt
        #[arg(
            short = 'p',
            long = "prompt",
            value_name = "TEXT",
            conflicts_with = "prompt"
        )]
        prompt_option: Option<String>,

        /// Don't attach to the agent (never attaches without a terminal)
        #[arg(short, long)]
        detach: bool,

        /// Print a JSON receipt instead of attaching (see README, Scripting)
        #[arg(long)]
        json: bool,
    },

    /// Stop the agents of a task (or of one member: TASK/MEMBER), or a
    /// chat's agent (the chat stays: tam run resumes it); or close a shell
    ///
    /// Closing a shell hangs it up: what runs in it stops. Nothing on disk
    /// changes.
    Stop {
        /// Task name, TASK/MEMBER, a chat's id or name, or a shell's id
        /// (resolved from current directory if omitted)
        name: Option<String>,
    },

    /// Attach to a running agent, or to a shell
    Attach {
        /// Task name, TASK/MEMBER, a chat's id or name, or a shell's id
        /// (resolved from current directory if omitted)
        name: Option<String>,
    },

    /// Stop a task's agents, remove its worktrees and forget it
    ///
    /// Refuses when uncommitted changes would be lost, or with -b commits no
    /// other branch holds, unless confirmed or forced. TASK/MEMBER drops one
    /// member alone (its agent, its worktree); the last one takes the task.
    Drop {
        /// Task name, or TASK/MEMBER
        name: String,

        /// Also delete the worktrees' git branches
        #[arg(short, long)]
        branch: bool,

        /// Drop even if uncommitted changes or unmerged commits are lost
        #[arg(short, long)]
        force: bool,
    },

    /// Make a worktree created outside tam a task (or a member of one)
    ///
    /// The task owns the worktree from then on: dropping it removes the
    /// worktree, as for any task.
    Adopt {
        /// The worktree, or a directory in it (default: the current directory)
        path: Option<PathBuf>,

        /// Task name (default: the worktree's branch)
        #[arg(short, long, conflicts_with = "join")]
        name: Option<String>,

        /// Add the worktree to this existing task instead
        #[arg(long)]
        join: Option<String>,
    },

    /// Recreate the worktree of a task (or of one member: TASK/MEMBER) that
    /// was deleted outside tam, on its branch
    Repair {
        /// Task name, or TASK/MEMBER
        name: String,
    },

    /// List chats, shells and tasks, the tasks with computed status
    Ps {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Discover projects and worktrees
    Ls {
        /// Directory to search (default: discovery roots)
        path: Option<PathBuf>,

        /// Output as JSON
        #[arg(long)]
        json: bool,

        /// Output paths only, one per line
        #[arg(long)]
        raw: bool,

        /// Output pretty names only, one per line (same format as `tam pick`)
        #[arg(long)]
        porcelain: bool,
    },

    /// Fuzzy project picker
    Pick {
        /// Finder command (overrides config finder, e.g. fzf, "dmenu -l 20")
        #[arg(short = 'F', long)]
        finder: Option<String>,
    },

    /// Remove the agent hooks older versions of tam installed globally
    /// (hooks are now injected into each agent tam spawns)
    Init {
        /// Agent to clean up
        #[arg(long, default_value = "claude")]
        agent: String,
    },

    /// Stop all agents and shut down the daemon
    Shutdown,

    /// Check if the daemon is running
    Status,

    /// Run the daemon (used internally by auto-start)
    #[command(hide = true)]
    Daemon,

    /// Forward a hook event to the daemon (run by the hooks tam injects into
    /// the agents it spawns). Reads the hook's JSON input on stdin.
    #[command(hide = true)]
    HookNotify {
        /// Agent ID (defaults to $TAM_AGENT_ID; exits quietly if absent)
        #[arg(short, long, env = "TAM_AGENT_ID")]
        agent: Option<String>,

        /// Daemon socket (defaults to $TAM_SOCKET, set by the daemon that
        /// spawned the agent)
        #[arg(long, env = "TAM_SOCKET")]
        socket: Option<std::path::PathBuf>,

        /// Legacy event name, passed by the hooks older `tam init` wrote to
        /// the global Claude settings. Such calls are ignored: hooks are
        /// injected per spawn now.
        #[arg(short, long)]
        event: Option<String>,
    },

    /// Send a message to the agent of a task member (or of a chat)
    ///
    /// The agent gets it right away: when idle it wakes up, when busy it
    /// reads it at its next step. A member with no running agent gets it
    /// when its next agent starts. Sent from an agent tam started, it comes
    /// from that agent's member (or chat); else from the user.
    Msg {
        /// Recipient: task name, TASK/MEMBER or member id; from an agent,
        /// also the short name of another member of its task (see tam
        /// peers); then a chat's id or name
        target: String,

        /// Message text (read from stdin when omitted)
        text: Vec<String>,

        /// Wait for the reply and print it: the recipient's next message back,
        /// or its response at the end of the turn that got the message
        #[arg(short, long)]
        wait: bool,

        /// How long --wait waits for the reply, in seconds
        #[arg(long, default_value_t = 90, requires = "wait")]
        timeout: u64,

        /// Agent id of the sender (set by tam in the agents it starts)
        #[arg(long, env = "TAM_AGENT_ID", hide = true)]
        from: Option<String>,
    },

    /// Show a task's (or a chat's) history: its agent runs and messages
    History {
        /// Task name, or a chat's id or name (resolved from current
        /// directory if omitted)
        task: Option<String>,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Wait for the agent's messages and print them (run in the background
    /// by the inbox hooks tam injects into the Claude agents it spawns)
    #[command(hide = true)]
    InboxWait {
        /// Agent ID (defaults to $TAM_AGENT_ID; exits quietly if absent)
        #[arg(short, long, env = "TAM_AGENT_ID")]
        agent: Option<String>,

        /// Daemon socket (defaults to $TAM_SOCKET)
        #[arg(long, env = "TAM_SOCKET")]
        socket: Option<PathBuf>,
    },

    /// List the other members of your task, with what their agents are doing
    ///
    /// Each member of a task is a worktree of one repository with its own
    /// agent. Run from an agent tam started, or from a task's worktree. NAME
    /// is how to name a member to tam msg from there.
    Peers {
        /// List the members of every task
        #[arg(short, long)]
        all: bool,

        /// Output as JSON
        #[arg(long)]
        json: bool,

        /// Agent id of the caller (set by tam in the agents it starts)
        #[arg(long, env = "TAM_AGENT_ID", hide = true)]
        from: Option<String>,
    },

    /// Wait until the agents of a task (or of one member, or a chat's) stop
    /// working
    ///
    /// Done once each agent is in one of the --until states (default: any
    /// but working; ready also takes idle, the state of agents without
    /// hooks). Gives up at once when an agent exits or its turn fails,
    /// unless --until takes exit or error. Exit status: 0 done, 1 gave up,
    /// 3 no such task or member, 4 timed out, 5 daemon unreachable.
    Wait {
        /// Task name (all its members), TASK/MEMBER, member id, or a chat's
        /// id or name (resolved from current directory if omitted)
        name: Option<String>,

        /// States to wait for, comma-separated
        #[arg(short, long, value_enum, value_delimiter = ',', value_name = "STATE")]
        until: Vec<Until>,

        /// Give up after this long: 90 (seconds), 90s, 15m, 2h (default: no
        /// limit)
        #[arg(short, long, value_name = "DURATION", value_parser = parse_duration)]
        timeout: Option<std::time::Duration>,

        /// Output as JSON (see README, Scripting)
        #[arg(long)]
        json: bool,
    },

    /// Print the last response of a task's agents (or of one member's, or
    /// a chat's)
    ///
    /// The text that ended the agent's last turn, in full, kept once the
    /// agent exits. There is none while a turn runs, before the first one
    /// ends, after a failed or interrupted turn, and for agents that report
    /// none (only Claude reports them). Exit status: 0 printed, 1 no
    /// response, 3 no such task or member, 5 daemon unreachable.
    Result {
        /// Task name (all its members), TASK/MEMBER, member id, or a chat's
        /// id or name (resolved from current directory if omitted)
        name: Option<String>,

        /// Output as JSON (see README, Scripting)
        #[arg(long)]
        json: bool,
    },

    /// Show or change a task's brief: what its members work from
    ///
    /// The brief is a reference you give (a URL, a ticket id, a path: tam
    /// neither fetches nor reads it), else the file brief.md in the task's
    /// directory, next to scratch/, a directory its members share for
    /// transient notes. The members' agents are told at session start (a
    /// change too: at their next one), and tam peers shows it. Dropping the
    /// task removes the directory; a brief.md its members wrote in is kept.
    Brief {
        /// Task name, TASK/MEMBER or member id (default: the task of the
        /// agent tam started, else of the current directory)
        name: Option<String>,

        /// Make REF the brief (expanded by [[brief.patterns]] in the config)
        #[arg(short, long, value_name = "REF", conflicts_with = "local")]
        set: Option<String>,

        /// Make the file brief.md the brief again
        #[arg(long)]
        local: bool,

        /// Output as JSON (see README, Scripting)
        #[arg(long)]
        json: bool,

        /// Agent id of the caller (set by tam in the agents it starts)
        #[arg(long, env = "TAM_AGENT_ID", hide = true)]
        from: Option<String>,
    },

    /// Merge tasks: the members of each FROM task join task INTO, and the
    /// FROM tasks are gone. Worktrees, branches and running agents stay as
    /// they are; Claude agents learn of their new task and peers at their
    /// next prompt or tool call. Exit status: 0 merged, 1 refused, 3 no
    /// such task, 5 daemon unreachable.
    Merge {
        /// The task that stays, with its name
        into: String,

        /// The tasks merged into it
        #[arg(required = true)]
        from: Vec<String>,

        /// Output as JSON (see README, Scripting)
        #[arg(long)]
        json: bool,
    },

    /// Split a member out of its task into a task of its own (the inverse
    /// of merge). Its worktree, branch and running agent stay as they are.
    /// Exit status: 0 split, 1 refused (e.g. a task's only member), 3 no
    /// such member, 5 daemon unreachable.
    Split {
        /// TASK/MEMBER or member id
        member: String,

        /// Name of the new task (default: the member's branch, unless a task
        /// has that name)
        name: Option<String>,

        /// Output as JSON (see README, Scripting)
        #[arg(long)]
        json: bool,
    },

    /// Start a chat: a Claude session on a repository's main checkout, then
    /// attach
    ///
    /// A chat owns nothing on disk (no worktree, no branch); several can run
    /// on one repository. The commands that take a task take a chat too
    /// (attach, stop, run, msg, wait, result, history), by its id or its
    /// name: a task of that name comes first. tam ps lists chats.
    Chat {
        /// Repository: a path to it (or to any directory in it), or its name
        /// as tam ls shows it (default: the current one, from a worktree its
        /// main checkout)
        repo: Option<String>,

        /// Name the chat (unique among chats, and no task's name; default:
        /// shown by its Claude title, else its id)
        #[arg(short, long)]
        name: Option<String>,

        /// Resume this existing Claude session of the repository instead of
        /// starting a new one
        #[arg(long, value_name = "ID")]
        session: Option<String>,

        /// Keep the chat: never closed for want of turns
        #[arg(long)]
        keep: bool,

        /// Start the agent on this prompt
        #[arg(short, long, value_name = "TEXT")]
        prompt: Option<String>,

        /// Don't attach to the agent (never attaches without a terminal)
        #[arg(short, long)]
        detach: bool,

        /// Print a JSON receipt instead of attaching (see README, Scripting)
        #[arg(long)]
        json: bool,
    },

    /// Close a chat: stop its agent and forget the chat
    ///
    /// Asks first, on the terminal. Nothing on disk changes: Claude keeps
    /// the session, and tam chat REPO --session ID makes the chat again
    /// (tam close prints it). Exit status: 0 closed, 1 not confirmed or
    /// refused, 3 no such chat, 5 daemon unreachable.
    Close {
        /// The chat's id or name
        chat: String,

        /// Close without asking (needed without a terminal)
        #[arg(short, long)]
        yes: bool,

        /// Output as JSON (see README, Scripting)
        #[arg(long)]
        json: bool,
    },

    /// Name a chat, or take its name away
    Rename {
        /// The chat's id or name
        chat: String,

        /// Its new name, unique among chats and no task's name (none: the
        /// chat is shown by its Claude title, else its id, again)
        name: Option<String>,

        /// Output as JSON (see README, Scripting)
        #[arg(long)]
        json: bool,
    },

    /// Keep a chat: never closed for want of turns
    Keep {
        /// The chat's id or name
        chat: String,

        /// Stop keeping it
        #[arg(long)]
        off: bool,

        /// Output as JSON (see README, Scripting)
        #[arg(long)]
        json: bool,
    },

    /// Open a shell in a task's worktree or on a repository's main
    /// checkout, then attach
    ///
    /// Your $SHELL, run by the daemon in your environment, through [spawn]
    /// wrapper: it keeps running once you detach (ctrl-]), until it exits
    /// or tam stop closes it; dropping its task closes it too. It is named
    /// by its id (tam attach ID, tam stop ID), and tam ps lists it.
    Shell {
        /// Task name, TASK/MEMBER or member id (its worktree), or a chat's
        /// id or name (its main checkout). Default: the task whose worktree
        /// holds the current directory, else the main checkout holding it
        #[arg(conflicts_with = "repo")]
        target: Option<String>,

        /// Open it on a repository's main checkout instead: a path to it (or
        /// to any directory in it), or its name as tam ls shows it
        #[arg(short, long, value_name = "REPO")]
        repo: Option<String>,

        /// Don't attach: print its id (never attaches without a terminal)
        #[arg(short, long)]
        detach: bool,

        /// Print a JSON receipt instead of attaching (see README, Scripting)
        #[arg(long)]
        json: bool,
    },
}

/// A state `tam wait` waits for: an agent state, or `exit` (no agent runs).
#[derive(Clone, Copy, Debug, PartialEq, Eq, clap::ValueEnum)]
pub enum Until {
    Working,
    Blocked,
    /// Waits for a prompt (`input` is its former name).
    #[value(alias = "input")]
    Ready,
    Idle,
    Question,
    Error,
    Exit,
}

/// A duration: whole seconds (`90`), or a number followed by `s`, `m` or `h`.
fn parse_duration(s: &str) -> Result<std::time::Duration, String> {
    let invalid = || format!("invalid duration '{s}': use e.g. 90, 90s, 15m or 2h");
    let s = s.trim();
    let split = s.find(|c: char| !c.is_ascii_digit()).unwrap_or(s.len());
    let (number, unit) = s.split_at(split);
    let n: u64 = number.parse().map_err(|_| invalid())?;
    let scale = match unit {
        "" | "s" => 1,
        "m" => 60,
        "h" => 3600,
        _ => return Err(invalid()),
    };
    let secs = n.checked_mul(scale).ok_or_else(invalid)?;
    Ok(std::time::Duration::from_secs(secs))
}