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, screenshots, 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 Skipped, NoCommits, WorktreeMissing, }
429
430fn is_missing_path_error(err: &anyhow::Error) -> bool {
431 err.chain().any(|cause| {
432 cause
433 .downcast_ref::<io::Error>()
434 .is_some_and(|ioe| ioe.kind() == ErrorKind::NotFound)
435 }) || err
436 .to_string()
437 .to_ascii_lowercase()
438 .contains("no such file or directory")
439}
440
441async fn recover_missing_worktree_for_push(
442 ctx: &StoreCtx<'_>,
443 wt: &worktree::WorktreeSetup,
444 task_title: &str,
445 agent_name: &str,
446 model_name: Option<&str>,
447) -> anyhow::Result<worktree::WorktreeSetup> {
448 tracing::warn!(
449 task_id = ctx.task_id,
450 worktree = %wt.work_dir.display(),
451 branch = wt.branch,
452 "worktree missing during push; attempting one-time recovery"
453 );
454 ctx.append_activity(
455 "push_recover_worktree",
456 Some(agent_name),
457 model_name,
458 Some(&serde_json::json!({
459 "status": "start",
460 "worktree": wt.work_dir.display().to_string(),
461 "branch": wt.branch,
462 })),
463 )
464 .await;
465
466 let recovered = worktree::setup_worktree(
467 ctx.task_id,
468 task_title,
469 &wt.main_project_dir,
470 ctx.store,
471 ctx.repo,
472 )
473 .await?;
474
475 if !recovered.work_dir.exists() {
476 anyhow::bail!(
477 "recovered worktree path is still missing: {}",
478 recovered.work_dir.display()
479 );
480 }
481
482 ctx.append_activity(
483 "push_recover_worktree",
484 Some(agent_name),
485 model_name,
486 Some(&serde_json::json!({
487 "status": "ok",
488 "worktree": recovered.work_dir.display().to_string(),
489 "branch": recovered.branch,
490 })),
491 )
492 .await;
493
494 Ok(recovered)
495}
496
497async fn push_after_rebase(
502 ctx: &StoreCtx<'_>,
503 work_dir: &Path,
504 branch: &str,
505 default_branch: &str,
506 agent_name: &str,
507 model_name: Option<&str>,
508) -> PushResult {
509 match git_ops::push_branch(work_dir, branch, default_branch).await {
510 Ok(true) => {
511 ctx.append_activity(
512 "push",
513 Some(agent_name),
514 model_name,
515 Some(&serde_json::json!({
516 "status": "ok",
517 "branch": branch,
518 "default_branch": default_branch,
519 "reason": "pushed_after_rebase",
520 })),
521 )
522 .await;
523 ctx.set(&[
524 ("last_error", serde_json::json!("")),
525 ("push_failures", serde_json::json!(0)),
526 ])
527 .await;
528 PushResult::Success
529 }
530 Ok(false) => {
531 tracing::info!(
532 task_id = ctx.task_id,
533 branch,
534 default_branch,
535 "push skipped after rebase"
536 );
537 ctx.append_activity(
538 "push",
539 Some(agent_name),
540 model_name,
541 Some(&serde_json::json!({
542 "status": "skipped",
543 "branch": branch,
544 "default_branch": default_branch,
545 "reason": "pushed_after_rebase",
546 })),
547 )
548 .await;
549 PushResult::Skipped
550 }
551 Err(e) => {
552 tracing::error!(
553 task_id = ctx.task_id,
554 branch = branch,
555 error = ?e,
556 "push failed again after rebasing on the remote branch"
557 );
558 ctx.append_activity(
559 "push",
560 Some(agent_name),
561 model_name,
562 Some(&serde_json::json!({
563 "status": "error",
564 "branch": branch,
565 "default_branch": default_branch,
566 "reason": "pushed_after_rebase",
567 "error": e.to_string(),
568 })),
569 )
570 .await;
571 ctx.set(&[(
572 "last_error",
573 serde_json::json!(format!("push failed after rebase on remote branch: {e}")),
574 )])
575 .await;
576 PushResult::Failed
577 }
578 }
579}
580
581async fn push_branch_with_log(
582 ctx: &StoreCtx<'_>,
583 work_dir: &Path,
584 branch: &str,
585 default_branch: &str,
586 agent_name: &str,
587 model_name: Option<&str>,
588) -> PushResult {
589 match git_ops::push_branch(work_dir, branch, default_branch).await {
590 Ok(true) => {
591 ctx.append_activity(
592 "push",
593 Some(agent_name),
594 model_name,
595 Some(&serde_json::json!({
596 "status": "ok",
597 "branch": branch,
598 "default_branch": default_branch,
599 })),
600 )
601 .await;
602 ctx.set(&[
605 ("last_error", serde_json::json!("")),
606 ("push_failures", serde_json::json!(0)),
607 ])
608 .await;
609 PushResult::Success
610 }
611 Ok(false) => {
612 tracing::info!(
613 task_id = ctx.task_id,
614 branch,
615 default_branch,
616 "push skipped"
617 );
618 ctx.append_activity(
619 "push",
620 Some(agent_name),
621 model_name,
622 Some(&serde_json::json!({
623 "status": "skipped",
624 "branch": branch,
625 "default_branch": default_branch,
626 })),
627 )
628 .await;
629 PushResult::Skipped
630 }
631 Err(e) => {
632 let err_str = e.to_string();
633 tracing::error!(task_id = ctx.task_id, error = ?e, "push failed");
634
635 if is_missing_path_error(&e) && !work_dir.exists() {
636 tracing::warn!(
637 task_id = ctx.task_id,
638 worktree = %work_dir.display(),
639 "push failed because worktree path disappeared; will attempt recovery"
640 );
641 ctx.append_activity(
642 "push",
643 Some(agent_name),
644 model_name,
645 Some(&serde_json::json!({
646 "status": "retry",
647 "branch": branch,
648 "default_branch": default_branch,
649 "reason": "worktree_missing",
650 "error": err_str,
651 })),
652 )
653 .await;
654 return PushResult::WorktreeMissing;
655 }
656
657 if push_error_is_diverged(&err_str) {
659 tracing::info!(
660 task_id = ctx.task_id,
661 branch = branch,
662 "attempting fetch and rebase to recover from diverged branch"
663 );
664
665 match git_ops::rebase_on_branch(work_dir, branch).await {
667 Ok(true) => {
668 tracing::info!(
672 task_id = ctx.task_id,
673 branch = branch,
674 "rebase succeeded — retrying the push"
675 );
676 ctx.append_activity(
677 "push_rebased",
678 Some(agent_name),
679 model_name,
680 Some(&serde_json::json!({
681 "status": "rebased",
682 "branch": branch,
683 "reason": "diverged_from_remote",
684 })),
685 )
686 .await;
687 return push_after_rebase(
688 ctx,
689 work_dir,
690 branch,
691 default_branch,
692 agent_name,
693 model_name,
694 )
695 .await;
696 }
697 Ok(false) => {
698 tracing::info!(
700 task_id = ctx.task_id,
701 branch = branch,
702 "no local commits after fetch — marking as success"
703 );
704 ctx.append_activity(
705 "push",
706 Some(agent_name),
707 model_name,
708 Some(&serde_json::json!({
709 "status": "ok",
710 "branch": branch,
711 "reason": "no_commits_after_fetch",
712 })),
713 )
714 .await;
715 ctx.set(&[
716 ("last_error", serde_json::json!("")),
717 ("push_failures", serde_json::json!(0)),
718 ])
719 .await;
720 return PushResult::Success;
721 }
722 Err(rebase_err) if git_ops::is_missing_remote_ref_error(&rebase_err) => {
723 tracing::warn!(
726 task_id = ctx.task_id,
727 branch = branch,
728 "remote branch does not exist, skipping rebase recovery"
729 );
730 ctx.append_activity(
731 "push",
732 Some(agent_name),
733 model_name,
734 Some(&serde_json::json!({
735 "status": "error",
736 "branch": branch,
737 "default_branch": default_branch,
738 "error": err_str,
739 })),
740 )
741 .await;
742 ctx.set(&[("last_error", serde_json::json!(format!("push failed: {e}")))])
743 .await;
744 return PushResult::Failed;
745 }
746 Err(rebase_err) => {
747 tracing::error!(
749 task_id = ctx.task_id,
750 branch = branch,
751 error = ?rebase_err,
752 "rebase failed — blocking task for human intervention"
753 );
754 ctx.append_activity(
755 "push_rebased",
756 Some(agent_name),
757 model_name,
758 Some(&serde_json::json!({
759 "status": "error",
760 "branch": branch,
761 "error": rebase_err.to_string(),
762 })),
763 )
764 .await;
765 ctx.set(&[(
766 "last_error",
767 serde_json::json!(format!(
768 "push failed and rebase failed: {rebase_err} — manual resolution required"
769 )),
770 )])
771 .await;
772 return PushResult::Failed;
773 }
774 }
775 }
776
777 ctx.append_activity(
779 "push",
780 Some(agent_name),
781 model_name,
782 Some(&serde_json::json!({
783 "status": "error",
784 "branch": branch,
785 "default_branch": default_branch,
786 "error": err_str,
787 })),
788 )
789 .await;
790 ctx.set(&[("last_error", serde_json::json!(format!("push failed: {e}")))])
791 .await;
792 PushResult::Failed
793 }
794 }
795}
796
797fn push_error_is_diverged(err_str: &str) -> bool {
800 err_str.contains("non-fast-forward")
801 || err_str.contains("! [rejected]")
802 || err_str.contains("fetch first")
803 || err_str.contains("behind")
804}
805
806fn is_local_rate_limit_skip(err_str: &str) -> bool {
808 err_str.contains("rate-limited, skipping call")
809}
810
811async fn create_pr_with_log(
818 ctx: &StoreCtx<'_>,
819 wt: &worktree::WorktreeSetup,
820 task_title: &str,
821 resp: &AgentResponse,
822 agent_name: &str,
823 model_name: Option<&str>,
824 mut has_pushed: bool,
825) -> (bool, bool, bool) {
826 match git_ops::create_pr_if_needed(
827 &wt.work_dir,
828 &wt.branch,
829 task_title,
830 &resp.summary,
831 &resp.accomplished,
832 &resp.remaining,
833 &resp.files,
834 ctx.task_id,
835 agent_name,
836 model_name,
837 ctx.repo,
838 &wt.default_branch,
839 )
840 .await
841 {
842 Ok(ref url) => {
843 match review::parse_pr_number_from_url(url) {
847 Ok(pr_num) => {
848 if let Err(e) = store::store_set_result(
849 ctx.store,
850 ctx.repo,
851 ctx.task_id,
852 &[("pr_number", serde_json::json!(pr_num))],
853 )
854 .await
855 {
856 tracing::error!(
857 task_id = ctx.task_id,
858 pr_url = %url,
859 err = %e,
860 "CRITICAL: failed to save pr_number to store — downstream review gate may trigger duplicate gh pr create"
861 );
862 }
863 }
864 Err(e) => {
865 tracing::warn!(
866 task_id = ctx.task_id,
867 pr_url = %url,
868 error = %e,
869 "PR created but failed to parse PR number from URL"
870 );
871 }
872 }
873 ctx.append_activity(
874 "pr_create",
875 Some(agent_name),
876 model_name,
877 Some(&serde_json::json!({"status": "created", "url": url})),
878 )
879 .await;
880 (true, has_pushed, false)
881 }
882 Err(e) => {
883 let err_str = format!("{e}");
884 tracing::error!(task_id = ctx.task_id, error = ?e, "create PR failed");
885 ctx.append_activity(
886 "pr_create",
887 Some(agent_name),
888 model_name,
889 Some(&serde_json::json!({
890 "status": "error",
891 "branch": wt.branch,
892 "error": err_str.clone(),
893 })),
894 )
895 .await;
896 ctx.set(&[(
897 "last_error",
898 serde_json::json!(format!("create PR failed: {e}")),
899 )])
900 .await;
901 let mut pr_base_invalid = false;
911 if is_local_rate_limit_skip(&err_str) {
912 tracing::warn!(
915 task_id = ctx.task_id,
916 "PR creation skipped by local GitHub rate limit — review gate will create the PR"
917 );
918 return (true, has_pushed, false);
919 }
920 if agents::patterns::detect_auth_error(&err_str).is_some() {
921 tracing::error!(
922 task_id = ctx.task_id,
923 "PR creation failed with GitHub auth error — blocking task"
924 );
925 pr_base_invalid = true;
926 } else if err_str.contains("422") {
927 if err_str.contains("No commits between") || err_str.contains("head") {
928 tracing::info!(
929 task_id = ctx.task_id,
930 "PR creation returned 422/no-commits/head-invalid — agent made no code changes or branch merged, marking done"
931 );
932 has_pushed = false;
933 } else if git_ops::is_invalid_base_error(&err_str) {
934 tracing::error!(
935 task_id = ctx.task_id,
936 "PR creation failed with invalid base branch — blocking task"
937 );
938 pr_base_invalid = true;
939 }
940 }
941 (false, has_pushed, pr_base_invalid)
942 }
943 }
944}
945
946async fn run_git_ops(
948 ctx: &StoreCtx<'_>,
949 wt: &worktree::WorktreeSetup,
950 task_title: &str,
951 resp: &AgentResponse,
952 agent_name: &str,
953 model_name: Option<&str>,
954 new_attempts: u32,
955) -> GitOpsResult {
956 auto_commit_changes(ctx, &wt.work_dir, task_title, agent_name, new_attempts).await;
957
958 let mut has_commits =
959 check_commits_and_clear_stale_errors(ctx, &wt.work_dir, &wt.default_branch).await;
960
961 let push_result = if has_commits {
965 push_branch_with_log(
966 ctx,
967 &wt.work_dir,
968 &wt.branch,
969 &wt.default_branch,
970 agent_name,
971 model_name,
972 )
973 .await
974 } else {
975 store::store_log_activity(
976 ctx.store,
977 ctx.repo,
978 ctx.task_id,
979 "push",
980 None,
981 None,
982 Some(agent_name),
983 model_name,
984 Some(&serde_json::json!({
985 "status": "skipped",
986 "reason": "no_commits_ahead",
987 "branch": wt.branch,
988 "default_branch": wt.default_branch,
989 })),
990 )
991 .await;
992 PushResult::NoCommits
993 };
994 let push_result = if matches!(push_result, PushResult::WorktreeMissing) {
995 match recover_missing_worktree_for_push(ctx, wt, task_title, agent_name, model_name).await {
996 Ok(recovered) => {
997 let wt_for_ops = &recovered;
998 has_commits = check_commits_and_clear_stale_errors(
999 ctx,
1000 &wt_for_ops.work_dir,
1001 &wt_for_ops.default_branch,
1002 )
1003 .await;
1004 if has_commits {
1005 push_branch_with_log(
1006 ctx,
1007 &wt_for_ops.work_dir,
1008 &wt_for_ops.branch,
1009 &wt_for_ops.default_branch,
1010 agent_name,
1011 model_name,
1012 )
1013 .await
1014 } else {
1015 PushResult::NoCommits
1016 }
1017 }
1018 Err(e) => {
1019 tracing::error!(
1020 task_id = ctx.task_id,
1021 err = %e,
1022 "worktree recovery before push failed"
1023 );
1024 ctx.append_activity(
1025 "push_recover_worktree",
1026 Some(agent_name),
1027 model_name,
1028 Some(&serde_json::json!({
1029 "status": "error",
1030 "worktree": wt.work_dir.display().to_string(),
1031 "branch": wt.branch,
1032 "error": e.to_string(),
1033 })),
1034 )
1035 .await;
1036 ctx.set(&[(
1037 "last_error",
1038 serde_json::json!(format!(
1039 "push failed: worktree disappeared and recovery failed: {e}"
1040 )),
1041 )])
1042 .await;
1043 PushResult::Failed
1044 }
1045 }
1046 } else {
1047 push_result
1048 };
1049
1050 let mut has_pushed = matches!(push_result, PushResult::Success);
1051 let mut has_pr = false;
1052
1053 if has_commits && matches!(push_result, PushResult::Failed) {
1055 tracing::warn!(
1056 task_id = ctx.task_id,
1057 "skipping PR creation due to push failure"
1058 );
1059 } else if matches!(push_result, PushResult::Skipped) {
1060 tracing::info!(task_id = ctx.task_id, "push skipped, skipping PR creation");
1061 } else if matches!(push_result, PushResult::NoCommits) {
1062 tracing::info!(
1063 task_id = ctx.task_id,
1064 "no commits ahead of default branch, skipping push + PR"
1065 );
1066 } else if !has_pushed {
1067 } else if ctx.repo.is_empty() {
1069 tracing::warn!(
1070 task_id = ctx.task_id,
1071 "skipping PR creation — repo is empty (internal task?)"
1072 );
1073 } else {
1074 let (pr, updated_pushed, pr_base_invalid) = create_pr_with_log(
1075 ctx, wt, task_title, resp, agent_name, model_name, has_pushed,
1076 )
1077 .await;
1078 has_pr = pr;
1079 has_pushed = updated_pushed;
1080 return GitOpsResult {
1081 has_pr,
1082 has_pushed,
1083 has_commits,
1084 pr_base_invalid,
1085 };
1086 }
1087
1088 GitOpsResult {
1089 has_pr,
1090 has_pushed,
1091 has_commits,
1092 pr_base_invalid: false,
1093 }
1094}
1095
1096async fn post_screenshots(
1100 ctx: &StoreCtx<'_>,
1101 work_dir: &Path,
1102 resp: &AgentResponse,
1103) -> Option<String> {
1104 let note = resp.screenshot_note.as_deref().map(str::trim);
1105 let note = note.filter(|n| !n.is_empty());
1106 if resp.screenshots.is_empty() && note.is_none() {
1107 return None;
1108 }
1109 let pr_number = ctx.get_task().await.and_then(|t| t.pr_number);
1110 let (Some(pr_number), false) = (pr_number, ctx.repo.is_empty()) else {
1111 return note.map(String::from);
1112 };
1113 let Ok(pr) = u64::try_from(pr_number) else {
1114 return note.map(String::from);
1115 };
1116
1117 let roots = screenshots::allowed_roots(work_dir).await;
1118 let (images, skipped) = screenshots::prepare(&resp.screenshots, work_dir, &roots, pr);
1119 let commit = if images.is_empty() {
1120 String::new()
1121 } else {
1122 match screenshots::publish(work_dir, &images, pr).await {
1123 Ok(commit) => commit,
1124 Err(e) => {
1125 tracing::warn!(task_id = ctx.task_id, err = %e, "failed to push screenshots");
1126 let body = screenshots::build_unpublished_comment(note, &skipped);
1127 comment_screenshots(ctx, pr, &body).await;
1128 return note.map(String::from);
1129 }
1130 }
1131 };
1132 let body = screenshots::build_comment(ctx.repo, &commit, &images, note, &skipped);
1133 comment_screenshots(ctx, pr, &body).await;
1134 note.map(String::from)
1135}
1136
1137async fn comment_screenshots(ctx: &StoreCtx<'_>, pr: u64, body: &str) {
1138 match GhHttp::new() {
1139 Ok(gh) => {
1140 if let Err(e) = gh.add_comment(ctx.repo, &pr.to_string(), body).await {
1141 tracing::warn!(task_id = ctx.task_id, pr, err = %e, "failed to comment screenshots");
1142 }
1143 }
1144 Err(e) => tracing::warn!(task_id = ctx.task_id, err = %e, "cannot comment screenshots"),
1145 }
1146}
1147
1148async fn close_superseded_pr(ctx: &StoreCtx<'_>, pr_number: u64) {
1150 if ctx.repo.is_empty() {
1151 return;
1152 }
1153 match GhHttp::new() {
1154 Ok(gh) => {
1155 let note = "Closing as superseded: the change is already on the default branch, so this branch has no commits ahead of it.";
1156 if let Err(e) = gh.add_comment(ctx.repo, &pr_number.to_string(), note).await {
1157 tracing::warn!(task_id = ctx.task_id, pr_number, err = %e, "failed to comment on superseded PR");
1158 }
1159 if let Err(e) = gh.close_pr(ctx.repo, pr_number).await {
1160 tracing::warn!(task_id = ctx.task_id, pr_number, err = %e, "failed to close superseded PR");
1161 }
1162 }
1163 Err(e) => {
1164 tracing::warn!(task_id = ctx.task_id, err = %e, "cannot close superseded PR");
1165 }
1166 }
1167 ctx.set(&[
1169 (
1170 "last_error",
1171 serde_json::json!(
1172 "PR closed as superseded: the fix already landed on the default branch"
1173 ),
1174 ),
1175 ("pr_number", serde_json::json!(null)),
1176 ])
1177 .await;
1178}
1179
1180fn superseded_conflict_pr(
1186 merge_conflict_retries: i32,
1187 pr_number: Option<i32>,
1188 has_commits: bool,
1189) -> Option<u64> {
1190 if has_commits || merge_conflict_retries <= 0 {
1191 return None;
1192 }
1193 pr_number
1194 .filter(|n| *n > 0)
1195 .and_then(|n| u64::try_from(n).ok())
1196}
1197
1198async fn detect_push_failure_state(
1200 ctx: &StoreCtx<'_>,
1201 has_commits: bool,
1202 has_pushed: bool,
1203) -> PushFailureState {
1204 let stored_last_error = ctx
1206 .get_task()
1207 .await
1208 .map(|t| t.last_error)
1209 .unwrap_or_default();
1210
1211 let push_failed = !has_pushed && has_commits && stored_last_error.contains("push failed");
1212
1213 let is_workflow_scope_failure = push_failed
1216 && (stored_last_error.contains("workflow` scope")
1217 || stored_last_error.contains("workflow' scope")
1218 || (stored_last_error.contains("refusing to allow")
1219 && stored_last_error.contains("workflow")));
1220
1221 let is_rebase_conflict_failure =
1222 push_failed && stored_last_error.contains("push failed and rebase failed:");
1223
1224 let is_transient_failure = push_failed
1225 && !is_workflow_scope_failure
1226 && !is_rebase_conflict_failure
1227 && git_ops::is_transient_network_error(&stored_last_error);
1228
1229 let push_failures = if push_failed && !is_workflow_scope_failure && !is_rebase_conflict_failure
1234 {
1235 match ctx.increment("push_failures").await {
1236 Ok(v) => v,
1237 Err(e) => {
1238 tracing::warn!(
1239 task_id = ctx.task_id,
1240 err = %e,
1241 "failed to increment push_failures — reading current value from store"
1242 );
1243 ctx.get_task()
1245 .await
1246 .map_or(1, |t| u64::try_from(t.push_failures).unwrap_or(0) + 1)
1247 }
1249 }
1250 } else {
1251 0
1252 };
1253
1254 PushFailureState {
1255 push_failed,
1256 is_workflow_scope_failure,
1257 is_rebase_conflict_failure,
1258 is_transient_failure,
1259 push_failures,
1260 stored_last_error,
1261 }
1262}
1263
1264async fn detect_no_code_reroutes(
1267 ctx: &StoreCtx<'_>,
1268 is_no_code_reroute: bool,
1269 new_attempts: u32,
1270 max_reroutes: u32,
1271) -> u64 {
1272 if !is_no_code_reroute {
1273 return 0;
1274 }
1275 tracing::warn!(
1276 task_id = ctx.task_id,
1277 attempts = new_attempts,
1278 max_reroutes,
1279 "agent reported done but produced no code changes on external task requiring PR"
1280 );
1281 match ctx.increment("no_code_reroutes").await {
1282 Ok(v) => v,
1283 Err(e) => {
1284 tracing::warn!(
1285 task_id = ctx.task_id,
1286 err = %e,
1287 "failed to increment no_code_reroutes — reading current value from store"
1288 );
1289 ctx.get_task()
1291 .await
1292 .map_or(1, |t| u64::try_from(t.no_code_reroutes).unwrap_or(0) + 1)
1293 }
1295 }
1296}
1297
1298fn agent_blocked_reason(resp: &AgentResponse) -> String {
1305 let summary_opt = if resp.summary.is_empty() {
1306 None
1307 } else {
1308 Some(resp.summary.as_str())
1309 };
1310 let base = resp
1311 .error
1312 .as_deref()
1313 .filter(|s| !s.is_empty())
1314 .or(summary_opt)
1315 .unwrap_or("agent returned blocked status without a reason");
1316 if resp.remaining.is_empty() {
1317 base.to_string()
1318 } else {
1319 format!("{base}. Remaining: {}", resp.remaining.join("; "))
1320 }
1321}
1322
1323fn is_denied_push_block(resp: &AgentResponse) -> bool {
1325 let r = agent_blocked_reason(resp).to_lowercase();
1326 r.contains("push")
1327 && [
1328 "denied",
1329 "not allowed",
1330 "permission",
1331 "disallowed",
1332 "approval",
1333 ]
1334 .iter()
1335 .any(|p| r.contains(p))
1336}
1337
1338fn is_retryable_blocked_reason(reason: &str) -> bool {
1344 let r = reason.to_lowercase();
1345
1346 let permanent_patterns = [
1347 "not found",
1348 "does not exist",
1349 "invalid",
1350 "malformed",
1351 "syntax error",
1352 "unsupported",
1353 "unimplemented",
1354 "manual intervention required",
1355 "human intervention required",
1356 ];
1357 if permanent_patterns.iter().any(|p| r.contains(p)) {
1358 return false;
1359 }
1360
1361 let transient_patterns = [
1362 "unavailable",
1363 "temporarily unavailable",
1364 "timeout",
1365 "timed out",
1366 "connection reset",
1367 "connection refused",
1368 "network error",
1369 "rate limit",
1370 "try again",
1371 "service down",
1372 "permission denied",
1373 "worktree lock",
1374 "git lock",
1375 "lockfile permission",
1376 "lock permission",
1377 "resource busy",
1378 "could not resolve host",
1379 "gpg decryption fails",
1382 "gpg decryption failed",
1383 "gpg agent",
1384 "no passphrase available",
1385 "passphrase unavailable",
1386 "pass store",
1387 "passwordstoreprovider",
1388 "agentvaultprovider",
1389 "onepasswordprovider",
1390 "decryption failed in this agent session",
1391 ];
1392 transient_patterns.iter().any(|p| r.contains(p))
1393}
1394
1395#[expect(
1397 clippy::too_many_arguments,
1398 reason = "the decision and its context arrive as separate values"
1399)]
1400async fn apply_post_decision_effects(
1401 ctx: &StoreCtx<'_>,
1402 final_status: &str,
1403 resp_status: &str,
1404 push_state: &PushFailureState,
1405 is_no_code_reroute: bool,
1406 has_pr: bool,
1407 has_pushed: bool,
1408 is_pr_base_invalid: bool,
1409 has_delegations: bool,
1410 is_external: bool,
1411 no_code_reroutes: u64,
1412 max_reroutes: u32,
1413 agent_name: &str,
1414) {
1415 if push_state.is_workflow_scope_failure {
1416 tracing::error!(
1418 task_id = ctx.task_id,
1419 "push failed: token lacks `workflow` OAuth scope — blocking immediately \
1420 (rerouting would not help)"
1421 );
1422 ctx.set(&[(
1423 "last_error",
1424 serde_json::json!(format!(
1425 "push failed: GitHub token lacks `workflow` OAuth scope. \
1426 The agent modified .github/workflows/ files but the token cannot push them. \
1427 Fix: add `workflow` scope to your GitHub token, or use a GitHub App for auth. \
1428 Original error: {}",
1429 push_state.stored_last_error
1430 )),
1431 )])
1432 .await;
1433 } else if push_state.is_rebase_conflict_failure {
1434 tracing::error!(
1435 task_id = ctx.task_id,
1436 "push failed and automatic rebase recovery failed — blocking for human intervention"
1437 );
1438 ctx.set(&[
1439 ("agent", serde_json::json!(null)),
1440 ("model", serde_json::json!(null)),
1441 ])
1442 .await;
1443 } else if push_state.push_failed && final_status == "needs_review" {
1444 tracing::warn!(
1447 task_id = ctx.task_id,
1448 push_failures = push_state.push_failures,
1449 "agent done but push failed on a GitHub or network error — engine retries the push"
1450 );
1451 } else if push_state.push_failed {
1452 ctx.set(&[
1454 ("agent", serde_json::json!(null)),
1455 ("model", serde_json::json!(null)),
1456 ])
1457 .await;
1458 let push_failures = push_state.push_failures;
1459 if push_failures >= 3 {
1460 tracing::error!(
1461 task_id = ctx.task_id,
1462 push_failures,
1463 "push failed {push_failures} times — blocking for human intervention"
1464 );
1465 } else {
1466 tracing::warn!(
1467 task_id = ctx.task_id,
1468 push_failures,
1469 "agent done but push failed ({push_failures}/3) — rerouting to different agent"
1470 );
1471 }
1472 } else if resp_status == "done" && !has_pr && has_delegations {
1473 tracing::info!(
1474 task_id = ctx.task_id,
1475 "agent reported done with delegations but no PR — setting blocked"
1476 );
1477 } else if (resp_status == "done" || resp_status == "completed")
1478 && !has_pr
1479 && has_pushed
1480 && is_pr_base_invalid
1481 {
1482 tracing::error!(
1483 task_id = ctx.task_id,
1484 "PR creation failed with invalid base branch — blocking for human intervention"
1485 );
1486 ctx.set(&[(
1487 "last_error",
1488 serde_json::json!(
1489 "PR creation failed terminally (invalid base branch or GitHub auth error). The agent's commits were pushed successfully. \
1490 Check the repository default branch and the GitHub token, then create the PR manually or unblock the task."
1491 ),
1492 )])
1493 .await;
1494 } else if resp_status == "done" && !has_pr && has_pushed {
1495 tracing::warn!(
1497 task_id = ctx.task_id,
1498 "agent done, commits pushed, but PR creation failed — re-dispatching as routed"
1499 );
1500 } else if is_no_code_reroute {
1501 if final_status == "blocked" {
1506 let prev_no_code_agent = ctx
1510 .get_task()
1511 .await
1512 .map(|t| t.no_code_last_agent)
1513 .unwrap_or_default();
1514 let is_same_agent = !prev_no_code_agent.is_empty() && prev_no_code_agent == agent_name;
1515
1516 let msg = if is_same_agent {
1517 tracing::error!(
1518 task_id = ctx.task_id,
1519 agent = %agent_name,
1520 "blocking same-agent no-code loop: agent {} would be selected again",
1521 agent_name
1522 );
1523 format!(
1524 "agent {agent_name} completed without code changes twice — same-agent loop detected, blocking for human review"
1525 )
1526 } else {
1527 tracing::error!(
1528 task_id = ctx.task_id,
1529 no_code_reroutes,
1530 max_reroutes,
1531 "blocking no-code reroute: reached max reroute attempts ({}/{}) for no-code-result",
1532 no_code_reroutes,
1533 max_reroutes
1534 );
1535 format!(
1536 "agent completed without code changes after {no_code_reroutes}/{max_reroutes} reroute attempts on external task requiring PR"
1537 )
1538 };
1539 ctx.set(&[("last_error", serde_json::json!(msg))]).await;
1540 } else {
1541 ctx.set(&[(
1543 "last_error",
1544 serde_json::json!(
1545 "agent completed without code changes on external task requiring PR"
1546 ),
1547 )])
1548 .await;
1549 }
1550
1551 ctx.set(&[
1556 ("no_code_last_agent", serde_json::json!(agent_name)),
1557 ("agent", serde_json::json!(null)),
1558 ("model", serde_json::json!(null)),
1559 ])
1560 .await;
1561 } else if resp_status == "done" && !has_pr && is_external {
1562 tracing::info!(
1565 task_id = ctx.task_id,
1566 "external task with non-code labels reported done — marking done without PR"
1567 );
1568 } else if resp_status == "done" && !has_pr {
1569 tracing::info!(
1570 task_id = ctx.task_id,
1571 "internal task reported done with no PR — marking done"
1572 );
1573 }
1574}
1575
1576async fn store_token_usage(
1579 ctx: &StoreCtx<'_>,
1580 input_tokens: Option<u64>,
1581 output_tokens: Option<u64>,
1582 cost_usd: Option<f64>,
1583) {
1584 let (Some(input), Some(output)) = (input_tokens, output_tokens) else {
1585 return;
1586 };
1587 if let Some(st) = ctx.store {
1588 if let Some(store_id) = ctx.store_id_opt {
1589 if let Err(e) = st.store_tokens(store_id, input, output, cost_usd).await {
1590 tracing::warn!(task_id = ctx.task_id, ?e, "failed to store token usage");
1591 }
1592 } else if let Ok(Some(store_id)) = st.resolve_task_id(ctx.repo, ctx.task_id).await
1593 && let Err(e) = st.store_tokens(store_id, input, output, cost_usd).await
1594 {
1595 tracing::warn!(task_id = ctx.task_id, ?e, "failed to store token usage");
1596 }
1597 }
1598}
1599
1600#[expect(
1606 clippy::too_many_arguments,
1607 reason = "the runner passes each part of the run result"
1608)]
1609pub async fn handle_success(
1610 task_id: &str,
1611 parsed: agents::ParsedResponse,
1612 wt: &worktree::WorktreeSetup,
1613 task_title: &str,
1614 agent_name: &str,
1615 model_name: Option<&str>,
1616 new_attempts: u32,
1617 repo: &str,
1618 store: &Option<Arc<TaskStore>>,
1619 raw_stdout: &str,
1620) -> anyhow::Result<(String, bool)> {
1621 let input_tokens = parsed.input_tokens;
1623 let output_tokens = parsed.output_tokens;
1624 let cost_usd = parsed.cost_usd;
1625 let mut resp = parsed.response;
1626
1627 tracing::info!(
1628 task_id,
1629 status = resp.status,
1630 summary = resp.summary,
1631 "agent completed successfully"
1632 );
1633
1634 let push_denied = resp.status == "blocked" && is_denied_push_block(&resp);
1635
1636 let store_id_opt = store::resolve_store_id(store, repo, task_id).await;
1639 let ctx = StoreCtx {
1640 store,
1641 store_id_opt,
1642 repo,
1643 task_id,
1644 };
1645
1646 ctx.reset_retry_streaks().await;
1647
1648 let git = if resp.status == "done"
1650 || resp.status == "completed"
1651 || resp.status == "in_progress"
1652 || resp.status == "needs_review"
1653 || push_denied
1654 {
1655 run_git_ops(
1656 &ctx,
1657 wt,
1658 task_title,
1659 &resp,
1660 agent_name,
1661 model_name,
1662 new_attempts,
1663 )
1664 .await
1665 } else {
1666 GitOpsResult::default()
1667 };
1668
1669 if push_denied && (git.has_pr || git.has_pushed) {
1672 tracing::info!(
1673 task_id,
1674 "agent blocked on denied push, orch pushed the branch"
1675 );
1676 resp.status = "done".to_string();
1677 }
1678
1679 let screenshot_note = if git.has_pr {
1680 post_screenshots(&ctx, &wt.work_dir, &resp).await
1681 } else {
1682 None
1683 };
1684
1685 if !resp.delegations.is_empty() {
1687 ctx.set(&[("delegations", serde_json::json!(resp.delegations))])
1688 .await;
1689 }
1690
1691 let has_delegations = !resp.delegations.is_empty();
1698
1699 let push_state = detect_push_failure_state(&ctx, git.has_commits, git.has_pushed).await;
1701
1702 let is_external = !task_id.starts_with("internal:");
1710 let requires_pr = is_external && !git.has_pr && !resp.status.starts_with("needs_review");
1711
1712 let max_reroutes: u32 = config::get("workflow.max_reroute_attempts")
1715 .ok()
1716 .and_then(|s| s.parse().ok())
1717 .or_else(|| {
1718 config::get("workflow.max_attempts")
1719 .ok()
1720 .and_then(|s| s.parse().ok())
1721 })
1722 .unwrap_or(3);
1723
1724 let is_no_code_reroute = !push_state.is_workflow_scope_failure
1727 && !push_state.push_failed
1728 && (resp.status == "done" || resp.status == "completed")
1729 && !has_delegations
1730 && !git.has_pushed
1731 && requires_pr;
1732
1733 let superseded_pr = match (is_no_code_reroute, ctx.get_task().await) {
1736 (true, Some(task)) => {
1737 superseded_conflict_pr(task.merge_conflict_retries, task.pr_number, git.has_commits)
1738 }
1739 _ => None,
1740 };
1741 let is_no_code_reroute = is_no_code_reroute && superseded_pr.is_none();
1742
1743 let no_code_reroutes =
1744 detect_no_code_reroutes(&ctx, is_no_code_reroute, new_attempts, max_reroutes).await;
1745
1746 let blocked_reason = if resp.status == "blocked" {
1747 Some(agent_blocked_reason(&resp))
1748 } else {
1749 None
1750 };
1751 let is_retryable_blocked = blocked_reason
1752 .as_deref()
1753 .is_some_and(is_retryable_blocked_reason);
1754
1755 let is_same_agent = if is_no_code_reroute {
1760 let prev_no_code_agent = ctx
1761 .get_task()
1762 .await
1763 .map(|t| t.no_code_last_agent)
1764 .unwrap_or_default();
1765 !prev_no_code_agent.is_empty() && prev_no_code_agent == agent_name
1766 } else {
1767 false
1768 };
1769
1770 let final_status_owned = classify_final_status(&DecisionInput {
1773 agent_status: &resp.status,
1774 is_workflow_scope_failure: push_state.is_workflow_scope_failure,
1775 is_rebase_conflict_failure: push_state.is_rebase_conflict_failure,
1776 push_failed: push_state.push_failed,
1777 push_failures: push_state.push_failures,
1778 is_transient_push_failure: push_state.is_transient_failure,
1779 has_pr: git.has_pr,
1780 has_delegations,
1781 has_pushed: git.has_pushed,
1782 is_pr_base_invalid: git.pr_base_invalid,
1783 requires_pr,
1784 no_code_reroutes,
1785 max_reroutes,
1786 is_same_agent,
1787 is_completed_status: resp.status == "completed",
1788 is_retryable_blocked,
1789 is_superseded_conflict: superseded_pr.is_some(),
1790 });
1791 let final_status = final_status_owned.as_str();
1792
1793 apply_post_decision_effects(
1796 &ctx,
1797 final_status,
1798 &resp.status,
1799 &push_state,
1800 is_no_code_reroute,
1801 git.has_pr,
1802 git.has_pushed,
1803 git.pr_base_invalid,
1804 has_delegations,
1805 is_external,
1806 no_code_reroutes,
1807 max_reroutes,
1808 agent_name,
1809 )
1810 .await;
1811
1812 if resp.status == "blocked" {
1818 let reason = blocked_reason.unwrap_or_else(|| agent_blocked_reason(&resp));
1819 if final_status == "new" {
1820 cooldown::record_agent_failure_with_message(agent_name, &reason).await;
1823 if let Some(m) = model_name {
1824 cooldown::record_model_failure(agent_name, m).await;
1825 }
1826 ctx.set(&[
1827 (
1828 "last_error",
1829 serde_json::json!(format!(
1830 "transient blocker (auto-retry scheduled): {reason}"
1831 )),
1832 ),
1833 ("agent", serde_json::json!(null)),
1834 ("model", serde_json::json!(null)),
1835 ])
1836 .await;
1837 tracing::warn!(
1838 task_id = ctx.task_id,
1839 reason = %reason,
1840 "agent returned retryable blocked status — rerouting as new with cooldown"
1841 );
1842 } else if final_status == "blocked" {
1843 ctx.set(&[("last_error", serde_json::json!(reason))]).await;
1844 }
1845 }
1846
1847 if let Some(pr_number) = superseded_pr {
1848 tracing::info!(
1849 task_id = ctx.task_id,
1850 pr_number,
1851 "conflict reroute found the fix already on the default branch — closing superseded PR"
1852 );
1853 close_superseded_pr(&ctx, pr_number).await;
1854 }
1855
1856 let summary = match &screenshot_note {
1858 Some(note) => format!("{}\n\nScreenshots: {note}", resp.summary),
1859 None => resp.summary.clone(),
1860 };
1861 ctx.set(&[("summary", serde_json::json!(summary))]).await;
1862
1863 store_token_usage(
1865 &ctx,
1866 input_tokens.or(resp.input_tokens),
1867 output_tokens.or(resp.output_tokens),
1868 cost_usd,
1869 )
1870 .await;
1871
1872 response::store_learnings_from_response(
1874 task_id,
1875 new_attempts,
1876 agent_name,
1877 model_name,
1878 &resp,
1879 resp.error.as_deref(),
1880 store,
1881 repo,
1882 raw_stdout,
1883 )
1884 .await;
1885
1886 Ok((final_status.to_string(), push_state.push_failed))
1889}
1890
1891#[cfg(test)]
1893mod tests {
1894 use std::fs as std_fs;
1895 use std::process::Command;
1896
1897 use super::*;
1898 use crate::store::NewTask;
1899
1900 #[tokio::test]
1902 async fn successful_run_resets_timeout_retries() {
1903 let store = Some(Arc::new(TaskStore::open_memory().await.unwrap()));
1904 let id = store
1905 .as_ref()
1906 .unwrap()
1907 .create_internal("owner/repo", "t", "b", "test", "", None)
1908 .await
1909 .unwrap();
1910 let task_id = format!("internal:{id}");
1911 let ctx = StoreCtx {
1912 store: &store,
1913 store_id_opt: None,
1914 repo: "owner/repo",
1915 task_id: &task_id,
1916 };
1917
1918 ctx.increment("timeout_retries").await.unwrap();
1919 ctx.increment("network_retries").await.unwrap();
1920 ctx.reset_retry_streaks().await;
1921 ctx.increment("timeout_retries").await.unwrap();
1922 ctx.increment("timeout_retries").await.unwrap();
1923
1924 let task = ctx.get_task().await.unwrap();
1925 assert_eq!(task.timeout_retries, 2);
1926 assert_eq!(task.network_retries, 0);
1927 assert_ne!(task.status.as_str(), "blocked");
1928 }
1929
1930 #[test]
1931 fn local_rate_limit_skip_is_detected() {
1932 assert!(is_local_rate_limit_skip(
1933 "GitHub API error: GitHub REST API rate-limited, skipping call for 236s"
1934 ));
1935 assert!(!is_local_rate_limit_skip(
1936 "GitHub API POST failed (502): Bad Gateway"
1937 ));
1938 }
1939
1940 fn git(dir: &Path, args: &[&str]) {
1942 let out = Command::new("git")
1943 .current_dir(dir)
1944 .args(args)
1945 .output()
1946 .unwrap_or_else(|e| panic!("git {args:?} failed to start: {e}"));
1947 assert!(
1948 out.status.success(),
1949 "git {args:?} failed:\nstdout: {}\nstderr: {}",
1950 String::from_utf8_lossy(&out.stdout),
1951 String::from_utf8_lossy(&out.stderr)
1952 );
1953 }
1954
1955 fn init_clone(remote: &Path, dir: &Path) {
1956 git(dir, &["clone", remote.to_str().unwrap(), "."]);
1957 git(dir, &["config", "user.email", "t@t.com"]);
1958 git(dir, &["config", "user.name", "T"]);
1959 git(dir, &["config", "commit.gpgsign", "false"]);
1960 }
1961
1962 #[test]
1963 fn remote_rejected_failure_is_not_diverged() {
1964 let err = "remote: fatal error in commit_refs\n ! [remote rejected] b -> b (failure)";
1965 assert!(!push_error_is_diverged(err));
1966 assert!(push_error_is_diverged("! [rejected] b -> b (fetch first)"));
1967 assert!(push_error_is_diverged(
1968 "! [rejected] b -> b (non-fast-forward)"
1969 ));
1970 }
1971
1972 #[tokio::test]
1979 async fn push_rejected_after_remote_rebase_is_pushed_not_rerouted() {
1980 let remote = tempfile::tempdir().unwrap();
1981 git(remote.path(), &["init", "--bare", "-b", "main"]);
1982
1983 let work = tempfile::tempdir().unwrap();
1985 init_clone(remote.path(), work.path());
1986 std_fs::write(work.path().join("README.md"), "base").unwrap();
1987 git(work.path(), &["add", "."]);
1988 git(work.path(), &["commit", "-m", "init"]);
1989 git(work.path(), &["push", "origin", "HEAD:main"]);
1990 git(work.path(), &["checkout", "-b", "feat"]);
1991 std_fs::write(work.path().join("fix.txt"), "the fix").unwrap();
1992 git(work.path(), &["add", "."]);
1993 git(work.path(), &["commit", "-m", "apply the fix"]);
1994 git(work.path(), &["push", "-u", "origin", "feat"]);
1995
1996 let other = tempfile::tempdir().unwrap();
1998 init_clone(remote.path(), other.path());
1999 git(other.path(), &["checkout", "feat"]);
2000 git(
2001 other.path(),
2002 &["commit", "--amend", "-m", "apply the fix (rebased)"],
2003 );
2004 git(other.path(), &["push", "--force", "origin", "feat"]);
2005
2006 std_fs::write(work.path().join("more.txt"), "follow-up").unwrap();
2008 git(work.path(), &["add", "."]);
2009 git(work.path(), &["commit", "-m", "follow-up"]);
2010
2011 let store = Arc::new(TaskStore::open_memory().await.unwrap());
2012 let store_id = store
2013 .create_internal("owner/repo", "Push recovery", "", "task", "3783", None)
2014 .await
2015 .unwrap();
2016 let task_id = format!("internal:{store_id}");
2017 let store_opt = Some(Arc::clone(&store));
2018 let ctx = StoreCtx {
2019 store: &store_opt,
2020 store_id_opt: Some(store_id),
2021 repo: "owner/repo",
2022 task_id: &task_id,
2023 };
2024
2025 let result =
2026 push_branch_with_log(&ctx, work.path(), "feat", "main", "claude", Some("opus")).await;
2027
2028 assert!(
2029 matches!(result, PushResult::Success),
2030 "orch must land the push itself after adopting the moved remote branch"
2031 );
2032
2033 let task = store.get(store_id).await.unwrap();
2034 assert!(
2035 task.last_error.is_empty(),
2036 "a recovered push must leave no error behind, got: {}",
2037 task.last_error
2038 );
2039 assert_eq!(task.push_failures, 0, "recovery is not a push failure");
2040
2041 let log = Command::new("git")
2044 .current_dir(work.path())
2045 .args(["log", "--oneline", "origin/feat"])
2046 .output()
2047 .unwrap();
2048 let log = String::from_utf8_lossy(&log.stdout);
2049 assert!(
2050 log.contains("follow-up"),
2051 "remote must carry the task's commit: {log}"
2052 );
2053 assert!(
2054 log.contains("apply the fix (rebased)"),
2055 "orch must adopt the rebased remote history, not re-push its own: {log}"
2056 );
2057 assert!(
2058 !log.contains("apply the fix\n"),
2059 "the superseded commit must not come back: {log}"
2060 );
2061 }
2062
2063 #[test]
2064 fn missing_path_error_detects_io_not_found() {
2065 let err = anyhow::Error::from(io::Error::new(
2066 ErrorKind::NotFound,
2067 "simulated missing path",
2068 ));
2069 assert!(is_missing_path_error(&err));
2070 }
2071
2072 #[test]
2073 fn missing_path_error_detects_message_fallback() {
2074 let err = anyhow::anyhow!("push failed: No such file or directory (os error 2)");
2075 assert!(is_missing_path_error(&err));
2076 }
2077
2078 #[test]
2079 fn superseded_conflict_pr_requires_conflict_reroute_and_no_commits() {
2080 assert_eq!(superseded_conflict_pr(1, Some(42), false), Some(42));
2082 assert_eq!(superseded_conflict_pr(1, Some(42), true), None);
2084 assert_eq!(superseded_conflict_pr(0, Some(42), false), None);
2086 assert_eq!(superseded_conflict_pr(1, None, false), None);
2088 }
2089
2090 #[tokio::test]
2094 async fn conflict_reroute_with_no_commits_closes_pr_and_marks_done() {
2095 fn git(dir: &Path, args: &[&str]) {
2096 let ok = Command::new("git")
2097 .args(args)
2098 .current_dir(dir)
2099 .env("GIT_AUTHOR_NAME", "t")
2100 .env("GIT_AUTHOR_EMAIL", "t@example.com")
2101 .env("GIT_COMMITTER_NAME", "t")
2102 .env("GIT_COMMITTER_EMAIL", "t@example.com")
2103 .output()
2104 .expect("git runs")
2105 .status
2106 .success();
2107 assert!(ok, "git {args:?} failed");
2108 }
2109
2110 let dir = tempfile::tempdir().unwrap();
2113 let origin = dir.path().join("origin.git");
2114 std_fs::create_dir(&origin).unwrap();
2115 git(&origin, &["init", "--bare", "-b", "main"]);
2116 let work_dir = dir.path().join("work");
2117 std_fs::create_dir(&work_dir).unwrap();
2118 git(&work_dir, &["init", "-b", "main"]);
2119 git(
2120 &work_dir,
2121 &["remote", "add", "origin", &origin.display().to_string()],
2122 );
2123 std_fs::write(work_dir.join("a.txt"), "a").unwrap();
2124 git(&work_dir, &["add", "-A"]);
2125 git(&work_dir, &["commit", "-m", "init"]);
2126 git(&work_dir, &["push", "-u", "origin", "main"]);
2127 git(&work_dir, &["checkout", "-b", "fix-branch"]);
2128
2129 let wt = worktree::WorktreeSetup {
2130 work_dir: work_dir.clone(),
2131 branch: "fix-branch".to_string(),
2132 default_branch: "main".to_string(),
2133 main_project_dir: work_dir.clone(),
2134 };
2135
2136 let store = Arc::new(TaskStore::open_memory().await.unwrap());
2137 let store_id = store
2138 .create(&NewTask {
2139 external_id: Some("3772".to_string()),
2140 repo: "owner/repo".to_string(),
2141 origin: "github".to_string(),
2142 title: "Fix the thing".to_string(),
2143 body: String::new(),
2144 source: "manual".to_string(),
2145 source_id: String::new(),
2146 author: "bot".to_string(),
2147 url: String::new(),
2148 labels: vec![],
2149 parent_id: None,
2150 })
2151 .await
2152 .unwrap();
2153 store
2154 .set_fields(store_id, &[("pr_number", serde_json::json!(42))])
2155 .await
2156 .unwrap();
2157 store
2159 .increment(store_id, "merge_conflict_retries")
2160 .await
2161 .unwrap();
2162 store
2165 .set_fields(
2166 store_id,
2167 &[("no_code_last_agent", serde_json::json!("claude"))],
2168 )
2169 .await
2170 .unwrap();
2171
2172 let parsed = agents::ParsedResponse {
2173 response: AgentResponse {
2174 status: "done".to_string(),
2175 summary: "the fix is already on main".to_string(),
2176 ..Default::default()
2177 },
2178 input_tokens: None,
2179 output_tokens: None,
2180 cost_usd: None,
2181 duration_ms: None,
2182 };
2183
2184 let (final_status, push_failed) = handle_success(
2185 "3772",
2186 parsed,
2187 &wt,
2188 "Fix the thing",
2189 "claude",
2190 Some("sonnet"),
2191 1,
2192 "owner/repo",
2193 &Some(Arc::clone(&store)),
2194 "",
2195 )
2196 .await
2197 .unwrap();
2198
2199 assert_eq!(
2200 final_status, "done",
2201 "a superseded conflict reroute must end done, not blocked"
2202 );
2203 assert!(!push_failed);
2204
2205 let task = store.get(store_id).await.unwrap();
2206 assert!(
2207 task.last_error.contains("superseded"),
2208 "the close attempt must be recorded on the task: {}",
2209 task.last_error
2210 );
2211 assert_eq!(
2212 task.pr_number, None,
2213 "a superseded PR must not stay on the task, the blocker refresh would read it as closed unmerged"
2214 );
2215 }
2216
2217 #[test]
2220 fn classify_superseded_conflict_marks_done() {
2221 let status = classify_final_status(&DecisionInput {
2222 agent_status: "done",
2223 requires_pr: true,
2224 is_same_agent: true,
2225 is_superseded_conflict: true,
2226 ..Default::default()
2227 });
2228 assert_eq!(status, "done");
2229 }
2230
2231 #[test]
2235 fn classify_workflow_scope_failure_blocks() {
2236 let status = classify_final_status(&DecisionInput {
2237 is_workflow_scope_failure: true,
2238 push_failed: true,
2239 push_failures: 1,
2240 ..Default::default()
2241 });
2242 assert_eq!(status, "blocked");
2243 }
2244
2245 #[test]
2247 fn classify_rebase_conflict_failure_blocks() {
2248 let status = classify_final_status(&DecisionInput {
2249 is_rebase_conflict_failure: true,
2250 push_failed: true,
2251 ..Default::default()
2252 });
2253 assert_eq!(status, "blocked");
2254 }
2255
2256 #[test]
2258 fn classify_push_failed_under_threshold_reroutes() {
2259 let status = classify_final_status(&DecisionInput {
2260 push_failed: true,
2261 push_failures: 2, ..Default::default()
2263 });
2264 assert_eq!(status, "new");
2265 }
2266
2267 #[test]
2268 fn denied_push_block_detected() {
2269 let resp = |s: &str| AgentResponse {
2270 status: "blocked".to_string(),
2271 summary: s.to_string(),
2272 ..Default::default()
2273 };
2274 assert!(is_denied_push_block(&resp(
2275 "Work committed, but `git push -u origin HEAD` was denied"
2276 )));
2277 assert!(is_denied_push_block(&resp(
2278 "It is not pushed: `git push --force-with-lease` needs approval that this session could not get."
2279 )));
2280 assert!(is_denied_push_block(&resp(
2281 "`git push --force-with-lease` was denied because this session cannot get approval"
2282 )));
2283 assert!(is_denied_push_block(&resp("The push requires approval")));
2284 assert!(!is_denied_push_block(&resp("Missing API key")));
2285 }
2286
2287 #[test]
2289 fn classify_push_failed_at_threshold_blocks() {
2290 let status = classify_final_status(&DecisionInput {
2291 push_failed: true,
2292 push_failures: 3, ..Default::default()
2294 });
2295 assert_eq!(status, "blocked");
2296 }
2297
2298 #[test]
2300 fn classify_transient_push_failure_waits_for_the_engine_retry() {
2301 for push_failures in 1..push_retry::MAX_PUSH_FAILURES {
2302 let status = classify_final_status(&DecisionInput {
2303 push_failed: true,
2304 is_transient_push_failure: true,
2305 push_failures,
2306 ..Default::default()
2307 });
2308 assert_eq!(status, "needs_review", "push_failures={push_failures}");
2309 }
2310 }
2311
2312 #[test]
2313 fn classify_transient_push_failure_blocks_after_the_last_retry() {
2314 let status = classify_final_status(&DecisionInput {
2315 push_failed: true,
2316 is_transient_push_failure: true,
2317 push_failures: push_retry::MAX_PUSH_FAILURES,
2318 ..Default::default()
2319 });
2320 assert_eq!(status, "blocked");
2321 }
2322
2323 #[test]
2325 fn classify_done_with_pr_goes_to_needs_review() {
2326 let status = classify_final_status(&DecisionInput {
2327 agent_status: "done",
2328 has_pr: true,
2329 ..Default::default()
2330 });
2331 assert_eq!(status, "needs_review");
2332 }
2333
2334 #[test]
2336 fn classify_done_with_delegations_no_pr_blocks() {
2337 let status = classify_final_status(&DecisionInput {
2338 agent_status: "done",
2339 has_delegations: true,
2340 ..Default::default()
2341 });
2342 assert_eq!(status, "blocked");
2343 }
2344
2345 #[test]
2347 fn classify_done_pushed_no_pr_reroutes_for_pr_creation() {
2348 let status = classify_final_status(&DecisionInput {
2349 agent_status: "done",
2350 has_pushed: true,
2351 ..Default::default()
2352 });
2353 assert_eq!(status, "routed");
2354 }
2355
2356 #[test]
2358 fn classify_done_pushed_pr_base_invalid_blocks() {
2359 let status = classify_final_status(&DecisionInput {
2360 agent_status: "done",
2361 has_pushed: true,
2362 is_pr_base_invalid: true,
2363 ..Default::default()
2364 });
2365 assert_eq!(status, "blocked");
2366 }
2367
2368 #[test]
2370 fn classify_completed_pushed_pr_base_invalid_blocks() {
2371 let status = classify_final_status(&DecisionInput {
2372 agent_status: "completed",
2373 is_completed_status: true,
2374 has_pushed: true,
2375 is_pr_base_invalid: true,
2376 ..Default::default()
2377 });
2378 assert_eq!(status, "blocked");
2379 }
2380
2381 #[test]
2383 fn classify_done_requires_pr_under_max_reroutes() {
2384 let status = classify_final_status(&DecisionInput {
2385 agent_status: "done",
2386 requires_pr: true,
2387 no_code_reroutes: 1,
2388 max_reroutes: 3,
2389 is_same_agent: false,
2390 ..Default::default()
2391 });
2392 assert_eq!(status, "new");
2393 }
2394
2395 #[test]
2397 fn classify_done_requires_pr_same_agent_blocks_immediately() {
2398 let status = classify_final_status(&DecisionInput {
2399 agent_status: "done",
2400 requires_pr: true,
2401 no_code_reroutes: 1,
2402 max_reroutes: 3,
2403 is_same_agent: true, ..Default::default()
2405 });
2406 assert_eq!(status, "blocked");
2407 }
2408
2409 #[test]
2411 fn classify_done_requires_pr_at_max_reroutes_blocks() {
2412 let status = classify_final_status(&DecisionInput {
2413 agent_status: "done",
2414 requires_pr: true,
2415 no_code_reroutes: 3,
2416 max_reroutes: 3,
2417 is_same_agent: false,
2418 ..Default::default()
2419 });
2420 assert_eq!(status, "blocked");
2421 }
2422
2423 #[test]
2425 fn classify_done_requires_pr_exceeds_max_reroutes_blocks() {
2426 let status = classify_final_status(&DecisionInput {
2427 agent_status: "done",
2428 requires_pr: true,
2429 no_code_reroutes: 5,
2430 max_reroutes: 3,
2431 is_same_agent: false,
2432 ..Default::default()
2433 });
2434 assert_eq!(status, "blocked");
2435 }
2436
2437 #[test]
2439 fn classify_done_external_no_pushed_same_agent_blocks_immediately() {
2440 let status = classify_final_status(&DecisionInput {
2441 agent_status: "done",
2442 requires_pr: true, no_code_reroutes: 0,
2444 max_reroutes: 3,
2445 is_same_agent: true,
2446 ..Default::default()
2447 });
2448 assert_eq!(status, "blocked");
2449 }
2450
2451 #[test]
2459 fn classify_done_external_no_pushed_commits_reroutes() {
2460 let status = classify_final_status(&DecisionInput {
2461 agent_status: "done",
2462 has_pushed: false,
2463 requires_pr: true, no_code_reroutes: 0,
2465 max_reroutes: 3,
2466 is_same_agent: false,
2467 ..Default::default()
2468 });
2469 assert_eq!(status, "new");
2470 }
2471
2472 #[test]
2475 fn classify_done_internal_task_no_pr_is_done() {
2476 let status = classify_final_status(&DecisionInput {
2477 agent_status: "done",
2478 has_pushed: false,
2479 requires_pr: false,
2480 ..Default::default()
2481 });
2482 assert_eq!(status, "done");
2483 }
2484
2485 #[test]
2487 fn classify_non_done_status_passes_through() {
2488 for status_str in &["in_progress", "needs_review", "blocked"] {
2489 let result = classify_final_status(&DecisionInput {
2490 agent_status: status_str,
2491 ..Default::default()
2492 });
2493 assert_eq!(
2494 result, *status_str,
2495 "status '{status_str}' should pass through unchanged"
2496 );
2497 }
2498 }
2499
2500 #[test]
2502 fn classify_descriptive_completion_status_normalizes_to_done() {
2503 let status = classify_final_status(&DecisionInput {
2504 agent_status: "Trading scan complete. Nothing to trade.",
2505 ..Default::default()
2506 });
2507 assert_eq!(status, "done");
2508 }
2509
2510 #[test]
2512 fn classify_descriptive_completion_with_failure_cues_does_not_normalize() {
2513 let status = classify_final_status(&DecisionInput {
2514 agent_status: "completed but failed to push branch due to error",
2515 ..Default::default()
2516 });
2517 assert_eq!(status, "completed but failed to push branch due to error");
2518 }
2519
2520 #[test]
2522 fn classify_push_failed_takes_precedence_over_has_pr() {
2523 let status = classify_final_status(&DecisionInput {
2524 agent_status: "done",
2525 has_pr: true,
2526 push_failed: true,
2527 push_failures: 1,
2528 ..Default::default()
2529 });
2530 assert_eq!(status, "new");
2532 }
2533
2534 #[test]
2536 fn classify_workflow_scope_failure_takes_precedence_over_push_reroute() {
2537 let status = classify_final_status(&DecisionInput {
2538 is_workflow_scope_failure: true,
2539 push_failed: true,
2540 push_failures: 1, ..Default::default()
2542 });
2543 assert_eq!(status, "blocked");
2544 }
2545
2546 #[test]
2550 fn classify_completed_with_pr_goes_to_needs_review() {
2551 let status = classify_final_status(&DecisionInput {
2552 agent_status: "completed",
2553 has_pr: true,
2554 is_completed_status: true,
2555 ..Default::default()
2556 });
2557 assert_eq!(status, "needs_review");
2558 }
2559
2560 #[test]
2562 fn classify_completed_with_delegations_no_pr_blocks() {
2563 let status = classify_final_status(&DecisionInput {
2564 agent_status: "completed",
2565 has_delegations: true,
2566 is_completed_status: true,
2567 ..Default::default()
2568 });
2569 assert_eq!(status, "blocked");
2570 }
2571
2572 #[test]
2574 fn classify_completed_pushed_no_pr_reroutes_for_pr_creation() {
2575 let status = classify_final_status(&DecisionInput {
2576 agent_status: "completed",
2577 has_pushed: true,
2578 is_completed_status: true,
2579 ..Default::default()
2580 });
2581 assert_eq!(status, "routed");
2582 }
2583
2584 #[test]
2586 fn classify_completed_requires_pr_under_max_reroutes() {
2587 let status = classify_final_status(&DecisionInput {
2588 agent_status: "completed",
2589 requires_pr: true,
2590 no_code_reroutes: 1,
2591 max_reroutes: 3,
2592 is_completed_status: true,
2593 is_same_agent: false,
2594 ..Default::default()
2595 });
2596 assert_eq!(status, "new");
2597 }
2598
2599 #[test]
2601 fn classify_completed_requires_pr_at_max_reroutes_blocks() {
2602 let status = classify_final_status(&DecisionInput {
2603 agent_status: "completed",
2604 requires_pr: true,
2605 no_code_reroutes: 3,
2606 max_reroutes: 3,
2607 is_completed_status: true,
2608 is_same_agent: false,
2609 ..Default::default()
2610 });
2611 assert_eq!(status, "blocked");
2612 }
2613
2614 #[test]
2616 fn classify_completed_same_agent_blocks_immediately() {
2617 let status = classify_final_status(&DecisionInput {
2618 agent_status: "completed",
2619 requires_pr: true,
2620 no_code_reroutes: 1,
2621 max_reroutes: 3,
2622 is_completed_status: true,
2623 is_same_agent: true,
2624 ..Default::default()
2625 });
2626 assert_eq!(status, "blocked");
2627 }
2628
2629 #[test]
2631 fn classify_completed_internal_task_is_done() {
2632 let status = classify_final_status(&DecisionInput {
2633 agent_status: "completed",
2634 requires_pr: false,
2635 is_completed_status: true,
2636 ..Default::default()
2637 });
2638 assert_eq!(status, "done");
2639 }
2640
2641 #[test]
2645 fn agent_blocked_reason_prefers_error_field() {
2646 let resp = AgentResponse {
2647 status: "blocked".to_string(),
2648 error: Some("CDP endpoint not reachable".to_string()),
2649 summary: "Could not complete research".to_string(),
2650 remaining: vec!["Run x-twitter-brave with reachable endpoint".to_string()],
2651 ..Default::default()
2652 };
2653 let reason = agent_blocked_reason(&resp);
2654 assert!(
2655 reason.contains("CDP endpoint not reachable"),
2656 "reason: {reason}"
2657 );
2658 assert!(reason.contains("Remaining:"), "reason: {reason}");
2659 assert!(reason.contains("x-twitter-brave"), "reason: {reason}");
2660 }
2661
2662 #[test]
2664 fn agent_blocked_reason_falls_back_to_summary() {
2665 let resp = AgentResponse {
2666 status: "blocked".to_string(),
2667 error: None,
2668 summary: "Could not access required data sources in sandboxed environment".to_string(),
2669 remaining: vec![],
2670 ..Default::default()
2671 };
2672 let reason = agent_blocked_reason(&resp);
2673 assert_eq!(
2674 reason,
2675 "Could not access required data sources in sandboxed environment"
2676 );
2677 }
2678
2679 #[test]
2681 fn agent_blocked_reason_generic_fallback() {
2682 let resp = AgentResponse {
2683 status: "blocked".to_string(),
2684 ..Default::default()
2685 };
2686 let reason = agent_blocked_reason(&resp);
2687 assert_eq!(reason, "agent returned blocked status without a reason");
2688 }
2689
2690 #[test]
2692 fn classify_agent_blocked_passes_through() {
2693 let status = classify_final_status(&DecisionInput {
2694 agent_status: "blocked",
2695 ..Default::default()
2696 });
2697 assert_eq!(status, "blocked");
2698 }
2699
2700 #[test]
2701 fn classify_retryable_agent_blocked_reroutes() {
2702 let status = classify_final_status(&DecisionInput {
2703 agent_status: "blocked",
2704 is_retryable_blocked: true,
2705 ..Default::default()
2706 });
2707 assert_eq!(status, "new");
2708 }
2709
2710 #[test]
2711 fn retryable_blocked_reason_detects_transient_patterns() {
2712 assert!(is_retryable_blocked_reason(
2713 "Twitter access unavailable: service timed out"
2714 ));
2715 assert!(is_retryable_blocked_reason(
2716 "final commit blocked by git worktree lock permission error"
2717 ));
2718 assert!(is_retryable_blocked_reason(
2719 "could not commit due a worktree git lock permission error"
2720 ));
2721 assert!(is_retryable_blocked_reason(
2722 "git lockfile permission restrictions in this environment"
2723 ));
2724 assert!(is_retryable_blocked_reason(
2725 "API rate limit hit, try again later"
2726 ));
2727 assert!(is_retryable_blocked_reason(
2728 "Credential bean/hyperliquid-address exists in the pass store but GPG decryption fails in this agent session — no passphrase available."
2729 ));
2730 assert!(is_retryable_blocked_reason(
2731 "credential resolution failed via PasswordStoreProvider; AgentVaultProvider unavailable"
2732 ));
2733 }
2734
2735 #[test]
2736 fn retryable_blocked_reason_rejects_permanent_patterns() {
2737 assert!(!is_retryable_blocked_reason(
2738 "invalid request payload: malformed JSON"
2739 ));
2740 assert!(!is_retryable_blocked_reason(
2741 "resource not found in repository"
2742 ));
2743 }
2744}