Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

72.1 KB
Raw Blame
//! Background shell tasks — Sprint C #7.
//!
//! Spawns a shell command as a detached subprocess and keeps it tracked
//! in a process-wide registry keyed by a UUID. The browser polls
//! `shell_task_status` (or lists everything via `shell_task_list`) and
//! can `shell_task_kill` to cancel. Stdout/stderr are tail-buffered
//! (last 64 KiB each) so a long-running script's tail is always
//! available even after the browser reconnects.
//!
//! Architectural decision: we deliberately do NOT use a Tokio task per
//! waiter. Each spawned child has one reader task that pumps bytes into
//! the tail buffer + updates exit status when the process ends; client
//! requests resolve from that buffer synchronously. This means the
//! browser tab can close and reopen without losing any output.
//!
//! Ported 1:1 from the bridge daemon —
//! every body is identical; only the outermost layer is wrapped in
//! `#[tauri::command]` for the desktop IPC bridge.

use crate::os_error;
use crate::commands::shell::{captured_full, settle, Captured};
use crate::commands::{bad_request, internal, not_found, CmdResult};
use std::sync::atomic::AtomicBool;
use crate::state::AppState;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::sync::{Arc, Mutex};
use tokio::io::AsyncReadExt;
use uuid::Uuid;

// tokio::process::Command has `creation_flags` as an inherent method on
// Windows (since tokio 1.6) so no `CommandExt` trait import is needed.
#[cfg(target_os = "windows")]
const CREATE_NO_WINDOW: u32 = 0x08000000;

const TAIL_BYTES: usize = 64 * 1024;

/// How often the reader task checks whether the child has exited.
///
/// It polls instead of awaiting `child.wait()` because the reap has to happen
/// under the task lock, together with clearing the pid — see the loop in
/// `shell_task_start_impl`. These tasks are builds and installs; 50 ms of
/// latency on noticing the exit of a multi-minute command is not observable,
/// and it buys an ordering the shutdown sweep can rely on.
const REAP_POLL: std::time::Duration = std::time::Duration::from_millis(50);

#[derive(Clone, Debug, Serialize)]
pub struct BgTaskStatus {
    pub id: String,
    pub command: String,
    pub cwd: Option<String>,
    pub started_at: u64,
    pub finished_at: Option<u64>,
    pub exit_code: Option<i32>,
    pub running: bool,
    pub cancelled: bool,
    /// Tail of combined stdout+stderr (last ~64 KiB).
    pub output_tail: String,
}

struct BgTaskInner {
    status: BgTaskStatus,
    /// stdout + stderr appended together — order-preserving for the user.
    output_buf: Vec<u8>,
    /// Send `()` to ask the reader task to terminate the child.
    cancel_tx: Option<tokio::sync::oneshot::Sender<()>>,
    /// The shell's pid, kept out of the serialized status. The reader task owns
    /// the `Child`, so this is the only handle the shutdown sweep below has on
    /// a task it must kill.
    pid: Option<u32>,
    /// Set for a task that began life as a foreground `shell_execute` (T1).
    /// Its pipes are still read by `shell::drain`, into these buffers, so the
    /// output comes from here and `output_buf` stays empty.
    adopted: Option<AdoptedOutput>,
    /// A finished foreground run kept only so the model can read the output
    /// its tool result cut (`keep_finished_output`). Capped separately, see
    /// `KEPT_OUTPUTS_MAX`.
    kept_output: bool,
}

/// The two capture buffers of a foreground run, stdout and stderr.
#[derive(Clone)]
struct AdoptedOutput {
    out: Arc<Mutex<Captured>>,
    err: Arc<Mutex<Captured>>,
}

/// What `shell_execute` hands over when its timeout hits: the buffers its
/// drain threads fill, and the flags those threads set at EOF.
pub(crate) struct AdoptedPipes {
    pub out_buf: Arc<Mutex<Captured>>,
    pub err_buf: Arc<Mutex<Captured>>,
    pub out_done: Arc<AtomicBool>,
    pub err_done: Arc<AtomicBool>,
}

/// How many finished foreground outputs the registry keeps for reading. Each
/// holds up to 2 x (256 + 16) KiB; the 200-task cap alone would have allowed
/// a hundred megabytes of old build logs.
const KEPT_OUTPUTS_MAX: usize = 20;

impl BgTaskInner {
    /// The task's output as the status shows it.
    fn render_output(&self) -> String {
        match &self.adopted {
            Some(a) => render_adopted(a),
            None => render_tail(&self.output_buf),
        }
    }
}

/// stdout first, then stderr under a marker. Two pipes read by two threads
/// have no common order to restore, so this does not pretend to one.
fn render_adopted(a: &AdoptedOutput) -> String {
    let out = captured_full(&a.out);
    let err = captured_full(&a.err);
    if err.trim().is_empty() {
        return out;
    }
    if out.trim().is_empty() {
        return format!("[stderr]\n{err}");
    }
    format!("{out}\n[stderr]\n{err}")
}

#[derive(Clone)]
struct BgTask {
    inner: Arc<Mutex<BgTaskInner>>,
}

#[derive(Default)]
struct BgRegistry {
    tasks: Mutex<Vec<BgTask>>,
}

impl BgRegistry {
    fn insert(&self, t: BgTask) {
        let mut g = self.tasks.lock().unwrap();
        // Cap the registry at 200 tasks — drop the oldest *finished* one
        // when we hit the cap so live tasks never get evicted by accident.
        // Kept foreground outputs have their own, smaller cap: drop the
        // oldest one before a new one comes in.
        if t.inner.lock().unwrap().kept_output {
            let kept: Vec<usize> = g
                .iter()
                .enumerate()
                .filter(|(_, t)| t.inner.lock().unwrap().kept_output)
                .map(|(i, _)| i)
                .collect();
            if kept.len() >= KEPT_OUTPUTS_MAX {
                g.remove(kept[0]);
            }
        }
        if g.len() >= 200 {
            if let Some(idx) = g
                .iter()
                .position(|t| !t.inner.lock().unwrap().status.running)
            {
                g.remove(idx);
            }
        }
        g.push(t);
    }
    fn get(&self, id: &str) -> Option<BgTask> {
        self.tasks
            .lock()
            .unwrap()
            .iter()
            .find(|t| t.inner.lock().unwrap().status.id == id)
            .cloned()
    }
    fn list(&self) -> Vec<BgTaskStatus> {
        self.tasks
            .lock()
            .unwrap()
            .iter()
            .map(|t| t.inner.lock().unwrap().status.clone())
            .collect()
    }
}

static REGISTRY: Lazy<BgRegistry> = Lazy::new(BgRegistry::default);

/// Test isolation for the process-wide registry.
///
/// `kill_all_background_tasks` kills every RUNNING task in the registry, and
/// the registry is one static shared by every test in this binary — so a test
/// that sweeps and a test that is standing up a live task must not overlap, or
/// the second one loses its child to the first one's quit. Both kinds hold this
/// lock.
///
/// It is an ASYNC mutex, and that is not cosmetic. Every holder is a
/// `#[tokio::test]` that awaits while holding it — spawning a task, waiting for
/// its children, sleeping through a grace period — so a `std::sync::Mutex`
/// guard would live across those awaits. `clippy::await_holding_lock` flagged
/// exactly that at four of the six call sites (a fifth carried a hand-written
/// `#[allow]`), and the lint is right about the fact even though the deadlock
/// it warns about could not fire here: a `#[tokio::test]` is a current-thread
/// runtime whose only task is the test body, and nothing but these tests ever
/// takes this lock. What the std guard really cost was a parked OS thread per
/// waiting test and a rule that only held as long as nobody spawned a second
/// task. `tokio::sync::Mutex` removes both — waiters yield instead of blocking
/// their runtime — and it removes the lint honestly rather than silencing it.
///
/// Poisoning disappears with it, which is the behaviour that was wanted anyway:
/// the std version deliberately called `into_inner()` on a poisoned lock,
/// because a panicking test has already failed and turning that into a cascade
/// of poisoned-lock failures hides which one. A tokio mutex simply hands the
/// next waiter the lock.
#[cfg(test)]
static SWEEP_ISOLATION: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

#[cfg(test)]
async fn sweep_isolation() -> tokio::sync::MutexGuard<'static, ()> {
    SWEEP_ISOLATION.lock().await
}

