terraphim_orchestrator 1.20.1

AI Dark Factory orchestrator wiring spawner, router, supervisor into a reconciliation loop
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
//! Integration tests for ROC v1 Step G — AutoMerge handler execution.
//!
//! Drives [`AgentOrchestrator::handle_auto_merge_for_project`] against an
//! in-memory [`AutoMergeExecutor`] that records every call. No mocks, no
//! network, no Gitea — the test impl is a plain struct backed by `Mutex`
//! so the handler can observe state mutations across async calls.
//!
//! Asserts four behaviours:
//!
//! 1. Merge success enqueues a [`DispatchTask::PostMergeTestGate`] carrying
//!    the merge commit SHA + PR title, and records the revision in the
//!    dedupe set so late polls never re-enqueue.
//! 2. A HEAD SHA mismatch between the dispatch payload and the live PR
//!    causes the handler to skip the merge entirely (stale decision).
//! 3. A merge failure (conflict, API error) opens an `[ADF]` tracking
//!    issue on the project repo and does **not** enqueue a post-merge gate.
//! 4. A successful merge leaves the dedupe set marked — documented via
//!    [`AgentOrchestrator::auto_merge_enqueued`].
//!
//! See `cto-executive-system/plans/adf-rate-of-change-design.md` §Step G
//! and Gitea issue `terraphim/adf-fleet#35`.

use std::path::PathBuf;
use std::sync::Mutex;

use async_trait::async_trait;
use terraphim_orchestrator::pr_poller::{
    AutoMergeExecutor, MergeOutcome, PrComment, PrSummary, PrTracker,
};
use terraphim_orchestrator::{
    AgentOrchestrator, CompoundReviewConfig, DispatchTask, NightwatchConfig, OrchestratorConfig,
};

const PROJECT: &str = "alpha";

fn minimal_config(working_dir: PathBuf) -> OrchestratorConfig {
    let worktree_root = working_dir.join(".worktrees");
    OrchestratorConfig {
        working_dir,
        nightwatch: NightwatchConfig::default(),
        compound_review: CompoundReviewConfig {
            cli_tool: None,
            provider: None,
            model: None,
            schedule: "0 2 * * *".to_string(),
            max_duration_secs: 60,
            repo_path: PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../.."),
            create_prs: false,
            worktree_root,
            base_branch: "main".to_string(),
            max_concurrent_agents: 1,
            ..Default::default()
        },
        workflow: None,
        agents: vec![],
        restart_cooldown_secs: 60,
        max_restart_count: 10,
        restart_budget_window_secs: 43_200,
        disk_usage_threshold: 100,
        tick_interval_secs: 30,
        handoff_buffer_ttl_secs: None,
        persona_data_dir: None,
        skill_data_dir: None,
        flows: vec![],
        flow_state_dir: None,
        gitea: None,
        mentions: None,
        webhook: None,
        role_config_path: None,
        routing: None,
        #[cfg(feature = "quickwit")]
        quickwit: None,
        projects: vec![],
        include: vec![],
        providers: vec![],
        provider_budget_state_file: None,
        pause_dir: None,
        project_circuit_breaker_threshold: 3,
        fleet_escalation_owner: None,
        fleet_escalation_repo: None,
        post_merge_gate: None,
        learning: terraphim_orchestrator::LearningConfig::default(),
        evolution: terraphim_orchestrator::EvolutionConfig::default(),
        pr_dispatch: None,
        pr_dispatch_per_project: Default::default(),
        gitea_skill_repo: None,
        gate_reconcile_interval_ticks: 20,
    }
}

/// Recorded `merge_pr` call.
#[derive(Debug, Clone, PartialEq, Eq)]
struct MergeCall {
    pr_number: u64,
}

/// Recorded `open_failure_issue` call.
#[derive(Debug, Clone, PartialEq, Eq)]
struct FailureIssueCall {
    title: String,
    body: String,
    labels: Vec<String>,
}

