mod comments;
mod unblock;
use super::EngineConfig;
use super::ci_bootstrap;
use super::cleanup::{check_merged_prs, cleanup_done_worktrees, prune_old_run_output};
use super::review_poll::review_open_prs;
use crate::backends::ExternalTask;
use crate::backends::{ExternalBackend, Status};
use crate::cooldown;
use crate::cooldown::{
cooldown_reason, github_circuit_remaining_secs, is_agent_in_cooldown, is_github_circuit_open,
is_model_in_cooldown, refresh_degraded_agents,
};
use crate::engine;
use crate::engine::dependencies;
use crate::engine::router::Router;
use crate::engine::router::RouterConfig;
use crate::engine::router::config as router_config;
use crate::engine::runner::agents::patterns;
use crate::engine::tasks;
use crate::engine::tasks::{TaskManager, status_to_task_status};
use crate::github::http::GhHttp;
use crate::store;
use crate::store::Task;
use crate::store::TaskRun;
use crate::store::TaskStatus;
use crate::store::TaskStore;
use crate::tmux::TmuxManager;
use comments::{fetch_comments_since, filter_mentions_by_since, scan_comments};
use dashmap::{DashMap, DashSet};
use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::RwLock;
use unblock::{REBROADCAST_BLOCK_REASON, auto_unblock_blocked_tasks};
pub(crate) use unblock::{
auto_recover_rebroadcast_blocked_tasks_global, auto_unblock_ci_failure_blocked_tasks_global,
};
async fn kv_get_prefer_store(store: Option<&Arc<TaskStore>>, key: &str) -> Option<String> {
match try_kv_get_prefer_store(store, key).await {
Ok(opt) => opt,
Err(e) => {
tracing::error!(key, err = %e, "kv_get failed");
None
}
}
}
async fn try_kv_get_prefer_store(
store: Option<&Arc<TaskStore>>,
key: &str,
) -> anyhow::Result<Option<String>> {
if let Some(s) = store {
Ok(s.kv_get(key).await?)
} else {
Ok(None)
}
}
async fn kv_set_prefer_store(store: Option<&Arc<TaskStore>>, key: &str, value: &str) {
if let Err(e) = try_kv_set_prefer_store(store, key, value).await {
tracing::error!(key, err = %e, "kv_set failed");
}
}
async fn try_kv_set_prefer_store(
store: Option<&Arc<TaskStore>>,
key: &str,
value: &str,
) -> anyhow::Result<()> {
if let Some(s) = store {
s.kv_set(key, value).await?;
}
Ok(())
}
#[derive(Debug, Clone)]
pub(crate) struct ReviewTaskSnapshot {
pub external: ExternalTask,
pub stored: Task,
}
async fn prefetch_review_tasks(
store: &Arc<TaskStore>,
repo: &str,
) -> anyhow::Result<(
Vec<ReviewTaskSnapshot>,
Vec<ReviewTaskSnapshot>,
Vec<ReviewTaskSnapshot>,
)> {
let mut in_review = store.list_by_status(repo, TaskStatus::InReview).await?;
in_review.extend(store.list_by_status(repo, TaskStatus::Approved).await?);
let in_review = in_review
.into_iter()
.map(|stored| ReviewTaskSnapshot {
external: tasks::store_task_to_external(&stored),
stored,
})
.collect();
let needs_review = store
.list_by_status(repo, TaskStatus::NeedsReview)
.await?
.into_iter()
.map(|stored| ReviewTaskSnapshot {
external: tasks::store_task_to_external(&stored),
stored,
})
.collect();
let blocked_candidates = store
.list_by_status(repo, TaskStatus::Blocked)
.await?
.into_iter()
.filter(|stored| stored.pr_number.is_some() || !stored.branch.is_empty())
.map(|stored| ReviewTaskSnapshot {
external: tasks::store_task_to_external(&stored),
stored,
})
.collect();
Ok((in_review, needs_review, blocked_candidates))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FailureCategory {
FalseFailure,
RateLimit,
CreditExhausted,
SilentExit0,
Timeout,
ConnectionError,
CliFlagError,
AllAgentsExhausted,
ParseError,
MaxAttempts,
PushFailed,
PrCreateFailed,
ModelUnavailable,
Unknown,
}
impl FailureCategory {
fn is_recoverable(self) -> bool {
matches!(
self,
FailureCategory::FalseFailure
| FailureCategory::RateLimit
| FailureCategory::CreditExhausted
| FailureCategory::SilentExit0
| FailureCategory::Timeout
| FailureCategory::ConnectionError
| FailureCategory::CliFlagError
| FailureCategory::AllAgentsExhausted
| FailureCategory::ParseError
| FailureCategory::ModelUnavailable
)
}
fn as_str(self) -> &'static str {
match self {
FailureCategory::FalseFailure => "FalseFailure",
FailureCategory::RateLimit => "RateLimit",
FailureCategory::CreditExhausted => "CreditExhausted",
FailureCategory::SilentExit0 => "SilentExit0",
FailureCategory::Timeout => "Timeout",
FailureCategory::ConnectionError => "ConnectionError",
FailureCategory::CliFlagError => "CliFlagError",
FailureCategory::AllAgentsExhausted => "AllAgentsExhausted",
FailureCategory::ParseError => "ParseError",
FailureCategory::MaxAttempts => "MaxAttempts",
FailureCategory::PushFailed => "PushFailed",
FailureCategory::PrCreateFailed => "PrCreateFailed",
FailureCategory::ModelUnavailable => "ModelUnavailable",
FailureCategory::Unknown => "Unknown",
}
}
}
fn classify_failure(error: &str, outcome: &str) -> FailureCategory {
let lower = error.to_lowercase();
if outcome == "timeout" || lower.contains("timeout") {
return FailureCategory::Timeout;
}
if outcome == "billing_cycle_exhausted"
|| outcome == "credit_exhausted"
|| outcome == "org_level_disabled"
|| cooldown::detect_credit_exhaustion(&lower).is_some()
{
return FailureCategory::CreditExhausted;
}
if outcome == "rate_limit" || lower.contains("rate limit") || lower.contains("usage limit") {
return FailureCategory::RateLimit;
}
if error.trim().is_empty() {
return FailureCategory::FalseFailure;
}
if lower.contains("all agents exhausted") {
return FailureCategory::AllAgentsExhausted;
}
if lower.contains("max attempts") || lower.contains("exceeded max attempts") {
return FailureCategory::MaxAttempts;
}
if lower.contains("output-format=stream-json") && lower.contains("requires --verbose") {
return FailureCategory::CliFlagError;
}
if lower.contains("model unavailable")
|| lower.contains("model not found")
|| (lower.contains("does not exist") && lower.contains("model"))
{
return FailureCategory::ModelUnavailable;
}
if lower.contains("push failed") {
return FailureCategory::PushFailed;
}
if lower.contains("create pr failed")
|| lower.contains("failed to create pull request")
|| lower.contains("pull request creation failed")
{
return FailureCategory::PrCreateFailed;
}
if lower.contains("exit 0") {
return FailureCategory::SilentExit0;
}
if outcome == "parse_error"
|| outcome == "truncated"
|| lower.contains("invalid response")
|| lower.contains("parse error")
|| lower.contains("truncated")
{
return FailureCategory::ParseError;
}
if patterns::detect_network_error(&lower).is_some() {
return FailureCategory::ConnectionError;
}
FailureCategory::Unknown
}
fn classify_failure_from_run(run: &TaskRun) -> Option<FailureCategory> {
if run.outcome == "success" && run.error.trim().is_empty() {
return None;
}
if run.outcome.trim().is_empty()
&& run.error.trim().is_empty()
&& run.exit_code.is_none()
&& run.stdout.trim().is_empty()
&& run.stderr.trim().is_empty()
{
return None;
}
let has_output = !run.stdout.trim().is_empty() || !run.stderr.trim().is_empty();
if run.exit_code == Some(0) && !has_output && run.outcome != "success" {
return Some(FailureCategory::SilentExit0);
}
let mut category = classify_failure(&run.error, &run.outcome);
if category == FailureCategory::FalseFailure {
let has_nonzero_exit = run.exit_code.is_some() && run.exit_code != Some(0);
let has_output = !run.stdout.trim().is_empty() || !run.stderr.trim().is_empty();
if has_nonzero_exit || has_output {
category = FailureCategory::Unknown;
}
}
Some(category)
}
pub(crate) async fn refire_and_escalate_stale_needs_review_global(
task_manager: &Arc<TaskManager>,
store: &Arc<TaskStore>,
router: &Router,
dispatching: &Arc<DashMap<String, String>>,
active_repos: &HashSet<String>,
) -> anyhow::Result<()> {
const MIN_STALE_NEEDS_REVIEW_MINUTES: i64 = 1;
const MAX_NEEDS_REVIEW_REFIRE_ATTEMPTS: u64 = 5;
let needs_review_tasks = match store
.list_all_by_status_global(TaskStatus::NeedsReview)
.await
{
Ok(tasks) => tasks,
Err(e) => {
tracing::warn!(err = %e, "global NeedsReview refire sweep: failed to list needs_review tasks");
return Ok(());
}
};
if needs_review_tasks.is_empty() {
return Ok(());
}
let all_review_agents_cooled = router.healthy_agent_count("review") == 0;
if all_review_agents_cooled {
tracing::info!(
count = needs_review_tasks.len(),
"global NeedsReview refire sweep: no review agent available — skipping this pass"
);
return Ok(());
}
for stored in needs_review_tasks {
if !active_repos.contains(&stored.repo) {
continue;
}
let external = tasks::store_task_to_external(&stored);
let ext_id = external.id.clone();
let repo = stored.repo.clone();
let age_minutes = match chrono::DateTime::parse_from_rfc3339(&external.updated_at) {
Ok(updated_at) => {
let age = chrono::Utc::now() - updated_at.with_timezone(&chrono::Utc);
if age.num_minutes() < MIN_STALE_NEEDS_REVIEW_MINUTES {
continue;
}
Some(age.num_minutes())
}
Err(_) => None,
};
let dispatch_key = format!("{}/{}", repo, ext_id.0);
if dispatching.contains_key(&dispatch_key) {
continue;
}
let current_refires = u64::try_from(stored.needs_review_refires).unwrap_or(0);
let new_refires = current_refires + 1;
if new_refires > MAX_NEEDS_REVIEW_REFIRE_ATTEMPTS {
tracing::warn!(
task_id = stored.id,
repo = %repo,
new_refires,
"global NeedsReview refire sweep: escalating to Blocked after repeated refires"
);
let fields = [
("block_reason", serde_json::json!(REBROADCAST_BLOCK_REASON)),
(
"last_error",
serde_json::json!(format!("escalated after {} retries", new_refires)),
),
];
if let Err(e) = task_manager
.update_task_status_and_result_by_store_id(
stored.id,
&repo,
&ext_id,
Status::Blocked,
&fields,
)
.await
{
tracing::error!(task_id = stored.id, repo = %repo, err = %e, "global NeedsReview refire sweep: escalation to Blocked failed — skipping to avoid silent auto-unblock loop");
continue;
}
if let Err(e) = store::store_increment_by_id(
&Some(Arc::clone(store)),
stored.id,
"needs_review_refires",
)
.await
{
tracing::warn!(
task_id = stored.id,
repo = %repo,
err = %e,
"global NeedsReview refire sweep: failed to increment needs_review_refires after escalation"
);
}
continue;
}
let required_minutes =
MIN_STALE_NEEDS_REVIEW_MINUTES * (1i64 << u32::try_from(current_refires).unwrap_or(0));
let should_fire = match age_minutes {
Some(age) => age >= required_minutes,
None => true,
};
if !should_fire {
continue;
}
if let Err(e) = task_manager
.update_task_status_by_store_id(stored.id, &repo, &ext_id, Status::NeedsReview)
.await
{
tracing::warn!(
task_id = stored.id,
repo = %repo,
err = %e,
"global NeedsReview refire sweep: failed to re-fire NeedsReview event — not incrementing counter"
);
continue;
}
let fired_refires = match store::store_increment_by_id(
&Some(Arc::clone(store)),
stored.id,
"needs_review_refires",
)
.await
{
Ok(v) => v,
Err(e) => {
tracing::warn!(
task_id = stored.id,
repo = %repo,
err = %e,
"global NeedsReview refire sweep: failed to increment needs_review_refires after re-fire"
);
current_refires + 1
}
};
tracing::info!(
task_id = stored.id,
repo = %repo,
age_minutes,
refires = fired_refires,
required_minutes,
"global NeedsReview refire sweep: re-firing NeedsReview event for stale task"
);
}
Ok(())
}
#[expect(
clippy::too_many_arguments,
reason = "the engine passes its shared handles one by one"
)]
pub(crate) async fn sync_tick(
backend: &Arc<dyn ExternalBackend>,
tmux: &Arc<TmuxManager>,
repo: &str,
config: &EngineConfig,
router: &Arc<RwLock<Router>>,
task_manager: &Arc<TaskManager>,
store: &Arc<TaskStore>,
dispatching: &Arc<DashMap<String, String>>,
auto_merge_in_flight: &Arc<DashSet<String>>,
) -> anyhow::Result<()> {
if !store.is_leader() {
return Ok(());
}
tracing::debug!("sync tick");
let mut phases: Vec<(&str, u128)> = Vec::new();
let mut at = Instant::now();
let gh_circuit_open = is_github_circuit_open();
if gh_circuit_open {
let remaining = github_circuit_remaining_secs();
tracing::debug!(
remaining_secs = remaining,
"GitHub 5xx circuit breaker open — skipping non-critical sync work"
);
if let Err(e) =
try_kv_set_prefer_store(Some(store), "metrics:orch.github_5xx_circuit.open", "1").await
{
tracing::error!(err = %e, "failed to persist circuit breaker state — may cause thundering herd after restart");
}
} else if let Err(e) =
try_kv_set_prefer_store(Some(store), "metrics:orch.github_5xx_circuit.open", "0").await
{
tracing::error!(err = %e, "failed to clear circuit breaker state");
}
if !gh_circuit_open {
if let Err(e) = ingest_external_tasks(backend, repo, store).await {
tracing::debug!(err = %e, "external task ingest failed");
}
phases.push(("ingest", at.elapsed().as_millis()));
at = Instant::now();
if let Err(e) = backend.reconcile_project_statuses().await {
tracing::warn!(err = %e, "project status reconciliation failed");
}
phases.push(("board_reconcile", at.elapsed().as_millis()));
}
at = Instant::now();
if !gh_circuit_open {
let backend = Arc::clone(backend);
let repo = repo.to_string();
let task_manager = Arc::clone(task_manager);
let store = Arc::clone(store);
tokio::spawn(async move {
if let Err(e) = cleanup_done_worktrees(&backend, &repo, &task_manager, &store).await {
tracing::warn!(err = %e, "worktree cleanup failed");
}
if let Err(e) = prune_old_run_output(&store).await {
tracing::warn!(err = %e, "task run pruning failed");
}
});
}
let (in_review_tasks, needs_review_tasks, blocked_review_candidates) =
match prefetch_review_tasks(store, repo).await {
Ok(tasks) => tasks,
Err(e) => {
tracing::warn!(err = %e, "failed to prefetch review tasks");
(vec![], vec![], vec![])
}
};
phases.push(("prefetch_review", at.elapsed().as_millis()));
at = Instant::now();
if !gh_circuit_open {
let mention_fallback = chrono::Utc::now() - chrono::Duration::hours(24);
let mention_since = kv_get_prefer_store(Some(store), "mentions_last_checked")
.await
.unwrap_or_else(|| mention_fallback.format("%Y-%m-%dT%H:%M:%SZ").to_string());
let shared_since = mention_since.clone();
let cached_router_config = router.read().await.config.clone();
let mut combined_needs = needs_review_tasks.clone();
combined_needs.extend(blocked_review_candidates.clone());
let (merge_result, comments_result, review_result) = tokio::join!(
check_merged_prs(
backend,
repo,
store,
task_manager,
&in_review_tasks,
&combined_needs,
),
fetch_comments_since(backend, &shared_since),
async {
let gh = match GhHttp::new() {
Ok(g) => g,
Err(e) => {
tracing::warn!(err = %e, "failed to create GitHub HTTP client for review poll");
return Ok(());
}
};
Box::pin(review_open_prs(
backend,
repo,
config,
task_manager,
store,
dispatching,
auto_merge_in_flight,
&in_review_tasks,
&gh,
&cached_router_config,
))
.await
}
);
if let Err(e) = merge_result {
tracing::warn!(err = %e, "PR merge check failed");
}
match comments_result {
Ok(comments) => {
let filtered = filter_mentions_by_since(&comments, &mention_since);
if let Err(e) =
scan_comments(backend, Some(store), repo, task_manager, Some(&filtered)).await
{
tracing::warn!(err = %e, "comment scan failed");
}
}
Err(e) => {
tracing::warn!(err = %e, "comment fetch failed");
}
}
if let Err(e) = review_result {
tracing::warn!(err = %e, "PR review failed");
}
if let Err(e) = ci_bootstrap::apply_pending(store, task_manager, repo).await {
tracing::warn!(err = %e, "CI bootstrap ruleset apply failed");
}
phases.push(("merge_comments_review", at.elapsed().as_millis()));
}
at = Instant::now();
let in_progress_tasks: Vec<_> = match store.list_by_status(repo, TaskStatus::InProgress).await {
Ok(tasks) => tasks
.into_iter()
.map(|stored| {
let external = tasks::store_task_to_external(&stored);
(stored, external)
})
.collect(),
Err(e) => {
tracing::warn!(err = %e, "failed to list in_progress tasks — skipping stale InProgress check");
vec![]
}
};
if !in_progress_tasks.is_empty() {
let live_sessions: HashSet<String> = tmux
.list_sessions()
.await
.unwrap_or_default()
.into_iter()
.map(|s| s.name)
.collect();
for (_stored, external) in &in_progress_tasks {
const MIN_STALE_MINUTES: i64 = 5;
let task_id = &external.id.0;
let dispatch_key = format!("{repo}/{task_id}");
{
if dispatching.contains_key(&dispatch_key) {
tracing::debug!(
task_id = %task_id,
"task locked by dispatch flow, skipping stale check"
);
continue;
}
}
match chrono::DateTime::parse_from_rfc3339(&external.updated_at) {
Ok(updated_at) => {
let age = chrono::Utc::now() - updated_at.with_timezone(&chrono::Utc);
if age.num_minutes() < MIN_STALE_MINUTES {
tracing::debug!(
task_id = %task_id,
age_seconds = age.num_seconds(),
"InProgress task is too young to be considered stale, skipping"
);
continue;
}
}
Err(e) => {
tracing::warn!(
task_id = %task_id,
ts = %external.updated_at,
err = %e,
"invalid updated_at timestamp — treating task as potentially stale"
);
}
}
let agent_session = tmux.session_name(repo, task_id);
if live_sessions.contains(&agent_session) {
continue;
}
{
tracing::warn!(
task_id = %task_id,
session = %agent_session,
"InProgress task has no active agent session — resetting to Routed"
);
match task_manager
.update_task_status_if(&external.id, Status::Routed, Status::InProgress)
.await
{
Err(e) => {
tracing::error!(task_id = %task_id, err = %e, "failed to reset stale InProgress task — task may be stuck in InProgress indefinitely");
}
Ok(false) => {
tracing::debug!(
task_id = %task_id,
"stale InProgress reset skipped — task already transitioned (concurrent Done/Blocked/NeedsReview)"
);
}
Ok(true) => {
}
}
}
}
}
phases.push(("stale_in_progress", at.elapsed().as_millis()));
at = Instant::now();
if let Err(e) =
auto_unblock_blocked_tasks(repo, task_manager, store, dispatching, auto_merge_in_flight)
.await
{
tracing::warn!(err = %e, "auto-unblock failed");
}
phases.push(("auto_unblock", at.elapsed().as_millis()));
engine::log_phase_timings("sync", repo, &phases);
Ok(())
}
pub(crate) async fn sync_shared_tail(router: &Arc<RwLock<Router>>, store: Option<&Arc<TaskStore>>) {
let Some(store) = store else {
return;
};
if !store.is_leader() {
return;
}
let (available_agents, config) = {
let r = router.read().await;
(r.available_agents.clone(), r.config.clone())
};
let model_checker = |agent: &str| -> bool {
for comp in &["simple", "medium", "complex", "review"] {
if config.has_available_model_for_complexity(agent, comp) {
return true;
}
}
false
};
refresh_degraded_agents(
store,
&available_agents,
&model_checker,
router_config::health_check_window_hours(),
router_config::degraded_rate_limit_threshold(),
)
.await;
emit_degraded_agents_if_needed(&available_agents, &config, Some(store)).await;
}
struct DegradedAgentDetail {
agent: String,
reason: String,
cooled_models: Vec<String>,
}
struct AgentPoolHealth {
agent: String,
cooled_models: Vec<String>,
persistent_failed_models: Vec<String>,
total_configured_models: usize,
}
pub(crate) async fn emit_degraded_agents_if_needed(
available_agents: &[String],
config: &RouterConfig,
store: Option<&Arc<TaskStore>>,
) {
const STALE_AGENTS_KV: &str = "metrics:orch.model_pool_stale_persistent.agents";
const COMPLEXITIES: &[&str] = &["simple", "medium", "complex", "review"];
const HEAVILY_COOLED_RATIO_THRESHOLD: f64 = 0.5;
let mut details: Vec<DegradedAgentDetail> = Vec::new();
let mut pool_health: Vec<AgentPoolHealth> = Vec::new();
for agent in available_agents {
let agent_in_cd = is_agent_in_cooldown(agent);
let mut cooled_models: Vec<String> = Vec::new();
let mut persistent_failed_models: Vec<String> = Vec::new();
let mut seen_models = HashSet::new();
for comp in COMPLEXITIES {
if let Some(pool) = config.model_pool_for_complexity(agent, comp) {
for model in pool {
if seen_models.insert(model.clone()) && is_model_in_cooldown(agent, &model) {
let key = format!("{agent}:{model}");
if matches!(
cooldown_reason(&key).as_deref(),
Some("persistent_model_error" | "model_not_found")
) {
persistent_failed_models.push(model.clone());
}
cooled_models.push(model);
}
}
}
}
let total_configured_models = seen_models.len();
pool_health.push(AgentPoolHealth {
agent: agent.clone(),
cooled_models: cooled_models.clone(),
persistent_failed_models: persistent_failed_models.clone(),
total_configured_models,
});
let has_model = COMPLEXITIES
.iter()
.any(|comp| config.has_available_model_for_complexity(agent, comp));
if agent_in_cd || !has_model {
let reason = if agent_in_cd {
cooldown_reason(agent).unwrap_or_else(|| "agent_cooldown".to_string())
} else {
"no_available_model".to_string()
};
details.push(DegradedAgentDetail {
agent: agent.clone(),
reason,
cooled_models,
});
}
}
let count = details.len();
if let Err(e) = try_kv_set_prefer_store(
store,
"metrics:orch.agents_degraded.count",
&count.to_string(),
)
.await
{
tracing::warn!(err = %e, "failed to persist degraded agents count metric");
}
if count >= 3 {
let degraded_agents: Vec<&str> = details.iter().map(|d| d.agent.as_str()).collect();
let cooled_models_dim: Vec<String> = details
.iter()
.filter(|d| !d.cooled_models.is_empty())
.map(|d| format!("{}:[{}]", d.agent, d.cooled_models.join(",")))
.collect();
let reasons_dim: Vec<String> = details
.iter()
.map(|d| format!("{}={}", d.agent, d.reason))
.collect();
tracing::warn!(
degraded_count = count,
degraded_agents = ?degraded_agents,
cooled_models = %cooled_models_dim.join("; "),
cooldown_reasons = %reasons_dim.join("; "),
"multi-agent degradation detected"
);
if let Err(e) =
try_kv_set_prefer_store(store, "metrics:orch.agents_degraded.alert", "1").await
{
tracing::warn!(err = %e, "failed to persist degraded agents alert metric");
}
} else if let Err(e) =
try_kv_set_prefer_store(store, "metrics:orch.agents_degraded.alert", "0").await
{
tracing::warn!(err = %e, "failed to clear degraded agents alert metric");
}
let heavily_cooled_agents: Vec<String> = pool_health
.iter()
.filter_map(|d| {
if d.total_configured_models == 0 || d.persistent_failed_models.is_empty() {
return None;
}
#[expect(
clippy::cast_precision_loss,
reason = "model counts are far below 2^52"
)]
let ratio = d.cooled_models.len() as f64 / d.total_configured_models as f64;
(ratio >= HEAVILY_COOLED_RATIO_THRESHOLD).then(|| {
format!(
"{}:{}/{}:{}",
d.agent,
d.cooled_models.len(),
d.total_configured_models,
d.persistent_failed_models.join(",")
)
})
})
.collect();
let current_dim = heavily_cooled_agents.join(",");
let prev_dim = kv_get_prefer_store(store, STALE_AGENTS_KV)
.await
.unwrap_or_default();
if heavily_cooled_agents.is_empty() {
if !prev_dim.is_empty() {
tracing::info!("agent model pool stale condition cleared");
}
if let Err(e) = try_kv_set_prefer_store(store, STALE_AGENTS_KV, "").await {
tracing::warn!(err = %e, "failed to clear stale model pool agents state");
}
if let Err(e) =
try_kv_set_prefer_store(store, "metrics:orch.model_pool_stale_persistent.alert", "0")
.await
{
tracing::warn!(err = %e, "failed to clear stale model pool alert metric");
}
} else {
if current_dim != prev_dim {
tracing::warn!(
affected_agents = ?heavily_cooled_agents,
"agent model pool appears stale: persistent model failures in heavily cooled pool"
);
}
if let Err(e) = try_kv_set_prefer_store(store, STALE_AGENTS_KV, ¤t_dim).await {
tracing::warn!(err = %e, "failed to persist stale model pool agents state");
}
if let Err(e) =
try_kv_set_prefer_store(store, "metrics:orch.model_pool_stale_persistent.alert", "1")
.await
{
tracing::warn!(err = %e, "failed to persist stale model pool alert metric");
}
}
}
pub(crate) async fn ingest_external_tasks(
backend: &Arc<dyn ExternalBackend>,
repo: &str,
store: &Arc<TaskStore>,
) -> anyhow::Result<()> {
#[derive(Clone)]
struct DuplicateTarget {
id: String,
status_label: String,
}
let mut existing_by_title: HashMap<String, DuplicateTarget> = HashMap::new();
let mut accepted_by_title: HashMap<String, DuplicateTarget> = HashMap::new();
let cutoff = (chrono::Utc::now() - chrono::Duration::hours(24))
.format("%Y-%m-%dT%H:%M:%SZ")
.to_string();
match store.list_for_doctor(repo, &cutoff).await {
Ok(recent) => {
let mut insert_target = |task: &Task| {
if task.origin == "internal" {
return;
}
let Some(ext_id) = task.external_id.as_ref() else {
return;
};
let status_label = task
.labels
.iter()
.find(|l| l.starts_with("status:"))
.cloned()
.unwrap_or_default();
existing_by_title
.entry(task.title.clone())
.or_insert(DuplicateTarget {
id: ext_id.clone(),
status_label,
});
};
for task in recent.iter().filter(|t| t.status != TaskStatus::Done) {
insert_target(task);
}
for task in recent.iter().filter(|t| t.status == TaskStatus::Done) {
insert_target(task);
}
}
Err(e) => {
tracing::warn!(repo, err = %e, "ingest: failed to load recent tasks for dedup");
}
}
let all_tasks: Vec<(ExternalTask, Option<Status>)> =
match backend.list_active_open_issues().await {
Ok(tasks) => tasks,
Err(e) => {
tracing::warn!(
repo,
err = %e,
"ingest: failed to list open issues — no new issue is ingested this cycle"
);
return Ok(());
}
};
let mut id_status_pairs: Vec<(i64, Status)> = Vec::new();
let mut new_task_node_ids: Vec<(i64, String)> = Vec::new();
let existing_ext_ids: HashSet<String> = match store.existing_external_ids(repo).await {
Ok(s) => s,
Err(e) => {
tracing::warn!(repo = repo, err = %e, "ingest: failed to load existing external ids — proceeding");
HashSet::new()
}
};
for (task, status) in &all_tasks {
let is_new = !existing_ext_ids.contains(&task.id.0);
if is_new {
let duplicate_target = existing_by_title
.get(&task.title)
.or_else(|| accepted_by_title.get(&task.title));
if let Some(target) = duplicate_target
&& target.id != task.id.0
{
let label = target.status_label.as_str();
let should_dedupe =
match backend.has_open_issue_with_title(&task.title, label).await {
Ok(true) => true,
Ok(false) => false,
Err(e) => {
tracing::warn!(
task_id = task.id.0,
err = %e,
"ingest: dedup check failed — defaulting to keep issue"
);
false
}
};
if should_dedupe {
let comment = format!(
"Duplicate of #{id} — closing to avoid duplicate work.{footer}",
id = target.id,
footer = engine::orch_footer()
);
if let Err(e) = backend.post_comment(&task.id, &comment).await {
tracing::warn!(
task_id = task.id.0,
err = %e,
"ingest: failed to comment on duplicate issue"
);
}
match backend.update_status(&task.id, Status::Done).await {
Ok(()) => {
tracing::info!(
task_id = task.id.0,
duplicate_of = target.id,
"ingest: closed duplicate issue"
);
continue;
}
Err(e) => {
tracing::warn!(
task_id = task.id.0,
err = %e,
"ingest: failed to close duplicate issue — falling through to ingest normally"
);
}
}
}
}
}
match store.upsert_external(&task.upsert_row(repo)).await {
Ok(store_id) => {
if let Ok(number) = task.id.0.parse::<i64>()
&& let Err(err) = dependencies::sync_issue_dependencies(
backend.as_ref(),
store,
store_id,
repo,
Some(number),
&task.body,
)
.await
{
tracing::warn!(
task_id = task.id.0,
err = %err,
"ingest: dependency sync failed"
);
}
if let Some(s) = status {
id_status_pairs.push((store_id, *s));
}
if is_new {
let status_label = task
.labels
.iter()
.find(|l| l.starts_with("status:"))
.cloned()
.unwrap_or_default();
accepted_by_title
.entry(task.title.clone())
.or_insert(DuplicateTarget {
id: task.id.0.clone(),
status_label,
});
if let Err(e) = backend.acknowledge_issue(&task.id).await {
tracing::debug!(
task_id = task.id.0,
err = %e,
"ingest: acknowledgment failed"
);
}
let task_status = status.unwrap_or(Status::New);
match backend.sync_to_project(&task.id, task_status).await {
Ok(Some(node_id)) => {
new_task_node_ids.push((store_id, node_id));
}
Ok(None) => {
}
Err(e) => {
tracing::debug!(
task_id = task.id.0,
err = %e,
"ingest: project board sync failed"
);
}
}
}
}
Err(e) => {
tracing::debug!(task_id = task.id.0, ?e, "ingest: upsert failed");
}
}
}
if !id_status_pairs.is_empty() {
let all_ids: Vec<i64> = id_status_pairs.iter().map(|(id, _)| *id).collect();
let existing_map = match store.get_batch(&all_ids).await {
Ok(map) => map,
Err(e) => {
tracing::warn!(err = %e, task_count = all_ids.len(), "ingest: get_batch failed — skipping initial status sync for newly ingested tasks this tick");
return Ok(());
}
};
for (store_id, status) in id_status_pairs {
if let Some(existing) = existing_map.get(&store_id)
&& existing.status == TaskStatus::New
{
let db_status = status_to_task_status(status);
if db_status != TaskStatus::New
&& let Err(e) = store.update_status(store_id, db_status).await
{
tracing::debug!(?e, "ingest: status sync failed");
}
}
}
}
if !new_task_node_ids.is_empty() {
let node_ids: Vec<String> = new_task_node_ids.iter().map(|(_, n)| n.clone()).collect();
match backend.get_project_item_estimates(&node_ids).await {
Ok(estimates) => {
for (store_id, node_id) in new_task_node_ids {
if let Some(&estimate) = estimates.get(&node_id)
&& estimate > 0
&& let Err(e) = store.set_estimate_if_zero(store_id, estimate).await
{
tracing::debug!(
store_id,
estimate,
err = %e,
"ingest: failed to set estimate from project"
);
}
}
}
Err(e) => {
tracing::debug!(
err = %e,
"ingest: project estimate fetch failed — skipping estimate sync this tick"
);
}
}
}
Ok(())
}
#[cfg(test)]
mod tests;
#[cfg(test)]
mod unblock_tests;
#[cfg(test)]
mod comments_tests;