#[cfg(test)]
async fn finish_test_task(id: &str) {
    // Eine Abbruchanforderung ist noch kein ausgefuehrter Abbruch.
    // Endet die Testlaufzeit vorher, verwirft Tokio den Empfaenger und wartet
    // unter Windows auf dessen blockierende Pipe-Leser bis zum Prozessende.
    // 30 s, not 5: on a loaded Windows CI runner (release gate 05.11.2026,
    // 245 s for the whole suite) PowerShell and its ping child took longer
    // than 5 s to go, and the test failed although the task did end.
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
    loop {
        let status = shell_task_status_impl(&json!({ "id": id })).await.unwrap();
        if status["running"] == false { return; }
        assert!(std::time::Instant::now() < deadline, "test left a background task running");
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
    }
}

/// Start arguments for a task that keeps a CHILD process of its own alive —
/// the shape every teardown test needs, because a task with a deep tree is the
/// reason this module exists (`pnpm install`, `cargo build`).
///
/// Unix: `sh` running `sleep 30 & echo $! ; wait`, unchanged.
///
/// Windows: no `shell` at all, i.e. the PowerShell this module spawns by
/// default and therefore the shell a real background task actually runs
/// there, with `ping` as the child that outlives the test. Git Bash is still
/// deliberately not asked for — not because it would be mis-invoked any more
/// (the argument form follows the shell name now), but because a Git Bash
/// install is not something a Windows test run may assume.
#[cfg(test)]
fn a_task_that_starts_a_child() -> Value {
    if cfg!(target_os = "windows") {
        // -n 60 pings once a second, so the child stays for about a minute.
        json!({ "command": "ping -n 60 127.0.0.1" })
    } else {
        json!({ "command": "sleep 30 & echo $! ; wait", "shell": "sh" })
    }
}

/// A one-liner that exits at once, in the dialect of the platform's shell.
#[cfg(test)]
fn a_task_that_finishes_at_once() -> Value {
    if cfg!(target_os = "windows") {
        json!({ "command": "exit 0" })
    } else {
        json!({ "command": "true", "shell": "sh" })
    }
}

/// A one-liner that prints the directory the task is running in.
#[cfg(test)]
fn a_task_that_prints_its_directory(extra: Value) -> Value {
    let mut args = if cfg!(target_os = "windows") {
        json!({ "command": "(Get-Location).Path" })
    } else {
        json!({ "command": "pwd", "shell": "sh" })
    };
    let map = args.as_object_mut().expect("an object");
    for (k, v) in extra.as_object().expect("an object") {
        map.insert(k.clone(), v.clone());
    }
    args
}

/// The pid the registry holds for a task — the only handle the shutdown sweep
/// has on it, and the root of the tree the cancel path kills.
#[cfg(test)]
fn shell_pid_of(id: &str) -> u32 {
    REGISTRY
        .get(id)
        .and_then(|t| t.inner.lock().unwrap().pid)
        .expect("a running task must carry its pid")
}

/// Every process the task's shell has started, once there is at least one.
///
/// Read out of the process table rather than out of the task's own output:
/// PowerShell has no `$!`, and Git Bash's `$!` is an MSYS pid that no Windows
/// API can address. The process table is also the view `kill_tree` walks when
/// it takes the tree down, so the test and the code under test are looking at
/// the same processes.
#[cfg(test)]
async fn wait_for_children_of(shell_pid: u32) -> Vec<u32> {
    // 30 s for the same reason as reported_directory: the loop leaves as soon
    // as a child shows up, so a fast machine pays nothing for the margin.
    for _ in 0..300 {
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        let kids = crate::test_support::worker_descendants_of(shell_pid);
        if !kids.is_empty() {
            return kids;
        }
    }
    panic!("the task's shell ({shell_pid}) never started a child process");
}

fn now_secs() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

fn append_tail(buf: &mut Vec<u8>, bytes: &[u8]) {
    buf.extend_from_slice(bytes);
    if buf.len() > TAIL_BYTES {
        let excess = buf.len() - TAIL_BYTES;
        buf.drain(0..excess);
    }
}

fn render_tail(buf: &[u8]) -> String {
    // Lossy UTF-8 is fine — the browser only needs to *read* the tail.
    String::from_utf8_lossy(buf).to_string()
}

#[derive(Deserialize)]
struct StartArgs {
    command: String,
    #[serde(default)]
    cwd: Option<String>,
    #[serde(default)]
    shell: Option<String>,
    // Same chat context the flat `shell_execute` path carries, so a background
    // task defaults to the SAME folder its foreground twin would use.
    #[serde(default)]
    chat_id: Option<String>,
    #[serde(default)]
    working_directory: Option<String>,
}

// ── Internal impls (verbatim from the web app, sans &AppState) ──────────────
// The Tauri-command wrappers below delegate here. Tests bypass the
// State-wrapping layer and call these directly — same as the web app's tests
// did against the originally `pub` fns with a hand-built `AppState`.

