apm-cli 0.1.24

CLI project manager for running AI coding agents in parallel, isolated by design.
Documentation
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
use anyhow::Result;
use std::io::IsTerminal;
use std::path::Path;
use crate::ctx::CmdContext;
use apm_core::epic::{branch_to_title, epic_id_from_branch};

pub fn run_list(root: &Path) -> Result<()> {
    let ctx = CmdContext::load(root, false)?;

    let epic_branches = apm_core::epic::epic_branches(root)?;
    if epic_branches.is_empty() {
        return Ok(());
    }

    let tickets = ctx.tickets;

    for branch in &epic_branches {
        let id = epic_id_from_branch(branch);
        let title = branch_to_title(branch);

        // Find tickets belonging to this epic.
        let epic_tickets: Vec<_> = tickets
            .iter()
            .filter(|t| t.frontmatter.epic.as_deref() == Some(id))
            .collect();

        // Collect StateConfig references for each ticket (skip unknown states).
        let state_configs: Vec<&apm_core::config::StateConfig> = epic_tickets
            .iter()
            .filter_map(|t| ctx.config.workflow.states.iter().find(|s| s.id == t.frontmatter.state))
            .collect();

        let derived = apm_core::epic::derive_epic_state(&state_configs);

        // Build per-state counts (non-zero only).
        let mut counts: std::collections::BTreeMap<String, usize> = std::collections::BTreeMap::new();
        for t in &epic_tickets {
            *counts.entry(t.frontmatter.state.clone()).or_insert(0) += 1;
        }
        let counts_str: String = counts
            .iter()
            .filter(|(_, &v)| v > 0)
            .map(|(k, v)| format!("{v} {k}"))
            .collect::<Vec<_>>()
            .join(", ");

        println!("{id:<8} [{derived:<12}] {title:<40} {counts_str}");
    }

    Ok(())
}

pub fn run_new(root: &Path, title: String) -> Result<()> {
    let config = apm_core::config::Config::load(root)?;
    let branch = apm_core::epic::create(root, &title, &config)?;
    println!("{branch}");
    Ok(())
}

pub fn run_close(root: &Path, id_arg: &str) -> Result<()> {
    let config = CmdContext::load_config_only(root)?;

    // 1. Resolve the epic branch from the id prefix.
    let matches = apm_core::epic::find_epic_branches(root, id_arg);
    let epic_branch = match matches.len() {
        0 => anyhow::bail!("no epic branch found matching '{id_arg}'"),
        1 => matches.into_iter().next().unwrap(),
        _ => anyhow::bail!(
            "ambiguous id '{id_arg}': matches {}\n  {}",
            matches.len(),
            matches.join("\n  ")
        ),
    };

    // 2. Parse the 8-char epic ID from the branch name: epic/<id>-<slug>
    let epic_id = epic_id_from_branch(&epic_branch);

    // 3. Quiescence check: no ticket may be in an active state or have a live worker.
    let worktrees = apm_core::worktree::list_ticket_worktrees(root)?;
    let blockers = apm_core::epic::epic_is_quiescent(root, epic_id, &config, &worktrees)?;
    if !blockers.is_empty() {
        anyhow::bail!(
            "cannot close epic: the following tickets are not quiescent:\n{}",
            blockers.join("\n")
        );
    }

    // 4. Derive a human-readable title from the branch name.
    let pr_title = branch_to_title(&epic_branch);

    // 5. Check whether the epic branch is already fully merged into default.
    let default_branch = &config.project.default_branch;
    let ahead = std::process::Command::new("git")
        .current_dir(root)
        .args(["log", "--oneline", &format!("{default_branch}..{epic_branch}")])
        .output()?;
    if String::from_utf8_lossy(&ahead.stdout).trim().is_empty() {
        // Branch has no commits ahead of default — already merged.
        println!("epic/{epic_id} is already merged into {default_branch}; skipping PR");
        delete_epic_branch(root, &epic_branch)?;
        return Ok(());
    }

    // 6. Push the epic branch and create or reuse an open PR.
    apm_core::git::push_branch_tracking(root, &epic_branch)?;
    let mut messages = vec![];
    apm_core::github::gh_pr_create_or_update(
        root,
        &epic_branch,
        default_branch,
        epic_id,
        &pr_title,
        &format!("Epic: {epic_branch}"),
        &mut messages,
    )?;
    for m in &messages {
        println!("{m}");
    }
    Ok(())
}