/// Configurable `merge_pr` outcome: either succeed with a specific merge
/// commit sha/title, or fail with an error message.
#[derive(Debug, Clone)]
enum MergeReply {
    Ok {
        merge_commit_sha: String,
        title: String,
    },
    Err(String),
}

/// In-memory [`AutoMergeExecutor`] that records every call and replays
/// programmable outcomes. No mock framework involved — just a struct with
/// mutex-guarded state.
struct RecordingExecutor {
    open_prs: Vec<PrSummary>,
    merge_reply: MergeReply,
    merge_calls: Mutex<Vec<MergeCall>>,
    failure_issue_calls: Mutex<Vec<FailureIssueCall>>,
    next_issue_number: Mutex<u64>,
}

impl RecordingExecutor {
    fn new(open_prs: Vec<PrSummary>, merge_reply: MergeReply) -> Self {
        Self {
            open_prs,
            merge_reply,
            merge_calls: Mutex::new(Vec::new()),
            failure_issue_calls: Mutex::new(Vec::new()),
            next_issue_number: Mutex::new(1001),
        }
    }

    fn merge_calls(&self) -> Vec<MergeCall> {
        self.merge_calls.lock().unwrap().clone()
    }

    fn failure_issues(&self) -> Vec<FailureIssueCall> {
        self.failure_issue_calls.lock().unwrap().clone()
    }
}

#[async_trait]
impl PrTracker for RecordingExecutor {
    async fn list_open_prs(&self) -> Result<Vec<PrSummary>, String> {
        Ok(self.open_prs.clone())
    }

    async fn fetch_pr_comments(&self, _pr_number: u64) -> Result<Vec<PrComment>, String> {
        Ok(vec![])
    }
}

#[async_trait]
impl AutoMergeExecutor for RecordingExecutor {
    async fn merge_pr(&self, pr_number: u64) -> Result<MergeOutcome, String> {
        self.merge_calls
            .lock()
            .unwrap()
            .push(MergeCall { pr_number });
        match &self.merge_reply {
            MergeReply::Ok {
                merge_commit_sha,
                title,
            } => Ok(MergeOutcome {
                pr_number,
                merge_commit_sha: merge_commit_sha.clone(),
                title: title.clone(),
            }),
            MergeReply::Err(msg) => Err(msg.clone()),
        }
    }

    async fn open_failure_issue(
        &self,
        title: &str,
        body: &str,
        labels: &[&str],
    ) -> Result<u64, String> {
        self.failure_issue_calls
            .lock()
            .unwrap()
            .push(FailureIssueCall {
                title: title.to_string(),
                body: body.to_string(),
                labels: labels.iter().map(|l| l.to_string()).collect(),
            });
        let mut n = self.next_issue_number.lock().unwrap();
        let issued = *n;
        *n += 1;
        Ok(issued)
    }
}

fn pr(number: u64, head: &str, diff_loc: u32) -> PrSummary {
    PrSummary {
        number,
        author_login: "claude-code".to_string(),
        head_sha: head.to_string(),
        base_ref: "main".to_string(),
        diff_loc,
    }
}

fn auto_merge_task(pr_number: u64, project: &str, head: &str) -> DispatchTask {
    DispatchTask::AutoMerge {
        pr_number,
        project: project.to_string(),
        head_sha: head.to_string(),
    }
}

fn post_merge_depth(orch: &AgentOrchestrator) -> u64 {
    orch.dispatcher()
        .stats()
        .by_source
        .get("post_merge_gate")
        .copied()
        .unwrap_or(0)
}

fn auto_merge_depth(orch: &AgentOrchestrator) -> u64 {
    orch.dispatcher()
        .stats()
        .by_source
        .get("auto_merge")
        .copied()
        .unwrap_or(0)
}

// -------- Test 1: merge success enqueues PostMergeTestGate --------