pub(crate) async fn shell_task_start_impl(args: &Value) -> CmdResult {
    let a: StartArgs =
        serde_json::from_value(args.clone()).map_err(|e| bad_request(e.to_string()))?;
    if a.command.trim().is_empty() {
        return Err(bad_request("command is empty"));
    }
    // Without an explicit cwd the child used to inherit LU's OWN process
    // directory — the install folder on Windows. The tool exists for
    // `pnpm install` / `cargo build`, so a task the model started "in the
    // project" ran somewhere else entirely. Fall back to the workspace the
    // foreground shell tool resolves.
    let root = crate::commands::filesystem::workspace_root(
        a.chat_id.as_deref(),
        a.working_directory.as_deref(),
    );
    let cwd = match a.cwd.clone().filter(|c| !c.trim().is_empty()) {
        // A relative cwd is relative to the workspace, as the tool says,
        // not to LU's own process folder (C59).
        Some(c) if std::path::Path::new(c.trim()).is_relative() => {
            root.join(c.trim()).to_str().map(|s| s.to_string())
        }
        Some(c) => Some(c),
        None => root
            .to_str()
            .map(|s| s.to_string())
            .filter(|s| std::path::Path::new(s).is_dir()),
    };
    if let Some(cwd) = &cwd {
        let p = std::path::Path::new(cwd);
        if !p.is_dir() {
            return Err(bad_request(format!("cwd does not exist: {}", cwd)));
        }
    }

    let id = Uuid::new_v4().to_string();
    // The argument form follows the SHELL, not the platform. This file used to
    // decide it here, on its own, and always built PowerShell's form on
    // Windows — so a caller-named `cmd` or `bash` was handed
    // `-NoProfile -NonInteractive -Command` and never ran the command at all.
    // The foreground twin already derived it from the shell name; both now ask
    // the same function, so a task started in the background and the same task
    // run in the foreground produce the identical command line.
    let (program, args_vec) = crate::commands::shell::shell_argv(
        cfg!(target_os = "windows"),
        a.shell.as_deref(),
        &a.command,
    );

    // K14 Runde 2, Punkt 5/6: the shell itself (bash/powershell/cmd) is a
    // foreign program exactly like the one `shell.rs`'s foreground twin
    // runs, an AppImage's poisoned LD_LIBRARY_PATH can break it the same
    // way it broke `git`. The COMMAND the user typed stays theirs, only the
    // shell binary's own environment is cleaned.
    let mut cmd = crate::process_util::foreign_system_command_tokio(&program);
    cmd.args(&args_vec);
    // Same as the foreground twin: on a Mac the shell gets the PATH of the
    // user's login shell, not the launchd PATH of a Finder-started app.
    #[cfg(target_os = "macos")]
    cmd.env("PATH", crate::finder_path::child_path());
    if let Some(cwd) = &cwd {
        cmd.current_dir(cwd);
    }
    cmd.stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .stdin(std::process::Stdio::null());
    #[cfg(windows)]
    {
        cmd.creation_flags(CREATE_NO_WINDOW);
    }
    let mut child = cmd
        .spawn()
        .map_err(|e| internal(format!("spawn {}: {}", program, os_error::english(&e))))?;

    // Orphan-safety: tie the child to a kill-on-close Job Object so a true app
    // Quit (tray Quit / parent death) tears these long-running background tasks
    // down too — matching how Ollama/ComfyUI are handled. shutdown_subprocesses
    // (state.rs) only knows the AppState-held PIDs, never this static REGISTRY,
    // so without this a `shell_execute_background` task (e.g. a long build) would
    // be orphaned on Windows after Quit. (v2.5.0 audit fix.)
    #[cfg(target_os = "windows")]
    if let Some(pid) = child.id() {
        crate::commands::process::assign_pid_to_kill_on_close_job(pid);
    }

    let stdout = child.stdout.take();
    let stderr = child.stderr.take();
    let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel::<()>();

    let inner = Arc::new(Mutex::new(BgTaskInner {
        status: BgTaskStatus {
            id: id.clone(),
            command: a.command.clone(),
            cwd: cwd.clone(),
            started_at: now_secs(),
            finished_at: None,
            exit_code: None,
            running: true,
            cancelled: false,
            output_tail: String::new(),
        },
        output_buf: Vec::with_capacity(8 * 1024),
        cancel_tx: Some(cancel_tx),
        pid: child.id(),
        adopted: None,
        kept_output: false,
    }));
    arm_exit_sweep();

    REGISTRY.insert(BgTask {
        inner: Arc::clone(&inner),
    });

    let reader_inner = Arc::clone(&inner);
    tokio::spawn(async move {
        // Pump stdout + stderr into the tail buffer concurrently.
        let inner_so = Arc::clone(&reader_inner);
        let inner_se = Arc::clone(&reader_inner);
        let stdout_task = tokio::spawn(async move {
            if let Some(mut s) = stdout {
                let mut tmp = [0u8; 4096];
                loop {
                    match s.read(&mut tmp).await {
                        Ok(0) | Err(_) => break,
                        Ok(n) => {
                            let mut g = inner_so.lock().unwrap();
                            append_tail(&mut g.output_buf, &tmp[..n]);
                        }
                    }
                }
            }
        });
        let stderr_task = tokio::spawn(async move {
            if let Some(mut s) = stderr {
                let mut tmp = [0u8; 4096];
                loop {
                    match s.read(&mut tmp).await {
                        Ok(0) | Err(_) => break,
                        Ok(n) => {
                            let mut g = inner_se.lock().unwrap();
                            append_tail(&mut g.output_buf, &tmp[..n]);
                        }
                    }
                }
            }
        });

        // REAPING THE CHILD FREES ITS PID, and the shutdown sweep kills by pid.
        // Those two must never be able to interleave.
        //
        // `tokio::select! { child.wait() }` could not give us that: the reap
        // happens inside the await, while `pid` stays in the registry until
        // this task takes the lock again — which is only after both readers
        // have drained, and a grandchild that inherited the pipe can keep them
        // draining for minutes. Everything in that window was a kill aimed at a
        // number the kernel had already handed to somebody else, and this
        // module's tasks are `pnpm install` / `cargo build`, so "somebody else"
        // is whatever the user started next.
        //
        // The reap is therefore a non-blocking `try_wait` performed while
        // HOLDING the task lock, in the same critical section that clears the
        // pid — and the sweep kills while holding that same lock (see
        // `kill_all_background_tasks`). Either the sweep sees a pid that is
        // still a live process, or it sees none. The price is one poll interval
        // of latency on noticing the exit of a task that runs for minutes by
        // design.
        let mut cancel_rx = cancel_rx;
        let (exit_code, cancelled) = loop {
            let cancel_requested = tokio::select! {
                biased;
                _ = &mut cancel_rx => true,
                _ = tokio::time::sleep(REAP_POLL) => false,
            };
            if cancel_requested {
                // Nothing has reaped this child — this task is its only reaper
                // and it is right here — so the pid still means what it meant
                // at spawn. Take it out of the registry BEFORE the kill below
                // reaps it, so the sweep cannot pick it up afterwards.
                let pid = reader_inner.lock().unwrap().pid.take();
                // `child.kill()` sends SIGKILL to the SHELL only. This module
                // exists for `pnpm install` / `cargo build` — the commands with
                // the deepest process trees — so cancelling used to leave the
                // actual worker running detached, exactly the bug 743c310 fixed
                // for the foreground shell tool. The Windows job object does not
                // help here either: it fires when LU dies, not on a cancel.
                if let Some(pid) = pid {
                    // Der Baumabbau wartet auf das Betriebssystem. Dabei muss
                    // der asynchrone Lauf weiter seine Ausgabepipes lesen und
                    // andere Aufgaben bedienen koennen, auf jeder Plattform.
                    let _ = tokio::task::spawn_blocking(move || {
                        crate::commands::shell::kill_tree(pid);
                    }).await;
                }
                let _ = child.kill().await;
                break (None, true);
            }
            let mut g = reader_inner.lock().unwrap();
            match child.try_wait() {
                Ok(Some(status)) => {
                    g.pid = None;
                    break (status.code(), false);
                }
                // Still running: keep the pid, it still addresses this process.
                Ok(None) => {}
                Err(_) => {
                    // We can no longer tell whether it is alive, so the pid is
                    // no longer safe to shoot at.
                    g.pid = None;
                    break (None, false);
                }
            }
        };
        // Wait for readers to drain so the tail is final.
        let _ = stdout_task.await;
        let _ = stderr_task.await;
        let mut g = reader_inner.lock().unwrap();
        g.status.exit_code = exit_code;
        g.status.cancelled = cancelled;
        g.status.running = false;
        g.status.finished_at = Some(now_secs());
        g.status.output_tail = g.render_output();
        g.cancel_tx = None;
        // Already cleared above, in the same critical section as the reap —
        // this only pins the invariant for any path that reaches here without
        // going through the loop. Clearing it HERE was the whole bug: by this
        // line the process has been reaped for as long as the readers took to
        // drain, and the number has belonged to a stranger for just as long.
        debug_assert!(g.pid.is_none(), "a finished task still carries a reaped pid");
        g.pid = None;
    });

    Ok(json!({ "id": id }))
}

#[derive(Deserialize)]
struct IdArgs {
    id: String,
}

pub(crate) async fn shell_task_status_impl(args: &Value) -> CmdResult {
    let a: IdArgs = serde_json::from_value(args.clone()).map_err(|e| bad_request(e.to_string()))?;
    let task = REGISTRY
        .get(&a.id)
        .ok_or_else(|| not_found(format!("task not found: {}", a.id)))?;
    let mut g = task.inner.lock().unwrap();
    // Refresh the tail every time so live tasks aren't stuck at start-time.
    g.status.output_tail = g.render_output();
    Ok(json!(g.status))
}

/// Take over a foreground `shell_execute` whose timeout hit (T1).
///
/// WHY A HANDOVER AND NOT "START EVERYTHING AS A TASK": the foreground path
/// has what the task path lacks and the agent needs: stdin, separate stdout
/// and stderr (the git and test parsers read stdout alone), and the Stop
/// wiring through `shell_execute_cancel`. Starting every command here and
/// waiting on it would have meant rebuilding all three on this side. The
/// handover moves the live `Child` and the buffers its drain threads already
/// fill, so nothing about the process changes and no output is lost or read
/// twice.
///
/// The child is a `std::process::Child`, not a tokio one, so the reaper is a
/// plain thread. It keeps the same rule the async reaper above keeps: the
/// reap (`try_wait`) and clearing the pid happen in ONE critical section of
/// the task lock, and `kill_all_background_tasks` kills while holding that
/// same lock, so the exit sweep never shoots at a number the kernel has
/// already handed to somebody else.
pub(crate) fn adopt_foreground(
    mut child: std::process::Child,
    command: &str,
    cwd: Option<String>,
    pipes: AdoptedPipes,
) -> String {
    let id = Uuid::new_v4().to_string();
    let (cancel_tx, mut cancel_rx) = tokio::sync::oneshot::channel::<()>();
    let adopted = AdoptedOutput { out: pipes.out_buf, err: pipes.err_buf };
    let inner = Arc::new(Mutex::new(BgTaskInner {
        status: BgTaskStatus {
            id: id.clone(),
            command: command.to_string(),
            cwd,
            started_at: now_secs(),
            finished_at: None,
            exit_code: None,
            running: true,
            cancelled: false,
            output_tail: String::new(),
        },
        output_buf: Vec::new(),
        cancel_tx: Some(cancel_tx),
        pid: Some(child.id()),
        adopted: Some(adopted),
        kept_output: false,
    }));
    arm_exit_sweep();
    REGISTRY.insert(BgTask { inner: Arc::clone(&inner) });

    let (out_done, err_done) = (pipes.out_done, pipes.err_done);
    std::thread::spawn(move || {
        let (exit_code, cancelled) = loop {
            // Empty is the only answer that means "keep going". A sent
            // cancel and a dropped sender both stop the task, as the
            // `&mut cancel_rx` arm of the async reaper does.
            let cancel_requested = !matches!(
                cancel_rx.try_recv(),
                Err(tokio::sync::oneshot::error::TryRecvError::Empty)
            );
            if cancel_requested {
                // Out of the registry BEFORE the kill reaps it, as above.
                let pid = inner.lock().unwrap().pid.take();
                if let Some(pid) = pid {
                    crate::commands::shell::kill_tree(pid);
                }
                let _ = child.kill();
                let _ = child.wait();
                break (None, true);
            }
            {
                let mut g = inner.lock().unwrap();
                match child.try_wait() {
                    Ok(Some(status)) => {
                        g.pid = None;
                        break (status.code(), false);
                    }
                    Ok(None) => {}
                    Err(_) => {
                        g.pid = None;
                        break (None, false);
                    }
                }
            }
            std::thread::sleep(REAP_POLL);
        };
        // Never joined: a grandchild can hold the pipes open for as long as
        // it likes. The status reads the live buffers anyway, so a late line
        // still shows up.
        settle(&out_done, &err_done, std::time::Duration::from_millis(500));
        let mut g = inner.lock().unwrap();
        g.status.exit_code = exit_code;
        g.status.cancelled = cancelled;
        g.status.running = false;
        g.status.finished_at = Some(now_secs());
        g.status.output_tail = g.render_output();
        g.cancel_tx = None;
        g.pid = None;
    });
    id
}

