apm-cli 0.1.17

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
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/// End-to-end tests that run the real `apm` binary and real git commands.
///
/// Each test simulates a human or agent workflow step-by-step and asserts
/// the expected state of git, files, and apm output at every transition.
use std::path::Path;
use std::process::{Command, Output};
use tempfile::TempDir;

const APM: &str = env!("CARGO_BIN_EXE_apm");

// ---------------------------------------------------------------------------
// Test environment
// ---------------------------------------------------------------------------

struct Env {
    dir: TempDir,
}

impl Env {
    /// Create a fresh git repo with testdata source files committed on main
    /// and apm fully initialized.
    fn setup() -> Self {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path();

        // Init git with main as the default branch.
        git_ok(p, &["init", "-q", "-b", "main"]);
        git_ok(p, &["config", "user.email", "test@test.com"]);
        git_ok(p, &["config", "user.name", "test"]);

        // Copy testdata source files so tickets can reference real paths.
        let src_dir = p.join("src");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::copy(
            concat!(env!("CARGO_MANIFEST_DIR"), "/../testdata/src/parser.rs"),
            src_dir.join("parser.rs"),
        ).unwrap();
        std::fs::copy(
            concat!(env!("CARGO_MANIFEST_DIR"), "/../testdata/src/main.rs"),
            src_dir.join("main.rs"),
        ).unwrap();

        // Write apm.toml before init so worktrees dir stays inside the tempdir.
        std::fs::write(p.join("apm.toml"), r#"[project]
name = "test-repo"

[tickets]
dir = "tickets"

[worktrees]
dir = ".worktrees"

[agents]
max_concurrent = 3

[workflow.prioritization]
priority_weight = 10.0
effort_weight = -2.0
risk_weight = -1.0

[[workflow.states]]
id         = "new"
label      = "New"

[[workflow.states]]
id         = "groomed"
label      = "Groomed"
actionable = ["agent"]

[[workflow.states]]
id         = "question"
label      = "Question"
actionable = ["supervisor"]

[[workflow.states]]
id         = "in_design"
label      = "In Design"

[[workflow.states]]
id         = "specd"
label      = "Specd"
actionable = ["supervisor"]

[[workflow.states]]
id         = "ammend"
label      = "Ammend"
actionable = ["agent"]

[[workflow.states]]
id         = "ready"
label      = "Ready"
actionable = ["agent"]

[[workflow.states]]
id    = "in_progress"
label = "In Progress"

[[workflow.states]]
id         = "implemented"
label      = "Implemented"
actionable = ["supervisor"]

[[workflow.states]]
id         = "accepted"
label      = "Accepted"
actionable = ["supervisor"]

[[workflow.states]]
id       = "closed"
label    = "Closed"
terminal = true
"#).unwrap();

        // Commit source files and apm.toml to main before apm init.
        git_ok(p, &["add", "src/", "apm.toml"]);
        git_ok(p, &["-c", "commit.gpgsign=false", "commit", "-m", "Add source files"]);

        // apm init (--no-claude skips the interactive settings.json prompt).
        let out = apm(p, "apm", &["init", "--no-claude"]);
        assert!(out.status.success(), "apm init failed:\n{}", stderr(&out));

        // Patch the workflow so in_progress → implemented uses completion = "none".
        // Tests run without a git remote, so strategies that push (pr_or_epic_merge,
        // pr) would fail. The state transition itself is still fully exercised.
        let wf_path = p.join(".apm/workflow.toml");
        let wf = std::fs::read_to_string(&wf_path).unwrap();
        let wf = wf.replace(
            "  completion = \"pr_or_epic_merge\"",
            "  completion = \"none\"",
        );
        std::fs::write(&wf_path, wf).unwrap();

        Env { dir }
    }

    fn root(&self) -> &Path {
        self.dir.path()
    }

    /// Run an apm command as a given agent.
    fn apm_as(&self, agent: &str, args: &[&str]) -> Output {
        apm_env(self.root(), agent, args)
    }

    /// Run an apm command with no agent identity (APM_AGENT_NAME unset).
    fn apm(&self, args: &[&str]) -> Output {
        apm(self.root(), "apm", args)
    }

    /// Read a file from a git branch without touching the working tree.
    fn branch_content(&self, branch: &str, path: &str) -> String {
        let out = git(self.root(), &["show", &format!("{branch}:{path}")]);
        assert!(
            out.status.success(),
            "git show {branch}:{path} failed:\n{}",
            stderr(&out)
        );
        String::from_utf8(out.stdout).unwrap()
    }

    /// Read a file from the working tree.
    fn read(&self, rel: &str) -> String {
        std::fs::read_to_string(self.root().join(rel)).unwrap()
    }

    /// Write a file in the working tree.
    fn write(&self, rel: &str, content: &str) {
        let full = self.root().join(rel);
        if let Some(parent) = full.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(full, content).unwrap();
    }

    /// Return the name of the currently checked-out branch.
    fn current_branch(&self) -> String {
        let out = git(self.root(), &["rev-parse", "--abbrev-ref", "HEAD"]);
        String::from_utf8(out.stdout).unwrap().trim().to_string()
    }

    /// Return true if a local branch exists.
    fn branch_exists(&self, branch: &str) -> bool {
        git(self.root(), &["rev-parse", "--verify", &format!("refs/heads/{branch}")])
            .status
            .success()
    }

    /// Return commits on `branch` that are not on `base`, most-recent first.
    fn commits_on_branch(&self, branch: &str, base: &str) -> Vec<String> {
        let out = git(
            self.root(),
            &["log", "--oneline", &format!("{base}..{branch}")],
        );
        String::from_utf8(out.stdout)
            .unwrap()
            .lines()
            .map(|l| l.to_string())
            .collect()
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn apm(dir: &Path, _name: &str, args: &[&str]) -> Output {
    Command::new(APM)
        .args(args)
        .current_dir(dir)
        .env("GIT_AUTHOR_NAME", "test")
        .env("GIT_AUTHOR_EMAIL", "test@test.com")
        .env("GIT_COMMITTER_NAME", "test")
        .env("GIT_COMMITTER_EMAIL", "test@test.com")
        .env("GIT_CONFIG_NOSYSTEM", "1")
        .env("HOME", dir) // isolate git config
        .env("VISUAL", "true") // prevent vi from blocking in tests
        .output()
        .unwrap()
}

fn apm_env(dir: &Path, agent: &str, args: &[&str]) -> Output {
    Command::new(APM)
        .args(args)
        .current_dir(dir)
        .env("APM_AGENT_NAME", agent)
        .env("GIT_AUTHOR_NAME", "test")
        .env("GIT_AUTHOR_EMAIL", "test@test.com")
        .env("GIT_COMMITTER_NAME", "test")
        .env("GIT_COMMITTER_EMAIL", "test@test.com")
        .env("GIT_CONFIG_NOSYSTEM", "1")
        .env("HOME", dir)
        .env("VISUAL", "true") // prevent vi from blocking in tests
        .output()
        .unwrap()
}

fn git(dir: &Path, args: &[&str]) -> Output {
    Command::new("git")
        .args(args)
        .current_dir(dir)
        .env("GIT_AUTHOR_NAME", "test")
        .env("GIT_AUTHOR_EMAIL", "test@test.com")
        .env("GIT_COMMITTER_NAME", "test")
        .env("GIT_COMMITTER_EMAIL", "test@test.com")
        .env("GIT_CONFIG_NOSYSTEM", "1")
        .output()
        .unwrap()
}

fn git_ok(dir: &Path, args: &[&str]) {
    let out = git(dir, args);
    assert!(out.status.success(), "git {:?} failed:\n{}", args, stderr(&out));
}

/// Check out ticket branch, write a valid spec body, commit, return to main.
fn write_valid_spec_for_test(dir: &Path, branch: &str, ticket_path: &str) {
    git_ok(dir, &["checkout", branch]);
    let existing = std::fs::read_to_string(dir.join(ticket_path)).unwrap();
    let fm_end = existing.find("\n+++\n").expect("frontmatter close not found") + 5;
    let frontmatter = &existing[..fm_end];
    let body = "\n## Spec\n\n### Problem\n\nTest problem.\n\n### Acceptance criteria\n\n- [ ] One criterion\n\n### Out of scope\n\nNothing.\n\n### Approach\n\nDirect approach.\n\n## History\n\n| When | From | To | By |\n|------|------|----|-----|\n| 2026-01-01T00:00Z | — | new | test-agent |\n";
    std::fs::write(dir.join(ticket_path), format!("{frontmatter}{body}")).unwrap();
    git_ok(dir, &["-c", "commit.gpgsign=false", "add", ticket_path]);
    git_ok(dir, &["-c", "commit.gpgsign=false", "commit", "-m", "write spec"]);
    git_ok(dir, &["checkout", "main"]);
}

fn stdout(out: &Output) -> String {
    String::from_utf8_lossy(&out.stdout).into_owned()
}

fn stderr(out: &Output) -> String {
    String::from_utf8_lossy(&out.stderr).into_owned()
}

/// Parse the ticket ID from `apm new` output.
/// Output format: "Created ticket {id}: {filename} (branch: {branch})"
fn parse_new_ticket_id(out: &Output) -> String {
    let s = stdout(out);
    s.lines()
        .find(|l| l.starts_with("Created ticket "))
        .and_then(|l| l.strip_prefix("Created ticket "))
        .and_then(|s| s.split(':').next())
        .unwrap_or_else(|| panic!("could not parse ticket ID from: {s}"))
        .trim()
        .to_string()
}

/// Parse the branch name from `apm new` output.
fn parse_new_ticket_branch(out: &Output) -> String {
    let s = stdout(out);
    s.lines()
        .find(|l| l.starts_with("Created ticket "))
        .and_then(|l| l.split("(branch: ").nth(1))
        .and_then(|s| s.strip_suffix(')').or_else(|| s.trim_end_matches('\n').strip_suffix(')')))
        .map(|s| s.trim().to_string())
        .unwrap_or_else(|| panic!("could not parse branch from: {s}"))
}

// ---------------------------------------------------------------------------
// Full ticket lifecycle: new → specd → ready → in_progress → implemented →
//                        (sync detects merge) → accepted
// ---------------------------------------------------------------------------

#[test]
fn full_ticket_lifecycle() {
    let env = Env::setup();

    // ── Step 1: apm init ────────────────────────────────────────────────────
    // setup() already ran init. Verify the expected files are in place.

    assert!(env.root().join("CLAUDE.md").exists(), "CLAUDE.md missing");
    assert!(env.root().join(".apm/agents.md").exists(), ".apm/agents.md missing");
    assert!(env.root().join(".apm/config.toml").exists(), ".apm/config.toml missing");
    assert!(!env.root().join(".git/hooks/pre-push").exists(), "pre-push hook should not be installed");
    assert!(!env.root().join(".git/hooks/post-merge").exists(), "post-merge hook should not be installed");

    let claude = env.read("CLAUDE.md");
    assert!(claude.contains("@.apm/agents.md"), "CLAUDE.md missing @.apm/agents.md import");

    // ── Step 2: create a ticket ─────────────────────────────────────────────
    // Agent creates a ticket for the parse_count bug.

    // Write local identity so resolve_identity returns "test-agent".
    env.write(".apm/local.toml", "username = \"test-agent\"\n");
    let out = env.apm_as("test-agent", &["new", "Fix parse_count off-by-one"]);
    assert!(out.status.success(), "apm new failed:\n{}", stderr(&out));
    let out_text = stdout(&out);
    assert!(out_text.contains("Created ticket "), "unexpected output: {out_text}");
    assert!(out_text.contains("fix-parse-count-off-by-one"), "slug missing: {out_text}");

    let ticket_id = parse_new_ticket_id(&out);
    let branch = parse_new_ticket_branch(&out);
    let ticket_suffix = branch.strip_prefix("ticket/").unwrap().to_string();
    let ticket_path = format!("tickets/{ticket_suffix}.md");

    // Branch exists locally.
    assert!(env.branch_exists(&branch), "ticket branch not created");

    // Frontmatter is correct — read from the branch, not the working tree.
    let ticket = env.branch_content(&branch, &ticket_path);
    // id is now an 8-char hex string
    assert!(ticket.contains(&format!("id = \"{ticket_id}\"")), "wrong id in frontmatter");
    assert!(ticket.contains("state = \"new\""), "wrong state");
    assert!(ticket.contains("author = \"test-agent\""), "author not set");
    assert!(ticket.contains(&format!("branch = \"{branch}\"")), "branch not set");
    assert!(ticket.contains("created_at"), "created_at missing");
    assert!(ticket.contains("updated_at"), "updated_at missing");

    // Body scaffold includes all four required sections and history.
    assert!(ticket.contains("### Problem"), "missing ### Problem");
    assert!(ticket.contains("### Acceptance criteria"), "missing ### Acceptance criteria");
    assert!(ticket.contains("### Out of scope"), "missing ### Out of scope");
    assert!(ticket.contains("### Approach"), "missing ### Approach");
    assert!(ticket.contains("## History"), "missing ## History");
    assert!(ticket.contains("| — | new | test-agent |"), "missing creation history row");

    // ── Step 3: agent writes the spec ───────────────────────────────────────
    // Simulate: git checkout <branch>, edit ticket, commit, checkout main.

    git_ok(env.root(), &["checkout", &branch]);
    assert_eq!(env.current_branch(), branch);

    // Preserve the frontmatter written by apm new; replace only the body.
    let existing = env.read(&ticket_path);
    let fm_end = existing.find("\n+++\n").expect("frontmatter close not found") + 5;
    let frontmatter = &existing[..fm_end];

    let new_body = r#"
## Spec

### Problem

`parse_count` in `src/parser.rs` subtracts 1 from the split count, causing a
panic on empty input and returning 0 for a single-item string. Any caller
expecting a correct count gets wrong results.

### Acceptance criteria

- [ ] `parse_count("")` returns 0 without panicking
- [ ] `parse_count("a")` returns 1
- [ ] `parse_count("a,b,c")` returns 3
- [ ] Existing `parse_items` behaviour is unchanged

### Out of scope

- Changing the delimiter from comma to anything else
- Unicode or whitespace normalisation

### Approach

Remove the `- 1` in `parse_count`. Add a guard for empty input that returns 0
immediately. Update the existing tests to cover the single-item case.

## History

| When | From | To | By |
|------|------|----|-----|
| 2026-03-26T00:00Z | — | new | test-agent |
"#;

    env.write(&ticket_path, &format!("{frontmatter}{new_body}"));
    git_ok(env.root(), &["-c", "commit.gpgsign=false", "add", &ticket_path]);
    git_ok(env.root(), &["-c", "commit.gpgsign=false", "commit", "-m", &format!("ticket({ticket_id}): write spec")]);
    git_ok(env.root(), &["checkout", "main"]);
    assert_eq!(env.current_branch(), "main");

    // Spec commit is on the ticket branch but not on main.
    let branch_commits = env.commits_on_branch(&branch, "main");
    assert!(
        branch_commits.iter().any(|c| c.contains("write spec")),
        "spec commit not found on ticket branch"
    );

    // ── Step 4: walk new → groomed → in_design → specd ────────────────────
    // The workflow requires this path; direct new→specd is not a valid transition.

    let out = env.apm_as("philippe", &["state", &ticket_id, "groomed"]);
    assert!(out.status.success(), "apm state groomed failed:\n{}", stderr(&out));

    let out = env.apm_as("test-agent", &["state", &ticket_id, "in_design"]);
    assert!(out.status.success(), "apm state in_design failed:\n{}", stderr(&out));

    let out = env.apm_as("test-agent", &["state", &ticket_id, "specd"]);
    assert!(out.status.success(), "apm state specd failed:\n{}", stderr(&out));

    let ticket = env.branch_content(&branch, &ticket_path);
    assert!(ticket.contains("state = \"specd\""), "state not updated to specd");
    assert!(ticket.contains("| in_design | specd |"), "history row missing");
    assert!(ticket.contains("updated_at"), "updated_at not refreshed");

    // ── Step 5: supervisor approves — apm state specd → ready ───────────────

    let out = env.apm_as("philippe", &["state", &ticket_id, "ready"]);
    assert!(out.status.success(), "apm state ready failed:\n{}", stderr(&out));

    let ticket = env.branch_content(&branch, &ticket_path);
    assert!(ticket.contains("state = \"ready\""), "state not updated to ready");
    assert!(ticket.contains("| specd | ready |"), "history row missing");

    // ── Step 6: agent claims ticket — apm start ──────────────────────────────
    // apm start transitions ready → in_progress, sets agent, provisions worktree.

    let out = env.apm_as("test-agent", &["start", &ticket_id]);
    assert!(out.status.success(), "apm start failed:\n{}", stderr(&out));
    let start_out = stdout(&out);
    assert!(start_out.contains("in_progress"), "unexpected output: {}", start_out);
    assert!(start_out.contains("Worktree:"), "worktree path missing from output: {}", start_out);

    // Main worktree is still on main — agent works in the provisioned worktree.
    assert_eq!(env.current_branch(), "main", "main worktree should stay on main");

    let ticket = env.branch_content(&branch, &ticket_path);
    assert!(ticket.contains("state = \"in_progress\""), "state not in_progress");
    assert!(!ticket.contains("agent ="), "agent field must not be written");
    assert!(ticket.contains("| ready | in_progress |"), "history row missing");

    // Parse the worktree path from the output line "Worktree: /path/to/wt"
    let wt_path = start_out
        .lines()
        .find(|l| l.starts_with("Worktree:"))
        .and_then(|l| l.strip_prefix("Worktree:"))
        .map(|s| std::path::PathBuf::from(s.trim()))
        .expect("could not parse worktree path from apm start output");

    // ── Step 7: agent fixes the bug ──────────────────────────────────────────
    // Work in the worktree (the agent's checkout of the ticket branch).

    let fixed = r#"/// Parse a comma-separated list and return the item count.
pub fn parse_count(input: &str) -> usize {
    if input.is_empty() {
        return 0;
    }
    input.split(',').count()
}

pub fn parse_items(input: &str) -> Vec<&str> {
    input.split(',').map(str::trim).collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn count_empty() {
        assert_eq!(parse_count(""), 0);
    }

    #[test]
    fn count_multiple() {
        assert_eq!(parse_count("a,b,c"), 3);
    }

    #[test]
    fn count_single() {
        assert_eq!(parse_count("a"), 1);
    }
}
"#;
    std::fs::create_dir_all(wt_path.join("src")).unwrap();
    std::fs::write(wt_path.join("src/parser.rs"), fixed).unwrap();
    git_ok(&wt_path, &["-c", "commit.gpgsign=false", "add", "src/parser.rs"]);
    git_ok(&wt_path, &["-c", "commit.gpgsign=false", "commit", "-m", &format!("ticket({ticket_id}): fix parse_count off-by-one")]);

    // Code fix commit is on the ticket branch.
    let branch_commits = env.commits_on_branch(&branch, "main");
    assert!(
        branch_commits.iter().any(|c| c.contains("fix parse_count")),
        "code fix commit not found on ticket branch"
    );

    // Fixed file is in the worktree.
    let src = std::fs::read_to_string(wt_path.join("src/parser.rs")).unwrap();
    assert!(!src.contains("- 1"), "bug still present in fixed file");
    assert!(src.contains("if input.is_empty()"), "empty guard missing");

    // ── Step 8: agent checks acceptance criteria boxes ───────────────────────
    // Work in the worktree.

    let ticket_content = std::fs::read_to_string(wt_path.join(&ticket_path)).unwrap();
    let checked = ticket_content
        .replace("- [ ] `parse_count(\"\")` returns 0 without panicking", "- [x] `parse_count(\"\")` returns 0 without panicking")
        .replace("- [ ] `parse_count(\"a\")` returns 1", "- [x] `parse_count(\"a\")` returns 1")
        .replace("- [ ] `parse_count(\"a,b,c\")` returns 3", "- [x] `parse_count(\"a,b,c\")` returns 3")
        .replace("- [ ] Existing `parse_items` behaviour is unchanged", "- [x] Existing `parse_items` behaviour is unchanged");
    std::fs::write(wt_path.join(&ticket_path), &checked).unwrap();
    git_ok(&wt_path, &["-c", "commit.gpgsign=false", "add", &ticket_path]);
    git_ok(&wt_path, &["-c", "commit.gpgsign=false", "commit", "-m", &format!("ticket({ticket_id}): check acceptance criteria")]);

    // All boxes checked.
    let ticket = env.branch_content(&branch, &ticket_path);
    assert!(!ticket.contains("- [ ]"), "unchecked boxes remain");
    assert_eq!(ticket.matches("- [x]").count(), 4, "expected 4 checked boxes");

    // ── Step 9: apm state in_progress → implemented ─────────────────────────

    let out = env.apm_as("test-agent", &["state", &ticket_id, "implemented"]);
    assert!(out.status.success(), "apm state implemented failed:\n{}", stderr(&out));

    let ticket = env.branch_content(&branch, &ticket_path);
    assert!(ticket.contains("state = \"implemented\""), "state not implemented");
    assert!(ticket.contains("| in_progress | implemented |"), "history row missing");

    // ── Step 10: merge ticket branch into main ───────────────────────────────
    // Simulates a PR merge.

    git_ok(env.root(), &["checkout", "main"]);
    git_ok(env.root(), &[
        "-c", "commit.gpgsign=false",
        "merge", "--no-ff", &branch,
        "-m", &format!("Merge {branch} — Fix parse_count off-by-one"),
    ]);

    // After merge: main has the fixed parser.rs.
    let src = env.read("src/parser.rs");
    assert!(!src.contains("- 1"), "merged main still has the bug");
    assert!(src.contains("if input.is_empty()"), "fix not in main after merge");

    // ── Step 11: apm sync detects merged branch and offers to close ─────────────
    // --offline skips the remote fetch/push.
    // sync reads tickets from git blobs directly — no working-tree prep needed.

    let out = env.apm(&["sync", "--offline"]);
    assert!(out.status.success(), "apm sync failed:\n{}", stderr(&out));
    assert!(
        stdout(&out).contains("branch merged"),
        "merge suggestion not reported:\n{}",
        stdout(&out)
    );

    // Ticket is still in implemented — no auto-close without --auto-close flag.
    let ticket_after = env.branch_content(&branch, &ticket_path);
    assert!(ticket_after.contains("state = \"implemented\""), "state should still be implemented after sync");
}

// ---------------------------------------------------------------------------
// State transition enforcement
// ---------------------------------------------------------------------------

#[test]
fn state_rejects_illegal_transition() {
    // Use a config with explicit transition rules.
    let dir = tempfile::tempdir().unwrap();
    let p = dir.path();
    git_ok(p, &["init", "-q", "-b", "main"]);
    git_ok(p, &["config", "user.email", "test@test.com"]);
    git_ok(p, &["config", "user.name", "test"]);

    std::fs::write(
        p.join("apm.toml"),
        r#"[project]
name = "test"

[tickets]
dir = "tickets"

[agents]
max_concurrent = 1

[workflow.prioritization]
priority_weight = 10.0
effort_weight   = -2.0
risk_weight     = -1.0

[[workflow.states]]
id         = "new"
label      = "New"
actionable = ["agent"]

[[workflow.states.transitions]]
to      = "specd"
trigger = "manual"

[[workflow.states]]
id = "specd"
label = "Specd"

[[workflow.states.transitions]]
to      = "ready"
trigger = "manual"

[[workflow.states]]
id         = "ready"
label      = "Ready"
actionable = ["agent"]

[[workflow.states]]
id = "closed"
label = "Closed"
terminal = true
"#,
    ).unwrap();

    git_ok(p, &["-c", "commit.gpgsign=false", "add", "apm.toml"]);
    git_ok(p, &["-c", "commit.gpgsign=false", "commit", "-m", "init", "--allow-empty"]);
    std::fs::create_dir_all(p.join("tickets")).unwrap();

    // Create a ticket and write a valid spec body before transitioning to specd.
    let out = apm_env(p, "test-agent", &["new", "Enforcement test"]);
    assert!(out.status.success());
    let id1 = parse_new_ticket_id(&out);
    let branch1 = parse_new_ticket_branch(&out);
    let path1 = format!("tickets/{}.md", branch1.strip_prefix("ticket/").unwrap());
    write_valid_spec_for_test(p, &branch1, &path1);

    // new → specd is allowed.
    let out = apm_env(p, "test-agent", &["state", &id1, "specd"]);
    assert!(out.status.success(), "new → specd should be allowed:\n{}", stderr(&out));

    // specd → new is NOT allowed (no such transition defined, and new is not terminal).
    let out = apm_env(p, "test-agent", &["state", &id1, "new"]);
    assert!(!out.status.success(), "specd → new should be rejected");
    let err = stderr(&out);
    assert!(err.contains("no transition"), "expected transition error, got: {err}");
    assert!(err.contains("specd"), "error should mention current state");
    assert!(err.contains("new"), "error should mention target state");

    // Terminal states are always reachable regardless of transition rules.
    let out = apm_env(p, "test-agent", &["state", &id1, "closed"]);
    assert!(out.status.success(), "specd → closed should be allowed (terminal state)");

    // new → specd → ready via defined transitions (need a fresh ticket since #1 is now closed).
    let out = apm_env(p, "test-agent", &["new", "Second enforcement test"]);
    assert!(out.status.success());
    let id2 = parse_new_ticket_id(&out);
    let branch2 = parse_new_ticket_branch(&out);
    let path2 = format!("tickets/{}.md", branch2.strip_prefix("ticket/").unwrap());
    write_valid_spec_for_test(p, &branch2, &path2);
    let out = apm_env(p, "test-agent", &["state", &id2, "specd"]);
    assert!(out.status.success(), "new → specd should be allowed");
    let out = apm_env(p, "test-agent", &["state", &id2, "ready"]);
    assert!(out.status.success(), "specd → ready should be allowed");
}

// ---------------------------------------------------------------------------
// apm next prioritisation
// ---------------------------------------------------------------------------

#[test]
fn next_respects_priority_and_actionable_states() {
    let env = Env::setup();

    // Set local identity so resolve_identity() returns "test-agent",
    // matching the APM_AGENT_NAME used by apm_as("test-agent", ...).
    std::fs::create_dir_all(env.root().join(".apm")).unwrap();
    std::fs::write(
        env.root().join(".apm/local.toml"),
        "username = \"test-agent\"\n",
    ).unwrap();

    // Create three tickets with different priorities.
    let out = env.apm_as("test-agent", &["new", "Low priority task"]);
    assert!(out.status.success());
    let id1 = parse_new_ticket_id(&out);
    let out = env.apm_as("test-agent", &["new", "High priority task"]);
    assert!(out.status.success());
    let id2 = parse_new_ticket_id(&out);
    let branch2 = parse_new_ticket_branch(&out);
    let path2 = format!("tickets/{}.md", branch2.strip_prefix("ticket/").unwrap());
    let out = env.apm_as("test-agent", &["new", "Medium priority task"]);
    assert!(out.status.success());
    let id3 = parse_new_ticket_id(&out);

    // Set priorities (apm set/next/state read from git blobs — no working-tree prep needed).
    env.apm(&["set", &id1, "priority", "1"]);
    env.apm(&["set", &id2, "priority", "9"]);
    env.apm(&["set", &id3, "priority", "5"]);

    // Promote all tickets to groomed so they are agent-actionable.
    env.apm(&["state", &id1, "groomed"]);
    env.apm(&["state", &id2, "groomed"]);
    env.apm(&["state", &id3, "groomed"]);

    // apm next --json should return the highest-priority actionable ticket.
    let out = env.apm_as("test-agent", &["next", "--json"]);
    assert!(out.status.success(), "apm next failed:\n{}", stderr(&out));
    let json = stdout(&out);
    // id is now a hex string
    assert!(
        json.contains(&format!("\"id\":\"{id2}\"")) || json.contains(&format!("\"id\": \"{id2}\"")),
        "expected ticket id2 (highest priority), got: {json}"
    );

    // Move id2 to specd (not actionable) — next should now return id3.
    write_valid_spec_for_test(env.root(), &branch2, &path2);
    env.apm(&["state", &id2, "in_design"]);
    env.apm(&["state", &id2, "specd"]);

    let out = env.apm_as("test-agent", &["next", "--json"]);
    assert!(out.status.success());
    let json = stdout(&out);
    assert!(
        json.contains(&format!("\"id\":\"{id3}\"")) || json.contains(&format!("\"id\": \"{id3}\"")),
        "expected ticket id3 after id2 moved to specd, got: {json}"
    );
}