orch-cli 0.94.2

Orch, the agent orchestration engine: runs coding agents on tasks and cron jobs
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
//! GitHub API response types — deserialized from `gh api` JSON output.

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubIssue {
    pub number: u64,
    /// GitHub database id, the value the dependencies API calls issue_id
    #[serde(default)]
    pub id: u64,
    pub title: String,
    pub body: Option<String>,
    pub state: String,
    pub labels: Vec<GitHubLabel>,
    pub user: GitHubUser,
    pub created_at: String,
    pub updated_at: String,
    pub html_url: String,
    pub node_id: Option<String>,
    /// Present when the item is actually a pull request (GitHub API returns PRs in /issues).
    pub pull_request: Option<serde_json::Value>,
    /// Author's relationship to the repo: OWNER, MEMBER, COLLABORATOR, CONTRIBUTOR,
    /// FIRST_TIME_CONTRIBUTOR, FIRST_TIMER, MANNEQUIN, NONE.
    #[serde(default)]
    pub author_association: Option<String>,
    /// `completed`, `not_planned`, `duplicate`, `reopened`, or absent
    #[serde(default)]
    pub state_reason: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubLabel {
    pub name: String,
    pub color: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubUser {
    pub login: String,
}

/// Pull request details from the GitHub API.
/// The `mergeable` field indicates if the PR can be merged:
/// - `true` - PR can be merged
/// - `false` - PR has merge conflicts (CONFLICTING)
/// - `null` - GitHub is still computing mergeability
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubPullRequest {
    pub number: u64,
    pub title: String,
    pub body: Option<String>,
    pub state: String,
    pub head: GitHubBranchRef,
    pub base: GitHubBranchRef,
    pub mergeable: Option<bool>,
    pub mergeable_state: Option<String>,
    pub merged: Option<bool>,
    pub html_url: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubCheckRun {
    pub id: u64,
    pub name: String,
    pub status: String,
    pub conclusion: Option<String>,
    /// GitHub App that posted the run, used to match a ruleset `integration_id`
    #[serde(default)]
    pub app: Option<GitHubApp>,
}

/// The GitHub App that created a check run (`check_run.app`)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubApp {
    pub id: i64,
}

/// A required status check from classic branch protection or a ruleset
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RequiredStatusCheck {
    pub context: String,
    /// GitHub App that must post this check. `None` means any app's check with this name counts
    pub integration_id: Option<i64>,
}

/// A check run annotation returned by the GitHub Checks API.
///
/// Annotations appear on the "Annotations" tab of a check run in the GitHub UI.
/// GitHub uses annotations for job-level messages, including billing failures
/// ("The job was not started because recent account payments have failed …").
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubAnnotation {
    pub annotation_level: Option<String>,
    pub message: Option<String>,
    pub title: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubBranchRef {
    #[serde(rename = "ref")]
    pub ref_: String,
    pub sha: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubComment {
    pub id: u64,
    pub body: String,
    pub user: GitHubUser,
    pub created_at: String,
    pub updated_at: Option<String>,
    pub html_url: Option<String>,
    /// Present when fetched from the repo-level /issues/comments endpoint.
    /// Format: https://api.github.com/repos/owner/repo/issues/123
    pub issue_url: Option<String>,
    /// Author's relationship to the repo for comment payloads when present.
    /// This field is present on repo-level comment endpoints and may be
    /// missing for some GraphQL-derived nodes. See `author_association` on
    /// issues for the corresponding field.
    #[serde(default)]
    pub author_association: Option<String>,
}

/// Extract issue/PR number from a GitHub API URL.
///
/// Parses the trailing numeric segment from URLs like:
/// `https://api.github.com/repos/owner/repo/issues/123`
pub fn extract_issue_number_from_url(url: &str) -> Option<String> {
    url.rsplit('/')
        .next()
        .filter(|s| !s.is_empty() && s.chars().all(|c| c.is_ascii_digit()))
        .map(String::from)
}

/// PR review state - "APPROVED", "CHANGES_REQUESTED", "COMMENTED", "DISMISSED"
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubReview {
    pub id: u64,
    pub user: GitHubUser,
    pub body: Option<String>,
    pub state: String,
    pub html_url: Option<String>,
    pub submitted_at: String,
    pub commit_id: Option<String>,
}

/// PR review comment on a specific line of code.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubReviewComment {
    pub id: u64,
    pub user: GitHubUser,
    pub body: String,
    pub path: String,
    pub line: Option<u32>,
    pub original_line: Option<u32>,
    pub commit_id: String,
    pub original_commit_id: String,
    pub html_url: String,
    pub created_at: String,
    pub updated_at: String,
    pub in_reply_to_id: Option<u64>,
    pub diff_hunk: Option<String>,
}

/// A PR review with its associated comments.
#[derive(Debug, Clone)]
pub struct PullRequestReview {
    pub review: GitHubReview,
    pub comments: Vec<GitHubReviewComment>,
}

impl PullRequestReview {
    /// Get actionable comments (non-empty, not replies).
    pub fn actionable_comments(&self) -> Vec<&GitHubReviewComment> {
        self.comments
            .iter()
            .filter(|c| !c.body.trim().is_empty() && c.in_reply_to_id.is_none())
            .collect()
    }
}

#[cfg(test)]
impl PullRequestReview {
    /// Check if this review requests changes (test helper).
    pub fn requests_changes(&self) -> bool {
        self.review.state == "CHANGES_REQUESTED"
    }
}

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

    #[test]
    fn test_github_review_deserialization() {
        let json = r#"{
            "id": 789,
            "user": {"login": "reviewer"},
            "body": "Please fix this",
            "state": "CHANGES_REQUESTED",
            "html_url": "https://github.com/test/repo/pull/123#pullrequestreview-789",
            "submitted_at": "2024-01-01T12:00:00Z",
            "commit_id": "abc123"
        }"#;

        let review: GitHubReview = serde_json::from_str(json).unwrap();
        assert_eq!(review.id, 789);
        assert_eq!(review.user.login, "reviewer");
        assert_eq!(review.body, Some("Please fix this".to_string()));
        assert_eq!(review.state, "CHANGES_REQUESTED");
        assert_eq!(review.submitted_at, "2024-01-01T12:00:00Z");
        assert_eq!(review.commit_id, Some("abc123".to_string()));
    }

    #[test]
    fn test_github_review_comment_deserialization() {
        let json = r#"{
            "id": 101112,
            "user": {"login": "reviewer"},
            "body": "This line needs fixing",
            "path": "src/main.rs",
            "line": 42,
            "original_line": 42,
            "commit_id": "abc123",
            "original_commit_id": "abc123",
            "html_url": "https://github.com/test/repo/pull/123#discussion_r101112",
            "created_at": "2024-01-01T12:00:00Z",
            "updated_at": "2024-01-01T12:00:00Z",
            "in_reply_to_id": null,
            "diff_hunk": "@@ -40,5 +40,5 @@ fn main() {\n     let x = 1;\n-    let y = 2;\n+    let y = 3;"
        }"#;

        let comment: GitHubReviewComment = serde_json::from_str(json).unwrap();
        assert_eq!(comment.id, 101112);
        assert_eq!(comment.user.login, "reviewer");
        assert_eq!(comment.body, "This line needs fixing");
        assert_eq!(comment.path, "src/main.rs");
        assert_eq!(comment.line, Some(42));
        assert_eq!(comment.commit_id, "abc123");
        assert_eq!(comment.in_reply_to_id, None);
        assert!(comment.diff_hunk.is_some());
    }

    #[test]
    fn github_check_run_deserializes_app_id() {
        let json = r#"{
            "id": 4,
            "name": "ci/test",
            "status": "completed",
            "conclusion": "success",
            "app": { "id": 42, "slug": "shipcheck", "name": "shipcheck" }
        }"#;
        let run: GitHubCheckRun = serde_json::from_str(json).unwrap();
        assert_eq!(run.name, "ci/test");
        assert_eq!(run.app.unwrap().id, 42);
    }

    #[test]
    fn github_check_run_deserializes_without_app() {
        let json = r#"{
            "id": 4,
            "name": "ci/test",
            "status": "completed",
            "conclusion": "success"
        }"#;
        let run: GitHubCheckRun = serde_json::from_str(json).unwrap();
        assert!(run.app.is_none());
    }

    #[test]
    fn test_github_branch_ref_deserialization() {
        let json = r#"{
            "ref": "main",
            "sha": "abc123"
        }"#;

        let branch_ref: GitHubBranchRef = serde_json::from_str(json).unwrap();
        assert_eq!(branch_ref.ref_, "main");
        assert_eq!(branch_ref.sha, "abc123");
    }

    #[test]
    fn test_pull_request_review_requests_changes() {
        let review = PullRequestReview {
            review: GitHubReview {
                id: 1,
                user: GitHubUser {
                    login: "reviewer".to_string(),
                },
                body: Some("Please fix".to_string()),
                state: "CHANGES_REQUESTED".to_string(),
                html_url: None,
                submitted_at: "2024-01-01T00:00:00Z".to_string(),
                commit_id: None,
            },
            comments: vec![],
        };

        assert!(review.requests_changes());
    }

    #[test]
    fn test_pull_request_review_approved() {
        let review = PullRequestReview {
            review: GitHubReview {
                id: 1,
                user: GitHubUser {
                    login: "reviewer".to_string(),
                },
                body: Some("LGTM".to_string()),
                state: "APPROVED".to_string(),
                html_url: None,
                submitted_at: "2024-01-01T00:00:00Z".to_string(),
                commit_id: None,
            },
            comments: vec![],
        };

        assert!(!review.requests_changes());
    }

    #[test]
    fn test_pull_request_review_actionable_comments() {
        let review = PullRequestReview {
            review: GitHubReview {
                id: 1,
                user: GitHubUser {
                    login: "reviewer".to_string(),
                },
                body: None,
                state: "CHANGES_REQUESTED".to_string(),
                html_url: None,
                submitted_at: "2024-01-01T00:00:00Z".to_string(),
                commit_id: None,
            },
            comments: vec![
                GitHubReviewComment {
                    id: 1,
                    user: GitHubUser {
                        login: "reviewer".to_string(),
                    },
                    body: "Fix this".to_string(),
                    path: "src/main.rs".to_string(),
                    line: Some(10),
                    original_line: Some(10),
                    commit_id: "abc".to_string(),
                    original_commit_id: "abc".to_string(),
                    html_url: "url".to_string(),
                    created_at: "2024-01-01T00:00:00Z".to_string(),
                    updated_at: "2024-01-01T00:00:00Z".to_string(),
                    in_reply_to_id: None,
                    diff_hunk: None,
                },
                GitHubReviewComment {
                    id: 2,
                    user: GitHubUser {
                        login: "reviewer".to_string(),
                    },
                    body: "".to_string(), // Empty - not actionable
                    path: "src/lib.rs".to_string(),
                    line: Some(20),
                    original_line: Some(20),
                    commit_id: "abc".to_string(),
                    original_commit_id: "abc".to_string(),
                    html_url: "url".to_string(),
                    created_at: "2024-01-01T00:00:00Z".to_string(),
                    updated_at: "2024-01-01T00:00:00Z".to_string(),
                    in_reply_to_id: None,
                    diff_hunk: None,
                },
                GitHubReviewComment {
                    id: 3,
                    user: GitHubUser {
                        login: "reviewer".to_string(),
                    },
                    body: "Reply to this".to_string(),
                    path: "src/lib.rs".to_string(),
                    line: Some(30),
                    original_line: Some(30),
                    commit_id: "abc".to_string(),
                    original_commit_id: "abc".to_string(),
                    html_url: "url".to_string(),
                    created_at: "2024-01-01T00:00:00Z".to_string(),
                    updated_at: "2024-01-01T00:00:00Z".to_string(),
                    in_reply_to_id: Some(1), // Reply - not actionable as standalone
                    diff_hunk: None,
                },
            ],
        };

        let actionable = review.actionable_comments();
        assert_eq!(actionable.len(), 1);
        assert_eq!(actionable[0].id, 1);
        assert_eq!(actionable[0].body, "Fix this");
    }

    #[test]
    fn extract_issue_number_from_url_works() {
        assert_eq!(
            extract_issue_number_from_url("https://api.github.com/repos/owner/repo/issues/123"),
            Some("123".into())
        );
    }

    #[test]
    fn extract_issue_number_from_url_empty() {
        assert_eq!(extract_issue_number_from_url(""), None);
    }
}