#[tokio::test]
async fn auto_merge_success_enqueues_post_merge_gate() {
    let mut orch =
        AgentOrchestrator::new(minimal_config(tempfile::tempdir().unwrap().keep())).unwrap();

    let executor = RecordingExecutor::new(
        vec![pr(101, "2ef451d8", 42)],
        MergeReply::Ok {
            merge_commit_sha: "merge-sha-abc".to_string(),
            title: "feat(foo): green change".to_string(),
        },
    );

    orch.handle_auto_merge_for_project(auto_merge_task(101, PROJECT, "2ef451d8"), &executor)
        .await
        .expect("handler succeeded");

    // Merge actually happened.
    assert_eq!(
        executor.merge_calls(),
        vec![MergeCall { pr_number: 101 }],
        "handler must have invoked merge_pr exactly once on success"
    );

    // No [ADF] issues opened on success.
    assert!(
        executor.failure_issues().is_empty(),
        "no failure issue must be opened on merge success"
    );

    // Exactly one PostMergeTestGate enqueued, carrying the merge sha + title.
    assert_eq!(
        post_merge_depth(&orch),
        1,
        "successful merge must enqueue one PostMergeTestGate"
    );
    match orch.dispatcher().peek().cloned() {
        Some(DispatchTask::PostMergeTestGate {
            pr_number,
            project,
            merge_sha,
            title,
        }) => {
            assert_eq!(pr_number, 101);
            assert_eq!(project, PROJECT);
            assert_eq!(merge_sha, "merge-sha-abc");
            assert_eq!(title, "feat(foo): green change");
        }
        other => panic!("expected DispatchTask::PostMergeTestGate, got {:?}", other),
    }
}

// -------- Test 2: HEAD SHA changed -> skip merge (stale decision) --------

#[tokio::test]
async fn auto_merge_skipped_when_head_sha_changed() {
    let mut orch =
        AgentOrchestrator::new(minimal_config(tempfile::tempdir().unwrap().keep())).unwrap();

    // Live PR has a DIFFERENT head sha from the dispatch payload.
    let executor = RecordingExecutor::new(
        vec![pr(202, "live-sha-xyz", 42)],
        MergeReply::Ok {
            merge_commit_sha: "should-not-happen".to_string(),
            title: "n/a".to_string(),
        },
    );

    orch.handle_auto_merge_for_project(auto_merge_task(202, PROJECT, "stale-sha-abc"), &executor)
        .await
        .expect("handler returns Ok(()) on skip");

    // The merge API must NEVER be called when the head sha has moved.
    assert!(
        executor.merge_calls().is_empty(),
        "stale head sha must prevent the merge call from ever being issued"
    );

    // And nothing must be enqueued downstream.
    assert_eq!(
        post_merge_depth(&orch),
        0,
        "skipped stale merge must not enqueue PostMergeTestGate"
    );
    assert_eq!(
        auto_merge_depth(&orch),
        0,
        "skipped stale merge must not re-enqueue AutoMerge"
    );
    assert!(
        executor.failure_issues().is_empty(),
        "a stale decision is not a failure; no [ADF] issue must be opened"
    );
}

// -------- Test 3: merge failure opens [ADF] tracking issue --------

