orch-cli 0.105.10

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
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
use super::comments::{CommentAction, MentionCursor, classify_comment, handle_slash_command};
use super::*;
use crate::backends::test_helpers::NoopBackend;
use crate::backends::{Conversation, MergeState, TaskReader, TaskWriter, Tracker};
use crate::backends::{ExternalId, ExternalTask, Mention, Status};
use crate::engine::tasks::commands::CommandGh;
use crate::engine::tasks::commands::OwnerCommand;
use crate::github::types::GitHubIssue;
use crate::github::types::GitHubUser;
use crate::store::TaskStore;
use async_trait::async_trait;
use serial_test::serial;
use std::slice;
use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use tokio::sync::Mutex;

#[serial(cooldown_state)]
#[test]
fn classify_skip_already_processed() {
    let action = classify_comment(
        "@orch /retry",
        Some("https://api.github.com/repos/o/r/issues/1"),
        "@bot",
        true,
    );
    assert_eq!(action, CommentAction::Skip);
}

#[serial(cooldown_state)]
#[test]
fn classify_skip_irrelevant_comment() {
    let action = classify_comment("just a regular comment", None, "@bot", false);
    assert_eq!(action, CommentAction::Skip);
}

#[serial(cooldown_state)]
#[test]
fn classify_command_without_mention() {
    let action = classify_comment(
        "/retry",
        Some("https://api.github.com/repos/o/r/issues/42"),
        "@bot",
        false,
    );
    assert_eq!(
        action,
        CommentAction::ExecuteCommand {
            command: OwnerCommand::Retry,
            issue_num: "42".to_string(),
        }
    );
}

#[serial(cooldown_state)]
#[test]
fn classify_command_with_mention() {
    let action = classify_comment(
        "@orch /close",
        Some("https://api.github.com/repos/o/r/issues/7"),
        "@bot",
        false,
    );
    assert_eq!(
        action,
        CommentAction::ExecuteCommandForMention {
            command: OwnerCommand::Close,
            issue_num: "7".to_string(),
        }
    );
}

#[serial(cooldown_state)]
#[test]
fn classify_mention_without_command() {
    let action = classify_comment(
        "@orch can you look at this?",
        Some("https://api.github.com/repos/o/r/issues/99"),
        "@bot",
        false,
    );
    assert_eq!(
        action,
        CommentAction::CreateMentionTask {
            issue_num: Some("99".to_string()),
        }
    );
}

#[serial(cooldown_state)]
#[test]
fn classify_mention_without_issue_url() {
    let action = classify_comment("@orch help me", None, "@bot", false);
    assert_eq!(action, CommentAction::CreateMentionTask { issue_num: None });
}

#[serial(cooldown_state)]
#[test]
fn classify_command_no_mention_no_url_skips() {
    let action = classify_comment("/retry", None, "@bot", false);
    assert_eq!(action, CommentAction::Skip);
}

#[serial(cooldown_state)]
#[test]
fn classify_mention_via_current_user() {
    let action = classify_comment(
        "@bot please help",
        Some("https://api.github.com/repos/o/r/issues/5"),
        "@bot",
        false,
    );
    assert_eq!(
        action,
        CommentAction::CreateMentionTask {
            issue_num: Some("5".to_string()),
        }
    );
}

#[serial(cooldown_state)]
#[test]
fn classify_command_with_mention_no_url_creates_task() {
    // @mention + command but no issue URL → can't run command, create mention task
    let action = classify_comment("@orch /retry", None, "@bot", false);
    assert_eq!(action, CommentAction::CreateMentionTask { issue_num: None });
}

#[serial(cooldown_state)]
#[test]
fn mention_cursor_stops_advancing_after_gap() {
    let mut cursor = MentionCursor::default();
    cursor.advance("2026-01-01T00:00:00Z");
    cursor.block_on_gap();
    cursor.advance("2026-01-02T00:00:00Z");

    assert_eq!(
        cursor.into_last_safe_ts().as_deref(),
        Some("2026-01-01T00:00:00Z")
    );
}

#[serial(cooldown_state)]
#[test]
fn mention_cursor_tracks_max_timestamp_until_gap() {
    let mut cursor = MentionCursor::default();
    cursor.advance("2026-01-01T00:00:00Z");
    cursor.advance("2026-01-01T00:05:00Z");
    cursor.block_on_gap();
    cursor.advance("2026-01-01T00:10:00Z");

    assert_eq!(
        cursor.into_last_safe_ts().as_deref(),
        Some("2026-01-01T00:05:00Z")
    );
}

