1use crate::config;
8use crate::parser::AgentResponse;
9use crate::store;
10use crate::store::TaskStore;
11use std::io;
12use std::io::ErrorKind;
13use std::path::Path;
14use std::sync::Arc;
15
16use tokio::fs;
17
18use super::{agents, git_ops, response, worktree};
19use crate::engine::cooldown;
20use crate::engine::push_retry;
21use crate::engine::review;
22use crate::github::http::GhHttp;
23use crate::status_heuristics;
24use crate::store::Task;
25
26#[expect(
28 clippy::too_many_arguments,
29 reason = "each field of result.json is a separate argument"
30)]
31pub async fn write_result_json(
32 attempt_dir: &Path,
33 task_id: &str,
34 agent_name: &str,
35 model_name: Option<&str>,
36 exit_code: i32,
37 new_attempts: u32,
38 parse_result: &Result<agents::ParsedResponse, agents::AgentError>,
39 raw_stdout: &str,
40 raw_stderr: &str,
41) {
42 let result_json = match parse_result {
43 Ok(parsed) => {
44 serde_json::json!({
45 "outcome": "success",
46 "agent": agent_name,
47 "model": model_name.unwrap_or("default"),
48 "exit_code": exit_code,
49 "attempt": new_attempts,
50 "status": parsed.response.status,
51 "summary": parsed.response.summary,
52 "input_tokens": parsed.input_tokens,
53 "output_tokens": parsed.output_tokens,
54 "duration_ms": parsed.duration_ms,
55 "files": parsed.response.files,
56 "accomplished": parsed.response.accomplished,
57 "remaining": parsed.response.remaining,
58 "error": parsed.response.error,
59 "learnings": parsed.response.learnings,
60 "delegations": parsed.response.delegations.iter()
61 .map(|d| serde_json::json!({"title": d.title, "body": d.body}))
62 .collect::<Vec<_>>(),
63 })
64 }
65 Err(agent_err) => {
66 serde_json::json!({
67 "outcome": "error",
68 "agent": agent_name,
69 "model": model_name.unwrap_or("default"),
70 "exit_code": exit_code,
71 "attempt": new_attempts,
72 "error_class": agents::error_class_name(agent_err),
73 "error_message": agent_err.to_string(),
74 "stderr_tail": agents::patterns::safe_tail(raw_stderr, 2000),
75 "stdout_tail": agents::patterns::safe_tail(raw_stdout, 2000),
76 })
77 }
78 };
79
80 if let Err(e) = fs::write(
81 attempt_dir.join("result.json"),
82 serde_json::to_string_pretty(&result_json).unwrap_or_default(),
83 )
84 .await
85 {
86 tracing::debug!(task_id, ?e, "failed to write result.json");
87 }
88}
89
90#[derive(Default)]
96#[expect(
97 clippy::struct_excessive_bools,
98 reason = "each flag is an independent input to the status decision"
99)]
100struct DecisionInput<'a> {
101 agent_status: &'a str,
103 is_workflow_scope_failure: bool,
106 is_rebase_conflict_failure: bool,
109 push_failed: bool,
111 push_failures: u64,
114 is_transient_push_failure: bool,
117 has_pr: bool,
119 has_delegations: bool,
121 has_pushed: bool,
123 is_pr_base_invalid: bool,
127 requires_pr: bool,
130 no_code_reroutes: u64,
132 max_reroutes: u32,
134 is_same_agent: bool,
137 is_completed_status: bool,
142 is_retryable_blocked: bool,
144 is_superseded_conflict: bool,
148}
149
150fn classify_final_status(input: &DecisionInput<'_>) -> String {
157 if input.is_workflow_scope_failure || input.is_rebase_conflict_failure {
158 "blocked".to_string()
159 } else if input.push_failed {
160 if input.is_transient_push_failure && input.push_failures < push_retry::MAX_PUSH_FAILURES {
161 "needs_review".to_string()
162 } else if input.push_failures >= 3 {
163 "blocked".to_string()
164 } else {
165 "new".to_string()
166 }
167 } else if input.agent_status == "done" && input.has_pr {
168 "needs_review".to_string()
169 } else if input.agent_status == "done" && !input.has_pr && input.has_delegations {
170 "blocked".to_string()
171 } else if input.agent_status == "done"
172 && !input.has_pr
173 && input.has_pushed
174 && input.is_pr_base_invalid
175 {
176 "blocked".to_string()
180 } else if input.agent_status == "done" && !input.has_pr && input.has_pushed {
181 "routed".to_string()
182 } else if input.is_superseded_conflict {
183 "done".to_string()
184 } else if input.agent_status == "done" && input.requires_pr {
185 if input.is_same_agent || input.no_code_reroutes >= u64::from(input.max_reroutes) {
189 "blocked".to_string()
190 } else {
191 "new".to_string()
192 }
193 } else if input.agent_status == "done" && !input.has_pushed {
194 "done".to_string()
199 } else if input.is_completed_status && input.has_pr {
200 "needs_review".to_string()
202 } else if input.is_completed_status && !input.has_pr && input.has_delegations {
203 "blocked".to_string()
205 } else if input.is_completed_status
206 && !input.has_pr
207 && input.has_pushed
208 && input.is_pr_base_invalid
209 {
210 "blocked".to_string()
211 } else if input.is_completed_status && !input.has_pr && input.has_pushed {
212 "routed".to_string()
214 } else if input.is_completed_status && input.requires_pr {
215 if input.is_same_agent || input.no_code_reroutes >= u64::from(input.max_reroutes) {
218 "blocked".to_string()
219 } else {
220 "new".to_string()
221 }
222 } else if input.is_completed_status {
223 "done".to_string()
227 } else if input.agent_status == "blocked" && input.is_retryable_blocked {
228 "new".to_string()
229 } else if status_heuristics::status_looks_like_descriptive_completion(input.agent_status) {
230 "done".to_string()
231 } else {
232 input.agent_status.to_string()
233 }
234}
235
236struct StoreCtx<'a> {
240 store: &'a Option<Arc<TaskStore>>,
241 store_id_opt: Option<i64>,
242 repo: &'a str,
243 task_id: &'a str,
244}
245
246impl StoreCtx<'_> {
247 async fn set(&self, fields: &[(&str, serde_json::Value)]) {
249 if let Some(store_id) = self.store_id_opt {
250 let _ = store::store_set_by_id(self.store.as_ref(), store_id, fields).await;
251 } else {
252 store::store_set(self.store, self.repo, self.task_id, fields).await;
253 }
254 }
255
256 async fn reset_retry_streaks(&self) {
258 self.set(&[
259 ("network_retries", serde_json::json!(0)),
260 ("timeout_retries", serde_json::json!(0)),
261 ])
262 .await;
263 }
264
265 async fn get_task(&self) -> Option<Task> {
267 if let Some(store_id) = self.store_id_opt {
268 store::opt_store_get_task_by_id(self.store, store_id).await
269 } else {
270 store::opt_store_get_task(self.store, self.repo, self.task_id).await
271 }
272 }
273
274 async fn increment(&self, field: &str) -> anyhow::Result<u64> {
276 if let Some(store_id) = self.store_id_opt {
277 store::store_increment_by_id(self.store, store_id, field).await
278 } else {
279 store::store_increment(self.store, self.repo, self.task_id, field).await
280 }
281 }
282
283 async fn append_activity(
285 &self,
286 event_type: &str,
287 agent: Option<&str>,
288 model: Option<&str>,
289 details: Option<&serde_json::Value>,
290 ) {
291 if let Some(store_id) = self.store_id_opt {
292 if let Some(s) = self.store
293 && let Err(e) = s
294 .append_activity(store_id, event_type, None, None, agent, model, details)
295 .await
296 {
297 tracing::warn!(
298 task_id = self.task_id,
299 event_type,
300 error = %e,
301 "store append_activity failed"
302 );
303 }
304 } else {
305 store::store_log_activity(
306 self.store,
307 self.repo,
308 self.task_id,
309 event_type,
310 None,
311 None,
312 agent,
313 model,
314 details,
315 )
316 .await;
317 }
318 }
319}
320
321#[derive(Default)]
325#[expect(
326 clippy::struct_excessive_bools,
327 reason = "each flag is an independent outcome of the git pipeline"
328)]
329struct GitOpsResult {
330 has_pr: bool,
331 has_pushed: bool,
332 has_commits: bool,
333 pr_base_invalid: bool,
337}
338
339#[expect(
341 clippy::struct_excessive_bools,
342 reason = "each flag is an independent kind of push failure"
343)]
344struct PushFailureState {
345 push_failed: bool,
346 is_workflow_scope_failure: bool,
347 is_rebase_conflict_failure: bool,
348 is_transient_failure: bool,
350 push_failures: u64,
352 stored_last_error: String,
354}
355
356async fn auto_commit_changes(
360 ctx: &StoreCtx<'_>,
361 work_dir: &Path,
362 task_title: &str,
363 agent_name: &str,
364 new_attempts: u32,
365) {
366 if let Err(e) = git_ops::auto_commit(
367 work_dir,
368 ctx.task_id,
369 task_title,
370 agent_name,
371 new_attempts,
372 super::commit::When::Always,
373 )
374 .await
375 {
376 tracing::error!(task_id = ctx.task_id, error = ?e, "auto commit failed");
377 ctx.set(&[(
378 "last_error",
379 serde_json::json!(format!("auto commit failed: {e}")),
380 )])
381 .await;
382 }
383}
384
385async fn check_commits_and_clear_stale_errors(
390 ctx: &StoreCtx<'_>,
391 work_dir: &Path,
392 default_branch: &str,
393) -> bool {
394 let has_commits = git_ops::has_commits_ahead(work_dir, default_branch).await;
395 if !has_commits {
396 tracing::info!(
397 task_id = ctx.task_id,
398 "no commits ahead of default branch, skipping push + PR"
399 );
400 let last_err = ctx
402 .get_task()
403 .await
404 .map(|t| t.last_error)
405 .unwrap_or_default();
406 if last_err.contains("push failed") {
407 ctx.set(&[("last_error", serde_json::json!(""))]).await;
408 }
409 }
410 has_commits
411}
412
413enum PushResult {
423 Success,
424 Failed,
425 NoCommits, WorktreeMissing, }
428
429fn is_missing_path_error(err: &anyhow::Error) -> bool {
430 err.chain().any(|cause| {
431 cause
432 .downcast_ref::<io::Error>()
433 .is_some_and(|ioe| ioe.kind() == ErrorKind::NotFound)
434 }) || err
435 .to_string()
436 .to_ascii_lowercase()
437 .contains("no such file or directory")
438}
439
440async fn recover_missing_worktree_for_push(
441 ctx: &StoreCtx<'_>,
442 wt: &worktree::WorktreeSetup,
443 task_title: &str,
444 agent_name: &str,
445 model_name: Option<&str>,
446) -> anyhow::Result<worktree::WorktreeSetup> {
447 tracing::warn!(
448 task_id = ctx.task_id,
449 worktree = %wt.work_dir.display(),
450 branch = wt.branch,
451 "worktree missing during push; attempting one-time recovery"
452 );
453 ctx.append_activity(
454 "push_recover_worktree",
455 Some(agent_name),
456 model_name,
457 Some(&serde_json::json!({
458 "status": "start",
459 "worktree": wt.work_dir.display().to_string(),
460 "branch": wt.branch,
461 })),
462 )
463 .await;
464
465 let recovered = worktree::setup_worktree(
466 ctx.task_id,
467 task_title,
468 &wt.main_project_dir,
469 ctx.store,
470 ctx.repo,
471 )
472 .await?;
473
474 if !recovered.work_dir.exists() {
475 anyhow::bail!(
476 "recovered worktree path is still missing: {}",
477 recovered.work_dir.display()
478 );
479 }
480
481 ctx.append_activity(
482 "push_recover_worktree",
483 Some(agent_name),
484 model_name,
485 Some(&serde_json::json!({
486 "status": "ok",
487 "worktree": recovered.work_dir.display().to_string(),
488 "branch": recovered.branch,
489 })),
490 )
491 .await;
492
493 Ok(recovered)
494}
495
496async fn push_after_rebase(
501 ctx: &StoreCtx<'_>,
502 work_dir: &Path,
503 branch: &str,
504 default_branch: &str,
505 agent_name: &str,
506 model_name: Option<&str>,
507) -> PushResult {
508 match git_ops::push_branch(work_dir, branch, default_branch).await {
509 Ok(_) => {
510 ctx.append_activity(
511 "push",
512 Some(agent_name),
513 model_name,
514 Some(&serde_json::json!({
515 "status": "ok",
516 "branch": branch,
517 "default_branch": default_branch,
518 "reason": "pushed_after_rebase",
519 })),
520 )
521 .await;
522 ctx.set(&[
523 ("last_error", serde_json::json!("")),
524 ("push_failures", serde_json::json!(0)),
525 ])
526 .await;
527 PushResult::Success
528 }
529 Err(e) => {
530 tracing::error!(
531 task_id = ctx.task_id,
532 branch = branch,
533 error = ?e,
534 "push failed again after rebasing on the remote branch"
535 );
536 ctx.append_activity(
537 "push",
538 Some(agent_name),
539 model_name,
540 Some(&serde_json::json!({
541 "status": "error",
542 "branch": branch,
543 "default_branch": default_branch,
544 "reason": "pushed_after_rebase",
545 "error": e.to_string(),
546 })),
547 )
548 .await;
549 ctx.set(&[(
550 "last_error",
551 serde_json::json!(format!("push failed after rebase on remote branch: {e}")),
552 )])
553 .await;
554 PushResult::Failed
555 }
556 }
557}
558
559async fn push_branch_with_log(
560 ctx: &StoreCtx<'_>,
561 work_dir: &Path,
562 branch: &str,
563 default_branch: &str,
564 agent_name: &str,
565 model_name: Option<&str>,
566) -> PushResult {
567 match git_ops::push_branch(work_dir, branch, default_branch).await {
568 Ok(_) => {
569 ctx.append_activity(
570 "push",
571 Some(agent_name),
572 model_name,
573 Some(&serde_json::json!({
574 "status": "ok",
575 "branch": branch,
576 "default_branch": default_branch,
577 })),
578 )
579 .await;
580 ctx.set(&[
583 ("last_error", serde_json::json!("")),
584 ("push_failures", serde_json::json!(0)),
585 ])
586 .await;
587 PushResult::Success
588 }
589 Err(e) => {
590 let err_str = e.to_string();
591 tracing::error!(task_id = ctx.task_id, error = ?e, "push failed");
592
593 if is_missing_path_error(&e) && !work_dir.exists() {
594 tracing::warn!(
595 task_id = ctx.task_id,
596 worktree = %work_dir.display(),
597 "push failed because worktree path disappeared; will attempt recovery"
598 );
599 ctx.append_activity(
600 "push",
601 Some(agent_name),
602 model_name,
603 Some(&serde_json::json!({
604 "status": "retry",
605 "branch": branch,
606 "default_branch": default_branch,
607 "reason": "worktree_missing",
608 "error": err_str,
609 })),
610 )
611 .await;
612 return PushResult::WorktreeMissing;
613 }
614
615 if push_error_is_diverged(&err_str) {
617 tracing::info!(
618 task_id = ctx.task_id,
619 branch = branch,
620 "attempting fetch and rebase to recover from diverged branch"
621 );
622
623 match git_ops::rebase_on_branch(work_dir, branch).await {
625 Ok(true) => {
626 tracing::info!(
630 task_id = ctx.task_id,
631 branch = branch,
632 "rebase succeeded — retrying the push"
633 );
634 ctx.append_activity(
635 "push_rebased",
636 Some(agent_name),
637 model_name,
638 Some(&serde_json::json!({
639 "status": "rebased",
640 "branch": branch,
641 "reason": "diverged_from_remote",
642 })),
643 )
644 .await;
645 return push_after_rebase(
646 ctx,
647 work_dir,
648 branch,
649 default_branch,
650 agent_name,
651 model_name,
652 )
653 .await;
654 }
655 Ok(false) => {
656 tracing::info!(
658 task_id = ctx.task_id,
659 branch = branch,
660 "no local commits after fetch — marking as success"
661 );
662 ctx.append_activity(
663 "push",
664 Some(agent_name),
665 model_name,
666 Some(&serde_json::json!({
667 "status": "ok",
668 "branch": branch,
669 "reason": "no_commits_after_fetch",
670 })),
671 )
672 .await;
673 ctx.set(&[
674 ("last_error", serde_json::json!("")),
675 ("push_failures", serde_json::json!(0)),
676 ])
677 .await;
678 return PushResult::Success;
679 }
680 Err(rebase_err) if git_ops::is_missing_remote_ref_error(&rebase_err) => {
681 tracing::warn!(
684 task_id = ctx.task_id,
685 branch = branch,
686 "remote branch does not exist, skipping rebase recovery"
687 );
688 ctx.append_activity(
689 "push",
690 Some(agent_name),
691 model_name,
692 Some(&serde_json::json!({
693 "status": "error",
694 "branch": branch,
695 "default_branch": default_branch,
696 "error": err_str,
697 })),
698 )
699 .await;
700 ctx.set(&[("last_error", serde_json::json!(format!("push failed: {e}")))])
701 .await;
702 return PushResult::Failed;
703 }
704 Err(rebase_err) => {
705 tracing::error!(
707 task_id = ctx.task_id,
708 branch = branch,
709 error = ?rebase_err,
710 "rebase failed — blocking task for human intervention"
711 );
712 ctx.append_activity(
713 "push_rebased",
714 Some(agent_name),
715 model_name,
716 Some(&serde_json::json!({
717 "status": "error",
718 "branch": branch,
719 "error": rebase_err.to_string(),
720 })),
721 )
722 .await;
723 ctx.set(&[(
724 "last_error",
725 serde_json::json!(format!(
726 "push failed and rebase failed: {rebase_err} — manual resolution required"
727 )),
728 )])
729 .await;
730 return PushResult::Failed;
731 }
732 }
733 }
734
735 ctx.append_activity(
737 "push",
738 Some(agent_name),
739 model_name,
740 Some(&serde_json::json!({
741 "status": "error",
742 "branch": branch,
743 "default_branch": default_branch,
744 "error": err_str,
745 })),
746 )
747 .await;
748 ctx.set(&[("last_error", serde_json::json!(format!("push failed: {e}")))])
749 .await;
750 PushResult::Failed
751 }
752 }
753}
754
755fn push_error_is_diverged(err_str: &str) -> bool {
758 err_str.contains("non-fast-forward")
759 || err_str.contains("! [rejected]")
760 || err_str.contains("fetch first")
761 || err_str.contains("behind")
762}
763
764fn is_local_rate_limit_skip(err_str: &str) -> bool {
766 err_str.contains("rate-limited, skipping call")
767}
768
769async fn create_pr_with_log(
776 ctx: &StoreCtx<'_>,
777 wt: &worktree::WorktreeSetup,
778 task_title: &str,
779 resp: &AgentResponse,
780 agent_name: &str,
781 model_name: Option<&str>,
782 mut has_pushed: bool,
783) -> (bool, bool, bool) {
784 match git_ops::create_pr_if_needed(
785 &wt.work_dir,
786 &wt.branch,
787 task_title,
788 &resp.summary,
789 &resp.accomplished,
790 &resp.remaining,
791 &resp.files,
792 ctx.task_id,
793 agent_name,
794 model_name,
795 ctx.repo,
796 &wt.default_branch,
797 )
798 .await
799 {
800 Ok(ref url) => {
801 match review::parse_pr_number_from_url(url) {
805 Ok(pr_num) => {
806 if let Err(e) = store::store_set_result(
807 ctx.store,
808 ctx.repo,
809 ctx.task_id,
810 &[("pr_number", serde_json::json!(pr_num))],
811 )
812 .await
813 {
814 tracing::error!(
815 task_id = ctx.task_id,
816 pr_url = %url,
817 err = %e,
818 "CRITICAL: failed to save pr_number to store — downstream review gate may trigger duplicate gh pr create"
819 );
820 }
821 }
822 Err(e) => {
823 tracing::warn!(
824 task_id = ctx.task_id,
825 pr_url = %url,
826 error = %e,
827 "PR created but failed to parse PR number from URL"
828 );
829 }
830 }
831 ctx.append_activity(
832 "pr_create",
833 Some(agent_name),
834 model_name,
835 Some(&serde_json::json!({"status": "created", "url": url})),
836 )
837 .await;
838 (true, has_pushed, false)
839 }
840 Err(e) => {
841 let err_str = format!("{e}");
842 tracing::error!(task_id = ctx.task_id, error = ?e, "create PR failed");
843 ctx.append_activity(
844 "pr_create",
845 Some(agent_name),
846 model_name,
847 Some(&serde_json::json!({
848 "status": "error",
849 "branch": wt.branch,
850 "error": err_str.clone(),
851 })),
852 )
853 .await;
854 ctx.set(&[(
855 "last_error",
856 serde_json::json!(format!("create PR failed: {e}")),
857 )])
858 .await;
859 let mut pr_base_invalid = false;
869 if is_local_rate_limit_skip(&err_str) {
870 tracing::warn!(
873 task_id = ctx.task_id,
874 "PR creation skipped by local GitHub rate limit — review gate will create the PR"
875 );
876 return (true, has_pushed, false);
877 }
878 if agents::patterns::detect_auth_error(&err_str).is_some() {
879 tracing::error!(
880 task_id = ctx.task_id,
881 "PR creation failed with GitHub auth error — blocking task"
882 );
883 pr_base_invalid = true;
884 } else if err_str.contains("422") {
885 if err_str.contains("No commits between") || err_str.contains("head") {
886 tracing::info!(
887 task_id = ctx.task_id,
888 "PR creation returned 422/no-commits/head-invalid — agent made no code changes or branch merged, marking done"
889 );
890 has_pushed = false;
891 } else if git_ops::is_invalid_base_error(&err_str) {
892 tracing::error!(
893 task_id = ctx.task_id,
894 "PR creation failed with invalid base branch — blocking task"
895 );
896 pr_base_invalid = true;
897 }
898 }
899 (false, has_pushed, pr_base_invalid)
900 }
901 }
902}
903
904async fn run_git_ops(
906 ctx: &StoreCtx<'_>,
907 wt: &worktree::WorktreeSetup,
908 task_title: &str,
909 resp: &AgentResponse,
910 agent_name: &str,
911 model_name: Option<&str>,
912 new_attempts: u32,
913) -> GitOpsResult {
914 auto_commit_changes(ctx, &wt.work_dir, task_title, agent_name, new_attempts).await;
915
916 let mut has_commits =
917 check_commits_and_clear_stale_errors(ctx, &wt.work_dir, &wt.default_branch).await;
918
919 let push_result = if has_commits {
923 push_branch_with_log(
924 ctx,
925 &wt.work_dir,
926 &wt.branch,
927 &wt.default_branch,
928 agent_name,
929 model_name,
930 )
931 .await
932 } else {
933 store::store_log_activity(
934 ctx.store,
935 ctx.repo,
936 ctx.task_id,
937 "push",
938 None,
939 None,
940 Some(agent_name),
941 model_name,
942 Some(&serde_json::json!({
943 "status": "skipped",
944 "reason": "no_commits_ahead",
945 "branch": wt.branch,
946 "default_branch": wt.default_branch,
947 })),
948 )
949 .await;
950 PushResult::NoCommits
951 };
952 let push_result = if matches!(push_result, PushResult::WorktreeMissing) {
953 match recover_missing_worktree_for_push(ctx, wt, task_title, agent_name, model_name).await {
954 Ok(recovered) => {
955 let wt_for_ops = &recovered;
956 has_commits = check_commits_and_clear_stale_errors(
957 ctx,
958 &wt_for_ops.work_dir,
959 &wt_for_ops.default_branch,
960 )
961 .await;
962 if has_commits {
963 push_branch_with_log(
964 ctx,
965 &wt_for_ops.work_dir,
966 &wt_for_ops.branch,
967 &wt_for_ops.default_branch,
968 agent_name,
969 model_name,
970 )
971 .await
972 } else {
973 PushResult::NoCommits
974 }
975 }
976 Err(e) => {
977 tracing::error!(
978 task_id = ctx.task_id,
979 err = %e,
980 "worktree recovery before push failed"
981 );
982 ctx.append_activity(
983 "push_recover_worktree",
984 Some(agent_name),
985 model_name,
986 Some(&serde_json::json!({
987 "status": "error",
988 "worktree": wt.work_dir.display().to_string(),
989 "branch": wt.branch,
990 "error": e.to_string(),
991 })),
992 )
993 .await;
994 ctx.set(&[(
995 "last_error",
996 serde_json::json!(format!(
997 "push failed: worktree disappeared and recovery failed: {e}"
998 )),
999 )])
1000 .await;
1001 PushResult::Failed
1002 }
1003 }
1004 } else {
1005 push_result
1006 };
1007
1008 let mut has_pushed = matches!(push_result, PushResult::Success);
1009 let mut has_pr = false;
1010
1011 if has_commits && matches!(push_result, PushResult::Failed) {
1013 tracing::warn!(
1014 task_id = ctx.task_id,
1015 "skipping PR creation due to push failure"
1016 );
1017 } else if matches!(push_result, PushResult::NoCommits) {
1018 tracing::info!(
1019 task_id = ctx.task_id,
1020 "no commits ahead of default branch, skipping push + PR"
1021 );
1022 } else if !has_pushed {
1023 } else if ctx.repo.is_empty() {
1025 tracing::warn!(
1026 task_id = ctx.task_id,
1027 "skipping PR creation — repo is empty (internal task?)"
1028 );
1029 } else {
1030 let (pr, updated_pushed, pr_base_invalid) = create_pr_with_log(
1031 ctx, wt, task_title, resp, agent_name, model_name, has_pushed,
1032 )
1033 .await;
1034 has_pr = pr;
1035 has_pushed = updated_pushed;
1036 return GitOpsResult {
1037 has_pr,
1038 has_pushed,
1039 has_commits,
1040 pr_base_invalid,
1041 };
1042 }
1043
1044 GitOpsResult {
1045 has_pr,
1046 has_pushed,
1047 has_commits,
1048 pr_base_invalid: false,
1049 }
1050}
1051
1052async fn close_superseded_pr(ctx: &StoreCtx<'_>, pr_number: u64) {
1056 if ctx.repo.is_empty() {
1057 return;
1058 }
1059 match GhHttp::new() {
1060 Ok(gh) => {
1061 let note = "Closing as superseded: the change is already on the default branch, so this branch has no commits ahead of it.";
1062 if let Err(e) = gh.add_comment(ctx.repo, &pr_number.to_string(), note).await {
1063 tracing::warn!(task_id = ctx.task_id, pr_number, err = %e, "failed to comment on superseded PR");
1064 }
1065 if let Err(e) = gh.close_pr(ctx.repo, pr_number).await {
1066 tracing::warn!(task_id = ctx.task_id, pr_number, err = %e, "failed to close superseded PR");
1067 }
1068 }
1069 Err(e) => {
1070 tracing::warn!(task_id = ctx.task_id, err = %e, "cannot close superseded PR");
1071 }
1072 }
1073 ctx.set(&[
1075 (
1076 "last_error",
1077 serde_json::json!(
1078 "PR closed as superseded: the fix already landed on the default branch"
1079 ),
1080 ),
1081 ("pr_number", serde_json::json!(null)),
1082 ])
1083 .await;
1084}
1085
1086fn superseded_conflict_pr(
1092 merge_conflict_retries: i32,
1093 pr_number: Option<i32>,
1094 has_commits: bool,
1095) -> Option<u64> {
1096 if has_commits || merge_conflict_retries <= 0 {
1097 return None;
1098 }
1099 pr_number
1100 .filter(|n| *n > 0)
1101 .and_then(|n| u64::try_from(n).ok())
1102}
1103
1104async fn detect_push_failure_state(
1106 ctx: &StoreCtx<'_>,
1107 has_commits: bool,
1108 has_pushed: bool,
1109) -> PushFailureState {
1110 let stored_last_error = ctx
1112 .get_task()
1113 .await
1114 .map(|t| t.last_error)
1115 .unwrap_or_default();
1116
1117 let push_failed = !has_pushed && has_commits && stored_last_error.contains("push failed");
1118
1119 let is_workflow_scope_failure = push_failed
1122 && (stored_last_error.contains("workflow` scope")
1123 || stored_last_error.contains("workflow' scope")
1124 || (stored_last_error.contains("refusing to allow")
1125 && stored_last_error.contains("workflow")));
1126
1127 let is_rebase_conflict_failure =
1128 push_failed && stored_last_error.contains("push failed and rebase failed:");
1129
1130 let is_transient_failure = push_failed
1131 && !is_workflow_scope_failure
1132 && !is_rebase_conflict_failure
1133 && git_ops::is_transient_network_error(&stored_last_error);
1134
1135 let push_failures = if push_failed && !is_workflow_scope_failure && !is_rebase_conflict_failure
1140 {
1141 match ctx.increment("push_failures").await {
1142 Ok(v) => v,
1143 Err(e) => {
1144 tracing::warn!(
1145 task_id = ctx.task_id,
1146 err = %e,
1147 "failed to increment push_failures — reading current value from store"
1148 );
1149 ctx.get_task()
1151 .await
1152 .map_or(1, |t| u64::try_from(t.push_failures).unwrap_or(0) + 1)
1153 }
1155 }
1156 } else {
1157 0
1158 };
1159
1160 PushFailureState {
1161 push_failed,
1162 is_workflow_scope_failure,
1163 is_rebase_conflict_failure,
1164 is_transient_failure,
1165 push_failures,
1166 stored_last_error,
1167 }
1168}
1169
1170async fn detect_no_code_reroutes(
1173 ctx: &StoreCtx<'_>,
1174 is_no_code_reroute: bool,
1175 new_attempts: u32,
1176 max_reroutes: u32,
1177) -> u64 {
1178 if !is_no_code_reroute {
1179 return 0;
1180 }
1181 tracing::warn!(
1182 task_id = ctx.task_id,
1183 attempts = new_attempts,
1184 max_reroutes,
1185 "agent reported done but produced no code changes on external task requiring PR"
1186 );
1187 match ctx.increment("no_code_reroutes").await {
1188 Ok(v) => v,
1189 Err(e) => {
1190 tracing::warn!(
1191 task_id = ctx.task_id,
1192 err = %e,
1193 "failed to increment no_code_reroutes — reading current value from store"
1194 );
1195 ctx.get_task()
1197 .await
1198 .map_or(1, |t| u64::try_from(t.no_code_reroutes).unwrap_or(0) + 1)
1199 }
1201 }
1202}
1203
1204fn agent_blocked_reason(resp: &AgentResponse) -> String {
1211 let summary_opt = if resp.summary.is_empty() {
1212 None
1213 } else {
1214 Some(resp.summary.as_str())
1215 };
1216 let base = resp
1217 .error
1218 .as_deref()
1219 .filter(|s| !s.is_empty())
1220 .or(summary_opt)
1221 .unwrap_or("agent returned blocked status without a reason");
1222 if resp.remaining.is_empty() {
1223 base.to_string()
1224 } else {
1225 format!("{base}. Remaining: {}", resp.remaining.join("; "))
1226 }
1227}
1228
1229fn is_denied_push_block(resp: &AgentResponse) -> bool {
1231 let r = agent_blocked_reason(resp).to_lowercase();
1232 r.contains("push")
1233 && [
1234 "denied",
1235 "not allowed",
1236 "permission",
1237 "disallowed",
1238 "approval",
1239 ]
1240 .iter()
1241 .any(|p| r.contains(p))
1242}
1243
1244fn is_retryable_blocked_reason(reason: &str) -> bool {
1250 let r = reason.to_lowercase();
1251
1252 let permanent_patterns = [
1253 "not found",
1254 "does not exist",
1255 "invalid",
1256 "malformed",
1257 "syntax error",
1258 "unsupported",
1259 "unimplemented",
1260 "manual intervention required",
1261 "human intervention required",
1262 ];
1263 if permanent_patterns.iter().any(|p| r.contains(p)) {
1264 return false;
1265 }
1266
1267 let transient_patterns = [
1268 "unavailable",
1269 "temporarily unavailable",
1270 "timeout",
1271 "timed out",
1272 "connection reset",
1273 "connection refused",
1274 "network error",
1275 "rate limit",
1276 "try again",
1277 "service down",
1278 "permission denied",
1279 "worktree lock",
1280 "git lock",
1281 "lockfile permission",
1282 "lock permission",
1283 "resource busy",
1284 "could not resolve host",
1285 "gpg decryption fails",
1288 "gpg decryption failed",
1289 "gpg agent",
1290 "no passphrase available",
1291 "passphrase unavailable",
1292 "pass store",
1293 "passwordstoreprovider",
1294 "agentvaultprovider",
1295 "onepasswordprovider",
1296 "decryption failed in this agent session",
1297 ];
1298 transient_patterns.iter().any(|p| r.contains(p))
1299}
1300
1301#[expect(
1303 clippy::too_many_arguments,
1304 reason = "the decision and its context arrive as separate values"
1305)]
1306async fn apply_post_decision_effects(
1307 ctx: &StoreCtx<'_>,
1308 final_status: &str,
1309 resp_status: &str,
1310 push_state: &PushFailureState,
1311 is_no_code_reroute: bool,
1312 has_pr: bool,
1313 has_pushed: bool,
1314 is_pr_base_invalid: bool,
1315 has_delegations: bool,
1316 is_external: bool,
1317 no_code_reroutes: u64,
1318 max_reroutes: u32,
1319 agent_name: &str,
1320) {
1321 if push_state.is_workflow_scope_failure {
1322 tracing::error!(
1324 task_id = ctx.task_id,
1325 "push failed: token lacks `workflow` OAuth scope — blocking immediately \
1326 (rerouting would not help)"
1327 );
1328 ctx.set(&[(
1329 "last_error",
1330 serde_json::json!(format!(
1331 "push failed: GitHub token lacks `workflow` OAuth scope. \
1332 The agent modified .github/workflows/ files but the token cannot push them. \
1333 Fix: add `workflow` scope to your GitHub token, or use a GitHub App for auth. \
1334 Original error: {}",
1335 push_state.stored_last_error
1336 )),
1337 )])
1338 .await;
1339 } else if push_state.is_rebase_conflict_failure {
1340 tracing::error!(
1341 task_id = ctx.task_id,
1342 "push failed and automatic rebase recovery failed — blocking for human intervention"
1343 );
1344 ctx.set(&[
1345 ("agent", serde_json::json!(null)),
1346 ("model", serde_json::json!(null)),
1347 ])
1348 .await;
1349 } else if push_state.push_failed && final_status == "needs_review" {
1350 tracing::warn!(
1353 task_id = ctx.task_id,
1354 push_failures = push_state.push_failures,
1355 "agent done but push failed on a GitHub or network error — engine retries the push"
1356 );
1357 } else if push_state.push_failed {
1358 ctx.set(&[
1360 ("agent", serde_json::json!(null)),
1361 ("model", serde_json::json!(null)),
1362 ])
1363 .await;
1364 let push_failures = push_state.push_failures;
1365 if push_failures >= 3 {
1366 tracing::error!(
1367 task_id = ctx.task_id,
1368 push_failures,
1369 "push failed {push_failures} times — blocking for human intervention"
1370 );
1371 } else {
1372 tracing::warn!(
1373 task_id = ctx.task_id,
1374 push_failures,
1375 "agent done but push failed ({push_failures}/3) — rerouting to different agent"
1376 );
1377 }
1378 } else if resp_status == "done" && !has_pr && has_delegations {
1379 tracing::info!(
1380 task_id = ctx.task_id,
1381 "agent reported done with delegations but no PR — setting blocked"
1382 );
1383 } else if (resp_status == "done" || resp_status == "completed")
1384 && !has_pr
1385 && has_pushed
1386 && is_pr_base_invalid
1387 {
1388 tracing::error!(
1389 task_id = ctx.task_id,
1390 "PR creation failed with invalid base branch — blocking for human intervention"
1391 );
1392 ctx.set(&[(
1393 "last_error",
1394 serde_json::json!(
1395 "PR creation failed terminally (invalid base branch or GitHub auth error). The agent's commits were pushed successfully. \
1396 Check the repository default branch and the GitHub token, then create the PR manually or unblock the task."
1397 ),
1398 )])
1399 .await;
1400 } else if resp_status == "done" && !has_pr && has_pushed {
1401 tracing::warn!(
1403 task_id = ctx.task_id,
1404 "agent done, commits pushed, but PR creation failed — re-dispatching as routed"
1405 );
1406 } else if is_no_code_reroute {
1407 if final_status == "blocked" {
1412 let prev_no_code_agent = ctx
1416 .get_task()
1417 .await
1418 .map(|t| t.no_code_last_agent)
1419 .unwrap_or_default();
1420 let is_same_agent = !prev_no_code_agent.is_empty() && prev_no_code_agent == agent_name;
1421
1422 let msg = if is_same_agent {
1423 tracing::error!(
1424 task_id = ctx.task_id,
1425 agent = %agent_name,
1426 "blocking same-agent no-code loop: agent {} would be selected again",
1427 agent_name
1428 );
1429 format!(
1430 "agent {agent_name} completed without code changes twice — same-agent loop detected, blocking for human review"
1431 )
1432 } else {
1433 tracing::error!(
1434 task_id = ctx.task_id,
1435 no_code_reroutes,
1436 max_reroutes,
1437 "blocking no-code reroute: reached max reroute attempts ({}/{}) for no-code-result",
1438 no_code_reroutes,
1439 max_reroutes
1440 );
1441 format!(
1442 "agent completed without code changes after {no_code_reroutes}/{max_reroutes} reroute attempts on external task requiring PR"
1443 )
1444 };
1445 ctx.set(&[("last_error", serde_json::json!(msg))]).await;
1446 } else {
1447 ctx.set(&[(
1449 "last_error",
1450 serde_json::json!(
1451 "agent completed without code changes on external task requiring PR"
1452 ),
1453 )])
1454 .await;
1455 }
1456
1457 ctx.set(&[
1462 ("no_code_last_agent", serde_json::json!(agent_name)),
1463 ("agent", serde_json::json!(null)),
1464 ("model", serde_json::json!(null)),
1465 ])
1466 .await;
1467 } else if resp_status == "done" && !has_pr && is_external {
1468 tracing::info!(
1471 task_id = ctx.task_id,
1472 "external task with non-code labels reported done — marking done without PR"
1473 );
1474 } else if resp_status == "done" && !has_pr {
1475 tracing::info!(
1476 task_id = ctx.task_id,
1477 "internal task reported done with no PR — marking done"
1478 );
1479 }
1480}
1481
1482async fn store_token_usage(
1485 ctx: &StoreCtx<'_>,
1486 input_tokens: Option<u64>,
1487 output_tokens: Option<u64>,
1488 cost_usd: Option<f64>,
1489) {
1490 let (Some(input), Some(output)) = (input_tokens, output_tokens) else {
1491 return;
1492 };
1493 if let Some(st) = ctx.store {
1494 if let Some(store_id) = ctx.store_id_opt {
1495 if let Err(e) = st.store_tokens(store_id, input, output, cost_usd).await {
1496 tracing::warn!(task_id = ctx.task_id, ?e, "failed to store token usage");
1497 }
1498 } else if let Ok(Some(store_id)) = st.resolve_task_id(ctx.repo, ctx.task_id).await
1499 && let Err(e) = st.store_tokens(store_id, input, output, cost_usd).await
1500 {
1501 tracing::warn!(task_id = ctx.task_id, ?e, "failed to store token usage");
1502 }
1503 }
1504}
1505
1506#[expect(
1512 clippy::too_many_arguments,
1513 reason = "the runner passes each part of the run result"
1514)]
1515pub async fn handle_success(
1516 task_id: &str,
1517 parsed: agents::ParsedResponse,
1518 wt: &worktree::WorktreeSetup,
1519 task_title: &str,
1520 agent_name: &str,
1521 model_name: Option<&str>,
1522 new_attempts: u32,
1523 repo: &str,
1524 store: &Option<Arc<TaskStore>>,
1525 raw_stdout: &str,
1526) -> anyhow::Result<(String, bool)> {
1527 let input_tokens = parsed.input_tokens;
1529 let output_tokens = parsed.output_tokens;
1530 let cost_usd = parsed.cost_usd;
1531 let mut resp = parsed.response;
1532
1533 tracing::info!(
1534 task_id,
1535 status = resp.status,
1536 summary = resp.summary,
1537 "agent completed successfully"
1538 );
1539
1540 let push_denied = resp.status == "blocked" && is_denied_push_block(&resp);
1541
1542 let store_id_opt = store::resolve_store_id(store, repo, task_id).await;
1545 let ctx = StoreCtx {
1546 store,
1547 store_id_opt,
1548 repo,
1549 task_id,
1550 };
1551
1552 ctx.reset_retry_streaks().await;
1553
1554 let git = if resp.status == "done"
1556 || resp.status == "completed"
1557 || resp.status == "in_progress"
1558 || resp.status == "needs_review"
1559 || push_denied
1560 {
1561 run_git_ops(
1562 &ctx,
1563 wt,
1564 task_title,
1565 &resp,
1566 agent_name,
1567 model_name,
1568 new_attempts,
1569 )
1570 .await
1571 } else {
1572 GitOpsResult::default()
1573 };
1574
1575 if push_denied && (git.has_pr || git.has_pushed) {
1578 tracing::info!(
1579 task_id,
1580 "agent blocked on denied push, orch pushed the branch"
1581 );
1582 resp.status = "done".to_string();
1583 }
1584
1585 if !resp.delegations.is_empty() {
1587 ctx.set(&[("delegations", serde_json::json!(resp.delegations))])
1588 .await;
1589 }
1590
1591 let has_delegations = !resp.delegations.is_empty();
1598
1599 let push_state = detect_push_failure_state(&ctx, git.has_commits, git.has_pushed).await;
1601
1602 let is_external = !task_id.starts_with("internal:");
1610 let requires_pr = is_external && !git.has_pr && !resp.status.starts_with("needs_review");
1611
1612 let max_reroutes: u32 = config::get("workflow.max_reroute_attempts")
1615 .ok()
1616 .and_then(|s| s.parse().ok())
1617 .or_else(|| {
1618 config::get("workflow.max_attempts")
1619 .ok()
1620 .and_then(|s| s.parse().ok())
1621 })
1622 .unwrap_or(3);
1623
1624 let is_no_code_reroute = !push_state.is_workflow_scope_failure
1627 && !push_state.push_failed
1628 && (resp.status == "done" || resp.status == "completed")
1629 && !has_delegations
1630 && !git.has_pushed
1631 && requires_pr;
1632
1633 let superseded_pr = match (is_no_code_reroute, ctx.get_task().await) {
1636 (true, Some(task)) => {
1637 superseded_conflict_pr(task.merge_conflict_retries, task.pr_number, git.has_commits)
1638 }
1639 _ => None,
1640 };
1641 let is_no_code_reroute = is_no_code_reroute && superseded_pr.is_none();
1642
1643 let no_code_reroutes =
1644 detect_no_code_reroutes(&ctx, is_no_code_reroute, new_attempts, max_reroutes).await;
1645
1646 let blocked_reason = if resp.status == "blocked" {
1647 Some(agent_blocked_reason(&resp))
1648 } else {
1649 None
1650 };
1651 let is_retryable_blocked = blocked_reason
1652 .as_deref()
1653 .is_some_and(is_retryable_blocked_reason);
1654
1655 let is_same_agent = if is_no_code_reroute {
1660 let prev_no_code_agent = ctx
1661 .get_task()
1662 .await
1663 .map(|t| t.no_code_last_agent)
1664 .unwrap_or_default();
1665 !prev_no_code_agent.is_empty() && prev_no_code_agent == agent_name
1666 } else {
1667 false
1668 };
1669
1670 let final_status_owned = classify_final_status(&DecisionInput {
1673 agent_status: &resp.status,
1674 is_workflow_scope_failure: push_state.is_workflow_scope_failure,
1675 is_rebase_conflict_failure: push_state.is_rebase_conflict_failure,
1676 push_failed: push_state.push_failed,
1677 push_failures: push_state.push_failures,
1678 is_transient_push_failure: push_state.is_transient_failure,
1679 has_pr: git.has_pr,
1680 has_delegations,
1681 has_pushed: git.has_pushed,
1682 is_pr_base_invalid: git.pr_base_invalid,
1683 requires_pr,
1684 no_code_reroutes,
1685 max_reroutes,
1686 is_same_agent,
1687 is_completed_status: resp.status == "completed",
1688 is_retryable_blocked,
1689 is_superseded_conflict: superseded_pr.is_some(),
1690 });
1691 let final_status = final_status_owned.as_str();
1692
1693 apply_post_decision_effects(
1696 &ctx,
1697 final_status,
1698 &resp.status,
1699 &push_state,
1700 is_no_code_reroute,
1701 git.has_pr,
1702 git.has_pushed,
1703 git.pr_base_invalid,
1704 has_delegations,
1705 is_external,
1706 no_code_reroutes,
1707 max_reroutes,
1708 agent_name,
1709 )
1710 .await;
1711
1712 if resp.status == "blocked" {
1718 let reason = blocked_reason.unwrap_or_else(|| agent_blocked_reason(&resp));
1719 if final_status == "new" {
1720 cooldown::record_agent_failure_with_message(agent_name, &reason).await;
1723 if let Some(m) = model_name {
1724 cooldown::record_model_failure(agent_name, m).await;
1725 }
1726 ctx.set(&[
1727 (
1728 "last_error",
1729 serde_json::json!(format!(
1730 "transient blocker (auto-retry scheduled): {reason}"
1731 )),
1732 ),
1733 ("agent", serde_json::json!(null)),
1734 ("model", serde_json::json!(null)),
1735 ])
1736 .await;
1737 tracing::warn!(
1738 task_id = ctx.task_id,
1739 reason = %reason,
1740 "agent returned retryable blocked status — rerouting as new with cooldown"
1741 );
1742 } else if final_status == "blocked" {
1743 ctx.set(&[("last_error", serde_json::json!(reason))]).await;
1744 }
1745 }
1746
1747 if let Some(pr_number) = superseded_pr {
1748 tracing::info!(
1749 task_id = ctx.task_id,
1750 pr_number,
1751 "conflict reroute found the fix already on the default branch — closing superseded PR"
1752 );
1753 close_superseded_pr(&ctx, pr_number).await;
1754 }
1755
1756 ctx.set(&[("summary", serde_json::json!(resp.summary))])
1757 .await;
1758
1759 store_token_usage(
1761 &ctx,
1762 input_tokens.or(resp.input_tokens),
1763 output_tokens.or(resp.output_tokens),
1764 cost_usd,
1765 )
1766 .await;
1767
1768 response::store_learnings_from_response(
1770 task_id,
1771 new_attempts,
1772 agent_name,
1773 model_name,
1774 &resp,
1775 resp.error.as_deref(),
1776 store,
1777 repo,
1778 raw_stdout,
1779 )
1780 .await;
1781
1782 Ok((final_status.to_string(), push_state.push_failed))
1785}
1786
1787#[cfg(test)]
1789mod tests {
1790 use std::fs as std_fs;
1791 use std::process::Command;
1792
1793 use super::*;
1794 use crate::store::NewTask;
1795
1796 #[tokio::test]
1798 async fn successful_run_resets_timeout_retries() {
1799 let store = Some(Arc::new(TaskStore::open_memory().await.unwrap()));
1800 let id = store
1801 .as_ref()
1802 .unwrap()
1803 .create_internal("owner/repo", "t", "b", "test", "", None)
1804 .await
1805 .unwrap();
1806 let task_id = format!("internal:{id}");
1807 let ctx = StoreCtx {
1808 store: &store,
1809 store_id_opt: None,
1810 repo: "owner/repo",
1811 task_id: &task_id,
1812 };
1813
1814 ctx.increment("timeout_retries").await.unwrap();
1815 ctx.increment("network_retries").await.unwrap();
1816 ctx.reset_retry_streaks().await;
1817 ctx.increment("timeout_retries").await.unwrap();
1818 ctx.increment("timeout_retries").await.unwrap();
1819
1820 let task = ctx.get_task().await.unwrap();
1821 assert_eq!(task.timeout_retries, 2);
1822 assert_eq!(task.network_retries, 0);
1823 assert_ne!(task.status.as_str(), "blocked");
1824 }
1825
1826 #[test]
1827 fn local_rate_limit_skip_is_detected() {
1828 assert!(is_local_rate_limit_skip(
1829 "GitHub API error: GitHub REST API rate-limited, skipping call for 236s"
1830 ));
1831 assert!(!is_local_rate_limit_skip(
1832 "GitHub API POST failed (502): Bad Gateway"
1833 ));
1834 }
1835
1836 fn git(dir: &Path, args: &[&str]) {
1838 let out = Command::new("git")
1839 .current_dir(dir)
1840 .args(args)
1841 .output()
1842 .unwrap_or_else(|e| panic!("git {args:?} failed to start: {e}"));
1843 assert!(
1844 out.status.success(),
1845 "git {args:?} failed:\nstdout: {}\nstderr: {}",
1846 String::from_utf8_lossy(&out.stdout),
1847 String::from_utf8_lossy(&out.stderr)
1848 );
1849 }
1850
1851 fn init_clone(remote: &Path, dir: &Path) {
1852 git(dir, &["clone", remote.to_str().unwrap(), "."]);
1853 git(dir, &["config", "user.email", "t@t.com"]);
1854 git(dir, &["config", "user.name", "T"]);
1855 git(dir, &["config", "commit.gpgsign", "false"]);
1856 }
1857
1858 #[test]
1859 fn remote_rejected_failure_is_not_diverged() {
1860 let err = "remote: fatal error in commit_refs\n ! [remote rejected] b -> b (failure)";
1861 assert!(!push_error_is_diverged(err));
1862 assert!(push_error_is_diverged("! [rejected] b -> b (fetch first)"));
1863 assert!(push_error_is_diverged(
1864 "! [rejected] b -> b (non-fast-forward)"
1865 ));
1866 }
1867
1868 #[tokio::test]
1875 async fn push_rejected_after_remote_rebase_is_pushed_not_rerouted() {
1876 let remote = tempfile::tempdir().unwrap();
1877 git(remote.path(), &["init", "--bare", "-b", "main"]);
1878
1879 let work = tempfile::tempdir().unwrap();
1881 init_clone(remote.path(), work.path());
1882 std_fs::write(work.path().join("README.md"), "base").unwrap();
1883 git(work.path(), &["add", "."]);
1884 git(work.path(), &["commit", "-m", "init"]);
1885 git(work.path(), &["push", "origin", "HEAD:main"]);
1886 git(work.path(), &["checkout", "-b", "feat"]);
1887 std_fs::write(work.path().join("fix.txt"), "the fix").unwrap();
1888 git(work.path(), &["add", "."]);
1889 git(work.path(), &["commit", "-m", "apply the fix"]);
1890 git(work.path(), &["push", "-u", "origin", "feat"]);
1891
1892 let other = tempfile::tempdir().unwrap();
1894 init_clone(remote.path(), other.path());
1895 git(other.path(), &["checkout", "feat"]);
1896 git(
1897 other.path(),
1898 &["commit", "--amend", "-m", "apply the fix (rebased)"],
1899 );
1900 git(other.path(), &["push", "--force", "origin", "feat"]);
1901
1902 std_fs::write(work.path().join("more.txt"), "follow-up").unwrap();
1904 git(work.path(), &["add", "."]);
1905 git(work.path(), &["commit", "-m", "follow-up"]);
1906
1907 let store = Arc::new(TaskStore::open_memory().await.unwrap());
1908 let store_id = store
1909 .create_internal("owner/repo", "Push recovery", "", "task", "3783", None)
1910 .await
1911 .unwrap();
1912 let task_id = format!("internal:{store_id}");
1913 let store_opt = Some(Arc::clone(&store));
1914 let ctx = StoreCtx {
1915 store: &store_opt,
1916 store_id_opt: Some(store_id),
1917 repo: "owner/repo",
1918 task_id: &task_id,
1919 };
1920
1921 let result =
1922 push_branch_with_log(&ctx, work.path(), "feat", "main", "claude", Some("opus")).await;
1923
1924 assert!(
1925 matches!(result, PushResult::Success),
1926 "orch must land the push itself after adopting the moved remote branch"
1927 );
1928
1929 let task = store.get(store_id).await.unwrap();
1930 assert!(
1931 task.last_error.is_empty(),
1932 "a recovered push must leave no error behind, got: {}",
1933 task.last_error
1934 );
1935 assert_eq!(task.push_failures, 0, "recovery is not a push failure");
1936
1937 let log = Command::new("git")
1940 .current_dir(work.path())
1941 .args(["log", "--oneline", "origin/feat"])
1942 .output()
1943 .unwrap();
1944 let log = String::from_utf8_lossy(&log.stdout);
1945 assert!(
1946 log.contains("follow-up"),
1947 "remote must carry the task's commit: {log}"
1948 );
1949 assert!(
1950 log.contains("apply the fix (rebased)"),
1951 "orch must adopt the rebased remote history, not re-push its own: {log}"
1952 );
1953 assert!(
1954 !log.contains("apply the fix\n"),
1955 "the superseded commit must not come back: {log}"
1956 );
1957 }
1958
1959 #[test]
1960 fn missing_path_error_detects_io_not_found() {
1961 let err = anyhow::Error::from(io::Error::new(
1962 ErrorKind::NotFound,
1963 "simulated missing path",
1964 ));
1965 assert!(is_missing_path_error(&err));
1966 }
1967
1968 #[test]
1969 fn missing_path_error_detects_message_fallback() {
1970 let err = anyhow::anyhow!("push failed: No such file or directory (os error 2)");
1971 assert!(is_missing_path_error(&err));
1972 }
1973
1974 #[test]
1975 fn superseded_conflict_pr_requires_conflict_reroute_and_no_commits() {
1976 assert_eq!(superseded_conflict_pr(1, Some(42), false), Some(42));
1978 assert_eq!(superseded_conflict_pr(1, Some(42), true), None);
1980 assert_eq!(superseded_conflict_pr(0, Some(42), false), None);
1982 assert_eq!(superseded_conflict_pr(1, None, false), None);
1984 }
1985
1986 #[tokio::test]
1990 async fn conflict_reroute_with_no_commits_closes_pr_and_marks_done() {
1991 fn git(dir: &Path, args: &[&str]) {
1992 let ok = Command::new("git")
1993 .args(args)
1994 .current_dir(dir)
1995 .env("GIT_AUTHOR_NAME", "t")
1996 .env("GIT_AUTHOR_EMAIL", "t@example.com")
1997 .env("GIT_COMMITTER_NAME", "t")
1998 .env("GIT_COMMITTER_EMAIL", "t@example.com")
1999 .output()
2000 .expect("git runs")
2001 .status
2002 .success();
2003 assert!(ok, "git {args:?} failed");
2004 }
2005
2006 let dir = tempfile::tempdir().unwrap();
2009 let origin = dir.path().join("origin.git");
2010 std_fs::create_dir(&origin).unwrap();
2011 git(&origin, &["init", "--bare", "-b", "main"]);
2012 let work_dir = dir.path().join("work");
2013 std_fs::create_dir(&work_dir).unwrap();
2014 git(&work_dir, &["init", "-b", "main"]);
2015 git(
2016 &work_dir,
2017 &["remote", "add", "origin", &origin.display().to_string()],
2018 );
2019 std_fs::write(work_dir.join("a.txt"), "a").unwrap();
2020 git(&work_dir, &["add", "-A"]);
2021 git(&work_dir, &["commit", "-m", "init"]);
2022 git(&work_dir, &["push", "-u", "origin", "main"]);
2023 git(&work_dir, &["checkout", "-b", "fix-branch"]);
2024
2025 let wt = worktree::WorktreeSetup {
2026 work_dir: work_dir.clone(),
2027 branch: "fix-branch".to_string(),
2028 default_branch: "main".to_string(),
2029 main_project_dir: work_dir.clone(),
2030 };
2031
2032 let store = Arc::new(TaskStore::open_memory().await.unwrap());
2033 let store_id = store
2034 .create(&NewTask {
2035 external_id: Some("3772".to_string()),
2036 repo: "owner/repo".to_string(),
2037 origin: "github".to_string(),
2038 title: "Fix the thing".to_string(),
2039 body: String::new(),
2040 source: "manual".to_string(),
2041 source_id: String::new(),
2042 author: "bot".to_string(),
2043 url: String::new(),
2044 labels: vec![],
2045 parent_id: None,
2046 })
2047 .await
2048 .unwrap();
2049 store
2050 .set_fields(store_id, &[("pr_number", serde_json::json!(42))])
2051 .await
2052 .unwrap();
2053 store
2055 .increment(store_id, "merge_conflict_retries")
2056 .await
2057 .unwrap();
2058 store
2061 .set_fields(
2062 store_id,
2063 &[("no_code_last_agent", serde_json::json!("claude"))],
2064 )
2065 .await
2066 .unwrap();
2067
2068 let parsed = agents::ParsedResponse {
2069 response: AgentResponse {
2070 status: "done".to_string(),
2071 summary: "the fix is already on main".to_string(),
2072 ..Default::default()
2073 },
2074 input_tokens: None,
2075 output_tokens: None,
2076 cost_usd: None,
2077 duration_ms: None,
2078 };
2079
2080 let (final_status, push_failed) = handle_success(
2081 "3772",
2082 parsed,
2083 &wt,
2084 "Fix the thing",
2085 "claude",
2086 Some("sonnet"),
2087 1,
2088 "owner/repo",
2089 &Some(Arc::clone(&store)),
2090 "",
2091 )
2092 .await
2093 .unwrap();
2094
2095 assert_eq!(
2096 final_status, "done",
2097 "a superseded conflict reroute must end done, not blocked"
2098 );
2099 assert!(!push_failed);
2100
2101 let task = store.get(store_id).await.unwrap();
2102 assert!(
2103 task.last_error.contains("superseded"),
2104 "the close attempt must be recorded on the task: {}",
2105 task.last_error
2106 );
2107 assert_eq!(
2108 task.pr_number, None,
2109 "a superseded PR must not stay on the task, the blocker refresh would read it as closed unmerged"
2110 );
2111 }
2112
2113 #[test]
2116 fn classify_superseded_conflict_marks_done() {
2117 let status = classify_final_status(&DecisionInput {
2118 agent_status: "done",
2119 requires_pr: true,
2120 is_same_agent: true,
2121 is_superseded_conflict: true,
2122 ..Default::default()
2123 });
2124 assert_eq!(status, "done");
2125 }
2126
2127 #[test]
2131 fn classify_workflow_scope_failure_blocks() {
2132 let status = classify_final_status(&DecisionInput {
2133 is_workflow_scope_failure: true,
2134 push_failed: true,
2135 push_failures: 1,
2136 ..Default::default()
2137 });
2138 assert_eq!(status, "blocked");
2139 }
2140
2141 #[test]
2143 fn classify_rebase_conflict_failure_blocks() {
2144 let status = classify_final_status(&DecisionInput {
2145 is_rebase_conflict_failure: true,
2146 push_failed: true,
2147 ..Default::default()
2148 });
2149 assert_eq!(status, "blocked");
2150 }
2151
2152 #[test]
2154 fn classify_push_failed_under_threshold_reroutes() {
2155 let status = classify_final_status(&DecisionInput {
2156 push_failed: true,
2157 push_failures: 2, ..Default::default()
2159 });
2160 assert_eq!(status, "new");
2161 }
2162
2163 #[test]
2164 fn denied_push_block_detected() {
2165 let resp = |s: &str| AgentResponse {
2166 status: "blocked".to_string(),
2167 summary: s.to_string(),
2168 ..Default::default()
2169 };
2170 assert!(is_denied_push_block(&resp(
2171 "Work committed, but `git push -u origin HEAD` was denied"
2172 )));
2173 assert!(is_denied_push_block(&resp(
2174 "It is not pushed: `git push --force-with-lease` needs approval that this session could not get."
2175 )));
2176 assert!(is_denied_push_block(&resp(
2177 "`git push --force-with-lease` was denied because this session cannot get approval"
2178 )));
2179 assert!(is_denied_push_block(&resp("The push requires approval")));
2180 assert!(!is_denied_push_block(&resp("Missing API key")));
2181 }
2182
2183 #[test]
2185 fn classify_push_failed_at_threshold_blocks() {
2186 let status = classify_final_status(&DecisionInput {
2187 push_failed: true,
2188 push_failures: 3, ..Default::default()
2190 });
2191 assert_eq!(status, "blocked");
2192 }
2193
2194 #[test]
2196 fn classify_transient_push_failure_waits_for_the_engine_retry() {
2197 for push_failures in 1..push_retry::MAX_PUSH_FAILURES {
2198 let status = classify_final_status(&DecisionInput {
2199 push_failed: true,
2200 is_transient_push_failure: true,
2201 push_failures,
2202 ..Default::default()
2203 });
2204 assert_eq!(status, "needs_review", "push_failures={push_failures}");
2205 }
2206 }
2207
2208 #[test]
2209 fn classify_transient_push_failure_blocks_after_the_last_retry() {
2210 let status = classify_final_status(&DecisionInput {
2211 push_failed: true,
2212 is_transient_push_failure: true,
2213 push_failures: push_retry::MAX_PUSH_FAILURES,
2214 ..Default::default()
2215 });
2216 assert_eq!(status, "blocked");
2217 }
2218
2219 #[test]
2221 fn classify_done_with_pr_goes_to_needs_review() {
2222 let status = classify_final_status(&DecisionInput {
2223 agent_status: "done",
2224 has_pr: true,
2225 ..Default::default()
2226 });
2227 assert_eq!(status, "needs_review");
2228 }
2229
2230 #[test]
2232 fn classify_done_with_delegations_no_pr_blocks() {
2233 let status = classify_final_status(&DecisionInput {
2234 agent_status: "done",
2235 has_delegations: true,
2236 ..Default::default()
2237 });
2238 assert_eq!(status, "blocked");
2239 }
2240
2241 #[test]
2243 fn classify_done_pushed_no_pr_reroutes_for_pr_creation() {
2244 let status = classify_final_status(&DecisionInput {
2245 agent_status: "done",
2246 has_pushed: true,
2247 ..Default::default()
2248 });
2249 assert_eq!(status, "routed");
2250 }
2251
2252 #[test]
2254 fn classify_done_pushed_pr_base_invalid_blocks() {
2255 let status = classify_final_status(&DecisionInput {
2256 agent_status: "done",
2257 has_pushed: true,
2258 is_pr_base_invalid: true,
2259 ..Default::default()
2260 });
2261 assert_eq!(status, "blocked");
2262 }
2263
2264 #[test]
2266 fn classify_completed_pushed_pr_base_invalid_blocks() {
2267 let status = classify_final_status(&DecisionInput {
2268 agent_status: "completed",
2269 is_completed_status: true,
2270 has_pushed: true,
2271 is_pr_base_invalid: true,
2272 ..Default::default()
2273 });
2274 assert_eq!(status, "blocked");
2275 }
2276
2277 #[test]
2279 fn classify_done_requires_pr_under_max_reroutes() {
2280 let status = classify_final_status(&DecisionInput {
2281 agent_status: "done",
2282 requires_pr: true,
2283 no_code_reroutes: 1,
2284 max_reroutes: 3,
2285 is_same_agent: false,
2286 ..Default::default()
2287 });
2288 assert_eq!(status, "new");
2289 }
2290
2291 #[test]
2293 fn classify_done_requires_pr_same_agent_blocks_immediately() {
2294 let status = classify_final_status(&DecisionInput {
2295 agent_status: "done",
2296 requires_pr: true,
2297 no_code_reroutes: 1,
2298 max_reroutes: 3,
2299 is_same_agent: true, ..Default::default()
2301 });
2302 assert_eq!(status, "blocked");
2303 }
2304
2305 #[test]
2307 fn classify_done_requires_pr_at_max_reroutes_blocks() {
2308 let status = classify_final_status(&DecisionInput {
2309 agent_status: "done",
2310 requires_pr: true,
2311 no_code_reroutes: 3,
2312 max_reroutes: 3,
2313 is_same_agent: false,
2314 ..Default::default()
2315 });
2316 assert_eq!(status, "blocked");
2317 }
2318
2319 #[test]
2321 fn classify_done_requires_pr_exceeds_max_reroutes_blocks() {
2322 let status = classify_final_status(&DecisionInput {
2323 agent_status: "done",
2324 requires_pr: true,
2325 no_code_reroutes: 5,
2326 max_reroutes: 3,
2327 is_same_agent: false,
2328 ..Default::default()
2329 });
2330 assert_eq!(status, "blocked");
2331 }
2332
2333 #[test]
2335 fn classify_done_external_no_pushed_same_agent_blocks_immediately() {
2336 let status = classify_final_status(&DecisionInput {
2337 agent_status: "done",
2338 requires_pr: true, no_code_reroutes: 0,
2340 max_reroutes: 3,
2341 is_same_agent: true,
2342 ..Default::default()
2343 });
2344 assert_eq!(status, "blocked");
2345 }
2346
2347 #[test]
2355 fn classify_done_external_no_pushed_commits_reroutes() {
2356 let status = classify_final_status(&DecisionInput {
2357 agent_status: "done",
2358 has_pushed: false,
2359 requires_pr: true, no_code_reroutes: 0,
2361 max_reroutes: 3,
2362 is_same_agent: false,
2363 ..Default::default()
2364 });
2365 assert_eq!(status, "new");
2366 }
2367
2368 #[test]
2371 fn classify_done_internal_task_no_pr_is_done() {
2372 let status = classify_final_status(&DecisionInput {
2373 agent_status: "done",
2374 has_pushed: false,
2375 requires_pr: false,
2376 ..Default::default()
2377 });
2378 assert_eq!(status, "done");
2379 }
2380
2381 #[test]
2383 fn classify_non_done_status_passes_through() {
2384 for status_str in &["in_progress", "needs_review", "blocked"] {
2385 let result = classify_final_status(&DecisionInput {
2386 agent_status: status_str,
2387 ..Default::default()
2388 });
2389 assert_eq!(
2390 result, *status_str,
2391 "status '{status_str}' should pass through unchanged"
2392 );
2393 }
2394 }
2395
2396 #[test]
2398 fn classify_descriptive_completion_status_normalizes_to_done() {
2399 let status = classify_final_status(&DecisionInput {
2400 agent_status: "Trading scan complete. Nothing to trade.",
2401 ..Default::default()
2402 });
2403 assert_eq!(status, "done");
2404 }
2405
2406 #[test]
2408 fn classify_descriptive_completion_with_failure_cues_does_not_normalize() {
2409 let status = classify_final_status(&DecisionInput {
2410 agent_status: "completed but failed to push branch due to error",
2411 ..Default::default()
2412 });
2413 assert_eq!(status, "completed but failed to push branch due to error");
2414 }
2415
2416 #[test]
2418 fn classify_push_failed_takes_precedence_over_has_pr() {
2419 let status = classify_final_status(&DecisionInput {
2420 agent_status: "done",
2421 has_pr: true,
2422 push_failed: true,
2423 push_failures: 1,
2424 ..Default::default()
2425 });
2426 assert_eq!(status, "new");
2428 }
2429
2430 #[test]
2432 fn classify_workflow_scope_failure_takes_precedence_over_push_reroute() {
2433 let status = classify_final_status(&DecisionInput {
2434 is_workflow_scope_failure: true,
2435 push_failed: true,
2436 push_failures: 1, ..Default::default()
2438 });
2439 assert_eq!(status, "blocked");
2440 }
2441
2442 #[test]
2446 fn classify_completed_with_pr_goes_to_needs_review() {
2447 let status = classify_final_status(&DecisionInput {
2448 agent_status: "completed",
2449 has_pr: true,
2450 is_completed_status: true,
2451 ..Default::default()
2452 });
2453 assert_eq!(status, "needs_review");
2454 }
2455
2456 #[test]
2458 fn classify_completed_with_delegations_no_pr_blocks() {
2459 let status = classify_final_status(&DecisionInput {
2460 agent_status: "completed",
2461 has_delegations: true,
2462 is_completed_status: true,
2463 ..Default::default()
2464 });
2465 assert_eq!(status, "blocked");
2466 }
2467
2468 #[test]
2470 fn classify_completed_pushed_no_pr_reroutes_for_pr_creation() {
2471 let status = classify_final_status(&DecisionInput {
2472 agent_status: "completed",
2473 has_pushed: true,
2474 is_completed_status: true,
2475 ..Default::default()
2476 });
2477 assert_eq!(status, "routed");
2478 }
2479
2480 #[test]
2482 fn classify_completed_requires_pr_under_max_reroutes() {
2483 let status = classify_final_status(&DecisionInput {
2484 agent_status: "completed",
2485 requires_pr: true,
2486 no_code_reroutes: 1,
2487 max_reroutes: 3,
2488 is_completed_status: true,
2489 is_same_agent: false,
2490 ..Default::default()
2491 });
2492 assert_eq!(status, "new");
2493 }
2494
2495 #[test]
2497 fn classify_completed_requires_pr_at_max_reroutes_blocks() {
2498 let status = classify_final_status(&DecisionInput {
2499 agent_status: "completed",
2500 requires_pr: true,
2501 no_code_reroutes: 3,
2502 max_reroutes: 3,
2503 is_completed_status: true,
2504 is_same_agent: false,
2505 ..Default::default()
2506 });
2507 assert_eq!(status, "blocked");
2508 }
2509
2510 #[test]
2512 fn classify_completed_same_agent_blocks_immediately() {
2513 let status = classify_final_status(&DecisionInput {
2514 agent_status: "completed",
2515 requires_pr: true,
2516 no_code_reroutes: 1,
2517 max_reroutes: 3,
2518 is_completed_status: true,
2519 is_same_agent: true,
2520 ..Default::default()
2521 });
2522 assert_eq!(status, "blocked");
2523 }
2524
2525 #[test]
2527 fn classify_completed_internal_task_is_done() {
2528 let status = classify_final_status(&DecisionInput {
2529 agent_status: "completed",
2530 requires_pr: false,
2531 is_completed_status: true,
2532 ..Default::default()
2533 });
2534 assert_eq!(status, "done");
2535 }
2536
2537 #[test]
2541 fn agent_blocked_reason_prefers_error_field() {
2542 let resp = AgentResponse {
2543 status: "blocked".to_string(),
2544 error: Some("CDP endpoint not reachable".to_string()),
2545 summary: "Could not complete research".to_string(),
2546 remaining: vec!["Run x-twitter-brave with reachable endpoint".to_string()],
2547 ..Default::default()
2548 };
2549 let reason = agent_blocked_reason(&resp);
2550 assert!(
2551 reason.contains("CDP endpoint not reachable"),
2552 "reason: {reason}"
2553 );
2554 assert!(reason.contains("Remaining:"), "reason: {reason}");
2555 assert!(reason.contains("x-twitter-brave"), "reason: {reason}");
2556 }
2557
2558 #[test]
2560 fn agent_blocked_reason_falls_back_to_summary() {
2561 let resp = AgentResponse {
2562 status: "blocked".to_string(),
2563 error: None,
2564 summary: "Could not access required data sources in sandboxed environment".to_string(),
2565 remaining: vec![],
2566 ..Default::default()
2567 };
2568 let reason = agent_blocked_reason(&resp);
2569 assert_eq!(
2570 reason,
2571 "Could not access required data sources in sandboxed environment"
2572 );
2573 }
2574
2575 #[test]
2577 fn agent_blocked_reason_generic_fallback() {
2578 let resp = AgentResponse {
2579 status: "blocked".to_string(),
2580 ..Default::default()
2581 };
2582 let reason = agent_blocked_reason(&resp);
2583 assert_eq!(reason, "agent returned blocked status without a reason");
2584 }
2585
2586 #[test]
2588 fn classify_agent_blocked_passes_through() {
2589 let status = classify_final_status(&DecisionInput {
2590 agent_status: "blocked",
2591 ..Default::default()
2592 });
2593 assert_eq!(status, "blocked");
2594 }
2595
2596 #[test]
2597 fn classify_retryable_agent_blocked_reroutes() {
2598 let status = classify_final_status(&DecisionInput {
2599 agent_status: "blocked",
2600 is_retryable_blocked: true,
2601 ..Default::default()
2602 });
2603 assert_eq!(status, "new");
2604 }
2605
2606 #[test]
2607 fn retryable_blocked_reason_detects_transient_patterns() {
2608 assert!(is_retryable_blocked_reason(
2609 "Twitter access unavailable: service timed out"
2610 ));
2611 assert!(is_retryable_blocked_reason(
2612 "final commit blocked by git worktree lock permission error"
2613 ));
2614 assert!(is_retryable_blocked_reason(
2615 "could not commit due a worktree git lock permission error"
2616 ));
2617 assert!(is_retryable_blocked_reason(
2618 "git lockfile permission restrictions in this environment"
2619 ));
2620 assert!(is_retryable_blocked_reason(
2621 "API rate limit hit, try again later"
2622 ));
2623 assert!(is_retryable_blocked_reason(
2624 "Credential bean/hyperliquid-address exists in the pass store but GPG decryption fails in this agent session — no passphrase available."
2625 ));
2626 assert!(is_retryable_blocked_reason(
2627 "credential resolution failed via PasswordStoreProvider; AgentVaultProvider unavailable"
2628 ));
2629 }
2630
2631 #[test]
2632 fn retryable_blocked_reason_rejects_permanent_patterns() {
2633 assert!(!is_retryable_blocked_reason(
2634 "invalid request payload: malformed JSON"
2635 ));
2636 assert!(!is_retryable_blocked_reason(
2637 "resource not found in repository"
2638 ));
2639 }
2640}