/// Keep the output of a FINISHED foreground run readable under a task id, so
/// a tool result that was cut can point at the rest (T1). It is a finished
/// task from the start: nothing to kill, nothing for the sweep.
pub(crate) fn keep_finished_output(
    command: &str,
    cwd: Option<String>,
    exit_code: Option<i32>,
    out: Arc<Mutex<Captured>>,
    err: Arc<Mutex<Captured>>,
) -> String {
    let id = Uuid::new_v4().to_string();
    let now = now_secs();
    let adopted = AdoptedOutput { out, err };
    let output_tail = render_adopted(&adopted);
    let inner = Arc::new(Mutex::new(BgTaskInner {
        status: BgTaskStatus {
            id: id.clone(),
            command: command.to_string(),
            cwd,
            started_at: now,
            finished_at: Some(now),
            exit_code,
            running: false,
            cancelled: false,
            output_tail,
        },
        output_buf: Vec::new(),
        cancel_tx: None,
        pid: None,
        adopted: Some(adopted),
        kept_output: true,
    }));
    REGISTRY.insert(BgTask { inner });
    id
}

pub(crate) async fn shell_task_kill_impl(args: &Value) -> CmdResult {
    let a: IdArgs = serde_json::from_value(args.clone()).map_err(|e| bad_request(e.to_string()))?;
    let task = REGISTRY
        .get(&a.id)
        .ok_or_else(|| not_found(format!("task not found: {}", a.id)))?;
    let tx = {
        let mut g = task.inner.lock().unwrap();
        g.cancel_tx.take()
    };
    if let Some(tx) = tx {
        let _ = tx.send(());
        Ok(json!({ "ok": true, "cancelled": true }))
    } else {
        Ok(json!({ "ok": true, "cancelled": false, "reason": "already finished" }))
    }
}

/// Kill every still-running background task, children included.
///
/// These tasks exist for `pnpm install` / `cargo build`, so they are long-lived
/// by design and their trees are deep. Windows ties each one to a kill-on-close
/// Job Object at spawn, so LU dying takes them along; macOS and Linux have no
/// equivalent, and `AppState::shutdown_subprocesses` only knows the pids IT
/// holds — never this process-wide REGISTRY. A background build therefore
/// outlived every quit, forever, still holding its port and CPU.
// On Windows only the tests reach it — the Job Object gets there first.
#[cfg_attr(target_os = "windows", allow(dead_code))]
pub(crate) fn kill_all_background_tasks() {
    let tasks: Vec<BgTask> = {
        // try_lock, not lock: this also runs from the process-exit hook, where
        // a thread that is holding the registry mutex may never be scheduled
        // again. Losing the sweep beats hanging the quit.
        let mut got = None;
        for _ in 0..20 {
            if let Ok(g) = REGISTRY.tasks.try_lock() {
                got = Some(g.iter().cloned().collect::<Vec<BgTask>>());
                break;
            }
            std::thread::sleep(std::time::Duration::from_millis(10));
        }
        match got {
            Some(t) => t,
            None => return,
        }
    };
    for task in tasks {
        // Kill while HOLDING the task's own lock, rather than collecting pids
        // and firing afterwards. That lock is what makes the pid mean anything:
        // the reader task reaps and clears the pid inside the same critical
        // section, so a pid read here cannot be freed — and handed to a
        // stranger — before the kill goes out. Reading it and releasing first
        // reopened exactly the window this is closing.
        let inner = match lock_briefly(&task) {
            Some(g) => g,
            None => continue,
        };
        if !inner.status.running {
            continue;
        }
        // Read, do not take: the reader task is the one that clears the pid,
        // and it does so in the same breath as the reap. Holding the lock is
        // what makes the number valid here; removing it would only hide the
        // task from a second sweep, which is already harmless.
        if let Some(pid) = inner.pid {
            crate::commands::shell::kill_tree(pid);
        }
    }
}

/// `try_lock` with a short retry — same reasoning as the registry lock above:
/// never block the quit forever, but do not give up on the first contention
/// either (the reader task holds this lock for a `try_wait` at a time).
fn lock_briefly(task: &BgTask) -> Option<std::sync::MutexGuard<'_, BgTaskInner>> {
    for _ in 0..20 {
        if let Ok(g) = task.inner.try_lock() {
            return Some(g);
        }
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
    None
}

/// Run the sweep when the process exits.
///
/// Every quit path ends in `exit()` — the tray Quit, `exit_app`, the updater,
/// Tauri's own `RunEvent::Exit` — and a C `atexit` handler fires on all of
/// them, including the ones that never construct or drop `AppState`. Windows
/// needs none of this: the Job Object from `shell_task_start_impl` already
/// kills the tree when the app's handle closes.
#[cfg(not(target_os = "windows"))]
fn arm_exit_sweep() {
    use std::sync::Once;
    static ARMED: Once = Once::new();
    extern "C" fn sweep() {
        kill_all_background_tasks();
    }
    extern "C" {
        fn atexit(cb: extern "C" fn()) -> std::os::raw::c_int;
    }
    ARMED.call_once(|| unsafe {
        atexit(sweep);
    });
}

#[cfg(target_os = "windows")]
fn arm_exit_sweep() {}

pub(crate) async fn shell_task_list_impl(_args: &Value) -> CmdResult {
    // Newest first reads better in the UI. The registry keeps the tasks in
    // the order they were started, so the listing is that order turned round.
    // It used to be sorted by `started_at`, a wall clock in whole seconds: two
    // tasks started within the same second came out oldest first, and so did
    // any pair the clock stepped back between.
    let mut tasks = REGISTRY.list();
    tasks.reverse();
    Ok(json!({ "tasks": tasks }))
}

// ── Tauri-callable wrappers ───────────────────────────────────────────

#[tauri::command]
pub async fn shell_task_start(
    _state: tauri::State<'_, AppState>,
    args: serde_json::Value,
) -> Result<serde_json::Value, String> {
    shell_task_start_impl(&args).await
}

#[tauri::command]
pub async fn shell_task_status(
    _state: tauri::State<'_, AppState>,
    args: serde_json::Value,
) -> Result<serde_json::Value, String> {
    shell_task_status_impl(&args).await
}

#[tauri::command]
pub async fn shell_task_kill(
    _state: tauri::State<'_, AppState>,
    args: serde_json::Value,
) -> Result<serde_json::Value, String> {
    shell_task_kill_impl(&args).await
}

