pma-core 0.7.0

pma's store, scanning, dispatch and workflows, and the Query/Action API its front ends share
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
//! Publishing approved runs, one of two ways, each its own command:
//! `pma push` commits a run and pushes it to the default branch, and `pma pr`
//! commits it and opens a pull request. Both remove the worktree afterwards. A
//! run with a pull request stays `pr-open`, holding its task, until the pull
//! request is merged or closed. A third way, `land_local`, writes a verified
//! run into its clone as unstaged changes and commits nothing.

use std::process::Command;

use crate::config::{Attribution, Config};
use crate::dispatch::{remove_worktree, wt_git};
use crate::store::{Result, Run, RunState, Store};
use crate::todo;

/// Where a run's commit goes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Target {
    /// Rebased onto the remote default branch and pushed to it.
    Push,
    /// Pushed as the run's own branch, with a pull request opened for it.
    Pr,
}

/// Publishes approved runs in id order, project by project. A failure stops
/// the rest of that project and leaves its runs approved; running the command
/// again resumes them. `done` sees each run with its outcome.
pub fn publish(
    store: &Store,
    home: &std::path::Path,
    cfg: &Config,
    runs: Vec<Run>,
    target: Target,
    mut done: impl FnMut(&Run, &std::result::Result<String, String>),
) -> Result<()> {
    let mut blocked: Vec<String> = Vec::new();
    for mut run in runs {
        if blocked.contains(&run.project) {
            done(
                &run,
                &Err("skipped: an earlier run in this project failed".into()),
            );
            continue;
        }
        match publish_one(home, cfg, &run, target) {
            Ok(outcome) => {
                run.enter(match target {
                    Target::Push => RunState::Pushed,
                    Target::Pr => RunState::PrOpen,
                });
                run.outcome = Some(outcome.clone());
                run.error = None;
                // Saved before cleanup, so a cleanup failure cannot hide
                // what was published.
                store.update_run(&run)?;
                let report = match remove_worktree(&run.repo, &run.worktree, &run.branch) {
                    Ok(()) => outcome,
                    Err(e) => {
                        run.error = Some(format!("cleanup: {e}"));
                        store.update_run(&run)?;
                        format!("{outcome}; warning: worktree not removed: {e}")
                    }
                };
                done(&run, &Ok(report));
            }
            Err(e) => {
                run.error = Some(format!("publish: {e}"));
                store.update_run(&run)?;
                blocked.push(run.project.clone());
                done(&run, &Err(e.to_string()));
            }
        }
    }
    Ok(())
}

/// Writes a run that passed verify into its clone as unstaged changes, then
/// removes its worktree; a run that did not pass is rejected. Nothing is
/// staged or committed: the developer stages, commits or discards. Returns
/// whether it was applied, with what to report. The patch stays in the run's
/// log directory either way.
pub fn land_local(store: &Store, home: &std::path::Path, run: &mut Run) -> Result<(bool, String)> {
    let passed = run.state == RunState::Ready && run.verify_ok == Some(true) && run.error.is_none();
    let refused = if !passed {
        Some(match (&run.verify, run.verify_ok) {
            (None, _) => format!("`{}` states no verify command", run.project),
            (_, Some(false)) => "verify failed".to_string(),
            _ => run
                .error
                .clone()
                .unwrap_or_else(|| format!("run is {}", run.state.name())),
        })
    } else {
        let tree = match &run.reviewed_tree {
            Some(t) => t.clone(),
            None => crate::dispatch::tree(run)?,
        };
        let dir = home.join("runs").join(run.id.to_string());
        std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?;
        let file = dir.join("change.patch");
        // Written by git rather than through `git`'s trimmed output, which
        // drops a final context line that is a single space. The flags undo
        // settings that make a diff `apply` cannot read.
        let output = format!("--output={}", crate::dispatch::path_arg(&file)?);
        wt_git(
            run,
            &[
                "diff",
                "--binary",
                "--no-color",
                "--no-ext-diff",
                "--no-textconv",
                "--src-prefix=a/",
                "--dst-prefix=b/",
                &output,
                &run.base,
                &tree,
            ],
        )?;
        if std::fs::metadata(&file).map_or(0, |m| m.len()) == 0 {
            Some("no changes".to_string())
        } else {
            crate::dispatch::clone_unchanged(store, &run.repo)?;
            // Without `--index`: the working tree only, all or nothing.
            match crate::dispatch::git(
                &run.repo,
                &["apply", "--binary", crate::dispatch::path_arg(&file)?],
            ) {
                Ok(_) => None,
                Err(e) => Some(format!(
                    "does not apply to the clone: {e}; patch: {}",
                    file.display()
                )),
            }
        }
    };
    match refused {
        None => {
            remove_worktree(&run.repo, &run.worktree, &run.branch)?;
            run.enter(RunState::Applied);
            let draft = crate::dispatch::changelog_draft(home, run.id);
            run.outcome = Some(match draft.is_file() {
                true => format!(
                    "applied to {} unstaged; changelog entries in {}",
                    run.repo.display(),
                    draft.display()
                ),
                false if crate::dispatch::wants_changelog(run) => format!(
                    "applied to {} unstaged; no changelog entry drafted",
                    run.repo.display()
                ),
                false => format!("applied to {} unstaged", run.repo.display()),
            });
            store.update_run(run)?;
            Ok((true, run.outcome.clone().unwrap_or_default()))
        }
        Some(why) => {
            run.outcome = Some(format!("discarded: {why}"));
            crate::dispatch::discard(store, run, false)?;
            Ok((false, format!("discarded: {why}")))
        }
    }
}