#[tokio::test]
async fn auto_merge_failure_opens_adf_issue() {
    let mut orch =
        AgentOrchestrator::new(minimal_config(tempfile::tempdir().unwrap().keep())).unwrap();

    let executor = RecordingExecutor::new(
        vec![pr(303, "2ef451d8", 42)],
        MergeReply::Err(
            "Gitea merge_pull error 409 on PR 303: Please resolve merge conflicts".to_string(),
        ),
    );

    orch.handle_auto_merge_for_project(auto_merge_task(303, PROJECT, "2ef451d8"), &executor)
        .await
        .expect("handler returns Ok(()) even on merge failure");

    // Merge was attempted exactly once.
    assert_eq!(
        executor.merge_calls(),
        vec![MergeCall { pr_number: 303 }],
        "handler must have invoked merge_pr once before giving up"
    );

    // A single [ADF] failure issue was opened with the failure reason
    // and the PR number in the title, and the adf + auto-merge-failed
    // labels were attached.
    let failures = executor.failure_issues();
    assert_eq!(
        failures.len(),
        1,
        "merge failure must open exactly one [ADF] tracking issue"
    );
    let issue = &failures[0];
    assert!(
        issue.title.starts_with("[ADF]") && issue.title.contains("303"),
        "failure issue title must start with [ADF] and reference PR 303; got `{}`",
        issue.title
    );
    assert!(
        issue.body.contains("409") && issue.body.contains("2ef451d8"),
        "failure issue body must record the error + head sha for forensics; got `{}`",
        issue.body
    );
    assert!(
        issue.labels.iter().any(|l| l == "adf"),
        "failure issue must carry the `adf` label"
    );
    assert!(
        issue.labels.iter().any(|l| l == "auto-merge-failed"),
        "failure issue must carry the `auto-merge-failed` label"
    );

    // No PostMergeTestGate must be enqueued when the merge failed.
    assert_eq!(
        post_merge_depth(&orch),
        0,
        "merge failure must NOT enqueue PostMergeTestGate"
    );
}

// -------- Test 4: success marks dedupe set so subsequent polls skip --------

#[tokio::test]
async fn auto_merge_marks_dedupe_set_on_success() {
    let mut orch =
        AgentOrchestrator::new(minimal_config(tempfile::tempdir().unwrap().keep())).unwrap();

    let executor = RecordingExecutor::new(
        vec![pr(404, "2ef451d8", 42)],
        MergeReply::Ok {
            merge_commit_sha: "merge-sha-dedupe".to_string(),
            title: "feat: dedupe test".to_string(),
        },
    );

    orch.handle_auto_merge_for_project(auto_merge_task(404, PROJECT, "2ef451d8"), &executor)
        .await
        .expect("handler succeeded");

    // Dedupe set must contain the (project, pr_number, head_sha) triple
    // so a subsequent poll for the same revision skips re-enqueuing.
    assert!(
        orch.auto_merge_enqueued()
            .contains(PROJECT, 404, "2ef451d8"),
        "successful AutoMerge must record (project, pr, head_sha) in the dedupe set"
    );

    // PostMergeTestGate was enqueued; no AutoMerge is left in the queue.
    assert_eq!(post_merge_depth(&orch), 1);
    assert_eq!(
        auto_merge_depth(&orch),
        0,
        "handler never re-enqueues AutoMerge for itself"
    );
}

// -------- Test 5: PR no longer open -> skip merge silently --------

#[tokio::test]
async fn auto_merge_skipped_when_pr_already_closed() {
    let mut orch =
        AgentOrchestrator::new(minimal_config(tempfile::tempdir().unwrap().keep())).unwrap();

    // The open-PR list does NOT contain PR 505 — someone already merged or
    // closed it between the verdict and this tick.
    let executor = RecordingExecutor::new(
        vec![pr(999, "other-sha", 10)],
        MergeReply::Ok {
            merge_commit_sha: "should-not-happen".to_string(),
            title: "n/a".to_string(),
        },
    );

    orch.handle_auto_merge_for_project(auto_merge_task(505, PROJECT, "any-sha"), &executor)
        .await
        .expect("handler returns Ok(()) on skip");

    assert!(
        executor.merge_calls().is_empty(),
        "handler must not merge a PR that is no longer open"
    );
    assert_eq!(
        post_merge_depth(&orch),
        0,
        "no PostMergeTestGate when PR was already closed"
    );
    assert!(
        executor.failure_issues().is_empty(),
        "an already-closed PR is not a failure; no [ADF] issue"
    );
}