pub fn run_refresh_epic(root: &Path, id_arg: &str) -> Result<()> {
    let config = CmdContext::load_config_only(root)?;

    let matches = apm_core::epic::find_epic_branches(root, id_arg);
    let epic_branch = match matches.len() {
        0 => anyhow::bail!("no epic branch found matching '{id_arg}'"),
        1 => matches.into_iter().next().unwrap(),
        _ => anyhow::bail!(
            "ambiguous id '{id_arg}': matches {}\n  {}",
            matches.len(),
            matches.join("\n  ")
        ),
    };

    let epic_id = epic_id_from_branch(&epic_branch);

    let worktrees = apm_core::worktree::list_ticket_worktrees(root)?;
    let blockers = apm_core::epic::epic_is_quiescent(root, epic_id, &config, &worktrees)?;
    if !blockers.is_empty() {
        anyhow::bail!(
            "cannot refresh epic: the following tickets are not quiescent:\n{}",
            blockers.join("\n")
        );
    }

    let default_branch = &config.project.default_branch;

    let log_out = std::process::Command::new("git")
        .current_dir(root)
        .args(["log", "--oneline", "--no-decorate", &format!("{epic_branch}..{default_branch}")])
        .output()?;
    if !log_out.status.success() {
        anyhow::bail!("git log failed: {}", String::from_utf8_lossy(&log_out.stderr).trim());
    }
    let log_str = String::from_utf8_lossy(&log_out.stdout);
    let log_str = log_str.trim();

    if log_str.is_empty() {
        println!("epic branch is up to date with {default_branch}");
        return Ok(());
    }

    let pr_title = format!("{epic_id}: refresh from {default_branch}");
    let pr_body = log_str.to_string();

    apm_core::git::push_branch_tracking(root, &epic_branch)?;

    let mut messages = vec![];
    apm_core::github::gh_pr_create_or_update_between(
        root,
        default_branch,
        &epic_branch,
        &pr_title,
        &pr_body,
        &mut messages,
    )?;
    for m in &messages {
        println!("{m}");
    }
    Ok(())
}

pub fn run_show(root: &std::path::Path, id_arg: &str, no_aggressive: bool) -> anyhow::Result<()> {
    let ctx = CmdContext::load(root, no_aggressive)?;

    let matches = apm_core::epic::find_epic_branches(root, id_arg);
    let branch = match matches.len() {
        0 => anyhow::bail!("no epic matching '{id_arg}'"),
        1 => matches.into_iter().next().unwrap(),
        _ => anyhow::bail!(
            "ambiguous prefix '{id_arg}', matches:\n  {}",
            matches.join("\n  ")
        ),
    };

    let epic_id = epic_id_from_branch(&branch);
    let title = branch_to_title(&branch);

    let epic_tickets: Vec<_> = ctx.tickets
        .iter()
        .filter(|t| t.frontmatter.epic.as_deref() == Some(epic_id))
        .collect();

    let state_configs: Vec<&apm_core::config::StateConfig> = epic_tickets
        .iter()
        .filter_map(|t| ctx.config.workflow.states.iter().find(|s| s.id == t.frontmatter.state))
        .collect();

    let derived = apm_core::epic::derive_epic_state(&state_configs);

    println!("Epic:   {title}");
    println!("Branch: {branch}");
    println!("State:  {derived}");

    if epic_tickets.is_empty() {
        println!();
        println!("(no tickets)");
        return Ok(());
    }

    // Column widths
    let id_w = 8usize;
    let state_w = 13usize;
    let title_w = 32usize;

    println!();
    println!(
        "{:<id_w$}  {:<state_w$}  {:<title_w$}  {}",
        "ID", "State", "Title", "Depends on"
    );
    println!(
        "{:-<id_w$}  {:-<state_w$}  {:-<title_w$}  {}",
        "", "", "", "----------"
    );

    for t in &epic_tickets {
        let fm = &t.frontmatter;
        let deps = fm
            .depends_on
            .as_deref()
            .map(|d| d.join(", "))
            .unwrap_or_else(|| "-".to_string());
        println!(
            "{:<id_w$}  {:<state_w$}  {:<title_w$}  {}",
            fm.id, fm.state, fm.title, deps
        );
    }

    Ok(())
}

pub fn run_set(root: &std::path::Path, id_arg: &str, field: &str, value: &str) -> anyhow::Result<()> {
    if field != "owner" {
        anyhow::bail!("unknown field {field:?}; valid fields: owner");
    }

    // Validate the epic exists.
    let matches = apm_core::epic::find_epic_branches(root, id_arg);
    if matches.is_empty() {
        eprintln!("error: no epic branch found matching '{id_arg}'");
        std::process::exit(1);
    }
    if matches.len() > 1 {
        anyhow::bail!(
            "ambiguous id '{id_arg}': matches {}\n  {}",
            matches.len(),
            matches.join("\n  ")
        );
    }
    let branch = &matches[0];
    let epic_id = epic_id_from_branch(branch).to_string();

    let config = apm_core::config::Config::load(root)?;

    // Pre-flight: validate the new owner
    let local = apm_core::config::LocalConfig::load(root);
    apm_core::validate::validate_owner(&config, &local, value)?;

    let (changed, skipped) = apm_core::epic::set_epic_owner(root, &epic_id, value, &config)?;
    println!("updated {changed} ticket(s), skipped {skipped} terminal ticket(s)");
    Ok(())
}


