Skip to main content

oven_cli/pipeline/
executor.rs

1use std::{fmt::Write as _, path::PathBuf, sync::Arc};
2
3use anyhow::{Context, Result};
4use rusqlite::Connection;
5use tokio::sync::Mutex;
6use tokio_util::sync::CancellationToken;
7use tracing::{debug, info, warn};
8
9use crate::{
10    agents::{
11        self, AgentContext, AgentInvocation, AgentRole, Complexity, InFlightIssue, PlannerOutput,
12        Severity, invoke_agent, parse_planner_output, parse_review_output,
13    },
14    config::Config,
15    db::{self, AgentRun, ReviewFinding, Run, RunStatus},
16    git,
17    github::{self, GhClient},
18    issues::{IssueOrigin, IssueProvider, PipelineIssue},
19    process::CommandRunner,
20};
21
22/// Runs a single issue through the full pipeline.
23pub struct PipelineExecutor<R: CommandRunner> {
24    pub runner: Arc<R>,
25    pub github: Arc<GhClient<R>>,
26    pub issues: Arc<dyn IssueProvider>,
27    pub db: Arc<Mutex<Connection>>,
28    pub config: Config,
29    pub cancel_token: CancellationToken,
30    pub repo_dir: PathBuf,
31}
32
33impl<R: CommandRunner + 'static> PipelineExecutor<R> {
34    /// Run the full pipeline for a single issue.
35    pub async fn run_issue(&self, issue: &PipelineIssue, auto_merge: bool) -> Result<()> {
36        self.run_issue_with_complexity(issue, auto_merge, None).await
37    }
38
39    /// Run the full pipeline for a single issue with an optional complexity classification.
40    pub async fn run_issue_with_complexity(
41        &self,
42        issue: &PipelineIssue,
43        auto_merge: bool,
44        complexity: Option<Complexity>,
45    ) -> Result<()> {
46        let run_id = generate_run_id();
47
48        // Determine target repo for worktrees and PRs (multi-repo routing)
49        let (target_dir, is_multi_repo) = self.resolve_target_dir(issue.target_repo.as_ref())?;
50
51        let base_branch = git::default_branch(&target_dir).await?;
52
53        let mut run = new_run(&run_id, issue, auto_merge);
54        if let Some(ref c) = complexity {
55            run.complexity = c.to_string();
56        }
57        {
58            let conn = self.db.lock().await;
59            db::runs::insert_run(&conn, &run)?;
60        }
61
62        self.issues
63            .transition(issue.number, &self.config.labels.ready, &self.config.labels.cooking)
64            .await?;
65
66        let worktree = git::create_worktree(&target_dir, issue.number, &base_branch).await?;
67        self.record_worktree(&run_id, &worktree).await?;
68
69        // Seed branch with an empty commit so GitHub accepts the draft PR
70        git::empty_commit(
71            &worktree.path,
72            &format!("chore: start oven pipeline for issue #{}", issue.number),
73        )
74        .await?;
75
76        info!(
77            run_id = %run_id,
78            issue = issue.number,
79            branch = %worktree.branch,
80            target_repo = ?issue.target_repo,
81            "starting pipeline"
82        );
83
84        let pr_number = self.create_pr(&run_id, issue, &worktree.branch, &target_dir).await?;
85
86        let ctx = AgentContext {
87            issue_number: issue.number,
88            issue_title: issue.title.clone(),
89            issue_body: issue.body.clone(),
90            branch: worktree.branch.clone(),
91            pr_number: Some(pr_number),
92            test_command: self.config.project.test.clone(),
93            lint_command: self.config.project.lint.clone(),
94            review_findings: None,
95            cycle: 1,
96            target_repo: if is_multi_repo { issue.target_repo.clone() } else { None },
97            issue_source: issue.source.as_str().to_string(),
98            base_branch: base_branch.clone(),
99        };
100
101        let result = self.run_steps(&run_id, &ctx, &worktree.path, auto_merge, &base_branch).await;
102        self.finalize_run(&run_id, issue, pr_number, &result).await?;
103
104        if let Err(e) = git::remove_worktree(&target_dir, &worktree.path).await {
105            warn!(run_id = %run_id, error = %e, "failed to clean up worktree");
106        }
107
108        result
109    }
110
111    /// Invoke the planner agent to decide batching and complexity for a set of issues.
112    ///
113    /// `in_flight` describes issues currently running through the pipeline so the planner
114    /// can avoid scheduling conflicting work in batch 1.
115    ///
116    /// Returns `None` if the planner fails or returns unparseable output (fallback to default).
117    pub async fn plan_issues(
118        &self,
119        issues: &[PipelineIssue],
120        in_flight: &[InFlightIssue],
121    ) -> Option<PlannerOutput> {
122        let prompt = match agents::planner::build_prompt(issues, in_flight) {
123            Ok(p) => p,
124            Err(e) => {
125                warn!(error = %e, "planner prompt build failed");
126                return None;
127            }
128        };
129        let invocation = AgentInvocation {
130            role: AgentRole::Planner,
131            prompt,
132            working_dir: self.repo_dir.clone(),
133            max_turns: Some(self.config.pipeline.turn_limit),
134        };
135
136        match invoke_agent(self.runner.as_ref(), &invocation).await {
137            Ok(result) => {
138                debug!(output = %result.output, "raw planner output");
139                let parsed = parse_planner_output(&result.output);
140                if parsed.is_none() {
141                    warn!(output = %result.output, "planner returned unparseable output, falling back to single batch");
142                }
143                parsed
144            }
145            Err(e) => {
146                warn!(error = %e, "planner agent failed, falling back to single batch");
147                None
148            }
149        }
150    }
151
152    /// Determine the effective repo directory for worktrees and PRs.
153    ///
154    /// Returns `(target_dir, is_multi_repo)`. When multi-repo is disabled or no target
155    /// is specified, falls back to `self.repo_dir`.
156    fn resolve_target_dir(&self, target_repo: Option<&String>) -> Result<(PathBuf, bool)> {
157        if !self.config.multi_repo.enabled {
158            return Ok((self.repo_dir.clone(), false));
159        }
160        match target_repo {
161            Some(name) => {
162                let path = self.config.resolve_repo(name)?;
163                Ok((path, true))
164            }
165            None => Ok((self.repo_dir.clone(), false)),
166        }
167    }
168
169    async fn record_worktree(&self, run_id: &str, worktree: &git::Worktree) -> Result<()> {
170        let conn = self.db.lock().await;
171        db::runs::update_run_worktree(
172            &conn,
173            run_id,
174            &worktree.branch,
175            &worktree.path.to_string_lossy(),
176        )?;
177        drop(conn);
178        Ok(())
179    }
180
181    async fn create_pr(
182        &self,
183        run_id: &str,
184        issue: &PipelineIssue,
185        branch: &str,
186        repo_dir: &std::path::Path,
187    ) -> Result<u32> {
188        let (pr_title, pr_body) = match issue.source {
189            IssueOrigin::Github => (
190                format!("fix(#{}): {}", issue.number, issue.title),
191                format!(
192                    "Resolves #{}\n\nAutomated by [oven](https://github.com/clayharmon/oven-cli).",
193                    issue.number
194                ),
195            ),
196            IssueOrigin::Local => (
197                format!("fix: {}", issue.title),
198                format!(
199                    "From local issue #{}\n\nAutomated by [oven](https://github.com/clayharmon/oven-cli).",
200                    issue.number
201                ),
202            ),
203        };
204
205        git::push_branch(repo_dir, branch).await?;
206        let pr_number =
207            self.github.create_draft_pr_in(&pr_title, branch, &pr_body, repo_dir).await?;
208
209        {
210            let conn = self.db.lock().await;
211            db::runs::update_run_pr(&conn, run_id, pr_number)?;
212        }
213
214        info!(run_id = %run_id, pr = pr_number, "draft PR created");
215        Ok(pr_number)
216    }
217
218    async fn finalize_run(
219        &self,
220        run_id: &str,
221        issue: &PipelineIssue,
222        pr_number: u32,
223        result: &Result<()>,
224    ) -> Result<()> {
225        let (final_status, error_msg) = match result {
226            Ok(()) => {
227                self.issues
228                    .transition(
229                        issue.number,
230                        &self.config.labels.cooking,
231                        &self.config.labels.complete,
232                    )
233                    .await?;
234
235                // Close the issue when the merger can't do it:
236                // - Local issues: merger can't use `gh issue close`
237                // - Multi-repo: merger runs in target repo, can't close god-repo issue
238                let should_close =
239                    issue.source == IssueOrigin::Local || issue.target_repo.is_some();
240
241                if should_close {
242                    let comment = issue.target_repo.as_ref().map_or_else(
243                        || format!("Implemented in #{pr_number}"),
244                        |repo_name| format!("Implemented in {repo_name}#{pr_number}"),
245                    );
246                    if let Err(e) = self.issues.close(issue.number, Some(&comment)).await {
247                        warn!(
248                            run_id = %run_id,
249                            error = %e,
250                            "failed to close issue"
251                        );
252                    }
253                }
254
255                (RunStatus::Complete, None)
256            }
257            Err(e) => {
258                warn!(run_id = %run_id, error = %e, "pipeline failed");
259                github::safe_comment(&self.github, pr_number, &format!("Pipeline failed: {e:#}"))
260                    .await;
261                let _ = self
262                    .issues
263                    .transition(
264                        issue.number,
265                        &self.config.labels.cooking,
266                        &self.config.labels.failed,
267                    )
268                    .await;
269                (RunStatus::Failed, Some(format!("{e:#}")))
270            }
271        };
272
273        let conn = self.db.lock().await;
274        db::runs::finish_run(&conn, run_id, final_status, error_msg.as_deref())
275    }
276
277    async fn run_steps(
278        &self,
279        run_id: &str,
280        ctx: &AgentContext,
281        worktree_path: &std::path::Path,
282        auto_merge: bool,
283        base_branch: &str,
284    ) -> Result<()> {
285        self.check_cancelled()?;
286
287        // 1. Implement
288        self.update_status(run_id, RunStatus::Implementing).await?;
289        let impl_prompt = agents::implementer::build_prompt(ctx)?;
290        self.run_agent(run_id, AgentRole::Implementer, &impl_prompt, worktree_path, 1).await?;
291
292        git::push_branch(worktree_path, &ctx.branch).await?;
293
294        // 2. Review-fix loop
295        let clean = self.run_review_fix_loop(run_id, ctx, worktree_path).await?;
296
297        if !clean {
298            anyhow::bail!("unresolved findings after max review cycles");
299        }
300
301        // 3. Rebase onto base branch to resolve any conflicts from parallel merges
302        self.check_cancelled()?;
303        info!(run_id = %run_id, base = base_branch, "rebasing onto base branch");
304        if let Err(e) = git::rebase_on_base(worktree_path, base_branch).await {
305            if let Some(pr_number) = ctx.pr_number {
306                github::safe_comment(
307                    &self.github,
308                    pr_number,
309                    &format!(
310                        "Pipeline stopped: {e}\n\nPlease rebase manually and re-run the pipeline."
311                    ),
312                )
313                .await;
314            }
315            return Err(e);
316        }
317        git::force_push_branch(worktree_path, &ctx.branch).await?;
318
319        // 4. Merge
320        self.check_cancelled()?;
321        ctx.pr_number.context("no PR number for merge step")?;
322        self.update_status(run_id, RunStatus::Merging).await?;
323        let merge_prompt = agents::merger::build_prompt(ctx, auto_merge)?;
324        self.run_agent(run_id, AgentRole::Merger, &merge_prompt, worktree_path, 1).await?;
325
326        Ok(())
327    }
328
329    async fn run_review_fix_loop(
330        &self,
331        run_id: &str,
332        ctx: &AgentContext,
333        worktree_path: &std::path::Path,
334    ) -> Result<bool> {
335        for cycle in 1..=2 {
336            self.check_cancelled()?;
337
338            self.update_status(run_id, RunStatus::Reviewing).await?;
339            let review_prompt = agents::reviewer::build_prompt(ctx)?;
340            let review_result = self
341                .run_agent(run_id, AgentRole::Reviewer, &review_prompt, worktree_path, cycle)
342                .await?;
343
344            let review_output = match parse_review_output(&review_result.output) {
345                Ok(output) => output,
346                Err(e) => {
347                    warn!(run_id = %run_id, cycle, error = %e, "review output unparseable, treating as failed review");
348                    if let Some(pr_number) = ctx.pr_number {
349                        github::safe_comment(
350                            &self.github,
351                            pr_number,
352                            &format!("Review cycle {cycle} returned unparseable output. Stopping pipeline."),
353                        )
354                        .await;
355                    }
356                    anyhow::bail!("reviewer returned unparseable output in cycle {cycle}");
357                }
358            };
359            self.store_findings(run_id, &review_output.findings).await?;
360
361            let actionable: Vec<_> =
362                review_output.findings.iter().filter(|f| f.severity != Severity::Info).collect();
363
364            if actionable.is_empty() {
365                info!(run_id = %run_id, cycle, "review clean");
366                return Ok(true);
367            }
368
369            info!(run_id = %run_id, cycle, findings = actionable.len(), "review found issues");
370
371            if cycle == 2 {
372                if let Some(pr_number) = ctx.pr_number {
373                    let comment = format_unresolved_comment(&actionable);
374                    github::safe_comment(&self.github, pr_number, &comment).await;
375                } else {
376                    warn!(run_id = %run_id, "no PR number, cannot post unresolved findings");
377                }
378                return Ok(false);
379            }
380
381            // Fix
382            self.check_cancelled()?;
383            self.update_status(run_id, RunStatus::Fixing).await?;
384
385            let unresolved = {
386                let conn = self.db.lock().await;
387                db::agent_runs::get_unresolved_findings(&conn, run_id)?
388            };
389
390            let fix_prompt = agents::fixer::build_prompt(ctx, &unresolved)?;
391            self.run_agent(run_id, AgentRole::Fixer, &fix_prompt, worktree_path, cycle).await?;
392
393            git::push_branch(worktree_path, &ctx.branch).await?;
394        }
395
396        Ok(false)
397    }
398
399    async fn store_findings(&self, run_id: &str, findings: &[agents::Finding]) -> Result<()> {
400        let conn = self.db.lock().await;
401        let agent_runs = db::agent_runs::get_agent_runs_for_run(&conn, run_id)?;
402        let reviewer_run_id = agent_runs
403            .iter()
404            .rev()
405            .find_map(|ar| if ar.agent == "reviewer" { Some(ar.id) } else { None });
406        if let Some(ar_id) = reviewer_run_id {
407            for finding in findings {
408                let db_finding = ReviewFinding {
409                    id: 0,
410                    agent_run_id: ar_id,
411                    severity: finding.severity.to_string(),
412                    category: finding.category.clone(),
413                    file_path: finding.file_path.clone(),
414                    line_number: finding.line_number,
415                    message: finding.message.clone(),
416                    resolved: false,
417                };
418                db::agent_runs::insert_finding(&conn, &db_finding)?;
419            }
420        }
421        drop(conn);
422        Ok(())
423    }
424
425    async fn run_agent(
426        &self,
427        run_id: &str,
428        role: AgentRole,
429        prompt: &str,
430        working_dir: &std::path::Path,
431        cycle: u32,
432    ) -> Result<crate::process::AgentResult> {
433        let agent_run_id = self.record_agent_start(run_id, role, cycle).await?;
434
435        info!(run_id = %run_id, agent = %role, cycle, "agent starting");
436
437        let invocation = AgentInvocation {
438            role,
439            prompt: prompt.to_string(),
440            working_dir: working_dir.to_path_buf(),
441            max_turns: Some(self.config.pipeline.turn_limit),
442        };
443
444        let result = invoke_agent(self.runner.as_ref(), &invocation).await;
445
446        match &result {
447            Ok(agent_result) => {
448                self.record_agent_success(run_id, agent_run_id, agent_result).await?;
449            }
450            Err(e) => {
451                let conn = self.db.lock().await;
452                db::agent_runs::finish_agent_run(
453                    &conn,
454                    agent_run_id,
455                    "failed",
456                    0.0,
457                    0,
458                    None,
459                    Some(&format!("{e:#}")),
460                    None,
461                )?;
462            }
463        }
464
465        result
466    }
467
468    async fn record_agent_start(&self, run_id: &str, role: AgentRole, cycle: u32) -> Result<i64> {
469        let agent_run = AgentRun {
470            id: 0,
471            run_id: run_id.to_string(),
472            agent: role.to_string(),
473            cycle,
474            status: "running".to_string(),
475            cost_usd: 0.0,
476            turns: 0,
477            started_at: chrono::Utc::now().to_rfc3339(),
478            finished_at: None,
479            output_summary: None,
480            error_message: None,
481            raw_output: None,
482        };
483        let conn = self.db.lock().await;
484        db::agent_runs::insert_agent_run(&conn, &agent_run)
485    }
486
487    async fn record_agent_success(
488        &self,
489        run_id: &str,
490        agent_run_id: i64,
491        agent_result: &crate::process::AgentResult,
492    ) -> Result<()> {
493        let conn = self.db.lock().await;
494        db::agent_runs::finish_agent_run(
495            &conn,
496            agent_run_id,
497            "complete",
498            agent_result.cost_usd,
499            agent_result.turns,
500            Some(&truncate(&agent_result.output, 500)),
501            None,
502            Some(&agent_result.output),
503        )?;
504
505        let new_cost = db::runs::increment_run_cost(&conn, run_id, agent_result.cost_usd)?;
506        drop(conn);
507
508        if new_cost > self.config.pipeline.cost_budget {
509            anyhow::bail!(
510                "cost budget exceeded: ${:.2} > ${:.2}",
511                new_cost,
512                self.config.pipeline.cost_budget
513            );
514        }
515        Ok(())
516    }
517
518    async fn update_status(&self, run_id: &str, status: RunStatus) -> Result<()> {
519        let conn = self.db.lock().await;
520        db::runs::update_run_status(&conn, run_id, status)
521    }
522
523    fn check_cancelled(&self) -> Result<()> {
524        if self.cancel_token.is_cancelled() {
525            anyhow::bail!("pipeline cancelled");
526        }
527        Ok(())
528    }
529}
530
531fn format_unresolved_comment(actionable: &[&agents::Finding]) -> String {
532    let mut comment = String::from("## Unresolved findings after 2 review cycles\n\n");
533    for f in actionable {
534        let loc = match (&f.file_path, f.line_number) {
535            (Some(path), Some(line)) => format!(" at `{path}:{line}`"),
536            (Some(path), None) => format!(" in `{path}`"),
537            _ => String::new(),
538        };
539        let _ = writeln!(comment, "- **[{}]** {}{}: {}", f.severity, f.category, loc, f.message);
540    }
541    comment
542}
543
544fn new_run(run_id: &str, issue: &PipelineIssue, auto_merge: bool) -> Run {
545    Run {
546        id: run_id.to_string(),
547        issue_number: issue.number,
548        status: RunStatus::Pending,
549        pr_number: None,
550        branch: None,
551        worktree_path: None,
552        cost_usd: 0.0,
553        auto_merge,
554        started_at: chrono::Utc::now().to_rfc3339(),
555        finished_at: None,
556        error_message: None,
557        complexity: "full".to_string(),
558        issue_source: issue.source.to_string(),
559    }
560}
561
562/// Generate an 8-character hex run ID.
563pub fn generate_run_id() -> String {
564    uuid::Uuid::new_v4().to_string()[..8].to_string()
565}
566
567/// Truncate a string to at most `max_len` bytes, appending "..." if truncated.
568///
569/// Reserves 3 bytes for the "..." suffix so the total output never exceeds `max_len`.
570/// Always cuts at a valid UTF-8 character boundary to avoid panics on multi-byte input.
571pub(crate) fn truncate(s: &str, max_len: usize) -> String {
572    if s.len() <= max_len {
573        return s.to_string();
574    }
575    let target = max_len.saturating_sub(3);
576    let mut end = target;
577    while end > 0 && !s.is_char_boundary(end) {
578        end -= 1;
579    }
580    format!("{}...", &s[..end])
581}
582
583#[cfg(test)]
584mod tests {
585    use proptest::prelude::*;
586
587    use super::*;
588
589    proptest! {
590        #[test]
591        fn run_ids_always_8_hex_chars(_seed in any::<u64>()) {
592            let id = generate_run_id();
593            prop_assert_eq!(id.len(), 8);
594            prop_assert!(id.chars().all(|c| c.is_ascii_hexdigit()));
595        }
596    }
597
598    #[test]
599    fn run_id_is_8_hex_chars() {
600        let id = generate_run_id();
601        assert_eq!(id.len(), 8);
602        assert!(id.chars().all(|c| c.is_ascii_hexdigit()));
603    }
604
605    #[test]
606    fn run_ids_are_unique() {
607        let ids: Vec<_> = (0..100).map(|_| generate_run_id()).collect();
608        let unique: std::collections::HashSet<_> = ids.iter().collect();
609        assert_eq!(ids.len(), unique.len());
610    }
611
612    #[test]
613    fn truncate_short_string() {
614        assert_eq!(truncate("hello", 10), "hello");
615    }
616
617    #[test]
618    fn truncate_long_string() {
619        let long = "a".repeat(100);
620        let result = truncate(&long, 10);
621        assert_eq!(result.len(), 10); // 7 chars + "..."
622        assert!(result.ends_with("..."));
623    }
624
625    #[test]
626    fn truncate_multibyte_does_not_panic() {
627        // Each emoji is 4 bytes. "πŸ˜€πŸ˜€πŸ˜€" = 12 bytes.
628        // max_len=8, target=5, walks back to boundary at 4 (one emoji).
629        let s = "πŸ˜€πŸ˜€πŸ˜€";
630        let result = truncate(s, 8);
631        assert!(result.ends_with("..."));
632        assert!(result.starts_with("πŸ˜€"));
633        assert!(result.len() <= 8);
634    }
635
636    #[test]
637    fn truncate_cjk_boundary() {
638        // CJK chars are 3 bytes each
639        let s = "δ½ ε₯½δΈ–η•Œζ΅‹θ―•"; // 18 bytes
640        // max_len=10, target=7, walks back to boundary at 6 (two 3-byte chars).
641        let result = truncate(s, 10);
642        assert!(result.ends_with("..."));
643        assert!(result.starts_with("δ½ ε₯½"));
644        assert!(result.len() <= 10);
645    }
646
647    #[test]
648    fn format_unresolved_comment_includes_findings() {
649        let findings = [
650            agents::Finding {
651                severity: Severity::Critical,
652                category: "bug".to_string(),
653                file_path: Some("src/main.rs".to_string()),
654                line_number: Some(42),
655                message: "null pointer".to_string(),
656            },
657            agents::Finding {
658                severity: Severity::Warning,
659                category: "style".to_string(),
660                file_path: None,
661                line_number: None,
662                message: "missing docs".to_string(),
663            },
664        ];
665        let refs: Vec<_> = findings.iter().collect();
666        let comment = format_unresolved_comment(&refs);
667        assert!(comment.contains("Unresolved findings"));
668        assert!(comment.contains("null pointer"));
669        assert!(comment.contains("`src/main.rs:42`"));
670        assert!(comment.contains("missing docs"));
671    }
672}