/// Backend that tracks acknowledge_mention calls and can be configured to fail them.
struct AckTrackingBackend {
    ack_calls: Mutex<Vec<String>>,
    posted: Mutex<Vec<String>>,
    ack_failure: bool,
    inner: NoopBackend,
}

impl AckTrackingBackend {
    fn new(ack_failure: bool) -> Arc<Self> {
        Arc::new(Self {
            ack_calls: Mutex::new(Vec::new()),
            posted: Mutex::new(Vec::new()),
            ack_failure,
            inner: NoopBackend,
        })
    }

    fn fail_on_ack(&self) -> bool {
        self.ack_failure
    }
}

#[async_trait]
impl TaskReader for AckTrackingBackend {
    async fn get_task(&self, id: &ExternalId) -> anyhow::Result<ExternalTask> {
        self.inner.get_task(id).await
    }

    async fn list_by_status(&self, s: Status) -> anyhow::Result<Vec<ExternalTask>> {
        self.inner.list_by_status(s).await
    }

    async fn list_routable(&self) -> anyhow::Result<Vec<ExternalTask>> {
        self.inner.list_routable().await
    }

    async fn has_open_issue_with_title(&self, t: &str, l: &str) -> anyhow::Result<bool> {
        self.inner.has_open_issue_with_title(t, l).await
    }
}

#[async_trait]
impl TaskWriter for AckTrackingBackend {
    // Delegate everything else to NoopBackend.
    async fn create_task(&self, t: &str, b: &str, l: &[String]) -> anyhow::Result<ExternalId> {
        self.inner.create_task(t, b, l).await
    }

    async fn set_labels(&self, id: &ExternalId, l: &[String]) -> anyhow::Result<()> {
        self.inner.set_labels(id, l).await
    }

    async fn remove_label(&self, id: &ExternalId, l: &str) -> anyhow::Result<()> {
        self.inner.remove_label(id, l).await
    }

    async fn get_sub_issues(&self, id: &ExternalId) -> anyhow::Result<Vec<ExternalId>> {
        self.inner.get_sub_issues(id).await
    }

    async fn create_sub_task(
        &self,
        p: &ExternalId,
        t: &str,
        b: &str,
        l: &[String],
    ) -> anyhow::Result<ExternalId> {
        self.inner.create_sub_task(p, t, b, l).await
    }

    async fn ensure_status_label(&self, l: &str) -> anyhow::Result<()> {
        self.inner.ensure_status_label(l).await
    }

    async fn update_status(&self, id: &ExternalId, s: Status) -> anyhow::Result<()> {
        self.inner.update_status(id, s).await
    }
}

#[async_trait]
impl Conversation for AckTrackingBackend {
    async fn acknowledge_mention(&self, comment_id: &str) -> anyhow::Result<()> {
        self.ack_calls.lock().await.push(comment_id.to_string());
        if self.fail_on_ack() {
            // Return a transient error to simulate network glitch.
            anyhow::bail!("network error: temporary failure posting reaction");
        }
        Ok(())
    }

    // Override get_authenticated_user to ensure scan proceeds.
    async fn get_authenticated_user(&self) -> anyhow::Result<Option<String>> {
        Ok(Some("bot".into()))
    }

    // Override get_mentions to return empty (we use prefetched comments).
    async fn get_mentions(&self, _since: &str) -> anyhow::Result<Vec<Mention>> {
        Ok(vec![])
    }

    async fn post_comment(&self, id: &ExternalId, b: &str) -> anyhow::Result<()> {
        self.posted.lock().await.push(b.to_string());
        self.inner.post_comment(id, b).await
    }
}

#[async_trait]
impl MergeState for AckTrackingBackend {
    async fn is_pr_merged(&self, b: &str) -> anyhow::Result<bool> {
        self.inner.is_pr_merged(b).await
    }
}

#[async_trait]
impl Tracker for AckTrackingBackend {
    fn name(&self) -> &'static str {
        "ack-tracking"
    }

    async fn health_check(&self) -> anyhow::Result<()> {
        self.inner.health_check().await
    }
}

