githubclaw 0.2.2

Near-autonomous AI agents that manage open-source projects end-to-end using GitHub as the single source of truth.
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
586
//! Pipeline state machine for Issue Request lifecycle.
//!
//! Manages the full lifecycle: classification → approval → implementation → merge.
//! Each issue progresses through discrete states, driven by GitHub HTML markers.

use serde::{Deserialize, Serialize};

use crate::constants::{
    BUG_REPRODUCER_MAX_INFO_REQUESTS, IMPLEMENTER_REVIEWER_MAX_LOOP, IMPLEMENTER_VERIFIER_MAX_LOOP,
};
use crate::markers::MarkerType;

// ---------------------------------------------------------------------------
// Issue classification
// ---------------------------------------------------------------------------

/// Classification of an incoming issue.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IssueType {
    Bug,
    Feature,
    Refactoring,
}

// ---------------------------------------------------------------------------
// Pipeline state
// ---------------------------------------------------------------------------

/// Current state of an issue in the pipeline.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PipelineState {
    /// Orchestrator is classifying the issue.
    Classifying,
    /// Bug Reproducer is attempting reproduction.
    Reproducing,
    /// Waiting for reporter to provide additional info (bug reproduction failed).
    AwaitingInfo { attempts: u32 },
    /// Vision-gap Analyst is running (Feature/Refactoring).
    AnalyzingVision,
    /// Waiting for human Interactive Session (Feature/Refactoring).
    AwaitingInteractiveSession,
    /// Issue approved — implementation pipeline starting.
    Approved,
    /// Orchestrator is decomposing into sub-issues.
    Decomposing,
    /// Verifier is writing test code.
    WritingTests { sub_issue: u64 },
    /// Implementer is coding.
    Implementing { sub_issue: u64, attempt: u32 },
    /// Reviewer is reviewing code.
    Reviewing { sub_issue: u64, attempt: u32 },
    /// Verifier is performing e2e validation.
    VerifyingE2e,
    /// Human escalation needed (loop limit exceeded).
    Stuck { reason: String },
    /// Feature branch merged to dev.
    Merged,
    /// Issue rejected/closed.
    Rejected { reason: String },
}

// ---------------------------------------------------------------------------
// Pipeline tracker
// ---------------------------------------------------------------------------

/// Tracks the pipeline state for a single root issue.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PipelineTracker {
    pub issue_id: u64,
    pub issue_type: Option<IssueType>,
    pub state: PipelineState,
    pub sub_issues: Vec<u64>,
    pub current_sub_issue_index: usize,
    pub branch_name: Option<String>,
}

impl PipelineTracker {
    /// Create a new tracker for an incoming issue.
    pub fn new(issue_id: u64) -> Self {
        Self {
            issue_id,
            issue_type: None,
            state: PipelineState::Classifying,
            sub_issues: Vec::new(),
            current_sub_issue_index: 0,
            branch_name: None,
        }
    }