#[tauri::command]
pub async fn shell_task_list(
    _state: tauri::State<'_, AppState>,
    args: serde_json::Value,
) -> Result<serde_json::Value, String> {
    shell_task_list_impl(&args).await
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    // `echo` works on Linux/macOS; PowerShell's `Write-Output` works on
    // Windows. The spawn path picks the right shell automatically based
    // on `cfg!(target_os = "windows")`.
    // `echo` is a builtin of cmd and an alias in PowerShell, and a local ping
    // sleeps without needing a console, so the same two commands run under
    // whichever shell the platform default resolves to. The PowerShell-only
    // forms used before (Write-Output, Start-Sleep) came back with exit code 1
    // on the Windows CI runner, where the test process has no console (2.6.8
    // release run, 2026-09-06); the tests are about the registry and its
    // polling, not about one shell's console handling.
    fn echo_cmd(msg: &str) -> String {
        format!("echo {}", msg)
    }
    fn sleep_cmd_30s() -> &'static str {
        if cfg!(target_os = "windows") {
            "ping -n 31 127.0.0.1"
        } else {
            "sleep 30"
        }
    }

    #[tokio::test]
    async fn start_runs_a_command_and_status_eventually_reports_finished() {
        let _isolation = super::sweep_isolation().await;
        let r = shell_task_start_impl(&json!({ "command": echo_cmd("hi") }))
            .await
            .unwrap();
        let id = r["id"].as_str().unwrap().to_string();
        // Poll for completion (test envs vary; cap at 5s).
        for _ in 0..50 {
            let s = shell_task_status_impl(&json!({ "id": id })).await.unwrap();
            if !s["running"].as_bool().unwrap_or(true) {
                assert_eq!(s["exit_code"].as_i64(), Some(0));
                assert!(s["output_tail"].as_str().unwrap_or("").contains("hi"));
                return;
            }
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        }
        panic!("command never finished");
    }

    #[tokio::test]
    async fn start_rejects_empty_commands() {
        let err = shell_task_start_impl(&json!({ "command": "" })).await;
        assert!(err.is_err());
    }

    #[tokio::test]
    async fn status_returns_404_for_unknown_id() {
        let err = shell_task_status_impl(&json!({ "id": "nonexistent" })).await;
        assert!(err.is_err());
    }

    #[tokio::test]
    async fn kill_cancels_a_running_task() {
        let _isolation = super::sweep_isolation().await;
        let r = shell_task_start_impl(&json!({ "command": sleep_cmd_30s() }))
            .await
            .unwrap();
        let id = r["id"].as_str().unwrap().to_string();
        // Give the spawn a moment.
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        let _ = shell_task_kill_impl(&json!({ "id": id.clone() })).await.unwrap();
        for _ in 0..50 {
            let s = shell_task_status_impl(&json!({ "id": id.clone() })).await.unwrap();
            if !s["running"].as_bool().unwrap_or(true) {
                assert!(s["cancelled"].as_bool().unwrap_or(false));
                return;
            }
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        }
        panic!("kill did not cancel the task");
    }

    #[tokio::test]
    async fn list_returns_active_tasks_newest_first() {
        let _isolation = super::sweep_isolation().await;
        let r1 = shell_task_start_impl(&json!({ "command": echo_cmd("a") }))
            .await
            .unwrap();
        // No pause between the two: both land in the same second of
        // `started_at`, which is the case the listing used to get wrong.
        let r2 = shell_task_start_impl(&json!({ "command": echo_cmd("b") }))
            .await
            .unwrap();
        let id1 = r1["id"].as_str().unwrap().to_string();
        let id2 = r2["id"].as_str().unwrap().to_string();
        let listing = shell_task_list_impl(&json!({})).await.unwrap();
        let tasks = listing["tasks"].as_array().unwrap();
        let ids: Vec<&str> = tasks
            .iter()
            .map(|t| t["id"].as_str().unwrap())
            .collect();
        let pos1 = ids.iter().position(|s| *s == id1).unwrap();
        let pos2 = ids.iter().position(|s| *s == id2).unwrap();
        assert!(pos2 < pos1, "newer task should appear first");
        super::finish_test_task(&id1).await;
        super::finish_test_task(&id2).await;
    }

    #[cfg(windows)]
    #[tokio::test]
    async fn cancelling_during_shell_startup_drains_the_pipes() {
        let _isolation = super::sweep_isolation().await;
        // Exercise the startup window repeatedly, not just a settled tree.
        // A missed ping child holds stdout open for 30 seconds even after the
        // shell itself is gone, which is what `kill_tree`'s settle window is
        // there to prevent; measured on the box as a 30.4 s stall in two of
        // five rounds before that window existed.
        for _ in 0..3 {
            let started = shell_task_start_impl(&json!({ "command": sleep_cmd_30s() })).await.unwrap();
            let id = started["id"].as_str().unwrap();
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
            shell_task_kill_impl(&json!({ "id": id })).await.unwrap();
            let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
            loop {
                let status = shell_task_status_impl(&json!({ "id": id })).await.unwrap();
                if status["running"] == false {
                    assert_eq!(status["cancelled"], true);
                    break;
                }
                assert!(std::time::Instant::now() < deadline, "cancelled tree kept its output pipes open");
                tokio::time::sleep(std::time::Duration::from_millis(50)).await;
            }
        }
    }
}

#[cfg(test)]
mod cancel_tests {
    use super::*;
    use crate::test_support::is_alive as alive;

    /// A background task is started for the long builds — `pnpm install`,
    /// `cargo build` — so its process tree is deep by definition. Cancelling
    /// used to SIGKILL the shell alone and leave the real worker running
    /// detached, the same defect 743c310 fixed for the foreground shell tool.
    ///
    /// Runs on Windows too: the cancel path is `kill_tree` on every platform
    /// (the Job Object does NOT help here, it fires when LU dies, not on a
    /// cancel), so this is live production behaviour there and not a Unix
    /// mechanism in disguise.
    #[tokio::test]
    async fn cancelling_takes_the_grandchild_with_it() {
        let _isolation = super::sweep_isolation().await;
        let start = shell_task_start_impl(&super::a_task_that_starts_a_child())
            .await
            .expect("start");
        let id = start["id"].as_str().expect("id").to_string();

        let shell_pid = super::shell_pid_of(&id);
        let grandchildren = super::wait_for_children_of(shell_pid).await;
        for pid in &grandchildren {
            assert!(alive(*pid), "grandchild {pid} was not running to begin with");
        }

        shell_task_kill_impl(&json!({ "id": id })).await.expect("kill");
        tokio::time::sleep(std::time::Duration::from_millis(700)).await;

        for pid in &grandchildren {
            assert!(
                !alive(*pid),
                "cancel killed the shell but left the grandchild ({pid}) running",
            );
        }
    }
}

/// Shutdown sweep (audit: bg tasks were only tied to the app's lifetime on
/// Windows). The atexit registration itself cannot be exercised in-process —
/// the handler runs after the test harness is gone — so the sweep it calls is
/// tested directly, with a real tree.
#[cfg(test)]
mod shutdown_sweep_tests {
    use super::*;
    use crate::test_support::is_alive as alive;

    /// Unix only, and not because of the commands it runs: `kill_all_background_tasks`
    /// IS the quit mechanism here — `arm_exit_sweep` hangs it on `atexit` — while on
    /// Windows nothing in production ever calls it. There the same assurance is
    /// delivered by the Job Object, and it has its own test right below.
    #[tokio::test]
    #[cfg(unix)]
    async fn quitting_takes_a_running_task_and_its_children_with_it() {
        let _isolation = super::sweep_isolation().await;
        let start = shell_task_start_impl(&super::a_task_that_starts_a_child())
            .await
            .expect("start");
        let id = start["id"].as_str().expect("id").to_string();

        let shell_pid = super::shell_pid_of(&id);
        let grandchildren = super::wait_for_children_of(shell_pid).await;
        let whole_tree: Vec<u32> = std::iter::once(shell_pid)
            .chain(grandchildren.iter().copied())
            .collect();
        for pid in &whole_tree {
            assert!(alive(*pid), "test setup is wrong: {pid} is not running");
        }

        kill_all_background_tasks();

        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        loop {
            let survivors: Vec<u32> = whole_tree.iter().copied().filter(|p| alive(*p)).collect();
            if survivors.is_empty() {
                break;
            }
            assert!(
                std::time::Instant::now() < deadline,
                "the quit sweep left {survivors:?} running",
            );
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        }
    }