fn delete_epic_branch(root: &Path, branch: &str) -> Result<()> {
    if let Some(wt_path) = apm_core::worktree::find_worktree_for_branch(root, branch) {
        apm_core::worktree::remove_worktree(root, &wt_path, false)?;
    }
    let del = std::process::Command::new("git")
        .current_dir(root)
        .args(["branch", "-d", branch])
        .output()?;
    if del.status.success() {
        println!("deleted local branch {branch}");
    } else {
        eprintln!("warning: could not delete local branch {branch}: {}", String::from_utf8_lossy(&del.stderr).trim());
    }
    let del_remote = std::process::Command::new("git")
        .current_dir(root)
        .args(["push", "origin", "--delete", branch])
        .output()?;
    if !del_remote.status.success() {
        let stderr = String::from_utf8_lossy(&del_remote.stderr);
        if !stderr.contains("remote ref does not exist") && !stderr.contains("error: unable to delete") {
            eprintln!("warning: could not delete remote branch {branch}: {}", stderr.trim());
        }
    }
    Ok(())
}

pub(crate) fn run_epic_clean(
    root: &Path,
    config: &apm_core::config::Config,
    dry_run: bool,
    yes: bool,
) -> Result<()> {
    // Get local epic branches.
    let local_output = std::process::Command::new("git")
        .current_dir(root)
        .args(["branch", "--list", "epic/*"])
        .output()?;

    let local_branches: Vec<String> = String::from_utf8_lossy(&local_output.stdout)
        .lines()
        .map(|l| l.trim().trim_start_matches(['*', '+']).trim().to_string())
        .filter(|l| !l.is_empty())
        .collect();

    // Load all tickets.
    let tickets = apm_core::ticket::load_all_from_git(root, &config.tickets.dir)?;

    // Find epic branches whose derived state is "done".
    let mut candidates: Vec<String> = Vec::new();
    for branch in &local_branches {
        let id = apm_core::epic::epic_id_from_branch(branch);

        let epic_tickets: Vec<_> = tickets
            .iter()
            .filter(|t| t.frontmatter.epic.as_deref() == Some(id))
            .collect();

        let state_configs: Vec<&apm_core::config::StateConfig> = epic_tickets
            .iter()
            .filter_map(|t| config.workflow.states.iter().find(|s| s.id == t.frontmatter.state))
            .collect();

        if apm_core::epic::derive_epic_state(&state_configs) == "done" {
            candidates.push(branch.clone());
        }
    }

    if candidates.is_empty() {
        println!("Nothing to clean.");
        return Ok(());
    }

    // Print candidate list.
    println!("Would delete {} epic(s):", candidates.len());
    for branch in &candidates {
        let id = apm_core::epic::epic_id_from_branch(branch);
        let title = apm_core::epic::branch_to_title(branch);
        println!("  {id}  {title}");
    }

    if dry_run {
        println!("Dry run — no changes made.");
        return Ok(());
    }

    // Confirmation gate.
    if !yes {
        if std::io::stdout().is_terminal() {
            if !crate::util::prompt_yes_no(&format!("Delete {} epic(s)? [y/N] ", candidates.len()))? {
                println!("Aborted.");
                return Ok(());
            }
        } else {
            println!("Skipping — non-interactive terminal. Use --yes to confirm.");
            return Ok(());
        }
    }

    // Delete each candidate.
    for branch in &candidates {
        // Remove active worktree before attempting branch deletion.
        if let Some(wt_path) = apm_core::worktree::find_worktree_for_branch(root, branch) {
            if let Err(e) = apm_core::worktree::remove_worktree(root, &wt_path, false) {
                eprintln!(
                    "skipping {branch}: could not remove worktree at {}: {e}",
                    wt_path.display()
                );
                continue;
            }
        }

        // Delete local branch.
        let del_local = std::process::Command::new("git")
            .current_dir(root)
            .args(["branch", "-d", branch])
            .output()?;
        if !del_local.status.success() {
            eprintln!(
                "error: failed to delete local branch {branch}: {}",
                String::from_utf8_lossy(&del_local.stderr).trim()
            );
            continue;
        }

        // Delete remote branch; suppress "remote ref does not exist".
        let del_remote = std::process::Command::new("git")
            .current_dir(root)
            .args(["push", "origin", "--delete", branch])
            .output()?;
        if !del_remote.status.success() {
            let stderr = String::from_utf8_lossy(&del_remote.stderr);
            if !stderr.contains("remote ref does not exist")
                && !stderr.contains("error: unable to delete")
            {
                eprintln!(
                    "warning: failed to delete remote {branch}: {}",
                    stderr.trim()
                );
            }
        }

        println!("deleted {branch}");
    }

    Ok(())
}