    /// Process a marker event and return the next action to take.
    pub fn on_marker(
        &mut self,
        marker: &MarkerType,
        attrs: &std::collections::HashMap<String, String>,
    ) -> PipelineAction {
        match (&self.state, marker) {
            // Bug: reproduced=true → auto-approve
            (PipelineState::Reproducing, MarkerType::Reproduced) => {
                let reproduced = attrs
                    .get("reproduced")
                    .map(|v| v == "true")
                    .unwrap_or(false);
                if reproduced {
                    self.state = PipelineState::Approved;
                    PipelineAction::PostMarker(MarkerType::Approved)
                } else {
                    let attempts = match &self.state {
                        PipelineState::AwaitingInfo { attempts } => *attempts,
                        _ => 0,
                    };
                    let new_attempts = attempts + 1;
                    if new_attempts >= BUG_REPRODUCER_MAX_INFO_REQUESTS {
                        self.state = PipelineState::Rejected {
                            reason: "Bug could not be reproduced after maximum attempts".into(),
                        };
                        PipelineAction::CloseIssue("Could not reproduce after 3 attempts.".into())
                    } else {
                        self.state = PipelineState::AwaitingInfo {
                            attempts: new_attempts,
                        };
                        PipelineAction::RequestInfo
                    }
                }
            }

            // Approved → start implementation (decompose first)
            (PipelineState::Approved, MarkerType::Approved)
            | (PipelineState::AwaitingInteractiveSession, MarkerType::Approved) => {
                self.state = PipelineState::Decomposing;
                PipelineAction::Decompose
            }

            // After decomposition, start first sub-issue: write tests
            (PipelineState::Decomposing, _) => self.advance_to_next_sub_issue(),

            // Reviewed → next sub-issue or e2e
            (PipelineState::Reviewing { .. }, MarkerType::Reviewed) => {
                self.current_sub_issue_index += 1;
                self.advance_to_next_sub_issue()
            }

            // Verified → merge to dev
            (PipelineState::VerifyingE2e, MarkerType::Verified) => {
                self.state = PipelineState::Merged;
                PipelineAction::MergeToDev
            }

            // Stuck → wait for human
            (_, MarkerType::Stuck) => {
                self.state = PipelineState::Stuck {
                    reason: "Loop limit exceeded".into(),
                };
                PipelineAction::WaitForHuman
            }

            _ => PipelineAction::None,
        }
    }

    /// Handle implementer completion (tests pass/fail).
    pub fn on_implementer_done(&mut self, tests_passed: bool) -> PipelineAction {
        match &self.state {
            PipelineState::Implementing { sub_issue, attempt } => {
                let sub = *sub_issue;
                let att = *attempt;
                if tests_passed {
                    // Move to review
                    self.state = PipelineState::Reviewing {
                        sub_issue: sub,
                        attempt: 1,
                    };
                    PipelineAction::DispatchReviewer(sub)
                } else if att >= IMPLEMENTER_VERIFIER_MAX_LOOP {
                    self.state = PipelineState::Stuck {
                        reason: format!("Implementer failed to pass tests after {} attempts", att),
                    };
                    PipelineAction::PostStuck(format!(
                        "Implementer-Verifier loop exceeded {} iterations for sub-issue #{}",
                        IMPLEMENTER_VERIFIER_MAX_LOOP, sub
                    ))
                } else {
                    self.state = PipelineState::Implementing {
                        sub_issue: sub,
                        attempt: att + 1,
                    };
                    PipelineAction::DispatchImplementer(sub)
                }
            }
            _ => PipelineAction::None,
        }
    }

    /// Handle reviewer feedback (pass/fail).
    pub fn on_review_done(&mut self, passed: bool) -> PipelineAction {
        match &self.state {
            PipelineState::Reviewing { sub_issue, attempt } => {
                let sub = *sub_issue;
                let att = *attempt;
                if passed {
                    // Advance to next sub-issue or e2e
                    self.current_sub_issue_index += 1;
                    self.advance_to_next_sub_issue()
                } else if att >= IMPLEMENTER_REVIEWER_MAX_LOOP {
                    self.state = PipelineState::Stuck {
                        reason: format!(
                            "Review loop exceeded {} rounds for sub-issue #{}",
                            att, sub
                        ),
                    };
                    PipelineAction::PostStuck(format!(
                        "Reviewer-Implementer loop exceeded {} iterations for sub-issue #{}",
                        IMPLEMENTER_REVIEWER_MAX_LOOP, sub
                    ))
                } else {
                    // Back to implementer for fixes
                    self.state = PipelineState::Implementing {
                        sub_issue: sub,
                        attempt: att + 1,
                    };
                    PipelineAction::DispatchImplementer(sub)
                }
            }
            _ => PipelineAction::None,
        }
    }

    /// Classify the issue and transition to the appropriate state.
    pub fn classify(&mut self, issue_type: IssueType) -> PipelineAction {
        self.issue_type = Some(issue_type.clone());
        match issue_type {
            IssueType::Bug => {
                self.state = PipelineState::Reproducing;
                PipelineAction::DispatchBugReproducer
            }
            IssueType::Feature | IssueType::Refactoring => {
                self.state = PipelineState::AnalyzingVision;
                PipelineAction::DispatchVisionGapAnalyst
            }
        }
    }