    /// The Windows counterpart: same assurance — nothing a background task
    /// started survives LU's death — through the mechanism that delivers it
    /// there.
    ///
    /// Unix arms an `atexit` sweep. On Windows `arm_exit_sweep` is a no-op and
    /// `kill_all_background_tasks` is never called in production: the shell is
    /// handed to the app-wide Job Object with KILL_ON_JOB_CLOSE at spawn, so
    /// the kernel tears the tree down even for the deaths no handler ever sees
    /// (Task Manager, a hard kill from a build script). Running the Unix test
    /// here would prove something about a code path that does not run on this
    /// platform.
    ///
    /// What cannot be done in-process is watch it happen: that job's handle is
    /// deliberately never closed, and closing it here would disarm the
    /// mechanism for the rest of the test binary — and take every other test's
    /// children with it. What CAN be established is everything the kernel
    /// needs at that moment, and it is exactly what was missing when a
    /// background build outlived the app: the task's shell AND the process it
    /// started are both inside that one job, and that job carries
    /// KILL_ON_JOB_CLOSE. A process created by a process in a job belongs to
    /// the same job, which is why the second half is a real question and not a
    /// tautology — a breakaway flag on the job would end that inheritance.
    #[tokio::test]
    #[cfg(windows)]
    async fn quitting_takes_a_running_task_and_its_children_with_it_via_the_job_object() {
        use windows_sys::Win32::Foundation::CloseHandle;
        use windows_sys::Win32::System::JobObjects::{
            IsProcessInJob, JobObjectExtendedLimitInformation, QueryInformationJobObject,
            JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
        };
        use windows_sys::Win32::System::Threading::{OpenProcess, PROCESS_QUERY_INFORMATION};

        /// Is this pid inside that job? False when the process cannot even be
        /// opened, which for a pid the test itself just spawned means the
        /// process is gone and the question is moot.
        fn in_job(pid: u32, job: isize) -> bool {
            unsafe {
                let handle = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid);
                if handle.is_null() {
                    return false;
                }
                let mut member: i32 = 0;
                let queried = IsProcessInJob(handle, job as _, &mut member);
                CloseHandle(handle);
                queried != 0 && member != 0
            }
        }

        let _isolation = super::sweep_isolation().await;
        let start = shell_task_start_impl(&super::a_task_that_starts_a_child())
            .await
            .expect("start");
        let id = start["id"].as_str().expect("id").to_string();

        let shell_pid = super::shell_pid_of(&id);
        let grandchildren = super::wait_for_children_of(shell_pid).await;
        // Membership of a job the kernel will never have to act on proves
        // nothing, so the tree has to be running when it is asked.
        for pid in std::iter::once(shell_pid).chain(grandchildren.iter().copied()) {
            assert!(alive(pid), "test setup is wrong: {pid} is not running");
        }

        let job = crate::commands::process::kill_on_close_job_for_tests();
        assert!(job != 0, "the app-wide kill-on-close job was never created");

        // The limit flag is the entire mechanism. Without it the job is a
        // bookkeeping device and every child outlives the app.
        let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = unsafe { std::mem::zeroed() };
        let mut returned: u32 = 0;
        let queried = unsafe {
            QueryInformationJobObject(
                job as _,
                JobObjectExtendedLimitInformation,
                &mut info as *mut _ as *mut _,
                std::mem::size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() as u32,
                &mut returned,
            )
        };
        assert!(queried != 0, "the app-wide job object could not be queried");
        assert!(
            info.BasicLimitInformation.LimitFlags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE != 0,
            "the job does not kill its processes when the app's handle closes, \
             so quitting would leave every background task running",
        );

        assert!(
            in_job(shell_pid, job),
            "the task's shell ({shell_pid}) is not in the kill-on-close job, \
             so LU dying would leave the whole background task behind",
        );
        for pid in &grandchildren {
            assert!(
                in_job(*pid, job),
                "the task's shell is in the job but the process it started \
                 ({pid}) is not, so the actual worker would survive LU",
            );
        }

        // Do not leave a minute of ping behind for the rest of the run.
        let _ = shell_task_kill_impl(&json!({ "id": id })).await;
        super::finish_test_task(&id).await;
    }

    /// The window the reaping fix closes.
    ///
    /// `child.wait()` reaps, and from that instant the pid may be reused. The
    /// pid was cleared much later — after both output readers had drained —
    /// and a grandchild that inherited stdout keeps them draining for as long
    /// as it lives. So: shell exits immediately, grandchild holds the pipe, and
    /// for the next three seconds the registry kept handing the sweep a number
    /// that belonged to whatever the kernel gave it to next.
    ///
    /// Unix only, and there is nothing to port: the race it guards is the
    /// reap. Windows recycles a pid only once the LAST handle to the process
    /// object is closed, and `tokio::process::Child` holds one until it is
    /// dropped — which happens after this loop, not inside it — so the number
    /// cannot be handed to a stranger while the registry still names it. The
    /// registry hygiene itself (a finished task carries no pid) is asserted on
    /// both platforms by `a_finished_task_is_not_swept` below.
    #[tokio::test]
    #[cfg(unix)]
    async fn a_reaped_pid_is_gone_from_the_registry_the_instant_it_is_freed() {
        let _isolation = super::sweep_isolation().await;
        let start = shell_task_start_impl(&json!({
            // The shell exits at once; the grandchild inherits stdout and keeps
            // the reader task running long after the shell has been reaped.
            "command": "sleep 3 & exit 0",
            "shell": "sh",
        }))
        .await
        .expect("start");
        let id = start["id"].as_str().expect("id").to_string();

        let task = REGISTRY.get(&id).expect("the task is in the registry");
        let shell_pid = task
            .inner
            .lock()
            .unwrap()
            .pid
            .expect("a running task must carry its pid");

        // Wait for the kernel to release the number — i.e. for the reap. A
        // zombie still holds it, so this asks whether `ps` lists the pid at
        // all, not whether it is alive.
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        while crate::test_support::pid_is_taken(shell_pid) {
            assert!(
                std::time::Instant::now() < deadline,
                "the shell ({shell_pid}) never exited, so there is nothing to prove",
            );
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        }

        // The reader is still draining the grandchild's pipe, so the task is
        // still marked running — and that is exactly when the sweep would fire.
        // Read the state out and release the lock BEFORE asserting: a panic
        // holding this guard poisons the shared registry and every other test
        // in the binary fails on the poison instead of on its own subject.
        let (still_listed, running) = {
            let inner = task.inner.lock().unwrap();
            (inner.pid, inner.status.running)
        };
        assert!(
            still_listed.is_none(),
            "the registry still hands out {shell_pid}, which the kernel has already freed \
             (running={running})",
        );
    }

    /// A finished task's pid may already belong to somebody else — the sweep
    /// must never fire at it. Sweeping an idle registry must also not panic.
    #[tokio::test]
    async fn a_finished_task_is_not_swept() {
        let _isolation = super::sweep_isolation().await;
        let start = shell_task_start_impl(&super::a_task_that_finishes_at_once())
            .await
            .expect("start");
        let id = start["id"].as_str().unwrap().to_string();
        for _ in 0..50 {
            let st = shell_task_status_impl(&json!({ "id": id })).await.unwrap();
            if !st["running"].as_bool().unwrap_or(true) {
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        }
        assert!(
            REGISTRY.get(&id).and_then(|t| t.inner.lock().unwrap().pid).is_none(),
            "a finished task still carries a pid the sweep could kill",
        );
        kill_all_background_tasks();
    }
}

#[cfg(test)]
mod cwd_default_tests {
    use super::*;

    /// What the task printed as its own directory, once it has printed
    /// anything. Five seconds, because a cold PowerShell start on Windows is
    /// not instant.
    async fn reported_directory(id: &str) -> String {
        let mut seen = String::new();
        // 30 s, not 5: on a loaded CI runner the shell can take longer than
        // that to start, and a slow runner is not a failed test.
        for _ in 0..600 {
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
            let st = shell_task_status_impl(&json!({ "id": id })).await.unwrap();
            seen = st["output_tail"].as_str().unwrap_or("").to_string();
            if !seen.trim().is_empty() && st["running"] == false {
                break;
            }
        }
        seen.trim().to_string()
    }

