apm-cli 0.1.42

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
use anyhow::Result;
use serde_json::Value;
use std::io::{self, BufRead, IsTerminal, Write};
use std::path::{Path, PathBuf};
use std::process::Command;

pub fn run(root: &Path, no_claude: bool, migrate: bool, with_docker: bool, quiet: bool, yes: bool) -> Result<()> {
    if migrate {
        let msgs = apm_core::init::migrate(root)?;
        for msg in msgs {
            println!("{msg}");
        }
        return Ok(());
    }

    let is_tty = std::io::stdin().is_terminal();
    let yes = yes || !is_tty;

    // Check if git_host is configured
    let has_git_host = {
        let config_path = root.join(".apm/config.toml");
        config_path.exists() && apm_core::config::Config::load(root)
            .map(|cfg| cfg.git_host.provider.is_some())
            .unwrap_or(false)
    };
    let local_toml = root.join(".apm/local.toml");

    let username = if !has_git_host && !local_toml.exists() && is_tty {
        let gh_default = apm_core::github::gh_username();
        prompt_username(gh_default.as_deref())?
    } else {
        String::new()
    };

    let default_name = root.file_name().and_then(|n| n.to_str()).unwrap_or("project").to_string();
    let (name, description) = if is_tty && !root.join(".apm/config.toml").exists() {
        prompt_project_info(&default_name)?
    } else {
        (String::new(), String::new())
    };

    let name_opt = if name.is_empty() { None } else { Some(name.as_str()) };
    let desc_opt = if description.is_empty() { None } else { Some(description.as_str()) };
    let user_opt = if username.is_empty() { None } else { Some(username.as_str()) };

    let workers_default = if no_claude { Some("debug/worker") } else { None };
    let setup_out = apm_core::init::setup(root, name_opt, desc_opt, user_opt, workers_default)?;
    for msg in &setup_out.messages {
        println!("{msg}");
    }

    if with_docker {
        let docker_out = apm_core::init::setup_docker(root)?;
        for msg in &docker_out.messages {
            if msg.is_empty() {
                println!();
            } else {
                println!("{msg}");
            }
        }
    }
    update_claude_settings(root, no_claude, yes)?;
    update_user_claude_settings(yes)?;
    warn_if_settings_untracked(root);
    println!("apm initialized.");
    if std::io::stdout().is_terminal() && !quiet {
        println!();
        println!("Next steps:");
        println!("  * Commit the config:   git add .apm/ && git commit -m 'chore: init apm'");
        println!("  * Create a ticket:     apm new");
        println!("  * Open the web UI:     apm-server");
        println!("  * Full CLI reference:  apm --help");
    }
    Ok(())
}

fn prompt_username(default: Option<&str>) -> Result<String> {
    let mut stdout = std::io::stdout();
    let stdin = std::io::stdin();
    match default {
        Some(d) => print!("Username [{}]: ", d),
        None => print!("Username []: "),
    }
    stdout.flush()?;
    let mut input = String::new();
    stdin.lock().read_line(&mut input)?;
    let trimmed = input.trim();
    if trimmed.is_empty() {
        Ok(default.unwrap_or("").to_string())
    } else {
        Ok(trimmed.to_string())
    }
}

fn prompt_project_info(default_name: &str) -> Result<(String, String)> {
    let mut stdout = std::io::stdout();
    let stdin = std::io::stdin();

    print!("Project name [{}]: ", default_name);
    stdout.flush()?;
    let mut name_input = String::new();
    stdin.lock().read_line(&mut name_input)?;
    let name = {
        let trimmed = name_input.trim();
        if trimmed.is_empty() {
            default_name.to_string()
        } else {
            trimmed.to_string()
        }
    };

    print!("Project description []: ");
    stdout.flush()?;
    let mut desc_input = String::new();
    stdin.lock().read_line(&mut desc_input)?;
    let description = desc_input.trim().to_string();

    Ok((name, description))
}

fn warn_if_settings_untracked(root: &Path) {
    let settings = root.join(".claude/settings.json");
    if !settings.exists() {
        return;
    }
    let tracked = Command::new("git")
        .args(["ls-files", "--error-unmatch", ".claude/settings.json"])
        .current_dir(root)
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);
    if !tracked {
        eprintln!(
            "Warning: .claude/settings.json exists but is not committed. \
Agent worktrees won't have it — run: git add .claude/settings.json && git commit"
        );
    }
}