/// When acknowledge_mention fails for CreateMentionTask, the cursor must NOT
/// advance and no mention task should be created. The next tick will retry.
#[serial(cooldown_state)]
#[tokio::test]
async fn create_mention_task_skips_on_ack_failure() {
    cooldown::reset_global_state().await;
    let backend: Arc<AckTrackingBackend> = AckTrackingBackend::new(true);
    let store: Arc<TaskStore> = Arc::new(TaskStore::open_memory().await.unwrap());

    // Set an initial cursor so we can verify it wasn't advanced.
    let initial_ts = "2026-01-01T00:00:00Z";
    store
        .kv_set("mentions_last_checked", initial_ts)
        .await
        .unwrap();

    let mention = Mention {
        id: "mention-abc".into(),
        body: "@bot can you help?".into(),
        author: "alice".into(),
        created_at: "2026-01-02T00:00:00Z".into(),
        issue_url: Some("https://api.github.com/repos/owner/repo/issues/42".into()),
    };

    let backend_trait: Arc<dyn ExternalBackend> = Arc::<AckTrackingBackend>::clone(&backend);
    let task_manager = Arc::new(TaskManager::new(Arc::clone(&backend_trait)));

    scan_comments(
        &backend_trait,
        Some(&store),
        "owner/repo",
        &task_manager,
        Some(&[mention]),
    )
    .await
    .unwrap();

    // acknowledge_mention was called with the mention ID.
    let calls = backend.ack_calls.lock().await;
    assert_eq!(
        &**calls,
        &["mention-abc"],
        "acknowledge_mention must be called"
    );

    // No mention task was created.
    let all = store.list_all("owner/repo").await.unwrap();
    assert!(
        all.is_empty(),
        "no task should be created when acknowledge_mention fails"
    );

    // Cursor was NOT advanced.
    let cursor = store.kv_get("mentions_last_checked").await.unwrap();
    assert_eq!(
        cursor.as_deref(),
        Some(initial_ts),
        "cursor must not advance when acknowledge_mention fails"
    );
}

/// When acknowledge_mention fails for ExecuteCommandForMention, handle_slash_command
/// must NOT be called and no mention task should be created.
#[serial(cooldown_state)]
#[tokio::test]
async fn execute_command_for_mention_skips_on_ack_failure() {
    cooldown::reset_global_state().await;
    let backend: Arc<AckTrackingBackend> = AckTrackingBackend::new(true);
    let store: Arc<TaskStore> = Arc::new(TaskStore::open_memory().await.unwrap());

    let initial_ts = "2026-01-01T00:00:00Z";
    store
        .kv_set("mentions_last_checked", initial_ts)
        .await
        .unwrap();

    // Mention with a slash command and issue URL → ExecuteCommandForMention.
    let mention = Mention {
        id: "mention-def".into(),
        body: "@bot /close".into(),
        author: "alice".into(),
        created_at: "2026-01-02T00:00:00Z".into(),
        issue_url: Some("https://api.github.com/repos/owner/repo/issues/42".into()),
    };

    let backend_trait: Arc<dyn ExternalBackend> = Arc::<AckTrackingBackend>::clone(&backend);
    let task_manager = Arc::new(TaskManager::new(Arc::clone(&backend_trait)));

    scan_comments(
        &backend_trait,
        Some(&store),
        "owner/repo",
        &task_manager,
        Some(&[mention]),
    )
    .await
    .unwrap();

    // acknowledge_mention was called.
    let calls = backend.ack_calls.lock().await;
    assert_eq!(&**calls, &["mention-def"]);

    // No mention task was created (handle_slash_command was not called).
    let all = store.list_all("owner/repo").await.unwrap();
    assert!(
        all.is_empty(),
        "no task should be created when acknowledge_mention fails for ExecuteCommandForMention"
    );

    // Cursor was NOT advanced.
    let cursor = store.kv_get("mentions_last_checked").await.unwrap();
    assert_eq!(
        cursor.as_deref(),
        Some(initial_ts),
        "cursor must not advance when acknowledge_mention fails for ExecuteCommandForMention"
    );
}

/// GitHub stub whose `get_issue` fails once, then succeeds.
#[derive(Default)]
struct FlakyGh {
    get_issue_calls: AtomicUsize,
}

#[async_trait]
impl CommandGh for FlakyGh {
    async fn get_issue(&self, _repo: &str, number: &str) -> anyhow::Result<GitHubIssue> {
        if self.get_issue_calls.fetch_add(1, Ordering::SeqCst) == 0 {
            anyhow::bail!("network error: temporary failure fetching issue");
        }
        Ok(GitHubIssue {
            number: number.parse().unwrap_or(0),
            id: 1,
            title: "t".into(),
            body: None,
            state: "open".into(),
            labels: Vec::new(),
            user: GitHubUser {
                login: "alice".into(),
            },
            created_at: String::new(),
            updated_at: String::new(),
            html_url: String::new(),
            node_id: None,
            pull_request: None,
            author_association: Some("OWNER".into()),
            state_reason: None,
        })
    }

    async fn is_collaborator(&self, _repo: &str, _username: &str) -> anyhow::Result<bool> {
        Ok(true)
    }

    async fn close_issue(&self, _repo: &str, _number: &str) -> anyhow::Result<()> {
        Ok(())
    }
}