    /// Did the task run in `expected`?
    ///
    /// Both sides go through `canonicalize`, because the two spellings of one
    /// directory differ per platform and neither side is wrong: macOS hands
    /// out `/var/...` which is a symlink to `/private/var` and `pwd` reports
    /// the resolved path, while on Windows the shell prints a plain `C:\...`
    /// and `canonicalize` yields the `\\?\C:\...` verbatim form. Resolving
    /// both is still an exact directory identity — the assertion is not
    /// loosened, it is asked in a form both kernels can answer.
    fn same_directory(reported: &str, expected: &std::path::Path) -> bool {
        match (
            std::fs::canonicalize(reported),
            std::fs::canonicalize(expected),
        ) {
            (Ok(a), Ok(b)) => a == b,
            _ => false,
        }
    }

    /// The tool exists for `pnpm install` / `cargo build`, so the folder it
    /// runs in IS the point. Without an explicit cwd the child used to inherit
    /// LU's own process directory — the install folder on Windows — so a task
    /// the model started "in the project" ran somewhere else.
    #[tokio::test]
    async fn a_task_without_an_explicit_cwd_lands_in_the_workspace() {
        let _isolation = super::sweep_isolation().await;
        // `os_paths::test_dir` statt eines handgebauten `temp_dir().join(…)`:
        // das Aufräumen hängt am `Drop` und läuft auch dann, wenn das
        // `assert!` unten panickt — die letzte Zeile des Tests tat das nicht.
        let ws = crate::os_paths::test_dir("bg-ws");

        let start = shell_task_start_impl(&super::a_task_that_prints_its_directory(json!({
            "working_directory": ws.to_string_lossy(),
        })))
        .await
        .expect("start");
        let id = start["id"].as_str().unwrap().to_string();

        let seen = reported_directory(&id).await;
        assert!(
            same_directory(&seen, &ws),
            "background task did not start in the workspace: reported {seen:?}, wanted {ws:?}",
        );
    }

    /// An explicit cwd from the caller still wins over the derived default.
    #[tokio::test]
    async fn an_explicit_cwd_still_wins() {
        let _isolation = super::sweep_isolation().await;
        // Siehe oben: beide Verzeichnisse räumen sich beim Verlassen selbst
        // ab, auch über die drei `assert!` hinweg.
        let a = crate::os_paths::test_dir("bg-a");
        let b = crate::os_paths::test_dir("bg-b");

        let start = shell_task_start_impl(&super::a_task_that_prints_its_directory(json!({
            "cwd": b.to_string_lossy(),
            "working_directory": a.to_string_lossy(),
        })))
        .await
        .expect("start");
        let id = start["id"].as_str().unwrap().to_string();

        let seen = reported_directory(&id).await;
        assert!(
            same_directory(&seen, &b),
            "the explicit cwd lost: reported {seen:?}, wanted {b:?}",
        );
        // Negative control: the folder that must NOT have won.
        assert!(!same_directory(&seen, &a), "the task ran in the fallback {a:?}");
    }

    /// The tool says cwd is relative to the workspace. A relative one used to
    /// be taken against LU's own process folder (C59).
    #[tokio::test]
    async fn a_relative_cwd_is_inside_the_workspace() {
        let _isolation = super::sweep_isolation().await;
        let ws = crate::os_paths::test_dir("bg-rel");
        std::fs::create_dir_all(ws.join("sub")).unwrap();

        let start = shell_task_start_impl(&super::a_task_that_prints_its_directory(json!({
            "cwd": "sub",
            "working_directory": ws.to_string_lossy(),
        })))
        .await
        .expect("start");
        let id = start["id"].as_str().unwrap().to_string();

        let seen = reported_directory(&id).await;
        assert!(
            same_directory(&seen, &ws.join("sub")),
            "relative cwd not joined onto the workspace: reported {seen:?}",
        );
    }
}

/// The background task and its foreground twin, held against each other.
///
/// They are the same tool with two lifetimes: `shell_execute` waits,
/// `shell_execute` with `background: true` hands back a task id. A caller that
/// names a shell must get that shell, invoked the same way, on either path.
/// They drifted apart once — this file built PowerShell's argument form for
/// every program on Windows while the foreground built it from the shell's
/// name — and the drift was invisible to every non-Windows test run.
#[cfg(test)]
mod foreground_parity_tests {
    use super::*;

    /// A shell that is deliberately NOT the platform default, so the `shell`
    /// field is actually exercised rather than skipped.
    ///
    /// Windows: `cmd`, the exact value that used to be handed
    /// `-NoProfile -NonInteractive -Command` and fail. Unix: `sh`, which is
    /// not the `bash` default and is present on every machine this runs on.
    fn a_named_shell() -> &'static str {
        if cfg!(target_os = "windows") {
            "cmd"
        } else {
            "sh"
        }
    }

    /// One line, one word of output, and the same source text in cmd.exe and
    /// in a POSIX shell — so the assertion is about the invocation, not about
    /// a dialect difference in the command itself.
    const TOKEN: &str = "lu-parity-7f3a";

    fn the_command() -> String {
        format!("echo {TOKEN}")
    }

    /// A directory both runs can start in, so neither path has to fall back to
    /// the per-chat workspace and touch the user's home.
    ///
    /// `os_paths::test_dir` statt eines selbstgebauten `temp_dir().join(…)`:
    /// das Aufräumen hängt dort am `Drop` und läuft deshalb auch dann, wenn
    /// eine der Assertions dazwischen scheitert — die letzte Zeile des Tests
    /// tat das nicht, und genau im Fehlerfall blieb der Ordner liegen. Der
    /// Name trägt zusätzlich die ThreadId, und unter Windows liegt er unter
    /// `target/`, wo ein `cargo clean` ihn erwischt.
    fn a_directory() -> crate::os_paths::TestDir {
        crate::os_paths::test_dir("parity")
    }

    async fn what_the_foreground_printed(dir: &std::path::Path) -> (String, i64) {
        let out = crate::commands::shell::shell_execute(
            the_command(),
            None,
            Some(dir.to_string_lossy().to_string()),
            Some(20_000),
            Some(a_named_shell().to_string()),
            None,
            None,
            None,
            // Keine Abbruchkennung: dieser Vergleich prueft die AUSGABE der
            // beiden Wege, nicht den Stop. Der hat seine eigenen Tests in
            // commands/shell.rs.
            None,
            None,
        )
        .await
        .expect("the foreground shell tool ran");
        (
            out["stdout"].as_str().unwrap_or_default().trim().to_string(),
            out["exitCode"].as_i64().unwrap_or(-1),
        )
    }

    async fn what_the_background_printed(dir: &std::path::Path) -> (String, i64) {
        let start = shell_task_start_impl(&json!({
            "command": the_command(),
            "shell": a_named_shell(),
            "cwd": dir.to_string_lossy(),
        }))
        .await
        .expect("the background task started");
        let id = start["id"].as_str().expect("id").to_string();
        for _ in 0..200 {
            let st = shell_task_status_impl(&json!({ "id": id })).await.expect("status");
            if !st["running"].as_bool().unwrap_or(true) {
                return (
                    st["output_tail"].as_str().unwrap_or_default().trim().to_string(),
                    st["exit_code"].as_i64().unwrap_or(-1),
                );
            }
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        }
        panic!("the background task never finished");
    }

    /// The behavioural half: same shell, same command, same result.
    ///
    /// On Windows this fails outright without the shared argument builder —
    /// `cmd -NoProfile -NonInteractive -Command echo …` prints cmd.exe's usage
    /// complaint and never echoes the token. On Unix it holds `sh` to the same
    /// contract on both paths, which is the property that has to keep holding
    /// for the Windows one to stay true.
    ///
    /// The isolation guard is held across the awaits below, on purpose and like
    /// every other test here that stands up a live task: it is what keeps the
    /// shutdown-sweep tests from killing this task's child mid-run. It used to
    /// carry an `#[allow(clippy::await_holding_lock)]` for that; the lock is an
    /// async mutex now, so there is nothing left to allow.
    #[tokio::test]
    async fn a_named_shell_behaves_the_same_in_the_background_as_in_the_foreground() {
        let _isolation = super::sweep_isolation().await;
        let dir = a_directory();

        let (foreground, fg_code) = what_the_foreground_printed(&dir).await;
        let (background, bg_code) = what_the_background_printed(&dir).await;

        // Both really ran — without this the assertion below would also pass
        // on two empty strings, i.e. on two equally broken paths.
        assert!(
            foreground.contains(TOKEN),
            "the foreground shell tool printed {foreground:?} with shell {:?}",
            a_named_shell(),
        );
        assert!(
            background.contains(TOKEN),
            "the background task printed {background:?} with shell {:?} — \
             the shell it was given was invoked with the wrong argument form",
            a_named_shell(),
        );
        assert_eq!(
            foreground, background,
            "the same command in the same shell came out differently on the two paths",
        );
        assert_eq!((fg_code, bg_code), (0, 0), "one of the two paths failed");
        // Kein `remove_dir_all` mehr: `dir` ist ein `TestDir` und räumt beim
        // Verlassen selbst auf, auch wenn eine Assertion oben vorher panickt.
    }

    /// The structural half: this file must not grow a second copy of the
    /// branch. The behavioural test above can only observe ONE shell per
    /// platform per run; a re-inlined branch that breaks a different shell
    /// would slip past it, and that is exactly how the original bug survived.
    ///
    /// Scoped to this file on purpose: `system.rs` and the `install/` tree also spawn
    /// PowerShell with these flags, but they hardcode PowerShell for scripts of
    /// their own and never take a shell from a caller, so they are not twins of
    /// anything and are none of this test's business.
    #[test]
    fn the_background_path_builds_no_shell_flags_of_its_own() {
        const BG_TASKS_RS: &str = include_str!("bg_tasks.rs");
        // Split out of one string rather than written as separate literals:
        // a list of quoted flags would itself be the quoted flag this test
        // searches for, and the test would trip over its own source.
        for flag in "-NoProfile -NonInteractive -Command /C -c".split(' ') {
            let literal = format!("{:?}", flag);
            assert!(
                !BG_TASKS_RS.contains(&literal),
                "bg_tasks.rs builds the shell argument {flag} itself again — \
                 that branch belongs in shell::shell_argv, which the foreground \
                 path uses too",
            );
        }
        assert!(
            BG_TASKS_RS.contains("shell::shell_argv("),
            "the background path no longer goes through the shared argument builder",
        );
    }
}