const APM_ALLOW_ENTRIES: &[&str] = &[
    "Bash(apm sync*)",
    "Bash(apm next*)",
    "Bash(apm list*)",
    "Bash(apm show*)",
    "Bash(apm set *)",
    "Bash(apm state *)",
    "Bash(apm start *)",
    "Bash(apm spec *)",
    "Bash(apm agents*)",
    "Bash(apm _hook *)",
    "Bash(apm verify*)",
    "Bash(apm new *)",
    "Bash(apm worktrees*)",
    "Bash(apm help*)",
    "Bash(apm review *)",
    "Bash(apm close *)",
    "Bash(apm assign *)",
    "Bash(apm validate*)",
    "Bash(apm work *)",
    "Bash(apm move *)",
    "Bash(apm archive *)",
    "Bash(apm clean *)",
    "Bash(apm workers*)",
    "Bash(apm epic *)",
    "Bash(apm register *)",
    "Bash(apm sessions *)",
    "Bash(apm revoke *)",
    "Bash(apm version*)",
    "Bash(apm instructions*)",
    // code editing
    "Edit",
    "Write",
    // read-only tools
    "Read",
    "Glob",
    "Grep",
    // git ops in worktree
    "Bash(git -C *)",
    // read helpers
    "Bash(ls *)",
    "Bash(rg *)",
    "Bash(grep *)",
    "Bash(find *)",
    "Bash(cat *)",
    "Bash(head *)",
    "Bash(tail *)",
    "Bash(wc *)",
    "Bash(sort *)",
    "Bash(uniq *)",
    "Bash(diff *)",
    "Bash(which *)",
    // text manipulation
    "Bash(sed *)",
    "Bash(awk *)",
    // file ops (safe areas)
    "Bash(mv *)",
    "Bash(cp *)",
    "Bash(rm /tmp/*)",
    "Bash(mkdir -p /tmp/*)",
    // shell building blocks
    "Bash(echo *)",
    "Bash(test *)",
    "Bash(true)",
    "Bash(false)",
];

/// Entries added to ~/.claude/settings.json so subagents running in isolated
/// worktrees (which don't inherit project settings) can use git and apm.
const APM_USER_ALLOW_ENTRIES: &[&str] = &[
    "Bash(git add*)",
    "Bash(git commit*)",
    "Bash(git -C*)",
    "Bash(apm sync*)",
    "Bash(apm next*)",
    "Bash(apm list*)",
    "Bash(apm show*)",
    "Bash(apm set *)",
    "Bash(apm state *)",
    "Bash(apm start *)",
    "Bash(apm spec *)",
    "Bash(apm agents*)",
    "Bash(apm verify*)",
    "Bash(apm new *)",
    "Bash(apm worktrees*)",
    "Bash(apm help*)",
    "Bash(apm review *)",
    "Bash(apm close *)",
    "Bash(apm assign *)",
    "Bash(apm validate*)",
    "Bash(apm work *)",
    "Bash(apm move *)",
    "Bash(apm archive *)",
    "Bash(apm clean *)",
    "Bash(apm workers*)",
    "Bash(apm epic *)",
    "Bash(apm register *)",
    "Bash(apm sessions *)",
    "Bash(apm revoke *)",
    "Bash(apm version*)",
    "Bash(apm instructions*)",
    // code editing
    "Edit",
    "Write",
    // read-only tools
    "Read",
    "Glob",
    "Grep",
    // git ops in worktree
    "Bash(git -C *)",
    // read helpers
    "Bash(ls *)",
    "Bash(rg *)",
    "Bash(grep *)",
    "Bash(find *)",
    "Bash(cat *)",
    "Bash(head *)",
    "Bash(tail *)",
    "Bash(wc *)",
    "Bash(sort *)",
    "Bash(uniq *)",
    "Bash(diff *)",
    "Bash(which *)",
    // text manipulation
    "Bash(sed *)",
    "Bash(awk *)",
    // file ops (safe areas)
    "Bash(mv *)",
    "Bash(cp *)",
    "Bash(rm /tmp/*)",
    "Bash(mkdir -p /tmp/*)",
    // shell building blocks
    "Bash(echo *)",
    "Bash(test *)",
    "Bash(true)",
    "Bash(false)",
    // language toolchains (unconditional at user level)
    "Bash(cargo *)",
    "Bash(npm *)",
    "Bash(npx *)",
    "Bash(python3 *)",
];