fn publish_one(home: &std::path::Path, cfg: &Config, run: &Run, target: Target) -> Result<String> {
    let wt = &run.worktree;
    if !wt.is_dir() {
        return Err(format!("{} no longer exists", wt.display()).into());
    }
    let (Some(approved), Some(approved_head)) = (&run.approved_tree, &run.approved_head) else {
        return Err(format!(
            "run #{} has no approved tree and head to publish; approve it again",
            run.id
        )
        .into());
    };
    let head = wt_git(run, &["rev-parse", "HEAD"])?;
    // Before any publish has committed, an edit since approval is someone's
    // work, so it is refused rather than discarded below.
    if &head == approved_head && &crate::dispatch::tree(run)? != approved {
        return Err(format!(
            "the worktree changed after it was approved by {}; read \
             `pma review {}` and approve it again, or reject it",
            run.approved_by.as_deref().unwrap_or("?"),
            run.id
        )
        .into());
    }
    if target == Target::Pr && which("gh").is_none() {
        return Err("`pma pr` needs `gh` on PATH; `pma push` pushes without it".into());
    }
    if approved == &wt_git(run, &["rev-parse", &format!("{}^{{tree}}", run.base)])? {
        return Err("nothing to publish: no changes".into());
    }
    let upstream = format!("origin/{}", run.default_branch);
    if target == Target::Push {
        wt_git(run, &["fetch", "--quiet", "origin"])?;
        // An earlier push pushed these commits and stopped before recording
        // it. Pushed commits keep their ids, so HEAD is in the upstream.
        if head != run.base
            && &head != approved_head
            && wt_git(run, &["merge-base", "--is-ancestor", "HEAD", &upstream]).is_ok()
        {
            let sha = wt_git(run, &["rev-parse", "--short", "HEAD"])?;
            return Ok(format!("already pushed {sha} to {}", run.default_branch));
        }
    }
    let message = message(cfg, run);

    // Every attempt builds one commit of the approved tree on the base. A
    // failed publish leaves its commits, and verify its output, in the
    // worktree; the agent's own commits hold whatever their history held,
    // and review showed only the net diff. `-B` also recovers an agent that
    // switched branches.
    wt_git(
        run,
        &[
            "checkout",
            "--quiet",
            "--force",
            "-B",
            &run.branch,
            &run.base,
        ],
    )?;
    wt_git(run, &["read-tree", "--reset", "-u", approved])?;
    wt_git(run, &["commit", "--quiet", "-m", &message])?;
    if target == Target::Push
        && let Err(e) = wt_git(run, &["rebase", "--quiet", &upstream])
    {
        let _ = wt_git(run, &["rebase", "--abort"]);
        return Err(format!(
            "rebase onto {upstream} failed: {e}. Reject the run and dispatch the \
             task again, or rebase it in {} and approve it again",
            wt.display()
        )
        .into());
    }

    // After the rebase, so ticks on nearby lines by tasks published together
    // do not conflict.
    if !crate::dispatch::without_item(&run.task_key) {
        let path = wt.join("TODO.md");
        let text =
            std::fs::read_to_string(&path).map_err(|e| format!("{}: {e}", path.display()))?;
        if let Some(edited) = todo::mark_done(&text, &run.task_key, &run.text) {
            std::fs::write(&path, edited).map_err(|e| format!("{}: {e}", path.display()))?;
            wt_git(run, &["add", "TODO.md"])?;
            wt_git(run, &["commit", "--quiet", "--amend", "--no-edit"])?;
        }
    }
    if wt_git(run, &["rev-list", "--count", &format!("{upstream}..HEAD")])? == "0" {
        return Err("nothing to publish: no changes".into());
    }
    // The rebase merged other work into this tree, and the tick above added
    // a line. Two changes that each pass against the same base can fail
    // together, and a clean rebase is not a semantic one.
    if let Some(command) = &run.verify {
        let timeout = std::time::Duration::from_secs(cfg.timeout as u64 * 60);
        let agent_env = home.join("agent-env");
        let log = home
            .join("runs")
            .join(run.id.to_string())
            .join("verify-publish.log");
        let _ = std::fs::create_dir_all(log.parent().unwrap_or(&agent_env));
        let sandbox = cfg.project(&run.project).sandbox;
        match crate::dispatch::verify_once(command, wt, sandbox, &agent_env, &log, timeout) {
            Ok((Some(true), _)) => {}
            Ok((Some(false), _)) => {
                return Err(format!(
                    "`{command}` failed on the integrated tree; see {}",
                    log.display()
                )
                .into());
            }
            Ok((None, _)) => {
                return Err(format!("`{command}` timed out on the integrated tree").into());
            }
            Err(e) => return Err(format!("`{command}` could not run: {e}").into()),
        }
    }

    // Verify ran code from the tree and could have installed a `pre-push`
    // hook; `wt_git` compares the snapshot again before the push.
    let outcome = match target {
        Target::Push => {
            wt_git(
                run,
                &[
                    "push",
                    "--quiet",
                    "origin",
                    &format!("HEAD:refs/heads/{}", run.default_branch),
                ],
            )?;
            let sha = wt_git(run, &["rev-parse", "--short", "HEAD"])?;
            format!("pushed {sha} to {}", run.default_branch)
        }
        Target::Pr => {
            // A retry rebuilt its commits, so their ids differ from what an
            // earlier attempt pushed. The same tree there means it pushed.
            let pushed = wt_git(run, &["fetch", "--quiet", "origin", &run.branch]).is_ok()
                && wt_git(
                    run,
                    &["rev-parse", &format!("origin/{}^{{tree}}", run.branch)],
                )? == wt_git(run, &["rev-parse", "HEAD^{tree}"])?;
            if !pushed {
                wt_git(run, &["push", "--quiet", "-u", "origin", &run.branch])?;
            }
            // A retry after `gh pr create` failed may find the pull request
            // made anyway.
            let open = gh_in(
                wt,
                &[
                    "pr",
                    "list",
                    "--head",
                    &run.branch,
                    "--state",
                    "open",
                    "--json",
                    "url",
                    "--jq",
                    ".[0].url // empty",
                ],
            )?;
            if open.is_empty() {
                let (title, body) = message.split_once("\n\n").unwrap_or((&message, ""));
                gh_in(
                    wt,
                    &[
                        "pr",
                        "create",
                        "--base",
                        &run.default_branch,
                        "--head",
                        &run.branch,
                        "--title",
                        title,
                        "--body",
                        body,
                    ],
                )?
            } else {
                open
            }
        }
    };
    Ok(outcome)
}