/// T1: a foreground `shell_execute` that runs into its timeout moves into the
/// registry instead of dying, and everything that ends a task still ends it.
///
/// Unix only. The handover itself is platform neutral, but the commands and
/// the pid checks here are POSIX (`sh`, `$!`); the Windows path differs only
/// in the Job Object at spawn, which these tests cannot observe from a Mac.
///
/// Lauf: cargo test --lib handover
#[cfg(all(test, unix))]
mod handover_tests {
    use super::*;

    /// One foreground call, in a folder of its own so neither path falls
    /// back to the per-chat workspace in $HOME.
    async fn run(command: &str, timeout_ms: u64, hand_over: bool) -> Value {
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().to_string_lossy().to_string();
        let command = command.to_string();
        let out = tokio::task::spawn_blocking(move || {
            crate::commands::shell::shell_execute_sync(
                command,
                None,
                Some(path.clone()),
                Some(timeout_ms),
                Some("sh".into()),
                None,
                None,
                Some(path),
                None,
                hand_over,
            )
        })
        .await
        .expect("join")
        .expect("the shell ran");
        drop(dir);
        out
    }

    fn alive(pid: u32) -> bool {
        let mut sys = sysinfo::System::new();
        sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true);
        sys.process(sysinfo::Pid::from_u32(pid)).is_some()
    }

    async fn gone_within(pid: u32, secs: u64) {
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(secs);
        while alive(pid) {
            assert!(std::time::Instant::now() < deadline, "pid {pid} survived");
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        }
    }

    /// The pid `sh` printed for its background `sleep`.
    fn first_line_pid(stdout: &str) -> u32 {
        stdout
            .lines()
            .next()
            .and_then(|l| l.trim().parse().ok())
            .expect("a pid on the first line")
    }

    #[tokio::test]
    async fn a_timed_out_command_keeps_running_as_a_task_and_finishes_there() {
        let _isolation = super::sweep_isolation().await;
        let out = run("echo before; sleep 2; echo after", 400, true).await;
        assert_eq!(out["timedOut"], true);
        assert_eq!(out["exitCode"], -1);
        let id = out["taskId"].as_str().expect("a task id").to_string();
        let so_far = out["stdout"].as_str().unwrap();
        assert!(so_far.contains("before"), "{so_far}");
        assert!(!so_far.contains("after"), "{so_far}");

        let st = shell_task_status_impl(&json!({ "id": id })).await.unwrap();
        assert_eq!(st["running"], true, "the process was killed instead of handed over");

        finish_test_task(&id).await;
        let st = shell_task_status_impl(&json!({ "id": id })).await.unwrap();
        assert_eq!(st["exit_code"], 0);
        assert_eq!(st["cancelled"], false);
        let all = st["output_tail"].as_str().unwrap();
        assert!(all.contains("before") && all.contains("after"), "{all}");
    }

    #[tokio::test]
    async fn killing_a_handed_over_task_takes_its_tree_down() {
        let _isolation = super::sweep_isolation().await;
        let out = run("sleep 30 & echo $!; wait", 400, true).await;
        let id = out["taskId"].as_str().expect("a task id").to_string();
        let worker = first_line_pid(out["stdout"].as_str().unwrap());
        assert!(alive(worker), "the worker died with the timeout");

        let r = shell_task_kill_impl(&json!({ "id": id })).await.unwrap();
        assert_eq!(r["cancelled"], true);
        finish_test_task(&id).await;
        let st = shell_task_status_impl(&json!({ "id": id })).await.unwrap();
        assert_eq!(st["cancelled"], true);
        gone_within(worker, 5).await;
    }

    #[tokio::test]
    async fn the_exit_sweep_reaches_a_handed_over_task() {
        let _isolation = super::sweep_isolation().await;
        let out = run("sleep 30 & echo $!; wait", 400, true).await;
        let id = out["taskId"].as_str().expect("a task id").to_string();
        let worker = first_line_pid(out["stdout"].as_str().unwrap());

        kill_all_background_tasks();
        gone_within(worker, 5).await;
        finish_test_task(&id).await;
        assert!(
            REGISTRY.get(&id).and_then(|t| t.inner.lock().unwrap().pid).is_none(),
            "a finished task still carries a pid",
        );
    }

    #[tokio::test]
    async fn without_the_switch_the_timeout_still_kills() {
        let _isolation = super::sweep_isolation().await;
        let out = run("sleep 30 & echo $!; wait", 400, false).await;
        assert_eq!(out["timedOut"], true);
        assert!(out.get("taskId").is_none(), "{out}");
        let worker = first_line_pid(out["stdout"].as_str().unwrap());
        gone_within(worker, 5).await;
    }

    #[tokio::test]
    async fn a_long_finished_output_stays_readable_under_an_id() {
        let out = run(
            "i=0; while [ $i -lt 400 ]; do echo line-$i-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; i=$((i+1)); done; echo END",
            20_000,
            true,
        )
        .await;
        assert_eq!(out["timedOut"], false);
        assert_eq!(out["exitCode"], 0);
        let id = out["outputId"].as_str().expect("an output id").to_string();
        let st = shell_task_status_impl(&json!({ "id": id })).await.unwrap();
        assert_eq!(st["running"], false);
        let all = st["output_tail"].as_str().unwrap();
        assert!(all.contains("line-0-") && all.contains("line-399-"), "{all}");
        assert!(all.trim_end().ends_with("END"), "{all}");
        // Nothing to kill on a kept output.
        let r = shell_task_kill_impl(&json!({ "id": id })).await.unwrap();
        assert_eq!(r["cancelled"], false);

        let short = run("echo short", 20_000, true).await;
        assert!(short.get("outputId").is_none(), "{short}");
    }

    #[test]
    fn kept_outputs_have_their_own_cap() {
        let ids: Vec<String> = (0..KEPT_OUTPUTS_MAX + 5)
            .map(|i| {
                let out = Arc::new(Mutex::new(Captured::default()));
                let err = Arc::new(Mutex::new(Captured::default()));
                keep_finished_output(&format!("echo {i}"), None, Some(0), out, err)
            })
            .collect();
        assert!(REGISTRY.get(&ids[0]).is_none(), "the oldest kept output was not dropped");
        assert!(REGISTRY.get(ids.last().unwrap()).is_some());
        let kept = REGISTRY
            .tasks
            .lock()
            .unwrap()
            .iter()
            .filter(|t| t.inner.lock().unwrap().kept_output)
            .count();
        assert!(kept <= KEPT_OUTPUTS_MAX, "{kept} kept outputs");
    }
}