    /// Vision-gap analysis complete → await interactive session.
    pub fn on_vision_analysis_done(&mut self) -> PipelineAction {
        self.state = PipelineState::AwaitingInteractiveSession;
        PipelineAction::AwaitInteractiveSession
    }

    /// Set sub-issues after decomposition.
    pub fn set_sub_issues(&mut self, sub_issues: Vec<u64>) {
        self.sub_issues = sub_issues;
        self.current_sub_issue_index = 0;
    }

    /// Handle human direction after stuck state.
    pub fn on_human_direction(&mut self) -> PipelineAction {
        // Reset to implementing the current sub-issue with counter reset
        if let Some(&sub) = self.sub_issues.get(self.current_sub_issue_index) {
            self.state = PipelineState::Implementing {
                sub_issue: sub,
                attempt: 1,
            };
            PipelineAction::DispatchImplementer(sub)
        } else {
            PipelineAction::None
        }
    }

    // -----------------------------------------------------------------------
    // Internal helpers
    // -----------------------------------------------------------------------

    fn advance_to_next_sub_issue(&mut self) -> PipelineAction {
        if self.current_sub_issue_index >= self.sub_issues.len() {
            // All sub-issues done → e2e verification
            self.state = PipelineState::VerifyingE2e;
            PipelineAction::DispatchVerifierE2e
        } else {
            let sub = self.sub_issues[self.current_sub_issue_index];
            self.state = PipelineState::WritingTests { sub_issue: sub };
            PipelineAction::DispatchVerifierTests(sub)
        }
    }
}

// ---------------------------------------------------------------------------
// Pipeline actions (output of state transitions)
// ---------------------------------------------------------------------------

/// Actions the pipeline engine requests after a state transition.
#[derive(Debug, Clone, PartialEq)]
pub enum PipelineAction {
    /// No action needed.
    None,
    /// Post an approval marker on the issue.
    PostMarker(MarkerType),
    /// Close the issue with a reason.
    CloseIssue(String),
    /// Request additional info from the issue reporter.
    RequestInfo,
    /// Decompose the issue into sub-issues.
    Decompose,
    /// Dispatch Bug Reproducer agent.
    DispatchBugReproducer,
    /// Dispatch Vision-gap Analyst agent.
    DispatchVisionGapAnalyst,
    /// Wait for human Interactive Session.
    AwaitInteractiveSession,
    /// Dispatch Verifier to write tests for a sub-issue.
    DispatchVerifierTests(u64),
    /// Dispatch Implementer for a sub-issue.
    DispatchImplementer(u64),
    /// Dispatch Reviewer for a sub-issue.
    DispatchReviewer(u64),
    /// Dispatch Verifier for e2e validation.
    DispatchVerifierE2e,
    /// Post stuck marker with reason.
    PostStuck(String),
    /// Wait for human direction.
    WaitForHuman,
    /// Merge feature branch to dev.
    MergeToDev,
}