/// Moves each `pr-open` run to `merged` when its pull request is merged, or
/// to `closed` when it is closed without merging. An open pull request keeps
/// the run `pr-open`; `done` still sees it when someone has reviewed or
/// commented on it, as GitHub shows it. `done` also sees each run whose pull
/// request `gh` cannot read, which stays `pr-open`.
pub fn settle(
    store: &Store,
    mut done: impl FnMut(&Run, &std::result::Result<String, String>),
) -> Result<()> {
    for mut run in store.runs()? {
        if run.state != RunState::PrOpen {
            continue;
        }
        let url = run.outcome.clone().unwrap_or_default();
        let read = crate::sync::gh(&[
            "pr",
            "view",
            &url,
            "--json",
            "state,reviewDecision,comments,reviews",
            "--jq",
            "[.state, .reviewDecision, (.comments | length), (.reviews | length)] | @tsv",
        ]);
        let read = match read {
            Ok(s) => s,
            Err(e) => {
                done(&run, &Err(e));
                continue;
            }
        };
        let mut fields = read.trim().split('\t');
        let state = fields.next().unwrap_or_default();
        let decision = fields.next().unwrap_or_default();
        let comments: usize = fields.next().and_then(|n| n.parse().ok()).unwrap_or(0);
        let reviews: usize = fields.next().and_then(|n| n.parse().ok()).unwrap_or(0);
        let outcome = match state {
            "MERGED" => {
                // `published_at` keeps the time the pull request was opened.
                run.enter(RunState::Merged);
                format!("pull request merged: {url}")
            }
            "CLOSED" => {
                run.enter(RunState::Closed);
                run.error = Some("pull request closed without merging".into());
                format!("pull request closed without merging: {url}")
            }
            _ => {
                if let Some(activity) = activity(decision, comments, reviews) {
                    done(&run, &Ok(format!("pull request open: {activity}: {url}")));
                }
                continue;
            }
        };
        store.update_run(&run)?;
        done(&run, &Ok(outcome));
    }
    Ok(())
}

