Skip to main content

orch/engine/
mod.rs

1//! Engine — the core orchestration loop.
2//!
3//! The engine is the central coordinator. It owns the tick loop, backend
4//! connections, channel registry, transport layer, and tmux session manager.
5//!
6//! Submodules handle the heavy lifting:
7//! - [`tick`] — core tick phases (session polling, stuck recovery, routing, dispatch)
8//! - [`sync`] — periodic sync operations (cleanup, PR review, mentions)
9//! - [`review`] — PR review pipeline (review agent, auto-merge, change handling)
10//! - [`cleanup`] — worktree cleanup and merged-PR detection
11//!
12//! This file contains struct definitions, project initialization, and the
13//! main event loop (`serve()`).
14
15pub mod auto_merge;
16pub mod channel_handler;
17pub mod ci_bootstrap;
18pub mod cleanup;
19pub mod commands;
20pub mod cooldown;
21pub mod dependencies;
22pub mod dispatch_guard;
23pub mod events;
24pub mod jobs;
25pub mod priority;
26pub mod push_retry;
27pub mod relay;
28pub mod review;
29pub mod review_gate;
30pub mod review_poll;
31pub mod review_shipcheck;
32pub mod router;
33pub mod runner;
34pub mod subscribers;
35pub mod suspend;
36pub mod sync;
37pub mod tasks;
38pub mod tick;
39
40/// Standard Orch attribution footer for issue bodies, PR bodies, and comments.
41///
42/// Append to every user-visible string posted to GitHub so activity is
43/// clearly attributed to orch rather than appearing as a human post.
44use crate::backends::{ExternalBackend, ExternalId};
45use crate::channels::capture::CaptureService;
46use crate::channels::discord_ws::DiscordGateway;
47use crate::channels::github::start_webhook_server;
48use crate::channels::notification::NotificationLevel;
49use crate::channels::routing::{ChannelRouter, GlobalChannelConfig, ProjectChannelConfig};
50use crate::channels::slack::SlackChannel;
51use crate::channels::telegram::TelegramChannel;
52use crate::channels::tmux::TmuxChannel;
53use crate::channels::transport::Transport;
54use crate::channels::{Channel, ChannelRegistry, IncomingMessage, OutgoingMessage};
55use crate::cmd::CommandErrorContext;
56use crate::config;
57use crate::engine::cleanup::{
58    local_branch_exists, remove_worktree_and_branch, remove_worktree_and_branch_for,
59};
60use crate::engine::router::{Router, RouterConfig};
61use crate::engine::runner::worktree::{
62    abort_worktree_rebase, list_project_worktrees, task_id_from_worktree_name,
63    validate_worktree_gitdir,
64};
65use crate::engine::tasks::TaskManager;
66use crate::github::http::{GhHttp, rate_limit_metrics};
67use crate::repo_context::REPO_CONTEXT;
68use crate::store::{TaskStatus, TaskStore};
69use crate::tmux::TmuxManager;
70use runner::WeightSignal;
71// AtomicBool/Ordering removed — shutdown is now immediate (reset tasks + break)
72use crate::backends::Status;
73use crate::backends::github::GitHubBackend;
74use crate::channels::github;
75use crate::channels::github::is_transient_bind_error;
76use crate::channels::github::webhook_backoff_delay;
77use crate::channels::notification;
78use crate::channels::notification::TaskNotification;
79use crate::channels::transport;
80use crate::cluster;
81use crate::cluster::local;
82use crate::engine::cooldown as engine_cooldown;
83use crate::engine::review as engine_review;
84use crate::engine::runner::git_ops;
85use crate::engine::runner::worktree;
86use crate::engine::suspend as engine_suspend;
87use crate::engine::suspend::SuspendDetector;
88use crate::engine::tasks as engine_tasks;
89use crate::github::app;
90use crate::home;
91use crate::secret_ref;
92use crate::store;
93use crate::ui;
94use crate::webhook_status::WebhookStatus;
95use futures::future;
96use std::collections::HashMap;
97use std::collections::HashSet;
98use std::fmt;
99use std::fmt::Debug;
100use std::fmt::Formatter;
101use std::fs;
102use std::path::Path;
103use std::path::PathBuf;
104use std::sync::Arc;
105use std::sync::atomic::AtomicBool;
106use std::sync::atomic::AtomicU64;
107use std::sync::atomic::Ordering;
108use std::time as std_time;
109use std::time::Duration;
110use std::time::Instant;
111use std::time::SystemTime;
112use tokio::process::Command;
113use tokio::signal;
114use tokio::signal::unix;
115use tokio::signal::unix::SignalKind;
116use tokio::sync::Mutex;
117use tokio::sync::broadcast::Sender;
118use tokio::sync::broadcast::error::RecvError;
119use tokio::sync::mpsc::Receiver;
120use tokio::sync::{Notify, RwLock, Semaphore, mpsc};
121use tokio::task;
122use tokio::time;
123
124pub fn orch_footer() -> &'static str {
125    "\n\n---\n*Posted by [Orch](https://github.com/gabrielkoerich/orch)*"
126}
127
128/// Build a standard attribution footer for GitHub comments posted by orch bots.
129///
130/// `verb` is "Created", "Reviewed", or "Commented" depending on context.
131pub fn attribution_footer(verb: &str, agent: &str, model: Option<&str>) -> String {
132    match model {
133        Some(m) => format!(
134            "\n\n---\n*{verb} by {agent}[bot] via [Orch](https://github.com/gabrielkoerich/orch) using `{m}`*"
135        ),
136        None => format!(
137            "\n\n---\n*{verb} by {agent}[bot] via [Orch](https://github.com/gabrielkoerich/orch)*"
138        ),
139    }
140}
141
142/// Lightweight reference tuple for channel message handling.
143type EngineRef = (
144    String,
145    Arc<dyn ExternalBackend>,
146    Arc<TaskManager>,
147    Option<Arc<TaskStore>>,
148);
149
150/// Per-project engine state.
151///
152/// Each project has its own backend, task runner, and task manager,
153/// but they share the global tmux manager, transport, and semaphore.
154#[derive(Clone)]
155pub struct ProjectEngine {
156    pub repo: String,
157    pub project_dir: PathBuf,
158    pub backend: Arc<dyn ExternalBackend>,
159    pub task_manager: Arc<TaskManager>,
160    pub runner: Arc<runner::TaskRunner>,
161    pub store: Arc<TaskStore>,
162}
163
164impl Debug for ProjectEngine {
165    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
166        f.debug_struct("ProjectEngine")
167            .field("repo", &self.repo)
168            .field("project_dir", &self.project_dir)
169            .finish_non_exhaustive()
170    }
171}
172
173/// A restart ends every review session, so an `in_review` task with no session has no reviewer.
174/// Hand it back to `needs_review` at start, so it does not wait for the stuck-review recovery
175pub(crate) async fn reset_orphaned_reviews(
176    task_manager: &TaskManager,
177    store: &Arc<TaskStore>,
178    repo: &str,
179    tmux: &TmuxManager,
180) -> usize {
181    // The store holds external and internal tasks, the second list is the legacy internal table
182    let mut ids: Vec<ExternalId> = store
183        .list_by_status(repo, TaskStatus::InReview)
184        .await
185        .unwrap_or_default()
186        .iter()
187        .map(|t| engine_tasks::store_task_to_external(t).id)
188        .collect();
189    if let Ok(legacy) = task_manager
190        .list_internal_by_status(TaskStatus::InReview)
191        .await
192    {
193        ids.extend(legacy.into_iter().map(|t| t.id));
194    }
195    ids.sort_by(|a, b| a.0.cmp(&b.0));
196    ids.dedup_by(|a, b| a.0 == b.0);
197
198    let mut reset = 0;
199    for id in &ids {
200        let session = tmux.session_name(repo, &format!("{}-review", id.0));
201
202        if tmux.session_exists(&session).await {
203            continue;
204        }
205
206        if let Err(e) = task_manager
207            .update_task_status(id, Status::NeedsReview)
208            .await
209        {
210            tracing::warn!(
211                task_id = id.0,
212                ?e,
213                "failed to reset an orphaned review at start"
214            );
215            continue;
216        }
217
218        if let Ok(Some(store_id)) = store.resolve_task_id(repo, &id.0).await {
219            let reason = "startup: review session gone, reset to needs_review";
220
221            if let Err(e) = store
222                .finalize_incomplete_runs(store_id, "aborted", reason)
223                .await
224            {
225                tracing::warn!(
226                    task_id = id.0,
227                    ?e,
228                    "failed to finalize the orphaned review's runs"
229                );
230            }
231        }
232
233        reset += 1;
234    }
235
236    reset
237}
238
239/// Starts the relay client when `relay.url` is set. The token is read in the background, so a
240/// passbox prompt nobody answers never holds up the engine
241fn start_relay_client(store: &Arc<TaskStore>, notify: &Arc<Notify>) {
242    // The global config only: a project file must never point a secret at its own endpoint
243    let url = config::get_global("relay.url").unwrap_or_default();
244    let store = Arc::clone(store);
245
246    if url.is_empty() {
247        return;
248    }
249
250    let token_ref = config::get_global("relay.token").unwrap_or_default();
251    let notify = Arc::clone(notify);
252    tokio::spawn(async move {
253        let mut warned = false;
254        let token = loop {
255            let reference = token_ref.clone();
256            let read = task::spawn_blocking(move || secret_ref::resolve(&reference))
257                .await
258                .map_err(anyhow::Error::from)
259                .and_then(|r| r);
260
261            match read {
262                Ok(token) if !token.is_empty() => break token,
263                Ok(_) => {
264                    tracing::warn!("relay.token is empty, the relay client stays off");
265                    return;
266                }
267                Err(e) if !warned => {
268                    tracing::warn!(err = %e, "could not read relay.token, retrying every 5 minutes");
269                    warned = true;
270                }
271                Err(_) => {}
272            }
273
274            time::sleep(Duration::from_secs(300)).await;
275        };
276
277        relay::run(url, token, store, notify).await;
278    });
279}
280
281/**
282 * Runs `sync_tick` for every project at once, then the tail that is the same for all of them,
283 * in a spawned task. Sync time is the slowest project instead of the sum of all of them
284 * (#3864). The caller must already hold `sync_in_progress`; this releases it when done
285 **/
286#[expect(
287    clippy::too_many_arguments,
288    reason = "the spawned task takes its own handle to each piece of engine state"
289)]
290fn spawn_sync_pass(
291    project_engines: &[ProjectEngine],
292    tmux: &Arc<TmuxManager>,
293    engine_config: &EngineConfig,
294    router: &Arc<RwLock<Router>>,
295    dispatching: &Arc<dashmap::DashMap<String, String>>,
296    auto_merge_in_flight: &Arc<dashmap::DashSet<String>>,
297    sync_in_progress: &Arc<AtomicBool>,
298    trigger: &'static str,
299) {
300    let engines = project_engines.to_vec();
301    let tmux = Arc::clone(tmux);
302    let engine_config = engine_config.clone();
303    let router = Arc::clone(router);
304    let dispatching = Arc::clone(dispatching);
305    let auto_merge_in_flight = Arc::clone(auto_merge_in_flight);
306    let guard = Arc::clone(sync_in_progress);
307
308    tokio::spawn(async move {
309        let started = Instant::now();
310        let mut passes = Vec::with_capacity(engines.len());
311        for engine in &engines {
312            passes.push(REPO_CONTEXT.scope(
313                engine.repo.clone(),
314                sync_one_project(
315                    engine,
316                    &tmux,
317                    &engine_config,
318                    &router,
319                    &dispatching,
320                    &auto_merge_in_flight,
321                ),
322            ));
323        }
324        future::join_all(passes).await;
325
326        sync::sync_shared_tail(&router, engines.first().map(|e| &e.store)).await;
327        tracing::info!(
328            trigger,
329            elapsed_ms = u64::try_from(started.elapsed().as_millis()).unwrap_or(u64::MAX),
330            "sync tick complete"
331        );
332        guard.store(false, Ordering::Release);
333    });
334}
335
336async fn sync_one_project(
337    engine: &ProjectEngine,
338    tmux: &Arc<TmuxManager>,
339    engine_config: &EngineConfig,
340    router: &Arc<RwLock<Router>>,
341    dispatching: &Arc<dashmap::DashMap<String, String>>,
342    auto_merge_in_flight: &Arc<dashmap::DashSet<String>>,
343) {
344    if let Err(e) = Box::pin(sync::sync_tick(
345        &engine.backend,
346        tmux,
347        &engine.repo,
348        engine_config,
349        router,
350        &engine.task_manager,
351        &engine.store,
352        dispatching,
353        auto_merge_in_flight,
354    ))
355    .await
356    {
357        tracing::error!(repo = %engine.repo, ?e, "sync tick failed for project");
358    }
359}
360
361/// Milliseconds a group of tick or sync phases may take before it is worth an operator's eye
362const SLOW_PHASE_GROUP_MS: u128 = 1_000;
363
364/**
365 * Logs how long each phase of a tick or sync group took, for one project. Tick and sync time
366 * grows with the project count, and the totals alone did not say which project or phase spent
367 * it (#3864). Info once a group crosses `SLOW_PHASE_GROUP_MS`, so a quiet install stays quiet
368 **/
369pub(crate) fn log_phase_timings(group: &str, repo: &str, phases: &[(&str, u128)]) {
370    let total_ms: u128 = phases.iter().map(|(_, ms)| ms).sum();
371    if total_ms >= SLOW_PHASE_GROUP_MS {
372        tracing::info!(group, repo, total_ms, ?phases, "phase timings");
373    } else {
374        tracing::debug!(group, repo, total_ms, ?phases, "phase timings");
375    }
376}
377
378/// Return the configured agent list from top-level `agents` in config.yml,
379/// falling back to [`router::config::DEFAULT_AGENTS`] when not set.
380///
381/// This is the canonical source of truth for "which agents does orch know
382/// about" — use it instead of `DEFAULT_AGENTS` directly so that
383/// Claude-compatible agents added via config (e.g. `olm`) are recognized
384/// without code changes.
385///
386/// The key is top-level (`agents:`) rather than nested (`engine.agents:`)
387/// because `config::get` serializes nested YAML arrays via `serde_norway::to_string`
388/// which produces YAML block format, not JSON — breaking downstream parsing.
389pub fn configured_agents() -> Vec<String> {
390    if let Ok(agents_str) = config::get("agents")
391        && !agents_str.is_empty()
392        && agents_str != "[]"
393    {
394        // config::get returns serde_norway::to_string for arrays, which is
395        // YAML block format ("- claude\n- codex\n..."). Parse as YAML first.
396        if let Ok(agents_arr) = serde_norway::from_str::<Vec<String>>(&agents_str)
397            && !agents_arr.is_empty()
398        {
399            return agents_arr;
400        }
401        // Fallback: try JSON (inline arrays) or comma-separated
402        if let Ok(agents_arr) = serde_json::from_str::<Vec<String>>(&agents_str)
403            && !agents_arr.is_empty()
404        {
405            return agents_arr;
406        }
407    }
408    router::config::DEFAULT_AGENTS
409        .iter()
410        .map(ToString::to_string)
411        .collect()
412}
413
414/// Seconds a live session may run past its own timeout before stuck recovery takes it. The
415/// `timeout` wrapper ends the agent at the limit, and the runner still collects the result
416pub const RUN_TIMEOUT_GRACE_SECS: u64 = 300;
417
418/// The limit the `timeout` wrapper puts on one agent run, per complexity
419#[derive(Clone, Copy, Debug, PartialEq, Eq)]
420pub struct RunTimeouts {
421    pub base: u64,
422    pub medium: u64,
423    pub complex: u64,
424}
425
426impl Default for RunTimeouts {
427    fn default() -> Self {
428        Self {
429            base: 1800,
430            medium: 1800,
431            complex: 1800,
432        }
433    }
434}
435
436impl RunTimeouts {
437    /// `workflow.timeout_seconds` (at least 1800), raised by
438    /// `workflow.timeout_by_complexity.{medium,complex}` but never below the base
439    pub fn from_config() -> Self {
440        let read =
441            |key: &str| -> Option<u64> { config::get(key).ok().and_then(|s| s.parse().ok()) };
442        let base = read("workflow.timeout_seconds").unwrap_or(1800).max(1800);
443        Self {
444            base,
445            medium: read("workflow.timeout_by_complexity.medium")
446                .unwrap_or(base)
447                .max(base),
448            complex: read("workflow.timeout_by_complexity.complex")
449                .unwrap_or(base)
450                .max(base),
451        }
452    }
453
454    pub fn for_complexity(&self, complexity: Option<&str>) -> u64 {
455        match complexity {
456            Some("complex") => self.complex,
457            Some("medium") => self.medium,
458            _ => self.base,
459        }
460    }
461}
462
463/// Engine configuration.
464#[derive(Clone, Debug)]
465pub struct EngineConfig {
466    /// Main tick interval
467    pub tick_interval: Duration,
468    /// GitHub sync interval (cleanup, PR review, mentions)
469    pub sync_interval: Duration,
470    /// Webhook health check interval (seconds)
471    pub webhook_health_check_interval: Option<Duration>,
472    /// Maximum parallel task executions
473    pub max_parallel: usize,
474    /// Stuck task timeout for tasks with an active tmux session (seconds)
475    pub stuck_timeout: u64,
476    /// Stuck task timeout for tasks with no active tmux session (seconds).
477    /// Shorter than `stuck_timeout` because no session means the agent has already exited.
478    pub no_session_stuck_timeout: u64,
479    /// Stuck task timeout for in_review tasks with no active tmux session (seconds).
480    /// Longer than `no_session_stuck_timeout` because a review agent ends its tmux session on
481    /// normal completion before its result reaches the engine. A shorter limit resets a task
482    /// whose finished review is still being delivered, and the result is dropped as stale.
483    /// `from_config` keeps it above the review agent's own timeout plus
484    /// `review::REVIEW_RESULT_DELIVERY_SECS`.
485    pub in_review_no_session_stuck_timeout: u64,
486    /// Per-run agent timeouts. A live session is not stuck before its own timeout runs out
487    pub run_timeouts: RunTimeouts,
488    /// Auto-create follow-up tasks when PR reviews request changes
489    pub auto_create_followup_on_changes: bool,
490    /// Graceful shutdown timeout — how long to wait for running agents before exiting.
491    pub graceful_shutdown_timeout: Duration,
492    /// Grace period before silence detection kicks in (seconds).
493    /// Allows time for agent startup, model download, API handshake.
494    pub silence_grace_period: u64,
495    /// Cooldown duration for a model detected as silent (seconds).
496    pub silence_cooldown: u64,
497    /// How often to check for a newer orch release and notify channels (seconds).
498    /// Set to 0 to disable. Default: 3600 (1 hour).
499    pub upgrade_check_interval: u64,
500    /// Seconds of continuous startup failure (GitHub unreachable / auth errors)
501    /// before the engine escalates from WARN to ERROR-level logging and sends a
502    /// one-time push notification to configured channels. Set to 0 to disable.
503    /// Default: 3600 (1 hour).
504    pub startup_failure_escalation_secs: u64,
505}
506
507impl Default for EngineConfig {
508    fn default() -> Self {
509        Self {
510            tick_interval: Duration::from_secs(10),
511            sync_interval: Duration::from_secs(45),
512            webhook_health_check_interval: Some(Duration::from_secs(60)),
513            max_parallel: 4,
514            stuck_timeout: 1800,
515            no_session_stuck_timeout: 600,
516            in_review_no_session_stuck_timeout: 1920,
517            run_timeouts: RunTimeouts::default(),
518            auto_create_followup_on_changes: true,
519            graceful_shutdown_timeout: Duration::from_secs(600),
520            silence_grace_period: 300,
521            silence_cooldown: 3600,
522            upgrade_check_interval: 3600,
523            startup_failure_escalation_secs: 3600,
524        }
525    }
526}
527
528impl EngineConfig {
529    /// Stuck limit for an in_progress task whose session is alive: `stuck_timeout`, raised to the
530    /// run's own timeout plus `RUN_TIMEOUT_GRACE_SECS`
531    pub fn live_session_stuck_limit(&self, complexity: Option<&str>) -> u64 {
532        self.stuck_timeout.max(
533            self.run_timeouts
534                .for_complexity(complexity)
535                .saturating_add(RUN_TIMEOUT_GRACE_SECS),
536        )
537    }
538
539    /// Load engine configuration from config files, falling back to defaults.
540    pub fn from_config() -> Self {
541        let mut config = Self::default();
542
543        if let Ok(val) = config::get("engine.tick_interval") {
544            if let Ok(secs) = val.parse::<u64>() {
545                config.tick_interval = Duration::from_secs(secs);
546            } else {
547                tracing::warn!(key = "engine.tick_interval", value = %val, default_secs = config.tick_interval.as_secs(), "invalid value for engine.tick_interval, using default");
548            }
549        }
550
551        if let Ok(val) = config::get("engine.sync_interval") {
552            if let Ok(secs) = val.parse::<u64>() {
553                config.sync_interval = Duration::from_secs(secs);
554            } else {
555                tracing::warn!(key = "engine.sync_interval", value = %val, default_secs = config.sync_interval.as_secs(), "invalid value for engine.sync_interval, using default");
556            }
557        }
558
559        if let Ok(val) = config::get("engine.max_parallel") {
560            if let Ok(n) = val.parse::<usize>() {
561                config.max_parallel = n;
562            } else {
563                tracing::warn!(key = "engine.max_parallel", value = %val, default = config.max_parallel, "invalid value for engine.max_parallel, using default");
564            }
565        }
566
567        if let Ok(val) = config::get("engine.stuck_timeout") {
568            if let Ok(secs) = val.parse::<u64>() {
569                config.stuck_timeout = secs;
570            } else {
571                tracing::warn!(key = "engine.stuck_timeout", value = %val, default_secs = config.stuck_timeout, "invalid value for engine.stuck_timeout, using default");
572            }
573        }
574
575        if let Ok(val) = config::get("engine.no_session_stuck_timeout") {
576            if let Ok(secs) = val.parse::<u64>() {
577                config.no_session_stuck_timeout = secs;
578            } else {
579                tracing::warn!(key = "engine.no_session_stuck_timeout", value = %val, default_secs = config.no_session_stuck_timeout, "invalid value for engine.no_session_stuck_timeout, using default");
580            }
581        }
582
583        let mut in_review_from_config = false;
584        if let Ok(val) = config::get("engine.in_review_no_session_stuck_timeout") {
585            if let Ok(secs) = val.parse::<u64>() {
586                config.in_review_no_session_stuck_timeout = secs;
587                in_review_from_config = true;
588            } else {
589                tracing::warn!(key = "engine.in_review_no_session_stuck_timeout", value = %val, default_secs = config.in_review_no_session_stuck_timeout, "invalid value for engine.in_review_no_session_stuck_timeout, using default");
590            }
591        }
592
593        // Keep the no-session review limit over the review agent's own timeout plus delivery
594        // time, whatever either is set to. Under that, recovery resets a task whose finished
595        // review is still on its way to the engine and the result is dropped as stale
596        let review_timeout = engine_review::review_timeout_seconds();
597        let floored = engine_review::no_session_review_floor(
598            config.in_review_no_session_stuck_timeout,
599            review_timeout,
600        );
601        if floored != config.in_review_no_session_stuck_timeout {
602            if in_review_from_config {
603                tracing::warn!(
604                    configured_secs = config.in_review_no_session_stuck_timeout,
605                    review_timeout_secs = review_timeout,
606                    raised_to_secs = floored,
607                    "engine.in_review_no_session_stuck_timeout is below the review timeout plus result delivery time, raising it"
608                );
609            } else {
610                tracing::debug!(
611                    review_timeout_secs = review_timeout,
612                    raised_to_secs = floored,
613                    "raised the default in_review no-session limit over a long review timeout"
614                );
615            }
616            config.in_review_no_session_stuck_timeout = floored;
617        }
618
619        config.run_timeouts = RunTimeouts::from_config();
620
621        if let Ok(val) = config::get("engine.webhook_health_check_interval") {
622            if let Ok(secs) = val.parse::<u64>() {
623                if secs > 0 {
624                    config.webhook_health_check_interval = Some(Duration::from_secs(secs));
625                } else {
626                    config.webhook_health_check_interval = None;
627                }
628            } else {
629                tracing::warn!(key = "engine.webhook_health_check_interval", value = %val, default = ?config.webhook_health_check_interval, "invalid value for engine.webhook_health_check_interval, using default");
630            }
631        }
632
633        if let Ok(val) = config::get("workflow.auto_create_followup_on_changes") {
634            config.auto_create_followup_on_changes = !val.eq_ignore_ascii_case("false");
635        }
636
637        if let Ok(val) = config::get("engine.graceful_shutdown_timeout") {
638            if let Ok(secs) = val.parse::<u64>() {
639                config.graceful_shutdown_timeout = Duration::from_secs(secs);
640            } else {
641                tracing::warn!(key = "engine.graceful_shutdown_timeout", value = %val, default_secs = config.graceful_shutdown_timeout.as_secs(), "invalid value for engine.graceful_shutdown_timeout, using default");
642            }
643        }
644
645        if let Ok(val) = config::get("engine.silence_grace_period") {
646            if let Ok(secs) = val.parse::<u64>() {
647                config.silence_grace_period = secs;
648            } else {
649                tracing::warn!(key = "engine.silence_grace_period", value = %val, default = config.silence_grace_period, "invalid value for engine.silence_grace_period, using default");
650            }
651        }
652
653        if let Ok(val) = config::get("engine.silence_cooldown") {
654            if let Ok(secs) = val.parse::<u64>() {
655                config.silence_cooldown = secs;
656            } else {
657                tracing::warn!(key = "engine.silence_cooldown", value = %val, default = config.silence_cooldown, "invalid value for engine.silence_cooldown, using default");
658            }
659        }
660
661        if let Ok(val) = config::get("engine.upgrade_check_interval") {
662            if let Ok(secs) = val.parse::<u64>() {
663                config.upgrade_check_interval = secs;
664            } else {
665                tracing::warn!(key = "engine.upgrade_check_interval", value = %val, default = config.upgrade_check_interval, "invalid value for engine.upgrade_check_interval, using default");
666            }
667        }
668
669        if let Ok(val) = config::get("engine.startup_failure_escalation_secs") {
670            if let Ok(secs) = val.parse::<u64>() {
671                config.startup_failure_escalation_secs = secs;
672            } else {
673                tracing::warn!(
674                    key = "engine.startup_failure_escalation_secs",
675                    value = %val,
676                    default_secs = config.startup_failure_escalation_secs,
677                    "invalid value for engine.startup_failure_escalation_secs, using default"
678                );
679            }
680        }
681
682        config
683    }
684}
685
686/// KV key holding the epoch second the GitHub core window resets, so `orch task status`
687/// can show the limit from a separate process.
688pub const RATE_LIMITED_UNTIL_KEY: &str = "github:rate_limited_until";
689
690/// Format an epoch second as a local `HH:MM:SS` for operator-facing output.
691pub fn format_rate_limited_until(reset_at: i64) -> String {
692    chrono::DateTime::from_timestamp(reset_at, 0).map_or_else(
693        || reset_at.to_string(),
694        |t| {
695            t.with_timezone(&chrono::Local)
696                .format("%H:%M:%S %Z")
697                .to_string()
698        },
699    )
700}
701
702/// Announce a GitHub rate-limit window on every operator surface: the log (`orch log`),
703/// a KV flag (`orch task status`) and the channels (Telegram, Discord).
704async fn report_rate_limited(
705    reset_at: i64,
706    store: Option<&Arc<TaskStore>>,
707    transport: Option<&Arc<Transport>>,
708) {
709    let m = rate_limit_metrics();
710    let until = format_rate_limited_until(reset_at);
711    tracing::warn!(
712        reset_at,
713        rate_limit_hits = m.hits,
714        skipped_calls = m.skipped_calls,
715        five_xx_hits = m.five_xx_hits,
716        rest_remaining = ?m.rest_remaining,
717        graphql_remaining = ?m.graphql_remaining,
718        "GitHub API rate limited until {until} — skipping GitHub calls, local work continues"
719    );
720
721    if let Some(store) = store
722        && let Err(e) = store
723            .kv_set(RATE_LIMITED_UNTIL_KEY, &reset_at.to_string())
724            .await
725    {
726        tracing::warn!(err = %e, "failed to persist GitHub rate-limit flag");
727    }
728
729    if let Some(transport) = transport {
730        transport.push_notification(TaskNotification {
731            task_id: "github".to_string(),
732            title: "GitHub rate limit".to_string(),
733            status: "blocked".to_string(),
734            agent: "orch".to_string(),
735            duration_seconds: 0.0,
736            summary: format!(
737                "rate limited until {until}. GitHub calls are skipped until then; running agents and local work continue."
738            ),
739            repo: None,
740            notify_target: None,
741            pr_number: None,
742        });
743    }
744}
745
746/// Grow or shrink the dispatch pool toward `target` without cancelling running work.
747///
748/// Growing adds permits at once. Shrinking can only take back the permits that
749/// are free right now, so the return value is the limit actually in force; call
750/// again as running tasks release theirs
751fn resize_dispatch_pool(semaphore: &Semaphore, in_force: usize, target: usize) -> usize {
752    if target > in_force {
753        semaphore.add_permits(target - in_force);
754        return target;
755    }
756
757    if target < in_force {
758        return in_force - semaphore.forget_permits(in_force - target);
759    }
760
761    in_force
762}
763
764/// Config subtrees the engine only reads at startup: the channel gateways hold
765/// long-lived websockets and the webhook listener binds its port once.
766const RESTART_ONLY_KEYS: [&str; 3] = ["channels", "webhook", "relay"];
767
768/// Read the restart-only subtrees so a later reload can tell they changed.
769fn restart_only_snapshot() -> Vec<Option<String>> {
770    RESTART_ONLY_KEYS
771        .iter()
772        .map(|key| config::get(key).ok())
773        .collect()
774}
775
776/// Report the restart-only keys an edit changed. Only the key is logged, never
777/// the value, because `channels.*` holds bot tokens
778fn log_restart_only_changes(old: &[Option<String>], new: &[Option<String>]) -> Vec<&'static str> {
779    let changed: Vec<&'static str> = RESTART_ONLY_KEYS
780        .iter()
781        .zip(old)
782        .zip(new)
783        .filter(|((_, old), new)| old != new)
784        .map(|((key, _), _)| *key)
785        .collect();
786
787    for key in &changed {
788        tracing::info!(key = key, "config changed, takes effect after restart");
789    }
790
791    changed
792}
793
794/// Log every engine setting whose value changed across a config reload, so
795/// `orch log` shows what the operator's edit actually did.
796fn log_engine_config_changes(old: &EngineConfig, new: &EngineConfig) {
797    macro_rules! log_changed {
798        ($($key:literal => $field:ident),+ $(,)?) => {
799            $(if old.$field != new.$field {
800                tracing::info!(key = $key, old = ?old.$field, new = ?new.$field, "config changed");
801            })+
802        };
803    }
804
805    log_changed!(
806        "engine.tick_interval" => tick_interval,
807        "engine.sync_interval" => sync_interval,
808        "engine.webhook_health_check_interval" => webhook_health_check_interval,
809        "engine.max_parallel" => max_parallel,
810        "engine.stuck_timeout" => stuck_timeout,
811        "engine.no_session_stuck_timeout" => no_session_stuck_timeout,
812        "engine.in_review_no_session_stuck_timeout" => in_review_no_session_stuck_timeout,
813        "engine.graceful_shutdown_timeout" => graceful_shutdown_timeout,
814        "engine.silence_grace_period" => silence_grace_period,
815        "engine.silence_cooldown" => silence_cooldown,
816        "engine.upgrade_check_interval" => upgrade_check_interval,
817        "engine.startup_failure_escalation_secs" => startup_failure_escalation_secs,
818        "workflow.auto_create_followup_on_changes" => auto_create_followup_on_changes,
819    );
820}
821
822fn should_run_upgrade_check(
823    last_upgrade_check: Option<Instant>,
824    upgrade_check_interval: u64,
825) -> bool {
826    if upgrade_check_interval == 0 {
827        return false;
828    }
829
830    last_upgrade_check
831        .is_none_or(|last| last.elapsed() >= Duration::from_secs(upgrade_check_interval))
832}
833
834/// Connect one project's backend and build its task manager and runner over
835/// the shared store.
836///
837/// `event_tx` wires status transitions into the event bus; pass `None` during
838/// startup, where the bus does not exist yet and task managers are rebuilt
839/// once it does
840async fn connect_project_engine(
841    repo: String,
842    project_dir: PathBuf,
843    store: Arc<TaskStore>,
844    event_tx: Option<Sender<events::TaskEvent>>,
845) -> anyhow::Result<ProjectEngine> {
846    let backend: Arc<dyn ExternalBackend> = Arc::new(GitHubBackend::new(repo.clone())?);
847
848    // A failed health check (rate limit, network, auth) must not drop the project: it would have
849    // no review subscriber until the next restart. Later ticks hit the API again and retry on
850    // their own, so build the engine and warn while the check keeps failing.
851    match backend.health_check().await {
852        Ok(()) => tracing::info!(repo = %repo, backend = backend.name(), "backend connected"),
853        Err(e) => tracing::warn!(
854            repo = %repo,
855            error = %e,
856            "GitHub health check failed for {repo}, starting engine anyway (rate limited, offline or bad credentials?)"
857        ),
858    }
859
860    let task_manager = Arc::new(match event_tx {
861        Some(tx) => {
862            TaskManager::with_events(Arc::clone(&backend), Arc::clone(&store), repo.clone(), tx)
863        }
864        None => TaskManager::with_store(Arc::clone(&backend), Arc::clone(&store), repo.clone()),
865    });
866    let runner = Arc::new(runner::TaskRunner::new(repo.clone()).with_store(Arc::clone(&store)));
867
868    Ok(ProjectEngine {
869        repo,
870        project_dir,
871        backend,
872        task_manager,
873        runner,
874        store,
875    })
876}
877
878/**
879 * Split the configured project list into the ones not running yet and the names of the ones that
880 * went away. Kept separate from `sync_project_engines` so the comparison can be tested without a
881 * GitHub backend: getting it wrong either spawns a second set of subscribers for a project already
882 * running, or silently never picks a new one up
883 **/
884fn diff_configured_projects(
885    configured: &[(String, PathBuf)],
886    active_repos: &HashSet<String>,
887) -> (Vec<(String, PathBuf)>, Vec<String>) {
888    let configured_repos: HashSet<&str> = configured.iter().map(|(r, _)| r.as_str()).collect();
889
890    let added = configured
891        .iter()
892        .filter(|(repo, _)| !active_repos.contains(repo))
893        .cloned()
894        .collect();
895    let removed = active_repos
896        .iter()
897        .filter(|repo| !configured_repos.contains(repo.as_str()))
898        .cloned()
899        .collect();
900
901    (added, removed)
902}
903
904/// Pick up projects added to `projects:` since startup, and report the ones
905/// that were removed.
906///
907/// A removed project keeps running until the next restart: its subscriber
908/// tasks and any agent it already dispatched outlive the config read
909#[expect(
910    clippy::too_many_arguments,
911    reason = "a new project engine needs every shared engine handle"
912)]
913async fn sync_project_engines(
914    store: &Arc<TaskStore>,
915    project_engines: &mut Vec<ProjectEngine>,
916    active_repos: &mut HashSet<String>,
917    event_bus: &events::EventBus,
918    tmux: &Arc<TmuxManager>,
919    capture: &Arc<CaptureService>,
920    semaphore: &Arc<Semaphore>,
921    review_semaphore: &Arc<Semaphore>,
922    weight_tx: &mpsc::Sender<WeightSignal>,
923    router: &Arc<RwLock<Router>>,
924    dispatching: &Arc<dashmap::DashMap<String, String>>,
925) {
926    let configured = match config::get_projects_with_paths() {
927        Ok(p) if !p.is_empty() => p,
928        Ok(_) => return,
929        Err(e) => {
930            tracing::warn!(err = %e, "could not re-read the project list, keeping the current one");
931            return;
932        }
933    };
934    let (added, removed) = diff_configured_projects(&configured, active_repos);
935    for repo in removed {
936        tracing::info!(
937            repo = %repo,
938            "project removed from config, takes effect after restart"
939        );
940    }
941
942    for (repo, project_dir) in added {
943        config::watch(&project_dir.join(".orch.yml"));
944        let engine = match connect_project_engine(
945            repo.clone(),
946            project_dir,
947            Arc::clone(store),
948            Some(event_bus.sender()),
949        )
950        .await
951        {
952            Ok(engine) => engine,
953            Err(e) => {
954                // A failed health check no longer lands here, so this is a backend that could
955                // not be built at all, such as a repo slug that is not `owner/name`
956                tracing::warn!(
957                    repo = %repo,
958                    err = %e,
959                    "project added to config but its backend could not be built, retrying on the next config change"
960                );
961                continue;
962            }
963        };
964
965        subscribers::dispatch::spawn(
966            event_bus.subscribe(),
967            Arc::clone(&engine.backend),
968            Arc::clone(tmux),
969            Arc::clone(&engine.runner),
970            Arc::clone(capture),
971            Arc::clone(semaphore),
972            Arc::clone(&engine.task_manager),
973            weight_tx.clone(),
974            Arc::clone(router),
975            Arc::clone(dispatching),
976            Arc::clone(&engine.store),
977            engine.repo.clone(),
978        );
979        subscribers::review::spawn(
980            event_bus.subscribe(),
981            Arc::clone(&engine.backend),
982            Arc::clone(tmux),
983            Arc::clone(review_semaphore),
984            Arc::clone(&engine.task_manager),
985            Arc::clone(router),
986            Arc::clone(dispatching),
987            Arc::clone(&engine.store),
988            engine.repo.clone(),
989        );
990        subscribers::unblock::spawn(
991            event_bus.subscribe(),
992            Arc::clone(&engine.backend),
993            Arc::clone(&engine.task_manager),
994            Arc::clone(&engine.store),
995            engine.repo.clone(),
996        );
997
998        active_repos.insert(repo.clone());
999        project_engines.push(engine);
1000        tracing::info!(repo = %repo, "project added from config, now syncing");
1001    }
1002}
1003
1004/// Initialize all project engines from config.
1005///
1006/// Returns a vector of ProjectEngine, one for each configured project.
1007/// The task store every project shares, with the cluster and its local write endpoint when on
1008async fn open_shared_store() -> anyhow::Result<Arc<TaskStore>> {
1009    let store = Arc::new(TaskStore::open(&store::default_db_path().await?).await?);
1010    if cluster::enabled() {
1011        cluster::start(&store, &home::orch_home()?.join("raft.db")).await?;
1012        local::serve((*store).clone()).await?;
1013    }
1014    Ok(store)
1015}
1016
1017/// One engine per configured project. None is fine, the web UI adds the first one and
1018/// `sync_project_engines` starts it
1019async fn init_project_engines(store: &Arc<TaskStore>) -> anyhow::Result<Vec<ProjectEngine>> {
1020    let projects = config::get_projects_with_paths()?;
1021    let repos: Vec<&str> = projects.iter().map(|(r, _)| r.as_str()).collect();
1022    tracing::info!(repos = ?repos, "loading projects from config");
1023
1024    if projects.is_empty() {
1025        tracing::info!("no projects yet, add one with `orch ui` or `orch project add`");
1026    }
1027
1028    let mut engines = Vec::new();
1029    for (repo, project_dir) in projects {
1030        tracing::info!(repo = %repo, "initializing project engine");
1031        config::watch(&project_dir.join(".orch.yml"));
1032
1033        engines.push(connect_project_engine(repo, project_dir, Arc::clone(store), None).await?);
1034    }
1035
1036    Ok(engines)
1037}
1038
1039async fn reconcile_startup_worktrees(project_engines: &[ProjectEngine]) -> anyhow::Result<()> {
1040    for engine in project_engines {
1041        let repo_root = worktree::resolve_main_repo(&engine.project_dir).await;
1042        let repo_root_path = PathBuf::from(&repo_root);
1043        let default_branch = worktree::detect_default_branch(&repo_root_path).await;
1044        let worktrees = list_project_worktrees(&repo_root_path).await?;
1045
1046        // Fetch refs once for all worktrees in this project, so rebase below
1047        // operates on up-to-date origin/* refs without N sequential fetches.
1048        if let Err(e) = git_ops::git()
1049            .await
1050            .args(["-C", &repo_root.to_string_lossy(), "fetch", "origin"])
1051            .output_with_context()
1052            .await
1053        {
1054            tracing::warn!(repo = %engine.repo, err = %e, "startup git fetch origin failed, rebases may use stale refs");
1055        }
1056
1057        let mut orphans_removed: u32 = 0;
1058        let mut invalid_removed: u32 = 0;
1059        let mut cleaned: u32 = 0;
1060        let mut valid_kept: u32 = 0;
1061
1062        for worktree_dir in worktrees {
1063            let Some(name) = worktree_dir.file_name().and_then(|n| n.to_str()) else {
1064                continue;
1065            };
1066
1067            // Validate gitdir link before doing anything else — a broken .git file
1068            // means git commands on this worktree will fail with "not a git repository".
1069            if !validate_worktree_gitdir(&worktree_dir).await {
1070                tracing::warn!(repo = %engine.repo, worktree = %worktree_dir.display(), "worktree has invalid or missing gitdir, removing");
1071                remove_worktree_and_branch(name, &worktree_dir, Some(name), &repo_root_path, false)
1072                    .await;
1073                invalid_removed += 1;
1074                continue;
1075            }
1076
1077            let Some(task_id) = task_id_from_worktree_name(name) else {
1078                tracing::info!(repo = %engine.repo, worktree = %worktree_dir.display(), "orphan worktree without task id, removing");
1079                remove_worktree_and_branch(name, &worktree_dir, Some(name), &repo_root_path, false)
1080                    .await;
1081                orphans_removed += 1;
1082                continue;
1083            };
1084
1085            let Some(store_id) = engine.store.resolve_task_id(&engine.repo, &task_id).await? else {
1086                tracing::info!(repo = %engine.repo, task_id = %task_id, worktree = %worktree_dir.display(), "worktree has no matching task, removing");
1087                remove_worktree_and_branch(
1088                    &task_id,
1089                    &worktree_dir,
1090                    Some(name),
1091                    &repo_root_path,
1092                    false,
1093                )
1094                .await;
1095                orphans_removed += 1;
1096                continue;
1097            };
1098
1099            let task = engine.store.get(store_id).await?;
1100            let branch_name = if task.branch.is_empty() {
1101                name
1102            } else {
1103                task.branch.as_str()
1104            };
1105
1106            match task.status {
1107                TaskStatus::New
1108                | TaskStatus::Routed
1109                | TaskStatus::InProgress
1110                | TaskStatus::NeedsReview
1111                | TaskStatus::InReview => {
1112                    let keep_conflict = worktree::keep_conflicted_rebase(
1113                        task.merge_conflict_retries,
1114                        task.pr_number,
1115                    );
1116                    if keep_conflict && git_ops::rebase_in_progress(&worktree_dir).await {
1117                        tracing::info!(
1118                            repo = %engine.repo,
1119                            task_id = %task_id,
1120                            worktree = %worktree_dir.display(),
1121                            "keeping the conflicted rebase auto_merge left for the agent"
1122                        );
1123                        valid_kept += 1;
1124                        continue;
1125                    }
1126                    tracing::info!(repo = %engine.repo, task_id = %task_id, worktree = %worktree_dir.display(), "rebasing startup worktree");
1127                    let origin_branch = format!("origin/{default_branch}");
1128                    let outcome = git_ops::stash_rebase_restore(
1129                        &worktree_dir,
1130                        &origin_branch,
1131                        git_ops::StashRebaseOpts {
1132                            abort_on_failure: false,
1133                        },
1134                    )
1135                    .await;
1136                    let conflict_left = git_ops::rebase_in_progress(&worktree_dir).await;
1137                    let failed_err = match outcome {
1138                        Ok(
1139                            git_ops::RebaseOutcome::Succeeded | git_ops::RebaseOutcome::Skipped(_),
1140                        ) => None,
1141                        Ok(git_ops::RebaseOutcome::Failed(e)) if keep_conflict && conflict_left => {
1142                            tracing::info!(
1143                                repo = %engine.repo,
1144                                task_id = %task_id,
1145                                err = %e,
1146                                "startup rebase hit a content conflict, leaving it for the agent"
1147                            );
1148                            None
1149                        }
1150                        Ok(git_ops::RebaseOutcome::Failed(e)) => Some(e),
1151                        Err(e) => Some(e.to_string()),
1152                    };
1153                    if let Some(e) = failed_err {
1154                        reset_startup_task_after_failed_rebase(
1155                            engine,
1156                            &task_id,
1157                            &worktree_dir,
1158                            branch_name,
1159                            &repo_root_path,
1160                            task.pr_number.is_some(),
1161                            &e,
1162                        )
1163                        .await;
1164                        cleaned += 1;
1165                    } else {
1166                        valid_kept += 1;
1167                    }
1168                }
1169                // A blocked or waiting task resumes from this work, and without a PR the branch
1170                // may hold the only copy of it. An approved task's PR is updated on GitHub
1171                TaskStatus::Blocked | TaskStatus::Waiting | TaskStatus::Approved => {
1172                    valid_kept += 1;
1173                }
1174                TaskStatus::Done => {
1175                    tracing::info!(repo = %engine.repo, task_id = %task_id, worktree = %worktree_dir.display(), status = ?task.status, "terminal task worktree, removing");
1176                    remove_worktree_and_branch_for(
1177                        &task_id,
1178                        &worktree_dir,
1179                        Some(branch_name),
1180                        &repo_root_path,
1181                        false,
1182                        task.pr_number.is_some(),
1183                    )
1184                    .await;
1185                    cleaned += 1;
1186                }
1187            }
1188        }
1189
1190        tracing::info!(
1191            repo = %engine.repo,
1192            orphans_removed,
1193            invalid_removed,
1194            cleaned,
1195            valid_kept,
1196            "worktree reconciliation: {orphans_removed} orphans removed, {invalid_removed} invalid removed, {cleaned} cleaned, {valid_kept} valid kept"
1197        );
1198
1199        // Prune stale .git/worktrees/ entries whose directories no longer exist.
1200        // This handles entries left behind by crashes or manual directory removal.
1201        let prune = Command::new("git")
1202            .args(["-C", &repo_root.to_string_lossy(), "worktree", "prune"])
1203            .output_with_context()
1204            .await;
1205        match prune {
1206            Ok(output) if output.status.success() => {
1207                tracing::debug!(repo = %engine.repo, "startup worktree prune completed");
1208            }
1209            Ok(output) => {
1210                let stderr = String::from_utf8_lossy(&output.stderr);
1211                tracing::warn!(repo = %engine.repo, err = %stderr, "startup worktree prune failed");
1212            }
1213            Err(e) => {
1214                tracing::warn!(repo = %engine.repo, err = %e, "startup worktree prune failed");
1215            }
1216        }
1217    }
1218
1219    Ok(())
1220}
1221
1222async fn reset_startup_task_after_failed_rebase(
1223    engine: &ProjectEngine,
1224    task_id: &str,
1225    worktree_dir: &Path,
1226    branch_name: &str,
1227    repo_root: &Path,
1228    keep_remote: bool,
1229    err: &str,
1230) {
1231    tracing::warn!(repo = %engine.repo, task_id, err, "startup rebase failed, resetting task");
1232    abort_worktree_rebase(worktree_dir).await;
1233    remove_worktree_and_branch(
1234        task_id,
1235        worktree_dir,
1236        Some(branch_name),
1237        repo_root,
1238        keep_remote,
1239    )
1240    .await;
1241    let keep_branch = local_branch_exists(repo_root, branch_name).await;
1242    match engine.store.resolve_task_id(&engine.repo, task_id).await {
1243        Ok(Some(reset_id)) => {
1244            let result = if keep_branch {
1245                engine.store.reset_to_new_keep_branch(reset_id).await
1246            } else {
1247                engine.store.reset_to_new(reset_id).await
1248            };
1249            if let Err(e) = result {
1250                tracing::warn!(task_id, err = %e, "failed to reset task to new after rebase failure");
1251            }
1252        }
1253        Ok(None) => {
1254            tracing::warn!(task_id, "task not found in store during reconciliation");
1255        }
1256        Err(e) => {
1257            tracing::warn!(task_id, err = %e, "failed to resolve task ID during reconciliation");
1258        }
1259    }
1260    if let Err(e) = engine
1261        .task_manager
1262        .update_task_status(&ExternalId(task_id.to_string()), Status::New)
1263        .await
1264    {
1265        tracing::warn!(task_id, err = %e, "failed to update backend status during reconciliation");
1266    }
1267}
1268
1269/// Sync Fibonacci estimates stored in SQLite to the GitHub Projects board for
1270/// all tasks that have a positive estimate.
1271///
1272/// Runs once on startup to catch tasks that were routed before the estimate
1273/// sync feature was available or before `project_estimate_field_id` was
1274/// configured. The underlying mutations are idempotent, so re-syncing an
1275/// already-correct estimate is harmless.
1276async fn reconcile_startup_estimates(project_engines: &[ProjectEngine]) {
1277    for engine in project_engines {
1278        let tasks = match engine.store.list_external_tasks_with_estimates().await {
1279            Ok(t) => t,
1280            Err(e) => {
1281                tracing::warn!(repo = %engine.repo, err = %e, "startup estimate reconciliation: store query failed");
1282                continue;
1283            }
1284        };
1285
1286        if tasks.is_empty() {
1287            continue;
1288        }
1289
1290        let mut synced: u32 = 0;
1291        let mut failed: u32 = 0;
1292        for (external_id, estimate) in &tasks {
1293            match engine
1294                .backend
1295                .sync_estimate_to_project(&ExternalId(external_id.clone()), *estimate)
1296                .await
1297            {
1298                Ok(()) => synced += 1,
1299                Err(e) => {
1300                    tracing::debug!(
1301                        repo = %engine.repo,
1302                        task_id = %external_id,
1303                        estimate,
1304                        err = %e,
1305                        "startup estimate reconciliation: sync failed"
1306                    );
1307                    failed += 1;
1308                }
1309            }
1310        }
1311
1312        if synced > 0 || failed > 0 {
1313            tracing::info!(
1314                repo = %engine.repo,
1315                synced,
1316                failed,
1317                "startup estimate reconciliation complete"
1318            );
1319        }
1320    }
1321}
1322
1323/// Read per-project channel configuration from `.orch.yml`.
1324fn read_project_channel_config(project_dir: &Path) -> ProjectChannelConfig {
1325    let config_path = project_dir.join(".orch.yml");
1326    if !config_path.exists() {
1327        return ProjectChannelConfig::default();
1328    }
1329    let content = match fs::read_to_string(&config_path) {
1330        Ok(c) => c,
1331        Err(e) => {
1332            tracing::warn!(
1333                path = %config_path.display(),
1334                error = %e,
1335                "failed to read .orch.yml; channel config will use defaults"
1336            );
1337            return ProjectChannelConfig::default();
1338        }
1339    };
1340    let val: serde_norway::Value = match serde_norway::from_str(&content) {
1341        Ok(v) => v,
1342        Err(e) => {
1343            tracing::warn!(
1344                path = %config_path.display(),
1345                error = %e,
1346                "failed to parse .orch.yml; channel config will use defaults"
1347            );
1348            return ProjectChannelConfig::default();
1349        }
1350    };
1351    let Some(channels) = val.get("channels") else {
1352        return ProjectChannelConfig::default();
1353    };
1354    let get_str = |section: &str, key: &str| -> Option<String> {
1355        channels
1356            .get(section)
1357            .and_then(|s| s.get(key))
1358            .and_then(|v| v.as_str())
1359            .map(String::from)
1360    };
1361    ProjectChannelConfig {
1362        telegram_topic_id: get_str("telegram", "topic_id"),
1363        discord_channel_id: get_str("discord", "channel_id"),
1364    }
1365}
1366
1367/// Fetch the latest orch release tag from GitHub via the `gh` CLI (async).
1368///
1369/// Returns the version string (without the leading "v"), or `None` when the
1370/// check fails (no network, unauthenticated, `gh` not installed, etc.).
1371async fn fetch_latest_release_version() -> Option<String> {
1372    let output = Command::new("gh")
1373        .args([
1374            "api",
1375            "repos/gabrielkoerich/orch/releases/latest",
1376            "--jq",
1377            ".tag_name",
1378        ])
1379        .output()
1380        .await
1381        .ok()?;
1382    if !output.status.success() {
1383        return None;
1384    }
1385    let tag = String::from_utf8(output.stdout).ok()?;
1386    let version = tag.trim().trim_start_matches('v').to_string();
1387    if version.is_empty() {
1388        None
1389    } else {
1390        Some(version)
1391    }
1392}
1393
1394/// Check if a newer orch release is available and send channel notifications
1395/// asking the operator to upgrade manually. Upgrading is operator-only — the
1396/// service never runs `brew` itself.
1397///
1398/// Uses the store's KV to throttle checks (`upgrade:last_check_at`) and
1399/// deduplicate notifications (`upgrade:last_notified_version`). Each distinct
1400/// latest version is notified about once; the notification repeats only when
1401/// an even newer version is released.
1402async fn check_and_notify_upgrade(
1403    store: &Arc<TaskStore>,
1404    channels: &Arc<ChannelRegistry>,
1405    current_version: &str,
1406    check_interval: Duration,
1407) {
1408    let last_check_key = "upgrade:last_check_at";
1409    let last_notified_key = "upgrade:last_notified_version";
1410
1411    // Throttle: only check once per interval.
1412    let now = chrono::Utc::now().timestamp();
1413    if let Ok(Some(last_check_str)) = store.kv_get(last_check_key).await
1414        && let Ok(last_check) = last_check_str.parse::<i64>()
1415        && now - last_check < i64::try_from(check_interval.as_secs()).unwrap_or(i64::MAX)
1416    {
1417        return;
1418    }
1419
1420    // Record check timestamp (best-effort).
1421    let _ = store.kv_set(last_check_key, &now.to_string()).await;
1422
1423    let Some(latest) = fetch_latest_release_version().await else {
1424        tracing::debug!("upgrade check: failed to fetch latest release version");
1425        return;
1426    };
1427
1428    if latest == current_version {
1429        tracing::debug!(current = %current_version, "orch is up to date");
1430        return;
1431    }
1432
1433    tracing::warn!(
1434        current_version = %current_version,
1435        latest_version = %latest,
1436        "orch upgrade available"
1437    );
1438
1439    // Operator-only upgrade: send channel notifications so the operator can
1440    // upgrade on their own schedule. The service never runs brew itself.
1441
1442    // Deduplicate: don't re-notify for the same latest version.
1443    if let Ok(Some(last_notified)) = store.kv_get(last_notified_key).await
1444        && last_notified == latest
1445    {
1446        tracing::debug!(latest = %latest, "upgrade already notified");
1447        return;
1448    }
1449
1450    let msg_telegram = format!(
1451        "⚠️ <b>Orch Upgrade Available</b>\n\n\
1452         Service: <code>{current_version}</code>\n\
1453         Latest:  <code>{latest}</code>\n\n\
1454         Run: <code>brew update && brew upgrade orch && brew services restart orch</code>"
1455    );
1456
1457    let msg_discord = format!(
1458        "⚠️ **Orch Upgrade Available**\n\n\
1459         Service: `{current_version}`\n\
1460         Latest:  `{latest}`\n\n\
1461         Run: `brew update && brew upgrade orch && brew services restart orch`"
1462    );
1463
1464    let msg_slack = format!(
1465        "⚠️ *Orch Upgrade Available*\n\n\
1466         Service: `{current_version}`\n\
1467         Latest:  `{latest}`\n\n\
1468         Run: `brew update && brew upgrade orch && brew services restart orch`"
1469    );
1470
1471    for channel in channels.iter() {
1472        let (body, metadata, topic_id) = match channel.name() {
1473            "telegram" => (
1474                msg_telegram.clone(),
1475                serde_json::json!({"preformatted_html": true}),
1476                None,
1477            ),
1478            "discord" => (msg_discord.clone(), serde_json::json!({}), None),
1479            "slack" => (msg_slack.clone(), serde_json::json!({}), None),
1480            _ => continue,
1481        };
1482
1483        let msg = OutgoingMessage {
1484            thread_id: "upgrade".to_string(),
1485            body,
1486            reply_to: None,
1487            metadata,
1488            topic_id,
1489        };
1490
1491        if let Err(e) = channel.send(&msg).await {
1492            tracing::warn!(
1493                channel = channel.name(),
1494                ?e,
1495                "failed to send upgrade notification"
1496            );
1497        }
1498    }
1499
1500    // Record that we notified for this latest version.
1501    let _ = store.kv_set(last_notified_key, &latest).await;
1502}
1503
1504/// Send a one-time escalation notification when the startup retry loop has been
1505/// spinning for longer than `startup_failure_escalation_secs`.
1506///
1507/// This is called *before* the channel registry is initialised (the retry loop
1508/// sits above it in `serve()`), so we construct channel objects directly from
1509/// config — mirroring the approach used during normal channel registration, but
1510/// without relying on `ChannelRegistry`.
1511async fn send_startup_escalation_notification(elapsed_secs: u64) {
1512    #[expect(clippy::cast_precision_loss, reason = "a display duration")]
1513    let duration = notification::format_duration(elapsed_secs as f64);
1514
1515    let msg_telegram = format!(
1516        "🚨 <b>Orch startup blocked — GitHub unreachable</b>\n\n\
1517         The engine has been retrying backend connections for {duration}\n\
1518         without success. All task processing, routing, and scheduled\n\
1519         jobs are paused. Check service logs with `orch log -f`."
1520    );
1521
1522    let msg_discord = format!(
1523        "🚨 **Orch startup blocked — GitHub unreachable**\n\n\
1524         The engine has been retrying backend connections for {duration}\n\
1525         without success. All task processing, routing, and scheduled\n\
1526         jobs are paused. Check service logs with `orch log -f`."
1527    );
1528
1529    let msg_slack = format!(
1530        "🚨 *Orch startup blocked — GitHub unreachable*\n\n\
1531         The engine has been retrying backend connections for {duration}\n\
1532         without success. All task processing, routing, and scheduled\n\
1533         jobs are paused. Check service logs with `orch log -f`."
1534    );
1535
1536    // Telegram
1537    let tg_token = secret_ref::config_secret("channels.telegram.bot_token").await;
1538    let tg_chat_id = config::get("channels.telegram.chat_id");
1539    if let (Ok(token), Ok(chat_id)) = (&tg_token, &tg_chat_id)
1540        && !token.is_empty()
1541        && !chat_id.is_empty()
1542        && let Ok(channel) = TelegramChannel::new(token.clone(), Some(chat_id.clone()))
1543    {
1544        let msg = OutgoingMessage {
1545            thread_id: "startup_escalation".to_string(),
1546            body: msg_telegram.clone(),
1547            reply_to: None,
1548            metadata: serde_json::json!({ "preformatted_html": true }),
1549            topic_id: config::get("channels.telegram.general_topic_id").ok(),
1550        };
1551        match channel.send(&msg).await {
1552            Err(e) => {
1553                tracing::warn!(?e, "failed to send startup escalation via telegram");
1554            }
1555            _ => {
1556                tracing::info!("startup escalation notification sent to telegram");
1557            }
1558        }
1559    }
1560
1561    // Discord
1562    if let Ok(token) = secret_ref::config_secret("channels.discord.bot_token").await {
1563        let channel_id = config::get("channels.discord.channel_id")
1564            .or_else(|_| config::get("channels.discord.general_channel_id"));
1565        if !token.is_empty()
1566            && let Some(cid) = channel_id.ok().filter(|s| !s.is_empty())
1567        {
1568            let shard_id = config::get("channels.discord.shard_id")
1569                .ok()
1570                .and_then(|v| v.parse::<u64>().ok())
1571                .unwrap_or(0);
1572            let shard_count = config::get("channels.discord.shard_count")
1573                .ok()
1574                .and_then(|v| v.parse::<u64>().ok())
1575                .unwrap_or(1);
1576            if let Ok(gateway) = DiscordGateway::new(token, Some(cid), shard_id, shard_count) {
1577                let msg = OutgoingMessage {
1578                    thread_id: "startup_escalation".to_string(),
1579                    body: msg_discord.clone(),
1580                    reply_to: None,
1581                    metadata: serde_json::Value::Null,
1582                    topic_id: None,
1583                };
1584                match gateway.send(&msg).await {
1585                    Err(e) => {
1586                        tracing::warn!(?e, "failed to send startup escalation via discord");
1587                    }
1588                    _ => {
1589                        tracing::info!("startup escalation notification sent to discord");
1590                    }
1591                }
1592            }
1593        }
1594    }
1595
1596    // Slack
1597    if let Ok(token) = secret_ref::config_secret("channels.slack.bot_token").await {
1598        let channel_id = config::get("channels.slack.channel_id");
1599        if !token.is_empty()
1600            && let Ok(cid) = channel_id
1601            && let Ok(channel) = SlackChannel::new(token, Some(cid))
1602        {
1603            let msg = OutgoingMessage {
1604                thread_id: "startup_escalation".to_string(),
1605                body: msg_slack.clone(),
1606                reply_to: None,
1607                metadata: serde_json::Value::Null,
1608                topic_id: None,
1609            };
1610            match channel.send(&msg).await {
1611                Err(e) => {
1612                    tracing::warn!(?e, "failed to send startup escalation via slack");
1613                }
1614                _ => {
1615                    tracing::info!("startup escalation notification sent to slack");
1616                }
1617            }
1618        }
1619    }
1620}
1621
1622/// Start the orch service.
1623///
1624/// This is the main entry point — called by `orch serve`.
1625pub async fn serve() -> anyhow::Result<()> {
1626    let killed = home::kill_other_orch_serve_processes(Duration::from_secs(30));
1627    if killed > 0 {
1628        tracing::info!(count = killed, "terminated stale orch serve processes");
1629    }
1630
1631    tracing::info!("orch engine starting");
1632
1633    // Pre-create standard directories with async I/O so subsequent synchronous
1634    // callers (state_dir, orch_home, etc.) find them already present and their
1635    // create_dir_all calls become fast no-ops.
1636    if let Err(e) = home::init_dirs().await {
1637        tracing::warn!("failed to initialize orch directories: {e}");
1638    }
1639
1640    // Pre-warm the config cache with async I/O so get() / get_list() calls in
1641    // hot async paths never block a Tokio thread on std::fs::read_to_string.
1642    if let Err(e) = config::warm_cache().await {
1643        tracing::warn!("failed to warm config cache: {e}");
1644    }
1645
1646    let mut config = EngineConfig::from_config();
1647
1648    // One store for every project, opened before them so the web UI can run with none. A failure
1649    // here retries like a project failure did, so launchd does not restart the service in a loop
1650    let store = {
1651        let mut delay_secs = 5u64;
1652        let mut escalated = false;
1653        let start = Instant::now();
1654        loop {
1655            match open_shared_store().await {
1656                Ok(store) => break store,
1657                Err(e) => {
1658                    let elapsed = start.elapsed().as_secs();
1659                    let escalation = config.startup_failure_escalation_secs;
1660                    // Escalates once like a project that will not start, then keeps retrying
1661                    if escalation > 0 && !escalated && elapsed >= escalation {
1662                        escalated = true;
1663                        tracing::error!(
1664                            elapsed_secs = elapsed,
1665                            "the store or the cluster has not started, escalating: {e:#}"
1666                        );
1667                        send_startup_escalation_notification(elapsed).await;
1668                    } else {
1669                        tracing::warn!(
1670                            delay_secs,
1671                            "could not open the store or start the cluster, retrying: {e:#}"
1672                        );
1673                    }
1674
1675                    time::sleep(Duration::from_secs(delay_secs)).await;
1676                    delay_secs = (delay_secs * 2).min(120);
1677                }
1678            }
1679        }
1680    };
1681
1682    // Load persisted model cooldowns and register store for future writes.
1683    // Runs once here so the global cooldown store is not overwritten per project.
1684    engine_cooldown::init_cooldown_store(Arc::clone(&store)).await;
1685    tracing::info!("internal database ready");
1686
1687    // Create the event bus for task status transitions
1688    let event_bus = events::EventBus::new(256);
1689    if let Err(e) = event_bus.start_ws_server().await {
1690        tracing::warn!(
1691            ?e,
1692            "failed to start event websocket server, continuing without it"
1693        );
1694    }
1695
1696    // Before the projects, so a first run with none can add one from the UI
1697    if let Err(e) = ui::serve(Arc::clone(&store), event_bus.sender()).await {
1698        tracing::warn!(?e, "failed to start the web UI, continuing without it");
1699    }
1700
1701    // Initialize project engines — retry with backoff so a network outage at
1702    // startup doesn't cause a crash-loop (launchd KeepAlive would restart us
1703    // immediately, filling the error log with 600+ identical messages).
1704    //
1705    // IMPORTANT: errors from init_project_engines() must NEVER propagate past
1706    // this loop to main().  If they did, anyhow would write "Error: …" to stderr,
1707    // which brew routes to orch.error.log — polluting it even when projects ARE
1708    // configured correctly but GitHub is temporarily unreachable.
1709    let startup_escalation_secs = config.startup_failure_escalation_secs;
1710    let mut project_engines = {
1711        let mut delay_secs = 5u64;
1712        let mut attempt = 0u32;
1713        let mut escalated = false;
1714        let start = Instant::now();
1715        loop {
1716            attempt += 1;
1717            match init_project_engines(&store).await {
1718                Ok(engines) => break engines,
1719                Err(e) => {
1720                    let elapsed = start.elapsed().as_secs();
1721
1722                    // Short blips (under the escalation threshold) stay at WARN
1723                    // so they don't flood the operator's attention. Once the
1724                    // outage crosses the threshold, escalate to ERROR-level
1725                    // logging and send a one-time push notification to configured
1726                    // channels — "transient" has clearly become "prolonged."
1727                    if startup_escalation_secs > 0
1728                        && !escalated
1729                        && elapsed >= startup_escalation_secs
1730                    {
1731                        escalated = true;
1732                        #[expect(clippy::cast_precision_loss, reason = "a display duration")]
1733                        let outage = notification::format_duration(elapsed as f64);
1734                        tracing::error!(
1735                            delay_secs,
1736                            attempt,
1737                            elapsed_secs = elapsed,
1738                            threshold_secs = startup_escalation_secs,
1739                            "project backends unavailable for {outage} — escalating startup failure: {e}",
1740                        );
1741                        // Send one-time notification to configured channels.
1742                        // Channels aren't registered yet at this stage, so we
1743                        // construct them directly from config (mirrors orch notify).
1744                        send_startup_escalation_notification(elapsed).await;
1745                    } else {
1746                        tracing::warn!(
1747                            delay_secs,
1748                            attempt,
1749                            "project backends unavailable, retrying: {e}"
1750                        );
1751                    }
1752
1753                    time::sleep(Duration::from_secs(delay_secs)).await;
1754                    delay_secs = (delay_secs * 2).min(120);
1755                }
1756            }
1757        }
1758    };
1759
1760    tracing::info!(
1761        project_count = project_engines.len(),
1762        "initialized project engines"
1763    );
1764
1765    // Full set of currently-configured project repos, used by the per-tick global
1766    // sweeps (e.g. `dispatch_routed_tasks_global`) to distinguish "orphaned" tasks
1767    // (repo no longer in config) from tasks belonging to some other active project.
1768    let mut active_repos: HashSet<String> =
1769        project_engines.iter().map(|e| e.repo.clone()).collect();
1770
1771    // Check for a newer orch release in the background and log if the service is behind.
1772    // The periodic checker sends channel notifications after channels are initialized;
1773    // upgrading is operator-only and never runs automatically.
1774    {
1775        let current = env!("ORCH_VERSION").to_string();
1776        tokio::spawn(async move {
1777            if let Some(latest) = fetch_latest_release_version().await
1778                && latest != current
1779            {
1780                tracing::warn!(
1781                    current_version = %current,
1782                    latest_version = %latest,
1783                    "orch service is behind the latest release — run: brew update && brew upgrade orch && brew services restart orch"
1784                );
1785            }
1786        });
1787    }
1788
1789    // Re-create task managers with shared store and event bus
1790    for engine in &mut project_engines {
1791        engine.task_manager = Arc::new(TaskManager::with_events(
1792            Arc::clone(&engine.backend),
1793            Arc::clone(&engine.store),
1794            engine.repo.clone(),
1795            event_bus.sender(),
1796        ));
1797    }
1798
1799    // Startup reconciliation decides task status, so only the leader runs it
1800    let leads = project_engines.first().is_none_or(|e| e.store.is_leader());
1801    if leads && let Err(e) = reconcile_startup_worktrees(&project_engines).await {
1802        tracing::warn!(err = %e, "startup worktree reconciliation failed");
1803    }
1804
1805    if leads {
1806        // Run estimate reconciliation in the background so it never blocks startup.
1807        // Each GraphQL mutation takes ~0.8 s; with hundreds of tasks this would
1808        // otherwise delay the engine reaching its main loop by several minutes.
1809        let engines_snapshot: Vec<ProjectEngine> = project_engines.clone();
1810        tokio::spawn(async move {
1811            reconcile_startup_estimates(&engines_snapshot).await;
1812        });
1813    }
1814
1815    // Build ChannelRouter from global config + per-project configs
1816    let global_channel_config = GlobalChannelConfig {
1817        telegram_general_topic_id: config::get("channels.telegram.general_topic_id").ok(),
1818        discord_general_channel_id: config::get("channels.discord.general_channel_id").ok(),
1819        control_telegram_topic_id: config::get("control.channels.telegram.topic_id").ok(),
1820        control_discord_channel_id: config::get("control.channels.discord.channel_id").ok(),
1821    };
1822    let project_channel_configs: Vec<(String, ProjectChannelConfig)> = project_engines
1823        .iter()
1824        .map(|e| {
1825            let cfg = read_project_channel_config(&e.project_dir);
1826            (e.repo.clone(), cfg)
1827        })
1828        .collect();
1829    let channel_router = Arc::new(ChannelRouter::new(
1830        &global_channel_config,
1831        &project_channel_configs,
1832    ));
1833    tracing::info!(
1834        projects = ?channel_router.projects(),
1835        "channel router initialized"
1836    );
1837
1838    // Initialize tmux manager (shared across all projects)
1839    let tmux = Arc::new(TmuxManager::new());
1840    // Kill leftover sessions from a previous process. This is daemon start only,
1841    // never a tick, because nextest makes a once-flag in tick_all kill live agents
1842    tick::startup_cleanup(&tmux).await;
1843
1844    // Initialize transport
1845    let transport = Arc::new(Transport::new());
1846
1847    // Initialize capture service and start background loop
1848    let capture = Arc::new(CaptureService::new(Arc::clone(&transport)));
1849    let capture_for_tick = Arc::clone(&capture);
1850    tokio::spawn(async move {
1851        capture.start().await;
1852    });
1853
1854    // Initialize channel registry
1855    let mut channel_registry = ChannelRegistry::new();
1856
1857    // Try to initialize Telegram channel
1858    if let Ok(token) = secret_ref::config_secret("channels.telegram.bot_token").await
1859        && !token.is_empty()
1860    {
1861        let chat_id = config::get("channels.telegram.chat_id").ok();
1862        let allowed_users =
1863            config::get_list("channels.telegram.allowed_user_ids").unwrap_or_default();
1864        match TelegramChannel::new(token, chat_id).map(|t| t.with_allowed_users(allowed_users)) {
1865            Ok(telegram) => {
1866                if let Err(e) = telegram.health_check().await {
1867                    tracing::warn!(?e, "telegram channel health check failed, skipping");
1868                } else {
1869                    channel_registry.register(Box::new(telegram));
1870                    tracing::info!("telegram channel registered");
1871                }
1872            }
1873            Err(e) => {
1874                tracing::warn!(?e, "failed to create telegram channel, skipping");
1875            }
1876        }
1877    }
1878
1879    // Try to initialize Discord Gateway channel (websocket)
1880    if let Ok(token) = secret_ref::config_secret("channels.discord.bot_token").await
1881        && !token.is_empty()
1882    {
1883        let channel_id = config::get("channels.discord.channel_id").ok();
1884        let shard_id = config::get("channels.discord.shard_id")
1885            .ok()
1886            .and_then(|v| v.parse::<u64>().ok())
1887            .unwrap_or(0);
1888        let shard_count = config::get("channels.discord.shard_count")
1889            .ok()
1890            .and_then(|v| v.parse::<u64>().ok())
1891            .unwrap_or(1);
1892        let allowed_users =
1893            config::get_list("channels.discord.allowed_user_ids").unwrap_or_default();
1894        match DiscordGateway::new(token, channel_id, shard_id, shard_count)
1895            .map(|d| d.with_access(channel_router.discord_channel_ids(), allowed_users))
1896        {
1897            Ok(discord) => {
1898                if let Err(e) = discord.health_check().await {
1899                    tracing::warn!(?e, "discord gateway health check failed, skipping");
1900                } else {
1901                    channel_registry.register(Box::new(discord));
1902                    tracing::info!(shard_id, shard_count, "discord gateway registered");
1903                }
1904            }
1905            Err(e) => {
1906                tracing::warn!(?e, "failed to create discord gateway, skipping");
1907            }
1908        }
1909    }
1910
1911    // Try to initialize Slack channel
1912    if let Ok(token) = secret_ref::config_secret("channels.slack.bot_token").await
1913        && !token.is_empty()
1914    {
1915        let channel_id = config::get("channels.slack.channel_id").ok();
1916        match SlackChannel::new(token, channel_id) {
1917            Ok(slack) => {
1918                if let Err(e) = slack.health_check().await {
1919                    tracing::warn!(?e, "slack channel health check failed, skipping");
1920                } else {
1921                    channel_registry.register(Box::new(slack));
1922                    tracing::info!("slack channel registered");
1923                }
1924            }
1925            Err(e) => {
1926                tracing::warn!(?e, "failed to create slack channel, skipping");
1927            }
1928        }
1929    }
1930
1931    // Initialize tmux channel with transport for output streaming
1932    let tmux_channel = TmuxChannel::with_transport(Arc::clone(&transport));
1933    channel_registry.register(Box::new(tmux_channel));
1934    tracing::info!("tmux channel registered");
1935
1936    // Start all channels and collect their message receivers
1937    let mut channel_receivers: Vec<Receiver<IncomingMessage>> = Vec::new();
1938    for channel in channel_registry.iter() {
1939        match channel.start().await {
1940            Ok(rx) => {
1941                tracing::info!(channel = channel.name(), "channel started");
1942                channel_receivers.push(rx);
1943            }
1944            Err(e) => {
1945                tracing::warn!(channel = channel.name(), ?e, "failed to start channel");
1946            }
1947        }
1948    }
1949
1950    // Wrap channel registry in Arc for shared access (notification dispatcher needs it)
1951    let channel_registry = Arc::new(channel_registry);
1952
1953    // Spawn tasks to handle incoming channel messages (if any channels are active)
1954    let transport_for_messages = Arc::clone(&transport);
1955    let tmux_for_messages = Arc::clone(&tmux);
1956    let capture_for_messages = Arc::clone(&capture_for_tick);
1957    let channels_for_messages = Arc::clone(&channel_registry);
1958    // Lightweight engine references for command execution and task creation
1959    let engine_refs: Vec<EngineRef> = project_engines
1960        .iter()
1961        .map(|e| {
1962            (
1963                e.repo.clone(),
1964                Arc::clone(&e.backend),
1965                Arc::clone(&e.task_manager),
1966                Some(Arc::clone(&e.store)),
1967            )
1968        })
1969        .collect();
1970    // Shared in-memory map for pending project picks (General channel multi-project picker).
1971    let pending_picks: channel_handler::PendingPicks = Arc::new(Mutex::new(HashMap::new()));
1972    let router_for_messages = Arc::clone(&channel_router);
1973    for mut rx in channel_receivers {
1974        let transport = Arc::clone(&transport_for_messages);
1975        let tmux = Arc::clone(&tmux_for_messages);
1976        let capture = Arc::clone(&capture_for_messages);
1977        let channels = Arc::clone(&channels_for_messages);
1978        let engine_refs = engine_refs.clone();
1979        let ch_router = Arc::clone(&router_for_messages);
1980        let picks = Arc::clone(&pending_picks);
1981        tokio::spawn(async move {
1982            while let Some(msg) = rx.recv().await {
1983                tracing::debug!(channel = %msg.channel, thread = %msg.thread_id, "received message from channel");
1984                channel_handler::handle_channel_message(
1985                    msg,
1986                    &transport,
1987                    &tmux,
1988                    &capture,
1989                    &channels,
1990                    &engine_refs,
1991                    &ch_router,
1992                    &picks,
1993                )
1994                .await;
1995            }
1996        });
1997    }
1998
1999    // Post-restart confirmation: if the service was restarted via the /restart control command,
2000    // a KV marker was stored before shutdown. Send a confirmation to the originating channel now
2001    // that we're back online. Use a short delay to let the channel connections re-establish.
2002    {
2003        let restart_channels = Arc::clone(&channel_registry);
2004        let restart_store = Some(Arc::clone(&store));
2005        if let Some(store) = restart_store {
2006            tokio::spawn(async move {
2007                time::sleep(Duration::from_secs(5)).await;
2008                match store.kv_get("control:restart_pending").await {
2009                    Ok(Some(conv_key)) => {
2010                        if let Some((ch_name, thread_id, topic_id)) =
2011                            transport::parse_conversation_key(&conv_key)
2012                        {
2013                            for ch in restart_channels.iter() {
2014                                if ch.name() == ch_name {
2015                                    let msg = OutgoingMessage {
2016                                        thread_id: thread_id.to_string(),
2017                                        body: "orch restarted successfully.".to_string(),
2018                                        reply_to: None,
2019                                        metadata: serde_json::json!({}),
2020                                        topic_id: topic_id.map(ToString::to_string),
2021                                    };
2022                                    if let Err(e) = ch.send(&msg).await {
2023                                        tracing::warn!(
2024                                            channel = ch_name,
2025                                            error = %e,
2026                                            "failed to send restart confirmation"
2027                                        );
2028                                    }
2029                                    break;
2030                                }
2031                            }
2032                        }
2033                        if let Err(e) = store.kv_delete("control:restart_pending").await {
2034                            tracing::warn!(error = %e, "failed to clear control:restart_pending");
2035                        }
2036                    }
2037                    Ok(None) => {} // no pending restart, nothing to do
2038                    Err(e) => {
2039                        tracing::warn!(error = %e, "failed to read control:restart_pending");
2040                    }
2041                }
2042            });
2043        }
2044    }
2045
2046    // Spawn notification dispatcher — reads task completion notifications
2047    // from transport and broadcasts to configured channels.
2048    //
2049    // Routing priority:
2050    // 1. Dedicated channel: if the notification has a `repo` and the
2051    //    ChannelRouter maps it to a topic/channel, send there with `topic_id`.
2052    // 2. Subscribed channels: send to each (channel, thread_id) subscribed to
2053    //    the repo, using `format_with_project()` so recipients know the source.
2054    // 3. Fallback: if no repo or no dedicated channel, broadcast to all
2055    //    channels without a topic_id (legacy behaviour).
2056    {
2057        let mut notification_rx = transport.subscribe_notifications();
2058        let channels = Arc::clone(&channel_registry);
2059        let notif_router = Arc::clone(&channel_router);
2060        // Grab a store reference for subscription lookups.
2061        let notif_store: Option<Arc<TaskStore>> = Some(Arc::clone(&store));
2062        tokio::spawn(async move {
2063            loop {
2064                match notification_rx.recv().await {
2065                    Ok(notification) => {
2066                        let level = NotificationLevel::from_config();
2067                        if !level.should_notify(&notification.status) {
2068                            tracing::debug!(
2069                                task_id = %notification.task_id,
2070                                status = %notification.status,
2071                                "notification suppressed by level={:?}",
2072                                level
2073                            );
2074                            continue;
2075                        }
2076
2077                        tracing::info!(
2078                            task_id = %notification.task_id,
2079                            status = %notification.status,
2080                            repo = ?notification.repo,
2081                            "routing notification to channels"
2082                        );
2083
2084                        // Track whether we sent to at least one dedicated/subscribed target.
2085                        let mut routed = false;
2086
2087                        // 0. Direct target: if notify_target is set, send straight to
2088                        //    that Telegram chat_id and skip all other routing.
2089                        if let Some(ref target) = notification.notify_target {
2090                            let telegram = channels.iter().find(|c| c.name() == "telegram");
2091                            if let Some(channel) = telegram {
2092                                let body = notification.format_telegram();
2093                                let metadata = serde_json::json!({
2094                                    "preformatted_html": true,
2095                                    "chat_id_override": target
2096                                });
2097                                let msg = OutgoingMessage {
2098                                    thread_id: notification.task_id.clone(),
2099                                    body,
2100                                    reply_to: None,
2101                                    metadata,
2102                                    topic_id: None,
2103                                };
2104                                match channel.send(&msg).await {
2105                                    Err(e) => {
2106                                        tracing::warn!(
2107                                            task_id = %notification.task_id,
2108                                            target,
2109                                            ?e,
2110                                            "failed to send to notify_target"
2111                                        );
2112                                    }
2113                                    _ => {
2114                                        routed = true;
2115                                    }
2116                                }
2117                            }
2118                        }
2119
2120                        if routed {
2121                            // notify_target handled — skip further routing
2122                        } else if let Some(repo) = notification.repo.as_deref() {
2123                            // Track destinations already sent to in this iteration so
2124                            // the dedicated (path 1) and subscribed (path 2) loops
2125                            // don't double-fire the same Telegram/Discord destination
2126                            // when a repo's configured topic and a `channel_subscriptions`
2127                            // row resolve to the same `(channel, topic_id)`.
2128                            let mut sent: HashSet<(String, Option<String>)> = HashSet::new();
2129
2130                            // 1. Dedicated channel targets for this repo.
2131                            for channel in channels.iter() {
2132                                let ch_name = channel.name();
2133                                if let Some(topic_id) =
2134                                    notif_router.target_for_project(repo, ch_name)
2135                                {
2136                                    let body = match ch_name {
2137                                        "telegram" => notification.format_telegram(),
2138                                        "discord" => notification.format_discord(),
2139                                        "slack" => notification.format_slack(),
2140                                        _ => continue,
2141                                    };
2142                                    let metadata = if ch_name == "telegram" {
2143                                        serde_json::json!({"preformatted_html": true})
2144                                    } else {
2145                                        serde_json::json!({})
2146                                    };
2147                                    let resolved_topic = Some(topic_id.to_string());
2148                                    let msg = OutgoingMessage {
2149                                        thread_id: notification.task_id.clone(),
2150                                        body,
2151                                        reply_to: None,
2152                                        metadata,
2153                                        topic_id: resolved_topic.clone(),
2154                                    };
2155                                    if let Err(e) = channel.send(&msg).await {
2156                                        tracing::warn!(
2157                                            channel = ch_name,
2158                                            task_id = %notification.task_id,
2159                                            ?e,
2160                                            "failed to send to dedicated channel"
2161                                        );
2162                                    } else {
2163                                        sent.insert((ch_name.to_string(), resolved_topic));
2164                                        routed = true;
2165                                    }
2166                                }
2167                            }
2168
2169                            // 2. Subscribed channels for this repo.
2170                            if let Some(store) = &notif_store {
2171                                match store.list_subscribers_for_repo(repo).await {
2172                                    Ok(subscribers) => {
2173                                        for (ch_name, thread_id, topic_id) in subscribers {
2174                                            let channel =
2175                                                channels.iter().find(|c| c.name() == ch_name);
2176                                            let Some(channel) = channel else {
2177                                                continue;
2178                                            };
2179                                            let resolved_topic = if topic_id.is_empty() {
2180                                                None
2181                                            } else {
2182                                                Some(topic_id.clone())
2183                                            };
2184                                            // Skip if a dedicated send to the same
2185                                            // (channel, topic_id) already happened.
2186                                            if sent.contains(&(
2187                                                ch_name.clone(),
2188                                                resolved_topic.clone(),
2189                                            )) {
2190                                                tracing::debug!(
2191                                                    channel = %ch_name,
2192                                                    task_id = %notification.task_id,
2193                                                    topic_id = ?resolved_topic,
2194                                                    "skipping duplicate subscriber dispatch — destination already received from dedicated path"
2195                                                );
2196                                                continue;
2197                                            }
2198                                            let body = notification.format_with_project(&ch_name);
2199                                            let metadata = if ch_name == "telegram" {
2200                                                serde_json::json!({"preformatted_html": true})
2201                                            } else {
2202                                                serde_json::json!({})
2203                                            };
2204                                            let msg = OutgoingMessage {
2205                                                thread_id: thread_id.clone(),
2206                                                body,
2207                                                reply_to: None,
2208                                                metadata,
2209                                                topic_id: resolved_topic.clone(),
2210                                            };
2211                                            if let Err(e) = channel.send(&msg).await {
2212                                                tracing::warn!(
2213                                                    channel = %ch_name,
2214                                                    task_id = %notification.task_id,
2215                                                    ?e,
2216                                                    "failed to send to subscribed channel"
2217                                                );
2218                                            } else {
2219                                                sent.insert((ch_name.clone(), resolved_topic));
2220                                                routed = true;
2221                                            }
2222                                        }
2223                                    }
2224                                    Err(e) => {
2225                                        tracing::warn!(
2226                                            repo,
2227                                            ?e,
2228                                            "failed to list subscribers for repo"
2229                                        );
2230                                    }
2231                                }
2232                            }
2233                        }
2234
2235                        // 3. Fallback: broadcast to all channels when no routing happened.
2236                        if !routed {
2237                            for channel in channels.iter() {
2238                                let (body, should_send, is_telegram) = match channel.name() {
2239                                    "telegram" => (notification.format_telegram(), true, true),
2240                                    "discord" => (notification.format_discord(), true, false),
2241                                    "slack" => (notification.format_slack(), true, false),
2242                                    // GitHub is already handled by backend.post_comment()
2243                                    // tmux doesn't need task completion notifications
2244                                    _ => (String::new(), false, false),
2245                                };
2246
2247                                if !should_send {
2248                                    continue;
2249                                }
2250
2251                                let metadata = if is_telegram {
2252                                    serde_json::json!({"preformatted_html": true})
2253                                } else {
2254                                    serde_json::json!({})
2255                                };
2256                                let msg = OutgoingMessage {
2257                                    thread_id: notification.task_id.clone(),
2258                                    body,
2259                                    reply_to: None,
2260                                    metadata,
2261                                    topic_id: None,
2262                                };
2263
2264                                if let Err(e) = channel.send(&msg).await {
2265                                    tracing::warn!(
2266                                        channel = channel.name(),
2267                                        task_id = %notification.task_id,
2268                                        ?e,
2269                                        "failed to send notification"
2270                                    );
2271                                }
2272                            }
2273                        }
2274                    }
2275                    Err(RecvError::Lagged(n)) => {
2276                        tracing::warn!(missed = n, "notification receiver lagged");
2277                    }
2278                    Err(RecvError::Closed) => {
2279                        tracing::debug!("notification channel closed");
2280                        break;
2281                    }
2282                }
2283            }
2284        });
2285        tracing::info!("notification dispatcher started");
2286    }
2287
2288    // Notify used by webhook events to wake up the engine tick immediately
2289    let webhook_notify = Arc::new(Notify::new());
2290
2291    // `WebhookStatus::fallback_mode` is only shown by `orch webhook status`, the sync
2292    // tick always runs every `engine.sync_interval` whether or not webhooks work
2293    let webhook_port: Option<u16>;
2294    let mut webhook_healthy: bool;
2295    let mut last_webhook_health_check = Instant::now();
2296    // Start webhook server if configured
2297    let webhook_enabled = config::get_global("webhook.enabled").is_ok_and(|v| v == "true");
2298    // No readable secret keeps the webhook server off, it never accepts unsigned requests
2299    let webhook_secret = if webhook_enabled {
2300        secret_ref::config_secret("webhook.secret")
2301            .await
2302            .ok()
2303            .filter(|s| !s.trim().is_empty())
2304    } else {
2305        None
2306    };
2307    if webhook_enabled && webhook_secret.is_none() {
2308        tracing::error!(
2309            "webhook.enabled needs a readable webhook.secret, the webhook server stays off"
2310        );
2311    }
2312    let webhook_enabled = webhook_enabled && webhook_secret.is_some();
2313    let webhook_secret = webhook_secret.unwrap_or_default();
2314
2315    // Shared status updated by the server-spawn task and the health-check loop.
2316    let webhook_status = Arc::new(Mutex::new(WebhookStatus::default()));
2317
2318    if webhook_enabled {
2319        // Spawn the HTTP server with exponential-backoff retry on transient
2320        // bind errors (e.g. EADDRINUSE).  After MAX_ATTEMPTS the engine falls
2321        // back to polling and updates the shared status accordingly.
2322        const WEBHOOK_MAX_ATTEMPTS: u32 = 5;
2323        let port: u16 = config::get("webhook.port")
2324            .ok()
2325            .and_then(|v| v.parse().ok())
2326            .unwrap_or(8080);
2327        webhook_port = Some(port);
2328        let secret = webhook_secret.clone();
2329        let webhook_repo = project_engines
2330            .first()
2331            .map(|e| e.repo.clone())
2332            .unwrap_or_default();
2333
2334        let (tx, mut rx) = mpsc::channel::<IncomingMessage>(64);
2335
2336        // Initialise status: configured, not yet healthy.
2337        let to_save = {
2338            let mut s = webhook_status.lock().await;
2339            s.configured = true;
2340            s.port = Some(port);
2341            s.healthy = false;
2342            s.fallback_mode = false;
2343            s.clone()
2344        };
2345        to_save.save().await;
2346
2347        let status_for_spawn = Arc::clone(&webhook_status);
2348        tokio::spawn(async move {
2349            let mut attempt = 0u32;
2350            loop {
2351                attempt += 1;
2352                tracing::info!(attempt, port, "attempting to start webhook server");
2353                match start_webhook_server(port, secret.clone(), webhook_repo.clone(), tx.clone())
2354                    .await
2355                {
2356                    Ok(()) => {
2357                        tracing::info!(attempt, "webhook server exited cleanly");
2358                        break;
2359                    }
2360                    Err(e) => {
2361                        let reason = e.to_string();
2362                        tracing::error!(attempt, %reason, "webhook server failed");
2363                        if attempt >= WEBHOOK_MAX_ATTEMPTS || !is_transient_bind_error(&e) {
2364                            let kind = if attempt >= WEBHOOK_MAX_ATTEMPTS {
2365                                "max attempts reached"
2366                            } else {
2367                                "non-transient error"
2368                            };
2369                            tracing::error!(
2370                                attempt,
2371                                %reason,
2372                                kind,
2373                                orch_webhook_in_fallback = true,
2374                                "webhook server giving up, switching to polling fallback"
2375                            );
2376                            let to_save = {
2377                                let mut s = status_for_spawn.lock().await;
2378                                s.fallback_mode = true;
2379                                s.healthy = false;
2380                                s.last_failure_reason = Some(reason);
2381                                s.startup_attempts = attempt;
2382                                s.clone()
2383                            };
2384                            to_save.save().await;
2385                            break;
2386                        }
2387                        let delay = webhook_backoff_delay(attempt);
2388                        tracing::warn!(
2389                            attempt,
2390                            delay_ms = delay.as_millis(),
2391                            %reason,
2392                            "webhook bind failed (transient), retrying with backoff"
2393                        );
2394                        {
2395                            let to_save = {
2396                                let mut s = status_for_spawn.lock().await;
2397                                s.startup_attempts = attempt;
2398                                s.last_failure_reason = Some(reason);
2399                                s.clone()
2400                            };
2401                            to_save.save().await;
2402                        }
2403                        time::sleep(delay).await;
2404                    }
2405                }
2406            }
2407        });
2408
2409        // Spawn the message forwarding task (reads from webhook channel)
2410        let notify = Arc::clone(&webhook_notify);
2411        let transport_for_webhook = Arc::clone(&transport);
2412        tokio::spawn(async move {
2413            while let Some(msg) = rx.recv().await {
2414                tracing::info!(
2415                    channel = %msg.channel,
2416                    thread = %msg.thread_id,
2417                    event = %msg.metadata.get("event").and_then(|v| v.as_str()).unwrap_or("unknown"),
2418                    action = %msg.metadata.get("action").and_then(|v| v.as_str()).unwrap_or("unknown"),
2419                    "webhook event received, triggering immediate tick"
2420                );
2421                let route = transport_for_webhook.route(&msg).await;
2422                tracing::info!(
2423                    channel = %msg.channel,
2424                    route = ?route,
2425                    "webhook event routed"
2426                );
2427                // Wake up the engine tick immediately instead of waiting up to 10s
2428                notify.notify_one();
2429            }
2430        });
2431
2432        webhook_healthy = true;
2433        tracing::info!(port, orch_webhook_up = true, "webhook server started");
2434    } else {
2435        webhook_port = None;
2436        webhook_healthy = false;
2437        {
2438            let to_save = {
2439                let mut s = webhook_status.lock().await;
2440                s.configured = false;
2441                s.fallback_mode = true;
2442                s.clone()
2443            };
2444            to_save.save().await;
2445        }
2446        tracing::info!(
2447            orch_webhook_in_fallback = true,
2448            "webhook server disabled, using polling fallback mode"
2449        );
2450    }
2451
2452    start_relay_client(&store, &webhook_notify);
2453    app::start_loader();
2454
2455    // Agent router (selects agent + model per task) - shared across projects.
2456    // Router::from_config() may run `opencode models` when the config uses opencode.
2457    // spawn_blocking keeps the Tokio runtime thread free during that I/O
2458    let router = Arc::new(RwLock::new(
2459        match task::spawn_blocking(Router::from_config).await {
2460            Ok(r) => r,
2461            Err(e) => {
2462                tracing::error!(?e, "router init panicked in spawn_blocking, using default");
2463                task::spawn_blocking(|| Router::new(RouterConfig::default()))
2464                    .await
2465                    .unwrap_or_else(|e2| {
2466                        panic!("router default init also panicked in spawn_blocking — cannot start engine without a router: {e2:?}");
2467                    })
2468            }
2469        },
2470    ));
2471    {
2472        let r = router.read().await;
2473        tracing::info!(
2474            mode = %r.config.mode,
2475            agents = ?r.available_agents,
2476            fallback = %r.config.fallback_executor,
2477            "router initialized"
2478        );
2479    }
2480
2481    // Concurrency limiter (shared across all projects). `parallel_in_force` is the
2482    // limit actually handed out, which lags `config.max_parallel` after a shrink
2483    // until the running tasks give their permits back.
2484    let semaphore = Arc::new(Semaphore::new(config.max_parallel));
2485    let mut parallel_in_force = config.max_parallel;
2486    // Reviews get their own pool, one reviewer per worker, so a busy worker pool never
2487    // starves a review (#3971)
2488    let review_semaphore = Arc::new(Semaphore::new(config.max_parallel));
2489    let mut review_in_force = config.max_parallel;
2490
2491    // In-memory dispatch guard: tracks task IDs currently being dispatched.
2492    // Guards against GitHub API eventual consistency — after update_status(InProgress),
2493    // the label-removal webhook can trigger an immediate tick where list_by_status(Routed)
2494    // still returns the task (search index propagation delay). The tmux session does not
2495    // exist until the runner completes worktree setup (~10s later), so session_exists
2496    // alone is insufficient. Keyed by "{repo}/{task_id}".
2497    let dispatching: Arc<dashmap::DashMap<String, String>> = Arc::new(dashmap::DashMap::new());
2498
2499    // In-memory guard to prevent double-spawn of auto-merge background tasks.
2500    // Since review_open_prs runs every sync_tick (~10s) but CI polling can take
2501    // up to 10 minutes, we track which tasks already have an auto-merge in flight.
2502    let auto_merge_in_flight: Arc<dashmap::DashSet<String>> = Arc::new(dashmap::DashSet::new());
2503
2504    // Subscribe to config file changes for hot reload
2505    let mut config_rx = config::subscribe();
2506
2507    // Track sync interval
2508    let mut last_sync = Instant::now();
2509
2510    // Track upgrade check interval
2511    let mut last_upgrade_check: Option<Instant> = None;
2512
2513    // Reset epoch of the GitHub rate-limit window already reported, so the log line,
2514    // the KV flag and the channel notification fire once per window instead of per tick
2515    let mut rate_limit_reported: Option<i64> = None;
2516
2517    // Channel for weight signals from task runners back to the router
2518    let (weight_tx, mut weight_rx) = mpsc::channel::<WeightSignal>(64);
2519
2520    // A restart ends every review session, so reset each in_review task left without one
2521    for engine in project_engines.iter().filter(|_| leads) {
2522        let reset =
2523            reset_orphaned_reviews(&engine.task_manager, &engine.store, &engine.repo, &tmux).await;
2524
2525        if reset > 0 {
2526            tracing::info!(repo = %engine.repo, reset, "reset orphaned reviews to needs_review on startup");
2527        }
2528    }
2529
2530    // Spawn dispatch subscribers — react to Routed events immediately
2531    for engine in &project_engines {
2532        subscribers::dispatch::spawn(
2533            event_bus.subscribe(),
2534            Arc::clone(&engine.backend),
2535            Arc::clone(&tmux),
2536            Arc::clone(&engine.runner),
2537            Arc::clone(&capture_for_tick),
2538            Arc::clone(&semaphore),
2539            Arc::clone(&engine.task_manager),
2540            weight_tx.clone(),
2541            Arc::clone(&router),
2542            Arc::clone(&dispatching),
2543            Arc::clone(&engine.store),
2544            engine.repo.clone(),
2545        );
2546    }
2547    tracing::info!("dispatch subscriber started");
2548
2549    // Spawn review subscribers — react to NeedsReview events immediately
2550    for engine in &project_engines {
2551        subscribers::review::spawn(
2552            event_bus.subscribe(),
2553            Arc::clone(&engine.backend),
2554            Arc::clone(&tmux),
2555            Arc::clone(&review_semaphore),
2556            Arc::clone(&engine.task_manager),
2557            Arc::clone(&router),
2558            Arc::clone(&dispatching),
2559            Arc::clone(&engine.store),
2560            engine.repo.clone(),
2561        );
2562    }
2563    tracing::info!("review subscriber started");
2564
2565    // Spawn unblock subscribers — react to Done events, unblock parent tasks immediately
2566    for engine in &project_engines {
2567        subscribers::unblock::spawn(
2568            event_bus.subscribe(),
2569            Arc::clone(&engine.backend),
2570            Arc::clone(&engine.task_manager),
2571            Arc::clone(&engine.store),
2572            engine.repo.clone(),
2573        );
2574    }
2575    tracing::info!("unblock subscriber started");
2576
2577    // Spawn notify subscriber — reacts to ALL events, pushes to transport.
2578    // Spawned once (not per-project) since the transport handles all repos.
2579    subscribers::notify::spawn(event_bus.subscribe(), Arc::clone(&transport));
2580    tracing::info!("notify subscriber started");
2581
2582    // Main loop
2583    tracing::info!(
2584        tick = ?config.tick_interval,
2585        sync = ?config.sync_interval,
2586        parallel = config.max_parallel,
2587        "entering main loop"
2588    );
2589    // Values of the settings that only take effect at startup, so a reload can
2590    // tell the operator their edit needs a restart.
2591    let mut restart_only = restart_only_snapshot();
2592
2593    let mut interval = time::interval(config.tick_interval);
2594
2595    // Signal handlers (launchd/systemd send SIGTERM to stop services, SIGHUP for reload/restart)
2596    let mut sigterm = unix::signal(SignalKind::terminate())?;
2597    let mut sighup = unix::signal(SignalKind::hangup())?;
2598
2599    // Guard to prevent concurrent sync_tick runs (sync is spawned as a background task).
2600    let sync_in_progress = Arc::new(AtomicBool::new(false));
2601
2602    // Tick watchdog: detects when the main loop hasn't completed a tick in > 60s.
2603    // Updates an atomic timestamp at the end of each tick iteration.
2604    let last_tick_epoch = Arc::new(AtomicU64::new(
2605        SystemTime::now()
2606            .duration_since(std_time::UNIX_EPOCH)
2607            .unwrap_or_default()
2608            .as_secs(),
2609    ));
2610    {
2611        let last_tick_epoch = Arc::clone(&last_tick_epoch);
2612        let tick_interval_secs = config.tick_interval.as_secs();
2613        tokio::spawn(async move {
2614            // Check every 30s whether the main loop is still ticking.
2615            let mut watchdog_interval = time::interval(Duration::from_secs(30));
2616            // Own suspend baseline, independent of the main loop's checkpoint(). The
2617            // watchdog's interval timer also freezes during a host suspend, so this
2618            // detects the gap from its own wake-up instead of racing the main loop's
2619            // checkpoint() for the same GAP_LOG entry (see #3544).
2620            let mut suspend_detector = SuspendDetector::new();
2621            loop {
2622                watchdog_interval.tick().await;
2623                let own_gap = suspend_detector.check();
2624                let last = last_tick_epoch.load(Ordering::Relaxed);
2625                let now = SystemTime::now()
2626                    .duration_since(std_time::UNIX_EPOCH)
2627                    .unwrap_or_default()
2628                    .as_secs();
2629                let stale_secs = now.saturating_sub(last);
2630                // Warn if the tick loop hasn't completed in > 6× the tick interval (at least 60s).
2631                let threshold = (tick_interval_secs * 6).max(60);
2632                if stale_secs > threshold {
2633                    // A host suspend/resume gap fully explains a stale window that
2634                    // isn't an actual stall — the process (and its timers) were
2635                    // simply not scheduled while the host was asleep. Prefer this
2636                    // watchdog's own detector (immune to the checkpoint race above);
2637                    // fall back to the shared log in case the main loop's checkpoint()
2638                    // already ran and recorded a larger gap.
2639                    let shared_gap =
2640                        engine_suspend::gap_detected_within(Duration::from_secs(stale_secs));
2641                    let gap = match (own_gap, shared_gap) {
2642                        (Some(a), Some(b)) => Some(a.max(b)),
2643                        (Some(a), None) => Some(a),
2644                        (None, Some(b)) => Some(b),
2645                        (None, None) => None,
2646                    };
2647                    if let Some(gap) = gap {
2648                        let gap_secs = u64::try_from(gap.num_seconds()).unwrap_or(0);
2649                        if gap_secs >= stale_secs {
2650                            tracing::info!(
2651                                stale_secs,
2652                                threshold,
2653                                suspend_gap_secs = gap_secs,
2654                                "tick loop stale window explained by host suspend/resume, not a stall"
2655                            );
2656                        } else {
2657                            tracing::error!(
2658                                stale_secs,
2659                                threshold,
2660                                suspend_gap_secs = gap_secs,
2661                                "WATCHDOG: tick loop has not completed a tick in {}s (threshold {}s) — suspend gap of {}s only partially explains the stall",
2662                                stale_secs,
2663                                threshold,
2664                                gap_secs,
2665                            );
2666                        }
2667                    } else {
2668                        tracing::error!(
2669                            stale_secs,
2670                            threshold,
2671                            "WATCHDOG: tick loop has not completed a tick in {}s (threshold {}s) — possible stall",
2672                            stale_secs,
2673                            threshold,
2674                        );
2675                    }
2676                }
2677            }
2678        });
2679    }
2680
2681    loop {
2682        tokio::select! {
2683            _ = interval.tick() => {
2684                let tick_start = Instant::now();
2685
2686                // Detect a host suspend/resume gap once per iteration, in one place.
2687                // The watchdog and stuck-task recovery both read from this instead of
2688                // re-deriving suspend information from their own wall-clock deltas.
2689                if let Some(gap) = engine_suspend::checkpoint() {
2690                    tracing::info!(
2691                        gap_secs = gap.num_seconds(),
2692                        "host suspend/resume gap detected, discounting from stuck-task age checks"
2693                    );
2694                }
2695
2696                // Finish a pending shrink of the dispatch pool as running tasks
2697                // release their permits. Only logs when a permit was actually
2698                // taken back, so a long-running task does not log every tick.
2699                if review_in_force != config.max_parallel {
2700                    review_in_force =
2701                        resize_dispatch_pool(&review_semaphore, review_in_force, config.max_parallel);
2702                }
2703                if parallel_in_force != config.max_parallel {
2704                    let resized =
2705                        resize_dispatch_pool(&semaphore, parallel_in_force, config.max_parallel);
2706                    if resized != parallel_in_force {
2707                        parallel_in_force = resized;
2708                        tracing::info!(
2709                            in_force = parallel_in_force,
2710                            target = config.max_parallel,
2711                            "dispatch pool resized"
2712                        );
2713                    }
2714                }
2715
2716                // Tick weight recovery for rate-limited agents
2717                {
2718                    let mut rw = router.write().await;
2719                    rw.tick_weight_recovery();
2720                }
2721
2722                // Report the GitHub rate limit once per window, then tick anyway. Calls
2723                // into GitHub now fail fast instead of sleeping until the reset, so the
2724                // local phases keep moving and the watchdog does not see a stalled loop.
2725                if let Some(remaining) = GhHttp::is_rate_limited() {
2726                    let reset_at = chrono::Utc::now()
2727                        .timestamp()
2728                        .saturating_add(i64::try_from(remaining.as_secs()).unwrap_or(i64::MAX));
2729                    // Tolerate jitter in the header so one window reports once
2730                    if rate_limit_reported.is_none_or(|prev| (reset_at - prev).abs() > 60) {
2731                        rate_limit_reported = Some(reset_at);
2732                        report_rate_limited(reset_at, Some(&store), Some(&transport)).await;
2733                    }
2734                } else if let Some(prev) = rate_limit_reported.take() {
2735                    tracing::info!(
2736                        limited_until = prev,
2737                        "GitHub API rate limit cleared"
2738                    );
2739                    let _ = store.kv_delete(RATE_LIMITED_UNTIL_KEY).await;
2740                }
2741
2742                // Core tick: poll tasks for all projects
2743                tick::tick_all(
2744                    &project_engines,
2745                    &tmux,
2746                    &capture_for_tick,
2747                    &semaphore,
2748                    &config,
2749                    &router,
2750                    &weight_tx,
2751                    &dispatching,
2752                    Some(&transport),
2753                    &active_repos,
2754                    &auto_merge_in_flight,
2755                ).await;
2756
2757                // Drain pending weight signals after tick processing so the router
2758                // learns from outcomes produced by this iteration's task dispatches.
2759                while let Ok(signal) = weight_rx.try_recv() {
2760                    let mut rw = router.write().await;
2761                    match signal {
2762                        WeightSignal::RateLimited { ref agent } => {
2763                            rw.record_rate_limit(agent);
2764                        }
2765                        WeightSignal::Success { ref agent } => {
2766                            rw.record_success(agent);
2767                        }
2768                        WeightSignal::Failed { .. }
2769                        | WeightSignal::Rerouted
2770                        | WeightSignal::Blocked
2771                        | WeightSignal::None => {}
2772                    }
2773                }
2774
2775                // Periodic sync (less frequent) — spawned as a background task so it
2776                // cannot block the main tick loop. A stalled sync (hung HTTP call, slow
2777                // DB query, lock contention) previously parked all tokio workers for 12+
2778                // minutes because it ran inline. See issue #2574.
2779                if last_sync.elapsed() >= config.sync_interval {
2780                    if sync_in_progress.compare_exchange(
2781                        false,
2782                        true,
2783                        Ordering::AcqRel,
2784                        Ordering::Relaxed,
2785                    ).is_ok() {
2786                        spawn_sync_pass(
2787                            &project_engines,
2788                            &tmux,
2789                            &config,
2790                            &router,
2791                            &dispatching,
2792                            &auto_merge_in_flight,
2793                            &sync_in_progress,
2794                            "periodic",
2795                        );
2796                    } else {
2797                        tracing::debug!("sync tick still in progress, skipping");
2798                    }
2799                    // Records sync *schedule* time (not completion). This prevents
2800                    // re-entering the sync branch every tick while one is in flight.
2801                    // The sync_in_progress guard handles actual concurrency control.
2802                    last_sync = Instant::now();
2803                }
2804
2805                // Periodic webhook health check
2806                if webhook_enabled {
2807                    let health_check_interval = config.webhook_health_check_interval
2808                        .unwrap_or(Duration::from_secs(60));
2809
2810                    if last_webhook_health_check.elapsed() >= health_check_interval {
2811                        if let Some(port) = webhook_port {
2812                            let (health, failure_reason) =
2813                                github::check_webhook_health(port).await;
2814                            if health != webhook_healthy {
2815                                webhook_healthy = health;
2816                                if webhook_healthy {
2817                                    tracing::info!(port, orch_webhook_up = true, "webhook health restored");
2818                                } else {
2819                                    tracing::warn!(
2820                                        port,
2821                                        orch_webhook_up = false,
2822                                        reason = failure_reason.as_deref().unwrap_or("unknown"),
2823                                        "webhook health check failed"
2824                                    );
2825                                }
2826                            }
2827                            // Persist updated status.
2828                            {
2829                                let to_save = {
2830                                    let mut s = webhook_status.lock().await;
2831                                    s.healthy = health;
2832                                    s.last_check_utc = Some(chrono::Utc::now());
2833                                    if health {
2834                                        s.last_failure_reason = None;
2835                                    } else if failure_reason.is_some() {
2836                                        s.last_failure_reason = failure_reason;
2837                                    }
2838                                    s.clone()
2839                                };
2840                                to_save.save().await;
2841                            }
2842                        }
2843                        last_webhook_health_check = Instant::now();
2844                    }
2845                }
2846
2847                // Periodic upgrade check — notify channels when a newer release is available.
2848                if should_run_upgrade_check(last_upgrade_check, config.upgrade_check_interval) {
2849                    let store = Arc::clone(&store);
2850                    let channels = Arc::clone(&channel_registry);
2851                    let current = env!("ORCH_VERSION").to_string();
2852                    let check_interval = Duration::from_secs(config.upgrade_check_interval);
2853                    tokio::spawn(async move {
2854                        check_and_notify_upgrade(&store, &channels, &current, check_interval)
2855                            .await;
2856                    });
2857                    last_upgrade_check = Some(Instant::now());
2858                }
2859
2860                // Update watchdog timestamp + log tick duration.
2861                let tick_elapsed = tick_start.elapsed();
2862                if tick_elapsed.as_secs() > 30 {
2863                    tracing::warn!(
2864                        elapsed_ms = u64::try_from(tick_elapsed.as_millis()).unwrap_or(u64::MAX),
2865                        "slow tick"
2866                    );
2867                } else {
2868                    tracing::debug!(
2869                        elapsed_ms = u64::try_from(tick_elapsed.as_millis()).unwrap_or(u64::MAX),
2870                        "tick complete"
2871                    );
2872                }
2873                last_tick_epoch.store(
2874                    SystemTime::now()
2875                        .duration_since(std_time::UNIX_EPOCH)
2876                        .unwrap_or_default()
2877                        .as_secs(),
2878                    Ordering::Relaxed,
2879                );
2880            }
2881            // Webhook events trigger an immediate tick (bypass polling interval)
2882            () = webhook_notify.notified() => {
2883                if let Some(remaining) = GhHttp::is_rate_limited() {
2884                    tracing::warn!(
2885                        remaining_secs = remaining.as_secs(),
2886                        "GitHub API rate-limited, skipping webhook-triggered tick"
2887                    );
2888                } else {
2889                    tracing::info!("webhook event triggered immediate tick");
2890
2891                    tick::tick_all(
2892                        &project_engines,
2893                        &tmux,
2894                        &capture_for_tick,
2895                        &semaphore,
2896                        &config,
2897                        &router,
2898                        &weight_tx,
2899                        &dispatching,
2900                        Some(&transport),
2901                        &active_repos,
2902                        &auto_merge_in_flight,
2903                    ).await;
2904
2905                // Drain pending weight signals after webhook-triggered tick processing.
2906                while let Ok(signal) = weight_rx.try_recv() {
2907                    let mut rw = router.write().await;
2908                    match signal {
2909                        WeightSignal::RateLimited { ref agent } => {
2910                            rw.record_rate_limit(agent);
2911                        }
2912                        WeightSignal::Success { ref agent } => {
2913                            rw.record_success(agent);
2914                        }
2915                        WeightSignal::Failed { .. }
2916                        | WeightSignal::Rerouted
2917                        | WeightSignal::Blocked
2918                        | WeightSignal::None => {}
2919                    }
2920                }
2921
2922                // Also attempt to run an immediate sync (ingest/cleanup) in the
2923                // background so webhook-driven events (e.g. new issues) are
2924                // picked up without waiting for the periodic sync interval.
2925                // Reuse the same sync_in_progress guard used by the periodic
2926                // branch to avoid concurrent syncs.
2927                if sync_in_progress.compare_exchange(
2928                    false,
2929                    true,
2930                    Ordering::AcqRel,
2931                    Ordering::Relaxed,
2932                ).is_ok() {
2933                    spawn_sync_pass(
2934                        &project_engines,
2935                        &tmux,
2936                        &config,
2937                        &router,
2938                        &dispatching,
2939                        &auto_merge_in_flight,
2940                        &sync_in_progress,
2941                        "webhook",
2942                    );
2943                    // Record schedule time so periodic sync doesn't immediately re-run.
2944                    last_sync = Instant::now();
2945                } else {
2946                    tracing::debug!("sync tick still in progress, skipping webhook-triggered immediate sync");
2947                }
2948                }
2949
2950                // Also reset the interval so we don't get a redundant tick right after
2951                interval.reset();
2952            }
2953            result = config_rx.recv() => {
2954                // A notification only arrives for a file that parsed, so there is no
2955                // broken-config case to guard against here (see `config::reload_file`).
2956                let reload = match result {
2957                    Ok(path) => {
2958                        tracing::info!(path = %path.display(), "config file changed, reloading");
2959                        true
2960                    }
2961                    Err(RecvError::Lagged(n)) => {
2962                        tracing::warn!(missed = n, "config change receiver lagged, reloading");
2963                        true
2964                    }
2965                    Err(RecvError::Closed) => {
2966                        tracing::warn!("config change channel closed");
2967                        false
2968                    }
2969                };
2970
2971                if reload {
2972                    // Reload engine config
2973                    let new_config = EngineConfig::from_config();
2974                    log_engine_config_changes(&config, &new_config);
2975
2976                    let new_restart_only = restart_only_snapshot();
2977                    log_restart_only_changes(&restart_only, &new_restart_only);
2978                    restart_only = new_restart_only;
2979
2980                    let tick_changed = new_config.tick_interval != config.tick_interval;
2981                    config = new_config;
2982
2983                    // Reset tick interval if it changed
2984                    if tick_changed {
2985                        interval = time::interval(config.tick_interval);
2986                        tracing::info!(tick = ?config.tick_interval, "tick interval updated");
2987                    }
2988
2989                    // Resize the dispatch pool. A shrink only takes the permits that are
2990                    // free right now; the rest are taken as running tasks finish.
2991                    if review_in_force != config.max_parallel {
2992                        review_in_force = resize_dispatch_pool(
2993                            &review_semaphore,
2994                            review_in_force,
2995                            config.max_parallel,
2996                        );
2997                    }
2998                    if parallel_in_force != config.max_parallel {
2999                        parallel_in_force = resize_dispatch_pool(
3000                            &semaphore,
3001                            parallel_in_force,
3002                            config.max_parallel,
3003                        );
3004                        tracing::info!(
3005                            in_force = parallel_in_force,
3006                            target = config.max_parallel,
3007                            "dispatch pool resized"
3008                        );
3009                    }
3010
3011                    // Reload router config (async-safe: use reload_async to avoid blocking the runtime)
3012                    {
3013                        let mut router_guard = router.write().await;
3014                        router_guard.reload_async().await;
3015                    }
3016
3017                    // Pick up projects added to `projects:`
3018                    sync_project_engines(
3019                        &store,
3020                        &mut project_engines,
3021                        &mut active_repos,
3022                        &event_bus,
3023                        &tmux,
3024                        &capture_for_tick,
3025                        &semaphore,
3026                        &review_semaphore,
3027                        &weight_tx,
3028                        &router,
3029                        &dispatching,
3030                    ).await;
3031
3032                    tracing::info!(
3033                        tick = ?config.tick_interval,
3034                        sync = ?config.sync_interval,
3035                        parallel = config.max_parallel,
3036                        projects = project_engines.len(),
3037                        "config reloaded"
3038                    );
3039                }
3040            }
3041            signal_name = async {
3042                tokio::select! {
3043                    _ = signal::ctrl_c() => "SIGINT",
3044                    _ = sigterm.recv() => "SIGTERM",
3045                    _ = sighup.recv() => "SIGHUP",
3046                }
3047            } => {
3048                tracing::info!(signal = signal_name, "beginning graceful shutdown");
3049
3050                // Wrap the entire shutdown sequence in a timeout to enforce the configured limit.
3051                // This prevents indefinite blocking if status updates or tmux operations hang.
3052                let shutdown_result = time::timeout(
3053                    config.graceful_shutdown_timeout,
3054                    async {
3055                        // Reset in_progress tasks to routed so they re-dispatch after restart.
3056                        // Also reset in_review tasks to needs_review — their review agent
3057                        // tmux sessions will be killed when the process exits.
3058                        let mut reset_count = 0u32;
3059                        let mut review_reset_count = 0u32;
3060                        for engine in &project_engines {
3061                            if let Ok(tasks) = engine.task_manager.list_external_by_status(Status::InProgress).await {
3062                                for task in &tasks {
3063                                    if let Err(e) = engine.task_manager.update_task_status(&task.id, Status::Routed).await {
3064                                        tracing::warn!(task_id = task.id.0, ?e, "failed to reset task on shutdown");
3065                                    } else {
3066                                        if let Ok(Some(store_id)) =
3067                                            engine.store.resolve_task_id(&engine.repo, &task.id.0).await
3068                                            && let Err(e) = engine
3069                                                .store
3070                                                .finalize_incomplete_runs(
3071                                                    store_id,
3072                                                    "aborted",
3073                                                    "graceful shutdown: reset in_progress task to routed",
3074                                                )
3075                                                .await
3076                                            {
3077                                                tracing::warn!(
3078                                                    task_id = task.id.0,
3079                                                    ?e,
3080                                                    "failed to finalize incomplete runs on shutdown"
3081                                                );
3082                                            }
3083                                        reset_count += 1;
3084                                    }
3085                                }
3086                            }
3087                            // Also reset internal in_progress tasks
3088                            if let Ok(tasks) = engine.store.list_internal_by_status(&engine.repo, TaskStatus::InProgress).await {
3089                                for task in &tasks {
3090                                    let task_id = format!("internal:{}", task.id);
3091                                    if let Err(e) = engine.task_manager.update_task_status(
3092                                        &ExternalId(task_id.clone()),
3093                                        Status::Routed,
3094                                    ).await {
3095                                        tracing::warn!(task_id, ?e, "failed to reset internal task on shutdown");
3096                                    } else {
3097                                        if let Err(e) = engine
3098                                            .store
3099                                            .finalize_incomplete_runs(
3100                                                task.id,
3101                                                "aborted",
3102                                                "graceful shutdown: reset in_progress task to routed",
3103                                            )
3104                                            .await
3105                                        {
3106                                            tracing::warn!(
3107                                                task_id,
3108                                                ?e,
3109                                                "failed to finalize incomplete internal runs on shutdown"
3110                                            );
3111                                        }
3112                                        reset_count += 1;
3113                                    }
3114                                }
3115                            }
3116                            // Reset in_review tasks — review agent sessions die on shutdown
3117                            if let Ok(tasks) = engine.task_manager.list_external_by_status(Status::InReview).await {
3118                                for task in &tasks {
3119                                    if let Err(e) = engine.task_manager.update_task_status(&task.id, Status::NeedsReview).await {
3120                                        tracing::warn!(task_id = task.id.0, ?e, "failed to reset in_review task on shutdown");
3121                                    } else {
3122                                        if let Ok(Some(store_id)) =
3123                                            engine.store.resolve_task_id(&engine.repo, &task.id.0).await
3124                                            && let Err(e) = engine
3125                                                .store
3126                                                .finalize_incomplete_runs(
3127                                                    store_id,
3128                                                    "aborted",
3129                                                    "graceful shutdown: reset in_review task to needs_review",
3130                                                )
3131                                                .await
3132                                            {
3133                                                tracing::warn!(
3134                                                    task_id = task.id.0,
3135                                                    ?e,
3136                                                    "failed to finalize incomplete review runs on shutdown"
3137                                                );
3138                                            }
3139                                        review_reset_count += 1;
3140                                    }
3141                                }
3142                            }
3143                            // Also reset internal in_review tasks
3144                            if let Ok(tasks) = engine.store.list_internal_by_status(&engine.repo, TaskStatus::InReview).await {
3145                                for task in &tasks {
3146                                    let task_id = format!("internal:{}", task.id);
3147                                    if let Err(e) = engine.task_manager.update_task_status(
3148                                        &ExternalId(task_id.clone()),
3149                                        Status::NeedsReview,
3150                                    ).await {
3151                                        tracing::warn!(task_id, ?e, "failed to reset internal in_review task on shutdown");
3152                                    } else {
3153                                        if let Err(e) = engine
3154                                            .store
3155                                            .finalize_incomplete_runs(
3156                                                task.id,
3157                                                "aborted",
3158                                                "graceful shutdown: reset in_review task to needs_review",
3159                                            )
3160                                            .await
3161                                        {
3162                                            tracing::warn!(
3163                                                task_id,
3164                                                ?e,
3165                                                "failed to finalize incomplete internal review runs on shutdown"
3166                                            );
3167                                        }
3168                                        review_reset_count += 1;
3169                                    }
3170                                }
3171                            }
3172                        }
3173                        if reset_count > 0 {
3174                            tracing::info!(reset_count, "reset in_progress tasks to routed for re-dispatch");
3175                        }
3176                        if review_reset_count > 0 {
3177                            tracing::info!(review_reset_count, "reset in_review tasks to needs_review for re-dispatch");
3178                        }
3179
3180                        // Kill all orch-managed tmux sessions so stale sessions don't
3181                        // block dispatch after restart (session_exists check).
3182                        let mut killed_sessions = Vec::new();
3183                        if let Ok(sessions) = tmux.list_sessions().await {
3184                            for session in &sessions {
3185                                if session.name.starts_with("orch-") {
3186                                    match tmux.kill_session(&session.name).await { Err(e) => {
3187                                        tracing::warn!(session = %session.name, error = %e, "failed to kill tmux session");
3188                                    } _ => {
3189                                        killed_sessions.push(session.name.clone());
3190                                    }}
3191                                }
3192                            }
3193                            if !killed_sessions.is_empty() {
3194                                tracing::info!(killed = killed_sessions.len(), "killing orch tmux sessions on shutdown");
3195                            }
3196                        }
3197
3198                        // Wait for sessions to actually die before exiting.
3199                        // Prevents race where process exits before tmux finishes cleanup.
3200                        if !killed_sessions.is_empty() {
3201                            let still_alive = tmux
3202                                .wait_for_sessions_dead(
3203                                    &killed_sessions,
3204                                    Duration::from_millis(100),
3205                                    Duration::from_secs(5),
3206                                )
3207                                .await;
3208                            if still_alive > 0 {
3209                                tracing::warn!(
3210                                    still_alive,
3211                                    "some tmux sessions did not terminate cleanly - they will be cleaned up on startup"
3212                                );
3213                            }
3214                            tracing::info!(killed = killed_sessions.len() - still_alive, "confirmed tmux sessions terminated");
3215                        }
3216                    },
3217                ).await;
3218
3219                if shutdown_result.is_err() {
3220                    tracing::error!(
3221                        timeout_secs = config.graceful_shutdown_timeout.as_secs(),
3222                        "graceful shutdown timed out, forcing exit"
3223                    );
3224                }
3225
3226                break;
3227            }
3228        }
3229    }
3230
3231    // Clean up event bus port file and service version file
3232    events::cleanup_port_file().await;
3233    local::cleanup().await;
3234    ui::cleanup().await;
3235    events::cleanup_version_file();
3236
3237    // transport and channels drop here at end of scope
3238    let _ = transport;
3239    tracing::info!("orch engine stopped");
3240    Ok(())
3241}
3242
3243#[cfg(test)]
3244mod tests {
3245    use std::env;
3246    use std::process;
3247    use std::slice;
3248    use std::sync::Arc;
3249
3250    use super::*;
3251    use crate::backends::ExternalBackend;
3252    use crate::backends::test_helpers::NoopBackend;
3253    use crate::channels::capture::CaptureService;
3254    use crate::channels::transport::Transport;
3255    use crate::engine::runner::git_ops;
3256    use crate::store::UpsertExternal;
3257    use crate::tmux::SessionGuard;
3258    use crate::tmux::TmuxManager;
3259
3260    async fn in_review_task(store: &TaskStore, number: &str) -> i64 {
3261        let id = store
3262            .upsert_external(&UpsertExternal {
3263                repo: "owner/repo",
3264                ext_id: number,
3265                title: "Under review",
3266                body: "",
3267                author: "",
3268                url: "",
3269                labels: &[],
3270                origin: "github",
3271            })
3272            .await
3273            .unwrap();
3274        store.update_status(id, TaskStatus::InReview).await.unwrap();
3275        id
3276    }
3277
3278    #[tokio::test]
3279    async fn a_review_without_a_session_goes_back_to_needs_review_and_a_live_one_stays() {
3280        let store = Arc::new(TaskStore::open_memory().await.unwrap());
3281        let backend: Arc<dyn ExternalBackend> = Arc::new(NoopBackend);
3282        let task_manager =
3283            TaskManager::with_store(backend, Arc::clone(&store), "owner/repo".to_string());
3284        let tmux = TmuxManager::isolated();
3285        let orphaned = in_review_task(&store, "11").await;
3286        let live = in_review_task(&store, "12").await;
3287        let session = tmux
3288            .create_session("owner/repo", "12-review", "/tmp", "sleep 30", &[])
3289            .await
3290            .unwrap();
3291        let _guard = SessionGuard::new(&tmux, session);
3292
3293        let reset = reset_orphaned_reviews(&task_manager, &store, "owner/repo", &tmux).await;
3294
3295        assert_eq!(reset, 1);
3296        assert_eq!(
3297            store.get(orphaned).await.unwrap().status,
3298            TaskStatus::NeedsReview
3299        );
3300        assert_eq!(store.get(live).await.unwrap().status, TaskStatus::InReview);
3301    }
3302
3303    #[test]
3304    fn growing_max_parallel_lets_more_tasks_run_without_a_restart() {
3305        let semaphore = Arc::new(Semaphore::new(4));
3306        let held: Vec<_> = (0..4)
3307            .map(|_| Arc::clone(&semaphore).try_acquire_owned().unwrap())
3308            .collect();
3309        assert!(
3310            Arc::clone(&semaphore).try_acquire_owned().is_err(),
3311            "pool is full"
3312        );
3313
3314        let in_force = resize_dispatch_pool(&semaphore, 4, 8);
3315        assert_eq!(in_force, 8);
3316
3317        let grown: Vec<_> = (0..4)
3318            .map(|_| Arc::clone(&semaphore).try_acquire_owned().unwrap())
3319            .collect();
3320        assert_eq!(held.len() + grown.len(), 8, "8 tasks run at once");
3321        assert!(
3322            Arc::clone(&semaphore).try_acquire_owned().is_err(),
3323            "pool is full"
3324        );
3325    }
3326
3327    #[test]
3328    fn shrinking_max_parallel_never_cancels_a_running_task() {
3329        let semaphore = Arc::new(Semaphore::new(8));
3330        let mut held: Vec<_> = (0..8)
3331            .map(|_| Arc::clone(&semaphore).try_acquire_owned().unwrap())
3332            .collect();
3333
3334        // Nothing is free, so all 8 tasks keep their permits and keep running.
3335        let mut in_force = resize_dispatch_pool(&semaphore, 8, 4);
3336        assert_eq!(in_force, 8);
3337        assert_eq!(held.len(), 8);
3338
3339        // Three finish. Their permits are taken back instead of handed out again.
3340        held.truncate(5);
3341        in_force = resize_dispatch_pool(&semaphore, in_force, 4);
3342        assert_eq!(in_force, 5);
3343        assert!(Arc::clone(&semaphore).try_acquire_owned().is_err());
3344
3345        // One more finishes and the pool settles at the new limit.
3346        held.truncate(4);
3347        in_force = resize_dispatch_pool(&semaphore, in_force, 4);
3348        assert_eq!(in_force, 4);
3349        assert!(Arc::clone(&semaphore).try_acquire_owned().is_err());
3350
3351        // A slot freed after that is handed out, since 4 is now the real limit.
3352        held.truncate(3);
3353        assert!(Arc::clone(&semaphore).try_acquire_owned().is_ok());
3354    }
3355
3356    #[test]
3357    fn a_project_added_to_the_config_is_picked_up_and_running_ones_are_not_respawned() {
3358        let configured = vec![
3359            ("owner/one".to_string(), PathBuf::from("/srv/one")),
3360            ("owner/two".to_string(), PathBuf::from("/srv/two")),
3361        ];
3362        let active: HashSet<String> = ["owner/one".to_string()].into_iter().collect();
3363
3364        let (added, removed) = diff_configured_projects(&configured, &active);
3365        assert_eq!(
3366            added,
3367            vec![("owner/two".to_string(), PathBuf::from("/srv/two"))],
3368            "only the new project is started, so owner/one keeps its single set of subscribers"
3369        );
3370        assert!(removed.is_empty(), "{removed:?}");
3371    }
3372
3373    #[test]
3374    fn a_project_dropped_from_the_config_is_reported_and_not_restarted() {
3375        let configured = vec![("owner/one".to_string(), PathBuf::from("/srv/one"))];
3376        let active: HashSet<String> = ["owner/one".to_string(), "owner/gone".to_string()]
3377            .into_iter()
3378            .collect();
3379
3380        let (added, removed) = diff_configured_projects(&configured, &active);
3381        assert!(added.is_empty(), "nothing new to start");
3382        assert_eq!(removed, vec!["owner/gone".to_string()]);
3383    }
3384
3385    #[test]
3386    fn an_unchanged_project_list_starts_nothing() {
3387        let configured = vec![("owner/one".to_string(), PathBuf::from("/srv/one"))];
3388        let active: HashSet<String> = ["owner/one".to_string()].into_iter().collect();
3389
3390        assert_eq!(
3391            diff_configured_projects(&configured, &active),
3392            (vec![], vec![])
3393        );
3394    }
3395
3396    #[test]
3397    fn restart_only_keys_are_the_settings_read_once_at_startup() {
3398        // Guards the docs table: every key listed here is documented as needing
3399        // `orch service restart`, and nothing reloadable may be added to it.
3400        assert_eq!(RESTART_ONLY_KEYS, ["channels", "webhook", "relay"]);
3401    }
3402
3403    #[test]
3404    fn a_changed_restart_only_key_is_reported_and_an_unchanged_one_is_not() {
3405        let before = vec![Some("bot_token: a\n".to_string()), None];
3406
3407        assert!(
3408            log_restart_only_changes(&before, &before.clone()).is_empty(),
3409            "a reload that did not touch these keys must stay quiet"
3410        );
3411
3412        // An edited token and a newly added webhook block are both changes
3413        let after = vec![
3414            Some("bot_token: b\n".to_string()),
3415            Some("port: 8080\n".to_string()),
3416        ];
3417        assert_eq!(
3418            log_restart_only_changes(&before, &after),
3419            ["channels", "webhook"]
3420        );
3421
3422        // Only the edited key is reported when the other is untouched
3423        let token_only = vec![Some("bot_token: b\n".to_string()), None];
3424        assert_eq!(log_restart_only_changes(&before, &token_only), ["channels"]);
3425    }
3426
3427    #[test]
3428    fn resize_dispatch_pool_is_a_noop_at_the_current_limit() {
3429        let semaphore = Arc::new(Semaphore::new(4));
3430        assert_eq!(resize_dispatch_pool(&semaphore, 4, 4), 4);
3431        assert_eq!(semaphore.available_permits(), 4);
3432    }
3433
3434    #[test]
3435    fn live_session_stuck_limit_follows_the_run_timeout() {
3436        let config = EngineConfig {
3437            stuck_timeout: 1800,
3438            run_timeouts: RunTimeouts {
3439                base: 1800,
3440                medium: 2700,
3441                complex: 3600,
3442            },
3443            ..EngineConfig::default()
3444        };
3445        assert_eq!(
3446            config.live_session_stuck_limit(Some("simple")),
3447            1800 + RUN_TIMEOUT_GRACE_SECS
3448        );
3449        assert_eq!(
3450            config.live_session_stuck_limit(Some("medium")),
3451            2700 + RUN_TIMEOUT_GRACE_SECS
3452        );
3453        assert_eq!(
3454            config.live_session_stuck_limit(Some("complex")),
3455            3600 + RUN_TIMEOUT_GRACE_SECS
3456        );
3457
3458        let long_stuck = EngineConfig {
3459            stuck_timeout: 7200,
3460            ..config
3461        };
3462        assert_eq!(long_stuck.live_session_stuck_limit(Some("complex")), 7200);
3463    }
3464
3465    #[test]
3466    fn engine_config_defaults() {
3467        let config = EngineConfig::default();
3468        assert_eq!(config.tick_interval, Duration::from_secs(10));
3469        assert_eq!(config.sync_interval, Duration::from_secs(45));
3470        assert_eq!(config.max_parallel, 4);
3471        assert_eq!(config.stuck_timeout, 1800);
3472        assert_eq!(config.no_session_stuck_timeout, 600);
3473        assert!(
3474            config.no_session_stuck_timeout < config.stuck_timeout,
3475            "no_session_stuck_timeout must be shorter than stuck_timeout"
3476        );
3477        assert_eq!(config.graceful_shutdown_timeout, Duration::from_secs(600));
3478        assert_eq!(config.upgrade_check_interval, 3600);
3479    }
3480
3481    #[test]
3482    fn engine_config_from_config_uses_defaults_when_no_config() {
3483        let config = EngineConfig::from_config();
3484        assert_eq!(config.tick_interval, Duration::from_secs(10));
3485        assert_eq!(config.sync_interval, Duration::from_secs(45));
3486        // max_parallel may be overridden by user config; just check it's reasonable
3487        assert!(config.max_parallel >= 1 && config.max_parallel <= 64);
3488        assert_eq!(config.stuck_timeout, 1800);
3489        assert_eq!(config.no_session_stuck_timeout, 600);
3490        assert_eq!(config.graceful_shutdown_timeout, Duration::from_secs(600));
3491        assert_eq!(config.upgrade_check_interval, 3600);
3492    }
3493
3494    #[test]
3495    fn engine_config_no_session_timeout_is_under_15_minutes() {
3496        let config = EngineConfig::default();
3497        assert!(
3498            config.no_session_stuck_timeout <= 900,
3499            "no_session_stuck_timeout ({}) exceeds 15 minutes",
3500            config.no_session_stuck_timeout
3501        );
3502    }
3503
3504    #[test]
3505    fn upgrade_check_runs_immediately_when_never_checked() {
3506        assert!(should_run_upgrade_check(None, 3600));
3507    }
3508
3509    #[test]
3510    fn upgrade_check_waits_for_interval_after_first_run() {
3511        assert!(!should_run_upgrade_check(Some(Instant::now()), 3600));
3512    }
3513
3514    #[test]
3515    fn upgrade_check_can_be_disabled() {
3516        assert!(!should_run_upgrade_check(None, 0));
3517    }
3518
3519    #[test]
3520    fn configured_agents_parses_yaml_block_array() {
3521        // config::get returns serde_norway::to_string for arrays, which is YAML block format
3522        let yaml_str = "- claude\n- codex\n- opencode\n- olm\n";
3523        let parsed: Vec<String> = serde_norway::from_str(yaml_str).unwrap();
3524        assert_eq!(parsed, vec!["claude", "codex", "opencode", "olm"]);
3525    }
3526
3527    #[test]
3528    fn configured_agents_parses_json_inline_array() {
3529        let json_str = r#"["claude","codex","olm"]"#;
3530        let parsed: Vec<String> = serde_json::from_str(json_str).unwrap();
3531        assert_eq!(parsed, vec!["claude", "codex", "olm"]);
3532    }
3533
3534    #[test]
3535    fn configured_agents_fallback_returns_default_agents() {
3536        // When no config is present, configured_agents() returns DEFAULT_AGENTS
3537        let defaults = configured_agents();
3538        assert!(defaults.contains(&"claude".to_string()));
3539        assert!(defaults.contains(&"codex".to_string()));
3540        assert!(defaults.contains(&"opencode".to_string()));
3541    }
3542
3543    /// Integration test that requires a fully working tmux environment with
3544    /// reliable capture-pane timing. Runs locally but not in CI (GitHub Actions
3545    /// has tmux installed but capture-pane timing is unreliable).
3546    ///
3547    /// DO NOT REMOVE #[ignore] — this test was un-ignored in PR #608 which
3548    /// broke CI. The `tmux -V` skip check is insufficient because tmux CAN
3549    /// create sessions on CI runners, but capture-pane output is empty or
3550    /// delayed, causing the assertion to fail after a 10s timeout.
3551    #[tokio::test]
3552    #[ignore = "capture-pane timing is unreliable on CI runners"]
3553    async fn integration_channel_to_tmux_to_capture() {
3554        // Create transport and capture service
3555        let transport = Arc::new(Transport::new());
3556        let capture = Arc::new(CaptureService::new(Arc::clone(&transport)));
3557        let tmux = TmuxManager::new();
3558
3559        let task_id = "testtask1".to_string();
3560        let session_name = format!("orch-test-{task_id}");
3561
3562        // Start a detached tmux session
3563        let _ = tmux
3564            .command()
3565            .args(["new-session", "-d", "-s", &session_name])
3566            .output()
3567            .await
3568            .expect("failed to start tmux session");
3569
3570        // Guard ensures session cleanup even on panic
3571        let _guard = SessionGuard::new(&tmux, session_name.clone());
3572
3573        // Register session with capture service and transport binding
3574        capture
3575            .register_session("owner/repo", &task_id, &session_name)
3576            .await;
3577        transport
3578            .bind(
3579                "owner/repo",
3580                &task_id,
3581                &session_name,
3582                "telegram",
3583                "12345",
3584                None,
3585            )
3586            .await;
3587
3588        // Spawn capture.run() which exits when session is unregistered
3589        let capture_clone = Arc::clone(&capture);
3590        let capture_handle = tokio::spawn(async move { capture_clone.run().await });
3591
3592        // Subscribe to transport output for this task
3593        let mut rx = transport
3594            .subscribe("owner/repo", &task_id)
3595            .await
3596            .expect("no subscription");
3597
3598        // Send a message into the tmux session via tmux send-keys (simulate channel input)
3599        let send = tmux
3600            .command()
3601            .args([
3602                "send-keys",
3603                "-t",
3604                &session_name,
3605                "echo from-channel",
3606                "Enter",
3607            ])
3608            .output()
3609            .await
3610            .expect("failed to send keys");
3611        assert!(send.status.success());
3612
3613        // Wait for an output chunk from capture -> transport
3614        let mut got = false;
3615        for _ in 0..20 {
3616            if let Ok(Ok(c)) = time::timeout(Duration::from_millis(500), rx.recv()).await
3617                && c.content.contains("from-channel")
3618            {
3619                got = true;
3620                break;
3621            }
3622        }
3623
3624        // Clean up: unregister session (tmux kill handled by SessionGuard on drop)
3625        capture.unregister_session("owner/repo", &task_id).await;
3626
3627        // Wait for capture.run to finish
3628        let _ = capture_handle.await;
3629
3630        assert!(got, "did not observe tmux output via capture/transport");
3631    }
3632
3633    // ── read_project_channel_config tests ─────────────────────────────────
3634
3635    #[test]
3636    fn read_project_channel_config_returns_defaults_when_no_file() {
3637        let dir = tempfile::tempdir().expect("tempdir");
3638        let cfg = read_project_channel_config(dir.path());
3639        assert!(cfg.telegram_topic_id.is_none());
3640        assert!(cfg.discord_channel_id.is_none());
3641    }
3642
3643    #[test]
3644    fn read_project_channel_config_parses_telegram_and_discord() {
3645        let dir = tempfile::tempdir().expect("tempdir");
3646        let orch_yml = dir.path().join(".orch.yml");
3647        fs::write(
3648            &orch_yml,
3649            "channels:\n  telegram:\n    topic_id: \"42\"\n  discord:\n    channel_id: \"9999\"\n",
3650        )
3651        .expect("write .orch.yml");
3652        let cfg = read_project_channel_config(dir.path());
3653        assert_eq!(cfg.telegram_topic_id.as_deref(), Some("42"));
3654        assert_eq!(cfg.discord_channel_id.as_deref(), Some("9999"));
3655    }
3656
3657    #[test]
3658    fn read_project_channel_config_handles_partial_channels() {
3659        let dir = tempfile::tempdir().expect("tempdir");
3660        let orch_yml = dir.path().join(".orch.yml");
3661        fs::write(&orch_yml, "channels:\n  telegram:\n    topic_id: \"7\"\n")
3662            .expect("write .orch.yml");
3663        let cfg = read_project_channel_config(dir.path());
3664        assert_eq!(cfg.telegram_topic_id.as_deref(), Some("7"));
3665        assert!(cfg.discord_channel_id.is_none());
3666    }
3667
3668    #[test]
3669    fn read_project_channel_config_handles_malformed_yaml() {
3670        let dir = tempfile::tempdir().expect("tempdir");
3671        let orch_yml = dir.path().join(".orch.yml");
3672        fs::write(&orch_yml, "channels: [\nbad yaml").expect("write .orch.yml");
3673        let cfg = read_project_channel_config(dir.path());
3674        // Should return defaults (not panic) and emit a warn! log with the parse error.
3675        assert!(cfg.telegram_topic_id.is_none());
3676        assert!(cfg.discord_channel_id.is_none());
3677    }
3678
3679    #[test]
3680    #[cfg(unix)]
3681    fn read_project_channel_config_handles_unreadable_file() {
3682        let dir = tempfile::tempdir().expect("tempdir");
3683        // A directory fails to read for every user, root included, where chmod 000 does not
3684        fs::create_dir(dir.path().join(".orch.yml")).expect("create .orch.yml dir");
3685        let cfg = read_project_channel_config(dir.path());
3686        // Should return defaults (not panic) and emit a warn! log with the read error.
3687        assert!(cfg.telegram_topic_id.is_none());
3688        assert!(cfg.discord_channel_id.is_none());
3689    }
3690
3691    fn git(dir: &Path, args: &[&str]) {
3692        let mut cmd = process::Command::new("git");
3693        cmd.current_dir(dir);
3694        if args.first() == Some(&"commit") {
3695            cmd.args(["-c", "commit.gpgsign=false"]);
3696        }
3697        let out = cmd
3698            .args(args)
3699            .output()
3700            .unwrap_or_else(|e| panic!("git {args:?} failed to start: {e}"));
3701        assert!(
3702            out.status.success(),
3703            "git {:?} failed:\nstdout: {}\nstderr: {}",
3704            args,
3705            String::from_utf8_lossy(&out.stdout),
3706            String::from_utf8_lossy(&out.stderr)
3707        );
3708    }
3709
3710    fn restore_orch_home(prev: Option<String>) {
3711        match prev {
3712            // SAFETY: nextest runs each test in its own process, so no other test touches the environment
3713            Some(v) => unsafe { env::set_var("ORCH_HOME", v) },
3714            // SAFETY: nextest runs each test in its own process, so no other test touches the environment
3715            None => unsafe { env::remove_var("ORCH_HOME") },
3716        }
3717    }
3718
3719    struct StartupConflict {
3720        _tmp: tempfile::TempDir,
3721        _orch_home: tempfile::TempDir,
3722        prev_orch_home: Option<String>,
3723        project_dir: PathBuf,
3724        worktree_dir: PathBuf,
3725        branch: String,
3726        store: Arc<TaskStore>,
3727        store_id: i64,
3728        engine: ProjectEngine,
3729    }
3730
3731    impl Drop for StartupConflict {
3732        fn drop(&mut self) {
3733            restore_orch_home(self.prev_orch_home.take());
3734        }
3735    }
3736
3737    async fn startup_conflict_fixture(
3738        retries: i32,
3739        pr_number: Option<i32>,
3740        start_rebase: bool,
3741    ) -> StartupConflict {
3742        let orch_home = tempfile::tempdir().unwrap();
3743        let prev_orch_home = env::var("ORCH_HOME").ok();
3744        // SAFETY: nextest runs each test in its own process, so no other test touches the environment
3745        unsafe { env::set_var("ORCH_HOME", orch_home.path()) };
3746
3747        let tmp = tempfile::tempdir().unwrap();
3748        let origin = tmp.path().join("origin.git");
3749        fs::create_dir_all(&origin).unwrap();
3750        git(&origin, &["init", "--bare", "-b", "main"]);
3751        let project_dir = tmp.path().join("repo");
3752        git(
3753            tmp.path(),
3754            &[
3755                "clone",
3756                origin.to_str().unwrap(),
3757                project_dir.to_str().unwrap(),
3758            ],
3759        );
3760        git(&project_dir, &["config", "user.email", "t@t.com"]);
3761        git(&project_dir, &["config", "user.name", "T"]);
3762        fs::write(project_dir.join("f.txt"), "base\n").unwrap();
3763        git(&project_dir, &["add", "."]);
3764        git(&project_dir, &["commit", "-m", "base"]);
3765        git(&project_dir, &["push", "origin", "HEAD:main"]);
3766        git(&project_dir, &["remote", "set-head", "origin", "main"]);
3767
3768        let branch = "gh-issue-4087-conflict".to_string();
3769        git(&project_dir, &["checkout", "-b", &branch]);
3770        fs::write(project_dir.join("f.txt"), "agent\n").unwrap();
3771        git(&project_dir, &["commit", "-am", "agent"]);
3772        git(&project_dir, &["checkout", "main"]);
3773        fs::write(project_dir.join("f.txt"), "main\n").unwrap();
3774        git(&project_dir, &["commit", "-am", "main"]);
3775        git(&project_dir, &["push", "origin", "HEAD:main"]);
3776
3777        let wt_base = orch_home.path().join("worktrees").join("repo");
3778        fs::create_dir_all(&wt_base).unwrap();
3779        let worktree_dir = wt_base.join(&branch);
3780        git(
3781            &project_dir,
3782            &["worktree", "add", worktree_dir.to_str().unwrap(), &branch],
3783        );
3784        git(&worktree_dir, &["config", "user.email", "t@t.com"]);
3785        git(&worktree_dir, &["config", "user.name", "T"]);
3786        git(&worktree_dir, &["fetch", "origin"]);
3787        if start_rebase {
3788            let rebase = process::Command::new("git")
3789                .args(["rebase", "origin/main"])
3790                .current_dir(&worktree_dir)
3791                .output()
3792                .unwrap();
3793            assert!(!rebase.status.success(), "precondition: rebase conflicts");
3794        }
3795
3796        let store = Arc::new(TaskStore::open_memory().await.unwrap());
3797        let store_id = store
3798            .upsert_external(&UpsertExternal {
3799                repo: "owner/repo",
3800                ext_id: "4087",
3801                title: "Conflict",
3802                body: "",
3803                author: "",
3804                url: "",
3805                labels: &[],
3806                origin: "github",
3807            })
3808            .await
3809            .unwrap();
3810        store
3811            .update_status(store_id, TaskStatus::Routed)
3812            .await
3813            .unwrap();
3814        let mut fields = vec![
3815            ("branch", serde_json::json!(branch)),
3816            (
3817                "worktree",
3818                serde_json::json!(worktree_dir.to_string_lossy().as_ref()),
3819            ),
3820            ("merge_conflict_retries", serde_json::json!(retries)),
3821        ];
3822        if let Some(pr) = pr_number {
3823            fields.push(("pr_number", serde_json::json!(pr)));
3824        }
3825        store.set_fields(store_id, &fields).await.unwrap();
3826
3827        let backend: Arc<dyn ExternalBackend> = Arc::new(NoopBackend);
3828        let engine = ProjectEngine {
3829            repo: "owner/repo".to_string(),
3830            project_dir: project_dir.clone(),
3831            backend: Arc::clone(&backend),
3832            task_manager: Arc::new(TaskManager::with_store(
3833                backend,
3834                Arc::clone(&store),
3835                "owner/repo".to_string(),
3836            )),
3837            runner: Arc::new(
3838                runner::TaskRunner::new("owner/repo".to_string()).with_store(Arc::clone(&store)),
3839            ),
3840            store: Arc::clone(&store),
3841        };
3842
3843        StartupConflict {
3844            _tmp: tmp,
3845            _orch_home: orch_home,
3846            prev_orch_home,
3847            project_dir,
3848            worktree_dir,
3849            branch,
3850            store,
3851            store_id,
3852            engine,
3853        }
3854    }
3855
3856    #[tokio::test]
3857    #[serial_test::serial(orch_home)]
3858    async fn startup_keeps_a_content_conflict_when_the_task_retried_a_merge() {
3859        let fx = startup_conflict_fixture(2, Some(4043), false).await;
3860        reconcile_startup_worktrees(slice::from_ref(&fx.engine))
3861            .await
3862            .unwrap();
3863
3864        assert!(fx.worktree_dir.exists(), "worktree must stay");
3865        assert!(
3866            git_ops::rebase_in_progress(&fx.worktree_dir).await,
3867            "the content conflict must stay for the agent"
3868        );
3869        let task = fx.store.get(fx.store_id).await.unwrap();
3870        assert_eq!(task.status, TaskStatus::Routed);
3871        assert_eq!(task.branch, fx.branch);
3872        assert!(!task.worktree.is_empty());
3873    }
3874
3875    #[tokio::test]
3876    #[serial_test::serial(orch_home)]
3877    async fn startup_keeps_an_in_progress_rebase_when_the_task_has_a_pr() {
3878        let fx = startup_conflict_fixture(0, Some(12), true).await;
3879        reconcile_startup_worktrees(slice::from_ref(&fx.engine))
3880            .await
3881            .unwrap();
3882
3883        assert!(fx.worktree_dir.exists(), "worktree must stay");
3884        assert!(
3885            git_ops::rebase_in_progress(&fx.worktree_dir).await,
3886            "an in-progress rebase for a PR task must stay"
3887        );
3888        let task = fx.store.get(fx.store_id).await.unwrap();
3889        assert_eq!(task.status, TaskStatus::Routed);
3890        assert_eq!(task.branch, fx.branch);
3891    }
3892
3893    #[tokio::test]
3894    #[serial_test::serial(orch_home)]
3895    async fn startup_resets_a_conflict_without_retries_or_a_pr() {
3896        let fx = startup_conflict_fixture(0, None, false).await;
3897        reconcile_startup_worktrees(slice::from_ref(&fx.engine))
3898            .await
3899            .unwrap();
3900
3901        assert!(
3902            !fx.worktree_dir.exists(),
3903            "worktree is removed when there is nothing to keep"
3904        );
3905        let task = fx.store.get(fx.store_id).await.unwrap();
3906        assert_eq!(task.status, TaskStatus::New);
3907    }
3908
3909    #[tokio::test]
3910    #[serial_test::serial(orch_home)]
3911    async fn startup_reset_keeps_branch_when_rescue_cannot_push() {
3912        let fx = startup_conflict_fixture(0, None, false).await;
3913        git(
3914            &fx.project_dir,
3915            &["remote", "set-url", "origin", "/nonexistent/origin.git"],
3916        );
3917        reconcile_startup_worktrees(slice::from_ref(&fx.engine))
3918            .await
3919            .unwrap();
3920
3921        let task = fx.store.get(fx.store_id).await.unwrap();
3922        assert_eq!(task.status, TaskStatus::New);
3923        assert_eq!(
3924            task.branch, fx.branch,
3925            "the task still names the local branch cleanup kept"
3926        );
3927        assert!(
3928            fx.worktree_dir.exists(),
3929            "rescue failure keeps the worktree on disk"
3930        );
3931    }
3932}