// ===========================================================================
// Tests
// ===========================================================================

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

    // 1. New tracker starts in Classifying
    #[test]
    fn new_tracker_starts_classifying() {
        let t = PipelineTracker::new(42);
        assert_eq!(t.state, PipelineState::Classifying);
        assert_eq!(t.issue_id, 42);
        assert!(t.issue_type.is_none());
    }

    // 2. Classify as Bug → Reproducing
    #[test]
    fn classify_bug_dispatches_reproducer() {
        let mut t = PipelineTracker::new(1);
        let action = t.classify(IssueType::Bug);
        assert_eq!(t.state, PipelineState::Reproducing);
        assert_eq!(action, PipelineAction::DispatchBugReproducer);
    }

    // 3. Classify as Feature → AnalyzingVision
    #[test]
    fn classify_feature_dispatches_vision_analyst() {
        let mut t = PipelineTracker::new(1);
        let action = t.classify(IssueType::Feature);
        assert_eq!(t.state, PipelineState::AnalyzingVision);
        assert_eq!(action, PipelineAction::DispatchVisionGapAnalyst);
    }

    // 4. Bug reproduced=true → Approved
    #[test]
    fn bug_reproduced_true_auto_approves() {
        let mut t = PipelineTracker::new(1);
        t.classify(IssueType::Bug);

        let mut attrs = HashMap::new();
        attrs.insert("reproduced".into(), "true".into());
        let action = t.on_marker(&MarkerType::Reproduced, &attrs);

        assert_eq!(t.state, PipelineState::Approved);
        assert_eq!(action, PipelineAction::PostMarker(MarkerType::Approved));
    }

    // 5. Bug reproduced=false → AwaitingInfo
    #[test]
    fn bug_not_reproduced_requests_info() {
        let mut t = PipelineTracker::new(1);
        t.classify(IssueType::Bug);

        let mut attrs = HashMap::new();
        attrs.insert("reproduced".into(), "false".into());
        let action = t.on_marker(&MarkerType::Reproduced, &attrs);

        assert!(matches!(
            t.state,
            PipelineState::AwaitingInfo { attempts: 1 }
        ));
        assert_eq!(action, PipelineAction::RequestInfo);
    }

    // 6. Vision analysis done → AwaitingInteractiveSession
    #[test]
    fn vision_done_awaits_session() {
        let mut t = PipelineTracker::new(1);
        t.classify(IssueType::Feature);
        let action = t.on_vision_analysis_done();
        assert_eq!(t.state, PipelineState::AwaitingInteractiveSession);
        assert_eq!(action, PipelineAction::AwaitInteractiveSession);
    }

    // 7. Approved → Decomposing
    #[test]
    fn approved_triggers_decompose() {
        let mut t = PipelineTracker::new(1);
        t.state = PipelineState::AwaitingInteractiveSession;
        let action = t.on_marker(&MarkerType::Approved, &HashMap::new());
        assert_eq!(t.state, PipelineState::Decomposing);
        assert_eq!(action, PipelineAction::Decompose);
    }

    // 8. Sub-issue flow: tests → implement → review
    #[test]
    fn sub_issue_flow_tests_implement_review() {
        let mut t = PipelineTracker::new(1);
        t.state = PipelineState::Approved;
        t.set_sub_issues(vec![100, 101]);

        // Advance from decomposing to first sub-issue
        let action = t.advance_to_next_sub_issue();
        assert_eq!(action, PipelineAction::DispatchVerifierTests(100));
        assert!(matches!(
            t.state,
            PipelineState::WritingTests { sub_issue: 100 }
        ));

        // Tests written, now implementing
        t.state = PipelineState::Implementing {
            sub_issue: 100,
            attempt: 1,
        };
        let action = t.on_implementer_done(true);
        assert_eq!(action, PipelineAction::DispatchReviewer(100));
        assert!(matches!(
            t.state,
            PipelineState::Reviewing { sub_issue: 100, .. }
        ));

        // Review passed → next sub-issue
        let action = t.on_review_done(true);
        assert_eq!(action, PipelineAction::DispatchVerifierTests(101));
    }

    // 9. All sub-issues done → e2e
    #[test]
    fn all_sub_issues_done_triggers_e2e() {
        let mut t = PipelineTracker::new(1);
        t.set_sub_issues(vec![100]);
        t.current_sub_issue_index = 0;
        t.state = PipelineState::Reviewing {
            sub_issue: 100,
            attempt: 1,
        };

        let action = t.on_review_done(true);
        assert_eq!(t.state, PipelineState::VerifyingE2e);
        assert_eq!(action, PipelineAction::DispatchVerifierE2e);
    }

    // 10. Verified → MergeToDev
    #[test]
    fn verified_merges_to_dev() {
        let mut t = PipelineTracker::new(1);
        t.state = PipelineState::VerifyingE2e;
        let action = t.on_marker(&MarkerType::Verified, &HashMap::new());
        assert_eq!(t.state, PipelineState::Merged);
        assert_eq!(action, PipelineAction::MergeToDev);
    }

    // 11. Implementer exceeds loop limit → Stuck
    #[test]
    fn implementer_exceeds_loop_limit_stuck() {
        let mut t = PipelineTracker::new(1);
        t.state = PipelineState::Implementing {
            sub_issue: 100,
            attempt: IMPLEMENTER_VERIFIER_MAX_LOOP,
        };
        let action = t.on_implementer_done(false);
        assert!(matches!(t.state, PipelineState::Stuck { .. }));
        assert!(matches!(action, PipelineAction::PostStuck(_)));
    }

    // 12. Reviewer exceeds loop limit → Stuck
    #[test]
    fn reviewer_exceeds_loop_limit_stuck() {
        let mut t = PipelineTracker::new(1);
        t.state = PipelineState::Reviewing {
            sub_issue: 100,
            attempt: IMPLEMENTER_REVIEWER_MAX_LOOP,
        };
        let action = t.on_review_done(false);
        assert!(matches!(t.state, PipelineState::Stuck { .. }));
        assert!(matches!(action, PipelineAction::PostStuck(_)));
    }

    // 13. Stuck marker transitions to Stuck state
    #[test]
    fn stuck_marker_transitions() {
        let mut t = PipelineTracker::new(1);
        t.state = PipelineState::Implementing {
            sub_issue: 100,
            attempt: 5,
        };
        let action = t.on_marker(&MarkerType::Stuck, &HashMap::new());
        assert!(matches!(t.state, PipelineState::Stuck { .. }));
        assert_eq!(action, PipelineAction::WaitForHuman);
    }

    // 14. Human direction resets loop
    #[test]
    fn human_direction_resets_loop() {
        let mut t = PipelineTracker::new(1);
        t.set_sub_issues(vec![100, 101]);
        t.current_sub_issue_index = 0;
        t.state = PipelineState::Stuck {
            reason: "test".into(),
        };

        let action = t.on_human_direction();
        assert!(matches!(
            t.state,
            PipelineState::Implementing {
                sub_issue: 100,
                attempt: 1
            }
        ));
        assert_eq!(action, PipelineAction::DispatchImplementer(100));
    }

    // 15. Classify as Refactoring → same as Feature
    #[test]
    fn classify_refactoring_same_as_feature() {
        let mut t = PipelineTracker::new(1);
        let action = t.classify(IssueType::Refactoring);
        assert_eq!(t.state, PipelineState::AnalyzingVision);
        assert_eq!(action, PipelineAction::DispatchVisionGapAnalyst);
        assert_eq!(t.issue_type, Some(IssueType::Refactoring));
    }

    // 16. No sub-issues → direct to e2e
    #[test]
    fn no_sub_issues_goes_to_e2e() {
        let mut t = PipelineTracker::new(1);
        t.set_sub_issues(vec![]);
        let action = t.advance_to_next_sub_issue();
        assert_eq!(t.state, PipelineState::VerifyingE2e);
        assert_eq!(action, PipelineAction::DispatchVerifierE2e);
    }

    // 17. Serialization roundtrip
    #[test]
    fn serialization_roundtrip() {
        let mut t = PipelineTracker::new(42);
        t.classify(IssueType::Bug);
        t.set_sub_issues(vec![100, 101]);
        t.branch_name = Some("Feature/#42".into());

        let json = serde_json::to_string(&t).unwrap();
        let deserialized: PipelineTracker = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.issue_id, 42);
        assert_eq!(deserialized.issue_type, Some(IssueType::Bug));
        assert_eq!(deserialized.sub_issues, vec![100, 101]);
    }

    // 18. Review fail loops back to implementer
    #[test]
    fn review_fail_loops_to_implementer() {
        let mut t = PipelineTracker::new(1);
        t.set_sub_issues(vec![100]);
        t.state = PipelineState::Reviewing {
            sub_issue: 100,
            attempt: 3,
        };

        let action = t.on_review_done(false);
        assert!(matches!(
            t.state,
            PipelineState::Implementing {
                sub_issue: 100,
                attempt: 4
            }
        ));
        assert_eq!(action, PipelineAction::DispatchImplementer(100));
    }
}