/// What has happened on an open pull request, in GitHub's terms: its review
/// decision, and how many comments and reviews it has. `None` when nothing
/// has.
fn activity(decision: &str, comments: usize, reviews: usize) -> Option<String> {
    let plural = |n: usize, one: &str| format!("{n} {one}{}", if n == 1 { "" } else { "s" });
    let mut parts = Vec::new();
    match decision {
        "APPROVED" => parts.push("approved".to_string()),
        "CHANGES_REQUESTED" => parts.push("changes requested".to_string()),
        _ => {}
    }
    if comments > 0 {
        parts.push(plural(comments, "comment"));
    }
    if reviews > 0 {
        parts.push(plural(reviews, "review"));
    }
    (!parts.is_empty()).then(|| parts.join(", "))
}

/// Runs `gh` in `dir`, so it finds the repository from the git remote.
fn gh_in(dir: &std::path::Path, args: &[&str]) -> Result<String> {
    let out = Command::new("gh")
        .args(args)
        .current_dir(dir)
        .output()
        .map_err(|e| format!("gh: {e}"))?;
    if !out.status.success() {
        return Err(format!(
            "gh {}: {}",
            args[..2].join(" "),
            String::from_utf8_lossy(&out.stderr).trim()
        )
        .into());
    }
    Ok(String::from_utf8_lossy(&out.stdout).trim().to_string())
}

/// The task text as subject; `Closes #N` and the co-author trailer as body.
fn message(cfg: &Config, run: &Run) -> String {
    let mut body = Vec::new();
    if let Some(n) = run.gh {
        body.push(format!("Closes #{n}"));
    }
    // Only a worker whose identity `pma` knows gets a trailer. Inventing an
    // address for an unknown one would attribute the commit to nobody.
    if cfg.attribution == Attribution::CoAuthor && run.agent == "claude" {
        body.push("Co-Authored-By: Claude <noreply@anthropic.com>".into());
    }
    if body.is_empty() {
        run.text.clone()
    } else {
        format!("{}\n\n{}", run.text, body.join("\n\n"))
    }
}

fn which(program: &str) -> Option<std::path::PathBuf> {
    std::env::var_os("PATH").and_then(|paths| {
        std::env::split_paths(&paths)
            .map(|d| d.join(program))
            .find(|p| p.is_file())
    })
}

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

    #[test]
    fn activity_on_an_open_pull_request_reads_as_github_shows_it() {
        assert_eq!(activity("", 0, 0), None);
        assert_eq!(activity("REVIEW_REQUIRED", 0, 0), None);
        assert_eq!(
            activity("CHANGES_REQUESTED", 2, 1).as_deref(),
            Some("changes requested, 2 comments, 1 review")
        );
        assert_eq!(
            activity("APPROVED", 0, 1).as_deref(),
            Some("approved, 1 review")
        );
        assert_eq!(activity("", 1, 0).as_deref(), Some("1 comment"));
    }
}