fn update_settings_json(
    path: &Path,
    entries: &[&str],
    prompt_header: &str,
    prompt_confirm: &str,
    updated_msg: &str,
    create_if_missing: bool,
    yes: bool,
) -> Result<()> {
    let mut val: Value = if path.exists() {
        let raw = std::fs::read_to_string(path)?;
        serde_json::from_str(&raw).unwrap_or(Value::Object(Default::default()))
    } else if create_if_missing {
        Value::Object(Default::default())
    } else {
        return Ok(());
    };

    let allow = val.pointer_mut("/permissions/allow").and_then(|v| v.as_array_mut());
    let missing: Vec<&str> = if let Some(arr) = allow {
        entries.iter().filter(|&&e| !arr.iter().any(|v| v.as_str() == Some(e))).copied().collect()
    } else {
        entries.to_vec()
    };

    if missing.is_empty() {
        return Ok(());
    }

    if !yes {
        println!("{prompt_header}");
        for e in &missing {
            println!("  {e}");
        }
        print!("{prompt_confirm} [y/N] ");
        io::stdout().flush()?;

        let mut line = String::new();
        io::stdin().lock().read_line(&mut line)?;
        if !line.trim().eq_ignore_ascii_case("y") {
            println!("Skipped.");
            return Ok(());
        }
    }

    if val.pointer("/permissions/allow").is_none() {
        let perms = val
            .as_object_mut()
            .ok_or_else(|| anyhow::anyhow!("settings.json root is not an object"))?
            .entry("permissions")
            .or_insert_with(|| Value::Object(Default::default()));
        perms.as_object_mut().unwrap()
            .entry("allow")
            .or_insert_with(|| Value::Array(vec![]));
    }

    let arr = val.pointer_mut("/permissions/allow")
        .and_then(|v| v.as_array_mut())
        .unwrap();
    for e in missing {
        arr.push(Value::String(e.to_string()));
    }

    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let updated = serde_json::to_string_pretty(&val)?;
    std::fs::write(path, updated + "\n")?;
    println!("{updated_msg}");
    Ok(())
}

fn detected_toolchain_entries(root: &Path) -> Vec<&'static str> {
    let mut entries = Vec::new();
    if root.join("Cargo.toml").exists() {
        entries.push("Bash(cargo *)");
    }
    if root.join("package.json").exists() {
        entries.extend_from_slice(&["Bash(npm *)", "Bash(npx *)"]);
    }
    if root.join("pyproject.toml").exists() || root.join("requirements.txt").exists() {
        entries.push("Bash(python3 *)");
    }
    entries
}

fn update_claude_settings(root: &Path, skip: bool, yes: bool) -> Result<()> {
    if skip {
        return Ok(());
    }
    let claude_dir = root.join(".claude");
    if !claude_dir.exists() {
        return Ok(());
    }
    let mut entries: Vec<&str> = APM_ALLOW_ENTRIES.to_vec();
    entries.extend(detected_toolchain_entries(root));
    update_settings_json(
        &claude_dir.join("settings.json"),
        &entries,
        "The following entries will be added to .claude/settings.json permissions.allow:",
        "Add apm commands to Claude allow list?",
        "Updated .claude/settings.json",
        true,
        yes,
    )
}

fn update_user_claude_settings(yes: bool) -> Result<()> {
    let home = match std::env::var("HOME") {
        Ok(h) if !h.is_empty() => h,
        _ => return Ok(()),
    };
    update_settings_json(
        &PathBuf::from(&home).join(".claude/settings.json"),
        APM_USER_ALLOW_ENTRIES,
        "The following entries will be added to ~/.claude/settings.json (user-level,\nrequired so apm subagents in isolated worktrees can run git and apm commands):",
        "Add to ~/.claude/settings.json?",
        "Updated ~/.claude/settings.json",
        true,
        yes,
    )
}