/// A transient `get_issue` failure must leave the cursor before the comment so
/// the next scan retries it — and the command must then run exactly once.
#[serial(cooldown_state)]
#[tokio::test]
async fn slash_command_retries_after_transient_fetch_failure() {
    cooldown::reset_global_state().await;
    let backend: Arc<AckTrackingBackend> = AckTrackingBackend::new(false);
    let store: Arc<TaskStore> = Arc::new(TaskStore::open_memory().await.unwrap());
    let initial_ts = "2026-01-01T00:00:00Z";

    let mention = Mention {
        id: "mention-ghi".into(),
        body: "@bot /unblock".into(),
        author: "alice".into(),
        created_at: "2026-01-02T00:00:00Z".into(),
        issue_url: Some("https://api.github.com/repos/owner/repo/issues/42".into()),
    };

    let backend_trait: Arc<dyn ExternalBackend> = Arc::<AckTrackingBackend>::clone(&backend);
    let task_manager = Arc::new(TaskManager::new(Arc::clone(&backend_trait)));
    let gh = FlakyGh::default();
    let mut cursor = MentionCursor::default();

    // First scan: get_issue fails.
    let advanced_safely = handle_slash_command(
        &backend_trait,
        Some(&store),
        "owner/repo",
        &task_manager,
        &gh,
        &mention,
        &OwnerCommand::Unblock,
        &mut cursor,
    )
    .await
    .unwrap();

    assert!(
        !advanced_safely,
        "a transient fetch failure must stop cursor advancement"
    );
    assert!(
        cursor.last_safe_ts.is_none(),
        "cursor must stay before the comment so the next tick retries it"
    );
    assert!(
        store.list_all("owner/repo").await.unwrap().is_empty(),
        "no mention task on a transient failure"
    );
    assert!(
        backend.posted.lock().await.is_empty(),
        "nothing is posted to GitHub on a transient failure"
    );
    assert_eq!(
        filter_mentions_by_since(slice::from_ref(&mention), initial_ts).len(),
        1,
        "the comment is still in range for the next scan"
    );

    // Second scan: get_issue succeeds and the command runs.
    let mut cursor = MentionCursor::default();
    let advanced_safely = handle_slash_command(
        &backend_trait,
        Some(&store),
        "owner/repo",
        &task_manager,
        &gh,
        &mention,
        &OwnerCommand::Unblock,
        &mut cursor,
    )
    .await
    .unwrap();

    assert!(advanced_safely);
    assert_eq!(
        cursor.into_last_safe_ts().as_deref(),
        Some("2026-01-02T00:00:00Z"),
        "cursor advances once the command actually runs"
    );
    assert_eq!(
        backend.posted.lock().await.len(),
        1,
        "the command runs once, posting a single confirmation"
    );
    assert_eq!(
        store.list_all("owner/repo").await.unwrap().len(),
        1,
        "one sentinel mention task for the executed command"
    );
}

/// In the plain `ExecuteCommand` arm the GitHub client points at a dead port
/// under `cfg(test)`, so `get_issue` always fails — the cursor must stay put.
#[serial(cooldown_state)]
#[tokio::test]
async fn execute_command_keeps_cursor_on_fetch_failure() {
    cooldown::reset_global_state().await;
    let backend: Arc<AckTrackingBackend> = AckTrackingBackend::new(false);
    let store: Arc<TaskStore> = Arc::new(TaskStore::open_memory().await.unwrap());

    let initial_ts = "2026-01-01T00:00:00Z";
    store
        .kv_set("mentions_last_checked", initial_ts)
        .await
        .unwrap();

    // Slash command without a mention → ExecuteCommand.
    let comment = Mention {
        id: "comment-jkl".into(),
        body: "/unblock".into(),
        author: "alice".into(),
        created_at: "2026-01-02T00:00:00Z".into(),
        issue_url: Some("https://api.github.com/repos/owner/repo/issues/42".into()),
    };

    let backend_trait: Arc<dyn ExternalBackend> = Arc::<AckTrackingBackend>::clone(&backend);
    let task_manager = Arc::new(TaskManager::new(Arc::clone(&backend_trait)));

    scan_comments(
        &backend_trait,
        Some(&store),
        "owner/repo",
        &task_manager,
        Some(&[comment]),
    )
    .await
    .unwrap();

    assert!(
        backend.posted.lock().await.is_empty(),
        "nothing is posted when the issue fetch fails"
    );
    assert_eq!(
        store
            .kv_get("mentions_last_checked")
            .await
            .unwrap()
            .as_deref(),
        Some(initial_ts),
        "cursor must not advance past a command that never ran"
    );
}