magi-code 0.96.1

Repository-aware CLI coding agent for terminal work
Documentation
use std::{
    thread::{self, JoinHandle},
    time::{Duration, Instant},
};

const JOIN_POLL_INTERVAL: Duration = Duration::from_millis(1);

/// Joins `handle` if it finishes within `timeout`; otherwise returns the handle
/// so the caller decides whether to detach, retry, or report it.
pub(crate) fn join_with_timeout<T>(
    handle: JoinHandle<T>,
    timeout: Duration,
) -> Result<thread::Result<T>, JoinHandle<T>> {
    let deadline = Instant::now() + timeout;
    loop {
        if handle.is_finished() {
            return Ok(handle.join());
        }
        let now = Instant::now();
        if now >= deadline {
            return Err(handle);
        }
        thread::sleep((deadline - now).min(JOIN_POLL_INTERVAL));
    }
}