Skip to main content

orch/cli/
proof.rs

1use std::collections::BTreeMap;
2use std::collections::HashMap;
3use std::fs;
4use std::path::Path;
5use std::process::Command;
6
7use anyhow::Context;
8use chrono::DateTime;
9use chrono::Duration;
10use chrono::Utc;
11use serde::Deserialize;
12use serde::Serialize;
13
14use crate::cli;
15use crate::cmd::SyncCommandErrorContext;
16use crate::config;
17
18const FOOTER_MARKER: &str = "Created by ";
19const FOOTER_BOT: &str = "[bot] via [Orch]";
20
21#[derive(Deserialize)]
22struct GhPr {
23    number: u64,
24    body: Option<String>,
25    #[serde(rename = "createdAt")]
26    created_at: DateTime<Utc>,
27    #[serde(rename = "mergedAt")]
28    merged_at: Option<DateTime<Utc>>,
29    #[serde(rename = "closingIssuesReferences", default)]
30    closing_issues: Vec<GhIssueRef>,
31}
32
33#[derive(Deserialize)]
34struct GhIssueRef {
35    number: u64,
36}
37
38#[derive(Deserialize)]
39struct GhIssue {
40    number: u64,
41    #[serde(rename = "createdAt")]
42    created_at: DateTime<Utc>,
43}
44
45#[derive(Debug, PartialEq, Serialize)]
46pub struct Proof {
47    pub repo: String,
48    pub generated_at: DateTime<Utc>,
49    pub days: Vec<ProofDay>,
50}
51
52#[derive(Debug, PartialEq, Serialize)]
53pub struct ProofDay {
54    pub date: String,
55    /// Every PR merged that day, the figure `gh pr list --state merged` shows
56    pub merged: usize,
57    pub merged_by_orch: usize,
58    pub prs: Vec<ProofPr>,
59}
60
61#[derive(Debug, PartialEq, Serialize)]
62pub struct ProofPr {
63    pub number: u64,
64    pub agent: String,
65    pub model: Option<String>,
66    pub merged_at: DateTime<Utc>,
67    pub issue: Option<u64>,
68    /// Starts at the issue creation, or at the PR creation when the PR closes no issue
69    pub hours_to_merge: f64,
70}
71
72/// Read the agent and model from the footer orch writes on every PR body
73fn parse_footer(body: &str) -> Option<(String, Option<String>)> {
74    let line = body.lines().rev().find(|l| l.contains(FOOTER_BOT))?;
75    let after_marker = &line[line.find(FOOTER_MARKER)? + FOOTER_MARKER.len()..];
76    let agent = after_marker[..after_marker.find(FOOTER_BOT)?].to_string();
77    if agent.is_empty() {
78        return None;
79    }
80    let tail = &line[line.find(FOOTER_BOT)? + FOOTER_BOT.len()..];
81    let model = tail
82        .split('`')
83        .nth(1)
84        .filter(|m| !m.is_empty())
85        .map(str::to_string);
86    Some((agent, model))
87}
88
89/// Group the merged PRs by UTC day of the merge
90fn build_proof(
91    repo: &str,
92    now: DateTime<Utc>,
93    since: DateTime<Utc>,
94    prs: Vec<GhPr>,
95    issues: &[GhIssue],
96) -> Proof {
97    let issue_created: HashMap<u64, DateTime<Utc>> =
98        issues.iter().map(|i| (i.number, i.created_at)).collect();
99    let mut days: BTreeMap<String, ProofDay> = BTreeMap::new();
100
101    for pr in prs {
102        let Some(merged_at) = pr.merged_at else {
103            continue;
104        };
105        if merged_at < since || merged_at > now {
106            continue;
107        }
108        let date = merged_at.format("%Y-%m-%d").to_string();
109        let day = days.entry(date.clone()).or_insert_with(|| ProofDay {
110            date,
111            merged: 0,
112            merged_by_orch: 0,
113            prs: Vec::new(),
114        });
115        day.merged += 1;
116
117        let Some((agent, model)) = pr.body.as_deref().and_then(parse_footer) else {
118            continue;
119        };
120        let issue = pr
121            .closing_issues
122            .iter()
123            .map(|i| i.number)
124            .find(|n| issue_created.contains_key(n));
125        let start = issue
126            .and_then(|n| issue_created.get(&n).copied())
127            .unwrap_or(pr.created_at);
128        #[expect(
129            clippy::cast_precision_loss,
130            reason = "a duration in seconds far below 2^52"
131        )]
132        let hours_to_merge =
133            ((merged_at - start).num_seconds().max(0) as f64 / 3600.0 * 100.0).round() / 100.0;
134        day.merged_by_orch += 1;
135        day.prs.push(ProofPr {
136            number: pr.number,
137            agent,
138            model,
139            merged_at,
140            issue,
141            hours_to_merge,
142        });
143    }
144
145    let mut days: Vec<ProofDay> = days.into_values().collect();
146    for day in &mut days {
147        day.prs.sort_by_key(|p| p.number);
148    }
149    Proof {
150        repo: repo.to_string(),
151        generated_at: now,
152        days,
153    }
154}
155
156fn gh_json<T: for<'de> Deserialize<'de>>(args: &[&str]) -> anyhow::Result<T> {
157    let output = Command::new("gh").args(args).output_with_context()?;
158    if !output.status.success() {
159        anyhow::bail!(
160            "gh {} failed: {}",
161            args.join(" "),
162            String::from_utf8_lossy(&output.stderr).trim()
163        );
164    }
165    serde_json::from_slice(&output.stdout).context("gh printed JSON that does not parse")
166}
167
168/// Write the merged PRs of a repository per day, the agent of each and the time to merge
169pub fn export(repo: Option<String>, since: &str, out: Option<&Path>) -> anyhow::Result<()> {
170    let repo = match repo {
171        Some(r) => r,
172        None => config::get_current_repo()?,
173    };
174    let hours = cli::parse_since(since)
175        .ok_or_else(|| anyhow::anyhow!("unrecognised --since value {since:?}"))?;
176    let now = Utc::now();
177    let since_at = now - Duration::hours(i64::from(hours));
178
179    let prs: Vec<GhPr> = gh_json(&[
180        "pr",
181        "list",
182        "--repo",
183        &repo,
184        "--state",
185        "merged",
186        "--limit",
187        "5000",
188        "--json",
189        "number,body,createdAt,mergedAt,closingIssuesReferences",
190    ])?;
191    let issues: Vec<GhIssue> = gh_json(&[
192        "issue",
193        "list",
194        "--repo",
195        &repo,
196        "--state",
197        "all",
198        "--limit",
199        "10000",
200        "--json",
201        "number,createdAt",
202    ])?;
203
204    let proof = build_proof(&repo, now, since_at, prs, &issues);
205    let json = serde_json::to_string_pretty(&proof)?;
206    match out {
207        Some(path) => {
208            if let Some(parent) = path.parent() {
209                fs::create_dir_all(parent)?;
210            }
211            fs::write(path, format!("{json}\n"))?;
212        }
213        None => println!("{json}"),
214    }
215    Ok(())
216}
217
218#[cfg(test)]
219mod tests {
220    use super::*;
221
222    fn at(s: &str) -> DateTime<Utc> {
223        s.parse().unwrap()
224    }
225
226    fn pr(number: u64, body: &str, created: &str, merged: &str, issue: Option<u64>) -> GhPr {
227        GhPr {
228            number,
229            body: Some(body.to_string()),
230            created_at: at(created),
231            merged_at: Some(at(merged)),
232            closing_issues: issue
233                .into_iter()
234                .map(|number| GhIssueRef { number })
235                .collect(),
236        }
237    }
238
239    const FOOTER: &str = "text\n\n---\n*Task #1 · Created by claude[bot] via [Orch](https://github.com/x/y) using `sonnet`*";
240
241    #[test]
242    fn footer_gives_agent_and_model() {
243        assert_eq!(
244            parse_footer(FOOTER),
245            Some(("claude".to_string(), Some("sonnet".to_string())))
246        );
247        let no_model = "*Task #1 · Created by codex[bot] via [Orch](https://x)*";
248        assert_eq!(parse_footer(no_model), Some(("codex".to_string(), None)));
249        assert_eq!(parse_footer("a human wrote this"), None);
250    }
251
252    #[test]
253    fn groups_by_day_and_counts_every_merged_pr() {
254        let prs = vec![
255            pr(
256                1,
257                FOOTER,
258                "2026-11-05T10:00:00Z",
259                "2026-11-05T12:00:00Z",
260                Some(7),
261            ),
262            pr(
263                2,
264                "human",
265                "2026-11-05T10:00:00Z",
266                "2026-11-05T13:00:00Z",
267                None,
268            ),
269            pr(
270                3,
271                FOOTER,
272                "2026-11-06T01:00:00Z",
273                "2026-11-06T02:30:00Z",
274                None,
275            ),
276            pr(
277                4,
278                FOOTER,
279                "2026-10-01T01:00:00Z",
280                "2026-10-01T02:00:00Z",
281                None,
282            ),
283        ];
284        let issues = [GhIssue {
285            number: 7,
286            created_at: at("2026-11-05T06:00:00Z"),
287        }];
288        let proof = build_proof(
289            "o/r",
290            at("2026-11-06T12:00:00Z"),
291            at("2026-11-01T00:00:00Z"),
292            prs,
293            &issues,
294        );
295        assert_eq!(proof.days.len(), 2);
296        let d5 = &proof.days[0];
297        assert_eq!(
298            (d5.date.as_str(), d5.merged, d5.merged_by_orch),
299            ("2026-11-05", 2, 1)
300        );
301        assert_eq!(d5.prs[0].issue, Some(7));
302        assert!((d5.prs[0].hours_to_merge - 6.0).abs() < f64::EPSILON);
303        let d6 = &proof.days[1];
304        assert_eq!((d6.merged, d6.merged_by_orch), (1, 1));
305        assert!((d6.prs[0].hours_to_merge - 1.5).abs() < f64::EPSILON);
306    }
307}